source: trunk/source/run/src/G4RunManagerKernel.cc@ 1261

Last change on this file since 1261 was 1196, checked in by garnier, 16 years ago

update CVS release candidate geant4.9.3.01

File size: 19.1 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: G4RunManagerKernel.cc,v 1.47 2009/11/13 23:13:40 asaim Exp $
28// GEANT4 tag $Name: geant4-09-03-cand-01 $
29//
30//
31
32#include "G4RunManagerKernel.hh"
33
34#include <vector>
35
36#include "G4StateManager.hh"
37#include "G4ApplicationState.hh"
38#include "G4ExceptionHandler.hh"
39#include "G4PrimaryTransformer.hh"
40#include "G4GeometryManager.hh"
41#include "G4TransportationManager.hh"
42#include "G4VPhysicalVolume.hh"
43#include "G4LogicalVolume.hh"
44#include "G4VUserPhysicsList.hh"
45#include "G4ParticleTable.hh"
46#include "G4Region.hh"
47#include "G4RegionStore.hh"
48#include "G4ProductionCuts.hh"
49#include "G4ProductionCutsTable.hh"
50#include "G4SDManager.hh"
51#include "G4UImanager.hh"
52#include "G4VVisManager.hh"
53#include "G4UnitsTable.hh"
54#include "G4Version.hh"
55#include "G4ios.hh"
56
57#ifdef G4FPE_DEBUG
58 #include "G4FPEDetection.hh"
59#endif
60
61G4RunManagerKernel* G4RunManagerKernel::fRunManagerKernel = 0;
62
63G4RunManagerKernel* G4RunManagerKernel::GetRunManagerKernel()
64{ return fRunManagerKernel; }
65
66G4RunManagerKernel::G4RunManagerKernel()
67:physicsList(0),currentWorld(0),
68 geometryInitialized(false),physicsInitialized(false),
69 geometryNeedsToBeClosed(true),geometryToBeOptimized(true),
70 physicsNeedsToBeReBuilt(true),verboseLevel(0),
71 numberOfParallelWorld(0)
72{
73#ifdef G4FPE_DEBUG
74 InvalidOperationDetection();
75#endif
76
77 defaultExceptionHandler = new G4ExceptionHandler();
78 if(fRunManagerKernel)
79 {
80 G4Exception("G4RunManagerKernel::G4RunManagerKernel()","MoreThanOneRunManager",
81 FatalException,"More than one G4RunManagerKernel is constructed.");
82 }
83 fRunManagerKernel = this;
84
85 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
86 if(particleTable->entries()>0)
87 {
88 // No particle should be registered beforehand
89 G4cerr<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<G4endl;
90 G4cerr<<" G4RunManagerKernel fatal exception"<<G4endl;
91 G4cerr<<" -- Following particles have already been registered"<<G4endl;
92 G4cerr<<" before G4RunManagerKernel is instantiated."<<G4endl;
93 for(int i=0;i<particleTable->entries();i++)
94 { G4cerr<<" "<<particleTable->GetParticle(i)->GetParticleName()<<G4endl; }
95 G4cerr<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<G4endl;
96 G4Exception("G4RunManagerKernel::G4RunManagerKernel()","StaticParticleDefinition",
97 FatalException,"Particles have already been instantiated before G4RunManagerKernel.");
98 }
99
100 // construction of Geant4 kernel classes
101 eventManager = new G4EventManager();
102 defaultRegion = new G4Region("DefaultRegionForTheWorld"); // deleted by store
103 defaultRegion->SetProductionCuts(
104 G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
105
106 // Following line is tentatively moved from SetPhysics method
107 // Commented out for introduction of non-static particle definition // G4ParticleTable::GetParticleTable()->SetReadiness();
108 // set the initial application state
109 G4StateManager::GetStateManager()->SetNewState(G4State_PreInit);
110
111 // version banner
112 G4String vs = G4Version;
113 vs = vs.substr(1,vs.size()-2);
114 versionString = " Geant4 version ";
115 versionString += vs;
116 versionString += " ";
117 versionString += G4Date;
118 G4cout << G4endl
119 << "*************************************************************" << G4endl
120 << versionString << G4endl
121 << " Copyright : Geant4 Collaboration" << G4endl
122 << " Reference : NIM A 506 (2003), 250-303" << G4endl
123 << " WWW : http://cern.ch/geant4" << G4endl
124 << "*************************************************************" << G4endl
125 << G4endl;
126}
127
128G4RunManagerKernel::~G4RunManagerKernel()
129{
130 // set the application state to the quite state
131 if(verboseLevel>0) G4cout << "G4 kernel has come to Quit state." << G4endl;
132 G4StateManager* pStateManager = G4StateManager::GetStateManager();
133 pStateManager->SetNewState(G4State_Quit);
134
135 // open geometry for deletion
136 G4GeometryManager::GetInstance()->OpenGeometry();
137
138 // deletion of Geant4 kernel classes
139 G4SDManager* fSDM = G4SDManager::GetSDMpointerIfExist();
140 if(fSDM)
141 {
142 delete fSDM;
143 if(verboseLevel>1) G4cout << "G4SDManager deleted." << G4endl;
144 }
145 delete eventManager;
146 if(verboseLevel>1) G4cout << "EventManager deleted." << G4endl;
147 G4UImanager* pUImanager = G4UImanager::GetUIpointer();
148 {
149 if(pUImanager) delete pUImanager;
150 if(verboseLevel>1) G4cout << "UImanager deleted." << G4endl;
151 }
152 G4UnitDefinition::ClearUnitsTable();
153 if(verboseLevel>1) G4cout << "Units table cleared." << G4endl;
154 delete pStateManager;
155 if(verboseLevel>1) G4cout << "StateManager deleted." << G4endl;
156 delete defaultExceptionHandler;
157 if(verboseLevel>1) G4cout << "RunManagerKernel is deleted." << G4endl;
158 fRunManagerKernel = 0;
159}
160
161void G4RunManagerKernel::DefineWorldVolume(G4VPhysicalVolume* worldVol,
162 G4bool topologyIsChanged)
163{
164 G4StateManager* stateManager = G4StateManager::GetStateManager();
165 G4ApplicationState currentState = stateManager->GetCurrentState();
166 if(!(currentState==G4State_Idle||currentState==G4State_PreInit))
167 {
168 G4Exception("G4RunManagerKernel::DefineWorldVolume",
169 "DefineWorldVolumeAtIncorrectState",
170 JustWarning,
171 "Geant4 kernel is not PreInit or Idle state : Method ignored.");
172 if(verboseLevel>1) G4cerr << "Current application state is "
173 << stateManager->GetStateString(currentState) << G4endl;
174 return;
175 }
176
177 // The world volume MUST NOT have a region defined by the user
178 if(worldVol->GetLogicalVolume()->GetRegion())
179 {
180 if(worldVol->GetLogicalVolume()->GetRegion()!=defaultRegion)
181 {
182 G4cerr << "The world volume has a user-defined region <"
183 << worldVol->GetLogicalVolume()->GetRegion()->GetName()
184 << ">." << G4endl;
185 G4Exception("G4RunManager::DefineWorldVolume",
186 "RUN:WorldHasUserDefinedRegion",
187 FatalException,
188 "World would have a default region assigned by RunManagerKernel.");
189 }
190 }
191
192 // Remove old world logical volume from the default region, if exist
193 if(defaultRegion->GetNumberOfRootVolumes())
194 {
195 if(defaultRegion->GetNumberOfRootVolumes()>size_t(1))
196 {
197 G4Exception("G4RunManager::DefineWorldVolume",
198 "DefaultRegionHasMoreThanOneVolume",
199 FatalException,
200 "Default world region should have a unique logical volume.");
201 }
202 std::vector<G4LogicalVolume*>::iterator lvItr
203 = defaultRegion->GetRootLogicalVolumeIterator();
204 defaultRegion->RemoveRootLogicalVolume(*lvItr,false);
205 if(verboseLevel>1) G4cout
206 << "Obsolete world logical volume is removed from the default region." << G4endl;
207 }
208
209 // Accept the world volume
210 currentWorld = worldVol;
211
212 // Set the default region to the world
213 G4LogicalVolume* worldLog = currentWorld->GetLogicalVolume();
214 worldLog->SetRegion(defaultRegion);
215 defaultRegion->AddRootLogicalVolume(worldLog);
216 if(verboseLevel>1) G4cout << worldLog->GetName()
217 << " is registered to the default region." << G4endl;
218
219 // Set the world volume, notify the Navigator and reset its state
220 G4TransportationManager::GetTransportationManager()
221 ->SetWorldForTracking(currentWorld);
222 if(topologyIsChanged) geometryNeedsToBeClosed = true;
223
224 // Notify the VisManager as well
225 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
226 if(pVVisManager) pVVisManager->GeometryHasChanged();
227
228 geometryInitialized = true;
229 if(physicsInitialized && currentState!=G4State_Idle)
230 { stateManager->SetNewState(G4State_Idle); }
231}
232
233void G4RunManagerKernel::SetPhysics(G4VUserPhysicsList* uPhys)
234{
235 physicsList = uPhys;
236 G4ParticleTable::GetParticleTable()->SetReadiness();
237 physicsList->ConstructParticle();
238 if(verboseLevel>2) G4ParticleTable::GetParticleTable()->DumpTable();
239 if(verboseLevel>1)
240 {
241 G4cout << "List of instantiated particles ============================================" << G4endl;
242 G4int nPtcl = G4ParticleTable::GetParticleTable()->entries();
243 for(G4int i=0;i<nPtcl;i++)
244 {
245 G4ParticleDefinition* pd = G4ParticleTable::GetParticleTable()->GetParticle(i);
246 G4cout << pd->GetParticleName() << " ";
247 if(i%10==9) G4cout << G4endl;
248 }
249 G4cout << G4endl;
250 }
251}
252
253void G4RunManagerKernel::InitializePhysics()
254{
255 G4StateManager* stateManager = G4StateManager::GetStateManager();
256 G4ApplicationState currentState = stateManager->GetCurrentState();
257 if(!(currentState==G4State_Idle||currentState==G4State_PreInit))
258 {
259 G4Exception("G4RunManagerKernel::InitializePhysics",
260 "InitializePhysicsAtIncorrectState",
261 JustWarning,
262 "Geant4 kernel is not PreInit or Idle state : Method ignored.");
263 return;
264 }
265
266 if(!physicsList)
267 {
268 G4Exception("G4RunManagerKernel::InitializePhysics",
269 "PhysicsListIsNotDefined",
270 FatalException,
271 "G4VUserPhysicsList is not defined");
272 }
273
274 //if(verboseLevel>1) G4cout << "physicsList->ConstructParticle() start." << G4endl;
275 //physicsList->ConstructParticle();
276
277 if(verboseLevel>1) G4cout << "physicsList->Construct() start." << G4endl;
278 if(numberOfParallelWorld>0) physicsList->UseCoupledTransportation();
279 physicsList->Construct();
280
281 if(verboseLevel>1) G4cout << "physicsList->CheckParticleList() start." << G4endl;
282 physicsList->CheckParticleList();
283 if(verboseLevel>1) G4cout << "physicsList->setCut() start." << G4endl;
284 physicsList->SetCuts();
285 CheckRegions();
286 physicsInitialized = true;
287 if(geometryInitialized && currentState!=G4State_Idle)
288 { stateManager->SetNewState(G4State_Idle); }
289}
290
291G4bool G4RunManagerKernel::RunInitialization()
292{
293 G4StateManager* stateManager = G4StateManager::GetStateManager();
294 G4ApplicationState currentState = stateManager->GetCurrentState();
295
296 if(!geometryInitialized)
297 {
298 G4Exception("G4RunManagerKernel::RunInitialization",
299 "GeometryHasNotYetInitialized",
300 JustWarning,
301 "Geometry has not yet initialized : method ignored.");
302 return false;
303 }
304
305 if(!physicsInitialized)
306 {
307 G4Exception("G4RunManagerKernel::RunInitialization",
308 "PhysicsHasNotYetInitialized",
309 JustWarning,
310 "Physics has not yet initialized : method ignored.");
311 return false;
312 }
313
314 if( currentState != G4State_Idle )
315 {
316 G4Exception("G4RunManagerKernel::RunInitialization",
317 "RunInitializationAtIncorrectState",
318 JustWarning,
319 "Geant4 kernel not in Idle state : method ignored.");
320 return false;
321 }
322
323 //if(numberOfParallelWorld>0)
324 //{ // Confirm G4CoupledTransportation is used
325 // if(!ConfirmCoupledTransportation())
326 // { G4Exception("G4CoupledTransportation must be used for parallel world."); }
327 //}
328
329 UpdateRegion();
330 BuildPhysicsTables();
331
332 if(geometryNeedsToBeClosed) ResetNavigator();
333
334 GetPrimaryTransformer()->CheckUnknown();
335
336 stateManager->SetNewState(G4State_GeomClosed);
337 return true;
338}
339
340void G4RunManagerKernel::RunTermination()
341{ G4StateManager::GetStateManager()->SetNewState(G4State_Idle); }
342
343void G4RunManagerKernel::ResetNavigator()
344{
345 // We have to tweak the navigator's state in case a geometry has been
346 // modified between runs. By the following calls we ensure that navigator's
347 // state is reset properly. It is required the geometry to be closed
348 // and previous optimisations to be cleared.
349
350 G4GeometryManager* geomManager = G4GeometryManager::GetInstance();
351 if(verboseLevel>1) G4cout << "Start closing geometry." << G4endl;
352 geomManager->OpenGeometry();
353 geomManager->CloseGeometry(geometryToBeOptimized, verboseLevel>1);
354
355 // Reseting Navigator has been moved to G4Eventmanager, so that resetting
356 // is now done for every event.
357 // G4ThreeVector center(0,0,0);
358 // G4Navigator* navigator =
359 // G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
360 // navigator->LocateGlobalPointAndSetup(center,0,false);
361
362 geometryNeedsToBeClosed = false;
363}
364
365void G4RunManagerKernel::UpdateRegion()
366{
367 G4StateManager* stateManager = G4StateManager::GetStateManager();
368 G4ApplicationState currentState = stateManager->GetCurrentState();
369 if( currentState != G4State_Idle )
370 {
371 G4Exception("G4RunManagerKernel::UpdateRegion",
372 "RegionUpdateAtIncorrectState",
373 JustWarning,
374 "Geant4 kernel not in Idle state : method ignored.");
375 return;
376 }
377
378 CheckRegions();
379 G4RegionStore::GetInstance()->UpdateMaterialList(currentWorld);
380 G4ProductionCutsTable::GetProductionCutsTable()->UpdateCoupleTable(currentWorld);
381}
382
383void G4RunManagerKernel::BuildPhysicsTables()
384{
385 if(G4ProductionCutsTable::GetProductionCutsTable()->IsModified()
386 || physicsNeedsToBeReBuilt)
387 {
388 physicsList->BuildPhysicsTable();
389 G4ProductionCutsTable::GetProductionCutsTable()->PhysicsTableUpdated();
390 physicsNeedsToBeReBuilt = false;
391 }
392
393 if(verboseLevel>1) DumpRegion();
394 if(verboseLevel>0) physicsList->DumpCutValuesTable();
395 physicsList->DumpCutValuesTableIfRequested();
396}
397
398void G4RunManagerKernel::CheckRegions()
399{
400 G4TransportationManager* transM = G4TransportationManager::GetTransportationManager();
401 size_t nWorlds = transM->GetNoWorlds();
402 std::vector<G4VPhysicalVolume*>::iterator wItr;
403 for(size_t i=0;i<G4RegionStore::GetInstance()->size();i++)
404 {
405 G4Region* region = (*(G4RegionStore::GetInstance()))[i];
406
407 //Let each region have a pointer to the world volume where it belongs to.
408 //G4Region::SetWorld() checks if the region belongs to the given world and set it
409 //only if it does. Thus, here we go through all the registered world volumes.
410 wItr = transM->GetWorldsIterator();
411 for(size_t iw=0;iw<nWorlds;iw++)
412 {
413 region->SetWorld(*wItr);
414 wItr++;
415 }
416
417 //Now check for the regions which belongs to the tracking world
418 if(region->GetWorldPhysical()!=currentWorld) continue;
419
420 G4ProductionCuts* cuts = region->GetProductionCuts();
421 if(!cuts)
422 {
423 G4cerr << "Warning : Region <" << region->GetName()
424 << "> does not have specific production cuts," << G4endl
425 << "even though it appears in the current tracking world." << G4endl;
426 G4cerr << "Default cuts are used for this region." << G4endl;
427 region->SetProductionCuts(
428 G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
429 }
430 }
431}
432
433void G4RunManagerKernel::DumpRegion(G4String rname) const
434{
435 G4Region* region = G4RegionStore::GetInstance()->GetRegion(rname);
436 if(region) DumpRegion(region);
437}
438
439void G4RunManagerKernel::DumpRegion(G4Region* region) const
440{
441 if(!region)
442 {
443 for(size_t i=0;i<G4RegionStore::GetInstance()->size();i++)
444 { DumpRegion((*(G4RegionStore::GetInstance()))[i]); }
445 }
446 else
447 {
448 G4cout << G4endl;
449 G4cout << "Region <" << region->GetName() << ">";
450 if(region->GetWorldPhysical())
451 {
452 G4cout << " -- appears in <"
453 << region->GetWorldPhysical()->GetName() << "> world volume";
454 }
455 else
456 { G4cout << " -- is not associated to any world."; }
457 G4cout << G4endl;
458
459 G4cout << " Root logical volume(s) : ";
460 size_t nRootLV = region->GetNumberOfRootVolumes();
461 std::vector<G4LogicalVolume*>::iterator lvItr = region->GetRootLogicalVolumeIterator();
462 for(size_t j=0;j<nRootLV;j++)
463 { G4cout << (*lvItr)->GetName() << " "; lvItr++; }
464 G4cout << G4endl;
465
466 G4cout << " Pointers : G4VUserRegionInformation[" << region->GetUserInformation()
467 << "], G4UserLimits[" << region->GetUserLimits()
468 << "], G4FastSimulationManager[" << region->GetFastSimulationManager()
469 << "], G4UserSteppingAction[" << region->GetRegionalSteppingAction() << "]" << G4endl;
470
471 if(region->GetWorldPhysical()!=currentWorld)
472 {
473 G4cout << G4endl;
474 return;
475 }
476 G4cout << " Materials : ";
477 std::vector<G4Material*>::const_iterator mItr = region->GetMaterialIterator();
478 size_t nMaterial = region->GetNumberOfMaterials();
479 for(size_t iMate=0;iMate<nMaterial;iMate++)
480 {
481 G4cout << (*mItr)->GetName() << " ";
482 mItr++;
483 }
484 G4cout << G4endl;
485 G4ProductionCuts* cuts = region->GetProductionCuts();
486 if(!cuts)
487 {
488 G4cerr << "Warning : Region <" << region->GetName()
489 << "> does not have specific production cuts." << G4endl;
490 G4cerr << "Default cuts are used for this region." << G4endl;
491 region->SetProductionCuts(
492 G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
493 }
494 G4cout << " Production cuts : "
495 << " gamma " << G4BestUnit(cuts->GetProductionCut("gamma"),"Length")
496 << " e- " << G4BestUnit(cuts->GetProductionCut("e-"),"Length")
497 << " e+ " << G4BestUnit(cuts->GetProductionCut("e+"),"Length")
498 << " proton " << G4BestUnit(cuts->GetProductionCut("proton"),"Length")
499 << G4endl;
500 }
501}
502
503#include "G4ParticleTable.hh"
504#include "G4ParticleDefinition.hh"
505#include "G4ProcessManager.hh"
506#include "G4ProcessVector.hh"
507#include "G4VProcess.hh"
508G4bool G4RunManagerKernel::ConfirmCoupledTransportation()
509{
510 G4ParticleTable* theParticleTable = G4ParticleTable::GetParticleTable();
511 G4ParticleTable::G4PTblDicIterator* theParticleIterator = theParticleTable->GetIterator();
512 theParticleIterator->reset();
513 if((*theParticleIterator)())
514 {
515 G4ParticleDefinition* pd = theParticleIterator->value();
516 G4ProcessManager* pm = pd->GetProcessManager();
517 G4ProcessVector* pv = pm->GetAlongStepProcessVector(typeDoIt);
518 G4VProcess* p = (*pv)[0];
519 return ( (p->GetProcessName()) == "CoupledTransportation" );
520 }
521 return false;
522}
523
Note: See TracBrowser for help on using the repository browser.