ROOT logo
#include "TMultiGraph.h"
#include "TRandom.h"
#include "TF1.h"
#include "TGraphErrors.h"
#include "TCanvas.h"
#include "TMath.h"

void fitMultiGraph()
{
   //fitting a parabola to a multigraph of 3 partly overlapping graphs
   //with different errors
   //Author: Anna Kreshuk
      
   Int_t n = 30;
   Double_t *x1 = new Double_t[n];
   Double_t *x2 = new Double_t[n];
   Double_t *x3 = new Double_t[n];
   Double_t *y1 = new Double_t[n];
   Double_t *y2 = new Double_t[n];
   Double_t *y3 = new Double_t[n];
   Double_t *e1 = new Double_t[n];
   Double_t *e2 = new Double_t[n];
   Double_t *e3 = new Double_t[n];
   
   //generate the data for the graphs
   TRandom r;
   Int_t i;
   for (i=0; i<n; i++) {
      x1[i] = r.Uniform(0.1, 5);
      x2[i] = r.Uniform(3, 8);
      x3[i] = r.Uniform(9, 15);
      y1[i] = 3 + 2*x1[i] + x1[i]*x1[i] + r.Gaus();
      y2[i] = 3 + 2*x2[i] + x2[i]*x2[i] + r.Gaus()*10;
      e1[i] = 1;
      e2[i] = 10;
      e3[i] = 20;
      y3[i] = 3 + 2*x3[i] + x3[i]*x3[i] + r.Gaus()*20;
   }
   
   //create the graphs and set their drawing options
   TGraphErrors *gr1 = new TGraphErrors(n, x1, y1, 0, e1);
   TGraphErrors *gr2 = new TGraphErrors(n, x2, y2, 0, e2);
   TGraphErrors *gr3 = new TGraphErrors(n, x3, y3, 0, e3);
   gr1->SetLineColor(kRed);
   gr2->SetLineColor(kBlue);
   gr2->SetMarkerStyle(24);
   gr2->SetMarkerSize(0.3);
   gr3->SetLineColor(kGreen);
   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->SetFillColor(42);
   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 *x1 = new Double_t[n];
   Double_t *x2 = new Double_t[n];
   Double_t *x3 = new Double_t[n];
   Double_t *y1 = new Double_t[n];
   Double_t *y2 = new Double_t[n];
   Double_t *y3 = new Double_t[n];
   Double_t *e1 = new Double_t[n];
   Double_t *e2 = new Double_t[n];
   Double_t *e3 = 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];
   
   TRandom r;
   Int_t i;
   for (i=0; i<n; i++) {
      x1[i] = r.Uniform(-3, -1);
      x2[i] = r.Uniform(-1, 1);
      x3[i] = r.Uniform(1, 3);
      y1[i] = TMath::Gaus(x1[i], 0, 1);
      y2[i] = TMath::Gaus(x2[i], 0, 1);
      e1[i] = 0.00001;
      e2[i] = 0.00001;
      e3[i] = 0.00001;
      y3[i] = TMath::Gaus(x3[i], 0, 1);
   }
   for (i=0; i<n; i++) 
      {xtotal[i]=x1[i]; ytotal[i]=y1[i]; etotal[i]=0.00001;}
   for (i=n; i<2*n; i++) 
      {xtotal[i] = x2[i-n]; ytotal[i]=y2[i-n]; etotal[i]=0.00001;}
   for (i=2*n; i<3*n; i++) 
      {xtotal[i] = x3[i-2*n]; ytotal[i]=y3[i-2*n]; etotal[i]=0.00001;}

   //create the graphs and set their drawing options
   TGraphErrors *gr1 = new TGraphErrors(n, x1, y1, 0, e1);
   TGraphErrors *gr2 = new TGraphErrors(n, x2, y2, 0, e2);
   TGraphErrors *gr3 = new TGraphErrors(n, x3, y3, 0, e3);
   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");
}
 fitMultiGraph.C:1
 fitMultiGraph.C:2
 fitMultiGraph.C:3
 fitMultiGraph.C:4
 fitMultiGraph.C:5
 fitMultiGraph.C:6
 fitMultiGraph.C:7
 fitMultiGraph.C:8
 fitMultiGraph.C:9
 fitMultiGraph.C:10
 fitMultiGraph.C:11
 fitMultiGraph.C:12
 fitMultiGraph.C:13
 fitMultiGraph.C:14
 fitMultiGraph.C:15
 fitMultiGraph.C:16
 fitMultiGraph.C:17
 fitMultiGraph.C:18
 fitMultiGraph.C:19
 fitMultiGraph.C:20
 fitMultiGraph.C:21
 fitMultiGraph.C:22
 fitMultiGraph.C:23
 fitMultiGraph.C:24
 fitMultiGraph.C:25
 fitMultiGraph.C:26
 fitMultiGraph.C:27
 fitMultiGraph.C:28
 fitMultiGraph.C:29
 fitMultiGraph.C:30
 fitMultiGraph.C:31
 fitMultiGraph.C:32
 fitMultiGraph.C:33
 fitMultiGraph.C:34
 fitMultiGraph.C:35
 fitMultiGraph.C:36
 fitMultiGraph.C:37
 fitMultiGraph.C:38
 fitMultiGraph.C:39
 fitMultiGraph.C:40
 fitMultiGraph.C:41
 fitMultiGraph.C:42
 fitMultiGraph.C:43
 fitMultiGraph.C:44
 fitMultiGraph.C:45
 fitMultiGraph.C:46
 fitMultiGraph.C:47
 fitMultiGraph.C:48
 fitMultiGraph.C:49
 fitMultiGraph.C:50
 fitMultiGraph.C:51
 fitMultiGraph.C:52
 fitMultiGraph.C:53
 fitMultiGraph.C:54
 fitMultiGraph.C:55
 fitMultiGraph.C:56
 fitMultiGraph.C:57
 fitMultiGraph.C:58
 fitMultiGraph.C:59
 fitMultiGraph.C:60
 fitMultiGraph.C:61
 fitMultiGraph.C:62
 fitMultiGraph.C:63
 fitMultiGraph.C:64
 fitMultiGraph.C:65
 fitMultiGraph.C:66
 fitMultiGraph.C:67
 fitMultiGraph.C:68
 fitMultiGraph.C:69
 fitMultiGraph.C:70
 fitMultiGraph.C:71
 fitMultiGraph.C:72
 fitMultiGraph.C:73
 fitMultiGraph.C:74
 fitMultiGraph.C:75
 fitMultiGraph.C:76
 fitMultiGraph.C:77
 fitMultiGraph.C:78
 fitMultiGraph.C:79
 fitMultiGraph.C:80
 fitMultiGraph.C:81
 fitMultiGraph.C:82
 fitMultiGraph.C:83
 fitMultiGraph.C:84
 fitMultiGraph.C:85
 fitMultiGraph.C:86
 fitMultiGraph.C:87
 fitMultiGraph.C:88
 fitMultiGraph.C:89
 fitMultiGraph.C:90
 fitMultiGraph.C:91
 fitMultiGraph.C:92
 fitMultiGraph.C:93
 fitMultiGraph.C:94
 fitMultiGraph.C:95
 fitMultiGraph.C:96
 fitMultiGraph.C:97
 fitMultiGraph.C:98
 fitMultiGraph.C:99
 fitMultiGraph.C:100
 fitMultiGraph.C:101
 fitMultiGraph.C:102
 fitMultiGraph.C:103
 fitMultiGraph.C:104
 fitMultiGraph.C:105
 fitMultiGraph.C:106
 fitMultiGraph.C:107
 fitMultiGraph.C:108
 fitMultiGraph.C:109
 fitMultiGraph.C:110
 fitMultiGraph.C:111
 fitMultiGraph.C:112
 fitMultiGraph.C:113
 fitMultiGraph.C:114
 fitMultiGraph.C:115
 fitMultiGraph.C:116
 fitMultiGraph.C:117
 fitMultiGraph.C:118
 fitMultiGraph.C:119
 fitMultiGraph.C:120
 fitMultiGraph.C:121
 fitMultiGraph.C:122
 fitMultiGraph.C:123
 fitMultiGraph.C:124
 fitMultiGraph.C:125
 fitMultiGraph.C:126
 fitMultiGraph.C:127
 fitMultiGraph.C:128
thumb