import ROOT
import math

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





Xmin=400
Xmax=10000
Xmin=Xmin*0.001
Xmax=Xmax*0.001

MyMinX=Xmin
MyMaxX=Xmax


pycall= FiveParam2015TEV_plus_Gauss()
ball=ROOT.TF1("ball",pycall,MyMinX,MyMaxX,8);
ball.SetNpx(200); ball.SetLineColor(3); ball.SetLineStyle(1)
ball.SetLineWidth(2)
ball.SetParameter(0,1.77279e+06)#ball.SetParameter(0,20.12911e-01)
ball.SetParameter(1,15.4542)#ball.SetParameter(1,1.33731e+01)
ball.SetParameter(2,5.16633)#ball.SetParameter(2,-1.87060e-01)
ball.SetParameter(3,3.13384)#ball.SetParameter(3,8.84446e-01)
ball.SetParameter(4,0.427836)#ball.SetParameter(4,1.21341e-01)
#ball.SetParameter(0,2.01569e+06)#ball.SetParameter(0,20.12911e-01)
#ball.SetParameter(1,15.587)#ball.SetParameter(1,1.33731e+01)
#ball.SetParameter(2,5.26436)#ball.SetParameter(2,-1.87060e-01)
#ball.SetParameter(3,3.16089)#ball.SetParameter(3,8.84446e-01)
#ball.SetParameter(4,0.430464)#ball.SetParameter(4,1.21341e-01)



ball.Draw()




file = ROOT.TFile("histo.root")
hh = file.jjlmass_tev
c = ROOT.TCanvas()
hh.Draw()

#fit first background

ball.SetParameter(5,0)
ball.FixParameter(5,0)
ball.FixParameter(6,5.53)
ball.FixParameter(7,1)

hh.Fit(ball,"SL","")



ball.ReleaseParameter(5)
ball.ReleaseParameter(6)
ball.ReleaseParameter(7)
ball.SetParameter(5,100)

#ball.SetParameter(5,0.1)
ball.SetParLimits(5,0,10000)
# need to divide mean parameter by 13 as done in function
ball.SetParameter(6,5.5/13.)
ball.SetParLimits(6,5.4/13.,5.6/13.)
ball.SetParameter(7,0.2)
ball.SetParLimits(7,0,10)



hh.Fit(ball,"SL","")
#fithr.Print()

