ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
graph2derrorsfit.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphs
3 /// Draw and fit a TGraph2DErrors
4 ///
5 /// \macro_image
6 /// \macro_code
7 ///
8 /// \author Olivier Couet
9 
10 #include <TMath.h>
11 #include <TGraph2DErrors.h>
12 #include <TRandom.h>
13 #include <TStyle.h>
14 #include <TCanvas.h>
15 #include <TF2.h>
16 
17 void graph2derrorsfit()
18 {
19  TCanvas *c1 = new TCanvas("c1");
20 
21  Double_t rnd, x, y, z, ex, ey, ez;
22  Double_t e = 0.3;
23  Int_t nd = 500;
24 
25  TRandom r;
26  TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6);
27  f2->SetParameters(1,1);
28  TGraph2DErrors *dte = new TGraph2DErrors(nd);
29 
30  // Fill the 2D graph
31  Double_t zmax = 0;
32  for (Int_t i=0; i<nd; i++) {
33  f2->GetRandom2(x,y);
34  rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
35  z = f2->Eval(x,y)*(1+rnd);
36  if (z>zmax) zmax = z;
37  dte->SetPoint(i,x,y,z);
38  ex = 0.05*r.Rndm();
39  ey = 0.05*r.Rndm();
40  ez = TMath::Abs(z*rnd);
41  dte->SetPointError(i,ex,ey,ez);
42  }
43 
44  f2->SetParameters(0.5,1.5);
45  dte->Fit(f2);
46  TF2 *fit2 = (TF2*)dte->FindObject("f2");
47  fit2->SetTitle("Minuit fit result on the Graph2DErrors points");
48  fit2->SetMaximum(zmax);
50  fit2->SetLineColor(1);
51  fit2->SetLineWidth(1);
52  fit2->Draw("surf1");
53  dte->Draw("same p0");
54 }
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
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
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
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
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
virtual void SetPointError(Int_t i, Double_t ex, Double_t ey, Double_t ez)
Set ex, ey and ez values for point number i.
Float_t z[5]
Definition: Ifit.C:16
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
ROOT::R::TRInterface & r
Definition: Object.C:4
void SetHistTopMargin(Double_t hmax=0.05)
Definition: TStyle.h:380
A 2-Dim function with parameters.
Definition: TF2.h:33
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 i, Double_t x, Double_t y, Double_t z)
Set x, y and z values for point number i.
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
Double_t ey[n]
Definition: legend1.C:17
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
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
Double_t ex[n]
Definition: legend1.C:17
Graph 2D class with errors.