00001 #include "TMultiGraph.h"
00002 #include "TRandom.h"
00003 #include "TF1.h"
00004 #include "TGraphErrors.h"
00005 #include "TCanvas.h"
00006 #include "TMath.h"
00007
00008 void fitMultiGraph()
00009 {
00010
00011
00012
00013
00014 Int_t n = 30;
00015 Double_t *x1 = new Double_t[n];
00016 Double_t *x2 = new Double_t[n];
00017 Double_t *x3 = new Double_t[n];
00018 Double_t *y1 = new Double_t[n];
00019 Double_t *y2 = new Double_t[n];
00020 Double_t *y3 = new Double_t[n];
00021 Double_t *e1 = new Double_t[n];
00022 Double_t *e2 = new Double_t[n];
00023 Double_t *e3 = new Double_t[n];
00024
00025
00026 TRandom r;
00027 Int_t i;
00028 for (i=0; i<n; i++) {
00029 x1[i] = r.Uniform(0.1, 5);
00030 x2[i] = r.Uniform(3, 8);
00031 x3[i] = r.Uniform(9, 15);
00032 y1[i] = 3 + 2*x1[i] + x1[i]*x1[i] + r.Gaus();
00033 y2[i] = 3 + 2*x2[i] + x2[i]*x2[i] + r.Gaus()*10;
00034 e1[i] = 1;
00035 e2[i] = 10;
00036 e3[i] = 20;
00037 y3[i] = 3 + 2*x3[i] + x3[i]*x3[i] + r.Gaus()*20;
00038 }
00039
00040
00041 TGraphErrors *gr1 = new TGraphErrors(n, x1, y1, 0, e1);
00042 TGraphErrors *gr2 = new TGraphErrors(n, x2, y2, 0, e2);
00043 TGraphErrors *gr3 = new TGraphErrors(n, x3, y3, 0, e3);
00044 gr1->SetLineColor(kRed);
00045 gr2->SetLineColor(kBlue);
00046 gr2->SetMarkerStyle(24);
00047 gr2->SetMarkerSize(0.3);
00048 gr3->SetLineColor(kGreen);
00049 gr3->SetMarkerStyle(24);
00050 gr3->SetMarkerSize(0.3);
00051
00052
00053 TMultiGraph *mg=new TMultiGraph("mg",
00054 "TMultiGraph of 3 TGraphErrors");
00055 mg->Add(gr1);
00056 mg->Add(gr2);
00057 mg->Add(gr3);
00058
00059 TCanvas *myc = new TCanvas("myc",
00060 "Fitting a MultiGraph of 3 TGraphErrors");
00061 myc->SetFillColor(42);
00062 myc->SetGrid();
00063
00064 mg->Draw("ap");
00065
00066
00067 mg->Fit("pol2", "F");
00068
00069
00070 TF1 *fpol = mg->GetFunction("pol2");
00071 fpol->SetLineWidth(1);
00072
00073 }
00074
00075 void fitminuit()
00076 {
00077 Int_t n = 30;
00078 Double_t *x1 = new Double_t[n];
00079 Double_t *x2 = new Double_t[n];
00080 Double_t *x3 = new Double_t[n];
00081 Double_t *y1 = new Double_t[n];
00082 Double_t *y2 = new Double_t[n];
00083 Double_t *y3 = new Double_t[n];
00084 Double_t *e1 = new Double_t[n];
00085 Double_t *e2 = new Double_t[n];
00086 Double_t *e3 = new Double_t[n];
00087 Double_t *xtotal = new Double_t[n*3];
00088 Double_t *ytotal = new Double_t[n*3];
00089 Double_t *etotal = new Double_t[n*3];
00090
00091 TRandom r;
00092 Int_t i;
00093 for (i=0; i<n; i++) {
00094 x1[i] = r.Uniform(-3, -1);
00095 x2[i] = r.Uniform(-1, 1);
00096 x3[i] = r.Uniform(1, 3);
00097 y1[i] = TMath::Gaus(x1[i], 0, 1);
00098 y2[i] = TMath::Gaus(x2[i], 0, 1);
00099 e1[i] = 0.00001;
00100 e2[i] = 0.00001;
00101 e3[i] = 0.00001;
00102 y3[i] = TMath::Gaus(x3[i], 0, 1);
00103 }
00104 for (i=0; i<n; i++)
00105 {xtotal[i]=x1[i]; ytotal[i]=y1[i]; etotal[i]=0.00001;}
00106 for (i=n; i<2*n; i++)
00107 {xtotal[i] = x2[i-n]; ytotal[i]=y2[i-n]; etotal[i]=0.00001;}
00108 for (i=2*n; i<3*n; i++)
00109 {xtotal[i] = x3[i-2*n]; ytotal[i]=y3[i-2*n]; etotal[i]=0.00001;}
00110
00111
00112 TGraphErrors *gr1 = new TGraphErrors(n, x1, y1, 0, e1);
00113 TGraphErrors *gr2 = new TGraphErrors(n, x2, y2, 0, e2);
00114 TGraphErrors *gr3 = new TGraphErrors(n, x3, y3, 0, e3);
00115 TGraphErrors *grtotal = new TGraphErrors(n*3, xtotal, ytotal, 0, etotal);
00116 TMultiGraph *mg=new TMultiGraph("mg", "TMultiGraph of 3 TGraphErrors");
00117 mg->Add(gr1);
00118 mg->Add(gr2);
00119 mg->Add(gr3);
00120
00121
00122
00123
00124
00125 grtotal->Fit("gaus");
00126 mg->Fit("gaus");
00127 }