Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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"
48
49#include <unordered_set>
50
51G4ThreadLocal G4EventManager* G4EventManager::fpEventManager = nullptr;
52
54{
55 return fpEventManager;
56}
57
59{
60 if(fpEventManager != nullptr)
61 {
62 G4Exception("G4EventManager::G4EventManager", "Event0001", FatalException,
63 "G4EventManager::G4EventManager() has already been made.");
64 }
65 else
66 {
67 trackManager = new G4TrackingManager;
68 transformer = new G4PrimaryTransformer;
69 trackContainer = new G4StackManager;
70 theMessenger = new G4EvManMessenger(this);
72 stateManager = G4StateManager::GetStateManager();
73 fpEventManager = this;
74 }
75}
76
78{
79 delete trackContainer;
80 delete transformer;
81 delete trackManager;
82 delete theMessenger;
83 delete userEventAction;
84 fpEventManager = nullptr;
85}
86
87void G4EventManager::DoProcessing(G4Event* anEvent)
88{
89 abortRequested = false;
90 G4ApplicationState currentState = stateManager->GetCurrentState();
91 if(currentState != G4State_GeomClosed)
92 {
93 G4Exception("G4EventManager::ProcessOneEvent", "Event0002", JustWarning,
94 "IllegalState -- Geometry not closed: cannot process an event.");
95 return;
96 }
97 currentEvent = anEvent;
98 stateManager->SetNewState(G4State_EventProc);
99 if(storetRandomNumberStatusToG4Event > 1)
100 {
101 std::ostringstream oss;
103 randomNumberStatusToG4Event = oss.str();
104 currentEvent->SetRandomNumberStatusForProcessing(randomNumberStatusToG4Event);
105 }
106
107 // Resetting Navigator has been moved to G4EventManager,
108 // so that resetting is now done for every event.
109 G4ThreeVector center(0,0,0);
112 navigator->LocateGlobalPointAndSetup(center,nullptr,false);
113
114 G4Track* track = nullptr;
115 G4TrackStatus istop = fAlive;
116
117#ifdef G4VERBOSE
118 if ( verboseLevel > 0 )
119 {
120 G4cout << "=====================================" << G4endl;
121 G4cout << " G4EventManager::ProcessOneEvent() " << G4endl;
122 G4cout << "=====================================" << G4endl;
123 }
124#endif
125
126 trackContainer->PrepareNewEvent();
127
128#ifdef G4_STORE_TRAJECTORY
129 trajectoryContainer = nullptr;
130#endif
131
133 if(sdManager != nullptr)
134 { currentEvent->SetHCofThisEvent(sdManager->PrepareNewEvent()); }
135
136 if(userEventAction != nullptr) userEventAction->BeginOfEventAction(currentEvent);
137
138#if defined(GEANT4_USE_TIMEMORY)
139 eventProfiler.reset(new ProfilerConfig(currentEvent));
140#endif
141
142#ifdef G4VERBOSE
143 if ( verboseLevel > 1 )
144 {
145 G4cout << currentEvent->GetNumberOfPrimaryVertex()
146 << " vertices passed from G4Event." << G4endl;
147 }
148#endif
149
150 if(!abortRequested)
151 {
152 StackTracks(transformer->GimmePrimaries(currentEvent,trackIDCounter), true);
153 }
154
155#ifdef G4VERBOSE
156 if ( verboseLevel > 0 )
157 {
158 G4cout << trackContainer->GetNTotalTrack() << " primaries "
159 << "are passed from G4EventTransformer." << G4endl;
160 G4cout << "!!!!!!! Now start processing an event !!!!!!!" << G4endl;
161 }
162#endif
163
164 std::unordered_set<G4VTrackingManager *> trackingManagersToFlush;
165
166 do
167 {
168 G4VTrajectory* previousTrajectory;
169 while( (track=trackContainer->PopNextTrack(&previousTrajectory)) != nullptr )
170 { // Loop checking 12.28.2015 M.Asai
171
172 const G4ParticleDefinition* partDef = track->GetParticleDefinition();
173 G4VTrackingManager* particleTrackingManager = partDef->GetTrackingManager();
174
175 if (particleTrackingManager != nullptr)
176 {
177#ifdef G4VERBOSE
178 if ( verboseLevel > 1 )
179 {
180 G4cout << "Track " << track << " (trackID " << track->GetTrackID()
181 << ", parentID " << track->GetParentID()
182 << ") is handed over to custom TrackingManager." << G4endl;
183 }
184#endif
185
186 particleTrackingManager->HandOverOneTrack(track);
187 // The particle's tracking manager may either track immediately or
188 // defer processing until FlushEvent is called. Thus, we must neither
189 // check the track's status nor stack secondaries.
190
191 // Remember this tracking manager to later call FlushEvent.
192 trackingManagersToFlush.insert(particleTrackingManager);
193
194 } else {
195#ifdef G4VERBOSE
196 if ( verboseLevel > 1 )
197 {
198 G4cout << "Track " << track << " (trackID " << track->GetTrackID()
199 << ", parentID " << track->GetParentID()
200 << ") is passed to G4TrackingManager." << G4endl;
201 }
202#endif
203
204 tracking = true;
205 trackManager->ProcessOneTrack( track );
206 istop = track->GetTrackStatus();
207 tracking = false;
208
209#ifdef G4VERBOSE
210 if ( verboseLevel > 0 )
211 {
212 G4cout << "Track (trackID " << track->GetTrackID()
213 << ", parentID " << track->GetParentID()
214 << ") is processed with stopping code " << istop << G4endl;
215 }
216#endif
217
218 G4VTrajectory* aTrajectory = nullptr;
219#ifdef G4_STORE_TRAJECTORY
220 aTrajectory = trackManager->GimmeTrajectory();
221
222 if(previousTrajectory != nullptr)
223 {
224 previousTrajectory->MergeTrajectory(aTrajectory);
225 delete aTrajectory;
226 aTrajectory = previousTrajectory;
227 }
228 if((aTrajectory != nullptr)&&(istop!=fStopButAlive)&&(istop!=fSuspend))
229 {
230 if(trajectoryContainer == nullptr)
231 {
232 trajectoryContainer = new G4TrajectoryContainer;
233 currentEvent->SetTrajectoryContainer(trajectoryContainer);
234 }
235 trajectoryContainer->insert(aTrajectory);
236 }
237#endif
238
239 G4TrackVector* secondaries = trackManager->GimmeSecondaries();
240 switch (istop)
241 {
242 case fStopButAlive:
243 case fSuspend:
244 trackContainer->PushOneTrack( track, aTrajectory );
245 StackTracks( secondaries );
246 break;
247
249 trackContainer->PushOneTrack( track );
250 StackTracks( secondaries );
251 break;
252
253 case fStopAndKill:
254 StackTracks( secondaries );
255 delete track;
256 break;
257
258 case fAlive:
259 G4Exception("G4EventManager::DoProcessing", "Event004", JustWarning,
260 "Illegal trackstatus returned from G4TrackingManager."\
261 " Continue with simulation.");
262 break;
264 if( secondaries != nullptr )
265 {
266 for(auto & secondarie : *secondaries)
267 { delete secondarie; }
268 secondaries->clear();
269 }
270 delete track;
271 break;
272 }
273 }
274 }
275
276 // Flush all tracking managers, which may have deferred processing until now.
277 for (G4VTrackingManager *tm : trackingManagersToFlush)
278 {
279 tm->FlushEvent();
280 }
281 trackingManagersToFlush.clear();
282
283 // flush any fast simulation models
285
286 // Check if flushing one of the tracking managers or a fast simulation model
287 // stacked new secondaries.
288 } while (trackContainer->GetNUrgentTrack() > 0);
289
290#ifdef G4VERBOSE
291 if ( verboseLevel > 0 )
292 {
293 G4cout << "NULL returned from G4StackManager." << G4endl;
294 G4cout << "Terminate current event processing." << G4endl;
295 }
296#endif
297
298 if(sdManager != nullptr)
299 {
300 sdManager->TerminateCurrentEvent(currentEvent->GetHCofThisEvent());
301 }
302
303#if defined(GEANT4_USE_TIMEMORY)
304 eventProfiler.reset();
305#endif
306
307 if(userEventAction != nullptr)
308 {
309 userEventAction->EndOfEventAction(currentEvent);
310 }
311
312 stateManager->SetNewState(G4State_GeomClosed);
313 currentEvent = nullptr;
314 abortRequested = false;
315}
316
318 G4bool IDhasAlreadySet)
319{
320 if( trackVector != nullptr )
321 {
322 if( trackVector->empty() ) return;
323 for( auto newTrack : *trackVector )
324 {
325 ++trackIDCounter;
326 if(!IDhasAlreadySet)
327 {
328 newTrack->SetTrackID( trackIDCounter );
329 if(newTrack->GetDynamicParticle()->GetPrimaryParticle() != nullptr)
330 {
331 auto* pp
332 = (G4PrimaryParticle*)(newTrack->GetDynamicParticle()->GetPrimaryParticle());
333 pp->SetTrackID(trackIDCounter);
334 }
335 }
336 newTrack->SetOriginTouchableHandle(newTrack->GetTouchableHandle());
337 trackContainer->PushOneTrack( newTrack );
338#ifdef G4VERBOSE
339 if ( verboseLevel > 1 )
340 {
341 G4cout << "A new track " << newTrack
342 << " (trackID " << newTrack->GetTrackID()
343 << ", parentID " << newTrack->GetParentID()
344 << ") is passed to G4StackManager." << G4endl;
345 }
346#endif
347 }
348 trackVector->clear();
349 }
350}
351
353{
354 userEventAction = userAction;
355 if(userEventAction != nullptr)
356 {
357 userEventAction->SetEventManager(this);
358 }
359}
360
362{
363 userStackingAction = userAction;
364 trackContainer->SetUserStackingAction(userAction);
365}
366
368{
369 userTrackingAction = userAction;
370 trackManager->SetUserAction(userAction);
371}
372
374{
375 userSteppingAction = userAction;
376 trackManager->SetUserAction(userAction);
377}
378
380{
381 trackIDCounter = 0;
382 DoProcessing(anEvent);
383}
384
386 G4Event* anEvent)
387{
388 static G4ThreadLocal G4String* randStat = nullptr;
389 if (randStat == nullptr) randStat = new G4String;
390 trackIDCounter = 0;
391 G4bool tempEvent = false;
392 if(anEvent == nullptr)
393 {
394 anEvent = new G4Event();
395 tempEvent = true;
396 }
397 if (storetRandomNumberStatusToG4Event==1
398 || storetRandomNumberStatusToG4Event==3)
399 {
400 std::ostringstream oss;
402 (*randStat) = oss.str();
403 anEvent->SetRandomNumberStatus(*randStat);
404 }
405 StackTracks(trackVector,false);
406 DoProcessing(anEvent);
407 if(tempEvent) { delete anEvent; }
408}
409
411{
412 G4ApplicationState currentState = stateManager->GetCurrentState();
413 if(currentState != G4State_EventProc || currentEvent == nullptr)
414 {
415 G4Exception("G4EventManager::SetUserInformation",
416 "Event0003", JustWarning,
417 "G4VUserEventInformation cannot be set because of absence "\
418 "of G4Event.");
419 return;
420 }
421
422 currentEvent->SetUserInformation(anInfo);
423}
424
426{
427 G4ApplicationState currentState = stateManager->GetCurrentState();
428 if(currentState != G4State_EventProc || currentEvent == nullptr)
429 {
430 return nullptr;
431 }
432
433 return currentEvent->GetUserInformation();
434}
435
437{
438 if(currentEvent != nullptr) { currentEvent->KeepTheEvent(); }
439}
440
442{
443 abortRequested = true;
444 trackContainer->clear();
445 if(tracking) trackManager->EventAborted();
446}
G4ApplicationState
@ G4State_EventProc
@ G4State_GeomClosed
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
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
void SetUserAction(G4UserEventAction *userAction)
void SetUserInformation(G4VUserEventInformation *anInfo)
void KeepTheCurrentEvent()
G4ProfilerConfig< G4ProfileType::Event > ProfilerConfig
void AbortCurrentEvent()
static G4EventManager * GetEventManager()
void StackTracks(G4TrackVector *trackVector, G4bool IDhasAlreadySet=false)
void ProcessOneEvent(G4Event *anEvent)
G4VUserEventInformation * GetUserInformation()
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
static G4GlobalFastSimulationManager * GetGlobalFastSimulationManager()
virtual G4VPhysicalVolume * LocateGlobalPointAndSetup(const G4ThreeVector &point, const G4ThreeVector *direction=nullptr, const G4bool pRelativeSearch=true, const G4bool ignoreDirection=true)
Definition: G4Navigator.cc:132
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