# TH1 Fit Method Error

**URL:** <https://root-forum.cern.ch/t/th1-fit-method-error/40895>\
**Category:** Newbie\
**Tags:** python\
**Created:** [August 7, 2020, 1:14am UTC](https://root-forum.cern.ch/t/th1-fit-method-error/40895 "2020-08-07T01:14:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [August 7, 2020, 1:14am UTC](https://root-forum.cern.ch/t/th1-fit-method-error/40895/1 "2020-08-07T01:14:56Z")

</div>

Hello,  
I am trying to figure out how some code works, so I am trying to execute it line by line. Well, after the I executed the attached code line by line, I typed the following line:  
`fitr=hh.Fit(back,"SMR0")`  
and I get the following error:

````auto
  File "<stdin>", line 1, in <module>
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: callable was deleted
  TFitResultPtr TH1::Fit(const char* formula, const char* option = "", const char* goption = "", double xmin = 0, double xmax = 0) =>
    TypeError: could not convert argument 1 (expected string or Unicode object, TF1 found)```

Does someone know how to fix this error? Any help is appreciated.

import ROOT
from ROOT import TFile
from ROOT import TPad
from ROOT import TCanvas
from ROOT import TLatex
c1=TCanvas("c","BPRE",10,10,600,600);
c1.Divide(1,1,0.005,0.005);
c1.SetTickx()
c1.SetTicky()
c1.SetTitle("")
c1.SetLineWidth(3)
c1.SetBottomMargin(0.1)
c1.SetTopMargin(0.05)
c1.SetRightMargin(0.01)
c1.SetFillColor(0)
pad1 = TPad("pad1","pad1",0,0.3,1,0.97)
pad1.SetBottomMargin(0)
pad1.SetLeftMargin(0.13)
pad1.SetRightMargin(0.04)
pad1.SetTopMargin(0.02)
pad1.Draw()
pad1.cd()
pad1.SetLogy(1)
pad1.SetLogx(1)
nameX="m_{jjl} [TeV]"
nameY="Events / TeV"
Ymin=0.9
Ymax=1000000000-100000
Xmin=400 
Xmax=10000
Xmin=Xmin*0.001
Xmax=Xmax*0.001
h=pad1.DrawFrame(Xmin,Ymin,Xmax,Ymax)
ay=h.GetYaxis();
ay.SetLabelFont(42)
ay.SetLabelSize(0.05)
ay.SetTitleSize(0.06)
ay.SetNdivisions(505);
ay.SetTitle(nameY)
ax=h.GetXaxis();
ax.SetTitle(nameX);
ax.SetTitleOffset(1.18)
ay.SetTitleOffset(0.8)
ax.SetLabelFont(42)
ay.SetLabelFont(42)
ax.SetLabelSize(0.12)
ax.SetTitleSize(0.14)
ax.Draw("same");
ay.Draw("same");
ff=TFile("../analysis/out/sys42/data/data_2015_2016_2017_2018.root")
ff.ls()
name="jjlmass_tev"
hh=ff.Get(name)
hhorg=hh.Clone()
TFdata=[]
for i in range (0,1):
	sdir='../analysis/out/'+'sys'+str(i)+"/data/"
	TFdata.append( TFile(sdir+"data_2015_2016_2017_2018.root") )
fdata=TFdata[0]
bins=fdata.Get("bins_m_tev")
m1=bins.GetBinContent(1)
nCPU=int(m1/(13.0*0.001))
print "Nr of cores=",nCPU
bins.Scale(1.0/nCPU)
hh.Divide(bins)
Events=hh.GetBinContent(1)
hh.SetTitle("")
hh.SetStats(0)
hh.SetLineWidth(2)
hh.Print("All")
hh.SetAxisRange(Ymin,Ymax,"y");
hh.SetAxisRange(Xmin,Xmax,"x");
hh.SetMarkerColor( 1 )
hh.SetMarkerStyle( 20 )
hh.SetMarkerSize( 0.8 )
hh.Draw("pe")
MyMinX=Xmin
MyMaxX=Xmax
class FiveParam2015TEV:
    def __call__ ( self, x, par ):
        Ecm=13.;
        fCenterOfMassEnergy = Ecm;
        fUseWindowExclusion = False;
        x = x[0] / fCenterOfMassEnergy;
        ff1=par[0]*TMath.Power((1.0-x),par[1])
        ff2=TMath.Power(x,(par[2]+par[3]*log(x)+par[4]*log(x)*log(x)))
        ff=ff1*ff2;
        return ff;

from ROOT import TF1
back=TF1("back",FiveParam2015TEV(),MyMinX,MyMaxX,5);
back.SetNpx(200); back.SetLineColor(4); back.SetLineStyle(1)
back.SetLineWidth(2)
back.SetParameter(0,20.12911e-01)
back.SetParameter(1,1.33731e+01)
back.SetParameter(2,-1.87060e-01)
back.SetParameter(3,8.84446e-01)
back.SetParameter(4,1.21341e-01)
import random
nn=0
chi2min=10000
parbest=[]

````

Unfortunately, I cannot upload the file because it is too big.

\_ROOT Version: 6.23/01  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [August 12, 2020, 8:21am UTC](https://root-forum.cern.ch/t/th1-fit-method-error/40895/2 "2020-08-12T08:21:23Z")

</div>

Hi,

It’s a bit hidden, but in the error there is this message:

`TypeError: callable was deleted`

this indicates that, at the time of calling `hh.Fit`, `back` points to a Python callable that has already been deleted. I believe the problem is in this line:

`back=TF1("back",FiveParam2015TEV(),MyMinX,MyMaxX,5);`

here you invoke `FiveParam2015TEV()`, which creates the Python callable, but that is a temporary. After `back` is constructed, the Python callable will be garbage collected by Python. To solve it, you can do it in two steps:

```auto
pycallable = FiveParam2015TEV()
back=TF1("back",pycallable,MyMinX,MyMaxX,5)

```

Cheers,  
Enric

---

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [August 12, 2020, 3:38pm UTC](https://root-forum.cern.ch/t/th1-fit-method-error/40895/3 "2020-08-12T15:38:24Z")

</div>

Thank you! It turns out I also needed to import TMath and use math.log instead of log. It works now though!
