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  sigy.reserve(nptfit);
283  for (int j = 0; j < nptfit; j++)
284  sigy.push_back(sigma);
285 
286  float a = 0.;
287  float b = 0.;
288  float covss, covii, covsi;
289 
290  fit(xfit, yfit, nptfit, sigy, b, a, chi2fit, covss, covii, covsi);
291 
292  bminf = b;
293  aminf = a;
294  nppar = 2;
295  nppar2 = nppar;
296  chi2fitN2 = chi2fit / (nptfit - nppar);
297 
298  // cout << "dt0 = 0 chi2fit = " << chi2fit << " slope = "<<b<<endl;
299 
300  if (nptfit >= 3) {
301  fitNpar(3, xfit, yfit, lfit, tfit, sigy, bminf, aminf, cminf, vminf, chi2fit, debug);
302  chi2fit3 = chi2fit;
303 
304  if (cminf != -999.) {
305  nppar = 3;
306  } else {
307  bminf = b;
308  aminf = a;
309  }
310  chi2fitN3 = (nptfit > nppar) ? chi2fit / (nptfit - nppar) : -1.;
311 
312  float aminf3 = aminf;
313  float bminf3 = bminf;
314  float cminf3 = cminf;
315  nppar3 = nppar;
316 
317  if (debug) {
318  cout << "dt0= 0 : slope 2 = " << b << " pos in = " << a << " chi2fitN2 = " << chi2fitN2 << " nppar = " << nppar2
319  << " nptfit = " << nptfit << endl;
320  cout << "dt0 = 0 : slope 3 = " << bminf << " pos out = " << aminf << " chi2fitN3 = " << chi2fitN3
321  << " nppar = " << nppar3 << " T0_ev ns = " << cminf / 0.00543 << endl;
322  cout << " vdrift_4parfit " << vdrift_4parfit << endl;
323  }
324 
325  if (nptfit >= 5) {
326  fitNpar(4, xfit, yfit, lfit, tfit, sigy, bminf, aminf, cminf, vminf, chi2fit, debug);
327 
328  if (vminf != 0)
329  nppar = 4;
330 
331  chi2fitN4 = (nptfit > nppar) ? chi2fit / (nptfit - nppar) : -1.;
332 
333  if (fabs(vminf) >= 0.29) {
334  // for safety and for code construction..dont accept correction on dv/vdrift greater then 0.09
335  vminf = 0.;
336  aminf = aminf3;
337  bminf = bminf3;
338  cminf = cminf3;
339  nppar = 3;
340  chi2fit = chi2fit3;
341  }
342  }
343 
344  if (!vdrift_4parfit) { //if not required explicitly leave the t0 and track step as at step 3
345  // just update vdrift value vmin for storing in the segments for monitoring
346  aminf = aminf3;
347  bminf = bminf3;
348  cminf = cminf3;
349  nppar = 3;
350  chi2fit = chi2fit3;
351  }
352 
353  nppar4 = nppar;
354 
355  } //end nptfit >=3
356 
357  if (debug) {
358  cout << " dt0= 0 : slope 4 = " << bminf << " pos out = " << aminf << " chi2fitN4 = " << chi2fitN4
359  << " nppar= " << nppar4 << " T0_ev ns= " << cminf / 0.00543 << " delta v = " << vminf << endl;
360  cout << nptfit << " nptfit "
361  << " end chi2fit = " << chi2fit / (nptfit - nppar) << " T0_ev ns= " << cminf / 0.00543
362  << " delta v = " << vminf << endl;
363  }
364 
365  if (fabs(vminf) >= 0.09 && debug) { //checks only vdrift less then 10 % accepted
366  // cout << "vminf gt 0.09 det= " << endl;
367  // cout << "dt0= 0 : slope 4 = "<< bminf << " pos out = " << aminf << " chi2fitN4 = " << chi2fitN4
368  // << " T0_ev ns = " << cminf/0.00543 << " delta v = "<< vminf << endl;
369  // cout << "dt0 = 0 : slope 2 = "<< b << " pos in = " << a <<" chi2fitN2 = " << chi2fitN2
370  // << " nppar = " << nppar-1 << " nptfit = " << nptfit <<endl;
371  // cout << "dt0 = 0 : slope 3 = " << bminf << " pos out = " << aminf << " chi2fitN3 = "
372  // << chi2fitN3 << " T0_ev ns = " << cminf/0.00543 << endl;
373  cout << nptfit << " nptfit "
374  << " end chi2fit = " << chi2fit << "T0_ev ns= " << cminf / 0.00543 << "delta v = " << vminf << endl;
375  }
376 
377  if (nptfit != nppar)
378  chi2fit = chi2fit / (nptfit - nppar);
379 }
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
weight_default_t b3[10]
Definition: b3.h:9
weight_default_t b1[25]
Definition: b1.h:9
weight_default_t b4[1]
Definition: b4.h:9
static const double slope[3]
~DTLinearFit()
Destructor.
Definition: DTLinearFit.cc:21
Double_t margin
DTLinearFit()
Constructor.
Definition: DTLinearFit.cc:18
weight_default_t b2[10]
Definition: b2.h:9
#define debug
Definition: HDRShower.cc:19
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
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
float x
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
static constexpr float d1
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