# FIT a TH2F with a TF2 function

Hy everyone !
I hope to post my question in the right place. I’m trying to fit a TH2F histogram with a self made 2D gaussian function. Anyhow the fit doen’t shape look right, it doesn’t follow the shape of the histogram. Th code I use is the following:

[code]Double_t x[200000];
Double_t y[200000];
Int_t n=0;

``````for (Int_t i=0; i<t[2]; i++)
{
for (Int_t j=0; j<numpunt[i]; j++)
{
x[n]=foto_area[i][j];
y[n]=foto_rapporto_raggi[i][j];
n++;
}
};

TCanvas *canvas6 = new TCanvas("rapporto raggi VS area","rapporto raggi VS area",200,10,1400,500);
TH2F *Lego = new TH2F("rapporto raggi VS area","rapporto raggi VS area",xbins,xmin,xmax,ybins,ymin,ymax);
Lego->SetFillColor(kBlue);
canvas6->Divide(2,1);

for (Int_t k=0; k<=n; k++)
{
Lego->Fill(x[k],y[k]);
}
Lego->GetXaxis()->SetTitle("area [px]");
Lego->GetXaxis()->CenterTitle();
Lego->GetYaxis()->CenterTitle();
Lego->GetXaxis()->SetTitleOffset(2);
Lego->GetYaxis()->SetTitleOffset(2);
canvas6->cd(1);
Lego->Draw("lego");

TF2 *Gaus2D = new TF2("Gaus2D","[0]*exp(-0.5*(((x-[2])/[1])*((x-[2])/[1])+((y-[4])/[3])*((y-[4])/[3])))",0.00001,0.00004,0,5);
Gaus2D->SetParameter(0,400);
Gaus2D->SetParameter(1,0.000001);
Gaus2D->SetParameter(2,0.00003);
Gaus2D->SetParameter(3,0.1);
Gaus2D->SetParameter(4,1);
Gaus2D->SetRange(0.00001,0,0.00004,5);

canvas6->cd(2);
Lego->Fit("Gaus2D");

//Lego->Draw("lego");
Gaus2D->Draw("surf1");
``````

[/code]

while the result is the following are in the figure attached to the topic. The gaussian shape starts from an x value of 0.00001, therefore i would like to fit only from that point onwards.

I hope that someone can help me out

cheers
Ian

You’re possibly interested in the “R” fit option: http://root.cern.ch/root/html/TH1.html#TH1:Fit

I tried but it doesn’t work

Hi,

You should consider re-posting this question in the Math and Stats forum.

Thanks,
Philippe.