18 jcalib = japp->GetJCalibration(runnumber);
20 JParameterManager *jparms = japp->GetJParameterManager();
21 jparms->SetDefaultParameter(
"BFIELD_MAP", namepath);
23 int Npoints = ReadMap(namepath, runnumber);
25 _DBG_<<
"Error getting JCalibration object for magnetic field!"<<endl;
35 this->jcalib = jcalib;
36 if(ReadMap(namepath)==0){
37 _DBG_<<
"Error getting JCalibration object for magnetic field!"<<endl;
66 jout<<
"Reading Magnetic field map from "<<namepath<<
" ..."<<endl;
67 vector< vector<float> > Bmap;
68 jcalib->Get(namepath, Bmap);
69 jout<<Bmap.size()<<
" entries found (";
79 map<float, int> xvals;
80 map<float, int> yvals;
81 map<float, int> zvals;
84 for(
unsigned int i=0; i<Bmap.size(); i++){
85 vector<float> &a = Bmap[i];
112 jout<<
" ) at 0x"<<hex<<(
unsigned long)
this<<dec<<endl;
115 vector<DBfieldPoint_t> zvec(Nz);
116 vector< vector<DBfieldPoint_t> > yvec;
117 for(
int i=0; i<Ny; i++)yvec.push_back(zvec);
118 for(
int i=0; i<Nx; i++)Btable.push_back(yvec);
121 dx = (xmax-xmin)/(
double)(Nx-1);
122 dy = (
ymax-
ymin)/(
double)((Ny < 2)? 1 : Ny-1);
126 for(
unsigned int i=0; i<Bmap.size(); i++){
127 vector<float> &a = Bmap[i];
128 int xindex = (int)floor((a[0]-xmin+dx/2.0)/dx);
129 int yindex = (int)(Ny<2 ? 0:floor((a[1]-
ymin+dy/2.0)/dy));
130 int zindex = (int)floor((a[2]-
zmin+dz/2.0)/dz);
145 for(
int index_x=0; index_x<Nx; index_x++){
146 int index_x0 = index_x - (index_x>0 ? 1:0);
147 int index_x1 = index_x + (index_x<Nx-1 ? 1:0);
148 for(
int index_z=1; index_z<Nz-1; index_z++){
149 int index_z0 = index_z - (index_z>0 ? 1:0);
150 int index_z1 = index_z + (index_z<Nz-1 ? 1:0);
159 _DBG_<<
"Field map appears to be 3 dimensional. Code is currently"<<endl;
160 _DBG_<<
"unable to handle this. Exiting..."<<endl;
168 double d_index_x=double(index_x1-index_x0);
169 double d_index_z=double(index_z1-index_z0);
179 g->
dBxdx = (Bx1->
Bx - Bx0->
Bx)/d_index_x;
181 g->
dBxdz = (Bz1->
Bx - Bz0->
Bx)/d_index_z;
183 g->
dBydx = (Bx1->
By - Bx0->
By)/d_index_x;
185 g->
dBydz = (Bz1->
By - Bz0->
By)/d_index_z;
187 g->
dBzdx = (Bx1->
Bz - Bx0->
Bz)/d_index_x;
189 g->
dBzdz = (Bz1->
Bz - Bz0->
Bz)/d_index_z;
196 g->
dBxdxdz=(B11->
Bx - B01->
Bx - B10->
Bx + B00->
Bx)/d_index_x/d_index_z;
197 g->
dBzdxdz=(B11->
Bz - B01->
Bz - B10->
Bz + B00->
Bz)/d_index_x/d_index_z;
208 double &Bx_,
double &By_,
211 static const int wt[16][16]=
212 { {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
213 {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
214 {-3, 0, 0, 3, 0, 0, 0, 0,-2, 0, 0,-1, 0, 0, 0, 0},
215 {2, 0, 0,-2, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0},
216 {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
217 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
218 {0, 0, 0, 0,-3, 0, 0, 3, 0, 0, 0, 0,-2, 0, 0,-1},
219 {0, 0, 0, 0, 2, 0, 0,-2, 0, 0, 0, 0, 1, 0, 0, 1},
220 {-3,3, 0, 0,-2,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
221 {0, 0, 0, 0, 0, 0, 0, 0,-3, 3, 0, 0,-2,-1, 0, 0},
222 {9,-9, 9,-9, 6, 3,-3,-6, 6,-6,-3, 3, 4, 2, 1, 2},
223 {-6,6,-6, 6,-4,-2, 2, 4,-3, 3, 3,-3,-2,-1,-1,-2},
224 {2,-2, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
225 {0, 0, 0, 0, 0, 0, 0, 0, 2,-2, 0, 0, 1, 1, 0, 0},
226 {-6, 6,-6,6,-3,-3, 3, 3,-4, 4, 2,-2,-2,-2,-1,-1},
227 {4,-4, 4,-4, 2, 2,-2,-2, 2,-2,-2, 2, 1, 1, 1, 1}};
229 double cl[16],coeff[4][4];
232 double r =
sqrt(x*x + y*y);
233 int index_x = (int)floor((r-xmin)/dx + 0.5);
235 if (index_x>=Nx)
return;
236 else if (index_x<0) index_x=0;
237 int index_z = (int)floor((z-
zmin)/dz + 0.5);
238 if(index_z<0 || index_z>=
Nz)
return;
243 int index_x1 = index_x + (index_x<Nx-1 ? 1:0);
244 int index_z1 = index_z + (index_z<
Nz-1 ? 1:0);
268 temp[12]=B00->dBxdxdz;
275 for (k=0;k<16;k++) tmp2+=wt[i][k]*temp[k];
279 for (j=0;j<4;j++) coeff[i][j]=cl[m++];
281 double t=(z - B00->z)/dz;
282 double u=(r - B00->x)/dx;
285 Br_=t*Br_+((coeff[i][3]*u+coeff[i][2])*u+coeff[i][1])*u+coeff[i][0];
304 temp[12]=B00->dBzdxdz;
311 for (k=0;k<16;k++) tmp2+=wt[i][k]*temp[k];
316 for (j=0;j<4;j++) coeff[i][j]=cl[m++];
320 Bz_=t*Bz_+((coeff[i][3]*u+coeff[i][2])*u+coeff[i][1])*u+coeff[i][0];
324 double cos_theta = x/r;
325 double sin_theta = y/r;
340 double &Bx_,
double &By_,
350 double &dBzdz_)
const{
352 static const int wt[16][16]=
353 { {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
354 {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
355 {-3, 0, 0, 3, 0, 0, 0, 0,-2, 0, 0,-1, 0, 0, 0, 0},
356 {2, 0, 0,-2, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0},
357 {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
358 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
359 {0, 0, 0, 0,-3, 0, 0, 3, 0, 0, 0, 0,-2, 0, 0,-1},
360 {0, 0, 0, 0, 2, 0, 0,-2, 0, 0, 0, 0, 1, 0, 0, 1},
361 {-3,3, 0, 0,-2,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
362 {0, 0, 0, 0, 0, 0, 0, 0,-3, 3, 0, 0,-2,-1, 0, 0},
363 {9,-9, 9,-9, 6, 3,-3,-6, 6,-6,-3, 3, 4, 2, 1, 2},
364 {-6,6,-6, 6,-4,-2, 2, 4,-3, 3, 3,-3,-2,-1,-1,-2},
365 {2,-2, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
366 {0, 0, 0, 0, 0, 0, 0, 0, 2,-2, 0, 0, 1, 1, 0, 0},
367 {-6, 6,-6,6,-3,-3, 3, 3,-4, 4, 2,-2,-2,-2,-1,-1},
368 {4,-4, 4,-4, 2, 2,-2,-2, 2,-2,-2, 2, 1, 1, 1, 1}};
370 double cl[16],coeff[4][4];
373 double r =
sqrt(x*x + y*y);
374 int index_x = (int)floor((r-xmin)/dx + 0.5);
376 if (index_x>=Nx)
return;
377 else if (index_x<0) index_x=0;
378 int index_z = (int)floor((z-
zmin)/dz + 0.5);
379 if(index_z<0 || index_z>=
Nz)
return;
384 int index_x1 = index_x + (index_x<Nx-1 ? 1:0);
385 int index_z1 = index_z + (index_z<
Nz-1 ? 1:0);
409 temp[12]=B00->dBxdxdz;
416 for (k=0;k<16;k++) tmp2+=wt[i][k]*temp[k];
420 for (j=0;j<4;j++) coeff[i][j]=cl[m++];
422 double t=(z - B00->z)/dz;
423 double u=(r - B00->x)/dx;
424 double Br_=0.,dBrdx_=0.,dBrdz_=0.;
426 Br_=t*Br_+((coeff[i][3]*u+coeff[i][2])*u+coeff[i][1])*u+coeff[i][0];
427 dBrdx_=t*dBrdx_+(3.*coeff[i][3]*u+2.*coeff[i][2])*u+coeff[i][1];
428 dBrdz_=u*dBrdz_+(3.*coeff[3][i]*t+2.*coeff[2][i])*t+coeff[1][i];
449 temp[12]=B00->dBzdxdz;
456 for (k=0;k<16;k++) tmp2+=wt[i][k]*temp[k];
461 for (j=0;j<4;j++) coeff[i][j]=cl[m++];
463 Bz_=dBzdx_=dBzdz_=0.;
465 Bz_=t*Bz_+((coeff[i][3]*u+coeff[i][2])*u+coeff[i][1])*u+coeff[i][0];
466 dBzdx_=t*dBzdx_+(3.*coeff[i][3]*u+2.*coeff[i][2])*u+coeff[i][1];
467 dBzdz_=u*dBzdz_+(3.*coeff[3][i]*t+2.*coeff[2][i])*t+coeff[1][i];
473 double cos_theta = x/r;
474 double sin_theta = y/r;
483 dBxdx_ =dBrdx_*cos_theta*cos_theta;
484 dBxdy_ =dBrdx_*cos_theta*sin_theta;
485 dBxdz_ =dBrdz_*cos_theta;
486 dBydx_ = dBrdx_*sin_theta*cos_theta;
487 dBydy_ = dBrdx_*sin_theta*sin_theta;
488 dBydz_ = dBrdz_*sin_theta;
489 dBzdx_ = dBzdx_*cos_theta;
490 dBzdy_ = dBzdx_*sin_theta;
501 double &dBxdx,
double &dBxdy,
503 double &dBydx,
double &dBydy,
505 double &dBzdx,
double &dBzdy,
506 double &dBzdz)
const{
509 double r =
sqrt(x*x + y*y);
510 int index_x = (int)floor((r-xmin)/dx + 0.5);
512 if (index_x>=Nx)
return;
513 else if (index_x<0) index_x=0;
514 int index_z = (int)floor((z-
zmin)/dz + 0.5);
515 if(index_z<0 || index_z>=
Nz)
return;
522 double cos_theta = x/r;
523 double sin_theta = y/r;
530 dBxdx = B->
dBxdx*cos_theta*cos_theta/dx;
531 dBxdy = B->
dBxdx*cos_theta*sin_theta/dx;
532 dBxdz = B->
dBxdz*cos_theta/dz;
533 dBydx = B->
dBxdx*sin_theta*cos_theta/dx;
534 dBydy = B->
dBxdx*sin_theta*sin_theta/dx;
535 dBydz = B->
dBxdz*sin_theta/dz;
536 dBzdx = B->
dBzdx*cos_theta/dx;
537 dBzdy = B->
dBzdx*sin_theta/dx;
561 _DBG_<<
"Field map appears to be 3 dimensional. Code is currently"<<endl;
562 _DBG_<<
"unable to handle this. Treating as phi symmetric using y=0."<<endl;
566 double r =
sqrt(x*x + y*y);
567 int index_x = (int)floor((r-xmin)/dx + 0.5);
569 if (index_x>=Nx)
return;
570 else if (index_x<0) index_x=0;
571 int index_z = (int)floor((z-
zmin)/dz + 0.5);
572 if(index_z<0 || index_z>=
Nz)
return;
579 double ur = (r - B->
x)/dx;
580 double uz = (z - B->
z)/dz;
588 double cos_theta = x/r;
589 double sin_theta = y/r;
612 _DBG_<<
"Field map appears to be 3 dimensional. Code is currently"<<endl;
613 _DBG_<<
"unable to handle this. Treating as phi symmetric using y=0."<<endl;
617 double r = pos.Perp();
619 int index_x = (int)floor((r-xmin)/dx + 0.5);
621 if (index_x>=Nx)
return;
622 else if (index_x<0) index_x=0;
623 int index_z = (int)floor((z-
zmin)/dz + 0.5);
624 if(index_z<0 || index_z>=
Nz)
return;
631 double ur = (r - B->
x)/dx;
632 double uz = (z - B->
z)/dz;
639 double cos_theta = pos.x()/r;
640 double sin_theta = pos.y()/r;
647 Bout.SetXYZ(Br*cos_theta,Br*sin_theta,Bz);
653 double r =
sqrt(x*x + y*y);
656 int index_x = (int)floor((r-xmin)/dx + 0.5);
657 if (index_x>=Nx)
return 0.;
658 else if (index_x<0) index_x=0;
659 int index_z = (int)floor((z-
zmin)/dz + 0.5);
660 if(index_z<0 || index_z>=
Nz)
return 0.;
667 double ur = (r - B->
x)/dx;
668 double uz = (z - B->
z)/dz;
double GetBz(double x, double y, double z) const
void GetField(const DVector3 &pos, DVector3 &Bout) const
DMagneticFieldMapCalibDB(JApplication *japp, int32_t runnumber=1, string namepath="Magnets/Solenoid/solenoid_1500_poisson_20090814_01")
virtual ~DMagneticFieldMapCalibDB()
int ReadMap(string namepath, int32_t runnumber=1, string context="")
void GetFieldGradient(double x, double y, double z, double &dBxdx, double &dBxdy, double &dBxdz, double &dBydx, double &dBydy, double &dBydz, double &dBzdx, double &dBzdy, double &dBzdz) const
void GetFieldBicubic(double x, double y, double z, double &Bx, double &By, double &Bz) const
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