G4EventManager.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 //
00030 //
00031 
00032 #include "G4EventManager.hh"
00033 #include "G4ios.hh"
00034 #include "G4EvManMessenger.hh"
00035 #include "G4Event.hh"
00036 #include "G4UserEventAction.hh"
00037 #include "G4UserStackingAction.hh"
00038 #include "G4SDManager.hh"
00039 #include "G4StateManager.hh"
00040 #include "G4ApplicationState.hh"
00041 #include "G4TransportationManager.hh"
00042 #include "G4Navigator.hh"
00043 #include "Randomize.hh"
00044 
00045 G4EventManager* G4EventManager::fpEventManager = 0;
00046 G4EventManager* G4EventManager::GetEventManager()
00047 { return fpEventManager; }
00048 
00049 G4EventManager::G4EventManager()
00050 :currentEvent(0),trajectoryContainer(0),
00051  verboseLevel(0),tracking(false),abortRequested(false),
00052  storetRandomNumberStatusToG4Event(false)
00053 {
00054  if(fpEventManager)
00055  {
00056   G4Exception("G4EventManager::G4EventManager","Event0001",FatalException,
00057   "G4EventManager::G4EventManager() has already been made.");
00058  }
00059  else
00060  {
00061   trackManager = new G4TrackingManager;
00062   transformer = new G4PrimaryTransformer;
00063   trackContainer = new G4StackManager;
00064   theMessenger = new G4EvManMessenger(this);
00065   sdManager = G4SDManager::GetSDMpointerIfExist();
00066   fpEventManager = this;
00067   userEventAction = 0;
00068   userStackingAction = 0;
00069   userTrackingAction = 0;
00070   userSteppingAction = 0;
00071  }
00072 }
00073 
00074 // private -> never called
00075 G4EventManager::G4EventManager(const G4EventManager&) {;}
00076 G4EventManager& G4EventManager::operator=(const G4EventManager&)
00077 { return *this; }
00078 
00079 G4EventManager::~G4EventManager()
00080 {
00081    delete trackContainer;
00082    delete transformer;
00083    delete trackManager;
00084    delete theMessenger;
00085    if(userEventAction) delete userEventAction;
00086    fpEventManager = 0;
00087 }
00088 
00089 /*
00090 const G4EventManager & G4EventManager::operator=(const G4EventManager &right)
00091 { }
00092 G4int G4EventManager::operator==(const G4EventManager &right) const { }
00093 G4int G4EventManager::operator!=(const G4EventManager &right) const { }
00094 */
00095 
00096 
00097 
00098 void G4EventManager::DoProcessing(G4Event* anEvent)
00099 {
00100   abortRequested = false;
00101   G4StateManager* stateManager = G4StateManager::GetStateManager();
00102   G4ApplicationState currentState = stateManager->GetCurrentState();
00103   if(currentState!=G4State_GeomClosed)
00104   {
00105     G4Exception("G4EventManager::ProcessOneEvent",
00106                 "Event0002", JustWarning,
00107                 "IllegalApplicationState -- Geometry is not closed : cannot process an event.");
00108     return;
00109   }
00110   currentEvent = anEvent;
00111   stateManager->SetNewState(G4State_EventProc);
00112   if(storetRandomNumberStatusToG4Event>1)
00113   {
00114     std::ostringstream oss;
00115     CLHEP::HepRandom::saveFullState(oss);
00116     randomNumberStatusToG4Event = oss.str();
00117     currentEvent->SetRandomNumberStatusForProcessing(randomNumberStatusToG4Event); 
00118   }
00119 
00120   // Reseting Navigator has been moved to G4Eventmanager, so that resetting
00121   // is now done for every event.
00122   G4ThreeVector center(0,0,0);
00123   G4Navigator* navigator =
00124       G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
00125   navigator->LocateGlobalPointAndSetup(center,0,false);
00126                                                                                       
00127   G4Track * track;
00128   G4TrackStatus istop;
00129 
00130 #ifdef G4VERBOSE
00131   if ( verboseLevel > 0 )
00132   {
00133     G4cout << "=====================================" << G4endl;
00134     G4cout << "  G4EventManager::ProcessOneEvent()  " << G4endl;
00135     G4cout << "=====================================" << G4endl;
00136   }
00137 #endif
00138 
00139   trackContainer->PrepareNewEvent();
00140 
00141 #ifdef G4_STORE_TRAJECTORY
00142   trajectoryContainer = 0;
00143 #endif
00144 
00145   sdManager = G4SDManager::GetSDMpointerIfExist();
00146   if(sdManager)
00147   { currentEvent->SetHCofThisEvent(sdManager->PrepareNewEvent()); }
00148 
00149   if(userEventAction) userEventAction->BeginOfEventAction(currentEvent);
00150 
00151 #ifdef G4VERBOSE
00152   if ( verboseLevel > 1 )
00153   {
00154     G4cout << currentEvent->GetNumberOfPrimaryVertex()
00155          << " vertices passed from G4Event." << G4endl;
00156   }
00157 #endif
00158 
00159   if(!abortRequested)
00160   { StackTracks( transformer->GimmePrimaries( currentEvent, trackIDCounter ),true ); }
00161 
00162 #ifdef G4VERBOSE
00163   if ( verboseLevel > 0 )
00164   {
00165     G4cout << trackContainer->GetNTotalTrack() << " primaries "
00166          << "are passed from G4EventTransformer." << G4endl;
00167     G4cout << "!!!!!!! Now start processing an event !!!!!!!" << G4endl;
00168   }
00169 #endif
00170   
00171   G4VTrajectory* previousTrajectory;
00172   while( ( track = trackContainer->PopNextTrack(&previousTrajectory) ) != 0 )
00173   {
00174 
00175 #ifdef G4VERBOSE
00176     if ( verboseLevel > 1 )
00177     {
00178       G4cout << "Track " << track << " (trackID " << track->GetTrackID()
00179          << ", parentID " << track->GetParentID() 
00180          << ") is passed to G4TrackingManager." << G4endl;
00181     }
00182 #endif
00183 
00184     tracking = true;
00185     trackManager->ProcessOneTrack( track );
00186     istop = track->GetTrackStatus();
00187     tracking = false;
00188 
00189 #ifdef G4VERBOSE
00190     if ( verboseLevel > 0 )
00191     {
00192       G4cout << "Track (trackID " << track->GetTrackID()
00193          << ", parentID " << track->GetParentID()
00194          << ") is processed with stopping code " << istop << G4endl;
00195     }
00196 #endif
00197 
00198     G4VTrajectory * aTrajectory = 0;
00199 #ifdef G4_STORE_TRAJECTORY
00200     aTrajectory = trackManager->GimmeTrajectory();
00201 
00202     if(previousTrajectory)
00203     {
00204       previousTrajectory->MergeTrajectory(aTrajectory);
00205       delete aTrajectory;
00206       aTrajectory = previousTrajectory;
00207     }
00208     if(aTrajectory&&(istop!=fStopButAlive)&&(istop!=fSuspend))
00209     {
00210       if(!trajectoryContainer)
00211       { trajectoryContainer = new G4TrajectoryContainer; 
00212         currentEvent->SetTrajectoryContainer(trajectoryContainer); }
00213       trajectoryContainer->insert(aTrajectory);
00214     }
00215 #endif
00216 
00217     G4TrackVector * secondaries = trackManager->GimmeSecondaries();
00218     switch (istop)
00219     {
00220       case fStopButAlive:
00221       case fSuspend:
00222         trackContainer->PushOneTrack( track, aTrajectory );
00223         StackTracks( secondaries );
00224         break;
00225 
00226       case fPostponeToNextEvent:
00227         trackContainer->PushOneTrack( track );
00228         StackTracks( secondaries );
00229         break;
00230 
00231       case fStopAndKill:
00232         StackTracks( secondaries );
00233         delete track;
00234         break;
00235 
00236       case fAlive:
00237         G4cout << "Illeagal TrackStatus returned from G4TrackingManager!"
00238              << G4endl;
00239       case fKillTrackAndSecondaries:
00240         //if( secondaries ) secondaries->clearAndDestroy();
00241         if( secondaries )
00242         {
00243           for(size_t i=0;i<secondaries->size();i++)
00244           { delete (*secondaries)[i]; }
00245           secondaries->clear();
00246         }
00247         delete track;
00248         break;
00249     }
00250   }
00251 
00252 #ifdef G4VERBOSE
00253   if ( verboseLevel > 0 )
00254   {
00255     G4cout << "NULL returned from G4StackManager." << G4endl;
00256     G4cout << "Terminate current event processing." << G4endl;
00257   }
00258 #endif
00259 
00260   if(sdManager)
00261   { sdManager->TerminateCurrentEvent(currentEvent->GetHCofThisEvent()); }
00262 
00263   if(userEventAction) userEventAction->EndOfEventAction(currentEvent);
00264 
00265   stateManager->SetNewState(G4State_GeomClosed);
00266   currentEvent = 0;
00267   abortRequested = false;
00268 }
00269 
00270 void G4EventManager::StackTracks(G4TrackVector *trackVector,G4bool IDhasAlreadySet)
00271 {
00272   G4Track * newTrack;
00273 
00274   if( trackVector )
00275   {
00276 
00277     size_t n_passedTrack = trackVector->size();
00278     if( n_passedTrack == 0 ) return;
00279     for( size_t i = 0; i < n_passedTrack; i++ )
00280     {
00281       newTrack = (*trackVector)[ i ];
00282       trackIDCounter++;
00283       if(!IDhasAlreadySet)
00284       {
00285         newTrack->SetTrackID( trackIDCounter );
00286         if(newTrack->GetDynamicParticle()->GetPrimaryParticle())
00287         {
00288           G4PrimaryParticle* pp
00289             = (G4PrimaryParticle*)(newTrack->GetDynamicParticle()->GetPrimaryParticle());
00290           pp->SetTrackID(trackIDCounter);
00291         }
00292       }
00293       newTrack->SetOriginTouchableHandle(newTrack->GetTouchableHandle());
00294       trackContainer->PushOneTrack( newTrack );
00295 #ifdef G4VERBOSE
00296       if ( verboseLevel > 1 )
00297       {
00298         G4cout << "A new track " << newTrack 
00299              << " (trackID " << newTrack->GetTrackID()
00300              << ", parentID " << newTrack->GetParentID() 
00301              << ") is passed to G4StackManager." << G4endl;
00302       }
00303 #endif
00304     }
00305     trackVector->clear();
00306   }
00307 }
00308 
00309 void G4EventManager::SetUserAction(G4UserEventAction* userAction)
00310 {
00311   userEventAction = userAction;
00312   if(userEventAction) userEventAction->SetEventManager(this);
00313 }
00314 
00315 void G4EventManager::SetUserAction(G4UserStackingAction* userAction)
00316 {
00317   userStackingAction = userAction;
00318   trackContainer->SetUserStackingAction(userAction);
00319 }
00320 
00321 void G4EventManager::SetUserAction(G4UserTrackingAction* userAction)
00322 {
00323   userTrackingAction = userAction;
00324   trackManager->SetUserAction(userAction);
00325 }
00326 
00327 void G4EventManager::SetUserAction(G4UserSteppingAction* userAction)
00328 {
00329   userSteppingAction = userAction;
00330   trackManager->SetUserAction(userAction);
00331 }
00332 
00333 void G4EventManager::ProcessOneEvent(G4Event* anEvent)
00334 {
00335   trackIDCounter = 0;
00336   DoProcessing(anEvent);
00337 }
00338 
00339 void G4EventManager::ProcessOneEvent(G4TrackVector* trackVector,G4Event* anEvent)
00340 {
00341   static G4String randStat;
00342   trackIDCounter = 0;
00343   G4bool tempEvent = false;
00344   if(!anEvent)
00345   {
00346     anEvent = new G4Event();
00347     tempEvent = true;
00348   }
00349   if(storetRandomNumberStatusToG4Event==1 || storetRandomNumberStatusToG4Event==3)
00350   {
00351     std::ostringstream oss;
00352     CLHEP::HepRandom::saveFullState(oss);
00353     anEvent->SetRandomNumberStatus(randStat=oss.str());
00354   }
00355   StackTracks(trackVector,false);
00356   DoProcessing(anEvent);
00357   if(tempEvent)
00358   { delete anEvent; }
00359 }
00360 
00361 void G4EventManager::SetUserInformation(G4VUserEventInformation* anInfo)
00362 { 
00363   G4StateManager* stateManager = G4StateManager::GetStateManager();
00364   G4ApplicationState currentState = stateManager->GetCurrentState();
00365   if(currentState!=G4State_EventProc || currentEvent==0)
00366   {
00367     G4Exception("G4EventManager::SetUserInformation",
00368                 "Event0003", JustWarning,
00369                 "G4VUserEventInformation cannot be set because of ansense of G4Event.");
00370     return;
00371   }
00372   
00373   currentEvent->SetUserInformation(anInfo);
00374 }
00375 
00376 G4VUserEventInformation* G4EventManager::GetUserInformation()
00377 { 
00378   G4StateManager* stateManager = G4StateManager::GetStateManager();
00379   G4ApplicationState currentState = stateManager->GetCurrentState();
00380   if(currentState!=G4State_EventProc || currentEvent==0)
00381   { return 0; }
00382   
00383   return currentEvent->GetUserInformation();
00384 }
00385 
00386 void G4EventManager::KeepTheCurrentEvent()
00387 { if(currentEvent) currentEvent->KeepTheEvent(); }
00388 
00389 void G4EventManager::AbortCurrentEvent()
00390 {
00391   abortRequested = true;
00392   trackContainer->clear();
00393   if(tracking) trackManager->EventAborted();
00394 }
00395 

Generated on Mon May 27 17:48:13 2013 for Geant4 by  doxygen 1.4.7