22 #include "G4EventManager.hh"
23 #include "G4GDMLParser.hh"
24 #include "G4Navigator.hh"
25 #include "G4ParallelWorldPhysics.hh"
26 #include "G4PropagatorInField.hh"
27 #include "G4RunManagerKernel.hh"
28 #include "G4ScoringManager.hh"
29 #include "G4StackManager.hh"
30 #include "G4StateManager.hh"
31 #include "G4TrackingManager.hh"
32 #include "G4TransportationManager.hh"
33 #include "G4UImanager.hh"
34 #include "G4VModularPhysicsList.hh"
35 #include "G4VUserPhysicsList.hh"
38 #include "CLHEP/Random/RandomEngine.h"
41 #include "GaudiKernel/IThreadInitTool.h"
54 static std::once_flag initializeOnceFlag;
55 static std::once_flag finalizeOnceFlag;
56 static std::once_flag releaseGeoModelOnceFlag;
76 if(!m_simplifiedGeoPath.empty()) {
79 if (geoFile.empty()) {
80 ATH_MSG_FATAL(
"Could not find simplified geometry file: " << m_simplifiedGeoPath);
81 return StatusCode::FAILURE;
85 parser.Read(geoFile,
false);
89 if (m_recordFlux) G4ScoringManager::GetScoringManager();
97 ATH_MSG_ERROR(
"Failure in G4AtlasAlg::initializeOnce: " <<
e.what());
98 return StatusCode::FAILURE;
108 ATH_CHECK( m_truthRecordSvc.retrieve() );
109 ATH_MSG_INFO(
"- Using ISF TruthRecordSvc : " << m_truthRecordSvc.typeAndName() );
111 ATH_MSG_INFO(
"- Using ISF GeoIDSvc : " << m_geoIDSvc.typeAndName() );
118 ATH_CHECK( m_inputTruthCollectionKey.initialize());
119 ATH_CHECK( m_outputTruthCollectionKey.initialize());
120 ATH_CHECK( m_eventInfoKey.initialize() );
123 if ( not m_truthPreselectionTool.empty() ) {
124 ATH_CHECK(m_truthPreselectionTool.retrieve());
127 if ( not m_qspatcher.empty() ) {
132 return StatusCode::SUCCESS;
140 throw std::runtime_error(
"Could not initialize ATLAS PhysicsListSvc!");
144 throw std::runtime_error(
"Failed to add action tool "+action_tool.name());
148 ATH_MSG_INFO(
"retrieving the Detector Construction tool" );
150 throw std::runtime_error(
"Could not initialize ATLAS DetectorConstruction!");
155 #ifdef G4MULTITHREADED
157 G4AtlasMTRunManager::GetG4AtlasMTRunManager();
163 std::unique_ptr<G4AtlasUserWorkerThreadInitialization> workerInit =
164 std::make_unique<G4AtlasUserWorkerThreadInitialization>();
166 runMgr->SetUserInitialization( workerInit.release() );
167 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
169 runMgr->SetUserInitialization(actionInitialization.release());
171 throw std::runtime_error(
"Trying to use multi-threading in non-MT build!");
177 G4AtlasRunManager::GetG4AtlasRunManager();
179 runMgr->SetRecordFlux(
m_recordFlux, std::make_unique<G4AtlasFluxRecorder>() );
180 runMgr->SetLogLevel(
int(
msg().
level()) );
184 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
186 runMgr->SetUserInitialization(actionInitialization.release());
190 G4UImanager *ui = G4UImanager::GetUIpointer();
194 ATH_MSG_INFO(
"G4AtlasAlg specific libraries requested ");
196 ui->ApplyCommand(temp);
201 std::string temp=
"/Physics/GetPhysicsList "+
m_physList;
202 ui->ApplyCommand(temp);
208 std::string temp=
"/MagneticField/Select "+
m_fieldMap;
209 ui->ApplyCommand(temp);
210 ui->ApplyCommand(
"/MagneticField/Initialize");
214 ATH_MSG_DEBUG(
"G4 Command: Trying at the end of initializeOnce()");
216 int returnCode = ui->ApplyCommand( g4command );
221 auto* rm = G4RunManager::GetRunManager();
223 throw std::runtime_error(
"Run manager retrieval has failed");
227 if(!
m_useMT && rm->ConfirmBeamOnCondition()) {
228 rm->RunInitialization();
233 if (physicsTool->initializePhysics().isFailure()) {
234 throw std::runtime_error(
"Failed to initialize physics with tool " + physicsTool.name());
239 throw std::runtime_error(
"Could not initialize ATLAS UserLimitsSvc!");
243 G4VModularPhysicsList* thePhysicsList=
dynamic_cast<G4VModularPhysicsList*
>(
m_physListSvc->GetPhysicsList());
244 if (!thePhysicsList) {
245 throw std::runtime_error(
"Failed dynamic_cast!! this is not a G4VModularPhysicsList!");
247 #if G4VERSION_NUMBER >= 1010
248 std::vector<std::string>& parallelWorldNames=
m_detConstruction->GetParallelWorldNames();
249 for (
auto&
it: parallelWorldNames) {
250 thePhysicsList->RegisterPhysics(
new G4ParallelWorldPhysics(
it,
true));
261 G4TransportationManager *tm = G4TransportationManager::GetTransportationManager();
262 G4RunManagerKernel *rmk = G4RunManagerKernel::GetRunManagerKernel();
263 G4EventManager *em = G4EventManager::GetEventManager();
267 tm->GetNavigatorForTracking()->SetVerboseLevel(
atof(itr->second.data()) );
270 tm->GetPropagatorInField()->SetVerboseLevel(
atof(itr->second.data()) );
273 rmk->GetTrackingManager()->SetVerboseLevel(
atof(itr->second.data()) );
276 rmk->GetTrackingManager()->GetSteppingManager()->
277 SetVerboseLevel(
atof(itr->second.data()) );
280 rmk->GetStackManager()->SetVerboseLevel(
atof(itr->second.data()) );
283 em->SetVerboseLevel(
atof(itr->second.data()) );
294 ATH_MSG_DEBUG(
"++++++++++++ G4AtlasAlg finalized ++++++++++++" <<std::endl<<std::endl);
301 ATH_MSG_ERROR(
"Failure in G4AtlasAlg::finalizeOnce: " <<
e.what());
302 return StatusCode::FAILURE;
305 return StatusCode::SUCCESS;
312 auto runMgr = G4RunManager::GetRunManager();
313 runMgr->RunTermination();
320 static std::atomic<unsigned int> n_Event=0;
321 ATH_MSG_DEBUG(
"++++++++++++ G4AtlasAlg execute ++++++++++++");
325 if (n_Event<=10 || (n_Event%100) == 0) {
326 ATH_MSG_ALWAYS(
"G4AtlasAlg: Event num. " << n_Event <<
" start processing");
335 ATH_MSG_ERROR(
"Failure in G4AtlasAlg::releaseGeoModel: " <<
e.what());
336 return StatusCode::FAILURE;
340 const EventContext& ctx = Gaudi::Hive::currentContext();
345 G4Random::setTheEngine(*rngWrapper);
349 auto eventInfo = std::make_unique<AtlasG4EventUserInfo>();
351 std::shared_ptr<HitCollectionMap> hitCollections = eventInfo->GetHitCollectionMap();
357 if (!inputTruthCollection.
isValid()) {
358 ATH_MSG_FATAL(
"Unable to read input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
359 return StatusCode::FAILURE;
361 ATH_MSG_DEBUG(
"Found input GenEvent collection " << inputTruthCollection.
name() <<
" in store " << inputTruthCollection.
store());
364 std::unique_ptr<McEventCollection> shadowTruth{};
366 outputTruthCollection = std::make_unique<McEventCollection>();
368 shadowTruth = std::make_unique<McEventCollection>(*inputTruthCollection);
369 for (HepMC::GenEvent* currentGenEvent : *shadowTruth ) {
376 outputTruthCollection->
push_back(outputEvent.release());
381 outputTruthCollection = std::make_unique<McEventCollection>(*inputTruthCollection);
383 shadowTruth = std::make_unique<McEventCollection>();
386 for (HepMC::GenEvent* currentGenEvent : *outputTruthCollection ) {
392 ATH_MSG_DEBUG(
"Recorded output GenEvent collection " << outputTruthCollection.
name() <<
" in store " << outputTruthCollection.
store());
405 auto inputEvent = std::make_unique<G4Event>(ctx.eventID().event_number());
406 inputEvent->SetUserInformation(eventInfo.release());
409 *outputTruthCollection, *inputEvent, *shadowTruth));
415 #ifdef G4MULTITHREADED
416 auto* workerRM = G4AtlasWorkerRunManager::GetG4AtlasWorkerRunManager();
417 abort = workerRM->ProcessEvent(inputEvent.release());
419 ATH_MSG_ERROR(
"Trying to use multi-threading in non-MT build!");
420 return StatusCode::FAILURE;
424 G4AtlasRunManager::GetG4AtlasRunManager();
425 abort = workerRM->ProcessEvent(inputEvent.release());
433 setFilterPassed(
false);
439 "Failed to retrieve xAOD::EventInfo while trying to update the "
441 return StatusCode::FAILURE;
457 for (HepMC::GenEvent* currentGenEvent : *outputTruthCollection ) {
462 return StatusCode::SUCCESS;
469 SmartIF<IGeoModelSvc> geoModel{Gaudi::svcLocator()->service(
"GeoModelSvc")};
474 if (geoModel->clear().isFailure()) {
478 ATH_MSG_INFO(
" ----> GeoModelSvc::clear() succeeded " );
488 case 0: {
ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Command Succeeded"); }
break;
489 case 100: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Command Not Found!"); }
break;
491 auto* stateManager = G4StateManager::GetStateManager();
492 ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Illegal Application State (" <<
493 stateManager->GetStateString(stateManager->GetCurrentState()) <<
")!");
495 case 300: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Range!"); }
break;
496 case 400: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Unreadable!"); }
break;
497 case 500: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Candidates!"); }
break;
498 case 600: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Alias Not Found!"); }
break;
499 default: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Unknown Status!"); }
break;