graph2derrorsfit.C

Go to the documentation of this file.
00001 //Create, Draw and fit a TGraph2DErrors
00002 //Author: Olivier Couet
00003    
00004 #include <TMath.h>
00005 #include <TGraph2DErrors.h>
00006 #include <TRandom.h>
00007 #include <TStyle.h>
00008 #include <TCanvas.h>
00009 #include <TF2.h>
00010 
00011 void graph2derrorsfit()
00012 {
00013    TCanvas *c1 = new TCanvas("c1");
00014 
00015    Double_t rnd, x, y, z, ex, ey, ez;
00016    Double_t e = 0.3;
00017    Int_t nd = 500;
00018 
00019    TRandom r;
00020    TF2  *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6);
00021    f2->SetParameters(1,1);
00022    TGraph2DErrors *dte = new TGraph2DErrors(nd);
00023 
00024    // Fill the 2D graph
00025    Double_t zmax = 0;
00026    for (Int_t i=0; i<nd; i++) {
00027       f2->GetRandom2(x,y);      
00028       rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
00029       z = f2->Eval(x,y)*(1+rnd);
00030       if (z>zmax) zmax = z;
00031       dte->SetPoint(i,x,y,z);
00032       ex = 0.05*r.Rndm();
00033       ey = 0.05*r.Rndm();
00034       ez = TMath::Abs(z*rnd);
00035       dte->SetPointError(i,ex,ey,ez);
00036    }
00037 
00038    f2->SetParameters(0.5,1.5);
00039    dte->Fit(f2);
00040    TF2 *fit2 = (TF2*)dte->FindObject("f2");
00041    fit2->SetTitle("Minuit fit result on the Graph2DErrors points");
00042    fit2->SetMaximum(zmax);
00043    gStyle->SetHistTopMargin(0); 
00044    fit2->Draw("surf1");
00045    dte->Draw("same p0");
00046 }

Generated on Tue Jul 5 15:44:22 2011 for ROOT_528-00b_version by  doxygen 1.5.1