Skip to content

Commit c8d64a5

Browse files
committed
[PWGHF] Fix ML score handling in Lc to K0s p tree creator
Accept timeframes with zero candidates and zero ML scores in both data and MC, while rejecting mismatched row counts when ML is enabled. Resolve scores by the underlying candidate row so filtering the MC view cannot shift the score association.
1 parent 8a210cb commit c8d64a5

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

‎PWGHF/TableProducer/treeCreatorLcToK0sP.cxx‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <Framework/AnalysisTask.h>
3333
#include <Framework/Configurable.h>
3434
#include <Framework/InitContext.h>
35+
#include <Framework/Logger.h>
3536
#include <Framework/runDataProcessing.h>
3637

3738
#include <algorithm>
@@ -251,6 +252,13 @@ struct HfTreeCreatorLcToK0sP {
251252
{
252253
}
253254

255+
void checkMlScoresSize(int64_t candidateCount, aod::HfMlLcToK0sP const& candidateMlScores)
256+
{
257+
if (applyMl && candidateCount != candidateMlScores.size()) {
258+
LOGP(fatal, "Candidate and ML score tables have different row counts ({} vs {}) while applyMl is enabled. Check applyMl in the Lc to K0s p candidate selector.", candidateCount, candidateMlScores.size());
259+
}
260+
}
261+
254262
/// \brief function to get ML score values for the current candidate and assign them to input parameters
255263
/// \param candidate candidate instance
256264
/// \param candidateMlScore instance of handler of vectors with ML scores associated with the current candidate
@@ -276,14 +284,16 @@ struct HfTreeCreatorLcToK0sP {
276284
}
277285

278286
template <typename T, typename U>
279-
void fillCandidate(const T& candidate, const U& bach, int8_t flagMc, int8_t originMcRec, aod::HfMlLcToK0sP::iterator const& candidateMlScore)
287+
void fillCandidate(const T& candidate, const U& bach, int8_t flagMc, int8_t originMcRec, aod::HfMlLcToK0sP const& candidateMlScores)
280288
{
281289

282290
float mlScoreFirstClass{UndefValueFloat};
283291
float mlScoreSecondClass{UndefValueFloat};
284292
float mlScoreThirdClass{UndefValueFloat};
285293

286294
if (applyMl) {
295+
// The selector writes scores in unfiltered candidate order.
296+
const auto candidateMlScore = candidateMlScores.rawIteratorAt(candidate.mRowIndex);
287297
assignMlScores(candidateMlScore, mlScoreFirstClass, mlScoreSecondClass, mlScoreThirdClass);
288298
}
289299

@@ -422,11 +432,7 @@ struct HfTreeCreatorLcToK0sP {
422432
soa::Join<aod::McParticles, aod::HfCandCascadeMcGen> const& particles,
423433
TracksWPid const&)
424434
{
425-
426-
if (applyMl && candidateMlScores.size() == 0) {
427-
LOG(fatal) << "ML enabled but table with the ML scores is empty! Please check your configurables.";
428-
return;
429-
}
435+
checkMlScoresSize(candidates.tableSize(), candidateMlScores);
430436

431437
// Filling event properties
432438
rowCandidateFullEvents.reserve(collisions.size());
@@ -440,15 +446,12 @@ struct HfTreeCreatorLcToK0sP {
440446
rowCandidateFull.reserve(candidates.size());
441447
}
442448

443-
int iCand{0};
444449
for (const auto& candidate : candidates) {
445-
auto candidateMlScore = candidateMlScores.rawIteratorAt(iCand);
446-
++iCand;
447450
auto bach = candidate.prong0_as<TracksWPid>(); // bachelor
448451
const int flag = candidate.flagMcMatchRec();
449452

450453
if ((fillOnlySignal && flag != 0) || (fillOnlyBackground && flag == 0) || (!fillOnlySignal && !fillOnlyBackground)) {
451-
fillCandidate(candidate, bach, candidate.flagMcMatchRec(), candidate.originMcRec(), candidateMlScore);
454+
fillCandidate(candidate, bach, candidate.flagMcMatchRec(), candidate.originMcRec(), candidateMlScores);
452455
}
453456
}
454457

@@ -474,11 +477,7 @@ struct HfTreeCreatorLcToK0sP {
474477
aod::HfMlLcToK0sP const& candidateMlScores,
475478
TracksWPid const&)
476479
{
477-
478-
if (applyMl && candidateMlScores.size() == 0) {
479-
LOG(fatal) << "ML enabled but table with the ML scores is empty! Please check your configurables.";
480-
return;
481-
}
480+
checkMlScoresSize(candidates.size(), candidateMlScores);
482481

483482
// Filling event properties
484483
rowCandidateFullEvents.reserve(collisions.size());
@@ -493,14 +492,11 @@ struct HfTreeCreatorLcToK0sP {
493492
rowCandidateFull.reserve(candidates.size());
494493
}
495494

496-
int iCand{0};
497495
for (const auto& candidate : candidates) {
498-
auto candidateMlScore = candidateMlScores.rawIteratorAt(iCand);
499-
++iCand;
500496
auto bach = candidate.prong0_as<TracksWPid>(); // bachelor
501497
double const pseudoRndm = bach.pt() * 1000. - static_cast<int16_t>(bach.pt() * 1000);
502498
if (candidate.isSelLcToK0sP() >= 1 && pseudoRndm < downSampleBkgFactor) {
503-
fillCandidate(candidate, bach, 0, 0, candidateMlScore);
499+
fillCandidate(candidate, bach, 0, 0, candidateMlScores);
504500
}
505501
}
506502
}

0 commit comments

Comments
 (0)