11 optimizing simulation#12
Open
along4 wants to merge 15 commits into
Open
Conversation
…ble secondaries in geant4runner
…ton culling logic
…n geant4runner section
7 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a configurable photon-culling optimization to the Geant4 simulation pipeline to avoid tracking optical photons emitted outside a detector acceptance cone, with configuration exposed via YAML, macro generation, and Geant4 UI commands.
Changes:
- Introduces photon-culling parameters (
enabled,acceptanceAngleDeg) end-to-end: Python config model → macro commands → Geant4 messenger/config → tracking logic. - Implements photon culling in
TrackingAction(kills tracks early) and plumbsConfig*into actions viaActionInitialization. - Updates example YAMLs to the unified
geant4runnersection and addsphotonCullingblocks.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| test_yaml_update.py | Adds a YAML validation check (currently as an import-time script; should be converted to a real test). |
| src/models/geant4runtime.py | Adds PhotonCullingConfig and exposes it under Geant4RunTime.photon_culling. |
| src/config/macro.py | Emits /photonCulling/* macro commands from YAML/runtime config. |
| sim/src/TrackingAction.cc | Adds culling decision logic and kills optical-photon tracks early when enabled. |
| sim/src/SteppingAction.cc | Plumbs Config*, records photon origins, and adds an (currently unused) culling helper. |
| sim/src/messenger.cc | Adds /photonCulling/enabled and /photonCulling/acceptanceAngleDeg UI commands and wiring to Config. |
| sim/src/config.cc | Adds thread-safe getters/setters for photon-culling settings. |
| sim/src/ActionInitialization.cc | Passes Config* into SteppingAction and TrackingAction. |
| sim/include/TrackingAction.hh | Updates constructor signature and adds ShouldCullPhoton helper declaration. |
| sim/include/SteppingAction.hh | Updates constructor signature and adds photon-culling members/helper declaration. |
| sim/include/messenger.hh | Declares new photon-culling UI directory and commands. |
| sim/include/config.hh | Declares photon-culling getters/setters and adds config storage fields. |
| examples/yamlFiles/CanonEF50mmf1p0L_example.yaml | Migrates to geant4runner and adds photonCulling config. |
| examples/yamlFiles/EJ276D.yaml | Migrates to geant4runner and adds photonCulling config. |
| examples/yamlFiles/EJ200.yaml | Adds photonCulling config to geant4runner. |
| examples/yamlFiles/continuous_neutron_source_timing.yaml | Migrates to geant4runner and adds photonCulling config. |
| examples/yamlFiles/three_component_timing_example.yaml | Migrates to geant4runner, adds photonCulling, and cleans up metadata/output fields. |
| examples/yamlFiles/pulsed_neutron_source_timing.yaml | Sets maskRadius: 0.0 and enables photonCulling by default. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1
to
+30
| """Test that updated YAML files validate correctly.""" | ||
| from src.config.yaml import from_yaml | ||
|
|
||
| yaml_files = [ | ||
| 'examples/yamlFiles/EJ200.yaml', | ||
| 'examples/yamlFiles/CanonEF50mmf1p0L_example.yaml', | ||
| 'examples/yamlFiles/continuous_neutron_source_timing.yaml', | ||
| 'examples/yamlFiles/EJ276D.yaml', | ||
| 'examples/yamlFiles/three_component_timing_example.yaml', | ||
| 'examples/yamlFiles/pulsed_neutron_source_timing.yaml', | ||
| ] | ||
|
|
||
| print("Testing all YAML examples...") | ||
| print("=" * 60) | ||
|
|
||
| for yaml_file in yaml_files: | ||
| try: | ||
| config = from_yaml(yaml_file) | ||
| culling = config.geant4runner.photon_culling | ||
| status = "✓" | ||
| msg = f"enabled={culling.enabled}, angle={culling.acceptance_angle_deg}°" | ||
| except Exception as e: | ||
| status = "✗" | ||
| msg = f"ERROR: {str(e)}" | ||
|
|
||
| print(f"{status} {yaml_file.split('/')[-1]}") | ||
| print(f" {msg}") | ||
|
|
||
| print("=" * 60) | ||
| print("All YAML files validated successfully!") |
Comment on lines
9
to
+12
| #include "G4Step.hh" | ||
| #include "G4StepPoint.hh" | ||
| #include "G4StepStatus.hh" | ||
| #include "G4SystemOfUnits.hh" |
Comment on lines
+45
to
+49
| bool SteppingAction::ShouldCullPhoton(const G4ThreeVector& photonDirection, | ||
| const G4ThreeVector& scintillatorCenter, | ||
| const G4ThreeVector& detectorCenter) const { | ||
| // If culling is disabled, don't cull any photons | ||
| if (!fConfig || !fConfig->GetPhotonCullingEnabled()) { |
Comment on lines
+131
to
+135
| const G4int primaryTrackID = ResolvePrimaryTrackID(track, fEventAction); | ||
|
|
||
| for (const auto* secondary : *secondaries) { | ||
| if (!secondary) { | ||
| continue; |
Comment on lines
473
to
+480
| G4bool Config::GetWritePhotonsOutput() const { | ||
| std::lock_guard<std::mutex> lock(fMutex); | ||
| return fWritePhotonsOutput; | ||
| std::lock_guard<std::mutex> lock(fMutex); | ||
| return fWritePhotonsOutput; | ||
| } | ||
|
|
||
| G4bool Config::GetPhotonCullingEnabled() const { | ||
| std::lock_guard<std::mutex> lock(fMutex); | ||
| return fPhotonCullingEnabled; |
Comment on lines
+275
to
+287
| /// Source timing settings in Geant4 internal units. | ||
| SourceTimingMode fSourceTimingMode = SourceTimingMode::None; | ||
| G4double fSourceTimingStartTime = 0.0; | ||
| G4double fSourceTimingEventSpacing = 0.0; | ||
| G4double fSourceTimingPulsePeriod = 0.0; | ||
| G4int fSourceTimingNeutronsPerPulse = 1; | ||
| G4double fSourceTimingPulseTimeOffset = 0.0; | ||
| G4double fSourceTimingPulseTimeWidth = 0.0; | ||
| std::string fSourceTimingPulseShape = "uniform"; | ||
|
|
||
| /// Photon culling settings | ||
| bool fPhotonCullingEnabled = false; | ||
| G4double fPhotonCullingAcceptanceAngleDeg = 30.0; |
Comment on lines
+62
to
+73
| class PhotonCullingConfig(StrictModel): | ||
| """Photon culling optimization settings. | ||
|
|
||
| When enabled, photons emitted away from the detector are not tracked, | ||
| reducing simulation time while maintaining accuracy for detected photons. | ||
| """ | ||
|
|
||
| enabled: bool = False | ||
| acceptance_angle_deg: float = Field( | ||
| default=30.0, alias="acceptanceAngleDeg", gt=0.0, le=180.0 | ||
| ) | ||
|
|
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.
This pull request introduces a photon culling optimization to the simulation, allowing photons that are unlikely to reach the detector (based on their emission angle) to be filtered out early. This feature is configurable via both YAML input files and runtime UI commands, and is integrated into both the
SteppingActionandTrackingActionclasses. The PR also updates several example YAML files to support and demonstrate this new feature.Photon Culling Feature Implementation
SteppingActionandTrackingAction, with methods to determine if a photon should be culled based on its emission direction relative to the detector and a configurable acceptance angle. (sim/include/SteppingAction.hh,sim/src/SteppingAction.cc,sim/include/TrackingAction.hh,sim/src/TrackingAction.cc) [1] [2] [3] [4]Configclass, including enable/disable and acceptance angle parameters, with thread-safe getters and setters. (sim/include/config.hh,sim/src/config.cc) [1] [2] [3]Messengerclass. (sim/include/messenger.hh,sim/src/messenger.cc) [1] [2]ActionInitializationto pass the configuration object toSteppingActionandTrackingAction. (sim/src/ActionInitialization.cc)YAML Example and Input Updates
simulationandrunnersections with a unifiedgeant4runnersection, and added photon culling configuration options (enabled,acceptanceAngleDeg). (examples/yamlFiles/CanonEF50mmf1p0L_example.yaml,examples/yamlFiles/EJ276D.yaml,examples/yamlFiles/EJ200.yaml,examples/yamlFiles/continuous_neutron_source_timing.yaml,examples/yamlFiles/three_component_timing_example.yaml,examples/yamlFiles/pulsed_neutron_source_timing.yaml) [1] [2] [3] [4] [5] [6]maskRadiusto0.0to ensure all photons are considered, and enabled photon culling by default. (examples/yamlFiles/pulsed_neutron_source_timing.yaml) [1] [2]Other Improvements and Cleanups
OutputInfoand duplicate runner sections) for clarity and consistency. [1] [2] [3] [4]These changes collectively improve simulation efficiency by allowing users to skip tracking photons that cannot reach the detector, and make this optimization easily configurable for different experiments.