Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
fitMultiGraph.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_fit
3/// \notebook -js
4/// fitting a parabola to a multigraph of 3 partly overlapping graphs
5/// with different errors
6///
7/// \macro_image
8/// \macro_output
9/// \macro_code
10///
11/// \author Anna Kreshuk
12
13#include "TMultiGraph.h"
14#include "TRandom.h"
15#include "TF1.h"
16#include "TGraphErrors.h"
17#include "TCanvas.h"
18#include "TMath.h"
19
20void fitMultiGraph()
21{
22 Int_t n = 30;
23 Double_t *xvalues1 = new Double_t[n];
24 Double_t *xvalues2 = new Double_t[n];
25 Double_t *xvalues3 = new Double_t[n];
26 Double_t *yvalues1 = new Double_t[n];
27 Double_t *yvalues2 = new Double_t[n];
28 Double_t *yvalues3 = new Double_t[n];
29 Double_t *evalues1 = new Double_t[n];
30 Double_t *evalues2 = new Double_t[n];
31 Double_t *evalues3 = new Double_t[n];
32
33 //generate the data for the graphs
34 TRandom r;
35 Int_t i;
36 for (i=0; i<n; i++) {
37 xvalues1[i] = r.Uniform(0.1, 5);
38 xvalues2[i] = r.Uniform(3, 8);
39 xvalues3[i] = r.Uniform(9, 15);
40 yvalues1[i] = 3 + 2*xvalues1[i] + xvalues1[i]*xvalues1[i] + r.Gaus();
41 yvalues2[i] = 3 + 2*xvalues2[i] + xvalues2[i]*xvalues2[i] + r.Gaus()*10;
42 evalues1[i] = 1;
43 evalues2[i] = 10;
44 evalues3[i] = 20;
45 yvalues3[i] = 3 + 2*xvalues3[i] + xvalues3[i]*xvalues3[i] + r.Gaus()*20;
46 }
47
48 //create the graphs and set their drawing options
49 TGraphErrors *gr1 = new TGraphErrors(n, xvalues1, yvalues1, 0, evalues1);
50 TGraphErrors *gr2 = new TGraphErrors(n, xvalues2, yvalues2, 0, evalues2);
51 TGraphErrors *gr3 = new TGraphErrors(n, xvalues3, yvalues3, 0, evalues3);
52 gr1->SetLineColor(kRed);
53 gr2->SetLineColor(kBlue);
54 gr2->SetMarkerStyle(24);
55 gr2->SetMarkerSize(0.3);
56 gr3->SetLineColor(kGreen);
57 gr3->SetMarkerStyle(24);
58 gr3->SetMarkerSize(0.3);
59
60 //add the graphs to the multigraph
61 TMultiGraph *mg=new TMultiGraph("mg",
62 "TMultiGraph of 3 TGraphErrors");
63 mg->Add(gr1);
64 mg->Add(gr2);
65 mg->Add(gr3);
66
67 TCanvas *myc = new TCanvas("myc",
68 "Fitting a MultiGraph of 3 TGraphErrors");
69 myc->SetGrid();
70
71 mg->Draw("ap");
72
73 //fit
74 mg->Fit("pol2", "F");
75
76 //access to the fit function
77 TF1 *fpol = mg->GetFunction("pol2");
78 fpol->SetLineWidth(1);
79
80}
81
82void fitminuit()
83{
84 Int_t n = 30;
85 Double_t *xvalues1 = new Double_t[n];
86 Double_t *xvalues2 = new Double_t[n];
87 Double_t *xvalues3 = new Double_t[n];
88 Double_t *yvalues1 = new Double_t[n];
89 Double_t *yvalues2 = new Double_t[n];
90 Double_t *yvalues3 = new Double_t[n];
91 Double_t *evalues1 = new Double_t[n];
92 Double_t *evalues2 = new Double_t[n];
93 Double_t *evalues3 = new Double_t[n];
94 Double_t *xtotal = new Double_t[n*3];
95 Double_t *ytotal = new Double_t[n*3];
96 Double_t *etotal = new Double_t[n*3];
97
98 TRandom r;
99 Int_t i;
100 for (i=0; i<n; i++) {
101 xvalues1[i] = r.Uniform(-3, -1);
102 xvalues2[i] = r.Uniform(-1, 1);
103 xvalues3[i] = r.Uniform(1, 3);
104 yvalues1[i] = TMath::Gaus(xvalues1[i], 0, 1);
105 yvalues2[i] = TMath::Gaus(xvalues2[i], 0, 1);
106 evalues1[i] = 0.00001;
107 evalues2[i] = 0.00001;
108 evalues3[i] = 0.00001;
109 yvalues3[i] = TMath::Gaus(xvalues3[i], 0, 1);
110 }
111 for (i=0; i<n; i++)
112 {xtotal[i]=xvalues1[i]; ytotal[i]=yvalues1[i]; etotal[i]=0.00001;}
113 for (i=n; i<2*n; i++)
114 {xtotal[i] = xvalues2[i-n]; ytotal[i]=yvalues2[i-n]; etotal[i]=0.00001;}
115 for (i=2*n; i<3*n; i++)
116 {xtotal[i] = xvalues3[i-2*n]; ytotal[i]=yvalues3[i-2*n]; etotal[i]=0.00001;}
117
118 //create the graphs and set their drawing options
119 TGraphErrors *gr1 = new TGraphErrors(n, xvalues1, yvalues1, 0, evalues1);
120 TGraphErrors *gr2 = new TGraphErrors(n, xvalues2, yvalues2, 0, evalues2);
121 TGraphErrors *gr3 = new TGraphErrors(n, xvalues3, yvalues3, 0, evalues3);
122 TGraphErrors *grtotal = new TGraphErrors(n*3, xtotal, ytotal, 0, etotal);
123 TMultiGraph *mg=new TMultiGraph("mg", "TMultiGraph of 3 TGraphErrors");
124 mg->Add(gr1);
125 mg->Add(gr2);
126 mg->Add(gr3);
127 //mg->Draw("ap");
128 //TF1 *ffit = new TF1("ffit", "TMath::Gaus(x, [0], [1], [2])", -3, 3);
129 //ffit->SetParameters(0, 1, 0);
130 //mg->Fit(ffit);
131
132 grtotal->Fit("gaus");
133 mg->Fit("gaus");
134}
ROOT::R::TRInterface & r
Definition Object.C:4
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:41
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:213
A TGraphErrors is a TGraph with error bars.
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:1073
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition TMultiGraph.h:36
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 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.
virtual void Draw(Option_t *chopt="")
Draw this multigraph with its current attributes.
void SetGrid(Int_t valuex=1, Int_t valuey=1) override
Definition TPad.h:326
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
const Int_t n
Definition legend1.C:16
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:448