G4RunManager.hh

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 // class description:
00032 //
00033 //      This is a class for run control in GEANT4
00034 // 
00035 //     User must provide his own classes derived from the following
00036 //     three abstract classes and register them to the RunManager. 
00037 //        G4VUserDetectorConstruction       - Detector Geometry, Materials
00038 //        G4VUserPhysicsList                - Particle types and Processes 
00039 //        G4VUserPrimaryGeneratorAction     - Event Generator selection
00040 // 
00041 //     In addition to the above mandatory classes, user can easily 
00042 //     customize of the default functionality of GEANT4 simulation
00043 //     by making his own classes derived from the following 5 user
00044 //     action classes. 
00045 //         G4UserRunAction                   - Actions for each Run
00046 //         G4UserEventAction                 - Actions for each Event
00047 //         G4UserStackingAction              - Tracks Stacking selection
00048 //         G4UserTrackingAction              - Actions for each Track
00049 //         G4UserSteppingAction              - Actions for each Step
00050 //     
00051 //     G4RunManager is the only manager class in Geant4 kernel which 
00052 //     the user MUST construct an object by him/herself in the main(). 
00053 //     Also, G4RunManager is the only manager class in Geant4 kernel
00054 //     which the user CAN derive it to costomize the behavior of the
00055 //     run control. For this case, user should use protected methods
00056 //     provided in this class for procedures he/she does not want to
00057 //     change.
00058 //
00059 //     G4RunManager or the derived class of it MUST be a singleton.
00060 //     The user MUST NOT construct more than one object even if there
00061 //     are two different concrete implementations.
00062 //
00063 //     G4RunManager controls all of state changes. See G4ApplicationState.hh
00064 //     in intercoms category for the meanings of each state.
00065 //
00066 
00067 #ifndef G4RunManager_h
00068 #define G4RunManager_h 1
00069 
00070 // userAction classes
00071 class G4VUserDetectorConstruction;
00072 class G4VUserPhysicsList;
00073 class G4UserRunAction;
00074 class G4VUserPrimaryGeneratorAction;
00075 class G4UserEventAction;
00076 class G4UserStackingAction;
00077 class G4UserTrackingAction;
00078 class G4UserSteppingAction;
00079 
00080 class G4VPhysicalVolume;
00081 class G4LogicalVolume;
00082 class G4Region;
00083 class G4Timer;
00084 class G4RunMessenger;
00085 class G4DCtable;
00086 class G4Run;
00087 class G4PrimaryTransformer;
00088 
00089 #include "G4RunManagerKernel.hh"
00090 #include "G4Event.hh"
00091 #include "G4EventManager.hh"
00092 #include "globals.hh"
00093 #include <vector>
00094 #include <algorithm>
00095 
00096 class G4RunManager
00097 {
00098   public: // with description
00099     static G4RunManager* GetRunManager();
00100     //  Static method which returns the singleton pointer of G4RunManager or
00101     // its derived class.
00102 
00103   private:
00104     static G4RunManager* fRunManager;
00105 
00106   public: // with description
00107     G4RunManager();
00108     virtual ~G4RunManager();
00109     //  The constructor and the destructor. The user must construct this class
00110     // object at the beginning of his/her main() and must delete it at the 
00111     // bottom of the main().
00112 
00113   public: // with description
00114     virtual void BeamOn(G4int n_event,const char* macroFile=0,G4int n_select=-1);
00115     //  This method starts an event loof of "n_event" events. The condition of Geant4
00116     // is examined before starting the event loop. This method must be invoked at
00117     // Idle state. The state will be changed to GeomClosed during the event loop and
00118     // will go back to Idle when the loop is over or aborted.
00119     //  In case a string "macroFile" which represents the name of a macro file is given,
00120     // this macro file will be executed AT THE END of each event processing. In case
00121     // "n_select" is greater than zero, at the ond of first "n_select" events the macro
00122     // file is executed.
00123     virtual void Initialize();
00124     //  This method invokes all the necessary initialization procedures for an event
00125     // loop. This method must be invoked at the Geant4 state of PreInit or Idle. The
00126     // state will be changed to Init during the initialization procedures and then
00127     // changed to Idle.
00128     //  This method invokes two protected methods, InitializeGeometry() and
00129     // InitializePhysics().
00130     //  After some event loops, the user can invoke this method once again. It is
00131     // required if the user changes geometry, physics process, and/or cut off value.
00132     // If the user forget the second invokation, G4RunManager will invoke BeamOn()
00133     // method will invoke this method. (Note that this feature is not valid for the
00134     // first initialization.)
00135     virtual void DefineWorldVolume(G4VPhysicalVolume * worldVol,
00136                                    G4bool topologyIsChanged=true);
00137     //  This method must be invoked if the geometry setup has been changed between
00138     // runs. The flag 'topologyIsChanged' will specify if the geometry topology is
00139     // different from the original one used in the previous run; if not, it must be
00140     // set to false, so that the original optimisation and navigation history is
00141     // preserved. This method is invoked also at initialisation.
00143     //  Resets the state of the navigator for tracking; needed for geometry updates.
00144     // It forces the optimisation and navigation history to be reset.
00145     virtual void AbortRun(G4bool softAbort=false);
00146     //  This method safely aborts the current event loop even if an event is in progress.
00147     // This method is available for Geant4 states of GeomClosed and EventProc. The state
00148     // will be changed to Idle, so that another event loop can be done.
00149     //  If softAbort is true, the event loop is aborted after processing the current
00150     // event, while the current event is aborted if it is false.
00151     virtual void AbortEvent();
00152     //  This method aborts the currently processing event, remaining events in the
00153     // current event loop will be processed. This method is available only for
00154     // EventProc state.
00155 
00156   public: // with description
00157 
00158     virtual void InitializeGeometry();
00159     virtual void InitializePhysics();
00160     //  These protected methods are invoked from Initialize() method for the 
00161     // initializations of geometry and physics processes. The user's concrete
00162     // G4VUserDetectorConstruction class will be accessed from InitializeGeometry() and
00163     // G4VUserPhysicsList class will be accessed from InitializePhysics().
00164 
00165     virtual G4bool ConfirmBeamOnCondition();
00166     virtual void RunInitialization();
00167     virtual void DoEventLoop(G4int n_event,const char* macroFile=0,G4int n_select=-1);
00168     virtual void RunTermination();
00169     //  These four protected methods are invoked from BeamOn() method. These four methods
00170     // are invoked in this order.
00171     //  ConfirmBeamOnCondition() method checks if all the necessary initializations have
00172     // already done. If the condition is not satisfied, false is returned and the follwing
00173     // three methods will be skipped.
00174     //  RunInitialization() method initializes a run. For example, a G4Run class object 
00175     // is constructed in this method.
00176     //  DoEventLoop() method control an event loop. Arguments are same as BeamOn() method.
00177     // Inide the event loop, two following protected methods are invoked at the begining
00178     // and the end of each event. 
00179     //  RunTermination() method terminates a run processing. For example, a G4Run class
00180     // object is deleted in this class. If the user uses ODBMS and wants to store the
00181     // G4Run class object, he/she must override this method.
00182 
00183     virtual void InitializeEventLoop(G4int n_event,const char* macroFile=0,G4int n_select=-1);
00184     virtual void ProcessOneEvent(G4int i_event);
00185     virtual void TerminateOneEvent();
00186     virtual void TerminateEventLoop();
00187     //  Granular virtual methods invoked from DoEventLoop() method.
00188 
00190     //  This method is invoked from RunInitialization() to create physics tables.
00191 
00192     virtual G4Event* GenerateEvent(G4int i_event);
00193     virtual void AnalyzeEvent(G4Event* anEvent);
00194     //  These two protected methods are invoked from DoEventLoop() method at the begining
00195     // and the end of each event processing.
00196     //  GenerateEvent() method constructs a G4Event class object and invoke the user's
00197     // G4VUserPrimaryGeneratorAction concrete class. If the user is using ODBMS and event 
00198     // objects have been created and stored in the data base, he/she must override this
00199     // method.
00200     //  AnalyzeEvent() stores an event to a data base if a concrete G4VPersistentManager
00201     // class is defined.
00202 
00203   public: // with description
00205     // Update region list. 
00206     // This method is mandatory before invoking following two dump methods.
00207     // At RunInitialization(), this method is automatically invoked, and thus
00208     // the user needs not invoke.
00209 
00210     void DumpRegion(const G4String& rname) const;
00211     // Dump information of a region.
00212 
00213     void DumpRegion(G4Region* region=0) const;
00214     // Dump information of a region.
00215     // If the pointer is NULL, all regions are shown.
00216 
00217   protected:
00218     void StackPreviousEvent(G4Event* anEvent);
00219 
00220   protected:
00221     G4RunManagerKernel * kernel;
00222     G4EventManager * eventManager;
00223 
00224     G4VUserDetectorConstruction * userDetector;
00225     G4VUserPhysicsList * physicsList;
00226     G4UserRunAction * userRunAction;
00227     G4VUserPrimaryGeneratorAction * userPrimaryGeneratorAction;
00228     G4UserEventAction * userEventAction;
00229     G4UserStackingAction * userStackingAction;
00230     G4UserTrackingAction * userTrackingAction;
00231     G4UserSteppingAction * userSteppingAction;
00232 
00233   private:
00234     G4RunMessenger* runMessenger;
00235 
00236   protected:
00237     G4bool geometryInitialized;
00238     G4bool physicsInitialized;
00239     G4bool runAborted;
00240     G4bool initializedAtLeastOnce;
00241     G4bool geometryToBeOptimized;
00242 
00243     G4int runIDCounter;
00244     G4int verboseLevel;
00245     G4Timer * timer;
00246     G4DCtable* DCtable;
00247 
00248     G4Run* currentRun;
00249     G4Event* currentEvent;
00250     std::vector<G4Event*>* previousEvents;
00251     G4int n_perviousEventsToBeStored;
00252     G4int numberOfEventToBeProcessed;
00253 
00254     G4bool storeRandomNumberStatus;
00255     G4int storeRandomNumberStatusToG4Event;
00256     G4String randomNumberStatusDir;
00257     G4String randomNumberStatusForThisRun;
00258     G4String randomNumberStatusForThisEvent;
00259 
00260     G4VPhysicalVolume* currentWorld;
00261 
00262     G4int nParallelWorlds;
00263 
00264     G4String msgText;
00265     G4int n_select_msg;
00266     G4int numberOfEventProcessed;
00267 
00268   public:
00269     virtual void rndmSaveThisRun();
00270     virtual void rndmSaveThisEvent();
00271     virtual void RestoreRandomNumberStatus(const G4String& fileN);
00272 
00273   public: // with description
00274     inline void SetUserInitialization(G4VUserDetectorConstruction* userInit)
00275     { userDetector = userInit; }
00276     inline void SetUserInitialization(G4VUserPhysicsList* userInit)
00277     {
00278       physicsList = userInit;
00279       kernel->SetPhysics(userInit);
00280     }
00281     inline void SetUserAction(G4UserRunAction* userAction)
00282     { userRunAction = userAction; }
00283     inline void SetUserAction(G4VUserPrimaryGeneratorAction* userAction)
00284     { userPrimaryGeneratorAction = userAction; }
00285     inline void SetUserAction(G4UserEventAction* userAction)
00286     { 
00287       eventManager->SetUserAction(userAction); 
00288       userEventAction = userAction;
00289     }
00290     inline void SetUserAction(G4UserStackingAction* userAction)
00291     { 
00292       eventManager->SetUserAction(userAction); 
00293       userStackingAction = userAction;
00294     }
00295     inline void SetUserAction(G4UserTrackingAction* userAction)
00296     { 
00297       eventManager->SetUserAction(userAction); 
00298       userTrackingAction = userAction;
00299     }
00300     inline void SetUserAction(G4UserSteppingAction* userAction)
00301     { 
00302       eventManager->SetUserAction(userAction); 
00303       userSteppingAction = userAction;
00304     }
00305     //  These methods store respective user initialization and action classes.
00306     inline const G4VUserDetectorConstruction* GetUserDetectorConstruction() const
00307     { return userDetector; }
00308     inline const G4VUserPhysicsList* GetUserPhysicsList() const
00309     { return physicsList; }
00310     inline const G4UserRunAction* GetUserRunAction() const
00311     { return userRunAction; }
00312     inline const G4VUserPrimaryGeneratorAction* GetUserPrimaryGeneratorAction() const
00313     { return userPrimaryGeneratorAction; }
00314     inline const G4UserEventAction* GetUserEventAction() const
00315     { return userEventAction; }
00316     inline const G4UserStackingAction* GetUserStackingAction() const
00317     { return userStackingAction; }
00318     inline const G4UserTrackingAction* GetUserTrackingAction() const
00319     { return userTrackingAction; }
00320     inline const G4UserSteppingAction* GetUserSteppingAction() const
00321     { return userSteppingAction; }
00322     //  These methods returns respective user initialization and action classes.
00323      
00324     inline void SetNumberOfAdditionalWaitingStacks(G4int iAdd)
00325     { eventManager->SetNumberOfAdditionalWaitingStacks(iAdd); }
00326       //  Set the number of additional (optional) waiting stacks.
00327       // This method must be invoked at PreInit, Init or Idle states.
00328       // Once the user set the number of additional waiting stacks,
00329       // he/she can use the corresponding ENUM in G4ClassificationOfNewTrack.
00330 
00331     inline const G4String& GetVersionString() const
00332     { return kernel->GetVersionString(); }
00333 
00334     inline void SetPrimaryTransformer(G4PrimaryTransformer* pt)
00335     { kernel->SetPrimaryTransformer(pt); }
00336 
00337     inline void StoreRandomNumberStatusToG4Event(G4int vl)
00338       // if vl = 1 : status before primary particle generation is stored
00339       // if vl = 2 : status before event processing (after primary particle generation) is stored
00340       // if vl = 3 : both are stored
00341       // if vl = 0 : none is stored (default)
00342     { 
00343       storeRandomNumberStatusToG4Event = vl;
00344       eventManager->StoreRandomNumberStatusToG4Event(vl);
00345     }
00346     inline G4int GetFlagRandomNumberStatusToG4Event() const
00347     { return storeRandomNumberStatusToG4Event; }
00348 
00349   public:
00350     inline void SetRandomNumberStore(G4bool flag)
00351     { storeRandomNumberStatus = flag; }
00352     inline G4bool GetRandomNumberStore() const
00353     { return storeRandomNumberStatus; }
00354     inline void SetRandomNumberStoreDir(const G4String& dir)
00355     { 
00356       G4String dirStr = dir;
00357       if( dirStr(dirStr.length()-1) != '/' ) dirStr += "/";
00358 #ifndef WIN32
00359       G4String shellCmd = "mkdir -p ";
00360 #else
00361       std::replace(dirStr.begin(), dirStr.end(),'/','\\');
00362       G4String shellCmd = "if not exist " + dirStr + " mkdir ";
00363 #endif
00364       shellCmd += dirStr;
00365       randomNumberStatusDir = dirStr;
00366       G4int sysret = system(shellCmd);
00367       if(sysret!=0)
00368       { 
00369         G4String errmsg = "\"" + shellCmd + "\" returns non-zero value. Directory creation failed.";
00370         G4Exception("GrRunManager::SetRandomNumberStoreDir","Run0071",JustWarning,errmsg);
00371         G4cerr << " return value = " << sysret << G4endl;
00372       }
00373     }
00374     inline const G4String& GetRandomNumberStoreDir() const
00375     { return randomNumberStatusDir; }
00376     inline const G4String& GetRandomNumberStatusForThisRun() const
00377     { return randomNumberStatusForThisRun; }
00378     inline const G4String& GetRandomNumberStatusForThisEvent() const
00379     {
00380       if(storeRandomNumberStatusToG4Event==0 || storeRandomNumberStatusToG4Event==2)
00381       { G4Exception("GrRunManager::SetRandomNumberStoreDir",
00382                     "Run0072",JustWarning,
00383                     "Random number status is not available for this event."); }
00384       return randomNumberStatusForThisEvent;
00385     }
00386 
00387   public: // with description
00388     inline void GeometryHasBeenModified()
00389     { kernel->GeometryHasBeenModified(); }
00390     //  This method must be invoked (or equivalent UI command can be used)
00391     // in case the user changes his/her detector geometry after
00392     // Initialize() metho has been invoked. Then, at the begining of the next BeamOn(),
00393     // all necessary re-initialization will be done.
00394 
00395     inline void PhysicsHasBeenModified()
00396     { kernel->PhysicsHasBeenModified(); }
00397     //  This method must be invoked (or equivalent UI command can be used)
00398     // in case the user changes his/her physics process(es), e.g. (in)activate 
00399     // some processes. Once this method is invoked, regardless of cuts are 
00400     // changed or not, BuildPhysicsTable() of PhysicsList is invoked for 
00401     // refreshing all physics tables.
00402 
00403     inline void CutOffHasBeenModified()
00404     {
00405       G4cerr << "CutOffHasBeenModified becomes obsolete." << G4endl;
00406       G4cerr << "It is safe to remove invoking this method." << G4endl;
00407     }  
00408 
00409   public: // with description
00410     void ReOptimizeMotherOf(G4VPhysicalVolume*);
00411     //  This method may be used if the orientation and/or size of this
00412     // particular physical volume has been modified while rest of the
00413     // geometries in the world has not been changed. This avoids the
00414     // full re-optimization of the entire geometry tree which is forced
00415     // if GeometryHasBeenModified() method is invoked.
00416 
00417     void ReOptimize(G4LogicalVolume*);
00418     //  Same as above, but the mother logical volume is specified.
00419 
00420   public:
00421     inline void SetVerboseLevel(G4int vl)
00422     { verboseLevel = vl; 
00423       kernel->SetVerboseLevel(vl); }
00424     inline G4int GetVerboseLevel() const
00425     { return verboseLevel; }
00426 
00427     inline void SetGeometryToBeOptimized(G4bool vl)
00428     { 
00429       if(geometryToBeOptimized != vl)
00430       {
00431         geometryToBeOptimized = vl;
00432         kernel->GeometryHasBeenModified();
00433         kernel->SetGeometryToBeOptimized(vl);
00434       }
00435     }
00436     inline G4bool GetGeometryToBeOptimized()
00437     { return geometryToBeOptimized; }
00438 
00439   public: // with description
00440     inline void SetNumberOfEventsToBeStored(G4int val)
00441     { n_perviousEventsToBeStored = val; }
00442     //  Sets the number of events to be kept after processing. That is, "val" previous
00443     // events can be used with the most recent event for digitizing pileup. "val"+1
00444     // previous event is deleted.
00445     //  This method must be invoked before starting the event loop.
00446     inline const G4Run* GetCurrentRun() const
00447     { return currentRun; }
00448     //  Returns the pointer to the current run. This method is available for Geant4
00449     // states of GeomClosed and EventProc.
00450     inline const G4Event* GetCurrentEvent() const
00451     { return currentEvent; }
00452     //  Returns the pointer to the current event. This method is available for EventProc
00453     // state.
00454     inline const G4Event* GetPreviousEvent(G4int i) const
00455     {
00456       if(i>=1 && i<=n_perviousEventsToBeStored)
00457       { return (*previousEvents)[i-1]; }
00458       return 0;
00459     }
00460     //  Returns the pointer to the "i" previous event. This method is availavle for
00461     // EventProc state. In case the event loop has not yet to reach to the requested
00462     // event, null will be returned. To use this method, SetNumberOfEventsToBeStored()
00463     // method mentioned above must be invoked previously to the event loop.
00464     inline void SetRunIDCounter(G4int i)
00465     { runIDCounter = i; }
00466     //  Set the run number counter. Initially, the counter is initialized to zero and
00467     // incremented by one for every BeamOn().
00468 
00469   public:
00470     inline G4int GetNumberOfParallelWorld() const
00471     { return nParallelWorlds; }
00472     inline void SetNumberOfEventsToBeProcessed(G4int val)
00473     { numberOfEventToBeProcessed = val; }
00474     inline void SetDCtable(G4DCtable* DCtbl)
00475     { DCtable = DCtbl; }
00476 
00477   public:
00478     void ConstructScoringWorlds();
00479   protected:
00480     void UpdateScoring();
00481 };
00482 
00483 #endif
00484 

Generated on Mon May 27 17:49:47 2013 for Geant4 by  doxygen 1.4.7