9#include <geometry/dbobjects/MagneticFieldComponent3D.h>
27 template<
int ORDER = 4>
constexpr double fast_atan2_minimax(
double y,
double x)
29 static_assert(ORDER >= 1 && ORDER <= 6,
"fast_atan2_minimax: Only orders 1-6 are supported");
30 constexpr double pi4 = M_PI / 4, pi2 = M_PI / 2;
31 const double ax = std::abs(x), ay = std::abs(y);
32 const double z = (ax - ay) / (ay + ax);
33 const double v = std::abs(z), v2 = v * v;
37 }
else if (ORDER == 3) {
40 }
else if (ORDER == 4) {
43 constexpr double c4[] = {
49 p0 = c4[0] + v2 * c4[2];
50 p1 = c4[1] + v2 * c4[3];
51 }
else if (ORDER == 5) {
54 constexpr double c5[] = {
61 p0 = c5[0] + v2 * (c5[2] + v2 * c5[4]);
62 p1 = c5[1] + v2 * (c5[3]);
63 }
else if (ORDER == 6) {
66 constexpr double c6[] = {
74 p0 = c6[0] + v2 * (c6[2] + v2 * (c6[4]));
75 p1 = c6[1] + v2 * (c6[3] + v2 * (c6[5]));
77 double phi = (z + 1) * pi4 - (z * (v - 1)) * (p0 + v * p1);
78 phi = pi2 - copysign(phi, x);
79 return copysign(phi, y);
106 auto getIndexWeight = [
this](
double value,
int coordinate) -> std::tuple<unsigned int, double> {
108 auto index =
static_cast<unsigned int>(weight);
109 index = std::min(index,
static_cast<unsigned int>(
m_mapSize[coordinate] - 2));
111 return std::make_tuple(index, weight);
114 const double z = pos.Z();
115 const double r2 = pos.Perp2();
119 const auto iz = getIndexWeight(
m_maxZ - z, 2);
122 if (r2 == 0)
return interpolate(0, 0, get<0>(iz), 0, 0, get<1>(iz));
124 const double r = std::sqrt(r2);
125 const auto ir = getIndexWeight(r -
m_minR, 0);
128 const double ay = std::abs(pos.Y());
129 const auto iphi = getIndexWeight(fast_atan2_minimax<4>(ay, pos.X()), 1);
132 ROOT::Math::XYZVector b =
interpolate(get<0>(ir), get<0>(iphi), get<0>(iz), get<1>(ir), get<1>(iphi), get<1>(iz));
134 const double norm = 1 / r;
135 const double s = ay * norm;
136 const double c = pos.X() * norm;
138 const double sgny = (pos.Y() >= 0) - (pos.Y() < 0);
140 return ROOT::Math::XYZVector(-(b.X() * c - b.Y() * s), -sgny * (b.X() * s + b.Y() * c), b.Z());
144 double wr1,
double wphi1,
double wz1)
const
147 const unsigned int strideR =
m_mapSize[1];
149 const double wz0 = 1 - wz1;
150 const double wr0 = 1 - wr1;
151 const double wphi0 = 1 - wphi1;
152 const unsigned int j000 = iz * strideZ + ir * strideR + iphi;
153 const unsigned int j001 = j000 + 1;
154 const unsigned int j010 = j000 + strideR;
155 const unsigned int j011 = j001 + strideR;
156 const unsigned int j100 = j000 + strideZ;
157 const unsigned int j101 = j001 + strideZ;
158 const unsigned int j110 = j010 + strideZ;
159 const unsigned int j111 = j011 + strideZ;
160 const double w00 = wphi0 * wr0;
161 const double w10 = wphi0 * wr1;
162 const double w01 = wphi1 * wr0;
163 const double w11 = wphi1 * wr1;
164 const std::vector<ROOT::Math::XYZVector>& B =
m_bmap;
166 (B[j000] * w00 + B[j001] * w01 + B[j010] * w10 + B[j011] * w11) * wz0 +
167 (B[j100] * w00 + B[j101] * w01 + B[j110] * w10 + B[j111] * w11) * wz1;
int m_mapSize[3]
number of bins in r, phi and z
float m_maxZ
maximal Z for which this field is present
std::vector< ROOT::Math::XYZVector > m_bmap
magnetic field strength
float m_invgridPitch[3]
inverted grid pitch in r, phi and z
float m_minR
minimal R=sqrt(x^2+y^2) for which this field is present
ROOT::Math::XYZVector interpolate(unsigned int ir, unsigned int iphi, unsigned int iz, double wr, double wphi, double wz) const
Linear interpolate the magnetic field inside a bin.
ROOT::Math::XYZVector getField(const ROOT::Math::XYZVector &pos) const override
return the field assuming we are inside the active region as returned by inside()
Abstract base class for different kinds of events.