Skip to content

Commit 66e2102

Browse files
Refactor event and stacking handling in RMG framework
Co-authored-by: Manuel Huber <info@manuelhu.de> fix(staging): correct volume selection, clarify germanium-only safety, couple optical to electron staging - Fix inverted AddVolumeName logic for electrons and gammas: staging is now active *inside* the named volumes (matching the command guidance and the replaced RMGVolumeDistanceStacker), instead of everywhere except them. - Enabling electron staging now also defers optical photons, as documented. - Drop the unimplemented DistanceCheckGermaniumOnly option: surface-safety checks are always Germanium-only. Clarify guidance/docs accordingly. - Add test_secondary_electron_respects_volume_selection to lock in the volume-selection behavior (matched volume defers, unmatched does not). test(staging): run correctness staging tests on every push The staging tests were all labeled val-only, so they only ran in the validation workflow. Run the correctness tests (test_staging_logic, test_isotope_filter_condition) on every push like the other functional tests, while keeping the heavy physics/performance tests (test_physics_regression, test_muon_stress, test_throughput) validation-only. feat(staging): remove gamma staging Gamma staging provided little benefit in the use cases considered and only added complexity. Remove it entirely: the Classify_Gamma classification, the gamma stepping-time suspension branch, all gamma members and messengers (/RMG/Staging/Gammas/{DeferToWaitingStage,VolumeSafety,AddVolumeName, MaxEnergyThresholdForStacking,SuspendOnEnergyDrop}), the gamma suspension test, and the corresponding documentation. Optical-photon and electron staging are unchanged. style: pre-commit fixes fix: generated the command docs in the wrong environment fix: used deprecated lgdo.lh5 import fix: broken tests
1 parent 77d7141 commit 66e2102

29 files changed

Lines changed: 2035 additions & 204 deletions

docs/manual/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ confinement.md
1313
generators.md
1414
input.md
1515
output.md
16+
staging.md
1617
visualization.md
1718
analysis.md
1819
extend.md

docs/manual/output.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,20 @@ function returning `true`, like the `RMGTrackOutputScheme` or the
190190

191191
Similar commands are available for the scintillator output scheme.
192192

193-
Similarly, for simulations involving optical photons it is possible to discard
194-
all optical photon tracks before simulating them if no energy was deposited in
195-
germanium. This can be enabled with
196-
<project:../rmg-commands.md#rmgoutputgermaniumdiscardphotonsifnogermaniumedep>.
193+
Similarly, for simulations involving optical photons, the waiting-stack behavior
194+
can be configured in two explicit steps:
195+
196+
- defer optical photons to the waiting stack with
197+
<project:../rmg-commands.md#rmgstagingopticalphotonsdefertowaitingstage>
198+
- clear waiting tracks at stage transition unless germanium energy deposition
199+
occurred with
200+
<project:../rmg-commands.md#rmgoutputgermaniumdiscardwaitingtracksunlessgermaniumedep>
201+
202+
When waiting tracks are cleared, this applies to the full waiting stack (not
203+
only optical photons deferred by the staging scheme).
204+
205+
For a detailed staging guide (including electron caveats and example setups),
206+
see {ref}`manual-staging`.
197207

198208
By default, the position saved for each step is the average of the pre and
199209
post-step point. This can be controlled with

docs/manual/staging.md

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
(manual-staging)=
2+
3+
# Staging and suspending tracks
4+
5+
This page provides an introduction to the staging and suspension options in
6+
_remage_. In this context, "staging" refers to deferring selected tracks from
7+
immediate processing to the waiting stack, which is then handled in a later
8+
stage based on configurable conditions. "Suspending", conversely, refers to
9+
halting and deferring the further processing of tracks that meet certain
10+
criteria. These mechanisms can help manage computational load in scenarios with
11+
many low-energy secondaries or optical photons. For complete command signatures,
12+
see the command reference in <project:../rmg-commands.md>.
13+
14+
## Overview
15+
16+
### Motivation
17+
18+
The motivation for deferring certain tracks is to reduce immediate processing
19+
load in configurations with many particles (for example optical physics or muon
20+
showers). Staging relates to track classification at creation, i.e., whether a
21+
track should be simulated immediately or placed on a waiting stack for later
22+
processing. Suspension relates to tracks that are already being processed and
23+
are then halted and moved to the waiting stack under certain conditions.
24+
25+
At the moment, _remage_ supports staging and suspension for:
26+
27+
- optical photons,
28+
- secondary electrons.
29+
30+
Staging of optical photons is a common use case, since they are at the end of
31+
the physics simulation chain and do not carry energy that would typically alter
32+
the stage-transition conditions discussed below. Deferring their calculation to
33+
a later stage until certain conditions are met can significantly reduce
34+
computational load in scenarios with many optical photons.
35+
36+
Staging and suspension of secondary electrons are more physics-sensitive, since
37+
these particles can carry significant energy. However, there are two scenarios
38+
where this can be beneficial:
39+
40+
1. In radiogenic simulations of far-away sources with the focus on energy
41+
deposition in a detector, where many low-energetic electrons are produced far
42+
away from the detectors and can be safely deferred. To avoid deferring
43+
electrons close to the detectors, a safety distance condition can be applied,
44+
which requires electrons to be a minimum distance away from any Germanium
45+
detector surface to be deferred.
46+
2. In muon-showers where the main interest is production of isotopes, where
47+
electrons below the threshold of isotope production can be deferred. This
48+
energy threshold can be tuned by the user.
49+
50+
### Implementation details
51+
52+
The staging and suspension backend in _remage_ is based on Geant4 stacking
53+
actions.
54+
55+
Staging is implemented in the `RMGStagingScheme` class, which implements
56+
`StackingActionClassify` and assigns tracks to `fWaiting` or `fUrgent`.
57+
58+
- `fUrgent` tracks are processed immediately.
59+
- `fWaiting` tracks are deferred and revisited in the next stage.
60+
61+
After the urgent stack is exhausted, Geant4 transitions to a new stage. At this
62+
point, `StackingActionNewStage` implementations from other active output/filter
63+
schemes (for example _Germanium_ and _IsotopeFilter_) can decide whether waiting
64+
tracks should be kept or cleared for that event.
65+
66+
Suspension is implemented separately from track-initialization classification.
67+
`RMGStagingScheme` implements a stepping action hook (`SteppingAction`) for
68+
optional suspension of secondary electrons when they cross from above to below a
69+
configured energy threshold. This is implemented by calling
70+
`SetTrackStatus(fSuspend)` on the track, which halts processing and places the
71+
track on the waiting stack.
72+
73+
### Caveats regarding electron staging
74+
75+
Electron staging is more physics-sensitive than optical-only staging because
76+
electrons can carry significant energy in electromagnetic cascades.
77+
78+
If stage transition conditions depend on energy deposition in specific volumes
79+
(for example `Germanium`), deferring electrons can move part of the energy
80+
deposit in the conditioned volume to a later stage. This can make a
81+
stage-transition condition fail in the initial stage and discard the waiting
82+
stack. Electron staging can still be viable if a safety-distance condition is
83+
applied so that only tracks far from Germanium detector surfaces are deferred.
84+
To do this, use `VolumeSafety` and `AddVolumeName` to defer tracks only in
85+
controlled regions. The best values depend on geometry and source type, but
86+
`20 cm` is often a conservative starting point for far-away sources.
87+
88+
If stage transition conditions depend on isotope production (for example
89+
`IsotopeFilter`), deferring high-energy shower components may suppress relevant
90+
production channels in the initial stage. To mitigate this, use
91+
`MaxEnergyThresholdForStacking` so only lower-energy tracks are deferred. The
92+
threshold should be tuned for the isotope and reaction channels of interest.
93+
This threshold applies to both staging and suspension.
94+
95+
Because of these caveats, validate staging and suspension against the physics
96+
observables of interest. In some cases (for example close-by sources), optical
97+
staging alone may be preferable to avoid the risk of biasing physics
98+
observables.
99+
100+
### Recommended use cases
101+
102+
Optical photon staging can always be beneficial compared to no staging with
103+
optical physics. However, there can be a bottleneck when putting too many
104+
particles onto the waiting stack. It seems it takes about ~10µs per track to be
105+
put on the waiting stack, and in cases with 10k optical photons, this can lead
106+
to a significant slowdown per event. In addition, in muon simulations, one can
107+
expect 1e8 optical photons per event, which can exhaust the available memory and
108+
crash the simulation.
109+
110+
In these cases, electron staging can be beneficial. Due to the smaller number of
111+
electrons, the positioning on the waiting stack is not a bottleneck, and one can
112+
get significant performance improvements. However, considering the caveats
113+
described above, it is important to validate the setup for the physics
114+
observables of interest. This improvement is only relevant when the fraction of
115+
events skipping the waiting stack is small (<1%). Examples of speedup can be
116+
found in the validation section of the documentation.
117+
118+
**Therefore**, electron staging is recommended for cases with potentially many
119+
optical photons, such as far-away radiogenic, and cosmogenic simulations. In
120+
other cases, such as close-by sources, it may be preferable to stage only
121+
optical photons.
122+
123+
## Commands
124+
125+
### Optical-photon staging
126+
127+
- `/RMG/Staging/OpticalPhotons/DeferToWaitingStage` enables deferral of optical
128+
photons to the waiting stack during stage 0.
129+
130+
### Electron staging
131+
132+
- `/RMG/Staging/Electrons/DeferToWaitingStage` enables deferral of secondary
133+
electrons **as well as optical photons**.
134+
- `/RMG/Staging/Electrons/VolumeSafety` sets a minimum distance to a Germanium
135+
detector surface condition.
136+
- `/RMG/Staging/Electrons/MaxEnergyThresholdForStacking` limits deferred
137+
electrons by kinetic energy.
138+
- `/RMG/Staging/Electrons/AddVolumeName` restricts deferral to named logical
139+
volumes.
140+
- `/RMG/Staging/Electrons/SuspendOnEnergyDrop` enables stepping-time suspension
141+
when a track crosses from above to below the configured threshold.
142+
143+
### Suspension behavior
144+
145+
- Suspension is evaluated in stepping, not at end-of-track.
146+
- The threshold is reused from `MaxEnergyThresholdForStacking` for the
147+
corresponding particle class.
148+
- Suspension is applied only to secondary tracks.
149+
150+
### Stage transition conditions
151+
152+
You can configure conditions that clear waiting tracks at stage transition.
153+
These are separate from the defer-to-waiting staging commands.
154+
155+
- **Germanium condition:**
156+
`/RMG/Output/Germanium/DiscardWaitingTracksUnlessGermaniumEdep` clears waiting
157+
tracks unless Germanium energy deposition occurred in the event.
158+
- **IsotopeFilter condition:**
159+
`/RMG/Output/IsotopeFilter/DiscardWaitingTracksUnlessIsotopeProduced` clears
160+
waiting tracks unless one of the configured isotopes was produced. Typical
161+
setup also requires: `/RMG/Output/ActivateOutputScheme` with `IsotopeFilter`
162+
and at least one `/RMG/Output/IsotopeFilter/AddIsotope` command.
163+
164+
## Configuration checklist
165+
166+
1. Activate required optional output schemes (required: `Staging`, stage
167+
transition conditions: e.g., `IsotopeFilter`, or `Germanium`).
168+
2. Specify which particles to defer to the waiting stack (e.g., optical photons,
169+
electrons) with the `Staging` commands.
170+
3. Define the conditions for stage transition (e.g., energy deposition in
171+
Germanium, isotope production) with the relevant output scheme or filter
172+
commands.
173+
4. (Optional) Enable suspension for electrons.
174+
5. (Optional) Tune safety distances and energy thresholds based on source,
175+
geometry, and physics observables.
176+
177+
## Examples
178+
179+
The examples below are intended as a skeleton. Replace placeholders with your
180+
actual geometry volume names, isotopes, and source definitions.
181+
182+
### Gamma example 1: optical staging with Germanium skip condition
183+
184+
```geant4
185+
/RMG/Output/ActivateOutputScheme Staging
186+
/RMG/Geometry/RegisterDetector Germanium detector_phys 0
187+
188+
/run/initialize
189+
190+
/RMG/Staging/OpticalPhotons/DeferToWaitingStage true
191+
/RMG/Output/Germanium/DiscardWaitingTracksUnlessGermaniumEdep true
192+
/RMG/Output/Germanium/EdepCutLow 25 keV
193+
194+
/RMG/Generator/Confine UnConfined
195+
/RMG/Generator/Select GPS
196+
/gps/particle gamma
197+
/gps/energy 2.6 MeV
198+
/gps/ang/type iso
199+
200+
/run/beamOn 1000
201+
```
202+
203+
### Gamma example 2: optical staging with IsotopeFilter skip condition
204+
205+
```geant4
206+
/RMG/Output/ActivateOutputScheme Staging
207+
/RMG/Output/ActivateOutputScheme IsotopeFilter
208+
209+
/run/initialize
210+
211+
/RMG/Staging/OpticalPhotons/DeferToWaitingStage true
212+
/RMG/Output/IsotopeFilter/AddIsotope 41 18
213+
/RMG/Output/IsotopeFilter/DiscardWaitingTracksUnlessIsotopeProduced true
214+
215+
/RMG/Generator/Confine UnConfined
216+
/RMG/Generator/Select GPS
217+
/gps/particle neutron
218+
/gps/energy 0.025 eV
219+
/gps/direction 0 0 1
220+
221+
/run/beamOn 1000
222+
```
223+
224+
### Electron and optical staging together
225+
226+
```geant4
227+
/RMG/Output/ActivateOutputScheme Staging
228+
/RMG/Geometry/RegisterDetector Germanium detector_phys 0
229+
230+
/run/initialize
231+
232+
/RMG/Staging/OpticalPhotons/DeferToWaitingStage true
233+
/RMG/Staging/Electrons/DeferToWaitingStage true
234+
/RMG/Staging/Electrons/VolumeSafety 5.0 cm
235+
/RMG/Staging/Electrons/MaxEnergyThresholdForStacking 10.0 MeV
236+
/RMG/Staging/Electrons/AddVolumeName world_vol
237+
238+
/RMG/Output/Germanium/DiscardWaitingTracksUnlessGermaniumEdep true
239+
/RMG/Output/Germanium/EdepCutLow 25 keV
240+
241+
/RMG/Generator/Select GPS
242+
/gps/particle gamma
243+
/gps/energy 2.6 MeV
244+
245+
/run/beamOn 1000
246+
```
247+
248+
### Muon example with electron and optical staging (8 MeV threshold)
249+
250+
The value `8.0 MeV` is a rough estimate for the neutron separation energy of the
251+
isotopes in liquid argon. Below this, no new neutrons are emitted, therefore,
252+
electrons produced below this threshold cannot contribute to isotope production
253+
and can be safely deferred without risking missed isotope production. Optional
254+
suspension can further reduce the cost of low-energy particles by deferring
255+
tracks after they cross below the configured energy threshold.
256+
257+
```geant4
258+
/RMG/Output/ActivateOutputScheme Staging
259+
/RMG/Output/ActivateOutputScheme IsotopeFilter
260+
261+
/run/initialize
262+
263+
/RMG/Staging/OpticalPhotons/DeferToWaitingStage true
264+
/RMG/Staging/Electrons/DeferToWaitingStage true
265+
/RMG/Staging/Electrons/MaxEnergyThresholdForStacking 8.0 MeV
266+
/RMG/Staging/Electrons/SuspendOnEnergyDrop true
267+
/RMG/Staging/Electrons/AddVolumeName world_vol
268+
269+
/RMG/Output/IsotopeFilter/AddIsotope 77 32
270+
/RMG/Output/IsotopeFilter/DiscardWaitingTracksUnlessIsotopeProduced true
271+
272+
/RMG/Generator/Select GPS
273+
/gps/particle mu-
274+
/gps/energy 273 GeV
275+
/gps/ang/type iso
276+
277+
/run/beamOn 500
278+
```
279+
280+
## See also
281+
282+
- {ref}`manual-output`
283+
- <project:../rmg-commands.md#rmgstaging>
284+
- <project:../rmg-commands.md#rmgoutputgermanium>
285+
- <project:../rmg-commands.md#rmgoutputisotopefilter>

0 commit comments

Comments
 (0)