diff --git a/PWGLF/Tasks/Nuspex/hadronnucleicorrelation.cxx b/PWGLF/Tasks/Nuspex/hadronnucleicorrelation.cxx index 71d3dcd51ce..7ff39d98b09 100644 --- a/PWGLF/Tasks/Nuspex/hadronnucleicorrelation.cxx +++ b/PWGLF/Tasks/Nuspex/hadronnucleicorrelation.cxx @@ -211,11 +211,8 @@ struct HadronNucleiCorrelation { [[nodiscard]] float pz() const { return ptVal * std::sinh(etaVal); } [[nodiscard]] float p() const { return std::hypot(ptVal, pz()); } [[nodiscard]] float energyForMass(const float mass) const { return std::hypot(p(), mass); } - [[nodiscard]] float rapidityForMass(const float mass) const - { - const float e = energyForMass(mass); - return 0.5f * std::log((e + pz()) / (e - pz())); - }; + static float computeRapidity(float pt, float pz, float mass) { return std::asinh(pz / std::hypot(pt, mass)); } + [[nodiscard]] float rapidityForMass(const float mass) const { return computeRapidity(pt(), pz(), mass); } [[nodiscard]] float mass(const int pdg) const { switch (pdg) { @@ -563,6 +560,20 @@ struct HadronNucleiCorrelation { registry.add("Generated/hAntiDeuteronsVsPt", "hAntiDeuteronsVsPt", {HistType::kTH1D, {ptAxisGen}}); registry.add("Generated/hProtonsVsPt", "hProtonsVsPt", {HistType::kTH1D, {ptAxisGen}}); registry.add("Generated/hAntiProtonsVsPt", "hAntiProtonsVsPt", {HistType::kTH1D, {ptAxisGen}}); + + if (settingsCoalescence.doMCGenCoalescence.value) { + // Original generated particles before coalescence + registry.add("Generated/Coal/hOriginalDeuteronsVsPt", "hOriginalDeuteronsVsPt", {HistType::kTH1D, {ptAxisGen}}); + registry.add("Generated/Coal/hOriginalAntiDeuteronsVsPt", "hOriginalAntiDeuteronsVsPt", {HistType::kTH1D, {ptAxisGen}}); + registry.add("Generated/Coal/hOriginalProtonsVsPt", "hOriginalProtonsVsPt", {HistType::kTH1D, {ptAxisGen}}); + registry.add("Generated/Coal/hOriginalAntiProtonsVsPt", "hOriginalAntiProtonsVsPt", {HistType::kTH1D, {ptAxisGen}}); + + // Generated particles after coalescence + registry.add("Generated/Coal/hCoalescedDeuteronsVsPt", "hCoalescedDeuteronsVsPt", {HistType::kTH1D, {ptAxisGen}}); + registry.add("Generated/Coal/hCoalescedAntiDeuteronsVsPt", "hCoalescedAntiDeuteronsVsPt", {HistType::kTH1D, {ptAxisGen}}); + registry.add("Generated/Coal/hCoalescedProtonsVsPt", "hCoalescedProtonsVsPt", {HistType::kTH1D, {ptAxisGen}}); + registry.add("Generated/Coal/hCoalescedAntiProtonsVsPt", "hCoalescedAntiProtonsVsPt", {HistType::kTH1D, {ptAxisGen}}); + } } else if (settingsCoalescence.doMCGenCoalescence.value) { LOG(fatal) << "This is not a Gen Run but the Gen coalescence is required. Turn off doMCGenCoalescence"; } @@ -809,8 +820,8 @@ struct HadronNucleiCorrelation { // Generated-level candidate selection, shared by same- and mixed-event processing: // same cuts as the pairing (primary if requested, |eta| <= etaCut, PDG code of one of the two species) - template - void addGenCandidate(TParticle const& particle, GenCollisionCache& colCache) + template + void addGenCandidate(Tp const& particle, GenCollisionCache& colCache) { if (isPrim && !particle.isPhysicalPrimary()) { return; @@ -824,35 +835,329 @@ struct HadronNucleiCorrelation { return; } } - const int pdg = particle.pdgCode(); - if (pdg != pdgPart0 && pdg != pdgPart1) { - return; - } - const GenCandidate cand{particle.pt(), particle.eta(), particle.phi()}; - if (pdg == pdgPart0) { - colCache.cand0.push_back(cand); - } - if (pdg == pdgPart1) { - colCache.cand1.push_back(cand); - } + const int pdg = particle.pdgCode(); + if (pdg != pdgPart0 && pdg != pdgPart1) { + return; + } + const GenCandidate cand{particle.pt(), particle.eta(), particle.phi()}; + if (pdg == pdgPart0) { + colCache.cand0.push_back(cand); + } + if (pdg == pdgPart1) { + colCache.cand1.push_back(cand); + } + } + + /// Prepare generated particles for processing, including optional MC coalescence simulation. + template + auto prepareGenParticlesForProcessing(Tps const& mcParticles, bool fillhisto = false) + { + // Local representation of a generated particle. + struct GenCoalescenceCandidate { + GenCoalescenceCandidate(float pt, + float eta, + float phi, + int pdg, + float vx, + float vy, + float vz, + float vt, + bool physicalPrimary) + : mKinematics{.ptVal = pt, .etaVal = eta, .phiVal = phi}, + mPdg{pdg}, + mVx{vx}, + mVy{vy}, + mVz{vz}, + mVt{vt}, + mPhysicalPrimary{physicalPrimary} + { + } + [[nodiscard]] float pt() const { return mKinematics.pt(); } + [[nodiscard]] float eta() const { return mKinematics.eta(); } + [[nodiscard]] float phi() const { return mKinematics.phi(); } + + [[nodiscard]] float px() const { return pt() * std::cos(phi()); } + [[nodiscard]] float py() const { return pt() * std::sin(phi()); } + [[nodiscard]] float pz() const { return mKinematics.pz(); } + + [[nodiscard]] float vx() const { return mVx; } + [[nodiscard]] float vy() const { return mVy; } + [[nodiscard]] float vz() const { return mVz; } + [[nodiscard]] float vt() const { return mVt; } + + [[nodiscard]] float mass() const + { + switch (std::abs(mPdg)) { + case PDG_t::kProton: + return o2::track::PID::getMass(o2::track::PID::Proton); + case PDG_t::kNeutron: + return o2::constants::physics::MassNeutron; + case o2::constants::physics::Pdg::kDeuteron: + return o2::track::PID::getMass(o2::track::PID::Deuteron); + default: + LOG(fatal) << "Unhandled pdg " << mPdg; + return 0.f; + } + } + [[nodiscard]] float energy() const { return std::hypot(mKinematics.p(), mass()); } + [[nodiscard]] float rapidity() const { return mKinematics.rapidityForMass(mass()); } + [[nodiscard]] float y() const { return rapidity(); } + [[nodiscard]] bool isPhysicalPrimary() const { return mPhysicalPrimary; } + [[nodiscard]] int pdgCode() const { return mPdg; } + + private: + GenCandidate mKinematics; + + int mPdg = 0; + float mVx = 0.f; + float mVy = 0.f; + float mVz = 0.f; + float mVt = 0.f; + + bool mPhysicalPrimary = false; + }; + + std::vector particlesToProcess; + particlesToProcess.reserve(mcParticles.size()); + + for (const auto& particle : mcParticles) { + switch (particle.pdgCode()) { + case PDG_t::kProton: + case PDG_t::kProtonBar: + case PDG_t::kNeutron: + case PDG_t::kNeutronBar: + case o2::constants::physics::Pdg::kDeuteron: + case -o2::constants::physics::Pdg::kDeuteron: + particlesToProcess.emplace_back(particle.pt(), + particle.eta(), + particle.phi(), + particle.pdgCode(), + particle.vx(), + particle.vy(), + particle.vz(), + particle.vt(), + particle.isPhysicalPrimary()); + break; + default: + break; + } + } + + if (settingsCoalescence.doMCGenCoalescence.value) { // Simulate coalescence of p+n -> d and pbar+nbar -> dbar + + if (fillhisto) { + for (const auto& particle : particlesToProcess) { // Fill histograms for original generated particles before coalescence + switch (particle.pdgCode()) { + case PDG_t::kProton: + registry.fill(HIST("Generated/Coal/hOriginalProtonsVsPt"), particle.pt()); + break; + case PDG_t::kProtonBar: + registry.fill(HIST("Generated/Coal/hOriginalAntiProtonsVsPt"), particle.pt()); + break; + case o2::constants::physics::Pdg::kDeuteron: + registry.fill(HIST("Generated/Coal/hOriginalDeuteronsVsPt"), particle.pt()); + break; + case -o2::constants::physics::Pdg::kDeuteron: + registry.fill(HIST("Generated/Coal/hOriginalAntiDeuteronsVsPt"), particle.pt()); + break; + default: + break; + } + } + } + + std::vector consumed(particlesToProcess.size(), false); + std::vector deuterons; + + // Try p+n -> d and pbar+nbar -> dbar independently. + for (const int sign : {+1, -1}) { + const int protonPDG = sign * PDG_t::kProton; + const int neutronPDG = sign * PDG_t::kNeutron; + const int deuteronPDG = sign * o2::constants::physics::Pdg::kDeuteron; + + for (size_t ip = 0; ip < particlesToProcess.size(); ++ip) { + if (consumed[ip] || particlesToProcess[ip].pdgCode() != protonPDG) { + continue; + } + + const auto& proton = particlesToProcess[ip]; + + for (size_t in = 0; in < particlesToProcess.size(); ++in) { + if (consumed[in] || particlesToProcess[in].pdgCode() != neutronPDG) { + continue; + } + + const auto& neutron = particlesToProcess[in]; + + const float ep = proton.energy(); + const float en = neutron.energy(); + + // Propagate the particle freezing out first to the freeze-out time of the other particle + float xp = proton.vx(); + float yp = proton.vy(); + float zp = proton.vz(); + const float tp = proton.vt(); + + float xn = neutron.vx(); + float yn = neutron.vy(); + float zn = neutron.vz(); + const float tn = neutron.vt(); + + const float commonTime = std::max(static_cast(tp), static_cast(tn)); + + if (tp < commonTime) { + const float dt = commonTime - tp; + xp += proton.px() / ep * dt; + yp += proton.py() / ep * dt; + zp += proton.pz() / ep * dt; + } + + if (tn < commonTime) { + const float dt = commonTime - tn; + xn += neutron.px() / en * dt; + yn += neutron.py() / en * dt; + zn += neutron.pz() / en * dt; + } + + // Velocity of the p-n centre-of-mass frame. + const float totalE = ep + en; + const float totalPx = proton.px() + neutron.px(); + const float totalPy = proton.py() + neutron.py(); + const float totalPz = proton.pz() + neutron.pz(); + + const float bx = totalPx / totalE; + const float by = totalPy / totalE; + const float bz = totalPz / totalE; + + const float beta2 = bx * bx + by * by + bz * bz; + if (beta2 >= 1.) { + continue; + } + + const float gamma = 1. / std::sqrt(1. - beta2); + + // Boost the proton momentum into the p-n rest frame. + // In that frame p_p* = -p_n*, therefore |p_p*| is the relative momentum entering the coalescence cut. + float pxStar = proton.px(); + float pyStar = proton.py(); + float pzStar = proton.pz(); + + if (beta2 > 0.) { + const float betaDotP = bx * proton.px() + by * proton.py() + bz * proton.pz(); + + const float factor = ((gamma - 1.) * betaDotP / beta2) - gamma * ep; + + pxStar += factor * bx; + pyStar += factor * by; + pzStar += factor * bz; + } + + const float pRelative = std::sqrt(pxStar * pxStar + pyStar * pyStar + pzStar * pzStar); + + if (pRelative >= settingsCoalescence.pMax.value) { + continue; + } + + // The two particles have already been propagated to equal time in the lab. Transform their spatial separation to the pair CM frame. + float dx = xp - xn; + float dy = yp - yn; + float dz = zp - zn; + + if (beta2 > 0.) { + const float betaDotR = bx * dx + by * dy + bz * dz; + const float factor = (gamma - 1.) * betaDotR / beta2; + + dx += factor * bx; + dy += factor * by; + dz += factor * bz; + } + + const float rRelative = std::sqrt(dx * dx + dy * dy + dz * dz); + + if (rRelative >= settingsCoalescence.rMax.value) { + continue; + } + + // Successful coalescence: remove the constituent proton and neutron and replace them by a deuteron carrying their total three-momentum + consumed[ip] = true; + consumed[in] = true; + + const float deuteronPt = std::hypot(totalPx, totalPy); + const float deuteronPhi = std::atan2(totalPy, totalPx); + const float deuteronEta = std::asinh(totalPz / std::max(deuteronPt, 1.e-12f)); + + deuterons.emplace_back(deuteronPt, + deuteronEta, + deuteronPhi, + deuteronPDG, + 0.5f * (xp + xn), + 0.5f * (yp + yn), + 0.5f * (zp + zn), + commonTime, + proton.isPhysicalPrimary() && neutron.isPhysicalPrimary()); + break; + } + } + } + + // Construct the post-coalescence particle list. + std::vector coalescedParticles; + coalescedParticles.reserve(particlesToProcess.size() + deuterons.size()); + + for (size_t i = 0; i < particlesToProcess.size(); ++i) { + if (!consumed[i]) { + coalescedParticles.push_back(std::move(particlesToProcess[i])); + } + } + + for (auto& deuteron : deuterons) { + coalescedParticles.push_back(std::move(deuteron)); + } + + particlesToProcess = std::move(coalescedParticles); + if (fillhisto) { + for (const auto& particle : particlesToProcess) { // Fill histograms after coalescence + switch (particle.pdgCode()) { + case PDG_t::kProton: + registry.fill(HIST("Generated/Coal/hCoalescedProtonsVsPt"), particle.pt()); + break; + case PDG_t::kProtonBar: + registry.fill(HIST("Generated/Coal/hCoalescedAntiProtonsVsPt"), particle.pt()); + break; + case o2::constants::physics::Pdg::kDeuteron: + registry.fill(HIST("Generated/Coal/hCoalescedDeuteronsVsPt"), particle.pt()); + break; + case -o2::constants::physics::Pdg::kDeuteron: + registry.fill(HIST("Generated/Coal/hCoalescedAntiDeuteronsVsPt"), particle.pt()); + break; + default: + break; + } + } + } + } + return particlesToProcess; } // Single pass over the particles of one generated collision: charged-primary multiplicity // (|eta| < 1) for the mixing bins and the pairing candidates - template - GenCollisionCache buildGenCollisionCache(TParticles const& particles) + template + GenCollisionCache buildGenCollisionCache(Tps const& mcParticles) { GenCollisionCache colCache; - for (const auto& mcParticle : particles) { - addGenCandidate(mcParticle, colCache); + const auto particlesToProcess = prepareGenParticlesForProcessing(mcParticles); + for (const auto& particle : particlesToProcess) { + addGenCandidate(particle, colCache); + } - if (!mcParticle.isPhysicalPrimary()) { + for (const auto& particle : mcParticles) { + if (!particle.isPhysicalPrimary()) { continue; } - if (std::abs(mcParticle.eta()) > 1.0f) { + if (std::abs(particle.eta()) > 1.0f) { continue; } - TParticlePDG* p = pdgDB->GetParticle(mcParticle.pdgCode()); + TParticlePDG* p = pdgDB->GetParticle(particle.pdgCode()); if (!p) { // unknown PDG code (e.g. exotic fragments not in the database) continue; } @@ -1734,251 +2039,8 @@ struct HadronNucleiCorrelation { void processSameEventGen(SimCollisions::iterator const&, SimParticles const& mcParticles) { - registry.fill(HIST("Generated/hNEventsMC"), 0.5); - - // Local representation of a generated particle. - struct GenCoalescenceCandidate { - GenCoalescenceCandidate(float pt, - float eta, - float phi, - int pdg, - float vx, - float vy, - float vz, - float vt, - bool physicalPrimary) - : mKinematics{pt, eta, phi}, - mPdg{pdg}, - mVx{vx}, - mVy{vy}, - mVz{vz}, - mVt{vt}, - mPhysicalPrimary{physicalPrimary} - { - } - [[nodiscard]] float pt() const { return mKinematics.pt(); } - [[nodiscard]] float eta() const { return mKinematics.eta(); } - [[nodiscard]] float phi() const { return mKinematics.phi(); } - - [[nodiscard]] float px() const { return pt() * std::cos(phi()); } - [[nodiscard]] float py() const { return pt() * std::sin(phi()); } - [[nodiscard]] float pz() const { return mKinematics.pz(); } - - [[nodiscard]] float vx() const { return mVx; } - [[nodiscard]] float vy() const { return mVy; } - [[nodiscard]] float vz() const { return mVz; } - [[nodiscard]] float vt() const { return mVt; } - - [[nodiscard]] float mass() const - { - switch (std::abs(mPdg)) { - case PDG_t::kProton: - return o2::track::PID::getMass(o2::track::PID::Proton); - case PDG_t::kNeutron: - return o2::constants::physics::MassNeutron; - case o2::constants::physics::Pdg::kDeuteron: - return o2::track::PID::getMass(o2::track::PID::Deuteron); - default: - LOG(fatal) << "Unhandled pdg " << mPdg; - return 0.f; - } - } - [[nodiscard]] float energy() const { return std::hypot(mKinematics.p(), mass()); } - [[nodiscard]] float rapidity() const { return mKinematics.rapidityForMass(mass()); } - [[nodiscard]] float y() const { return rapidity(); } - [[nodiscard]] bool isPhysicalPrimary() const { return mPhysicalPrimary; } - [[nodiscard]] int pdgCode() const { return mPdg; } - - private: - GenCandidate mKinematics; - - int mPdg = 0; - float mVx = 0.f; - float mVy = 0.f; - float mVz = 0.f; - float mVt = 0.f; - - bool mPhysicalPrimary = false; - }; - - std::vector particlesToProcess; - particlesToProcess.reserve(mcParticles.size()); - - for (const auto& particle : mcParticles) { - switch (particle.pdgCode()) { - case PDG_t::kProton: - case PDG_t::kProtonBar: - case PDG_t::kNeutron: - case PDG_t::kNeutronBar: - case o2::constants::physics::Pdg::kDeuteron: - case -o2::constants::physics::Pdg::kDeuteron: - particlesToProcess.emplace_back(particle.pt(), - particle.eta(), - particle.phi(), - particle.pdgCode(), - particle.vx(), - particle.vy(), - particle.vz(), - particle.vt(), - particle.isPhysicalPrimary()); - break; - default: - break; - } - } - - if (settingsCoalescence.doMCGenCoalescence.value) { - std::vector consumed(particlesToProcess.size(), false); - std::vector deuterons; - - // Try p+n -> d and pbar+nbar -> dbar independently. - for (const int sign : {+1, -1}) { - const int protonPDG = sign * PDG_t::kProton; - const int neutronPDG = sign * PDG_t::kNeutron; - const int deuteronPDG = sign * o2::constants::physics::Pdg::kDeuteron; - - for (size_t ip = 0; ip < particlesToProcess.size(); ++ip) { - if (consumed[ip] || particlesToProcess[ip].pdgCode() != protonPDG) { - continue; - } - - const auto& proton = particlesToProcess[ip]; - - for (size_t in = 0; in < particlesToProcess.size(); ++in) { - if (consumed[in] || particlesToProcess[in].pdgCode() != neutronPDG) { - continue; - } - - const auto& neutron = particlesToProcess[in]; - - const float ep = proton.energy(); - const float en = neutron.energy(); - - // Propagate the particle freezing out first to the freeze-out time of the other particle - float xp = proton.vx(); - float yp = proton.vy(); - float zp = proton.vz(); - const float tp = proton.vt(); - - float xn = neutron.vx(); - float yn = neutron.vy(); - float zn = neutron.vz(); - const float tn = neutron.vt(); - - const float commonTime = std::max(static_cast(tp), static_cast(tn)); - - if (tp < commonTime) { - const float dt = commonTime - tp; - xp += proton.px() / ep * dt; - yp += proton.py() / ep * dt; - zp += proton.pz() / ep * dt; - } - - if (tn < commonTime) { - const float dt = commonTime - tn; - xn += neutron.px() / en * dt; - yn += neutron.py() / en * dt; - zn += neutron.pz() / en * dt; - } - - // Velocity of the p-n centre-of-mass frame. - const float totalE = ep + en; - const float totalPx = proton.px() + neutron.px(); - const float totalPy = proton.py() + neutron.py(); - const float totalPz = proton.pz() + neutron.pz(); - - const float bx = totalPx / totalE; - const float by = totalPy / totalE; - const float bz = totalPz / totalE; - - const float beta2 = bx * bx + by * by + bz * bz; - if (beta2 >= 1.) { - continue; - } - - const float gamma = 1. / std::sqrt(1. - beta2); - - // Boost the proton momentum into the p-n rest frame. - // In that frame p_p* = -p_n*, therefore |p_p*| is the relative momentum entering the coalescence cut. - float pxStar = proton.px(); - float pyStar = proton.py(); - float pzStar = proton.pz(); - - if (beta2 > 0.) { - const float betaDotP = bx * proton.px() + by * proton.py() + bz * proton.pz(); - - const float factor = ((gamma - 1.) * betaDotP / beta2) - gamma * ep; - - pxStar += factor * bx; - pyStar += factor * by; - pzStar += factor * bz; - } - - const float pRelative = std::sqrt(pxStar * pxStar + pyStar * pyStar + pzStar * pzStar); - - if (pRelative >= settingsCoalescence.pMax.value) { - continue; - } - - // The two particles have already been propagated to equal time in the lab. Transform their spatial separation to the pair CM frame. - float dx = xp - xn; - float dy = yp - yn; - float dz = zp - zn; - - if (beta2 > 0.) { - const float betaDotR = bx * dx + by * dy + bz * dz; - const float factor = (gamma - 1.) * betaDotR / beta2; - - dx += factor * bx; - dy += factor * by; - dz += factor * bz; - } - - const float rRelative = std::sqrt(dx * dx + dy * dy + dz * dz); - - if (rRelative >= settingsCoalescence.rMax.value) { - continue; - } - - // Successful coalescence: remove the constituent proton and neutron and replace them by a deuteron carrying their total three-momentum - consumed[ip] = true; - consumed[in] = true; - - const float deuteronPt = std::hypot(totalPx, totalPy); - const float deuteronPhi = std::atan2(totalPy, totalPx); - const float deuteronEta = std::asinh(totalPz / std::max(deuteronPt, 1.e-12f)); - - deuterons.emplace_back(deuteronPt, - deuteronEta, - deuteronPhi, - deuteronPDG, - 0.5f * (xp + xn), - 0.5f * (yp + yn), - 0.5f * (zp + zn), - commonTime, - proton.isPhysicalPrimary() && neutron.isPhysicalPrimary()); - break; - } - } - } - - // Construct the post-coalescence particle list. - std::vector coalescedParticles; - coalescedParticles.reserve(particlesToProcess.size() + deuterons.size()); - - for (size_t i = 0; i < particlesToProcess.size(); ++i) { - if (!consumed[i]) { - coalescedParticles.push_back(std::move(particlesToProcess[i])); - } - } - - for (auto& deuteron : deuterons) { - coalescedParticles.push_back(std::move(deuteron)); - } - - particlesToProcess = std::move(coalescedParticles); - } + const auto particlesToProcess = prepareGenParticlesForProcessing(mcParticles, true); // Pairing candidates are collected during the QA loop below (which already visits every particle) GenCollisionCache genCache; @@ -2091,7 +2153,7 @@ struct HadronNucleiCorrelation { void processMixedEventGen(SimCollisions const& mcCollisions, SimParticles const& mcParticles) { - // One pass per collision: multiplicity for the mixing bins + pairing candidates. + // Prepare multiplicity and pairing candidates once per collision. // The mixing loop below never touches the MC particle table again. std::unordered_map genCaches; genCaches.reserve(mcCollisions.size()); @@ -2124,7 +2186,7 @@ struct HadronNucleiCorrelation { PROCESS_SWITCH(HadronNucleiCorrelation, processMixedEventGen, "processMixedEventGen", false); }; -WorkflowSpec defineDataProcessing(ConfigContext const& cfgc) +WorkflowSpec defineDataProcessing(ConfigContext const& context) { - return WorkflowSpec{adaptAnalysisTask(cfgc)}; + return WorkflowSpec{adaptAnalysisTask(context)}; }