5 #include <TDirectory.h>
10 #include <Geant4/G4SystemOfUnits.hh>
13 #pragma GCC diagnostic push
14 #pragma GCC diagnostic ignored "-Wshadow"
15 #include <boost/stacktrace.hpp>
16 #pragma GCC diagnostic pop
29 for (
int i = 0;
i < 2;
i++)
31 for (
int j = 0;
j < 2;
j++)
33 for (
int k = 0;
k < 2;
k++)
35 for (
int l = 0; l < 3; l++)
43 std::cout <<
"\n================ Begin Construct Mag Field =====================" << std::endl;
44 std::cout <<
"\n-----------------------------------------------------------"
45 <<
"\n Magnetic field Module - Verbosity:"
46 <<
"\n-----------------------------------------------------------";
49 TFile *rootinput = TFile::Open(
filename.c_str());
52 std::cout <<
"\n could not open " <<
filename <<
" exiting now" << std::endl;
56 std::cout <<
"\n ---> "
57 "Reading the field grid from "
61 TNtuple *field_map =
nullptr;
62 rootinput->GetObject(
"fieldmap", field_map);
63 if (field_map ==
nullptr)
65 std::cout <<
PHWHERE <<
" Could not load fieldmap ntuple from "
66 <<
filename <<
" exiting now" << std::endl;
70 Float_t ROOT_X, ROOT_Y, ROOT_Z;
71 Float_t ROOT_BX, ROOT_BY, ROOT_BZ;
72 field_map->SetBranchAddress(
"x", &ROOT_X);
73 field_map->SetBranchAddress(
"y", &ROOT_Y);
74 field_map->SetBranchAddress(
"z", &ROOT_Z);
75 field_map->SetBranchAddress(
"bx", &ROOT_BX);
76 field_map->SetBranchAddress(
"by", &ROOT_BY);
77 field_map->SetBranchAddress(
"bz", &ROOT_BZ);
78 for (
int i = 0;
i < field_map->GetEntries();
i++)
80 field_map->GetEntry(
i);
81 trio coord_key(ROOT_X *
cm, ROOT_Y * cm, ROOT_Z * cm);
82 trio field_val(ROOT_BX * tesla * magfield_rescale, ROOT_BY * tesla * magfield_rescale, ROOT_BZ * tesla * magfield_rescale);
83 xvals.insert(ROOT_X * cm);
84 yvals.insert(ROOT_Y * cm);
85 zvals.insert(ROOT_Z * cm);
86 if ((std::sqrt(ROOT_X * cm * ROOT_X * cm + ROOT_Y * cm * ROOT_Y * cm) >= innerradius &&
87 std::sqrt(ROOT_X * cm * ROOT_X * cm + ROOT_Y * cm * ROOT_Y * cm) <= outerradius) ||
88 std::abs(ROOT_Z * cm) > size_z)
100 std::cout <<
"PHField3DCartesian: Compiler bug!!!!!!!! Do not use inlining!!!!!!" << std::endl;
101 std::cout <<
"exiting now - recompile with -fno-inline" << std::endl;
114 std::cout <<
"\n================= End Construct Mag Field ======================\n"
122 std::cout <<
"PHField3DCartesian: cache hits: " <<
cache_hits
130 static double xsav = -1000000.;
131 static double ysav = -1000000.;
132 static double zsav = -1000000.;
141 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z))
143 static int ifirst = 0;
146 std::cout <<
"PHField3DCartesian::GetFieldValue: "
147 <<
"Invalid coordinates: "
151 <<
" bailing out returning zero bfield"
153 std::cout <<
"previous point: "
154 <<
"x: " << xsav /
cm
155 <<
", y: " << ysav /
cm
156 <<
", z: " << zsav /
cm
158 std::cout <<
"Here is the stacktrace: " << std::endl;
159 std::cout << boost::stacktrace::stacktrace();
160 std::cout <<
"This is not a segfault. Check the stacktrace for the guilty party (typically #2)" << std::endl;
169 if (point[0] <
xmin || point[0] >
xmax ||
170 point[1] <
ymin || point[1] >
ymax ||
176 std::set<float>::const_iterator
it =
xvals.lower_bound(x);
178 if (it ==
xvals.begin())
183 std::cout <<
PHWHERE <<
": This should not happen! x too small - outside range: " << x /
cm << std::endl;
194 it =
yvals.lower_bound(y);
196 if (it ==
yvals.begin())
201 std::cout <<
PHWHERE <<
": This should not happen! y too small - outside range: " << y /
cm << std::endl;
211 it =
zvals.lower_bound(z);
213 if (it ==
zvals.begin())
218 std::cout <<
PHWHERE <<
": This should not happen! z too small - outside range: " << z /
cm << std::endl;
236 std::map<std::tuple<float, float, float>, std::tuple<float, float, float> >::const_iterator magval;
238 for (
int i = 0;
i < 2;
i++)
240 for (
int j = 0;
j < 2;
j++)
242 for (
int k = 0;
k < 2;
k++)
249 <<
" value: x: " << xkey[
i] /
cm
250 <<
", y: " << ykey[
j] /
cm
251 <<
", z: " << zkey[
k] /
cm << std::endl;
254 xyz[
i][
j][
k][0] = std::get<0>(magval->first);
255 xyz[
i][
j][
k][1] = std::get<1>(magval->first);
256 xyz[
i][
j][
k][2] = std::get<2>(magval->first);
257 bf[
i][
j][
k][0] = std::get<0>(magval->second);
258 bf[
i][
j][
k][1] = std::get<1>(magval->second);
259 bf[
i][
j][
k][2] = std::get<2>(magval->second);
262 std::cout <<
"read x/y/z: " << xyz[
i][
j][
k][0] /
cm <<
"/"
263 << xyz[
i][
j][
k][1] /
cm <<
"/"
264 << xyz[
i][
j][
k][2] /
cm <<
" bx/by/bz: "
265 << bf[
i][
j][
k][0] / tesla <<
"/"
266 << bf[
i][
j][
k][1] / tesla <<
"/"
267 << bf[
i][
j][
k][2] / tesla << std::endl;
279 double xinblock = point[0] - xkey[1];
280 double yinblock = point[1] - ykey[1];
281 double zinblock = point[2] - zkey[1];
289 std::cout <<
"x/y/z inblock: " << xinblock /
cm <<
"/" << yinblock /
cm <<
"/" << zinblock /
cm << std::endl;
290 std::cout <<
"x/y/z fraction: " << fractionx <<
"/" << fractiony <<
"/" << fractionz << std::endl;
305 for (
int i = 0;
i < 3;
i++)
307 Bfield[
i] =
bf[0][0][0][
i] * fractionx * fractiony * fractionz +
308 bf[1][0][0][
i] * (1. - fractionx) * fractiony * fractionz +
309 bf[0][1][0][
i] * fractionx * (1. - fractiony) * fractionz +
310 bf[0][0][1][
i] * fractionx * fractiony * (1. - fractionz) +
311 bf[1][0][1][
i] * (1. - fractionx) * fractiony * (1. - fractionz) +
312 bf[0][1][1][
i] * fractionx * (1. - fractiony) * (1. - fractionz) +
313 bf[1][1][0][
i] * (1. - fractionx) * (1. - fractiony) * fractionz +
314 bf[1][1][1][
i] * (1. - fractionx) * (1. - fractiony) * (1. - fractionz);