CMS 3D CMS Logo

DTLinearFit.cc
Go to the documentation of this file.
1 
6 /* This Class Header */
8 
9 /* Collaborating Class Header */
10 
11 /* C++ Headers */
12 #include <cmath>
13 using namespace std;
14 
15 /* ====================================================================== */
16 
19 
22 
23 /* Operations */
24 void DTLinearFit::fit(const vector<float>& x,
25  const vector<float>& y,
26  int nptfit,
27  const vector<float>& sigy,
28  float& slope,
29  float& intercept,
30  double& chi2,
31  float& covss,
32  float& covii,
33  float& covsi) const {
34  chi2 = 0;
35  float sy = 0, sx = 0;
36  float s11 = 0, sxy = 0, sxx = 0;
37  for (int i = 0; i != nptfit; i++) {
38  float sy2 = sigy[i] * sigy[i];
39  sy += y[i] / sy2;
40  sxy += x[i] * y[i] / sy2;
41  s11 += 1. / sy2;
42  sx += x[i] / sy2;
43  sxx += x[i] * x[i] / sy2;
44  }
45 
46  float delta = s11 * sxx - sx * sx;
47 
48  if (delta == 0)
49  return;
50 
51  intercept = (sy * sxx - sxy * sx) / delta;
52  slope = (sxy * s11 - sy * sx) / delta;
53 
54  covii = sxx / delta;
55  covss = s11 / delta;
56  covsi = -sx / delta;
57 
58  for (int j = 0; j < nptfit; j++) {
59  const double ypred = intercept + slope * x[j];
60  const double dy = (y[j] - ypred) / sigy[j];
61  chi2 += dy * dy;
62  }
63 }
64 
65 void DTLinearFit::fitNpar(const int npar,
66  const vector<float>& xfit,
67  const vector<float>& yfit,
68  const vector<int>& lfit,
69  const vector<double>& tfit,
70  const vector<float>& sigy,
71  float& aminf,
72  float& bminf,
73  float& cminf,
74  float& vminf,
75  double& chi2fit,
76  const bool debug = false) const {
77  int nptfit = xfit.size();
78  if (nptfit < npar)
79  return;
80 
81  double sx = 0.;
82  double sxx = 0.;
83  double sy = 0.;
84  double sxy = 0.;
85  double sl = 0.;
86  double sl2 = 0.;
87  double sly = 0.;
88  double slx = 0.;
89  double st = 0.;
90  double st2 = 0.;
91  double slt = 0.;
92  double sltx = 0.;
93  double slty = 0.;
94 
95  for (int j = 0; j < nptfit; j++) {
96  sx += xfit[j];
97  sy += yfit[j];
98  sxx += xfit[j] * xfit[j];
99  sxy += xfit[j] * yfit[j];
100  sl += lfit[j];
101  sl2 += lfit[j] * lfit[j];
102  sly += lfit[j] * yfit[j];
103  slx += lfit[j] * xfit[j];
104  st += tfit[j];
105  st2 += tfit[j] * tfit[j];
106  slt += lfit[j] * tfit[j];
107  sltx += lfit[j] * tfit[j] * xfit[j];
108  slty += lfit[j] * tfit[j] * yfit[j];
109  } //end loop
110 
111  const double d1 = sy;
112  const double d2 = sxy;
113  const double d3 = sly;
114  const double c1 = sl;
115  const double c2 = slx;
116  const double c3 = sl2;
117  const double b1 = sx;
118  const double b2 = sxx;
119  const double b3 = slx;
120  const double a1 = nptfit;
121  const double a2 = sx;
122  const double a3 = sl;
123 
124  const double b4 = b2 * a1 - b1 * a2;
125  const double c4 = c2 * a1 - c1 * a2;
126  const double d4 = d2 * a1 - d1 * a2;
127  const double b5 = a1 * b3 - a3 * b1;
128  const double c5 = a1 * c3 - a3 * c1;
129  const double d5 = a1 * d3 - d1 * a3;
130  const double a6 = slt;
131  const double b6 = sltx;
132  const double c6 = st;
133  const double v6 = st2;
134  const double d6 = slty;
135 
136  chi2fit = -1.;
137 
138  if (npar == 3) {
139  if (((c5 * b4 - c4 * b5) * b4 * a1) != 0) {
140  cminf = (d5 * b4 - d4 * b5) / (c5 * b4 - c4 * b5);
141  if (fabs(cminf) < 0.000001)
142  cminf = 0;
143  aminf = d4 / b4 - cminf * c4 / b4;
144  bminf = (d1 / a1 - cminf * c1 / a1 - aminf * b1 / a1);
145 
146  chi2fit = 0.;
147  for (int j = 0; j < nptfit; j++) {
148  const double ypred = aminf * xfit[j] + bminf;
149  const double ycorr = yfit[j] - cminf * lfit[j];
150  const double dy = (ycorr - ypred) / sigy[j];
151  chi2fit += dy * dy;
152 
153  if (tfit[j] == 0.)
154  continue;
155  double xwire = yfit[j] - tfit[j];
156 
157  // define a small safety margin
158  double margin = 0.1;
159  // cout << " pred: " << ypred << " hit: " << ycorr << "chi2: " << dy*dy << " t0: " << -cminf/0.00543 << endl;
160 
161  // Sanity checks - check that the hit after the fit is still withing the DT cell volume
162  if ((fabs(yfit[j] - xwire) > margin) && (fabs(ycorr - xwire) > margin) &&
163  ((yfit[j] - xwire) * (ycorr - xwire) < 0)) {
164  // cout << " segmentpred: " << ypred << " hit: " << ycorr << " xwire: " << xwire << " yhit: " << yfit[j] << " t0: " << -cminf/0.00543 << endl;
165  // cout << " XXX hit moved across wire!!!" << endl;
166  chi2fit = -1.;
167  return;
168  }
169 
170  if (fabs(ypred - xwire) > 2.1 + margin) {
171  // cout << " segmentpred: " << ypred << " hit: " << ycorr << " xwire: " << xwire << " yhit: " << yfit[j] << " t0: " << -cminf/0.00543 << endl;
172  // cout << " XXX segment outside chamber!!!" << endl;
173  chi2fit = -1.;
174  return;
175  }
176 
177  if (fabs(ycorr - xwire) > 2.1 + margin) {
178  // cout << " segmentpred: " << ypred << " hit: " << ycorr << " xwire: " << xwire << " yhit: " << yfit[j] << " t0: " << -cminf/0.00543 << endl;
179  // cout << " XXX hit outside chamber!!!" << endl;
180  chi2fit = -1.;
181  return;
182  }
183  if (fabs(chi2fit < 0.0001))
184  chi2fit = 0;
185  } //end loop chi2
186  }
187  } else if (npar == 4) {
188  const double det =
189  (a1 * a1 * (b2 * v6 - b6 * b6) -
190  a1 * (a2 * a2 * v6 - 2 * a2 * a6 * b6 + a6 * a6 * b2 + b2 * c6 * c6 + b3 * (b3 * v6 - 2 * b6 * c6)) +
191  a2 * a2 * c6 * c6 + 2 * a2 * (a3 * (b3 * v6 - b6 * c6) - a6 * b3 * c6) + a3 * a3 * (b6 * b6 - b2 * v6) +
192  a6 * (2 * a3 * (b2 * c6 - b3 * b6) + a6 * b3 * b3));
193 
194  if (det != 0) {
195  // computation of a, b, c and v
196  bminf = (a1 * (a2 * (b6 * d6 - v6 * d2) + a6 * (b6 * d2 - b2 * d6) + d1 * (b2 * v6 - b6 * b6)) -
197  a2 * (b3 * (c6 * d6 - v6 * d3) + c6 * (b6 * d3 - c6 * d2)) +
198  a3 * (b2 * (c6 * d6 - v6 * d3) + b3 * (v6 * d2 - b6 * d6) + b6 * (b6 * d3 - c6 * d2)) +
199  a6 * (b2 * c6 * d3 + b3 * (b3 * d6 - b6 * d3 - c6 * d2)) -
200  d1 * (b2 * c6 * c6 + b3 * (b3 * v6 - 2 * b6 * c6))) /
201  det;
202  aminf =
203  -(a1 * a1 * (b6 * d6 - v6 * d2) -
204  a1 * (a2 * (a6 * d6 - v6 * d1) - a6 * a6 * d2 + a6 * b6 * d1 + b3 * (c6 * d6 - v6 * d3) +
205  c6 * (b6 * d3 - c6 * d2)) +
206  a2 * (a3 * (c6 * d6 - v6 * d3) + c6 * (a6 * d3 - c6 * d1)) + a3 * a3 * (v6 * d2 - b6 * d6) +
207  a3 * (a6 * (b3 * d6 + b6 * d3 - 2 * c6 * d2) - d1 * (b3 * v6 - b6 * c6)) - a6 * b3 * (a6 * d3 - c6 * d1)) /
208  det;
209  cminf = -(a1 * (b2 * (c6 * d6 - v6 * d3) + b3 * (v6 * d2 - b6 * d6) + b6 * (b6 * d3 - c6 * d2)) +
210  a2 * a2 * (v6 * d3 - c6 * d6) +
211  a2 * (a3 * (b6 * d6 - v6 * d2) + a6 * (b3 * d6 - 2 * b6 * d3 + c6 * d2) - d1 * (b3 * v6 - b6 * c6)) +
212  a3 * (d1 * (b2 * v6 - b6 * b6) - a6 * (b2 * d6 - b6 * d2)) +
213  a6 * (a6 * (b2 * d3 - b3 * d2) - d1 * (b2 * c6 - b3 * b6))) /
214  det;
215  vminf = -(a1 * a1 * (b2 * d6 - b6 * d2) -
216  a1 * (a2 * a2 * d6 - a2 * (a6 * d2 + b6 * d1) + a6 * b2 * d1 + b2 * c6 * d3 +
217  b3 * (b3 * d6 - b6 * d3 - c6 * d2)) +
218  a2 * a2 * c6 * d3 + a2 * (a3 * (2 * b3 * d6 - b6 * d3 - c6 * d2) - b3 * (a6 * d3 + c6 * d1)) +
219  a3 * a3 * (b6 * d2 - b2 * d6) + a3 * (a6 * (b2 * d3 - b3 * d2) + d1 * (b2 * c6 - b3 * b6)) +
220  a6 * b3 * b3 * d1) /
221  det;
222 
223  chi2fit = 0.;
224  for (int j = 0; j < nptfit; j++) {
225  const double ypred = aminf * xfit[j] + bminf;
226  const double dy = (yfit[j] + vminf * lfit[j] * tfit[j] - cminf * lfit[j] - ypred) / sigy[j];
227  chi2fit += dy * dy;
228  } //end loop chi2
229  }
230  }
231 }
232 
233 void DTLinearFit::fit3par(const vector<float>& xfit,
234  const vector<float>& yfit,
235  const vector<int>& lfit,
236  const int nptfit,
237  const vector<float>& sigy,
238  float& aminf,
239  float& bminf,
240  float& cminf,
241  double& chi2fit,
242  const bool debug = false) const {
243  float vminf;
244  vector<double> tfit(nptfit, 0.);
245 
246  fitNpar(3, xfit, yfit, lfit, tfit, sigy, aminf, bminf, cminf, vminf, chi2fit, debug);
247 }
248 
249 void DTLinearFit::fit4Var(const vector<float>& xfit,
250  const vector<float>& yfit,
251  const vector<int>& lfit,
252  const vector<double>& tfit,
253  const int nptfit,
254  float& aminf,
255  float& bminf,
256  float& cminf,
257  float& vminf,
258  double& chi2fit,
259  const bool vdrift_4parfit = false,
260  const bool debug = false) const {
261  if (debug)
262  cout << "Entering Fit4Var" << endl;
263 
264  const double sigma =
265  0.0295; // FIXME errors can be inserted .just load them/that is the usual TB resolution value for DT chambers
266  vector<float> sigy;
267 
268  aminf = 0.;
269  bminf = 0.;
270  cminf = -999.;
271  vminf = 0.;
272 
273  int nppar = 0;
274  double chi2fitN2 = -1.;
275  double chi2fit3 = -1.;
276  double chi2fitN3 = -1.;
277  double chi2fitN4 = -1.;
278  int nppar2 = 0;
279  int nppar3 = 0;
280  int nppar4 = 0;
281 
282  for (int j = 0; j < nptfit; j++)
283  sigy.push_back(sigma);
284 
285  float a = 0.;
286  float b = 0.;
287  float covss, covii, covsi;
288 
289  fit(xfit, yfit, nptfit, sigy, b, a, chi2fit, covss, covii, covsi);
290 
291  bminf = b;
292  aminf = a;
293  nppar = 2;
294  nppar2 = nppar;
295  chi2fitN2 = chi2fit / (nptfit - nppar);
296 
297  // cout << "dt0 = 0 chi2fit = " << chi2fit << " slope = "<<b<<endl;
298 
299  if (nptfit >= 3) {
300  fitNpar(3, xfit, yfit, lfit, tfit, sigy, bminf, aminf, cminf, vminf, chi2fit, debug);
301  chi2fit3 = chi2fit;
302 
303  if (cminf != -999.) {
304  nppar = 3;
305  } else {
306  bminf = b;
307  aminf = a;
308  }
309  chi2fitN3 = (nptfit > nppar) ? chi2fit / (nptfit - nppar) : -1.;
310 
311  float aminf3 = aminf;
312  float bminf3 = bminf;
313  float cminf3 = cminf;
314  nppar3 = nppar;
315 
316  if (debug) {
317  cout << "dt0= 0 : slope 2 = " << b << " pos in = " << a << " chi2fitN2 = " << chi2fitN2 << " nppar = " << nppar2
318  << " nptfit = " << nptfit << endl;
319  cout << "dt0 = 0 : slope 3 = " << bminf << " pos out = " << aminf << " chi2fitN3 = " << chi2fitN3
320  << " nppar = " << nppar3 << " T0_ev ns = " << cminf / 0.00543 << endl;
321  cout << " vdrift_4parfit " << vdrift_4parfit << endl;
322  }
323 
324  if (nptfit >= 5) {
325  fitNpar(4, xfit, yfit, lfit, tfit, sigy, bminf, aminf, cminf, vminf, chi2fit, debug);
326 
327  if (vminf != 0)
328  nppar = 4;
329 
330  chi2fitN4 = (nptfit > nppar) ? chi2fit / (nptfit - nppar) : -1.;
331 
332  if (fabs(vminf) >= 0.29) {
333  // for safety and for code construction..dont accept correction on dv/vdrift greater then 0.09
334  vminf = 0.;
335  aminf = aminf3;
336  bminf = bminf3;
337  cminf = cminf3;
338  nppar = 3;
339  chi2fit = chi2fit3;
340  }
341  }
342 
343  if (!vdrift_4parfit) { //if not required explicitly leave the t0 and track step as at step 3
344  // just update vdrift value vmin for storing in the segments for monitoring
345  aminf = aminf3;
346  bminf = bminf3;
347  cminf = cminf3;
348  nppar = 3;
349  chi2fit = chi2fit3;
350  }
351 
352  nppar4 = nppar;
353 
354  } //end nptfit >=3
355 
356  if (debug) {
357  cout << " dt0= 0 : slope 4 = " << bminf << " pos out = " << aminf << " chi2fitN4 = " << chi2fitN4
358  << " nppar= " << nppar4 << " T0_ev ns= " << cminf / 0.00543 << " delta v = " << vminf << endl;
359  cout << nptfit << " nptfit "
360  << " end chi2fit = " << chi2fit / (nptfit - nppar) << " T0_ev ns= " << cminf / 0.00543
361  << " delta v = " << vminf << endl;
362  }
363 
364  if (fabs(vminf) >= 0.09 && debug) { //checks only vdrift less then 10 % accepted
365  // cout << "vminf gt 0.09 det= " << endl;
366  // cout << "dt0= 0 : slope 4 = "<< bminf << " pos out = " << aminf << " chi2fitN4 = " << chi2fitN4
367  // << " T0_ev ns = " << cminf/0.00543 << " delta v = "<< vminf << endl;
368  // cout << "dt0 = 0 : slope 2 = "<< b << " pos in = " << a <<" chi2fitN2 = " << chi2fitN2
369  // << " nppar = " << nppar-1 << " nptfit = " << nptfit <<endl;
370  // cout << "dt0 = 0 : slope 3 = " << bminf << " pos out = " << aminf << " chi2fitN3 = "
371  // << chi2fitN3 << " T0_ev ns = " << cminf/0.00543 << endl;
372  cout << nptfit << " nptfit "
373  << " end chi2fit = " << chi2fit << "T0_ev ns= " << cminf / 0.00543 << "delta v = " << vminf << endl;
374  }
375 
376  if (nptfit != nppar)
377  chi2fit = chi2fit / (nptfit - nppar);
378 }
mps_fire.i
i
Definition: mps_fire.py:355
gather_cfg.cout
cout
Definition: gather_cfg.py:144
DTLinearFit::fit4Var
void fit4Var(const std::vector< float > &xfit, const std::vector< float > &yfit, const std::vector< int > &lfit, const std::vector< double > &tfit, const int nptfit, float &aminf, float &bminf, float &cminf, float &vminf, double &chi2fit, const bool vdrift_4parfit, const bool debug) const
Definition: DTLinearFit.cc:249
L1TowerCalibrationProducer_cfi.fit
fit
Definition: L1TowerCalibrationProducer_cfi.py:36
hltPixelTracks_cff.chi2
chi2
Definition: hltPixelTracks_cff.py:25
b2
static constexpr float b2
Definition: L1EGammaCrystalsEmulatorProducer.cc:82
testProducerWithPsetDescEmpty_cfi.a2
a2
Definition: testProducerWithPsetDescEmpty_cfi.py:35
b1
static constexpr float b1
Definition: L1EGammaCrystalsEmulatorProducer.cc:82
debug
#define debug
Definition: HDRShower.cc:19
DTLinearFit::DTLinearFit
DTLinearFit()
Constructor.
Definition: DTLinearFit.cc:18
b
double b
Definition: hdecay.h:118
a
double a
Definition: hdecay.h:119
dumpMFGeometry_cfg.delta
delta
Definition: dumpMFGeometry_cfg.py:25
alignmentValidation.c1
c1
do drawing
Definition: alignmentValidation.py:1025
DTLinearFit::~DTLinearFit
~DTLinearFit()
Destructor.
Definition: DTLinearFit.cc:21
fftjetcommon_cfi.sy
sy
Definition: fftjetcommon_cfi.py:203
DTLinearFit.h
PVValHelper::dy
Definition: PVValidationHelpers.h:49
DTLinearFit::fitNpar
void fitNpar(const int npar, const std::vector< float > &xfit, const std::vector< float > &yfit, const std::vector< int > &lfit, const std::vector< double > &tfit, const std::vector< float > &sigy, float &aminf, float &bminf, float &cminf, float &vminf, double &chi2fit, const bool debug) const
Definition: DTLinearFit.cc:65
DTLinearFit::fit
void fit(const std::vector< float > &x, const std::vector< float > &y, int ndat, const std::vector< float > &sigy, float &slope, float &intercept, double &chi2, float &covss, float &covii, float &covsi) const
Definition: DTLinearFit.cc:24
std
Definition: JetResolutionObject.h:76
testProducerWithPsetDescEmpty_cfi.b3
b3
Definition: testProducerWithPsetDescEmpty_cfi.py:36
DTLinearFit::fit3par
void fit3par(const std::vector< float > &xfit, const std::vector< float > &yfit, const std::vector< int > &lfit, const int nptfit, const std::vector< float > &sigy, float &aminf, float &bminf, float &cminf, double &chi2fit, const bool debug) const
Definition: DTLinearFit.cc:233
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
slope
static const double slope[3]
Definition: CastorTimeSlew.cc:6
d1
static constexpr float d1
Definition: L1EGammaCrystalsEmulatorProducer.cc:84
margin
Double_t margin
Definition: trackSplitPlot.h:45
fftjetcommon_cfi.sx
sx
Definition: fftjetcommon_cfi.py:202