ROOT
git-r3/HEAD
Reference Guide
Loading...
Searching...
No Matches
graph2dfit.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_fit
3
/// \notebook
4
/// Fitting a TGraph2D
5
///
6
/// \macro_image
7
/// \macro_output
8
/// \macro_code
9
///
10
/// \author Olivier Couet
11
12
#include <
TMath.h
>
13
#include <
TGraph2D.h
>
14
#include <
TRandom.h
>
15
#include <
TStyle.h
>
16
#include <
TCanvas.h
>
17
#include <
TF2.h
>
18
#include <
TH1.h
>
19
20
void
graph2dfit()
21
{
22
gStyle
->SetOptStat(0);
23
gStyle
->SetOptFit();
24
25
auto
c
=
new
TCanvas
(
"c"
,
"Graph2D example"
,0,0,600,800);
26
c
->Divide(2,3);
27
28
double
rnd,
x
,
y
, z;
29
double
e
= 0.3;
30
int
nd = 400;
31
int
np = 10000;
32
33
TRandom
r
;
34
double
fl = 6;
35
auto
f2 =
new
TF2
(
"f2"
,
"1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200"
,
36
-fl,fl,-fl,fl);
37
f2->SetParameters(1,1);
38
auto
dt =
new
TGraph2D
();
39
40
// Fill the 2D graph
41
double
zmax = 0;
42
for
(
int
N
=0;
N
<nd;
N
++) {
43
f2->GetRandom2(
x
,
y
);
44
// Generate a random number in [-e,e]
45
rnd = 2*
r
.Rndm()*
e
-
e
;
46
z = f2->Eval(
x
,
y)*(1+rnd);
47
if
(z>zmax) zmax = z;
48
dt->SetPoint(
N
,
x
,
y
,z);
49
}
50
51
double
hr = 350;
52
auto
h1
=
new
TH1D
(
"h1"
,
53
"#splitline{Difference between Original}{#splitline{function and Function}{with noise}}"
,
54
100, -hr, hr);
55
auto
h2 =
new
TH1D
(
"h2"
,
56
"#splitline{Difference between Original}{#splitline{function and Delaunay triangles}{interpolation}}"
,
57
100, -hr, hr);
58
auto
h3 =
new
TH1D
(
"h3"
,
59
"#splitline{Difference between Original}{function and Minuit fit}"
,
60
500, -hr, hr);
61
62
f2->SetParameters(0.5,1.5);
63
dt->Fit(f2);
64
auto
fit2 = (
TF2
*)dt->FindObject(
"f2"
);
65
66
f2->SetParameters(1,1);
67
68
for
(
int
N
=0;
N
<np;
N
++) {
69
f2->GetRandom2(
x
,
y
);
70
// Generate a random number in [-e,e]
71
rnd = 2*
r
.Rndm()*
e
-
e
;
72
z = f2->Eval(
x
,
y)*(1+rnd);
73
h1
->Fill(f2->Eval(
x
,
y
)-z);
74
z = dt->Interpolate(
x
,
y);
75
h2->Fill(f2->Eval(
x
,
y
)-z);
76
z = fit2->Eval(
x
,
y);
77
h3->Fill(f2->Eval(
x
,
y
)-z);
78
}
79
80
c
->cd(1);
81
f2->SetTitle(
"Original function with Graph2D points on top"
);
82
f2->SetMaximum(zmax);
83
gStyle
->SetHistTopMargin(0);
84
f2->Draw(
"surf1"
);
85
dt->Draw(
"same p0"
);
86
87
c
->cd(3);
88
dt->SetMargin(0.1);
89
dt->SetFillColor(36);
90
dt->SetTitle(
"Histogram produced with Delaunay interpolation"
);
91
dt->Draw(
"surf4"
);
92
93
c
->cd(5);
94
fit2->SetTitle(
"Minuit fit result on the Graph2D points"
);
95
fit2->Draw(
"surf1"
);
96
97
h1
->SetFillColor(47);
98
h2->SetFillColor(38);
99
h3->SetFillColor(29);
100
101
c
->cd(2);
h1
->Fit(
"gaus"
,
"Q"
) ;
h1
->Draw();
102
c
->cd(4); h2->Fit(
"gaus"
,
"Q"
) ; h2->Draw();
103
c
->cd(6); h3->Fit(
"gaus"
,
"Q"
) ; h3->Draw();
104
c
->cd();
105
}
r
ROOT::R::TRInterface & r
Definition
Object.C:4
c
#define c(i)
Definition
RSha256.hxx:101
e
#define e(i)
Definition
RSha256.hxx:103
TCanvas.h
TF2.h
N
#define N
TGraph2D.h
TH1.h
TMath.h
TRandom.h
TStyle.h
gStyle
externTStyle * gStyle
Definition
TStyle.h:442
TCanvas
The Canvas class.
Definition
TCanvas.h:23
TF2
Definition
TF2.h:29
TGraph2D
Definition
TGraph2D.h:41
TH1D
1-D histogram with a double per channel (see TH1 documentation)
Definition
TH1.h:926
TRandom
This is the base class for the ROOT Random number generators.
Definition
TRandom.h:27
y
Double_t y[n]
Definition
legend1.C:17
x
Double_t x[n]
Definition
legend1.C:17
h1
TH1F * h1
Definition
legend1.C:5
hist015_TH1_read_and_draw_uhi.z
z
Definition
hist015_TH1_read_and_draw_uhi.py:56
tutorials
math
fit
graph2dfit.C
ROOTgit-r3/HEAD - Reference Guide Generated on
(GVA Time) using Doxygen 1.16.1