ATLAS Offline Software
BFieldCorFunc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 // 19.06.2006, AUTHOR: OLIVER KORTNER
7 // Modified: 16.01.2008 by O. Kortner, RtSpline allowed as input; faster, but
8 // less accurate implementation of correction funtion can
9 // be requested; bug fix in integral calculation.
10 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11 
13 
15 #include "GaudiKernel/MsgStream.h"
19 #include "cmath"
20 
21 using namespace MuonCalib;
22 
23 //*****************************************************************************
24 
25 //:::::::::::::::::
26 //:: METHOD init ::
27 //:::::::::::::::::
28 void BFieldCorFunc::init(const std::string &quality, const CalibFunc::ParVec &params, const IRtRelation *rt) {
30  // PARAMETERS //
32  m_quality = quality;
33  m_param = params;
34 
36  // CONSISTENCY CHECK //
38  if (m_param.size() != 2) {
39  MsgStream log(Athena::getMessageSvc(), "BFieldCorFunc");
40  log << MSG::ERROR << "Wrong number of parameters!" << endmsg;
41  m_Legendre = nullptr;
42  return;
43  }
44 
46  // VARIABLES //
48  unsigned int nb_points(31); // number of sample points for the integral
49  // in the correction function
50  double step; // r step size
51  double time; // auxiliary time variable
52  BaseFunctionFitter fitter(6); // 6 fit parameters for the integral by
53  // default ("medium quality")
54  LegendrePolynomial legendre;
55 
57  // QUALITY SETTING //
59  if (m_quality == "high") {
60  fitter.set_number_of_coefficients(8);
61  nb_points = 31;
62  m_step_size = 0.02;
63  }
64  if (m_quality == "medium") {
65  fitter.set_number_of_coefficients(8);
66  nb_points = 31;
67  m_step_size = 0.06;
68  }
69  if (m_quality == "low") {
70  fitter.set_number_of_coefficients(8);
71  nb_points = 31;
72  m_step_size = 0.12;
73  }
74  // sample points for the integral factor in the correction function
75  std::vector<SamplePoint> sample_points(nb_points);
76 
78  // CALCULATE THE INTEGRAL PART OF THE CORRECTION FUNCTION //
80  m_t_min = (rt)->tLower();
81  m_t_max = (rt)->tUpper();
82 
83  // minimum and maximum radius //
84  m_r_min = 0.025 * CLHEP::mm; // minimum radius
85  m_r_max = rt->radius(m_t_max); // maximum radius
86  if (m_r_max > 17.0 || m_r_max < m_r_min) {
87  MsgStream log(Athena::getMessageSvc(), "BFieldCorFunc");
88  log << MSG::INFO << "UNPHYSICAL MAXIMUM DRIFT RADIUS OF " << m_r_max << ", WILL BE SET TO 17.0!" << endmsg;
89  m_r_max = 17.0;
90  }
91  step = ((m_r_max - m_r_min) / static_cast<double>(nb_points - 1));
92 
93  // set the sample points //
94  double prev_r = 0;
95  double prev_integral = 0;
96  for (unsigned int k = 0; k < nb_points; k++) {
97  time = t_from_r(m_r_min + k * step, rt);
98  sample_points[k].set_x1(2 * (time - 0.5 * (m_t_min + m_t_max)) / (m_t_max - m_t_min));
99  double new_r = rt->radius(time);
100  double new_integral = 1.0e9 * integral(prev_r, new_r, rt) + prev_integral;
101  sample_points[k].set_x2(new_integral);
102  sample_points[k].set_error(1.0);
103  prev_r = new_r;
104  prev_integral = new_integral;
105  }
106 
107  // perform the fit //
108  if (fitter.fit_parameters(sample_points, 1, nb_points, &legendre)) {
109  MsgStream log(Athena::getMessageSvc(), "BFieldCorFunc");
110  log << MSG::WARNING << "Unable to fit the integral in the correction!" << endmsg;
111  m_Legendre = nullptr;
112  return;
113  }
114  m_alpha = fitter.coefficients();
115 
117  // SET A POINTER TO THE LEGENDRE POLYNOMIAL //
120 
121  return;
122 } // end BFieldCorFunc::init
123 
124 //*****************************************************************************
125 
126 //:::::::::::::::::::::
127 //:: METHOD t_from_r ::
128 //:::::::::::::::::::::
129 double BFieldCorFunc::t_from_r(const double &r, const IRtRelation *rt) const {
131  // VARIABLES //
133  double precision(0.010); // spatial precision of the inversion
134  double t_max(m_t_max); // upper time search limit
135  double t_min(m_t_min); // lower time search limit
136  double r_max(m_r_max); // upper radius search limit
137  double r_min(m_r_min); // lower radius search limit
139  // SEARCH FOR THE CORRESPONDING DRIFT TIME //
141  double t_guess, r_guess;
142 
143  do {
144  t_guess = t_min + (t_max - t_min) / (r_max - r_min) * (r - r_min);
145  r_guess = rt->radius(t_guess);
146  if (r_guess > r) {
147  r_max = r_guess;
148  t_max = t_guess;
149  } else {
150  r_min = r_guess;
151  t_min = t_guess;
152  }
153  } while (t_max - t_min > 0.1 && std::abs(r_guess - r) > precision);
154  return t_guess;
155 } // end BFieldCorFunc::t_from_r
156 
157 //*****************************************************************************
158 
159 //:::::::::::::::::::::
160 //:: METHOD integral ::
161 //:::::::::::::::::::::
162 double BFieldCorFunc::integral(const double &r_min, const double &r_max, const IRtRelation *rt) const {
163  // catch fp exceptions//
164  if (m_r_min < 1e-10 || r_min < m_r_min) return 0.0;
165 
167  // VARIABLES //
169  double E0(m_param[0] / std::log(m_r_max / m_r_min)); // E(r)=E0/r
170  double radius(r_max), rp(r_min); // auxiliary radius variables
171  double integ(0.0); // current value of the integral
172  // double step(0.010); // integration step size [mm]
173  double step(m_step_size); // integration step size [mm]
174  double time; // drift time
175 
177  // r IN [m_r_min, m_r_max]? //
179  if (r_max < r_min) { return 0.0; }
180  if (r_max > m_r_max) { radius = m_r_max; }
181 
183  // INTEGRATION //
185  double delta = step;
186  while (rp < radius) {
187  time = t_from_r(rp, rt);
188  if (rp + step > radius) delta = radius - rp;
189  integ = integ + 1.0e-3 * delta * std::pow(std::abs(rt->driftvelocity(time)) * 1.0e6, 1.0 - m_param[1]) /
190  std::pow(E0 / (rp * 1.0e-3), 2.0 - m_param[1]);
191  rp = rp + step;
192  }
193 
194  return integ;
195 } // end BFieldCorFunc::integral
196 
197 //*****************************************************************************
198 
199 //::::::::::::::::::::
200 //:: METHOD epsilon ::
201 //::::::::::::::::::::
202 double BFieldCorFunc::epsilon(void) const { return m_param[1]; }
203 
204 //*****************************************************************************
205 
206 //:::::::::::::::::::::::
207 //:: METHOD setEpsilon ::
208 //:::::::::::::::::::::::
209 /*void BFieldCorFunc::setEpsilon(const double & eps) {
210  m_param[1] = eps;
211  init(m_quality, m_param);
212  return;
213 }*/
214 
215 //*****************************************************************************
216 
217 //::::::::::::::::::::::::::::::
218 //:: METHOD setRtRelationship ::
219 //::::::::::::::::::::::::::::::
221  init(m_quality, m_param, &rt);
222  return;
223 }
224 
225 //*****************************************************************************
226 
227 //:::::::::::::::::
228 //:: METHOD name ::
229 //:::::::::::::::::
230 std::string BFieldCorFunc::name() const { return std::string("BFieldCorFunc"); }
231 
232 //*****************************************************************************
233 
234 //:::::::::::::::::::::::
235 //:: METHOD correction ::
236 //:::::::::::::::::::::::
237 double BFieldCorFunc::correction(double t, double B_wire, double B_mu) const {
238  if (m_Legendre == nullptr) { return 0.0; }
239 
241  // VARIABLES //
243  double B_perp(std::hypot(B_wire, B_mu)); // B orthogonal to the
244  // electron drift path
245  double B_factor(std::pow(B_perp, 2.0 - m_param[1]));
246  double precision(0.1); // precision of the correction in ns
247  double t_max(t); // upper time search limit
248  double t_min(t - 2 * correction_to_B(t, B_wire, B_mu, B_factor)); // lower time search limit
249  if (t_min < m_t_min) t_min = m_t_min;
250  double time(t); // auxiliary time variable
251  double integ(0.0); // integral
252  double tmean(0.5 * (m_t_min + m_t_max)); // mean time
253  double tlength(m_t_max - m_t_min); // length of drift-time interval
254 
256  // DRIFT TIME CHECK //
258  if (t <= m_t_min) { return 0.0; }
259  if (t > m_t_max) {
260  t_max = m_t_max;
261  time = m_t_max;
262  }
263 
265  // SEARCH FOR THE CORRECTED DRIFT TIME //
267  while (t_max - t_min > precision) {
268  integ = 0.0;
269  for (int k = 0; k < m_alpha.rows(); k++) {
270  integ = integ + m_alpha[k] * m_Legendre->value(k, 2 * (0.5 * (t_min + t_max) - tmean) / tlength);
271  }
272  if (0.5 * (t_min + t_max) + B_factor * integ > time) {
273  t_max = 0.5 * (t_min + t_max);
274  } else {
275  t_min = 0.5 * (t_min + t_max);
276  }
277  }
278 
279  return B_factor * integ;
280 } // end BFieldCorFunc::correction
281 
282 //*****************************************************************************
283 
284 //::::::::::::::::::::::::::::
285 //:: METHOD correction_to_B ::
286 //::::::::::::::::::::::::::::
287 double BFieldCorFunc::correction_to_B(double t, double B_wire, double B_mu, double B_factor) const {
288  if (m_Legendre == nullptr) { return 0.0; }
290  // VARIABLES //
292  if (B_factor < 0) {
293  double B_perp(std::hypot(B_wire, B_mu));
294  // B orthogonal to the electron drift path
295  B_factor = std::pow(B_perp, 2.0 - m_param[1]);
296  }
297  double time(t);
298  double integ(0.0); // integral
299  double tmean(0.5 * (m_t_min + m_t_max)); // mean time
300  double tlength(m_t_max - m_t_min); // length of drift-time interval
301 
303  // DRIFT TIME CHECK //
305  if (t <= m_t_min) { return 0.0; }
306  if (t > m_t_max) { time = m_t_max; }
307 
309  // CALCULATE THE CORRECTION //
311  integ = 0.0;
312  for (int k = 0; k < m_alpha.rows(); k++) { integ = integ + m_alpha[k] * m_Legendre->value(k, 2 * (time - tmean) / tlength); }
313 
314  return B_factor * integ;
315 } // end BFieldCorFunc::correction_to_B
MuonCalib::BFieldCorFunc::m_r_max
double m_r_max
Definition: BFieldCorFunc.h:139
beamspotman.r
def r
Definition: beamspotman.py:676
LArSamples::FitterData::fitter
const ShapeFitter * fitter
Definition: ShapeFitter.cxx:23
MuonCalib::BFieldCorFunc::correction
double correction(double t, double B_wire, double B_mu) const
get t(r, !=0)-t(r, =0); t = drift time t [ns] for B=0; B_wire = magnetic field parallel to the anode ...
Definition: BFieldCorFunc.cxx:237
MuonCalib::BFieldCorFunc::m_t_max
double m_t_max
Definition: BFieldCorFunc.h:137
MuonCalib::BFieldCorFunc::m_t_min
double m_t_min
Definition: BFieldCorFunc.h:137
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
MuonCalib::BFieldCorFunc::init
void init(const std::string &quality, const CalibFunc::ParVec &params, const IRtRelation *rt)
Definition: BFieldCorFunc.cxx:28
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
MuonCalib::Legendre_polynomial::get_Legendre_polynomial
static const Legendre_polynomial * get_Legendre_polynomial(void)
get a pointer to the Legendre polynomial
Definition: Legendre_polynomial.cxx:31
MuonCalib::BFieldCorFunc::correction_to_B
double correction_to_B(double t, double B_wire, double B_mu, double B_factor=-1.0) const
Definition: BFieldCorFunc.cxx:287
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
BFieldCorFunc.h
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
MuonCalib::BFieldCorFunc::integral
double integral(const double &r_min, const double &r_max, const IRtRelation *rt) const
Definition: BFieldCorFunc.cxx:162
MuonCalib::BFieldCorFunc::name
std::string name() const
get the class name
Definition: BFieldCorFunc.cxx:230
MuonCalib::BaseFunctionFitter
Definition: BaseFunctionFitter.h:47
MuonCalib::CalibFunc::ParVec
std::vector< double > ParVec
Definition: CalibFunc.h:36
MuonCalib::IRtRelation::driftvelocity
virtual double driftvelocity(double t) const =0
MuonCalib::BFieldCorFunc::epsilon
double epsilon() const
< get the parameter of the B-field correction function
Definition: BFieldCorFunc.cxx:202
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
MuonCalib::Legendre_polynomial::value
double value(const int &order, const double &x) const
get the value of the Legendre polynomial of order m_order at x
MuonCalib::LegendrePolynomial
Definition: LegendrePolynomial.h:39
MuonCalib
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.
Definition: CscCalcPed.cxx:22
MuonCalib::BFieldCorFunc::t_from_r
double t_from_r(const double &r, const IRtRelation *rt) const
Definition: BFieldCorFunc.cxx:129
MuonCalib::BFieldCorFunc::m_alpha
Amg::VectorX m_alpha
Definition: BFieldCorFunc.h:128
BaseFunctionFitter.h
MuonCalib::IRtRelation::radius
virtual double radius(double t) const =0
returns drift radius for a given time
MuonCalib::BFieldCorFunc::m_step_size
double m_step_size
Definition: BFieldCorFunc.h:125
ParticleGun_SamplingFraction.radius
radius
Definition: ParticleGun_SamplingFraction.py:96
python.SystemOfUnits.mm
int mm
Definition: SystemOfUnits.py:83
MuonCalib::BFieldCorFunc::m_r_min
double m_r_min
Definition: BFieldCorFunc.h:139
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
MuonCalib::BFieldCorFunc::m_Legendre
const Legendre_polynomial * m_Legendre
Definition: BFieldCorFunc.h:132
IRtRelation.h
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArCellBinning.step
step
Definition: LArCellBinning.py:158
MuonCalib::BFieldCorFunc::m_param
std::vector< double > m_param
Definition: BFieldCorFunc.h:121
MuonCalib::BFieldCorFunc::setRtRelationship
void setRtRelationship(const IRtRelation &rt)
< set the parameter of the B-field correction function = eps
Definition: BFieldCorFunc.cxx:220
MuonCalib::BFieldCorFunc::m_quality
std::string m_quality
Definition: BFieldCorFunc.h:124
PowhegControl_ttFCNC_NLO.params
params
Definition: PowhegControl_ttFCNC_NLO.py:226
rp
ReadCards * rp
Definition: IReadCards.cxx:26
LegendrePolynomial.h
MuonCalib::IRtRelation
generic interface for a rt-relation
Definition: IRtRelation.h:14
fitman.k
k
Definition: fitman.py:528