CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CommonTools/Statistics/interface/LinearFit.h

Go to the documentation of this file.
00001 #ifndef LinearFit_H
00002 #define LinearFit_H
00003 
00004 #include <vector>
00005 
00009 class LinearFit {
00010 
00011 public:
00012 
00018   void fit(const std::vector<float> & x, const std::vector<float> & y, int ndat, 
00019            const std::vector<float> & sigy, float& slope, float& intercept, 
00020            float& covss, float& covii, float& covsi) const;
00021 
00022 };
00023 
00024 // template version, no std (error alrady double...)
00025 template<typename T> 
00026 void linearFit( T const  * __restrict__ x, T const  * __restrict__ y, int ndat,
00027                 T const  * __restrict__ sigy2,  
00028                 T & slope, T & intercept,
00029                 T & covss, T & covii, T & covsi) {
00030   T g1 = 0, g2 = 0;
00031   T s11 = 0, s12 = 0, s22 = 0;
00032   for (int i = 0; i != ndat; i++) {
00033     T sy2 = T(1)/sigy2[i];
00034     g1 += y[i] *sy2;
00035     g2 += x[i]*y[i] * sy2;
00036     s11 += sy2;
00037     s12 += x[i] * sy2;
00038     s22 += x[i]*x[i] * sy2;
00039   }
00040   
00041   T d = T(1)/(s11*s22 - s12*s12);
00042   intercept = (g1*s22 - g2*s12) * d;
00043   slope = (g2*s11 - g1*s12) * d;
00044   
00045   covii =  s22 * d;
00046   covss =  s11 * d;
00047   covsi = -s12 * d;
00048 }
00049 
00050 
00051 #endif