# 2-D Data Filling & Gaussian Fit

Hello everyone,

Now, i’m try to make 2-d fit using my simulation data. and i already fitting 1-d histogram and Gaussian graph.
when i fit my 1-d(TH1) histogram, i’m used ‘Fill’ function. And i’m also try same method to 2-d figure. but TH2 class doesn’t have similar function ‘Fill’. Does anyone know kind of ‘fill’ function in 2-d histogram class?

here is my code.

`````` Double_t String,DOM,theta,phi,avg_y,avg_x;
nentries = (Int_t)T->GetEntries();

TF2 *image1 = new TF2("image1","[0]*TMath::Gaus(x,[1],[2])*TMath::Gaus(y,[3],[4])",0,10,0,10);

for(k=0;k<nentries;k++){
T->GetEntry(k);
avg_x+=abs(phi);
//avg_y+=abs(theta);
}
avg_x = avg_x / nentries;
//avg_y = avg_y / nentries;

for(i=0;i<nentries;i++)
{
T->GetEntry(i);
if(String==2&&DOM==60)
{
if((avg_x-M_PI)<phi&&phi<M_PI) {
phi = phi - avg_x;
}
else if(-M_PI < phi &&phi < (avg_x-M_PI)) {
phi = phi+(2*M_PI)-avg_x;
}
/*if( (avg_y-(0.5*M_PI)) < theta && theta < M_PI ){
theta = theta - avg_y +(0.5*M_PI);
}
else if(0<theta && theta <(avg_y-(0.5*M_PI))){
theta = theta +(1.5*M_PI) - avg_y;
}*/
//image1->Fill(phi,theta);

}
}
image1->SetParameters(0,0,0,0);
//image1->SetOption("c");
image1->Draw();
//image1->Fit("gaus");
``````

First, I`m searched user tutorial and follow the similar topic in root user forum. but doesn’t work.
How can i fix this. Thank you for your help.

best regard.
Zee

`image1->Fill(phi, theta)` does not work, because it is not a `TH2`, as you claim, but a `TF2`, i.e. a 2D function and not a 2D histogram. The `TH2*` classes have a fill option with two parameters:

https://root.cern.ch/doc/master/classTH2.html#a7a9b61f6ee62e07c3840b6004f51ea71

The fit function you define (a 2D-Gauss where the x and y direction are uncorrelated) is already pre-defined as `"xygaus"`, which you can use as `image1->Fit("xygaus")`, after having it initialized similar to what I did in the example below. If there is a correlation between the x and y direction, use `"bigaus"` instead.

Example:

``````auto f2 = new TF2("bigaus","bigaus",-3,3,-3,3);
f2->SetParameters(1,0,1,0,1,0.3);
auto h2 = new TH2D("h2", "", 100, -3, 3, 100, -3, 3);
h2->FillRandom("bigaus", 100000);
h2->Draw("colz");
h2->Fit("bigaus");
``````
2 Likes

Dear Graipher,

I made changed classes ‘TF2’ to ‘TH2’ and then “bigaus” put in my Fit function so finally problem is solved!
Lastly, I have a little question.
If i want to make gaussian graph using TH3 classes, Is there something special function in TH3 classes?

Best Regard.
Zee

I’m sorry, but I don’t think there are any 3D functions pre-defined, so you would have to define them yourself (as you originally did with your 2D function).

Cheers,
Andreas

It was really helpful for me.
Thank you for help!

Best Regard.
Zee

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.