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