MathUtil.cxx
Go to the documentation of this file.
2 
3 #include <cassert>
4 
5 namespace util
6 {
7  //......................................................................
8  void LinFitUnweighted(const std::vector<double>& x,
9  const std::vector<double>& y,
10  double& m, double& c)
11  {
12  // Before going ahead, make sure we have sensible arrays
13  assert(x.size() == y.size());
14  assert(x.size() >= 2);
15 
16  // Accumulate the sums for the fit
17  double Sx = 0;
18  double Sy = 0;
19  double Sxy = 0;
20  double Sy2 = 0;
21  double Sx2 = 0;
22  const unsigned int I = x.size();
23  for(unsigned int i = 0; i < I; ++i) {
24  Sx += x[i];
25  Sy += y[i];
26  Sx2 += x[i]*x[i];
27  Sxy += x[i]*y[i];
28  Sy2 += y[i]*y[i];
29  }
30  const double d = I*Sx2 - Sx*Sx;
31  m = (I*Sxy - Sx*Sy)/d;
32  c = (Sy*Sx2 - Sx*Sxy)/d;
33  }
34 
35  //......................................................................
36  double LinFit(const std::vector<double>& x,
37  const std::vector<double>& y,
38  const std::vector<double>& w,
39  double& m, double& c)
40  {
41  // Before going ahead, make sure we have sensible arrays
42  assert(x.size() == y.size());
43  assert(x.size() == w.size());
44  assert(x.size() >= 2);
45 
46  // Accumulate the sums for the fit
47  double Sw = 0;
48  double Swx = 0;
49  double Swy = 0;
50  double Swxy = 0;
51  double Swy2 = 0;
52  double Swx2 = 0;
53  for(unsigned int i = 0; i < w.size(); ++i) {
54  Sw += w[i];
55  Swx += w[i]*x[i];
56  Swy += w[i]*y[i];
57  Swx2 += w[i]*x[i]*x[i];
58  Swxy += w[i]*x[i]*y[i];
59  Swy2 += w[i]*y[i]*y[i];
60  }
61  const double d = Sw*Swx2 - Swx*Swx;
62  m = (Sw*Swxy - Swx*Swy)/d;
63  c = (Swy*Swx2 - Swx*Swxy)/d;
64 
65  const double chi2 =
66  Swy2 - 2.0*m*Swxy - 2.0*c*Swy + 2.0*m*c*Swx +
67  c*c*Sw + m*m*Swx2;
68 
69  return chi2;
70  }
71 } // namespace
Filter events based on their run/event numbers.
double chi2()
Float_t d
Definition: plot.C:236
void LinFitUnweighted(const std::vector< double > &x, const std::vector< double > &y, double &m, double &c)
Simplified version of LinFit.
Definition: MathUtil.cxx:8
double LinFit(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &w, double &m, double &c)
Find the best-fit line to a collection of points in 2-D by minimizing the squared vertical distance f...
Definition: MathUtil.cxx:36
assert(nhit_max >=nhit_nbins)
Float_t w
Definition: plot.C:20