From 0207c48272d9051e5c8f8cd8017bc40f1d3db6b6 Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Tue, 2 Apr 2024 14:48:08 +0200
Subject: [PATCH] FIX: ddsim to exit with non-zero exit code when something
 went wrong

---
 DDG4/python/DDSim/DD4hepSimulation.py | 14 ++++++++++----
 DDG4/python/DDSim/bin/ddsim.in.py     |  2 +-
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/DDG4/python/DDSim/DD4hepSimulation.py b/DDG4/python/DDSim/DD4hepSimulation.py
index 509de7923..660e50a38 100644
--- a/DDG4/python/DDSim/DD4hepSimulation.py
+++ b/DDG4/python/DDSim/DD4hepSimulation.py
@@ -331,7 +331,7 @@ class DD4hepSimulation(object):
       uiaction = geant4.setupUI(typ="tcsh", vis=False, macro=None, ui=False)
     else:
       logger.error("unknown runType")
-      exit(1)
+      return 1
 
     # User Configuration for the Geant4Phases
     uiaction.ConfigureCommands = self.ui._commandsConfigure
@@ -476,7 +476,7 @@ class DD4hepSimulation(object):
       self.filter.setupFilters(kernel)
     except RuntimeError as e:
       logger.error("%s", e)
-      exit(1)
+      return 1
 
     # =================================================================================
     # get lists of trackers and calorimeters in detectorDescription
@@ -515,8 +515,13 @@ class DD4hepSimulation(object):
 
     startUpTime, _sysTime, _cuTime, _csTime, _elapsedTime = os.times()
 
-    kernel.run()
-    kernel.terminate()
+    exitCode = 0
+    if not kernel.run():
+      logger.error("Simulation failed!")
+      exitCode += 1
+    if not kernel.terminate():
+      exitCode += 1
+      logger.error("Termination failed!")
 
     totalTimeUser, totalTimeSys, _cuTime, _csTime, _elapsedTime = os.times()
     if self.printLevel <= 3:
@@ -527,6 +532,7 @@ class DD4hepSimulation(object):
         perEventTime = eventTime / self.numberOfEvents
         logger.info("DDSim            INFO  StartUp Time: %3.2f s, Event Processing: %3.2f s (%3.2f s/Event) "
                     % (startUpTime, eventTime, perEventTime))
+    return exitCode
 
   def __setMagneticFieldOptions(self, geant4):
     """ create and configure the magnetic tracking setup """
diff --git a/DDG4/python/DDSim/bin/ddsim.in.py b/DDG4/python/DDSim/bin/ddsim.in.py
index ce20d73b7..938042fbc 100644
--- a/DDG4/python/DDSim/bin/ddsim.in.py
+++ b/DDG4/python/DDSim/bin/ddsim.in.py
@@ -22,7 +22,7 @@ if __name__ == "__main__":
   RUNNER.parseOptions()
 
   try:
-    RUNNER.run()
+    sys.exit(RUNNER.run())
   except NameError as e:
     if "global name" in str(e):
       globalToSet = str(e).split("'")[1]
-- 
GitLab