20 #include "G4StateManager.hh"
21 #include "G4TransportationManager.hh"
22 #include "G4RunManagerKernel.hh"
23 #include "G4EventManager.hh"
24 #include "G4Navigator.hh"
25 #include "G4PropagatorInField.hh"
26 #include "G4TrackingManager.hh"
27 #include "G4StackManager.hh"
28 #include "G4UImanager.hh"
29 #include "G4ScoringManager.hh"
30 #include "G4VUserPhysicsList.hh"
31 #include "G4VModularPhysicsList.hh"
32 #include "G4ParallelWorldPhysics.hh"
33 #include "G4GDMLParser.hh"
36 #include "CLHEP/Random/RandomEngine.h"
43 #include "GaudiKernel/IThreadInitTool.h"
50 static std::once_flag initializeOnceFlag;
51 static std::once_flag finalizeOnceFlag;
52 static std::once_flag releaseGeoModelOnceFlag;
72 if(!m_simplifiedGeoPath.empty()) {
75 if (geoFile.empty()) {
76 ATH_MSG_FATAL(
"Could not find simplified geometry file: " << m_simplifiedGeoPath);
77 return StatusCode::FAILURE;
81 parser.Read(geoFile,
false);
85 if (m_recordFlux) G4ScoringManager::GetScoringManager();
94 ATH_MSG_ERROR(
"Failure in G4AtlasAlg::initializeOnce: " <<
e.what());
95 return StatusCode::FAILURE;
105 ATH_CHECK( m_truthRecordSvc.retrieve() );
106 ATH_MSG_INFO(
"- Using ISF TruthRecordSvc : " << m_truthRecordSvc.typeAndName() );
108 ATH_MSG_INFO(
"- Using ISF GeoIDSvc : " << m_geoIDSvc.typeAndName() );
115 ATH_CHECK( m_inputTruthCollectionKey.initialize());
116 ATH_CHECK( m_outputTruthCollectionKey.initialize());
117 ATH_CHECK( m_eventInfoKey.initialize() );
120 if ( not m_truthPreselectionTool.empty() ) {
121 ATH_CHECK(m_truthPreselectionTool.retrieve());
124 if ( not m_qspatcher.empty() ) {
129 return StatusCode::SUCCESS;
137 throw std::runtime_error(
"Could not initialize ATLAS PhysicsListSvc!");
141 throw std::runtime_error(
"Failed to add action tool "+action_tool.name());
147 #ifdef G4MULTITHREADED
149 G4AtlasMTRunManager::GetG4AtlasMTRunManager();
155 std::unique_ptr<G4AtlasUserWorkerThreadInitialization> workerInit =
156 std::make_unique<G4AtlasUserWorkerThreadInitialization>();
157 workerInit->SetDetGeoSvc(
m_detGeoSvc.typeAndName() );
158 workerInit->SetFastSimMasterTool(
m_fastSimTool.typeAndName() );
159 runMgr->SetUserInitialization( workerInit.release() );
160 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
162 runMgr->SetUserInitialization(actionInitialization.release());
164 throw std::runtime_error(
"Trying to use multi-threading in non-MT build!");
170 G4AtlasRunManager::GetG4AtlasRunManager();
172 runMgr->SetRecordFlux(
m_recordFlux, std::make_unique<G4AtlasFluxRecorder>() );
173 runMgr->SetLogLevel(
int(
msg().
level()) );
177 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
179 runMgr->SetUserInitialization(actionInitialization.release());
183 G4UImanager *ui = G4UImanager::GetUIpointer();
187 ATH_MSG_INFO(
"G4AtlasAlg specific libraries requested ");
189 ui->ApplyCommand(temp);
194 std::string temp=
"/Physics/GetPhysicsList "+
m_physList;
195 ui->ApplyCommand(temp);
201 std::string temp=
"/MagneticField/Select "+
m_fieldMap;
202 ui->ApplyCommand(temp);
203 ui->ApplyCommand(
"/MagneticField/Initialize");
207 ATH_MSG_DEBUG(
"G4 Command: Trying at the end of initializeOnce()");
209 int returnCode = ui->ApplyCommand( g4command );
214 auto* rm = G4RunManager::GetRunManager();
216 throw std::runtime_error(
"Run manager retrieval has failed");
220 if(!
m_useMT && rm->ConfirmBeamOnCondition()) {
221 rm->RunInitialization();
224 ATH_MSG_INFO(
"retireving the Detector Geometry Service" );
226 throw std::runtime_error(
"Could not initialize ATLAS DetectorGeometrySvc!");
230 throw std::runtime_error(
"Could not initialize ATLAS UserLimitsSvc!");
234 G4VModularPhysicsList* thePhysicsList=
dynamic_cast<G4VModularPhysicsList*
>(
m_physListSvc->GetPhysicsList());
235 if (!thePhysicsList) {
236 throw std::runtime_error(
"Failed dynamic_cast!! this is not a G4VModularPhysicsList!");
238 #if G4VERSION_NUMBER >= 1010
239 std::vector<std::string>& parallelWorldNames=
m_detGeoSvc->GetParallelWorldNames();
240 for (
auto&
it: parallelWorldNames) {
241 thePhysicsList->RegisterPhysics(
new G4ParallelWorldPhysics(
it,
true));
252 G4TransportationManager *tm = G4TransportationManager::GetTransportationManager();
253 G4RunManagerKernel *rmk = G4RunManagerKernel::GetRunManagerKernel();
254 G4EventManager *em = G4EventManager::GetEventManager();
258 tm->GetNavigatorForTracking()->SetVerboseLevel(
atof(itr->second.data()) );
261 tm->GetPropagatorInField()->SetVerboseLevel(
atof(itr->second.data()) );
264 rmk->GetTrackingManager()->SetVerboseLevel(
atof(itr->second.data()) );
267 rmk->GetTrackingManager()->GetSteppingManager()->
268 SetVerboseLevel(
atof(itr->second.data()) );
271 rmk->GetStackManager()->SetVerboseLevel(
atof(itr->second.data()) );
274 em->SetVerboseLevel(
atof(itr->second.data()) );
285 ATH_MSG_DEBUG(
"++++++++++++ G4AtlasAlg finalized ++++++++++++" <<std::endl<<std::endl);
292 ATH_MSG_ERROR(
"Failure in G4AtlasAlg::finalizeOnce: " <<
e.what());
293 return StatusCode::FAILURE;
296 return StatusCode::SUCCESS;
303 auto runMgr = G4RunManager::GetRunManager();
304 runMgr->RunTermination();
311 static std::atomic<unsigned int> n_Event=0;
312 ATH_MSG_DEBUG(
"++++++++++++ G4AtlasAlg execute ++++++++++++");
314 #ifdef G4MULTITHREADED
320 if (G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume() ==
nullptr)
322 ToolHandle<IThreadInitTool> ti (
"G4ThreadInitTool",
nullptr);
330 if (n_Event<=10 || (n_Event%100) == 0) {
331 ATH_MSG_ALWAYS(
"G4AtlasAlg: Event num. " << n_Event <<
" start processing");
340 ATH_MSG_ERROR(
"Failure in G4AtlasAlg::releaseGeoModel: " <<
e.what());
341 return StatusCode::FAILURE;
345 const EventContext& ctx = Gaudi::Hive::currentContext();
350 G4Random::setTheEngine(*rngWrapper);
358 if (!inputTruthCollection.
isValid()) {
359 ATH_MSG_FATAL(
"Unable to read input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
360 return StatusCode::FAILURE;
362 ATH_MSG_DEBUG(
"Found input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
365 std::unique_ptr<McEventCollection> shadowTruth{};
367 outputTruthCollection = std::make_unique<McEventCollection>();
369 shadowTruth = std::make_unique<McEventCollection>(*inputTruthCollection);
370 for (HepMC::GenEvent* currentGenEvent : *shadowTruth ) {
377 outputTruthCollection->
push_back(outputEvent.release());
382 outputTruthCollection = std::make_unique<McEventCollection>(*inputTruthCollection);
384 shadowTruth = std::make_unique<McEventCollection>();
387 for (HepMC::GenEvent* currentGenEvent : *outputTruthCollection ) {
393 ATH_MSG_DEBUG(
"Recorded output GenEvent collection " << outputTruthCollection.
name() <<
" in store " << outputTruthCollection.
store());
402 G4Event *inputEvent{};
411 #ifdef G4MULTITHREADED
412 auto* workerRM = G4AtlasWorkerRunManager::GetG4AtlasWorkerRunManager();
413 abort = workerRM->ProcessEvent(inputEvent);
415 ATH_MSG_ERROR(
"Trying to use multi-threading in non-MT build!");
416 return StatusCode::FAILURE;
421 G4AtlasRunManager::GetG4AtlasRunManager();
422 abort = workerRM->ProcessEvent(inputEvent);
429 setFilterPassed(
false);
434 ATH_MSG_FATAL(
"Failed to retrieve xAOD::EventInfo while trying to update the error state!" );
435 return StatusCode::FAILURE;
452 for (HepMC::GenEvent* currentGenEvent : *outputTruthCollection ) {
457 return StatusCode::SUCCESS;
464 ISvcLocator *svcLocator = Gaudi::svcLocator();
466 if(svcLocator->service(
"GeoModelSvc",geoModel).isFailure()) {
470 if(geoModel->
clear().isFailure()) {
474 ATH_MSG_INFO(
" ----> GeoModelSvc::clear() succeeded " );
484 case 0: {
ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Command Succeeded"); }
break;
485 case 100: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Command Not Found!"); }
break;
487 auto* stateManager = G4StateManager::GetStateManager();
488 ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Illegal Application State (" <<
489 stateManager->GetStateString(stateManager->GetCurrentState()) <<
")!");
491 case 300: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Range!"); }
break;
492 case 400: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Unreadable!"); }
break;
493 case 500: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Candidates!"); }
break;
494 case 600: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Alias Not Found!"); }
break;
495 default: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Unknown Status!"); }
break;