CMS 3D CMS Logo

LinearFit.h
Go to the documentation of this file.
1 #ifndef LinearFit_H
2 #define LinearFit_H
3 
4 #include <vector>
5 
9 class LinearFit {
10 public:
16  void fit(const std::vector<float>& x,
17  const std::vector<float>& y,
18  int ndat,
19  const std::vector<float>& sigy,
20  float& slope,
21  float& intercept,
22  float& covss,
23  float& covii,
24  float& covsi) const;
25 };
26 
27 // template version, no std (error alrady double...)
28 template <typename T>
29 void linearFit(T const* __restrict__ x,
30  T const* __restrict__ y,
31  int ndat,
32  T const* __restrict__ sigy2,
33  T& slope,
34  T& intercept,
35  T& covss,
36  T& covii,
37  T& covsi) {
38  T g1 = 0, g2 = 0;
39  T s11 = 0, s12 = 0, s22 = 0;
40  for (int i = 0; i != ndat; i++) {
41  T sy2 = T(1) / sigy2[i];
42  g1 += y[i] * sy2;
43  g2 += x[i] * y[i] * sy2;
44  s11 += sy2;
45  s12 += x[i] * sy2;
46  s22 += x[i] * x[i] * sy2;
47  }
48 
49  T d = T(1) / (s11 * s22 - s12 * s12);
50  intercept = (g1 * s22 - g2 * s12) * d;
51  slope = (g2 * s11 - g1 * s12) * d;
52 
53  covii = s22 * d;
54  covss = s11 * d;
55  covsi = -s12 * d;
56 }
57 
58 #endif
void fit(const std::vector< float > &x, const std::vector< float > &y, int ndat, const std::vector< float > &sigy, float &slope, float &intercept, float &covss, float &covii, float &covsi) const
Definition: LinearFit.cc:3
static const double slope[3]
d
Definition: ztail.py:151
void linearFit(T const *__restrict__ x, T const *__restrict__ y, int ndat, T const *__restrict__ sigy2, T &slope, T &intercept, T &covss, T &covii, T &covsi)
Definition: LinearFit.h:29
long double T