From 5e8d55c2caf8537dd0f90bb11e1406d38efbfb94 Mon Sep 17 00:00:00 2001 From: Alexis Pereda Date: Fri, 21 Mar 2025 16:15:52 +0100 Subject: [PATCH] Add InitializeG4PreInitState step in Actors --- core/opengate_core/opengate_lib/GateVActor.h | 3 +++ core/opengate_core/opengate_lib/pyGateVActor.cpp | 1 + opengate/engines.py | 15 +++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/core/opengate_core/opengate_lib/GateVActor.h b/core/opengate_core/opengate_lib/GateVActor.h index 2f1282a56..7c84f589a 100644 --- a/core/opengate_core/opengate_lib/GateVActor.h +++ b/core/opengate_core/opengate_lib/GateVActor.h @@ -27,6 +27,9 @@ class GateVActor : public G4VPrimitiveScorer { virtual void AddActions(std::set &actions); + // Called at initialisation in late G4 PreInit state + virtual void InitializeG4PreInitState() {} + // Called at initialisation virtual void InitializeCpp(); diff --git a/core/opengate_core/opengate_lib/pyGateVActor.cpp b/core/opengate_core/opengate_lib/pyGateVActor.cpp index 6a60c30f5..729cf9c2e 100644 --- a/core/opengate_core/opengate_lib/pyGateVActor.cpp +++ b/core/opengate_core/opengate_lib/pyGateVActor.cpp @@ -79,6 +79,7 @@ void init_GateVActor(py::module &m) { // .def_readonly("fActions", &GateVActor::fActions) // avoid wrapping // this -> problems with pickle .def_readwrite("fFilters", &GateVActor::fFilters) + .def("InitializeG4PreInitState", &GateVActor::InitializeG4PreInitState) .def("InitializeCpp", &GateVActor::InitializeCpp) .def("InitializeUserInfo", &GateVActor::InitializeUserInfo) .def("AddActions", &GateVActor::AddActions) diff --git a/opengate/engines.py b/opengate/engines.py index 034016e4d..23d6a4855 100644 --- a/opengate/engines.py +++ b/opengate/engines.py @@ -591,6 +591,13 @@ def close(self): actor.close() super().close() + def initialize_preinit(self): + for actor in self.actor_manager.sorted_actors: + global_log.debug( + f"Actor: initialize_preinit [{actor.type_name}] {actor.name}" + ) + actor.InitializeG4PreInitState() + def initialize(self): for actor in self.actor_manager.sorted_actors: global_log.debug(f"Actor: initialize [{actor.type_name}] {actor.name}") @@ -1278,6 +1285,9 @@ def initialize(self): """ Build the main geant4 objects and initialize them. """ + + # From this line, G4 is in G4State_PreInit state + # get log log = global_log @@ -1346,6 +1356,9 @@ def initialize(self): self.action_engine ) # G4 internally calls action_engine.Build() + # late-G4 PreInit state actor initialisation phase + self.actor_engine.initialize_preinit() + # Important: The volumes are constructed # when the G4RunManager calls the Construct method of the VolumeEngine, # which happens in the InitializeGeometry() method of the @@ -1360,6 +1373,8 @@ def initialize(self): else: self.g4_RunManager.Initialize() + # From this line, G4 is in G4State_Idle state + log.info("Simulation: initialize PhysicsEngine after RunManager initialization") self.physics_engine.initialize_after_runmanager() self.g4_RunManager.PhysicsHasBeenModified()