6868#include < RtypesCore.h>
6969
7070#include < algorithm>
71+ #include < charconv>
7172#include < chrono>
7273#include < cmath>
7374#include < cstdint>
7879#include < map>
7980#include < memory>
8081#include < string>
82+ #include < system_error>
8183#include < utility>
8284#include < variant>
8385#include < vector>
@@ -1511,6 +1513,7 @@ struct AnalysisSameEventPairing {
15111513 o2::framework::Configurable<std::string> fConfigMixingVariables {" cfgMixingVars" , " " , " Mixing configs separated by a comma, default no mixing" };
15121514 o2::framework::Configurable<std::string> fConfigMixingVariablesJson {" cfgMixingVarsJSON" , " " , " Mixing configs in JSON format" };
15131515 o2::framework::Configurable<bool > fConfigRunMixingAcrossTFs {" cfgRunMixingAcrossTFs" , false , " If true, run event mixing using the MixingHandler which accumulates events across multiple TFs" };
1516+ o2::framework::Configurable<std::string> fConfigMixingNSelEdges {" cfgMixingNSelEdges" , " " , " Comma separated class edges in the number of selected pairing tracks per event, (e.g. 2,3,4,1000)" };
15141517 // o2::framework::Configurable<std::string> fConfigAddEventMixingHistogram{"cfgAddEventMixingHistogram", "", "Comma separated list of histograms"};
15151518 o2::framework::Configurable<std::string> fConfigAddSEPHistogram {" cfgAddSEPHistogram" , " " , " Comma separated list of histograms" };
15161519 o2::framework::Configurable<std::string> fConfigAddJSONHistograms {" cfgAddJSONHistograms" , " " , " Histograms in JSON format" };
@@ -1587,6 +1590,7 @@ struct AnalysisSameEventPairing {
15871590 int fNCutsMuon = 0 ;
15881591 int fNPairCuts = 0 ;
15891592 int fNPairPerEvent = 0 ;
1593+ std::vector<int > fMixNSelEdges ;
15901594
15911595 bool fEnableBarrelMixingHistos = false ;
15921596 bool fEnableBarrelHistos = false ;
@@ -1606,6 +1610,9 @@ struct AnalysisSameEventPairing {
16061610 {
16071611 fEnableBarrelHistos = context.mOptions .get <bool >(" processAllSkimmed" ) || context.mOptions .get <bool >(" processBarrelOnlySkimmed" ) || context.mOptions .get <bool >(" processBarrelOnlyWithCollSkimmed" ) || context.mOptions .get <bool >(" processBarrelOnlySkimmedNoCov" ) || context.mOptions .get <bool >(" processBarrelOnlySkimmedNoCovWithMultExtra" ) || context.mOptions .get <bool >(" processBarrelOnlyWithQvectorCentrSkimmedNoCov" ) || context.mOptions .get <bool >(" processBarrelOnlyWithQvectorCentrSkimmed" ) || context.mOptions .get <bool >(" processBarrelOnlyWithQvectorCentrVtxCovSkimmed" ) || context.mOptions .get <bool >(" processBarrelOnlyDiKaons" ) || context.mOptions .get <bool >(" processBarrelOnlyDiProtons" ) || context.mOptions .get <bool >(" processBarrelOnlySkimmedWithGlobalMult" );
16081612 fEnableBarrelMixingHistos = context.mOptions .get <bool >(" processMixingAllSkimmed" ) || context.mOptions .get <bool >(" processMixingBarrelSkimmed" ) || context.mOptions .get <bool >(" processMixingBarrelSkimmedFlow" ) || context.mOptions .get <bool >(" processMixingBarrelWithQvectorCentrSkimmedNoCov" ) || context.mOptions .get <bool >(" processMixingBarrelSkimmedGlobalMult" );
1613+ if (fEnableBarrelMixingHistos && fConfigRunMixingAcrossTFs ) {
1614+ LOG (fatal) << " cfgRunMixingAcrossTFs is on together with an in-TF processMixing* function" ;
1615+ }
16091616 fEnableBarrelMixingHistos |= fConfigRunMixingAcrossTFs ;
16101617 fEnableMuonHistos = context.mOptions .get <bool >(" processAllSkimmed" ) || context.mOptions .get <bool >(" processMuonOnlySkimmed" ) || context.mOptions .get <bool >(" processMuonOnlySkimmedMultExtra" ) || context.mOptions .get <bool >(" processMuonOnlySkimmedFlow" ) || context.mOptions .get <bool >(" processMuonOnlyVertexingSkimmed" );
16111618 fEnableMuonMixingHistos = context.mOptions .get <bool >(" processMixingAllSkimmed" ) || context.mOptions .get <bool >(" processMixingMuonSkimmed" ) || context.mOptions .get <bool >(" processMixingMuonSkimmedFlow" );
@@ -1856,6 +1863,23 @@ struct AnalysisSameEventPairing {
18561863 }
18571864 }
18581865
1866+ fMixNSelEdges .clear ();
1867+ if (!fConfigMixingNSelEdges .value .empty ()) {
1868+ std::unique_ptr<TObjArray> edgeArr (TString (fConfigMixingNSelEdges .value ).Tokenize (" ," ));
1869+ for (int ie = 0 ; ie < edgeArr->GetEntries (); ++ie) {
1870+ const std::string edgeStr = dynamic_cast <TObjString*>(edgeArr->At (ie))->GetString ().Data ();
1871+ int edge = 0 ;
1872+ const auto [ptr, ec] = std::from_chars (edgeStr.data (), edgeStr.data () + edgeStr.size (), edge);
1873+ if (ec != std::errc () || ptr != edgeStr.data () + edgeStr.size ()) {
1874+ LOG (fatal) << " Edges must be integers: '" << fConfigMixingNSelEdges .value << " '" ;
1875+ }
1876+ fMixNSelEdges .push_back (edge);
1877+ }
1878+ if (fMixNSelEdges .size () < 2 || !std::is_sorted (fMixNSelEdges .begin (), fMixNSelEdges .end ()) || std::adjacent_find (fMixNSelEdges .begin (), fMixNSelEdges .end ()) != fMixNSelEdges .end ()) {
1879+ LOG (fatal) << " cfgMixingNSelEdges needs at least two edges in strictly increasing order: '" << fConfigMixingNSelEdges .value << " '" ;
1880+ }
1881+ LOGF (info, " Event mixing restricted to classes of selected pairing tracks per event: %s" , fConfigMixingNSelEdges .value .c_str ());
1882+ }
18591883 if (fConfigRunMixingAcrossTFs ) {
18601884 if (fNCutsBarrel > MixingHandler::NMaxCuts) {
18611885 LOGF (fatal, " Across-TF mixing supports at most %d barrel track-cut bits, got %d" , MixingHandler::NMaxCuts, fNCutsBarrel );
@@ -2142,6 +2166,16 @@ struct AnalysisSameEventPairing {
21422166 if (groupedAssocs.size () == 0 ) {
21432167 continue ;
21442168 }
2169+ if constexpr (TPairType == VarManager::kDecayToEE || TPairType == VarManager::kDecayToPiPi || TPairType == VarManager::kDecayToKchKch || TPairType == VarManager::kDecayToPrAPr ) {
2170+ // number of tracks passing the pairing filter
2171+ int nSel = 0 ;
2172+ for (auto const & a : groupedAssocs) {
2173+ if (a.isBarrelSelected_raw () & a.isBarrelSelectedPrefilter_raw () & fTrackFilterMask ) {
2174+ nSel++;
2175+ }
2176+ }
2177+ VarManager::fgValues[VarManager::kNSelTracksForPairing ] = nSel;
2178+ }
21452179
21462180 if (fConfigOptions .useFlowReso ) {
21472181 if (ResoFlowSP == nullptr || ResoFlowEP == nullptr ) {
@@ -2152,6 +2186,13 @@ struct AnalysisSameEventPairing {
21522186 int mixingCategory = -1 ;
21532187 if (fConfigRunMixingAcrossTFs ) {
21542188 mixingCategory = fMixingHandler .FindEventCategory (dqtablereader_helpers::varValues ());
2189+ // mix only events in the same class of cfgMixingNSelEdges
2190+ if (mixingCategory >= 0 && !fMixNSelEdges .empty ()) {
2191+ const int nSel = static_cast <int >(VarManager::fgValues[VarManager::kNSelTracksForPairing ]);
2192+ const int nSelClassIndex = static_cast <int >(std::upper_bound (fMixNSelEdges .begin (), fMixNSelEdges .end (), nSel) - fMixNSelEdges .begin ()) - 1 ;
2193+ const int nNSelClasses = static_cast <int >(fMixNSelEdges .size ()) - 1 ;
2194+ mixingCategory = (nSelClassIndex >= 0 && nSelClassIndex < nNSelClasses) ? mixingCategory * nNSelClasses + nSelClassIndex : -1 ;
2195+ }
21552196 }
21562197
21572198 fNPairPerEvent = 0 ;
@@ -2675,6 +2716,7 @@ struct AnalysisSameEventPairing {
26752716 }
26762717 // 2) run the mixing with the events in the pool corresponding to this event
26772718 auto & pool = fMixingHandler .GetPool (mixingCategory);
2719+ int nMEPairs = 0 ;
26782720 for (auto const & poolEvent : pool.GetEvents ()) {
26792721 for (auto const & t1 : mixingEvent.tracks1 ) {
26802722 // run +- pairing
@@ -2685,6 +2727,7 @@ struct AnalysisSameEventPairing {
26852727 continue ;
26862728 }
26872729 VarManager::FillPairMEAcrossTFs (t1, t2);
2730+ nMEPairs++;
26882731 for (int icut = 0 ; icut < ncuts; icut++) {
26892732 if (mixedTwoTrackFilter & (static_cast <uint32_t >(1 ) << icut)) {
26902733 if (fConfigQA ) {
@@ -2715,6 +2758,7 @@ struct AnalysisSameEventPairing {
27152758 continue ;
27162759 }
27172760 VarManager::FillPairMEAcrossTFs (t1, t2);
2761+ nMEPairs++;
27182762 for (int icut = 0 ; icut < ncuts; icut++) {
27192763 if (mixedTwoTrackFilter & (static_cast <uint32_t >(1 ) << icut)) {
27202764 if (fConfigQA ) {
@@ -2733,6 +2777,7 @@ struct AnalysisSameEventPairing {
27332777 continue ;
27342778 }
27352779 VarManager::FillPairMEAcrossTFs (t1, t2);
2780+ nMEPairs++;
27362781 for (int icut = 0 ; icut < ncuts; icut++) {
27372782 if (mixedTwoTrackFilter & (static_cast <uint32_t >(1 ) << icut)) {
27382783 if (fConfigQA ) {
@@ -2762,6 +2807,7 @@ struct AnalysisSameEventPairing {
27622807 continue ;
27632808 }
27642809 VarManager::FillPairMEAcrossTFs (t1, t2);
2810+ nMEPairs++;
27652811 for (int icut = 0 ; icut < ncuts; icut++) {
27662812 if (mixedTwoTrackFilter & (static_cast <uint32_t >(1 ) << icut)) {
27672813 if (fConfigQA ) {
@@ -2772,6 +2818,10 @@ struct AnalysisSameEventPairing {
27722818 }
27732819 }
27742820 }
2821+ if (fEnableBarrelMixingHistos && fConfigQA ) {
2822+ VarManager::fgValues[VarManager::kNPairsPerEvent ] = nMEPairs;
2823+ fHistMan ->FillHistClass (" PairingMEQA" , dqtablereader_helpers::varValues ());
2824+ }
27752825 // 3) add the current event to the pool
27762826 pool.UpdatePool (mixingEvent, fMixingHandler .GetPoolDepth (), mixingEvent.filteringMask );
27772827 // pool.Print();
@@ -2964,27 +3014,72 @@ struct AnalysisSameEventPairing {
29643014 events.bindExternalIndices (&assocs);
29653015 int mixingDepth = fConfigMixingDepth .value ;
29663016 fAmbiguousPairs .clear ();
3017+ if constexpr (TPairType == VarManager::kDecayToEE || TPairType == VarManager::kDecayToPiPi || TPairType == VarManager::kDecayToKchKch || TPairType == VarManager::kDecayToPrAPr ) {
3018+ if (!fMixNSelEdges .empty ()) {
3019+ // mix only events in the same class of cfgMixingNSelEdges
3020+ const int nNSelClasses = static_cast <int >(fMixNSelEdges .size ()) - 1 ;
3021+ std::map<int , std::vector<decltype (events.begin ())>> nSelPools;
3022+ // fill the pools with events
3023+ for (auto const & event : events) {
3024+ int nSel = 0 ;
3025+ for (auto const & a : assocs.sliceBy (preSlice, event.globalIndex ())) {
3026+ if (a.isBarrelSelected_raw () & a.isBarrelSelectedPrefilter_raw () & fTrackFilterMask ) {
3027+ nSel++;
3028+ }
3029+ }
3030+ const int nSelClassIndex = static_cast <int >(std::upper_bound (fMixNSelEdges .begin (), fMixNSelEdges .end (), nSel) - fMixNSelEdges .begin ()) - 1 ;
3031+ if (nSelClassIndex >= 0 && nSelClassIndex < nNSelClasses && event.mixingHash () >= 0 ) {
3032+ nSelPools[event.mixingHash () * nNSelClasses + nSelClassIndex].push_back (event);
3033+ }
3034+ }
3035+ // same sliding window as selfCombinations
3036+ for (auto const & [key, poolEvents] : nSelPools) {
3037+ for (size_t i = 0 ; i < poolEvents.size (); i++) {
3038+ for (size_t j = i + 1 ; j < poolEvents.size () && j <= i + static_cast <size_t >(mixingDepth); j++) {
3039+ mixTwoEvents<TPairType, TEventFillMap>(poolEvents[i], poolEvents[j], events, assocs, tracks, preSlice);
3040+ }
3041+ }
3042+ }
3043+ return ;
3044+ }
3045+ }
29673046 for (auto const & [event1, event2] : selfCombinations (hashBin, mixingDepth, -1 , events, events)) {
2968- VarManager::ResetValues (0 , VarManager::kNVars );
2969- VarManager::FillEvent<TEventFillMap>(event1, dqtablereader_helpers::varValues ());
3047+ mixTwoEvents<TPairType, TEventFillMap>(event1, event2, events, assocs, tracks, preSlice);
3048+ }
3049+ }
29703050
2971- auto assocs1 = assocs.sliceBy (preSlice, event1.globalIndex ());
2972- assocs1.bindExternalIndices (&events);
3051+ // mix one pair of events
3052+ template <int TPairType, uint32_t TEventFillMap, typename TEvent, typename TEvents, typename TAssocs, typename TTracks>
3053+ void mixTwoEvents (TEvent const & event1, TEvent const & event2, TEvents& events, TAssocs const & assocs, TTracks const & tracks, o2::framework::Preslice<TAssocs>& preSlice)
3054+ {
3055+ VarManager::ResetValues (0 , VarManager::kNVars );
3056+ VarManager::FillEvent<TEventFillMap>(event1, dqtablereader_helpers::varValues ());
29733057
2974- auto assocs2 = assocs.sliceBy (preSlice, event2 .globalIndex ());
2975- assocs2 .bindExternalIndices (&events);
3058+ auto assocs1 = assocs.sliceBy (preSlice, event1 .globalIndex ());
3059+ assocs1 .bindExternalIndices (&events);
29763060
2977- fNPairPerEvent = 0 ;
2978- VarManager::FillTwoMixEvents<TEventFillMap>(event1, event2, assocs1, assocs2);
2979- if (fConfigOptions .useFlowReso ) {
2980- VarManager::FillTwoMixEventsFlowResoFactor (ResoFlowSP, ResoFlowEP);
2981- }
2982- runMixedPairing<TPairType, TEventFillMap>(assocs1, assocs2, tracks, tracks);
2983- VarManager::fgValues[VarManager::kNPairsPerEvent ] = fNPairPerEvent ;
2984- if (fEnableBarrelMixingHistos && fConfigQA ) {
2985- fHistMan ->FillHistClass (" PairingMEQA" , dqtablereader_helpers::varValues ());
3061+ auto assocs2 = assocs.sliceBy (preSlice, event2.globalIndex ());
3062+ assocs2.bindExternalIndices (&events);
3063+
3064+ fNPairPerEvent = 0 ;
3065+ VarManager::FillTwoMixEvents<TEventFillMap>(event1, event2, assocs1, assocs2);
3066+ if constexpr (TPairType == VarManager::kDecayToEE || TPairType == VarManager::kDecayToPiPi || TPairType == VarManager::kDecayToKchKch || TPairType == VarManager::kDecayToPrAPr ) {
3067+ int nSel1 = 0 ; // number of tracks of event1 passing the pairing filter
3068+ for (auto const & a : assocs1) {
3069+ if (a.isBarrelSelected_raw () & a.isBarrelSelectedPrefilter_raw () & fTrackFilterMask ) {
3070+ nSel1++;
3071+ }
29863072 }
2987- } // end event loop
3073+ VarManager::fgValues[VarManager::kNSelTracksForPairing ] = nSel1;
3074+ }
3075+ if (fConfigOptions .useFlowReso ) {
3076+ VarManager::FillTwoMixEventsFlowResoFactor (ResoFlowSP, ResoFlowEP);
3077+ }
3078+ runMixedPairing<TPairType, TEventFillMap>(assocs1, assocs2, tracks, tracks);
3079+ VarManager::fgValues[VarManager::kNPairsPerEvent ] = fNPairPerEvent ;
3080+ if (fEnableBarrelMixingHistos && fConfigQA ) {
3081+ fHistMan ->FillHistClass (" PairingMEQA" , dqtablereader_helpers::varValues ());
3082+ }
29883083 }
29893084
29903085 template <bool TTwoProngFitter, int TPairType, uint32_t TEventFillMap, uint32_t TTrackFillMap, uint32_t TMuonFillMap, typename TEvents, typename TTrackAssocs, typename TTracks, typename TMuonAssocs, typename TMuons>
0 commit comments