From a8c11b2e5b9c511c6fa6adbe8b9c8b03d0d98ea9 Mon Sep 17 00:00:00 2001 From: ALICE Action Bot Date: Tue, 10 Mar 2026 00:06:36 +0000 Subject: [PATCH] Please consider the following formatting changes --- PWGLF/Tasks/Nuspex/MultiplicityPt.cxx | 2553 ++++++++++++------------- 1 file changed, 1276 insertions(+), 1277 deletions(-) diff --git a/PWGLF/Tasks/Nuspex/MultiplicityPt.cxx b/PWGLF/Tasks/Nuspex/MultiplicityPt.cxx index 8dff2a01237..f7b29b3dee8 100644 --- a/PWGLF/Tasks/Nuspex/MultiplicityPt.cxx +++ b/PWGLF/Tasks/Nuspex/MultiplicityPt.cxx @@ -66,7 +66,6 @@ struct MultiplicityPt { // Service Service pdg; - Service ccdb; Configurable isRun3{"isRun3", true, "is Run3 dataset"}; @@ -543,7 +542,7 @@ struct MultiplicityPt { template bool passesTrackSelection(TrackType const& track) const - bool passesTrackSelection(TrackType const& track, float magField = 0) const + bool passesTrackSelection(TrackType const& track, float magField = 0) const { if (track.eta() < cfgCutEtaMin.value || track.eta() > cfgCutEtaMax.value) return false; @@ -620,25 +619,25 @@ struct MultiplicityPt { bestSpecies = kKaon; } if (nsigmaPr < cfgCutNsigmaPr.value && nsigmaPr < minNSigma) { - constexpr float largeNSigmaValue = 999.0f; - float minNSigma = largeNSigmaValue; - int bestSpecies = -1; + constexpr float largeNSigmaValue = 999.0f; + float minNSigma = largeNSigmaValue; + int bestSpecies = -1; - if (nsigmaPi < cfgCutNsigma.value && nsigmaPi < minNSigma) { - minNSigma = nsigmaPi; - bestSpecies = kPion; - } - if (nsigmaKa < cfgCutNsigma.value && nsigmaKa < minNSigma) { - minNSigma = nsigmaKa; - bestSpecies = kKaon; - } - if (nsigmaPr < cfgCutNsigma.value && nsigmaPr < minNSigma) { - minNSigma = nsigmaPr; - bestSpecies = kProton; - } + if (nsigmaPi < cfgCutNsigma.value && nsigmaPi < minNSigma) { + minNSigma = nsigmaPi; + bestSpecies = kPion; + } + if (nsigmaKa < cfgCutNsigma.value && nsigmaKa < minNSigma) { + minNSigma = nsigmaKa; + bestSpecies = kKaon; + } + if (nsigmaPr < cfgCutNsigma.value && nsigmaPr < minNSigma) { + minNSigma = nsigmaPr; + bestSpecies = kProton; + } - return bestSpecies; - } + return bestSpecies; + } // ======================================================================== // EVENT SELECTION FUNCTION @@ -764,15 +763,15 @@ struct MultiplicityPt { } LOG(info) << "=== END OF STREAM COMPLETE ==="; } -}; + }; -//============================================================================= -// Workflow Definition -//============================================================================= - if (std::abs(particle.y()) > cfgCutY.value) - return false; + //============================================================================= + // Workflow Definition + //============================================================================= + if (std::abs(particle.y()) > cfgCutY.value) + return false; - return true; + return true; } template @@ -811,1501 +810,1501 @@ void MultiplicityPt::init(InitContext const&) LOG(info) << "=================================================="; LOG(info) << "Initializing MultiplicityPt task with full centrality diagnostics"; LOG(info) << "=================================================="; -void MultiplicityPt::init(InitContext const&) -{ - // ======================================================================== - // CUSTOM TRACK CUTS INITIALIZATION - // ======================================================================== - - if (useCustomTrackCuts.value) { - LOG(info) << "Using custom track cuts matching spectraTOF approach"; - customTrackCuts = getGlobalTrackSelectionRun3ITSMatch(itsPattern.value); - - customTrackCuts.SetRequireITSRefit(requireITS.value); - customTrackCuts.SetRequireTPCRefit(requireTPC.value); - customTrackCuts.SetMinNClustersITS(min_ITS_nClusters.value); - customTrackCuts.SetRequireGoldenChi2(requireGoldenChi2.value); - customTrackCuts.SetMaxChi2PerClusterTPC(maxChi2PerClusterTPC.value); - customTrackCuts.SetMaxChi2PerClusterITS(maxChi2PerClusterITS.value); - customTrackCuts.SetMinNCrossedRowsTPC(minNCrossedRowsTPC.value); - customTrackCuts.SetMinNClustersTPC(minTPCNClsFound.value); - customTrackCuts.SetMinNCrossedRowsOverFindableClustersTPC(minNCrossedRowsOverFindableClustersTPC.value); - customTrackCuts.SetMaxDcaXYPtDep([](float /*pt*/) { return 10000.f; }); - customTrackCuts.SetMaxDcaZ(maxDcaZ.value); - - customTrackCuts.print(); - } - - // Axis definitions - // ======================================================================== - // PHI CUT INITIALIZATION - // ======================================================================== - - if (applyPhiCut.value) { - fphiCutLow = new TF1("StandardPhiCutLow", - Form("%f/x/x+pi/18.0-%f", - phiCutLowParam1.value, phiCutLowParam2.value), - 0, 50); - fphiCutHigh = new TF1("StandardPhiCutHigh", - Form("%f/x+pi/18.0+%f", - phiCutHighParam1.value, phiCutHighParam2.value), - 0, 50); - - LOGF(info, "=== Phi Cut Parameters ==="); - LOGF(info, "Low cut: %.6f/x² + pi/18 - %.6f", - phiCutLowParam1.value, phiCutLowParam2.value); - LOGF(info, "High cut: %.6f/x + pi/18 + %.6f", - phiCutHighParam1.value, phiCutHighParam2.value); - LOGF(info, "Applied for pT > %.1f GeV/c", pTthresholdPhiCut.value); - } - - // ======================================================================== - // AXIS DEFINITIONS - // ======================================================================== - - ConfigurableAxis ptBinning{ - "ptBinning", - {0.0, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, - 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, - 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.2, 2.4, 2.6, 2.8, - 3.0, 3.5, 4.0, 4.5, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, - 12.0, 14.0, 16.0, 18.0, 20.0, 25.0, 30.0, 40.0, 50.0}, - "pT bin limits"}; - AxisSpec ptAxis = {ptBinning, "#it{p}_{T} (GeV/#it{c})"}; - - std::vector centBinningStd = {0., 1., 5., 10., 15., 20., 30., 40., 50., 60., 70., 80., 90., 100.}; - - // Fine centrality binning for diagnostics (100 bins, guaranteed increasing) - std::vector centBinningFine; - for (int i = 0; i <= 100; i++) { - centBinningFine.push_back(static_cast(i)); - } - - AxisSpec centAxis = {centBinningStd, "FT0M Centrality (%)"}; - AxisSpec centFineAxis = {centBinningFine, "FT0M Centrality (%)"}; - - // Multiplicity axes - properly defined - std::vector multBins; - for (int i = 0; i <= 200; i++) { - multBins.push_back(static_cast(i)); - } - AxisSpec multAxis = {multBins, "N_{ch}^{gen} (|#eta|<0.8)"}; - - // Reconstructed multiplicity axis - properly defined with explicit bin edges - std::vector recoMultBins; - for (int i = 0; i <= 100; i++) { - recoMultBins.push_back(static_cast(i)); - } - AxisSpec recoMultAxis = {recoMultBins, "N_{ch}^{reco}"}; - - //=========================================================================== - // Comprehensive Histogram Registration - //=========================================================================== - - // Centrality diagnostic histograms - USE FINE BINNING - ue.add("Centrality/hCentRaw", "Raw FT0M Centrality (no cuts);Centrality (%);Counts", - HistType::kTH1D, {centFineAxis}); - ue.add("Centrality/hCentAfterVtx", "Centrality after vertex cut;Centrality (%);Counts", - HistType::kTH1D, {centFineAxis}); - ue.add("Centrality/hCentAfterINEL", "Centrality after INEL cut;Centrality (%);Counts", - HistType::kTH1D, {centFineAxis}); - ue.add("Centrality/hCentAfterAll", "Centrality after all cuts;Centrality (%);Counts", - HistType::kTH1D, {centFineAxis}); - - // 2D correlations - USE FINE BINNING FOR DIAGNOSTICS - ue.add("Centrality/hCentVsMult", "Centrality vs Generated Multiplicity;Centrality (%);N_{ch}^{gen}", - HistType::kTH2D, {centFineAxis, multAxis}); - ue.add("Centrality/hMultVsCent", "Generated Multiplicity vs Centrality;N_{ch}^{gen};Centrality (%)", - HistType::kTH2D, {multAxis, centFineAxis}); - ue.add("Centrality/hCentVsVz", "Centrality vs Vertex Z;Centrality (%);V_{z} (cm)", - HistType::kTH2D, {centFineAxis, {40, -20, 20}}); - ue.add("Centrality/hRecoMultVsCent", "Reconstructed Track Multiplicity vs Centrality;Centrality (%);N_{tracks}^{reco}", - HistType::kTH2D, {centFineAxis, recoMultAxis}); - ue.add("Centrality/hGenMultPerCent", "Generated Multiplicity Distribution per Centrality Bin;Centrality (%);", - HistType::kTH2D, {centFineAxis, multAxis}); - - // Vertex resolution vs centrality - ue.add("Centrality/hVertexResVsCent", "Vertex Resolution vs Centrality;Centrality (%);V_{z} resolution (cm)", - HistType::kTH2D, {centFineAxis, {100, -1, 1}}); - - // INEL class distributions - ue.add("INEL/hINELClass", "INEL Class for MC Collisions;INEL Class;Counts", - HistType::kTH1D, {{3, 0.5, 3.5}}); - auto hINEL = ue.get(HIST("INEL/hINELClass")); - hINEL->GetXaxis()->SetBinLabel(1, "INEL0"); - hINEL->GetXaxis()->SetBinLabel(2, "INEL>0"); - hINEL->GetXaxis()->SetBinLabel(3, "INEL>1"); - - ue.add("INEL/hINELVsCent", "INEL Class vs Centrality;Centrality (%);INEL Class", - HistType::kTH2D, {centFineAxis, {3, 0.5, 3.5}}); - - // Cut flow - ue.add("CutFlow/hCutStats", "Cut Statistics;Cut Stage;Counts", - HistType::kTH1D, {{6, 0.5, 6.5}}); - auto hCut = ue.get(HIST("CutFlow/hCutStats")); - hCut->GetXaxis()->SetBinLabel(1, "All reco events"); - hCut->GetXaxis()->SetBinLabel(2, "Has MC match"); - hCut->GetXaxis()->SetBinLabel(3, "Has centrality"); - hCut->GetXaxis()->SetBinLabel(4, "Pass vertex"); - hCut->GetXaxis()->SetBinLabel(5, "Pass INEL"); - hCut->GetXaxis()->SetBinLabel(6, "Selected"); - - ue.add("CutFlow/hCentPerCut", "Centrality Distribution at Each Cut;Cut Stage;Centrality (%)", - HistType::kTH2D, {{6, 0.5, 6.5}, centFineAxis}); - - ue.add("MC/GenRecoCollisions", "Generated and Reconstructed MC Collisions", - HistType::kTH1D, {{10, 0.5, 10.5}}); - auto hColl = ue.get(HIST("MC/GenRecoCollisions")); - hColl->GetXaxis()->SetBinLabel(1, "Collisions generated"); - hColl->GetXaxis()->SetBinLabel(2, "Collisions reconstructed"); - hColl->GetXaxis()->SetBinLabel(3, "INEL>0"); - hColl->GetXaxis()->SetBinLabel(4, "INEL>1"); - - ue.add("hEventLossBreakdown", "Event loss breakdown", - HistType::kTH1D, {{4, 0.5, 4.5}}); - // Multiplicity axis - initially raw multiplicity, will represent percentiles after calibration - std::vector centBins(CentClasses, CentClasses + kCentralityClasses + 1); - AxisSpec multAxis = {centBins, "Centrality/Multiplicity Class (%)"}; - - // Raw multiplicity axis for calibration - AxisSpec rawMultAxis = {150, 0, 150, "N_{ch} (|#eta| < 1.0)"}; - - // ======================================================================== - // HISTOGRAM REGISTRY - // ======================================================================== + void MultiplicityPt::init(InitContext const&) + { + // ======================================================================== + // CUSTOM TRACK CUTS INITIALIZATION + // ======================================================================== - // Multiplicity distribution for percentile calibration - ue.add("Calibration/hRawMultiplicity", "Raw multiplicity distribution;N_{ch};Events", - HistType::kTH1D, {rawMultAxis}); - - // Event counting - ue.add("MC/GenRecoCollisions", "Generated and Reconstructed MC Collisions", HistType::kTH1D, {{10, 0.5, 10.5}}); - auto hColl = ue.get(HIST("MC/GenRecoCollisions")); - hColl->GetXaxis()->SetBinLabel(1, "Collisions generated"); - hColl->GetXaxis()->SetBinLabel(2, "Collisions reconstructed"); - - // Event loss histograms - ue.add("MC/EventLoss/MultGenerated", "Generated events vs multiplicity", - HistType::kTH1D, {multAxis}); - ue.add("MC/EventLoss/MultBadVertex", "Events with bad vertex vs multiplicity", - HistType::kTH1D, {multAxis}); - ue.add("MC/EventLoss/MultPhysicsSelected", "Physics-selected events vs multiplicity", - HistType::kTH1D, {multAxis}); - ue.add("MC/EventLoss/MultReconstructed", "Reconstructed events vs multiplicity", - HistType::kTH1D, {multAxis}); - ue.add("MC/EventLoss/MultRecoSelected", "Reconstructed+selected events vs multiplicity", - HistType::kTH1D, {multAxis}); - - ue.add("hEventLossBreakdown", "Event loss breakdown", HistType::kTH1D, {{4, 0.5, 4.5}}); - auto hLoss = ue.get(HIST("hEventLossBreakdown")); - hLoss->GetXaxis()->SetBinLabel(1, "Physics selected"); - hLoss->GetXaxis()->SetBinLabel(2, "Reconstructed"); - hLoss->GetXaxis()->SetBinLabel(3, "Selected"); - hLoss->GetXaxis()->SetBinLabel(4, "Final efficiency"); - - // Multiplicity histograms - ue.add("MC/EventLoss/NchGenerated", "Generated charged multiplicity;N_{ch}^{gen} (|#eta|<0.8);Counts", - HistType::kTH1D, {{200, 0, 200}}); - ue.add("MC/EventLoss/NchGenerated_PhysicsSelected", "Generated charged multiplicity (physics selected);N_{ch}^{gen} (|#eta|<0.8);Counts", - HistType::kTH1D, {{200, 0, 200}}); - ue.add("MC/EventLoss/NchGenerated_Reconstructed", "Generated charged multiplicity (reconstructed);N_{ch}^{gen} (|#eta|<0.8);Counts", - HistType::kTH1D, {{200, 0, 200}}); - - // pT vs Multiplicity - ue.add("MC/GenPtVsNch", "Generated pT vs Multiplicity;#it{p}_{T} (GeV/#it{c});N_{ch}^{gen}", - HistType::kTH2D, {ptAxis, {200, 0, 200}}); - ue.add("MC/GenPtVsNch_PhysicsSelected", "Generated pT vs Multiplicity (physics selected);#it{p}_{T} (GeV/#it{c});N_{ch}^{gen}", - HistType::kTH2D, {ptAxis, {200, 0, 200}}); - - // Centrality vs Multiplicity correlations - USE STANDARD BINNING FOR THESE - ue.add("MC/EventLoss/GenMultVsCent", "Generated charged particles vs FT0M centrality;FT0M Centrality (%);N_{ch}^{gen} (|#eta|<0.8)", - HistType::kTH2D, {centAxis, multAxis}); - ue.add("MC/EventLoss/GenMultVsCent_Selected", "Generated vs FT0M centrality (selected events);FT0M Centrality (%);N_{ch}^{gen}", - HistType::kTH2D, {centAxis, multAxis}); - ue.add("MC/EventLoss/GenMultVsCent_Rejected", "Generated vs FT0M centrality (rejected events);FT0M Centrality (%);N_{ch}^{gen}", - HistType::kTH2D, {centAxis, multAxis}); - - // TPC cluster histograms - ue.add("hNclFoundTPC", "Number of TPC found clusters", - HistType::kTH1D, {{200, 0, 200, "N_{cl, found}"}}); - ue.add("hNclPIDTPC", "Number of TPC PID clusters", - HistType::kTH1D, {{200, 0, 200, "N_{cl, PID}"}}); - ue.add("hNclFoundTPCvsPt", "TPC found clusters vs pT;#it{p}_{T} (GeV/#it{c});N_{cl,found}", - HistType::kTH2D, {ptAxis, {200, 0., 200.}}); - ue.add("hNclPIDTPCvsPt", "TPC PID clusters vs pT;#it{p}_{T} (GeV/#it{c});N_{cl,PID}", - HistType::kTH2D, {ptAxis, {200, 0., 200.}}); - - // Inclusive histograms - ue.add("Inclusive/hPtPrimGenAll", "All generated primaries (no cuts);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimBadVertex", "Generated primaries (bad vertex);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimGen", "Generated primaries (after physics selection);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimRecoEv", "Generated primaries (reco events);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimGoodEv", "Generated primaries (good events);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - - ue.add("Inclusive/hPtNumEff", "Tracking efficiency numerator;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtDenEff", "Tracking efficiency denominator;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - - ue.add("Inclusive/hPtAllReco", "All reconstructed tracks;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimReco", "Reconstructed primaries;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtSecReco", "Reconstructed secondaries;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - - ue.add("Inclusive/hPtMeasuredVsCent", "All measured tracks (PID) vs centrality;#it{p}_{T} (GeV/#it{c});FT0M Centrality (%)", - HistType::kTH2D, {ptAxis, centAxis}); - - // Particle-specific histograms - // ======================================================================== - // INCLUSIVE CHARGED PARTICLE HISTOGRAMS - // ======================================================================== + if (useCustomTrackCuts.value) { + LOG(info) << "Using custom track cuts matching spectraTOF approach"; + customTrackCuts = getGlobalTrackSelectionRun3ITSMatch(itsPattern.value); - ue.add("Inclusive/hPtPrimGenAll", "All generated primaries (no cuts);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimGenAllVsMult", "All generated primaries vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtPrimBadVertex", "Generated primaries (bad vertex);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimBadVertexVsMult", "Generated primaries (bad vertex) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtPrimGen", "Generated primaries (after physics selection);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimGenVsMult", "Generated primaries (after phys sel) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtPrimRecoEv", "Generated primaries (reco events);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimRecoEvVsMult", "Generated primaries (reco events) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtPrimGoodEv", "Generated primaries (good events);#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimGoodEvVsMult", "Generated primaries (good events) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtNumEff", "Tracking efficiency numerator;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtNumEffVsMult", "Tracking efficiency numerator vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtDenEff", "Tracking efficiency denominator;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtDenEffVsMult", "Tracking efficiency denominator vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtAllReco", "All reconstructed tracks;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtAllRecoVsMult", "All reconstructed tracks vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtPrimReco", "Reconstructed primaries;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtPrimRecoVsMult", "Reconstructed primaries vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtSecReco", "Reconstructed secondaries;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtSecRecoVsMult", "Reconstructed secondaries vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); - - ue.add("Inclusive/hPtMeasured", "All measured tracks;#it{p}_{T} (GeV/#it{c});Counts", - HistType::kTH1D, {ptAxis}); - ue.add("Inclusive/hPtMeasuredVsMult", "All measured tracks vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", - HistType::kTH2D, {ptAxis, multAxis}); + customTrackCuts.SetRequireITSRefit(requireITS.value); + customTrackCuts.SetRequireTPCRefit(requireTPC.value); + customTrackCuts.SetMinNClustersITS(min_ITS_nClusters.value); + customTrackCuts.SetRequireGoldenChi2(requireGoldenChi2.value); + customTrackCuts.SetMaxChi2PerClusterTPC(maxChi2PerClusterTPC.value); + customTrackCuts.SetMaxChi2PerClusterITS(maxChi2PerClusterITS.value); + customTrackCuts.SetMinNCrossedRowsTPC(minNCrossedRowsTPC.value); + customTrackCuts.SetMinNClustersTPC(minTPCNClsFound.value); + customTrackCuts.SetMinNCrossedRowsOverFindableClustersTPC(minNCrossedRowsOverFindableClustersTPC.value); + customTrackCuts.SetMaxDcaXYPtDep([](float /*pt*/) { return 10000.f; }); + customTrackCuts.SetMaxDcaZ(maxDcaZ.value); - // ======================================================================== - // PARTICLE-SPECIFIC HISTOGRAMS - // ======================================================================== - - const std::array particleNames = {"Pion", "Kaon", "Proton"}; - const std::array particleSymbols = {"#pi^{#pm}", "K^{#pm}", "p+#bar{p}"}; + customTrackCuts.print(); + } - for (int iSpecies = 0; iSpecies < kNSpecies; ++iSpecies) { - const auto& name = particleNames[iSpecies]; - const auto& symbol = particleSymbols[iSpecies]; + // Axis definitions + // ======================================================================== + // PHI CUT INITIALIZATION + // ======================================================================== - ue.add(Form("%s/hPtPrimGenAll", name.c_str()), - Form("All generated %s (no cuts);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), - HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtPrimBadVertex", name.c_str()), - Form("Generated %s (bad vertex);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), - HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtPrimGen", name.c_str()), - Form("Generated %s (after physics selection);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), - HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtPrimRecoEv", name.c_str()), - Form("Generated %s (reco events);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), - HistType::kTH1D, {ptAxis}); - // 1D versions - ue.add(Form("%s/hPtPrimGenAll", name.c_str()), - Form("All generated %s (no cuts);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + if (applyPhiCut.value) { + fphiCutLow = new TF1("StandardPhiCutLow", + Form("%f/x/x+pi/18.0-%f", + phiCutLowParam1.value, phiCutLowParam2.value), + 0, 50); + fphiCutHigh = new TF1("StandardPhiCutHigh", + Form("%f/x+pi/18.0+%f", + phiCutHighParam1.value, phiCutHighParam2.value), + 0, 50); + + LOGF(info, "=== Phi Cut Parameters ==="); + LOGF(info, "Low cut: %.6f/x² + pi/18 - %.6f", + phiCutLowParam1.value, phiCutLowParam2.value); + LOGF(info, "High cut: %.6f/x + pi/18 + %.6f", + phiCutHighParam1.value, phiCutHighParam2.value); + LOGF(info, "Applied for pT > %.1f GeV/c", pTthresholdPhiCut.value); + } + + // ======================================================================== + // AXIS DEFINITIONS + // ======================================================================== + + ConfigurableAxis ptBinning{ + "ptBinning", + {0.0, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, + 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, + 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.2, 2.4, 2.6, 2.8, + 3.0, 3.5, 4.0, 4.5, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, + 12.0, 14.0, 16.0, 18.0, 20.0, 25.0, 30.0, 40.0, 50.0}, + "pT bin limits"}; + AxisSpec ptAxis = {ptBinning, "#it{p}_{T} (GeV/#it{c})"}; + + std::vector centBinningStd = {0., 1., 5., 10., 15., 20., 30., 40., 50., 60., 70., 80., 90., 100.}; + + // Fine centrality binning for diagnostics (100 bins, guaranteed increasing) + std::vector centBinningFine; + for (int i = 0; i <= 100; i++) { + centBinningFine.push_back(static_cast(i)); + } + + AxisSpec centAxis = {centBinningStd, "FT0M Centrality (%)"}; + AxisSpec centFineAxis = {centBinningFine, "FT0M Centrality (%)"}; + + // Multiplicity axes - properly defined + std::vector multBins; + for (int i = 0; i <= 200; i++) { + multBins.push_back(static_cast(i)); + } + AxisSpec multAxis = {multBins, "N_{ch}^{gen} (|#eta|<0.8)"}; + + // Reconstructed multiplicity axis - properly defined with explicit bin edges + std::vector recoMultBins; + for (int i = 0; i <= 100; i++) { + recoMultBins.push_back(static_cast(i)); + } + AxisSpec recoMultAxis = {recoMultBins, "N_{ch}^{reco}"}; + + //=========================================================================== + // Comprehensive Histogram Registration + //=========================================================================== + + // Centrality diagnostic histograms - USE FINE BINNING + ue.add("Centrality/hCentRaw", "Raw FT0M Centrality (no cuts);Centrality (%);Counts", + HistType::kTH1D, {centFineAxis}); + ue.add("Centrality/hCentAfterVtx", "Centrality after vertex cut;Centrality (%);Counts", + HistType::kTH1D, {centFineAxis}); + ue.add("Centrality/hCentAfterINEL", "Centrality after INEL cut;Centrality (%);Counts", + HistType::kTH1D, {centFineAxis}); + ue.add("Centrality/hCentAfterAll", "Centrality after all cuts;Centrality (%);Counts", + HistType::kTH1D, {centFineAxis}); + + // 2D correlations - USE FINE BINNING FOR DIAGNOSTICS + ue.add("Centrality/hCentVsMult", "Centrality vs Generated Multiplicity;Centrality (%);N_{ch}^{gen}", + HistType::kTH2D, {centFineAxis, multAxis}); + ue.add("Centrality/hMultVsCent", "Generated Multiplicity vs Centrality;N_{ch}^{gen};Centrality (%)", + HistType::kTH2D, {multAxis, centFineAxis}); + ue.add("Centrality/hCentVsVz", "Centrality vs Vertex Z;Centrality (%);V_{z} (cm)", + HistType::kTH2D, {centFineAxis, {40, -20, 20}}); + ue.add("Centrality/hRecoMultVsCent", "Reconstructed Track Multiplicity vs Centrality;Centrality (%);N_{tracks}^{reco}", + HistType::kTH2D, {centFineAxis, recoMultAxis}); + ue.add("Centrality/hGenMultPerCent", "Generated Multiplicity Distribution per Centrality Bin;Centrality (%);", + HistType::kTH2D, {centFineAxis, multAxis}); + + // Vertex resolution vs centrality + ue.add("Centrality/hVertexResVsCent", "Vertex Resolution vs Centrality;Centrality (%);V_{z} resolution (cm)", + HistType::kTH2D, {centFineAxis, {100, -1, 1}}); + + // INEL class distributions + ue.add("INEL/hINELClass", "INEL Class for MC Collisions;INEL Class;Counts", + HistType::kTH1D, {{3, 0.5, 3.5}}); + auto hINEL = ue.get(HIST("INEL/hINELClass")); + hINEL->GetXaxis()->SetBinLabel(1, "INEL0"); + hINEL->GetXaxis()->SetBinLabel(2, "INEL>0"); + hINEL->GetXaxis()->SetBinLabel(3, "INEL>1"); + + ue.add("INEL/hINELVsCent", "INEL Class vs Centrality;Centrality (%);INEL Class", + HistType::kTH2D, {centFineAxis, {3, 0.5, 3.5}}); + + // Cut flow + ue.add("CutFlow/hCutStats", "Cut Statistics;Cut Stage;Counts", + HistType::kTH1D, {{6, 0.5, 6.5}}); + auto hCut = ue.get(HIST("CutFlow/hCutStats")); + hCut->GetXaxis()->SetBinLabel(1, "All reco events"); + hCut->GetXaxis()->SetBinLabel(2, "Has MC match"); + hCut->GetXaxis()->SetBinLabel(3, "Has centrality"); + hCut->GetXaxis()->SetBinLabel(4, "Pass vertex"); + hCut->GetXaxis()->SetBinLabel(5, "Pass INEL"); + hCut->GetXaxis()->SetBinLabel(6, "Selected"); + + ue.add("CutFlow/hCentPerCut", "Centrality Distribution at Each Cut;Cut Stage;Centrality (%)", + HistType::kTH2D, {{6, 0.5, 6.5}, centFineAxis}); + + ue.add("MC/GenRecoCollisions", "Generated and Reconstructed MC Collisions", + HistType::kTH1D, {{10, 0.5, 10.5}}); + auto hColl = ue.get(HIST("MC/GenRecoCollisions")); + hColl->GetXaxis()->SetBinLabel(1, "Collisions generated"); + hColl->GetXaxis()->SetBinLabel(2, "Collisions reconstructed"); + hColl->GetXaxis()->SetBinLabel(3, "INEL>0"); + hColl->GetXaxis()->SetBinLabel(4, "INEL>1"); + + ue.add("hEventLossBreakdown", "Event loss breakdown", + HistType::kTH1D, {{4, 0.5, 4.5}}); + // Multiplicity axis - initially raw multiplicity, will represent percentiles after calibration + std::vector centBins(CentClasses, CentClasses + kCentralityClasses + 1); + AxisSpec multAxis = {centBins, "Centrality/Multiplicity Class (%)"}; + + // Raw multiplicity axis for calibration + AxisSpec rawMultAxis = {150, 0, 150, "N_{ch} (|#eta| < 1.0)"}; + + // ======================================================================== + // HISTOGRAM REGISTRY + // ======================================================================== + + // Multiplicity distribution for percentile calibration + ue.add("Calibration/hRawMultiplicity", "Raw multiplicity distribution;N_{ch};Events", + HistType::kTH1D, {rawMultAxis}); + + // Event counting + ue.add("MC/GenRecoCollisions", "Generated and Reconstructed MC Collisions", HistType::kTH1D, {{10, 0.5, 10.5}}); + auto hColl = ue.get(HIST("MC/GenRecoCollisions")); + hColl->GetXaxis()->SetBinLabel(1, "Collisions generated"); + hColl->GetXaxis()->SetBinLabel(2, "Collisions reconstructed"); + + // Event loss histograms + ue.add("MC/EventLoss/MultGenerated", "Generated events vs multiplicity", + HistType::kTH1D, {multAxis}); + ue.add("MC/EventLoss/MultBadVertex", "Events with bad vertex vs multiplicity", + HistType::kTH1D, {multAxis}); + ue.add("MC/EventLoss/MultPhysicsSelected", "Physics-selected events vs multiplicity", + HistType::kTH1D, {multAxis}); + ue.add("MC/EventLoss/MultReconstructed", "Reconstructed events vs multiplicity", + HistType::kTH1D, {multAxis}); + ue.add("MC/EventLoss/MultRecoSelected", "Reconstructed+selected events vs multiplicity", + HistType::kTH1D, {multAxis}); + + ue.add("hEventLossBreakdown", "Event loss breakdown", HistType::kTH1D, {{4, 0.5, 4.5}}); + auto hLoss = ue.get(HIST("hEventLossBreakdown")); + hLoss->GetXaxis()->SetBinLabel(1, "Physics selected"); + hLoss->GetXaxis()->SetBinLabel(2, "Reconstructed"); + hLoss->GetXaxis()->SetBinLabel(3, "Selected"); + hLoss->GetXaxis()->SetBinLabel(4, "Final efficiency"); + + // Multiplicity histograms + ue.add("MC/EventLoss/NchGenerated", "Generated charged multiplicity;N_{ch}^{gen} (|#eta|<0.8);Counts", + HistType::kTH1D, {{200, 0, 200}}); + ue.add("MC/EventLoss/NchGenerated_PhysicsSelected", "Generated charged multiplicity (physics selected);N_{ch}^{gen} (|#eta|<0.8);Counts", + HistType::kTH1D, {{200, 0, 200}}); + ue.add("MC/EventLoss/NchGenerated_Reconstructed", "Generated charged multiplicity (reconstructed);N_{ch}^{gen} (|#eta|<0.8);Counts", + HistType::kTH1D, {{200, 0, 200}}); + + // pT vs Multiplicity + ue.add("MC/GenPtVsNch", "Generated pT vs Multiplicity;#it{p}_{T} (GeV/#it{c});N_{ch}^{gen}", + HistType::kTH2D, {ptAxis, {200, 0, 200}}); + ue.add("MC/GenPtVsNch_PhysicsSelected", "Generated pT vs Multiplicity (physics selected);#it{p}_{T} (GeV/#it{c});N_{ch}^{gen}", + HistType::kTH2D, {ptAxis, {200, 0, 200}}); + + // Centrality vs Multiplicity correlations - USE STANDARD BINNING FOR THESE + ue.add("MC/EventLoss/GenMultVsCent", "Generated charged particles vs FT0M centrality;FT0M Centrality (%);N_{ch}^{gen} (|#eta|<0.8)", + HistType::kTH2D, {centAxis, multAxis}); + ue.add("MC/EventLoss/GenMultVsCent_Selected", "Generated vs FT0M centrality (selected events);FT0M Centrality (%);N_{ch}^{gen}", + HistType::kTH2D, {centAxis, multAxis}); + ue.add("MC/EventLoss/GenMultVsCent_Rejected", "Generated vs FT0M centrality (rejected events);FT0M Centrality (%);N_{ch}^{gen}", + HistType::kTH2D, {centAxis, multAxis}); + + // TPC cluster histograms + ue.add("hNclFoundTPC", "Number of TPC found clusters", + HistType::kTH1D, {{200, 0, 200, "N_{cl, found}"}}); + ue.add("hNclPIDTPC", "Number of TPC PID clusters", + HistType::kTH1D, {{200, 0, 200, "N_{cl, PID}"}}); + ue.add("hNclFoundTPCvsPt", "TPC found clusters vs pT;#it{p}_{T} (GeV/#it{c});N_{cl,found}", + HistType::kTH2D, {ptAxis, {200, 0., 200.}}); + ue.add("hNclPIDTPCvsPt", "TPC PID clusters vs pT;#it{p}_{T} (GeV/#it{c});N_{cl,PID}", + HistType::kTH2D, {ptAxis, {200, 0., 200.}}); + + // Inclusive histograms + ue.add("Inclusive/hPtPrimGenAll", "All generated primaries (no cuts);#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - - ue.add(Form("%s/hPtPrimBadVertex", name.c_str()), - Form("Generated %s (bad vertex);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtPrimBadVertex", "Generated primaries (bad vertex);#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - - ue.add(Form("%s/hPtPrimGen", name.c_str()), - Form("Generated %s (after physics selection);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtPrimGen", "Generated primaries (after physics selection);#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - - ue.add(Form("%s/hPtPrimRecoEv", name.c_str()), - Form("Generated %s (reco events);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtPrimRecoEv", "Generated primaries (reco events);#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - - ue.add(Form("%s/hPtPrimGoodEv", name.c_str()), - Form("Generated %s (good events);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtPrimGoodEv", "Generated primaries (good events);#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtNumEff", name.c_str()), - Form("%s tracking efficiency numerator;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtNumEff", "Tracking efficiency numerator;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtDenEff", name.c_str()), - Form("%s tracking efficiency denominator;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtDenEff", "Tracking efficiency denominator;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtAllReco", name.c_str()), - Form("All reconstructed %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtAllReco", "All reconstructed tracks;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtPrimReco", name.c_str()), - Form("Reconstructed primary %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtPrimReco", "Reconstructed primaries;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtSecReco", name.c_str()), - Form("Reconstructed secondary %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtSecReco", "Reconstructed secondaries;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtMeasuredVsCent", name.c_str()), - Form("Measured %s (PID) vs centrality;#it{p}_{T} (GeV/#it{c});FT0M Centrality (%%)", symbol.c_str()), + ue.add("Inclusive/hPtMeasuredVsCent", "All measured tracks (PID) vs centrality;#it{p}_{T} (GeV/#it{c});FT0M Centrality (%)", HistType::kTH2D, {ptAxis, centAxis}); - // 2D versions (vs multiplicity class) - ue.add(Form("%s/hPtPrimGenAllVsMult", name.c_str()), - Form("All generated %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + // Particle-specific histograms + // ======================================================================== + // INCLUSIVE CHARGED PARTICLE HISTOGRAMS + // ======================================================================== + + ue.add("Inclusive/hPtPrimGenAll", "All generated primaries (no cuts);#it{p}_{T} (GeV/#it{c});Counts", + HistType::kTH1D, {ptAxis}); + ue.add("Inclusive/hPtPrimGenAllVsMult", "All generated primaries vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - ue.add(Form("%s/hPtPrimBadVertexVsMult", name.c_str()), - Form("Generated %s (bad vertex) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtPrimBadVertex", "Generated primaries (bad vertex);#it{p}_{T} (GeV/#it{c});Counts", + HistType::kTH1D, {ptAxis}); + ue.add("Inclusive/hPtPrimBadVertexVsMult", "Generated primaries (bad vertex) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - ue.add(Form("%s/hPtPrimGenVsMult", name.c_str()), - Form("Generated %s (after phys sel) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtPrimGen", "Generated primaries (after physics selection);#it{p}_{T} (GeV/#it{c});Counts", + HistType::kTH1D, {ptAxis}); + ue.add("Inclusive/hPtPrimGenVsMult", "Generated primaries (after phys sel) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - ue.add(Form("%s/hPtPrimRecoEvVsMult", name.c_str()), - Form("Generated %s (reco events) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtPrimRecoEv", "Generated primaries (reco events);#it{p}_{T} (GeV/#it{c});Counts", + HistType::kTH1D, {ptAxis}); + ue.add("Inclusive/hPtPrimRecoEvVsMult", "Generated primaries (reco events) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - ue.add(Form("%s/hPtPrimGoodEvVsMult", name.c_str()), - Form("Generated %s (good events) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtPrimGoodEv", "Generated primaries (good events);#it{p}_{T} (GeV/#it{c});Counts", + HistType::kTH1D, {ptAxis}); + ue.add("Inclusive/hPtPrimGoodEvVsMult", "Generated primaries (good events) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - // Tracking efficiency - ue.add(Form("%s/hPtNumEff", name.c_str()), - Form("%s tracking efficiency numerator;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtNumEff", "Tracking efficiency numerator;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtNumEffVsMult", name.c_str()), - Form("%s tracking eff numerator vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtNumEffVsMult", "Tracking efficiency numerator vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - ue.add(Form("%s/hPtDenEff", name.c_str()), - Form("%s tracking efficiency denominator;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtDenEff", "Tracking efficiency denominator;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtDenEffVsMult", name.c_str()), - Form("%s tracking eff denominator vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtDenEffVsMult", "Tracking efficiency denominator vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - // Primary fraction - ue.add(Form("%s/hPtAllReco", name.c_str()), - Form("All reconstructed %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtAllReco", "All reconstructed tracks;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtAllRecoVsMult", name.c_str()), - Form("All reconstructed %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtAllRecoVsMult", "All reconstructed tracks vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - ue.add(Form("%s/hPtPrimReco", name.c_str()), - Form("Reconstructed primary %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtPrimReco", "Reconstructed primaries;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtPrimRecoVsMult", name.c_str()), - Form("Reconstructed primary %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtPrimRecoVsMult", "Reconstructed primaries vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - ue.add(Form("%s/hPtSecReco", name.c_str()), - Form("Reconstructed secondary %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtSecReco", "Reconstructed secondaries;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtSecRecoVsMult", name.c_str()), - Form("Reconstructed secondary %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtSecRecoVsMult", "Reconstructed secondaries vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - // Measured spectra - ue.add(Form("%s/hPtMeasured", name.c_str()), - Form("Measured %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + ue.add("Inclusive/hPtMeasured", "All measured tracks;#it{p}_{T} (GeV/#it{c});Counts", HistType::kTH1D, {ptAxis}); - ue.add(Form("%s/hPtMeasuredVsMult", name.c_str()), - Form("Measured %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + ue.add("Inclusive/hPtMeasuredVsMult", "All measured tracks vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%)", HistType::kTH2D, {ptAxis, multAxis}); - // PID quality - if (enablePIDHistograms) { - ue.add(Form("%s/hNsigmaTPC", name.c_str()), - Form("TPC n#sigma %s;#it{p}_{T} (GeV/#it{c});n#sigma_{TPC}", symbol.c_str()), - HistType::kTH2D, {ptAxis, {200, -10, 10}}); + // ======================================================================== + // PARTICLE-SPECIFIC HISTOGRAMS + // ======================================================================== + + const std::array particleNames = {"Pion", "Kaon", "Proton"}; + const std::array particleSymbols = {"#pi^{#pm}", "K^{#pm}", "p+#bar{p}"}; + + for (int iSpecies = 0; iSpecies < kNSpecies; ++iSpecies) { + const auto& name = particleNames[iSpecies]; + const auto& symbol = particleSymbols[iSpecies]; + + ue.add(Form("%s/hPtPrimGenAll", name.c_str()), + Form("All generated %s (no cuts);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtPrimBadVertex", name.c_str()), + Form("Generated %s (bad vertex);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtPrimGen", name.c_str()), + Form("Generated %s (after physics selection);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtPrimRecoEv", name.c_str()), + Form("Generated %s (reco events);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + // 1D versions + ue.add(Form("%s/hPtPrimGenAll", name.c_str()), + Form("All generated %s (no cuts);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + + ue.add(Form("%s/hPtPrimBadVertex", name.c_str()), + Form("Generated %s (bad vertex);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + + ue.add(Form("%s/hPtPrimGen", name.c_str()), + Form("Generated %s (after physics selection);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + + ue.add(Form("%s/hPtPrimRecoEv", name.c_str()), + Form("Generated %s (reco events);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + + ue.add(Form("%s/hPtPrimGoodEv", name.c_str()), + Form("Generated %s (good events);#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + + ue.add(Form("%s/hPtNumEff", name.c_str()), + Form("%s tracking efficiency numerator;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtDenEff", name.c_str()), + Form("%s tracking efficiency denominator;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + + ue.add(Form("%s/hPtAllReco", name.c_str()), + Form("All reconstructed %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtPrimReco", name.c_str()), + Form("Reconstructed primary %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtSecReco", name.c_str()), + Form("Reconstructed secondary %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + + ue.add(Form("%s/hPtMeasuredVsCent", name.c_str()), + Form("Measured %s (PID) vs centrality;#it{p}_{T} (GeV/#it{c});FT0M Centrality (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, centAxis}); + + // 2D versions (vs multiplicity class) + ue.add(Form("%s/hPtPrimGenAllVsMult", name.c_str()), + Form("All generated %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + ue.add(Form("%s/hPtPrimBadVertexVsMult", name.c_str()), + Form("Generated %s (bad vertex) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + ue.add(Form("%s/hPtPrimGenVsMult", name.c_str()), + Form("Generated %s (after phys sel) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + ue.add(Form("%s/hPtPrimRecoEvVsMult", name.c_str()), + Form("Generated %s (reco events) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + ue.add(Form("%s/hPtPrimGoodEvVsMult", name.c_str()), + Form("Generated %s (good events) vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + // Tracking efficiency + ue.add(Form("%s/hPtNumEff", name.c_str()), + Form("%s tracking efficiency numerator;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtNumEffVsMult", name.c_str()), + Form("%s tracking eff numerator vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + ue.add(Form("%s/hPtDenEff", name.c_str()), + Form("%s tracking efficiency denominator;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtDenEffVsMult", name.c_str()), + Form("%s tracking eff denominator vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + // Primary fraction + ue.add(Form("%s/hPtAllReco", name.c_str()), + Form("All reconstructed %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtAllRecoVsMult", name.c_str()), + Form("All reconstructed %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + ue.add(Form("%s/hPtPrimReco", name.c_str()), + Form("Reconstructed primary %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtPrimRecoVsMult", name.c_str()), + Form("Reconstructed primary %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + ue.add(Form("%s/hPtSecReco", name.c_str()), + Form("Reconstructed secondary %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtSecRecoVsMult", name.c_str()), + Form("Reconstructed secondary %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + // Measured spectra + ue.add(Form("%s/hPtMeasured", name.c_str()), + Form("Measured %s;#it{p}_{T} (GeV/#it{c});Counts", symbol.c_str()), + HistType::kTH1D, {ptAxis}); + ue.add(Form("%s/hPtMeasuredVsMult", name.c_str()), + Form("Measured %s vs mult;#it{p}_{T} (GeV/#it{c});Mult Class (%%)", symbol.c_str()), + HistType::kTH2D, {ptAxis, multAxis}); + + // PID quality + if (enablePIDHistograms) { + ue.add(Form("%s/hNsigmaTPC", name.c_str()), + Form("TPC n#sigma %s;#it{p}_{T} (GeV/#it{c});n#sigma_{TPC}", symbol.c_str()), + HistType::kTH2D, {ptAxis, {200, -10, 10}}); + } } - } - // Event selection histogram - // ======================================================================== - // PHI CUT MONITORING - // ======================================================================== + // Event selection histogram + // ======================================================================== + // PHI CUT MONITORING + // ======================================================================== - if (applyPhiCut.value) { - ue.add("PhiCut/hPtVsPhiPrimeBefore", "pT vs φ' before cut;p_{T} (GeV/c);φ'", - HistType::kTH2F, {{100, 0, 10}, {100, 0, 0.4}}); - ue.add("PhiCut/hPtVsPhiPrimeAfter", "pT vs φ' after cut;p_{T} (GeV/c);φ'", - HistType::kTH2F, {{100, 0, 10}, {100, 0, 0.4}}); - ue.add("PhiCut/hRejectionRate", "Track rejection rate by phi cut;p_{T} (GeV/c);Rejection Rate", - HistType::kTProfile, {{100, 0, 10}}); + if (applyPhiCut.value) { + ue.add("PhiCut/hPtVsPhiPrimeBefore", "pT vs φ' before cut;p_{T} (GeV/c);φ'", + HistType::kTH2F, {{100, 0, 10}, {100, 0, 0.4}}); + ue.add("PhiCut/hPtVsPhiPrimeAfter", "pT vs φ' after cut;p_{T} (GeV/c);φ'", + HistType::kTH2F, {{100, 0, 10}, {100, 0, 0.4}}); + ue.add("PhiCut/hRejectionRate", "Track rejection rate by phi cut;p_{T} (GeV/c);Rejection Rate", + HistType::kTProfile, {{100, 0, 10}}); + } + + // ======================================================================== + // EVENT SELECTION HISTOGRAM + // ======================================================================== + + constexpr int nEvSelBins = 20; + constexpr float evSelMin = 0.5f; + constexpr float evSelMax = 20.5f; + ue.add("evsel", "Event selection", HistType::kTH1D, {{nEvSelBins, evSelMin, evSelMax}}); + auto h = ue.get(HIST("evsel")); + h->GetXaxis()->SetBinLabel(1, "Events read"); + h->GetXaxis()->SetBinLabel(2, "INEL>0"); + h->GetXaxis()->SetBinLabel(3, "INEL>1"); + h->GetXaxis()->SetBinLabel(4, "Trigger passed"); + h->GetXaxis()->SetBinLabel(5, "NoITSROFrameBorder"); + h->GetXaxis()->SetBinLabel(6, "NoSameBunchPileup"); + h->GetXaxis()->SetBinLabel(7, "IsGoodZvtxFT0vsPV"); + h->GetXaxis()->SetBinLabel(8, "IsVertexITSTPC"); + h->GetXaxis()->SetBinLabel(9, "NoTimeFrameBorder"); + h->GetXaxis()->SetBinLabel(13, "posZ passed"); + + // Basic tracking histograms + h->GetXaxis()->SetBinLabel(14, "INEL>0 (final)"); + h->GetXaxis()->SetBinLabel(15, "INEL>1 (final)"); + + ue.add("hEta", "Track eta;#eta;Counts", HistType::kTH1D, {{20, -0.8, 0.8}}); + ue.add("hPhi", "Track phi;#varphi (rad);Counts", HistType::kTH1D, {{64, 0, 2.0 * M_PI}}); + ue.add("hvtxZ", "Vertex Z (data);Vertex Z (cm);Events", HistType::kTH1F, {{40, -20.0, 20.0}}); + ue.add("hvtxZmc", "MC vertex Z;Vertex Z (cm);Events", HistType::kTH1F, {{40, -20.0, 20.0}}); + + LOG(info) << "=== Initialized MultiplicityPt task with full centrality diagnostics ==="; + LOG(info) << "Standard centrality binning: " << centBinningStd.size() - 1 << " bins (0-100%)"; + LOG(info) << "Fine centrality binning: " << centBinningFine.size() - 1 << " bins (0-100%)"; } - // ======================================================================== - // EVENT SELECTION HISTOGRAM - // ======================================================================== - - constexpr int nEvSelBins = 20; - constexpr float evSelMin = 0.5f; - constexpr float evSelMax = 20.5f; - ue.add("evsel", "Event selection", HistType::kTH1D, {{nEvSelBins, evSelMin, evSelMax}}); - auto h = ue.get(HIST("evsel")); - h->GetXaxis()->SetBinLabel(1, "Events read"); - h->GetXaxis()->SetBinLabel(2, "INEL>0"); - h->GetXaxis()->SetBinLabel(3, "INEL>1"); - h->GetXaxis()->SetBinLabel(4, "Trigger passed"); - h->GetXaxis()->SetBinLabel(5, "NoITSROFrameBorder"); - h->GetXaxis()->SetBinLabel(6, "NoSameBunchPileup"); - h->GetXaxis()->SetBinLabel(7, "IsGoodZvtxFT0vsPV"); - h->GetXaxis()->SetBinLabel(8, "IsVertexITSTPC"); - h->GetXaxis()->SetBinLabel(9, "NoTimeFrameBorder"); - h->GetXaxis()->SetBinLabel(13, "posZ passed"); - - // Basic tracking histograms - h->GetXaxis()->SetBinLabel(14, "INEL>0 (final)"); - h->GetXaxis()->SetBinLabel(15, "INEL>1 (final)"); - - ue.add("hEta", "Track eta;#eta;Counts", HistType::kTH1D, {{20, -0.8, 0.8}}); - ue.add("hPhi", "Track phi;#varphi (rad);Counts", HistType::kTH1D, {{64, 0, 2.0 * M_PI}}); - ue.add("hvtxZ", "Vertex Z (data);Vertex Z (cm);Events", HistType::kTH1F, {{40, -20.0, 20.0}}); - ue.add("hvtxZmc", "MC vertex Z;Vertex Z (cm);Events", HistType::kTH1F, {{40, -20.0, 20.0}}); - - LOG(info) << "=== Initialized MultiplicityPt task with full centrality diagnostics ==="; - LOG(info) << "Standard centrality binning: " << centBinningStd.size() - 1 << " bins (0-100%)"; - LOG(info) << "Fine centrality binning: " << centBinningFine.size() - 1 << " bins (0-100%)"; -} + //============================================================================= + // Process Functions + //============================================================================= -//============================================================================= -// Process Functions -//============================================================================= + void MultiplicityPt::processData(CollisionTableData::iterator const& /*collision*/, + TrackTableData const& /*tracks*/, + BCsRun3 const& /*bcs*/) + { + // Intentionally empty - data processing disabled + } -void MultiplicityPt::processData(CollisionTableData::iterator const& /*collision*/, - TrackTableData const& /*tracks*/, + void MultiplicityPt::processMC(TrackTableMC const& tracks, + aod::McParticles const& particles, + aod::McCollisions const& mcCollisions, + RecoCollisions const& collisions, + aod::McCollisionLabels const& labels, + aod::McCentFT0Ms const& centTable, BCsRun3 const& /*bcs*/) -{ - // Intentionally empty - data processing disabled -} + { + LOG(info) << "\n=== processMC START ==="; + LOG(info) << "Total MC collisions (generated): " << mcCollisions.size(); + LOG(info) << "Total reconstructed collisions: " << collisions.size(); + LOG(info) << "Total collision labels: " << labels.size(); + LOG(info) << "Total centrality entries: " << centTable.size(); + + //=========================================================================== + // DEBUG: Print raw centrality information first + //=========================================================================== + LOG(info) << "\n=== CENTRALITY DEBUG - RAW DATA ==="; + LOG(info) << "First 20 centrality values from centTable:"; + int debugCount = 0; + float minCent = 999.0f, maxCent = -999.0f; + std::map centDistribution; + + for (const auto& cent : centTable) { + float c = cent.centFT0M(); + if (debugCount < 20) { + LOG(info) << " Cent entry " << debugCount << ": " << c; + } + minCent = std::min(minCent, c); + maxCent = std::max(maxCent, c); -void MultiplicityPt::processMC(TrackTableMC const& tracks, - aod::McParticles const& particles, - aod::McCollisions const& mcCollisions, - RecoCollisions const& collisions, - aod::McCollisionLabels const& labels, - aod::McCentFT0Ms const& centTable, - BCsRun3 const& /*bcs*/) -{ - LOG(info) << "\n=== processMC START ==="; - LOG(info) << "Total MC collisions (generated): " << mcCollisions.size(); - LOG(info) << "Total reconstructed collisions: " << collisions.size(); - LOG(info) << "Total collision labels: " << labels.size(); - LOG(info) << "Total centrality entries: " << centTable.size(); + int bin10 = static_cast(c / 10) * 10; + centDistribution[bin10]++; + debugCount++; + } - //=========================================================================== - // DEBUG: Print raw centrality information first - //=========================================================================== - LOG(info) << "\n=== CENTRALITY DEBUG - RAW DATA ==="; - LOG(info) << "First 20 centrality values from centTable:"; - int debugCount = 0; - float minCent = 999.0f, maxCent = -999.0f; - std::map centDistribution; - - for (const auto& cent : centTable) { - float c = cent.centFT0M(); - if (debugCount < 20) { - LOG(info) << " Cent entry " << debugCount << ": " << c; + LOG(info) << "Centrality range: [" << minCent << ", " << maxCent << "]"; + LOG(info) << "Distribution by 10% bins:"; + for (int i = 0; i < 100; i += 10) { + LOG(info) << " " << i << "-" << i + 10 << "%: " << centDistribution[i]; } - minCent = std::min(minCent, c); - maxCent = std::max(maxCent, c); - int bin10 = static_cast(c / 10) * 10; - centDistribution[bin10]++; - debugCount++; - } + // Check if centrality is inverted (0 = peripheral, 100 = central) + // If minCent is near 0 and maxCent near 100, check correlation with multiplicity + LOG(info) << "Checking if centrality might be inverted..."; + LOG(info) << "Will check correlation with multiplicity in the next step."; - LOG(info) << "Centrality range: [" << minCent << ", " << maxCent << "]"; - LOG(info) << "Distribution by 10% bins:"; - for (int i = 0; i < 100; i += 10) { - LOG(info) << " " << i << "-" << i + 10 << "%: " << centDistribution[i]; - } + //=========================================================================== + // FIRST PASS: Build maps of MC collision ID to generated particle counts + //=========================================================================== + std::map mcCollisionToNch; + std::map mcCollisionVz; + std::set physicsSelectedMCCollisions; + std::map mcCollisionToINELClass; // 0=INEL0, 1=INEL>0, 2=INEL>1 - // Check if centrality is inverted (0 = peripheral, 100 = central) - // If minCent is near 0 and maxCent near 100, check correlation with multiplicity - LOG(info) << "Checking if centrality might be inverted..."; - LOG(info) << "Will check correlation with multiplicity in the next step."; + ue.fill(HIST("MC/GenRecoCollisions"), 1.f, mcCollisions.size()); + ue.fill(HIST("MC/GenRecoCollisions"), 2.f, collisions.size()); - //=========================================================================== - // FIRST PASS: Build maps of MC collision ID to generated particle counts - //=========================================================================== - std::map mcCollisionToNch; - std::map mcCollisionVz; - std::set physicsSelectedMCCollisions; - std::map mcCollisionToINELClass; // 0=INEL0, 1=INEL>0, 2=INEL>1 + LOG(info) << "\n--- FIRST PASS: Building MC collision maps ---"; - ue.fill(HIST("MC/GenRecoCollisions"), 1.f, mcCollisions.size()); - ue.fill(HIST("MC/GenRecoCollisions"), 2.f, collisions.size()); + int mcWithParticles = 0; + int mcINEL0 = 0, mcINELgt0 = 0, mcINELgt1 = 0; - LOG(info) << "\n--- FIRST PASS: Building MC collision maps ---"; + for (const auto& mcCollision : mcCollisions) { + int64_t mcCollId = mcCollision.globalIndex(); + auto particlesInCollision = particles.sliceBy(perMCCol, mcCollId); - int mcWithParticles = 0; - int mcINEL0 = 0, mcINELgt0 = 0, mcINELgt1 = 0; + int nGenCharged = countGeneratedChargedPrimaries(particlesInCollision, cfgCutEtaMax.value, cfgTrkLowPtCut.value); - for (const auto& mcCollision : mcCollisions) { - int64_t mcCollId = mcCollision.globalIndex(); - auto particlesInCollision = particles.sliceBy(perMCCol, mcCollId); + mcCollisionToNch[mcCollId] = nGenCharged; + mcCollisionVz[mcCollId] = mcCollision.posZ(); - int nGenCharged = countGeneratedChargedPrimaries(particlesInCollision, cfgCutEtaMax.value, cfgTrkLowPtCut.value); + // Determine INEL class + bool inel0 = o2::pwglf::isINELgt0mc(particlesInCollision, pdg); + bool inel1 = o2::pwglf::isINELgt1mc(particlesInCollision, pdg); - mcCollisionToNch[mcCollId] = nGenCharged; - mcCollisionVz[mcCollId] = mcCollision.posZ(); + int inelClass = 0; + if (inel1) + inelClass = 2; + else if (inel0) + inelClass = 1; + mcCollisionToINELClass[mcCollId] = inelClass; - // Determine INEL class - bool inel0 = o2::pwglf::isINELgt0mc(particlesInCollision, pdg); - bool inel1 = o2::pwglf::isINELgt1mc(particlesInCollision, pdg); + ue.fill(HIST("INEL/hINELClass"), inelClass); - int inelClass = 0; - if (inel1) - inelClass = 2; - else if (inel0) - inelClass = 1; - mcCollisionToINELClass[mcCollId] = inelClass; + if (inel0) + mcINELgt0++; + if (inel1) + mcINELgt1++; + if (nGenCharged > 0) + mcWithParticles++; - ue.fill(HIST("INEL/hINELClass"), inelClass); + ue.fill(HIST("MC/EventLoss/NchGenerated"), nGenCharged); - if (inel0) - mcINELgt0++; - if (inel1) - mcINELgt1++; - if (nGenCharged > 0) - mcWithParticles++; + // Physics selection based on vertex and INEL cuts + bool physicsSelected = true; - ue.fill(HIST("MC/EventLoss/NchGenerated"), nGenCharged); + if (std::abs(mcCollision.posZ()) > cfgCutVertex.value) { + physicsSelected = false; + } - // Physics selection based on vertex and INEL cuts - bool physicsSelected = true; + // Apply INEL cut based on configuration + if (cfgINELCut.value == 1 && !inel0) { + physicsSelected = false; + } + if (cfgINELCut.value == 2 && !inel1) { + physicsSelected = false; + } - if (std::abs(mcCollision.posZ()) > cfgCutVertex.value) { - physicsSelected = false; - } + if (physicsSelected) { + physicsSelectedMCCollisions.insert(mcCollId); + ue.fill(HIST("MC/EventLoss/NchGenerated_PhysicsSelected"), nGenCharged); - // Apply INEL cut based on configuration - if (cfgINELCut.value == 1 && !inel0) { - physicsSelected = false; + if (inel0) { + ue.fill(HIST("MC/GenRecoCollisions"), 3.f); + } + if (inel1) { + ue.fill(HIST("MC/GenRecoCollisions"), 4.f); + } + } } - if (cfgINELCut.value == 2 && !inel1) { - physicsSelected = false; + + LOG(info) << "\n--- FIRST PASS SUMMARY ---"; + LOG(info) << "Total MC collisions processed: " << mcCollisions.size(); + LOG(info) << "MC collisions with particles: " << mcWithParticles; + LOG(info) << "INEL0: " << (mcCollisions.size() - mcINELgt0); + LOG(info) << "INEL>0: " << mcINELgt0; + LOG(info) << "INEL>1: " << mcINELgt1; + LOG(info) << "Physics-selected MC collisions: " << physicsSelectedMCCollisions.size(); + + //=========================================================================== + // Build maps for labels and centrality + //=========================================================================== + std::map recoToMcMap; + std::map recoToCentMap; + + size_t nCollisions = collisions.size(); + + // Associate labels with collisions by index + size_t iLabel = 0; + for (const auto& label : labels) { + if (iLabel < nCollisions) { + const auto& collision = collisions.iteratorAt(iLabel); + int64_t recoCollId = collision.globalIndex(); + int64_t mcCollId = label.mcCollisionId(); + recoToMcMap[recoCollId] = mcCollId; + } + iLabel++; } - if (physicsSelected) { - physicsSelectedMCCollisions.insert(mcCollId); - ue.fill(HIST("MC/EventLoss/NchGenerated_PhysicsSelected"), nGenCharged); + // Associate centrality with collisions by index + size_t iCent = 0; + for (const auto& cent : centTable) { + if (iCent < nCollisions) { + const auto& collision = collisions.iteratorAt(iCent); + int64_t recoCollId = collision.globalIndex(); + float centValue = cent.centFT0M(); - if (inel0) { - ue.fill(HIST("MC/GenRecoCollisions"), 3.f); - } - if (inel1) { - ue.fill(HIST("MC/GenRecoCollisions"), 4.f); + // Fill raw centrality histogram + ue.fill(HIST("Centrality/hCentRaw"), centValue); + + recoToCentMap[recoCollId] = centValue; } + iCent++; } - } - LOG(info) << "\n--- FIRST PASS SUMMARY ---"; - LOG(info) << "Total MC collisions processed: " << mcCollisions.size(); - LOG(info) << "MC collisions with particles: " << mcWithParticles; - LOG(info) << "INEL0: " << (mcCollisions.size() - mcINELgt0); - LOG(info) << "INEL>0: " << mcINELgt0; - LOG(info) << "INEL>1: " << mcINELgt1; - LOG(info) << "Physics-selected MC collisions: " << physicsSelectedMCCollisions.size(); + LOG(info) << "\n--- MAP SIZES ---"; + LOG(info) << "recoToMcMap size: " << recoToMcMap.size(); + LOG(info) << "recoToCentMap size: " << recoToCentMap.size(); - //=========================================================================== - // Build maps for labels and centrality - //=========================================================================== - std::map recoToMcMap; - std::map recoToCentMap; - - size_t nCollisions = collisions.size(); - - // Associate labels with collisions by index - size_t iLabel = 0; - for (const auto& label : labels) { - if (iLabel < nCollisions) { - const auto& collision = collisions.iteratorAt(iLabel); - int64_t recoCollId = collision.globalIndex(); - int64_t mcCollId = label.mcCollisionId(); - recoToMcMap[recoCollId] = mcCollId; + //=========================================================================== + // DEBUG: Check correlation between centrality and multiplicity + //=========================================================================== + LOG(info) << "\n=== CENTRALITY VS MULTIPLICITY DEBUG ==="; + + // Create temporary vectors to check correlation + std::vector> centMultPairs; + for (const auto& collision : collisions) { + int64_t collId = collision.globalIndex(); + + auto mcIt = recoToMcMap.find(collId); + if (mcIt == recoToMcMap.end()) + continue; + + auto centIt = recoToCentMap.find(collId); + if (centIt == recoToCentMap.end()) + continue; + + auto nchIt = mcCollisionToNch.find(mcIt->second); + if (nchIt == mcCollisionToNch.end()) + continue; + + centMultPairs.push_back({centIt->second, nchIt->second}); } - iLabel++; - } - // Associate centrality with collisions by index - size_t iCent = 0; - for (const auto& cent : centTable) { - if (iCent < nCollisions) { - const auto& collision = collisions.iteratorAt(iCent); - int64_t recoCollId = collision.globalIndex(); - float centValue = cent.centFT0M(); + // Sort by centrality + std::sort(centMultPairs.begin(), centMultPairs.end()); - // Fill raw centrality histogram - ue.fill(HIST("Centrality/hCentRaw"), centValue); + LOG(info) << "Correlation between centrality and multiplicity:"; + LOG(info) << " If centrality is normal (0=central, 100=peripheral), multiplicity should decrease with centrality"; + LOG(info) << " If inverted (0=peripheral, 100=central), multiplicity should increase with centrality"; - recoToCentMap[recoCollId] = centValue; + // Print a few samples across the range + if (centMultPairs.size() > 10) { + for (size_t i = 0; i < centMultPairs.size(); i += centMultPairs.size() / 10) { + LOG(info) << " Cent: " << centMultPairs[i].first + << "%, Mult: " << centMultPairs[i].second; + } } - iCent++; - } - LOG(info) << "\n--- MAP SIZES ---"; - LOG(info) << "recoToMcMap size: " << recoToMcMap.size(); - LOG(info) << "recoToCentMap size: " << recoToCentMap.size(); + //=========================================================================== + // SECOND PASS: Process reconstructed collisions with detailed cut accounting + //=========================================================================== - //=========================================================================== - // DEBUG: Check correlation between centrality and multiplicity - //=========================================================================== - LOG(info) << "\n=== CENTRALITY VS MULTIPLICITY DEBUG ==="; + LOG(info) << "\n--- SECOND PASS: Processing reconstructed collisions ---"; - // Create temporary vectors to check correlation - std::vector> centMultPairs; - for (const auto& collision : collisions) { - int64_t collId = collision.globalIndex(); + std::set reconstructedMCCollisions; + std::set selectedMCCollisions; - auto mcIt = recoToMcMap.find(collId); - if (mcIt == recoToMcMap.end()) - continue; + int nRecoCollisions = 0; + int nSelectedEvents = 0; + int nRejectedEvents = 0; + int nNoMCMatch = 0; + int nNoCent = 0; + int nInvalidCent = 0; - auto centIt = recoToCentMap.find(collId); - if (centIt == recoToCentMap.end()) - continue; + // Cut counters + int nPassVertex = 0; + int nPassINEL = 0; + int nPassAll = 0; - auto nchIt = mcCollisionToNch.find(mcIt->second); - if (nchIt == mcCollisionToNch.end()) - continue; + // For mean calculations + std::vector centAll, centVertex, centINEL, centSelected; - centMultPairs.push_back({centIt->second, nchIt->second}); - } + for (const auto& collision : collisions) { + nRecoCollisions++; - // Sort by centrality - std::sort(centMultPairs.begin(), centMultPairs.end()); + int64_t collId = collision.globalIndex(); - LOG(info) << "Correlation between centrality and multiplicity:"; - LOG(info) << " If centrality is normal (0=central, 100=peripheral), multiplicity should decrease with centrality"; - LOG(info) << " If inverted (0=peripheral, 100=central), multiplicity should increase with centrality"; + // Fill cut flow + ue.fill(HIST("CutFlow/hCutStats"), 1); - // Print a few samples across the range - if (centMultPairs.size() > 10) { - for (size_t i = 0; i < centMultPairs.size(); i += centMultPairs.size() / 10) { - LOG(info) << " Cent: " << centMultPairs[i].first - << "%, Mult: " << centMultPairs[i].second; - } - } + // Get MC collision ID from labels map + auto mcIt = recoToMcMap.find(collId); + if (mcIt == recoToMcMap.end()) { + nNoMCMatch++; + continue; + } + ue.fill(HIST("CutFlow/hCutStats"), 2); - //=========================================================================== - // SECOND PASS: Process reconstructed collisions with detailed cut accounting - //=========================================================================== + int64_t mcCollId = mcIt->second; - LOG(info) << "\n--- SECOND PASS: Processing reconstructed collisions ---"; + // Get generated multiplicity for this MC collision + auto nchIt = mcCollisionToNch.find(mcCollId); + if (nchIt == mcCollisionToNch.end()) { + continue; + } - std::set reconstructedMCCollisions; - std::set selectedMCCollisions; + int nGenCharged = nchIt->second; - int nRecoCollisions = 0; - int nSelectedEvents = 0; - int nRejectedEvents = 0; - int nNoMCMatch = 0; - int nNoCent = 0; - int nInvalidCent = 0; + // Get INEL class + auto inelIt = mcCollisionToINELClass.find(mcCollId); + int inelClass = (inelIt != mcCollisionToINELClass.end()) ? inelIt->second : 0; - // Cut counters - int nPassVertex = 0; - int nPassINEL = 0; - int nPassAll = 0; + // Get centrality from cent map + auto centIt = recoToCentMap.find(collId); + if (centIt == recoToCentMap.end()) { + nNoCent++; + continue; + } + ue.fill(HIST("CutFlow/hCutStats"), 3); - // For mean calculations - std::vector centAll, centVertex, centINEL, centSelected; + float cent = centIt->second; + if (cent < 0 || cent > 100) { + nInvalidCent++; + continue; + } - for (const auto& collision : collisions) { - nRecoCollisions++; + // Store all events with valid info + centAll.push_back(cent); + ue.fill(HIST("Centrality/hCentVsMult"), cent, nGenCharged); + ue.fill(HIST("Centrality/hMultVsCent"), nGenCharged, cent); + ue.fill(HIST("Centrality/hCentVsVz"), cent, collision.posZ()); + ue.fill(HIST("INEL/hINELVsCent"), cent, inelClass); + + // Track cuts progressively + bool passVertex = std::abs(collision.posZ()) <= cfgCutVertex.value; + if (passVertex) { + centVertex.push_back(cent); + ue.fill(HIST("Centrality/hCentAfterVtx"), cent); + ue.fill(HIST("CutFlow/hCutStats"), 4); + ue.fill(HIST("CutFlow/hCentPerCut"), 4, cent); + nPassVertex++; + } - int64_t collId = collision.globalIndex(); + // Check INEL selection at generator level + bool passINEL = true; + if (cfgINELCut.value == 1 && inelClass < 1) + passINEL = false; + if (cfgINELCut.value == 2 && inelClass < 2) + passINEL = false; + + if (passINEL) { + centINEL.push_back(cent); + ue.fill(HIST("Centrality/hCentAfterINEL"), cent); + ue.fill(HIST("CutFlow/hCutStats"), 5); + ue.fill(HIST("CutFlow/hCentPerCut"), 5, cent); + nPassINEL++; + } - // Fill cut flow - ue.fill(HIST("CutFlow/hCutStats"), 1); + // Fill GenMultVsCent for all reconstructed events + ue.fill(HIST("MC/EventLoss/GenMultVsCent"), cent, nGenCharged); + ue.fill(HIST("MC/EventLoss/NchGenerated_Reconstructed"), nGenCharged); - // Get MC collision ID from labels map - auto mcIt = recoToMcMap.find(collId); - if (mcIt == recoToMcMap.end()) { - nNoMCMatch++; - continue; - } - ue.fill(HIST("CutFlow/hCutStats"), 2); + reconstructedMCCollisions.insert(mcCollId); - int64_t mcCollId = mcIt->second; + // Apply all cuts + bool passedAll = passVertex && passINEL; - // Get generated multiplicity for this MC collision - auto nchIt = mcCollisionToNch.find(mcCollId); - if (nchIt == mcCollisionToNch.end()) { - continue; - } + if (!passedAll) { + ue.fill(HIST("MC/EventLoss/GenMultVsCent_Rejected"), cent, nGenCharged); + nRejectedEvents++; + continue; + } - int nGenCharged = nchIt->second; + // Event passed all selections + centSelected.push_back(cent); + ue.fill(HIST("Centrality/hCentAfterAll"), cent); + ue.fill(HIST("CutFlow/hCutStats"), 6); + ue.fill(HIST("CutFlow/hCentPerCut"), 6, cent); + ue.fill(HIST("MC/EventLoss/GenMultVsCent_Selected"), cent, nGenCharged); + ue.fill(HIST("hvtxZ"), collision.posZ()); + selectedMCCollisions.insert(mcCollId); + nSelectedEvents++; + nPassAll++; + + // Process tracks in selected events + int nTracksInEvent = 0; + for (const auto& track : tracks) { + if (!track.has_collision()) + continue; + if (track.collisionId() != collId) + continue; - // Get INEL class - auto inelIt = mcCollisionToINELClass.find(mcCollId); - int inelClass = (inelIt != mcCollisionToINELClass.end()) ? inelIt->second : 0; + if (!passesTrackSelection(track)) { + continue; + } + nTracksInEvent++; + + // Fill TPC cluster histograms + ue.fill(HIST("hNclFoundTPC"), track.tpcNClsFound()); + ue.fill(HIST("hNclPIDTPC"), track.tpcNClsPID()); + ue.fill(HIST("hNclFoundTPCvsPt"), track.pt(), track.tpcNClsFound()); + ue.fill(HIST("hNclPIDTPCvsPt"), track.pt(), track.tpcNClsPID()); + + ue.fill(HIST("Inclusive/hPtAllReco"), track.pt()); + ue.fill(HIST("Inclusive/hPtMeasuredVsCent"), track.pt(), cent); + ue.fill(HIST("hEta"), track.eta()); + ue.fill(HIST("hPhi"), track.phi()); + + if (track.has_mcParticle()) { + const auto& particle = track.mcParticle(); + int pdgCode = std::abs(particle.pdgCode()); + + if (particle.isPhysicalPrimary()) { + ue.fill(HIST("Inclusive/hPtNumEff"), particle.pt()); + ue.fill(HIST("Inclusive/hPtPrimReco"), track.pt()); + + if (pdgCode == PDGPion) { + ue.fill(HIST("Pion/hPtNumEff"), particle.pt()); + ue.fill(HIST("Pion/hPtPrimReco"), track.pt()); + } else if (pdgCode == PDGKaon) { + ue.fill(HIST("Kaon/hPtNumEff"), particle.pt()); + ue.fill(HIST("Kaon/hPtPrimReco"), track.pt()); + } else if (pdgCode == PDGProton) { + ue.fill(HIST("Proton/hPtNumEff"), particle.pt()); + ue.fill(HIST("Proton/hPtPrimReco"), track.pt()); + } + } else { + ue.fill(HIST("Inclusive/hPtSecReco"), track.pt()); + + if (pdgCode == PDGPion) { + ue.fill(HIST("Pion/hPtSecReco"), track.pt()); + } else if (pdgCode == PDGKaon) { + ue.fill(HIST("Kaon/hPtSecReco"), track.pt()); + } else if (pdgCode == PDGProton) { + ue.fill(HIST("Proton/hPtSecReco"), track.pt()); + } + } + } - // Get centrality from cent map - auto centIt = recoToCentMap.find(collId); - if (centIt == recoToCentMap.end()) { - nNoCent++; - continue; - } - ue.fill(HIST("CutFlow/hCutStats"), 3); + int bestSpecies = getBestPIDHypothesis(track); - float cent = centIt->second; - if (cent < 0 || cent > 100) { - nInvalidCent++; - continue; - } + if (bestSpecies == kPion) { + ue.fill(HIST("Pion/hPtMeasuredVsCent"), track.pt(), cent); + ue.fill(HIST("Pion/hPtAllReco"), track.pt()); - // Store all events with valid info - centAll.push_back(cent); - ue.fill(HIST("Centrality/hCentVsMult"), cent, nGenCharged); - ue.fill(HIST("Centrality/hMultVsCent"), nGenCharged, cent); - ue.fill(HIST("Centrality/hCentVsVz"), cent, collision.posZ()); - ue.fill(HIST("INEL/hINELVsCent"), cent, inelClass); - - // Track cuts progressively - bool passVertex = std::abs(collision.posZ()) <= cfgCutVertex.value; - if (passVertex) { - centVertex.push_back(cent); - ue.fill(HIST("Centrality/hCentAfterVtx"), cent); - ue.fill(HIST("CutFlow/hCutStats"), 4); - ue.fill(HIST("CutFlow/hCentPerCut"), 4, cent); - nPassVertex++; - } + if (enablePIDHistograms) { + ue.fill(HIST("Pion/hNsigmaTPC"), track.pt(), track.tpcNSigmaPi()); + } + } else if (bestSpecies == kKaon) { + ue.fill(HIST("Kaon/hPtMeasuredVsCent"), track.pt(), cent); + ue.fill(HIST("Kaon/hPtAllReco"), track.pt()); + + if (enablePIDHistograms) { + ue.fill(HIST("Kaon/hNsigmaTPC"), track.pt(), track.tpcNSigmaKa()); + } + } else if (bestSpecies == kProton) { + ue.fill(HIST("Proton/hPtMeasuredVsCent"), track.pt(), cent); + ue.fill(HIST("Proton/hPtAllReco"), track.pt()); - // Check INEL selection at generator level - bool passINEL = true; - if (cfgINELCut.value == 1 && inelClass < 1) - passINEL = false; - if (cfgINELCut.value == 2 && inelClass < 2) - passINEL = false; - - if (passINEL) { - centINEL.push_back(cent); - ue.fill(HIST("Centrality/hCentAfterINEL"), cent); - ue.fill(HIST("CutFlow/hCutStats"), 5); - ue.fill(HIST("CutFlow/hCentPerCut"), 5, cent); - nPassINEL++; + if (enablePIDHistograms) { + ue.fill(HIST("Proton/hNsigmaTPC"), track.pt(), track.tpcNSigmaPr()); + } + } + } + + // Fill event-level track multiplicity + ue.fill(HIST("Centrality/hRecoMultVsCent"), cent, nTracksInEvent); + } + + // Calculate and display cut statistics + LOG(info) << "\n=== CUT STATISTICS ==="; + LOG(info) << "Total collisions with valid info: " << centAll.size(); + LOG(info) << "Pass vertex cut: " << nPassVertex << " (" + << (centAll.size() > 0 ? 100.0 * nPassVertex / centAll.size() : 0.0) << "%)"; + LOG(info) << "Pass INEL cut: " << nPassINEL << " (" + << (centAll.size() > 0 ? 100.0 * nPassINEL / centAll.size() : 0.0) << "%)"; + LOG(info) << "Pass all cuts: " << nPassAll << " (" + << (centAll.size() > 0 ? 100.0 * nPassAll / centAll.size() : 0.0) << "%)"; + + // Calculate mean centrality at each stage + if (!centAll.empty()) { + float meanAll = std::accumulate(centAll.begin(), centAll.end(), 0.0) / centAll.size(); + float meanVertex = centVertex.empty() ? 0 : std::accumulate(centVertex.begin(), centVertex.end(), 0.0) / centVertex.size(); + float meanINEL = centINEL.empty() ? 0 : std::accumulate(centINEL.begin(), centINEL.end(), 0.0) / centINEL.size(); + float meanSelected = centSelected.empty() ? 0 : std::accumulate(centSelected.begin(), centSelected.end(), 0.0) / centSelected.size(); + + LOG(info) << "\n=== CENTRALITY MEANS ==="; + LOG(info) << "Mean centrality (all): " << meanAll; + LOG(info) << "Mean centrality (after vertex): " << meanVertex; + LOG(info) << "Mean centrality (after INEL): " << meanINEL; + LOG(info) << "Mean centrality (selected): " << meanSelected; + } + + ue.fill(HIST("hEventLossBreakdown"), 1.f, physicsSelectedMCCollisions.size()); + ue.fill(HIST("hEventLossBreakdown"), 2.f, reconstructedMCCollisions.size()); + ue.fill(HIST("hEventLossBreakdown"), 3.f, selectedMCCollisions.size()); + + float efficiency = physicsSelectedMCCollisions.size() > 0 ? 100.f * selectedMCCollisions.size() / physicsSelectedMCCollisions.size() : 0; + ue.fill(HIST("hEventLossBreakdown"), 4.f, efficiency); + + LOG(info) << "\n=== FINAL EFFICIENCY ==="; + LOG(info) << "Physics selected: " << physicsSelectedMCCollisions.size(); + LOG(info) << "Reconstructed: " << reconstructedMCCollisions.size(); + LOG(info) << "Selected: " << selectedMCCollisions.size(); + LOG(info) << "Efficiency: " << efficiency << "%"; + LOG(info) << "=== processMC END ==="; + LOG(info) << "=== Initialized MultiplicityPt task with ON-THE-FLY PERCENTILE COMPUTATION ==="; + LOG(info) << "Centrality classes: " << kCentralityClasses; + LOG(info) << "Multiplicity estimator: " << multiplicityEstimator.value; + LOG(info) << "IMPORTANT: Run processPercentileCalibration FIRST to build percentile boundaries!"; + if (applyPhiCut.value) { + LOG(info) << "Phi cut ENABLED for pT > " << pTthresholdPhiCut.value << " GeV/c"; } + } + + // ======================================================================== + // PERCENTILE CALIBRATION PASS + // ======================================================================== + void MultiplicityPt::processPercentileCalibration(CollisionTableMCTrue const& mcCollisions, + ParticleTableMC const& particles) + { + LOG(info) << "=== PERCENTILE CALIBRATION PASS ==="; + LOG(info) << "Processing " << mcCollisions.size() << " MC collisions"; + + multiplicityValues.clear(); + multiplicityValues.reserve(mcCollisions.size()); + + for (const auto& mcCollision : mcCollisions) { + // Apply basic cuts + if (std::abs(mcCollision.posZ()) > cfgCutVertex.value) + continue; - // Fill GenMultVsCent for all reconstructed events - ue.fill(HIST("MC/EventLoss/GenMultVsCent"), cent, nGenCharged); - ue.fill(HIST("MC/EventLoss/NchGenerated_Reconstructed"), nGenCharged); + auto particlesInCollision = particles.sliceBy(perMCCol, mcCollision.globalIndex()); - reconstructedMCCollisions.insert(mcCollId); + // Apply INEL cuts + if (cfgINELCut.value == 1 && !o2::pwglf::isINELgt0mc(particlesInCollision, pdg)) + continue; + if (cfgINELCut.value == 2 && !o2::pwglf::isINELgt1mc(particlesInCollision, pdg)) + continue; - // Apply all cuts - bool passedAll = passVertex && passINEL; + // Calculate multiplicity + float mcMult = getMultiplicityMC(mcCollision, particles); + multiplicityValues.push_back(mcMult); - if (!passedAll) { - ue.fill(HIST("MC/EventLoss/GenMultVsCent_Rejected"), cent, nGenCharged); - nRejectedEvents++; - continue; + ue.fill(HIST("Calibration/hRawMultiplicity"), mcMult); } - // Event passed all selections - centSelected.push_back(cent); - ue.fill(HIST("Centrality/hCentAfterAll"), cent); - ue.fill(HIST("CutFlow/hCutStats"), 6); - ue.fill(HIST("CutFlow/hCentPerCut"), 6, cent); - ue.fill(HIST("MC/EventLoss/GenMultVsCent_Selected"), cent, nGenCharged); + // Compute percentile boundaries + computePercentileBoundaries(); + + LOG(info) << "=== PERCENTILE CALIBRATION COMPLETE ==="; + LOG(info) << "Processed " << multiplicityValues.size() << " events"; + LOG(info) << "Now run processMC and processTrue with these percentiles"; + } + + // ======================================================================== + // DATA PROCESSING + // ======================================================================== + void MultiplicityPt::processData(CollisionTableData::iterator const& collision, + TrackTableData const& tracks, + BCsRun3 const& /*bcs*/) + { + if (!isEventSelected(collision)) { + return; + } ue.fill(HIST("hvtxZ"), collision.posZ()); - selectedMCCollisions.insert(mcCollId); - nSelectedEvents++; - nPassAll++; - // Process tracks in selected events - int nTracksInEvent = 0; + float magField = 0; + if (applyPhiCut.value) { + const auto& bc = collision.bc_as(); + magField = getMagneticField(bc.timestamp()); + } + for (const auto& track : tracks) { - if (!track.has_collision()) - continue; - if (track.collisionId() != collId) - continue; + if (applyPhiCut.value && track.pt() >= pTthresholdPhiCut.value) { + float phiPrime = getTransformedPhi(track.phi(), track.sign(), magField); + ue.fill(HIST("PhiCut/hPtVsPhiPrimeBefore"), track.pt(), phiPrime); + } - if (!passesTrackSelection(track)) { + if (!passesTrackSelection(track, magField)) { continue; } - nTracksInEvent++; - // Fill TPC cluster histograms - ue.fill(HIST("hNclFoundTPC"), track.tpcNClsFound()); - ue.fill(HIST("hNclPIDTPC"), track.tpcNClsPID()); - ue.fill(HIST("hNclFoundTPCvsPt"), track.pt(), track.tpcNClsFound()); - ue.fill(HIST("hNclPIDTPCvsPt"), track.pt(), track.tpcNClsPID()); + if (applyPhiCut.value && track.pt() >= pTthresholdPhiCut.value) { + float phiPrime = getTransformedPhi(track.phi(), track.sign(), magField); + ue.fill(HIST("PhiCut/hPtVsPhiPrimeAfter"), track.pt(), phiPrime); + } - ue.fill(HIST("Inclusive/hPtAllReco"), track.pt()); - ue.fill(HIST("Inclusive/hPtMeasuredVsCent"), track.pt(), cent); + ue.fill(HIST("Inclusive/hPtMeasured"), track.pt()); ue.fill(HIST("hEta"), track.eta()); ue.fill(HIST("hPhi"), track.phi()); - if (track.has_mcParticle()) { - const auto& particle = track.mcParticle(); - int pdgCode = std::abs(particle.pdgCode()); - - if (particle.isPhysicalPrimary()) { - ue.fill(HIST("Inclusive/hPtNumEff"), particle.pt()); - ue.fill(HIST("Inclusive/hPtPrimReco"), track.pt()); - - if (pdgCode == PDGPion) { - ue.fill(HIST("Pion/hPtNumEff"), particle.pt()); - ue.fill(HIST("Pion/hPtPrimReco"), track.pt()); - } else if (pdgCode == PDGKaon) { - ue.fill(HIST("Kaon/hPtNumEff"), particle.pt()); - ue.fill(HIST("Kaon/hPtPrimReco"), track.pt()); - } else if (pdgCode == PDGProton) { - ue.fill(HIST("Proton/hPtNumEff"), particle.pt()); - ue.fill(HIST("Proton/hPtPrimReco"), track.pt()); - } - } else { - ue.fill(HIST("Inclusive/hPtSecReco"), track.pt()); - - if (pdgCode == PDGPion) { - ue.fill(HIST("Pion/hPtSecReco"), track.pt()); - } else if (pdgCode == PDGKaon) { - ue.fill(HIST("Kaon/hPtSecReco"), track.pt()); - } else if (pdgCode == PDGProton) { - ue.fill(HIST("Proton/hPtSecReco"), track.pt()); - } - } - } - int bestSpecies = getBestPIDHypothesis(track); if (bestSpecies == kPion) { - ue.fill(HIST("Pion/hPtMeasuredVsCent"), track.pt(), cent); - ue.fill(HIST("Pion/hPtAllReco"), track.pt()); - + ue.fill(HIST("Pion/hPtMeasured"), track.pt()); if (enablePIDHistograms) { ue.fill(HIST("Pion/hNsigmaTPC"), track.pt(), track.tpcNSigmaPi()); } } else if (bestSpecies == kKaon) { - ue.fill(HIST("Kaon/hPtMeasuredVsCent"), track.pt(), cent); - ue.fill(HIST("Kaon/hPtAllReco"), track.pt()); - + ue.fill(HIST("Kaon/hPtMeasured"), track.pt()); if (enablePIDHistograms) { ue.fill(HIST("Kaon/hNsigmaTPC"), track.pt(), track.tpcNSigmaKa()); } } else if (bestSpecies == kProton) { - ue.fill(HIST("Proton/hPtMeasuredVsCent"), track.pt(), cent); - ue.fill(HIST("Proton/hPtAllReco"), track.pt()); - + ue.fill(HIST("Proton/hPtMeasured"), track.pt()); if (enablePIDHistograms) { ue.fill(HIST("Proton/hNsigmaTPC"), track.pt(), track.tpcNSigmaPr()); } } } - - // Fill event-level track multiplicity - ue.fill(HIST("Centrality/hRecoMultVsCent"), cent, nTracksInEvent); } - // Calculate and display cut statistics - LOG(info) << "\n=== CUT STATISTICS ==="; - LOG(info) << "Total collisions with valid info: " << centAll.size(); - LOG(info) << "Pass vertex cut: " << nPassVertex << " (" - << (centAll.size() > 0 ? 100.0 * nPassVertex / centAll.size() : 0.0) << "%)"; - LOG(info) << "Pass INEL cut: " << nPassINEL << " (" - << (centAll.size() > 0 ? 100.0 * nPassINEL / centAll.size() : 0.0) << "%)"; - LOG(info) << "Pass all cuts: " << nPassAll << " (" - << (centAll.size() > 0 ? 100.0 * nPassAll / centAll.size() : 0.0) << "%)"; - - // Calculate mean centrality at each stage - if (!centAll.empty()) { - float meanAll = std::accumulate(centAll.begin(), centAll.end(), 0.0) / centAll.size(); - float meanVertex = centVertex.empty() ? 0 : std::accumulate(centVertex.begin(), centVertex.end(), 0.0) / centVertex.size(); - float meanINEL = centINEL.empty() ? 0 : std::accumulate(centINEL.begin(), centINEL.end(), 0.0) / centINEL.size(); - float meanSelected = centSelected.empty() ? 0 : std::accumulate(centSelected.begin(), centSelected.end(), 0.0) / centSelected.size(); - - LOG(info) << "\n=== CENTRALITY MEANS ==="; - LOG(info) << "Mean centrality (all): " << meanAll; - LOG(info) << "Mean centrality (after vertex): " << meanVertex; - LOG(info) << "Mean centrality (after INEL): " << meanINEL; - LOG(info) << "Mean centrality (selected): " << meanSelected; - } - - ue.fill(HIST("hEventLossBreakdown"), 1.f, physicsSelectedMCCollisions.size()); - ue.fill(HIST("hEventLossBreakdown"), 2.f, reconstructedMCCollisions.size()); - ue.fill(HIST("hEventLossBreakdown"), 3.f, selectedMCCollisions.size()); - - float efficiency = physicsSelectedMCCollisions.size() > 0 ? 100.f * selectedMCCollisions.size() / physicsSelectedMCCollisions.size() : 0; - ue.fill(HIST("hEventLossBreakdown"), 4.f, efficiency); - - LOG(info) << "\n=== FINAL EFFICIENCY ==="; - LOG(info) << "Physics selected: " << physicsSelectedMCCollisions.size(); - LOG(info) << "Reconstructed: " << reconstructedMCCollisions.size(); - LOG(info) << "Selected: " << selectedMCCollisions.size(); - LOG(info) << "Efficiency: " << efficiency << "%"; - LOG(info) << "=== processMC END ==="; - LOG(info) << "=== Initialized MultiplicityPt task with ON-THE-FLY PERCENTILE COMPUTATION ==="; - LOG(info) << "Centrality classes: " << kCentralityClasses; - LOG(info) << "Multiplicity estimator: " << multiplicityEstimator.value; - LOG(info) << "IMPORTANT: Run processPercentileCalibration FIRST to build percentile boundaries!"; - if (applyPhiCut.value) { - LOG(info) << "Phi cut ENABLED for pT > " << pTthresholdPhiCut.value << " GeV/c"; - } -} - -// ======================================================================== -// PERCENTILE CALIBRATION PASS -// ======================================================================== -void MultiplicityPt::processPercentileCalibration(CollisionTableMCTrue const& mcCollisions, - ParticleTableMC const& particles) -{ - LOG(info) << "=== PERCENTILE CALIBRATION PASS ==="; - LOG(info) << "Processing " << mcCollisions.size() << " MC collisions"; - - multiplicityValues.clear(); - multiplicityValues.reserve(mcCollisions.size()); - - for (const auto& mcCollision : mcCollisions) { - // Apply basic cuts - if (std::abs(mcCollision.posZ()) > cfgCutVertex.value) - continue; - - auto particlesInCollision = particles.sliceBy(perMCCol, mcCollision.globalIndex()); - - // Apply INEL cuts - if (cfgINELCut.value == 1 && !o2::pwglf::isINELgt0mc(particlesInCollision, pdg)) - continue; - if (cfgINELCut.value == 2 && !o2::pwglf::isINELgt1mc(particlesInCollision, pdg)) - continue; - - // Calculate multiplicity - float mcMult = getMultiplicityMC(mcCollision, particles); - multiplicityValues.push_back(mcMult); - - ue.fill(HIST("Calibration/hRawMultiplicity"), mcMult); - } + // ======================================================================== + // MC PROCESSING - Using computed percentiles + // ======================================================================== + void MultiplicityPt::processMC(TrackTableMC const& tracks, + aod::McParticles const& particles, + CollisionTableMCTrue const& mcCollisions, + CollisionTableMC const& collisions, + BCsRun3 const& /*bcs*/) + { + if (!percentilesComputed) { + LOG(warning) << "Percentiles not computed yet! Run processPercentileCalibration first!"; + LOG(warning) << "Using fallback linear binning for now..."; + } - // Compute percentile boundaries - computePercentileBoundaries(); + LOG(info) << "=== DEBUG processMC START ==="; + LOG(info) << "MC collisions: " << mcCollisions.size(); + LOG(info) << "Reconstructed collisions: " << collisions.size(); - LOG(info) << "=== PERCENTILE CALIBRATION COMPLETE ==="; - LOG(info) << "Processed " << multiplicityValues.size() << " events"; - LOG(info) << "Now run processMC and processTrue with these percentiles"; -} + ue.fill(HIST("MC/GenRecoCollisions"), 1.f, mcCollisions.size()); + ue.fill(HIST("MC/GenRecoCollisions"), 2.f, collisions.size()); -// ======================================================================== -// DATA PROCESSING -// ======================================================================== -void MultiplicityPt::processData(CollisionTableData::iterator const& collision, - TrackTableData const& tracks, - BCsRun3 const& /*bcs*/) -{ - if (!isEventSelected(collision)) { - return; - } - ue.fill(HIST("hvtxZ"), collision.posZ()); + std::set physicsSelectedMCCollisions; + std::set reconstructedMCCollisions; + std::set selectedMCCollisions; - float magField = 0; - if (applyPhiCut.value) { - const auto& bc = collision.bc_as(); - magField = getMagneticField(bc.timestamp()); - } + std::map mcCollisionMultiplicity; + std::map mcCollisionPercentile; - for (const auto& track : tracks) { - if (applyPhiCut.value && track.pt() >= pTthresholdPhiCut.value) { - float phiPrime = getTransformedPhi(track.phi(), track.sign(), magField); - ue.fill(HIST("PhiCut/hPtVsPhiPrimeBefore"), track.pt(), phiPrime); - } + // First pass: classify MC collisions + for (const auto& mcCollision : mcCollisions) { + int64_t mcCollId = mcCollision.globalIndex(); - if (!passesTrackSelection(track, magField)) { - continue; - } + float mcMult = getMultiplicityMC(mcCollision, particles); + mcCollisionMultiplicity[mcCollId] = mcMult; - if (applyPhiCut.value && track.pt() >= pTthresholdPhiCut.value) { - float phiPrime = getTransformedPhi(track.phi(), track.sign(), magField); - ue.fill(HIST("PhiCut/hPtVsPhiPrimeAfter"), track.pt(), phiPrime); - } + // Convert to percentile + float percentile = multiplicityToPercentile(mcMult); + mcCollisionPercentile[mcCollId] = percentile; - ue.fill(HIST("Inclusive/hPtMeasured"), track.pt()); - ue.fill(HIST("hEta"), track.eta()); - ue.fill(HIST("hPhi"), track.phi()); + ue.fill(HIST("MC/EventLoss/MultGenerated"), percentile); - int bestSpecies = getBestPIDHypothesis(track); + auto particlesInCollision = particles.sliceBy(perMCCol, mcCollId); - if (bestSpecies == kPion) { - ue.fill(HIST("Pion/hPtMeasured"), track.pt()); - if (enablePIDHistograms) { - ue.fill(HIST("Pion/hNsigmaTPC"), track.pt(), track.tpcNSigmaPi()); + if (std::abs(mcCollision.posZ()) > cfgCutVertex.value) { + ue.fill(HIST("MC/EventLoss/MultBadVertex"), percentile); + continue; } - } else if (bestSpecies == kKaon) { - ue.fill(HIST("Kaon/hPtMeasured"), track.pt()); - if (enablePIDHistograms) { - ue.fill(HIST("Kaon/hNsigmaTPC"), track.pt(), track.tpcNSigmaKa()); + + if (cfgINELCut.value == 1 && !o2::pwglf::isINELgt0mc(particlesInCollision, pdg)) { + continue; } - } else if (bestSpecies == kProton) { - ue.fill(HIST("Proton/hPtMeasured"), track.pt()); - if (enablePIDHistograms) { - ue.fill(HIST("Proton/hNsigmaTPC"), track.pt(), track.tpcNSigmaPr()); + if (cfgINELCut.value == 2 && !o2::pwglf::isINELgt1mc(particlesInCollision, pdg)) { + continue; } + + physicsSelectedMCCollisions.insert(mcCollId); + ue.fill(HIST("MC/EventLoss/MultPhysicsSelected"), percentile); } - } -} -// ======================================================================== -// MC PROCESSING - Using computed percentiles -// ======================================================================== -void MultiplicityPt::processMC(TrackTableMC const& tracks, - aod::McParticles const& particles, - CollisionTableMCTrue const& mcCollisions, - CollisionTableMC const& collisions, - BCsRun3 const& /*bcs*/) -{ - if (!percentilesComputed) { - LOG(warning) << "Percentiles not computed yet! Run processPercentileCalibration first!"; - LOG(warning) << "Using fallback linear binning for now..."; - } + LOG(info) << "Physics-selected MC collisions: " << physicsSelectedMCCollisions.size(); - LOG(info) << "=== DEBUG processMC START ==="; - LOG(info) << "MC collisions: " << mcCollisions.size(); - LOG(info) << "Reconstructed collisions: " << collisions.size(); + // Second pass: track reconstructed events + std::set selectedCollisionIndices; - ue.fill(HIST("MC/GenRecoCollisions"), 1.f, mcCollisions.size()); - ue.fill(HIST("MC/GenRecoCollisions"), 2.f, collisions.size()); + for (const auto& collision : collisions) { + if (!collision.has_mcCollision()) { + continue; + } - std::set physicsSelectedMCCollisions; - std::set reconstructedMCCollisions; - std::set selectedMCCollisions; + const auto& mcCollision = collision.mcCollision_as(); + int64_t mcCollId = mcCollision.globalIndex(); - std::map mcCollisionMultiplicity; - std::map mcCollisionPercentile; + if (physicsSelectedMCCollisions.find(mcCollId) == physicsSelectedMCCollisions.end()) { + continue; + } - // First pass: classify MC collisions - for (const auto& mcCollision : mcCollisions) { - int64_t mcCollId = mcCollision.globalIndex(); + float percentile = mcCollisionPercentile[mcCollId]; - float mcMult = getMultiplicityMC(mcCollision, particles); - mcCollisionMultiplicity[mcCollId] = mcMult; + if (reconstructedMCCollisions.find(mcCollId) == reconstructedMCCollisions.end()) { + reconstructedMCCollisions.insert(mcCollId); + ue.fill(HIST("MC/EventLoss/MultReconstructed"), percentile); + } - // Convert to percentile - float percentile = multiplicityToPercentile(mcMult); - mcCollisionPercentile[mcCollId] = percentile; + if (isEventSelected(collision)) { + if (selectedMCCollisions.find(mcCollId) == selectedMCCollisions.end()) { + selectedMCCollisions.insert(mcCollId); + ue.fill(HIST("MC/EventLoss/MultRecoSelected"), percentile); + } + selectedCollisionIndices.insert(collision.globalIndex()); + ue.fill(HIST("hvtxZ"), collision.posZ()); + } + } - ue.fill(HIST("MC/EventLoss/MultGenerated"), percentile); + LOG(info) << "Reconstructed MC collisions: " << reconstructedMCCollisions.size(); + LOG(info) << "Selected MC collisions: " << selectedMCCollisions.size(); - auto particlesInCollision = particles.sliceBy(perMCCol, mcCollId); + int nPhysicsSelected = physicsSelectedMCCollisions.size(); + int nReconstructed = reconstructedMCCollisions.size(); + int nSelected = selectedMCCollisions.size(); - if (std::abs(mcCollision.posZ()) > cfgCutVertex.value) { - ue.fill(HIST("MC/EventLoss/MultBadVertex"), percentile); - continue; + if (nPhysicsSelected > 0) { + ue.fill(HIST("hEventLossBreakdown"), 1, nPhysicsSelected); + ue.fill(HIST("hEventLossBreakdown"), 2, nReconstructed); + ue.fill(HIST("hEventLossBreakdown"), 3, nSelected); + ue.fill(HIST("hEventLossBreakdown"), 4, (nSelected * 100.0 / nPhysicsSelected)); } - if (cfgINELCut.value == 1 && !o2::pwglf::isINELgt0mc(particlesInCollision, pdg)) { - continue; - } - if (cfgINELCut.value == 2 && !o2::pwglf::isINELgt1mc(particlesInCollision, pdg)) { - continue; - } + // Process tracks + int totalTracksProcessed = 0; + int tracksFromSelectedEvents = 0; + int tracksPassingSelection = 0; - physicsSelectedMCCollisions.insert(mcCollId); - ue.fill(HIST("MC/EventLoss/MultPhysicsSelected"), percentile); - } + std::array particleTracksIdentified = {0}; + std::array particleTracksPrimary = {0}; + std::array particleTracksSecondary = {0}; - LOG(info) << "Physics-selected MC collisions: " << physicsSelectedMCCollisions.size(); + for (const auto& track : tracks) { + totalTracksProcessed++; - // Second pass: track reconstructed events - std::set selectedCollisionIndices; + if (!track.has_collision()) + continue; - for (const auto& collision : collisions) { - if (!collision.has_mcCollision()) { - continue; - } + const auto& collision = track.collision_as(); - const auto& mcCollision = collision.mcCollision_as(); - int64_t mcCollId = mcCollision.globalIndex(); + if (selectedCollisionIndices.find(collision.globalIndex()) == selectedCollisionIndices.end()) { + continue; + } + tracksFromSelectedEvents++; - if (physicsSelectedMCCollisions.find(mcCollId) == physicsSelectedMCCollisions.end()) { - continue; - } + if (!collision.has_mcCollision()) + continue; - float percentile = mcCollisionPercentile[mcCollId]; + const auto& mcCollision = collision.mcCollision_as(); + float percentile = mcCollisionPercentile[mcCollision.globalIndex()]; - if (reconstructedMCCollisions.find(mcCollId) == reconstructedMCCollisions.end()) { - reconstructedMCCollisions.insert(mcCollId); - ue.fill(HIST("MC/EventLoss/MultReconstructed"), percentile); - } + float magField = 0; + if (applyPhiCut.value) { + const auto& bc = collision.bc_as(); + magField = getMagneticField(bc.timestamp()); + } - if (isEventSelected(collision)) { - if (selectedMCCollisions.find(mcCollId) == selectedMCCollisions.end()) { - selectedMCCollisions.insert(mcCollId); - ue.fill(HIST("MC/EventLoss/MultRecoSelected"), percentile); + if (!passesTrackSelection(track, magField)) { + continue; } - selectedCollisionIndices.insert(collision.globalIndex()); - ue.fill(HIST("hvtxZ"), collision.posZ()); - } - } + tracksPassingSelection++; - LOG(info) << "Reconstructed MC collisions: " << reconstructedMCCollisions.size(); - LOG(info) << "Selected MC collisions: " << selectedMCCollisions.size(); + // Inclusive charged particle + ue.fill(HIST("Inclusive/hPtMeasured"), track.pt()); + ue.fill(HIST("Inclusive/hPtMeasuredVsMult"), track.pt(), percentile); + ue.fill(HIST("Inclusive/hPtAllReco"), track.pt()); + ue.fill(HIST("Inclusive/hPtAllRecoVsMult"), track.pt(), percentile); + ue.fill(HIST("hEta"), track.eta()); + ue.fill(HIST("hPhi"), track.phi()); - int nPhysicsSelected = physicsSelectedMCCollisions.size(); - int nReconstructed = reconstructedMCCollisions.size(); - int nSelected = selectedMCCollisions.size(); + // Efficiency numerator + if (track.has_mcParticle()) { + const auto& particle = track.mcParticle(); + int pdgCode = std::abs(particle.pdgCode()); - if (nPhysicsSelected > 0) { - ue.fill(HIST("hEventLossBreakdown"), 1, nPhysicsSelected); - ue.fill(HIST("hEventLossBreakdown"), 2, nReconstructed); - ue.fill(HIST("hEventLossBreakdown"), 3, nSelected); - ue.fill(HIST("hEventLossBreakdown"), 4, (nSelected * 100.0 / nPhysicsSelected)); - } + if (particle.isPhysicalPrimary()) { + ue.fill(HIST("Inclusive/hPtNumEff"), particle.pt()); + ue.fill(HIST("Inclusive/hPtNumEffVsMult"), particle.pt(), percentile); + ue.fill(HIST("Inclusive/hPtPrimReco"), track.pt()); + ue.fill(HIST("Inclusive/hPtPrimRecoVsMult"), track.pt(), percentile); - // Process tracks - int totalTracksProcessed = 0; - int tracksFromSelectedEvents = 0; - int tracksPassingSelection = 0; + if (pdgCode == PDGPion) { + ue.fill(HIST("Pion/hPtNumEff"), particle.pt()); + ue.fill(HIST("Pion/hPtNumEffVsMult"), particle.pt(), percentile); + } + if (pdgCode == PDGKaon) { + ue.fill(HIST("Kaon/hPtNumEff"), particle.pt()); + ue.fill(HIST("Kaon/hPtNumEffVsMult"), particle.pt(), percentile); + } + if (pdgCode == PDGProton) { + ue.fill(HIST("Proton/hPtNumEff"), particle.pt()); + ue.fill(HIST("Proton/hPtNumEffVsMult"), particle.pt(), percentile); + } + } else { + ue.fill(HIST("Inclusive/hPtSecReco"), track.pt()); + ue.fill(HIST("Inclusive/hPtSecRecoVsMult"), track.pt(), percentile); + } + } - std::array particleTracksIdentified = {0}; - std::array particleTracksPrimary = {0}; - std::array particleTracksSecondary = {0}; + // Identified particle analysis + int bestSpecies = getBestPIDHypothesis(track); - for (const auto& track : tracks) { - totalTracksProcessed++; + if (bestSpecies == kPion) { + ue.fill(HIST("Pion/hPtMeasured"), track.pt()); + ue.fill(HIST("Pion/hPtMeasuredVsMult"), track.pt(), percentile); + ue.fill(HIST("Pion/hPtAllReco"), track.pt()); + ue.fill(HIST("Pion/hPtAllRecoVsMult"), track.pt(), percentile); + particleTracksIdentified[kPion]++; - if (!track.has_collision()) - continue; + if (enablePIDHistograms) { + ue.fill(HIST("Pion/hNsigmaTPC"), track.pt(), track.tpcNSigmaPi()); + } - const auto& collision = track.collision_as(); + if (track.has_mcParticle()) { + const auto& particle = track.mcParticle(); + if (particle.isPhysicalPrimary()) { + ue.fill(HIST("Pion/hPtPrimReco"), track.pt()); + ue.fill(HIST("Pion/hPtPrimRecoVsMult"), track.pt(), percentile); + particleTracksPrimary[kPion]++; + } else { + ue.fill(HIST("Pion/hPtSecReco"), track.pt()); + ue.fill(HIST("Pion/hPtSecRecoVsMult"), track.pt(), percentile); + particleTracksSecondary[kPion]++; + } + } - if (selectedCollisionIndices.find(collision.globalIndex()) == selectedCollisionIndices.end()) { - continue; - } - tracksFromSelectedEvents++; + } else if (bestSpecies == kKaon) { + ue.fill(HIST("Kaon/hPtMeasured"), track.pt()); + ue.fill(HIST("Kaon/hPtMeasuredVsMult"), track.pt(), percentile); + ue.fill(HIST("Kaon/hPtAllReco"), track.pt()); + ue.fill(HIST("Kaon/hPtAllRecoVsMult"), track.pt(), percentile); + particleTracksIdentified[kKaon]++; - if (!collision.has_mcCollision()) - continue; + if (enablePIDHistograms) { + ue.fill(HIST("Kaon/hNsigmaTPC"), track.pt(), track.tpcNSigmaKa()); + } - const auto& mcCollision = collision.mcCollision_as(); - float percentile = mcCollisionPercentile[mcCollision.globalIndex()]; + if (track.has_mcParticle()) { + const auto& particle = track.mcParticle(); + if (particle.isPhysicalPrimary()) { + ue.fill(HIST("Kaon/hPtPrimReco"), track.pt()); + ue.fill(HIST("Kaon/hPtPrimRecoVsMult"), track.pt(), percentile); + particleTracksPrimary[kKaon]++; + } else { + ue.fill(HIST("Kaon/hPtSecReco"), track.pt()); + ue.fill(HIST("Kaon/hPtSecRecoVsMult"), track.pt(), percentile); + particleTracksSecondary[kKaon]++; + } + } - float magField = 0; - if (applyPhiCut.value) { - const auto& bc = collision.bc_as(); - magField = getMagneticField(bc.timestamp()); - } + } else if (bestSpecies == kProton) { + ue.fill(HIST("Proton/hPtMeasured"), track.pt()); + ue.fill(HIST("Proton/hPtMeasuredVsMult"), track.pt(), percentile); + ue.fill(HIST("Proton/hPtAllReco"), track.pt()); + ue.fill(HIST("Proton/hPtAllRecoVsMult"), track.pt(), percentile); + particleTracksIdentified[kProton]++; - if (!passesTrackSelection(track, magField)) { - continue; - } - tracksPassingSelection++; - - // Inclusive charged particle - ue.fill(HIST("Inclusive/hPtMeasured"), track.pt()); - ue.fill(HIST("Inclusive/hPtMeasuredVsMult"), track.pt(), percentile); - ue.fill(HIST("Inclusive/hPtAllReco"), track.pt()); - ue.fill(HIST("Inclusive/hPtAllRecoVsMult"), track.pt(), percentile); - ue.fill(HIST("hEta"), track.eta()); - ue.fill(HIST("hPhi"), track.phi()); - - // Efficiency numerator - if (track.has_mcParticle()) { - const auto& particle = track.mcParticle(); - int pdgCode = std::abs(particle.pdgCode()); - - if (particle.isPhysicalPrimary()) { - ue.fill(HIST("Inclusive/hPtNumEff"), particle.pt()); - ue.fill(HIST("Inclusive/hPtNumEffVsMult"), particle.pt(), percentile); - ue.fill(HIST("Inclusive/hPtPrimReco"), track.pt()); - ue.fill(HIST("Inclusive/hPtPrimRecoVsMult"), track.pt(), percentile); - - if (pdgCode == PDGPion) { - ue.fill(HIST("Pion/hPtNumEff"), particle.pt()); - ue.fill(HIST("Pion/hPtNumEffVsMult"), particle.pt(), percentile); - } - if (pdgCode == PDGKaon) { - ue.fill(HIST("Kaon/hPtNumEff"), particle.pt()); - ue.fill(HIST("Kaon/hPtNumEffVsMult"), particle.pt(), percentile); + if (enablePIDHistograms) { + ue.fill(HIST("Proton/hNsigmaTPC"), track.pt(), track.tpcNSigmaPr()); } - if (pdgCode == PDGProton) { - ue.fill(HIST("Proton/hPtNumEff"), particle.pt()); - ue.fill(HIST("Proton/hPtNumEffVsMult"), particle.pt(), percentile); + + if (track.has_mcParticle()) { + const auto& particle = track.mcParticle(); + if (particle.isPhysicalPrimary()) { + ue.fill(HIST("Proton/hPtPrimReco"), track.pt()); + ue.fill(HIST("Proton/hPtPrimRecoVsMult"), track.pt(), percentile); + particleTracksPrimary[kProton]++; + } else { + ue.fill(HIST("Proton/hPtSecReco"), track.pt()); + ue.fill(HIST("Proton/hPtSecRecoVsMult"), track.pt(), percentile); + particleTracksSecondary[kProton]++; + } } - } else { - ue.fill(HIST("Inclusive/hPtSecReco"), track.pt()); - ue.fill(HIST("Inclusive/hPtSecRecoVsMult"), track.pt(), percentile); } } - // Identified particle analysis - int bestSpecies = getBestPIDHypothesis(track); - - if (bestSpecies == kPion) { - ue.fill(HIST("Pion/hPtMeasured"), track.pt()); - ue.fill(HIST("Pion/hPtMeasuredVsMult"), track.pt(), percentile); - ue.fill(HIST("Pion/hPtAllReco"), track.pt()); - ue.fill(HIST("Pion/hPtAllRecoVsMult"), track.pt(), percentile); - particleTracksIdentified[kPion]++; - - if (enablePIDHistograms) { - ue.fill(HIST("Pion/hNsigmaTPC"), track.pt(), track.tpcNSigmaPi()); - } + LOG(info) << "=== DEBUG TRACK COUNTING ==="; + LOG(info) << "Total tracks processed: " << totalTracksProcessed; + LOG(info) << "Tracks from selected events: " << tracksFromSelectedEvents; + LOG(info) << "Tracks passing selection: " << tracksPassingSelection; - if (track.has_mcParticle()) { - const auto& particle = track.mcParticle(); - if (particle.isPhysicalPrimary()) { - ue.fill(HIST("Pion/hPtPrimReco"), track.pt()); - ue.fill(HIST("Pion/hPtPrimRecoVsMult"), track.pt(), percentile); - particleTracksPrimary[kPion]++; - } else { - ue.fill(HIST("Pion/hPtSecReco"), track.pt()); - ue.fill(HIST("Pion/hPtSecRecoVsMult"), track.pt(), percentile); - particleTracksSecondary[kPion]++; - } - } + LOG(info) << "Pions identified: " << particleTracksIdentified[kPion] + << ", primary: " << particleTracksPrimary[kPion] + << ", secondary: " << particleTracksSecondary[kPion]; + LOG(info) << "Kaons identified: " << particleTracksIdentified[kKaon] + << ", primary: " << particleTracksPrimary[kKaon] + << ", secondary: " << particleTracksSecondary[kKaon]; + LOG(info) << "Protons identified: " << particleTracksIdentified[kProton] + << ", primary: " << particleTracksPrimary[kProton] + << ", secondary: " << particleTracksSecondary[kProton]; - } else if (bestSpecies == kKaon) { - ue.fill(HIST("Kaon/hPtMeasured"), track.pt()); - ue.fill(HIST("Kaon/hPtMeasuredVsMult"), track.pt(), percentile); - ue.fill(HIST("Kaon/hPtAllReco"), track.pt()); - ue.fill(HIST("Kaon/hPtAllRecoVsMult"), track.pt(), percentile); - particleTracksIdentified[kKaon]++; + LOG(info) << "=== DEBUG processMC END ==="; + } - if (enablePIDHistograms) { - ue.fill(HIST("Kaon/hNsigmaTPC"), track.pt(), track.tpcNSigmaKa()); - } + // ======================================================================== + // TRUE MC PROCESSING - Using computed percentiles + // ======================================================================== + void MultiplicityPt::processTrue(CollisionTableMCTrue const& mcCollisions, + ParticleTableMC const& particles) + { + if (!percentilesComputed) { + LOG(warning) << "Percentiles not computed yet! Run processPercentileCalibration first!"; + } - if (track.has_mcParticle()) { - const auto& particle = track.mcParticle(); - if (particle.isPhysicalPrimary()) { - ue.fill(HIST("Kaon/hPtPrimReco"), track.pt()); - ue.fill(HIST("Kaon/hPtPrimRecoVsMult"), track.pt(), percentile); - particleTracksPrimary[kKaon]++; - } else { - ue.fill(HIST("Kaon/hPtSecReco"), track.pt()); - ue.fill(HIST("Kaon/hPtSecRecoVsMult"), track.pt(), percentile); - particleTracksSecondary[kKaon]++; - } - } + LOG(info) << "=== DEBUG processTrue START ==="; + LOG(info) << "Number of MC collisions: " << mcCollisions.size(); - } else if (bestSpecies == kProton) { - ue.fill(HIST("Proton/hPtMeasured"), track.pt()); - ue.fill(HIST("Proton/hPtMeasuredVsMult"), track.pt(), percentile); - ue.fill(HIST("Proton/hPtAllReco"), track.pt()); - ue.fill(HIST("Proton/hPtAllRecoVsMult"), track.pt(), percentile); - particleTracksIdentified[kProton]++; + int nAllGenerated = 0; + int nBadVertex = 0; + int nPhysicsSelected = 0; - if (enablePIDHistograms) { - ue.fill(HIST("Proton/hNsigmaTPC"), track.pt(), track.tpcNSigmaPr()); - } + std::array particleCountAll = {0}; + std::array particleCountBadVertex = {0}; + std::array particleCountAfterPS = {0}; - if (track.has_mcParticle()) { - const auto& particle = track.mcParticle(); - if (particle.isPhysicalPrimary()) { - ue.fill(HIST("Proton/hPtPrimReco"), track.pt()); - ue.fill(HIST("Proton/hPtPrimRecoVsMult"), track.pt(), percentile); - particleTracksPrimary[kProton]++; - } else { - ue.fill(HIST("Proton/hPtSecReco"), track.pt()); - ue.fill(HIST("Proton/hPtSecRecoVsMult"), track.pt(), percentile); - particleTracksSecondary[kProton]++; - } - } - } - } + for (const auto& mcCollision : mcCollisions) { + nAllGenerated++; - LOG(info) << "=== DEBUG TRACK COUNTING ==="; - LOG(info) << "Total tracks processed: " << totalTracksProcessed; - LOG(info) << "Tracks from selected events: " << tracksFromSelectedEvents; - LOG(info) << "Tracks passing selection: " << tracksPassingSelection; - - LOG(info) << "Pions identified: " << particleTracksIdentified[kPion] - << ", primary: " << particleTracksPrimary[kPion] - << ", secondary: " << particleTracksSecondary[kPion]; - LOG(info) << "Kaons identified: " << particleTracksIdentified[kKaon] - << ", primary: " << particleTracksPrimary[kKaon] - << ", secondary: " << particleTracksSecondary[kKaon]; - LOG(info) << "Protons identified: " << particleTracksIdentified[kProton] - << ", primary: " << particleTracksPrimary[kProton] - << ", secondary: " << particleTracksSecondary[kProton]; - - LOG(info) << "=== DEBUG processMC END ==="; -} + float mcMult = getMultiplicityMC(mcCollision, particles); + float percentile = multiplicityToPercentile(mcMult); -// ======================================================================== -// TRUE MC PROCESSING - Using computed percentiles -// ======================================================================== -void MultiplicityPt::processTrue(CollisionTableMCTrue const& mcCollisions, - ParticleTableMC const& particles) -{ - if (!percentilesComputed) { - LOG(warning) << "Percentiles not computed yet! Run processPercentileCalibration first!"; - } + ue.fill(HIST("hvtxZmc"), mcCollision.posZ()); + auto particlesInCollision = particles.sliceBy(perMCCol, mcCollision.globalIndex()); - LOG(info) << "=== DEBUG processTrue START ==="; - LOG(info) << "Number of MC collisions: " << mcCollisions.size(); + // Fill ALL generated primaries BEFORE any cuts + for (const auto& particle : particlesInCollision) { + if (isGoodPrimary(particle)) { + ue.fill(HIST("Inclusive/hPtPrimGenAll"), particle.pt()); + ue.fill(HIST("Inclusive/hPtPrimGenAllVsMult"), particle.pt(), percentile); + } - int nAllGenerated = 0; - int nBadVertex = 0; - int nPhysicsSelected = 0; + if (isGoodPrimarySpecies(particle)) { + ue.fill(HIST("Pion/hPtPrimGenAll"), particle.pt()); + ue.fill(HIST("Pion/hPtPrimGenAllVsMult"), particle.pt(), percentile); + particleCountAll[kPion]++; + } - std::array particleCountAll = {0}; - std::array particleCountBadVertex = {0}; - std::array particleCountAfterPS = {0}; + if (isGoodPrimarySpecies(particle)) { + ue.fill(HIST("Kaon/hPtPrimGenAll"), particle.pt()); + ue.fill(HIST("Kaon/hPtPrimGenAllVsMult"), particle.pt(), percentile); + particleCountAll[kKaon]++; + } - for (const auto& mcCollision : mcCollisions) { - nAllGenerated++; + if (isGoodPrimarySpecies(particle)) { + ue.fill(HIST("Proton/hPtPrimGenAll"), particle.pt()); + ue.fill(HIST("Proton/hPtPrimGenAllVsMult"), particle.pt(), percentile); + particleCountAll[kProton]++; + } + } - float mcMult = getMultiplicityMC(mcCollision, particles); - float percentile = multiplicityToPercentile(mcMult); + // Apply vertex cut + if (std::abs(mcCollision.posZ()) > cfgCutVertex.value) { + nBadVertex++; - ue.fill(HIST("hvtxZmc"), mcCollision.posZ()); - auto particlesInCollision = particles.sliceBy(perMCCol, mcCollision.globalIndex()); + for (const auto& particle : particlesInCollision) { + if (isGoodPrimary(particle)) { + ue.fill(HIST("Inclusive/hPtPrimBadVertex"), particle.pt()); + ue.fill(HIST("Inclusive/hPtPrimBadVertexVsMult"), particle.pt(), percentile); + } - // Fill ALL generated primaries BEFORE any cuts - for (const auto& particle : particlesInCollision) { - if (isGoodPrimary(particle)) { - ue.fill(HIST("Inclusive/hPtPrimGenAll"), particle.pt()); - ue.fill(HIST("Inclusive/hPtPrimGenAllVsMult"), particle.pt(), percentile); - } + if (isGoodPrimarySpecies(particle)) { + ue.fill(HIST("Pion/hPtPrimBadVertex"), particle.pt()); + ue.fill(HIST("Pion/hPtPrimBadVertexVsMult"), particle.pt(), percentile); + particleCountBadVertex[kPion]++; + } - if (isGoodPrimarySpecies(particle)) { - ue.fill(HIST("Pion/hPtPrimGenAll"), particle.pt()); - ue.fill(HIST("Pion/hPtPrimGenAllVsMult"), particle.pt(), percentile); - particleCountAll[kPion]++; - } + if (isGoodPrimarySpecies(particle)) { + ue.fill(HIST("Kaon/hPtPrimBadVertex"), particle.pt()); + ue.fill(HIST("Kaon/hPtPrimBadVertexVsMult"), particle.pt(), percentile); + particleCountBadVertex[kKaon]++; + } - if (isGoodPrimarySpecies(particle)) { - ue.fill(HIST("Kaon/hPtPrimGenAll"), particle.pt()); - ue.fill(HIST("Kaon/hPtPrimGenAllVsMult"), particle.pt(), percentile); - particleCountAll[kKaon]++; + if (isGoodPrimarySpecies(particle)) { + ue.fill(HIST("Proton/hPtPrimBadVertex"), particle.pt()); + ue.fill(HIST("Proton/hPtPrimBadVertexVsMult"), particle.pt(), percentile); + particleCountBadVertex[kProton]++; + } + } + continue; } - if (isGoodPrimarySpecies(particle)) { - ue.fill(HIST("Proton/hPtPrimGenAll"), particle.pt()); - ue.fill(HIST("Proton/hPtPrimGenAllVsMult"), particle.pt(), percentile); - particleCountAll[kProton]++; - } - } + // Apply INEL cuts + if (cfgINELCut.value == 1 && !o2::pwglf::isINELgt0mc(particlesInCollision, pdg)) + continue; + if (cfgINELCut.value == 2 && !o2::pwglf::isINELgt1mc(particlesInCollision, pdg)) + continue; - // Apply vertex cut - if (std::abs(mcCollision.posZ()) > cfgCutVertex.value) { - nBadVertex++; + nPhysicsSelected++; + // Fill primaries AFTER physics selection (denominator for efficiency) for (const auto& particle : particlesInCollision) { if (isGoodPrimary(particle)) { - ue.fill(HIST("Inclusive/hPtPrimBadVertex"), particle.pt()); - ue.fill(HIST("Inclusive/hPtPrimBadVertexVsMult"), particle.pt(), percentile); + ue.fill(HIST("Inclusive/hPtDenEff"), particle.pt()); + ue.fill(HIST("Inclusive/hPtDenEffVsMult"), particle.pt(), percentile); + ue.fill(HIST("Inclusive/hPtPrimGen"), particle.pt()); + ue.fill(HIST("Inclusive/hPtPrimGenVsMult"), particle.pt(), percentile); } if (isGoodPrimarySpecies(particle)) { - ue.fill(HIST("Pion/hPtPrimBadVertex"), particle.pt()); - ue.fill(HIST("Pion/hPtPrimBadVertexVsMult"), particle.pt(), percentile); - particleCountBadVertex[kPion]++; + ue.fill(HIST("Pion/hPtDenEff"), particle.pt()); + ue.fill(HIST("Pion/hPtDenEffVsMult"), particle.pt(), percentile); + ue.fill(HIST("Pion/hPtPrimGen"), particle.pt()); + ue.fill(HIST("Pion/hPtPrimGenVsMult"), particle.pt(), percentile); + particleCountAfterPS[kPion]++; } if (isGoodPrimarySpecies(particle)) { - ue.fill(HIST("Kaon/hPtPrimBadVertex"), particle.pt()); - ue.fill(HIST("Kaon/hPtPrimBadVertexVsMult"), particle.pt(), percentile); - particleCountBadVertex[kKaon]++; + ue.fill(HIST("Kaon/hPtDenEff"), particle.pt()); + ue.fill(HIST("Kaon/hPtDenEffVsMult"), particle.pt(), percentile); + ue.fill(HIST("Kaon/hPtPrimGen"), particle.pt()); + ue.fill(HIST("Kaon/hPtPrimGenVsMult"), particle.pt(), percentile); + particleCountAfterPS[kKaon]++; } if (isGoodPrimarySpecies(particle)) { - ue.fill(HIST("Proton/hPtPrimBadVertex"), particle.pt()); - ue.fill(HIST("Proton/hPtPrimBadVertexVsMult"), particle.pt(), percentile); - particleCountBadVertex[kProton]++; + ue.fill(HIST("Proton/hPtDenEff"), particle.pt()); + ue.fill(HIST("Proton/hPtDenEffVsMult"), particle.pt(), percentile); + ue.fill(HIST("Proton/hPtPrimGen"), particle.pt()); + ue.fill(HIST("Proton/hPtPrimGenVsMult"), particle.pt(), percentile); + particleCountAfterPS[kProton]++; } } - continue; } - // Apply INEL cuts - if (cfgINELCut.value == 1 && !o2::pwglf::isINELgt0mc(particlesInCollision, pdg)) - continue; - if (cfgINELCut.value == 2 && !o2::pwglf::isINELgt1mc(particlesInCollision, pdg)) - continue; - - nPhysicsSelected++; - - // Fill primaries AFTER physics selection (denominator for efficiency) - for (const auto& particle : particlesInCollision) { - if (isGoodPrimary(particle)) { - ue.fill(HIST("Inclusive/hPtDenEff"), particle.pt()); - ue.fill(HIST("Inclusive/hPtDenEffVsMult"), particle.pt(), percentile); - ue.fill(HIST("Inclusive/hPtPrimGen"), particle.pt()); - ue.fill(HIST("Inclusive/hPtPrimGenVsMult"), particle.pt(), percentile); - } - - if (isGoodPrimarySpecies(particle)) { - ue.fill(HIST("Pion/hPtDenEff"), particle.pt()); - ue.fill(HIST("Pion/hPtDenEffVsMult"), particle.pt(), percentile); - ue.fill(HIST("Pion/hPtPrimGen"), particle.pt()); - ue.fill(HIST("Pion/hPtPrimGenVsMult"), particle.pt(), percentile); - particleCountAfterPS[kPion]++; - } - - if (isGoodPrimarySpecies(particle)) { - ue.fill(HIST("Kaon/hPtDenEff"), particle.pt()); - ue.fill(HIST("Kaon/hPtDenEffVsMult"), particle.pt(), percentile); - ue.fill(HIST("Kaon/hPtPrimGen"), particle.pt()); - ue.fill(HIST("Kaon/hPtPrimGenVsMult"), particle.pt(), percentile); - particleCountAfterPS[kKaon]++; - } - - if (isGoodPrimarySpecies(particle)) { - ue.fill(HIST("Proton/hPtDenEff"), particle.pt()); - ue.fill(HIST("Proton/hPtDenEffVsMult"), particle.pt(), percentile); - ue.fill(HIST("Proton/hPtPrimGen"), particle.pt()); - ue.fill(HIST("Proton/hPtPrimGenVsMult"), particle.pt(), percentile); - particleCountAfterPS[kProton]++; - } - } + LOG(info) << "=== DEBUG processTrue END ==="; + LOG(info) << "All generated events: " << nAllGenerated; + LOG(info) << "Events with bad vertex: " << nBadVertex; + LOG(info) << "Passing physics selection: " << nPhysicsSelected; + + LOG(info) << "=== PARTICLE-SPECIFIC STATISTICS ==="; + LOG(info) << "Pions - All: " << particleCountAll[kPion] + << ", Bad vertex: " << particleCountBadVertex[kPion] + << ", After PS: " << particleCountAfterPS[kPion]; + LOG(info) << "Kaons - All: " << particleCountAll[kKaon] + << ", Bad vertex: " << particleCountBadVertex[kKaon] + << ", After PS: " << particleCountAfterPS[kKaon]; + LOG(info) << "Protons - All: " << particleCountAll[kProton] + << ", Bad vertex: " << particleCountBadVertex[kProton] + << ", After PS: " << particleCountAfterPS[kProton]; } - - LOG(info) << "=== DEBUG processTrue END ==="; - LOG(info) << "All generated events: " << nAllGenerated; - LOG(info) << "Events with bad vertex: " << nBadVertex; - LOG(info) << "Passing physics selection: " << nPhysicsSelected; - - LOG(info) << "=== PARTICLE-SPECIFIC STATISTICS ==="; - LOG(info) << "Pions - All: " << particleCountAll[kPion] - << ", Bad vertex: " << particleCountBadVertex[kPion] - << ", After PS: " << particleCountAfterPS[kPion]; - LOG(info) << "Kaons - All: " << particleCountAll[kKaon] - << ", Bad vertex: " << particleCountBadVertex[kKaon] - << ", After PS: " << particleCountAfterPS[kKaon]; - LOG(info) << "Protons - All: " << particleCountAll[kProton] - << ", Bad vertex: " << particleCountBadVertex[kProton] - << ", After PS: " << particleCountAfterPS[kProton]; -}