BOSS 7.1.0
BESIII Offline Software System
Loading...
Searching...
No Matches
TrkReco/TrkReco-00-09-02/src/Bfield.cxx
Go to the documentation of this file.
1//
2// Bfield class
3//
4// 27-Mar-1999 : KUNIYA Toshio
5// Enabled QCS compornent
6//
7// 21-Feb-1999 : KUNIYA Toshio
8// Keeping comatibility, Bfield class is modified.
9// No longer fortran common block is used for bfield map.
10// Access functions are prepared for fortran call.
11//
12
13#include <iostream>
14
15//#include "belle.h"
16//#include "coil/Bfield.h"
17#include "TrkReco/Bfield.h"
18
19//#include "CLHEP/Geometry/Point3D.h"
20//#ifndef ENABLE_BACKWARDS_COMPATIBILITY
21// typedef HepGeom::Point3D<double> HepPoint3D;
22//#endif
23
24//#include BELLETDF_H
25
26// prototype of file scoped function to call fortran routine
27// ... read B field map with ID = imap from a file
28/*
29extern "C" {
30 extern void geo_coil_readmap_
31 (int *imap, float (*bz)[399], float (*br)[399], float (*bphi)[399]);
32 extern void geo_coil_readqcsrmap_
33 (float (*bzqr)[163], float (*brqr)[163], float (*bphiqr)[163]);
34 extern void geo_coil_readqcslmap_
35 (float (*bzrl)[51][52], float (*brql)[51][52], float (*bphiqr)[51][52]);
36}
37*/
38
39Bfield *
40Bfield::_field[200] = {0}; // All of 200 elements are initialized.
41
42Bfield *
43Bfield::getBfield(int imap) {
44 if (! _field[imap]) _field[imap] = new Bfield(imap);
45 return _field[imap];
46}
47
48
49Bfield::Bfield(int imap) {
50 std::cout << std::endl;
51 std::cout << "***********************************************" << std::endl;
52 std::cout << " Bfield class MAP ID = " << imap << std::endl;
53 std::cout << " #### R < 174 cm, -152 < Z < 246 cm #### " << std::endl;
54 std::cout << " C++ version 1.00 " << std::endl;
55 std::cout << "***********************************************" << std::endl;
56
57 const float uniformBz[10] = {0, 15, 14.5, 14, 13, 12.5, 12, 11, 10, 15.5};
58
59 //...initialization
60 for(int i=0; i<175; i++)
61 for(int j=0; j<399; j++) {
62 _Bz[i][j] = 0;
63 _Br[i][j] = 0;
64 _Bz[i][j] = 0;
65 if(i<101 && j<163) {
66 _BzQR[i][j] = 0;
67 _BrQR[i][j] = 0;
68 _BphiQR[i][j] = 0;
69 }
70 }
71 for(int i=0; i<17; i++)
72 for(int j=0; j<51; j++)
73 for(int k=0; k<52; k++) {
74 _BzQL[i][j][k] = 0;
75 _BrQL[i][j][k] = 0;
76 _BphiQL[i][j][k] =0;
77 }
78
79 //...
80 _fieldID = imap;
81
82 //...read B field map
83
84 if(imap<10){
85 //
86 // uniform B field map
87 //
88 m_Bx = 0.;
89 m_By = 0.;
90 m_Bz = uniformBz[imap];
91 m_Bfld.setX((double) m_Bx);
92 m_Bfld.setY((double) m_By);
93 m_Bfld.setZ((double) m_Bz);
94 std::cout << "Bfield class >> creating uniform B field with id = " << imap;
95 std::cout << ", (Bx,By,Bz) = "<<m_Bfld<<std::endl;
96 } else {
97 //
98 // non-uniform B field map
99 //
100 /*
101 std::cout << "Bfield class >> loading non-uniform B field map" << std::endl;
102 geo_coil_readmap_(&imap, _Bz, _Br, _Bphi);
103 if( _fieldID == 21 ) {
104 std::cout << "Bfield class >> loading QCS" << std::endl;
105 geo_coil_readqcsrmap_(_BzQR,_BrQR, _BphiQR);
106 geo_coil_readqcslmap_(_BzQL,_BrQL, _BphiQL);
107 }
108 updateCache(0., 0., 0.);
109 */
110 }
111 std::cout << std::endl;
112
113}
114
115//Bfield::~Bfield(){};
116
117const Hep3Vector &
118Bfield::fieldMap(float x, float y, float z) const {
119
120 if(_fieldID > 10){
121 if(x != m_x || y != m_y || z != m_z) updateCache(x, y, z);
122 }
123
124 return m_Bfld;
125}
126
127const Hep3Vector &
128Bfield::fieldMap(const HepPoint3D & xyz) const{
129
130 if(_fieldID > 10){
131 float x = xyz.x();
132 float y = xyz.y();
133 float z = xyz.z();
134 if(x != m_x || y != m_y || z != m_z) updateCache(x, y, z);
135 }
136
137 return m_Bfld;
138}
139
140void
141Bfield::fieldMap(float *position, float *field) {
142 if(_fieldID > 10){
143 float x = position[0];
144 float y = position[1];
145 float z = position[2];
146 if(x != m_x || y != m_y || z != m_z) updateCache(x, y, z);
147 }
148 field[0] = m_Bx;
149 field[1] = m_By;
150 field[2] = m_Bz;
151 return;
152}
153
154float
155Bfield::bx(float x, float y, float z) const {
156
157 if(_fieldID > 10){
158 if(x != m_x || y != m_y || z != m_z) updateCache(x, y, z);
159 }
160
161 return m_Bx;
162}
163
164float
165Bfield::by(float x, float y, float z) const {
166
167 if(_fieldID > 10){
168 if(x != m_x || y != m_y || z != m_z) updateCache(x, y, z);
169 }
170
171 return m_By;
172}
173
174float
175Bfield::bz(float x, float y, float z) const {
176
177 if(_fieldID > 10){
178 if(x != m_x || y != m_y || z != m_z) updateCache(x, y, z);
179 }
180
181 return m_Bz;
182}
183
184float
185Bfield::bx(const HepPoint3D & xyz) const{
186
187 if(_fieldID > 10){
188 float x = xyz.x();
189 float y = xyz.y();
190 float z = xyz.z();
191 if(x != m_x || y != m_y || z != m_z) updateCache(x, y, z);
192 }
193
194 return m_Bx;
195}
196
197float
198Bfield::by(const HepPoint3D & xyz) const{
199
200 if(_fieldID > 10){
201 float x = xyz.x();
202 float y = xyz.y();
203 float z = xyz.z();
204 if(x != m_x || y != m_y || z != m_z) updateCache(x, y, z);
205 }
206
207 return m_By;
208}
209
210float
211Bfield::bz(const HepPoint3D & xyz) const{
212
213 if(_fieldID > 10){
214 float x = xyz.x();
215 float y = xyz.y();
216 float z = xyz.z();
217 if(x != m_x || y != m_y || z != m_z) updateCache(x, y, z);
218 }
219 return m_Bz;
220}
221
222void
223Bfield::updateCache(float x, float y, float z) const{
224
225 // this function is only for non-uniform B field
226
227 if( _fieldID <= 10 ) return;
228
229 float PI = 3.14159;
230
231 //...
232 float r = (float)sqrt((double)x*(double)x + (double)y*(double)y);
233 float phi = (float)atan2((double)y, (double)x);
234
235 //... [cm] --> [mm]
236 float zmm = z * 10.;
237 float rmm = r * 10.;
238 //... make index
239 int tz = (int) (( zmm + 1520.)/10.);
240 int tr = (int) (rmm/10.);
241
242 //...
243 float bz = 0., br = 0., bphi = 0.;
244
245 if(zmm >= -1520. && zmm < 2460. && rmm >= 0. && rmm < 1740.){
246 if(_Bz[tr][tz] && _Bz[tr][tz+1]){
247 float pz = (zmm + 1520.)/10.- (float)tz;
248 float pr = rmm/10.- (float)tr;
249
250 //...
251 bz = (_Bz[tr][tz]*(1.- pz)+_Bz[tr][tz+1]*pz)*(1.-pr)+
252 (_Bz[tr+1][tz]*(1.-pz)+_Bz[tr+1][tz+1]*pz)*pr;
253 //...
254 br = (_Br[tr][tz]*(1.-pz)+_Br[tr][tz+1]*pz)*(1.-pr)+
255 (_Br[tr+1][tz]*(1.-pz)+_Br[tr+1][tz+1]*pz)*pr;
256 //...
257 bphi = (_Bphi[tr][tz]*(1.-pz)+_Bphi[tr][tz+1]*pz)*(1.-pr)+
258 (_Bphi[tr+1][tz]*(1.-pz)+_Bphi[tr+1][tz+1]*pz)*pr;
259
260 if(_fieldID == 21) {
261 //
262 // QCS Right
263 //
264 if( zmm>=800. && zmm < 2420. && rmm < 1000. ) {
265 int tqz = (int)( (zmm-800.)/10. );
266 bz += (((_BzQR[tr][tqz]*(1.-pz)+_BzQR[tr][tqz+1]*pz)*(1.-pr)
267 +(_BzQR[tr+1][tqz]*(1.-pz)+_BzQR[tr+1][tqz+1]*pz)*pr)
268 *(float)sin((double)(2.*phi+2./180.*(double)PI)));
269 br += (((_BrQR[tr][tqz]*(1.-pz)+_BrQR[tr][tqz+1]*pz)*(1.-pr)
270 +(_BrQR[tr+1][tqz]*(1.-pz)+_BrQR[tr+1][tqz+1]*pz)*pr)
271 *(float)sin((double)(2.*phi+2./180.*(double)PI)));
272 bphi += (((_BphiQR[tr][tqz]*(1.-pz)
273 +_BphiQR[tr][tqz+1]*pz)*(1.-pr)
274 +(_BphiQR[tr+1][tqz]*(1.-pz)
275 +_BphiQR[tr+1][tqz+1]*pz)*pr)
276 *(float)cos((double)(2.*phi+2./180.*(double)PI)));
277 }
278 //
279 // QCS Left
280 //
281 if(zmm<=-500. && zmm>-1520. && rmm<1000.) {
282 int tqz = (int)((-zmm-500.)/20.);
283 int tqr = (int)(tr/2.);
284 pz = (pz+(float)( tz-(int)(tz/2.)*2. ))/2.;
285 pr = ( pr + (float)(tr-tqr*2) )/2.;
286 float f = 1.;
287 // if( phi < (PI/2.) && phi >= (-PI/2.) ) {
288 // phi = PI-phi;
289 // f =-1.;
290 // } else if( phi < -PI/2. ) {
291 // phi = 2.*PI+phi;
292 // }
293 // float pphi = ( phi-PI/2. )/(11.25/180.*PI);
294 float phi_tmp = phi;
295 if( phi_tmp < (PI/2.) && phi_tmp >= (-PI/2.) ) {
296 phi_tmp = PI-phi_tmp;
297 f =-1.;
298 } else if( phi_tmp < -PI/2. ) {
299 phi_tmp = 2.*PI+phi_tmp;
300 }
301 float pphi = ( phi_tmp-PI/2. )/(11.25/180.*PI);
302 int tphi = (int)pphi;
303 pphi -= (float)tphi;
304 if (tphi >= 16) tphi -= 16;
305
306 bz += f*
307 (((_BzQL[tphi][tqr][tqz]*(1.-pz)+_BzQL[tphi][tqr][tqz+1]*pz)
308 *(1-pr)+(_BzQL[tphi][tqr+1][tqz]*(1.-pz)
309 +_BzQL[tphi][tqr+1][tqz+1]*pz)*pr)*(1.-pphi)
310 +((_BzQL[tphi+1][tqr][tqz]*(1.-pz)
311 +_BzQL[tphi+1][tqr][tqz+1]*pz)*(1.-pr)
312 +(_BzQL[tphi+1][tqr+1][tqz]*(1.-pz)
313 +_BzQL[tphi+1][tqr+1][tqz+1]*pz)*pr)*pphi);
314 br += f*
315 (((_BrQL[tphi][tqr][tqz]*(1.- pz)
316 +_BrQL[tphi][tqr][tqz+1]*pz)*(1.-pr)
317 +(_BrQL[tphi][tqr+1][tqz]*(1.-pz)
318 +_BrQL[tphi][tqr+1][tqz+1]*pz)*pr)*(1-pphi)
319 +((_BrQL[tphi+1][tqr][tqz]*(1.- pz)
320 +_BrQL[tphi+1][tqr][tqz+1]*pz)*(1.-pr)
321 +(_BrQL[tphi+1][tqr+1][tqz]*(1.-pz)
322 +_BrQL[tphi+1][tqr+1][tqz+1]*pz)*pr)*pphi);
323 bphi += f*
324 (((_BphiQL[tphi][tqr][tqz]*(1.- pz)
325 +_BphiQL[tphi][tqr][tqz+1]*pz)*(1.-pr)
326 +(_BphiQL[tphi][tqr+1][tqz]*(1.-pz)
327 +_BphiQL[tphi][tqr+1][tqz+1]*pz)*pr)*(1.-pphi)
328 +((_BphiQL[tphi+1][tqr][tqz]*(1.- pz)
329 +_BphiQL[tphi+1][tqr][tqz+1]*pz)*(1.-pr)
330 +(_BphiQL[tphi+1][tqr+1][tqz]*(1.-pz)
331 +_BphiQL[tphi+1][tqr+1][tqz+1]*pz)*pr)*pphi);
332 }
333 }
334 } else {
335 bz=0.;
336 br=0.;
337 bphi=0.;
338 }
339 } else if(zmm>=-1520. && zmm<=2460. && rmm<=0. && rmm<=1740.){
340 if(tz == 246) tz=tz-1;
341 if(tr == 174) tr=tr-1;
342 float pz= (zmm + 1520.)/10.- (float)tz;
343 float pr= rmm/10.- (float)tr;
344 bz = (_Bz[tr][tz]*(1.- pz)+_Bz[tr][tz+1]*pz)*(1.-pr)+
345 (_Bz[tr+1][tz]*(1.-pz)+_Bz[tr+1][tz+1]*pz)*pr;
346
347 br = (_Br[tr][tz]*(1.-pz)+_Br[tr][tz+1]*pz)*(1.-pr)+
348 (_Br[tr+1][tz]*(1.-pz)+_Br[tr+1][tz+1]*pz)*pr;
349
350 bphi = (_Bphi[tr][tz]*(1.-pz)+_Bphi[tr][tz+1]*pz)*(1.-pr)+
351 (_Bphi[tr+1][tz]*(1.-pz)+_Bphi[tr+1][tz+1]*pz)*pr;
352 } else {
353 bz =0.;
354 br =0.;
355 bphi =0.;
356 }
357
358
359 //... Set B field
360 float Bmag_xy = (float)sqrt(br*br + bphi*bphi);
361 double Bphi_rp = (float)atan2( (double)bphi, (double)br );
362 m_Bx = Bmag_xy * (float)cos((double)phi + Bphi_rp)/1000.;
363 m_By = Bmag_xy * (float)sin((double)phi + Bphi_rp)/1000.;
364 //m_Bx = br * (float)cos((double)phi)/1000.;
365 //m_By = br * (float)sin((double)phi)/1000.;
366 m_Bz = bz/1000.;
367 m_x = x;
368 m_y = y;
369 m_z = z;
370 m_Bfld.setX((double) m_Bx);
371 m_Bfld.setY((double) m_By);
372 m_Bfld.setZ((double) m_Bz);
373}
374
375/*
376//
377//... Fortran callable access functions to Bfield class.
378//
379
380// initialize bfield map
381extern "C"
382void init_bfield_(int *imap) {
383 // create Bfiled map ID = imap
384 // Bfield *thisMap = Bfield::getBfield(*imap);
385 (void) Bfield::getBfield(*imap);
386 // It is OK even though 'thisMap' losts its scope at here.
387 // Because address of field map is not deleted from memory
388 // due to static linkaged Bfield class.
389 return;
390}
391
392// retrieving B field corresponding to the POSition
393extern "C"
394void get_bfield_(int *imap, float *pos, float *field, int *error) {
395 Bfield *thisMap = Bfield::getBfield(*imap);
396 // std::cout << " > accessing Bfield class from fortran routine." << std::endl;
397 if( thisMap != 0 ) {
398 thisMap->fieldMap(pos,field);
399 *error = 0;
400 }
401 else *error = 1;
402 return;
403}
404*/
405
double sin(const BesAngle a)
Definition: BesAngle.h:210
double cos(const BesAngle a)
Definition: BesAngle.h:213
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
Double_t x[10]
DOUBLE_PRECISION tr[3]
const double PI
Definition: PipiJpsi.cxx:55
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.
double y[1000]