00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "G4RunManagerKernel.hh"
00032
00033 #include <vector>
00034
00035 #include "G4StateManager.hh"
00036 #include "G4ApplicationState.hh"
00037 #include "G4ExceptionHandler.hh"
00038 #include "G4PrimaryTransformer.hh"
00039 #include "G4GeometryManager.hh"
00040 #include "G4TransportationManager.hh"
00041 #include "G4VPhysicalVolume.hh"
00042 #include "G4LogicalVolume.hh"
00043 #include "G4VUserPhysicsList.hh"
00044 #include "G4ParticleTable.hh"
00045 #include "G4Region.hh"
00046 #include "G4RegionStore.hh"
00047 #include "G4ProductionCuts.hh"
00048 #include "G4ProductionCutsTable.hh"
00049 #include "G4SDManager.hh"
00050 #include "G4UImanager.hh"
00051 #include "G4VVisManager.hh"
00052 #include "G4UnitsTable.hh"
00053 #include "G4Version.hh"
00054 #include "G4ios.hh"
00055
00056 #ifdef G4FPE_DEBUG
00057 #include "G4FPEDetection.hh"
00058 #endif
00059
00060 G4RunManagerKernel* G4RunManagerKernel::fRunManagerKernel = 0;
00061
00062 G4RunManagerKernel* G4RunManagerKernel::GetRunManagerKernel()
00063 { return fRunManagerKernel; }
00064
00065 G4RunManagerKernel::G4RunManagerKernel()
00066 :physicsList(0),currentWorld(0),
00067 geometryInitialized(false),physicsInitialized(false),
00068 geometryNeedsToBeClosed(true),geometryToBeOptimized(true),
00069 physicsNeedsToBeReBuilt(true),verboseLevel(0),
00070 numberOfParallelWorld(0)
00071 {
00072 #ifdef G4FPE_DEBUG
00073 InvalidOperationDetection();
00074 #endif
00075
00076 defaultExceptionHandler = new G4ExceptionHandler();
00077 if(fRunManagerKernel)
00078 {
00079 G4Exception("G4RunManagerKernel::G4RunManagerKernel()","Run0001",
00080 FatalException,"More than one G4RunManagerKernel is constructed.");
00081 }
00082 fRunManagerKernel = this;
00083
00084 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
00085 if(particleTable->entries()>0)
00086 {
00087
00088 G4ExceptionDescription ED;
00089 ED<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<G4endl;
00090 ED<<" G4RunManagerKernel fatal exception"<<G4endl;
00091 ED<<" -- Following particles have already been registered"<<G4endl;
00092 ED<<" before G4RunManagerKernel is instantiated."<<G4endl;
00093 for(int i=0;i<particleTable->entries();i++)
00094 { ED<<" "<<particleTable->GetParticle(i)->GetParticleName()<<G4endl; }
00095 ED<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<G4endl;
00096 G4Exception("G4RunManagerKernel::G4RunManagerKernel()","Run0002",
00097 FatalException,ED);
00098 }
00099
00100
00101 eventManager = new G4EventManager();
00102 defaultRegion = new G4Region("DefaultRegionForTheWorld");
00103 defaultRegionForParallelWorld = new G4Region("DefaultRegionForParallelWorld");
00104 defaultRegion->SetProductionCuts(
00105 G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
00106 defaultRegionForParallelWorld->SetProductionCuts(
00107 G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
00108
00109
00110
00111
00112 G4StateManager::GetStateManager()->SetNewState(G4State_PreInit);
00113
00114
00115 G4String vs = G4Version;
00116 vs = vs.substr(1,vs.size()-2);
00117 versionString = " Geant4 version ";
00118 versionString += vs;
00119 versionString += " ";
00120 versionString += G4Date;
00121 G4cout << G4endl
00122 << "*************************************************************" << G4endl
00123 << versionString << G4endl
00124 << " Copyright : Geant4 Collaboration" << G4endl
00125 << " Reference : NIM A 506 (2003), 250-303" << G4endl
00126 << " WWW : http://cern.ch/geant4" << G4endl
00127 << "*************************************************************" << G4endl
00128 << G4endl;
00129 }
00130
00131 G4RunManagerKernel::~G4RunManagerKernel()
00132 {
00133 G4StateManager* pStateManager = G4StateManager::GetStateManager();
00134
00135 if(pStateManager->GetCurrentState()!=G4State_Quit)
00136 {
00137 if(verboseLevel>0) G4cout << "G4 kernel has come to Quit state." << G4endl;
00138 pStateManager->SetNewState(G4State_Quit);
00139 }
00140
00141
00142 G4GeometryManager::GetInstance()->OpenGeometry();
00143
00144
00145 G4SDManager* fSDM = G4SDManager::GetSDMpointerIfExist();
00146 if(fSDM)
00147 {
00148 delete fSDM;
00149 if(verboseLevel>1) G4cout << "G4SDManager deleted." << G4endl;
00150 }
00151 delete eventManager;
00152 if(verboseLevel>1) G4cout << "EventManager deleted." << G4endl;
00153 G4UImanager* pUImanager = G4UImanager::GetUIpointer();
00154 {
00155 if(pUImanager) delete pUImanager;
00156 if(verboseLevel>1) G4cout << "UImanager deleted." << G4endl;
00157 }
00158 G4UnitDefinition::ClearUnitsTable();
00159 if(verboseLevel>1) G4cout << "Units table cleared." << G4endl;
00160 delete pStateManager;
00161 if(verboseLevel>1) G4cout << "StateManager deleted." << G4endl;
00162 delete defaultExceptionHandler;
00163 if(verboseLevel>1) G4cout << "RunManagerKernel is deleted." << G4endl;
00164 fRunManagerKernel = 0;
00165 }
00166
00167 void G4RunManagerKernel::DefineWorldVolume(G4VPhysicalVolume* worldVol,
00168 G4bool topologyIsChanged)
00169 {
00170 G4StateManager* stateManager = G4StateManager::GetStateManager();
00171 G4ApplicationState currentState = stateManager->GetCurrentState();
00172 if(!(currentState==G4State_Idle||currentState==G4State_PreInit))
00173 {
00174 G4Exception("G4RunManagerKernel::DefineWorldVolume",
00175 "Run00031",
00176 JustWarning,
00177 "Geant4 kernel is not PreInit or Idle state : Method ignored.");
00178 return;
00179 }
00180
00181
00182 if(worldVol->GetLogicalVolume()->GetRegion())
00183 {
00184 if(worldVol->GetLogicalVolume()->GetRegion()!=defaultRegion)
00185 {
00186 G4ExceptionDescription ED;
00187 ED << "The world volume has a user-defined region <"
00188 << worldVol->GetLogicalVolume()->GetRegion()->GetName()
00189 << ">." << G4endl;
00190 ED << "World would have a default region assigned by RunManagerKernel."
00191 << G4endl;
00192 G4Exception("G4RunManager::DefineWorldVolume",
00193 "Run0004", FatalException, ED);
00194 }
00195 }
00196
00197
00198 if(defaultRegion->GetNumberOfRootVolumes())
00199 {
00200 if(defaultRegion->GetNumberOfRootVolumes()>size_t(1))
00201 {
00202 G4Exception("G4RunManager::DefineWorldVolume",
00203 "Run0005",
00204 FatalException,
00205 "Default world region should have a unique logical volume.");
00206 }
00207 std::vector<G4LogicalVolume*>::iterator lvItr
00208 = defaultRegion->GetRootLogicalVolumeIterator();
00209 defaultRegion->RemoveRootLogicalVolume(*lvItr,false);
00210 if(verboseLevel>1) G4cout
00211 << "Obsolete world logical volume is removed from the default region." << G4endl;
00212 }
00213
00214
00215 currentWorld = worldVol;
00216
00217
00218 G4LogicalVolume* worldLog = currentWorld->GetLogicalVolume();
00219 worldLog->SetRegion(defaultRegion);
00220 defaultRegion->AddRootLogicalVolume(worldLog);
00221 if(verboseLevel>1) G4cout << worldLog->GetName()
00222 << " is registered to the default region." << G4endl;
00223
00224
00225 G4TransportationManager::GetTransportationManager()
00226 ->SetWorldForTracking(currentWorld);
00227 if(topologyIsChanged) geometryNeedsToBeClosed = true;
00228
00229
00230 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
00231 if(pVVisManager) pVVisManager->GeometryHasChanged();
00232
00233 geometryInitialized = true;
00234 if(physicsInitialized && currentState!=G4State_Idle)
00235 { stateManager->SetNewState(G4State_Idle); }
00236 }
00237
00238 void G4RunManagerKernel::SetPhysics(G4VUserPhysicsList* uPhys)
00239 {
00240 physicsList = uPhys;
00241 G4ParticleTable::GetParticleTable()->SetReadiness();
00242 physicsList->ConstructParticle();
00243 if(verboseLevel>2) G4ParticleTable::GetParticleTable()->DumpTable();
00244 if(verboseLevel>1)
00245 {
00246 G4cout << "List of instantiated particles ============================================" << G4endl;
00247 G4int nPtcl = G4ParticleTable::GetParticleTable()->entries();
00248 for(G4int i=0;i<nPtcl;i++)
00249 {
00250 G4ParticleDefinition* pd = G4ParticleTable::GetParticleTable()->GetParticle(i);
00251 G4cout << pd->GetParticleName() << " ";
00252 if(i%10==9) G4cout << G4endl;
00253 }
00254 G4cout << G4endl;
00255 }
00256 }
00257
00258 void G4RunManagerKernel::InitializePhysics()
00259 {
00260 G4StateManager* stateManager = G4StateManager::GetStateManager();
00261 G4ApplicationState currentState = stateManager->GetCurrentState();
00262 if(!(currentState==G4State_Idle||currentState==G4State_PreInit))
00263 {
00264 G4Exception("G4RunManagerKernel::InitializePhysics",
00265 "Run0011", JustWarning,
00266 "Geant4 kernel is not PreInit or Idle state : Method ignored.");
00267 return;
00268 }
00269
00270 if(!physicsList)
00271 {
00272 G4Exception("G4RunManagerKernel::InitializePhysics",
00273 "Run0012", FatalException,
00274 "G4VUserPhysicsList is not defined");
00275 return;
00276 }
00277
00278 if(verboseLevel>1) G4cout << "physicsList->Construct() start." << G4endl;
00279 if(numberOfParallelWorld>0) physicsList->UseCoupledTransportation();
00280 physicsList->Construct();
00281
00282 if(verboseLevel>1) G4cout << "physicsList->CheckParticleList() start." << G4endl;
00283 physicsList->CheckParticleList();
00284 if(verboseLevel>1) G4cout << "physicsList->setCut() start." << G4endl;
00285 physicsList->SetCuts();
00286 CheckRegions();
00287 physicsInitialized = true;
00288 if(geometryInitialized && currentState!=G4State_Idle)
00289 { stateManager->SetNewState(G4State_Idle); }
00290 }
00291
00292 G4bool G4RunManagerKernel::RunInitialization()
00293 {
00294 G4StateManager* stateManager = G4StateManager::GetStateManager();
00295 G4ApplicationState currentState = stateManager->GetCurrentState();
00296
00297 if(!geometryInitialized)
00298 {
00299 G4Exception("G4RunManagerKernel::RunInitialization",
00300 "Run0021",
00301 JustWarning,
00302 "Geometry has not yet initialized : method ignored.");
00303 return false;
00304 }
00305
00306 if(!physicsInitialized)
00307 {
00308 G4Exception("G4RunManagerKernel::RunInitialization",
00309 "Run0022",
00310 JustWarning,
00311 "Physics has not yet initialized : method ignored.");
00312 return false;
00313 }
00314
00315 if( currentState != G4State_Idle )
00316 {
00317 G4Exception("G4RunManagerKernel::RunInitialization",
00318 "Run0023",
00319 JustWarning,
00320 "Geant4 kernel not in Idle state : method ignored.");
00321 return false;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330 UpdateRegion();
00331 BuildPhysicsTables();
00332
00333 if(geometryNeedsToBeClosed)
00334 {
00335 ResetNavigator();
00336 CheckRegularGeometry();
00337
00338 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
00339 if(pVVisManager) pVVisManager->GeometryHasChanged();
00340 }
00341
00342 GetPrimaryTransformer()->CheckUnknown();
00343
00344 stateManager->SetNewState(G4State_GeomClosed);
00345 return true;
00346 }
00347
00348 void G4RunManagerKernel::RunTermination()
00349 { G4StateManager::GetStateManager()->SetNewState(G4State_Idle); }
00350
00351 void G4RunManagerKernel::ResetNavigator()
00352 {
00353
00354
00355
00356
00357
00358 G4GeometryManager* geomManager = G4GeometryManager::GetInstance();
00359 if(verboseLevel>1) G4cout << "Start closing geometry." << G4endl;
00360 geomManager->OpenGeometry();
00361 geomManager->CloseGeometry(geometryToBeOptimized, verboseLevel>1);
00362
00363
00364
00365
00366
00367
00368
00369
00370 geometryNeedsToBeClosed = false;
00371 }
00372
00373 void G4RunManagerKernel::UpdateRegion()
00374 {
00375 G4StateManager* stateManager = G4StateManager::GetStateManager();
00376 G4ApplicationState currentState = stateManager->GetCurrentState();
00377 if( currentState != G4State_Idle )
00378 {
00379 G4Exception("G4RunManagerKernel::UpdateRegion",
00380 "Run0024",
00381 JustWarning,
00382 "Geant4 kernel not in Idle state : method ignored.");
00383 return;
00384 }
00385
00386 CheckRegions();
00387 G4RegionStore::GetInstance()->UpdateMaterialList(currentWorld);
00388 G4ProductionCutsTable::GetProductionCutsTable()->UpdateCoupleTable(currentWorld);
00389 }
00390
00391 void G4RunManagerKernel::BuildPhysicsTables()
00392 {
00393 if(G4ProductionCutsTable::GetProductionCutsTable()->IsModified()
00394 || physicsNeedsToBeReBuilt)
00395 {
00396 physicsList->BuildPhysicsTable();
00397 G4ProductionCutsTable::GetProductionCutsTable()->PhysicsTableUpdated();
00398 physicsNeedsToBeReBuilt = false;
00399 }
00400
00401 if(verboseLevel>1) DumpRegion();
00402 if(verboseLevel>0) physicsList->DumpCutValuesTable();
00403 physicsList->DumpCutValuesTableIfRequested();
00404 }
00405
00406 void G4RunManagerKernel::CheckRegions()
00407 {
00408 G4TransportationManager* transM = G4TransportationManager::GetTransportationManager();
00409 size_t nWorlds = transM->GetNoWorlds();
00410 std::vector<G4VPhysicalVolume*>::iterator wItr;
00411 for(size_t i=0;i<G4RegionStore::GetInstance()->size();i++)
00412 {
00413 G4Region* region = (*(G4RegionStore::GetInstance()))[i];
00414
00415
00416
00417
00418 region->SetWorld(0);
00419 region->UsedInMassGeometry(false);
00420 region->UsedInParallelGeometry(false);
00421 wItr = transM->GetWorldsIterator();
00422 for(size_t iw=0;iw<nWorlds;iw++)
00423 {
00424 if(region->BelongsTo(*wItr))
00425 {
00426 if(*wItr==currentWorld)
00427 { region->UsedInMassGeometry(true); }
00428 else
00429 { region->UsedInParallelGeometry(true); }
00430 }
00431 region->SetWorld(*wItr);
00432 wItr++;
00433 }
00434
00435 G4ProductionCuts* cuts = region->GetProductionCuts();
00436 if(!cuts)
00437 {
00438 if(region->IsInMassGeometry())
00439 {
00440 G4cerr << "Warning : Region <" << region->GetName()
00441 << "> does not have specific production cuts," << G4endl
00442 << "even though it appears in the current tracking world." << G4endl;
00443 G4cerr << "Default cuts are used for this region." << G4endl;
00444 }
00445
00446 if(region->IsInMassGeometry()||region->IsInParallelGeometry())
00447 {
00448 region->SetProductionCuts(
00449 G4ProductionCutsTable::GetProductionCutsTable()
00450 ->GetDefaultProductionCuts());
00451 }
00452 }
00453 }
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 wItr = transM->GetWorldsIterator();
00469 for(size_t iw=0;iw<nWorlds;iw++)
00470 {
00471
00472 if(*wItr!=currentWorld)
00473 {
00474 G4LogicalVolume* pwLogical = (*wItr)->GetLogicalVolume();
00475 if(!(pwLogical->GetRegion()))
00476 {
00477 pwLogical->SetRegion(defaultRegionForParallelWorld);
00478 defaultRegionForParallelWorld->AddRootLogicalVolume(pwLogical);
00479
00480
00481 }
00482 }
00483 wItr++;
00484 }
00485
00486 }
00487
00488 void G4RunManagerKernel::DumpRegion(const G4String& rname) const
00489 {
00490 G4Region* region = G4RegionStore::GetInstance()->GetRegion(rname);
00491 if(region) DumpRegion(region);
00492 }
00493
00494 void G4RunManagerKernel::DumpRegion(G4Region* region) const
00495 {
00496 if(!region)
00497 {
00498 for(size_t i=0;i<G4RegionStore::GetInstance()->size();i++)
00499 { DumpRegion((*(G4RegionStore::GetInstance()))[i]); }
00500 }
00501 else
00502 {
00503 G4cout << G4endl;
00504 G4cout << "Region <" << region->GetName() << "> -- ";
00505 if(region->GetWorldPhysical())
00506 {
00507 G4cout << " -- appears in <"
00508 << region->GetWorldPhysical()->GetName() << "> world volume";
00509 }
00510 else
00511 { G4cout << " -- is not associated to any world."; }
00512 G4cout << G4endl;
00513 if(region->IsInMassGeometry())
00514 { G4cout << " This region is in the mass world." << G4endl; }
00515 if(region->IsInParallelGeometry())
00516 { G4cout << " This region is in the parallel world." << G4endl; }
00517
00518 G4cout << " Root logical volume(s) : ";
00519 size_t nRootLV = region->GetNumberOfRootVolumes();
00520 std::vector<G4LogicalVolume*>::iterator lvItr = region->GetRootLogicalVolumeIterator();
00521 for(size_t j=0;j<nRootLV;j++)
00522 { G4cout << (*lvItr)->GetName() << " "; lvItr++; }
00523 G4cout << G4endl;
00524
00525 G4cout << " Pointers : G4VUserRegionInformation[" << region->GetUserInformation()
00526 << "], G4UserLimits[" << region->GetUserLimits()
00527 << "], G4FastSimulationManager[" << region->GetFastSimulationManager()
00528 << "], G4UserSteppingAction[" << region->GetRegionalSteppingAction() << "]" << G4endl;
00529
00530
00531
00532
00533
00534
00535 G4cout << " Materials : ";
00536 std::vector<G4Material*>::const_iterator mItr = region->GetMaterialIterator();
00537 size_t nMaterial = region->GetNumberOfMaterials();
00538 for(size_t iMate=0;iMate<nMaterial;iMate++)
00539 {
00540 G4cout << (*mItr)->GetName() << " ";
00541 mItr++;
00542 }
00543 G4cout << G4endl;
00544 G4ProductionCuts* cuts = region->GetProductionCuts();
00545 if(!cuts && region->IsInMassGeometry())
00546 {
00547 G4cerr << "Warning : Region <" << region->GetName()
00548 << "> does not have specific production cuts." << G4endl;
00549 G4cerr << "Default cuts are used for this region." << G4endl;
00550 region->SetProductionCuts(
00551 G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
00552 }
00553 else if(cuts)
00554 {
00555 G4cout << " Production cuts : "
00556 << " gamma "
00557 << G4BestUnit(cuts->GetProductionCut("gamma"),"Length")
00558 << " e- "
00559 << G4BestUnit(cuts->GetProductionCut("e-"),"Length")
00560 << " e+ "
00561 << G4BestUnit(cuts->GetProductionCut("e+"),"Length")
00562 << " proton "
00563 << G4BestUnit(cuts->GetProductionCut("proton"),"Length")
00564 << G4endl;
00565 }
00566 }
00567 }
00568
00569 #include "G4LogicalVolumeStore.hh"
00570 void G4RunManagerKernel::CheckRegularGeometry()
00571 {
00572 G4LogicalVolumeStore* store = G4LogicalVolumeStore::GetInstance();
00573 for(G4LogicalVolumeStore::iterator pos=store->begin(); pos!=store->end(); pos++)
00574 {
00575 if((*pos)&&((*pos)->GetNoDaughters()==1))
00576 {
00577 if((*pos)->GetDaughter(0)->IsRegularStructure())
00578 {
00579 SetScoreSplitter();
00580 return;
00581 }
00582 }
00583 }
00584 }
00585
00586 #include "G4ParticleTable.hh"
00587 #include "G4ParticleDefinition.hh"
00588 #include "G4ProcessManager.hh"
00589 #include "G4ProcessVector.hh"
00590 #include "G4VProcess.hh"
00591 G4bool G4RunManagerKernel::ConfirmCoupledTransportation()
00592 {
00593 G4ParticleTable* theParticleTable = G4ParticleTable::GetParticleTable();
00594 G4ParticleTable::G4PTblDicIterator* theParticleIterator = theParticleTable->GetIterator();
00595 theParticleIterator->reset();
00596 while((*theParticleIterator)())
00597 {
00598 G4ParticleDefinition* pd = theParticleIterator->value();
00599 G4ProcessManager* pm = pd->GetProcessManager();
00600 if(pm)
00601 {
00602 G4ProcessVector* pv = pm->GetAlongStepProcessVector(typeDoIt);
00603 G4VProcess* p = (*pv)[0];
00604 return ( (p->GetProcessName()) == "CoupledTransportation" );
00605 }
00606 }
00607 return false;
00608 }
00609
00610 #include "G4ScoreSplittingProcess.hh"
00611 void G4RunManagerKernel::SetScoreSplitter()
00612 {
00613 G4ScoreSplittingProcess* pSplitter = new G4ScoreSplittingProcess();
00614 G4ParticleTable* theParticleTable = G4ParticleTable::GetParticleTable();
00615 G4ParticleTable::G4PTblDicIterator* theParticleIterator = theParticleTable->GetIterator();
00616
00617
00618 static bool InitSplitter=false;
00619 if( ! InitSplitter ) {
00620 InitSplitter = true;
00621
00622 theParticleIterator->reset();
00623 while( (*theParticleIterator)() )
00624 {
00625 G4ParticleDefinition* particle = theParticleIterator->value();
00626 G4ProcessManager* pmanager = particle->GetProcessManager();
00627 if(pmanager)
00628 { pmanager->AddDiscreteProcess(pSplitter); }
00629 }
00630
00631 if(verboseLevel>0)
00632 {
00633 G4cout << "G4RunManagerKernel -- G4ScoreSplittingProcess is appended to all particles." << G4endl;
00634 }
00635 }
00636 }
00637