28 const vector<float> &
y,
30 const vector<float> & sigy,
40 float s11 = 0, sxy = 0, sxx = 0;
41 for (
int i = 0;
i != nptfit;
i++) {
42 float sy2 = sigy[
i]*sigy[
i];
44 sxy += x[
i]*y[
i] / sy2;
47 sxx += x[
i]*x[
i] / sy2;
50 float delta = s11*sxx - sx*sx;
54 intercept = (sy*sxx - sxy*sx) / delta;
55 slope = (sxy*s11 - sy*sx) / delta;
61 for (
int j=0;
j<nptfit;
j++){
62 const double ypred = intercept + slope*x[
j];
63 const double dy = (y[
j] - ypred)/sigy[
j];
71 const vector<float>& xfit,
72 const vector<float>& yfit,
73 const vector<int>& lfit,
74 const vector<double>& tfit,
75 const vector<float> & sigy,
81 const bool debug=0)
const {
83 int nptfit=xfit.size();
84 if (nptfit<npar)
return;
100 for (
int j=0;
j<nptfit;
j++){
103 sxx+= xfit[
j]*xfit[
j];
104 sxy+= xfit[
j]*yfit[
j];
106 sl2+= lfit[
j]*lfit[
j];
107 sly+= lfit[
j]*yfit[
j];
108 slx+= lfit[
j]*xfit[
j];
110 st2+= tfit[
j]*tfit[
j];
111 slt+= lfit[
j]*tfit[
j];
112 sltx+= lfit[
j]*tfit[
j]*xfit[
j];
113 slty+= lfit[
j]*tfit[
j]*yfit[
j];
116 const double d1 = sy;
117 const double d2 = sxy;
118 const double d3 = sly;
119 const double c1 = sl;
120 const double c2 = slx;
121 const double c3 = sl2;
122 const double b1 = sx;
123 const double b2 = sxx;
124 const double b3 = slx;
125 const double a1 = nptfit;
126 const double a2 = sx;
127 const double a3 = sl;
129 const double b4 = b2*a1-b1*a2;
130 const double c4 = c2*a1-c1*a2;
131 const double d4 = d2*a1-d1*a2;
132 const double b5 = a1*b3-a3*b1;
133 const double c5 = a1*c3-a3*
c1;
134 const double d5 = a1*d3-d1*a3;
135 const double a6 = slt;
136 const double b6 = sltx;
137 const double c6 = st;
138 const double v6 = st2;
139 const double d6 = slty;
145 if (((c5*b4-c4*b5)*b4*a1)!=0) {
146 cminf = (d5*b4-d4*b5)/(c5*b4-c4*b5);
147 if (fabs(cminf)<0.000001) cminf=0;
148 aminf = d4/b4 -cminf *c4/b4;
149 bminf = (d1/a1 -cminf*c1/a1 -aminf*b1/a1);
152 for (
int j=0;
j<nptfit;
j++){
153 const double ypred = aminf*xfit[
j] + bminf;
154 const double ycorr = yfit[
j]-cminf*lfit[
j];
155 const double dy = (ycorr - ypred)/sigy[
j];
158 if (tfit[
j]==0.)
continue;
159 double xwire = yfit[
j]-tfit[
j];
166 if ((fabs(yfit[
j]-xwire)>
margin) && (fabs(ycorr-xwire)>
margin) && ((yfit[
j]-xwire)*(ycorr-xwire)<0)) {
173 if (fabs(ypred-xwire)>2.1 +
margin) {
180 if (fabs(ycorr-xwire)>2.1 +
margin) {
186 if (fabs(chi2fit<0.0001)) chi2fit=0;
189 }
else if (npar==4) {
190 const double det = (a1*a1*(b2*v6 - b6*b6) - 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)) - a2*(b3*(c6*d6 - v6*d3)
197 + c6*(b6*d3 - c6*d2)) + a3*(b2*(c6*d6 - v6*d3) + b3*(v6*d2 - b6*d6) + b6*(b6*d3 - c6*d2))
198 + a6*(b2*c6*d3 + b3*(b3*d6 - b6*d3 - c6*d2)) - d1*(b2*c6*c6 + b3*(b3*v6 - 2*b6*c6)))/det;
199 aminf = - (a1*a1*(b6*d6 - v6*d2) - a1*(a2*(a6*d6 - v6*d1) - a6*a6*d2 + a6*b6*d1 + b3*(c6*d6 - v6*d3)
200 + c6*(b6*d3 - c6*d2)) + a2*(a3*(c6*d6 - v6*d3) + c6*(a6*d3 - c6*d1)) + a3*a3*(v6*d2 - b6*d6)
201 + a3*(a6*(b3*d6 + b6*d3 - 2*c6*d2) - d1*(b3*v6 - b6*c6)) - a6*b3*(a6*d3 - c6*d1))/det;
202 cminf = -(a1*(b2*(c6*d6 - v6*d3) + b3*(v6*d2 - b6*d6) + b6*(b6*d3 - c6*d2)) + a2*a2*(v6*d3 - c6*d6)
203 + a2*(a3*(b6*d6 - v6*d2) + a6*(b3*d6 - 2*b6*d3 + c6*d2) - d1*(b3*v6 - b6*c6))
204 + a3*(d1*(b2*v6 - b6*b6) - a6*(b2*d6 - b6*d2)) + a6*(a6*(b2*d3 - b3*d2) - d1*(b2*c6 - b3*b6)))/det;
205 vminf = - (a1*a1*(b2*d6 - b6*d2) - a1*(a2*a2*d6 - a2*(a6*d2 + b6*d1) + a6*b2*d1 + b2*c6*d3
206 + b3*(b3*d6 - b6*d3 - c6*d2)) + a2*a2*c6*d3 + a2*(a3*(2*b3*d6 - b6*d3 - c6*d2) - b3*(a6*d3 + c6*d1))
207 + a3*a3*(b6*d2 - b2*d6) + a3*(a6*(b2*d3 - b3*d2) + d1*(b2*c6 - b3*b6)) + a6*b3*b3*d1)/det;
210 for (
int j=0;
j<nptfit;
j++) {
211 const double ypred = aminf*xfit[
j] + bminf;
212 const double dy = (yfit[
j]+vminf*lfit[
j]*tfit[
j]-cminf*lfit[
j] -ypred)/sigy[
j];
221 const vector<float>& yfit,
222 const vector<int>& lfit,
224 const vector<float> & sigy,
229 const bool debug=0)
const {
232 vector <double> tfit( nptfit, 0.);
234 fitNpar(3,xfit,yfit,lfit,tfit,sigy,aminf,bminf,cminf,vminf,chi2fit,
debug);
239 const vector<float>& yfit,
240 const vector<int>& lfit,
241 const vector<double>& tfit,
248 const bool vdrift_4parfit=0,
249 const bool debug=0)
const {
251 if (
debug)
cout <<
"Entering Fit4Var" << endl;
253 const double sigma = 0.0295;
262 double chi2fitN2 = -1. ;
263 double chi2fit3 = -1.;
264 double chi2fitN3 = -1. ;
265 double chi2fitN4 = -1.;
266 float bminf3 = bminf;
267 float aminf3 = aminf;
268 float cminf3 = cminf;
276 for (
int j=0;
j<nptfit;
j++)
277 sigy.push_back(sigma);
281 float covss, covii, covsi;
283 fit(xfit,yfit,nptfit,sigy,b,a,chi2fit,covss,covii,covsi);
289 chi2fitN2 = chi2fit/(nptfit-2);
295 fitNpar(3,xfit,yfit,lfit,tfit,sigy,bminf,aminf,cminf,vminf,chi2fit,
debug);
301 chi2fitN3 = chi2fit /(nptfit-3);
305 chi2fitN3 = chi2fit /(nptfit-2);
314 cout <<
"dt0= 0 : slope 2 = " << b <<
" pos in = " << a <<
" chi2fitN2 = " << chi2fitN2
315 <<
" nppar = " << nppar2 <<
" nptfit = " << nptfit << endl;
316 cout <<
"dt0 = 0 : slope 3 = " << bminf <<
" pos out = " << aminf <<
" chi2fitN3 = "
317 << chi2fitN3 <<
" nppar = " << nppar3 <<
" T0_ev ns = " << cminf/0.00543 << endl;
318 cout <<
" vdrift_4parfit "<< vdrift_4parfit<<endl;
323 fitNpar(4,xfit,yfit,lfit,tfit,sigy,bminf,aminf,cminf,vminf,chi2fit,
debug);
330 chi2fitN4= chi2fit / (nptfit-nppar);
333 if (nptfit <= nppar) chi2fitN4=-1;
334 else chi2fitN4 = chi2fit / (nptfit-nppar);
337 if (fabs(vminf) >= 0.29) {
349 if (!vdrift_4parfit){
363 cout <<
" dt0= 0 : slope 4 = " << bminf <<
" pos out = " << aminf <<
" chi2fitN4 = " << chi2fitN4
364 <<
" nppar= " << nppar4 <<
" T0_ev ns= " << cminf/0.00543 <<
" delta v = " << vminf <<endl;
365 cout << nptfit <<
" nptfit " <<
" end chi2fit = " << chi2fit/ (nptfit-nppar ) <<
" T0_ev ns= " << cminf/0.00543 <<
" delta v = " << vminf <<endl;
368 if ( fabs(vminf) >= 0.09 &&
debug ) {
376 cout << nptfit <<
" nptfit "<<
" end chi2fit = " << chi2fit <<
"T0_ev ns= " << cminf/0.00543 <<
"delta v = "<< vminf <<endl;
379 if (nptfit != nppar) 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