Clone the registry GTouchable per step to stop cross-event state leak#165
Merged
Merged
Conversation
getGTouchable returned the persistent shared_ptr<GTouchable> stored in gTouchableMap, and ProcessHits/processTouchable mutate it each step (trackId, pid, stepTimeAtElectronicsIndex). Initialize() clears the per-event touchableVector but never resets those mutations, so at the start of each event the registry object still holds the previous event's time-cell index. processTouchableImpl branches on that index, so the first step of a new event can be misrouted (a spurious clone / wrong time cell), and stale trackId/pid carry over. Return a per-step copy so the registry entry stays pristine. Each step then starts from the clean UNSET identity, and within-event grouping still works because the hit collection / touchableVector group by operator== over field values (which are freshly assigned per step), not by object identity. gsd.h documents instances as thread-local with a per-worker map, so the clone introduces no cross-thread sharing. Verified: cherenkov example (gPhotonDetector SD, 20 events, fixed seed) produces byte-identical hit physics before and after (no regression); the fix removes the cross-event index carryover that affects readout SDs (Geant4 11.4.1 dev container). Fixes gemc#113 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
getGTouchablereturned the persistentshared_ptr<GTouchable>stored ingTouchableMap, andProcessHits/processTouchablemutate it each step (trackId,pid,stepTimeAtElectronicsIndex).Initialize()clears the per-eventtouchableVectorbut never resets those mutations, so at the start of each event the registry object still holds the previous event's time-cell index.processTouchableImplbranches on that index, so the first step of a new event can be misrouted (a spurious clone / wrong time cell), and staletrackId/pidcarry over.Return a per-step copy so the registry entry stays pristine. Each step then starts from the clean
UNSETidentity, and within-event grouping still works because the hit collection /touchableVectorgroup byoperator==over field values (freshly assigned per step), not by object identity.gsd.hdocuments instances as thread-local with a per-worker map, so the clone introduces no cross-thread sharing.Validation: ran the cherenkov example (gPhotonDetector SD, 20 events, fixed
-seed) before and after — the hit physics is byte-identical (only the wall-clock timestamp column differs), i.e. no regression, while the fix removes the cross-event index carryover that affects readout SDs (Geant4 11.4.1 dev container).Note: this is the issue's primary (clone) fix. For readout-type SDs it changes behavior by design — it stops the cross-event bleed and the spurious double-cell contribution from
processTouchableImpl's clone-on-change branch; each step now routes to exactly one time cell viaoperator==.Fixes #113