Skip to content

11 optimizing simulation#12

Open
along4 wants to merge 15 commits into
mainfrom
11-optimizing-simulation
Open

11 optimizing simulation#12
along4 wants to merge 15 commits into
mainfrom
11-optimizing-simulation

Conversation

@along4

@along4 along4 commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

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 SteppingAction and TrackingAction classes. The PR also updates several example YAML files to support and demonstrate this new feature.

Photon Culling Feature Implementation

  • Added photon culling logic to SteppingAction and TrackingAction, 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]
  • Integrated photon culling configuration into the Config class, including enable/disable and acceptance angle parameters, with thread-safe getters and setters. (sim/include/config.hh, sim/src/config.cc) [1] [2] [3]
  • Added UI commands for runtime control of photon culling settings via the Messenger class. (sim/include/messenger.hh, sim/src/messenger.cc) [1] [2]
  • Updated ActionInitialization to pass the configuration object to SteppingAction and TrackingAction. (sim/src/ActionInitialization.cc)

YAML Example and Input Updates

  • Updated all example YAML files to replace the simulation and runner sections with a unified geant4runner section, 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]
  • In the pulsed neutron source timing example, set maskRadius to 0.0 to ensure all photons are considered, and enabled photon culling by default. (examples/yamlFiles/pulsed_neutron_source_timing.yaml) [1] [2]

Other Improvements and Cleanups

  • Removed unused or redundant YAML fields (e.g., OutputInfo and 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.

@along4 along4 linked an issue Jun 19, 2026 that may be closed by this pull request
7 tasks
@along4 along4 requested a review from Copilot June 19, 2026 15:10
@along4 along4 self-assigned this Jun 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 plumbs Config* into actions via ActionInitialization.
  • Updates example YAMLs to the unified geant4runner section and adds photonCulling blocks.

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 thread test_yaml_update.py
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 thread sim/src/SteppingAction.cc
Comment on lines 9 to +12
#include "G4Step.hh"
#include "G4StepPoint.hh"
#include "G4StepStatus.hh"
#include "G4SystemOfUnits.hh"
Comment thread sim/src/SteppingAction.cc
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 thread sim/src/SteppingAction.cc
Comment on lines +131 to +135
const G4int primaryTrackID = ResolvePrimaryTrackID(track, fEventAction);

for (const auto* secondary : *secondaries) {
if (!secondary) {
continue;
Comment thread sim/src/config.cc
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 thread sim/include/config.hh
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
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

optimizing-simulation

3 participants