LDMX Software
BFieldXYZUtils.h
1#pragma once
2
3#include <fstream>
4#include <functional>
5#include <iostream>
6
7#include "Acts/Definitions/Algebra.hpp"
8#include "Acts/MagneticField/BFieldMapUtils.hpp"
9#include "Acts/MagneticField/InterpolatedBFieldMap.hpp"
10#include "Acts/MagneticField/MagneticFieldContext.hpp"
11#include "Acts/Utilities/AxisDefinitions.hpp"
12#include "Acts/Utilities/Grid.hpp"
13#include "Framework/Exception/Exception.h"
14
15static const double DIPOLE_OFFSET = 400.; // 400 mm
16
17using InterpolatedMagneticField3 = Acts::InterpolatedBFieldMap<
18 Acts::Grid<Acts::Vector3, Acts::Axis<Acts::AxisType::Equidistant>,
19 Acts::Axis<Acts::AxisType::Equidistant>,
20 Acts::Axis<Acts::AxisType::Equidistant>>>;
21
22using GenericTransformPos = std::function<Acts::Vector3(const Acts::Vector3&)>;
23using GenericTransformBField =
24 std::function<Acts::Vector3(const Acts::Vector3&, const Acts::Vector3&)>;
25
34Acts::Vector3 defaultTransformPos(const Acts::Vector3& pos_);
35
42Acts::Vector3 defaultTransformBField(const Acts::Vector3& field,
43 const Acts::Vector3& /*pos_*/);
44
45void testField(const std::shared_ptr<Acts::MagneticFieldProvider> bfield,
46 const Acts::Vector3& eval_pos,
47 const Acts::MagneticFieldContext& bctx);
48
63 Acts::Vector3 translation_{Acts::Vector3::Zero()};
65 Acts::Vector3 rotation_{Acts::Vector3::Zero()};
67 Acts::Vector3 pivot_{-DIPOLE_OFFSET, 0., 0.};
69 double scale_{1.};
70
72 Acts::RotationMatrix3 rotationMatrix() const {
73 return (Acts::AngleAxis3(rotation_(2), Acts::Vector3::UnitZ()) *
74 Acts::AngleAxis3(rotation_(1), Acts::Vector3::UnitY()) *
75 Acts::AngleAxis3(rotation_(0), Acts::Vector3::UnitX()))
76 .toRotationMatrix();
77 }
78
84 bool isNominal() const {
85 return translation_.isZero(0.) && rotation_.isZero(0.) && scale_ == 1.;
86 }
87};
88
94size_t localToGlobalBinXyz(std::array<size_t, 3> bins,
95 std::array<size_t, 3> sizes);
96
97inline InterpolatedMagneticField3 rotateFieldMapXYZ(
98 const std::function<size_t(std::array<size_t, 3> binsXYZ,
99 std::array<size_t, 3> nBinsXYZ)>&
100 localToGlobalBin,
101 std::vector<double> xPos, std::vector<double> yPos,
102 std::vector<double> zPos, std::vector<Acts::Vector3> bField,
103 double lengthUnit, double BFieldUnit, bool firstOctant,
104 GenericTransformPos transformPosition,
105 GenericTransformBField transformMagneticField) {
106 // [1] Create Grid
107 // Sort the values
108 std::sort(xPos.begin(), xPos.end());
109 std::sort(yPos.begin(), yPos.end());
110 std::sort(zPos.begin(), zPos.end());
111
112 // Get unique values
113 xPos.erase(std::unique(xPos.begin(), xPos.end()), xPos.end());
114 yPos.erase(std::unique(yPos.begin(), yPos.end()), yPos.end());
115 zPos.erase(std::unique(zPos.begin(), zPos.end()), zPos.end());
116 xPos.shrink_to_fit();
117 yPos.shrink_to_fit();
118 zPos.shrink_to_fit();
119
120 // get the number of bins
121 size_t n_bins_x = xPos.size();
122 size_t n_bins_y = yPos.size();
123 size_t n_bins_z = zPos.size();
124
125 // get the minimum and maximum
126 auto min_max_x = std::minmax_element(xPos.begin(), xPos.end());
127 auto min_max_y = std::minmax_element(yPos.begin(), yPos.end());
128 auto min_max_z = std::minmax_element(zPos.begin(), zPos.end());
129 // Create the axis for the grid
130 // get minima
131 double x_min = *min_max_x.first;
132 double y_min = *min_max_y.first;
133 double z_min = *min_max_z.first;
134 // get maxima
135 double x_max = *min_max_x.second;
136 double y_max = *min_max_y.second;
137 double z_max = *min_max_z.second;
138 // calculate maxima (add one last bin, because bin value always corresponds to
139 // left boundary)
140 double step_z = std::fabs(z_max - z_min) / (n_bins_z - 1);
141 double step_y = std::fabs(y_max - y_min) / (n_bins_y - 1);
142 double step_x = std::fabs(x_max - x_min) / (n_bins_x - 1);
143 x_max += step_x;
144 y_max += step_y;
145 z_max += step_z;
146
147 // If only the first octant is given
148 if (firstOctant) {
149 x_min = -*min_max_x.second;
150 y_min = -*min_max_y.second;
151 z_min = -*min_max_z.second;
152 n_bins_x = 2 * n_bins_x - 1;
153 n_bins_y = 2 * n_bins_y - 1;
154 n_bins_z = 2 * n_bins_z - 1;
155 }
156 Acts::Axis<Acts::AxisType::Equidistant> x_axis(x_min * lengthUnit,
157 x_max * lengthUnit, n_bins_x);
158 Acts::Axis<Acts::AxisType::Equidistant> y_axis(y_min * lengthUnit,
159 y_max * lengthUnit, n_bins_y);
160 Acts::Axis<Acts::AxisType::Equidistant> z_axis(z_min * lengthUnit,
161 z_max * lengthUnit, n_bins_z);
162 // Create the grid
163 using Grid_t =
164 Acts::Grid<Acts::Vector3, Acts::Axis<Acts::AxisType::Equidistant>,
165 Acts::Axis<Acts::AxisType::Equidistant>,
166 Acts::Axis<Acts::AxisType::Equidistant>>;
167 Grid_t grid(
168 std::make_tuple(std::move(x_axis), std::move(y_axis), std::move(z_axis)));
169
170 // [2] Set the bField values
171 for (size_t i = 1; i <= n_bins_x; ++i) {
172 for (size_t j = 1; j <= n_bins_y; ++j) {
173 for (size_t k = 1; k <= n_bins_z; ++k) {
174 Grid_t::index_t indices = {{i, j, k}};
175 std::array<size_t, 3> n_indices = {
176 {xPos.size(), yPos.size(), zPos.size()}};
177 if (firstOctant) {
178 // std::vectors begin with 0 and we do not want the user needing to
179 // take underflow or overflow bins in account this is why we need to
180 // subtract by one
181 size_t m = std::abs(int(i) - (int(xPos.size())));
182 size_t n = std::abs(int(j) - (int(yPos.size())));
183 size_t l = std::abs(int(k) - (int(zPos.size())));
184 Grid_t::index_t indices_first_octant = {{m, n, l}};
185
186 grid.atLocalBins(indices) =
187 bField.at(localToGlobalBin(indices_first_octant, n_indices)) *
188 BFieldUnit;
189
190 } else {
191 // std::vectors begin with 0 and we do not want the user needing to
192 // take underflow or overflow bins in account this is why we need to
193 // subtract by one
194 grid.atLocalBins(indices) =
195 bField.at(localToGlobalBin({{i - 1, j - 1, k - 1}}, n_indices)) *
196 BFieldUnit;
197 }
198 }
199 }
200 }
201 grid.setExteriorBins(Acts::Vector3::Zero());
202
203 // [3] Create the transformation for the position
204 // map (z,x,y) -> (x,y,z)
205
206 /*
207 auto transformPos = [](const Acts::Vector3& pos_, float offset=400.) {
208
209 Acts::Vector3 rot_pos;
210 rot_pos(0)=pos_(1);
211 rot_pos(1)=pos_(2);
212 rot_pos(2)=pos_(0) + offset;
213
214 return rot_pos;
215 };
216
217 */
218
219 // [4] Create the transformation for the bfield
220 // map (Bx,By,Bz) -> (Bx,By,Bz)
221
222 // auto transformBField = [](const Acts::Vector3& field,
223 // const Acts::Vector3& /*pos_*/) {
224 //
225 //
226 // Acts::Vector3 rot_field;
227 //
228 // rot_field(0) = field(2);
229 // rot_field(1) = field(0);
230 // rot_field(2) = field(1);
231
232 // return rot_field;
233 //};
234
235 // [5] Create the mapper and BField Service
236 // with the transformations passed from main producer
237 return Acts::InterpolatedBFieldMap<Grid_t>(
238 {transformPosition, transformMagneticField, std::move(grid)});
239}
240
241// This is a copy of
242// https://github.com/acts-project/acts/blob/main/Examples/Detectors/MagneticField/src/FieldMapTextIo.cpp
243// with additional rotateAxes flag to rotate the axes and field to be in the
244// tracking (ACTS) Frame
245
246inline InterpolatedMagneticField3 makeMagneticFieldMapXyzFromText(
247 std::function<size_t(std::array<size_t, 3> binsXYZ,
248 std::array<size_t, 3> nBinsXYZ)>
249 localToGlobalBin,
250 GenericTransformPos transformPosition,
251 GenericTransformBField transformMagneticField,
252 const std::string& fieldMapFile, double lengthUnit, double BFieldUnit,
253 bool firstOctant, bool rotateAxes) {
255 // Grid position points in x, y and z
256 std::vector<double> x_pos;
257 std::vector<double> y_pos;
258 std::vector<double> z_pos;
259 // components of magnetic field on grid points
260 std::vector<Acts::Vector3> b_field;
261
262 constexpr size_t k_default_size = 1 << 15;
263 // reserve estimated size
264 x_pos.reserve(k_default_size);
265 y_pos.reserve(k_default_size);
266 z_pos.reserve(k_default_size);
267 b_field.reserve(k_default_size);
268 // [1] Read in file and fill values
269 std::ifstream map_file(fieldMapFile.c_str(), std::ios::in);
270 if (!map_file.is_open()) {
271 EXCEPTION_RAISE("BadConf", "BFieldXYZUtils: cannot open field map file '" +
272 fieldMapFile + "'");
273 }
274 std::string line;
275 double pos_x = 0., pos_y = 0., pos_z = 0.;
276 double bx = 0., by = 0., bz = 0.;
277
278 bool header_found = false;
279
280 while (std::getline(map_file, line)) {
281 if (line.empty() || line[0] == '%' || line[0] == '#' || line[0] == ' ' ||
282 line.find_first_not_of(' ') == std::string::npos || !header_found) {
283 if (line.find("Header") != std::string::npos) header_found = true;
284 continue;
285 }
286 std::istringstream tmp(line);
287 tmp >> pos_x >> pos_y >> pos_z >> bx >> by >> bz;
288
289 x_pos.push_back(pos_x);
290 y_pos.push_back(pos_y);
291 z_pos.push_back(pos_z);
292 b_field.push_back(Acts::Vector3(bx, by, bz));
293 }
294 map_file.close();
295
296 if (!header_found) {
297 EXCEPTION_RAISE("BadConf",
298 "BFieldXYZUtils: no 'Header' line found in field map "
299 "file '" +
300 fieldMapFile + "'");
301 }
302 if (b_field.empty()) {
303 EXCEPTION_RAISE("BadConf", "BFieldXYZUtils: no field data read from '" +
304 fieldMapFile + "'");
305 }
306
307 x_pos.shrink_to_fit();
308 y_pos.shrink_to_fit();
309 z_pos.shrink_to_fit();
310 b_field.shrink_to_fit();
311
312 if (rotateAxes) {
313 return rotateFieldMapXYZ(localToGlobalBin, x_pos, y_pos, z_pos, b_field,
314 lengthUnit, BFieldUnit, firstOctant,
315 transformPosition, transformMagneticField);
316 } else
317 return Acts::fieldMapXYZ(localToGlobalBin, x_pos, y_pos, z_pos, b_field,
318 lengthUnit, BFieldUnit, firstOctant);
319}
320
321inline InterpolatedMagneticField3 loadDefaultBField(
322 const std::string& fieldMapFile, GenericTransformPos transformPosition,
323 GenericTransformBField transformMagneticField) {
324 // std::function<Acts::Vector3(const Acts::Vector3&, float)>
325 // transformPosition, std::function<Acts::Vector3(const Acts::Vector3&,const
326 // Acts::Vector3&)> transformMagneticField
327
328 return makeMagneticFieldMapXyzFromText(
329 std::move(localToGlobalBinXyz), transformPosition, transformMagneticField,
330 fieldMapFile,
331 1. * Acts::UnitConstants::mm, // default scale for axes length
332 1000. * Acts::UnitConstants::T, // The map is in kT, so scale it to T
333 false, // not symmetrical
334 true // rotate the axes to tracking frame
335 );
336}
337
338// R =
339//
340// 0 0 1
341// 1 0 0
342// 0 1 0
A deliberate mis-placement of the reconstruction magnetic field, used to quantify how well we need to...
double scale_
overall scaling of the field strength
Acts::Vector3 pivot_
centre of rotation [mm], default the field-map origin
Acts::Vector3 rotation_
rotation about x, y, z through pivot [rad]
Acts::RotationMatrix3 rotationMatrix() const
Rz(gamma) * Ry(beta) * Rx(alpha)
Acts::Vector3 translation_
displacement of the magnet [mm]
bool isNominal() const
Exact comparison on purpose: the nominal case reuses the default transforms unchanged,...