Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
G4AtlasWorkerRunManager.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Hide multi-threading classes from builds without G4MT
6 #include "G4Types.hh"
7 #ifdef G4MULTITHREADED
8 
10 
11 #include "G4StateManager.hh"
12 #include "G4Event.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"
19 #include "G4HadronicProcessStore.hh"
20 
22 #include "GaudiKernel/ISvcLocator.h"
23 #include "GaudiKernel/Bootstrap.h"
24 #include "GaudiKernel/IMessageSvc.h"
25 #include "GaudiKernel/GaudiException.h"
26 
27 
28 #include <mutex>
29 
30 static std::mutex workerInitMutex;
31 
32 G4AtlasWorkerRunManager::G4AtlasWorkerRunManager()
33  : G4WorkerRunManager()
34  , AthMessaging("G4AtlasWorkerRunManager")
35  // TODO: what if we need to make these configurable?
36  , m_detGeoSvc("DetectorGeometrySvc", "G4AtlasWorkerRunManager")
37  , m_fastSimTool("FastSimulationMasterTool")
38 {}
39 
40 
41 G4AtlasWorkerRunManager* G4AtlasWorkerRunManager::GetG4AtlasWorkerRunManager()
42 {
43  // Grab thread-local pointer from base class
44  auto* wrm = G4RunManager::GetRunManager();
45  if(wrm) { return static_cast<G4AtlasWorkerRunManager*>(wrm); }
46  else { return new G4AtlasWorkerRunManager; }
47 }
48 
49 
51 {
52  // Locking this initialization to protect currently thread-unsafe services
53  std::lock_guard<std::mutex> lock(workerInitMutex);
54 
55  // Setup geometry and physics via the base class
56  G4HadronicProcessStore::Instance()->SetVerbose(0);
58 
59  /*
60  ** The following fragment of code applies all UI commangs from the master command stack.
61  ** It has been moved over here from G4InitTool::initThread() because in its previous
62  ** location the code had no effect (invalid application state).
63  **
64  ** Having this code here is OK, but placing it here we are changing some assumptions
65  ** implemented in the design of Geant4. This has to do with the handling of multiple
66  ** (G4)Runs in the same job. If a second run is executed and no physics or geometry
67  ** changes happened between the two runs [Worker]::Initialize() is not called. But UI
68  ** commands need to be called anyway, which is not going to happen with our implementation.
69  **
70  ** If ATLAS ever decides to run multiple G4 runs in the same job, all the MT initialization
71  ** will have to be thoroughly reviewed.
72  */
73  ATH_MSG_DEBUG("G4 Command: Trying at the end of Initialize()");
74  const std::string methodName = "G4AtlasWorkerRunManager::Initialize";
75  G4MTRunManager* masterRM = G4MTRunManager::GetMasterRunManager();
76  std::vector<G4String> cmds = masterRM->GetCommandStack();
77  G4UImanager* uimgr = G4UImanager::GetUIpointer();
78  for(const auto& it : cmds) {
79  int retVal = uimgr->ApplyCommand(it);
80  CommandLog(retVal, it);
81  if(retVal!=fCommandSucceeded) {
82  std::string errMsg{"Failed to apply command <"};
83  errMsg += (it + ">. Return value " + std::to_string(retVal));
84  throw GaudiException(errMsg,methodName,StatusCode::FAILURE);
85  }
86  }
87 
88  // Does some extra setup that we need.
89  ConstructScoringWorlds();
90  // Run initialization in G4RunManager.
91  // Normally done in BeamOn.
92  if (m_quietMode) { SetVerboseLevel(0); } // HACK
93  RunInitialization();
94 }
95 
96 void G4AtlasWorkerRunManager::InitializeGeometry()
97 {
98  const std::string methodName = "G4AtlasWorkerRunManager::InitializeGeometry";
99 
100  // I don't think this does anything
101  if(G4RunManager::IfGeometryHasBeenDestroyed()) {
102  G4TransportationManager::GetTransportationManager()->ClearParallelWorlds();
103  }
104 
105  // Get the world volume and give it to the kernel
106  G4RunManagerKernel* masterKernel = G4MTRunManager::GetMasterRunManagerKernel();
107  G4VPhysicalVolume* worldVol = masterKernel->GetCurrentWorld();
108  kernel->WorkerDefineWorldVolume(worldVol, false);
109  // We don't currently use parallel worlds in ATLAS, but someday we might
110  kernel->SetNumberOfParallelWorld(masterKernel->GetNumberOfParallelWorld());
111 
112  // Construct sensitive detectors and magnetic field
113  userDetector->ConstructSDandField();
114  userDetector->ConstructParallelSD();
115 
116  // Mark g4 geo as initialized
117  geometryInitialized = true;
118 }
119 
120 void G4AtlasWorkerRunManager::InitializePhysics()
121 {
122  const std::string methodName = "G4AtlasWorkerRunManager::InitializePhysics";
123 
124  // Call the base class
125  G4RunManager::InitializePhysics();
126 
127  // Setup the fast simulations
128  if(m_fastSimTool.retrieve().isFailure()) {
129  throw GaudiException("Could not retrieve FastSims master tool",
130  methodName, StatusCode::FAILURE);
131  }
132  if(m_fastSimTool->initializeFastSims().isFailure()) {
133  throw GaudiException("Failed to initialize FastSims for worker thread",
134  methodName, StatusCode::FAILURE);
135  }
136 }
137 
138 
139 bool G4AtlasWorkerRunManager::ProcessEvent(G4Event* event)
140 {
141 
142  G4StateManager* stateManager = G4StateManager::GetStateManager();
143  stateManager->SetNewState(G4State_GeomClosed);
144 
145  currentEvent = event;
146 
147  //eventManager->SetVerboseLevel(3);
148  //eventManager->GetTrackingManager()->SetVerboseLevel(3);
149  eventManager->ProcessOneEvent(currentEvent);
150  if (currentEvent->IsAborted()) {
151  ATH_MSG_WARNING( "G4AtlasWorkerRunManager::SimulateFADSEvent: " <<
152  "Event Aborted at Detector Simulation level" );
153  currentEvent = nullptr;
154  return true;
155  }
156 
157  this->AnalyzeEvent(currentEvent);
158  if (currentEvent->IsAborted()) {
159  ATH_MSG_WARNING( "G4AtlasWorkerRunManager::SimulateFADSEvent: " <<
160  "Event Aborted at Analysis level" );
161  currentEvent = nullptr;
162  return true;
163  }
164 
165  this->StackPreviousEvent(currentEvent);
166  bool abort = currentEvent->IsAborted();
167  currentEvent = nullptr;
168 
169  return abort;
170 }
171 
172 
173 void G4AtlasWorkerRunManager::RunTermination()
174 {
175  // Not sure what I should put here...
176  // Maybe I can just use the base class?
177  G4WorkerRunManager::RunTermination();
178 }
179 
180 void G4AtlasWorkerRunManager::CommandLog(int returnCode, const std::string& commandString) const
181 {
182  switch(returnCode) {
183  case 0: { ATH_MSG_DEBUG("G4 Command: " << commandString << " - Command Succeeded"); } break;
184  case 100: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Command Not Found!"); } break;
185  case 200: {
186  auto* stateManager = G4StateManager::GetStateManager();
187  ATH_MSG_DEBUG("G4 Command: " << commandString << " - Illegal Application State (" <<
188  stateManager->GetStateString(stateManager->GetCurrentState()) << ")!");
189  } break;
190  case 300: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Parameter Out of Range!"); } break;
191  case 400: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Parameter Unreadable!"); } break;
192  case 500: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Parameter Out of Candidates!"); } break;
193  case 600: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Alias Not Found!"); } break;
194  default: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Unknown Status!"); } break;
195  }
196 
197 }
198 
199 #endif // G4MULTITHREADED
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
skel.it
it
Definition: skel.GENtoEVGEN.py:407
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
Herwig7_QED_EvtGen_Common.cmds
string cmds
Definition: Herwig7_QED_EvtGen_Common.py:11
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
G4AtlasWorkerRunManager.h
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
jobOptions.Initialize
Initialize
Definition: jobOptions.pA.py:28
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
IGeoModelSvc.h