Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 09:13:10

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
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   // A run boundary permits reconfiguration. In particular, Geant4's
0053   // SetEnergyCutVector() does not mark its physics tables as modified.
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       // Preserve the original initialization error.
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       // Preserve the event error if an end-run callback also fails.
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       // An exception interrupted event processing. Release the event through
0122       // Geant4's normal ownership path before terminating the run.
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     // In particular, an exception in EndOfRunAction must not leave Geant4 in
0141     // GeomClosed/EventProc state and prevent subsequent use of the handle.
0142     kernel->RunTermination();
0143     throw;
0144   }
0145 }
0146 
0147 }  // namespace ActsExamples::Geant4