Geant4-11
G4EventManager.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4EventManager class implementation
27//
28// Author: M.Asai, SLAC
29// --------------------------------------------------------------------
30
31#include "G4EventManager.hh"
32#include "G4ios.hh"
33#include "G4EvManMessenger.hh"
34#include "G4Event.hh"
36#include "G4VTrackingManager.hh"
37#include "G4UserEventAction.hh"
39#include "G4SDManager.hh"
40#include "G4StateManager.hh"
41#include "G4ApplicationState.hh"
43#include "G4Navigator.hh"
44#include "Randomize.hh"
45#include "G4Profiler.hh"
46#include "G4TiMemory.hh"
47
48#include <unordered_set>
49
51
53{
54 return fpEventManager;
55}
56
58{
59 if(fpEventManager != nullptr)
60 {
61 G4Exception("G4EventManager::G4EventManager", "Event0001", FatalException,
62 "G4EventManager::G4EventManager() has already been made.");
63 }
64 else
65 {
72 fpEventManager = this;
73 }
74}
75
77{
78 delete trackContainer;
79 delete transformer;
80 delete trackManager;
81 delete theMessenger;
82 delete userEventAction;
83 fpEventManager = nullptr;
84}
85
87{
88 abortRequested = false;
90 if(currentState != G4State_GeomClosed)
91 {
92 G4Exception("G4EventManager::ProcessOneEvent", "Event0002", JustWarning,
93 "IllegalState -- Geometry not closed: cannot process an event.");
94 return;
95 }
96 currentEvent = anEvent;
99 {
100 std::ostringstream oss;
102 randomNumberStatusToG4Event = oss.str();
104 }
105
106 // Resetting Navigator has been moved to G4EventManager,
107 // so that resetting is now done for every event.
108 G4ThreeVector center(0,0,0);
111 navigator->LocateGlobalPointAndSetup(center,0,false);
112
113 G4Track* track = nullptr;
114 G4TrackStatus istop = fAlive;
115
116#ifdef G4VERBOSE
117 if ( verboseLevel > 0 )
118 {
119 G4cout << "=====================================" << G4endl;
120 G4cout << " G4EventManager::ProcessOneEvent() " << G4endl;
121 G4cout << "=====================================" << G4endl;
122 }
123#endif
124
126
127#ifdef G4_STORE_TRAJECTORY
128 trajectoryContainer = nullptr;
129#endif
130
132 if(sdManager != nullptr)
134
136
137#if defined(GEANT4_USE_TIMEMORY)
139#endif
140
141#ifdef G4VERBOSE
142 if ( verboseLevel > 1 )
143 {
145 << " vertices passed from G4Event." << G4endl;
146 }
147#endif
148
149 if(!abortRequested)
150 {
152 }
153
154#ifdef G4VERBOSE
155 if ( verboseLevel > 0 )
156 {
157 G4cout << trackContainer->GetNTotalTrack() << " primaries "
158 << "are passed from G4EventTransformer." << G4endl;
159 G4cout << "!!!!!!! Now start processing an event !!!!!!!" << G4endl;
160 }
161#endif
162
163 std::unordered_set<G4VTrackingManager *> trackingManagersToFlush;
164
165 do
166 {
167 G4VTrajectory* previousTrajectory;
168 while( (track=trackContainer->PopNextTrack(&previousTrajectory)) != nullptr )
169 { // Loop checking 12.28.2015 M.Asai
170
171 const G4ParticleDefinition* partDef = track->GetParticleDefinition();
172 G4VTrackingManager* particleTrackingManager = partDef->GetTrackingManager();
173
174 if (particleTrackingManager != nullptr)
175 {
176#ifdef G4VERBOSE
177 if ( verboseLevel > 1 )
178 {
179 G4cout << "Track " << track << " (trackID " << track->GetTrackID()
180 << ", parentID " << track->GetParentID()
181 << ") is handed over to custom TrackingManager." << G4endl;
182 }
183#endif
184
185 particleTrackingManager->HandOverOneTrack(track);
186 // The particle's tracking manager may either track immediately or
187 // defer processing until FlushEvent is called. Thus, we must neither
188 // check the track's status nor stack secondaries.
189
190 // Remember this tracking manager to later call FlushEvent.
191 trackingManagersToFlush.insert(particleTrackingManager);
192
193 } else {
194#ifdef G4VERBOSE
195 if ( verboseLevel > 1 )
196 {
197 G4cout << "Track " << track << " (trackID " << track->GetTrackID()
198 << ", parentID " << track->GetParentID()
199 << ") is passed to G4TrackingManager." << G4endl;
200 }
201#endif
202
203 tracking = true;
205 istop = track->GetTrackStatus();
206 tracking = false;
207
208#ifdef G4VERBOSE
209 if ( verboseLevel > 0 )
210 {
211 G4cout << "Track (trackID " << track->GetTrackID()
212 << ", parentID " << track->GetParentID()
213 << ") is processed with stopping code " << istop << G4endl;
214 }
215#endif
216
217 G4VTrajectory* aTrajectory = nullptr;
218#ifdef G4_STORE_TRAJECTORY
219 aTrajectory = trackManager->GimmeTrajectory();
220
221 if(previousTrajectory != nullptr)
222 {
223 previousTrajectory->MergeTrajectory(aTrajectory);
224 delete aTrajectory;
225 aTrajectory = previousTrajectory;
226 }
227 if(aTrajectory&&(istop!=fStopButAlive)&&(istop!=fSuspend))
228 {
229 if(trajectoryContainer == nullptr)
230 {
233 }
234 trajectoryContainer->insert(aTrajectory);
235 }
236#endif
237
239 switch (istop)
240 {
241 case fStopButAlive:
242 case fSuspend:
243 trackContainer->PushOneTrack( track, aTrajectory );
244 StackTracks( secondaries );
245 break;
246
249 StackTracks( secondaries );
250 break;
251
252 case fStopAndKill:
253 StackTracks( secondaries );
254 delete track;
255 break;
256
257 case fAlive:
258 G4Exception("G4EventManager::DoProcessing", "Event004", JustWarning,
259 "Illegal trackstatus returned from G4TrackingManager."\
260 " Continue with simulation.");
261 break;
263 if( secondaries )
264 {
265 for(std::size_t i=0; i<secondaries->size(); ++i)
266 { delete (*secondaries)[i]; }
267 secondaries->clear();
268 }
269 delete track;
270 break;
271 }
272 }
273 }
274
275 // Flush all tracking managers, which may have deferred processing until now.
276 for (G4VTrackingManager *tm : trackingManagersToFlush)
277 {
278 tm->FlushEvent();
279 }
280 trackingManagersToFlush.clear();
281
282 // Check if flushing one of the tracking managers stacked new secondaries.
283 } while (trackContainer->GetNUrgentTrack() > 0);
284
285#ifdef G4VERBOSE
286 if ( verboseLevel > 0 )
287 {
288 G4cout << "NULL returned from G4StackManager." << G4endl;
289 G4cout << "Terminate current event processing." << G4endl;
290 }
291#endif
292
293 if(sdManager != nullptr)
294 {
296 }
297
298#if defined(GEANT4_USE_TIMEMORY)
299 eventProfiler.reset();
300#endif
301
303 {
305 }
306
308 currentEvent = nullptr;
309 abortRequested = false;
310}
311
313 G4bool IDhasAlreadySet)
314{
315 if( trackVector != nullptr )
316 {
317 if( trackVector->size() == 0 ) return;
318 for( auto newTrack : *trackVector )
319 {
321 if(!IDhasAlreadySet)
322 {
323 newTrack->SetTrackID( trackIDCounter );
324 if(newTrack->GetDynamicParticle()->GetPrimaryParticle())
325 {
327 = (G4PrimaryParticle*)(newTrack->GetDynamicParticle()->GetPrimaryParticle());
328 pp->SetTrackID(trackIDCounter);
329 }
330 }
331 newTrack->SetOriginTouchableHandle(newTrack->GetTouchableHandle());
332 trackContainer->PushOneTrack( newTrack );
333#ifdef G4VERBOSE
334 if ( verboseLevel > 1 )
335 {
336 G4cout << "A new track " << newTrack
337 << " (trackID " << newTrack->GetTrackID()
338 << ", parentID " << newTrack->GetParentID()
339 << ") is passed to G4StackManager." << G4endl;
340 }
341#endif
342 }
343 trackVector->clear();
344 }
345}
346
348{
349 userEventAction = userAction;
350 if(userEventAction != nullptr)
351 {
353 }
354}
355
357{
358 userStackingAction = userAction;
360}
361
363{
364 userTrackingAction = userAction;
365 trackManager->SetUserAction(userAction);
366}
367
369{
370 userSteppingAction = userAction;
371 trackManager->SetUserAction(userAction);
372}
373
375{
376 trackIDCounter = 0;
377 DoProcessing(anEvent);
378}
379
381 G4Event* anEvent)
382{
383 static G4ThreadLocal G4String* randStat = nullptr;
384 if (randStat == nullptr) randStat = new G4String;
385 trackIDCounter = 0;
386 G4bool tempEvent = false;
387 if(anEvent == nullptr)
388 {
389 anEvent = new G4Event();
390 tempEvent = true;
391 }
394 {
395 std::ostringstream oss;
397 (*randStat) = oss.str();
398 anEvent->SetRandomNumberStatus(*randStat);
399 }
400 StackTracks(trackVector,false);
401 DoProcessing(anEvent);
402 if(tempEvent) { delete anEvent; }
403}
404
406{
408 if(currentState != G4State_EventProc || currentEvent == 0)
409 {
410 G4Exception("G4EventManager::SetUserInformation",
411 "Event0003", JustWarning,
412 "G4VUserEventInformation cannot be set because of absence "\
413 "of G4Event.");
414 return;
415 }
416
418}
419
421{
423 if(currentState != G4State_EventProc || currentEvent == 0)
424 {
425 return nullptr;
426 }
427
429}
430
432{
433 if(currentEvent != nullptr) { currentEvent->KeepTheEvent(); }
434}
435
437{
438 abortRequested = true;
441}
G4ApplicationState
@ G4State_EventProc
@ G4State_GeomClosed
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
G4TrackStatus
@ fKillTrackAndSecondaries
@ fSuspend
@ fAlive
@ fStopAndKill
@ fStopButAlive
@ fPostponeToNextEvent
std::vector< G4Track * > G4TrackVector
bool G4bool
Definition: G4Types.hh:86
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:288
G4EvManMessenger * theMessenger
void SetUserAction(G4UserEventAction *userAction)
G4UserStackingAction * userStackingAction
void SetUserInformation(G4VUserEventInformation *anInfo)
std::unique_ptr< ProfilerConfig > eventProfiler
G4UserTrackingAction * userTrackingAction
void KeepTheCurrentEvent()
G4ProfilerConfig< G4ProfileType::Event > ProfilerConfig
G4int storetRandomNumberStatusToG4Event
void AbortCurrentEvent()
G4StateManager * stateManager
static G4ThreadLocal G4EventManager * fpEventManager
G4PrimaryTransformer * transformer
void DoProcessing(G4Event *anEvent)
static G4EventManager * GetEventManager()
G4StackManager * trackContainer
G4TrajectoryContainer * trajectoryContainer
G4SDManager * sdManager
G4Event * currentEvent
void StackTracks(G4TrackVector *trackVector, G4bool IDhasAlreadySet=false)
G4UserEventAction * userEventAction
void ProcessOneEvent(G4Event *anEvent)
G4TrackingManager * trackManager
G4UserSteppingAction * userSteppingAction
G4VUserEventInformation * GetUserInformation()
G4String randomNumberStatusToG4Event
G4int GetNumberOfPrimaryVertex() const
Definition: G4Event.hh:133
void SetRandomNumberStatus(G4String &st)
Definition: G4Event.hh:90
void KeepTheEvent(G4bool vl=true)
Definition: G4Event.hh:100
G4HCofThisEvent * GetHCofThisEvent() const
Definition: G4Event.hh:156
void SetRandomNumberStatusForProcessing(G4String &st)
Definition: G4Event.hh:95
void SetHCofThisEvent(G4HCofThisEvent *value)
Definition: G4Event.hh:82
void SetUserInformation(G4VUserEventInformation *anInfo)
Definition: G4Event.hh:171
G4VUserEventInformation * GetUserInformation() const
Definition: G4Event.hh:173
void SetTrajectoryContainer(G4TrajectoryContainer *value)
Definition: G4Event.hh:86
G4VTrackingManager * GetTrackingManager() const
G4TrackVector * GimmePrimaries(G4Event *anEvent, G4int trackIDCounter=0)
void TerminateCurrentEvent(G4HCofThisEvent *HCE)
Definition: G4SDManager.cc:120
G4HCofThisEvent * PrepareNewEvent()
Definition: G4SDManager.cc:113
static G4SDManager * GetSDMpointerIfExist()
Definition: G4SDManager.cc:47
G4int GetNTotalTrack() const
G4int GetNUrgentTrack() const
G4Track * PopNextTrack(G4VTrajectory **newTrajectory)
G4int PushOneTrack(G4Track *newTrack, G4VTrajectory *newTrajectory=nullptr)
G4int PrepareNewEvent()
void SetUserStackingAction(G4UserStackingAction *value)
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
G4bool SetNewState(const G4ApplicationState &requestedState)
G4TrackStatus GetTrackStatus() const
G4int GetTrackID() const
const G4ParticleDefinition * GetParticleDefinition() const
G4int GetParentID() const
void SetUserAction(G4UserTrackingAction *apAction)
G4TrackVector * GimmeSecondaries() const
void ProcessOneTrack(G4Track *apValueG4Track)
G4VTrajectory * GimmeTrajectory() const
G4bool insert(G4VTrajectory *p)
static G4TransportationManager * GetTransportationManager()
G4Navigator * GetNavigatorForTracking() const
virtual void SetEventManager(G4EventManager *value)
virtual void BeginOfEventAction(const G4Event *anEvent)
virtual void EndOfEventAction(const G4Event *anEvent)
virtual void HandOverOneTrack(G4Track *aTrack)=0
virtual void MergeTrajectory(G4VTrajectory *secondTrajectory)=0
#define G4ThreadLocal
Definition: tls.hh:77