ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
graph2dfit.C
Go to the documentation of this file.
1 //Fitting a TGraph2D
2 //Author: Olivier Couet
3 
4 #include <TMath.h>
5 #include <TGraph2D.h>
6 #include <TRandom.h>
7 #include <TStyle.h>
8 #include <TCanvas.h>
9 #include <TF2.h>
10 #include <TH1.h>
11 
13 {
14  gStyle->SetOptStat(0);
15  gStyle->SetOptFit();
16 
17  TCanvas *c = new TCanvas("c","Graph2D example",0,0,600,800);
18  c->Divide(2,3);
19 
20  Double_t rnd, x, y, z;
21  Double_t e = 0.3;
22  Int_t nd = 400;
23  Int_t np = 10000;
24 
25  TRandom r;
26  Double_t fl = 6;
27  TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",
28  -fl,fl,-fl,fl);
29  f2->SetParameters(1,1);
30  TGraph2D *dt = new TGraph2D();
31 
32  // Fill the 2D graph
33  Double_t zmax = 0;
34  for (Int_t N=0; N<nd; N++) {
35  f2->GetRandom2(x,y);
36  // Generate a random number in [-e,e]
37  rnd = 2*r.Rndm()*e-e;
38  z = f2->Eval(x,y)*(1+rnd);
39  if (z>zmax) zmax = z;
40  dt->SetPoint(N,x,y,z);
41  }
42 
43  Double_t hr = 350;
44  TH1D *h1 = new TH1D("h1",
45  "#splitline{Difference between Original}{#splitline{function and Function}{with noise}}",
46  100, -hr, hr);
47  TH1D *h2 = new TH1D("h2",
48  "#splitline{Difference between Original}{#splitline{function and Delaunay triangles}{interpolation}}",
49  100, -hr, hr);
50  TH1D *h3 = new TH1D("h3",
51  "#splitline{Difference between Original}{function and Minuit fit}",
52  500, -hr, hr);
53 
54  f2->SetParameters(0.5,1.5);
55  dt->Fit(f2);
56  TF2 *fit2 = (TF2*)dt->FindObject("f2");
57 
58  f2->SetParameters(1,1);
59 
60  for (Int_t N=0; N<np; N++) {
61  f2->GetRandom2(x,y);
62  // Generate a random number in [-e,e]
63  rnd = 2*r.Rndm()*e-e;
64  z = f2->Eval(x,y)*(1+rnd);
65  h1->Fill(f2->Eval(x,y)-z);
66  z = dt->Interpolate(x,y);
67  h2->Fill(f2->Eval(x,y)-z);
68  z = fit2->Eval(x,y);
69  h3->Fill(f2->Eval(x,y)-z);
70  }
71 
72  gStyle->SetPalette(1);
73  c->cd(1);
74  f2->SetTitle("Original function with Graph2D points on top");
75  f2->SetMaximum(zmax);
77  f2->Draw("surf1");
78  dt->Draw("same p0");
79 
80  c->cd(3);
81  dt->SetMargin(0.1);
82  dt->SetFillColor(36);
83  dt->SetTitle("Histogram produced with Delaunay interpolation");
84  dt->Draw("surf4");
85 
86  c->cd(5);
87  fit2->SetTitle("Minuit fit result on the Graph2D points");
88  fit2->Draw("surf1");
89 
90  h1->SetFillColor(47);
91  h2->SetFillColor(38);
92  h3->SetFillColor(29);
93 
94  c->cd(2); h1->Fit("gaus","Q") ; h1->Draw();
95  c->cd(4); h2->Fit("gaus","Q") ; h2->Draw();
96  c->cd(6); h3->Fit("gaus","Q") ; h3->Draw();
97  c->cd();
98  return c;
99 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
virtual TObject * FindObject(const char *name) const
search object named name in the list of functions
Definition: TGraph2D.cxx:748
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:432
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:216
virtual void Draw(Option_t *option="")
Specific drawing options can be used to paint a TGraph2D:
Definition: TGraph2D.cxx:719
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
return c
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
tuple f2
Definition: surfaces.py:24
void SetMargin(Double_t m=0.1)
Sets the extra space (in %) around interpolated area for the 2D histogram.
Definition: TGraph2D.cxx:1581
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
void fit2()
Definition: fit2.C:30
#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:772
Double_t x[n]
Definition: legend1.C:17
virtual void SetTitle(const char *title="")
Sets graph title.
Definition: TGraph2D.cxx:1747
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
tuple np
Definition: multifit.py:30
TH2D * h2
Definition: fit2dHist.C:45
TH1F * h1
Definition: legend1.C:5
Float_t z[5]
Definition: Ifit.C:16
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
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:1204
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:1279
void SetHistTopMargin(Double_t hmax=0.05)
Definition: TStyle.h:380
A 2-Dim function with parameters.
Definition: TF2.h:33
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
TCanvas * graph2dfit()
Definition: graph2dfit.C:12
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:3077
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:3227
The Canvas class.
Definition: TCanvas.h:48
virtual void SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)
Sets point number n.
Definition: TGraph2D.cxx:1707
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:504
Double_t y[n]
Definition: legend1.C:17
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:1073
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:1162
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:1252
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition: TGraph2D.h:50
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:3607
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1445