Logo ROOT  
Reference Guide
graph2derrorsfit.C File Reference

Detailed Description

View in nbviewer Open in SWAN Draw and fit a TGraph2DErrors

#include <TMath.h>
#include <TGraph2DErrors.h>
#include <TRandom.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TF2.h>
void graph2derrorsfit()
{
TCanvas *c1 = new TCanvas("c1");
Double_t rnd, x, y, z, ex, ey, ez;
Double_t e = 0.3;
Int_t nd = 500;
TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6);
f2->SetParameters(1,1);
// Fill the 2D graph
Double_t zmax = 0;
for (Int_t i=0; i<nd; i++) {
f2->GetRandom2(x,y);
rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
z = f2->Eval(x,y)*(1+rnd);
if (z>zmax) zmax = z;
dte->SetPoint(i,x,y,z);
ex = 0.05*r.Rndm();
ey = 0.05*r.Rndm();
ez = TMath::Abs(z*rnd);
dte->SetPointError(i,ex,ey,ez);
}
f2->SetParameters(0.5,1.5);
dte->Fit(f2);
TF2 *fit2 = (TF2*)dte->FindObject("f2");
fit2->SetTitle("Minuit fit result on the Graph2DErrors points");
fit2->SetMaximum(zmax);
fit2->SetLineColor(1);
fit2->SetLineWidth(1);
fit2->Draw("surf1");
dte->Draw("same p0");
}
Author
Olivier Couet

Definition in file graph2derrorsfit.C.

ex
Double_t ex[n]
Definition: legend1.C:17
TGraph2DErrors::SetPointError
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.
Definition: TGraph2DErrors.cxx:416
e
#define e(i)
Definition: RSha256.hxx:121
TGraph2D::Draw
virtual void Draw(Option_t *option="P0")
Specific drawing options can be used to paint a TGraph2D:
Definition: TGraph2D.cxx:712
TF1::SetMaximum
virtual void SetMaximum(Double_t maximum=-1111)
Set the maximum value along Y for this function In case the function is already drawn,...
Definition: TF1.cxx:3414
TGraph2D::Fit
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:765
r
ROOT::R::TRInterface & r
Definition: Object.C:4
TRandom.h
TStyle.h
Int_t
int Int_t
Definition: RtypesCore.h:45
TGraph2DErrors.h
x
Double_t x[n]
Definition: legend1.C:17
TMath::Abs
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
TCanvas.h
TF1::SetParameters
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:640
TStyle::SetHistTopMargin
void SetHistTopMargin(Double_t hmax=0.05)
Definition: TStyle.h:368
TGraph2DErrors
Definition: TGraph2DErrors.h:26
TF2.h
TRandom
Definition: TRandom.h:27
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TF2::Draw
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:241
y
Double_t y[n]
Definition: legend1.C:17
ey
Double_t ey[n]
Definition: legend1.C:17
TF2
Definition: TF2.h:29
Double_t
double Double_t
Definition: RtypesCore.h:59
TF1::SetTitle
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:3569
TCanvas
Definition: TCanvas.h:23
TF1::Eval
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:1450
TF2::GetRandom2
virtual void GetRandom2(Double_t &xrandom, Double_t &yrandom)
Return 2 random numbers following this function shape.
Definition: TF2.cxx:529
TGraph2D::FindObject
virtual TObject * FindObject(const char *name) const
search object named name in the list of functions
Definition: TGraph2D.cxx:741
TGraph2DErrors::SetPoint
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.
Definition: TGraph2DErrors.cxx:374
TMath.h
c1
return c1
Definition: legend1.C:41