From ea31b3a017fe1b903ea109419cdd201ceb5ed6f7 Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Thu, 13 Jul 2023 17:48:37 +0200
Subject: [PATCH] DDSim: implement access to run commands during different
 Geant4 UI Phases

---
 DDG4/python/DDSim/DD4hepSimulation.py | 18 ++++++++---
 DDG4/python/DDSim/Helper/Physics.py   | 46 +++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/DDG4/python/DDSim/DD4hepSimulation.py b/DDG4/python/DDSim/DD4hepSimulation.py
index 9b8708edf..bf7057549 100644
--- a/DDG4/python/DDSim/DD4hepSimulation.py
+++ b/DDG4/python/DDSim/DD4hepSimulation.py
@@ -317,19 +317,27 @@ class DD4hepSimulation(object):
     geant4.printDetectors()
 
     if self.runType == "vis":
-      geant4.setupUI(typ="tcsh", vis=True, macro=self.macroFile)
+      uiaction = geant4.setupUI(typ="tcsh", vis=True, macro=self.macroFile)
     elif self.runType == "qt":
-      geant4.setupUI(typ="qt", vis=True, macro=self.macroFile)
+      uiaction = geant4.setupUI(typ="qt", vis=True, macro=self.macroFile)
     elif self.runType == "run":
-      geant4.setupUI(typ="tcsh", vis=False, macro=self.macroFile, ui=False)
+      uiaction = geant4.setupUI(typ="tcsh", vis=False, macro=self.macroFile, ui=False)
     elif self.runType == "shell":
-      geant4.setupUI(typ="tcsh", vis=False, macro=None, ui=True)
+      uiaction = geant4.setupUI(typ="tcsh", vis=False, macro=None, ui=True)
     elif self.runType == "batch":
-      geant4.setupUI(typ="tcsh", vis=False, macro=None, ui=False)
+      uiaction = geant4.setupUI(typ="tcsh", vis=False, macro=None, ui=False)
     else:
       logger.error("unknown runType")
       exit(1)
 
+    # User Configuration for the Geant4Phases
+    uiaction.ConfigureCommands = self.physics._commandsConfigure
+    uiaction.TerminateCommands = self.physics._commandsTerminate
+    uiaction.PostRunCommands = self.physics._commandsPostRun
+    uiaction.PreRunCommands = self.physics._commandsPreRun
+    uiaction.InitializeCommands = self.physics._commandsInitialize
+
+
     kernel.NumEvents = self.numberOfEvents
 
     # -----------------------------------------------------------------------------------
diff --git a/DDG4/python/DDSim/Helper/Physics.py b/DDG4/python/DDSim/Helper/Physics.py
index db1e82e33..73ced27f9 100644
--- a/DDG4/python/DDSim/Helper/Physics.py
+++ b/DDG4/python/DDSim/Helper/Physics.py
@@ -30,6 +30,12 @@ class Physics(ConfigHelper):
     self._zeroTimePDGs = {11, 13, 15, 17}
     self._userFunctions = []
 
+    self._commandsConfigure = []
+    self._commandsTerminate = []
+    self._commandsPostRun = []
+    self._commandsPreRun = []
+    self._commandsInitialize = []
+
   @property
   def rejectPDGs(self):
     """Set of PDG IDs that will not be passed from the input record to Geant4.
@@ -116,6 +122,46 @@ class Physics(ConfigHelper):
   def list(self, val):  # noqa: A003
     self._list = val
 
+  @property
+  def commandsConfigure(self):
+    """List of UI commands to run during the 'Configure' phase."""
+    return self._commandsConfigure
+  @commandsConfigure.setter
+  def commandsConfigure(self, val):
+    self._commandsConfigure = self.makeList(val)
+
+  @property
+  def commandsInitialize(self):
+    """List of UI commands to run during the 'Initialize' phase."""
+    return self._commandsInitialize
+  @commandsInitialize.setter
+  def commandsInitialize(self, val):
+    self._commandsInitialize = self.makeList(val)
+
+  @property
+  def commandsTerminate(self):
+    """List of UI commands to run during the 'Terminate' phase."""
+    return self._commandsTerminate
+  @commandsTerminate.setter
+  def commandsTerminate(self, val):
+    self._commandsTerminate = self.makeList(val)
+
+  @property
+  def commandsPreRun(self):
+    """List of UI commands to run during the 'PreRun' phase."""
+    return self._commandsPreRun
+  @commandsPreRun.setter
+  def commandsPreRun(self, val):
+    self._commandsPreRun = self.makeList(val)
+
+  @property
+  def commandsPostRun(self):
+    """List of UI commands to run during the 'PostRun' phase."""
+    return self._commandsPostRun
+  @commandsPostRun.setter
+  def commandsPostRun(self, val):
+    self._commandsPostRun = self.makeList(val)
+
   def setupPhysics(self, kernel, name=None):
     seq = kernel.physicsList()
     seq.extends = name if name is not None else self.list
-- 
GitLab