Skip to content

Commit 048d727

Browse files
committed
[PWGEM] reduce memory usage in EMH
1 parent 28ce25d commit 048d727

7 files changed

Lines changed: 82 additions & 51 deletions

File tree

‎PWGEM/Dilepton/Core/Dilepton.h‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,11 +1534,11 @@ struct Dilepton {
15341534
std::pair<int, int> key_df_collision = std::make_pair(ndf, collision.globalIndex()); // this gives the current event.
15351535

15361536
// make a vector of selected photons in this collision.
1537-
auto selected_posTracks_in_this_event = emh_pos->GetTracksPerCollision(key_df_collision);
1538-
auto selected_negTracks_in_this_event = emh_neg->GetTracksPerCollision(key_df_collision);
1537+
const auto& selected_posTracks_in_this_event = emh_pos->GetTracksPerCollision(key_df_collision);
1538+
const auto& selected_negTracks_in_this_event = emh_neg->GetTracksPerCollision(key_df_collision);
15391539
// LOGF(info, "N selected tracks in current event (%d, %d), zvtx = %f, centrality = %f , npos = %d , nneg = %d, nuls = %d , nlspp = %d, nlsmm = %d", ndf, collision.globalIndex(), collision.posZ(), centralities[cfgCentEstimator], selected_posTracks_in_this_event.size(), selected_negTracks_in_this_event.size(), nuls, nlspp, nlsmm);
15401540

1541-
auto collisionIds_in_mixing_pool = emh_pos->GetCollisionIdsFromEventPool(key_bin); // pos/neg does not matter.
1541+
const auto& collisionIds_in_mixing_pool = emh_pos->GetCollisionIdsFromEventPool(key_bin); // pos/neg does not matter.
15421542
// LOGF(info, "collisionIds_in_mixing_pool.size() = %d", collisionIds_in_mixing_pool.size());
15431543

15441544
for (const auto& mix_dfId_collisionId : collisionIds_in_mixing_pool) {
@@ -1555,8 +1555,8 @@ struct Dilepton {
15551555
continue;
15561556
}
15571557

1558-
auto posTracks_from_event_pool = emh_pos->GetTracksPerCollision(mix_dfId_collisionId);
1559-
auto negTracks_from_event_pool = emh_neg->GetTracksPerCollision(mix_dfId_collisionId);
1558+
const auto& posTracks_from_event_pool = emh_pos->GetTracksPerCollision(mix_dfId_collisionId);
1559+
const auto& negTracks_from_event_pool = emh_neg->GetTracksPerCollision(mix_dfId_collisionId);
15601560
// LOGF(info, "Do event mixing: current event (%d, %d) | event pool (%d, %d), npos = %d , nneg = %d", ndf, collision.globalIndex(), mix_dfId, mix_collisionId, posTracks_from_event_pool.size(), negTracks_from_event_pool.size());
15611561

15621562
for (const auto& pos : selected_posTracks_in_this_event) { // ULS mix
@@ -1586,8 +1586,15 @@ struct Dilepton {
15861586

15871587
if (nuls > 0 || nlspp > 0 || nlsmm > 0) {
15881588
map_mixed_eventId_to_globalBC[key_df_collision] = collision.globalBC();
1589-
emh_pos->AddCollisionIdAtLast(key_bin, key_df_collision);
1590-
emh_neg->AddCollisionIdAtLast(key_bin, key_df_collision);
1589+
const auto evicted_pos = emh_pos->AddCollisionIdAtLast(key_bin, key_df_collision);
1590+
const auto evicted_neg = emh_neg->AddCollisionIdAtLast(key_bin, key_df_collision);
1591+
if (evicted_pos != evicted_neg) {
1592+
LOGF(fatal, "Positive and negative mixing pools are out of sync");
1593+
}
1594+
if (evicted_pos) {
1595+
map_mixed_eventId_to_globalBC.erase(*evicted_pos);
1596+
}
1597+
15911598
} // end of if pair exist
15921599

15931600
} // end of collision loop

‎PWGEM/Dilepton/Core/DileptonHadronMPC.h‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,10 +1178,10 @@ struct DileptonHadronMPC {
11781178
std::tuple<int, int, int, int> key_bin = std::make_tuple(zbin, centbin, epbin, occbin);
11791179

11801180
// make a vector of selected electrons in this collision.
1181-
auto selected_posTracks_in_this_event = emh_pos->GetTracksPerCollision(key_df_collision);
1182-
auto selected_negTracks_in_this_event = emh_neg->GetTracksPerCollision(key_df_collision);
1181+
const auto& selected_posTracks_in_this_event = emh_pos->GetTracksPerCollision(key_df_collision);
1182+
const auto& selected_negTracks_in_this_event = emh_neg->GetTracksPerCollision(key_df_collision);
11831183

1184-
auto collisionIds_in_mixing_pool = emh_pos->GetCollisionIdsFromEventPool(key_bin); // pos/neg does not matter.
1184+
const auto& collisionIds_in_mixing_pool = emh_pos->GetCollisionIdsFromEventPool(key_bin); // pos/neg does not matter.
11851185

11861186
// LOGF(info, "selected_posTracks_in_this_event.size() = %d, selected_negTracks_in_this_event.size() = %d, collisionIds_in_mixing_pool.size() = %d", selected_posTracks_in_this_event.size(), selected_negTracks_in_this_event.size(), collisionIds_in_mixing_pool.size());
11871187

@@ -1201,8 +1201,8 @@ struct DileptonHadronMPC {
12011201
continue;
12021202
}
12031203

1204-
auto posTracks_from_event_pool = emh_pos->GetTracksPerCollision(mix_dfId_collisionId);
1205-
auto negTracks_from_event_pool = emh_neg->GetTracksPerCollision(mix_dfId_collisionId);
1204+
const auto& posTracks_from_event_pool = emh_pos->GetTracksPerCollision(mix_dfId_collisionId);
1205+
const auto& negTracks_from_event_pool = emh_neg->GetTracksPerCollision(mix_dfId_collisionId);
12061206
// LOGF(info, "posTracks_from_event_pool.size() = %d, negTracks_from_event_pool.size() = %d", posTracks_from_event_pool.size(), negTracks_from_event_pool.size());
12071207

12081208
for (const auto& pos : selected_posTracks_in_this_event) { // ULS mix
@@ -1233,8 +1233,8 @@ struct DileptonHadronMPC {
12331233
} // end of loop over mixed event pool for lepton-lepton
12341234

12351235
if (cfgAnalysisType == static_cast<int>(o2::aod::pwgem::dilepton::utils::pairutil::DileptonHadronAnalysisType::kAzimuthalCorrelation)) {
1236-
auto selected_refTracks_in_this_event = emh_ref->GetTracksPerCollision(key_df_collision);
1237-
auto collisionIds_in_mixing_pool_hadron = emh_ref->GetCollisionIdsFromEventPool(key_bin);
1236+
const auto& selected_refTracks_in_this_event = emh_ref->GetTracksPerCollision(key_df_collision);
1237+
const auto& collisionIds_in_mixing_pool_hadron = emh_ref->GetCollisionIdsFromEventPool(key_bin);
12381238

12391239
// for ULS and hadron mix
12401240
for (const auto& pos : selected_posTracks_in_this_event) {
@@ -1253,7 +1253,7 @@ struct DileptonHadronMPC {
12531253
continue;
12541254
}
12551255

1256-
auto refTracks_from_event_pool = emh_ref->GetTracksPerCollision(mix_dfId_collisionId);
1256+
const auto& refTracks_from_event_pool = emh_ref->GetTracksPerCollision(mix_dfId_collisionId);
12571257
for (const auto& ref : refTracks_from_event_pool) {
12581258
fillDileptonHadron<1>(pos, neg, cut, ref);
12591259
}
@@ -1283,7 +1283,7 @@ struct DileptonHadronMPC {
12831283
continue;
12841284
}
12851285

1286-
auto refTracks_from_event_pool = emh_ref->GetTracksPerCollision(mix_dfId_collisionId);
1286+
const auto& refTracks_from_event_pool = emh_ref->GetTracksPerCollision(mix_dfId_collisionId);
12871287
for (const auto& ref : refTracks_from_event_pool) {
12881288
fillDileptonHadron<1>(pos1, pos2, cut, ref);
12891289
}
@@ -1311,7 +1311,7 @@ struct DileptonHadronMPC {
13111311
continue;
13121312
}
13131313

1314-
auto refTracks_from_event_pool = emh_ref->GetTracksPerCollision(mix_dfId_collisionId);
1314+
const auto& refTracks_from_event_pool = emh_ref->GetTracksPerCollision(mix_dfId_collisionId);
13151315
for (const auto& ref : refTracks_from_event_pool) {
13161316
fillDileptonHadron<1>(neg1, neg2, cut, ref);
13171317
}
@@ -1335,7 +1335,7 @@ struct DileptonHadronMPC {
13351335
continue;
13361336
}
13371337

1338-
auto refTracks_from_event_pool = emh_ref->GetTracksPerCollision(mix_dfId_collisionId);
1338+
const auto& refTracks_from_event_pool = emh_ref->GetTracksPerCollision(mix_dfId_collisionId);
13391339
// LOGF(info, "selected_refTracks_in_this_event.size() = %d, collisionIds_in_mixing_pool_hadron.size() = %d, refTracks_from_event_pool.size() = %d", selected_refTracks_in_this_event.size(), collisionIds_in_mixing_pool_hadron.size(), refTracks_from_event_pool.size());
13401340
for (const auto& ref1 : selected_refTracks_in_this_event) { // ref-ref mix
13411341
for (const auto& ref2 : refTracks_from_event_pool) {

‎PWGEM/Dilepton/Core/DileptonSV.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,11 +1695,11 @@ struct DileptonSV {
16951695
std::pair<int, int> key_df_collision = std::make_pair(ndf, collision.globalIndex()); // this gives the current event.
16961696

16971697
// make a vector of selected photons in this collision.
1698-
auto selected_posTracks_in_this_event = emh_pos->GetTracksPerCollision(key_df_collision);
1699-
auto selected_negTracks_in_this_event = emh_neg->GetTracksPerCollision(key_df_collision);
1698+
const auto& selected_posTracks_in_this_event = emh_pos->GetTracksPerCollision(key_df_collision);
1699+
const auto& selected_negTracks_in_this_event = emh_neg->GetTracksPerCollision(key_df_collision);
17001700
// LOGF(info, "N selected tracks in current event (%d, %d), zvtx = %f, centrality = %f , npos = %d , nneg = %d, nuls = %d , nlspp = %d, nlsmm = %d", ndf, collision.globalIndex(), collision.posZ(), centralities[cfgCentEstimator], selected_posTracks_in_this_event.size(), selected_negTracks_in_this_event.size(), nuls, nlspp, nlsmm);
17011701

1702-
auto collisionIds_in_mixing_pool = emh_pos->GetCollisionIdsFromEventPool(key_bin); // pos/neg does not matter.
1702+
const auto& collisionIds_in_mixing_pool = emh_pos->GetCollisionIdsFromEventPool(key_bin); // pos/neg does not matter.
17031703
// LOGF(info, "collisionIds_in_mixing_pool.size() = %d", collisionIds_in_mixing_pool.size());
17041704

17051705
for (const auto& mix_dfId_collisionId : collisionIds_in_mixing_pool) {
@@ -1717,8 +1717,8 @@ struct DileptonSV {
17171717
}
17181718
fRegistry.fill(HIST("Event/hsMixCounter"), collision.posZ(), centrality, ep2, occupancy);
17191719

1720-
auto posTracks_from_event_pool = emh_pos->GetTracksPerCollision(mix_dfId_collisionId);
1721-
auto negTracks_from_event_pool = emh_neg->GetTracksPerCollision(mix_dfId_collisionId);
1720+
const auto& posTracks_from_event_pool = emh_pos->GetTracksPerCollision(mix_dfId_collisionId);
1721+
const auto& negTracks_from_event_pool = emh_neg->GetTracksPerCollision(mix_dfId_collisionId);
17221722
// LOGF(info, "Do event mixing: current event (%d, %d) | event pool (%d, %d), npos = %d , nneg = %d", ndf, collision.globalIndex(), mix_dfId, mix_collisionId, posTracks_from_event_pool.size(), negTracks_from_event_pool.size());
17231723

17241724
for (const auto& pos : selected_posTracks_in_this_event) { // ULS mix

‎PWGEM/Dilepton/Utils/EventMixingHandler.h‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#define PWGEM_DILEPTON_UTILS_EVENTMIXINGHANDLER_H_
1717

1818
#include <map>
19+
#include <optional>
20+
#include <stdexcept>
1921
#include <vector>
2022

2123
namespace o2::aod::pwgem::dilepton::utils
@@ -36,6 +38,9 @@ class EventMixingHandler
3638
fNdepth = ndepth;
3739
fMapMixBins.clear();
3840
fMap_Tracks_per_collision.clear();
41+
if (fNdepth <= 0) {
42+
throw std::invalid_argument("mixing depth must be positive");
43+
}
3944
}
4045

4146
~EventMixingHandler()
@@ -44,7 +49,13 @@ class EventMixingHandler
4449
fMap_Tracks_per_collision.clear();
4550
}
4651

47-
void SetNdepth(int ndepth) { fNdepth = ndepth; }
52+
void SetNdepth(int ndepth)
53+
{
54+
if (ndepth <= 0) {
55+
throw std::invalid_argument("mixing depth must be positive");
56+
}
57+
fNdepth = ndepth;
58+
}
4859

4960
void ReserveNTracksPerCollision(U key_df_collision, int ntrack)
5061
{
@@ -56,20 +67,33 @@ class EventMixingHandler
5667
fMap_Tracks_per_collision[key_df_collision].emplace_back(obj);
5768
}
5869

59-
std::vector<U> GetCollisionIdsFromEventPool(T key_bin) { return fMapMixBins[key_bin]; }
60-
std::vector<V> GetTracksPerCollision(T key_bin, int index) { return fMap_Tracks_per_collision[fMapMixBins[key_bin][index]]; }
61-
std::vector<V> GetTracksPerCollision(U key_df_collision) { return fMap_Tracks_per_collision[key_df_collision]; }
70+
const std::vector<U>& GetCollisionIdsFromEventPool(const T& key_bin) const
71+
{
72+
const auto it = fMapMixBins.find(key_bin);
73+
static const std::vector<U> empty;
74+
return it == fMapMixBins.end() ? empty : it->second;
75+
}
76+
77+
const std::vector<V>& GetTracksPerCollision(const U& key) const
78+
{
79+
const auto it = fMap_Tracks_per_collision.find(key);
80+
static const std::vector<V> empty;
81+
return it == fMap_Tracks_per_collision.end() ? empty : it->second;
82+
}
6283

6384
// call this function at the end of collision loop
64-
void AddCollisionIdAtLast(T key_bin, U key_df_collision)
85+
std::optional<U> AddCollisionIdAtLast(T key_bin, U key_df_collision)
6586
{
6687
// LOGF(info, "fMapMixBins[key_bin].size() = %d", fMapMixBins[key_bin].size());
67-
if (static_cast<int>(fMapMixBins[key_bin].size()) >= fNdepth) {
68-
fMap_Tracks_per_collision[fMapMixBins[key_bin][0]].clear();
69-
fMap_Tracks_per_collision[fMapMixBins[key_bin][0]].shrink_to_fit();
70-
fMapMixBins[key_bin].erase(fMapMixBins[key_bin].begin());
88+
auto& ids = fMapMixBins[key_bin];
89+
std::optional<U> evicted;
90+
if (static_cast<int>(ids.size()) >= fNdepth) {
91+
evicted = ids.front();
92+
fMap_Tracks_per_collision.erase(*evicted);
93+
ids.erase(ids.begin());
7194
}
72-
fMapMixBins[key_bin].emplace_back(key_df_collision);
95+
ids.emplace_back(key_df_collision);
96+
return evicted;
7397
}
7498

7599
private:

‎PWGEM/PhotonMeson/Core/Pi0EtaToGammaGamma.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,11 +1007,11 @@ struct Pi0EtaToGammaGamma {
10071007
}
10081008

10091009
// make a vector of selected photons in this collision.
1010-
auto selected_photons1_in_this_event = emh1->GetTracksPerCollision(key_df_collision);
1011-
auto selected_photons2_in_this_event = emh2->GetTracksPerCollision(key_df_collision);
1010+
const auto& selected_photons1_in_this_event = emh1->GetTracksPerCollision(key_df_collision);
1011+
const auto& selected_photons2_in_this_event = emh2->GetTracksPerCollision(key_df_collision);
10121012

1013-
auto collisionIds1_in_mixing_pool = emh1->GetCollisionIdsFromEventPool(key_bin);
1014-
auto collisionIds2_in_mixing_pool = emh2->GetCollisionIdsFromEventPool(key_bin);
1013+
const auto& collisionIds1_in_mixing_pool = emh1->GetCollisionIdsFromEventPool(key_bin);
1014+
const auto& collisionIds2_in_mixing_pool = emh2->GetCollisionIdsFromEventPool(key_bin);
10151015

10161016
if constexpr (pairtype == o2::aod::pwgem::photonmeson::photonpair::PairType::kPCMPCM || pairtype == o2::aod::pwgem::photonmeson::photonpair::PairType::kPHOSPHOS || pairtype == o2::aod::pwgem::photonmeson::photonpair::PairType::kEMCEMC) { // same kinds pairing
10171017
for (const auto& mix_dfId_collisionId : collisionIds1_in_mixing_pool) {
@@ -1029,7 +1029,7 @@ struct Pi0EtaToGammaGamma {
10291029
continue;
10301030
}
10311031

1032-
auto photons1_from_event_pool = emh1->GetTracksPerCollision(mix_dfId_collisionId);
1032+
const auto& photons1_from_event_pool = emh1->GetTracksPerCollision(mix_dfId_collisionId);
10331033
// LOGF(info, "Do event mixing: current event (%d, %d), ngamma = %d | event pool (%d, %d), ngamma = %d", ndf, collision.globalIndex(), selected_photons1_in_this_event.size(), mix_dfId, mix_collisionId, photons1_from_event_pool.size());
10341034

10351035
for (const auto& g1 : selected_photons1_in_this_event) {
@@ -1093,7 +1093,7 @@ struct Pi0EtaToGammaGamma {
10931093
continue;
10941094
}
10951095

1096-
auto photons2_from_event_pool = emh2->GetTracksPerCollision(mix_dfId_collisionId);
1096+
const auto& photons2_from_event_pool = emh2->GetTracksPerCollision(mix_dfId_collisionId);
10971097
// LOGF(info, "Do event mixing: current event (%d, %d), ngamma = %d | event pool (%d, %d), nll = %d", ndf, collision.globalIndex(), selected_photons1_in_this_event.size(), mix_dfId, mix_collisionId, photons2_from_event_pool.size());
10981098

10991099
for (const auto& g1 : selected_photons1_in_this_event) {
@@ -1130,7 +1130,7 @@ struct Pi0EtaToGammaGamma {
11301130
continue;
11311131
}
11321132

1133-
auto photons1_from_event_pool = emh1->GetTracksPerCollision(mix_dfId_collisionId);
1133+
const auto& photons1_from_event_pool = emh1->GetTracksPerCollision(mix_dfId_collisionId);
11341134
// LOGF(info, "Do event mixing: current event (%d, %d), nll = %d | event pool (%d, %d), ngamma = %d", ndf, collision.globalIndex(), selected_photons2_in_this_event.size(), mix_dfId, mix_collisionId, photons1_from_event_pool.size());
11351135

11361136
for (const auto& g1 : selected_photons2_in_this_event) {

‎PWGEM/PhotonMeson/Core/TaggingPi0.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,11 @@ struct TaggingPi0 {
599599
}
600600

601601
// make a vector of selected photons in this collision.
602-
auto selected_photons1_in_this_event = emh1->GetTracksPerCollision(key_df_collision);
603-
// auto selected_photons2_in_this_event = emh2->GetTracksPerCollision(key_df_collision);
602+
const auto& selected_photons1_in_this_event = emh1->GetTracksPerCollision(key_df_collision);
603+
// const auto& selected_photons2_in_this_event = emh2->GetTracksPerCollision(key_df_collision);
604604

605-
// auto collisionIds1_in_mixing_pool = emh1->GetCollisionIdsFromEventPool(key_bin);
606-
auto collisionIds2_in_mixing_pool = emh2->GetCollisionIdsFromEventPool(key_bin);
605+
// const auto& collisionIds1_in_mixing_pool = emh1->GetCollisionIdsFromEventPool(key_bin);
606+
const auto& collisionIds2_in_mixing_pool = emh2->GetCollisionIdsFromEventPool(key_bin);
607607

608608
for (const auto& mix_dfId_collisionId : collisionIds2_in_mixing_pool) {
609609
int mix_dfId = mix_dfId_collisionId.first;
@@ -620,7 +620,7 @@ struct TaggingPi0 {
620620
continue;
621621
}
622622

623-
auto photons2_from_event_pool = emh2->GetTracksPerCollision(mix_dfId_collisionId);
623+
const auto& photons2_from_event_pool = emh2->GetTracksPerCollision(mix_dfId_collisionId);
624624
// LOGF(info, "Do event mixing: current event (%d, %d), ngamma = %d | event pool (%d, %d), nll = %d", ndf, collision.globalIndex(), selected_photons1_in_this_event.size(), mix_dfId, mix_collisionId, photons2_from_event_pool.size());
625625

626626
for (const auto& g1 : selected_photons1_in_this_event) { // [photon from event1, dilepton from event2]

‎PWGEM/PhotonMeson/Tasks/photonhbt.cxx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,8 +2782,8 @@ struct Photonhbt {
27822782
if (!mixing.cfgDoMix || ndiphoton == 0) {
27832783
continue;
27842784
}
2785-
auto selectedPhotons = emh1->GetTracksPerCollision(keyDFCollision);
2786-
auto poolIDs = emh1->GetCollisionIdsFromEventPool(keyBin);
2785+
const auto& selectedPhotons = emh1->GetTracksPerCollision(keyDFCollision);
2786+
const auto& poolIDs = emh1->GetCollisionIdsFromEventPool(keyBin);
27872787
for (const auto& mixID : poolIDs) {
27882788
if (mixID.second == collision.globalIndex() && mixID.first == ndf) {
27892789
continue;
@@ -2794,7 +2794,7 @@ struct Photonhbt {
27942794
if (diffBC < mixing.ndiffBCMix) {
27952795
continue;
27962796
}
2797-
auto poolPhotons = emh1->GetTracksPerCollision(mixID);
2797+
const auto& poolPhotons = emh1->GetTracksPerCollision(mixID);
27982798
for (const auto& g1 : selectedPhotons) {
27992799
for (const auto& g2 : poolPhotons) {
28002800
if (!passAsymmetryCut(g1.pt(), g2.pt())) {
@@ -3102,8 +3102,8 @@ struct Photonhbt {
31023102
if (!mixing.cfgDoMix || ndiphoton == 0) {
31033103
continue;
31043104
}
3105-
auto selectedPhotons = emh1->GetTracksPerCollision(keyDFCollision);
3106-
auto poolIDs = emh1->GetCollisionIdsFromEventPool(keyBin);
3105+
const auto& selectedPhotons = emh1->GetTracksPerCollision(keyDFCollision);
3106+
const auto& poolIDs = emh1->GetCollisionIdsFromEventPool(keyBin);
31073107
for (const auto& mixID : poolIDs) {
31083108
if (mixID.second == collision.globalIndex() && mixID.first == ndf) {
31093109
continue;
@@ -3114,7 +3114,7 @@ struct Photonhbt {
31143114
if (diffBC < mixing.ndiffBCMix) {
31153115
continue;
31163116
}
3117-
auto poolPhotons = emh1->GetTracksPerCollision(mixID);
3117+
const auto& poolPhotons = emh1->GetTracksPerCollision(mixID);
31183118
for (const auto& g1 : selectedPhotons) {
31193119
for (const auto& g2 : poolPhotons) {
31203120
if (!passAsymmetryCut(g1.pt(), g2.pt())) {

0 commit comments

Comments
 (0)