Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
macro4.C
Go to the documentation of this file.
1// Create, Draw and fit a TGraph2DErrors
2void macro4(){
3 gStyle->SetPalette(kBird);
4 const double e = 0.3;
5 const int nd = 500;
6
7 TRandom3 my_random_generator;
8 TF2 f2("f2",
9 "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",
10 -6,6,-6,6);
11 f2.SetParameters(1,1);
12 TGraph2DErrors dte(nd);
13 // Fill the 2D graph
14 double rnd, x, y, z, ex, ey, ez;
15 for (Int_t i=0; i<nd; i++) {
16 f2.GetRandom2(x,y);
17 // A random number in [-e,e]
18 rnd = my_random_generator.Uniform(-e,e);
19 z = f2.Eval(x,y)*(1+rnd);
20 dte.SetPoint(i,x,y,z);
21 ex = 0.05*my_random_generator.Uniform();
22 ey = 0.05*my_random_generator.Uniform();
23 ez = fabs(z*rnd);
24 dte.SetPointError(i,ex,ey,ez);
25 }
26 // Fit function to generated data
27 f2.SetParameters(0.7,1.5); // set initial values for fit
28 f2.SetTitle("Fitted 2D function");
29 dte.Fit(&f2);
30 // Plot the result
31 auto c1 = new TCanvas();
32 f2.SetLineWidth(1);
33 f2.SetLineColor(kBlue-5);
34 TF2 *f2c = (TF2*)f2.DrawClone("Surf1");
35 TAxis *Xaxis = f2c->GetXaxis();
36 TAxis *Yaxis = f2c->GetYaxis();
37 TAxis *Zaxis = f2c->GetZaxis();
38 Xaxis->SetTitle("X Title"); Xaxis->SetTitleOffset(1.5);
39 Yaxis->SetTitle("Y Title"); Yaxis->SetTitleOffset(1.5);
40 Zaxis->SetTitle("Z Title"); Zaxis->SetTitleOffset(1.5);
41 dte.DrawClone("P0 Same");
42 // Make the x and y projections
43 auto c_p= new TCanvas("ProjCan",
44 "The Projections",1000,400);
45 c_p->Divide(2,1);
46 c_p->cd(1);
47 dte.Project("x")->Draw();
48 c_p->cd(2);
49 dte.Project("y")->Draw();
50}
#define e(i)
Definition RSha256.hxx:103
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
@ kBlue
Definition Rtypes.h:67
@ kBird
Definition TColor.h:121
auto f2
Definition TGaxis_002.C:5
externTStyle * gStyle
Definition TStyle.h:442
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:279
Class to manage histogram axis.
Definition TAxis.h:32
The Canvas class.
Definition TCanvas.h:23
TAxis * GetYaxis() const
Get y axis of the function.
Definition TF1.cxx:2459
TAxis * GetZaxis() const
Get z axis of the function. (In case this object is a TF2 or TF3).
Definition TF1.cxx:2470
TAxis * GetXaxis() const
Get x axis of the function.
Definition TF1.cxx:2448
A 2-Dim function with parameters.
Definition TF2.h:29
Graph 2D class with errors.
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
Random number generator class based on M.
Definition TRandom3.h:27
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:681
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
void macro4()
Definition macro4.C:2
TRandom * rnd
Float_t z