Skip to content

Commit 8ace561

Browse files
sawenzelclaude
andcommitted
Reset the TPC hit grouping at the end of each event
This fixes TPC hits in parallel o2-sim depending on which events a worker simulated before. - The hit-grouping state in tpc::Detector::ProcessHits was kept in function-local statics and carried over into the next event. - In parallel mode the next event is any chunk, so the number of empty hit groups varied from run to run. - The state is now a set of detector members that EndOfEvent resets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 341b315 commit 8ace561

2 files changed

Lines changed: 24 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ class Detector : public o2::base::DetImpl<Detector>
156156
int mElectronCounter = 0;
157157
int mStepCounter = 0;
158158
ElementalHit mHitLast{}; ///<! buffer last processed hit to be able to fill it to mHitsPerSectorCollection
159+
HitGroup* mCurrentGroup = nullptr; ///<! hit group currently being filled
160+
int mOldTrackId = -1; ///<! track of the current hit group
161+
int mOldSectorId = -1; ///<! sector of the current hit group
162+
int mGroupCounter = 0; ///<! number of hits in the current hit group
159163

160164
/// Create the detector materials
161165
virtual void CreateMaterials();

‎Detectors/TPC/simulation/src/Detector.cxx‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ Bool_t Detector::ProcessHits(FairVolume* vol)
156156

157157
const float time = fMC->TrackTime() * 1.0e9;
158158
const int trackID = fMC->GetStack()->GetCurrentTrackNumber();
159-
const int detID = vol->getMCid();
160159
o2::data::Stack* stack = (o2::data::Stack*)fMC->GetStack();
161160
if (fMC->IsTrackEntering() || fMC->IsTrackExiting()) {
162161
stack->addTrackReference(o2::TrackReference(position.X(), position.Y(), position.Z(), momentum.X(), momentum.Y(),
@@ -258,37 +257,38 @@ Bool_t Detector::ProcessHits(FairVolume* vol)
258257
}
259258

260259
// ADD HIT
261-
static thread_local int oldTrackId = trackID;
262-
static thread_local int oldDetId = detID;
263-
static thread_local int groupCounter = 0;
264-
static thread_local int oldSectorId = sectorID;
260+
// the first hit of an event starts the grouping afresh
261+
if (!mCurrentGroup) {
262+
mOldTrackId = trackID;
263+
mOldSectorId = sectorID;
264+
mGroupCounter = 0;
265+
}
265266

266267
// a new group is starting -> put it into the container
267-
static thread_local HitGroup* currentgroup = nullptr;
268-
if (groupCounter == 0) {
268+
if (mGroupCounter == 0) {
269269
mHitsPerSectorCollection[sectorID]->emplace_back(trackID);
270-
currentgroup = &(mHitsPerSectorCollection[sectorID]->back());
270+
mCurrentGroup = &(mHitsPerSectorCollection[sectorID]->back());
271271
}
272-
if (trackID == oldTrackId && oldSectorId == sectorID) {
273-
groupCounter++;
272+
if (trackID == mOldTrackId && mOldSectorId == sectorID) {
273+
mGroupCounter++;
274274
mHitCounter++;
275275
mElectronCounter += numberOfElectrons;
276-
currentgroup->addHit(position.X(), position.Y(), position.Z(), time, numberOfElectrons);
276+
mCurrentGroup->addHit(position.X(), position.Y(), position.Z(), time, numberOfElectrons);
277277

278-
// add last buffered hit, which was not yet added to the currentgroup
278+
// add last buffered hit, which was not yet added to the current group
279279
if (mHitLast.GetEnergyLoss() >= 0) {
280-
currentgroup->addHit(mHitLast.GetX(), mHitLast.GetY(), mHitLast.GetZ(), mHitLast.GetTime(), mHitLast.GetEnergyLoss());
280+
mCurrentGroup->addHit(mHitLast.GetX(), mHitLast.GetY(), mHitLast.GetZ(), mHitLast.GetTime(), mHitLast.GetEnergyLoss());
281281
mHitLast.mELoss = -1;
282-
groupCounter++;
282+
mGroupCounter++;
283283
mHitCounter++;
284284
mElectronCounter += mHitLast.GetEnergyLoss();
285285
}
286286
}
287287
// finish group
288288
else {
289-
oldTrackId = trackID;
290-
oldSectorId = sectorID;
291-
groupCounter = 0;
289+
mOldTrackId = trackID;
290+
mOldSectorId = sectorID;
291+
mGroupCounter = 0;
292292

293293
// buffer this hit, otherwise it wouldnt be stored in the HitGroup
294294
mHitLast = ElementalHit(position.X(), position.Y(), position.Z(), time, numberOfElectrons);
@@ -310,6 +310,9 @@ Bool_t Detector::ProcessHits(FairVolume* vol)
310310

311311
void Detector::EndOfEvent()
312312
{
313+
// the hit grouping must not carry over into the next event
314+
mCurrentGroup = nullptr;
315+
mHitLast.mELoss = -1;
313316
if (!o2::utils::ShmManager::Instance().isOperational()) {
314317
for (int i = 0; i < Sector::MAXSECTOR; ++i) {
315318
mHitsPerSectorCollection[i]->clear();

0 commit comments

Comments
 (0)