From c8d64a5e48ea75f176db9f7bbab4e27420964edd Mon Sep 17 00:00:00 2001 From: BiaoZhang Date: Sun, 29 Nov 2026 20:56:32 +0100 Subject: [PATCH] [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. --- PWGHF/TableProducer/treeCreatorLcToK0sP.cxx | 34 +++++++++------------ 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/PWGHF/TableProducer/treeCreatorLcToK0sP.cxx b/PWGHF/TableProducer/treeCreatorLcToK0sP.cxx index d28e7c5b560..32b755f809f 100644 --- a/PWGHF/TableProducer/treeCreatorLcToK0sP.cxx +++ b/PWGHF/TableProducer/treeCreatorLcToK0sP.cxx @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -251,6 +252,13 @@ struct HfTreeCreatorLcToK0sP { { } + void checkMlScoresSize(int64_t candidateCount, aod::HfMlLcToK0sP const& candidateMlScores) + { + if (applyMl && candidateCount != candidateMlScores.size()) { + 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()); + } + } + /// \brief function to get ML score values for the current candidate and assign them to input parameters /// \param candidate candidate instance /// \param candidateMlScore instance of handler of vectors with ML scores associated with the current candidate @@ -276,7 +284,7 @@ struct HfTreeCreatorLcToK0sP { } template - void fillCandidate(const T& candidate, const U& bach, int8_t flagMc, int8_t originMcRec, aod::HfMlLcToK0sP::iterator const& candidateMlScore) + void fillCandidate(const T& candidate, const U& bach, int8_t flagMc, int8_t originMcRec, aod::HfMlLcToK0sP const& candidateMlScores) { float mlScoreFirstClass{UndefValueFloat}; @@ -284,6 +292,8 @@ struct HfTreeCreatorLcToK0sP { float mlScoreThirdClass{UndefValueFloat}; if (applyMl) { + // The selector writes scores in unfiltered candidate order. + const auto candidateMlScore = candidateMlScores.rawIteratorAt(candidate.mRowIndex); assignMlScores(candidateMlScore, mlScoreFirstClass, mlScoreSecondClass, mlScoreThirdClass); } @@ -422,11 +432,7 @@ struct HfTreeCreatorLcToK0sP { soa::Join const& particles, TracksWPid const&) { - - if (applyMl && candidateMlScores.size() == 0) { - LOG(fatal) << "ML enabled but table with the ML scores is empty! Please check your configurables."; - return; - } + checkMlScoresSize(candidates.tableSize(), candidateMlScores); // Filling event properties rowCandidateFullEvents.reserve(collisions.size()); @@ -440,15 +446,12 @@ struct HfTreeCreatorLcToK0sP { rowCandidateFull.reserve(candidates.size()); } - int iCand{0}; for (const auto& candidate : candidates) { - auto candidateMlScore = candidateMlScores.rawIteratorAt(iCand); - ++iCand; auto bach = candidate.prong0_as(); // bachelor const int flag = candidate.flagMcMatchRec(); if ((fillOnlySignal && flag != 0) || (fillOnlyBackground && flag == 0) || (!fillOnlySignal && !fillOnlyBackground)) { - fillCandidate(candidate, bach, candidate.flagMcMatchRec(), candidate.originMcRec(), candidateMlScore); + fillCandidate(candidate, bach, candidate.flagMcMatchRec(), candidate.originMcRec(), candidateMlScores); } } @@ -474,11 +477,7 @@ struct HfTreeCreatorLcToK0sP { aod::HfMlLcToK0sP const& candidateMlScores, TracksWPid const&) { - - if (applyMl && candidateMlScores.size() == 0) { - LOG(fatal) << "ML enabled but table with the ML scores is empty! Please check your configurables."; - return; - } + checkMlScoresSize(candidates.size(), candidateMlScores); // Filling event properties rowCandidateFullEvents.reserve(collisions.size()); @@ -493,14 +492,11 @@ struct HfTreeCreatorLcToK0sP { rowCandidateFull.reserve(candidates.size()); } - int iCand{0}; for (const auto& candidate : candidates) { - auto candidateMlScore = candidateMlScores.rawIteratorAt(iCand); - ++iCand; auto bach = candidate.prong0_as(); // bachelor double const pseudoRndm = bach.pt() * 1000. - static_cast(bach.pt() * 1000); if (candidate.isSelLcToK0sP() >= 1 && pseudoRndm < downSampleBkgFactor) { - fillCandidate(candidate, bach, 0, 0, candidateMlScore); + fillCandidate(candidate, bach, 0, 0, candidateMlScores); } } }