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

Last change on this file since 834 was 828, checked in by garnier, 17 years ago

import all except CVS

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