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/AxisFwd.hpp"
12#include "Acts/Utilities/Grid.hpp"
13#include "Acts/Utilities/Interpolation.hpp"
14#include "Acts/Utilities/Result.hpp"
15
16static const double DIPOLE_OFFSET = 400.; // 400 mm
17
18using InterpolatedMagneticField3 = Acts::InterpolatedBFieldMap<
19 Acts::Grid<Acts::Vector3, Acts::Axis<Acts::AxisType::Equidistant>,
20 Acts::Axis<Acts::AxisType::Equidistant>,
21 Acts::Axis<Acts::AxisType::Equidistant>>>;
22
23using GenericTransformPos = std::function<Acts::Vector3(const Acts::Vector3&)>;
24using GenericTransformBField =
25 std::function<Acts::Vector3(const Acts::Vector3&, const Acts::Vector3&)>;
26
35Acts::Vector3 defaultTransformPos(const Acts::Vector3& pos_);
36
43Acts::Vector3 defaultTransformBField(const Acts::Vector3& field,
44 const Acts::Vector3& /*pos_*/);
45
46void testField(const std::shared_ptr<Acts::MagneticFieldProvider> bfield,
47 const Acts::Vector3& eval_pos,
48 const Acts::MagneticFieldContext& bctx);
49
55size_t localToGlobalBinXyz(std::array<size_t, 3> bins,
56 std::array<size_t, 3> sizes);
57
58inline InterpolatedMagneticField3 rotateFieldMapXYZ(
59 const std::function<size_t(std::array<size_t, 3> binsXYZ,
60 std::array<size_t, 3> nBinsXYZ)>&
61 localToGlobalBin,
62 std::vector<double> xPos, std::vector<double> yPos,
63 std::vector<double> zPos, std::vector<Acts::Vector3> bField,
64 double lengthUnit, double BFieldUnit, bool firstOctant,
65 GenericTransformPos transformPosition,
66 GenericTransformBField transformMagneticField) {
67 // [1] Create Grid
68 // Sort the values
69 std::sort(xPos.begin(), xPos.end());
70 std::sort(yPos.begin(), yPos.end());
71 std::sort(zPos.begin(), zPos.end());
72
73 // Get unique values
74 xPos.erase(std::unique(xPos.begin(), xPos.end()), xPos.end());
75 yPos.erase(std::unique(yPos.begin(), yPos.end()), yPos.end());
76 zPos.erase(std::unique(zPos.begin(), zPos.end()), zPos.end());
77 xPos.shrink_to_fit();
78 yPos.shrink_to_fit();
79 zPos.shrink_to_fit();
80
81 // get the number of bins
82 size_t n_bins_x = xPos.size();
83 size_t n_bins_y = yPos.size();
84 size_t n_bins_z = zPos.size();
85
86 // get the minimum and maximum
87 auto min_max_x = std::minmax_element(xPos.begin(), xPos.end());
88 auto min_max_y = std::minmax_element(yPos.begin(), yPos.end());
89 auto min_max_z = std::minmax_element(zPos.begin(), zPos.end());
90 // Create the axis for the grid
91 // get minima
92 double x_min = *min_max_x.first;
93 double y_min = *min_max_y.first;
94 double z_min = *min_max_z.first;
95 // get maxima
96 double x_max = *min_max_x.second;
97 double y_max = *min_max_y.second;
98 double z_max = *min_max_z.second;
99 // calculate maxima (add one last bin, because bin value always corresponds to
100 // left boundary)
101 double step_z = std::fabs(z_max - z_min) / (n_bins_z - 1);
102 double step_y = std::fabs(y_max - y_min) / (n_bins_y - 1);
103 double step_x = std::fabs(x_max - x_min) / (n_bins_x - 1);
104 x_max += step_x;
105 y_max += step_y;
106 z_max += step_z;
107
108 // If only the first octant is given
109 if (firstOctant) {
110 x_min = -*min_max_x.second;
111 y_min = -*min_max_y.second;
112 z_min = -*min_max_z.second;
113 n_bins_x = 2 * n_bins_x - 1;
114 n_bins_y = 2 * n_bins_y - 1;
115 n_bins_z = 2 * n_bins_z - 1;
116 }
117 Acts::Axis<Acts::AxisType::Equidistant> x_axis(x_min * lengthUnit,
118 x_max * lengthUnit, n_bins_x);
119 Acts::Axis<Acts::AxisType::Equidistant> y_axis(y_min * lengthUnit,
120 y_max * lengthUnit, n_bins_y);
121 Acts::Axis<Acts::AxisType::Equidistant> z_axis(z_min * lengthUnit,
122 z_max * lengthUnit, n_bins_z);
123 // Create the grid
124 using Grid_t =
125 Acts::Grid<Acts::Vector3, Acts::Axis<Acts::AxisType::Equidistant>,
126 Acts::Axis<Acts::AxisType::Equidistant>,
127 Acts::Axis<Acts::AxisType::Equidistant>>;
128 Grid_t grid(
129 std::make_tuple(std::move(x_axis), std::move(y_axis), std::move(z_axis)));
130
131 // [2] Set the bField values
132 for (size_t i = 1; i <= n_bins_x; ++i) {
133 for (size_t j = 1; j <= n_bins_y; ++j) {
134 for (size_t k = 1; k <= n_bins_z; ++k) {
135 Grid_t::index_t indices = {{i, j, k}};
136 std::array<size_t, 3> n_indices = {
137 {xPos.size(), yPos.size(), zPos.size()}};
138 if (firstOctant) {
139 // std::vectors begin with 0 and we do not want the user needing to
140 // take underflow or overflow bins in account this is why we need to
141 // subtract by one
142 size_t m = std::abs(int(i) - (int(xPos.size())));
143 size_t n = std::abs(int(j) - (int(yPos.size())));
144 size_t l = std::abs(int(k) - (int(zPos.size())));
145 Grid_t::index_t indices_first_octant = {{m, n, l}};
146
147 grid.atLocalBins(indices) =
148 bField.at(localToGlobalBin(indices_first_octant, n_indices)) *
149 BFieldUnit;
150
151 } else {
152 // std::vectors begin with 0 and we do not want the user needing to
153 // take underflow or overflow bins in account this is why we need to
154 // subtract by one
155 grid.atLocalBins(indices) =
156 bField.at(localToGlobalBin({{i - 1, j - 1, k - 1}}, n_indices)) *
157 BFieldUnit;
158 }
159 }
160 }
161 }
162 grid.setExteriorBins(Acts::Vector3::Zero());
163
164 // [3] Create the transformation for the position
165 // map (z,x,y) -> (x,y,z)
166
167 /*
168 auto transformPos = [](const Acts::Vector3& pos_, float offset=400.) {
169
170 Acts::Vector3 rot_pos;
171 rot_pos(0)=pos_(1);
172 rot_pos(1)=pos_(2);
173 rot_pos(2)=pos_(0) + offset;
174
175 return rot_pos;
176 };
177
178 */
179
180 // [4] Create the transformation for the bfield
181 // map (Bx,By,Bz) -> (Bx,By,Bz)
182
183 // auto transformBField = [](const Acts::Vector3& field,
184 // const Acts::Vector3& /*pos_*/) {
185 //
186 //
187 // Acts::Vector3 rot_field;
188 //
189 // rot_field(0) = field(2);
190 // rot_field(1) = field(0);
191 // rot_field(2) = field(1);
192
193 // return rot_field;
194 //};
195
196 // [5] Create the mapper and BField Service
197 // with the transformations passed from main producer
198 return Acts::InterpolatedBFieldMap<Grid_t>(
199 {transformPosition, transformMagneticField, std::move(grid)});
200}
201
202// This is a copy of
203// https://github.com/acts-project/acts/blob/main/Examples/Detectors/MagneticField/src/FieldMapTextIo.cpp
204// with additional rotateAxes flag to rotate the axes and field to be in the
205// tracking (ACTS) Frame
206
207inline InterpolatedMagneticField3 makeMagneticFieldMapXyzFromText(
208 std::function<size_t(std::array<size_t, 3> binsXYZ,
209 std::array<size_t, 3> nBinsXYZ)>
210 localToGlobalBin,
211 GenericTransformPos transformPosition,
212 GenericTransformBField transformMagneticField,
213 const std::string& fieldMapFile, Acts::ActsScalar lengthUnit,
214 Acts::ActsScalar BFieldUnit, bool firstOctant, bool rotateAxes) {
216 // Grid position points in x, y and z
217 std::vector<double> x_pos;
218 std::vector<double> y_pos;
219 std::vector<double> z_pos;
220 // components of magnetic field on grid points
221 std::vector<Acts::Vector3> b_field;
222
223 constexpr size_t k_default_size = 1 << 15;
224 // reserve estimated size
225 x_pos.reserve(k_default_size);
226 y_pos.reserve(k_default_size);
227 z_pos.reserve(k_default_size);
228 b_field.reserve(k_default_size);
229 // [1] Read in file and fill values
230 std::ifstream map_file(fieldMapFile.c_str(), std::ios::in);
231 std::string line;
232 double pos_x = 0., pos_y = 0., pos_z = 0.;
233 double bx = 0., by = 0., bz = 0.;
234
235 bool header_found = false;
236
237 while (std::getline(map_file, line)) {
238 if (line.empty() || line[0] == '%' || line[0] == '#' || line[0] == ' ' ||
239 line.find_first_not_of(' ') == std::string::npos || !header_found) {
240 if (line.find("Header") != std::string::npos) header_found = true;
241 continue;
242 }
243 std::istringstream tmp(line);
244 tmp >> pos_x >> pos_y >> pos_z >> bx >> by >> bz;
245
246 x_pos.push_back(pos_x);
247 y_pos.push_back(pos_y);
248 z_pos.push_back(pos_z);
249 b_field.push_back(Acts::Vector3(bx, by, bz));
250 }
251 map_file.close();
252
253 if (!header_found) {
254 std::cout << "MAP LOADING ERROR:: line containing the word 'Header' not "
255 "found in the BMap."
256 << std::endl;
257 }
258
259 x_pos.shrink_to_fit();
260 y_pos.shrink_to_fit();
261 z_pos.shrink_to_fit();
262 b_field.shrink_to_fit();
263
264 if (rotateAxes) {
265 return rotateFieldMapXYZ(localToGlobalBin, x_pos, y_pos, z_pos, b_field,
266 lengthUnit, BFieldUnit, firstOctant,
267 transformPosition, transformMagneticField);
268 } else
269 return Acts::fieldMapXYZ(localToGlobalBin, x_pos, y_pos, z_pos, b_field,
270 lengthUnit, BFieldUnit, firstOctant);
271}
272
273inline InterpolatedMagneticField3 loadDefaultBField(
274 const std::string& fieldMapFile, GenericTransformPos transformPosition,
275 GenericTransformBField transformMagneticField) {
276 // std::function<Acts::Vector3(const Acts::Vector3&, float)>
277 // transformPosition, std::function<Acts::Vector3(const Acts::Vector3&,const
278 // Acts::Vector3&)> transformMagneticField
279
280 return makeMagneticFieldMapXyzFromText(
281 std::move(localToGlobalBinXyz), transformPosition, transformMagneticField,
282 fieldMapFile,
283 1. * Acts::UnitConstants::mm, // default scale for axes length
284 1000. * Acts::UnitConstants::T, // The map is in kT, so scale it to T
285 false, // not symmetrical
286 true // rotate the axes to tracking frame
287 );
288}
289
290// R =
291//
292// 0 0 1
293// 1 0 0
294// 0 1 0