ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4EventManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4EventManager.cc
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 //
28 //
29 //
30 
31 #include "G4EventManager.hh"
32 #include "G4ios.hh"
33 #include "G4EvManMessenger.hh"
34 #include "G4Event.hh"
35 #include "G4UserEventAction.hh"
36 #include "G4UserStackingAction.hh"
37 #include "G4SDManager.hh"
38 #include "G4StateManager.hh"
39 #include "G4ApplicationState.hh"
41 #include "G4Navigator.hh"
42 #include "Randomize.hh"
43 
46 { return fpEventManager; }
47 
49 :currentEvent(nullptr),trajectoryContainer(nullptr),
50  trackIDCounter(0),
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  {
64  theMessenger = new G4EvManMessenger(this);
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 G4bool G4EventManager::operator==(const G4EventManager &right) const { }
95 G4bool G4EventManager::operator!=(const G4EventManager &right) const { }
96 */
97 
99 {
100  abortRequested = false;
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;
112  {
113  std::ostringstream oss;
115  randomNumberStatusToG4Event = oss.str();
117  }
118 
119  // Resetting Navigator has been moved to G4EventManager, so that resetting
120  // is now done for every event.
121  G4ThreeVector center(0,0,0);
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 
139 
140 #ifdef G4_STORE_TRAJECTORY
141  trajectoryContainer = nullptr;
142 #endif
143 
145  if(sdManager)
147 
149 
150 #ifdef G4VERBOSE
151  if ( verboseLevel > 1 )
152  {
154  << " vertices passed from G4Event." << G4endl;
155  }
156 #endif
157 
158  if(!abortRequested)
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  {
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  G4Exception("G4EventManager::DoProcessing","Event004",JustWarning,
237  "Illegal trackstatus returned from G4TrackingManager. Continue with"\
238  "simulation.");
239  break;
241  //if( secondaries ) secondaries->clearAndDestroy();
242  if( secondaries )
243  {
244  for(size_t i=0;i<secondaries->size();i++)
245  { delete (*secondaries)[i]; }
246  secondaries->clear();
247  }
248  delete track;
249  break;
250  }
251  }
252 
253 #ifdef G4VERBOSE
254  if ( verboseLevel > 0 )
255  {
256  G4cout << "NULL returned from G4StackManager." << G4endl;
257  G4cout << "Terminate current event processing." << G4endl;
258  }
259 #endif
260 
261  if(sdManager)
263 
265 
267  currentEvent = nullptr;
268  abortRequested = false;
269 }
270 
271 void G4EventManager::StackTracks(G4TrackVector *trackVector,G4bool IDhasAlreadySet)
272 {
273  if( trackVector )
274  {
275  //size_t n_passedTrack = trackVector->size();
276  //if( n_passedTrack == 0 ) return;
277  //for( size_t i = 0; i < n_passedTrack; i++ )
278  //{
279  // newTrack = (*trackVector)[ i ];
280  if( trackVector->size() == 0 ) return;
281  for( auto newTrack : *trackVector )
282  {
283  trackIDCounter++;
284  if(!IDhasAlreadySet)
285  {
286  newTrack->SetTrackID( trackIDCounter );
287  if(newTrack->GetDynamicParticle()->GetPrimaryParticle())
288  {
290  = (G4PrimaryParticle*)(newTrack->GetDynamicParticle()->GetPrimaryParticle());
292  }
293  }
294  newTrack->SetOriginTouchableHandle(newTrack->GetTouchableHandle());
295  trackContainer->PushOneTrack( newTrack );
296 #ifdef G4VERBOSE
297  if ( verboseLevel > 1 )
298  {
299  G4cout << "A new track " << newTrack
300  << " (trackID " << newTrack->GetTrackID()
301  << ", parentID " << newTrack->GetParentID()
302  << ") is passed to G4StackManager." << G4endl;
303  }
304 #endif
305  }
306  trackVector->clear();
307  }
308 }
309 
311 {
312  userEventAction = userAction;
314 }
315 
317 {
318  userStackingAction = userAction;
320 }
321 
323 {
324  userTrackingAction = userAction;
325  trackManager->SetUserAction(userAction);
326 }
327 
329 {
330  userSteppingAction = userAction;
331  trackManager->SetUserAction(userAction);
332 }
333 
335 {
336  trackIDCounter = 0;
337  DoProcessing(anEvent);
338 }
339 
341 {
342  static G4ThreadLocal G4String *randStat = 0;
343  if (!randStat) randStat = new G4String;
344  trackIDCounter = 0;
345  G4bool tempEvent = false;
346  if(!anEvent)
347  {
348  anEvent = new G4Event();
349  tempEvent = true;
350  }
352  {
353  std::ostringstream oss;
355  anEvent->SetRandomNumberStatus(*randStat=oss.str());
356  }
357  StackTracks(trackVector,false);
358  DoProcessing(anEvent);
359  if(tempEvent)
360  { delete anEvent; }
361 }
362 
364 {
366  if(currentState!=G4State_EventProc || currentEvent==0)
367  {
368  G4Exception("G4EventManager::SetUserInformation",
369  "Event0003", JustWarning,
370  "G4VUserEventInformation cannot be set because of ansense of G4Event.");
371  return;
372  }
373 
375 }
376 
378 {
380  if(currentState!=G4State_EventProc || currentEvent==0)
381  { return 0; }
382 
384 }
385 
388 
390 {
391  abortRequested = true;
394 }
395