# How to fit landau functions to histograms using parameters

**URL:** <https://root-forum.cern.ch/t/how-to-fit-landau-functions-to-histograms-using-parameters/50918>\
**Category:** Newbie\
**Tags:** python\
**Created:** [July 22, 2022, 2:47pm UTC](https://root-forum.cern.ch/t/how-to-fit-landau-functions-to-histograms-using-parameters/50918 "2022-07-22T14:47:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ploranger](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@ploranger](https://root-forum.cern.ch/u/ploranger)\
**Post date:** [July 22, 2022, 2:47pm UTC](https://root-forum.cern.ch/t/how-to-fit-landau-functions-to-histograms-using-parameters/50918/1 "2022-07-22T14:47:19Z")

</div>

Hello. I just started working with Root, and I’m writing a script to take a histogram and fit it to a landau function using Pyroot. To my knowledge, the first parameter of the landau function is the location of its peak, so I set it to be the location of the peak of my plot. However, the landau function is shifted rightward, and its peak is not placed at the value which I set it to.  
Here’s the relevant portion of my code:

```auto
for E, file in files.iteritems():

   hists[E].Rebin(50)
   
   xAxis = hists[E].GetXaxis()
   nBins = xAxis.GetNbins()
   
   peakN = hists[E].GetMaximumBin()
   peakX = hists[E].GetXaxis().GetBinCenter(peakN)
   peak = hists[E].GetBinContent(peakN)

   print('final peak:', peak)
   print('final peak X:', peakX)
   fit = r.TF1("landau","landau(0)") # Define the fit object to be landau
   fit.SetParameter(0,peakX) # Initialize the fit object to the mean of the histogram...

   hists[E].Draw("Same PLC PMC") # Draw the histogram, the "Same" argument keeps previous content on the Canvas
 

   hists[E].Fit(fit, "B") # Fit the curve
   fit.Draw("Same") # Draw the fit onto the histogram
   
## SAVE THE HISTOGRAM

canvas.SaveAs("output.pdf");

```

I get an output of peakX = 252.5. This is the resulting plot and fitted function.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/4/2/42b3777676ff4790d2732331655e7c0e288c0d57.png)

I would prefer that the landau function be fit so that its peak matches up with the peak of my histogram plot. How would I achieve this? Thanks a lot in advance.

I’m using Root 6.26.04 and Python 3.10.5

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 22, 2022, 4:01pm UTC](https://root-forum.cern.ch/t/how-to-fit-landau-functions-to-histograms-using-parameters/50918/2 "2022-07-22T16:01:31Z")

</div>

Try with (see also “`fit.Print()`”):  
`fit.SetParameters(peak / 0.18, peakX, hists[E].GetStdDev() / 5.) # (Constant, MPV, Sigma)`
