32#include "TG4RootDetectorConstruction.h"
38#include <G4FieldManager.hh>
39#include <G4LogicalVolumeStore.hh>
40#include <G4Material.hh>
42#include <G4PVPlacement.hh>
43#include <G4ReflectionFactory.hh>
45#include <G4TransportationManager.hh>
47#include <TGeoMCGeometry.h>
48#include <TGeoManager.h>
49#include <TGeoMedium.h>
50#include <TGeoVolume.h>
52#include <TVirtualMC.h>
53#include <TVirtualMCApplication.h>
57#include <G4SystemOfUnits.hh>
60#include <G3MatTable.hh>
61#include <G3MedTable.hh>
62#include <G3SensVolVector.hh>
63#include <G3VolTable.hh>
65#include <G3toG4BuildTree.hh>
66#include <G3toG4MANY.hh>
70#include <Geant4GM/volumes/Factory.h>
71#include <RootGM/volumes/Factory.h>
86 fRootDetectorConstruction(0),
88 fFastModelsManager(0),
91 fUserGeometry(userGeometry),
93 fUserRegionConstruction(0),
94 fUserPostDetConstruction(0),
97 fIsUserMaxStep(false),
98 fIsMaxStepInLowDensityMaterials(true),
99 fLimitDensity(fgDefaultLimitDensity),
100 fMaxStepInLowDensityMaterials(fgDefaultMaxStep)
107 "TG4GeometryManager:",
"Cannot create two instances of singleton.");
162 if (!gGeoManager)
new TGeoManager(
"TGeo",
"Root geometry manager");
174 G4cout <<
"TG4GeometryManager::ConstructG4GeometryViaVMC" << G4endl;
189 G3VolTableEntry* first = G3Vol.GetFirstVTE();
195 G3toG4BuildTree(first, 0);
204 G4VPhysicalVolume* world =
new G4PVPlacement(
205 0, G4ThreeVector(), first->GetName(), first->GetLV(), 0,
false, 1);
215 "Geometry construction via VMC is not supported.");
227 G4cout <<
"TG4GeometryManager::ConstructG4GeometryViaVGM" << G4endl;
232 "Geometry was not defined via Root.");
236 TGeoVolume* topVolume = gGeoManager->GetTopVolume();
238 topVolume = (TGeoVolume*)gGeoManager->GetListOfVolumes()->First();
241 "Root top volume not found.");
243 gGeoManager->SetTopVolume(topVolume);
247 if (!gGeoManager->IsClosed()) gGeoManager->CloseGeometry();
251 G4cout <<
"Converting Root geometry to Geant4 via VGM ... " << G4endl;
254 RootGM::Factory rootFactory;
256 rootFactory.SetIgnore(
true);
257 rootFactory.Import(gGeoManager->GetTopNode());
260 Geant4GM::Factory g4Factory;
262 rootFactory.Export(&g4Factory);
264 G4VPhysicalVolume* g4World = g4Factory.World();
270 "Root geometry conversion is not supported.");
280 G4cout <<
"TG4GeometryManager::ConstructG4Geometry: "
288 G4cout <<
"Running TVirtualMCApplication::ConstructGeometry" << G4endl;
291 TVirtualMCApplication::Instance()->ConstructGeometry();
293 TVirtualMCApplication::Instance()->MisalignGeometry();
300 G4cout <<
"Running TVirtualMCApplication::ConstructGeometry" << G4endl;
302#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 22, 8)
304 TGeoManager::LockDefaultUnits(
false);
305 TGeoManager::SetDefaultUnits(TGeoManager::kRootUnits);
306 TGeoManager::LockDefaultUnits(
true);
310 TVirtualMCApplication::Instance()->ConstructGeometry();
315 if (!gGeoManager->IsClosed()) {
316 TGeoVolume* top = (TGeoVolume*)gGeoManager->GetListOfVolumes()->First();
317 gGeoManager->SetTopVolume(top);
318 gGeoManager->CloseGeometry();
322 TVirtualMCApplication::Instance()->MisalignGeometry();
334 G4cout <<
"G4 Stat: instantiated "
349 G4cout <<
"TG4GeometryManager::FillMediumMapFromG3()" << G4endl;
354 for (G4int i = 0; i < G4int(G3Med.GetSize()); i++) {
355 G3MedTableEntry* mediumEntry = G3Med.GetMTE(i);
356 G4int mediumID = mediumEntry->GetID();
359 G4cout <<
"Getting medium ID=" << mediumID << G4endl;
367 medium = mediumMap->
AddMedium(mediumID,
false);
369 TString message =
"Medium ";
371 message +=
" was not found in medium map. New medium will be created";
375 medium->
SetLimits(mediumEntry->GetLimits());
382 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
383 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
384 G4LogicalVolume* lv = (*lvStore)[i];
387 G4String name = lv->GetName();
388 G4String g3Name(name);
391 G4String ext = G4ReflectionFactory::Instance()->GetVolumesNameExtension();
392 if (name.find(ext)) g3Name = g3Name.substr(0, g3Name.find(ext));
393 G4int mediumID = G3Vol.GetVTE(g3Name)->GetNmed();
396 G4cout <<
"Mapping medium Id " << mediumID <<
" to LV " << name << G4endl;
410 "Geometry construction via VMC is not supported.");
421 G4cout <<
"TG4GeometryManager::FillMediumMapFromG4()" << G4endl;
426 const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
427 for (G4int i = 0; i < G4int(materialTable->size()); i++) {
428 G4Material* material = (*materialTable)[i];
431 G4cout <<
"Adding medium name= " << material->GetName()
432 <<
" Id=" << material->GetIndex() << G4endl;
435 medium->
SetName(material->GetName());
440 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
441 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
442 G4LogicalVolume* lv = (*lvStore)[i];
443 G4int mediumID = lv->GetMaterial()->GetIndex();
446 G4cout <<
"Mapping medium Id=" << mediumID <<
" to LV= " << lv->GetName()
459 G4cout <<
"TG4GeometryManager::FillMediumMapFromRoot()" << G4endl;
466 TIter next(gGeoManager->GetListOfMedia());
467 TGeoMedium* geoMedium;
468 while ((geoMedium = (TGeoMedium*)next())) {
469 Int_t mediumId = geoMedium->GetId();
470 G4String mediumName = geoMedium->GetName();
473 Int_t ifield = (Int_t)geoMedium->GetParam(1);
476 Double_t stemax = geoMedium->GetParam(4);
485 limits->SetMaxAllowedStep(stemax * cm);
489 G4cout <<
"Adding medium Id=" << mediumId <<
" name=" << mediumName
490 <<
" limits=" << limits << G4endl;
497 G4String matName = geoMedium->GetMaterial()->GetName();
498 G4Material* material = G4Material::GetMaterial(matName);
501 "Material " + TString(matName) +
" not found.");
507 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
508 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
509 G4LogicalVolume* lv = (*lvStore)[i];
511 TGeoVolume* geoVolume =
nullptr;
514 G4String volName = lv->GetName();
518 G4String ext = G4ReflectionFactory::Instance()->GetVolumesNameExtension();
519 if (volName.find(ext)) volName = volName.substr(0, volName.find(ext));
521 geoVolume = gGeoManager->GetVolume(volName.data());
529 "Root volume " + TString(lv->GetName()) +
" not found");
533 if (geoVolume && geoVolume->IsAssembly())
continue;
535 if (geoVolume && !geoVolume->GetMedium()) {
537 "Root volume " + TString(lv->GetName()) +
" has not medium defined.");
540 G4int mediumID = geoVolume->GetMedium()->GetId();
543 G4cout <<
"Mapping medium Id=" << mediumID <<
" to LV=" << lv->GetName()
578 fieldType =
"Global";
581 fieldType =
"Local (in ";
582 fieldType.append(lv->GetName());
583 fieldType.append(
")");
585 if (isCachedMagneticField) {
586 fieldType.append(
" cached");
589 G4cout << fieldType <<
" field created with stepper ";
597 fgFields =
new std::vector<TG4Field*>();
609 if (gMC->GetMagField()) {
614 G4cout <<
"Create G4MonopoleFieldSetup" << G4endl;
617 TG4Field* tg4Field = (*fgFields)[0];
622 G4cerr <<
"Wrong field type. Only magnetic field is supported in "
623 "G4MonopoleFieldSetup."
648 G4cout <<
"TG4GeometryManager::ConstructZeroFields()" << G4endl;
650 G4bool forceToAllDaughters =
false;
654 G4FieldManager* dummyFieldManager =
new G4FieldManager();
655 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
656 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
657 G4LogicalVolume* lv = (*lvStore)[i];
658 lv->SetFieldManager(dummyFieldManager, forceToAllDaughters);
663 G4FieldManager* fieldManager = 0;
664 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
666 G4LogicalVolume* lv = (*lvStore)[i];
671 if (!medium)
continue;
676 G4cout <<
"Global field in logical volume: " << lv->
GetName() << G4endl;
683 fieldManager =
new G4FieldManager();
685 fieldManager->SetDetectorField(0);
686 fieldManager->CreateChordFinder(0);
688 lv->SetFieldManager(fieldManager, forceToAllDaughters);
691 G4cout <<
"Zero magnetic field set to logical volume: " << lv->GetName()
697 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
698 G4LogicalVolume* lv = (*lvStore)[i];
700 if (lv->GetFieldManager() == dummyFieldManager) {
701 lv->SetFieldManager(0, forceToAllDaughters);
706 delete dummyFieldManager;
711 const G4String& volumeName)
727 return fieldParameters;
739 G4cout <<
"TG4GeometryManager::ConstructLocalFields()" << G4endl;
741 TIter next(gGeoManager->GetListOfVolumes());
742 TGeoVolume* geoVolume;
743 while ((geoVolume = (TGeoVolume*)next())) {
745 if (!geoVolume->GetField())
continue;
748 TVirtualMagField* magField =
749 dynamic_cast<TVirtualMagField*
>(geoVolume->GetField());
751 TString message = geoVolume->GetName();
752 message +=
": uknown field type will be ignored.";
754 "No magnetic field is defined.");
759 G4String volumeName = geoVolume->GetName();
762 G4LogicalVolume* lv =
765 TString message = geoVolume->GetName();
766 message +=
" volume not found in Geant4 geometry.";
768 "No magnetic field is defined.");
793 "TG4GeometryManager",
"GetMCGeometry",
"No MC geometry defined.");
812 TVirtualMCApplication::Instance()->ConstructOpGeometry();
827 G4cout <<
"TG4GeometryManager::ConstructSDandField() " << G4endl;
856 G4cout <<
"TG4GeometryManager::FinishGeometry" << G4endl;
866 G4TransportationManager::GetTransportationManager()
867 ->GetNavigatorForTracking()
871 G4cout <<
"TG4GeometryManager::FinishGeometry done" << G4endl;
883 "TG4GeometryManager",
"UpdateField",
"No magnetic field is defined.");
887 if (
VerboseLevel() > 1) G4cout <<
"TG4GeometryManager::UpdateField" << G4endl;
892 for (G4int i = 0; i < G4int(
fgFields->size()); ++i) {
910 const G4String& volName)
918 return radiatorDescription;
928 G4cout <<
"TG4GeometryManager::SetUserLimits" << G4endl;
930 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
932 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
933 G4LogicalVolume* lv = (*lvStore)[i];
936 if (!medium)
continue;
944 G4String name = medium->
GetName();
953 tg4Limits =
new TG4Limits(name, cuts, controls);
974 tg4Limits->
Update(controls);
977 lv->SetUserLimits(tg4Limits);
981 G4cout <<
"TG4GeometryManager::SetUserLimits done" << G4endl;
990 G4cout <<
"TG4GeometryManager::SetIsLocalField: " << std::boolalpha
991 << isLocalField << G4endl;
1002 G4cout <<
"TG4GeometryManager::SetIsZeroField: " << std::boolalpha
1003 << isZeroField << G4endl;
1014 G4cout <<
"TG4GeometryManager::SetIsUserMaxStep: " << std::boolalpha
1015 << isUserMaxStep << G4endl;
1026 G4cout <<
"TG4GeometryManager::SetIsMaxStepInLowDensityMaterials: "
1027 << std::boolalpha << isMaxStep << G4endl;
1054 if (!volumeName.size()) {
1069 G4MagIntegratorStepper* stepper, G4String volumeName)
1071 if (!volumeName.size()) {
1090 for (G4int i = 0; i < G4int(
fgFields->size()); ++i) {
Definition of the G4MonopoleFieldSetup class.
Definition of the TG4BiasingManager class.
Definition of the TG4FieldParameters class.
Definition of the TG4Field class.
Definition of the TG4G3ControlVector class.
Definition of the TG4G3CutVector class.
Definition of the TG4G3Units class.
Definition of the TG4GeometryManager class.
Definition of the TG4GeometryServices class.
Definition of the TG4Globals class and basic container types.
Definition of the TG4Limits class.
Definition of the TG4MCGeometry class.
Definition of the TG4MagneticField class.
Definition of the TG4MediumMap class.
Definition of the TG4Medium class.
Definition of the TG4ModelConfigurationManager class.
Definition of the TG4OpGeometryManager class.
Definition of the TG4RadiatorDescription class.
Definition of the TG4SDManager class.
Definition of the TG4StateManager class.
Definition of the TG4VUserPostDetConstruction class.
Definition of the TG4VUserRegionConstruction class.
void SetDefaultEquation(G4EquationOfMotion *equation)
static G4MonopoleFieldSetup * GetMonopoleFieldSetup()
void SetMagneticField(G4MagneticField *magneticField)
void SetDefaultStepper(G4MagIntegratorStepper *stepper)
void CreateBiasingOperator()
The magnetic field parameters.
void SetUserEquationOfMotion(G4EquationOfMotion *equation)
static FieldType GetFieldType(const G4String &name)
void SetUserStepper(G4MagIntegratorStepper *stepper)
G4double GetConstDistance() const
Return the distance within which the field is considered constant.
static G4String StepperTypeName(StepperType stepper)
static StepperType GetStepperType(const G4String &name)
static G4String FieldTypeName(FieldType field)
The class for constructing magnetic, electromagnetic and gravity fields which strength is defined via...
G4MagIntegratorStepper * GetStepper() const
G4Field * GetG4Field() const
G4EquationOfMotion * GetEquation() const
Vector of control process values with convenient set/get methods.
Vector of kinetic energy cut values with convenient set/get methods.
The manager class for building Geant4 geometry depending on a selected user input.
G4bool fIsZeroField
option to activate propagating 'ifield = 0' defined in tracking media
static const G4double fgDefaultMaxStep
default max allowed step in materials with density < fLimitDensity
static TG4GeometryManager * fgInstance
this instance
G4bool fIsLocalField
option to activate getting local magnetic fields from Root geometry
void ConstructG4Geometry()
TG4BiasingManager * fBiasingManager
Biasing manager.
TG4ModelConfigurationManager * fFastModelsManager
Fast simulation models manager.
TVirtualMCGeometry * GetMCGeometry() const
void FillMediumMapFromG4()
void ConstructZeroFields()
void ConstructSDandField()
static G4ThreadLocal std::vector< TG4Field * > * fgFields
Fields.
void CreateField(TVirtualMagField *magField, TG4FieldParameters *fieldParameters, G4LogicalVolume *lv)
void SetIsLocalField(G4bool isLocalField)
TG4FieldParameters * GetOrCreateFieldParameters(const G4String &volumeName)
void SetIsMaxStepInLowDensityMaterials(G4bool isMaxStep)
void ConstructLocalFields()
void ConstructG4GeometryViaVMC()
std::vector< TG4FieldParameters * > fFieldParameters
Field parameters.
TG4GeometryManager(const TString &userGeometry)
void ConstructGlobalField()
void SetUserEquationOfMotion(G4EquationOfMotion *equation, G4String volumeName="")
void SetUserPostDetConstruction(TG4VUserPostDetConstruction *userPostDetConstruction)
TG4GeometryServices * fGeometryServices
geometry services
G4bool fIsUserMaxStep
option to activate max step defined in tracking media
void SetIsZeroField(G4bool isZeroField)
void PrintFieldStatistics() const
void CreateFieldParameters(const G4String &fieldVolName)
G4double fLimitDensity
material density limit for setting max allowed step
void FillMediumMapFromG3()
TG4OpGeometryManager * fOpManager
optical geometry manager
G4bool fIsMaxStepInLowDensityMaterials
option to activate max step defined in low density materials
TG4VUserRegionConstruction * fUserRegionConstruction
User region construction.
TG4RadiatorDescription * CreateRadiator(const G4String &volName)
TG4ModelConfigurationManager * fEmModelsManager
EM models manager.
void SetUserRegionConstruction(TG4VUserRegionConstruction *userRegionConstruction)
std::vector< TG4RadiatorDescription * > fRadiators
Radiators.
void FillMediumMapFromRoot()
TG4RootDetectorConstruction * fRootDetectorConstruction
Root detector construction.
static const G4double fgDefaultLimitDensity
default material density limit for setting max allowed step
void SetIsUserMaxStep(G4bool isUserMaxStep)
virtual ~TG4GeometryManager()
void SetUserLimits(const TG4G3CutVector &cuts, const TG4G3ControlVector &controls) const
TG4VUserPostDetConstruction * fUserPostDetConstruction
User post detector construction.
void ConstructG4GeometryViaVGM()
G4String fUserGeometry
User geometry input.
void SetUserStepper(G4MagIntegratorStepper *stepper, G4String volumeName="")
G4double fMaxStepInLowDensityMaterials
max allowed step in materials with density < fLimitDensity
TVirtualMCGeometry * fMCGeometry
VirtualMC geometry.
Services for accessing to Geant4 geometry.
G4VPhysicalVolume * GetWorld() const
Int_t NofG4PhysicalVolumes() const
void SetWorld(G4VPhysicalVolume *world)
G4LogicalVolume * FindLogicalVolume(const G4String &name, G4bool silent=false) const
static TG4GeometryServices * Instance()
TG4Limits * FindLimits2(const G4String &name, G4bool silent=false) const
Int_t NofG4LogicalVolumes() const
TG4Limits * GetLimits(G4UserLimits *limits) const
void SetIsG3toG4(G4bool isG3toG4)
TG4MediumMap * GetMediumMap() const
static void Warning(const TString &className, const TString &methodName, const TString &text)
static void Exception(const TString &className, const TString &methodName, const TString &text)
Extended G4UserLimits class.
G4bool Update(const TG4G3ControlVector &controls)
void SetDefaultMaxAllowedStep()
void SetName(const G4String &name)
Geant4 implementation of the TVirtualMCGeometry interface.
The magnetic field defined via TVirtualMagField.
virtual void PrintStatistics() const
The map of media to logical volumes.
void MapMedium(G4LogicalVolume *lv, G4int mediumID)
TG4Medium * AddMedium(G4int mediumID, G4bool warn=true)
G4int GetNofMedia() const
TG4Medium * GetMedium(G4int mediumID, G4bool warn=true) const
Helper class to keep medium data.
G4UserLimits * GetLimits() const
void SetLimits(G4UserLimits *limits)
void SetIfield(G4int ifield)
void SetMaterial(G4Material *material)
void SetName(const G4String &name)
The model configuration vector with suitable setters and a messenger.
Geant4 implementation of the TVirtualMC interface methods for definition of material properties for o...
The radiator description.
static TG4SDManager * Instance()
void SetNewState(TG4ApplicationState state)
static TG4StateManager * Instance()
The abstract base class for user defined class to customize geometry.
virtual void Construct()=0
Method to be overriden by user.
The abstract base class for user defined regions.
virtual void Construct()=0
Method to be overriden by user.
Base class for defining the verbose level and a common messenger.
virtual G4int VerboseLevel() const
@ kMisalignGeometry
in MisalignGeometry
@ kConstructGeometry
in ConstructGeometry
@ kNotInApplication
not in VMC application
@ kConstructOpGeometry
in ConstructOpGeometry