Logo ROOT   6.10/09
Reference Guide
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 TCanvas* graph2dfit()
21 {
22  gStyle->SetOptStat(0);
23  gStyle->SetOptFit();
24 
25  TCanvas *c = new TCanvas("c","Graph2D example",0,0,600,800);
26  c->Divide(2,3);
27 
28  Double_t rnd, x, y, z;
29  Double_t e = 0.3;
30  Int_t nd = 400;
31  Int_t np = 10000;
32 
33  TRandom r;
34  Double_t fl = 6;
35  TF2 *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  TGraph2D *dt = new TGraph2D();
39 
40  // Fill the 2D graph
41  Double_t zmax = 0;
42  for (Int_t 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_t hr = 350;
52  TH1D *h1 = new TH1D("h1",
53  "#splitline{Difference between Original}{#splitline{function and Function}{with noise}}",
54  100, -hr, hr);
55  TH1D *h2 = new TH1D("h2",
56  "#splitline{Difference between Original}{#splitline{function and Delaunay triangles}{interpolation}}",
57  100, -hr, hr);
58  TH1D *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  TF2 *fit2 = (TF2*)dt->FindObject("f2");
65 
66  f2->SetParameters(1,1);
67 
68  for (Int_t 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);
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  return c;
106 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:588
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:217
virtual void Draw(Option_t *option="")
Specific drawing options can be used to paint a TGraph2D:
Definition: TGraph2D.cxx:704
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
void SetMargin(Double_t m=0.1)
Sets the extra space (in %) around interpolated area for the 2D histogram.
Definition: TGraph2D.cxx:1566
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
#define N
int Int_t
Definition: RtypesCore.h:41
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="")
Fits this graph with function with name fname Predefined functions such as gaus, expo and poln are au...
Definition: TGraph2D.cxx:757
Double_t x[n]
Definition: legend1.C:17
virtual void SetTitle(const char *title="")
Sets graph title.
Definition: TGraph2D.cxx:1732
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
TH1F * h1
Definition: legend1.C:5
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
TRandom2 r(17)
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
void SetOptFit(Int_t fit=1)
The type of information about fit parameters printed in the histogram statistics box can be selected ...
Definition: TStyle.cxx:1219
Double_t Interpolate(Double_t x, Double_t y)
Finds the z value at the position (x,y) thanks to the Delaunay interpolation.
Definition: TGraph2D.cxx:1264
void SetHistTopMargin(Double_t hmax=0.05)
Definition: TStyle.h:359
A 2-Dim function with parameters.
Definition: TF2.h:29
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:594
virtual void SetMaximum(Double_t maximum=-1111)
Set the maximum value along Y for this function In case the function is already drawn, set also the maximum in the helper histogram.
Definition: TF1.cxx:3163
virtual void SetTitle(const char *title="")
Set function title if title has the form "fffffff;xxxx;yyyy", it is assumed that the function title i...
Definition: TF1.cxx:3315
virtual TObject * FindObject(const char *name) const
search object named name in the list of functions
Definition: TGraph2D.cxx:733
The Canvas class.
Definition: TCanvas.h:31
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
Evaluate this function.
Definition: TF1.cxx:1197
virtual void SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)
Sets point number n.
Definition: TGraph2D.cxx:1692
double Double_t
Definition: RtypesCore.h:55
virtual void GetRandom2(Double_t &xrandom, Double_t &yrandom)
Return 2 random numbers following this function shape.
Definition: TF2.cxx:505
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1135
double f2(const double *x)
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1267
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:310
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition: TGraph2D.h:40
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3564