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

Last change on this file since 1107 was 1058, checked in by garnier, 15 years ago

file release beta

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