Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
G4RunManager.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$
28//
29//
30
31// On Sun, to prevent conflict with ObjectSpace, G4Timer.hh has to be
32// loaded *before* globals.hh...
33#include "G4Timer.hh"
34
35#include "G4RunManager.hh"
36#include "G4RunManagerKernel.hh"
37
38#include "G4StateManager.hh"
39#include "G4ApplicationState.hh"
40#include "Randomize.hh"
41#include "G4Run.hh"
42#include "G4RunMessenger.hh"
43#include "G4VUserPhysicsList.hh"
45#include "G4UserRunAction.hh"
48#include "G4ParticleTable.hh"
49#include "G4ProcessTable.hh"
50#include "G4UnitsTable.hh"
51#include "G4VVisManager.hh"
52#include "G4Material.hh"
53#include "G4SDManager.hh"
54#include "G4UImanager.hh"
55#include "G4ios.hh"
56#include <sstream>
57
58using namespace CLHEP;
59
60G4RunManager* G4RunManager::fRunManager = 0;
61
63{ return fRunManager; }
64
66:userDetector(0),physicsList(0),
67 userRunAction(0),userPrimaryGeneratorAction(0),userEventAction(0),
68 userStackingAction(0),userTrackingAction(0),userSteppingAction(0),
69 geometryInitialized(false),physicsInitialized(false),
70 runAborted(false),initializedAtLeastOnce(false),
71 geometryToBeOptimized(true),runIDCounter(0),verboseLevel(0),DCtable(0),
72 currentRun(0),currentEvent(0),n_perviousEventsToBeStored(0),
73 numberOfEventToBeProcessed(0),storeRandomNumberStatus(false),
74 storeRandomNumberStatusToG4Event(0),
75 currentWorld(0),nParallelWorlds(0),msgText(" "),n_select_msg(-1),
76 numberOfEventProcessed(0)
77{
78 if(fRunManager)
79 {
80 G4Exception("G4RunManager::G4RunManager()", "Run0031",
81 FatalException, "G4RunManager constructed twice.");
82 }
83 fRunManager = this;
84
87
88 timer = new G4Timer();
89 runMessenger = new G4RunMessenger(this);
90 previousEvents = new std::vector<G4Event*>;
94 std::ostringstream oss;
98}
99
101{
103 // set the application state to the quite state
104 if(pStateManager->GetCurrentState()!=G4State_Quit)
105 {
106 if(verboseLevel>0) G4cout << "G4 kernel has come to Quit state." << G4endl;
107 pStateManager->SetNewState(G4State_Quit);
108 }
109
110 if(currentRun) delete currentRun;
111 delete timer;
112 delete runMessenger;
115 delete previousEvents;
116 if(userDetector)
117 {
118 delete userDetector;
119 userDetector = 0;
120 if(verboseLevel>1) G4cout << "UserDetectorConstruction deleted." << G4endl;
121 }
122 if(physicsList)
123 {
124 delete physicsList;
125 physicsList = 0;
126 if(verboseLevel>1) G4cout << "UserPhysicsList deleted." << G4endl;
127 }
128 if(userRunAction)
129 {
130 delete userRunAction;
131 userRunAction = 0;
132 if(verboseLevel>1) G4cout << "UserRunAction deleted." << G4endl;
133 }
135 {
138 if(verboseLevel>1) G4cout << "UserPrimaryGenerator deleted." << G4endl;
139 }
140
141 delete kernel;
142
143 if(verboseLevel>1) G4cout << "RunManager is deleted." << G4endl;
144 fRunManager = 0;
145}
146
147void G4RunManager::BeamOn(G4int n_event,const char* macroFile,G4int n_select)
148{
150 if(cond)
151 {
155 if(n_event>0) DoEventLoop(n_event,macroFile,n_select);
157 }
158}
159
161{
163
164 G4ApplicationState currentState = stateManager->GetCurrentState();
165 if(currentState!=G4State_PreInit && currentState!=G4State_Idle)
166 {
167 G4cerr << "Illegal application state - BeamOn() ignored." << G4endl;
168 return false;
169 }
170
172 {
173 G4cerr << " Geant4 kernel should be initialized" << G4endl;
174 G4cerr << "before the first BeamOn(). - BeamOn ignored." << G4endl;
175 return false;
176 }
177
179 {
180 if(verboseLevel>0)
181 {
182 G4cout << "Start re-initialization because " << G4endl;
183 if(!geometryInitialized) G4cout << " Geometry" << G4endl;
184 if(!physicsInitialized) G4cout << " Physics processes" << G4endl;
185 G4cout << "has been modified since last Run." << G4endl;
186 }
187 Initialize();
188 }
189 return true;
190}
191
193{
194 if(!(kernel->RunInitialization())) return;
195 if(currentRun) delete currentRun;
196 currentRun = 0;
197
199 if(!currentRun) currentRun = new G4Run();
200
203
206 if(fSDM)
207 { currentRun->SetHCtable(fSDM->GetHCtable()); }
208
209 std::ostringstream oss;
213
214 previousEvents->clear();
215 for(G4int i_prev=0;i_prev<n_perviousEventsToBeStored;i_prev++)
216 { previousEvents->push_back((G4Event*)0); }
217
219
221 G4String fileN = randomNumberStatusDir + "currentRun.rndm";
223 }
224
225 runAborted = false;
227 if(verboseLevel>0) G4cout << "Start Run processing." << G4endl;
228}
229
230void G4RunManager::DoEventLoop(G4int n_event,const char* macroFile,G4int n_select)
231{
232 InitializeEventLoop(n_event,macroFile,n_select);
233
234// Event loop
235 for(G4int i_event=0; i_event<n_event; i_event++ )
236 {
237 ProcessOneEvent(i_event);
239 if(runAborted) break;
240 }
241
243}
244
245void G4RunManager::InitializeEventLoop(G4int n_event,const char* macroFile,G4int n_select)
246{
247 if(verboseLevel>0)
248 { timer->Start(); }
249
250 n_select_msg = n_select;
251 if(macroFile!=0)
252 {
253 if(n_select_msg<0) n_select_msg = n_event;
254 msgText = "/control/execute ";
255 msgText += macroFile;
256 }
257 else
258 { n_select_msg = -1; }
259}
260
262{
263 currentEvent = GenerateEvent(i_event);
268}
269
271{
273 currentEvent = 0;
275}
276
278{
279 if(verboseLevel>0)
280 {
281 timer->Stop();
282 G4cout << "Run terminated." << G4endl;
283 G4cout << "Run Summary" << G4endl;
284 if(runAborted)
285 { G4cout << " Run Aborted after " << numberOfEventProcessed << " events processed." << G4endl; }
286 else
287 { G4cout << " Number of events processed : " << numberOfEventProcessed << G4endl; }
288 G4cout << " " << *timer << G4endl;
289 }
290}
291
293{
295 {
296 G4Exception("G4RunManager::GenerateEvent()", "Run0032", FatalException,
297 "G4VUserPrimaryGeneratorAction is not defined!");
298 return 0;
299 }
300
301 G4Event* anEvent = new G4Event(i_event);
302
304 {
305 std::ostringstream oss;
309 }
310
312 G4String fileN = randomNumberStatusDir + "currentEvent.rndm";
314 }
315
317 return anEvent;
318}
319
321{
323 if(fPersM) fPersM->Store(anEvent);
324 currentRun->RecordEvent(anEvent);
325}
326
328{
329 for(size_t itr=0;itr<previousEvents->size();itr++)
330 {
331 G4Event* prevEv = (*previousEvents)[itr];
332 if((prevEv) && !(prevEv->ToBeKept())) delete prevEv;
333 }
334 previousEvents->clear();
335 for(G4int i_prev=0;i_prev<n_perviousEventsToBeStored;i_prev++)
336 { previousEvents->push_back((G4Event*)0); }
337
339
341 if(fPersM) fPersM->Store(currentRun);
342 runIDCounter++;
343
345}
346
348{
349 if(anEvent->ToBeKept()) currentRun->StoreEvent(anEvent);
350 G4Event* evt;
352 { evt = anEvent; }
353 else
354 {
355 previousEvents->insert(previousEvents->begin(),anEvent);
356 evt = previousEvents->back();
357 previousEvents->pop_back();
358 }
359 if(evt && !(evt->ToBeKept())) delete evt;
360}
361
363{
365 G4ApplicationState currentState = stateManager->GetCurrentState();
366 if(currentState!=G4State_PreInit && currentState!=G4State_Idle)
367 {
368 G4cerr << "Illegal application state - "
369 << "G4RunManager::Initialize() ignored." << G4endl;
370 return;
371 }
372
376}
377
379{
380 if(!userDetector)
381 {
382 G4Exception("G4RunManager::InitializeGeometry", "Run0033",
383 FatalException, "G4VUserDetectorConstruction is not defined!");
384 return;
385 }
386
387 if(verboseLevel>1) G4cout << "userDetector->Construct() start." << G4endl;
391 geometryInitialized = true;
392}
393
395{
396 if(physicsList)
397 {
398 if(verboseLevel>1) G4cout << "physicsList->Construct() start." << G4endl;
400 }
401 else
402 {
403 G4Exception("G4RunManager::InitializePhysics()", "Run0034",
404 FatalException, "G4VUserPhysicsList is not defined!");
405 }
406 physicsInitialized = true;
407}
408
410{
411 // This method is valid only for GeomClosed or EventProc state
412 G4ApplicationState currentState =
414 if(currentState==G4State_GeomClosed || currentState==G4State_EventProc)
415 {
416 runAborted = true;
417 if(currentState==G4State_EventProc && !softAbort)
418 {
421 }
422 }
423 else
424 {
425 G4cerr << "Run is not in progress. AbortRun() ignored." << G4endl;
426 }
427}
428
430{
431 // This method is valid only for EventProc state
432 G4ApplicationState currentState =
434 if(currentState==G4State_EventProc)
435 {
438 }
439 else
440 {
441 G4cerr << "Event is not in progress. AbortEevnt() ignored." << G4endl;
442 }
443}
444
446 G4bool topologyIsChanged)
447{
448 kernel->DefineWorldVolume(worldVol,topologyIsChanged);
449}
450
452{
453 G4int runNumber = 0;
454 if(currentRun) runNumber = currentRun->GetRunID();
456 G4cerr << "Warning from G4RunManager::rndmSaveThisRun():"
457 << " Random number status was not stored prior to this run."
458 << G4endl << "Command ignored." << G4endl;
459 return;
460 }
461
462 G4String fileIn = randomNumberStatusDir + "currentRun.rndm";
463
464 std::ostringstream os;
465 os << "run" << runNumber << ".rndm" << '\0';
466 G4String fileOut = randomNumberStatusDir + os.str();
467
468 G4String copCmd = "/control/shell cp "+fileIn+" "+fileOut;
470 if(verboseLevel>0) G4cout << "currentRun.rndm is copied to file: " << fileOut << G4endl;
471}
472
474{
476 G4cerr << "Warning from G4RunManager::rndmSaveThisEvent():"
477 << " there is no currentEvent or its RandomEngineStatus is not available."
478 << G4endl << "Command ignored." << G4endl;
479 return;
480 }
481
482 G4String fileIn = randomNumberStatusDir + "currentEvent.rndm";
483
484 std::ostringstream os;
485 os << "run" << currentRun->GetRunID() << "evt" << currentEvent->GetEventID()
486 << ".rndm" << '\0';
487 G4String fileOut = randomNumberStatusDir + os.str();
488
489 G4String copCmd = "/control/shell cp "+fileIn+" "+fileOut;
491 if(verboseLevel>0) G4cout << "currentEvent.rndm is copied to file: " << fileOut << G4endl;
492}
493
495{
496 G4String fileNameWithDirectory;
497 if(fileN.index("/")==std::string::npos)
498 { fileNameWithDirectory = randomNumberStatusDir+fileN; }
499 else
500 { fileNameWithDirectory = fileN; }
501
502 HepRandom::restoreEngineStatus(fileNameWithDirectory);
503 if(verboseLevel>0) G4cout << "RandomNumberEngineStatus restored from file: "
504 << fileNameWithDirectory << G4endl;
506}
507
508void G4RunManager::DumpRegion(const G4String& rname) const
509{
510// kernel->UpdateRegion();
511 kernel->DumpRegion(rname);
512}
513
515{
516// kernel->UpdateRegion();
517 kernel->DumpRegion(region);
518}
519
520#include "G4ScoringManager.hh"
522#include "G4VScoringMesh.hh"
523#include "G4ParticleTable.hh"
525#include "G4ProcessManager.hh"
527#include "G4HCofThisEvent.hh"
528#include "G4VHitsCollection.hh"
529
531{
533 if(!ScM) return;
534 G4int nPar = ScM->GetNumberOfMesh();
535 if(nPar<1) return;
536
537 G4ParticleTable::G4PTblDicIterator* theParticleIterator
539 for(G4int iw=0;iw<nPar;iw++)
540 {
541 G4VScoringMesh* mesh = ScM->GetMesh(iw);
542 G4VPhysicalVolume* pWorld
544 ->IsWorldExisting(ScM->GetWorldName(iw));
545 if(!pWorld)
546 {
548 ->GetParallelWorld(ScM->GetWorldName(iw));
549 pWorld->SetName(ScM->GetWorldName(iw));
550
551 G4ParallelWorldScoringProcess* theParallelWorldScoringProcess
553 theParallelWorldScoringProcess->SetParallelWorld(ScM->GetWorldName(iw));
554
555 theParticleIterator->reset();
556 while( (*theParticleIterator)() ){
557 G4ParticleDefinition* particle = theParticleIterator->value();
558 G4ProcessManager* pmanager = particle->GetProcessManager();
559 if(pmanager)
560 {
561 pmanager->AddProcess(theParallelWorldScoringProcess);
562 if(theParallelWorldScoringProcess->IsAtRestRequired(particle))
563 { pmanager->SetProcessOrdering(theParallelWorldScoringProcess, idxAtRest, 9999); }
564 pmanager->SetProcessOrderingToSecond(theParallelWorldScoringProcess, idxAlongStep);
565 pmanager->SetProcessOrdering(theParallelWorldScoringProcess, idxPostStep, 9999);
566 }
567 }
568 }
569 mesh->Construct(pWorld);
570 }
572}
573
575{
577 if(!ScM) return;
578 G4int nPar = ScM->GetNumberOfMesh();
579 if(nPar<1) return;
580
582 if(!HCE) return;
583 G4int nColl = HCE->GetCapacity();
584 for(G4int i=0;i<nColl;i++)
585 {
586 G4VHitsCollection* HC = HCE->GetHC(i);
587 if(HC) ScM->Accumulate(HC);
588 }
589}
590
591#include "G4VPhysicalVolume.hh"
592#include "G4LogicalVolume.hh"
593#include "G4SmartVoxelHeader.hh"
594
596{
597 G4LogicalVolume* pMotherL = pPhys->GetMotherLogical();
598 if(pMotherL) ReOptimize(pMotherL);
599}
600
602{
603 G4SmartVoxelHeader* header = pLog->GetVoxelHeader();
604 delete header;
605 header = new G4SmartVoxelHeader(pLog);
606 pLog->SetVoxelHeader(header);
607}
608
G4ApplicationState
@ G4State_EventProc
@ G4State_Idle
@ G4State_Quit
@ G4State_GeomClosed
@ G4State_PreInit
@ FatalException
@ idxPostStep
@ idxAtRest
@ idxAlongStep
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cerr
G4DLLIMPORT std::ostream G4cout
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:185
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:180
static void showEngineStatus()
Definition: Random.cc:203
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:175
void AbortCurrentEvent()
void ProcessOneEvent(G4Event *anEvent)
G4bool ToBeKept() const
Definition: G4Event.hh:135
void SetRandomNumberStatus(G4String &st)
Definition: G4Event.hh:123
G4HCofThisEvent * GetHCofThisEvent() const
Definition: G4Event.hh:173
void SetEventAborted()
Definition: G4Event.hh:121
G4int GetEventID() const
Definition: G4Event.hh:139
G4VHitsCollection * GetHC(G4int i)
void SetVoxelHeader(G4SmartVoxelHeader *pVoxel)
G4SmartVoxelHeader * GetVoxelHeader() const
G4bool IsAtRestRequired(G4ParticleDefinition *partDef)
void SetParallelWorld(G4String parallelWorldName)
G4ProcessManager * GetProcessManager() const
G4PTblDicIterator * GetIterator()
static G4ParticleTable * GetParticleTable()
G4UImessenger * CreateMessenger()
void SetProcessOrdering(G4VProcess *aProcess, G4ProcessVectorDoItIndex idDoIt, G4int ordDoIt=ordDefault)
void SetProcessOrderingToSecond(G4VProcess *aProcess, G4ProcessVectorDoItIndex idDoIt)
G4int AddProcess(G4VProcess *aProcess, G4int ordAtRestDoIt=ordInActive, G4int ordAlongSteptDoIt=ordInActive, G4int ordPostStepDoIt=ordInActive)
static G4ProcessTable * GetProcessTable()
G4UImessenger * CreateMessenger()
void DefineWorldVolume(G4VPhysicalVolume *worldVol, G4bool topologyIsChanged=true)
void SetNumberOfParallelWorld(G4int i)
G4EventManager * GetEventManager() const
void DumpRegion(const G4String &rname) const
G4bool geometryInitialized
G4int storeRandomNumberStatusToG4Event
virtual void AbortRun(G4bool softAbort=false)
virtual void Initialize()
G4int nParallelWorlds
G4Timer * timer
G4int n_select_msg
G4int numberOfEventProcessed
virtual void RestoreRandomNumberStatus(const G4String &fileN)
virtual void BeamOn(G4int n_event, const char *macroFile=0, G4int n_select=-1)
virtual void DoEventLoop(G4int n_event, const char *macroFile=0, G4int n_select=-1)
virtual void rndmSaveThisEvent()
G4int runIDCounter
G4RunManagerKernel * kernel
G4int verboseLevel
G4Run * currentRun
virtual G4bool ConfirmBeamOnCondition()
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:62
virtual void AbortEvent()
virtual void rndmSaveThisRun()
virtual void InitializePhysics()
G4DCtable * DCtable
G4String randomNumberStatusForThisRun
G4bool runAborted
G4String msgText
G4UserRunAction * userRunAction
virtual void DefineWorldVolume(G4VPhysicalVolume *worldVol, G4bool topologyIsChanged=true)
virtual void InitializeGeometry()
virtual void RunTermination()
G4bool physicsInitialized
void StackPreviousEvent(G4Event *anEvent)
void ReOptimize(G4LogicalVolume *)
void GeometryHasBeenModified()
G4VUserDetectorConstruction * userDetector
G4VUserPrimaryGeneratorAction * userPrimaryGeneratorAction
G4int numberOfEventToBeProcessed
virtual ~G4RunManager()
G4String randomNumberStatusDir
virtual void TerminateEventLoop()
G4String randomNumberStatusForThisEvent
G4int n_perviousEventsToBeStored
void ReOptimizeMotherOf(G4VPhysicalVolume *)
virtual void ProcessOneEvent(G4int i_event)
G4bool storeRandomNumberStatus
virtual G4Event * GenerateEvent(G4int i_event)
G4bool initializedAtLeastOnce
G4EventManager * eventManager
virtual void TerminateOneEvent()
virtual void RunInitialization()
void DumpRegion(const G4String &rname) const
G4VUserPhysicsList * physicsList
virtual void AnalyzeEvent(G4Event *anEvent)
void UpdateScoring()
G4Event * currentEvent
std::vector< G4Event * > * previousEvents
void ConstructScoringWorlds()
virtual void InitializeEventLoop(G4int n_event, const char *macroFile=0, G4int n_select=-1)
Definition: G4Run.hh:47
void SetHCtable(G4HCtable *HCtbl)
Definition: G4Run.hh:96
G4int GetRunID() const
Definition: G4Run.hh:73
void StoreEvent(G4Event *evt)
Definition: G4Run.cc:48
virtual void RecordEvent(const G4Event *)
Definition: G4Run.cc:45
void SetNumberOfEventToBeProcessed(G4int n_ev)
Definition: G4Run.hh:94
void SetRunID(G4int id)
Definition: G4Run.hh:92
void SetDCtable(G4DCtable *DCtbl)
Definition: G4Run.hh:98
void SetRandomNumberStatus(G4String &st)
Definition: G4Run.hh:100
static G4SDManager * GetSDMpointerIfExist()
Definition: G4SDManager.cc:49
G4HCtable * GetHCtable() const
Definition: G4SDManager.hh:101
G4VScoringMesh * GetMesh(G4int i) const
void Accumulate(G4VHitsCollection *map)
size_t GetNumberOfMesh() const
G4String GetWorldName(G4int i) const
static G4ScoringManager * GetScoringManagerIfExist()
G4ApplicationState GetCurrentState() const
static G4StateManager * GetStateManager()
G4bool SetNewState(G4ApplicationState requestedState)
str_size index(const char *, G4int pos=0) const
void Stop()
void Start()
G4VPhysicalVolume * GetParallelWorld(const G4String &worldName)
static G4TransportationManager * GetTransportationManager()
G4VPhysicalVolume * IsWorldExisting(const G4String &worldName)
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:369
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:51
virtual void EndOfRunAction(const G4Run *aRun)
virtual void BeginOfRunAction(const G4Run *aRun)
virtual G4Run * GenerateRun()
static G4VPersistencyManager * GetPersistencyManager()
virtual G4bool Store(const G4Event *anEvent)=0
G4LogicalVolume * GetMotherLogical() const
void SetName(const G4String &pName)
virtual void Construct(G4VPhysicalVolume *fWorldPhys)=0
virtual G4VPhysicalVolume * Construct()=0
virtual void GeneratePrimaries(G4Event *anEvent)=0
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
Definition: DoubConv.h:17