File indexing completed on 2026-11-28 09:13:10
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ActsExamples/Geant4/RunManager.hpp"
0010
0011 #include <exception>
0012 #include <limits>
0013 #include <stdexcept>
0014
0015 #include <G4Run.hh>
0016 #include <G4RunManagerKernel.hh>
0017 #include <G4StateManager.hh>
0018 #include <G4ios.hh>
0019
0020 namespace ActsExamples::Geant4 {
0021
0022 RunManager::~RunManager() {
0023 try {
0024 endRun();
0025 } catch (const std::exception& e) {
0026 G4cerr << "Failed to close ACTS Geant4 run: " << e.what() << G4endl;
0027 } catch (...) {
0028 G4cerr << "Failed to close ACTS Geant4 run" << G4endl;
0029 }
0030 }
0031
0032 void RunManager::checkThread() const {
0033 if (std::this_thread::get_id() != m_runThread) {
0034 throw std::logic_error(
0035 "Geant4 persistent runs must execute on the initializing thread; "
0036 "use numThreads=1");
0037 }
0038 }
0039
0040 void RunManager::beginRun(int numberOfEvents) {
0041 checkThread();
0042 if (m_runActive) {
0043 throw std::logic_error("Geant4 persistent run is already active");
0044 }
0045 if (numberOfEvents < 0) {
0046 throw std::invalid_argument("Negative Geant4 event count");
0047 }
0048 Initialize();
0049 if (!ConfirmBeamOnCondition()) {
0050 throw std::runtime_error("Cannot initialize Geant4 persistent run");
0051 }
0052
0053
0054 PhysicsHasBeenModified();
0055 m_eventCountKnown = numberOfEvents != 0;
0056 SetNumberOfEventsToBeProcessed(numberOfEvents);
0057 fakeRun = false;
0058 ConstructScoringWorlds();
0059 m_runActive = true;
0060 try {
0061 RunInitialization();
0062 if (currentRun == nullptr ||
0063 G4StateManager::GetStateManager()->GetCurrentState() !=
0064 G4State_GeomClosed) {
0065 throw std::runtime_error("Geant4 run initialization failed");
0066 }
0067 InitializeEventLoop(numberOfEvents);
0068 m_eventLoopInitialized = true;
0069 if (runAborted) {
0070 throw std::runtime_error("Geant4 run aborted during initialization");
0071 }
0072 } catch (...) {
0073 try {
0074 endRun();
0075 } catch (...) {
0076
0077 }
0078 throw;
0079 }
0080 }
0081
0082 void RunManager::processEvent() {
0083 if (!m_runActive) {
0084 throw std::logic_error("Geant4 persistent run is not active");
0085 }
0086 checkThread();
0087 try {
0088 if (runAborted) {
0089 throw std::runtime_error("Geant4 run aborted");
0090 }
0091 if (numberOfEventProcessed == std::numeric_limits<int>::max()) {
0092 throw std::overflow_error("Geant4 event count exceeds the integer range");
0093 }
0094 if (!m_eventCountKnown) {
0095 SetNumberOfEventsToBeProcessed(numberOfEventProcessed + 1);
0096 currentRun->SetNumberOfEventToBeProcessed(numberOfEventToBeProcessed);
0097 }
0098 ProcessOneEvent(numberOfEventProcessed);
0099 TerminateOneEvent();
0100 if (runAborted) {
0101 throw std::runtime_error("Geant4 run aborted");
0102 }
0103 } catch (...) {
0104 try {
0105 endRun();
0106 } catch (...) {
0107
0108 }
0109 throw;
0110 }
0111 }
0112
0113 void RunManager::endRun() {
0114 if (!m_runActive) {
0115 return;
0116 }
0117 checkThread();
0118 m_runActive = false;
0119 try {
0120 if (currentEvent != nullptr) {
0121
0122
0123 AbortRun();
0124 TerminateOneEvent();
0125 if (G4StateManager::GetStateManager()->GetCurrentState() ==
0126 G4State_EventProc) {
0127 G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
0128 }
0129 }
0130 if (!m_eventCountKnown && currentRun != nullptr) {
0131 SetNumberOfEventsToBeProcessed(currentRun->GetNumberOfEvent());
0132 currentRun->SetNumberOfEventToBeProcessed(numberOfEventToBeProcessed);
0133 }
0134 if (m_eventLoopInitialized) {
0135 m_eventLoopInitialized = false;
0136 TerminateEventLoop();
0137 }
0138 RunTermination();
0139 } catch (...) {
0140
0141
0142 kernel->RunTermination();
0143 throw;
0144 }
0145 }
0146
0147 }