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 n = 30;
23 double *xvalues1 = new double[n];
24 double *xvalues2 = new double[n];
25 double *xvalues3 = new double[n];
26 double *yvalues1 = new double[n];
27 double *yvalues2 = new double[n];
28 double *yvalues3 = new double[n];
29 double *evalues1 = new double[n];
30 double *evalues2 = new double[n];
31 double *evalues3 = new double[n];
32
33 //generate the data for the graphs
34 TRandom r;
35 int 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, nullptr, evalues1);
50 TGraphErrors *gr2 = new TGraphErrors(n, xvalues2, yvalues2, nullptr, evalues2);
51 TGraphErrors *gr3 = new TGraphErrors(n, xvalues3, yvalues3, nullptr, 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 n = 30;
85 double *xvalues1 = new double[n];
86 double *xvalues2 = new double[n];
87 double *xvalues3 = new double[n];
88 double *yvalues1 = new double[n];
89 double *yvalues2 = new double[n];
90 double *yvalues3 = new double[n];
91 double *evalues1 = new double[n];
92 double *evalues2 = new double[n];
93 double *evalues3 = new double[n];
94 double *xtotal = new double[n*3];
95 double *ytotal = new double[n*3];
96 double *etotal = new double[n*3];
97
98 TRandom r;
99 int 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, nullptr, evalues1);
120 TGraphErrors *gr2 = new TGraphErrors(n, xvalues2, yvalues2, nullptr, evalues2);
121 TGraphErrors *gr3 = new TGraphErrors(n, xvalues3, yvalues3, nullptr, evalues3);
122 TGraphErrors *grtotal = new TGraphErrors(n*3, xtotal, ytotal, nullptr, 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}
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
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
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:45
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:233
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:1232
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition TMultiGraph.h:34
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.
void Draw(Option_t *chopt="") override
Draw this multigraph with its current attributes.
void SetGrid(Int_t valuex=1, Int_t valuey=1) override
Definition TPad.h:332
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)
Calculates a gaussian function with mean and sigma.
Definition TMath.cxx:471