22#include "G4FieldManager.hh"
23#include "G4GeometryManager.hh"
24#include "G4LogicalVolumeStore.hh"
25#include "G4Material.hh"
26#include "G4PVPlacement.hh"
27#include "G4PhysicalConstants.hh"
28#include "G4PhysicalVolumeStore.hh"
29#include "G4SolidStore.hh"
30#include "G4SystemOfUnits.hh"
31#include "G4UnitsTable.hh"
33#include "TGeoManager.h"
34#include "TGeoMatrix.h"
43 fIsConstructed(kFALSE),
54 fIsConstructed(kFALSE),
60 if (!geom || !geom->IsClosed()) {
61 G4Exception(
"TG4RootDetectorConstruction::TG4RootDetectorConstruction",
62 "G4Root_F001", FatalException,
63 "Cannot create TG4RootDetectorConstruction without closed ROOT geometry "
73#ifdef G4GEOMETRY_VOXELDEBUG
74 G4cout <<
"Deleting Materials ... ";
76 G4MaterialTable* mtab = (G4MaterialTable*)G4Material::GetMaterialTable();
77 std::vector<G4Material*>::iterator pos;
79 for (pos = mtab->begin(); pos != mtab->end(); pos++) {
85#ifdef G4GEOMETRY_VOXELDEBUG
86 G4cout << icount <<
" materials deleted !" << G4endl;
87 G4cout <<
"Deleting Elements ... ";
89 G4ElementTable* eltab = (G4ElementTable*)G4Element::GetElementTable();
90 std::vector<G4Element*>::iterator pos1;
92 for (pos1 = eltab->begin(); pos1 != eltab->end(); pos1++) {
98#ifdef G4GEOMETRY_VOXELDEBUG
99 G4cout << icount <<
" elements deleted !" << G4endl;
100 G4cout <<
"Deleting Rotations ... ";
102 G4PhysicalVolumeStore* pvstore = G4PhysicalVolumeStore::GetInstance();
103 std::vector<G4VPhysicalVolume*>::iterator pos2;
105 for (pos2 = pvstore->begin(); pos2 != pvstore->end(); pos2++) {
106 if (*pos2 && (*pos2)->GetRotation()) {
107 delete (*pos2)->GetRotation();
111#ifdef G4GEOMETRY_VOXELDEBUG
112 G4cout << icount <<
" rotations deleted !" << G4endl;
114 G4GeometryManager* mgr = G4GeometryManager::GetInstance();
117 G4LogicalVolumeStore* lvstore = G4LogicalVolumeStore::GetInstance();
119 G4SolidStore* sstore = G4SolidStore::GetInstance();
144 G4Exception(
"TG4RootDetectorConstruction::Construct",
"G4Root_F001",
146 "Cannot create TG4RootDetectorConstruction without closed ROOT geometry "
158 nav->SetWorldVolume(
fTopPV);
159 G4cout <<
"### INFO: TG4RootDetectorConstruction::Construct() finished"
168 G4cout <<
"TG4RootDetectorConstruction::ConstructSDandField" << G4endl;
171 <<
"### INFO: TG4RootDetectorConstruction::ConstructSDandField finished"
179 TIter next(
fGeometry->GetListOfVolumes());
181 while ((vol = (TGeoVolume*)next())) {
184 G4cout <<
"===> GEANT4 logical volumes created and mapped to TGeo ones..."
192 TGeoNode* node =
fGeometry->GetTopNode();
194 TGeoIterator next(
fGeometry->GetTopVolume());
196 while ((node = next())) {
197 mother = next.GetNode(next.GetLevel() - 1);
198 if (mother && node->GetMotherVolume() != mother->GetVolume())
199 node->SetMotherVolume(mother->GetVolume());
204 <<
"===> GEANT4 physical volumes created and mapped to TGeo hierarchy..."
213 if (G4UnitDefinition::GetUnitsTable().size() == 0)
214 G4UnitDefinition::BuildUnitsTable();
218 TIter next(
fGeometry->GetListOfMaterials());
221 G4cout <<
"===> GEANT4 materials created and mapped to TGeo ones..."
229 TGeoElementTable* table =
fGeometry->GetElementTable();
230 Int_t nelements = table->GetNelements();
233 G4String name, symbol;
234 for (Int_t i = 0; i < nelements; i++) {
235 elem = table->GetElement(i);
236 a = G4double(elem->A()) * (g / mole);
237 z = G4double(elem->Z());
238 if ((z < 1) || (z > 101))
continue;
239 name = elem->GetTitle();
240 symbol = elem->GetName();
241 new G4Element(name, symbol, z, a);
243 G4cout <<
"===> GEANT4 elements created..." << G4endl;
252 if (!vol)
return NULL;
254 if (pVolume)
return pVolume;
255 G4String sname(vol->GetName());
258 G4ExceptionDescription description;
259 description <<
" " <<
"Cannot make solid from shape: "
260 << vol->GetShape()->GetName();
261 G4Exception(
"TG4RootDetectorConstruction::CreateG4LogicalVolume",
262 "G4Root_F002", FatalException, description);
264 G4Material* pMaterial = 0;
265 if (vol->IsAssembly()) {
273 G4ExceptionDescription description;
275 <<
"Cannot make material for volume: " << vol->GetName()
277 G4Exception(
"TG4RootDetectorConstruction::CreateG4LogicalVolume",
278 "G4Root_F003", FatalException, description);
281 new G4LogicalVolume(pSolid, pMaterial, sname, NULL, NULL, NULL,
false);
292 if (!node)
return NULL;
295 if (pPhysicalVolume)
return pPhysicalVolume;
296 TGeoMatrix* mat = node->GetMatrix();
297 const Double_t* tr = mat->GetTranslation();
298 G4ThreeVector tlate(tr[0] * cm, tr[1] * cm, tr[2] * cm);
300 G4String pName(node->GetVolume()->GetName());
302 if (!pCurrentLogical) {
303 G4ExceptionDescription description;
304 description <<
" " <<
"No G4 volume created for TGeo node "
305 << node->GetName() << G4endl;
306 G4Exception(
"TG4RootDetectorConstruction::CreateG4PhysicalVolume",
307 "G4Root_F004", FatalException, description);
309 G4LogicalVolume* pMotherLogical =
311 if (!pMotherLogical && node !=
fGeometry->GetTopNode()) {
312 G4ExceptionDescription description;
313 description <<
" " <<
"No G4 mother volume crated for TGeo node "
315 G4Exception(
"TG4RootDetectorConstruction::CreateG4PhysicalVolume",
316 "G4Root_F005", FatalException, description);
318 G4bool pMany =
false;
319 G4int pCopyNo = node->GetNumber();
321 pPhysicalVolume =
new G4PVPlacement(
322 pRot, tlate, pCurrentLogical, pName, pMotherLogical, pMany, pCopyNo);
325 return pPhysicalVolume;
330 const TGeoMaterial* mat)
335 if (pMaterial)
return pMaterial;
336 G4State state = kStateUndefined;
337 G4double temp = mat->GetTemperature();
338 G4double pressure = mat->GetPressure();
339 switch (mat->GetState()) {
340 case TGeoMaterial::kMatStateUndefined:
341 state = kStateUndefined;
343 case TGeoMaterial::kMatStateSolid:
346 case TGeoMaterial::kMatStateLiquid:
347 state = kStateLiquid;
349 case TGeoMaterial::kMatStateGas:
353 G4String elname, symbol;
354 TGeoElementTable* table =
fGeometry->GetElementTable();
355 G4String name(mat->GetName());
356 G4double density = mat->GetDensity() * (g / cm3);
357 if (density < universe_mean_density || mat->GetZ() < 1.) {
358 density = universe_mean_density;
359 pMaterial =
new G4Material(name, 1., 1.01 * g / mole, density, kStateGas,
360 STP_Temperature, 3.e-18 * pascal);
366 if (mat->IsMixture()) {
368 const TGeoMixture* mixt = (
const TGeoMixture*)mat;
369 G4int nComponents = mixt->GetNelements();
372 new G4Material(name, density, nComponents, state, temp, pressure);
373 for (Int_t i = 0; i < nComponents; i++) {
374 TGeoElement* elem = mixt->GetElement(i);
375 G4Element* pElement =
nullptr;
376 if (elem->GetNisotopes() > 0) {
380 TGeoElement* elemDb = table->GetElement(Int_t(mixt->GetZmixt()[i]));
382 G4ExceptionDescription description;
384 <<
"Woops: no element corresponding to Z=" << elemDb->Z();
385 G4Exception(
"TG4RootDetectorConstruction::CreateG4Material",
386 "G4Root_F006", FatalException, description);
388 elname = elemDb->GetTitle();
389 symbol = elemDb->GetName();
390 pElement =
new G4Element(elname, symbol, G4double(mixt->GetZmixt()[i]),
391 G4double(mixt->GetAmixt()[i]) * (g / mole));
393 pMaterial->AddElement(pElement, mixt->GetWmixt()[i]);
399 if (mat->GetElement()
402 pMaterial =
new G4Material(name, density, 1, state, temp, pressure);
403 pMaterial->AddElement(pElement, 1.);
406 pMaterial =
new G4Material(name, G4double(mat->GetZ()),
407 mat->GetA() * g / mole, density, state, temp, pressure);
419 G4Element* pElement =
420 G4Element::GetElement(elem->GetTitle(),
false);
423 if (pElement !=
nullptr) {
428 if (elem->HasIsotopes() > 0) {
429 G4int nIsotopes = elem->GetNisotopes();
430 for (G4int i = 0; i < nIsotopes; i++) {
431 TGeoIsotope* rIso = elem->GetIsotope(i);
433 G4String elname = elem->GetTitle();
435 fGeometry->GetElementTable()->GetElement(rIso->GetZ())->GetName();
436 pElement =
new G4Element(elname, symbol, nIsotopes);
438 G4Isotope* pIso =
new G4Isotope(
439 rIso->GetName(), rIso->GetZ(), rIso->GetN(), rIso->GetA() * (g / mole));
440 pElement->AddIsotope(pIso, elem->GetRelativeAbundance(i));
442 G4cout <<
"Created element " << pElement->GetName()
443 <<
" with user-defined isotope composition" << G4endl;
444 G4cout << pElement << G4endl;
447 TGeoElementTable* table =
fGeometry->GetElementTable();
448 TGeoElement* elemDb = table->GetElement(elem->Z());
450 G4ExceptionDescription description;
452 <<
"Woops: no element corresponding to Z=" << elemDb->Z();
453 G4Exception(
"TG4RootDetectorConstruction::CreateG4Material",
454 "G4Root_F006", FatalException, description);
456 G4String elname = elemDb->GetTitle();
457 G4String symbol = elemDb->GetName();
458 pElement =
new G4Element(
459 elname, symbol, G4double(elem->Z()), G4double(elem->A()) * (g / mole));
466 const TGeoMatrix* matrix)
470 G4RotationMatrix* g4rot = 0;
471 if (matrix->IsRotation()) {
473 const Double_t* marray = matrix->GetRotationMatrix();
475 invmat[0] = marray[0];
476 invmat[1] = marray[3];
477 invmat[2] = marray[6];
478 invmat[3] = marray[1];
479 invmat[4] = marray[4];
480 invmat[5] = marray[7];
481 invmat[6] = marray[2];
482 invmat[7] = marray[5];
483 invmat[8] = marray[8];
484 CLHEP::HepRep3x3 mclhep(invmat);
485 g4rot =
new G4RotationMatrix(mclhep);
502 const TGeoMaterial* mat)
const
512 const TGeoVolume* vol)
const
522 const G4LogicalVolume* g4vol)
const
526 if (it !=
fVolumeMap.end())
return it->second;
532 const TGeoNode* node)
const
542 const G4VPhysicalVolume* g4pvol)
const
Definition of the TG4RootDetectorConstruction and TVirtualUserPostDetConstruction classes.
Definition of the TG4RootNavMgr class.
Definition of the TG4RootNavigator class.
Definition of the TG4RootSolid class.
PVolumeMap_t::const_iterator PVolumeIt_t
the constant iterator for the map from G4VPhysicalVolume to TGeoNode
PVolumeMap_t fPVolumeMap
map of TGeo volumes
TVirtualUserPostDetConstruction * fSDInit
Sensitive detector hook.
G4VPhysicalVolume * GetG4VPhysicalVolume(const TGeoNode *node) const
G4VPhysicalVolume * CreateG4PhysicalVolume(TGeoNode *node)
G4PVolumeMap_t fG4PVolumeMap
map of G4 physical volumes
G4MaterialMap_t::const_iterator G4MaterialIt_t
the constant iterator for the map from TGeoMaterial to G4Material
G4RotationMatrix * CreateG4Rotation(const TGeoMatrix *matrix)
VolumeMap_t fVolumeMap
map of TGeo volumes
G4MaterialMap_t::value_type G4MaterialVal_t
the value type for the map from TGeoMaterial to G4Material
TGeoNode * GetNode(const G4VPhysicalVolume *g4vol) const
virtual ~TG4RootDetectorConstruction()
G4PVolumeMap_t::const_iterator G4PVolumeIt_t
the constant iterator for the map from TGeoNode to G4VPhysicalVolume
G4MaterialMap_t fG4MaterialMap
map of G4 materials
TG4RootDetectorConstruction()
G4Material * CreateG4Material(const TGeoMaterial *mat)
void Initialize(TVirtualUserPostDetConstruction *sdinit=0)
G4VPhysicalVolume * fTopPV
World G4 physical volume.
PVolumeMap_t::value_type PVolumeVal_t
the value type for the map from G4VPhysicalVolume to TGeoNode
Bool_t fIsConstructed
flag Construct() called
VolumeMap_t::value_type VolumeVal_t
the value type for the map from G4LogicalVolume to TGeoVolume
virtual G4VPhysicalVolume * Construct()
G4LogicalVolume * GetG4Volume(const TGeoVolume *vol) const
G4VSolid * CreateG4Solid(TGeoShape *shape)
G4Material * GetG4Material(const TGeoMaterial *mat) const
G4VolumeMap_t fG4VolumeMap
map of G4 volumes
void CreateG4PhysicalVolumes()
G4VolumeMap_t::const_iterator G4VolumeIt_t
the constant iterator for the map from TGeoVolume to G4LogicalVolume
TGeoVolume * GetVolume(const G4LogicalVolume *g4vol) const
G4PVolumeMap_t::value_type G4PVolumeVal_t
the value type for the map from TGeoNode to G4VPhysicalVolume
VolumeMap_t::const_iterator VolumeIt_t
the constant iterator for the map from G4LogicalVolume to TGeoVolume
void CreateG4LogicalVolumes()
G4Element * CreateG4Element(TGeoElement *elem)
virtual void ConstructSDandField()
G4VolumeMap_t::value_type G4VolumeVal_t
the value type for the map from TGeoVolume to G4LogicalVolume
G4LogicalVolume * CreateG4LogicalVolume(TGeoVolume *vol)
TGeoManager * fGeometry
TGeo geometry manager.
Manager class creating a G4Navigator based on a ROOT geometry.
TG4RootNavigator * GetNavigator() const
Return the G4 navigator working with TGeo.
static TG4RootNavMgr * GetInstance(TGeoManager *geom=0)
GEANT4 navigator using directly a TGeo geometry.
void SetDetectorConstruction(TG4RootDetectorConstruction *dc)
GEANT4 solid implemented by a ROOT shape.
Abstract class for defining links to G4 geometry.
virtual void InitializeSDandField()
virtual void Initialize(TG4RootDetectorConstruction *dc)=0
Initialize.