31 #include "G4ChargedGeantino.hh"
33 #include "G4Geantino.hh"
34 #include "G4LorentzVector.hh"
35 #include "G4ParallelWorldPhysics.hh"
36 #include "G4ParticleTable.hh"
37 #include "G4PrimaryParticle.hh"
38 #include "G4PrimaryVertex.hh"
39 #include "G4SDManager.hh"
40 #include "G4ScoringManager.hh"
41 #include "G4StateManager.hh"
43 #include "G4Trajectory.hh"
44 #include "G4TransportationManager.hh"
45 #include "G4UImanager.hh"
46 #include "G4VModularPhysicsList.hh"
47 #include "G4VUserPhysicsList.hh"
52 static std::once_flag initializeOnceFlag;
53 static std::once_flag finalizeOnceFlag;
57 const std::string&
name,
74 m_runTimer =
new G4Timer();
75 m_eventTimer =
new G4Timer();
80 if (m_recordFlux) G4ScoringManager::GetScoringManager();
84 std::call_once(initializeOnceFlag, &iGeant4::G4LegacyTransportTool::initializeOnce,
this);
87 ATH_MSG_ERROR(
"Failure in iGeant4::G4LegacyTransportTool::initializeOnce: " <<
e.what());
88 return StatusCode::FAILURE;
99 return StatusCode::SUCCESS;
108 if (m_g4RunManagerHelper.retrieve().isFailure()) {
109 throw std::runtime_error(
"Could not initialize G4RunManagerHelper!");
112 m_pRunMgr = m_g4RunManagerHelper ? m_g4RunManagerHelper->g4RunManager() :
nullptr;
114 throw std::runtime_error(
"G4RunManagerHelper::g4RunManager() returned nullptr.");
117 if(m_physListSvc.retrieve().isFailure()) {
118 throw std::runtime_error(
"Could not initialize ATLAS PhysicsListSvc!");
120 m_physListSvc->SetPhysicsList();
121 ATH_MSG_INFO(
"retireving the Detector Construction tool" );
122 if(m_detConstruction.retrieve().isFailure()) {
123 throw std::runtime_error(
"Could not initialize ATLAS DetectorConstruction!");
126 m_pRunMgr->SetRecordFlux( m_recordFlux, std::make_unique<ISFFluxRecorder>() );
127 m_pRunMgr->SetLogLevel(
int(
msg().
level()) );
128 m_pRunMgr->SetDetConstructionTool( m_detConstruction.get() );
129 m_pRunMgr->SetPhysListSvc(m_physListSvc.typeAndName() );
130 m_pRunMgr->SetQuietMode( m_quietMode );
131 std::unique_ptr<G4AtlasActionInitialization> actionInitialization =
132 std::make_unique<G4AtlasActionInitialization>(&*m_userActionSvc);
133 m_pRunMgr->SetUserInitialization(actionInitialization.release());
136 G4UImanager *ui = G4UImanager::GetUIpointer();
138 if (!m_libList.empty()) {
139 ATH_MSG_INFO(
"G4AtlasAlg specific libraries requested ") ;
140 std::string temp=
"/load "+m_libList;
141 ui->ApplyCommand(temp);
144 if (!m_physList.empty()) {
145 ATH_MSG_INFO(
"requesting a specific physics list "<< m_physList) ;
146 std::string temp=
"/Physics/GetPhysicsList "+m_physList;
147 ui->ApplyCommand(temp);
150 if (!m_fieldMap.empty()) {
151 ATH_MSG_INFO(
"requesting a specific field map "<< m_fieldMap) ;
152 ATH_MSG_INFO(
"the field is initialized straight away") ;
153 std::string temp=
"/MagneticField/Select "+m_fieldMap;
154 ui->ApplyCommand(temp);
155 ui->ApplyCommand(
"/MagneticField/Initialize");
159 ATH_MSG_DEBUG(
"G4 Command: Trying at the end of initializeOnce()");
160 for (
const auto& g4command : m_g4commands) {
161 int returnCode = ui->ApplyCommand( g4command );
162 commandLog(returnCode, g4command);
166 auto* rm = G4RunManager::GetRunManager();
168 throw std::runtime_error(
"Run manager retrieval has failed");
172 if(!m_useMT && rm->ConfirmBeamOnCondition()) {
173 rm->RunInitialization();
176 ATH_MSG_INFO(
"Initializing " << m_physicsInitializationTools.size() <<
" physics initialization tools");
177 for(
auto& physicsTool : m_physicsInitializationTools) {
178 if (physicsTool->initializePhysics().isFailure()) {
179 throw std::runtime_error(
"Failed to initialize physics with tool " + physicsTool.name());
183 if(m_userLimitsSvc.retrieve().isFailure()) {
184 throw std::runtime_error(
"Could not initialize ATLAS UserLimitsSvc!");
187 if (m_activateParallelGeometries) {
188 G4VModularPhysicsList* thePhysicsList=
dynamic_cast<G4VModularPhysicsList*
>(m_physListSvc->GetPhysicsList());
189 if (!thePhysicsList) {
190 throw std::runtime_error(
"Failed dynamic_cast!! this is not a G4VModularPhysicsList!");
192 #if G4VERSION_NUMBER >= 1010
193 std::vector<std::string>& parallelWorldNames=m_detConstruction->GetParallelWorldNames();
194 for (
auto&
it: parallelWorldNames) {
195 thePhysicsList->RegisterPhysics(
new G4ParallelWorldPhysics(
it,
true));
206 ATH_MSG_VERBOSE(
"++++++++++++ ISF G4 G4LegacyTransportTool finalized ++++++++++++");
213 ATH_MSG_ERROR(
"Failure in iGeant4::G4LegacyTransportTool::finalizeOnce: " <<
e.what());
214 return StatusCode::FAILURE;
219 float runTime=m_runTimer->GetUserElapsed()+m_runTimer->GetSystemElapsed();
220 float avgTimePerEvent=(m_nrOfEntries>1) ? m_accumulatedEventTime/(m_nrOfEntries-1.) : runTime;
221 float sigma=( m_nrOfEntries>2) ? std::sqrt((m_accumulatedEventTimeSq/
float(m_nrOfEntries-1)-
222 avgTimePerEvent*avgTimePerEvent)/
float(m_nrOfEntries-2)) : 0;
225 " End of run - time spent is "<<std::setprecision(4) <<
227 " Average time per event was "<<std::setprecision(4) <<
228 avgTimePerEvent <<
" +- "<< std::setprecision(4) <<
sigma<<
endmsg<<
230 "*****************************************");
233 return StatusCode::SUCCESS;
241 m_pRunMgr->RunTermination();
250 McEventCollection* mcEventCollection, std::shared_ptr<HitCollectionMap> hitCollections) {
259 StatusCode success = this->simulateVector(ctx, ispVector, secondaries,
260 mcEventCollection, hitCollections);
271 McEventCollection* mcEventCollection, std::shared_ptr<HitCollectionMap> hitCollections,
278 auto eventInfo = std::make_unique<AtlasG4EventUserInfo>();
279 eventInfo->SetHitCollectionMap(hitCollections);
281 auto inputEvent = std::make_unique<G4Event>(ctx.eventID().event_number());
282 inputEvent->SetUserInformation(eventInfo.release());
284 m_inputConverter->ISF_to_G4Event(*inputEvent,
particles,
285 genEvent(mcEventCollection));
288 return m_pRunMgr->ProcessEvent(inputEvent.release());
296 return StatusCode::FAILURE;
311 Slot& slot = *m_slots;
323 ATH_MSG_VERBOSE(
"Found secondaries: " << searchResult->second.size() );
324 secondaries.splice(
end(secondaries), std::move(searchResult->second) );
329 return StatusCode::SUCCESS;
340 rngWrapper->
setSeed( m_randomStreamName, ctx );
341 G4Random::setTheEngine(rngWrapper->
getEngine(ctx));
342 ATH_CHECK(m_senDetTool->BeginOfAthenaEvent(hitCollections));
345 if (m_doTiming) m_eventTimer->Start();
348 G4SDManager::GetSDMpointer()->PrepareNewEvent();
350 return StatusCode::SUCCESS;
376 m_eventTimer->Stop();
378 double eventTime=m_eventTimer->GetUserElapsed()+m_eventTimer->GetSystemElapsed();
379 if (m_nrOfEntries>1) {
380 m_accumulatedEventTime +=eventTime;
381 m_accumulatedEventTimeSq+=eventTime*eventTime;
384 float avgTimePerEvent=(m_nrOfEntries>1) ? m_accumulatedEventTime/(m_nrOfEntries-1.) : eventTime;
385 float sigma=(m_nrOfEntries>2) ? std::sqrt((m_accumulatedEventTimeSq/
float(m_nrOfEntries-1)-
386 avgTimePerEvent*avgTimePerEvent)/
float(m_nrOfEntries-2)) : 0.;
388 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) <<
389 eventTime <<
" s. New average " << std::setprecision(4) <<
390 avgTimePerEvent<<
" +- "<<std::setprecision(4) <<
sigma);
393 ATH_CHECK(m_senDetTool->EndOfAthenaEvent(hitCollections));
394 ATH_CHECK(m_fastSimTool->EndOfAthenaEvent());
396 return StatusCode::SUCCESS;
403 if(!mcEventCollection) {
405 if (evtStore()->contains<McEventCollection>(m_mcEventCollectionName)) {
406 if (evtStore()->
retrieve( mcEventCollection, m_mcEventCollectionName).isFailure()) {
407 ATH_MSG_ERROR(
"Unable to retrieve McEventCollection with name=" << m_mcEventCollectionName
412 ATH_MSG_WARNING(
"Fallback. Sucessfully retrieved McEventCollection with name=" << m_mcEventCollectionName);
415 else {
return nullptr; }
418 return mcEventCollection->
back();
425 case 0: {
ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Command Succeeded"); }
break;
426 case 100: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Command Not Found!"); }
break;
428 auto* stateManager = G4StateManager::GetStateManager();
429 ATH_MSG_DEBUG(
"G4 Command: " << commandString <<
" - Illegal Application State (" <<
430 stateManager->GetStateString(stateManager->GetCurrentState()) <<
")!");
432 case 300: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Range!"); }
break;
433 case 400: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Unreadable!"); }
break;
434 case 500: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Parameter Out of Candidates!"); }
break;
435 case 600: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Alias Not Found!"); }
break;
436 default: {
ATH_MSG_ERROR(
"G4 Command: " << commandString <<
" - Unknown Status!"); }
break;