Skip to content

Commit 6b2fd4d

Browse files
sawenzelclaude
andcommitted
Do not offset an invalid track index when merging sub-events
This fixes a problem in the sub-event merging and adds a unit test. - Merging offsets every track index, and the offset was applied unconditionally. - An index already flagged invalid therefore came out as offset - 1 and pointed at an unrelated track. - The MCTrack mother index in O2HitMerger has always guarded against this, hits and track references did not. - Both now use Detector::offsetTrackIndex, which leaves a negative index alone. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 726cbee commit 6b2fd4d

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

Detectors/Base/include/DetectorsBase/Detector.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ class Detector : public FairDetector
171171
return iter != indexmapping.end() ? iter->second : -1;
172172
}
173173

174+
// the index a hit or a track reference gets when the sub-events of one event
175+
// are concatenated; an index already flagged invalid carries no track to
176+
// shift and stays invalid
177+
static int offsetTrackIndex(int trackID, int nprimaries, int primaryOffset, int secondaryOffset)
178+
{
179+
if (trackID < 0) {
180+
return trackID;
181+
}
182+
return trackID + (trackID < nprimaries ? primaryOffset : secondaryOffset);
183+
}
184+
174185
// interfaces to attach properly encoded hit information to a FairMQ message
175186
// and to decode it
176187
virtual void attachHits(fair::mq::Channel&, fair::mq::Parts&) = 0;
@@ -398,10 +409,7 @@ class DetImpl : public o2::base::Detector
398409
if (incomingdata) {
399410
// fix the trackIDs for this data
400411
for (auto& hit : *incomingdata) {
401-
const auto oldID = hit.GetTrackID();
402-
// offset depends on whether the trackis a primary or secondary
403-
Int_t offset = (oldID < nprim) ? idelta0 : idelta1;
404-
hit.SetTrackID(oldID + offset);
412+
hit.SetTrackID(offsetTrackIndex(hit.GetTrackID(), nprim, idelta0, idelta1));
405413
}
406414
// this could be further generalized by using a policy for T
407415
std::copy(incomingdata->begin(), incomingdata->end(), std::back_inserter(*targetdata));
@@ -465,10 +473,7 @@ class DetImpl : public o2::base::Detector
465473
if (incomingdata) {
466474
// fix the trackIDs for this data
467475
for (auto& hit : *incomingdata) {
468-
const auto oldID = hit.GetTrackID();
469-
// offset depends on whether the trackis a primary or secondary
470-
int offset = (oldID < nprim) ? idelta0 : idelta1;
471-
hit.SetTrackID(oldID + offset);
476+
hit.SetTrackID(offsetTrackIndex(hit.GetTrackID(), nprim, idelta0, idelta1));
472477
}
473478
// this could be further generalized by using a policy for T
474479
std::copy(incomingdata->begin(), incomingdata->end(), std::back_inserter(*targetdata));

Detectors/Base/test/testStack.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,13 @@ BOOST_AUTO_TEST_CASE(Stack_does_not_reuse_index_map_of_previous_event)
170170

171171
BOOST_CHECK_EQUAL(det.mHits[0].GetTrackID(), -1);
172172
}
173+
174+
// An invalid index must not be offset when sub-events are merged
175+
BOOST_AUTO_TEST_CASE(Offsetting_keeps_an_invalid_index_invalid)
176+
{
177+
const int nprimaries = 5, primaryOffset = 10, secondaryOffset = 100;
178+
179+
BOOST_CHECK_EQUAL(o2::base::Detector::offsetTrackIndex(3, nprimaries, primaryOffset, secondaryOffset), 13);
180+
BOOST_CHECK_EQUAL(o2::base::Detector::offsetTrackIndex(7, nprimaries, primaryOffset, secondaryOffset), 107);
181+
BOOST_CHECK_EQUAL(o2::base::Detector::offsetTrackIndex(-1, nprimaries, primaryOffset, secondaryOffset), -1);
182+
}

run/O2HitMerger.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,7 @@ class O2HitMerger : public fair::mq::Device
627627

628628
void updateTrackIdWithOffset(TrackReference& ref, Int_t nprim, Int_t idelta0, Int_t idelta1)
629629
{
630-
Int_t cId = ref.getTrackID();
631-
Int_t ioffset = (cId < nprim) ? idelta0 : idelta1;
632-
ref.setTrackID(cId + ioffset);
630+
ref.setTrackID(o2::base::Detector::offsetTrackIndex(ref.getTrackID(), nprim, idelta0, idelta1));
633631
}
634632

635633
void initHitTreeAndOutFile(std::string prefix, int detID)

0 commit comments

Comments
 (0)