53#include <unordered_set>
59 return fpEventManager;
64 if(fpEventManager !=
nullptr)
67 "G4EventManager::G4EventManager() has already been made.");
77 fpEventManager =
this;
83 delete trackContainer;
87 delete userEventAction;
88 fpEventManager =
nullptr;
91void G4EventManager::DoProcessing(
G4Event* anEvent,
94 abortRequested =
false;
99 "IllegalState -- Geometry not closed: cannot process an event.");
102 currentEvent = anEvent;
104 if(storetRandomNumberStatusToG4Event > 1)
106 std::ostringstream oss;
108 randomNumberStatusToG4Event = oss.str();
109 currentEvent->SetRandomNumberStatusForProcessing(randomNumberStatusToG4Event);
119 G4Track* track =
nullptr;
123 if ( verboseLevel > 0 )
125 G4cout <<
"=====================================" <<
G4endl;
126 G4cout <<
" G4EventManager::ProcessOneEvent() " <<
G4endl;
127 if(trackVector!=
nullptr)
G4cout <<
" for a sub-event" <<
G4endl;
128 G4cout <<
"=====================================" <<
G4endl;
132 trackContainer->PrepareNewEvent(currentEvent);
134#ifdef G4_STORE_TRAJECTORY
135 trajectoryContainer =
nullptr;
144 if(sdManager !=
nullptr)
145 { currentEvent->SetHCofThisEvent(sdManager->PrepareNewEvent()); }
147 if(!subEventParaWorker && userEventAction !=
nullptr) userEventAction->BeginOfEventAction(currentEvent);
150 if ( verboseLevel > 1 )
152 G4cout << currentEvent->GetNumberOfPrimaryVertex()
153 <<
" vertices passed from G4Event." <<
G4endl;
157 if(trackVector!=
nullptr)
163 StackTracks(transformer->GimmePrimaries(currentEvent,trackIDCounter),
true);
167 if ( verboseLevel > 0 )
169 G4cout << trackContainer->GetNTotalTrack() <<
" primary tracks "
170 <<
"are passed to the stack." <<
G4endl;
171 G4cout <<
"!!!!!!! Now start processing an event !!!!!!!" <<
G4endl;
175 std::unordered_set<G4VTrackingManager *> trackingManagersToFlush;
179 G4VTrajectory* previousTrajectory;
180 while( (track=trackContainer->PopNextTrack(&previousTrajectory)) !=
nullptr )
186 if (particleTrackingManager !=
nullptr)
189 if ( verboseLevel > 1 )
193 <<
") is handed over to custom TrackingManager." <<
G4endl;
203 trackingManagersToFlush.insert(particleTrackingManager);
207 if ( verboseLevel > 1 )
211 <<
") is passed to G4TrackingManager." <<
G4endl;
216 trackManager->ProcessOneTrack( track );
221 if ( verboseLevel > 0 )
225 <<
") is processed with stopping code " << istop <<
G4endl;
229 G4VTrajectory* aTrajectory =
nullptr;
230#ifdef G4_STORE_TRAJECTORY
231 aTrajectory = trackManager->GimmeTrajectory();
233 if(previousTrajectory !=
nullptr)
237 aTrajectory = previousTrajectory;
242 if(trajectoryContainer ==
nullptr)
244 trajectoryContainer =
new G4TrajectoryContainer;
245 currentEvent->SetTrajectoryContainer(trajectoryContainer);
247 trajectoryContainer->insert(aTrajectory);
251 G4TrackVector* secondaries = trackManager->GimmeSecondaries();
257 trackContainer->PushOneTrack( track, aTrajectory );
262 trackContainer->PushOneTrack( track );
273 "Illegal track status returned from G4TrackingManager."\
274 " Continue with simulation.");
278 if( secondaries !=
nullptr )
280 for(
auto & secondarie : *secondaries)
281 {
delete secondarie; }
282 secondaries->clear();
291 for (G4VTrackingManager *tm : trackingManagersToFlush)
295 trackingManagersToFlush.clear();
302 }
while (trackContainer->GetNUrgentTrack() > 0);
305 if ( verboseLevel > 0 )
307 G4cout <<
"NULL returned from G4StackManager." <<
G4endl;
308 G4cout <<
"Terminate current event processing." <<
G4endl;
312 if(sdManager !=
nullptr)
314 sdManager->TerminateCurrentEvent(currentEvent->GetHCofThisEvent());
321 if(!subEventPara && (userEventAction !=
nullptr))
323 userEventAction->EndOfEventAction(currentEvent);
327 auto nses = trackContainer->GetNSubEventTypes();
331 if ( verboseLevel > 2 )
333 G4cout<<
"## End of processing an event --- "
334 <<nses<<
" sub-event types registered."<<
G4endl;
337 for(std::size_t i=0;i<nses;i++)
339 auto ty = trackContainer->GetSubEventType(i);
340 trackContainer->ReleaseSubEvent(ty);
345 currentEvent =
nullptr;
346 abortRequested =
false;
352 if(currentEvent==
nullptr)
return nullptr;
353 return currentEvent->PopSubEvent(ty);
361 if(!subEventParaWorker && userEventAction!=
nullptr) userEventAction->MergeSubEvent(ev,evt);
366 ev->TerminateSubEvent(
const_cast<G4SubEvent*
>(se));
368 if ( verboseLevel > 1 )
370 G4cout <<
"A sub-event of type " << seType
371 <<
" is merged to the event " << ev->GetEventID() <<
G4endl;
372 if(ev->GetNumberOfRemainingSubEvents()>0)
374 G4cout <<
" ---- This event still has " << ev->GetNumberOfRemainingSubEvents()
375 <<
" sub-events to be processed." <<
G4endl;
378 {
G4cout <<
" ---- This event has no more sub-event remaining." <<
G4endl; }
386 if(evt != currentEvent) {
388 "StoreSubEvent is invoked with a G4Event that is not the current event. PANIC!");
396 if( trackVector !=
nullptr )
398 if( trackVector->empty() )
return;
399 for(
auto newTrack : *trackVector )
404 newTrack->SetTrackID( trackIDCounter );
405 if(newTrack->GetDynamicParticle()->GetPrimaryParticle() !=
nullptr)
409 pp->SetTrackID(trackIDCounter);
412 newTrack->SetOriginTouchableHandle(newTrack->GetTouchableHandle());
413 trackContainer->PushOneTrack( newTrack );
415 if ( verboseLevel > 1 )
417 G4cout <<
"A new track " << newTrack
418 <<
" (trackID " << newTrack->GetTrackID()
419 <<
", parentID " << newTrack->GetParentID()
420 <<
") is passed to G4StackManager." <<
G4endl;
424 trackVector->clear();
430 userEventAction = userAction;
431 if(userEventAction !=
nullptr)
433 userEventAction->SetEventManager(
this);
439 userStackingAction = userAction;
440 trackContainer->SetUserStackingAction(userAction);
445 userTrackingAction = userAction;
446 trackManager->SetUserAction(userAction);
451 userSteppingAction = userAction;
452 trackManager->SetUserAction(userAction);
458 DoProcessing(anEvent);
465 if (randStat ==
nullptr) randStat =
new G4String;
468 if(anEvent ==
nullptr)
479 if(newEvent) trackIDCounter = 0;
481 if (storetRandomNumberStatusToG4Event==1
482 || storetRandomNumberStatusToG4Event==3)
484 std::ostringstream oss;
486 (*randStat) = oss.str();
489 DoProcessing(anEvent,trackVector,
false);
490 if(tempEvent) {
delete anEvent; }
500 "G4VUserEventInformation cannot be set because of absence "\
505 currentEvent->SetUserInformation(anInfo);
516 return currentEvent->GetUserInformation();
521 if(currentEvent !=
nullptr) { currentEvent->KeepTheEvent(); }
526 abortRequested =
true;
527 trackContainer->clear();
528 if(tracking) trackManager->EventAborted();
G4TemplateAutoLock< G4Mutex > G4AutoLock
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
#define G4MUTEX_INITIALIZER
CLHEP::Hep3Vector G4ThreeVector
@ fKillTrackAndSecondaries
std::vector< G4Track * > G4TrackVector
G4GLOB_DLL std::ostream G4cout
static std::ostream & saveFullState(std::ostream &os)
void SetUserAction(G4UserEventAction *userAction)
void SetUserInformation(G4VUserEventInformation *anInfo)
void KeepTheCurrentEvent()
G4int StoreSubEvent(G4Event *, G4int &, G4SubEvent *)
static G4EventManager * GetEventManager()
void StackTracks(G4TrackVector *trackVector, G4bool IDhasAlreadySet=false)
G4SubEvent * PopSubEvent(G4int ty)
void ProcessOneEvent(G4Event *anEvent)
G4VUserEventInformation * GetUserInformation()
void TerminateSubEvent(const G4SubEvent *se, const G4Event *evt)
G4int StoreSubEvent(G4int, G4SubEvent *)
void SetRandomNumberStatus(G4String &st)
void MergeSubEventResults(const G4Event *se)
static G4GlobalFastSimulationManager * GetGlobalFastSimulationManager()
virtual G4VPhysicalVolume * LocateGlobalPointAndSetup(const G4ThreeVector &point, const G4ThreeVector *direction=nullptr, const G4bool pRelativeSearch=true, const G4bool ignoreDirection=true)
G4VTrackingManager * GetTrackingManager() const
static G4SDManager * GetSDMpointerIfExist()
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
G4int GetSubEventType() const
G4Event * GetEvent() const
G4TrackStatus GetTrackStatus() const
const G4ParticleDefinition * GetParticleDefinition() const
G4int GetParentID() const
static G4TransportationManager * GetTransportationManager()
G4Navigator * GetNavigatorForTracking() const
virtual void HandOverOneTrack(G4Track *aTrack)=0
virtual void MergeTrajectory(G4VTrajectory *secondTrajectory)=0