19#include "FairLogger.h"
24 const std::string& mapFileName, Float_t xOffset,
25 Float_t yOffset, Float_t zOffset, Float_t phi,
26 Float_t theta, Float_t psi, Float_t scale,
28 : TVirtualMagField(label.c_str()),
77 Float_t newYOffset, Float_t newZOffset,
78 Float_t newPhi, Float_t newTheta, Float_t newPsi,
80 : TVirtualMagField(newName.c_str()),
123 Double_t localCoords[3] = {position[0], position[1], position[2]};
126 theTrans_->MasterToLocal(position, localCoords);
130 Float_t x = localCoords[0];
131 Float_t y = localCoords[1];
132 Float_t z = localCoords[2];
168 if (inside == kFALSE) {
177 Int_t iX = xBinInfo.first;
178 Int_t iY = yBinInfo.first;
179 Int_t iZ = zBinInfo.first;
182 if (iX == -1 || iY == -1 || iZ == -1) {
191 const Int_t binA = this->
getMapBin(iX, iY, iZ);
192 const Int_t binB = this->
getMapBin(iX1, iY, iZ);
193 const Int_t binC = this->
getMapBin(iX, iY1, iZ);
194 const Int_t binD = this->
getMapBin(iX1, iY1, iZ);
195 const Int_t binE = this->
getMapBin(iX, iY, iZ1);
196 const Int_t binF = this->
getMapBin(iX1, iY, iZ1);
197 const Int_t binG = this->
getMapBin(iX, iY1, iZ1);
198 const Int_t binH = this->
getMapBin(iX1, iY1, iZ1);
201 const Float_t xFrac = xBinInfo.second;
202 const Float_t yFrac = yBinInfo.second;
203 const Float_t zFrac = zBinInfo.second;
204 const Float_t xFrac1 = 1.0 - xFrac;
205 const Float_t yFrac1 = 1.0 - yFrac;
206 const Float_t zFrac1 = 1.0 - zFrac;
216 const std::array<Float_t, 3>& cA = fm[binA];
217 const std::array<Float_t, 3>& cB = fm[binB];
218 const std::array<Float_t, 3>& cC = fm[binC];
219 const std::array<Float_t, 3>& cD = fm[binD];
220 const std::array<Float_t, 3>& cE = fm[binE];
221 const std::array<Float_t, 3>& cF = fm[binF];
222 const std::array<Float_t, 3>& cG = fm[binG];
223 const std::array<Float_t, 3>& cH = fm[binH];
226 for (Int_t i = 0; i < 3; ++i) {
227 Bc[i] =
triLinearInterp(cA[i], cB[i], cC[i], cD[i], cE[i], cF[i], cG[i],
228 cH[i], xFrac, xFrac1, yFrac, yFrac1, zFrac, zFrac1);
231 B[0] = Bc[0] *
scale_ * BxSign;
233 B[2] = Bc[2] *
scale_ * BzSign;
243 if (fabs(
phi_) > 1e-6 || fabs(
theta_) > 1e-6 || fabs(
psi_) > 1e-6) {
260 LOG(info) <<
"ShipBFieldMap::readMapFile() creating field " << this->GetName()
276 LOG(fatal) <<
"ShipBFieldMap: could not find the file " <<
mapFileName_;
280 TTree* rTree =
dynamic_cast<TTree*
>(theFile->Get(
"Range"));
282 LOG(fatal) <<
"ShipBFieldMap: could not find Range tree in "
286 rTree->SetBranchAddress(
"xMin", &
xMin_);
287 rTree->SetBranchAddress(
"xMax", &
xMax_);
288 rTree->SetBranchAddress(
"dx", &
dx_);
289 rTree->SetBranchAddress(
"yMin", &
yMin_);
290 rTree->SetBranchAddress(
"yMax", &
yMax_);
291 rTree->SetBranchAddress(
"dy", &
dy_);
292 rTree->SetBranchAddress(
"zMin", &
zMin_);
293 rTree->SetBranchAddress(
"zMax", &
zMax_);
294 rTree->SetBranchAddress(
"dz", &
dz_);
306 TTree* dTree =
dynamic_cast<TTree*
>(theFile->Get(
"Data"));
308 LOG(fatal) <<
"ShipBFieldMap: could not find Data tree in "
312 const Int_t nEntries = dTree->GetEntries();
313 if (nEntries !=
N_) {
314 LOG(fatal) <<
"Expected " <<
N_ <<
" field map entries but found "
322 dTree->SetBranchStatus(
"*", 0);
323 dTree->SetBranchStatus(
"Bx", 1);
324 dTree->SetBranchStatus(
"By", 1);
325 dTree->SetBranchStatus(
"Bz", 1);
327 dTree->SetBranchAddress(
"Bx", &Bx);
328 dTree->SetBranchAddress(
"By", &By);
329 dTree->SetBranchAddress(
"Bz", &Bz);
333 for (Int_t i = 0; i < nEntries; i++) {
357 if (!dTree->GetBranch(
"x") || !dTree->GetBranch(
"y") ||
358 !dTree->GetBranch(
"z")) {
360 <<
" stores no x,y,z branches, so the assumed ascending z,y,x "
361 "ordering cannot be verified. Convert the map with the node "
362 "coordinates stored.";
366 Float_t x(0.0), y(0.0), z(0.0);
367 dTree->SetBranchStatus(
"*", 0);
368 dTree->SetBranchStatus(
"x", 1);
369 dTree->SetBranchStatus(
"y", 1);
370 dTree->SetBranchStatus(
"z", 1);
371 dTree->SetBranchAddress(
"x", &x);
372 dTree->SetBranchAddress(
"y", &y);
373 dTree->SetBranchAddress(
"z", &z);
377 const std::array<Int_t, 5> probes{0, 1,
Nz_,
Ny_ *
Nz_,
N_ - 1};
379 for (
const Int_t entry : probes) {
380 const Int_t iX = entry / (
Ny_ *
Nz_);
381 const Int_t iY = (entry /
Nz_) %
Ny_;
382 const Int_t iZ = entry %
Nz_;
384 dTree->GetEntry(entry);
388 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z)) {
389 LOG(fatal) <<
"ShipBFieldMap: field map " <<
mapFileName_ <<
" entry "
390 << entry <<
" holds a non-finite coordinate (" << x <<
", "
391 << y <<
", " << z <<
") cm.";
398 const Int_t nodeX =
static_cast<Int_t
>(std::lround((x -
xMin_) /
dx_));
399 const Int_t nodeY =
static_cast<Int_t
>(std::lround((y -
yMin_) /
dy_));
400 const Int_t nodeZ =
static_cast<Int_t
>(std::lround((z -
zMin_) /
dz_));
402 if (nodeX != iX || nodeY != iY || nodeZ != iZ) {
403 LOG(fatal) <<
"ShipBFieldMap: field map " <<
mapFileName_
404 <<
" is not stored in ascending z,y,x coordinate order. "
405 <<
"Entry " << entry <<
" should hold node (" << iX <<
", "
406 << iY <<
", " << iZ <<
") at (" <<
xMin_ + iX *
dx_ <<
", "
408 <<
") cm but holds node (" << nodeX <<
", " << nodeY <<
", "
409 << nodeZ <<
") at (" << x <<
", " << y <<
", " << z
410 <<
") cm. Repack it with field/canonicaliseFieldMap.py.";
417 dTree->ResetBranchAddresses();
418 dTree->SetBranchStatus(
"*", 1);
424 if (!getData.is_open()) {
425 LOG(fatal) <<
"Error: Cannot open magnetic field map file: "
429 std::string tmpString(
"");
442 getData >> tmpString >> tmpString >> tmpString;
449 Float_t Bx(0.0), By(0.0), Bz(0.0);
451 for (Int_t i = 0; i <
N_; i++) {
452 getData >> Bx >> By >> Bz;
469 Bool_t inside(kFALSE);
471 if (x >=
xMin_ && x <= xMax_ && y >=
yMin_ && y <= yMax_ && z >=
zMin_ &&
503 LOG(fatal) <<
"ShipBFieldMap: field map needs at least 2 bins per axis; "
504 <<
"got Nx = " <<
Nx_ <<
", Ny = " <<
Ny_ <<
", Nz = " <<
Nz_;
510 const Float_t tolerance(1e-2);
514 LOG(fatal) <<
"ShipBFieldMap: field map extents are not whole multiples of "
515 <<
"the bin widths: " <<
xRange_ <<
" / " <<
dx_ <<
", "
521 LOG(info) <<
"x limits: " <<
xMin_ <<
", " <<
xMax_ <<
", dx = " <<
dx_;
522 LOG(info) <<
"y limits: " <<
yMin_ <<
", " <<
yMax_ <<
", dy = " <<
dy_;
523 LOG(info) <<
"z limits: " <<
zMin_ <<
", " <<
zMax_ <<
", dz = " <<
dz_;
527 LOG(info) <<
"Angles : phi = " <<
phi_ <<
", theta = " <<
theta_
528 <<
", psi = " <<
psi_;
530 LOG(info) <<
"Total number of bins = " <<
N_ <<
"; Nx = " <<
Nx_
531 <<
", Ny = " <<
Ny_ <<
", Nz = " <<
Nz_;
536 Float_t du(0.0), uMin(0.0);
559 Float_t dist = (u - uMin) / du;
561 iBin =
static_cast<Int_t
>(dist);
564 fracL = (dist - iBin * 1.0);
568 if (iBin < 0 || iBin >= Nu) {
571 }
else if (iBin >= Nu - 1) {
592 Int_t index = (iX *
Ny_ + iY) *
Nz_ + iZ;
595 }
else if (index >=
N_) {
603 Float_t D, Float_t E, Float_t F,
604 Float_t G, Float_t H, Float_t xFrac,
605 Float_t xFrac1, Float_t yFrac,
606 Float_t yFrac1, Float_t zFrac,
611 const Float_t F00 = A * xFrac1 + B * xFrac;
612 const Float_t F10 = C * xFrac1 + D * xFrac;
613 const Float_t F01 = E * xFrac1 + F * xFrac;
614 const Float_t F11 = G * xFrac1 + H * xFrac;
617 const Float_t F0 = F00 * yFrac1 + F10 * yFrac;
618 const Float_t F1 = F01 * yFrac1 + F11 * yFrac;
621 return F0 * zFrac1 + F1 * zFrac;
Float_t xMax_
The maximum value of x for the map.
Float_t dy_
The bin width along y.
static Float_t triLinearInterp(Float_t A, Float_t B, Float_t C, Float_t D, Float_t E, Float_t F, Float_t G, Float_t H, Float_t xFrac, Float_t xFrac1, Float_t yFrac, Float_t yFrac1, Float_t zFrac, Float_t zFrac1)
Float_t psi_
The third Euler rotation angle about the new z axis.
Float_t dz_
The bin width along z.
Float_t zRange_
The coordinate range along z.
std::pair< Int_t, Float_t > binPair
Typedef for an int-double pair.
ShipBFieldMap(const std::string &label, const std::string &mapFileName, Float_t xOffset=0.0, Float_t yOffset=0.0, Float_t zOffset=0.0, Float_t phi=0.0, Float_t theta=0.0, Float_t psi=0.0, Float_t scale=1.0, Bool_t isSymmetric=kFALSE)
Constructor.
Int_t Nz_
The number of bins along z.
Float_t Tesla_
Double converting Tesla to kiloGauss (for VMC/FairRoot B field units).
Int_t N_
The total number of bins.
Float_t yMin_
The minimum value of y for the map.
Float_t zOffset_
The z value of the positional offset for the map.
std::string mapFileName_
The name of the map file.
Bool_t initialised_
Initialisation boolean.
Int_t Ny_
The number of bins along y.
std::string GetMapFileName() const
Get the name of the map file.
Float_t theta_
The second Euler rotation angle about the new x axis.
Float_t xMin_
The minimum value of x for the map.
void Field(const Double_t *position, Double_t *B) override
Implementation of evaluating the B field.
void initialise()
Initialisation.
CoordAxis
Enumeration to specify the coordinate type.
Int_t Nx_
The number of bins along x.
void checkRootFileOrdering(TTree *dTree)
Check that the ROOT map really uses the assumed data ordering.
Float_t xRange_
The coordinate range along x.
Float_t yOffset_
The y value of the positional offset for the map.
void readTextFile()
Process the text file containing the field map data.
Float_t scale_
The B field magnitude scaling factor.
TGeoMatrix * theTrans_
The combined translation and rotation transformation.
binPair getBinInfo(Float_t x, CoordAxis theAxis)
Get the bin number and fractional distance from the leftmost bin edge.
Bool_t insideRange(Float_t x, Float_t y, Float_t z)
Check to see if a point is within the map validity range.
std::vector< std::array< Float_t, 3 > > floatArray
void readRootFile()
Process the ROOT file containing the field map data.
~ShipBFieldMap() override
Destructor.
void readMapFile()
Read the field map data and store the information internally.
Int_t getMapBin(Int_t iX, Int_t iY, Int_t iZ)
Find the vector entry of the field map data given the bins iX, iY and iZ.
Float_t yMax_
The maximum value of y for the map.
Float_t yRange_
The coordinate range along y.
Float_t zMax_
The maximum value of z for the map.
Float_t phi_
The first Euler rotation angle about the z axis.
Float_t zMin_
The minimum value of z for the map.
Float_t dx_
The bin width along x.
Bool_t isSymmetric_
The boolean specifying if we have quadrant symmetry.
Float_t xOffset_
The x value of the positional offset for the map.