From 76e43b6ee03f7e3070a9ed5d4095e227a7c55195 Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Thu, 19 Jan 2023 18:16:17 +0100
Subject: [PATCH] Geant4ParticleHandler: move suspending to end of ::end

If we do not already store particles that become important'ish later we get a fatal error because the equilavent finding might not be able to find this particle
---
 DDG4/src/Geant4ParticleHandler.cpp       | 21 +++++++++++----------
 examples/DDG4/scripts/TestStepping.py    |  2 +-
 examples/DDG4/src/TestSteppingAction.cpp |  5 +++++
 3 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/DDG4/src/Geant4ParticleHandler.cpp b/DDG4/src/Geant4ParticleHandler.cpp
index 4fd39c54a..79d4719ad 100644
--- a/DDG4/src/Geant4ParticleHandler.cpp
+++ b/DDG4/src/Geant4ParticleHandler.cpp
@@ -316,16 +316,6 @@ void Geant4ParticleHandler::end(const G4Track* track)   {
   Geant4ParticleHandle ph(&m_currTrack);
   const int g4_id = h.id();
 
-  if(track->GetTrackStatus() == fSuspend) {
-    m_haveSuspended = true;
-    //track is already in particle map, we pick it up from there in begin again
-    if(m_particleMap.find(g4_id) != m_particleMap.end()) return;
-    //track is not already stored, keep it in special map
-    auto iPart = m_suspendedPM.emplace(g4_id, new Particle());
-    (iPart.first->second)->get_data(m_currTrack);
-    return; // we trust that we eventually return to this function with another status and go on then
-  }
-
   int track_reason = m_currTrack.reason;
   PropertyMask mask(m_currTrack.reason);
   // Update vertex end point and final momentum
@@ -401,6 +391,17 @@ void Geant4ParticleHandler::end(const G4Track* track)   {
     else
       ph.dumpWithVertex(outputLevel()+3,name(),"FATAL: No real particle parent present");
   }
+
+  if(track->GetTrackStatus() == fSuspend) {
+    m_haveSuspended = true;
+    //track is already in particle map, we pick it up from there in begin again
+    if(m_particleMap.find(g4_id) != m_particleMap.end()) return;
+    //track is not already stored, keep it in special map
+    auto iPart = m_suspendedPM.emplace(g4_id, new Particle());
+    (iPart.first->second)->get_data(m_currTrack);
+    return; // we trust that we eventually return to this function with another status and go on then
+  }
+
 }
 
 /// Pre-event action callback
diff --git a/examples/DDG4/scripts/TestStepping.py b/examples/DDG4/scripts/TestStepping.py
index 54cc739b9..59a14af55 100644
--- a/examples/DDG4/scripts/TestStepping.py
+++ b/examples/DDG4/scripts/TestStepping.py
@@ -61,7 +61,7 @@ def run():
   gun = geant4.setupGun("Gun", particle='gamma', energy=1 * GeV, multiplicity=1)
   gun.direction = (0.0, 0.0, 1.0)
   gun.OutputLevel = generator_output_level
-  kernel.NumEvents = 2
+  kernel.NumEvents = 10
   # Instantiate the stepping action
   stepping = DDG4.SteppingAction(kernel, 'TestSteppingAction/MyStepper')
   kernel.steppingAction().add(stepping)
diff --git a/examples/DDG4/src/TestSteppingAction.cpp b/examples/DDG4/src/TestSteppingAction.cpp
index 8c2693dce..0af8c1997 100644
--- a/examples/DDG4/src/TestSteppingAction.cpp
+++ b/examples/DDG4/src/TestSteppingAction.cpp
@@ -31,6 +31,7 @@ namespace dd4hep {
     class TestSteppingAction : public Geant4SteppingAction {
       std::size_t m_calls_steps { 0UL };
       std::size_t m_calls_suspended { 0UL };
+      std::size_t m_calls_kill { 0UL };
     
     public:
       /// Standard constructor
@@ -42,12 +43,16 @@ namespace dd4hep {
       virtual ~TestSteppingAction()   {
 	info("+++ Track Calls Steps: %ld", m_calls_steps);
 	info("+++ Track Calls Suspended: %ld", m_calls_suspended);
+	info("+++ Track Calls Killed: %ld", m_calls_kill);
       }
       /// stepping callback
       virtual void operator()(const G4Step* step, G4SteppingManager*) {
         if(m_calls_steps % 5 == 0 ) {
           ++m_calls_suspended;
           step->GetTrack()->SetTrackStatus(fSuspend);
+        } else if((m_calls_steps + 1) % 30 == 0 ) {
+          ++m_calls_kill;
+          step->GetTrack()->SetTrackStatus(fStopAndKill);
         }
 	++m_calls_steps;
       }
-- 
GitLab