Problem with Fitting background + signal

Hello,
I fit background data with a function. Then I performed a signal injection, and now I want to fit it with background+signal (pol5+Gauss), but I am getting an error with the Fit method.

from ROOT import *
import sys
sys.path.append("modules/")
import math


print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
print 'Use as: script.py -b 0 (or 1,2)'
myinput="interactive"
xdir=""
cross=0
expected_pb=1000.
if (len(sys.argv) ==2):
   myinput = sys.argv[1]
   



print 'Mode=',myinput
gROOT.Reset()
figdir="figs/"
nameX="m_{jjl} [TeV]"
nameY="Events / TeV"
Ymin=0.9
Ymax=1000000000-100000
YRMIN=-4.999
YRMAX=4.999
Xmin=400
Xmax=10000
Xmin=Xmin*0.001
Xmax=Xmax*0.001
NN=0
gROOT.SetStyle("Plain");
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)


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");
dir=xdir
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=[]
TFdata2015=[]
TFdata2016=[]
TFdata2017=[]
TFdata2018=[]
TFttbar=[]
TFwjet=[]
TFstop=[]
QCDsamples=[1,2,3,4,5,6,7]

MCqcd_list=[]
maindir='../analysis/out/'
for i in range (0,1):
	sdir=maindir+'sys'+str(i)+"/data/"
	TFdata.append( TFile(sdir+"data_2015_2016_2017_2018.root") )
    
    







systematics=0
fdata=TFdata[systematics]


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 )


#Signal Injection
class Gauss:
    def __call__( self, x, par ):
        out=par[0] * TMath.Gaus(x[0],par[1],par[2])
        return out;


a=0.1
b=5.5
c=0.2


pycaller = Gauss()
bla=TF1("bla",pycaller,5.2,5.8,3);
bla.SetParameters(a,b,c)
bla.SetParNames("a","b","c")

for i in range(0,287):
    TFdata.append(hh.Fill(bla.GetRandom(5.2,5.8)))

#End of Signal Injection





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]*math.log(x)+par[4]*math.log(x)*math.log(x)))
        ff=(ff1*ff2);
        return ff;



pycallable = FiveParam2015TEV()
back=TF1("back",pycallable,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=[]
for i in range(200):
     fitr=hh.Fit(back,"SMR0")
     print "Status=",int(fitr), " is valid=",fitr.IsValid()
     if (fitr.IsValid()==True):
             chi2=back.GetChisquare()/back.GetNDF()
             if chi2<chi2min:
                    chi2min=chi2;
                    if nn>4:
                           break;
                    back.SetParameter(0,random.randint(0,100))
                    back.SetParameter(1,random.randint(0,10))
                    back.SetParameter(2,random.randint(-1,1))
                    nn=nn+1




fitr.Print()
print "Histogram Normalization=", hh.Integral()
print "Originial Histogram Normalization=", hhorg.Integral()
par = back.GetParameters()
back.Draw("same")
chi2= back.GetChisquare()
ndf=back.GetNDF()
print "Chi2=", chi2," ndf=",ndf, " chi2/ndf=",chi2/ndf






class  FiveParam2015TEV_plus_Gauss:
    def __call__( self, x, har ):
        Ecm=13.;
        fCenterOfMassEnergy = Ecm;
        fUseWindowExclusion = False;
        x = x[0] / fCenterOfMassEnergy;
        ff1=har[0]*TMath.Power((1.0-x),har[1])
        ff2=TMath.Power(x,(har[2]+har[3]*math.log(x)+har[4]*math.log(x)*math.log(x)))
        ff3=har[5] * TMath.Gaus(x[0],har[6],har[7])
        ff=((ff1*ff2)+ff3);
        return ff;







pycall= FiveParam2015TEV_plus_Gauss()
ball=TF1("ball",pycall,5.2,5.8,5);
ball.SetNpx(200); ball.SetLineColor(4); ball.SetLineStyle(1)
ball.SetLineWidth(2)
ball.SetParameter(0,20.12911e-01)
ball.SetParameter(1,1.33731e+01)
ball.SetParameter(2,-1.87060e-01)
ball.SetParameter(3,8.84446e-01)
ball.SetParameter(4,1.21341e-01)
ball.SetParameter(5,0.1)
ball.SetParameter(6,5.5)
ball.SetParameter(7,0.2)
import random
nn=0
chi2min=10000
parbest=[]
for i in range(200):
    fithr=hh.Fit(ball,"SMR0") #<- Does not work!!!
    print "Status=",int(fithr), " is valid=",fithr.IsValid()
    if (fithr.IsValid()==True):
        chi2=ball.GetChisquare()/ball.GetNDF()
        if chi2<chi2min:
            chi2min=chi2;
            if nn>4:
                break;
            ball.SetParameter(0,random.randint(0,100))
            ball.SetParameter(1,random.randint(0,10))
            ball.SetParameter(2,random.randint(-1,1))
            nn=nn+1
    
 

The line fithr=hh.Fit(ball,"SMR0") is giving me the following error

Traceback (most recent call last):
  File "<stdin>", line 2, 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: 'float' object has no attribute '__getitem__'
  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)

I am unsure how to resolve this. Any help is greatly appreciated. Unfortunately, I cannot upload the file because it is too big.

_ROOT Version: 6.23/01
Python Version 2.7

Nevermind, the problem was I typed x[0] instead of x on the following line

ff3=har[5]*TMath.Gaus(x[0],har[6],har[7])
1 Like