4040#include < Framework/runDataProcessing.h>
4141
4242#include < TH1.h>
43+ #include < TH2.h>
4344#include < TH3.h>
4445#include < TList.h>
4546#include < TPDGCode.h>
4950#include < cmath>
5051#include < cstddef>
5152#include < cstdint>
53+ #include < cstring>
54+ #include < memory>
5255#include < string>
5356#include < string_view>
5457#include < vector>
@@ -60,6 +63,8 @@ using namespace o2::constants::physics;
6063using namespace std ;
6164
6265struct MeanPtFlucId {
66+ Configurable<bool > cfgSaveSubsamples{" cfgSaveSubsamples" , true , " Save 30 matched reconstructed-event N1/N2 matrix subsamples" };
67+ Configurable<int > cfgSubsampleSeed{" cfgSubsampleSeed" , 12345 , " Fixed hash seed; MUST match across cut variations" };
6368 Configurable<int > nPBins{" nPBins" , 300 , " " };
6469 Configurable<int > nPhiBins{" nPhiBins" , 100 , " " };
6570 Configurable<int > nPtBins{" nPtBins" , 50 , " Number of pT bins" };
@@ -166,6 +171,61 @@ struct MeanPtFlucId {
166171
167172 HistogramRegistry hist{" hist" , {}, OutputObjHandlingPolicy::AnalysisObject};
168173
174+ // Separate registries keep each stream below the registry histogram limit.
175+ HistogramRegistry subsampleRaw{" subsampleRaw" , {}, OutputObjHandlingPolicy::AnalysisObject};
176+ HistogramRegistry subsampleCorrected{" subsampleCorrected" , {}, OutputObjHandlingPolicy::AnalysisObject};
177+ HistogramRegistry subsampleTruth{" subsampleTruth" , {}, OutputObjHandlingPolicy::AnalysisObject};
178+ static constexpr int NSubsamples = 30 ;
179+ struct SubsampleMatrices {
180+ std::shared_ptr<TH2 > n1;
181+ std::shared_ptr<TH3 > n2;
182+ };
183+ std::array<std::array<std::array<SubsampleMatrices, 4 >, NSubsamples>, 3 > subsampleMatrices;
184+ int activeSubsample = -1 ;
185+
186+ static uint64_t mixSubsampleKey (uint64_t x)
187+ {
188+ x += 0x9e3779b97f4a7c15ULL ;
189+ x = (x ^ (x >> 30 )) * 0xbf58476d1ce4e5b9ULL ;
190+ x = (x ^ (x >> 27 )) * 0x94d049bb133111ebULL ;
191+ return x ^ (x >> 31 );
192+ }
193+
194+ template <typename C>
195+ void selectSubsample (C const & col)
196+ {
197+ activeSubsample = -1 ;
198+ if (!cfgSaveSubsamples) {
199+ return ;
200+ }
201+ const auto bc = col.template bc_as <aod::BCs>();
202+ uint64_t key = mixSubsampleKey (static_cast <uint64_t >(cfgSubsampleSeed.value ));
203+ key = mixSubsampleKey (key ^ static_cast <uint64_t >(bc.runNumber ()));
204+ key = mixSubsampleKey (key ^ bc.globalBC ());
205+ // Original vertex coordinates disambiguate collisions sharing a BC and MC
206+ // BC identifiers repeated in different input files. No cut-dependent state.
207+ for (const auto & coordinate : {col.posX (), col.posY (), col.posZ ()}) {
208+ uint32_t bits = 0 ;
209+ std::memcpy (&bits, &coordinate, sizeof (bits));
210+ key = mixSubsampleKey (key ^ bits);
211+ }
212+ activeSubsample = static_cast <int >(key % NSubsamples);
213+ }
214+
215+ void fillSubsampleN1 (int stream, int species, double cent, double pt, double value)
216+ {
217+ if (activeSubsample >= 0 ) {
218+ subsampleMatrices[stream][activeSubsample][species].n1 ->Fill (cent, pt, value);
219+ }
220+ }
221+
222+ void fillSubsampleN2 (int stream, int species, double cent, double pt1, double pt2, double value)
223+ {
224+ if (activeSubsample >= 0 ) {
225+ subsampleMatrices[stream][activeSubsample][species].n2 ->Fill (cent, pt1, pt2, value);
226+ }
227+ }
228+
169229 TH3 * efficiencyWeightCh = nullptr ;
170230 TH3 * efficiencyWeightPi = nullptr ;
171231 TH3 * efficiencyWeightKa = nullptr ;
@@ -624,6 +684,32 @@ struct MeanPtFlucId {
624684 addCorrectedHistograms (" CorrectedTruthMatched/Proton/" , axisPtPr);
625685 }
626686
687+ if (cfgSaveSubsamples && (doprocessRun3 || doprocessMCRecoSimRun3)) {
688+ const std::array<AxisSpec, 4 > axes{axisPtCh, axisPtPi, axisPtKa, axisPtPr};
689+ const std::array<std::string, 4 > names{" Charged" , " Pion" , " Kaon" , " Proton" };
690+ const std::array<std::string, 3 > streams{" Analysis" , " Corrected" , " CorrectedTruthMatched" };
691+ const std::array<HistogramRegistry*, 3 > registries{&subsampleRaw, &subsampleCorrected, &subsampleTruth};
692+ constexpr int TruthMatchedStream = 2 ;
693+ constexpr int FirstTwoDigitGroup = 10 ;
694+ for (std::size_t stream = 0 ; stream < streams.size (); ++stream) {
695+ if (stream > 0 && !cfgApplyEfficiencyCorrection) {
696+ continue ;
697+ }
698+ if (stream == TruthMatchedStream && (!doprocessMCRecoSimRun3 || cfgUseCombinedPurityEfficiencyWeight)) {
699+ continue ;
700+ }
701+ for (int group = 0 ; group < NSubsamples; ++group) {
702+ const std::string label = (group < FirstTwoDigitGroup ? " 0" : " " ) + std::to_string (group);
703+ for (std::size_t species = 0 ; species < names.size (); ++species) {
704+ const std::string path = " Subsamples/" + label + " /" + streams[stream] + " /" + names[species] + " /" ;
705+ auto & matrices = subsampleMatrices[stream][group][species];
706+ matrices.n1 = registries[stream]->add <TH2 >(path + " hN1Matrix" , " Subsample single-particle density" , kTH2D , {axisCent, axes[species]});
707+ matrices.n2 = registries[stream]->add <TH3 >(path + " hN2Matrix" , " Subsample ordered distinct-pair density" , kTH3D , {axisCent, axes[species], axes[species]});
708+ }
709+ }
710+ }
711+ }
712+
627713 auto addGeneratedHistograms = [&]() {
628714 // MC generated
629715 hist.add (" Gen/h_Counts" , " Counts" , kTH1D , {axisEvents});
@@ -1161,6 +1247,9 @@ struct MeanPtFlucId {
11611247 for (const int & ptBin : matrixOccupiedBins) {
11621248 const double ptCenter = ptMin + (ptBin + 0.5 ) * binWidth;
11631249 hist.fill (HIST (dire[Mode]) + HIST (" hN1Matrix" ), eventCoordinate, ptCenter, eventWeight * static_cast <double >(matrixNInBin[ptBin]));
1250+ if constexpr (Mode >= Analysis_Charged && Mode <= Analysis_Proton) {
1251+ fillSubsampleN1 (0 , Mode - Analysis_Charged, eventCoordinate, ptCenter, eventWeight * static_cast <double >(matrixNInBin[ptBin]));
1252+ }
11641253 }
11651254
11661255 for (size_t i = 0 ; i < matrixOccupiedBins.size (); ++i) {
@@ -1169,14 +1258,23 @@ struct MeanPtFlucId {
11691258 const double nPairDiagonal = static_cast <double >(matrixNInBin[ptBin1]) * (matrixNInBin[ptBin1] - 1 );
11701259 if (nPairDiagonal > 0.0 ) {
11711260 hist.fill (HIST (dire[Mode]) + HIST (" hN2Matrix" ), eventCoordinate, ptCenter1, ptCenter1, eventWeight * nPairDiagonal);
1261+ if constexpr (Mode >= Analysis_Charged && Mode <= Analysis_Proton) {
1262+ fillSubsampleN2 (0 , Mode - Analysis_Charged, eventCoordinate, ptCenter1, ptCenter1, eventWeight * nPairDiagonal);
1263+ }
11721264 }
11731265
11741266 for (size_t j = i + 1 ; j < matrixOccupiedBins.size (); ++j) {
11751267 const int ptBin2 = matrixOccupiedBins[j];
11761268 const double ptCenter2 = ptMin + (ptBin2 + 0.5 ) * binWidth;
11771269 const double nPair = static_cast <double >(matrixNInBin[ptBin1]) * matrixNInBin[ptBin2];
11781270 hist.fill (HIST (dire[Mode]) + HIST (" hN2Matrix" ), eventCoordinate, ptCenter1, ptCenter2, eventWeight * nPair);
1271+ if constexpr (Mode >= Analysis_Charged && Mode <= Analysis_Proton) {
1272+ fillSubsampleN2 (0 , Mode - Analysis_Charged, eventCoordinate, ptCenter1, ptCenter2, eventWeight * nPair);
1273+ }
11791274 hist.fill (HIST (dire[Mode]) + HIST (" hN2Matrix" ), eventCoordinate, ptCenter2, ptCenter1, eventWeight * nPair);
1275+ if constexpr (Mode >= Analysis_Charged && Mode <= Analysis_Proton) {
1276+ fillSubsampleN2 (0 , Mode - Analysis_Charged, eventCoordinate, ptCenter2, ptCenter1, eventWeight * nPair);
1277+ }
11801278 }
11811279 }
11821280 }
@@ -1247,8 +1345,10 @@ struct MeanPtFlucId {
12471345 const double ptCenter = ptMin + (ptBin + 0.5 ) * binWidth;
12481346 if constexpr (TruthMatched) {
12491347 hist.fill (HIST (TruthMatchedCorrectedDirectories[Mode]) + HIST (" hN1Matrix" ), centFT0M, ptCenter, matrixWInBin[ptBin]);
1348+ fillSubsampleN1 (2 , Mode, centFT0M, ptCenter, matrixWInBin[ptBin]);
12501349 } else {
12511350 hist.fill (HIST (CorrectedDirectories[Mode]) + HIST (" hN1Matrix" ), centFT0M, ptCenter, matrixWInBin[ptBin]);
1351+ fillSubsampleN1 (1 , Mode, centFT0M, ptCenter, matrixWInBin[ptBin]);
12521352 }
12531353 }
12541354
@@ -1259,8 +1359,10 @@ struct MeanPtFlucId {
12591359 if (diagonalPairDensity > 0 .) {
12601360 if constexpr (TruthMatched) {
12611361 hist.fill (HIST (TruthMatchedCorrectedDirectories[Mode]) + HIST (" hN2Matrix" ), centFT0M, ptCenter1, ptCenter1, diagonalPairDensity);
1362+ fillSubsampleN2 (2 , Mode, centFT0M, ptCenter1, ptCenter1, diagonalPairDensity);
12621363 } else {
12631364 hist.fill (HIST (CorrectedDirectories[Mode]) + HIST (" hN2Matrix" ), centFT0M, ptCenter1, ptCenter1, diagonalPairDensity);
1365+ fillSubsampleN2 (1 , Mode, centFT0M, ptCenter1, ptCenter1, diagonalPairDensity);
12641366 }
12651367 }
12661368
@@ -1270,10 +1372,14 @@ struct MeanPtFlucId {
12701372 const double pairDensity = matrixWInBin[ptBin1] * matrixWInBin[ptBin2];
12711373 if constexpr (TruthMatched) {
12721374 hist.fill (HIST (TruthMatchedCorrectedDirectories[Mode]) + HIST (" hN2Matrix" ), centFT0M, ptCenter1, ptCenter2, pairDensity);
1375+ fillSubsampleN2 (2 , Mode, centFT0M, ptCenter1, ptCenter2, pairDensity);
12731376 hist.fill (HIST (TruthMatchedCorrectedDirectories[Mode]) + HIST (" hN2Matrix" ), centFT0M, ptCenter2, ptCenter1, pairDensity);
1377+ fillSubsampleN2 (2 , Mode, centFT0M, ptCenter2, ptCenter1, pairDensity);
12741378 } else {
12751379 hist.fill (HIST (CorrectedDirectories[Mode]) + HIST (" hN2Matrix" ), centFT0M, ptCenter1, ptCenter2, pairDensity);
1380+ fillSubsampleN2 (1 , Mode, centFT0M, ptCenter1, ptCenter2, pairDensity);
12761381 hist.fill (HIST (CorrectedDirectories[Mode]) + HIST (" hN2Matrix" ), centFT0M, ptCenter2, ptCenter1, pairDensity);
1382+ fillSubsampleN2 (1 , Mode, centFT0M, ptCenter2, ptCenter1, pairDensity);
12771383 }
12781384 }
12791385 }
@@ -1663,15 +1769,20 @@ struct MeanPtFlucId {
16631769 SliceCache cache;
16641770 PresliceOptional<MyMCTracks> perCollision = aod::track::collisionId;
16651771
1666- void processRun3 (MyRun3Collisions::iterator const & col, MyAllTracks const & tracks)
1772+ void processRun3 (MyRun3Collisions::iterator const & col, MyAllTracks const & tracks, aod::BCs const & )
16671773 {
1774+ selectSubsample (col);
16681775 fillBeforeQAHistos (col, tracks);
16691776 fillRecoHistos<true , false >(col, tracks);
16701777 }
16711778 PROCESS_SWITCH (MeanPtFlucId, processRun3, " Process for Run-3" , false );
16721779
1673- void processMCRecoSimRun3 (aod::McCollisions::iterator const & mcCol, soa::SmallGroups<MyRun3MCCollisions> const & cols, MyMCTracks const & tracks, aod::McParticles const & mcParts)
1780+ void processMCRecoSimRun3 (aod::McCollisions::iterator const & mcCol, soa::SmallGroups<MyRun3MCCollisions> const & cols, MyMCTracks const & tracks, aod::McParticles const & mcParts, aod::BCs const & )
16741781 {
1782+ activeSubsample = -1 ;
1783+ if (cols.size () == 1 ) {
1784+ selectSubsample (cols.begin ());
1785+ }
16751786 analyzeMC (mcCol, cols, tracks, mcParts);
16761787 }
16771788 PROCESS_SWITCH (MeanPtFlucId, processMCRecoSimRun3, " process MC Reconstructed & Truth Run-3" , true );
0 commit comments