Beispiel #1
0
def get_mc_fit( sampMan, sampname, sel_base, eta_cuts, xvar, plot_var, binning, workspace, suffix='' ) :

    ph_selection_sr = 'ph_n == 1'
    xmin = xvar.getMin()
    xmax = xvar.getMax()
    addtl_cuts_sr = 'ph_pt[0] > 50 && %s > %d && %s < %d  ' %(plot_var, xmin, plot_var , xmax )

    results = {}

    for ieta in eta_cuts :

        eta_str_sr = 'ph_Is%s[0]' %( ieta )

        full_sel_sr    = ' && '.join( [sel_base, ph_selection_sr, eta_str_sr, addtl_cuts_sr] )

        hist_sr    = clone_sample_and_draw( sampMan, sampname, plot_var, full_sel_sr   , binning )
        
        label = '%s_%s_%s'%(sampname, suffix, ieta)

        fitManager = FitManager( 'dijet', 2, hist_sr, plot_var, ieta, xvar, label, options.useRooFit)

        fit_distribution( fitManager, sampMan, workspace, logy=True )
        results[ieta] = save_distribution( fitManager, sampMan, workspace, logy=True )

    return results
Beispiel #2
0
def makevariableplots(samp,ptlist,fitrange,basesel="1",tag="",var="ph_pt[0]"):
    parmnames = FitManager.ParamDCB
    parmvals = {name:[] for name in parmnames}
    parmerrs = {name:[] for name in parmnames}
    parmufloats = {name:[] for name in parmnames}
    xpoints, xerrs = [],[]
    fm = FitManager("dcbp", xvardata = (0,200,"GeV"))
    for ptrange in zip(ptlist[:-1],ptlist[1:]):
        xpoints.append((ptrange[1]+ptrange[0])/2)
        xerrs  .append((ptrange[1]-ptrange[0])/2)
        ### NOTE NOTE fitting step  NOTE NOTE
        parmdict = fitting(samp,fm,ptrange,fitrange.get(ptrange[0],fitrange[-1]),basesel=basesel,tag=tag,var=var)
        for name,val in parmdict.items():
            parmvals[name].append(val.n) #value
            parmerrs[name].append(val.s) #uncertainty
            parmufloats[name].append(val)
    # dealing with width of overflow/last bin
    xpoints[-1] = ptlist[-2] + xerrs[-2]
    xerrs[-1]   = xerrs[-2]
    print "values:"
    pprint(parmufloats)
    print "xpoints: \n", xpoints
    print "xerrors: \n", xerrs
    npt = len(ptlist)-1
    tod = lambda x: array("d",x)
    xpoints = tod(xpoints)
    xerrs   = tod(xerrs  )
    #for i in range(npt):
    parmgraph = {name:ROOT.TGraphErrors(npt,
                    xpoints,tod(parmvals[name]),xerrs,tod(parmerrs[name])) \
                    for name in parmnames}
    c1 = ROOT.TCanvas("c1","",800,500)
    parmlimits  = FitManager.setuparray["dcb"]
    parmlimits  ={n[0]:n[1:] for n in parmlimits}
    for name, tgraph in parmgraph.items():
        tgraph.SetTitle(name)
        tgraph.GetXaxis().SetTitle("pt")
        tgraph.GetYaxis().SetTitle(name)
        yplot   = parmrange[name]
        tgraph.SetMaximum(yplot[1])
        tgraph.SetMinimum(yplot[0])
        setlinestyle(tgraph)
        tgraph.Draw('AC*')
        ylimit = parmlimits[name]
        if len(ylimit)==3:
            if (ylimit[2]<yplot[1]): 
                hline = addline(tgraph,ylimit[2])
                hline.Draw()
                print name," ", ylimit[2]
            if (ylimit[1]>yplot[0]): 
                lline = addline(tgraph,ylimit[1])
                lline.Draw()
                print name," ", ylimit[1]
        c1.SaveAs(options.outputDir+"/tge_dcb_%s_%s.pdf" %(name,tag))
        c1.SaveAs(options.outputDir+"/tge_dcb_%s_%s.png" %(name,tag))
    data = {'ptlist':ptlist,'parm':parmvals,'error':parmerrs}
    with open('data/temp/dcbparms_%s.txt' %tag,'w') as outfile:
        json.dump(data,outfile)
Beispiel #3
0
def makevariableplots_simultaneous(samp,ptlist,fitrange,basesel="1",tag="",ic = None, dobkgd=True, donorm=True, maxtimes=5, doData=False, var = "ph_pt[0]"):
    """ extended pdf fit """
    if ic is None: ic = dict(sig="dcbp",bkgd="gaus", ext="simul")
    if "icparm" not in ic: ic["icparm"] = dict()
    parmufloats, parmvals, parmerrs = defaultdict(list),defaultdict(list),defaultdict(list)
    parmnames = FitManager.ParamDCB
    parmnames2 = ["gauszg_mean","gauszg_sig"]
    parm ,parm2= get_param("data/dcbparms_all.txt",ptlist), get_param("data/parms_zgamma.txt",ptlist)
    parmptlist = json.loads(open("data/dcbparms_all.txt").read())['ptlist']
    parm2ptlist = json.loads(open("data/parms_zgamma.txt").read())['ptlist']
    fm = FitManager("dcbexpo", xvardata = (0,200,"GeV"))

    ## FIXME: need better ways to obtain fitted parameters
    iconddcb = {pt:[(n,parm[n][parmptlist.index(pt)]  ) for n in parmnames]  for pt in ptlist[:-1]}
    icondgzg = {pt:[(n,parm2[n][parm2ptlist.index(pt)]) for n in parmnames2] for pt in ptlist[:-1]}
    #ieighty = parmptlist.index(80) # search for pt80 parameter
    #iconddcb = {pt:[(n,parm[n][ieighty]) for n in parmnames] for ipt,pt in enumerate(ptlist[:-1])}
    #icondgzg = {pt:[(n,parm2[n][ieighty]) for n in parmnames2] for ipt,pt in enumerate(ptlist[:-1])}
    print "iconddcb"; pprint (iconddcb)
    print "icondgzg"; pprint (icondgzg)
    for ptrange in zip(ptlist[:-1],ptlist[1:]):
        ptrange = tuple(ptrange)
        ptname = tuple(map(lambda x: str(x).replace('.','p'), ptrange))
        #xp.append(ptrange)
        ic['icparm']['dcbp'] = [("x",20,200)]+iconddcb[ptrange[0]] # FIXME: move up a fx level
        ic['icparm']['gauszg'] = [("x",20,200)]+icondgzg[ptrange[0]] # FIXME: move up a fx level
        ### fitting step
        ptfitrange = fitrange.get(ptrange[0],fitrange[-1]) # get default if entry doesnt exist
        values, stackcount = fitting_simultaneous(samp, fm, ptrange, ptfitrange, basesel=basesel, var = var,
                tag=tag, ic = ic, xbins=(200,0,200), dobkgd =dobkgd, maxtimes = maxtimes, doData=doData, donorm=donorm)
        if donorm: make_normalization_comparison(fm,values,stackcount,ptname,tag)
        for name,val in values.items():
            if val is None: #for case of non-initiated variable
                parmvals[name].append(-1)
                parmerrs[name].append(-1) 
            elif val.s>0: #check if the value is fitted
                parmvals[name].append(val.n) #value
                parmerrs[name].append(val.s) #uncertainty
            parmufloats[name].append(val)
    # dealing with width of overflow/last bin
    #xpoints, xerrs = xp.output()
    print "values:"; pprint(parmufloats.items())
    f = lambda p: {x:y for x,y in p.iteritems()}
    data = {'ptlist':ptlist,'parm':f(parmvals),'error':f(parmerrs)}
    with open('data/parms_%s.txt' %tag,'w') as outfile:
        json.dump(data,outfile)
Beispiel #4
0
def makevariableplots_simultaneous_zg( samp, ptlist, fitrange, basesel="1",
                                       tag="", ic = None, dobkgd=True,
                                       donorm=False, maxtimes=5, doData=False):
    """ 
        extended pdf fit for zgamma peak
    """

    if ic is None: ic = dict(sig="dcbp",bkgd="gaus", ext="simul")
    if "icparm" not in ic: ic["icparm"] = dict()
    parmufloats = defaultdict(list)
    parmvals    = defaultdict(list)
    parmerrs    = defaultdict(list)
    parmnames   = FitManager.ParamDCB

    parm = get_param("data/efake/%i/dcbparms_all%i.txt"%((options.year,)*2))
    fm = FitManager("dcbexpo", xvardata = (0,200,"GeV"))
    iconddcb = {pt:[(n,parm[n][ipt]) for n in parmnames] \
                    for ipt,pt in enumerate(ptlist[:-1])}

    for ptrange in zip(ptlist[:-1],ptlist[1:]):
        ptrange = tuple(ptrange)
        #xp.append(ptrange)
        ic['icparm']['dcbp'] = [("x",50,200)]+iconddcb[ptrange[0]] # FIXME: move up a fx level
        #ic['icparm']['gauszg'] = [("x",20,200)]+icondgzg[ptrange[0]] # FIXME: move up a fx level
        ### fitting step
        ptfitrange = fitrange.get(ptrange[0],fitrange[-1]) # get default if entry doesnt exist
        values, stackcount = fitting_simultaneous(samp, fm, ptrange, ptfitrange, basesel=basesel,
                tag=tag, ic = ic, xbins=(200,0,200), dobkgd =dobkgd, maxtimes = maxtimes, doData=doData, donorm=donorm)
        if donorm: make_normalization_comparison(fm,values,stackcount,ptrange,tag)
        for name,val in values.items():
            if val is None: #for case of non-initiated variable
                parmvals[name].append(-1)
                parmerrs[name].append(-1) 
            elif val.s>0: #check if the value is fitted
                parmvals[name].append(val.n) #value
                parmerrs[name].append(val.s) #uncertainty
            parmufloats[name].append(val)
    # dealing with width of overflow/last bin
    #xpoints, xerrs = xp.output()
    print "values:"; pprint(parmufloats.items())
    f = lambda p: {x:y for x,y in p.iteritems()}
    data = {'ptlist':ptlist,'parm':f(parmvals),'error':f(parmerrs)}
    with open('data/efake/%i/parms_%s.txt' %(options.year,tag),'w') as outfile:
        json.dump(data,outfile)
Beispiel #5
0
def fit_sample( hist, xvar,  workspace , suffix, sample_params, label_config ):

    fitManager = FitManager( 'dscb',  hist=hist,  xvardata = xvar,
                            sample_params=sample_params, label = suffix)

    fitManager.setup_fit()
    #fitManager.fit_histogram(workspace )
    #fitManager.run_fit_minuit( fitrange = (fit_min, fit_max) ,debug = True)
    fitManager.run_fit_minuit( fitrange = xvar, debug = True)
    fitManager.get_results( workspace )
    #fitManager.save_fit( sampMan, workspace, stats_pos='left',
    #                     extra_label = extra_label , plotParam =True)
    canv = fitManager.draw( subplot = "pull", paramlayout = (0.12,0.5,0.78),
                            useOldsetup = True, label_config = label_config)

    canv.Print("%s/%s.pdf" %(options.outputDir, suffix) )
    canv.Print("%s/%s.png" %(options.outputDir, suffix) )
    canv.Print("%s/%s.C"   %(options.outputDir, suffix) )

    print "************"
    print " RooFitResult Status: %d"%fitManager.roofitresult.status()
    print "************"

    fitmlist.append(fitManager)
def get_mc_fit(hist, var, fitfunc, workspace, suffix=''):

    if 'mu' in suffix:
        ch, extra_label = "mu", "Muon Channel"
    elif 'el' in suffix:
        ch, extra_label = "el", "Electron Channel"

    xvar = ROOT.RooRealVar(var, var, _XMIN_M, _XMAX_M)

    #full_sel_sr = sel_base.replace("ADDITION", " (%s > %d && %s < %d )"%(var, xmin, var , xmax))
    #print full_sel_sr

    #label = '%s_%s_%s'%(protag, suffix,  fitfunc)

    ## power, expow, vvdijet, atlas, dijet
    norders = 2
    if fitfunc == "expow" or fitfunc == "atlas" or fitfunc == "vvdijet":
        norders = 1
    fitManager = FitManager(fitfunc, hist, xvar, suffix, norders=norders)
    #fitManager.addhist(hist, "datahist"+fitfunc)
    canv = fitManager.draw(paramlayout=(0.7, 0.5, 0.82),
                           useOldsetup=True,
                           logy=1,
                           yrange=(5e-3, 2e4))

    #fitManager.setup_fit()
    fitManager.setup_rootfit(xvar)
    fitManager.func.Draw("same")
    savecanv(canv, "%sbefore_%s" % (suffix, fitfunc))
    fitManager.run_rootfit()

    #fit_distribution( fitManager, sampMan, workspace, logy=True )
    #fitManager.make_func_pdf()
    #fitManager.fit_histogram( workspace )

    print """ cast it from TF1 to RooGenericPdf """
    fitManager.calculate_func_pdf()
    fitManager.get_results(workspace, False)
    outhist = fitManager.get_pdf_histogram(xbins=hist.GetNbinsX())

    #results[ieta] = save_distribution( fitManager, sampMan, workspace, logy=True )
    #fitManager.save_fit( sampMan, workspace, logy = True, stats_pos='right', extra_label = extra_label)

    canv = fitManager.draw(subplot="pull",
                           paramlayout=(0.7, 0.5, 0.82),
                           useOldsetup=True,
                           logy=1,
                           yrange=(5e-3, 2e4))
    savecanv(canv, "%s_%s" % (suffix, fitfunc))

    return outhist
def make_signal_fits(sampMan,
                     sel_base,
                     eta_cuts,
                     plot_var,
                     xvar,
                     binning,
                     workspaces_to_save,
                     suffix,
                     plots_dir='plots'):

    sampMan.clear_hists()

    if not os.path.isdir(plots_dir):
        os.makedirs(plots_dir)

    for samp in sampMan.get_samples(isSignal=True):

        print 'Sample = ', samp.name

        res = re.match('(MadGraph|Pythia)ResonanceMass(\d+)_.*', samp.name)
        if res is None:
            print 'Could not interpret path ', samp.name
        else:

            mass = float(res.group(2))
            iwidth = 0
            if samp.name.count('width0p01'):
                width = 0.0001
                wid = "0p01"
            else:
                res2 = re.match(
                    '(MadGraph|Pythia)ResonanceMass(\d+)_width(\d)', samp.name)
                width = float(res2.group(3)) / 100.
                wid = res2.group(3)
                iwidth = 1

        if samp.name.count('MadGraph') == 0:
            continue
        if mass > 2000 or mass < 300:
            continue
        if iwidth >= len(binning):
            print "exclude", iwidth, binning
            continue
        if mass not in binning[iwidth]:
            print "exclude mass", mass, binning[iwidth]
            continue

        #ph_selection_sr = '%s==1' %defs.get_phid_selection('all')
        #ph_idx_sr =  defs.get_phid_idx( 'all' )
        #print binning[iwidth][mass]
        #addtl_cuts_sr = 'ph_pt[%s] > 50  && %s > %f && %s < %f ' %(ph_idx_sr, plot_var, binning[iwidth][mass][1], plot_var, binning[iwidth][mass][2] )

        #xvar.setBins(10000,'cache')
        #xvar.setMin('cache',-100)
        #xvar.setMax('cache',1500)

        weight_str = defs.get_weight_str()
        sel_base_mu = defs.get_base_selection('mu')
        sel_base_el = defs.get_base_selection('el')

        el_ip_str = '( fabs( el_d0[0] ) < 0.05 && fabs( el_dz[0] ) < 0.10 && fabs( el_sc_eta[0] )<= 1.479 ) || ( fabs( el_d0[0] ) < 0.10 && fabs( el_dz[0] ) < 0.20 && fabs( el_sc_eta[0] )> 1.479 )'

        el_tight = ' el_passVIDTight[0] == 1'
        el_eta = ' fabs( el_eta[0] ) < 2.1 '

        ph_str = 'ph_n==1 && ph_IsEB[0] && ph_pt[0] > 50 && !ph_hasPixSeed[0]'
        ph_tightpt_str = 'ph_n==1 && ph_IsEB[0] && ph_pt[0] > 80 && !ph_hasPixSeed[0]'

        met_str = 'met_pt > 25'

        Zveto_str = 'fabs(m_lep_ph-91)>15.0'

        sel_mu_nominal = '%s * ( %s && %s && %s )' % (weight_str, sel_base_mu,
                                                      ph_str, met_str)
        sel_el_nominal = '%s * ( %s && %s && %s && %s && %s && %s && ( %s ))' % (
            weight_str, sel_base_el, el_tight, el_eta, ph_str, met_str,
            Zveto_str, el_ip_str)

        sel_mu_phpt_nominal = '%s * ( %s && %s && %s && ADDITION)' % (
            weight_str, sel_base_mu, ph_tightpt_str, met_str)
        sel_el_phpt_nominal = '%s * ( %s && %s && %s && %s && %s && %s && ( %s ) && ADDITION)' % (
            weight_str, sel_base_el, el_tight, el_eta, ph_tightpt_str, met_str,
            Zveto_str, el_ip_str)

        sel_base_mu = sel_mu_phpt_nominal
        sel_base_el = sel_el_phpt_nominal

        for ieta in eta_cuts:

            #full_suffix = '%s_%s_%s' %(samp.name, suffix, ieta)
            name = "_".join(
                ['MadGraphResonance', 'Mass',
                 "%d" % mass, 'Width', wid])

            full_suffix = '%s_%s_%s' % (name, suffix, ieta)

            #eta_str_sr = 'ph_Is%s[%s]' %( ieta, ph_idx_sr )

            if 'mu' in suffix:
                sel_base = sel_base_mu
                extra_label = "Muon channel"
            else:
                sel_base = sel_base_el
                extra_label = "Electron channel"

            #full_sel_sr    = ' && '.join( [sel_base, ph_selection_sr, eta_str_sr, addtl_cuts_sr] )
            full_sel_sr = sel_base.replace(
                "ADDITION", " (%s > %d && %s < %d )" %
                (plot_var, binning[iwidth][mass][1], plot_var,
                 binning[iwidth][mass][2]))
            print full_sel_sr

            #hist_sr    = clone_sample_and_draw( sampMan, sampname, plot_var, full_sel_sr, binning )
            sampMan.create_hist(samp, plot_var, full_sel_sr,
                                binning[iwidth][mass])

            hist_sr = samp.hist

            integral = hist_sr.Integral()

            #hist_sr.Scale( 1.0/integral )

            #datahist = ROOT.RooDataHist( 'srhist_%s' %full_suffix, 'srhist', ROOT.RooArgList(xvar), hist_sr)

            #histpdf = ROOT.RooHistPdf( 'srhistpdf_%s' %(full_suffix), 'srhistpdf' , ROOT.RooArgSet( xvar), datahist, 3 )

            assert "mu" in suffix or "el" in suffix, "suffix %s must contain the mu or el channel." % suffix

            if 'mu' in suffix:
                workspace = ROOT.RooWorkspace(
                    'workspace_signal_Mass_%d_Width_%s_mu' % (mass, wid))
            else:
                workspace = ROOT.RooWorkspace(
                    'workspace_signal_Mass_%d_Width_%s_el' % (mass, wid))

            fit_max = mass * 1.20
            fit_min = max(mass * 0.50, 200.0)

            ## set the fit range
            xvar.setMin(fit_min)
            xvar.setMax(fit_max)

            #fitManager = FitManager( 'bwxcb', 0, samp.name, hist_sr, plot_var, ieta, xvar, full_suffix, True,
            #sample_params={'mass' : mass, 'width' : width}, )
            fitManager = FitManager(
                'dscb',
                hist=hist_sr,
                xvardata=xvar,
                label=full_suffix,
                sample_params={
                    'mass': mass,
                    'width': width
                },
            )

            #fitManager.make_func_pdf()
            fitManager.setup_fit()
            #fitManager.fit_histogram(workspace )
            fitManager.run_fit_minuit(fitrange=(fit_min, fit_max))
            fitManager.get_results(workspace)
            #fitManager.save_fit( sampMan, workspace, stats_pos='left' , extra_label = extra_label , plotParam =True)
            canv = fitManager.draw(subplot="pull",
                                   paramlayout=(0.15, 0.5, 0.82),
                                   useOldsetup=True)

            canv.Print("%s/%s.pdf" % (plots_dir, full_suffix))
            canv.Print("%s/%s.C" % (plots_dir, full_suffix))
            print "************"
            print " RooFitResult Status: %d" % fitManager.roofitresult.status()
            print "************"

        workspaces_to_save.update({workspace.GetName(): workspace})
Beispiel #8
0
def makevariableplots_simultaneous(samp,
                                   ptlist,
                                   fitrange,
                                   basesel="1",
                                   tag="",
                                   ic=None,
                                   dobkgd=True,
                                   donorm=True,
                                   maxtimes=5,
                                   doData=False,
                                   var="ph_pt[0]",
                                   useparms=True):
    """ extended pdf fit """

    if ic is None: ic = dict(sig="dcbp", bkgd="gaus", ext="simul")
    if "icparm" not in ic: ic["icparm"] = dict()

    parmufloats = defaultdict(list)
    parmvals, parmerrs = defaultdict(list), defaultdict(list)
    parmnames = FitManager.ParamDCB
    parmnames2 = ["gauszg_mean", "gauszg_sig"]

    ## Get Z+jet/ Zg shape from parameter files
    parm2fn = "data/efake/%i/parms_zgamma.txt" % options.year
    icondgzg = get_ic(parm2fn, parmnames2, ptlist)
    print("icondgzg")
    pprint(icondgzg)

    parmfn = "data/efake/%i/dcbparms_all%i.txt" % ((options.year, ) * 2)
    iconddcb = get_ic(parmfn, parmnames, ptlist)
    print("iconddcb")
    pprint(iconddcb)

    ## set up fit manager
    fm = FitManager("dcbexpo", xvardata=(50, 250, "GeV"))

    for ptrange in zip(ptlist[:-1], ptlist[1:]):
        ptrange = tuple(ptrange)
        ptname = tuple([str(x).replace('.', 'p') for x in ptrange])
        #xp.append(ptrange)
        if useparms:
            ## do some operations here
            icdcbtemp = iconddcb[ptrange[0]]

            for p in ["dcb_sigma", "dcb_mass"]:
                re_parm = getparm(FitManager.setuparray["dcbp"], p)
                replaceparm(icdcbtemp, re_parm)
            ic['icparm']['dcbp'] = [("x", 50, 250)] + icdcbtemp

            ic['icparm']['gauszg'] = [("x", 50, 250)] + icondgzg[ptrange[0]]

        ### NOTE fitting step
        # get default if entry doesnt exist
        ptfitrange = fitrange.get(ptrange[0], fitrange[-1])
        values, stackcount = fitting_simultaneous(
            samp,
            fm,
            ptrange,
            ptfitrange,
            basesel=basesel,
            var=var,
            tag=tag,
            ic=ic,
            #xbins=(20,50,250),
            xbins=[
                50,
            ] + list(range(75, 85, 5)) + list(range(85, 95, 1)) +
            list(range(95, 110, 5)) + list(range(110, 250, 10)) + [
                250,
            ],
            #xbins = range(50, 251, 1),
            dobkgd=dobkgd,
            maxtimes=maxtimes,
            doData=doData,
            donorm=donorm)

        if donorm:
            make_normalization_comparison(fm, values, stackcount, ptname, tag)

        for name, val in list(values.items()):
            if val is None:  #for case of non-initiated variable
                parmvals[name].append(-1)
                parmerrs[name].append(-1)
            elif val.s > 0:  #check if the value is fitted
                parmvals[name].append(val.n)  #value
                parmerrs[name].append(val.s)  #uncertainty
            parmufloats[name].append(val)

    # dealing with width of overflow/last bin
    #xpoints, xerrs = xp.output()
    print("values:")
    pprint(list(parmufloats.items()))
    f = lambda p: {x: y for x, y in p.items()}

    ## output values in json file
    data = {'ptlist': ptlist, 'parm': f(parmvals), 'error': f(parmerrs)}
    with open('data/efake/%i/parms_%s.txt' % (options.year, tag),
              'w') as outfile:
        json.dump(data, outfile)
Beispiel #9
0
def make_signal_fits( sampMan, sel_base, eta_cuts, plot_var, xvar, binning, workspace, suffix ) : 

    sampMan.clear_hists()

    for samp in sampMan.get_samples(isSignal=True ) :

        print 'Sample = ', samp.name


        res = re.match('(MadGraph|Pythia)ResonanceMass(\d+)_.*', samp.name )
        if res is None :
            print 'Could not interpret path ', samp.name
        else :

            mass = float(res.group(2))

            if samp.name.count( 'width0p01' ) :
                width = 0.0001
            else :
                res2 = re.match('(MadGraph|Pythia)ResonanceMass(\d+)_width(\d)', samp.name )
                width = float(res2.group(3))/100.

        if mass != 450 : 
            continue

        ph_selection_sr = '%s==1' %defs.get_phid_selection('all')
        ph_idx_sr =  defs.get_phid_idx( 'all' )
        print binning[mass]
        addtl_cuts_sr = 'ph_pt[%s] > 50  && %s > %f && %s < %f ' %(ph_idx_sr, plot_var, binning[mass][1], plot_var, binning[mass][2] )

        xvar.setBins(10000,'cache')
        xvar.setMin('cache',-100)
        xvar.setMax('cache',1500)

        for ieta in eta_cuts :

            full_suffix = '%s_%s_%s' %(samp.name, suffix, ieta)

            eta_str_sr = 'ph_Is%s[%s]' %( ieta, ph_idx_sr )

            full_sel_sr    = ' && '.join( [sel_base, ph_selection_sr, eta_str_sr, addtl_cuts_sr] )

            #hist_sr    = clone_sample_and_draw( sampMan, sampname, plot_var, full_sel_sr, binning )
            sampMan.create_hist( samp, plot_var, full_sel_sr, binning[mass] ) 

            hist_sr = samp.hist

            integral = hist_sr.Integral()

            #hist_sr.Scale( 1.0/integral )

            #datahist = ROOT.RooDataHist( 'srhist_%s' %full_suffix, 'srhist', ROOT.RooArgList(xvar), hist_sr)

            #histpdf = ROOT.RooHistPdf( 'srhistpdf_%s' %(full_suffix), 'srhistpdf' , ROOT.RooArgSet( xvar), datahist, 3 )

            fit_max = mass*1.1
            fit_min = mass/1.8
            if mass >= 1000 :
                fit_max = mass*1.05
                fit_min = mass*0.7

            xvar.setMin( fit_min )
            xvar.setMax( fit_max )


            fitManager = FitManager( 'bwxcb', 0, hist_sr, plot_var, ieta, xvar, full_suffix, True, 
                                    sample_params={'mass' : mass, 'width' : width}, )

            fit_distribution( fitManager)
            #save_fit( fitManager, sampMan, workspace, stats_pos='left' )

            iter_managers = []
            iter_managers.append( fitManager )

            saved_result = False
            for i in range( 0, 4 ) :
            #for i in [0.5, 0.3, 0.2, 0.1, 0.05] :

                print 'GOTHERE1'
                iter_managers.append(FitManager( 'bwxcb', 0, hist_sr, plot_var, ieta, xvar, full_suffix, True, 
                                        sample_params={'mass' : mass, 'width' : width}, ))

                print 'GOTHERE2'
                cv_sigma = iter_managers[-2].cb_sigma.getValV()
                lo_sigma = iter_managers[-2].cb_sigma.getErrorLo()
                hi_sigma = iter_managers[-2].cb_sigma.getErrorHi()
                cv_power = iter_managers[-2].cb_power.getValV()
                lo_power = iter_managers[-2].cb_power.getErrorLo()
                hi_power = iter_managers[-2].cb_power.getErrorHi()
                cv_mass  = iter_managers[-2].cb_mass.getValV()
                lo_mass  = iter_managers[-2].cb_mass.getErrorLo()
                hi_mass  = iter_managers[-2].cb_mass.getErrorHi()

                err_sigma = hi_sigma/cv_sigma
                err_power = hi_power/cv_power
                err_mass = hi_mass/cv_mass

                #new_lim_sigma = ( cv_sigma, cv_sigma*(1-i), cv_sigma*(1+i)) 
                #new_lim_power = ( cv_power, cv_power*(1-i), cv_power*(1+i) ) 
                #new_lim_mass  = ( cv_mass, cv_mass*(1-i), cv_mass*(1+i)    ) 

                new_lim_sigma = ( cv_sigma, cv_sigma+lo_sigma, cv_sigma+hi_sigma) 
                new_lim_power = ( cv_power, cv_power+lo_power, cv_power+hi_power ) 
                new_lim_mass  = ( cv_mass,  cv_mass+lo_mass, cv_mass+hi_mass    ) 

                #if cv_sigma < 0 :
                #    new_lim_sigma = ( new_lim_sigma[0], new_lim_sigma[2], new_lim_sigma[1]) 
                #if cv_power < 0 :
                #    new_lim_power = ( new_lim_power[0], new_lim_power[2], new_lim_power[1]) 
                #if cv_mass < 0 :
                #    new_lim_mass = ( new_lim_mass[0], new_lim_mass[2], new_lim_mass[1] ) 

                print 'NEW DEFAULTS'
                print new_lim_sigma
                print new_lim_power
                print new_lim_mass

                iter_managers[-1].set_vals('cb_sigma', mass, new_lim_sigma )
                iter_managers[-1].set_vals('cb_power', mass, new_lim_power )
                iter_managers[-1].set_vals('cb_mass',  mass, new_lim_mass  )

                print 'GOTHERE3'
                fit_distribution( iter_managers[-1] )
                print 'GOTHERE4'

                cv_sigma_new = iter_managers[-1].cb_sigma.getValV()
                lo_sigma_new = iter_managers[-1].cb_sigma.getErrorLo()
                hi_sigma_new = iter_managers[-1].cb_sigma.getErrorHi()
                cv_power_new = iter_managers[-1].cb_power.getValV()
                lo_power_new = iter_managers[-1].cb_power.getErrorLo()
                hi_power_new = iter_managers[-1].cb_power.getErrorHi()
                cv_mass_new  = iter_managers[-1].cb_mass.getValV()
                lo_mass_new  = iter_managers[-1].cb_mass.getErrorLo()
                hi_mass_new  = iter_managers[-1].cb_mass.getErrorHi()

                err_sigma_new = hi_sigma_new/cv_sigma_new
                err_power_new = hi_power_new/cv_power_new
                err_mass_new = hi_mass_new/cv_mass_new

                print 'Sigma : Previous error = %f, new error = %f' %( err_sigma, err_sigma_new )
                print 'Power : Previous error = %f, new error = %f' %( err_power, err_power_new )
                print 'Mass : Previous error = %f, new error = %f' %( err_mass, err_mass_new )

                print 'GOTHERE5'
                # if we get worse results with the new fit, then use the previous one
                if math.fabs(err_sigma_new) > math.fabs(err_sigma) or math.fabs(err_power_new) > math.fabs(err_power) or math.fabs(err_mass_new) > math.fabs(err_mass) :
                    print 'GOTHERE6'
                    save_fit( iter_managers[-2], sampMan, workspace, stats_pos='left' )
                    print 'GOTHERE7'
                    saved_result = True
                    break

            # if we haven't saved the fit yet, then the best
            # version is the latest
            if not saved_result :
                print 'GOTHERE8'
                save_fit( iter_managers[-1], sampMan, workspace, stats_pos='left' )
                print 'GOTHERE9'
Beispiel #10
0
    data_n=df.at[0, 'total_test_data'],
)
valid_ds_cls = DataManager(
    tfrecord_path=path_cls.get_validation_ds_path(),
    img_root=IMAGE_ROOT_PATH,
    batch_size=VALIDATION_BATCH_SIZE,
    net_cls=net_cls,
    data_n=df.at[0, 'total_valid_data'],
)

# ========== ネットワーク呼び出し ==========
fit_cls = FitManager(
    net_cls=net_cls,
    path_cls=path_cls,
    shuf_train_ds_cls=shuf_train_ds_cls,
    train_ds_cls=train_ds_cls,
    test_ds_cls=test_ds_cls,
    validation_ds_cls=valid_ds_cls,
    check_img_path=check_img_path,
)

# ========== 学習&評価 ==========
fit_cls.fit(
    end_epoch=END_EPOCH,
    device_list=DEVICE_LIST,
    ckpt_step=CHECK_STEP,
)

# ========== plot ==========
PlotFunc.accuracy_plot(
    path_cls=path_cls,
Beispiel #11
0
def make_wjets_fit( sampMan, sample, sel_base, eta_cut, plot_var, shape_var, num_var, binning, xvar, suffix='', closure=False, workspace=None) :

    #---------------------------------------
    # Get the base selection for each region
    #---------------------------------------
    ph_selection_sr    = '%s==1' %defs.get_phid_selection('medium')
    ph_selection_den   = '%s==1'%defs.get_phid_selection( num_var, shape_var )
    ph_selection_num   = '%s==1' %defs.get_phid_selection( num_var )
    ph_selection_shape = '%s==1' %defs.get_phid_selection( shape_var )
    #ph_selection_ABCD  = '%s==1' %defs.get_phid_selection('all')

    ph_idx_sr =  defs.get_phid_idx( 'medium' )
    ph_idx_den = defs.get_phid_idx( num_var, shape_var )
    ph_idx_num = defs.get_phid_idx( num_var )
    ph_idx_shape = defs.get_phid_idx( shape_var )
    #ph_idx_ABCD = defs.get_phid_idx('all')

    xmin = xvar.getMin()
    xmax = xvar.getMax()

    #---------------------------------------
    # Add eta cuts, (IsEB, IsEE)
    #---------------------------------------
    eta_str_shape = 'ph_Is%s[%s]' %( eta_cut, ph_idx_shape )
    eta_str_den = 'ph_Is%s[%s]' %( eta_cut, ph_idx_den)
    eta_str_num = 'ph_Is%s[%s]' %( eta_cut, ph_idx_num )
    eta_str_sr = 'ph_Is%s[%s]' %( eta_cut, ph_idx_sr )
    #eta_str_ABCD = 'ph_Is%s[%s]' %( eta_cut, ph_idx_ABCD )

    #---------------------------------------
    # Add additional cuts, mainly restricting
    # the fitted variable to the plotting limits
    #---------------------------------------
    addtl_cuts_sr = 'ph_pt[%s] > 50 && %s > %d && %s < %d '     %( ph_idx_sr, plot_var, xmin, plot_var, xmax )
    addtl_cuts_den = 'ph_pt[%s] > 50 && %s > %d && %s < %d  '   %( ph_idx_den, plot_var, xmin, plot_var, xmax )
    addtl_cuts_num = 'ph_pt[%s] > 50 && %s > %d && %s < %d  '   %( ph_idx_num, plot_var, xmin, plot_var, xmax )
    addtl_cuts_shape = 'ph_pt[%s] > 50 && %s > %d && %s < %d  ' %( ph_idx_shape, plot_var, xmin, plot_var, xmax )
    #addtl_cuts_ABCD = 'ph_pt[%s] > 50 && %s > %d && %s < %d  ' %( ph_idx_ABCD, plot_var, xmin, plot_var, xmax )


    #---------------------------------------
    # Get the cuts that define the photon
    # sideband regions
    #---------------------------------------
    cut_str_base = ' {var}[{idx}] > {val_low} && {var}[{idx}] < {val_high}'

    cut_var_shape = defs.get_phid_cut_var( shape_var )
    cut_var_num = defs.get_phid_cut_var( num_var )

    cut_vals_shape = get_cut_defaults( shape_var, eta_cut )
    cut_vals_num = get_cut_defaults( num_var, eta_cut )

    cut_str_shape = cut_str_base.format(var=cut_var_shape, idx=ph_idx_shape, val_low=cut_vals_shape[0], val_high=cut_vals_shape[1] )
    cut_str_num = cut_str_base.format(var=cut_var_num, idx=ph_idx_num, val_low=cut_vals_num[0], val_high=cut_vals_num[1] )

    cut_str_den_1 = cut_str_base.format(var=cut_var_shape, idx=ph_idx_den, val_low=cut_vals_shape[0], val_high=cut_vals_shape[1] )
    cut_str_den_2 = cut_str_base.format(var=cut_var_num, idx=ph_idx_den, val_low=cut_vals_num[0], val_high=cut_vals_num[1] )

    cut_str_den = cut_str_den_1 + ' && ' + cut_str_den_2

    #---------------------------------------
    # put the cuts together
    #---------------------------------------
    full_sel_shape = ' && '.join( [sel_base, ph_selection_shape, eta_str_shape, addtl_cuts_shape, cut_str_shape ] )
    full_sel_num   = ' && '.join( [sel_base, ph_selection_num, eta_str_num, addtl_cuts_num, cut_str_num] )
    full_sel_den   = ' && '.join( [sel_base, ph_selection_den, eta_str_den, addtl_cuts_den, cut_str_den] )
    full_sel_sr    = ' && '.join( [sel_base, ph_selection_sr, eta_str_sr, addtl_cuts_sr] )
    #full_sel_ABCD  = ' && '.join( [sel_base, ph_selection_ABCD, eta_str_ABCD, addtl_cuts_ABCD] )

    label_shape = 'shape_%s_%s' %(sample,suffix)
    label_num   = 'num_%s_%s' %(sample,suffix)
    label_den   = 'den_%s_%s' %(sample,suffix)
    label_sr    = 'sr_%s_%s' %(sample,suffix)

    if workspace is None :
        ws = ROOT.RooWorkspace( 'ws') 
    else :
        ws = workspace

    jet_var = 'leadjet_pt' # 0 to 640 in bins of 5
    jet_binning = (128,0.,640.)
    subjet_var = 'subljet_pt' # 0 to 380 in bins of 3.8
    subjet_binning = (100,0.,380.)
    ht_var = 'trueht' # 0 to 7000 in bins of 50
    ht_binning = (140,0.,7000.)

    ABCD_var = 'ph_sigmaIEIE:ph_chIsoCorr'
    ABCD_binning = (120,0.,12.,50,0.,0.05)

    #chIsovar   = 'ph_chIso'
    #sigIEIEvar = 'ph_sigmaIEIE'

    chIso_B_var     = 'ph_chIsoCorr[ptSorted_ph_mediumNoChIso_idx[0]]'
    sigIEIE_B_var = 'ph_sigmaIEIE[ptSorted_ph_mediumNoChIso_idx[0]]'
    chIso_C_var     = 'ph_chIsoCorr[ptSorted_ph_mediumNoSIEIE_idx[0]]'
    sigIEIE_C_var = 'ph_sigmaIEIE[ptSorted_ph_mediumNoSIEIE_idx[0]]'
    chIso_D_var     = 'ph_chIsoCorr[ptSorted_ph_mediumNoSIEIENoChIso_idx[0]]'
    sigIEIE_D_var = 'ph_sigmaIEIE[ptSorted_ph_mediumNoSIEIENoChIso_idx[0]]'

    twoD_B_var = 'ph_sigmaIEIE[ptSorted_ph_mediumNoChIso_idx[0]]:ph_chIsoCorr[ptSorted_ph_mediumNoChIso_idx[0]]'
    twoD_C_var = 'ph_sigmaIEIE[ptSorted_ph_mediumNoSIEIE_idx[0]]:ph_chIsoCorr[ptSorted_ph_mediumNoSIEIE_idx[0]]'
    twoD_D_var = 'ph_sigmaIEIE[ptSorted_ph_mediumNoSIEIENoChIso_idx[0]]:ph_chIsoCorr[ptSorted_ph_mediumNoSIEIENoChIso_idx[0]]'

    sigIEIE_binning = (50,0.,0.05)
    chIso_binning = (120,0.,12.)

    #---------------------------------------
    # draw the histograms
    #---------------------------------------
    hist_shape = clone_sample_and_draw( sampMan, sample, plot_var, full_sel_shape, binning )
    hist_num   = clone_sample_and_draw( sampMan, sample, plot_var, full_sel_num  , binning )
    hist_den   = clone_sample_and_draw( sampMan, sample, plot_var, full_sel_den  , binning )
    sampMan.outputs['%s_shapehist_%s' %(sample,suffix)] = hist_shape
    sampMan.outputs['%s_numhist_%s' %(sample,suffix)] = hist_num
    sampMan.outputs['%s_denhist_%s' %(sample,suffix)] = hist_den



    #hist_ABCD  = clone_sample_and_draw( sampMan, sample, ABCD_var, full_sel_ABCD, ABCD_binning )
    hist_B     = clone_sample_and_draw( sampMan, sample, twoD_B_var, full_sel_shape, ABCD_binning )
    hist_C     = clone_sample_and_draw( sampMan, sample, twoD_C_var, full_sel_num, ABCD_binning )
    hist_D     = clone_sample_and_draw( sampMan, sample, twoD_D_var, full_sel_den, ABCD_binning )

    hist_1d_sigmaIEIE_B  = clone_sample_and_draw( sampMan, sample, sigIEIE_B_var, full_sel_shape, sigIEIE_binning )
    hist_1d_chIso_B  = clone_sample_and_draw( sampMan, sample, chIso_B_var, full_sel_shape, chIso_binning )
    hist_1d_sigmaIEIE_C  = clone_sample_and_draw( sampMan, sample, sigIEIE_C_var, full_sel_num, sigIEIE_binning )
    hist_1d_chIso_C  = clone_sample_and_draw( sampMan, sample, chIso_C_var, full_sel_num, chIso_binning )
    hist_1d_sigmaIEIE_D  = clone_sample_and_draw( sampMan, sample, sigIEIE_D_var, full_sel_den, sigIEIE_binning )
    hist_1d_chIso_D  = clone_sample_and_draw( sampMan, sample, chIso_D_var, full_sel_den, chIso_binning )

    sampMan.outputs['%s_sigmaIEIE_B_%s' %(sample,suffix)] = hist_1d_sigmaIEIE_B
    sampMan.outputs['%s_sigmaIEIE_C_%s' %(sample,suffix)] = hist_1d_sigmaIEIE_C
    sampMan.outputs['%s_sigmaIEIE_D_%s' %(sample,suffix)] = hist_1d_sigmaIEIE_D
    sampMan.outputs['%s_chIso_B_%s' %(sample,suffix)] = hist_1d_chIso_B
    sampMan.outputs['%s_chIso_C_%s' %(sample,suffix)] = hist_1d_chIso_C
    sampMan.outputs['%s_chIso_D_%s' %(sample,suffix)] = hist_1d_chIso_D


    #---------------------------------------
    # make fit managers
    #---------------------------------------
    fitMan_shape = FitManager( 'dijet', 3, '%s_shape' %sample, hist_shape, plot_var, eta_cut, xvar, label_shape, useRooFit=False )
    fitMan_num   = FitManager( 'dijet', 3, '%s_num' %sample, hist_num, plot_var, eta_cut, xvar, label_num, useRooFit=False )
    fitMan_den   = FitManager( 'dijet', 3, '%s_den' %sample, hist_den, plot_var, eta_cut, xvar, label_den, useRooFit=False )

    #---------------------------------------
    # Do the fits
    #---------------------------------------
    result_shape= fitMan_shape.fit_histogram( ws )
    result_num = fitMan_num.fit_histogram( ws )
    result_den = fitMan_den.fit_histogram( ws )
    print 'DEBUG: results of fit manager (shape, num, den): ',result_shape,result_num,result_den

    #---------------------------------------
    # save the results
    #---------------------------------------
    fitMan_den.save_fit( sampMan, ws, logy=True )
    fitMan_shape.save_fit( sampMan, ws, logy=True )
    fitMan_num.save_fit( sampMan, ws, logy=True )

    #---------------------------------------
    # calculate the function that describes
    # the ratio of num/den
    #---------------------------------------
    _integral_num   =  ws.pdf( 'dijet_%s' %label_num ).getNormIntegral(ROOT.RooArgSet( xvar ) )
    _integral_den   =  ws.pdf( 'dijet_%s' %label_den).getNormIntegral(ROOT.RooArgSet( xvar ) )
    _integral_shape =  ws.pdf( 'dijet_%s' %label_shape).getNormIntegral(ROOT.RooArgSet( xvar ) )

    func_integral_num    = _integral_num.getValV()
    func_integral_den    = _integral_den.getValV()
    func_integral_shape  = _integral_shape.getValV()

    hist_integral_num = result_num['integral'].n
    hist_integral_den = result_den['integral'].n
    hist_integral_shape = result_shape['integral'].n

    norm_num = hist_integral_num / func_integral_num
    norm_den = hist_integral_den / func_integral_den
    norm_shape = hist_integral_shape / func_integral_shape

    print 'func integral Num = ', func_integral_num
    print 'hist integral Num = ', hist_integral_num
    print 'normalization Num = ', norm_num

    print 'func integral Den = ', func_integral_den
    print 'hist integral Den = ', hist_integral_den
    print 'normalization Den = ', norm_den

    print 'func integral Shape = ', func_integral_shape
    print 'hist integral Shape = ', hist_integral_shape
    print 'normalization Shape = ', norm_shape

    power_pred_name    = 'power_pred_%s' %suffix
    logcoef_pred_name  = 'logcoef_pred_%s' %suffix
    log2coef_pred_name  = 'log2coef_pred_%s' %suffix
    power_ratio_name   = 'power_ratio_%s' %suffix
    logcoef_ratio_name = 'logcoef_ratio_%s' %suffix


    #name_power_num     ='power' 
    #name_power_den     ='power' 
    #name_power_shape   ='power' 

    #name_logcoef_num   ='logcoef1' 
    #name_logcoef_den   ='logcoef1' 
    #name_logcoef_shape ='logcoef1' 

    name_power_num     ='dijet_order1_%s' %label_num  
    name_power_den     ='dijet_order1_%s' %label_den  
    name_power_shape   ='dijet_order1_%s' %label_shape

    name_logcoef_num   ='dijet_order2_%s' %label_num  
    name_logcoef_den   ='dijet_order2_%s' %label_den  
    name_logcoef_shape ='dijet_order2_%s' %label_shape

    name_log2coef_num   ='dijet_order3_%s' %label_num  
    name_log2coef_den   ='dijet_order3_%s' %label_den  
    name_log2coef_shape ='dijet_order3_%s' %label_shape


    val_power_num     = ws.var( name_power_num      )
    val_power_den     = ws.var( name_power_den      )
    val_power_shape   = ws.var( name_power_shape    )

    val_logcoef_num   = ws.var( name_logcoef_num    )
    val_logcoef_den   = ws.var( name_logcoef_den    )
    val_logcoef_shape = ws.var( name_logcoef_shape  )

    val_log2coef_num   = ws.var( name_log2coef_num    )
    val_log2coef_den   = ws.var( name_log2coef_den    )
    val_log2coef_shape = ws.var( name_log2coef_shape  )


    power_pred    = ROOT.RooRealVar( power_pred_name   , 'power'  , (val_power_num.getValV() + val_power_shape.getValV() - val_power_den.getValV()) , -100, 100)
    logcoef_pred  = ROOT.RooRealVar( logcoef_pred_name , 'logcoef', (val_logcoef_num.getValV() + val_logcoef_shape.getValV() - val_logcoef_den.getValV()), -10, 10 )
    log2coef_pred  = ROOT.RooRealVar( log2coef_pred_name , 'log2coef', (val_log2coef_num.getValV() + val_log2coef_shape.getValV() - val_log2coef_den.getValV()), -10, 10 )
    #power_ratio   = ROOT.RooRealVar( power_ratio_name  , 'power'  , val_power_num - val_power_den , -100, 100)
    #logcoef_ratio = ROOT.RooRealVar( logcoef_ratio_name, 'logcoef', val_logcoef_num - val_logcoef_den, -10, 10 )


    #func = 'TMath::Power(@0/13000, @1+@2*TMath::Log10(@0/13000))'  
    #prediction = ROOT.RooGenericPdf('dijet_prediction_%s' %suffix , 'prediction', func, ROOT.RooArgList(xvar,power_pred, logcoef_pred))
    func = 'TMath::Power(@0/13000, @1+@2*TMath::Log10(@0/13000)+@3*TMath::Log10(@0/13000)*TMath::Log10(@0/13000))'  
    prediction = ROOT.RooGenericPdf('dijet_prediction_%s' %suffix , 'prediction', func, ROOT.RooArgList(xvar,power_pred, logcoef_pred, log2coef_pred))
    norm_pred = ROOT.RooRealVar( 'dijet_prediction_%s_norm' %(suffix), 'prediction normalization', (hist_integral_shape * hist_integral_num) / hist_integral_den )
    getattr( ws , 'import' ) ( norm_pred )
    getattr( ws , 'import' ) ( prediction )


    #---------------------------------------
    # Save the ABCD plots
    #---------------------------------------

    #can_ABCD = ROOT.TCanvas( str(uuid.uuid4()), '' )
    #can_ABCD.SetLogz()
    #hist_ABCD.Scale(1./hist_ABCD.Integral())
    #hist_ABCD.Draw("colz")
    #print "DEBUG: ABCD integral = ",hist_ABCD.Integral()

    can_B = ROOT.TCanvas( str(uuid.uuid4()), '' )
    can_B.SetLogz()
    hist_B.Scale(1./hist_B.Integral())
    hist_B.Draw("colz")
    print "DEBUG: B integral = ",hist_B.Integral()

    can_C = ROOT.TCanvas( str(uuid.uuid4()), '' )
    hist_C.Scale(1./hist_C.Integral())
    can_C.SetLogz()
    hist_C.Draw("colz")
    print "DEBUG: C integral = ",hist_C.Integral()

    can_D = ROOT.TCanvas( str(uuid.uuid4()), '' )
    hist_D.Scale(1./hist_D.Integral())
    can_D.SetLogz()
    hist_D.Draw("colz")
    print "DEBUG: D integral = ",hist_D.Integral()

    can_1d_sig_B = ROOT.TCanvas( str(uuid.uuid4()), '' )
    can_1d_sig_B.SetLogy()
    hist_1d_sigmaIEIE_B.Draw('l')
    can_1d_iso_B = ROOT.TCanvas( str(uuid.uuid4()), '' )
    can_1d_iso_B.SetLogy()
    hist_1d_chIso_B.Draw('l')

    can_1d_sig_C = ROOT.TCanvas( str(uuid.uuid4()), '' )
    can_1d_sig_C.SetLogy()
    hist_1d_sigmaIEIE_C.Draw('l')
    can_1d_iso_C = ROOT.TCanvas( str(uuid.uuid4()), '' )
    can_1d_iso_C.SetLogy()
    hist_1d_chIso_C.Draw('l')

    can_1d_sig_D = ROOT.TCanvas( str(uuid.uuid4()), '' )
    can_1d_sig_D.SetLogy()
    hist_1d_sigmaIEIE_D.Draw('l')
    can_1d_iso_D = ROOT.TCanvas( str(uuid.uuid4()), '' )
    can_1d_iso_D.SetLogy()
    hist_1d_chIso_D.Draw('l')

    #plot sigmaIEIE in regions B, C, D
    can_1d_sig = ROOT.TCanvas( str(uuid.uuid4()), '' )
    can_1d_sig.SetLogy()

    hist_norm_1d_sigmaIEIE_B = ROOT.TH1F(str(uuid.uuid4()) , str(uuid.uuid4()), hist_1d_sigmaIEIE_B.GetNbinsX(), hist_1d_sigmaIEIE_B.GetXaxis().GetXmin(), hist_1d_sigmaIEIE_B.GetXaxis().GetXmax())
    hist_norm_1d_sigmaIEIE_C = ROOT.TH1F(str(uuid.uuid4()) , str(uuid.uuid4()), hist_1d_sigmaIEIE_C.GetNbinsX(), hist_1d_sigmaIEIE_C.GetXaxis().GetXmin(), hist_1d_sigmaIEIE_C.GetXaxis().GetXmax())
    hist_norm_1d_sigmaIEIE_D = ROOT.TH1F(str(uuid.uuid4()) , str(uuid.uuid4()), hist_1d_sigmaIEIE_D.GetNbinsX(), hist_1d_sigmaIEIE_D.GetXaxis().GetXmin(), hist_1d_sigmaIEIE_D.GetXaxis().GetXmax())

    hist_1d_sigmaIEIE_B.Copy(hist_norm_1d_sigmaIEIE_B)
    hist_1d_sigmaIEIE_C.Copy(hist_norm_1d_sigmaIEIE_C)
    hist_1d_sigmaIEIE_D.Copy(hist_norm_1d_sigmaIEIE_D)
    ROOT.SetOwnership( hist_norm_1d_sigmaIEIE_B, False )
    ROOT.SetOwnership( hist_norm_1d_sigmaIEIE_C, False )
    ROOT.SetOwnership( hist_norm_1d_sigmaIEIE_D, False )

    hist_norm_1d_sigmaIEIE_B.Scale(1./hist_norm_1d_sigmaIEIE_B.Integral())
    hist_norm_1d_sigmaIEIE_C.Scale(1./hist_norm_1d_sigmaIEIE_C.Integral())
    hist_norm_1d_sigmaIEIE_D.Scale(1./hist_norm_1d_sigmaIEIE_D.Integral())
    hist_norm_1d_sigmaIEIE_B.SetLineColor(2)
    hist_norm_1d_sigmaIEIE_C.SetLineColor(1)
    hist_norm_1d_sigmaIEIE_D.SetLineColor(4)
    hist_norm_1d_sigmaIEIE_B.SetMarkerColor(2)
    hist_norm_1d_sigmaIEIE_C.SetMarkerColor(1)
    hist_norm_1d_sigmaIEIE_D.SetMarkerColor(4)
    hist_norm_1d_sigmaIEIE_B.Draw('l')
    hist_norm_1d_sigmaIEIE_C.Draw('same')
    hist_norm_1d_sigmaIEIE_D.Draw('same')

    #plot chIso in regions B, C, D
    can_1d_iso = ROOT.TCanvas( str(uuid.uuid4()), '' )
    can_1d_iso.SetLogy()

    hist_norm_1d_chIso_B = ROOT.TH1F(str(uuid.uuid4()) , str(uuid.uuid4()), hist_1d_chIso_B.GetNbinsX(), hist_1d_chIso_B.GetXaxis().GetXmin(), hist_1d_chIso_B.GetXaxis().GetXmax())
    hist_norm_1d_chIso_C = ROOT.TH1F(str(uuid.uuid4()) , str(uuid.uuid4()), hist_1d_chIso_C.GetNbinsX(), hist_1d_chIso_C.GetXaxis().GetXmin(), hist_1d_chIso_C.GetXaxis().GetXmax())
    hist_norm_1d_chIso_D = ROOT.TH1F(str(uuid.uuid4()) , str(uuid.uuid4()), hist_1d_chIso_D.GetNbinsX(), hist_1d_chIso_D.GetXaxis().GetXmin(), hist_1d_chIso_D.GetXaxis().GetXmax())

    hist_1d_chIso_B.Copy(hist_norm_1d_chIso_B)
    hist_1d_chIso_C.Copy(hist_norm_1d_chIso_C)
    hist_1d_chIso_D.Copy(hist_norm_1d_chIso_D)
    ROOT.SetOwnership( hist_norm_1d_chIso_B, False )
    ROOT.SetOwnership( hist_norm_1d_chIso_C, False )
    ROOT.SetOwnership( hist_norm_1d_chIso_D, False )

    hist_norm_1d_chIso_B.Scale(1./hist_norm_1d_chIso_B.Integral())
    hist_norm_1d_chIso_C.Scale(1./hist_norm_1d_chIso_C.Integral())
    hist_norm_1d_chIso_D.Scale(1./hist_norm_1d_chIso_D.Integral())
    hist_norm_1d_chIso_B.SetLineColor(2)
    hist_norm_1d_chIso_C.SetLineColor(1)
    hist_norm_1d_chIso_D.SetLineColor(4)
    hist_norm_1d_chIso_B.SetMarkerColor(2)
    hist_norm_1d_chIso_C.SetMarkerColor(1)
    hist_norm_1d_chIso_D.SetMarkerColor(4)
    hist_norm_1d_chIso_C.Draw('l')
    hist_norm_1d_chIso_B.Draw('same')
    hist_norm_1d_chIso_D.Draw('same')


    #---------------------------------------
    # Save the ratio histogram
    #---------------------------------------

    # canvas for ratio histogram
    can_ratio = ROOT.TCanvas( 'canv_ratio_%s' %(plot_var), 'canv_ratio_%s' %(plot_var) )
    #ROOT.SetOwnership( can_ratio, False )

    # ratio histogram = numerator histogram/denominator histogram
    ratiohist = ROOT.TH1F( 'ratio_%s' %(plot_var), 'ratio_%s' %(plot_var), hist_num.GetNbinsX(), hist_num.GetXaxis().GetXmin(), hist_num.GetXaxis().GetXmax())
    hist_num.Copy( ratiohist)
    ROOT.SetOwnership( ratiohist, False )

    print "DEBUG: first bin of num before div = ",ratiohist.GetBinContent(1)
    print "DEBUG: first bin of den before div = ",hist_den.GetBinContent(1)

    ratiohist.Divide( hist_den )
    print "DEBUG: first bin of ratio after div = ",ratiohist.GetBinContent(1)

    ratiohist.SetMarkerStyle(20)
    ratiohist.SetMarkerSize(1)
    ratiohist.GetXaxis().SetTitle( 'Transverse Mass [GeV]' )
    ratiohist.GetYaxis().SetTitle( 'ratio of passing to failing %s' %shape_var )
    ratiohist.Draw()
    ratiohist.SetStats(0)
    ratiohist.SetMinimum( 0) 
    ratiohist.SetMaximum( 5) 

    #ratio_func = ROOT.TF1( 'ratio_func_%s'%(str(uuid.uuid4())), '( [2]*TMath::Power(x/13000, [0] + [1]*TMath::Log10(x/13000) ) ) ', xmin, xmax )
    ratio_func = ROOT.TF1( 'ratio_func', '[0]', xmin, xmax )
    print 'DEBUG: ratio function = ',ratio_func
    ROOT.SetOwnership( ratio_func, False )
    print 'DEBUG: ratio function value at 200 GeV = ',ratio_func.Eval(200.)
    print 'DEBUG: ratio of integrals = ',hist_num.Integral()/hist_den.Integral()
    ratio_func.SetParameter(0, hist_num.Integral()/hist_den.Integral())
    #ratio_func.SetParameter(0, result_num[name_power_num].n - result_den[name_power_den].n)
    #ratio_func.SetParameter(1, result_num[name_logcoef_num].n  - result_den[name_logcoef_den].n )
    #ratio_func.SetParameter(2, norm_num / norm_den )
    ratiohist.Fit("ratio_func","RL","",xmin,xmax)
    print 'xmin and xmax are ',xmin,xmax
    f = ratiohist.GetFunction("ratio_func")
    fitvalue = ratio_func.GetParameter(0)
    print 'DEBUG: fitted straight line at ',fitvalue
    print 'DEBUG: chisquare of fit = ',f.GetChisquare()
    print 'DEBUG: ndof of fit = ',f.GetNDF()
    print 'DEBUG: testing f->GetParameter(0) = ',f.GetParameter(0)
    print 'DEBUG: testing f->GetParError(0) = ',f.GetParError(0)
    ratio_func.Draw('same')

    print 'DEBUG: prediction in A from B*C/D histograms = ',hist_shape.Integral(hist_shape.FindBin( xmin ), hist_shape.FindBin( xmax ))*hist_num.Integral(hist_num.FindBin( xmin ), hist_num.FindBin( xmax ))/hist_den.Integral(hist_den.FindBin( xmin ), hist_den.FindBin( xmax ))

    #ratio_power_v = val_power_num.getValV() - val_power_den.getValV()
    #ratio_power_e = math.sqrt(val_power_num.getErrorHi()*val_power_num.getErrorHi() + val_power_den.getErrorHi()*val_power_den.getErrorHi() )
    #ratio_logcoef_v = val_logcoef_num.getValV() - val_logcoef_den.getValV()
    #ratio_logcoef_e = math.sqrt(val_logcoef_num.getErrorHi()*val_logcoef_num.getErrorHi() + val_logcoef_den.getErrorHi()*val_logcoef_den.getErrorHi() )

    #power_tex   = ROOT.TLatex(0, 0, 'power = %.01f #pm %.02f' %( ratio_power_v,  ratio_power_e ))
    #ROOT.SetOwnership( power_tex, False )
    #logcoef_tex = ROOT.TLatex(0, 0, 'logcoef = %.01f #pm %.02f' %( ratio_logcoef_v,  ratio_logcoef_e ))
    #ROOT.SetOwnership( logcoef_tex, False )

    #power_tex.SetNDC()
    #logcoef_tex.SetNDC()

    #power_tex  .SetX( 0.6 )
    #power_tex  .SetY( 0.84 )
    #logcoef_tex.SetX( 0.6 )
    #logcoef_tex.SetY( 0.78 )

    #power_tex.Draw('same')
    #logcoef_tex.Draw('same')

    #sampMan.outputs['wjets_ABCD_%s' %suffix] = can_ABCD
    sampMan.outputs['%s_B_%s' %(sample,suffix)] = can_B
    sampMan.outputs['%s_C_%s' %(sample,suffix)] = can_C
    sampMan.outputs['%s_D_%s' %(sample,suffix)] = can_D

    sampMan.outputs['%s_sigmaIEIE_B_%s' %(sample,suffix)] = can_1d_sig_B
    sampMan.outputs['%s_chIso_B_%s' %(sample,suffix)] = can_1d_iso_B
    sampMan.outputs['%s_sigmaIEIE_C_%s' %(sample,suffix)] = can_1d_sig_C
    sampMan.outputs['%s_chIso_C_%s' %(sample,suffix)] = can_1d_iso_C
    sampMan.outputs['%s_sigmaIEIE_D_%s' %(sample,suffix)] = can_1d_sig_D
    sampMan.outputs['%s_chIso_D_%s' %(sample,suffix)] = can_1d_iso_D
    sampMan.outputs['%s_sigmaIEIE_BCD_%s' %(sample,suffix)] = can_1d_sig
    sampMan.outputs['%s_chIso_BCD_%s' %(sample,suffix)] = can_1d_iso


    if closure :
 #       sampMan.outputs['wjetsclosure_ratio_%s' %suffix] = can_ratio 
        sampMan.outputs['%sclosure_ratio_hist_%s' %(sample,suffix)] = ratiohist 
        sampMan.outputs['%sclosure_ratio_func_%s' %(sample,suffix)] = ratio_func
        #sampMan.outputs['wjetsclosure_ratio_power_%s' %suffix] = power_tex
        #sampMan.outputs['wjetsclosure_ratio_logcoef_%s' %suffix] = logcoef_tex

    else :
 #       sampMan.outputs['wjets_ratio_%s' %suffix] = can_ratio 
        sampMan.outputs['%s_ratio_hist_%s' %(sample,suffix)] = ratiohist 
        sampMan.outputs['%s_ratio_func_%s' %(sample,suffix)] = ratio_func 
        #sampMan.outputs['wjets_ratio_power_%s' %suffix] = power_tex
        #sampMan.outputs['wjets_ratio_logcoef_%s' %suffix] = logcoef_tex
    
    if closure :

        # canvas for signal region predictions
        can_sr = ROOT.TCanvas( 'canv_sig_%s_%s' %(sample,plot_var), 'canv_sig_%s_%s' %(sample,plot_var) )
        ROOT.SetOwnership( can_sr, False )

        hist_sr    = clone_sample_and_draw( sampMan, sample, plot_var, full_sel_sr , binning )
        ROOT.SetOwnership( hist_sr, False )
        hist_sr.GetXaxis().SetTitle( 'Transverse Mass [GeV]' )
        #hist_sr.GetYaxis().SetTitle( 'ratio of passing to failing %s' %shape_var ) ##really?
        hist_sr.GetYaxis().SetTitle( 'Events in signal region' )

        # fit a dijet2 function to region A
        fitMan_sr = FitManager( 'dijet', 3, sample, hist_sr, plot_var, eta_cut, xvar, suffix, useRooFit=False )
        result_sr  = fitMan_sr.fit_histogram( ws )

        # from histograms in MC regions B, C, and D
        # predicted number of sr events
        pred_val = (hist_integral_shape * hist_integral_num) / hist_integral_den 

        # from histogram in MC region A
        # predicted number of sr events
        tot_sr = hist_sr.Integral( hist_sr.FindBin( xmin ), hist_sr.FindBin( xmax ) )
        print 'DEBUG: SR prediction from BCD MC = ',pred_val
        print 'DEBUG: SR MC events from A       = ',tot_sr
        print 'SR integral = ', tot_sr

        # from functions fitted to regions BCD
        # predicted number of sr events
        sr_func = ROOT.TF1( 'sr_func', '( [2]*TMath::Power(x/13000, [0] + [1]*TMath::Log10(x/13000) ) ) ', xmin, xmax )
        #sr_func = ROOT.TF1( 'sr_func', '[3]*TMath::Power(x/13000, [0]+[1]*TMath::Log10(x/13000)+[2]*TMath::Log10(x/13000)*TMath::Log10(x/13000))', xmin, xmax )

        ROOT.SetOwnership( sr_func, False )
        sr_func.SetParameter(0, result_num[name_power_num].n + result_shape[name_power_shape].n - result_den[name_power_den].n )
        sr_func.SetParameter(1, result_num[name_logcoef_num].n + result_shape[name_logcoef_shape].n - result_den[name_logcoef_den].n )
        #sr_func.SetParameter(2, result_num[name_log2coef_num].n + result_shape[name_log2coef_shape].n - result_den[name_log2coef_den].n  )
        sr_func.SetParameter(2, 1)
        sr_int = sr_func.Integral( xmin, xmax )
        print 'normalizations (shape, num, den) = ',norm_shape,norm_num,norm_den
        #print 'Normalization = ',(norm_shape*norm_num) / norm_den
        print 'func Integral SR Before = ', sr_func.Integral( xmin, xmax )
        sr_func.SetParameter(2, pred_val/sr_int )
        print 'func Integral SR After = ', sr_func.Integral( xmin, xmax )
        print 'DEBUG: closure SR fit function value at 200 GeV = ',sr_func.Eval(200.)
        hist_sr.Draw()
        sr_func.Draw('same')

        sampMan.outputs['%sclosure_sr_pred_%s' %(sample,suffix)] = can_sr
        sampMan.outputs['%sclosure_srhist_%s' %(sample,suffix)] = hist_sr

        tot_ratio = result_num['integral']/result_den['integral']
        print 'DEBUG: result_num[integral]/result_den[integral] = ',tot_ratio

        hist_jetpt        = clone_sample_and_draw( sampMan, sample, jet_var, sel_base, jet_binning )
        hist_subjetpt     = clone_sample_and_draw( sampMan, sample, subjet_var, sel_base, subjet_binning )
        hist_trueht       = clone_sample_and_draw( sampMan, sample, ht_var, sel_base, ht_binning )

        sampMan.outputs['%sclosure_jetpt_%s' %(sample,suffix)] = hist_jetpt
        sampMan.outputs['%sclosure_subjetpt_%s' %(sample,suffix)] = hist_subjetpt
        sampMan.outputs['%sclosure_trueht_%s' %(sample,suffix)] = hist_trueht
Beispiel #12
0
def get_mc_fit(sampMan,
               sampnames,
               sel_base,
               eta_cuts,
               xvar,
               plot_var,
               binning,
               workspace,
               extra_label="Muon Channel",
               suffix='',
               plots_dir="plots"):

    if not os.path.isdir(plots_dir):
        os.makedirs(plots_dir)

    print "\n *****************\n calling get_mc_fit for, ", tPurple % (
        sampnames), "\n *********************\n"

    xmin = xvar.getMin()
    xmax = xvar.getMax()

    results = {}

    full_sel_sr = sel_base.replace(
        "ADDITION",
        " (%s > %d && %s < %d )" % (plot_var, xmin, plot_var, xmax))
    print full_sel_sr

    outname = "Backgrounds"

    if isinstance(sampnames, str):
        outname = sampnames
        sampnames = [sampnames]
        #hist_sr    = clone_sample_and_draw( sampMan, sampname, plot_var, full_sel_sr   , binning )

    print sampnames

    # list of samples
    hist_sr = clone_sample_and_draw(sampMan, sampnames[0], plot_var,
                                    full_sel_sr, binning)
    for isamp in sampnames[1:]:
        hist_sr.Add(
            clone_sample_and_draw(sampMan, isamp, plot_var, full_sel_sr,
                                  binning))

    print " **** sampname %s number of total events %f **********" % (
        outname, hist_sr.Integral(0, 100000))
    print " **** sampname %s number of events %f **********" % (
        outname, hist_sr.Integral())

    ieta = "EB"

    fitfunc = "expow"
    label = '%s_%s_%s_%s' % (outname, suffix, ieta, fitfunc)

    #fitManager = FitManager( 'dijet', 3, sampnames, hist_sr, plot_var, ieta, xvar, label, False)
    if fitfunc == "dijet":
        fitManager = FitManager('dijet',
                                hist=hist_sr,
                                xvardata=xvar,
                                label=label,
                                norders=2)
    #fitManager = FitManager( 'power', 2, sampnames, hist_sr, plot_var, ieta, xvar, label, options.useRooFit)
    if fitfunc == "power":
        fitManager = FitManager('power', hist_sr, xvar, label, norders=1)
    if fitfunc == "expow":
        fitManager = FitManager('expow', hist_sr, xvar, label, norders=1)
    if fitfunc == "vvdijet":
        fitManager = FitManager('vvdijet', hist_sr, xvar, label, norders=1)
    if fitfunc == "atlas":
        fitManager = FitManager('atlas', hist_sr, xvar, label, norders=1)
    canv = fitManager.draw(paramlayout=(0.7, 0.5, 0.82),
                           useOldsetup=True,
                           logy=1,
                           yrange=(5e-3, 2e4))

    #fitManager.setup_fit()
    fitManager.setup_rootfit((xmin, xmax))
    fitManager.func.Draw("same")
    canv.Print("%s/%sbefore.pdf" % (plots_dir, label))
    canv.Print("%s/%sbefore.png" % (plots_dir, label))
    canv.Print("%s/%sbefore.C" % (plots_dir, label))
    fitManager.run_rootfit()

    #fit_distribution( fitManager, sampMan, workspace, logy=True )
    #fitManager.make_func_pdf()
    #fitManager.fit_histogram( workspace )
    """ cast it from TF1 to RooGenericPdf """
    fitManager.calculate_func_pdf()
    fitManager.get_results(workspace)
    #results[ieta] = save_distribution( fitManager, sampMan, workspace, logy=True )
    #fitManager.save_fit( sampMan, workspace, logy = True, stats_pos='right', extra_label = extra_label)
    canv = fitManager.draw(subplot="pull",
                           paramlayout=(0.7, 0.5, 0.82),
                           useOldsetup=True,
                           logy=1,
                           yrange=(5e-3, 2e4))
    canv.Print("%s/%s.pdf" % (plots_dir, label))
    canv.Print("%s/%s.png" % (plots_dir, label))
    canv.Print("%s/%s.C" % (plots_dir, label))

    return results