DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4FieldTrackingSetup.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 #ifndef DD4HEP_DDG4_GEANT4FIELDTRACKINGSETUP_H
15 #define DD4HEP_DDG4_GEANT4FIELDTRACKINGSETUP_H 1
16 
17 // Framework include files
18 #include <DD4hep/Detector.h>
19 #include <DDG4/Geant4ActionPhase.h>
21 
23 namespace dd4hep {
24 
26  namespace sim {
27 
29 
41  protected:
43  std::string eq_typ;
46  std::string stepper_typ;
50  double delta_chord;
56  double eps_min;
58  double eps_max;
60  double largest_step;
61 
62  public:
66  virtual ~Geant4FieldTrackingSetup();
68  virtual int execute(Detector& description);
69  };
70 
72 
81  public Geant4PhaseAction,
83  {
84  protected:
85  public:
87  Geant4FieldTrackingSetupAction(Geant4Context* context, const std::string& nam);
91  void operator()();
92  };
93 
95 
106  {
107  protected:
108  public:
110  Geant4FieldTrackingConstruction(Geant4Context* context, const std::string& nam);
111 
114 
117 
118  };
119  } // End namespace sim
120 } // End namespace dd4hep
121 #endif // DD4HEP_DDG4_GEANT4FIELDTRACKINGSETUP_H
122 
123 //==========================================================================
124 // AIDA Detector description implementation
125 //--------------------------------------------------------------------------
126 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
127 // All rights reserved.
128 //
129 // For the licensing terms see $DD4hepINSTALL/LICENSE.
130 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
131 //
132 // Author : M.Frank
133 //
134 //==========================================================================
135 
136 // Framework include files
137 #include <DD4hep/Handle.h>
138 #include <DD4hep/Fields.h>
139 #include <DDG4/Factories.h>
140 #include <DDG4/Geant4Field.h>
141 #include <DDG4/Geant4Converter.h>
142 
143 #include <G4TransportationManager.hh>
144 #include <G4MagIntegratorStepper.hh>
145 #include <G4Mag_EqRhs.hh>
146 #include <G4ChordFinder.hh>
147 #include <G4PropagatorInField.hh>
148 #include <limits>
149 
150 using namespace dd4hep::sim;
151 
153 namespace {
154 
155  struct Geant4SetupPropertyMap {
156  const std::map<std::string,std::string>& vals;
157  Geant4SetupPropertyMap(const std::map<std::string,std::string>& v) : vals(v) {}
158  std::string value(const std::string& key) const;
159  double toDouble(const std::string& key) const;
160  bool operator[](const std::string& key) const { return vals.find(key) != vals.end(); }
161  };
162 
163  std::string Geant4SetupPropertyMap::value(const std::string& key) const {
164  dd4hep::Detector::PropertyValues::const_iterator iV = vals.find(key);
165  return iV == vals.end() ? "" : (*iV).second;
166  }
167 
168  double Geant4SetupPropertyMap::toDouble(const std::string& key) const {
169  return dd4hep::_toDouble(this->value(key));
170  }
171 
172 }
173 
176  eps_min = -1.0;
177  eps_max = -1.0;
178  min_chord_step = 1.0e-2 *CLHEP::mm;
179  delta_chord = -1.0;
180  delta_one_step = -1.0;
181  delta_intersection = -1.0;
182  largest_step = -1.0;
183 }
184 
187 }
188 
191  OverlayedField fld = description.field();
192  G4ChordFinder* chordFinder;
193  G4TransportationManager* transportMgr;
194  G4PropagatorInField* propagator;
195  G4FieldManager* fieldManager;
196  G4MagneticField* mag_field = new sim::Geant4Field(fld);
197  G4Mag_EqRhs* mag_equation = PluginService::Create<G4Mag_EqRhs*>(eq_typ,mag_field);
198  G4EquationOfMotion* mag_eq = mag_equation;
199  if ( nullptr == mag_eq ) {
200  mag_eq = PluginService::Create<G4EquationOfMotion*>(eq_typ,mag_field);
201  if ( nullptr == mag_eq ) {
202  except("FieldSetup", "Cannot create G4EquationOfMotion of type: %s.",eq_typ.c_str());
203  }
204  }
205  G4MagIntegratorStepper* fld_stepper = PluginService::Create<G4MagIntegratorStepper*>(stepper_typ,mag_eq);
206  if ( nullptr == fld_stepper ) {
207  fld_stepper = PluginService::Create<G4MagIntegratorStepper*>(stepper_typ,mag_equation);
208  if ( nullptr == fld_stepper ) {
209  except("FieldSetup", "Cannot create stepper of type: %s.",stepper_typ.c_str());
210  }
211  }
212  chordFinder = new G4ChordFinder(mag_field,min_chord_step,fld_stepper);
213  transportMgr = G4TransportationManager::GetTransportationManager();
214  propagator = transportMgr->GetPropagatorInField();
215  fieldManager = transportMgr->GetFieldManager();
216 
217  fieldManager->SetFieldChangesEnergy(fld.changesEnergy());
218  fieldManager->SetDetectorField(mag_field);
219  fieldManager->SetChordFinder(chordFinder);
220 
221  if ( delta_chord >= 0e0 )
222  chordFinder->SetDeltaChord(delta_chord);
223  if ( delta_one_step >= 0e0 )
224  fieldManager->SetAccuraciesWithDeltaOneStep(delta_one_step);
225  if ( delta_intersection >= 0e0 )
226  fieldManager->SetDeltaIntersection(delta_intersection);
227  if ( eps_min >= 0e0 )
228  propagator->SetMinimumEpsilonStep(eps_min);
229  if ( eps_max >= 0e0 )
230  propagator->SetMaximumEpsilonStep(eps_max);
231  if ( largest_step >= 0e0 ) {
232  propagator->SetLargestAcceptableStep(largest_step);
233  } else {
234  largest_step = propagator->GetLargestAcceptableStep();
235  }
236  return 1;
237 }
238 
239 static long setup_fields(dd4hep::Detector& description,
240  const dd4hep::detail::GeoHandler& /* cnv */,
241  const std::map<std::string,std::string>& vals)
242 {
243  struct XMLFieldTrackingSetup : public Geant4FieldTrackingSetup {
244  XMLFieldTrackingSetup(const std::map<std::string,std::string>& values) : Geant4FieldTrackingSetup() {
245  Geant4SetupPropertyMap pm(values);
246  dd4hep::Detector::PropertyValues::const_iterator iV = values.find("min_chord_step");
247  eq_typ = pm.value("equation");
248  stepper_typ = pm.value("stepper");
249  min_chord_step = dd4hep::_toDouble((iV==values.end()) ? std::string("1e-2 * mm") : (*iV).second);
250  if ( pm["eps_min"] ) eps_min = pm.toDouble("eps_min");
251  if ( pm["eps_max"] ) eps_max = pm.toDouble("eps_max");
252  if ( pm["delta_chord"] ) delta_chord = pm.toDouble("delta_chord");
253  if ( pm["delta_one_step"] ) delta_one_step = pm.toDouble("delta_one_step");
254  if ( pm["delta_intersection"] ) delta_intersection = pm.toDouble("delta_intersection");
255  if ( pm["largest_step"] ) largest_step = pm.toDouble("largest_step");
256  }
257  virtual ~XMLFieldTrackingSetup() {}
258  } setup(vals);
259  return setup.execute(description);
260 }
261 
265 {
266  declareProperty("equation", eq_typ);
267  declareProperty("stepper", stepper_typ);
268  declareProperty("min_chord_step", min_chord_step = 1.0e-2);
269  declareProperty("delta_chord", delta_chord = -1.0);
270  declareProperty("delta_one_step", delta_one_step = -1.0);
271  declareProperty("delta_intersection", delta_intersection = -1.0);
272  declareProperty("eps_min", eps_min = -1.0);
273  declareProperty("eps_max", eps_max = -1.0);
274  declareProperty("largest_step", largest_step = -1.0);
275 }
276 
279  execute(context()->detectorDescription());
280  printout( INFO, "FieldSetup", "Geant4 magnetic field tracking configured.");
281  printout( INFO, "FieldSetup", "G4MagIntegratorStepper:%s G4Mag_EqRhs:%s",
282  stepper_typ.c_str(), eq_typ.c_str());
283  printout( INFO, "FieldSetup", "Epsilon:[min:%f mm max:%f mm]", eps_min, eps_max);
284  printout( INFO, "FieldSetup", "Delta:[chord:%f 1-step:%f intersect:%f] LargestStep %f mm",
286 }
287 
288 
292 {
293  declareProperty("equation", eq_typ);
294  declareProperty("stepper", stepper_typ);
295  declareProperty("min_chord_step", min_chord_step = 1.0e-2);
296  declareProperty("delta_chord", delta_chord = -1.0);
297  declareProperty("delta_one_step", delta_one_step = -1.0);
298  declareProperty("delta_intersection", delta_intersection = -1.0);
299  declareProperty("eps_min", eps_min = -1.0);
300  declareProperty("eps_max", eps_max = -1.0);
301  declareProperty("largest_step", largest_step = -1.0);
302 }
303 
306  execute(context()->detectorDescription());
307  printout( INFO, "FieldSetup", "Geant4 magnetic field tracking configured.");
308  printout( INFO, "FieldSetup", "G4MagIntegratorStepper:%s G4Mag_EqRhs:%s",
309  stepper_typ.c_str(), eq_typ.c_str());
310  printout( INFO, "FieldSetup", "Epsilon:[min:%f mm max:%f mm]", eps_min, eps_max);
311  printout( INFO, "FieldSetup", "Delta:[chord:%f 1-step:%f intersect:%f] LargestStep %f mm",
313 }
314 
315 DECLARE_GEANT4_SETUP(Geant4FieldSetup,setup_fields)
dd4hep::sim::Geant4FieldTrackingSetupAction::Geant4FieldTrackingSetupAction
Geant4FieldTrackingSetupAction(Geant4Context *context, const std::string &nam)
Standard constructor.
Definition: Geant4FieldTrackingSetup.cpp:263
Geant4Field.h
Geant4DetectorConstruction.h
v
View * v
Definition: MultiView.cpp:28
DECLARE_GEANT4_SETUP
#define DECLARE_GEANT4_SETUP(name, func)
Plugin defintion for setup actions.
Definition: Factories.h:231
Detector.h
dd4hep::sim::Geant4FieldTrackingSetup::delta_chord
double delta_chord
G4ChordFinder parameter: delta.
Definition: Geant4FieldTrackingSetup.cpp:50
dd4hep::sim::Geant4Field
Mediator class to allow Geant4 accessing magnetic fields defined in dd4hep.
Definition: Geant4Field.h:39
Handle.h
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::Geant4FieldTrackingSetup::delta_intersection
double delta_intersection
G4FieldManager parameter: delta_intersection.
Definition: Geant4FieldTrackingSetup.cpp:54
dd4hep::OverlayedField
Class describing a field overlay with several sources.
Definition: Fields.h:138
dd4hep::sim::Geant4FieldTrackingConstruction::constructField
void constructField(Geant4DetectorConstructionContext *ctxt)
Detector construction callback.
Definition: Geant4FieldTrackingSetup.cpp:305
dd4hep::sim::Geant4FieldTrackingSetup::eps_min
double eps_min
G4PropagatorInField parameter: eps_min.
Definition: Geant4FieldTrackingSetup.cpp:56
dd4hep::sim::Geant4DetectorConstructionContext
Geant4 detector construction context definition.
Definition: Geant4DetectorConstruction.h:61
dd4hep::sim::Geant4FieldTrackingSetupAction::~Geant4FieldTrackingSetupAction
virtual ~Geant4FieldTrackingSetupAction()
Default destructor.
Definition: Geant4FieldTrackingSetup.cpp:89
dd4hep::sim::Geant4FieldTrackingSetup::execute
virtual int execute(Detector &description)
Perform the setup of the magnetic field tracking in Geant4.
Definition: Geant4FieldTrackingSetup.cpp:190
dd4hep::sim::Geant4FieldTrackingSetup::eq_typ
std::string eq_typ
Name of the G4Mag_EqRhs class.
Definition: Geant4FieldTrackingSetup.cpp:44
dd4hep::detail::GeoHandler
The base class for all dd4hep geometry crawlers.
Definition: GeoHandler.h:87
dd4hep::sim::Geant4FieldTrackingSetup::Geant4FieldTrackingSetup
Geant4FieldTrackingSetup()
Default constructor.
Definition: Geant4FieldTrackingSetup.cpp:175
dd4hep::Detector::field
virtual OverlayedField field() const =0
Return handle to the combined electromagentic field description.
dd4hep::sim::Geant4FieldTrackingSetup::min_chord_step
double min_chord_step
G4ChordFinder parameter: min_chord_step.
Definition: Geant4FieldTrackingSetup.cpp:48
dd4hep::sim::Geant4FieldTrackingSetup::largest_step
double largest_step
G4PropagatorInField parameter: LargestAcceptableStep.
Definition: Geant4FieldTrackingSetup.cpp:60
dd4hep::sim::Geant4Action::declareProperty
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:366
dd4hep::sim::Geant4FieldTrackingSetup::~Geant4FieldTrackingSetup
virtual ~Geant4FieldTrackingSetup()
Default destructor.
Definition: Geant4FieldTrackingSetup.cpp:186
dd4hep::sim::Geant4FieldTrackingSetupAction
Phase action to perform the setup of the Geant4 tracking in magnetic fields.
Definition: Geant4FieldTrackingSetup.cpp:83
dd4hep::sim::Geant4PhaseAction
Generic action for Geant4 phases.
Definition: Geant4ActionPhase.h:36
Factories.h
dd4hep::_toDouble
double _toDouble(const std::string &value)
String conversions: string to double value.
Definition: Handle.cpp:116
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4FieldTrackingSetup::stepper_typ
std::string stepper_typ
Name of the G4MagIntegratorStepper class.
Definition: Geant4FieldTrackingSetup.cpp:46
dd4hep::sim::Geant4FieldTrackingSetup
Generic Setup component to perform the magnetic field tracking in Geant4.
Definition: Geant4FieldTrackingSetup.cpp:40
dd4hep::sim::Geant4FieldTrackingConstruction
Detector construction action to perform the setup of the Geant4 tracking in magnetic fields.
Definition: Geant4FieldTrackingSetup.cpp:106
dd4hep::sim::Geant4FieldTrackingConstruction::Geant4FieldTrackingConstruction
Geant4FieldTrackingConstruction(Geant4Context *context, const std::string &nam)
Standard constructor.
Definition: Geant4FieldTrackingSetup.cpp:290
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4FieldTrackingSetup::eps_max
double eps_max
G4PropagatorInField parameter: eps_min.
Definition: Geant4FieldTrackingSetup.cpp:58
dd4hep::OverlayedField::changesEnergy
bool changesEnergy() const
Does the field change the energy of charged particles?
Definition: Fields.cpp:104
dd4hep::sim::Geant4FieldTrackingSetup::delta_one_step
double delta_one_step
G4FieldManager parameter: delta_one_step.
Definition: Geant4FieldTrackingSetup.cpp:52
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::sim::Geant4DetectorConstruction
Basic implementation of the Geant4 detector construction action.
Definition: Geant4DetectorConstruction.h:102
Geant4ActionPhase.h
dd4hep::sim::Geant4FieldTrackingConstruction::~Geant4FieldTrackingConstruction
virtual ~Geant4FieldTrackingConstruction()
Default destructor.
Definition: Geant4FieldTrackingSetup.cpp:113
Fields.h
Geant4Converter.h
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270
G4MagneticField
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:11
dd4hep::sim::Geant4FieldTrackingSetupAction::operator()
void operator()()
Phase action callback.
Definition: Geant4FieldTrackingSetup.cpp:278