ROOT
Version v6.32
master
v6.34
v6.30
v6.28
v6.26
v6.24
v6.22
v6.20
v6.18
v6.16
v6.14
v6.12
v6.10
v6.08
v6.06
Reference Guide
►
ROOT
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Modules
Pages
Loading...
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
19
void
fitLinear2
()
20
{
21
int
n
=100;
22
int
i;
23
TRandom
randNum
;
24
TLinearFitter
*
lf
=
new
TLinearFitter
(5);
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
e
#define e(i)
Definition
RSha256.hxx:103
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
TF1.h
TLinearFitter.h
TRandom.h
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
TLinearFitter
Definition
TLinearFitter.h:153
TRandom
This is the base class for the ROOT Random number generators.
Definition
TRandom.h:27
TVectorT< Double_t >
y
Double_t y[n]
Definition
legend1.C:17
x
Double_t x[n]
Definition
legend1.C:17
n
const Int_t n
Definition
legend1.C:16
tutorials
fit
fitLinear2.C
ROOT v6-32 - Reference Guide Generated on Sun Apr 6 2025 05:41:35 (GVA Time) using Doxygen 1.10.0