ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fitMultiGraph.C
Go to the documentation of this file.
1 #include "TMultiGraph.h"
2 #include "TRandom.h"
3 #include "TF1.h"
4 #include "TGraphErrors.h"
5 #include "TCanvas.h"
6 #include "TMath.h"
7 
9 {
10  //fitting a parabola to a multigraph of 3 partly overlapping graphs
11  //with different errors
12  //Author: Anna Kreshuk
13 
14  Int_t n = 30;
15  Double_t *x1 = new Double_t[n];
16  Double_t *x2 = new Double_t[n];
17  Double_t *x3 = new Double_t[n];
18  Double_t *y1 = new Double_t[n];
19  Double_t *y2 = new Double_t[n];
20  Double_t *y3 = new Double_t[n];
21  Double_t *e1 = new Double_t[n];
22  Double_t *e2 = new Double_t[n];
23  Double_t *e3 = new Double_t[n];
24 
25  //generate the data for the graphs
26  TRandom r;
27  Int_t i;
28  for (i=0; i<n; i++) {
29  x1[i] = r.Uniform(0.1, 5);
30  x2[i] = r.Uniform(3, 8);
31  x3[i] = r.Uniform(9, 15);
32  y1[i] = 3 + 2*x1[i] + x1[i]*x1[i] + r.Gaus();
33  y2[i] = 3 + 2*x2[i] + x2[i]*x2[i] + r.Gaus()*10;
34  e1[i] = 1;
35  e2[i] = 10;
36  e3[i] = 20;
37  y3[i] = 3 + 2*x3[i] + x3[i]*x3[i] + r.Gaus()*20;
38  }
39 
40  //create the graphs and set their drawing options
41  TGraphErrors *gr1 = new TGraphErrors(n, x1, y1, 0, e1);
42  TGraphErrors *gr2 = new TGraphErrors(n, x2, y2, 0, e2);
43  TGraphErrors *gr3 = new TGraphErrors(n, x3, y3, 0, e3);
44  gr1->SetLineColor(kRed);
45  gr2->SetLineColor(kBlue);
46  gr2->SetMarkerStyle(24);
47  gr2->SetMarkerSize(0.3);
48  gr3->SetLineColor(kGreen);
49  gr3->SetMarkerStyle(24);
50  gr3->SetMarkerSize(0.3);
51 
52  //add the graphs to the multigraph
53  TMultiGraph *mg=new TMultiGraph("mg",
54  "TMultiGraph of 3 TGraphErrors");
55  mg->Add(gr1);
56  mg->Add(gr2);
57  mg->Add(gr3);
58 
59  TCanvas *myc = new TCanvas("myc",
60  "Fitting a MultiGraph of 3 TGraphErrors");
61  myc->SetFillColor(42);
62  myc->SetGrid();
63 
64  mg->Draw("ap");
65 
66  //fit
67  mg->Fit("pol2", "F");
68 
69  //access to the fit function
70  TF1 *fpol = mg->GetFunction("pol2");
71  fpol->SetLineWidth(1);
72 
73 }
74 
75 void fitminuit()
76 {
77  Int_t n = 30;
78  Double_t *x1 = new Double_t[n];
79  Double_t *x2 = new Double_t[n];
80  Double_t *x3 = new Double_t[n];
81  Double_t *y1 = new Double_t[n];
82  Double_t *y2 = new Double_t[n];
83  Double_t *y3 = new Double_t[n];
84  Double_t *e1 = new Double_t[n];
85  Double_t *e2 = new Double_t[n];
86  Double_t *e3 = new Double_t[n];
87  Double_t *xtotal = new Double_t[n*3];
88  Double_t *ytotal = new Double_t[n*3];
89  Double_t *etotal = new Double_t[n*3];
90 
91  TRandom r;
92  Int_t i;
93  for (i=0; i<n; i++) {
94  x1[i] = r.Uniform(-3, -1);
95  x2[i] = r.Uniform(-1, 1);
96  x3[i] = r.Uniform(1, 3);
97  y1[i] = TMath::Gaus(x1[i], 0, 1);
98  y2[i] = TMath::Gaus(x2[i], 0, 1);
99  e1[i] = 0.00001;
100  e2[i] = 0.00001;
101  e3[i] = 0.00001;
102  y3[i] = TMath::Gaus(x3[i], 0, 1);
103  }
104  for (i=0; i<n; i++)
105  {xtotal[i]=x1[i]; ytotal[i]=y1[i]; etotal[i]=0.00001;}
106  for (i=n; i<2*n; i++)
107  {xtotal[i] = x2[i-n]; ytotal[i]=y2[i-n]; etotal[i]=0.00001;}
108  for (i=2*n; i<3*n; i++)
109  {xtotal[i] = x3[i-2*n]; ytotal[i]=y3[i-2*n]; etotal[i]=0.00001;}
110 
111  //create the graphs and set their drawing options
112  TGraphErrors *gr1 = new TGraphErrors(n, x1, y1, 0, e1);
113  TGraphErrors *gr2 = new TGraphErrors(n, x2, y2, 0, e2);
114  TGraphErrors *gr3 = new TGraphErrors(n, x3, y3, 0, e3);
115  TGraphErrors *grtotal = new TGraphErrors(n*3, xtotal, ytotal, 0, etotal);
116  TMultiGraph *mg=new TMultiGraph("mg", "TMultiGraph of 3 TGraphErrors");
117  mg->Add(gr1);
118  mg->Add(gr2);
119  mg->Add(gr3);
120  //mg->Draw("ap");
121  //TF1 *ffit = new TF1("ffit", "TMath::Gaus(x, [0], [1], [2])", -3, 3);
122  //ffit->SetParameters(0, 1, 0);
123  //mg->Fit(ffit);
124 
125  grtotal->Fit("gaus");
126  mg->Fit("gaus");
127 }
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Axis_t xmin=0, Axis_t xmax=0)
Fit this graph with function with name fname.
Definition: TGraph.cxx:1024
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Axis_t xmin=0, Axis_t xmax=0)
Fit this graph with function with name fname.
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
Definition: Rtypes.h:61
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition: TMultiGraph.h:37
int Int_t
Definition: RtypesCore.h:41
Definition: Rtypes.h:61
static const double x2[5]
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:326
virtual void Draw(Option_t *chopt="")
Draw this multigraph with its current attributes.
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
virtual void SetMarkerSize(Size_t msize=1)
Definition: TAttMarker.h:54
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculate a gaussian function with mean and sigma.
Definition: TMath.cxx:453
The Canvas class.
Definition: TCanvas.h:48
static const double x1[5]
double Double_t
Definition: RtypesCore.h:55
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
void fitMultiGraph()
Definition: fitMultiGraph.C:8
void fitminuit()
Definition: fitMultiGraph.C:75
1-Dim function class
Definition: TF1.h:149
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:28
Definition: Rtypes.h:61
virtual void Add(TGraph *graph, Option_t *chopt="")
Add a new graph to the list of graphs.
TF1 * GetFunction(const char *name) const
Return pointer to function with name.
const Int_t n
Definition: legend1.C:16
static const double x3[11]