Langaus Fit with pyroot

Hi! I am trying to implement the langaus fit described here in c++

https://root.cern.ch/doc/v614/langaus_8C_source.html

into a python code to use it with pyroot.

My code looks as follows:

import ROOT as rt
class langaus:
	def __call__(self, t, par):
		

        width_landau = par[0]
		MP = par[1]
		Area = par[2]
		widt_gauss = par[3]
		x = t[0]

		invsq2pi = 0.3989422804014
		mpshift  = -0.22278298

		np = 100.0
		sc =   5.0

		mpc = par[1] - mpshift * par[0]

		xlow = x - sc * par[3]
		xupp = x + sc * par[3]
		step = (xupp-xlow) / np
        
        summ = 0.0
		for i in range(1, np/2):
			xx = xlow + (i-.5) * step
			fland = TMath.Landau(xx,mpc,par[0]) / par[0]
			summ += fland * TMath.Gaus(x[0],xx,par[3])

			xx = xupp - (i-.5) * step
			fland = TMath.Landau(xx,mpc,par[0]) / par[0]
			summ += fland * TMath.Gaus(x[0],xx,par[3])

		return (par[2] * step * summ * invsq2pi / par[3])

I try to fit with:

parN = 4
langaus = langaus()
func = rt.TF1("myfunc", langaus, 60, 120, parN);

histoname.Fit(func, "R")

Unfortunately I get the error:

Traceback (most recent call last):
  File ".........", line 359, in <module>
    histoname.Fit(func "R")
TypeError: none of the 2 overloaded methods succeeded. Full details:
  TFitResultPtr TH1::Fit(TF1* f1, const char* option = "", const char* goption = "", double xmin = 0, double xmax = 0) =>
    TypeError: 'float' object cannot be interpreted as an integer
  TFitResultPtr TH1::Fit(const char* formula, const char* option = "", const char* goption = "", double xmin = 0, double xmax = 0) =>
    TypeError: could not convert argument 1 (bad argument type for built-in operation)

I don’t get which float object it is talking about. Someone can help? It is working perfectly when using the c++ code though. Thank you!

Maybe @etejedor can take a look

What version of ROOT are you using? If it’s 6.14 (as I see in the docs you checked) please consider moving to a newer version (e.g. 6.22)

I am using ROOT 6.22.

I just ran the code above (I had to add the creation of a dummy histo) and I can’t reproduce your error:

import ROOT as rt
class langaus:
        def __call__(self, t, par):


                width_landau = par[0]
                MP = par[1]
                Area = par[2]
                widt_gauss = par[3]
                x = t[0]

                invsq2pi = 0.3989422804014
                mpshift  = -0.22278298

                np = 100.0
                sc =   5.0

                mpc = par[1] - mpshift * par[0]

                xlow = x - sc * par[3]
                xupp = x + sc * par[3]
                step = (xupp-xlow) / np

        summ = 0.0


parN = 4
langaus = langaus()
func = rt.TF1("myfunc", langaus, 60, 120, parN);
histoname = rt.TH1F("","",1,1,10)
histoname.Fit(func, "R")

prints

ROOT::Fit::FillData:0: RuntimeWarning: fit range is outside histogram range, no fit data for xaxis
Fit:0: RuntimeWarning: Fit data is empty 

Could you share how you create the histo?

In the TBrowser I saved a histo from a root file as a root macro which gave me the bin contents:

histo = rt.TH1D("histo","",2001,0,2000)

histo.SetBinContent(1,398)
histo.SetBinContent(1,398)
histo.SetBinContent(2,25)
histo.SetBinContent(3,6)
histo.SetBinContent(4,5)
histo.SetBinContent(5,4)
and so on..............

I assume I could have read the root file and extract the histo from it but i haven’t looked into on how to do this.
But anyways, I also tried using a custom gauss function:

class myGaus:
    def __call__( self, t, par ):
        a = par[0] #amplitude
        c = par[1] #center
        s = par[2] #sigma
        x = t[0]   
        tmp = -1.*(x-c)*(x-c)/2. /(s*s)
        return a * math.exp( tmp )

and fitting with this just works fine. And using the langaus function when writing in c++ also works fine.

I think I found my error.

for i in range(1, np/2):...

np/2 is not an integer in this case. Even if you define it as np = 100.

And

summ += fland * TMath.Gaus(x[0],xx,par[3])

x[0] needs to be replaced by x in my case. As is defined x = x[0] (or x = t[0] in my case).

Sorry about that. Thank you nevertheless!

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