Geant4 10.7.0
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"
35#include "G4UserEventAction.hh"
37#include "G4SDManager.hh"
38#include "G4StateManager.hh"
39#include "G4ApplicationState.hh"
41#include "G4Navigator.hh"
42#include "Randomize.hh"
43#include "G4Profiler.hh"
44#include "G4TiMemory.hh"
45
46G4ThreadLocal G4EventManager* G4EventManager::fpEventManager = nullptr;
47
49{
50 return fpEventManager;
51}
52
54{
55 if(fpEventManager != nullptr)
56 {
57 G4Exception("G4EventManager::G4EventManager", "Event0001", FatalException,
58 "G4EventManager::G4EventManager() has already been made.");
59 }
60 else
61 {
62 trackManager = new G4TrackingManager;
63 transformer = new G4PrimaryTransformer;
64 trackContainer = new G4StackManager;
65 theMessenger = new G4EvManMessenger(this);
67 stateManager = G4StateManager::GetStateManager();
68 fpEventManager = this;
69 }
70}
71
73{
74 delete trackContainer;
75 delete transformer;
76 delete trackManager;
77 delete theMessenger;
78 delete userEventAction;
79 fpEventManager = nullptr;
80}
81
82void G4EventManager::DoProcessing(G4Event* anEvent)
83{
84 abortRequested = false;
85 G4ApplicationState currentState = stateManager->GetCurrentState();
86 if(currentState != G4State_GeomClosed)
87 {
88 G4Exception("G4EventManager::ProcessOneEvent", "Event0002", JustWarning,
89 "IllegalState -- Geometry not closed: cannot process an event.");
90 return;
91 }
92 currentEvent = anEvent;
93 stateManager->SetNewState(G4State_EventProc);
94 if(storetRandomNumberStatusToG4Event > 1)
95 {
96 std::ostringstream oss;
98 randomNumberStatusToG4Event = oss.str();
99 currentEvent->SetRandomNumberStatusForProcessing(randomNumberStatusToG4Event);
100 }
101
102 // Resetting Navigator has been moved to G4EventManager,
103 // so that resetting is now done for every event.
104 G4ThreeVector center(0,0,0);
107 navigator->LocateGlobalPointAndSetup(center,0,false);
108
109 G4Track* track = nullptr;
110 G4TrackStatus istop = fAlive;
111
112#ifdef G4VERBOSE
113 if ( verboseLevel > 0 )
114 {
115 G4cout << "=====================================" << G4endl;
116 G4cout << " G4EventManager::ProcessOneEvent() " << G4endl;
117 G4cout << "=====================================" << G4endl;
118 }
119#endif
120
121 trackContainer->PrepareNewEvent();
122
123#ifdef G4_STORE_TRAJECTORY
124 trajectoryContainer = nullptr;
125#endif
126
128 if(sdManager != nullptr)
129 { currentEvent->SetHCofThisEvent(sdManager->PrepareNewEvent()); }
130
131 if(userEventAction) userEventAction->BeginOfEventAction(currentEvent);
132
133#if defined(GEANT4_USE_TIMEMORY)
134 eventProfiler.reset(new ProfilerConfig(currentEvent));
135#endif
136
137#ifdef G4VERBOSE
138 if ( verboseLevel > 1 )
139 {
140 G4cout << currentEvent->GetNumberOfPrimaryVertex()
141 << " vertices passed from G4Event." << G4endl;
142 }
143#endif
144
145 if(!abortRequested)
146 {
147 StackTracks(transformer->GimmePrimaries(currentEvent,trackIDCounter), true);
148 }
149
150#ifdef G4VERBOSE
151 if ( verboseLevel > 0 )
152 {
153 G4cout << trackContainer->GetNTotalTrack() << " primaries "
154 << "are passed from G4EventTransformer." << G4endl;
155 G4cout << "!!!!!!! Now start processing an event !!!!!!!" << G4endl;
156 }
157#endif
158
159 G4VTrajectory* previousTrajectory;
160 while( (track=trackContainer->PopNextTrack(&previousTrajectory)) != nullptr )
161 { // Loop checking 12.28.2015 M.Asai
162
163#ifdef G4VERBOSE
164 if ( verboseLevel > 1 )
165 {
166 G4cout << "Track " << track << " (trackID " << track->GetTrackID()
167 << ", parentID " << track->GetParentID()
168 << ") is passed to G4TrackingManager." << G4endl;
169 }
170#endif
171
172 tracking = true;
173 trackManager->ProcessOneTrack( track );
174 istop = track->GetTrackStatus();
175 tracking = false;
176
177#ifdef G4VERBOSE
178 if ( verboseLevel > 0 )
179 {
180 G4cout << "Track (trackID " << track->GetTrackID()
181 << ", parentID " << track->GetParentID()
182 << ") is processed with stopping code " << istop << G4endl;
183 }
184#endif
185
186 G4VTrajectory* aTrajectory = nullptr;
187#ifdef G4_STORE_TRAJECTORY
188 aTrajectory = trackManager->GimmeTrajectory();
189
190 if(previousTrajectory != nullptr)
191 {
192 previousTrajectory->MergeTrajectory(aTrajectory);
193 delete aTrajectory;
194 aTrajectory = previousTrajectory;
195 }
196 if(aTrajectory&&(istop!=fStopButAlive)&&(istop!=fSuspend))
197 {
198 if(trajectoryContainer == nullptr)
199 {
200 trajectoryContainer = new G4TrajectoryContainer;
201 currentEvent->SetTrajectoryContainer(trajectoryContainer);
202 }
203 trajectoryContainer->insert(aTrajectory);
204 }
205#endif
206
207 G4TrackVector* secondaries = trackManager->GimmeSecondaries();
208 switch (istop)
209 {
210 case fStopButAlive:
211 case fSuspend:
212 trackContainer->PushOneTrack( track, aTrajectory );
213 StackTracks( secondaries );
214 break;
215
217 trackContainer->PushOneTrack( track );
218 StackTracks( secondaries );
219 break;
220
221 case fStopAndKill:
222 StackTracks( secondaries );
223 delete track;
224 break;
225
226 case fAlive:
227 G4Exception("G4EventManager::DoProcessing", "Event004", JustWarning,
228 "Illegal trackstatus returned from G4TrackingManager."\
229 " Continue with simulation.");
230 break;
232 if( secondaries )
233 {
234 for(std::size_t i=0; i<secondaries->size(); ++i)
235 { delete (*secondaries)[i]; }
236 secondaries->clear();
237 }
238 delete track;
239 break;
240 }
241 }
242
243#ifdef G4VERBOSE
244 if ( verboseLevel > 0 )
245 {
246 G4cout << "NULL returned from G4StackManager." << G4endl;
247 G4cout << "Terminate current event processing." << G4endl;
248 }
249#endif
250
251 if(sdManager != nullptr)
252 {
253 sdManager->TerminateCurrentEvent(currentEvent->GetHCofThisEvent());
254 }
255
256#if defined(GEANT4_USE_TIMEMORY)
257 eventProfiler.reset();
258#endif
259
260 if(userEventAction)
261 {
262 userEventAction->EndOfEventAction(currentEvent);
263 }
264
265 stateManager->SetNewState(G4State_GeomClosed);
266 currentEvent = nullptr;
267 abortRequested = false;
268}
269
270void G4EventManager::StackTracks(G4TrackVector* trackVector,
271 G4bool IDhasAlreadySet)
272{
273 if( trackVector != nullptr )
274 {
275 if( trackVector->size() == 0 ) return;
276 for( auto newTrack : *trackVector )
277 {
278 ++trackIDCounter;
279 if(!IDhasAlreadySet)
280 {
281 newTrack->SetTrackID( trackIDCounter );
282 if(newTrack->GetDynamicParticle()->GetPrimaryParticle())
283 {
285 = (G4PrimaryParticle*)(newTrack->GetDynamicParticle()->GetPrimaryParticle());
286 pp->SetTrackID(trackIDCounter);
287 }
288 }
289 newTrack->SetOriginTouchableHandle(newTrack->GetTouchableHandle());
290 trackContainer->PushOneTrack( newTrack );
291#ifdef G4VERBOSE
292 if ( verboseLevel > 1 )
293 {
294 G4cout << "A new track " << newTrack
295 << " (trackID " << newTrack->GetTrackID()
296 << ", parentID " << newTrack->GetParentID()
297 << ") is passed to G4StackManager." << G4endl;
298 }
299#endif
300 }
301 trackVector->clear();
302 }
303}
304
306{
307 userEventAction = userAction;
308 if(userEventAction != nullptr)
309 {
310 userEventAction->SetEventManager(this);
311 }
312}
313
315{
316 userStackingAction = userAction;
317 trackContainer->SetUserStackingAction(userAction);
318}
319
321{
322 userTrackingAction = userAction;
323 trackManager->SetUserAction(userAction);
324}
325
327{
328 userSteppingAction = userAction;
329 trackManager->SetUserAction(userAction);
330}
331
333{
334 trackIDCounter = 0;
335 DoProcessing(anEvent);
336}
337
339 G4Event* anEvent)
340{
341 static G4ThreadLocal G4String* randStat = nullptr;
342 if (randStat == nullptr) randStat = new G4String;
343 trackIDCounter = 0;
344 G4bool tempEvent = false;
345 if(anEvent == nullptr)
346 {
347 anEvent = new G4Event();
348 tempEvent = true;
349 }
350 if (storetRandomNumberStatusToG4Event==1
351 || storetRandomNumberStatusToG4Event==3)
352 {
353 std::ostringstream oss;
355 anEvent->SetRandomNumberStatus(*randStat=oss.str());
356 }
357 StackTracks(trackVector,false);
358 DoProcessing(anEvent);
359 if(tempEvent) { delete anEvent; }
360}
361
363{
364 G4ApplicationState currentState = stateManager->GetCurrentState();
365 if(currentState != G4State_EventProc || currentEvent == 0)
366 {
367 G4Exception("G4EventManager::SetUserInformation",
368 "Event0003", JustWarning,
369 "G4VUserEventInformation cannot be set because of absence "\
370 "of G4Event.");
371 return;
372 }
373
374 currentEvent->SetUserInformation(anInfo);
375}
376
378{
379 G4ApplicationState currentState = stateManager->GetCurrentState();
380 if(currentState != G4State_EventProc || currentEvent == 0)
381 {
382 return nullptr;
383 }
384
385 return currentEvent->GetUserInformation();
386}
387
389{
390 if(currentEvent != nullptr) { currentEvent->KeepTheEvent(); }
391}
392
394{
395 abortRequested = true;
396 trackContainer->clear();
397 if(tracking) trackManager->EventAborted();
398}
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
void SetUserAction(G4UserEventAction *userAction)
void SetUserInformation(G4VUserEventInformation *anInfo)
void KeepTheCurrentEvent()
G4ProfilerConfig< G4ProfileType::Event > ProfilerConfig
void AbortCurrentEvent()
static G4EventManager * GetEventManager()
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
virtual G4VPhysicalVolume * LocateGlobalPointAndSetup(const G4ThreeVector &point, const G4ThreeVector *direction=nullptr, const G4bool pRelativeSearch=true, const G4bool ignoreDirection=true)
Definition: G4Navigator.cc:126
G4TrackVector * GimmePrimaries(G4Event *anEvent, G4int trackIDCounter=0)
void TerminateCurrentEvent(G4HCofThisEvent *HCE)
Definition: G4SDManager.cc:116
G4HCofThisEvent * PrepareNewEvent()
Definition: G4SDManager.cc:109
static G4SDManager * GetSDMpointerIfExist()
Definition: G4SDManager.cc:48
G4int GetNTotalTrack() 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
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 MergeTrajectory(G4VTrajectory *secondTrajectory)=0
#define G4ThreadLocal
Definition: tls.hh:77