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