From 27a775f7d0d87cf7ff18f124b9e41ce9c69ded5e Mon Sep 17 00:00:00 2001 From: mjkim525 Date: Fri, 4 Dec 2026 23:31:21 +0900 Subject: [PATCH 1/2] Compute resonance MC parent rapidity and energy from direct decay daughters --- .../Resonances/resonanceModuleInitializer.cxx | 58 +++++++++++++++---- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/PWGLF/TableProducer/Resonances/resonanceModuleInitializer.cxx b/PWGLF/TableProducer/Resonances/resonanceModuleInitializer.cxx index 57640b2d3fc..04c731dd070 100644 --- a/PWGLF/TableProducer/Resonances/resonanceModuleInitializer.cxx +++ b/PWGLF/TableProducer/Resonances/resonanceModuleInitializer.cxx @@ -21,6 +21,7 @@ #include "PWGLF/Utils/collisionCuts.h" #include "Common/CCDB/RCTSelectionFlags.h" +#include "Common/Core/RecoDecay.h" #include "Common/DataModel/EventSelection.h" #include "Common/DataModel/Multiplicity.h" #include "Common/DataModel/PIDResponseTOF.h" @@ -45,6 +46,7 @@ #include #include +#include #include #include @@ -666,6 +668,8 @@ struct ResonanceModuleInitializer { * * The source MC-particle global index is persisted as a scalar in * ResoMCParents_001; it is not a relation requiring McParticles at merge time. + * Resonance rapidity and energy are taken from the sum of the direct truth + * decay daughters when available; the other parent columns are unchanged. */ template void fillMCParents(int64_t reducedCollisionId, @@ -673,18 +677,52 @@ struct ResonanceModuleInitializer { MCParticlesType const& mcParticles) { for (auto const& mcPart : selectedParents) { - if (!GenCuts.cfgDoSignalLoss) { - const int absPdg = std::abs(mcPart.pdgCode()); - if (absPdg == PdgProton || absPdg == PdgLambda0 || absPdg == PdgXiMinus || absPdg == PdgXi0 || absPdg == PdgOmegaMinus) { - continue; - } + const int absPdg = std::abs(mcPart.pdgCode()); + const bool isSignalLossReference = absPdg == PdgProton || absPdg == PdgLambda0 || + absPdg == PdgXiMinus || absPdg == PdgXi0 || absPdg == PdgOmegaMinus; + if (isSignalLossReference && !GenCuts.cfgDoSignalLoss) { + continue; } std::array daughterPDGs{-1, -1}; + float rapidity = mcPart.y(); + float energy = mcPart.e(); if (mcPart.has_daughters()) { - const auto daughter1 = mcParticles.rawIteratorAt(mcPart.daughtersIds()[0] - mcParticles.offset()); - const auto daughter2 = mcParticles.rawIteratorAt(mcPart.daughtersIds()[1] - mcParticles.offset()); - daughterPDGs = {daughter1.pdgCode(), daughter2.pdgCode()}; + const auto daughterIds = mcPart.daughtersIds(); + const auto first = daughterIds.front() - mcParticles.offset(); + const auto last = daughterIds.back() - mcParticles.offset(); + daughterPDGs = {mcParticles.rawIteratorAt(first).pdgCode(), mcParticles.rawIteratorAt(last).pdgCode()}; + + if (!isSignalLossReference && first < last) { + std::array decayMomentum{}; + double decayEnergy = 0.; + int nDirectDaughters = 0; + for (auto i = first; i <= last; ++i) { + const auto daughter = mcParticles.rawIteratorAt(i); + // The slice can span descendants or unrelated rows. Only sum + // direct decay daughters, not products of material interactions. + const auto motherIds = daughter.mothersIds(); + if (daughter.globalIndex() == mcPart.globalIndex() || + std::find(motherIds.begin(), motherIds.end(), mcPart.globalIndex()) == motherIds.end() || + (!daughter.producedByGenerator() && daughter.getProcess() != TMCProcess::kPDecay)) { + continue; + } + decayMomentum[0] += daughter.px(); + decayMomentum[1] += daughter.py(); + decayMomentum[2] += daughter.pz(); + decayEnergy += daughter.e(); + ++nDirectDaughters; + } + const double massSquared = RecoDecay::m2(decayMomentum, decayEnergy); + if (nDirectDaughters >= 2 && std::isfinite(decayEnergy) && + std::isfinite(massSquared) && massSquared >= 0. && decayEnergy > std::abs(decayMomentum[2])) { + const double decayRapidity = RecoDecay::y(decayMomentum, std::sqrt(massSquared)); + if (std::isfinite(decayRapidity)) { + rapidity = static_cast(decayRapidity); + energy = static_cast(decayEnergy); + } + } + } } reso2mcparents(reducedCollisionId, mcPart.globalIndex(), @@ -697,8 +735,8 @@ struct ResonanceModuleInitializer { mcPart.px(), mcPart.py(), mcPart.pz(), - mcPart.y(), - mcPart.e(), + rapidity, + energy, mcPart.statusCode()); } } From 205ee9aa76e6032f014ff77311553895ea3cd83e Mon Sep 17 00:00:00 2001 From: mjkim525 Date: Sat, 5 Dec 2026 00:47:15 +0900 Subject: [PATCH 2/2] Use direct daughter sums for MC V0 and cascade mother rapidities --- .../Resonances/resonanceModuleInitializer.cxx | 254 +++++++----------- 1 file changed, 98 insertions(+), 156 deletions(-) diff --git a/PWGLF/TableProducer/Resonances/resonanceModuleInitializer.cxx b/PWGLF/TableProducer/Resonances/resonanceModuleInitializer.cxx index 04c731dd070..f4c547b8ffe 100644 --- a/PWGLF/TableProducer/Resonances/resonanceModuleInitializer.cxx +++ b/PWGLF/TableProducer/Resonances/resonanceModuleInitializer.cxx @@ -67,6 +67,82 @@ using namespace o2::constants::physics; using namespace o2::constants::math; using namespace o2::aod::rctsel; +namespace +{ +constexpr int PdgProton = PDG_t::kProton; +constexpr int PdgLambda0 = PDG_t::kLambda0; +constexpr int PdgXiMinus = PDG_t::kXiMinus; +constexpr int PdgXi0 = o2::constants::physics::Pdg::kXi0; +constexpr int PdgOmegaMinus = PDG_t::kOmegaMinus; + +bool isSignalLossReference(int pdg) +{ + const auto absPdg = std::abs(pdg); + return absPdg == PdgProton || absPdg == PdgLambda0 || absPdg == PdgXiMinus || absPdg == PdgXi0 || absPdg == PdgOmegaMinus; +} + +struct ParentKinematics { + float rapidity; + float energy; +}; + +ParentKinematics getParentKinematics(aod::McParticle const& parent) +{ + constexpr int MinDirectDaughters = 2; + const ParentKinematics generated{parent.y(), parent.e()}; + if (isSignalLossReference(parent.pdgCode()) || !parent.has_daughters()) { + return generated; + } + const auto daughterIds = parent.daughtersIds(); + if (daughterIds.front() >= daughterIds.back()) { + return generated; + } + std::array decayMomentum{}; + double decayEnergy = 0.; + int nDirectDaughters = 0; + for (const auto& daughter : parent.daughters_as()) { + // The stored interval can also contain descendants and material secondaries. + const auto motherIds = daughter.mothersIds(); + if (daughter.globalIndex() == parent.globalIndex() || + std::find(motherIds.begin(), motherIds.end(), parent.globalIndex()) == motherIds.end() || + (!daughter.producedByGenerator() && daughter.getProcess() != TMCProcess::kPDecay)) { + continue; + } + decayMomentum[0] += daughter.px(); + decayMomentum[1] += daughter.py(); + decayMomentum[2] += daughter.pz(); + decayEnergy += daughter.e(); + ++nDirectDaughters; + } + const double massSquared = RecoDecay::m2(decayMomentum, decayEnergy); + if (nDirectDaughters >= MinDirectDaughters && std::isfinite(decayEnergy) && std::isfinite(massSquared) && + massSquared >= 0. && decayEnergy > std::abs(decayMomentum[2])) { + const double y = RecoDecay::y(decayMomentum, std::sqrt(massSquared)); + if (std::isfinite(y)) { + return {static_cast(y), static_cast(decayEnergy)}; + } + } + return generated; +} + +struct TruthMotherInfo { + int id = -1; + int pdg = -1; + float pt = -1.f; + float rapidity = -1.f; +}; + +// The label tables store only the first mother, even for multi-parent records. +TruthMotherInfo firstMotherInfo(aod::McParticle const& particle) +{ + if (!particle.has_mothers()) { + return {}; + } + const auto mother = particle.mothers_first_as(); + return {static_cast(mother.globalIndex()), mother.pdgCode(), mother.pt(), getParentKinematics(mother).rapidity}; +} +} // namespace + /** * @brief Initializer for the event pool for resonance study * @@ -115,11 +191,6 @@ struct ResonanceModuleInitializer { static constexpr int Xi1820MinusCode = o2::constants::physics::Pdg::kXi1820Minus; static constexpr int Xi1820ZeroCode = o2::constants::physics::Pdg::kXi1820Zero; static constexpr int Omega2012MinusCode = o2::constants::physics::Pdg::kOmega2012Minus; - static constexpr int PdgProton = PDG_t::kProton; - static constexpr int PdgLambda0 = PDG_t::kLambda0; - static constexpr int PdgXiMinus = PDG_t::kXiMinus; - static constexpr int PdgXi0 = o2::constants::physics::Pdg::kXi0; - static constexpr int PdgOmegaMinus = PDG_t::kOmegaMinus; int mRunNumber = 0; ///< Run number for the current data int multEstimator = CentralityFT0M; ///< Centrality estimator type @@ -677,53 +748,18 @@ struct ResonanceModuleInitializer { MCParticlesType const& mcParticles) { for (auto const& mcPart : selectedParents) { - const int absPdg = std::abs(mcPart.pdgCode()); - const bool isSignalLossReference = absPdg == PdgProton || absPdg == PdgLambda0 || - absPdg == PdgXiMinus || absPdg == PdgXi0 || absPdg == PdgOmegaMinus; - if (isSignalLossReference && !GenCuts.cfgDoSignalLoss) { + if (!GenCuts.cfgDoSignalLoss && isSignalLossReference(mcPart.pdgCode())) { continue; } std::array daughterPDGs{-1, -1}; - float rapidity = mcPart.y(); - float energy = mcPart.e(); if (mcPart.has_daughters()) { const auto daughterIds = mcPart.daughtersIds(); const auto first = daughterIds.front() - mcParticles.offset(); const auto last = daughterIds.back() - mcParticles.offset(); daughterPDGs = {mcParticles.rawIteratorAt(first).pdgCode(), mcParticles.rawIteratorAt(last).pdgCode()}; - - if (!isSignalLossReference && first < last) { - std::array decayMomentum{}; - double decayEnergy = 0.; - int nDirectDaughters = 0; - for (auto i = first; i <= last; ++i) { - const auto daughter = mcParticles.rawIteratorAt(i); - // The slice can span descendants or unrelated rows. Only sum - // direct decay daughters, not products of material interactions. - const auto motherIds = daughter.mothersIds(); - if (daughter.globalIndex() == mcPart.globalIndex() || - std::find(motherIds.begin(), motherIds.end(), mcPart.globalIndex()) == motherIds.end() || - (!daughter.producedByGenerator() && daughter.getProcess() != TMCProcess::kPDecay)) { - continue; - } - decayMomentum[0] += daughter.px(); - decayMomentum[1] += daughter.py(); - decayMomentum[2] += daughter.pz(); - decayEnergy += daughter.e(); - ++nDirectDaughters; - } - const double massSquared = RecoDecay::m2(decayMomentum, decayEnergy); - if (nDirectDaughters >= 2 && std::isfinite(decayEnergy) && - std::isfinite(massSquared) && massSquared >= 0. && decayEnergy > std::abs(decayMomentum[2])) { - const double decayRapidity = RecoDecay::y(decayMomentum, std::sqrt(massSquared)); - if (std::isfinite(decayRapidity)) { - rapidity = static_cast(decayRapidity); - energy = static_cast(decayEnergy); - } - } - } } + const auto kinematics = getParentKinematics(mcParticles.rawIteratorAt(mcPart.globalIndex() - mcParticles.offset())); reso2mcparents(reducedCollisionId, mcPart.globalIndex(), mcPart.pdgCode(), @@ -735,8 +771,8 @@ struct ResonanceModuleInitializer { mcPart.px(), mcPart.py(), mcPart.pz(), - rapidity, - energy, + kinematics.rapidity, + kinematics.energy, mcPart.statusCode()); } } @@ -2573,39 +2609,6 @@ struct ResonanceDaughterInitializer { template void fillMCV0(V0Type const& v0) { - // ------ Temporal lambda function to prevent error in build - auto getMothersIndeces = [&](auto const& theMcParticle) { - std::vector lMothersIndeces{}; - for (auto const& lMother : theMcParticle.template mothers_as()) { - LOGF(debug, " mother index lMother: %d", lMother.globalIndex()); - lMothersIndeces.push_back(lMother.globalIndex()); - } - return lMothersIndeces; - }; - auto getMothersPDGCodes = [&](auto const& theMcParticle) { - std::vector lMothersPDGs{}; - for (auto const& lMother : theMcParticle.template mothers_as()) { - LOGF(debug, " mother pdgcode lMother: %d", lMother.pdgCode()); - lMothersPDGs.push_back(lMother.pdgCode()); - } - return lMothersPDGs; - }; - auto getMothersPt = [&](auto const& theMcParticle) { - std::vector lMothersPts{}; - for (auto const& lMother : theMcParticle.template mothers_as()) { - LOGF(debug, " mother pt lMother: %f", lMother.pt()); - lMothersPts.push_back(lMother.pt()); - } - return lMothersPts; - }; - auto getMothersRap = [&](auto const& theMcParticle) { - std::vector lMothersRaps{}; - for (auto const& lMother : theMcParticle.template mothers_as()) { - LOGF(debug, " mother rap lMother: %f", lMother.y()); - lMothersRaps.push_back(lMother.y()); - } - return lMothersRaps; - }; auto getDaughtersIndeces = [&](auto const& theMcParticle) { std::vector lDaughtersIndeces{}; for (auto const& lDaughter : theMcParticle.template daughters_as()) { @@ -2622,25 +2625,11 @@ struct ResonanceDaughterInitializer { } return lDaughtersPDGs; }; - // ------ - std::vector mothers = {-1, -1}; - std::vector motherPDGs = {-1, -1}; - std::vector mothersPts = {-1.0f, -1.0f}; - std::vector mothersRaps = {-1.0f, -1.0f}; std::vector daughters = {-1, -1}; std::vector daughterPDGs = {-1, -1}; if (v0.has_mcParticle()) { auto v0mc = v0.mcParticle(); - if (v0mc.has_mothers()) { - mothers = getMothersIndeces(v0mc); - motherPDGs = getMothersPDGCodes(v0mc); - mothersPts = getMothersPt(v0mc); - mothersRaps = getMothersRap(v0mc); - } - mothers.resize(StoredMCRelationCount, -1); - motherPDGs.resize(StoredMCRelationCount, -1); - mothersPts.resize(StoredMCRelationCount, -1.0f); - mothersRaps.resize(StoredMCRelationCount, -1.0f); + const auto mother = firstMotherInfo(v0mc); if (v0mc.has_daughters()) { daughters = getDaughtersIndeces(v0mc); daughterPDGs = getDaughtersPDGCodes(v0mc); @@ -2651,10 +2640,10 @@ struct ResonanceDaughterInitializer { daughters.resize(StoredMCRelationCount, -1); daughterPDGs.resize(StoredMCRelationCount, -1); reso2mcv0s(v0mc.pdgCode(), - mothers[0], - motherPDGs[0], - mothersPts[0], - mothersRaps[0], + mother.id, + mother.pdg, + mother.pt, + mother.rapidity, daughters[0], daughters[1], daughterPDGs[0], @@ -2663,10 +2652,10 @@ struct ResonanceDaughterInitializer { v0mc.producedByGenerator()); } else { reso2mcv0s(0, - mothers[0], - motherPDGs[0], - mothersPts[0], - mothersRaps[0], + -1, + -1, + -1.f, + -1.f, daughters[0], daughters[1], daughterPDGs[0], @@ -2757,39 +2746,6 @@ struct ResonanceDaughterInitializer { template void fillMCCascade(CascType const& casc) { - // ------ Temporal lambda function to prevent error in build - auto getMothersIndeces = [&](auto const& theMcParticle) { - std::vector lMothersIndeces{}; - for (auto const& lMother : theMcParticle.template mothers_as()) { - LOGF(debug, " mother index lMother: %d", lMother.globalIndex()); - lMothersIndeces.push_back(lMother.globalIndex()); - } - return lMothersIndeces; - }; - auto getMothersPDGCodes = [&](auto const& theMcParticle) { - std::vector lMothersPDGs{}; - for (auto const& lMother : theMcParticle.template mothers_as()) { - LOGF(debug, " mother pdgcode lMother: %d", lMother.pdgCode()); - lMothersPDGs.push_back(lMother.pdgCode()); - } - return lMothersPDGs; - }; - auto getMothersPt = [&](auto const& theMcParticle) { - std::vector lMothersPts{}; - for (auto const& lMother : theMcParticle.template mothers_as()) { - LOGF(debug, " mother pt lMother: %f", lMother.pt()); - lMothersPts.push_back(lMother.pt()); - } - return lMothersPts; - }; - auto getMothersRap = [&](auto const& theMcParticle) { - std::vector lMothersRaps{}; - for (auto const& lMother : theMcParticle.template mothers_as()) { - LOGF(debug, " mother rap lMother: %f", lMother.y()); - lMothersRaps.push_back(lMother.y()); - } - return lMothersRaps; - }; auto getDaughtersIndeces = [&](auto const& theMcParticle) { std::vector lDaughtersIndeces{}; for (auto const& lDaughter : theMcParticle.template daughters_as()) { @@ -2806,25 +2762,11 @@ struct ResonanceDaughterInitializer { } return lDaughtersPDGs; }; - // ------ - std::vector mothers = {-1, -1}; - std::vector motherPDGs = {-1, -1}; std::vector daughters = {-1, -1}; std::vector daughterPDGs = {-1, -1}; - std::vector mothersPts = {-1.0f, -1.0f}; - std::vector mothersRaps = {-1.0f, -1.0f}; if (casc.has_mcParticle()) { auto cascmc = casc.mcParticle(); - if (cascmc.has_mothers()) { - mothers = getMothersIndeces(cascmc); - motherPDGs = getMothersPDGCodes(cascmc); - mothersPts = getMothersPt(cascmc); - mothersRaps = getMothersRap(cascmc); - } - mothers.resize(StoredMCRelationCount, -1); - motherPDGs.resize(StoredMCRelationCount, -1); - mothersPts.resize(StoredMCRelationCount, -1.0f); - mothersRaps.resize(StoredMCRelationCount, -1.0f); + const auto mother = firstMotherInfo(cascmc); if (cascmc.has_daughters()) { daughters = getDaughtersIndeces(cascmc); daughterPDGs = getDaughtersPDGCodes(cascmc); @@ -2835,10 +2777,10 @@ struct ResonanceDaughterInitializer { daughters.resize(StoredMCRelationCount, -1); daughterPDGs.resize(StoredMCRelationCount, -1); reso2mccascades(cascmc.pdgCode(), - mothers[0], - motherPDGs[0], - mothersPts[0], - mothersRaps[0], + mother.id, + mother.pdg, + mother.pt, + mother.rapidity, daughters[0], daughters[1], daughterPDGs[0], @@ -2847,10 +2789,10 @@ struct ResonanceDaughterInitializer { cascmc.producedByGenerator()); } else { reso2mccascades(0, - mothers[0], - motherPDGs[0], - mothersPts[0], - mothersRaps[0], + -1, + -1, + -1.f, + -1.f, daughters[0], daughters[1], daughterPDGs[0],