Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 //
27 // $Id: G4EventManager.cc 94950 2016-01-07 11:53:14Z gcosmo $
28 //
29 //
30 //
31 
32 #include "G4EventManager.hh"
33 #include "G4ios.hh"
34 #include "G4EvManMessenger.hh"
35 #include "G4Event.hh"
36 #include "G4UserEventAction.hh"
37 #include "G4UserStackingAction.hh"
38 #include "G4SDManager.hh"
39 #include "G4StateManager.hh"
40 #include "G4ApplicationState.hh"
42 #include "G4Navigator.hh"
43 #include "Randomize.hh"
44 
45 G4ThreadLocal G4EventManager* G4EventManager::fpEventManager = nullptr;
47 { return fpEventManager; }
48 
50 :currentEvent(nullptr),trajectoryContainer(nullptr),
51  verboseLevel(0),tracking(false),abortRequested(false),
52  storetRandomNumberStatusToG4Event(false)
53 {
54  if(fpEventManager)
55  {
56  G4Exception("G4EventManager::G4EventManager","Event0001",FatalException,
57  "G4EventManager::G4EventManager() has already been made.");
58  }
59  else
60  {
61  trackManager = new G4TrackingManager;
62  transformer = new G4PrimaryTransformer;
63  trackContainer = new G4StackManager;
64  theMessenger = new G4EvManMessenger(this);
66  stateManager = G4StateManager::GetStateManager();
67  fpEventManager = this;
68  userEventAction = nullptr;
69  userStackingAction = nullptr;
70  userTrackingAction = nullptr;
71  userSteppingAction = nullptr;
72  }
73 }
74 
75 /* private -> never called
76 G4EventManager::G4EventManager(const G4EventManager&) {;}
77 G4EventManager& G4EventManager::operator=(const G4EventManager&)
78 { return *this; }
79 */
80 
82 {
83  delete trackContainer;
84  delete transformer;
85  delete trackManager;
86  delete theMessenger;
87  delete userEventAction;
88  fpEventManager = 0;
89 }
90 
91 /*
92 const G4EventManager & G4EventManager::operator=(const G4EventManager &right)
93 { }
94 G4int G4EventManager::operator==(const G4EventManager &right) const { }
95 G4int G4EventManager::operator!=(const G4EventManager &right) const { }
96 */
97 
98 void G4EventManager::DoProcessing(G4Event* anEvent)
99 {
100  abortRequested = false;
101  G4ApplicationState currentState = stateManager->GetCurrentState();
102  if(currentState!=G4State_GeomClosed)
103  {
104  G4Exception("G4EventManager::ProcessOneEvent",
105  "Event0002", JustWarning,
106  "IllegalApplicationState -- Geometry is not closed : cannot process an event.");
107  return;
108  }
109  currentEvent = anEvent;
110  stateManager->SetNewState(G4State_EventProc);
111  if(storetRandomNumberStatusToG4Event>1)
112  {
113  std::ostringstream oss;
115  randomNumberStatusToG4Event = oss.str();
116  currentEvent->SetRandomNumberStatusForProcessing(randomNumberStatusToG4Event);
117  }
118 
119  // Reseting Navigator has been moved to G4EventManager, so that resetting
120  // is now done for every event.
121  G4ThreeVector center(0,0,0);
122  G4Navigator* navigator =
124  navigator->LocateGlobalPointAndSetup(center,0,false);
125 
126  G4Track * track = nullptr;
127  G4TrackStatus istop = fAlive;
128 
129 #ifdef G4VERBOSE
130  if ( verboseLevel > 0 )
131  {
132  G4cout << "=====================================" << G4endl;
133  G4cout << " G4EventManager::ProcessOneEvent() " << G4endl;
134  G4cout << "=====================================" << G4endl;
135  }
136 #endif
137 
138  trackContainer->PrepareNewEvent();
139 
140 #ifdef G4_STORE_TRAJECTORY
141  trajectoryContainer = nullptr;
142 #endif
143 
144  sdManager = G4SDManager::GetSDMpointerIfExist();
145  if(sdManager)
146  { currentEvent->SetHCofThisEvent(sdManager->PrepareNewEvent()); }
147 
148  if(userEventAction) userEventAction->BeginOfEventAction(currentEvent);
149 
150 #ifdef G4VERBOSE
151  if ( verboseLevel > 1 )
152  {
153  G4cout << currentEvent->GetNumberOfPrimaryVertex()
154  << " vertices passed from G4Event." << G4endl;
155  }
156 #endif
157 
158  if(!abortRequested)
159  { StackTracks( transformer->GimmePrimaries( currentEvent, trackIDCounter ),true ); }
160 
161 #ifdef G4VERBOSE
162  if ( verboseLevel > 0 )
163  {
164  G4cout << trackContainer->GetNTotalTrack() << " primaries "
165  << "are passed from G4EventTransformer." << G4endl;
166  G4cout << "!!!!!!! Now start processing an event !!!!!!!" << G4endl;
167  }
168 #endif
169 
170  G4VTrajectory* previousTrajectory;
171  while( ( track = trackContainer->PopNextTrack(&previousTrajectory) ) != 0 ) // Loop checking 12.28.2015 M.Asai
172  {
173 
174 #ifdef G4VERBOSE
175  if ( verboseLevel > 1 )
176  {
177  G4cout << "Track " << track << " (trackID " << track->GetTrackID()
178  << ", parentID " << track->GetParentID()
179  << ") is passed to G4TrackingManager." << G4endl;
180  }
181 #endif
182 
183  tracking = true;
184  trackManager->ProcessOneTrack( track );
185  istop = track->GetTrackStatus();
186  tracking = false;
187 
188 #ifdef G4VERBOSE
189  if ( verboseLevel > 0 )
190  {
191  G4cout << "Track (trackID " << track->GetTrackID()
192  << ", parentID " << track->GetParentID()
193  << ") is processed with stopping code " << istop << G4endl;
194  }
195 #endif
196 
197  G4VTrajectory * aTrajectory = nullptr;
198 #ifdef G4_STORE_TRAJECTORY
199  aTrajectory = trackManager->GimmeTrajectory();
200 
201  if(previousTrajectory)
202  {
203  previousTrajectory->MergeTrajectory(aTrajectory);
204  delete aTrajectory;
205  aTrajectory = previousTrajectory;
206  }
207  if(aTrajectory&&(istop!=fStopButAlive)&&(istop!=fSuspend))
208  {
209  if(!trajectoryContainer)
210  { trajectoryContainer = new G4TrajectoryContainer;
211  currentEvent->SetTrajectoryContainer(trajectoryContainer); }
212  trajectoryContainer->insert(aTrajectory);
213  }
214 #endif
215 
216  G4TrackVector * secondaries = trackManager->GimmeSecondaries();
217  switch (istop)
218  {
219  case fStopButAlive:
220  case fSuspend:
221  trackContainer->PushOneTrack( track, aTrajectory );
222  StackTracks( secondaries );
223  break;
224 
226  trackContainer->PushOneTrack( track );
227  StackTracks( secondaries );
228  break;
229 
230  case fStopAndKill:
231  StackTracks( secondaries );
232  delete track;
233  break;
234 
235  case fAlive:
236  G4cout << "Illeagal TrackStatus returned from G4TrackingManager!"
237  << G4endl;
239  //if( secondaries ) secondaries->clearAndDestroy();
240  if( secondaries )
241  {
242  for(size_t i=0;i<secondaries->size();i++)
243  { delete (*secondaries)[i]; }
244  secondaries->clear();
245  }
246  delete track;
247  break;
248  }
249  }
250 
251 #ifdef G4VERBOSE
252  if ( verboseLevel > 0 )
253  {
254  G4cout << "NULL returned from G4StackManager." << G4endl;
255  G4cout << "Terminate current event processing." << G4endl;
256  }
257 #endif
258 
259  if(sdManager)
260  { sdManager->TerminateCurrentEvent(currentEvent->GetHCofThisEvent()); }
261 
262  if(userEventAction) userEventAction->EndOfEventAction(currentEvent);
263 
264  stateManager->SetNewState(G4State_GeomClosed);
265  currentEvent = nullptr;
266  abortRequested = false;
267 }
268 
269 void G4EventManager::StackTracks(G4TrackVector *trackVector,G4bool IDhasAlreadySet)
270 {
271  if( trackVector )
272  {
273  //size_t n_passedTrack = trackVector->size();
274  //if( n_passedTrack == 0 ) return;
275  //for( size_t i = 0; i < n_passedTrack; i++ )
276  //{
277  // newTrack = (*trackVector)[ i ];
278  if( trackVector->size() == 0 ) return;
279  for( auto newTrack : *trackVector )
280  {
281  trackIDCounter++;
282  if(!IDhasAlreadySet)
283  {
284  newTrack->SetTrackID( trackIDCounter );
285  if(newTrack->GetDynamicParticle()->GetPrimaryParticle())
286  {
288  = (G4PrimaryParticle*)(newTrack->GetDynamicParticle()->GetPrimaryParticle());
289  pp->SetTrackID(trackIDCounter);
290  }
291  }
292  newTrack->SetOriginTouchableHandle(newTrack->GetTouchableHandle());
293  trackContainer->PushOneTrack( newTrack );
294 #ifdef G4VERBOSE
295  if ( verboseLevel > 1 )
296  {
297  G4cout << "A new track " << newTrack
298  << " (trackID " << newTrack->GetTrackID()
299  << ", parentID " << newTrack->GetParentID()
300  << ") is passed to G4StackManager." << G4endl;
301  }
302 #endif
303  }
304  trackVector->clear();
305  }
306 }
307 
309 {
310  userEventAction = userAction;
311  if(userEventAction) userEventAction->SetEventManager(this);
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 {
340  static G4ThreadLocal G4String *randStat = 0;
341  if (!randStat) randStat = new G4String;
342  trackIDCounter = 0;
343  G4bool tempEvent = false;
344  if(!anEvent)
345  {
346  anEvent = new G4Event();
347  tempEvent = true;
348  }
349  if(storetRandomNumberStatusToG4Event==1 || storetRandomNumberStatusToG4Event==3)
350  {
351  std::ostringstream oss;
353  anEvent->SetRandomNumberStatus(*randStat=oss.str());
354  }
355  StackTracks(trackVector,false);
356  DoProcessing(anEvent);
357  if(tempEvent)
358  { delete anEvent; }
359 }
360 
362 {
363  G4ApplicationState currentState = stateManager->GetCurrentState();
364  if(currentState!=G4State_EventProc || currentEvent==0)
365  {
366  G4Exception("G4EventManager::SetUserInformation",
367  "Event0003", JustWarning,
368  "G4VUserEventInformation cannot be set because of ansense of G4Event.");
369  return;
370  }
371 
372  currentEvent->SetUserInformation(anInfo);
373 }
374 
376 {
377  G4ApplicationState currentState = stateManager->GetCurrentState();
378  if(currentState!=G4State_EventProc || currentEvent==0)
379  { return 0; }
380 
381  return currentEvent->GetUserInformation();
382 }
383 
385 { if(currentEvent) currentEvent->KeepTheEvent(); }
386 
388 {
389  abortRequested = true;
390  trackContainer->clear();
391  if(tracking) trackManager->EventAborted();
392 }
393 
G4int GetParentID() const
G4VTrajectory * GimmeTrajectory() const
G4VUserEventInformation * GetUserInformation() const
Definition: G4Event.hh:199
void ProcessOneTrack(G4Track *apValueG4Track)
G4TrackVector * GimmePrimaries(G4Event *anEvent, G4int trackIDCounter=0)
virtual void EndOfEventAction(const G4Event *anEvent)
G4int GetNumberOfPrimaryVertex() const
Definition: G4Event.hh:164
G4int GetNTotalTrack() const
G4TrackStatus GetTrackStatus() const
G4Navigator * GetNavigatorForTracking() const
void SetRandomNumberStatusForProcessing(G4String &st)
Definition: G4Event.hh:130
void SetHCofThisEvent(G4HCofThisEvent *value)
Definition: G4Event.hh:117
virtual void BeginOfEventAction(const G4Event *anEvent)
G4Track * PopNextTrack(G4VTrajectory **newTrajectory)
#define G4ThreadLocal
Definition: tls.hh:89
void TerminateCurrentEvent(G4HCofThisEvent *HCE)
Definition: G4SDManager.cc:116
virtual void SetEventManager(G4EventManager *value)
virtual void MergeTrajectory(G4VTrajectory *secondTrajectory)=0
static G4StateManager * GetStateManager()
void SetUserInformation(G4VUserEventInformation *anInfo)
Definition: G4Event.hh:198
void SetUserStackingAction(G4UserStackingAction *value)
G4GLOB_DLL std::ostream G4cout
G4bool SetNewState(G4ApplicationState requestedState)
G4TrackVector * GimmeSecondaries() const
void SetUserInformation(G4VUserEventInformation *anInfo)
bool G4bool
Definition: G4Types.hh:79
void SetTrackID(G4int id)
G4int PushOneTrack(G4Track *newTrack, G4VTrajectory *newTrajectory=0)
G4int PrepareNewEvent()
G4int GetTrackID() const
G4ApplicationState GetCurrentState() const
void SetUserAction(G4UserTrackingAction *apAction)
G4bool insert(G4VTrajectory *p)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void SetRandomNumberStatus(G4String &st)
Definition: G4Event.hh:125
static G4TransportationManager * GetTransportationManager()
G4HCofThisEvent * PrepareNewEvent()
Definition: G4SDManager.cc:109
std::vector< G4Track * > G4TrackVector
void SetUserAction(G4UserEventAction *userAction)
static G4EventManager * GetEventManager()
virtual G4VPhysicalVolume * LocateGlobalPointAndSetup(const G4ThreeVector &point, const G4ThreeVector *direction=0, const G4bool pRelativeSearch=true, const G4bool ignoreDirection=true)
Definition: G4Navigator.cc:125
void AbortCurrentEvent()
#define G4endl
Definition: G4ios.hh:61
void ProcessOneEvent(G4Event *anEvent)
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:285
G4HCofThisEvent * GetHCofThisEvent() const
Definition: G4Event.hh:185
void SetTrajectoryContainer(G4TrajectoryContainer *value)
Definition: G4Event.hh:121
void KeepTheEvent(G4bool vl=true)
Definition: G4Event.hh:135
G4TrackStatus
void KeepTheCurrentEvent()
G4ApplicationState
static G4SDManager * GetSDMpointerIfExist()
Definition: G4SDManager.cc:49
G4VUserEventInformation * GetUserInformation()