13#include "KalFitAlg/coil/Bfield.h"
15#include "CLHEP/Geometry/Point3D.h"
16#include "CLHEP/Matrix/Matrix.h"
17#include "CLHEP/Matrix/SymMatrix.h"
18#include "CLHEP/Matrix/Vector.h"
19#include "CLHEP/Vector/ThreeVector.h"
20#ifndef ENABLE_BACKWARDS_COMPATIBILITY
24using CLHEP::Hep3Vector;
25using CLHEP::HepMatrix;
26using CLHEP::HepSymMatrix;
27using CLHEP::HepVector;
44Bfield* Bfield::_field[200] = { 0 };
47 if ( !_field[imap] ) _field[imap] =
new Bfield( imap );
52 std::cout << std::endl;
53 std::cout <<
"***********************************************" << std::endl;
54 std::cout <<
" Bfield class MAP ID = " << imap << std::endl;
55 std::cout <<
" #### R < 174 cm, -152 < Z < 246 cm #### " << std::endl;
56 std::cout <<
" C++ version 1.00 " << std::endl;
57 std::cout <<
"***********************************************" << std::endl;
59 const double uniformBz[10] = { 0, 15, 14.5, 14, 13, 12.5, 12, 11, 10, 15.5 };
62 for (
int i = 0; i < 175; i++ )
63 for (
int j = 0; j < 399; j++ )
68 if ( i < 101 && j < 163 )
75 for (
int i = 0; i < 17; i++ )
76 for (
int j = 0; j < 51; j++ )
77 for (
int k = 0; k < 52; k++ )
96 m_Bz = uniformBz[imap];
97 m_Bfld.setX( (
double)m_Bx );
98 m_Bfld.setY( (
double)m_By );
99 m_Bfld.setZ( (
double)m_Bz );
100 std::cout <<
"Bfield class >> creating uniform B field with id = " << imap;
101 std::cout <<
", (Bx,By,Bz) = " << m_Bfld << std::endl;
108 std::cout <<
"Bfield class >> loading non-uniform B field map" << std::endl;
117 updateCache( 0., 0., 0. );
119 std::cout << std::endl;
128 if ( x != m_x || y != m_y || z != m_z ) updateCache( x, y, z );
141 if ( x != m_x || y != m_y || z != m_z ) updateCache( x, y, z );
150 double x = position[0];
151 double y = position[1];
152 double z = position[2];
153 if ( x != m_x || y != m_y || z != m_z ) updateCache( x, y, z );
165 if ( x != m_x || y != m_y || z != m_z ) updateCache( x, y, z );
175 if ( x != m_x || y != m_y || z != m_z ) updateCache( x, y, z );
185 if ( x != m_x || y != m_y || z != m_z ) updateCache( x, y, z );
198 if ( x != m_x || y != m_y || z != m_z ) updateCache( x, y, z );
211 if ( x != m_x || y != m_y || z != m_z ) updateCache( x, y, z );
224 if ( x != m_x || y != m_y || z != m_z ) updateCache( x, y, z );
229void Bfield::updateCache(
double x,
double y,
double z )
const {
233 if ( _fieldID <= 10 )
return;
238 double r = (double)sqrt( (
double)x * (
double)x + (
double)y * (
double)y );
239 double phi = (double)atan2( (
double)y, (
double)x );
242 double zmm = z * 10.;
243 double rmm = r * 10.;
245 int tz = (int)( ( zmm + 1520. ) / 10. );
246 int tr = (int)( rmm / 10. );
249 double bz = 0., br = 0., bphi = 0.;
251 if ( zmm >= -1520. && zmm < 2460. && rmm >= 0. && rmm < 1740. )
253 if ( _Bz[
tr][tz] && _Bz[
tr][tz + 1] )
255 double pz = ( zmm + 1520. ) / 10. - (
double)tz;
256 double pr = rmm / 10. - (double)
tr;
259 bz = ( _Bz[
tr][tz] * ( 1. - pz ) + _Bz[
tr][tz + 1] * pz ) * ( 1. - pr ) +
260 ( _Bz[
tr + 1][tz] * ( 1. - pz ) + _Bz[
tr + 1][tz + 1] * pz ) * pr;
262 br = ( _Br[
tr][tz] * ( 1. - pz ) + _Br[
tr][tz + 1] * pz ) * ( 1. - pr ) +
263 ( _Br[
tr + 1][tz] * ( 1. - pz ) + _Br[
tr + 1][tz + 1] * pz ) * pr;
265 bphi = ( _Bphi[
tr][tz] * ( 1. - pz ) + _Bphi[
tr][tz + 1] * pz ) * ( 1. - pr ) +
266 ( _Bphi[
tr + 1][tz] * ( 1. - pz ) + _Bphi[
tr + 1][tz + 1] * pz ) * pr;
268 if ( _fieldID == 21 )
273 if ( zmm >= 800. && zmm < 2420. && rmm < 1000. )
275 int tqz = (int)( ( zmm - 800. ) / 10. );
276 bz += ( ( ( _BzQR[
tr][tqz] * ( 1. - pz ) + _BzQR[
tr][tqz + 1] * pz ) * ( 1. - pr ) +
277 ( _BzQR[
tr + 1][tqz] * ( 1. - pz ) + _BzQR[
tr + 1][tqz + 1] * pz ) * pr ) *
278 (double)
sin( (
double)( 2. * phi + 2. / 180. * (
double)
PI ) ) );
279 br += ( ( ( _BrQR[
tr][tqz] * ( 1. - pz ) + _BrQR[
tr][tqz + 1] * pz ) * ( 1. - pr ) +
280 ( _BrQR[
tr + 1][tqz] * ( 1. - pz ) + _BrQR[
tr + 1][tqz + 1] * pz ) * pr ) *
281 (double)
sin( (
double)( 2. * phi + 2. / 180. * (
double)
PI ) ) );
282 bphi += ( ( ( _BphiQR[
tr][tqz] * ( 1. - pz ) + _BphiQR[
tr][tqz + 1] * pz ) *
284 ( _BphiQR[
tr + 1][tqz] * ( 1. - pz ) + _BphiQR[
tr + 1][tqz + 1] * pz ) *
286 (double)
cos( (
double)( 2. * phi + 2. / 180. * (
double)
PI ) ) );
291 if ( zmm <= -500. && zmm > -1520. && rmm < 1000. )
293 int tqz = (int)( ( -zmm - 500. ) / 20. );
294 int tqr = (int)(
tr / 2. );
295 pz = ( pz + (double)( tz - (int)( tz / 2. ) * 2. ) ) / 2.;
296 pr = ( pr + (double)(
tr - tqr * 2 ) ) / 2.;
305 double phi_tmp = phi;
306 if ( phi_tmp < (
PI / 2. ) && phi_tmp >= ( -
PI / 2. ) )
308 phi_tmp =
PI - phi_tmp;
311 else if ( phi_tmp < -
PI / 2. ) { phi_tmp = 2. *
PI + phi_tmp; }
312 double pphi = ( phi_tmp -
PI / 2. ) / ( 11.25 / 180. *
PI );
313 int tphi = (int)pphi;
314 pphi -= (double)tphi;
315 if ( tphi >= 16 ) tphi -= 16;
318 ( ( ( _BzQL[tphi][tqr][tqz] * ( 1. - pz ) + _BzQL[tphi][tqr][tqz + 1] * pz ) *
320 ( _BzQL[tphi][tqr + 1][tqz] * ( 1. - pz ) +
321 _BzQL[tphi][tqr + 1][tqz + 1] * pz ) *
324 ( ( _BzQL[tphi + 1][tqr][tqz] * ( 1. - pz ) +
325 _BzQL[tphi + 1][tqr][tqz + 1] * pz ) *
327 ( _BzQL[tphi + 1][tqr + 1][tqz] * ( 1. - pz ) +
328 _BzQL[tphi + 1][tqr + 1][tqz + 1] * pz ) *
332 ( ( ( _BrQL[tphi][tqr][tqz] * ( 1. - pz ) + _BrQL[tphi][tqr][tqz + 1] * pz ) *
334 ( _BrQL[tphi][tqr + 1][tqz] * ( 1. - pz ) +
335 _BrQL[tphi][tqr + 1][tqz + 1] * pz ) *
338 ( ( _BrQL[tphi + 1][tqr][tqz] * ( 1. - pz ) +
339 _BrQL[tphi + 1][tqr][tqz + 1] * pz ) *
341 ( _BrQL[tphi + 1][tqr + 1][tqz] * ( 1. - pz ) +
342 _BrQL[tphi + 1][tqr + 1][tqz + 1] * pz ) *
345 bphi +=
f * ( ( ( _BphiQL[tphi][tqr][tqz] * ( 1. - pz ) +
346 _BphiQL[tphi][tqr][tqz + 1] * pz ) *
348 ( _BphiQL[tphi][tqr + 1][tqz] * ( 1. - pz ) +
349 _BphiQL[tphi][tqr + 1][tqz + 1] * pz ) *
352 ( ( _BphiQL[tphi + 1][tqr][tqz] * ( 1. - pz ) +
353 _BphiQL[tphi + 1][tqr][tqz + 1] * pz ) *
355 ( _BphiQL[tphi + 1][tqr + 1][tqz] * ( 1. - pz ) +
356 _BphiQL[tphi + 1][tqr + 1][tqz + 1] * pz ) *
369 else if ( zmm >= -1520. && zmm <= 2460. && rmm <= 0. && rmm <= 1740. )
371 if ( tz == 246 ) tz = tz - 1;
372 if (
tr == 174 )
tr =
tr - 1;
373 double pz = ( zmm + 1520. ) / 10. - (
double)tz;
374 double pr = rmm / 10. - (double)
tr;
375 bz = ( _Bz[
tr][tz] * ( 1. - pz ) + _Bz[
tr][tz + 1] * pz ) * ( 1. - pr ) +
376 ( _Bz[
tr + 1][tz] * ( 1. - pz ) + _Bz[
tr + 1][tz + 1] * pz ) * pr;
378 br = ( _Br[
tr][tz] * ( 1. - pz ) + _Br[
tr][tz + 1] * pz ) * ( 1. - pr ) +
379 ( _Br[
tr + 1][tz] * ( 1. - pz ) + _Br[
tr + 1][tz + 1] * pz ) * pr;
381 bphi = ( _Bphi[
tr][tz] * ( 1. - pz ) + _Bphi[
tr][tz + 1] * pz ) * ( 1. - pr ) +
382 ( _Bphi[
tr + 1][tz] * ( 1. - pz ) + _Bphi[
tr + 1][tz + 1] * pz ) * pr;
392 double Bmag_xy = (double)sqrt( br * br + bphi * bphi );
393 double Bphi_rp = (double)atan2( (
double)bphi, (
double)br );
394 m_Bx = Bmag_xy * (double)
cos( (
double)phi + Bphi_rp ) / 1000.;
395 m_By = Bmag_xy * (double)
sin( (
double)phi + Bphi_rp ) / 1000.;
402 m_Bfld.setX( (
double)m_Bx );
403 m_Bfld.setY( (
double)m_By );
404 m_Bfld.setZ( (
double)m_Bz );
423extern "C" void get_bfield_(
int* imap,
double* pos,
double* field,
int* error ) {
HepGeom::Point3D< double > HepPoint3D
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
double sin(const BesAngle a)
double cos(const BesAngle a)
void get_bfield_(int *imap, double *pos, double *field, int *error)
void init_bfield_(int *imap)
static Bfield * getBfield(int)
returns Bfield object.
Bfield(int)
Constructor, Destructor.
double bz(double x, double y, double z) const
const Hep3Vector & fieldMap(double x, double y, double z) const
returns B field
double by(double x, double y, double z) const
double bx(double x, double y, double z) const
returns an element of B field
static Bfield * getBfield(int)
returns Bfield object.