30 #include "G4ChargedGeantino.hh"
32 #include "G4Geantino.hh"
33 #include "G4LorentzVector.hh"
34 #include "G4ParallelWorldPhysics.hh"
35 #include "G4ParticleTable.hh"
36 #include "G4PrimaryParticle.hh"
37 #include "G4PrimaryVertex.hh"
38 #include "G4SDManager.hh"
39 #include "G4ScoringManager.hh"
40 #include "G4StateManager.hh"
42 #include "G4Trajectory.hh"
43 #include "G4TransportationManager.hh"
44 #include "G4UImanager.hh"
45 #include "G4VModularPhysicsList.hh"
46 #include "G4VUserPhysicsList.hh"
51 static std::once_flag initializeOnceFlag;
52 static std::once_flag finalizeOnceFlag;
56 const std::string&
name,
73 m_runTimer =
new G4Timer();
74 m_eventTimer =
new G4Timer();
79 if (m_recordFlux) G4ScoringManager::GetScoringManager();
83 std::call_once(initializeOnceFlag, &iGeant4::G4LegacyTransportTool::initializeOnce,
this);
86 ATH_MSG_ERROR(
"Failure in iGeant4::G4LegacyTransportTool::initializeOnce: " <<
e.what());
87 return StatusCode::FAILURE;
98 return StatusCode::SUCCESS;
107 if (m_g4RunManagerHelper.retrieve().isFailure()) {
108 throw std::runtime_error(
"Could not initialize G4RunManagerHelper!");
111 m_pRunMgr = m_g4RunManagerHelper ? m_g4RunManagerHelper->g4RunManager() :
nullptr;
113 throw std::runtime_error(
"G4RunManagerHelper::g4RunManager() returned nullptr.");
116 if(m_physListSvc.retrieve().isFailure()) {
117 throw std::runtime_error(
"Could not initialize ATLAS PhysicsListSvc!");
119 m_physListSvc->SetPhysicsList();
120 ATH_MSG_INFO(
"retireving the Detector Construction tool" );
121 if(m_detConstruction.retrieve().isFailure()) {
122 throw std::runtime_error(
"Could not initialize ATLAS DetectorConstruction!");
125 m_pRunMgr->SetRecordFlux( m_recordFlux, std::make_unique<ISFFluxRecorder>() );
126 m_pRunMgr->SetLogLevel(
int(
msg().
level()) );
127 m_pRunMgr->SetDetConstructionTool( m_detConstruction.get() );
128 m_pRunMgr->SetPhysListSvc(m_physListSvc.typeAndName() );
129 m_pRunMgr->SetQuietMode( m_quietMode );
130 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
131 std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
132 m_pRunMgr->SetUserInitialization(actionInitialization.release());
134 G4UImanager *ui = G4UImanager::GetUIpointer();
136 if (!m_libList.empty()) {
137 ATH_MSG_INFO(
"G4AtlasAlg specific libraries requested ") ;
138 std::string temp=
"/load "+m_libList;
139 ui->ApplyCommand(temp);
142 if (!m_physList.empty()) {
143 ATH_MSG_INFO(
"requesting a specific physics list "<< m_physList) ;
144 std::string temp=
"/Physics/GetPhysicsList "+m_physList;
145 ui->ApplyCommand(temp);
148 if (!m_fieldMap.empty()) {
149 ATH_MSG_INFO(
"requesting a specific field map "<< m_fieldMap) ;
150 ATH_MSG_INFO(
"the field is initialized straight away") ;
151 std::string temp=
"/MagneticField/Select "+m_fieldMap;
152 ui->ApplyCommand(temp);
153 ui->ApplyCommand(
"/MagneticField/Initialize");
157 ATH_MSG_DEBUG(
"G4 Command: Trying at the end of initializeOnce()");
158 for (
const auto& g4command : m_g4commands) {
159 int returnCode = ui->ApplyCommand( g4command );
160 commandLog(returnCode, g4command);
164 auto* rm = G4RunManager::GetRunManager();
166 throw std::runtime_error(
"Run manager retrieval has failed");
170 if(!m_useMT && rm->ConfirmBeamOnCondition()) {
171 rm->RunInitialization();
174 ATH_MSG_INFO(
"Initializing " << m_physicsInitializationTools.size() <<
" physics initialization tools");
175 for(
auto& physicsTool : m_physicsInitializationTools) {
176 if (physicsTool->initializePhysics().isFailure()) {
177 throw std::runtime_error(
"Failed to initialize physics with tool " + physicsTool.name());
181 if(m_userLimitsSvc.retrieve().isFailure()) {
182 throw std::runtime_error(
"Could not initialize ATLAS UserLimitsSvc!");
185 if (m_activateParallelGeometries) {
186 G4VModularPhysicsList* thePhysicsList=
dynamic_cast<G4VModularPhysicsList*
>(m_physListSvc->GetPhysicsList());
187 if (!thePhysicsList) {
188 throw std::runtime_error(
"Failed dynamic_cast!! this is not a G4VModularPhysicsList!");
190 #if G4VERSION_NUMBER >= 1010
191 std::vector<std::string>& parallelWorldNames=m_detConstruction->GetParallelWorldNames();
192 for (
auto&
it: parallelWorldNames) {
193 thePhysicsList->RegisterPhysics(
new G4ParallelWorldPhysics(
it,
true));
204 ATH_MSG_VERBOSE(
"++++++++++++ ISF G4 G4LegacyTransportTool finalized ++++++++++++");
211 ATH_MSG_ERROR(
"Failure in iGeant4::G4LegacyTransportTool::finalizeOnce: " <<
e.what());
212 return StatusCode::FAILURE;
217 float runTime=m_runTimer->GetUserElapsed()+m_runTimer->GetSystemElapsed();
218 float avgTimePerEvent=(m_nrOfEntries>1) ? m_accumulatedEventTime/(m_nrOfEntries-1.) : runTime;
219 float sigma=( m_nrOfEntries>2) ? std::sqrt((m_accumulatedEventTimeSq/
float(m_nrOfEntries-1)-
220 avgTimePerEvent*avgTimePerEvent)/
float(m_nrOfEntries-2)) : 0;
223 " End of run - time spent is "<<std::setprecision(4) <<
225 " Average time per event was "<<std::setprecision(4) <<
226 avgTimePerEvent <<
" +- "<< std::setprecision(4) <<
sigma<<
endmsg<<
228 "*****************************************");
231 return StatusCode::SUCCESS;
239 m_pRunMgr->RunTermination();
248 McEventCollection* mcEventCollection, std::shared_ptr<HitCollectionMap> hitCollections) {
257 StatusCode success = this->simulateVector(ctx, ispVector, secondaries,
258 mcEventCollection, hitCollections);
269 McEventCollection* mcEventCollection, std::shared_ptr<HitCollectionMap> hitCollections,
276 auto eventInfo = std::make_unique<AtlasG4EventUserInfo>();
277 eventInfo->SetHitCollectionMap(hitCollections);
279 auto inputEvent = std::make_unique<G4Event>(ctx.eventID().event_number());
280 inputEvent->SetUserInformation(eventInfo.release());
282 m_inputConverter->ISF_to_G4Event(*inputEvent,
particles,
283 genEvent(mcEventCollection));
286 return m_pRunMgr->ProcessEvent(inputEvent.release());
294 return StatusCode::FAILURE;
309 Slot& slot = *m_slots;
321 ATH_MSG_VERBOSE(
"Found secondaries: " << searchResult->second.size() );
322 secondaries.splice(
end(secondaries), std::move(searchResult->second) );
327 return StatusCode::SUCCESS;
338 rngWrapper->
setSeed( m_randomStreamName, ctx );
339 G4Random::setTheEngine(rngWrapper->
getEngine(ctx));
340 ATH_CHECK(m_senDetTool->BeginOfAthenaEvent(hitCollections));
343 if (m_doTiming) m_eventTimer->Start();
346 G4SDManager::GetSDMpointer()->PrepareNewEvent();
348 return StatusCode::SUCCESS;
374 m_eventTimer->Stop();
376 double eventTime=m_eventTimer->GetUserElapsed()+m_eventTimer->GetSystemElapsed();
377 if (m_nrOfEntries>1) {
378 m_accumulatedEventTime +=eventTime;
379 m_accumulatedEventTimeSq+=eventTime*eventTime;
382 float avgTimePerEvent=(m_nrOfEntries>1) ? m_accumulatedEventTime/(m_nrOfEntries-1.) : eventTime;
383 float sigma=(m_nrOfEntries>2) ? std::sqrt((m_accumulatedEventTimeSq/
float(m_nrOfEntries-1)-
384 avgTimePerEvent*avgTimePerEvent)/
float(m_nrOfEntries-2)) : 0.;
386 ATH_MSG_INFO(
"\t Run:Event "<<ctx.eventID().run_number()<<
":"<<ctx.eventID().event_number() <<
"\t ("<<m_nrOfEntries<<
"th event for this worker) took " << std::setprecision(4) <<
387 eventTime <<
" s. New average " << std::setprecision(4) <<
388 avgTimePerEvent<<
" +- "<<std::setprecision(4) <<
sigma);
391 ATH_CHECK(m_senDetTool->EndOfAthenaEvent(hitCollections));
392 ATH_CHECK(m_fastSimTool->EndOfAthenaEvent());
394 return StatusCode::SUCCESS;
401 if(!mcEventCollection) {
403 if (evtStore()->contains<McEventCollection>(m_mcEventCollectionName)) {
404 if (evtStore()->
retrieve( mcEventCollection, m_mcEventCollectionName).isFailure()) {
405 ATH_MSG_ERROR(
"Unable to retrieve McEventCollection with name=" << m_mcEventCollectionName
410 ATH_MSG_WARNING(
"Fallback. Sucessfully retrieved McEventCollection with name=" << m_mcEventCollectionName);
413 else {
return nullptr; }
416 return mcEventCollection->
back();
423 case 0: {
ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Command Succeeded"); }
break;
424 case 100: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Command Not Found!"); }
break;
426 auto* stateManager = G4StateManager::GetStateManager();
427 ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Illegal Application State (" <<
428 stateManager->GetStateString(stateManager->GetCurrentState()) <<
")!");
430 case 300: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Range!"); }
break;
431 case 400: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Unreadable!"); }
break;
432 case 500: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Candidates!"); }
break;
433 case 600: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Alias Not Found!"); }
break;
434 default: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Unknown Status!"); }
break;