ATLAS Offline Software
DetectorFieldManagerTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Main header
7 
8 // From this package
10 #include "SwitchingFieldManager.h"
11 
12 // Geant4 includes
13 #include "G4ChordFinder.hh"
14 #include "G4FieldManager.hh"
15 #include "G4LogicalVolumeStore.hh"
16 #include "G4PhysicalVolumeStore.hh"
17 #include "G4MagIntegratorStepper.hh"
18 #include "G4MagneticField.hh"
19 #include "G4Version.hh"
20 #include "G4VIntegrationDriver.hh"
21 
22 //-----------------------------------------------------------------------------
23 // Tool constructor
24 //-----------------------------------------------------------------------------
26  const std::string& name,
27  const IInterface* parent)
29 {
30 }
31 
32 //-----------------------------------------------------------------------------
33 // Initialize a field manager
34 //-----------------------------------------------------------------------------
36 {
37  ATH_MSG_DEBUG("initializeField");
38 
39  if (m_fieldOn) {
40 
41  // If field manager already exists for current thread, error.
42  // There is no foreseen use-case for this situation.
43  if(m_fieldMgrHolder.get()) {
44  ATH_MSG_ERROR("DetectorFieldManagerTool::initializeField() - " <<
45  "Field manager already exists!");
46  return StatusCode::FAILURE;
47  }
48 
49  // Retrieve the G4MagneticField
50  G4MagneticField* field = m_fieldSvc->getField();
51 
52  // Create a new field manager
53  G4FieldManager * fieldMgr = nullptr;
54  if (m_muonOnlyField){
55  // fieldMgr = new TightMuonElseNoFieldManager();
56  fieldMgr = new SwitchingFieldManager(field);
57  } else {
58  fieldMgr = new G4FieldManager();
59  }
60 
61  // Save it in the TL holder
62  m_fieldMgrHolder.set(fieldMgr);
63 
64  // Configure the field manager
65  fieldMgr->SetDetectorField(field);
66  fieldMgr->CreateChordFinder(field);
67  ATH_CHECK( setFieldParameters(fieldMgr) );
68 
69  // Create and configure the ChordFinder
70  fieldMgr->CreateChordFinder(field);
71 
72 #if G4VERSION_NUMBER < 1040
73  ATH_MSG_DEBUG("Old style stepper setting");
74  G4MagIntegratorStepper* stepper = getStepper(m_integratorStepper, field);
75  G4MagInt_Driver* magDriver = fieldMgr->GetChordFinder()->GetIntegrationDriver();
76  magDriver->RenewStepperAndAdjust(stepper);
77 #else
78  ATH_MSG_DEBUG("New style stepper setting");
79  G4VIntegrationDriver* driver = createDriverAndStepper(m_integratorStepper, field);
80  G4ChordFinder* chordFinder = fieldMgr->GetChordFinder();
81  chordFinder->SetIntegrationDriver(driver);
82 #endif
83 
84  // Assign the field manager to volumes
85  if (!m_logVolumeList.empty()) {
86  auto logVolStore = G4LogicalVolumeStore::GetInstance();
87  for (const auto& volume: m_logVolumeList) {
88  G4LogicalVolume* logicalVolume = logVolStore->GetVolume(volume);
89  if (logicalVolume != nullptr) logicalVolume->SetFieldManager(fieldMgr, true);
90  else
91  ATH_MSG_WARNING("No volume called " << volume << " was found in the G4LogicalVolumeStore! Skipping this volume.");
92  }
93  }
94  else if (!m_physVolumeList.empty()) {
95  auto physVolStore = G4PhysicalVolumeStore::GetInstance();
96  for (const auto& volume: m_physVolumeList) {
97  G4VPhysicalVolume* physicalVolume = physVolStore->GetVolume(volume);
98  if (physicalVolume != nullptr) physicalVolume->GetLogicalVolume()->SetFieldManager(fieldMgr, true);
99  else
100  ATH_MSG_WARNING("No volume called " << volume << " was found in the G4PhysicalVolumeStore! Skipping this volume.");
101  }
102  }
103  else
104  ATH_MSG_WARNING("No volumes are provided. Field manager is NOT assigned.");
105 
106  }
107 
108  return StatusCode::SUCCESS;
109 }
DetectorFieldManagerTool::DetectorFieldManagerTool
DetectorFieldManagerTool(const std::string &type, const std::string &name, const IInterface *parent)
Standard constructor.
Definition: DetectorFieldManagerTool.cxx:25
G4FieldManagerToolBase::setFieldParameters
StatusCode setFieldParameters(G4FieldManager *fieldMgr) const
Common method to apply configuredfield parameters.
Definition: G4FieldManagerToolBase.cxx:239
DetectorFieldManagerTool::m_fieldMgrHolder
thread_utils::ThreadLocalOwner< G4FieldManager > m_fieldMgrHolder
My field manager.
Definition: DetectorFieldManagerTool.h:47
DetectorFieldManagerTool::m_logVolumeList
Gaudi::Property< std::vector< std::string > > m_logVolumeList
List of volumes to assign this field configuration to.
Definition: DetectorFieldManagerTool.h:40
SwitchingFieldManager
G4FieldManager that sets tight stepping for muons; disables magnetic field for other particles.
Definition: SwitchingFieldManager.h:20
G4FieldManagerToolBase::m_integratorStepper
Gaudi::Property< std::string > m_integratorStepper
The type of stepper to use.
Definition: G4FieldManagerToolBase.h:71
DetectorFieldManagerTool::m_muonOnlyField
Gaudi::Property< bool > m_muonOnlyField
Option for muons feeling the B-field only.
Definition: DetectorFieldManagerTool.h:44
G4FieldManagerToolBase
Definition: G4FieldManagerToolBase.h:34
G4FieldManagerToolBase::m_fieldOn
Gaudi::Property< bool > m_fieldOn
Definition: G4FieldManagerToolBase.h:68
G4FieldManagerToolBase::m_fieldSvc
ServiceHandle< IG4FieldSvc > m_fieldSvc
Handle to the G4 field service.
Definition: G4FieldManagerToolBase.h:65
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
FullCPAlgorithmsTest_eljob.driver
driver
Definition: FullCPAlgorithmsTest_eljob.py:175
DetectorFieldManagerTool::initializeField
StatusCode initializeField() override final
Initialize a field manager.
Definition: DetectorFieldManagerTool.cxx:35
thread_utils::ThreadLocalHolder::set
void set(T *obj)
Assign the object of the current thread.
Definition: ThreadLocalHolder.h:67
TightMuonElseNoFieldManager.h
DetectorFieldManagerTool.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
thread_utils::ThreadLocalHolder::get
T * get()
Get the object of the current thread.
Definition: ThreadLocalHolder.h:60
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
DetectorFieldManagerTool::m_physVolumeList
Gaudi::Property< std::vector< std::string > > m_physVolumeList
Definition: DetectorFieldManagerTool.h:41
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
SwitchingFieldManager.h
G4FieldManagerToolBase::getStepper
G4MagIntegratorStepper * getStepper(const std::string &stepperType, G4MagneticField *field) const
Common method to construct a stepper of requested type.
Definition: G4FieldManagerToolBase.cxx:190