Logo ROOT   6.14/05
Reference Guide
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

pict1_fitMultiGraph.C.png
Processing /mnt/build/workspace/root-makedoc-v614/rootspi/rdoc/src/v6-14-00-patches/tutorials/fit/fitMultiGraph.C...
FCN=95.1374 FROM MIGRAD STATUS=CONVERGED 54 CALLS 55 TOTAL
EDM=4.06527e-20 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 3.21134e+00 4.28055e-01 8.68677e-04 4.08980e-11
2 p1 1.87952e+00 2.48404e-01 2.95405e-04 -8.41861e-10
3 p2 1.00914e+00 3.49228e-02 6.26446e-05 -1.30438e-08
#include "TMultiGraph.h"
#include "TRandom.h"
#include "TF1.h"
#include "TGraphErrors.h"
#include "TCanvas.h"
#include "TMath.h"
void fitMultiGraph()
{
Int_t n = 30;
Double_t *xvalues1 = new Double_t[n];
Double_t *xvalues2 = new Double_t[n];
Double_t *xvalues3 = new Double_t[n];
Double_t *yvalues1 = new Double_t[n];
Double_t *yvalues2 = new Double_t[n];
Double_t *yvalues3 = new Double_t[n];
Double_t *evalues1 = new Double_t[n];
Double_t *evalues2 = new Double_t[n];
Double_t *evalues3 = new Double_t[n];
//generate the data for the graphs
Int_t 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, 0, evalues1);
TGraphErrors *gr2 = new TGraphErrors(n, xvalues2, yvalues2, 0, evalues2);
TGraphErrors *gr3 = new TGraphErrors(n, xvalues3, yvalues3, 0, evalues3);
gr1->SetLineColor(kRed);
gr2->SetLineColor(kBlue);
gr2->SetMarkerStyle(24);
gr2->SetMarkerSize(0.3);
gr3->SetMarkerStyle(24);
gr3->SetMarkerSize(0.3);
//add the graphs to the multigraph
"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_t n = 30;
Double_t *xvalues1 = new Double_t[n];
Double_t *xvalues2 = new Double_t[n];
Double_t *xvalues3 = new Double_t[n];
Double_t *yvalues1 = new Double_t[n];
Double_t *yvalues2 = new Double_t[n];
Double_t *yvalues3 = new Double_t[n];
Double_t *evalues1 = new Double_t[n];
Double_t *evalues2 = new Double_t[n];
Double_t *evalues3 = new Double_t[n];
Double_t *xtotal = new Double_t[n*3];
Double_t *ytotal = new Double_t[n*3];
Double_t *etotal = new Double_t[n*3];
Int_t 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, 0, evalues1);
TGraphErrors *gr2 = new TGraphErrors(n, xvalues2, yvalues2, 0, evalues2);
TGraphErrors *gr3 = new TGraphErrors(n, xvalues3, yvalues3, 0, evalues3);
TGraphErrors *grtotal = new TGraphErrors(n*3, xtotal, ytotal, 0, 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");
}
Author
Anna Kreshuk

Definition in file fitMultiGraph.C.