Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
graph2derrorsfit.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphs
3 /// \notebook
4 /// Draw and fit a TGraph2DErrors
5 ///
6 /// \macro_image
7 /// \macro_code
8 ///
9 /// \author Olivier Couet
10 
11 #include <TMath.h>
12 #include <TGraph2DErrors.h>
13 #include <TRandom.h>
14 #include <TStyle.h>
15 #include <TCanvas.h>
16 #include <TF2.h>
17 
18 void graph2derrorsfit()
19 {
20  TCanvas *c1 = new TCanvas("c1");
21 
22  Double_t rnd, x, y, z, ex, ey, ez;
23  Double_t e = 0.3;
24  Int_t nd = 500;
25 
26  TRandom r;
27  TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6);
28  f2->SetParameters(1,1);
29  TGraph2DErrors *dte = new TGraph2DErrors(nd);
30 
31  // Fill the 2D graph
32  Double_t zmax = 0;
33  for (Int_t i=0; i<nd; i++) {
34  f2->GetRandom2(x,y);
35  rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
36  z = f2->Eval(x,y)*(1+rnd);
37  if (z>zmax) zmax = z;
38  dte->SetPoint(i,x,y,z);
39  ex = 0.05*r.Rndm();
40  ey = 0.05*r.Rndm();
41  ez = TMath::Abs(z*rnd);
42  dte->SetPointError(i,ex,ey,ez);
43  }
44 
45  f2->SetParameters(0.5,1.5);
46  dte->Fit(f2);
47  TF2 *fit2 = (TF2*)dte->FindObject("f2");
48  fit2->SetTitle("Minuit fit result on the Graph2DErrors points");
49  fit2->SetMaximum(zmax);
50  gStyle->SetHistTopMargin(0);
51  fit2->SetLineColor(1);
52  fit2->SetLineWidth(1);
53  fit2->Draw("surf1");
54  dte->Draw("same p0");
55 }