source: trunk/source/event/src/G4EventManager.cc

Last change on this file was 1337, checked in by garnier, 14 years ago

tag geant4.9.4 beta 1 + modifs locales

File size: 11.5 KB
Line 
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,v 1.31 2010/06/12 04:07:45 asaim Exp $
28// GEANT4 tag $Name: geant4-09-04-beta-01 $
29//
30//
31//
32
33#include "G4EventManager.hh"
34#include "G4ios.hh"
35#include "G4EvManMessenger.hh"
36#include "G4Event.hh"
37#include "G4UserEventAction.hh"
38#include "G4UserStackingAction.hh"
39#include "G4SDManager.hh"
40#include "G4StateManager.hh"
41#include "G4ApplicationState.hh"
42#include "G4TransportationManager.hh"
43#include "G4Navigator.hh"
44#include "Randomize.hh"
45
46G4EventManager* G4EventManager::fpEventManager = 0;
47G4EventManager* G4EventManager::GetEventManager()
48{ return fpEventManager; }
49
50G4EventManager::G4EventManager()
51:currentEvent(0),trajectoryContainer(0),
52 verboseLevel(0),tracking(false),abortRequested(false),
53 storetRandomNumberStatusToG4Event(false)
54{
55 if(fpEventManager)
56 {
57  G4Exception("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);
65  sdManager = G4SDManager::GetSDMpointerIfExist();
66  fpEventManager = this;
67  userEventAction = 0;
68  userStackingAction = 0;
69  userTrackingAction = 0;
70  userSteppingAction = 0;
71 }
72}
73
74// private -> never called
75G4EventManager::G4EventManager(const G4EventManager&) {;}
76G4EventManager& G4EventManager::operator=(const G4EventManager&)
77{ return *this; }
78
79G4EventManager::~G4EventManager()
80{
81   delete trackContainer;
82   delete transformer;
83   delete trackManager;
84   delete theMessenger;
85   if(userEventAction) delete userEventAction;
86   fpEventManager = 0;
87}
88
89/*
90const G4EventManager & G4EventManager::operator=(const G4EventManager &right)
91{ }
92G4int G4EventManager::operator==(const G4EventManager &right) const { }
93G4int G4EventManager::operator!=(const G4EventManager &right) const { }
94*/
95
96
97
98void G4EventManager::DoProcessing(G4Event* anEvent)
99{
100  G4StateManager* stateManager = G4StateManager::GetStateManager();
101  G4ApplicationState currentState = stateManager->GetCurrentState();
102  if(currentState!=G4State_GeomClosed)
103  {
104    G4Exception("G4EventManager::ProcessOneEvent",
105                "IllegalApplicationState",
106                JustWarning,
107                "Geometry is not closed : cannot process an event.");
108    return;
109  }
110  currentEvent = anEvent;
111  stateManager->SetNewState(G4State_EventProc);
112  if(storetRandomNumberStatusToG4Event>1)
113  {
114    std::ostringstream oss;
115    CLHEP::HepRandom::saveFullState(oss);
116    randomNumberStatusToG4Event = oss.str();
117    currentEvent->SetRandomNumberStatusForProcessing(randomNumberStatusToG4Event); 
118  }
119
120  // Reseting Navigator has been moved to G4Eventmanager, so that resetting
121  // is now done for every event.
122  G4ThreeVector center(0,0,0);
123  G4Navigator* navigator =
124      G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
125  navigator->LocateGlobalPointAndSetup(center,0,false);
126                                                                                     
127  G4Track * track;
128  G4TrackStatus istop;
129
130#ifdef G4VERBOSE
131  if ( verboseLevel > 0 )
132  {
133    G4cout << "=====================================" << G4endl;
134    G4cout << "  G4EventManager::ProcessOneEvent()  " << G4endl;
135    G4cout << "=====================================" << G4endl;
136  }
137#endif
138
139  trackContainer->PrepareNewEvent();
140
141#ifdef G4_STORE_TRAJECTORY
142  trajectoryContainer = 0;
143#endif
144
145  sdManager = G4SDManager::GetSDMpointerIfExist();
146  if(sdManager)
147  { currentEvent->SetHCofThisEvent(sdManager->PrepareNewEvent()); }
148
149  if(userEventAction) userEventAction->BeginOfEventAction(currentEvent);
150
151#ifdef G4VERBOSE
152  if ( verboseLevel > 1 )
153  {
154    G4cout << currentEvent->GetNumberOfPrimaryVertex()
155         << " vertices passed from G4Event." << G4endl;
156  }
157#endif
158
159  if(!abortRequested)
160  { StackTracks( transformer->GimmePrimaries( currentEvent, trackIDCounter ),true ); }
161
162#ifdef G4VERBOSE
163  if ( verboseLevel > 0 )
164  {
165    G4cout << trackContainer->GetNTotalTrack() << " primaries "
166         << "are passed from G4EventTransformer." << G4endl;
167    G4cout << "!!!!!!! Now start processing an event !!!!!!!" << G4endl;
168  }
169#endif
170 
171  G4VTrajectory* previousTrajectory;
172  while( ( track = trackContainer->PopNextTrack(&previousTrajectory) ) != 0 )
173  {
174
175#ifdef G4VERBOSE
176    if ( verboseLevel > 1 )
177    {
178      G4cout << "Track " << track << " (trackID " << track->GetTrackID()
179         << ", parentID " << track->GetParentID() 
180         << ") is passed to G4TrackingManager." << G4endl;
181    }
182#endif
183
184    tracking = true;
185    trackManager->ProcessOneTrack( track );
186    istop = track->GetTrackStatus();
187    tracking = false;
188
189#ifdef G4VERBOSE
190    if ( verboseLevel > 0 )
191    {
192      G4cout << "Track (trackID " << track->GetTrackID()
193         << ", parentID " << track->GetParentID()
194         << ") is processed with stopping code " << istop << G4endl;
195    }
196#endif
197
198    G4VTrajectory * aTrajectory = 0;
199#ifdef G4_STORE_TRAJECTORY
200    aTrajectory = trackManager->GimmeTrajectory();
201
202    if(previousTrajectory)
203    {
204      previousTrajectory->MergeTrajectory(aTrajectory);
205      delete aTrajectory;
206      aTrajectory = previousTrajectory;
207    }
208    if(aTrajectory&&(istop!=fStopButAlive)&&(istop!=fSuspend))
209    {
210      if(!trajectoryContainer)
211      { trajectoryContainer = new G4TrajectoryContainer; 
212        currentEvent->SetTrajectoryContainer(trajectoryContainer); }
213      trajectoryContainer->insert(aTrajectory);
214    }
215#endif
216
217    G4TrackVector * secondaries = trackManager->GimmeSecondaries();
218    switch (istop)
219    {
220      case fStopButAlive:
221      case fSuspend:
222        trackContainer->PushOneTrack( track, aTrajectory );
223        StackTracks( secondaries );
224        break;
225
226      case fPostponeToNextEvent:
227        trackContainer->PushOneTrack( track );
228        StackTracks( secondaries );
229        break;
230
231      case fStopAndKill:
232        StackTracks( secondaries );
233        delete track;
234        break;
235
236      case fAlive:
237        G4cout << "Illeagal TrackStatus returned from G4TrackingManager!"
238             << G4endl;
239      case fKillTrackAndSecondaries:
240        //if( secondaries ) secondaries->clearAndDestroy();
241        if( secondaries )
242        {
243          for(size_t i=0;i<secondaries->size();i++)
244          { delete (*secondaries)[i]; }
245          secondaries->clear();
246        }
247        delete track;
248        break;
249    }
250  }
251
252#ifdef G4VERBOSE
253  if ( verboseLevel > 0 )
254  {
255    G4cout << "NULL returned from G4StackManager." << G4endl;
256    G4cout << "Terminate current event processing." << G4endl;
257  }
258#endif
259
260  if(sdManager)
261  { sdManager->TerminateCurrentEvent(currentEvent->GetHCofThisEvent()); }
262
263  if(userEventAction) userEventAction->EndOfEventAction(currentEvent);
264
265  stateManager->SetNewState(G4State_GeomClosed);
266  currentEvent = 0;
267  abortRequested = false;
268}
269
270void G4EventManager::StackTracks(G4TrackVector *trackVector,G4bool IDhasAlreadySet)
271{
272  G4Track * newTrack;
273
274  if( trackVector )
275  {
276
277    size_t n_passedTrack = trackVector->size();
278    if( n_passedTrack == 0 ) return;
279    for( size_t i = 0; i < n_passedTrack; i++ )
280    {
281      newTrack = (*trackVector)[ i ];
282      trackIDCounter++;
283      if(!IDhasAlreadySet)
284      {
285        newTrack->SetTrackID( trackIDCounter );
286        if(newTrack->GetDynamicParticle()->GetPrimaryParticle())
287        {
288          G4PrimaryParticle* pp
289            = (G4PrimaryParticle*)(newTrack->GetDynamicParticle()->GetPrimaryParticle());
290          pp->SetTrackID(trackIDCounter);
291        }
292      }
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
308void G4EventManager::SetUserAction(G4UserEventAction* userAction)
309{
310  userEventAction = userAction;
311  if(userEventAction) userEventAction->SetEventManager(this);
312}
313
314void G4EventManager::SetUserAction(G4UserStackingAction* userAction)
315{
316  userStackingAction = userAction;
317  trackContainer->SetUserStackingAction(userAction);
318}
319
320void G4EventManager::SetUserAction(G4UserTrackingAction* userAction)
321{
322  userTrackingAction = userAction;
323  trackManager->SetUserAction(userAction);
324}
325
326void G4EventManager::SetUserAction(G4UserSteppingAction* userAction)
327{
328  userSteppingAction = userAction;
329  trackManager->SetUserAction(userAction);
330}
331
332void G4EventManager::ProcessOneEvent(G4Event* anEvent)
333{
334  trackIDCounter = 0;
335  DoProcessing(anEvent);
336}
337
338void G4EventManager::ProcessOneEvent(G4TrackVector* trackVector,G4Event* anEvent)
339{
340  static G4String randStat;
341  trackIDCounter = 0;
342  G4bool tempEvent = false;
343  if(!anEvent)
344  {
345    anEvent = new G4Event();
346    tempEvent = true;
347  }
348  if(storetRandomNumberStatusToG4Event==1 || storetRandomNumberStatusToG4Event==3)
349  {
350    std::ostringstream oss;
351    CLHEP::HepRandom::saveFullState(oss);
352    anEvent->SetRandomNumberStatus(randStat=oss.str());
353  }
354  StackTracks(trackVector,false);
355  DoProcessing(anEvent);
356  if(tempEvent)
357  { delete anEvent; }
358}
359
360void G4EventManager::SetUserInformation(G4VUserEventInformation* anInfo)
361{ 
362  G4StateManager* stateManager = G4StateManager::GetStateManager();
363  G4ApplicationState currentState = stateManager->GetCurrentState();
364  if(currentState!=G4State_EventProc || currentEvent==0)
365  {
366    G4Exception("G4EventManager::SetUserInformation",
367                "IllegalApplicationState",
368                JustWarning,
369                "G4VUserEventInformation cannot be set because of ansense of G4Event.");
370    return;
371  }
372 
373  currentEvent->SetUserInformation(anInfo);
374}
375
376G4VUserEventInformation* G4EventManager::GetUserInformation()
377{ 
378  G4StateManager* stateManager = G4StateManager::GetStateManager();
379  G4ApplicationState currentState = stateManager->GetCurrentState();
380  if(currentState!=G4State_EventProc || currentEvent==0)
381  { return 0; }
382 
383  return currentEvent->GetUserInformation();
384}
385
386void G4EventManager::KeepTheCurrentEvent()
387{ if(currentEvent) currentEvent->KeepTheEvent(); }
388
389
Note: See TracBrowser for help on using the repository browser.