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;
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;
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 sigy.reserve(nptfit);
283 for (
int j = 0;
j < nptfit;
j++)
284 sigy.push_back(sigma);
288 float covss, covii, covsi;
290 fit(xfit, yfit, nptfit, sigy,
b,
a, chi2fit, covss, covii, covsi);
296 chi2fitN2 = chi2fit / (nptfit - nppar);
301 fitNpar(3, xfit, yfit, lfit, tfit, sigy, bminf, aminf, cminf, vminf, chi2fit,
debug);
304 if (cminf != -999.) {
310 chi2fitN3 = (nptfit > nppar) ? chi2fit / (nptfit - nppar) : -1.;
312 float aminf3 = aminf;
313 float bminf3 = bminf;
314 float cminf3 = cminf;
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;
326 fitNpar(4, xfit, yfit, lfit, tfit, sigy, bminf, aminf, cminf, vminf, chi2fit,
debug);
331 chi2fitN4 = (nptfit > nppar) ? chi2fit / (nptfit - nppar) : -1.;
333 if (fabs(vminf) >= 0.29) {
344 if (!vdrift_4parfit) {
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;
365 if (fabs(vminf) >= 0.09 &&
debug) {
373 cout << nptfit <<
" nptfit " 374 <<
" end chi2fit = " << chi2fit <<
"T0_ev ns= " << cminf / 0.00543 <<
"delta v = " << vminf << endl;
378 chi2fit = chi2fit / (nptfit - nppar);
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
static const double slope[3]
~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
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
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
static constexpr float b1