示例#1
0
def runFits(data,options):
    axis=ROOT.TAxis(options.binsy,options.miny,options.maxy)

   #first pass     
    graphs=[]
    for i in range(0,3):
        graphs.append(ROOT.TGraphErrors())

    for i in range(1,axis.GetNbins()+1):
    
        center=axis.GetBinCenter(i)
        h = data.drawTH1(options.varx,options.cut+"&&({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) 

        histo=copy.deepcopy(h)
        fitter=Fitter(['M'])
        fitter.w.var("M").setVal((options.maxx-options.minx)/2.0)
        fitter.w.var("M").setMax(options.maxx)
        fitter.w.var("M").setMin(options.minx)
        fitter.erfpow('model','M')

        fitter.importBinnedData(histo,['M'],'data')   
        fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(1)])

#        chi=fitter.projection("model","data","M","debugfitMVV_"+options.output+"_pass1_"+str(i)+".png")
    
        for j,g in enumerate(graphs):
            c,cerr=fitter.fetch("c_"+str(j))
            if abs(c-fitter.w.var("c_"+str(j)).getMin())<0.1:
                cerr=abs(c)*10000
            g.SetPoint(i-1,center,c)
            g.SetPointError(i-1,0.0,cerr)
    parameter0=ROOT.TF1("pol0","pol0",options.minx,options.maxx)
    graphs[0].Fit(parameter0)
    

    #Second pass after fixing par0
    for i in range(1,axis.GetNbins()+1):   
        center=axis.GetBinCenter(i)
        h = data.drawTH1(options.varx,options.cut+"&&({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) 
        histo=copy.deepcopy(h)
        fitter=Fitter(['M'])
        fitter.w.var("M").setVal((options.maxx-options.minx)/2.0)
        fitter.w.var("M").setMax(options.maxx)
        fitter.w.var("M").setMin(options.minx)
        fitter.erfpow('model','M')
        fitter.w.var("c_0").setVal(parameter0.Eval(center))
        fitter.w.var("c_0").setConstant(1)
        fitter.importBinnedData(histo,['M'],'data')   
        fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(1)])

#        chi=fitter.projection("model","data","M","debugfitMVV_"+options.output+"_pass2_"+str(i)+".png")
    
        for j,g in enumerate(graphs):
            if j>0:
                c,cerr=fitter.fetch("c_"+str(j))
                if abs(c-fitter.w.var("c_"+str(j)).getMin())<0.1:
                    cerr=abs(c)*10000
                g.SetPoint(i-1,center,c)
                g.SetPointError(i-1,0.0,cerr)

    parameter2=ROOT.TF1("pol3","pol3",options.minx,options.maxx)
#    log0.SetParameters(1,1)
    graphs[2].Fit(parameter2)
    

    #Third pass after fixing par0
    for i in range(1,axis.GetNbins()+1):   
        center=axis.GetBinCenter(i)
        h = data.drawTH1(options.varx,options.cut+"&&({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) 
        #protect for negative weights
        histo=copy.deepcopy(h)

        fitter=Fitter(['M'])
        fitter.w.var("M").setVal((options.maxx-options.minx)/2.0)
        fitter.w.var("M").setMax(options.maxx)
        fitter.w.var("M").setMin(options.minx)
        fitter.erfpow('model','M')


        fitter.w.var("c_0").setVal(parameter0.Eval(center))
        fitter.w.var("c_0").setConstant(1)
        fitter.w.var("c_2").setVal(parameter2.Eval(center))
        fitter.w.var("c_2").setConstant(1)
        fitter.importBinnedData(histo,['M'],'data')   
        fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(1)])
#        chi=fitter.projection("model","data","M","debugfitMVV_"+options.output+"_pass3_"+str(i)+".png")

    
        for j,g in enumerate(graphs):
            if j==1:
                c,cerr=fitter.fetch("c_"+str(j))
                if abs(c-fitter.w.var("c_"+str(j)).getMin())<0.1:
                    cerr=abs(c)*10000
                g.SetPoint(i-1,center,c)
                g.SetPointError(i-1,0.0,cerr)


    parameter1=ROOT.TF1("pol3","pol3",options.minx,options.maxx)
    graphs[1].Fit(parameter1)



    #Fourth pass - plotting
    for i in range(1,axis.GetNbins()+1):   
        center=axis.GetBinCenter(i)
        h = data.drawTH1(options.varx,options.cut+"&&({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) 
        #protect for negative weights
        histo=copy.deepcopy(h)

        fitter=Fitter(['M'])
        fitter.w.var("M").setVal((options.maxx-options.minx)/2.0)
        fitter.w.var("M").setMax(options.maxx)
        fitter.w.var("M").setMin(options.minx)
        fitter.erfpow('model','M')
        fitter.w.var("c_0").setVal(parameter0.Eval(center))
        fitter.w.var("c_0").setConstant(1)

        fitter.w.var("c_1").setVal(parameter1.Eval(center))
        fitter.w.var("c_1").setConstant(1)


        fitter.w.var("c_2").setVal(parameter2.Eval(center))
        fitter.w.var("c_2").setConstant(1)
        fitter.importBinnedData(histo,['M'],'data')   

        chi=fitter.projection("model","data","M","debugfitMVV_"+options.output+"_pass3_"+str(i)+".png")


    #create json
    data={}
    data['p0']=returnString(parameter0,options)
    data['p1']=returnString(parameter1,options)
    data['p2']=returnString(parameter2,options)
    f=open(options.output+".json","w")
    json.dump(data,f)
    f.close()
    return graphs
示例#2
0
(options, args) = parser.parse_args()

parameterization = {}

f = ROOT.TFile(args[0])
histo = f.Get(options.histo)

fitter = Fitter(['x'])
fitter.importBinnedData(histo, ['x'], 'data')

if options.function == 'expo':
    fitter.expo('model', 'x')
    parameterization['type'] = 'expo'

if options.function == 'erfpow':
    fitter.erfpow('model', 'x')
    parameterization['type'] = 'erfpow'

if options.function == 'erfexp':
    fitter.erfexp('model', 'x')
    parameterization['type'] = 'erfexp'

if options.function == 'erfexpCB':
    fitter.erfexpCB('model', 'x')
    parameterization['type'] = 'erfexpCB'

if options.function == 'erfexpTimesCB':
    fitter.erfexpTimesCB('model', 'x')
    parameterization['type'] = 'erfexpTimesCB'

if options.function.find('bernstein') != -1: