ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fitLinear2.C
Go to the documentation of this file.
1 #include "TLinearFitter.h"
2 #include "TF1.h"
3 #include "TRandom.h"
4 
5 void fitLinear2()
6 {
7  //Fit a 5d hyperplane by n points, using the linear fitter directly
8 
9  //This macro shows some features of the TLinearFitter class
10  //A 5-d hyperplane is fit, a constant term is assumed in the hyperplane
11  //equation (y = a0 + a1*x0 + a2*x1 + a3*x2 + a4*x3 + a5*x4)
12  //Author: Anna Kreshuk
13 
14  Int_t n=100;
15  Int_t i;
16  TRandom rand;
17  TLinearFitter *lf=new TLinearFitter(5);
18 
19  //The predefined "hypN" functions are the fastest to fit
20  lf->SetFormula("hyp5");
21 
22  Double_t *x=new Double_t[n*10*5];
23  Double_t *y=new Double_t[n*10];
24  Double_t *e=new Double_t[n*10];
25 
26  //Create the points and put them into the fitter
27  for (i=0; i<n; i++){
28  x[0 + i*5] = rand.Uniform(-10, 10);
29  x[1 + i*5] = rand.Uniform(-10, 10);
30  x[2 + i*5] = rand.Uniform(-10, 10);
31  x[3 + i*5] = rand.Uniform(-10, 10);
32  x[4 + i*5] = rand.Uniform(-10, 10);
33  e[i] = 0.01;
34  y[i] = 4*x[0+i*5] + x[1+i*5] + 2*x[2+i*5] + 3*x[3+i*5] + 0.2*x[4+i*5] + rand.Gaus()*e[i];
35  }
36 
37  //To avoid copying the data into the fitter, the following function can be used:
38  lf->AssignData(n, 5, x, y, e);
39  //A different way to put the points into the fitter would be to use
40  //the AddPoint function for each point. This way the points are copied and stored
41  //inside the fitter
42 
43  //Perform the fitting and look at the results
44  lf->Eval();
45  TVectorD params;
47  lf->GetParameters(params);
48  lf->GetErrors(errors);
49  for (Int_t i=0; i<6; i++)
50  printf("par[%d]=%f+-%f\n", i, params(i), errors(i));
51  Double_t chisquare=lf->GetChisquare();
52  printf("chisquare=%f\n", chisquare);
53 
54 
55  //Now suppose you want to add some more points and see if the parameters will change
56  for (i=n; i<n*2; i++) {
57  x[0+i*5] = rand.Uniform(-10, 10);
58  x[1+i*5] = rand.Uniform(-10, 10);
59  x[2+i*5] = rand.Uniform(-10, 10);
60  x[3+i*5] = rand.Uniform(-10, 10);
61  x[4+i*5] = rand.Uniform(-10, 10);
62  e[i] = 0.01;
63  y[i] = 4*x[0+i*5] + x[1+i*5] + 2*x[2+i*5] + 3*x[3+i*5] + 0.2*x[4+i*5] + rand.Gaus()*e[i];
64  }
65 
66  //Assign the data the same way as before
67  lf->AssignData(n*2, 5, x, y, e);
68  lf->Eval();
69  lf->GetParameters(params);
70  lf->GetErrors(errors);
71  printf("\nMore Points:\n");
72  for (Int_t i=0; i<6; i++)
73  printf("par[%d]=%f+-%f\n", i, params(i), errors(i));
74  chisquare=lf->GetChisquare();
75  printf("chisquare=%.15f\n", chisquare);
76 
77 
78  //Suppose, you are not satisfied with the result and want to try a different formula
79  //Without a constant:
80  //Since the AssignData() function was used, you don't have to add all points to the fitter again
81  lf->SetFormula("x0++x1++x2++x3++x4");
82 
83  lf->Eval();
84  lf->GetParameters(params);
85  lf->GetErrors(errors);
86  printf("\nWithout Constant\n");
87  for (Int_t i=0; i<5; i++)
88  printf("par[%d]=%f+-%f\n", i, params(i), errors(i));
89  chisquare=lf->GetChisquare();
90  printf("chisquare=%f\n", chisquare);
91 
92  //Now suppose that you want to fix the value of one of the parameters
93  //Let's fix the first parameter at 4:
94  lf->SetFormula("hyp5");
95  lf->FixParameter(1, 4);
96  lf->Eval();
97  lf->GetParameters(params);
98  lf->GetErrors(errors);
99  printf("\nFixed Constant:\n");
100  for (i=0; i<6; i++)
101  printf("par[%d]=%f+-%f\n", i, params(i), errors(i));
102  chisquare=lf->GetChisquare();
103  printf("chisquare=%.15f\n", chisquare);
104 
105  //The fixed parameters can then be released by the ReleaseParameter method
106  delete lf;
107 
108 }
109 
virtual void GetErrors(TVectorD &vpar)
Returns parameter errors.
virtual void FixParameter(Int_t ipar)
Fixes paramter #ipar at its current value.
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
The Linear Fitter - For fitting functions that are LINEAR IN PARAMETERS.
void fitLinear2()
Definition: fitLinear2.C:5
int Int_t
Definition: RtypesCore.h:41
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
virtual Double_t GetChisquare()
Get the Chisquare.
virtual Int_t Eval()
Perform the fit and evaluate the parameters Returns 0 if the fit is ok, 1 if there are errors...
double Double_t
Definition: RtypesCore.h:55
virtual void GetParameters(TVectorD &vpar)
Returns parameter values.
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Double_t y[n]
Definition: legend1.C:17
virtual void AssignData(Int_t npoints, Int_t xncols, Double_t *x, Double_t *y, Double_t *e=0)
This function is to use when you already have all the data in arrays and don't want to copy them into...
virtual void SetFormula(const char *formula)
Additive parts should be separated by "++".
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
std::vector< double > errors
Definition: TwoHistoFit2D.C:33
const Int_t n
Definition: legend1.C:16