From 354908a62756d6b8485855a4148cc3423b64485b Mon Sep 17 00:00:00 2001 From: Andre Sailer <andre.philippe.sailer@cern.ch> Date: Tue, 7 Aug 2018 18:37:25 +0200 Subject: [PATCH] Simulation: if -1 for number of events, simulate until EOF and end gracefully calling TerminateRun --- DDG4/include/DDG4/Geant4Kernel.h | 7 +++++++ DDG4/src/Geant4GeneratorAction.cpp | 1 + DDG4/src/Geant4InputAction.cpp | 16 ++++++++++++++++ DDG4/src/Geant4UIManager.cpp | 8 +++++++- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/DDG4/include/DDG4/Geant4Kernel.h b/DDG4/include/DDG4/Geant4Kernel.h index 7495aade6..761cfddda 100644 --- a/DDG4/include/DDG4/Geant4Kernel.h +++ b/DDG4/include/DDG4/Geant4Kernel.h @@ -20,6 +20,13 @@ #include <map> #include <typeinfo> +class DD4hep_End_Of_File : public std::exception { +public: + DD4hep_End_Of_File() : std::exception() {} + virtual const char* what() const noexcept { return "Reached end of input file"; } + +}; + // Forward declarations class G4RunManager; class G4UIdirectory; diff --git a/DDG4/src/Geant4GeneratorAction.cpp b/DDG4/src/Geant4GeneratorAction.cpp index da144575f..1d9cfce8c 100644 --- a/DDG4/src/Geant4GeneratorAction.cpp +++ b/DDG4/src/Geant4GeneratorAction.cpp @@ -14,6 +14,7 @@ // Framework include files #include "DD4hep/InstanceCount.h" #include "DDG4/Geant4GeneratorAction.h" +#include "DDG4/Geant4Kernel.h" // Geant4 headers #include "G4Threading.hh" #include "G4AutoLock.hh" diff --git a/DDG4/src/Geant4InputAction.cpp b/DDG4/src/Geant4InputAction.cpp index ae451357f..537696bf9 100644 --- a/DDG4/src/Geant4InputAction.cpp +++ b/DDG4/src/Geant4InputAction.cpp @@ -17,6 +17,7 @@ #include "DD4hep/Plugins.h" #include "DDG4/Geant4Primary.h" #include "DDG4/Geant4Context.h" +#include "DDG4/Geant4Kernel.h" #include "DDG4/Geant4InputAction.h" #include "G4Event.hh" @@ -174,6 +175,14 @@ int Geant4InputAction::readParticles(int evt_number, } } int status = m_reader->moveToEvent(evid); + if(status == Geant4EventReader::EVENT_READER_EOF ) { + long nEvents = context()->kernel().property("NumEvents").value<long>(); + if(nEvents < 0) { + //context()->kernel().runManager().AbortRun(true); + throw DD4hep_End_Of_File(); + } + } + if ( Geant4EventReader::EVENT_READER_OK != status ) { string msg = issue(evid)+"Error when moving to event - "; if ( status == Geant4EventReader::EVENT_READER_EOF ) msg += " EOF: [end of file]."; @@ -187,6 +196,13 @@ int Geant4InputAction::readParticles(int evt_number, return status; } status = m_reader->readParticles(evid, vertices, particles); + if(status == Geant4EventReader::EVENT_READER_EOF ) { + long nEvents = context()->kernel().property("NumEvents").value<long>(); + if(nEvents < 0) { + //context()->kernel().runManager().AbortRun(true); + throw DD4hep_End_Of_File(); + } + } if ( Geant4EventReader::EVENT_READER_OK != status ) { diff --git a/DDG4/src/Geant4UIManager.cpp b/DDG4/src/Geant4UIManager.cpp index 078c5ba61..b27b47326 100644 --- a/DDG4/src/Geant4UIManager.cpp +++ b/DDG4/src/Geant4UIManager.cpp @@ -140,8 +140,14 @@ void Geant4UIManager::start() { // No UI. Pure batch mode: Simply execute requested number of events long numEvent = context()->kernel().property("NumEvents").value<long>(); + if(numEvent < 0) numEvent = std::numeric_limits<int>::max(); printout(INFO,"Geant4UIManager","++ Start run with %d events.",numEvent); - context()->kernel().runManager().BeamOn(numEvent); + try { + context()->kernel().runManager().BeamOn(numEvent); + } catch (DD4hep_End_Of_File& e) { + printout(INFO,"Geant4UIManager","++ End of file reached, ending run..."); + context()->kernel().runManager().RunTermination(); + } } /// Stop and release resources -- GitLab