From 32ae1de921ccaeaeb39b788896e1b10b641bd9d1 Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Fri, 3 May 2024 18:04:24 +0200
Subject: [PATCH] DDSim: correct the number of events when running over all
 events (-1)

GenerationActionInit: declare properties to access number of processed runs and events
---
 DDG4/python/DDSim/DD4hepSimulation.py  | 23 ++++++++++++++++-------
 DDG4/src/Geant4GeneratorActionInit.cpp |  2 ++
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/DDG4/python/DDSim/DD4hepSimulation.py b/DDG4/python/DDSim/DD4hepSimulation.py
index 5d4b3a42b..75f6e4289 100644
--- a/DDG4/python/DDSim/DD4hepSimulation.py
+++ b/DDG4/python/DDSim/DD4hepSimulation.py
@@ -445,9 +445,10 @@ class DD4hepSimulation(object):
       actionList.append(gen)
       self.__applyBoostOrSmear(kernel, actionList, index)
 
+    generationInit = None
     if actionList:
-      self._buildInputStage(geant4, actionList, output_level=self.output.inputStage,
-                            have_mctruth=self._enablePrimaryHandler())
+      generationInit = self._buildInputStage(geant4, actionList, output_level=self.output.inputStage,
+                                             have_mctruth=self._enablePrimaryHandler())
 
     # ================================================================================================
 
@@ -524,13 +525,20 @@ class DD4hepSimulation(object):
       logger.error("Termination failed!")
 
     totalTimeUser, totalTimeSys, _cuTime, _csTime, _elapsedTime = os.times()
+    processedEvents = self.numberOfEvents
+    if generationInit:
+      processedEvents = int(generationInit.numberOfEvents)
+      if self.numberOfEvents < 0:
+        processedEvents -= 1
+        logger.debug(f"Correcting number of events to: {processedEvents}")
+
     if self.printLevel <= 3:
-      logger.info("DDSim            INFO  Total Time:   %3.2f s (User), %3.2f s (System)" %
+      logger.info("Total Time:   %3.2f s (User), %3.2f s (System)" %
                   (totalTimeUser, totalTimeSys))
-      if self.numberOfEvents != 0:
+      if processedEvents != 0:
         eventTime = totalTimeUser - startUpTime
-        perEventTime = eventTime / self.numberOfEvents
-        logger.info("DDSim            INFO  StartUp Time: %3.2f s, Event Processing: %3.2f s (%3.2f s/Event) "
+        perEventTime = eventTime / processedEvents
+        logger.info("StartUp Time: %3.2f s, Event Processing: %3.2f s (%3.2f s/Event) "
                     % (startUpTime, eventTime, perEventTime))
     return exitCode
 
@@ -752,6 +760,7 @@ class DD4hepSimulation(object):
 
     # Register Generation initialization action
     gen = GeneratorAction(geant4.kernel(), "Geant4GeneratorActionInit/GenerationInit")
+    generationInit = gen
     if output_level is not None:
       gen.OutputLevel = output_level
     ga.adopt(gen)
@@ -781,7 +790,7 @@ class DD4hepSimulation(object):
         gen.OutputLevel = output_level
       ga.adopt(gen)
     # Puuuhh! All done.
-    return None
+    return generationInit
 
 
 ################################################################################
diff --git a/DDG4/src/Geant4GeneratorActionInit.cpp b/DDG4/src/Geant4GeneratorActionInit.cpp
index 0d80f94f9..108e827d4 100644
--- a/DDG4/src/Geant4GeneratorActionInit.cpp
+++ b/DDG4/src/Geant4GeneratorActionInit.cpp
@@ -30,6 +30,8 @@ Geant4GeneratorActionInit::Geant4GeneratorActionInit(Geant4Context* ctxt, const
   InstanceCount::increment(this);
   context()->kernel().runAction().callAtEnd(this,&Geant4GeneratorActionInit::end);
   context()->kernel().runAction().callAtBegin(this,&Geant4GeneratorActionInit::begin);
+  declareProperty("numberOfEvents", m_evtTotal);
+  declareProperty("numberOfRuns",  m_evtRun);
 }
 
 /// Default destructor
-- 
GitLab