11 #include "G4StateManager.hh"
13 #include "G4LogicalVolumeStore.hh"
14 #include "G4GeometryManager.hh"
15 #include "G4MTRunManager.hh"
16 #include "G4TransportationManager.hh"
17 #include "G4VUserDetectorConstruction.hh"
18 #include "G4UImanager.hh"
21 #include "GaudiKernel/ISvcLocator.h"
22 #include "GaudiKernel/Bootstrap.h"
23 #include "GaudiKernel/IMessageSvc.h"
24 #include "GaudiKernel/GaudiException.h"
31 G4AtlasWorkerRunManager::G4AtlasWorkerRunManager()
32 : G4WorkerRunManager()
35 , m_detGeoSvc(
"DetectorGeometrySvc",
"G4AtlasWorkerRunManager")
36 , m_fastSimTool(
"FastSimulationMasterTool")
40 G4AtlasWorkerRunManager* G4AtlasWorkerRunManager::GetG4AtlasWorkerRunManager()
43 auto* wrm = G4RunManager::GetRunManager();
44 if(wrm) {
return static_cast<G4AtlasWorkerRunManager*
>(wrm); }
45 else {
return new G4AtlasWorkerRunManager; }
52 std::lock_guard<std::mutex> lock(workerInitMutex);
71 ATH_MSG_DEBUG(
"G4 Command: Trying at the end of Initialize()");
72 const std::string methodName =
"G4AtlasWorkerRunManager::Initialize";
73 G4MTRunManager* masterRM = G4MTRunManager::GetMasterRunManager();
74 std::vector<G4String>
cmds = masterRM->GetCommandStack();
75 G4UImanager* uimgr = G4UImanager::GetUIpointer();
76 for(
const auto&
it :
cmds) {
77 int retVal = uimgr->ApplyCommand(
it);
78 CommandLog(retVal,
it);
79 if(retVal!=fCommandSucceeded) {
80 std::string errMsg{
"Failed to apply command <"};
82 throw GaudiException(errMsg,methodName,StatusCode::FAILURE);
87 ConstructScoringWorlds();
93 void G4AtlasWorkerRunManager::InitializeGeometry()
95 const std::string methodName =
"G4AtlasWorkerRunManager::InitializeGeometry";
98 if(G4RunManager::IfGeometryHasBeenDestroyed()) {
99 G4TransportationManager::GetTransportationManager()->ClearParallelWorlds();
103 G4RunManagerKernel* masterKernel = G4MTRunManager::GetMasterRunManagerKernel();
104 G4VPhysicalVolume* worldVol = masterKernel->GetCurrentWorld();
105 kernel->WorkerDefineWorldVolume(worldVol,
false);
107 kernel->SetNumberOfParallelWorld(masterKernel->GetNumberOfParallelWorld());
110 userDetector->ConstructSDandField();
111 userDetector->ConstructParallelSD();
114 geometryInitialized =
true;
117 void G4AtlasWorkerRunManager::InitializePhysics()
119 const std::string methodName =
"G4AtlasWorkerRunManager::InitializePhysics";
122 G4RunManager::InitializePhysics();
125 if(m_fastSimTool.retrieve().isFailure()) {
126 throw GaudiException(
"Could not retrieve FastSims master tool",
127 methodName, StatusCode::FAILURE);
129 if(m_fastSimTool->initializeFastSims().isFailure()) {
130 throw GaudiException(
"Failed to initialize FastSims for worker thread",
131 methodName, StatusCode::FAILURE);
136 bool G4AtlasWorkerRunManager::ProcessEvent(G4Event*
event)
139 G4StateManager* stateManager = G4StateManager::GetStateManager();
140 stateManager->SetNewState(G4State_GeomClosed);
142 currentEvent =
event;
146 eventManager->ProcessOneEvent(currentEvent);
147 if (currentEvent->IsAborted()) {
149 "Event Aborted at Detector Simulation level" );
150 currentEvent =
nullptr;
154 this->AnalyzeEvent(currentEvent);
155 if (currentEvent->IsAborted()) {
157 "Event Aborted at Analysis level" );
158 currentEvent =
nullptr;
162 this->StackPreviousEvent(currentEvent);
163 bool abort = currentEvent->IsAborted();
164 currentEvent =
nullptr;
170 void G4AtlasWorkerRunManager::RunTermination()
174 G4WorkerRunManager::RunTermination();
177 void G4AtlasWorkerRunManager::CommandLog(
int returnCode,
const std::string& commandString)
const
180 case 0: {
ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Command Succeeded"); }
break;
181 case 100: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Command Not Found!"); }
break;
183 auto* stateManager = G4StateManager::GetStateManager();
184 ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Illegal Application State (" <<
185 stateManager->GetStateString(stateManager->GetCurrentState()) <<
")!");
187 case 300: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Range!"); }
break;
188 case 400: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Unreadable!"); }
break;
189 case 500: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Candidates!"); }
break;
190 case 600: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Alias Not Found!"); }
break;
191 default: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Unknown Status!"); }
break;
196 #endif // G4MULTITHREADED