16 #include <JANA/JParameterManager.h>
47 int main(
int narg,
char *argv[])
51 vector<char*> unused_args;
54 if( !unused_args.empty() ){
55 cout <<
"The following arguments will be passed to DApplication:" << endl;
56 for(
auto a : unused_args) cout <<
" " << a << endl;
65 TFile*
ROOTfile =
new TFile(
"bfield.root",
"RECREATE",
"Produced by bfield2root");
66 ROOTfile->SetCompressionLevel(6);
67 cout<<
"Opened ROOT file \"bfield.root\""<<endl;
73 TTree *tree =
new TTree(
"Bfield",
"Magnetic Field");
74 tree->SetMarkerStyle(8);
75 tree->SetMarkerColor(kBlue);
76 string leaves =
"x/F:y:z:r:phi:Bx:By:Bz:Br:Bphi";
78 leaves +=
":dBxdx:dBxdy:dBxdz";
79 leaves +=
":dBydx:dBydy:dBydz";
80 leaves +=
":dBzdx:dBzdy:dBzdz";
82 tree->Branch(
"B",val,leaves.c_str());
86 for(
int ir=0; ir<
Nr; ir++){
87 if(Nr>1) r =
Rmin + (double)ir*(
Rmax-
Rmin)/(double)(Nr-1);
89 for(
int iphi=0; iphi<
Nphi; iphi++){
92 for(
int iz=0; iz<
Nz; iz++){
93 if(Nz>1) z =
Zmin + (double)iz*(
Zmax-
Zmin)/(double)(Nz-1);
95 double x = r*cos(phi);
96 double y = r*
sin(phi);
99 double dBxdx, dBxdy, dBxdz;
100 double dBydx, dBydy, dBydz;
101 double dBzdx, dBzdy, dBzdz;
106 dBzdx, dBzdy, dBzdz);
108 TVector3 B(Bx, By, Bz);
109 double Br = B.Dot(TVector3(x, y, 0.0))/
sqrt(x*x + y*y);
110 double Bphi = atan2(By, Bx);
142 Bz_vs_r_vs_z->SetXTitle(
"z (cm)");
143 Bz_vs_r_vs_z->SetYTitle(
"r (cm)");
144 Bz_vs_r_vs_z->SetStats(0);
145 TH2D *
Btot_vs_r_vs_z = (TH2D*)Bz_vs_r_vs_z->Clone(
"Btot_vs_r_vs_z");
147 for(
int ibin=1; ibin<=Bz_vs_r_vs_z->GetNbinsX(); ibin++){
148 double z = Bz_vs_r_vs_z->GetXaxis()->GetBinCenter(ibin);
149 for(
int jbin=1; jbin<=Bz_vs_r_vs_z->GetNbinsY(); jbin++){
150 double r = Bz_vs_r_vs_z->GetYaxis()->GetBinCenter(jbin);
153 double dBxdx, dBxdy, dBxdz;
154 double dBydx, dBydy, dBydz;
155 double dBzdx, dBzdy, dBzdz;
160 dBzdx, dBzdy, dBzdz);
161 double Btot =
sqrt(Bx*Bx + By*By + Bz*Bz);
164 TVector3 gradient(dBxdx*Bx/Btot + dBydx*By/Btot + dBzdx*Bz/Btot,
165 dBxdy*Bx/Btot + dBydy*By/Btot + dBzdy*Bz/Btot,
166 dBxdz*Bx/Btot + dBydz*By/Btot + dBzdz*Bz/Btot);
167 double dBtot = gradient.Mag();
169 Bz_vs_r_vs_z->SetBinContent(ibin, jbin, Bz);
170 Btot_vs_r_vs_z->SetBinContent(ibin, jbin, Btot);
171 dBtot_vs_r_vs_z->SetBinContent(ibin, jbin, dBtot);
176 TH2D *Btot_vs_x_vs_z =
new TH2D(
"Btot_vs_x_vs_z",
"", 800, -100.0, 700.0, 400, -200.0, 200.0);
177 Btot_vs_x_vs_z->SetXTitle(
"x (cm)");
178 Btot_vs_x_vs_z->SetYTitle(
"r (cm)");
179 Btot_vs_x_vs_z->SetStats(0);
180 for(
int ibin=1; ibin<=Btot_vs_x_vs_z->GetNbinsX(); ibin++){
181 double z = Btot_vs_x_vs_z->GetXaxis()->GetBinCenter(ibin);
182 for(
int jbin=1; jbin<=Btot_vs_x_vs_z->GetNbinsY(); jbin++){
183 double x = Btot_vs_x_vs_z->GetYaxis()->GetBinCenter(jbin);
186 double dBxdx, dBxdy, dBxdz;
187 double dBydx, dBydy, dBydz;
188 double dBzdx, dBzdy, dBzdz;
193 dBzdx, dBzdy, dBzdz);
194 double Btot =
sqrt(Bx*Bx + By*By + Bz*Bz);
196 Btot_vs_x_vs_z->SetBinContent(ibin, jbin, Btot);
201 TH2D *cos_theta_vs_r_vs_z =
new TH2D(
"cos_theta_vs_r_vs_z",
"", 651, -25, 625.0, 400, 0.0, 200.0);
202 cos_theta_vs_r_vs_z->SetXTitle(
"z (cm)");
203 cos_theta_vs_r_vs_z->SetYTitle(
"r (cm)");
204 cos_theta_vs_r_vs_z->SetStats(0);
205 for(
int ibin=1; ibin<=cos_theta_vs_r_vs_z->GetNbinsX(); ibin++){
206 double z = cos_theta_vs_r_vs_z->GetXaxis()->GetBinCenter(ibin);
207 for(
int jbin=1; jbin<=cos_theta_vs_r_vs_z->GetNbinsY(); jbin++){
208 double r = cos_theta_vs_r_vs_z->GetYaxis()->GetBinCenter(jbin);
212 double Btot =
sqrt(Bx*Bx + By*By + Bz*Bz);
214 cos_theta_vs_r_vs_z->SetBinContent(ibin, jbin, Bz/Btot);
219 TH2D *Btot_vs_r_vs_z_tof =
new TH2D(
"Btot_vs_r_vs_z_tof",
"", 61, 590.0, 650.0, 200, 76.0, 180.0);
220 Btot_vs_r_vs_z_tof->SetXTitle(
"z (cm)");
221 Btot_vs_r_vs_z_tof->SetYTitle(
"r (cm)");
222 Btot_vs_r_vs_z_tof->SetStats(0);
223 TH2D *Bz_vs_r_vs_z_tof = (TH2D*)Btot_vs_r_vs_z_tof->Clone(
"Bz_vs_r_vs_z_tof");
224 TH2D *Br_vs_r_vs_z_tof = (TH2D*)Btot_vs_r_vs_z_tof->Clone(
"Br_vs_r_vs_z_tof");
225 for(
int ibin=1; ibin<=Btot_vs_r_vs_z_tof->GetNbinsX(); ibin++){
226 double z = Btot_vs_r_vs_z_tof->GetXaxis()->GetBinCenter(ibin);
227 for(
int jbin=1; jbin<=Btot_vs_r_vs_z_tof->GetNbinsY(); jbin++){
228 double r = Btot_vs_r_vs_z_tof->GetYaxis()->GetBinCenter(jbin);
231 double dBxdx, dBxdy, dBxdz;
232 double dBydx, dBydy, dBydz;
233 double dBzdx, dBzdy, dBzdz;
238 dBzdx, dBzdy, dBzdz);
239 double Btot =
sqrt(Bx*Bx + By*By + Bz*Bz);
240 double Br =
sqrt(Bx*Bx + By*By);
242 Btot_vs_r_vs_z_tof->SetBinContent(ibin, jbin, Btot);
243 Bz_vs_r_vs_z_tof->SetBinContent(ibin, jbin, Bz);
244 Br_vs_r_vs_z_tof->SetBinContent(ibin, jbin, Br);
250 cout<<endl<<
"Closed ROOT file"<<endl;
261 cout<<
"Usage:"<<endl;
262 cout<<
" bfield2root [options]"<<endl;
264 cout<<
" options:"<<endl;
265 cout<<
" -h, --help Show this Usage statement"<<endl;
266 cout<<
" -R # Set the run number to use when accessing field map"<<endl;
267 cout<<
" -Nr # Set the number of grid points in R"<<endl;
268 cout<<
" -Nphi # Set the number of grid points in Phi"<<endl;
269 cout<<
" -Nz # Set the number of grid points in Z"<<endl;
270 cout<<
" -Rmin # Set the minimum value for R (cm)"<<endl;
271 cout<<
" -Rmax # Set the maximum value for R (cm)"<<endl;
272 cout<<
" -Phimin # Set the minimum value for Phi (radians)"<<endl;
273 cout<<
" -Phimax # Set the maximum value for Phi (radians)"<<endl;
274 cout<<
" -Zmin # Set the minimum value for Z (cm)"<<endl;
275 cout<<
" -Zmax # Set the maximum value for Z (cm)"<<endl;
276 cout<<
" -Z0 # Shift the map's z-coordinate"<<endl;
277 cout<<
" -G Include gradient values in tree"<<endl;
279 cout<<
" The bfield to root program can be used to generate a ROOT TTree of the"<<endl;
280 cout<<
"magnetic field of the Hall-D solenoid. The field is read from the same source"<<endl;
281 cout<<
"as is used by the reconstruction and simulation programs. Since it is a DANA"<<endl;
282 cout<<
"based program, it accepts the same same arguments to manipulate the field as"<<endl;
283 cout<<
"other DANA programs. Namely, -PBFIELD_MAP=XXX and -PBFIELD_TYPE=YYY ."<<endl;
285 cout<<
"For example: -PBFIELD_MAP=Magnets/Solenoid/solenoid_1500"<<endl;
287 cout<<
"The entries in the TTree are evaluated on a grid in cylindrical coordinates"<<endl;
288 cout<<
"that likely does not reflect the points in the underlying map. As such, the"<<endl;
289 cout<<
"values in the tree are a result of the interpolation of the underlying map's"<<endl;
290 cout<<
"points."<<endl;
300 unused_args.push_back(argv[0]);
302 for(
int i=1; i<narg; i++){
304 string next(i<narg-1 ? argv[i+1]:
"");
305 float argf = atof(next.c_str());
306 int argi = atoi(next.c_str());
307 bool used_next =
false;
309 if(arg==
"-h" || arg==
"--help"){
Usage(); exit(0);}
310 else if(arg==
"-R" ){used_next=
true;
RUN_NUMBER = argi;}
311 else if(arg==
"-Nr" ){used_next=
true;
Nr = argi;}
312 else if(arg==
"-Nphi" ){used_next=
true;
Nphi = argi;}
313 else if(arg==
"-Nz" ){used_next=
true;
Nz = argi;}
314 else if(arg==
"-Rmin" ){used_next=
true;
Rmin = argf;}
315 else if(arg==
"-Rmax" ){used_next=
true;
Rmax = argf;}
316 else if(arg==
"-Phimin"){used_next=
true;
Phimin = argf;}
317 else if(arg==
"-Phimax"){used_next=
true;
Phimax = argf;}
318 else if(arg==
"-Zmin" ){used_next=
true;
Zmin = argf;}
319 else if(arg==
"-Zmax" ){used_next=
true;
Zmax = argf;}
320 else if(arg==
"-Z0" ){used_next=
true;
Z0 = argf;}
322 else{ unused_args.push_back(argv[i]); }
330 _DBG_<<
"No argument given for \""<<arg<<
"\" argument!"<<endl;
void dBtot_vs_r_vs_z(void)
virtual void GetFieldAndGradient(double x, double y, double z, double &Bx, double &By, double &Bz, double &dBxdx, double &dBxdy, double &dBxdz, double &dBydx, double &dBydy, double &dBydz, double &dBzdx, double &dBzdy, double &dBzdz) const =0
DMagneticFieldMap * GetBfield(unsigned int run_number=1)
void Btot_vs_r_vs_z(void)
void ParseCommandLineArgs(int narg, char *argv[], vector< char * > &unused_args)
static unsigned int RUN_NUMBER
virtual void GetField(const DVector3 &pos, DVector3 &Bout) const =0
void Usage(JApplication &app)
int main(int argc, char *argv[])