CMS 3D CMS Logo

BFit3D.cc
Go to the documentation of this file.
1 #include <cmath>
2 #ifndef M_PI
3 #define M_PI 3.1415926535897932385
4 #endif
5 
6 #include "BFit3D.h"
7 #include "BFit3D_data.h"
8 
9 using namespace magfieldparam;
10 
11 //_______________________________________________________________________________
12 void BFit3D::SetCoeff_Linear(const double B)
13 {
14  unsigned jj, kk = 1;
15  double w_0, w_1, B_mod = fabs(B);
16  if (B_mod <= B_nom[0]) {
17  w_0 = B / B_nom[0];
18  for (jj = 0; jj < 360; ++jj) {
19  C[jj] = w_0*C0[jj][0];
20  }
21  } else if (B_mod >= B_nom[3]) {
22  w_0 = B / B_nom[3];
23  for (jj = 0; jj < 360; ++jj) {
24  C[jj] = w_0*C0[jj][3];
25  }
26  } else {
27  while (B_nom[kk] < B_mod) ++kk; //Simple linear search
28  w_1 = (B_mod - B_nom[kk-1])/(B_nom[kk] - B_nom[kk-1]);
29  w_0 = 1.0 - w_1;
30  if (B < 0.) {
31  w_0 = -w_0;
32  w_1 = -w_1;
33  }
34  for (jj = 0; jj < 360; ++jj) {
35  C[jj] = w_0*C0[jj][kk-1] + w_1*C0[jj][kk];
36  }
37  }
38 }
39 
40 //_______________________________________________________________________________
41 void BFit3D::SetCoeff_Spline(const double B)
42 {
43  int jc, k0 = 0, k1 = 1;
44  double dB2, dB = fabs(B);
45  if (dB >= B_nom[3]) { //Linear extrapolation for a large field
46  dB -= B_nom[3];
47  for (jc = 0; jc < 360; ++jc) C[jc] = C0[jc][3] + C1[jc][4]*dB;
48  } else {
49  if (dB < B_nom[0]) {
50  dB2 = dB*dB / (3.*B_nom[0]);
51  for (jc = 0; jc < 360; ++jc) C[jc] = (C2[jc][0]*dB2 + C1[jc][0])*dB;
52  } else {
53  while (B_nom[k1] < dB) ++k1; //Simple linear search
54  k0 = k1-1;
55  dB2 = (dB -= B_nom[k0]) / (3.*(B_nom[k1] - B_nom[k0]));
56  if (k1 < 3) { //"Regular" interval
57  for (jc = 0; jc < 360; ++jc)
58  C[jc] = (((C2[jc][k1] - C2[jc][k0])*dB2+C2[jc][k0])*dB + C1[jc][k1])*dB + C0[jc][k0];
59  } else { //The last interval
60  dB2 = (1.- dB2)*dB;
61  for (jc = 0; jc < 360; ++jc)
62  C[jc] = (C2[jc][k0]*dB2 + C1[jc][k1])*dB + C0[jc][k0];
63  }
64  }
65  }
66  if (B < 0) for (jc = 0; jc < 360; ++jc) C[jc] = -C[jc];
67 }
68 
69 //_______________________________________________________________________________
70 void BFit3D::GetField(const double r, const double z, const double phi,
71  double &Br, double &Bz, double &Bphi)
72 {
73 //Return field components in Br, Bz, Bphi. SetField must be called before use.
74 //
75  if (signed_rad || (r >= 0.)) HB->SetPoint(r, z, phi);
76  else HB->SetPoint(-r, z, phi+M_PI);
77  HB->EvalBr();
78  HB->EvalBz();
79  HB->EvalBphi();
80  Br = HB->GetBr (C);
81  Bz = HB->GetBz (C);
82  Bphi = HB->GetBphi(C);
83 }
84 
86 // //
87 // T E S T A R E A //
88 // //
90 
double GetBphi(double *coeff)
double GetBr(double *coeff)
static const double B_nom[4]
Definition: BFit3D.h:14
void GetField(const double r, const double z, const double phi, double &Br, double &Bz, double &Bphi)
Definition: BFit3D.cc:70
static const double C0[360][4]
Definition: BFit3D.h:15
HarmBasis3DCyl * HB
Definition: BFit3D.h:25
void SetCoeff_Linear(const double B)
Definition: BFit3D.cc:12
#define M_PI
Definition: BFit3D.cc:3
static const double C1[360][5]
Definition: BFit3D.h:16
double C[360]
Definition: BFit3D.h:19
void SetPoint(const double r, const double z, const double phi)
void SetCoeff_Spline(const double B)
Definition: BFit3D.cc:41
static const double C2[360][3]
Definition: BFit3D.h:17
double GetBz(double *coeff)