MagneticField_service.cc
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////
2 /// \file MagneticField_service.cc
3 ///
4 /// \version $Id: MagneticField.cxx,v 1.2 2012-03-07 19:01:44 brebel Exp $
5 /// \author dmckee@phys.ksu.edu
6 //////////////////////////////////////////////////////////////////////////
7 /// \class MagneticField MagneticField.h
8 /// The initial implementation will be trivial: simply supporting a
9 /// constant field in a named detector volume. In principle we should
10 /// read a full field map from an external file of some kind.
11 ///
12 /// We support three FHICL values for now:
13 ///
14 /// - "UseField" a integer. When 0 we don't even instantiate a
15 /// Magnetic field object. Describes the description to use.
16 /// - "Constant Field" a vector< double > which should have three
17 /// elements and is interpreted in Tesla
18 /// - "MagnetizedVolume" names the G4logical volume to which the
19 /// field should be attached
20 //////////////////////////////////////////////////////////////////////////
21 
22 // Framework includes
24 
25 // nutools includes
26 #include "nutools/MagneticField/MagneticField.h"
27 
28 #include "TGeoManager.h"
29 
30 #include <vector>
31 #include <string>
32 
33 namespace mag {
34 
36  {
37  this->reconfigure(pset);
38  }
39 
40  //------------------------------------------------------------
42  {
43  auto fieldDescriptions = pset.get<std::vector<fhicl::ParameterSet> >("FieldDescriptions");
44 
45  MagneticFieldDescription fieldDescription;
46  for(auto itr : fieldDescriptions){
47  fieldDescription.fMode = (mag::MagFieldMode_t)(itr.get<int>("UseField"));
48 
49  // if the mode is turned to off, no point looking for a volume or
50  // trying to put a description into the fFieldDescriptions data member.
51  // if all input field descriptions are set to fMode = kNoBFieldMode, then the
52  // methods to return the fields will not go into the loop over fFieldDescriptions
53  // and will just return a 0 field.
54  if(fieldDescription.fMode == mag::kNoBFieldMode) continue;
55 
56  fieldDescription.fVolume = itr.get<std::string>("MagnetizedVolume");
57  fieldDescription.fGeoVol = gGeoManager->FindVolumeFast(fieldDescription.fVolume.c_str());
58 
59  // check that we have a good volume
60  if( fieldDescription.fGeoVol == nullptr )
61  throw cet::exception("MagneticField")
62  << "cannot locat volume "
63  << fieldDescription.fVolume
64  << " in gGeoManager, bail";
65 
66  // These need to be read as types that FHICL know about, but they
67  // are used by Geant, so I store them in Geant4 types.
68  std::vector<double> field = itr.get<std::vector<double> >("ConstantField");
69 
70  // Force the dimension of the field definition
71  field.resize(3);
72  for(size_t i = 0; i < 3; ++i) fieldDescription.fField[i] = field[i];
73 
74  fFieldDescriptions.push_back(fieldDescription);
75  }
76 
77  return;
78  }
79 
80  //------------------------------------------------------------
81  G4ThreeVector const MagneticField::FieldAtPoint(G4ThreeVector const& p) const
82  {
83  // check that the input point is in the magnetized volume
84  // Use the gGeoManager to determine what node the point
85  // is in
86  double point[3] = { p.x(), p.y(), p.z() };
87 
88  // loop over the field descriptions to see if the point is in any of them
89  for(auto fd : fFieldDescriptions){
90  // we found a node, see if its name is the same as
91  // the volume with the field
92  if(fd.fGeoVol->Contains(point)) return fd.fField;
93  }
94 
95  // if we get here, we can't find a field
96  return G4ThreeVector(0);
97  }
98 
99  //------------------------------------------------------------
100  G4ThreeVector const MagneticField::UniformFieldInVolume(std::string const& volName) const
101  {
102  // if the input volume name is the same as the magnetized volume
103  // return the uniform field
104 
105  for(auto fd : fFieldDescriptions){
106  if (fd.fVolume.compare(volName) == 0) return fd.fField;
107  }
108 
109  // if we get here, we can't find a field
110  return G4ThreeVector(0);
111  }
112 
113 }// namespace
114 
115 namespace mag {
116 
118 
119 } // namespace mag
G4ThreeVector const FieldAtPoint(G4ThreeVector const &p=G4ThreeVector(0)) const
std::string string
Definition: nybbler.cc:12
#define DEFINE_ART_SERVICE(svc)
Definition: ServiceMacros.h:88
std::vector< MagneticFieldDescription > fFieldDescriptions
Descriptions of the fields.
Definition: MagneticField.h:70
T get(std::string const &key) const
Definition: ParameterSet.h:231
MagneticField(fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)
enum mag::MagneticFieldMode MagFieldMode_t
p
Definition: test.py:223
G4ThreeVector const UniformFieldInVolume(std::string const &volName) const
void reconfigure(fhicl::ParameterSet const &pset)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33