Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
gr011_graph2d_errorsfit.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Create, draw and fit a TGraph2DErrors.

See the TGraph2DErrors documentation

#include <TMath.h>
#include <TGraph2DErrors.h>
#include <TRandom.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TF2.h>
{
TCanvas *c1 = new TCanvas("c1");
Double_t rnd, x, y, z, ex, ey, ez;
Double_t e = 0.3;
Int_t nd = 500;
// To generate some random data to put into the graph
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. It was created only specifying the number of points, so all
// elements are empty. We now "fill" the values and errors with SetPoint and SetPointError.
// Note that the first point has index zero
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();
dte->SetPointError(i,ex,ey,ez);
}
// If the fit is not needed, just draw dte here and skip the lines below
// dte->Draw("A p0");
// To do the fit we use a function, in this example the same f2 from above
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");
}
#define e(i)
Definition RSha256.hxx:103
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
The Canvas class.
Definition TCanvas.h:23
virtual void SetParameters(const Double_t *params)
Definition TF1.h:677
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:1439
A 2-Dim function with parameters.
Definition TF2.h:29
virtual void GetRandom2(Double_t &xrandom, Double_t &yrandom, TRandom *rng=nullptr)
Return 2 random numbers following this function shape.
Definition TF2.cxx:547
Graph 2D class with errors.
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
void SetHistTopMargin(Double_t hmax=0.05)
Definition TStyle.h:389
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
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:123
Author
Olivier Couet

Definition in file gr011_graph2d_errorsfit.C.