Skip to content

Commit 9640428

Browse files
authored
[PWGLF] Reduce MC memory usage: reuse vectors per event and shrink every 1000 events (#13342)
1 parent f2e857a commit 9640428

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

PWGLF/Tasks/Nuspex/antinucleiInJets.cxx

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ struct AntinucleiInJets {
151151
Configurable<std::string> weightsXi{"weightsXi", "", "weightsXi"};
152152
Configurable<std::string> weightsOmega{"weightsOmega", "", "weightsOmega"};
153153

154+
// Number of events
155+
Configurable<int> shrinkInterval{"shrinkInterval", 1000, "variable that controls how often shrinking happens"};
156+
154157
// Reweighting histograms
155158
TH1F* primaryAntiprotons;
156159
TH1F* primaryAntiLambda;
@@ -1616,9 +1619,23 @@ struct AntinucleiInJets {
16161619
// Generated events
16171620
void processJetsMCgen(GenCollisionsMc const& collisions, aod::McParticles const& mcParticles)
16181621
{
1622+
// Define per-event particle containers
1623+
std::vector<fastjet::PseudoJet> fjParticles;
1624+
std::vector<TVector3> protonMomentum;
1625+
1626+
// Event counter
1627+
int eventCounter = 0;
1628+
16191629
// Loop over all simulated collisions
16201630
for (const auto& collision : collisions) {
16211631

1632+
// Increment event counter
1633+
eventCounter++;
1634+
1635+
// Clear containers at the start of the event loop
1636+
fjParticles.clear();
1637+
protonMomentum.clear();
1638+
16221639
// Event counter: before event selection
16231640
registryMC.fill(HIST("genEvents"), 0.5);
16241641

@@ -1629,9 +1646,7 @@ struct AntinucleiInJets {
16291646
// Event counter: after event selection
16301647
registryMC.fill(HIST("genEvents"), 1.5);
16311648

1632-
// Loop over all MC particles
1633-
std::vector<fastjet::PseudoJet> fjParticles;
1634-
std::vector<TVector3> protonMomentum;
1649+
// Loop over MC particles
16351650
for (const auto& particle : mcParticles) {
16361651

16371652
// Select physical primaries within acceptance
@@ -1644,7 +1659,7 @@ struct AntinucleiInJets {
16441659
// Store 3-momentum vectors of antiprotons for further analysis
16451660
if (particle.pdgCode() == PDG_t::kProtonBar) {
16461661
TVector3 pVec(particle.px(), particle.py(), particle.pz());
1647-
protonMomentum.push_back(pVec);
1662+
protonMomentum.emplace_back(pVec);
16481663
}
16491664

16501665
// 4-momentum representation of a particle
@@ -1754,16 +1769,36 @@ struct AntinucleiInJets {
17541769
if (isAtLeastOneJetSelected) {
17551770
registryMC.fill(HIST("genEvents"), 3.5);
17561771
}
1772+
1773+
// Shrink large vectors
1774+
if (eventCounter % shrinkInterval == 0) {
1775+
fjParticles.shrink_to_fit();
1776+
protonMomentum.shrink_to_fit();
1777+
}
17571778
}
17581779
}
17591780
PROCESS_SWITCH(AntinucleiInJets, processJetsMCgen, "process jets mc gen", false);
17601781

17611782
// Reconstructed events
17621783
void processJetsMCrec(RecCollisionsMc const& collisions, AntiNucleiTracksMc const& mcTracks, McParticles const&)
17631784
{
1785+
// Define per-event containers
1786+
std::vector<fastjet::PseudoJet> fjParticles;
1787+
std::vector<int> antiprotonTrackIndex;
1788+
1789+
// Event counter
1790+
int eventCounter = 0;
1791+
17641792
// Loop over all reconstructed collisions
17651793
for (const auto& collision : collisions) {
17661794

1795+
// Increment event counter
1796+
eventCounter++;
1797+
1798+
// Clear containers at the start of the event loop
1799+
fjParticles.clear();
1800+
antiprotonTrackIndex.clear();
1801+
17671802
// Event counter: before event selection
17681803
registryMC.fill(HIST("recEvents"), 0.5);
17691804

@@ -1806,8 +1841,6 @@ struct AntinucleiInJets {
18061841

18071842
// Loop over reconstructed tracks
18081843
int id(-1);
1809-
std::vector<fastjet::PseudoJet> fjParticles;
1810-
std::vector<int> antiprotonTrackIndex;
18111844
for (auto const& track : mcTracks) {
18121845
id++;
18131846

@@ -2028,6 +2061,12 @@ struct AntinucleiInJets {
20282061
if (isAtLeastOneJetSelected) {
20292062
registryMC.fill(HIST("recEvents"), 9.5);
20302063
}
2064+
2065+
// Shrink large vectors
2066+
if (eventCounter % shrinkInterval == 0) {
2067+
fjParticles.shrink_to_fit();
2068+
antiprotonTrackIndex.shrink_to_fit();
2069+
}
20312070
}
20322071
}
20332072
PROCESS_SWITCH(AntinucleiInJets, processJetsMCrec, "process jets MC rec", false);

0 commit comments

Comments
 (0)