# Why Landau fitting not working

**URL:** <https://root-forum.cern.ch/t/why-landau-fitting-not-working/55604>\
**Category:** Newbie\
**Created:** [July 10, 2023, 6:42am UTC](https://root-forum.cern.ch/t/why-landau-fitting-not-working/55604 "2023-07-10T06:42:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamaanand](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/9d8465/32.png) [@iamaanand](https://root-forum.cern.ch/u/iamaanand)\
**Post date:** [July 10, 2023, 6:42am UTC](https://root-forum.cern.ch/t/why-landau-fitting-not-working/55604/1 "2023-07-10T06:42:01Z")

</div>

I am trying Landau fit but the location of the fitting peak shifted towards right. And change of the fitted function range also not working . When I change the fitting range I always getting same mean value 6.61.  
I am getting chisq/dof: 3.7104 for this fitting.  
Here is my code :  
void LandauFit()  
{  
// Open the ROOT file  
TFile \*file = TFile::Open(“output0.root”);

```
// Access the "Photons" tree
TTree *tree = (TTree*)file->Get("Photons");

// Create a histogram to hold the data from the "fT" branch
TH1F *histogram = new TH1F("histogram", "fT Branch", 103, -3, 100);

// Fill the histogram with data from the "fT" branch
tree->Draw("fT >> histogram");

// Create a Landau function
TF1 *landauFunc = new TF1("landauFunc", "landau", -5, 100);
landauFunc->SetParameter(1, 20); // Adjust the initial peak position

// Fit the histogram with the Landau function
histogram->Fit("landauFunc");

// Get the mean value from the fit
Double_t mean = landauFunc->GetParameter(1);

// Get the chi-square value

```

Double\_t chiSquare = landauFunc-\>GetChisquare();

// Get the degrees of freedom (number of data points minus the number of fit parameters)  
Int\_t dof = landauFunc-\>GetNDF();

// Calculate chisq/dof  
Double\_t chisqOverDof = chiSquare / dof;

// Print chisq/dof  
cout \<\< "chisq/dof: " \<\< chisqOverDof \<\< endl;

```
// Print the mean
cout << "Mean: " << mean << endl;  

```

}

 ![Screenshot from 2023-07-10 12-04-00](https://root-forum.cern.ch/uploads/default/original/3X/1/b/1bb48acd854587e30363cc02a4d03068a46bf8e2.png)

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [July 10, 2023, 8:15am UTC](https://root-forum.cern.ch/t/why-landau-fitting-not-working/55604/2 "2023-07-10T08:15:37Z")

</div>

Hi @iamaanand; maybe @jonas and/or @moneta can help here.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 10, 2023, 10:26am UTC](https://root-forum.cern.ch/t/why-landau-fitting-not-working/55604/3 "2023-07-10T10:26:39Z")

</div>

Hi,

Your underlined distribution for your histogram does not look to me to be a Landau, your are missing the negative part (i.e. the part before the Landau peak). It is probably a different distribution, maybe a Gamma distribution?

Lorenzo

---

<div class="post-metadata">

**Author:** ![iamaanand](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/9d8465/32.png) [@iamaanand](https://root-forum.cern.ch/u/iamaanand)\
**Post date:** [July 10, 2023, 10:42am UTC](https://root-forum.cern.ch/t/why-landau-fitting-not-working/55604/4 "2023-07-10T10:42:24Z")

</div>

Ok. @moneta I want to fit it with best possible distribution function. Yes, it also look like Gamma distribution. Can you tell me how to fit this distribution. What function I must define?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 11, 2023, 12:44pm UTC](https://root-forum.cern.ch/t/why-landau-fitting-not-working/55604/6 "2023-07-11T12:44:40Z")

</div>

Hi,  
For the Gamma you can use [`ROOT::Math::gamma_pdf`](https://root.cern.ch/doc/master/group__PdfFunc.html#ga4c777e3416e8cad940ad7fc950ef55dc).  
You can then create a TF1 for fitting as following:

```auto
auto f1 = new TF1("f1","[0]*ROOT::Math::gamma_pdf(x,[1],[2],[3])",0,100);
f1->SetParameters(100,2,10,0);
f2->Draw();

```

Lorenzo
