#include "TF2.h" #include "TH2.h" #include "TMath.h" // Fitting a 2-D histogram // This tutorial illustrates : // - how to create a 2-d function // - fill a 2-d histogram randomly from this function // - fit the histogram // - display the fitted function on top of the histogram // // This example can be executed via the interpreter or ACLIC // root > .x fit2.C // root > .x fit2.C++ //Author: Rene Brun Double_t oldpar=0; Double_t g2(Double_t *x, Double_t *par) { Double_t r1 = Double_t((x[0]-par[1])/par[2]); Double_t r2 = Double_t((x[1]-par[3])/par[4]); return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2)); } Double_t fun2(Double_t *x, Double_t *par) { Double_t *p1 = &par[0]; Double_t result = g2(x,p1); if(par[0]!=oldpar) cout << par[0] << endl; oldpar=par[0]; return result; } void fit2() { const Int_t npar = 5; Double_t f2params[npar] = {100,-3,3,-3,3}; TF2 *f2 = new TF2("f2",fun2,-10,10,-10,10, npar); f2->SetParameters(f2params); f2->SetParError(0, 0.01); //Create an histogram and fill it randomly with f2 TH2F *h2 = new TH2F("h2","from f2",40,-10,10,40,-10,10); Int_t nentries = 100000; h2->FillRandom("f2",nentries); //Fit h2 with original function f2 Float_t ratio = 4*nentries/100000; f2params[ 0] *= ratio; f2->SetParameters(f2params); h2->Fit("f2"); f2->Draw("cont1 same"); }