9 #include <geometry/bfieldmap/BFieldComponentBeamline.h>
11 #include <framework/utilities/FileSystem.h>
12 #include <framework/logging/Logger.h>
14 #include <boost/iostreams/filtering_stream.hpp>
15 #include <boost/iostreams/device/file.hpp>
16 #include <boost/iostreams/filter/gzip.hpp>
22 namespace io = boost::iostreams;
64 [[nodiscard]]
const vector<xy_t>&
getPoints()
const {
return m_points;}
66 [[nodiscard]]
const vector<triangle_t>&
getTriangles()
const {
return m_triangles;}
88 void init(vector<xy_t>& points, vector<triangle_t>& triangles,
double d)
90 std::swap(points, m_points);
91 std::swap(triangles, m_triangles);
92 const double inf = numeric_limits<double>::infinity();
93 double xmin = inf, xmax = -inf;
94 double ymin = inf, ymax = -inf;
95 auto limit = [&xmin, &xmax, &ymin, &ymax](
const xy_t & p) ->
void {
96 xmin = std::min(xmin, p.x); xmax = std::max(xmax, p.x);
97 ymin = std::min(ymin, p.y); ymax = std::max(ymax, p.y);
99 for (
const auto& t : m_triangles) {
100 limit(m_points[t.j0]);
101 limit(m_points[t.j1]);
102 limit(m_points[t.j2]);
104 double xw = xmax - xmin;
105 double yw = ymax - ymin;
107 xmin -= xw * (1. / 256), xmax += xw * (1. / 256);
108 ymin -= yw * (1. / 256), ymax += yw * (1. / 256);
109 int nx = lrint(xw * (1 + 1. / 128) / d), ny = lrint(yw * (1 + 1. / 128) / d);
110 nx = std::max(nx, 2);
111 ny = std::max(ny, 2);
112 makeIndex(nx, xmin, xmax, ny, ymin, ymax);
122 const xy_t& p0 = m_points[p.j0], &p1 = m_points[p.j1], &p2 = m_points[p.j2];
123 return {(p0.
x + p1.x + p2.x)* (1. / 3), (p0.
y + p1.y + p2.y)* (1. / 3)};
136 void makeIndex(
int nx,
double xmin,
double xmax,
int ny,
double ymin,
double ymax)
138 m_nx = nx, m_ny = ny, m_xmin = xmin, m_xmax = xmax, m_ymin = ymin, m_ymax = ymax;
139 m_ixnorm = (nx - 1) / (xmax - xmin), m_iynorm = (ny - 1) / (ymax - ymin);
140 double dx = (xmax - xmin) / (nx - 1), dy = (ymax - ymin) / (ny - 1);
142 m_spatialIndex.resize(nx * ny);
143 m_triangleCenters.resize(m_triangles.size());
144 for (
unsigned int i = 0; i < m_triangles.size(); i++) m_triangleCenters[i] = triangleCenter(m_triangles[i]);
146 m_triangleAreas.resize(m_triangles.size());
147 auto getTriangleArea = [
this](
const triangle_t& p) ->
double{
148 const xy_t& p0 = m_points[p.j0], &p1 = m_points[p.j1], &p2 = m_points[p.j2];
149 double d21x = p2.x - p1.x, d21y = p2.y - p1.y;
150 double d01x = p0.
x - p1.x, d01y = p0.
y - p1.y;
151 return 1 / (d21x * d01y - d21y * d01x);
153 for (
unsigned int i = 0; i < m_triangles.size(); i++) m_triangleAreas[i] = getTriangleArea(m_triangles[i]);
156 for (
int ix = 0; ix < nx; ix++) {
157 double x = xmin + ix * dx;
158 for (
int iy = 0; iy < ny; iy++) {
159 double y = ymin + iy * dy;
160 int imin = -1;
double dmin = 1e100;
161 for (
unsigned int i = 0; i < m_triangleCenters.size(); i++) {
162 const xy_t& p = m_triangleCenters[i];
163 double d = pow(p.x - x, 2) + pow(p.y - y, 2);
164 if (d < dmin) {imin = i; dmin = d;}
166 int k = iy + ny * ix;
167 m_spatialIndex[k] = imin;
181 [[nodiscard]]
short int sideCross(
short int prev,
short int curr,
const xy_t& r,
const xy_t& v0)
const
183 const double vx = r.x - v0.
x, vy = r.y - v0.
y;
184 auto isCrossed = [&vx, &vy, &v0](
const xy_t & p1,
const xy_t & p0) ->
bool {
185 double u0x = p0.x, u0y = p0.y;
186 double ux = p1.x - u0x, uy = p1.y - u0y;
187 double dx = u0x - v0.
x, dy = u0y - v0.
y;
188 double D = uy * vx - ux * vy;
189 double t = dx * vy - dy * vx;
190 double s = dx * uy - dy * ux;
191 return ((t < D) != (t < 0)) && ((s < D) != (s < 0));
195 const xy_t& p0 = m_points[p.j0], &p1 = m_points[p.j1], &p2 = m_points[p.j2];
196 if (p.n0 != prev && isCrossed(p1, p2))
return p.n0;
197 if (p.n1 != prev && isCrossed(p2, p0))
return p.n1;
198 if (p.n2 != prev && isCrossed(p0, p1))
return p.n2;
199 return m_triangles.size();
211 void weights(
short int i,
const xy_t& r,
double& w0,
double& w1,
double& w2)
const
214 const xy_t& p0 = m_points[p.j0], &p1 = m_points[p.j1], &p2 = m_points[p.j2];
215 double dx2 = p2.x - r.x, dy2 = p2.y - r.y;
216 double d21x = p2.x - p1.x, d21y = p2.y - p1.y;
217 double d02x = p0.
x - p2.x, d02y = p0.
y - p2.y;
218 w0 = (dx2 * d21y - dy2 * d21x) * m_triangleAreas[i];
219 w1 = (dx2 * d02y - dy2 * d02x) * m_triangleAreas[i];
234 unsigned int ix = lrint((r0.
x - m_xmin) * m_ixnorm), iy = lrint((r0.
y - m_ymin) * m_iynorm);
235 short int curr = (ix < m_nx && iy < m_ny) ? m_spatialIndex[iy + m_ny * ix] : 0;
236 xy_t r = m_triangleCenters[curr];
237 const short int end = m_triangles.size();
238 short int prev = end;
240 short int next = sideCross(prev, curr, r, r0);
241 if (next == end)
break;
310 void init(
const string& fieldmapname,
const string& interpolname,
double validRadius)
312 if (fieldmapname.empty()) {
313 B2ERROR(
"The filename for the beamline magnetic field component is empty !");
316 std::string l_fieldmapname = FileSystem::findFile(
"/data/" + fieldmapname);
318 if (interpolname.empty()) {
319 B2ERROR(
"The filename for the triangulation of the beamline magnetic field component is empty !");
322 std::string l_interpolname = FileSystem::findFile(
"/data/" + interpolname);
324 m_rmax = validRadius;
326 B2DEBUG(50,
"Delaunay triangulation of the beamline field map: " << l_interpolname);
327 B2DEBUG(50,
"Beamline field map: " << l_fieldmapname);
330 ifstream INd(l_interpolname);
332 B2DEBUG(50,
"Total number of triangles: " << nts);
333 vector<triangle_t> ts;
338 while (INd >> nts >> p.j0 >> p.j1 >> p.j2 >> p.n0 >> p.n1 >> p.n2) ts.push_back(p);
342 io::filtering_istream IN;
343 IN.push(io::gzip_decompressor());
344 IN.push(io::file_source(l_fieldmapname));
347 IN >> nrphi >> m_rj >> m_nr >> m_nphi;
348 IN >> m_nz >> m_zj >> m_dz0 >> m_dz1;
349 m_idphi = m_nphi / M_PI;
353 int nz0 = 2 * int(m_zj / m_dz0);
354 m_zmax = (m_nz - (nz0 + 1)) / 2 * m_dz1 + m_zj;
355 m_nz1 = (m_nz - (nz0 + 1)) / 2;
359 struct cs_t {
double c, s;};
360 vector<cs_t> cs(nrphi);
362 vector<ROOT::Math::XYZVector> tbc;
364 char cbuf[256]; IN.getline(cbuf, 256);
366 for (
int j = 0; j < nrphi; j++) {
367 IN.getline(cbuf, 256);
369 double r = strtod(next, &next);
370 double phi = strtod(next, &next);
372 double Br = strtod(next, &next);
373 double Bphi = strtod(next, &next);
374 double Bz = strtod(next,
nullptr);
376 rmax = std::max(r, rmax);
379 }
else if (phi == 180) {
383 cs[j] = {cos(phi), sin(phi)};
385 double x = r * cs[j].c, y = r * cs[j].s;
386 pc.push_back({x, y});
387 if (cs[j].s == 0) Bphi = 0;
388 double Bx = Br * cs[j].c - Bphi * cs[j].s;
389 double By = Br * cs[j].s + Bphi * cs[j].c;
390 tbc.emplace_back(Bx, By, Bz);
394 m_idr = m_nr / (rmax - m_rj);
395 m_rmax = std::min(m_rmax, rmax);
396 bool reduce = m_rj > m_rmax;
403 ip.resize(nrphi,
false);
404 vector<bool> it(ts.size(),
false);
405 auto inside = [
this](
const xy_t & p)->
bool{
406 return p.x * p.x + p.y * p.y < m_rmax * m_rmax;
409 for (
int i = 0, imax = ts.size(); i < imax; i++) {
411 const xy_t& p0 = pc[p.j0], &p1 = pc[p.j1], &p2 = pc[p.j2];
412 if (inside(p0) || inside(p1) || inside(p2)) {
420 vector<short int> pindx(nrphi, -1);
422 for (
int i = 0, imax = ip.size(); i < imax; i++) {
423 if (ip[i]) pindx[i] = rnp++;
427 for (
int i = 0, imax = pc.size(); i < imax; i++) {
428 if (ip[i]) rpc.push_back(pc[i]);
431 vector<short int> tindx(ts.size(), -1);
433 for (
int i = 0, imax = it.size(); i < imax; i++) {
434 if (it[i]) tindx[i] = rnt++;
436 vector<triangle_t> rts;
438 short int nt = ts.size();
439 auto newind = [&nt, &tindx, &rnt](
short int n) ->
short int {
return (n < nt) ? tindx[n] : rnt;};
440 for (
int i = 0, imax = ts.size(); i < imax; i++) {
443 rts.push_back({pindx[t.j0], pindx[t.j1], pindx[t.j2], newind(t.n0), newind(t.n1), newind(t.n2)});
447 B2DEBUG(50,
"Reduce map size to cover only region R<" << m_rmax <<
" cm: Ntriangles=" << rnt <<
" Nxypoints = " << rnp <<
448 " Nzslices=" << m_nz <<
" NBpoints = " << rnp * m_nz);
452 ip.resize(nrphi,
true);
456 m_triInterpol.init(pc, ts, 0.1);
458 vector<ROOT::Math::XYZVector> bc(m_nxy * m_nz);
459 unsigned int count = 0;
460 for (
int i = 0; i < nrphi; i++) {
461 if (ip[i]) bc[count++] = ROOT::Math::XYZVector(tbc[i]);
464 for (
int i = 1; i < m_nz; ++i) {
465 for (
int j = 0; j < nrphi; j++) {
466 IN.getline(cbuf, 256);
467 if (!ip[j])
continue;
469 next = strchr(next,
' ');
470 next = strchr(next + 1,
' ');
471 next = strchr(next + 1,
' ');
472 double Br = strtod(next, &next);
473 double Bphi = strtod(next, &next);
474 double Bz = strtod(next,
nullptr);
475 if (cs[j].s == 0) Bphi = 0;
476 double Bx = Br * cs[j].c - Bphi * cs[j].s;
477 double By = Br * cs[j].s + Bphi * cs[j].c;
478 bc[count++].SetXYZ(Bx, By, Bz);
481 assert(count == bc.size());
496 if (std::abs(z) > m_zmax)
return -1;
500 fz = (z + m_zmax) * m_idz1;
501 }
else if (z < m_zj) {
502 fz = (z + m_zj) * m_idz0;
505 fz = (z - m_zj) * m_idz1;
508 int jz =
static_cast<int>(fz);
527 [[nodiscard]]
bool inRange(
const ROOT::Math::XYZVector& v)
const
529 if (std::abs(v.Z()) > m_zmax)
return false;
530 double R2 = v.X() * v.X() + v.Y() * v.Y();
531 if (R2 > m_rmax * m_rmax)
return false;
546 ROOT::Math::XYZVector res = {0, 0, 0};
547 double R2 = v.X() * v.X() + v.Y() * v.Y();
548 if (R2 > m_rmax * m_rmax)
return res;
550 int iz = zIndexAndWeight(v.Z(), wz1);
551 if (iz < 0)
return res;
552 double wz0 = 1 - wz1;
555 xy_t xy = {v.X(), std::abs(v.Y())};
557 short int it = m_triInterpol.findTriangle(xy);
558 m_triInterpol.weights(it, xy, w0, w1, w2);
559 auto t = m_triInterpol.getTriangles().begin() + it;
560 int j0 = t->j0, j1 = t->j1, j2 = t->j2;
561 const ROOT::Math::XYZVector* B = m_B.data() + m_nxy * iz;
562 ROOT::Math::XYZVector b = (B[j0] * w0 + B[j1] * w1 + B[j2] * w2) * wz0;
564 b += (B[j0] * w0 + B[j1] * w1 + B[j2] * w2) * wz1;
567 double r =
sqrt(R2), phi = atan2(std::abs(v.Y()), v.X());
568 double fr = (r - m_rj) * m_idr;
569 double fphi = phi * m_idphi;
571 int ir =
static_cast<int>(fr);
572 int iphi =
static_cast<int>(fphi);
575 iphi -= (iphi == m_nphi);
577 double wr1 = fr - ir, wr0 = 1 - wr1;
578 double wphi1 = fphi - iphi, wphi0 = 1 - wphi1;
579 const int nr1 = m_nr + 1, nphi1 = m_nphi + 1;
580 int j00 = m_nxy - nr1 * (nphi1 - iphi) + ir;
585 double w00 = wr0 * wphi0, w01 = wphi0 * wr1, w10 = wphi1 * wr0, w11 = wphi1 * wr1;
586 const ROOT::Math::XYZVector* B = m_B.data() + m_nxy * iz;
587 ROOT::Math::XYZVector b = (B[j00] * w00 + B[j01] * w01 + B[j10] * w10 + B[j11] * w11) * wz0;
589 b += (B[j00] * w00 + B[j01] * w01 + B[j10] * w10 + B[j11] * w11) * wz1;
592 if (v.Y() < 0) res.SetY(-res.Y());
597 vector<ROOT::Math::XYZVector>
m_B;
636 void BFieldComponentBeamline::initialize()
640 m_ler->
init(m_mapFilename_ler, m_interFilename_ler, m_mapRegionR[1]);
641 m_her->init(m_mapFilename_her, m_interFilename_her, m_mapRegionR[1]);
644 BFieldComponentBeamline::~BFieldComponentBeamline()
646 if (m_ler)
delete m_ler;
647 if (m_her)
delete m_her;
650 bool BFieldComponentBeamline::isInRange(
const ROOT::Math::XYZVector& p)
const
652 if (!m_ler || !m_her)
return false;
653 double s = m_sinBeamCrossAngle, c = m_cosBeamCrossAngle;
654 ROOT::Math::XYZVector v = -p;
655 double xc = v.X() * c, zs = v.Z() * s, zc = v.Z() * c, xs = v.X() * s;
656 ROOT::Math::XYZVector hv{xc - zs, v.Y(), zc + xs};
657 ROOT::Math::XYZVector lv{xc + zs, v.Y(), zc - xs};
658 return m_ler->inRange(lv) || m_her->inRange(hv);
661 ROOT::Math::XYZVector BFieldComponentBeamline::calculate(
const ROOT::Math::XYZVector& p)
const
663 ROOT::Math::XYZVector res;
664 double s = m_sinBeamCrossAngle, c = m_cosBeamCrossAngle;
665 ROOT::Math::XYZVector v = -p;
666 double xc = v.X() * c, zs = v.Z() * s, zc = v.Z() * c, xs = v.X() * s;
667 ROOT::Math::XYZVector hv{xc - zs, v.Y(), zc + xs};
668 ROOT::Math::XYZVector lv{xc + zs, v.Y(), zc - xs};
669 ROOT::Math::XYZVector hb = m_her->interpolateField(hv);
670 ROOT::Math::XYZVector lb = m_ler->interpolateField(lv);
671 ROOT::Math::XYZVector rhb{hb.X()* c + hb.Z()* s, hb.Y(), hb.Z()* c - hb.X()* s};
672 ROOT::Math::XYZVector rlb{lb.X()* c - lb.Z()* s, lb.Y(), lb.Z()* c + lb.X()* s};
674 double mhb = std::abs(rhb.X()) + std::abs(rhb.Y()) + std::abs(rhb.Z());
675 double mlb = std::abs(rlb.X()) + std::abs(rlb.Y()) + std::abs(rlb.Z());
677 if (mhb < 1e-10) res = rlb;
678 else if (mlb < 1e-10) res = rhb;
680 res = 0.5 * (rlb + rhb);
685 void BFieldComponentBeamline::terminate()
700 if (*gInstance ==
nullptr) {
707 BFieldComponentBeamline::BFieldComponentBeamline()
710 if (*gInstance !=
nullptr) {
711 B2WARNING(
"BFieldComponentBeamline: object already instantiated");
The BFieldComponentBeamline class.
The BeamlineFieldMapInterpolation class.
bool inRange(const ROOT::Math::XYZVector &v) const
Check the space point if the interpolation exists.
int zIndexAndWeight(double z, double &w1) const
For a given Z coordinate calculate the index of Z slice and corresponding weight.
ROOT::Math::XYZVector interpolateField(const ROOT::Math::XYZVector &v) const
Interpolate the magnetic field vector at the specified space point.
const TriangularInterpolation & getTriangularInterpolation() const
Expose the triangular interpolation to outside.
vector< ROOT::Math::XYZVector > m_B
Buffer for the magnetic field map.
TriangularInterpolation m_triInterpol
Object to locate point in a triangular mesh.
void init(const string &fieldmapname, const string &interpolname, double validRadius)
Initializes the magnetic field component.
BeamlineFieldMapInterpolation()=default
Default constructor.
~BeamlineFieldMapInterpolation()=default
Default destructor.
The TriangularInterpolation class.
vector< short int > m_spatialIndex
Spatial index.
double m_xmin
Border of the region where the spatial index is constructed.
unsigned int m_ny
Spatial index grid size.
~TriangularInterpolation()=default
Destructor.
TriangularInterpolation(vector< xy_t > &pc, vector< triangle_t > &ts, double d)
More complex constructor.
vector< triangle_t > m_triangles
Triangle list.
vector< xy_t > m_points
Vertex list.
void init(vector< xy_t > &points, vector< triangle_t > &triangles, double d)
Calculate extents of a triangular mesh and build spatial index.
xy_t triangleCenter(const triangle_t &p) const
Calculate triangle center.
double m_ymin
Border of the region where the spatial index is constructed.
const vector< triangle_t > & getTriangles() const
returns list of triangles
vector< xy_t > m_triangleCenters
Triangle centers.
short int sideCross(short int prev, short int curr, const xy_t &r, const xy_t &v0) const
Determine which triangle side is crossed by a line segment defined by r and v0 points.
double m_xmax
Border of the region where the spatial index is constructed.
void makeIndex(int nx, double xmin, double xmax, int ny, double ymin, double ymax)
Make spatial index.
vector< double > m_triangleAreas
Triangle areas.
unsigned int m_nx
Spatial index grid size.
short int findTriangle(const xy_t &r0) const
Find the triangle which contain the point.
const vector< xy_t > & getPoints() const
returns list of verticies
TriangularInterpolation()=default
Default constructor.
void weights(short int i, const xy_t &r, double &w0, double &w1, double &w2) const
Calculate barycentric coordinates of a point inside triangle.
double m_ymax
Border of the region where the spatial index is constructed.
double sqrt(double a)
sqrt for double
BFieldComponentBeamline ** GetInstancePtr()
Static function holding the instance.
Abstract base class for different kinds of events.
A simple 2d vector stucture.