From $ROOTSYS/tutorials/graphs/graph2derrorsfit.C

//Create, Draw and fit a TGraph2DErrors
//Author: Olivier Couet
   
#include <TMath.h>
#include <TGraph2DErrors.h>
#include <TRandom.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TF2.h>

void graph2derrorsfit()
{
   TCanvas *c1 = new TCanvas("c1");

   Double_t rnd, x, y, z, ex, ey, ez;
   Double_t e = 0.3;
   Int_t nd = 500;

   TRandom r;
   TF2  *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6);
   f2->SetParameters(1,1);
   TGraph2DErrors *dte = new TGraph2DErrors(nd);

   // Fill the 2D graph
   Double_t zmax = 0;
   for (Int_t i=0; i<nd; i++) {
      f2->GetRandom2(x,y);      
      rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
      z = f2->Eval(x,y)*(1+rnd);
      if (z>zmax) zmax = z;
      dte->SetPoint(i,x,y,z);
      ex = 0.05*r.Rndm();
      ey = 0.05*r.Rndm();
      ez = TMath::Abs(z*rnd);
      dte->SetPointError(i,ex,ey,ez);
   }

   f2->SetParameters(0.5,1.5);
   dte->Fit(f2);
   TF2 *fit2 = (TF2*)dte->FindObject("f2");
   fit2->SetTitle("Minuit fit result on the Graph2DErrors points");
   fit2->SetMaximum(zmax);
   gStyle->SetHistTopMargin(0); 
   fit2->SetLineColor(1);
   fit2->SetLineWidth(1);
   fit2->Draw("surf1");
   dte->Draw("same p0");
}
 graph2derrorsfit.C:1
 graph2derrorsfit.C:2
 graph2derrorsfit.C:3
 graph2derrorsfit.C:4
 graph2derrorsfit.C:5
 graph2derrorsfit.C:6
 graph2derrorsfit.C:7
 graph2derrorsfit.C:8
 graph2derrorsfit.C:9
 graph2derrorsfit.C:10
 graph2derrorsfit.C:11
 graph2derrorsfit.C:12
 graph2derrorsfit.C:13
 graph2derrorsfit.C:14
 graph2derrorsfit.C:15
 graph2derrorsfit.C:16
 graph2derrorsfit.C:17
 graph2derrorsfit.C:18
 graph2derrorsfit.C:19
 graph2derrorsfit.C:20
 graph2derrorsfit.C:21
 graph2derrorsfit.C:22
 graph2derrorsfit.C:23
 graph2derrorsfit.C:24
 graph2derrorsfit.C:25
 graph2derrorsfit.C:26
 graph2derrorsfit.C:27
 graph2derrorsfit.C:28
 graph2derrorsfit.C:29
 graph2derrorsfit.C:30
 graph2derrorsfit.C:31
 graph2derrorsfit.C:32
 graph2derrorsfit.C:33
 graph2derrorsfit.C:34
 graph2derrorsfit.C:35
 graph2derrorsfit.C:36
 graph2derrorsfit.C:37
 graph2derrorsfit.C:38
 graph2derrorsfit.C:39
 graph2derrorsfit.C:40
 graph2derrorsfit.C:41
 graph2derrorsfit.C:42
 graph2derrorsfit.C:43
 graph2derrorsfit.C:44
 graph2derrorsfit.C:45
 graph2derrorsfit.C:46
 graph2derrorsfit.C:47
 graph2derrorsfit.C:48
 graph2derrorsfit.C:49