Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
fitMultiGraph.C File Reference

Detailed Description

View in nbviewer Open in SWAN
fitting a parabola to a multigraph of 3 partly overlapping graphs with different errors

****************************************
Minimizer is Minuit2 / Migrad
Chi2 = 95.1374
NDf = 87
Edm = 8.90994e-21
NCalls = 55
p0 = 3.21134 +/- 0.428055
p1 = 1.87952 +/- 0.248404
p2 = 1.00914 +/- 0.0349228
#include "TMultiGraph.h"
#include "TRandom.h"
#include "TF1.h"
#include "TGraphErrors.h"
#include "TCanvas.h"
#include "TMath.h"
void fitMultiGraph()
{
int n = 30;
double *xvalues1 = new double[n];
double *xvalues2 = new double[n];
double *xvalues3 = new double[n];
double *yvalues1 = new double[n];
double *yvalues2 = new double[n];
double *yvalues3 = new double[n];
double *evalues1 = new double[n];
double *evalues2 = new double[n];
double *evalues3 = new double[n];
//generate the data for the graphs
int i;
for (i=0; i<n; i++) {
xvalues1[i] = r.Uniform(0.1, 5);
xvalues2[i] = r.Uniform(3, 8);
xvalues3[i] = r.Uniform(9, 15);
yvalues1[i] = 3 + 2*xvalues1[i] + xvalues1[i]*xvalues1[i] + r.Gaus();
yvalues2[i] = 3 + 2*xvalues2[i] + xvalues2[i]*xvalues2[i] + r.Gaus()*10;
evalues1[i] = 1;
evalues2[i] = 10;
evalues3[i] = 20;
yvalues3[i] = 3 + 2*xvalues3[i] + xvalues3[i]*xvalues3[i] + r.Gaus()*20;
}
//create the graphs and set their drawing options
TGraphErrors *gr1 = new TGraphErrors(n, xvalues1, yvalues1, nullptr, evalues1);
TGraphErrors *gr2 = new TGraphErrors(n, xvalues2, yvalues2, nullptr, evalues2);
TGraphErrors *gr3 = new TGraphErrors(n, xvalues3, yvalues3, nullptr, evalues3);
gr2->SetMarkerStyle(24);
gr2->SetMarkerSize(0.3);
gr3->SetMarkerStyle(24);
gr3->SetMarkerSize(0.3);
//add the graphs to the multigraph
TMultiGraph *mg=new TMultiGraph("mg",
"TMultiGraph of 3 TGraphErrors");
mg->Add(gr1);
mg->Add(gr2);
mg->Add(gr3);
TCanvas *myc = new TCanvas("myc",
"Fitting a MultiGraph of 3 TGraphErrors");
myc->SetGrid();
mg->Draw("ap");
//fit
mg->Fit("pol2", "F");
//access to the fit function
TF1 *fpol = mg->GetFunction("pol2");
fpol->SetLineWidth(1);
}
void fitminuit()
{
int n = 30;
double *xvalues1 = new double[n];
double *xvalues2 = new double[n];
double *xvalues3 = new double[n];
double *yvalues1 = new double[n];
double *yvalues2 = new double[n];
double *yvalues3 = new double[n];
double *evalues1 = new double[n];
double *evalues2 = new double[n];
double *evalues3 = new double[n];
double *xtotal = new double[n*3];
double *ytotal = new double[n*3];
double *etotal = new double[n*3];
int i;
for (i=0; i<n; i++) {
xvalues1[i] = r.Uniform(-3, -1);
xvalues2[i] = r.Uniform(-1, 1);
xvalues3[i] = r.Uniform(1, 3);
yvalues1[i] = TMath::Gaus(xvalues1[i], 0, 1);
yvalues2[i] = TMath::Gaus(xvalues2[i], 0, 1);
evalues1[i] = 0.00001;
evalues2[i] = 0.00001;
evalues3[i] = 0.00001;
yvalues3[i] = TMath::Gaus(xvalues3[i], 0, 1);
}
for (i=0; i<n; i++)
{xtotal[i]=xvalues1[i]; ytotal[i]=yvalues1[i]; etotal[i]=0.00001;}
for (i=n; i<2*n; i++)
{xtotal[i] = xvalues2[i-n]; ytotal[i]=yvalues2[i-n]; etotal[i]=0.00001;}
for (i=2*n; i<3*n; i++)
{xtotal[i] = xvalues3[i-2*n]; ytotal[i]=yvalues3[i-2*n]; etotal[i]=0.00001;}
//create the graphs and set their drawing options
TGraphErrors *gr1 = new TGraphErrors(n, xvalues1, yvalues1, nullptr, evalues1);
TGraphErrors *gr2 = new TGraphErrors(n, xvalues2, yvalues2, nullptr, evalues2);
TGraphErrors *gr3 = new TGraphErrors(n, xvalues3, yvalues3, nullptr, evalues3);
TGraphErrors *grtotal = new TGraphErrors(n*3, xtotal, ytotal, nullptr, etotal);
TMultiGraph *mg=new TMultiGraph("mg", "TMultiGraph of 3 TGraphErrors");
mg->Add(gr1);
mg->Add(gr2);
mg->Add(gr3);
//mg->Draw("ap");
//TF1 *ffit = new TF1("ffit", "TMath::Gaus(x, [0], [1], [2])", -3, 3);
//ffit->SetParameters(0, 1, 0);
//mg->Fit(ffit);
grtotal->Fit("gaus");
mg->Fit("gaus");
}
@ 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:1254
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:335
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
Author
Anna Kreshuk

Definition in file fitMultiGraph.C.