38#include "TG4RootDetectorConstruction.h"
41#include <G4FieldManager.hh>
42#include <G4LogicalVolumeStore.hh>
43#include <G4Material.hh>
45#include <G4PVPlacement.hh>
46#include <G4ReflectionFactory.hh>
48#include <G4TransportationManager.hh>
50#include <TGeoMCGeometry.h>
51#include <TGeoManager.h>
52#include <TGeoMedium.h>
53#include <TGeoVolume.h>
55#include <TVirtualMC.h>
56#include <TVirtualMCApplication.h>
60#include <G4SystemOfUnits.hh>
63#include <G3MatTable.hh>
64#include <G3MedTable.hh>
65#include <G3SensVolVector.hh>
66#include <G3VolTable.hh>
68#include <G3toG4BuildTree.hh>
69#include <G3toG4MANY.hh>
73#include <Geant4GM/volumes/Factory.h>
74#include <RootGM/volumes/Factory.h>
89 fRootDetectorConstruction(0),
91 fFastModelsManager(0),
94 fUserGeometry(userGeometry),
96 fUserRegionConstruction(0),
97 fUserPostDetConstruction(0),
100 fIsUserMaxStep(false),
101 fIsMaxStepInLowDensityMaterials(true),
102 fLimitDensity(fgDefaultLimitDensity),
103 fMaxStepInLowDensityMaterials(fgDefaultMaxStep)
110 "TG4GeometryManager:",
"Cannot create two instances of singleton.");
165 if (!gGeoManager)
new TGeoManager(
"TGeo",
"Root geometry manager");
177 G4cout <<
"TG4GeometryManager::ConstructG4GeometryViaVMC" << G4endl;
192 G3VolTableEntry* first = G3Vol.GetFirstVTE();
198 G3toG4BuildTree(first, 0);
207 G4VPhysicalVolume* world =
new G4PVPlacement(
208 0, G4ThreeVector(), first->GetName(), first->GetLV(), 0,
false, 1);
218 "Geometry construction via VMC is not supported.");
230 G4cout <<
"TG4GeometryManager::ConstructG4GeometryViaVGM" << G4endl;
235 "Geometry was not defined via Root.");
239 TGeoVolume* topVolume = gGeoManager->GetTopVolume();
241 topVolume = (TGeoVolume*)gGeoManager->GetListOfVolumes()->First();
244 "Root top volume not found.");
246 gGeoManager->SetTopVolume(topVolume);
250 if (!gGeoManager->IsClosed()) gGeoManager->CloseGeometry();
254 G4cout <<
"Converting Root geometry to Geant4 via VGM ... " << G4endl;
257 RootGM::Factory rootFactory;
259 rootFactory.SetIgnore(
true);
260 rootFactory.Import(gGeoManager->GetTopNode());
263 Geant4GM::Factory g4Factory;
265 rootFactory.Export(&g4Factory);
267 G4VPhysicalVolume* g4World = g4Factory.World();
273 "Root geometry conversion is not supported.");
283 G4cout <<
"TG4GeometryManager::ConstructG4Geometry: "
291 G4cout <<
"Running TVirtualMCApplication::ConstructGeometry" << G4endl;
294 TVirtualMCApplication::Instance()->ConstructGeometry();
296 TVirtualMCApplication::Instance()->MisalignGeometry();
303 G4cout <<
"Running TVirtualMCApplication::ConstructGeometry" << G4endl;
305#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 22, 8)
307 TGeoManager::LockDefaultUnits(
false);
308 TGeoManager::SetDefaultUnits(TGeoManager::kRootUnits);
309 TGeoManager::LockDefaultUnits(
true);
313 TVirtualMCApplication::Instance()->ConstructGeometry();
318 if (!gGeoManager->IsClosed()) {
319 TGeoVolume* top = (TGeoVolume*)gGeoManager->GetListOfVolumes()->First();
320 gGeoManager->SetTopVolume(top);
321 gGeoManager->CloseGeometry();
325 TVirtualMCApplication::Instance()->MisalignGeometry();
337 G4cout <<
"G4 Stat: instantiated "
352 G4cout <<
"TG4GeometryManager::FillMediumMapFromG3()" << G4endl;
357 for (G4int i = 0; i < G4int(G3Med.GetSize()); i++) {
358 G3MedTableEntry* mediumEntry = G3Med.GetMTE(i);
359 G4int mediumID = mediumEntry->GetID();
362 G4cout <<
"Getting medium ID=" << mediumID << G4endl;
370 medium = mediumMap->
AddMedium(mediumID,
false);
372 TString message =
"Medium ";
374 message +=
" was not found in medium map. New medium will be created";
378 medium->
SetLimits(mediumEntry->GetLimits());
385 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
386 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
387 G4LogicalVolume* lv = (*lvStore)[i];
390 G4String name = lv->GetName();
391 G4String g3Name(name);
394 G4String ext = G4ReflectionFactory::Instance()->GetVolumesNameExtension();
395 if (name.find(ext)) g3Name = g3Name.substr(0, g3Name.find(ext));
396 G4int mediumID = G3Vol.GetVTE(g3Name)->GetNmed();
399 G4cout <<
"Mapping medium Id " << mediumID <<
" to LV " << name << G4endl;
413 "Geometry construction via VMC is not supported.");
424 G4cout <<
"TG4GeometryManager::FillMediumMapFromG4()" << G4endl;
429 const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
430 for (G4int i = 0; i < G4int(materialTable->size()); i++) {
431 G4Material* material = (*materialTable)[i];
434 G4cout <<
"Adding medium name= " << material->GetName()
435 <<
" Id=" << material->GetIndex() << G4endl;
438 medium->
SetName(material->GetName());
443 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
444 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
445 G4LogicalVolume* lv = (*lvStore)[i];
446 G4int mediumID = lv->GetMaterial()->GetIndex();
449 G4cout <<
"Mapping medium Id=" << mediumID <<
" to LV= " << lv->GetName()
462 G4cout <<
"TG4GeometryManager::FillMediumMapFromRoot()" << G4endl;
469 TIter next(gGeoManager->GetListOfMedia());
470 TGeoMedium* geoMedium;
471 while ((geoMedium = (TGeoMedium*)next())) {
472 Int_t mediumId = geoMedium->GetId();
473 G4String mediumName = geoMedium->GetName();
476 Int_t ifield = (Int_t)geoMedium->GetParam(1);
479 Double_t stemax = geoMedium->GetParam(4);
488 limits->SetMaxAllowedStep(stemax * cm);
492 G4cout <<
"Adding medium Id=" << mediumId <<
" name=" << mediumName
493 <<
" limits=" << limits << G4endl;
500 G4String matName = geoMedium->GetMaterial()->GetName();
501 G4Material* material = G4Material::GetMaterial(matName);
504 "Material " + TString(matName) +
" not found.");
510 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
511 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
512 G4LogicalVolume* lv = (*lvStore)[i];
514 TGeoVolume* geoVolume =
nullptr;
517 G4String volName = lv->GetName();
521 G4String ext = G4ReflectionFactory::Instance()->GetVolumesNameExtension();
522 if (volName.find(ext)) volName = volName.substr(0, volName.find(ext));
524 geoVolume = gGeoManager->GetVolume(volName.data());
534 "Root volume " + TString(lv->GetName()) +
" not found");
538 if (geoVolume && geoVolume->IsAssembly())
continue;
540 if (geoVolume && !geoVolume->GetMedium()) {
542 "Root volume " + TString(lv->GetName()) +
" has not medium defined.");
545 G4int mediumID = geoVolume->GetMedium()->GetId();
548 G4cout <<
"Mapping medium Id=" << mediumID <<
" to LV=" << lv->GetName()
583 fieldType =
"Global";
586 fieldType =
"Local (in ";
587 fieldType.append(lv->GetName());
588 fieldType.append(
")");
590 if (isCachedMagneticField) {
591 fieldType.append(
" cached");
594 G4cout << fieldType <<
" field created with stepper ";
602 fgFields =
new std::vector<TG4Field*>();
614 if (gMC->GetMagField()) {
619 G4cout <<
"Create G4MonopoleFieldSetup" << G4endl;
622 TG4Field* tg4Field = (*fgFields)[0];
627 G4cerr <<
"Wrong field type. Only magnetic field is supported in "
628 "G4MonopoleFieldSetup."
653 G4cout <<
"TG4GeometryManager::ConstructZeroFields()" << G4endl;
655 G4bool forceToAllDaughters =
false;
659 G4FieldManager* dummyFieldManager =
new G4FieldManager();
660 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
661 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
662 G4LogicalVolume* lv = (*lvStore)[i];
663 lv->SetFieldManager(dummyFieldManager, forceToAllDaughters);
668 G4FieldManager* fieldManager = 0;
669 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
671 G4LogicalVolume* lv = (*lvStore)[i];
676 if (!medium)
continue;
681 G4cout <<
"Global field in logical volume: " << lv->GetName() << G4endl;
688 fieldManager =
new G4FieldManager();
690 fieldManager->SetDetectorField(0);
691 fieldManager->CreateChordFinder(0);
693 lv->SetFieldManager(fieldManager, forceToAllDaughters);
696 G4cout <<
"Zero magnetic field set to logical volume: " << lv->GetName()
702 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
703 G4LogicalVolume* lv = (*lvStore)[i];
705 if (lv->GetFieldManager() == dummyFieldManager) {
706 lv->SetFieldManager(0, forceToAllDaughters);
711 delete dummyFieldManager;
716 const G4String& volumeName)
732 return fieldParameters;
744 G4cout <<
"TG4GeometryManager::ConstructLocalFields()" << G4endl;
746 TIter next(gGeoManager->GetListOfVolumes());
747 TGeoVolume* geoVolume;
748 while ((geoVolume = (TGeoVolume*)next())) {
750 if (!geoVolume->GetField())
continue;
753 TVirtualMagField* magField =
754 dynamic_cast<TVirtualMagField*
>(geoVolume->GetField());
756 TString message = geoVolume->GetName();
757 message +=
": uknown field type will be ignored.";
759 "No magnetic field is defined.");
764 G4String volumeName = geoVolume->GetName();
767 G4LogicalVolume* lv =
770 TString message = geoVolume->GetName();
771 message +=
" volume not found in Geant4 geometry.";
773 "No magnetic field is defined.");
798 "TG4GeometryManager",
"GetMCGeometry",
"No MC geometry defined.");
817 TVirtualMCApplication::Instance()->ConstructOpGeometry();
832 G4cout <<
"TG4GeometryManager::ConstructSDandField() " << G4endl;
861 G4cout <<
"TG4GeometryManager::FinishGeometry" << G4endl;
871 G4TransportationManager::GetTransportationManager()
872 ->GetNavigatorForTracking()
876 G4cout <<
"TG4GeometryManager::FinishGeometry done" << G4endl;
888 "TG4GeometryManager",
"UpdateField",
"No magnetic field is defined.");
892 if (
VerboseLevel() > 1) G4cout <<
"TG4GeometryManager::UpdateField" << G4endl;
897 for (G4int i = 0; i < G4int(
fgFields->size()); ++i) {
915 const G4String& volName)
923 return radiatorDescription;
933 G4cout <<
"TG4GeometryManager::SetUserLimits" << G4endl;
935 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
937 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
938 G4LogicalVolume* lv = (*lvStore)[i];
941 if (!medium)
continue;
949 G4String name = medium->
GetName();
958 tg4Limits =
new TG4Limits(name, cuts, controls);
979 tg4Limits->
Update(controls);
982 lv->SetUserLimits(tg4Limits);
986 G4cout <<
"TG4GeometryManager::SetUserLimits done" << G4endl;
995 G4cout <<
"TG4GeometryManager::SetIsLocalField: " << std::boolalpha
996 << isLocalField << G4endl;
1007 G4cout <<
"TG4GeometryManager::SetIsZeroField: " << std::boolalpha
1008 << isZeroField << G4endl;
1019 G4cout <<
"TG4GeometryManager::SetIsUserMaxStep: " << std::boolalpha
1020 << isUserMaxStep << G4endl;
1031 G4cout <<
"TG4GeometryManager::SetIsMaxStepInLowDensityMaterials: "
1032 << std::boolalpha << isMaxStep << G4endl;
1059 if (!volumeName.size()) {
1074 G4MagIntegratorStepper* stepper, G4String volumeName)
1076 if (!volumeName.size()) {
1095 for (G4int i = 0; i < G4int(
fgFields->size()); ++i) {
1100 mgfield->PrintStatistics();
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.
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