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 "Acts/Utilities/Interpolation.hpp"
14#include "Acts/Utilities/Result.hpp"
15#include "Framework/Exception/Exception.h"
17static const double DIPOLE_OFFSET = 400.;
19using InterpolatedMagneticField3 = Acts::InterpolatedBFieldMap<
20 Acts::Grid<Acts::Vector3, Acts::Axis<Acts::AxisType::Equidistant>,
21 Acts::Axis<Acts::AxisType::Equidistant>,
22 Acts::Axis<Acts::AxisType::Equidistant>>>;
24using GenericTransformPos = std::function<Acts::Vector3(
const Acts::Vector3&)>;
25using GenericTransformBField =
26 std::function<Acts::Vector3(
const Acts::Vector3&,
const Acts::Vector3&)>;
36Acts::Vector3 defaultTransformPos(
const Acts::Vector3& pos_);
44Acts::Vector3 defaultTransformBField(
const Acts::Vector3& field,
45 const Acts::Vector3& );
47void testField(
const std::shared_ptr<Acts::MagneticFieldProvider> bfield,
48 const Acts::Vector3& eval_pos,
49 const Acts::MagneticFieldContext& bctx);
69 Acts::Vector3
pivot_{-DIPOLE_OFFSET, 0., 0.};
75 return (Acts::AngleAxis3(
rotation_(2), Acts::Vector3::UnitZ()) *
76 Acts::AngleAxis3(
rotation_(1), Acts::Vector3::UnitY()) *
77 Acts::AngleAxis3(
rotation_(0), Acts::Vector3::UnitX()))
96size_t localToGlobalBinXyz(std::array<size_t, 3> bins,
97 std::array<size_t, 3> sizes);
99inline InterpolatedMagneticField3 rotateFieldMapXYZ(
100 const std::function<
size_t(std::array<size_t, 3> binsXYZ,
101 std::array<size_t, 3> nBinsXYZ)>&
103 std::vector<double> xPos, std::vector<double> yPos,
104 std::vector<double> zPos, std::vector<Acts::Vector3> bField,
105 double lengthUnit,
double BFieldUnit,
bool firstOctant,
106 GenericTransformPos transformPosition,
107 GenericTransformBField transformMagneticField) {
110 std::sort(xPos.begin(), xPos.end());
111 std::sort(yPos.begin(), yPos.end());
112 std::sort(zPos.begin(), zPos.end());
115 xPos.erase(std::unique(xPos.begin(), xPos.end()), xPos.end());
116 yPos.erase(std::unique(yPos.begin(), yPos.end()), yPos.end());
117 zPos.erase(std::unique(zPos.begin(), zPos.end()), zPos.end());
118 xPos.shrink_to_fit();
119 yPos.shrink_to_fit();
120 zPos.shrink_to_fit();
123 size_t n_bins_x = xPos.size();
124 size_t n_bins_y = yPos.size();
125 size_t n_bins_z = zPos.size();
128 auto min_max_x = std::minmax_element(xPos.begin(), xPos.end());
129 auto min_max_y = std::minmax_element(yPos.begin(), yPos.end());
130 auto min_max_z = std::minmax_element(zPos.begin(), zPos.end());
133 double x_min = *min_max_x.first;
134 double y_min = *min_max_y.first;
135 double z_min = *min_max_z.first;
137 double x_max = *min_max_x.second;
138 double y_max = *min_max_y.second;
139 double z_max = *min_max_z.second;
142 double step_z = std::fabs(z_max - z_min) / (n_bins_z - 1);
143 double step_y = std::fabs(y_max - y_min) / (n_bins_y - 1);
144 double step_x = std::fabs(x_max - x_min) / (n_bins_x - 1);
151 x_min = -*min_max_x.second;
152 y_min = -*min_max_y.second;
153 z_min = -*min_max_z.second;
154 n_bins_x = 2 * n_bins_x - 1;
155 n_bins_y = 2 * n_bins_y - 1;
156 n_bins_z = 2 * n_bins_z - 1;
158 Acts::Axis<Acts::AxisType::Equidistant> x_axis(x_min * lengthUnit,
159 x_max * lengthUnit, n_bins_x);
160 Acts::Axis<Acts::AxisType::Equidistant> y_axis(y_min * lengthUnit,
161 y_max * lengthUnit, n_bins_y);
162 Acts::Axis<Acts::AxisType::Equidistant> z_axis(z_min * lengthUnit,
163 z_max * lengthUnit, n_bins_z);
166 Acts::Grid<Acts::Vector3, Acts::Axis<Acts::AxisType::Equidistant>,
167 Acts::Axis<Acts::AxisType::Equidistant>,
168 Acts::Axis<Acts::AxisType::Equidistant>>;
170 std::make_tuple(std::move(x_axis), std::move(y_axis), std::move(z_axis)));
173 for (
size_t i = 1; i <= n_bins_x; ++i) {
174 for (
size_t j = 1; j <= n_bins_y; ++j) {
175 for (
size_t k = 1; k <= n_bins_z; ++k) {
176 Grid_t::index_t indices = {{i, j, k}};
177 std::array<size_t, 3> n_indices = {
178 {xPos.size(), yPos.size(), zPos.size()}};
183 size_t m = std::abs(
int(i) - (
int(xPos.size())));
184 size_t n = std::abs(
int(j) - (
int(yPos.size())));
185 size_t l = std::abs(
int(k) - (
int(zPos.size())));
186 Grid_t::index_t indices_first_octant = {{m, n, l}};
188 grid.atLocalBins(indices) =
189 bField.at(localToGlobalBin(indices_first_octant, n_indices)) *
196 grid.atLocalBins(indices) =
197 bField.at(localToGlobalBin({{i - 1, j - 1, k - 1}}, n_indices)) *
203 grid.setExteriorBins(Acts::Vector3::Zero());
239 return Acts::InterpolatedBFieldMap<Grid_t>(
240 {transformPosition, transformMagneticField, std::move(grid)});
248inline InterpolatedMagneticField3 makeMagneticFieldMapXyzFromText(
249 std::function<
size_t(std::array<size_t, 3> binsXYZ,
250 std::array<size_t, 3> nBinsXYZ)>
252 GenericTransformPos transformPosition,
253 GenericTransformBField transformMagneticField,
254 const std::string& fieldMapFile,
double lengthUnit,
double BFieldUnit,
255 bool firstOctant,
bool rotateAxes) {
258 std::vector<double> x_pos;
259 std::vector<double> y_pos;
260 std::vector<double> z_pos;
262 std::vector<Acts::Vector3> b_field;
264 constexpr size_t k_default_size = 1 << 15;
266 x_pos.reserve(k_default_size);
267 y_pos.reserve(k_default_size);
268 z_pos.reserve(k_default_size);
269 b_field.reserve(k_default_size);
271 std::ifstream map_file(fieldMapFile.c_str(), std::ios::in);
272 if (!map_file.is_open()) {
273 EXCEPTION_RAISE(
"BadConf",
"BFieldXYZUtils: cannot open field map file '" +
277 double pos_x = 0., pos_y = 0., pos_z = 0.;
278 double bx = 0., by = 0., bz = 0.;
280 bool header_found =
false;
282 while (std::getline(map_file, line)) {
283 if (line.empty() || line[0] ==
'%' || line[0] ==
'#' || line[0] ==
' ' ||
284 line.find_first_not_of(
' ') == std::string::npos || !header_found) {
285 if (line.find(
"Header") != std::string::npos) header_found =
true;
288 std::istringstream tmp(line);
289 tmp >> pos_x >> pos_y >> pos_z >> bx >> by >> bz;
291 x_pos.push_back(pos_x);
292 y_pos.push_back(pos_y);
293 z_pos.push_back(pos_z);
294 b_field.push_back(Acts::Vector3(bx, by, bz));
299 EXCEPTION_RAISE(
"BadConf",
300 "BFieldXYZUtils: no 'Header' line found in field map "
304 if (b_field.empty()) {
305 EXCEPTION_RAISE(
"BadConf",
"BFieldXYZUtils: no field data read from '" +
309 x_pos.shrink_to_fit();
310 y_pos.shrink_to_fit();
311 z_pos.shrink_to_fit();
312 b_field.shrink_to_fit();
315 return rotateFieldMapXYZ(localToGlobalBin, x_pos, y_pos, z_pos, b_field,
316 lengthUnit, BFieldUnit, firstOctant,
317 transformPosition, transformMagneticField);
319 return Acts::fieldMapXYZ(localToGlobalBin, x_pos, y_pos, z_pos, b_field,
320 lengthUnit, BFieldUnit, firstOctant);
323inline InterpolatedMagneticField3 loadDefaultBField(
324 const std::string& fieldMapFile, GenericTransformPos transformPosition,
325 GenericTransformBField transformMagneticField) {
330 return makeMagneticFieldMapXyzFromText(
331 std::move(localToGlobalBinXyz), transformPosition, transformMagneticField,
333 1. * Acts::UnitConstants::mm,
334 1000. * Acts::UnitConstants::T,
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,...