QwAnalysis
QwMagneticField.h
Go to the documentation of this file.
1 //
2 // date: Sun Nov 29 22:28:23 CST 2009
3 //
4 // jpan: QTOR field map reading class.
5 // Modified from the Qweak geant4 simulation code
6 //
7 
8 #ifndef _QwMagneticField_h_
9 #define _QwMagneticField_h_
10 
11 // System headers
12 #include <iostream>
13 #include <cmath>
14 #include <string>
15 #include <vector>
16 #include <fstream>
17 
18 // ROOT headers
19 #include <TVector3.h>
20 #include <TRotation.h>
21 
22 // Qweak headers
23 #include "QwUnits.h"
24 #include "QwOptions.h"
25 #include "QwInterpolator.h"
26 #include "QwParameterFile.h"
27 
28 // Forward declarations
29 template <class value_t, unsigned int value_n> class QwInterpolator;
30 
31 /**
32  * \class QwMagneticField
33  * \ingroup QwTracking
34  * \brief Magnetic field map object
35  *
36  * This class implements a magnetic field object using the QwInterpolator class.
37  *
38  * Five magnetic field components are stored (larger memory usage, computationally
39  * more intensive on creation, but faster access by avoiding trigonometric
40  * operations).
41  *
42  * Access to the field components is possible using two functions:
43  * \code
44  * field->GetCartesianFieldValue(double point_xyz[3], double field_xyz[3]);
45  * field->GetCylindricalFieldValue(double point_xyz[3], double field_rfz[3]);
46  * \endcode
47  *
48  * Currently no function exists that takes cylindrical coordinates.
49  */
51 
52  private:
53 
54  // Number of field component in the map (x,y,z,r,phi)
55  static const unsigned int value_n = 3;
56 
57  // Field map storage data type
58  typedef float field_t;
59 
60  public:
61 
62  /// \brief Default constructor
63  QwMagneticField(QwOptions& options, const bool suppress_read_field_map = false);
64  /// \brief Virtual destructor
65  virtual ~QwMagneticField();
66 
67 
68  /// \brief Define command line and config file options
69  static void DefineOptions(QwOptions& options);
70  /// \brief Process command line and config file options
71  void ProcessOptions(QwOptions& options);
72 
73  /// \brief Load beam property
74  void LoadBeamProperty(const TString& map);
75 
76  /// Set the filename
77  void SetFilename(const std::string& filename)
78  { fFilename = filename; };
79  /// Get the filename
80  const std::string GetFilename() const
81  { return fFilename; };
82 
83  /// Set the actual current
84  void SetActualCurrent(const double current) {
85  fActualCurrent = current;
86  if (fReferenceCurrent > 0.0)
88  }
89  /// Get the actual current
90  double GetActualCurrent() const
91  { return fActualCurrent; }
92 
93  /// Set the reference current
94  void SetReferenceCurrent(const double current) {
95  fReferenceCurrent = current;
96  if (fReferenceCurrent > 0.0)
98  }
99  /// Get the reference current
100  double GetReferenceCurrent() const
101  { return fReferenceCurrent; }
102 
103  /// Set the field rotation around z (with QwUnits)
104  void SetRotation(const double rotation) {
105  fRotation = rotation;
106  fRotationCos = cos(rotation);
107  fRotationSin = sin(rotation);
108  // Unit conversions
109  fRotationDeg = rotation / Qw::deg;
110  fRotationRad = rotation / Qw::rad;
111  };
112  /// Get the field rotation around z (with QwUnits)
113  double GetRotation() const
114  { return fRotationRad * Qw::rad; };
115 
116  /// Set the field translation along z
117  void SetTranslation(const double translation)
118  { fTranslation = translation; };
119  /// Get the field translation along z
120  double GetTranslation() const
121  { return fTranslation; };
122 
123  /// Get the cartesian components of the field value
124  void GetCartesianFieldValue(const double point_xyz[3], double field_xyz[3]) const {
125  double field[value_n];
126  GetFieldValue(point_xyz, field);
127  if (value_n >= 3) {
128  field_xyz[0] = field[0]; // x
129  field_xyz[1] = field[1]; // y
130  field_xyz[2] = field[2]; // z
131  }
132  };
133  /// Get the cylindrical components of the field value
134  void GetCylindricalFieldValue(const double point_xyz[3], double field_rfz[3]) const {
135  double field[value_n];
136  GetFieldValue(point_xyz, field);
137  if (value_n == 3) {
138  double r = sqrt(field[0] * field[0] + field[1] * field[1]) ;
139  double phi = atan2(field[1],field[0]);
140  if (phi < 0) phi += 2.0 * Qw::pi;
141  field_rfz[0] = r; // r
142  field_rfz[1] = phi; // phi
143  field_rfz[2] = field[2]; // z
144  } else if (value_n == 5) {
145  field_rfz[0] = field[3]; // r
146  field_rfz[1] = field[4]; // phi
147  field_rfz[2] = field[2]; // z
148  } else {
149  QwWarning << "Number of field components should be 3 or 5." << QwLog::endl;
150  }
151  };
152 
153  /// Get the cartesian components of the field value
154  void GetCartesianFieldValue(const TVector3& point, TVector3& field) const {
155  double point_xyz[3], field_xyz[3];
156  point.GetXYZ(point_xyz);
157  GetCartesianFieldValue(point_xyz, field_xyz);
158  field = TVector3(field_xyz);
159  }
160 
161  /// \brief Read a field map
162  bool ReadFieldMap();
163  /// \brief Read a field map input file
164  bool ReadFieldMapFile(const std::string& filename);
165  /// \brief Read a field map input gzip file
166  bool ReadFieldMapZip(const std::string& filename);
167  /// \brief Read the field map input stream
168  bool ReadFieldMapStream(std::istream& input);
169 
170  /// \brief Test the field map
171  bool TestFieldMap();
172 
173  /// \name Expose some functionality of underlying field map
174  // @{
175  /// Write a binary field map
176  bool WriteBinaryFile(const std::string& fieldmap) const {
177  std::string filename = getenv_safe_string("QW_FIELDMAP") + "/" + fieldmap;
178  if (fField) return fField->WriteBinaryFile(filename);
179  else return false;
180  }
181  /// Write a text field map
182  bool WriteTextFile(const std::string& fieldmap) const {
183  std::string filename = getenv_safe_string("QW_FIELDMAP") + "/" + fieldmap;
184  if (fField) return fField->WriteTextFile(filename);
185  else return false;
186  }
187  /// Set interpolation method
189  if (fField) fField->SetInterpolationMethod(method);
190  }
191  /// Get interpolation method
193  if (fField) return fField->GetInterpolationMethod();
194  else return kInterpolationMethodUnknown;
195  }
196  // @}
197 
198  private:
199 
200  /// File name
201  std::string fFilename;
202 
203  /// Field map
205 
206  /// Field map grid min, max, step, wrap
207  std::vector<double> fMin, fMax, fStep;
208  std::vector<size_t> fWrap;
209 
210  /// Field scale factor
213  double fScaleFactor;
214 
215  /// \brief Get the field value
216  void GetFieldValue(const double point[3], double field[value_n]) const;
217 
218  /// Field rotation and translation with respect to the z axis
219  // Defined as rotation/translation of the map in the standard coordinate
220  // system: rotation of +22.5 deg means that x in the map x is at +22.5 deg,
221  // likewise a translation of +5 cm means that the zero in the map is at +5 cm
222  // in the standard coordinate system.
223  // Note: This rotation and translation are only applied when reading from
224  // an ASCII field map generated by ANSYS.
226  double fTranslation;
227 
228 }; // class QwMagneticField
229 
230 #endif // _QwMagneticField_h_
static const double pi
Angles: base unit is radian.
Definition: QwUnits.h:102
std::string fFilename
File name.
void GetCartesianFieldValue(const TVector3 &point, TVector3 &field) const
Get the cartesian components of the field value.
Magnetic field map object.
void GetCylindricalFieldValue(const double point_xyz[3], double field_rfz[3]) const
Get the cylindrical components of the field value.
EQwInterpolationMethod
Allowed interpolation methods.
An options class.
Definition: QwOptions.h:133
void SetFilename(const std::string &filename)
Set the filename.
double GetRotation() const
Get the field rotation around z (with QwUnits)
static const double deg
Definition: QwUnits.h:106
QwMagneticField(QwOptions &options, const bool suppress_read_field_map=false)
Default constructor.
std::vector< double > fStep
double GetTranslation() const
Get the field translation along z.
double fRotation
Field rotation and translation with respect to the z axis.
static const unsigned int value_n
bool WriteTextFile(const std::string &fieldmap) const
Write a text field map.
bool ReadFieldMapStream(std::istream &input)
Read the field map input stream.
void SetActualCurrent(const double current)
Set the actual current.
void ProcessOptions(QwOptions &options)
Process command line and config file options.
static const double rad
Definition: QwUnits.h:105
QwInterpolator< field_t, value_n > * fField
Field map.
void SetInterpolationMethod(const EQwInterpolationMethod method)
Set the interpolation method.
void SetInterpolationMethod(const EQwInterpolationMethod method)
Set interpolation method.
void GetCartesianFieldValue(const double point_xyz[3], double field_xyz[3]) const
Get the cartesian components of the field value.
const std::string GetFilename() const
Get the filename.
bool WriteBinaryFile(const std::string &filename) const
Write the grid values to binary file.
std::vector< double > fMin
Field map grid min, max, step, wrap.
double fReferenceCurrent
Field scale factor.
void SetReferenceCurrent(const double current)
Set the reference current.
double GetReferenceCurrent() const
Get the reference current.
EQwInterpolationMethod GetInterpolationMethod() const
Get the interpolation method.
bool ReadFieldMapZip(const std::string &filename)
Read a field map input gzip file.
std::vector< size_t > fWrap
std::vector< double > fMax
void LoadBeamProperty(const TString &map)
Load beam property.
bool ReadFieldMapFile(const std::string &filename)
Read a field map input file.
void GetFieldValue(const double point[3], double field[value_n]) const
Get the field value.
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
const std::string getenv_safe_string(const char *name)
Definition: QwOptions.h:37
An options class which parses command line, config file and environment.
static void DefineOptions(QwOptions &options)
Define command line and config file options.
bool WriteTextFile(const std::string &filename) const
Write the grid to text file.
bool WriteBinaryFile(const std::string &fieldmap) const
Write a binary field map.
void SetTranslation(const double translation)
Set the field translation along z.
#define QwWarning
Predefined log drain for warnings.
Definition: QwLog.h:45
EQwInterpolationMethod GetInterpolationMethod() const
Get interpolation method.
void SetRotation(const double rotation)
Set the field rotation around z (with QwUnits)
bool TestFieldMap()
Test the field map.
virtual ~QwMagneticField()
Virtual destructor.
double GetActualCurrent() const
Get the actual current.
A multi-dimensional grid of values with interpolation methods.
bool ReadFieldMap()
Read a field map.