source: JEM-EUSO/esaf_lal/tags/v1_r0/esaf/packages/reconstruction/modules/shower/fitting/src/LeastSquaresFit.cc @ 117

Last change on this file since 117 was 117, checked in by moretto, 11 years ago

ESAF version compilable on mac OS

File size: 1.6 KB
Line 
1// ESAF : Euso Simulation and Analysis Framework
2// $Id: LeastSquaresFit.cc 2161 2005-10-05 15:56:52Z naumov $
3// R. Pesce created May, 10 2004
4
5#include "LeastSquaresFit.hh"
6#include "TMath.h"
7
8ClassImp(LeastSquaresFit)
9
10//_____________________________________________________________________________
11LeastSquaresFit::LeastSquaresFit(Int_t numpoints, vector<Double_t> x, vector<Double_t> y, vector<Double_t> sig) {
12    // ctor
13    fNumPoints = numpoints;
14    fPointsX = x;
15    fPointsY = y;
16    fErr = sig;
17    fSlope = 0;
18    fOffset = 0;
19    fSigmaSlope = 0;
20    fSigmaOffset = 0;
21    fChiSquare = 0;
22    Do();
23}
24
25//_____________________________________________________________________________
26LeastSquaresFit::~LeastSquaresFit() {
27    // dtor
28}
29
30//_____________________________________________________________________________
31void LeastSquaresFit::Do() {
32    // least squares fit
33    Double_t ss(0), sx(0), sy(0);
34    for( Int_t i=0; i<fNumPoints; i++ ) {
35        ss += 1./TMath::Power(fErr[i],2);
36        sx += fPointsX[i]/TMath::Power(fErr[i],2);
37        sy += fPointsY[i]/TMath::Power(fErr[i],2);
38    }
39   
40    Double_t sxoss = sx/ss;
41    Double_t dev;
42    Double_t st2(0);
43    for( Int_t i=0; i<fNumPoints; i++ ) {
44        dev = (fPointsX[i] - sxoss)/fErr[i];
45        st2 += dev*dev;
46        fSlope += dev*fPointsY[i]/fErr[i];
47    }
48    fSlope /= st2;
49    fOffset = (sy-sx*fSlope)/ss;
50    fSigmaOffset = TMath::Sqrt((1.+sx*sx/(ss*st2))/ss);
51    fSigmaSlope = TMath::Sqrt(1./st2);
52
53    for( Int_t i=0; i<fNumPoints; i++ ) {
54        fChiSquare += TMath::Power((fPointsY[i]-fOffset-fSlope*fPointsX[i])/fErr[i],2);
55    }
56    fChiSquare /= (fNumPoints-2);
57}
Note: See TracBrowser for help on using the repository browser.