Fitting function not converging

I am trying to fit a histogram (from data) with a TF1. But the fit never converges, and instead runs off into crazy values.

The relevant code:

``````  def drawAndFit(histo):
#fitfunc = TF1("fitfunc", landauSmear, dut_min, dut_max, 3)
fitfunc = TF1("landauSmear", landauSmear, 0, 1.0, 2)
fitfunc.SetParName(0, "Landau MPW")
fitfunc.SetParName(1, "Landau Sigma")
fitfunc.SetParName(2, "Gaus sigma")
#fitfunc.SetParameters(0.07, 0.007, 0.0001)
fitfunc.SetParameters(0.07, 0.007)
fitfunc.FixParameter(2,0)

#histo.Fit("fitfunc")
histo.Fit(fitfunc, "", "", dut_min, dut_max)
histo.SetXTitle("Energy Deposition [MeV]")
histo.SetYTitle("Counts");
histo.Write();
c1.Print("edep-" + histo.GetName() + ".png")``````

and:

``````from ROOT import TF1, TMath;

#Cutoff the integration at 5 sigma
cut = 5;

#Its good to know the suport of the function we are trying to generate
min = 0;
max = 1.0;

def setSupport(newMin, newMax):
min = newMin;
max = newMax;

def laundauSmear_integrand(x,par):
"""
The integrand for the LandauSmear function
"""
pass

def landauSmear(x,par):
"""
A Landau smeared with a Gaussian.

Parameters:
[0]: Landau MPW
[1]: Landau sigma
[2]: Gaus sigma
"""

Lmpw = par[0];
Lsig = par[1];
Gsig = 0 #par[2];

E = x[0]

#integrand = TF1("integrand", landauSmear_integrand, -Gsig*cut, Gsig*cut, 4);
#integrand.SetParameters(Lmpw, Lsig, Gsig, E)
#ret = integrand.Integral(-Gsig*cut, Gsig,cut)

lan = TF1("L", "TMath::Landau(x,[0],[1],0)" , min, max)
lan.SetParameters(Lmpw,Lsig);

ret = lan(E)
print E, Lmpw, Lsig, Gsig, ret;

return ret;``````

Does anyone know what is going on? It seems to start out quite good (i started it very close to “correct” values), and the values that are returned (ret) seems resonable - but then the fitter goes crazy…

If I just say histo.Fit(“landau”), it works - but I want to fit it with a landau convolved with a gaussian (eventually)…

I think I solved it - the Landau of TMath is a PDF, so you need to multiply it with another parameter to make it fit an unnormalized histogram - like this:

``````def landauSmear(x,par):
"""
A Landau smeared with a Gaussian.

Parameters:
[0]: Landau MPW
[1]: Landau sigma
[2]: Gaus sigma
"""

Lmpw = par[0];
Lsig = par[1];
Gsig = par[2];
const = par[3];

E = x[0]

#integrand = TF1("integrand", landauSmear_integrand, -Gsig*cut, Gsig*cut, 4);
#integrand.SetParameters(Lmpw, Lsig, Gsig, E)
#ret = integrand.Integral(-Gsig*cut, Gsig,cut)

#lan = TF1("L", "TMath::Landau(x,[0],[1],0)" , min, max)
#lan.SetParameters(Lmpw,Lsig);

ret = const*TMath.Landau(E,Lmpw, Lsig)

print E, Lmpw, Lsig, Gsig, ret;

return ret;``````

Now, for the smearing