Skip to content

Commit 3c27650

Browse files
sawenzelclaude
andcommitted
Move instead of copy when merging hits in the o2-sim hit merger
This removes a deep copy of every hit when the hit merger joins the sub-events of an event. - mergeAndAdjustHits copied each hit into the merged container; it now reserves the total size and moves them. - The track and track-reference merges now reserve their output. - TPC HitGroup declared a defaulted destructor, which suppresses its implicit move; the line is removed so HitGroup moves its five vectors. - The printf debug output in the merge loops is dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f019420 commit 3c27650

3 files changed

Lines changed: 17 additions & 11 deletions

File tree

‎Detectors/Base/include/DetectorsBase/Detector.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,22 +457,25 @@ class DetImpl : public o2::base::Detector
457457
{
458458
auto entries = hitbuffervector.size();
459459

460-
auto targetdata = new T; // used to collect data inside a single container
460+
T targetdata; // used to collect data inside a single container
461461
T* filladdress = nullptr; // pointer used for final ROOT IO
462462
if (entries == 1) {
463463
filladdress = hitbuffervector[0].get();
464464
// nothing to do; we can directly do IO from the existing buffer
465465
} else {
466466
// here we need to do merging and index adjustment
467467
int nprimTot = 0;
468+
size_t nhits = 0;
468469
for (auto entry = 0; entry < entries; entry++) {
469470
nprimTot += nprimaries[entry];
471+
nhits += hitbuffervector[entry] ? hitbuffervector[entry]->size() : 0;
470472
}
473+
targetdata.reserve(nhits);
471474
// offset for pimary track index
472475
int idelta0 = 0;
473476
// offset for secondary track index
474477
int idelta1 = nprimTot;
475-
filladdress = targetdata;
478+
filladdress = &targetdata;
476479
for (int entry = entries - 1; entry >= 0; --entry) {
477480
// proceed in the order of subevent Ids
478481
int index = subevtsOrdered[entry];
@@ -487,8 +490,8 @@ class DetImpl : public o2::base::Detector
487490
for (auto& hit : *incomingdata) {
488491
hit.SetTrackID(offsetTrackIndex(hit.GetTrackID(), nprim, idelta0, idelta1));
489492
}
490-
// this could be further generalized by using a policy for T
491-
std::copy(incomingdata->begin(), incomingdata->end(), std::back_inserter(*targetdata));
493+
// move rather than copy, since hits may own memory themselves (e.g. TPC HitGroup)
494+
targetdata.insert(targetdata.end(), std::make_move_iterator(incomingdata->begin()), std::make_move_iterator(incomingdata->end()));
492495
}
493496
// adjust offsets for next subevent
494497
idelta0 += nprim;
@@ -500,10 +503,7 @@ class DetImpl : public o2::base::Detector
500503
targetbr->SetAddress(&filladdress);
501504
targetbr->Fill();
502505
targetbr->ResetAddress();
503-
targetdata->clear();
504-
hitbuffervector.clear();
505506
hitbuffervector = L(); // swap with empty vector to release mem
506-
delete targetdata;
507507
}
508508

509509
void mergeHitEntries(TTree& origin, TTree& target, std::vector<int> const& trackoffsets, std::vector<int> const& nprimaries, std::vector<int> const& subevtsOrdered) final

‎Detectors/TPC/simulation/include/TPCSimulation/Point.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ class HitGroup : public o2::BaseHit
115115
{
116116
}
117117

118-
~HitGroup() = default;
119-
120118
void addHit(float x, float y, float z, float time, float e)
121119
{
122120
#ifdef HIT_AOS

‎run/O2HitMerger.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,18 @@ class O2HitMerger : public fair::mq::Device
489489
const auto entries = vectorOfSubEventMCTracks.size();
490490

491491
if (entries > 1) {
492+
size_t ntracks = 0;
493+
for (auto tracks : vectorOfSubEventMCTracks) {
494+
ntracks += tracks->size();
495+
}
496+
targetdata->reserve(ntracks);
492497
//
493498
// loop over subevents to store the primary events
494499
//
495500
int nprimTot = 0;
496501
for (int entry = entries - 1; entry >= 0; --entry) {
497502
int index = nsubevents[entry];
498503
nprimTot += nprimaries[index];
499-
printf("merge %d %5d %5d %5d \n", entry, index, nsubevents[entry], nsubevents[index]);
500504
for (int i = 0; i < nprimaries[index]; i++) {
501505
auto& track = (*vectorOfSubEventMCTracks[index])[i];
502506
if (track.isTransported()) { // reset daughters only if track was transported, it will be fixed below
@@ -595,6 +599,11 @@ class O2HitMerger : public fair::mq::Device
595599
incomingdata = vectorOfT[0];
596600
} else {
597601
targetdata = std::make_unique<T>();
602+
size_t nentries = 0;
603+
for (auto data : vectorOfT) {
604+
nentries += data->size();
605+
}
606+
targetdata->reserve(nentries);
598607
// loop over subevents
599608
Int_t nprimTot = 0;
600609
for (int entry = 0; entry < entries; entry++) {
@@ -726,7 +735,6 @@ class O2HitMerger : public fair::mq::Device
726735
std::vector<int> subevOrdered((int)(nsubevents.size()));
727736
for (int entry = entries - 1; entry >= 0; --entry) {
728737
subevOrdered[nsubevents[entry] - 1] = entry;
729-
printf("HitMerger entry: %d nprimry: %5d trackoffset: %5d \n", entry, nprimaries[entry], trackoffsets[entry]);
730738
}
731739

732740
// This is a hook that collects some useful statistics/properties on the event

0 commit comments

Comments
 (0)