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();
156 std::unique_ptr<G4AtlasUserWorkerThreadInitialization> workerInit =
157 std::make_unique<G4AtlasUserWorkerThreadInitialization>();
158 workerInit->SetDetGeoSvc(
m_detGeoSvc.typeAndName() );
159 workerInit->SetFastSimMasterTool(
m_fastSimTool.typeAndName() );
161 runMgr->SetUserInitialization( workerInit.release() );
162 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
164 runMgr->SetUserInitialization(actionInitialization.release());
166 throw std::runtime_error(
"Trying to use multi-threading in non-MT build!");
172 G4AtlasRunManager::GetG4AtlasRunManager();
174 runMgr->SetRecordFlux(
m_recordFlux, std::make_unique<G4AtlasFluxRecorder>() );
175 runMgr->SetLogLevel(
int(
msg().
level()) );
180 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
182 runMgr->SetUserInitialization(actionInitialization.release());
186 G4UImanager *ui = G4UImanager::GetUIpointer();
190 ATH_MSG_INFO(
"G4AtlasAlg specific libraries requested ");
192 ui->ApplyCommand(temp);
197 std::string temp=
"/Physics/GetPhysicsList "+
m_physList;
198 ui->ApplyCommand(temp);
204 std::string temp=
"/MagneticField/Select "+
m_fieldMap;
205 ui->ApplyCommand(temp);
206 ui->ApplyCommand(
"/MagneticField/Initialize");
210 ATH_MSG_DEBUG(
"G4 Command: Trying at the end of initializeOnce()");
212 int returnCode = ui->ApplyCommand( g4command );
217 auto* rm = G4RunManager::GetRunManager();
219 throw std::runtime_error(
"Run manager retrieval has failed");
223 if(!
m_useMT && rm->ConfirmBeamOnCondition()) {
224 rm->RunInitialization();
227 ATH_MSG_INFO(
"retireving the Detector Geometry Service" );
229 throw std::runtime_error(
"Could not initialize ATLAS DetectorGeometrySvc!");
233 throw std::runtime_error(
"Could not initialize ATLAS UserLimitsSvc!");
237 G4VModularPhysicsList* thePhysicsList=
dynamic_cast<G4VModularPhysicsList*
>(
m_physListSvc->GetPhysicsList());
238 if (!thePhysicsList) {
239 throw std::runtime_error(
"Failed dynamic_cast!! this is not a G4VModularPhysicsList!");
241 #if G4VERSION_NUMBER >= 1010
242 std::vector<std::string>& parallelWorldNames=
m_detGeoSvc->GetParallelWorldNames();
243 for (
auto&
it: parallelWorldNames) {
244 thePhysicsList->RegisterPhysics(
new G4ParallelWorldPhysics(
it,
true));
255 G4TransportationManager *tm = G4TransportationManager::GetTransportationManager();
256 G4RunManagerKernel *rmk = G4RunManagerKernel::GetRunManagerKernel();
257 G4EventManager *em = G4EventManager::GetEventManager();
261 tm->GetNavigatorForTracking()->SetVerboseLevel(
atof(itr->second.data()) );
264 tm->GetPropagatorInField()->SetVerboseLevel(
atof(itr->second.data()) );
267 rmk->GetTrackingManager()->SetVerboseLevel(
atof(itr->second.data()) );
270 rmk->GetTrackingManager()->GetSteppingManager()->
271 SetVerboseLevel(
atof(itr->second.data()) );
274 rmk->GetStackManager()->SetVerboseLevel(
atof(itr->second.data()) );
277 em->SetVerboseLevel(
atof(itr->second.data()) );
288 ATH_MSG_DEBUG(
"++++++++++++ G4AtlasAlg finalized ++++++++++++" <<std::endl<<std::endl);
295 ATH_MSG_ERROR(
"Failure in G4AtlasAlg::finalizeOnce: " <<
e.what());
296 return StatusCode::FAILURE;
299 return StatusCode::SUCCESS;
306 auto runMgr = G4RunManager::GetRunManager();
307 runMgr->RunTermination();
314 static std::atomic<unsigned int> n_Event=0;
315 ATH_MSG_DEBUG(
"++++++++++++ G4AtlasAlg execute ++++++++++++");
317 #ifdef G4MULTITHREADED
323 if (G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume() ==
nullptr)
325 ToolHandle<IThreadInitTool> ti (
"G4ThreadInitTool",
nullptr);
333 if (n_Event<=10 || (n_Event%100) == 0) {
334 ATH_MSG_ALWAYS(
"G4AtlasAlg: Event num. " << n_Event <<
" start processing");
343 ATH_MSG_ERROR(
"Failure in G4AtlasAlg::releaseGeoModel: " <<
e.what());
344 return StatusCode::FAILURE;
348 const EventContext& ctx = Gaudi::Hive::currentContext();
353 G4Random::setTheEngine(*rngWrapper);
361 if (!inputTruthCollection.
isValid()) {
362 ATH_MSG_FATAL(
"Unable to read input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
363 return StatusCode::FAILURE;
365 ATH_MSG_DEBUG(
"Found input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
368 std::unique_ptr<McEventCollection> shadowTruth{};
370 outputTruthCollection = std::make_unique<McEventCollection>();
372 shadowTruth = std::make_unique<McEventCollection>(*inputTruthCollection);
373 for (HepMC::GenEvent* currentGenEvent : *shadowTruth ) {
380 outputTruthCollection->
push_back(outputEvent.release());
385 outputTruthCollection = std::make_unique<McEventCollection>(*inputTruthCollection);
387 shadowTruth = std::make_unique<McEventCollection>();
390 for (HepMC::GenEvent* currentGenEvent : *outputTruthCollection ) {
396 ATH_MSG_DEBUG(
"Recorded output GenEvent collection " << outputTruthCollection.
name() <<
" in store " << outputTruthCollection.
store());
405 G4Event *inputEvent{};
414 #ifdef G4MULTITHREADED
415 auto* workerRM = G4AtlasWorkerRunManager::GetG4AtlasWorkerRunManager();
416 abort = workerRM->ProcessEvent(inputEvent);
418 ATH_MSG_ERROR(
"Trying to use multi-threading in non-MT build!");
419 return StatusCode::FAILURE;
424 G4AtlasRunManager::GetG4AtlasRunManager();
425 abort = workerRM->ProcessEvent(inputEvent);
432 setFilterPassed(
false);
437 ATH_MSG_FATAL(
"Failed to retrieve xAOD::EventInfo while trying to update the error state!" );
438 return StatusCode::FAILURE;
455 for (HepMC::GenEvent* currentGenEvent : *outputTruthCollection ) {
460 return StatusCode::SUCCESS;
467 SmartIF<IGeoModelSvc> geoModel{Gaudi::svcLocator()->service(
"GeoModelSvc")};
472 if (geoModel->clear().isFailure()) {
476 ATH_MSG_INFO(
" ----> GeoModelSvc::clear() succeeded " );
486 case 0: {
ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Command Succeeded"); }
break;
487 case 100: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Command Not Found!"); }
break;
489 auto* stateManager = G4StateManager::GetStateManager();
490 ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Illegal Application State (" <<
491 stateManager->GetStateString(stateManager->GetCurrentState()) <<
")!");
493 case 300: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Range!"); }
break;
494 case 400: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Unreadable!"); }
break;
495 case 500: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Candidates!"); }
break;
496 case 600: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Alias Not Found!"); }
break;
497 default: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Unknown Status!"); }
break;