Geant4 VMC Version 6.6
Loading...
Searching...
No Matches
TG4GeometryManager.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Geant4 Virtual Monte Carlo package
3// Copyright (C) 2007 - 2015 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
14
15#include "TG4GeometryManager.h"
16#include "TG4BiasingManager.h"
17#include "TG4Field.h"
18#include "TG4MagneticField.h"
19#include "TG4FieldParameters.h"
20#include "TG4G3ControlVector.h"
21#include "TG4G3CutVector.h"
22#include "TG4G3Units.h"
23#include "TG4GeometryServices.h"
24#include "TG4Globals.h"
25#include "TG4Limits.h"
26#include "TG4MCGeometry.h"
27#include "TG4Medium.h"
28#include "TG4MediumMap.h"
32#include "TG4SDManager.h"
33#include "TG4StateManager.h"
36
37#ifdef USE_G4ROOT
38#include "TG4RootDetectorConstruction.h"
39#endif
40
41#include <G4FieldManager.hh>
42#include <G4LogicalVolumeStore.hh>
43#include <G4Material.hh>
45#include <G4PVPlacement.hh>
46#include <G4ReflectionFactory.hh>
47//#include <G4SystemOfUnits.hh>
48#include <G4TransportationManager.hh>
49
50#include <TGeoMCGeometry.h>
51#include <TGeoManager.h>
52#include <TGeoMedium.h>
53#include <TGeoVolume.h>
54#include <TList.h>
55#include <TVirtualMC.h>
56#include <TVirtualMCApplication.h>
57
58// Moved after ROOT includes to avoid warnings about shadowing variables
59// from CLHEP units
60#include <G4SystemOfUnits.hh>
61
62#ifdef USE_G3TOG4
63#include <G3MatTable.hh>
64#include <G3MedTable.hh>
65#include <G3SensVolVector.hh>
66#include <G3VolTable.hh>
67#include <G3toG4.hh>
68#include <G3toG4BuildTree.hh>
69#include <G3toG4MANY.hh>
70#endif
71
72#ifdef USE_VGM
73#include <Geant4GM/volumes/Factory.h>
74#include <RootGM/volumes/Factory.h>
75#endif
76
78const G4double TG4GeometryManager::fgDefaultLimitDensity = 0.001 * (g / cm3);
79const G4double TG4GeometryManager::fgDefaultMaxStep = 10 * cm;
80
81G4ThreadLocal std::vector<TG4Field*>* TG4GeometryManager::fgFields = 0;
82
83//_____________________________________________________________________________
84TG4GeometryManager::TG4GeometryManager(const TString& userGeometry)
85 : TG4Verbose("geometryManager"),
86 fMessenger(this),
87 fGeometryServices(new TG4GeometryServices()),
88 fMCGeometry(0),
89 fRootDetectorConstruction(0),
90 fOpManager(0),
91 fFastModelsManager(0),
92 fEmModelsManager(0),
93 fBiasingManager(0),
94 fUserGeometry(userGeometry),
95 fFieldParameters(),
96 fUserRegionConstruction(0),
97 fUserPostDetConstruction(0),
98 fIsLocalField(false),
99 fIsZeroField(false),
100 fIsUserMaxStep(false),
101 fIsMaxStepInLowDensityMaterials(true),
102 fLimitDensity(fgDefaultLimitDensity),
103 fMaxStepInLowDensityMaterials(fgDefaultMaxStep)
104
105{
107
108 if (fgInstance) {
109 TG4Globals::Exception("TG4GeometryManager",
110 "TG4GeometryManager:", "Cannot create two instances of singleton.");
111 }
112
113 // Field parameters for global field
114 fFieldParameters.push_back(new TG4FieldParameters());
115
117
119
120 fFastModelsManager = new TG4ModelConfigurationManager("fastSimulation");
122 fBiasingManager = new TG4BiasingManager("biasing");
123
124 fgInstance = this;
125}
126
127//_____________________________________________________________________________
129{
131
132 for (G4int i = 0; i < G4int(fFieldParameters.size()); ++i) {
133 delete fFieldParameters[i];
134 }
135
136 delete fgFields;
137 // magnetic field objects are deleted via G4 kernel
138
139 delete fGeometryServices;
140 delete fOpManager;
141 delete fFastModelsManager;
142 delete fEmModelsManager;
143 delete fBiasingManager;
144
145 fgInstance = 0;
146 fgFields = 0;
147}
148
149//
150// private methods
151//
152
153//_____________________________________________________________________________
155{
157
158 if (fUserGeometry == "VMCtoGeant4" || fUserGeometry == "Geant4" ||
159 fUserGeometry == "RootToGeant4") {
161 }
162
163 if (fUserGeometry == "VMCtoRoot" || fUserGeometry == "Root" ||
164 fUserGeometry == "VMC+RootToGeant4") {
165 if (!gGeoManager) new TGeoManager("TGeo", "Root geometry manager");
166 fMCGeometry = new TGeoMCGeometry();
167 }
168}
169
170//_____________________________________________________________________________
172{
174
175#ifdef USE_G3TOG4
176 if (VerboseLevel() > 1)
177 G4cout << "TG4GeometryManager::ConstructG4GeometryViaVMC" << G4endl;
178
179 // check if G4 tables were filled
180 /*
181 if ( ! TG4G3MCGeometry::Instance()->IsGeometryDefined() ) {
182 TG4Globals::Exception(
183 "TG4GeometryManager", "ConstructG4GeometryViaVMC",
184 "Geometry was not defined via VMC.");
185 }
186 */
187 // pass info about using G3toG4 to geometry services
189
190 // set the first entry in the G3Vol table
191 G4ggclos();
192 G3VolTableEntry* first = G3Vol.GetFirstVTE();
193
194 // transform MANY to Boolean solids
195 G3toG4MANY(first);
196
197 // create G4 geometry
198 G3toG4BuildTree(first, 0);
199
200 // fill medium map
201 // FillMediumMapFromG3();
202
203 // position the first entry with copyNo = 1
204 // (in Geant3 the top volume cannot be positioned)
205 //
206 if (!fGeometryServices->GetWorld()) {
207 G4VPhysicalVolume* world = new G4PVPlacement(
208 0, G4ThreeVector(), first->GetName(), first->GetLV(), 0, false, 1);
210 }
211
212 // print G3 volume table statistics
213 G3Vol.VTEStat();
214
215#else
216 TG4Globals::Exception("TG4GeometryManager", "ConstructG4GeometryViaVMC",
217 "Geant4 VMC has been installed without G3toG4." + TG4Globals::Endl() +
218 "Geometry construction via VMC is not supported.");
219#endif
220}
221
222//_____________________________________________________________________________
224{
227
228#ifdef USE_VGM
229 if (VerboseLevel() > 1)
230 G4cout << "TG4GeometryManager::ConstructG4GeometryViaVGM" << G4endl;
231
232 // Check Root manager
233 if (!gGeoManager) {
234 TG4Globals::Exception("TG4GeometryManager", "ConstructG4GeometryViaVGM",
235 "Geometry was not defined via Root.");
236 }
237
238 // Get and eventually also set the Root top volume
239 TGeoVolume* topVolume = gGeoManager->GetTopVolume();
240 if (!topVolume) {
241 topVolume = (TGeoVolume*)gGeoManager->GetListOfVolumes()->First();
242 if (!topVolume) {
243 TG4Globals::Exception("TG4GeometryManager", "ConstructG4GeometryViaVGM",
244 "Root top volume not found.");
245 }
246 gGeoManager->SetTopVolume(topVolume);
247 }
248
249 // Close Root geometry
250 if (!gGeoManager->IsClosed()) gGeoManager->CloseGeometry();
251
252 // Convert Root geometry to G4
253 if (VerboseLevel() > 0)
254 G4cout << "Converting Root geometry to Geant4 via VGM ... " << G4endl;
255
256 // import Root geometry in VGM
257 RootGM::Factory rootFactory;
258 if (VerboseLevel() > 1) rootFactory.SetDebug(1);
259 rootFactory.SetIgnore(true);
260 rootFactory.Import(gGeoManager->GetTopNode());
261
262 // export Root VGM geometry in Geant4
263 Geant4GM::Factory g4Factory;
264 if (VerboseLevel() > 1) g4Factory.SetDebug(1);
265 rootFactory.Export(&g4Factory);
266
267 G4VPhysicalVolume* g4World = g4Factory.World();
268 fGeometryServices->SetWorld(g4World);
269
270#else
271 TG4Globals::Exception("TG4GeometryManager", "ConstructG4GeometryViaVGM",
272 "Geant4 VMC has been installed without VGM." + TG4Globals::Endl() +
273 "Root geometry conversion is not supported.");
274#endif
275}
276
277//_____________________________________________________________________________
279{
281
282 if (VerboseLevel() > 1) {
283 G4cout << "TG4GeometryManager::ConstructG4Geometry: "
284 << "userGeometry=" << fUserGeometry << G4endl;
285 }
286
287 // VMC application construct geometry
288 if (fUserGeometry == "VMCtoGeant4") {
289
290 if (VerboseLevel() > 1)
291 G4cout << "Running TVirtualMCApplication::ConstructGeometry" << G4endl;
292
294 TVirtualMCApplication::Instance()->ConstructGeometry();
296 TVirtualMCApplication::Instance()->MisalignGeometry();
298 }
299
300 // VMC application construct geometry
301 if (fUserGeometry == "RootToGeant4" || fUserGeometry == "VMC+RootToGeant4") {
302 if (VerboseLevel() > 1)
303 G4cout << "Running TVirtualMCApplication::ConstructGeometry" << G4endl;
304
305#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 22, 8)
306 // Set Root default units to TGeo
307 TGeoManager::LockDefaultUnits(false);
308 TGeoManager::SetDefaultUnits(TGeoManager::kRootUnits);
309 TGeoManager::LockDefaultUnits(true);
310#endif
311
313 TVirtualMCApplication::Instance()->ConstructGeometry();
315
316 // If Root geometry was not closed by user
317 // we have to do it here
318 if (!gGeoManager->IsClosed()) {
319 TGeoVolume* top = (TGeoVolume*)gGeoManager->GetListOfVolumes()->First();
320 gGeoManager->SetTopVolume(top);
321 gGeoManager->CloseGeometry();
322 }
323
325 TVirtualMCApplication::Instance()->MisalignGeometry();
327 }
328
329 // Build G4 geometry
330 if (fUserGeometry == "VMCtoGeant4") ConstructG4GeometryViaVMC();
331
332 if (fUserGeometry == "RootToGeant4" || fUserGeometry == "VMC+RootToGeant4")
334
335 // print G4 geometry statistics
336 if (VerboseLevel() > 0) {
337 G4cout << "G4 Stat: instantiated "
338 << fGeometryServices->NofG4LogicalVolumes() << " logical volumes \n"
339 << " "
340 << fGeometryServices->NofG4PhysicalVolumes() << " physical volumes"
341 << G4endl;
342 }
343}
344
345//_____________________________________________________________________________
347{
349
350#ifdef USE_G3TOG4
351 if (VerboseLevel() > 1)
352 G4cout << "TG4GeometryManager::FillMediumMapFromG3()" << G4endl;
353
355
356 // Create medium for each medium entry
357 for (G4int i = 0; i < G4int(G3Med.GetSize()); i++) {
358 G3MedTableEntry* mediumEntry = G3Med.GetMTE(i);
359 G4int mediumID = mediumEntry->GetID();
360
361 if (VerboseLevel() > 2) {
362 G4cout << "Getting medium ID=" << mediumID << G4endl;
363 }
364 // Get medium from medium map
365 TG4Medium* medium = mediumMap->GetMedium(mediumID);
366
367 // Create a medium if it does not exist
368 // (This should not happen, but let's check it anyway)
369 if (!medium) {
370 medium = mediumMap->AddMedium(mediumID, false);
371
372 TString message = "Medium ";
373 message += mediumID;
374 message += " was not found in medium map. New medium will be created";
375 TG4Globals::Warning("TG4GeometryManager", "FillMediumMapFromG3", message);
376 }
377
378 medium->SetLimits(mediumEntry->GetLimits());
379 medium->SetMaterial(mediumEntry->GetMaterial());
380 }
381
382 if (VerboseLevel() > 2) G3Vol.PrintAll();
383
384 // Map media to logical volumes
385 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
386 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
387 G4LogicalVolume* lv = (*lvStore)[i];
388
389 // Get medium ID from G3 tables
390 G4String name = lv->GetName();
391 G4String g3Name(name);
392 // Filter out the reflected volume name extension
393 // added by reflection factory
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();
397
398 if (VerboseLevel() > 2)
399 G4cout << "Mapping medium Id " << mediumID << " to LV " << name << G4endl;
400
401 // Map medium to LV
402 mediumMap->MapMedium(lv, mediumID);
403 }
404
405 // clear G3 tables
406 G3Vol.Clear();
407 G3SensVol.clear();
408 G3Mat.Clear();
409 G3Med.Clear();
410#else
411 TG4Globals::Exception("TG4GeometryManager", "FillMediumMapFromG3",
412 "Geant4 VMC has been installed without G3toG4." + TG4Globals::Endl() +
413 "Geometry construction via VMC is not supported.");
414#endif
415}
416
417//_____________________________________________________________________________
419{
422
423 if (VerboseLevel() > 1)
424 G4cout << "TG4GeometryManager::FillMediumMapFromG4()" << G4endl;
425
427
428 // Create medium for each material
429 const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
430 for (G4int i = 0; i < G4int(materialTable->size()); i++) {
431 G4Material* material = (*materialTable)[i];
432
433 if (VerboseLevel() > 2) {
434 G4cout << "Adding medium name= " << material->GetName()
435 << " Id=" << material->GetIndex() << G4endl;
436 }
437 TG4Medium* medium = mediumMap->AddMedium(material->GetIndex());
438 medium->SetName(material->GetName());
439 medium->SetMaterial(material);
440 }
441
442 // Map media to logical volumes
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();
447
448 if (VerboseLevel() > 2) {
449 G4cout << "Mapping medium Id=" << mediumID << " to LV= " << lv->GetName()
450 << G4endl;
451 }
452 mediumMap->MapMedium(lv, mediumID);
453 }
454}
455
456//_____________________________________________________________________________
458{
460
461 if (VerboseLevel() > 1)
462 G4cout << "TG4GeometryManager::FillMediumMapFromRoot()" << G4endl;
463
464 // fGeometryServices->PrintLogicalVolumeStore();
465
467
468 // Create TG4 medium for each TGeo madium
469 TIter next(gGeoManager->GetListOfMedia());
470 TGeoMedium* geoMedium;
471 while ((geoMedium = (TGeoMedium*)next())) {
472 Int_t mediumId = geoMedium->GetId();
473 G4String mediumName = geoMedium->GetName();
474
475 // Int_t isvol = (Int_t) geoMedium->GetParam(0);
476 Int_t ifield = (Int_t)geoMedium->GetParam(1);
477 // Double_t fieldm = geoMedium->GetParam(2);
478 // Double_t tmaxfd = geoMedium->GetParam(3);
479 Double_t stemax = geoMedium->GetParam(4);
480 // Double_t deemax = geoMedium->GetParam(5);
481 // Double_t epsil = geoMedium->GetParam(6);
482 // Double_t stmin = geoMedium->GetParam(7);
483
484 // Only stemax parameter is passed to G4 if it is positive
485 G4UserLimits* limits = 0;
486 if (stemax > 0) {
487 limits = new G4UserLimits();
488 limits->SetMaxAllowedStep(stemax * cm);
489 }
490
491 if (VerboseLevel() > 2) {
492 G4cout << "Adding medium Id=" << mediumId << " name=" << mediumName
493 << " limits=" << limits << G4endl;
494 }
495 TG4Medium* medium = mediumMap->AddMedium(mediumId);
496 medium->SetName(mediumName);
497 medium->SetLimits(limits);
498 medium->SetIfield(ifield);
499
500 G4String matName = geoMedium->GetMaterial()->GetName();
501 G4Material* material = G4Material::GetMaterial(matName);
502 if (!material) {
503 TG4Globals::Exception("TG4GeometryManager", "FillMediumMapFromRoot",
504 "Material " + TString(matName) + " not found.");
505 }
506 medium->SetMaterial(material);
507 }
508
509 // Map media to logical volumes
510 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
511 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
512 G4LogicalVolume* lv = (*lvStore)[i];
513
514 TGeoVolume* geoVolume = nullptr;
515
516 if (fRootDetectorConstruction == nullptr) {
517 G4String volName = lv->GetName();
518
519 // Filter out the reflected volumes name extension
520 // added by reflection factory
521 G4String ext = G4ReflectionFactory::Instance()->GetVolumesNameExtension();
522 if (volName.find(ext)) volName = volName.substr(0, volName.find(ext));
523
524 geoVolume = gGeoManager->GetVolume(volName.data());
525 }
526 else {
527#ifdef USE_G4ROOT
528 geoVolume = fRootDetectorConstruction->GetVolume(lv);
529#endif
530 }
531
532 if (!geoVolume) {
533 TG4Globals::Exception("TG4GeometryManager", "FillMediumMapFromRoot",
534 "Root volume " + TString(lv->GetName()) + " not found");
535 }
536
537 // skip assemblies
538 if (geoVolume && geoVolume->IsAssembly()) continue;
539
540 if (geoVolume && !geoVolume->GetMedium()) {
541 TG4Globals::Exception("TG4GeometryManager", "FillMediumMapFromRoot",
542 "Root volume " + TString(lv->GetName()) + " has not medium defined.");
543 }
544
545 G4int mediumID = geoVolume->GetMedium()->GetId();
546
547 if (VerboseLevel() > 2) {
548 G4cout << "Mapping medium Id=" << mediumID << " to LV=" << lv->GetName()
549 << G4endl;
550 }
551 mediumMap->MapMedium(lv, mediumID);
552 }
553}
554
555//_____________________________________________________________________________
557{
559
560 if (fUserGeometry == "VMCtoGeant4") FillMediumMapFromG3();
561
562 if (fUserGeometry == "VMCtoRoot" || fUserGeometry == "Root" ||
563 fUserGeometry == "RootToGeant4" || fUserGeometry == "VMC+RootToGeant4") {
565 }
566
567 if (fUserGeometry == "Geant4") FillMediumMapFromG4();
568}
569
570//_____________________________________________________________________________
571void TG4GeometryManager::CreateField(TVirtualMagField* magField,
572 TG4FieldParameters* fieldParameters, G4LogicalVolume* lv)
573{
575
576 TG4Field* tg4Field = new TG4Field(*fieldParameters, magField, lv);
577
578 if (VerboseLevel() > 0) {
579 G4String fieldType =
581 G4bool isCachedMagneticField = (fieldParameters->GetConstDistance() > 0.);
582 if (!lv) {
583 fieldType = "Global";
584 }
585 else {
586 fieldType = "Local (in ";
587 fieldType.append(lv->GetName());
588 fieldType.append(")");
589 }
590 if (isCachedMagneticField) {
591 fieldType.append(" cached");
592 }
593
594 G4cout << fieldType << " field created with stepper ";
596 fieldParameters->GetStepperType())
597 << G4endl;
598 }
599
600 // create magnetic field vector
601 if (!fgFields) {
602 fgFields = new std::vector<TG4Field*>();
603 }
604
605 fgFields->push_back(tg4Field);
606}
607
608//_____________________________________________________________________________
610{
612
613 // Create global magnetic field
614 if (gMC->GetMagField()) {
615 CreateField(gMC->GetMagField(), fFieldParameters[0], 0);
616
617 // create monopole field
618 if (fFieldParameters[0]->GetIsMonopole()) {
619 G4cout << "Create G4MonopoleFieldSetup" << G4endl;
620 G4MonopoleFieldSetup* monFieldSetup =
622 TG4Field* tg4Field = (*fgFields)[0];
623 G4Field* field = tg4Field->GetG4Field();
624 G4MagneticField* magField = dynamic_cast<G4MagneticField*>(field);
625 if (!magField) {
626 // add warning
627 G4cerr << "Wrong field type. Only magnetic field is supported in "
628 "G4MonopoleFieldSetup."
629 << G4endl;
630 }
631 else {
632 monFieldSetup->SetMagneticField(magField);
633 monFieldSetup->SetDefaultEquation(tg4Field->GetEquation());
634 monFieldSetup->SetDefaultStepper(tg4Field->GetStepper());
635 monFieldSetup->InitialiseAll();
636 }
637 }
638
639 if (fIsZeroField) {
641 }
642 }
643}
644
645//_____________________________________________________________________________
647{
651
652 if (VerboseLevel() > 1)
653 G4cout << "TG4GeometryManager::ConstructZeroFields()" << G4endl;
654
655 G4bool forceToAllDaughters = false;
656
657 // Set a dummy field manager to all LV first in order to avoid propagation
658 // of zero field to volume daughters
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);
664 }
665
666 // Set zero field manager to volumes associated with a tracking medium
667 // with ifield value = 0.
668 G4FieldManager* fieldManager = 0;
669 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
670
671 G4LogicalVolume* lv = (*lvStore)[i];
672
673 // skip volume without medium
674 TG4Medium* medium =
676 if (!medium) continue;
677
678 // Skip volumes with ifield != 0
679 if (medium->GetIfield() != 0) {
680 if (VerboseLevel() > 2) {
681 G4cout << "Global field in logical volume: " << lv->GetName() << G4endl;
682 }
683 continue;
684 }
685
686 // create field manager if it does not exist yet
687 if (!fieldManager) {
688 fieldManager = new G4FieldManager();
689 // CHECK if we need to delete it
690 fieldManager->SetDetectorField(0);
691 fieldManager->CreateChordFinder(0);
692 }
693 lv->SetFieldManager(fieldManager, forceToAllDaughters);
694
695 if (VerboseLevel() > 1) {
696 G4cout << "Zero magnetic field set to logical volume: " << lv->GetName()
697 << G4endl;
698 }
699 }
700
701 // Remove the dummy field manager to all LV without zero field
702 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
703 G4LogicalVolume* lv = (*lvStore)[i];
704
705 if (lv->GetFieldManager() == dummyFieldManager) {
706 lv->SetFieldManager(0, forceToAllDaughters);
707 }
708 }
709
710 // Delete the dummy field manager
711 delete dummyFieldManager;
712}
713
714//_____________________________________________________________________________
716 const G4String& volumeName)
717{
720
721 // Get user field parameters
722 TG4FieldParameters* fieldParameters = 0;
723 for (G4int i = 0; i < G4int(fFieldParameters.size()); ++i) {
724 if (fFieldParameters[i]->GetVolumeName() == volumeName) {
725 return fFieldParameters[i];
726 }
727 }
728
729 // Create field parameters if not yet defined
730 fieldParameters = new TG4FieldParameters(volumeName);
731 fFieldParameters.push_back(fieldParameters);
732 return fieldParameters;
733}
734
735//_____________________________________________________________________________
737{
739
740 // Supported only for geomRoot and geomRootToGeant4.
741 if ((fUserGeometry != "Root") && (fUserGeometry != "RootToGeant4")) return;
742
743 if (VerboseLevel() > 1)
744 G4cout << "TG4GeometryManager::ConstructLocalFields()" << G4endl;
745
746 TIter next(gGeoManager->GetListOfVolumes());
747 TGeoVolume* geoVolume;
748 while ((geoVolume = (TGeoVolume*)next())) {
749
750 if (!geoVolume->GetField()) continue;
751
752 // Get field
753 TVirtualMagField* magField =
754 dynamic_cast<TVirtualMagField*>(geoVolume->GetField());
755 if (!magField) {
756 TString message = geoVolume->GetName();
757 message += ": uknown field type will be ignored.";
758 TG4Globals::Warning("TG4GeometryManager", "ConstructLocalFields",
759 "No magnetic field is defined.");
760 continue;
761 }
762
763 // Volume name
764 G4String volumeName = geoVolume->GetName();
765
766 // Get Geant4 volume
767 G4LogicalVolume* lv =
769 if (!lv) {
770 TString message = geoVolume->GetName();
771 message += " volume not found in Geant4 geometry.";
772 TG4Globals::Warning("TG4GeometryManager", "ConstructLocalFields",
773 "No magnetic field is defined.");
774 continue;
775 }
776
777 // Get or create user field parameters
778 TG4FieldParameters* fieldParameters =
779 GetOrCreateFieldParameters(volumeName);
780
781 // Create magnetic field
782 CreateField(magField, fieldParameters, lv);
783 }
784}
785
786//
787// public methods
788//
789
790//_____________________________________________________________________________
792{
795
796 if (!fMCGeometry) {
798 "TG4GeometryManager", "GetMCGeometry", "No MC geometry defined.");
799 }
800
801 return fMCGeometry;
802}
803
804//_____________________________________________________________________________
806{
808
809 // Construct G4 geometry
811
812 // Fill medium map
814
815 // VMC application construct geometry for optical processes
817 TVirtualMCApplication::Instance()->ConstructOpGeometry();
819
820 // Construct user regions
822}
823
824#include "TG4SDManager.h"
825
826//_____________________________________________________________________________
828{
830
831 if (VerboseLevel() > 1)
832 G4cout << "TG4GeometryManager::ConstructSDandField() " << G4endl;
833
834 // Call user class for geometry customization
836
837 // Construct regions with fast simulation and EM models
840
841 // Construct biasing operator
843
844 // Initialize SD manager (create SDs)
846
847 // Create global field
849
850 if (fIsLocalField) {
852 }
853}
854
855//_____________________________________________________________________________
857{
859
860 if (VerboseLevel() > 1)
861 G4cout << "TG4GeometryManager::FinishGeometry" << G4endl;
862
863 // Create magnetic field
864 // ConstructField();
865
866 // Fill medium map if not yet done
868
869 // Set world to geometry services
871 G4TransportationManager::GetTransportationManager()
872 ->GetNavigatorForTracking()
873 ->GetWorldVolume());
874
875 if (VerboseLevel() > 1)
876 G4cout << "TG4GeometryManager::FinishGeometry done" << G4endl;
877}
878
879//_____________________________________________________________________________
881{
885
886 if (!fgFields) {
888 "TG4GeometryManager", "UpdateField", "No magnetic field is defined.");
889 return;
890 }
891
892 if (VerboseLevel() > 1) G4cout << "TG4GeometryManager::UpdateField" << G4endl;
893
894 // Only the parameters defined in TG4Magnetic field can be updated when
895 // field already exists, so we can safely call the base class non virtual
896 // method
897 for (G4int i = 0; i < G4int(fgFields->size()); ++i) {
898 fgFields->at(i)->Update(*fFieldParameters[i]);
899 }
900}
901
902//_____________________________________________________________________________
903void TG4GeometryManager::CreateFieldParameters(const G4String& fieldVolName)
904{
909
910 fFieldParameters.push_back(new TG4FieldParameters(fieldVolName));
911}
912
913//_____________________________________________________________________________
915 const G4String& volName)
916{
918
919 TG4RadiatorDescription* radiatorDescription =
920 new TG4RadiatorDescription(volName);
921 fRadiators.push_back(radiatorDescription);
922
923 return radiatorDescription;
924}
925
926//_____________________________________________________________________________
928 const TG4G3CutVector& cuts, const TG4G3ControlVector& controls) const
929{
931
932 if (VerboseLevel() > 1)
933 G4cout << "TG4GeometryManager::SetUserLimits" << G4endl;
934
935 G4LogicalVolumeStore* lvStore = G4LogicalVolumeStore::GetInstance();
936
937 for (G4int i = 0; i < G4int(lvStore->size()); i++) {
938 G4LogicalVolume* lv = (*lvStore)[i];
939 TG4Medium* medium = fGeometryServices->GetMediumMap()->GetMedium(lv, false);
940
941 if (!medium) continue;
942
943 // get limits if already exist
944 TG4Limits* tg4Limits = 0;
945 G4UserLimits* limits = medium->GetLimits();
946 tg4Limits = fGeometryServices->GetLimits(limits, cuts, controls);
947
948 // get tracking medium name
949 G4String name = medium->GetName();
950
951 if (tg4Limits) {
952 tg4Limits->SetName(name);
953 }
954 else {
955 // Check if the step below is needed
956 tg4Limits = fGeometryServices->FindLimits2(name, true);
957 if (!tg4Limits) {
958 tg4Limits = new TG4Limits(name, cuts, controls);
959 }
960 }
961
962 // set new limits back to medium
963 medium->SetLimits(tg4Limits);
964
965 // inactivate max step defined by user
966 // if its activation was not asked explicitely
967 if (!fIsUserMaxStep) tg4Limits->SetMaxAllowedStep(DBL_MAX);
968
969 // limit max step for low density materials (< AIR)
971 lv->GetMaterial()->GetDensity() < fLimitDensity)
972 tg4Limits->SetMaxAllowedStep(fMaxStepInLowDensityMaterials);
973
974 // set max step the default value
975 tg4Limits->SetDefaultMaxAllowedStep();
976
977 // update controls in limits according to the setup
978 // in the passed vector
979 tg4Limits->Update(controls);
980
981 // set limits to logical volume
982 lv->SetUserLimits(tg4Limits);
983 }
984
985 if (VerboseLevel() > 1)
986 G4cout << "TG4GeometryManager::SetUserLimits done" << G4endl;
987}
988
989//_____________________________________________________________________________
990void TG4GeometryManager::SetIsLocalField(G4bool isLocalField)
991{
993
994 if (VerboseLevel() > 1)
995 G4cout << "TG4GeometryManager::SetIsLocalField: " << std::boolalpha
996 << isLocalField << G4endl;
997
998 fIsLocalField = isLocalField;
999}
1000
1001//_____________________________________________________________________________
1003{
1005
1006 if (VerboseLevel() > 1)
1007 G4cout << "TG4GeometryManager::SetIsZeroField: " << std::boolalpha
1008 << isZeroField << G4endl;
1009
1010 fIsZeroField = isZeroField;
1011}
1012
1013//_____________________________________________________________________________
1015{
1017
1018 if (VerboseLevel() > 0)
1019 G4cout << "TG4GeometryManager::SetIsUserMaxStep: " << std::boolalpha
1020 << isUserMaxStep << G4endl;
1021
1022 fIsUserMaxStep = isUserMaxStep;
1023}
1024
1025//_____________________________________________________________________________
1027{
1029
1030 if (VerboseLevel() > 0)
1031 G4cout << "TG4GeometryManager::SetIsMaxStepInLowDensityMaterials: "
1032 << std::boolalpha << isMaxStep << G4endl;
1033
1035}
1036
1037//_____________________________________________________________________________
1039 TG4VUserRegionConstruction* userRegionConstruction)
1040{
1042
1043 fUserRegionConstruction = userRegionConstruction;
1044}
1045
1046//_____________________________________________________________________________
1048 TG4VUserPostDetConstruction* userPostDetConstruction)
1049{
1051
1052 fUserPostDetConstruction = userPostDetConstruction;
1053}
1054
1055//_____________________________________________________________________________
1057 G4EquationOfMotion* equation, G4String volumeName)
1058{
1059 if (!volumeName.size()) {
1060 // global field
1061 fFieldParameters[0]->SetUserEquationOfMotion(equation);
1062 }
1063 else {
1064 // local field
1065 // Get or create user field parameters
1066 TG4FieldParameters* fieldParameters =
1067 GetOrCreateFieldParameters(volumeName);
1068 fieldParameters->SetUserEquationOfMotion(equation);
1069 }
1070}
1071
1072//_____________________________________________________________________________
1074 G4MagIntegratorStepper* stepper, G4String volumeName)
1075{
1076 if (!volumeName.size()) {
1077 // global field
1078 fFieldParameters[0]->SetUserStepper(stepper);
1079 }
1080 else {
1081 // local field
1082 // Get or create user field parameters
1083 TG4FieldParameters* fieldParameters =
1084 GetOrCreateFieldParameters(volumeName);
1085 fieldParameters->SetUserStepper(stepper);
1086 }
1087}
1088
1089//_____________________________________________________________________________
1091{
1094 if (VerboseLevel() > 0 && fgFields) {
1095 for (G4int i = 0; i < G4int(fgFields->size()); ++i) {
1096 auto f = fgFields->at(i); // this is a TG4Field
1097 // we need to get the containing TG4MagneticField in order to print statistics
1098 auto mgfield = dynamic_cast<TG4MagneticField*>(f->GetG4Field());
1099 if (mgfield) {
1100 mgfield->PrintStatistics();
1101 }
1102 }
1103 }
1104}
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)
The biasing manager.
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...
Definition TG4Field.h:48
G4MagIntegratorStepper * GetStepper() const
Definition TG4Field.h:103
G4Field * GetG4Field() const
Definition TG4Field.h:91
G4EquationOfMotion * GetEquation() const
Definition TG4Field.h:97
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
TG4BiasingManager * fBiasingManager
Biasing manager.
TG4ModelConfigurationManager * fFastModelsManager
Fast simulation models manager.
TVirtualMCGeometry * GetMCGeometry() const
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)
std::vector< TG4FieldParameters * > fFieldParameters
Field parameters.
TG4GeometryManager(const TString &userGeometry)
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 CreateFieldParameters(const G4String &fieldVolName)
G4double fLimitDensity
material density limit for setting max allowed step
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.
TG4RootDetectorConstruction * fRootDetectorConstruction
Root detector construction.
static const G4double fgDefaultLimitDensity
default material density limit for setting max allowed step
void SetIsUserMaxStep(G4bool isUserMaxStep)
void SetUserLimits(const TG4G3CutVector &cuts, const TG4G3ControlVector &controls) const
TG4VUserPostDetConstruction * fUserPostDetConstruction
User post detector construction.
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
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
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)
static TString Endl()
Definition TG4Globals.h:100
Extended G4UserLimits class.
Definition TG4Limits.h:38
G4bool Update(const TG4G3ControlVector &controls)
void SetDefaultMaxAllowedStep()
void SetName(const G4String &name)
Definition TG4Limits.h:123
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.
Definition TG4Medium.h:29
G4UserLimits * GetLimits() const
Definition TG4Medium.h:99
G4int GetIfield() const
Definition TG4Medium.h:104
void SetLimits(G4UserLimits *limits)
Definition TG4Medium.cxx:83
void SetIfield(G4int ifield)
Definition TG4Medium.h:79
G4String GetName() const
Definition TG4Medium.h:89
void SetMaterial(G4Material *material)
Definition TG4Medium.cxx:68
void SetName(const G4String &name)
Definition TG4Medium.cxx:55
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.
Definition TG4Verbose.h:36
virtual G4int VerboseLevel() const
Definition TG4Verbose.h:78
@ kMisalignGeometry
in MisalignGeometry
@ kConstructGeometry
in ConstructGeometry
@ kNotInApplication
not in VMC application
@ kConstructOpGeometry
in ConstructOpGeometry