Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
gr011_graph2d_errorsfit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// \preview Create, draw and fit a TGraph2DErrors. See the [TGraph2DErrors documentation](https://root.cern/doc/master/classTGraph2DErrors.html)
5///
6/// \macro_image
7/// \macro_code
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
17void gr011_graph2d_errorsfit()
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 // To generate some random data to put into the graph
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
30 TGraph2DErrors *dte = new TGraph2DErrors(nd);
31
32 // Fill the 2D graph. It was created only specifying the number of points, so all
33 // elements are empty. We now "fill" the values and errors with SetPoint and SetPointError.
34 // Note that the first point has index zero
35 Double_t zmax = 0;
36 for (Int_t i=0; i<nd; i++) {
37 f2->GetRandom2(x,y);
38 rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
39 z = f2->Eval(x,y)*(1+rnd);
40 if (z>zmax) zmax = z;
41 dte->SetPoint(i,x,y,z);
42 ex = 0.05*r.Rndm();
43 ey = 0.05*r.Rndm();
44 ez = TMath::Abs(z*rnd);
45 dte->SetPointError(i,ex,ey,ez);
46 }
47 // If the fit is not needed, just draw dte here and skip the lines below
48 // dte->Draw("A p0");
49
50 // To do the fit we use a function, in this example the same f2 from above
51 f2->SetParameters(0.5,1.5);
52 dte->Fit(f2);
53 TF2 *fit2 = (TF2*)dte->FindObject("f2");
54 fit2->SetTitle("Minuit fit result on the Graph2DErrors points");
55 fit2->SetMaximum(zmax);
56 gStyle->SetHistTopMargin(0);
57 fit2->SetLineColor(1);
58 fit2->SetLineWidth(1);
59 fit2->Draw("surf1");
60 dte->Draw("same p0");
61}
ROOT::R::TRInterface & r
Definition Object.C:4
#define e(i)
Definition RSha256.hxx:103
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
externTStyle * gStyle
Definition TStyle.h:442
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
The Canvas class.
Definition TCanvas.h:23
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
void SetTitle(const char *title="") override
Set the title of the TNamed.
virtual void SetMaximum(Double_t maximum=-1111)
virtual void SetParameters(const Double_t *params)
Definition TF1.h:618
Definition TF2.h:29
virtual void GetRandom2(Double_t &xrandom, Double_t &yrandom, TRandom *rng=nullptr)
void Draw(Option_t *option="") override
Default Draw method for all objects.
virtual void SetPointError(Int_t i, Double_t ex, Double_t ey, Double_t ez)
void SetPoint(Int_t i, Double_t x, Double_t y, Double_t z) override
TObject * FindObject(const char *name) const override
Must be redefined in derived classes.
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="")
void Draw(Option_t *option="P0") override
Default Draw method for all objects.
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
return c1
Definition legend1.C:41
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Double_t ey[n]
Definition legend1.C:17
Double_t ex[n]
Definition legend1.C:17
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122