25 const vector<float>& y,
27 const vector<float>& sigy,
36 float s11 = 0, sxy = 0, sxx = 0;
37 for (
int i = 0;
i != nptfit;
i++) {
38 float sy2 = sigy[
i] * sigy[
i];
40 sxy += x[
i] * y[
i] / sy2;
43 sxx += x[
i] * x[
i] / sy2;
51 intercept = (sy * sxx - sxy *
sx) / delta;
52 slope = (sxy * s11 - sy *
sx) / delta;
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];
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,
76 const bool debug =
false)
const {
77 int nptfit = xfit.size();
95 for (
int j = 0;
j < nptfit;
j++) {
98 sxx += xfit[
j] * xfit[
j];
99 sxy += xfit[
j] * yfit[
j];
101 sl2 += lfit[
j] * lfit[
j];
102 sly += lfit[
j] * yfit[
j];
103 slx += lfit[
j] * xfit[
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];
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;
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;
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)
143 aminf = d4 / b4 - cminf * c4 / b4;
144 bminf = (d1 / a1 - cminf * c1 / a1 - aminf * b1 / a1);
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];
155 double xwire = yfit[
j] - tfit[
j];
162 if ((fabs(yfit[
j] - xwire) >
margin) && (fabs(ycorr - xwire) >
margin) &&
163 ((yfit[
j] - xwire) * (ycorr - xwire) < 0)) {
170 if (fabs(ypred - xwire) > 2.1 +
margin) {
177 if (fabs(ycorr - xwire) > 2.1 +
margin) {
183 if (fabs(chi2fit < 0.0001))
187 }
else if (npar == 4) {
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));
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))) /
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)) /
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))) /
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)) +
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];
234 const vector<float>& yfit,
235 const vector<int>& lfit,
237 const vector<float>& sigy,
242 const bool debug =
false)
const {
244 vector<double> tfit(nptfit, 0.);
246 fitNpar(3, xfit, yfit, lfit, tfit, sigy, aminf, bminf, cminf, vminf, chi2fit,
debug);
250 const vector<float>& yfit,
251 const vector<int>& lfit,
252 const vector<double>& tfit,
259 const bool vdrift_4parfit =
false,
260 const bool debug =
false)
const {
262 cout <<
"Entering Fit4Var" << endl;
274 double chi2fitN2 = -1.;
275 double chi2fit3 = -1.;
276 double chi2fitN3 = -1.;
277 double chi2fitN4 = -1.;
282 for (
int j = 0;
j < nptfit;
j++)
283 sigy.push_back(sigma);
287 float covss, covii, covsi;
289 fit(xfit, yfit, nptfit, sigy, b, a, chi2fit, covss, covii, covsi);
295 chi2fitN2 = chi2fit / (nptfit - nppar);
300 fitNpar(3, xfit, yfit, lfit, tfit, sigy, bminf, aminf, cminf, vminf, chi2fit,
debug);
303 if (cminf != -999.) {
309 chi2fitN3 = (nptfit > nppar) ? chi2fit / (nptfit - nppar) : -1.;
311 float aminf3 = aminf;
312 float bminf3 = bminf;
313 float cminf3 = cminf;
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;
325 fitNpar(4, xfit, yfit, lfit, tfit, sigy, bminf, aminf, cminf, vminf, chi2fit,
debug);
330 chi2fitN4 = (nptfit > nppar) ? chi2fit / (nptfit - nppar) : -1.;
332 if (fabs(vminf) >= 0.29) {
343 if (!vdrift_4parfit) {
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;
364 if (fabs(vminf) >= 0.09 &&
debug) {
372 cout << nptfit <<
" nptfit " 373 <<
" end chi2fit = " << chi2fit <<
"T0_ev ns= " << cminf / 0.00543 <<
"delta v = " << vminf << endl;
377 chi2fit = chi2fit / (nptfit - nppar);
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
static const double slope[3]
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
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
~DTLinearFit()
Destructor.
DTLinearFit()
Constructor.
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