Ejemplo n.º 1
0
def main(options,args) :
    
    print 'Loading c++...'
    anaplot.LoadRootCore()
    ROOT.gROOT.SetBatch(False)

    plotfunc.SetupStyle()
    ROOT.gStyle.SetPaintTextFormat("4.3f ")
    
    #
    # Draw plots
    #
    print 'Drawing the plots'
    cans = []
    for v in options.variables.split(',') :
        if not v : continue
        xlabel = options.histformat[v][3]
        n,low,high = options.histformat[v][:3]

        bkg_hists = []
        sig_hists = []
        data_hist = None

        rebin = []
        if hasattr(options.usermodule,'rebin') and v in options.usermodule.rebin.keys() :
            rebin = options.usermodule.rebin[v]

        if options.file :
            bkg_processes = options.processes.replace('data','').replace(',,',',')
            bkg_hists = anaplot.GetPassEventBkgHistos(v,options.key,bkg_processes,options.file,normalize=options.normalize,rebin=rebin,n=n,low=low,high=high)
            anaplot.PrepareBkgHistosForStack(bkg_hists)
            if 'data' in options.processes :
                data_hist = anaplot.GetPassEventBkgHistos(v,options.key,'data',options.file,normalize=options.normalize,rebin=rebin,n=n,low=low,high=high)[0]
                #data_hist = anaplot.GetPassEventBkgHistos(v,'FFTool_z_mccl_ltt_allNum','zjetdd',options.file,normalize=options.normalize,rebin=rebin,n=n,low=low,high=high)[0]

                
        if options.susy :
            sig_hists = anaplot.GetPassEventSigHistos(v,options.key,options.susy,normalize=options.normalize,rebin=rebin)


        cans.append(anaplot.DrawHistos('%s_%s'%(options.key,v),v,xlabel,bkg_hists,sig_hists,data_hist=data_hist,dostack=options.stack,log=options.log,ratio=options.ratio,fb=options.fb))
        taxisfunc.SetXaxisRanges(cans[-1],low,high)

    #
    # Cutflow
    #
    f = ROOT.TFile(options.file)
    if f.IsZombie() :
        print 'exiting'
        return
    PrintCutflow(options.key
                 ,f.Get('PassEvent_%s/PassEvent_%s_PassW'%(options.key,options.key))
                 ,samp_list=options.processes.split(',')+['all']
                 ,scientific=True)
    f.Close()

    raw_input('Pausing. Press enter to exit.')
            
    if options.save :
        for can in cans :
            can.Print(can.GetName()+'.pdf')
            can.Print(can.GetName()+'.eps')

    print 'done.'
    return
Ejemplo n.º 2
0
def main(options, args):

    plotfunc.SetupStyle()

    for i in options.histformat.keys():
        print '\'%s\':' % (i), options.histformat[i]

    anaplot.LoadRootCore()

    files_b, trees_b, keys_b, cf_b = anaplot.GetTreesFromFiles(options.bkgs)
    files_s, trees_s, keys_s, cf_s = anaplot.GetTreesFromFiles(options.susy)
    file_d, tree_d, key_d, cf_d = anaplot.GetTreesFromFiles(options.data)

    options.fb, lumi_scale_factor = helpers.GetTTreeLumiScaleFactor(
        files_b + files_s, options.fb)

    dweight = ''
    weight = 'TotalWeightNoSF*lepSFRecoIdIso[lep0_index]*lepSFRecoIdIso[lep1_index]*lepSFRecoIdIso[lep2_index]*EvtTrigSF*AntiIDSF_lep3*PassEvent_SMWZsig'
    #weight = 'TotalWeightNoPupNoSF'
    if ''.join(options.cuts):
        weight = weight + '*(%s)' % (' && '.join(options.cuts))
        dweight = '(' + ' && '.join(options.cuts) + ')'
    if hasattr(options.usermodule, 'weight'):
        weight += '*(%s)' % (options.usermodule.weight)
    if hasattr(options.usermodule, 'dataweight'):
        dweight += '*(%s)' % (options.usermodule.dataweight)

    cans = []

    # get the histograms from the files
    for vi, v1 in enumerate(options.variables.split(',')):
        n1, low1, high1 = options.histformat[v1][:3]
        xlabel1 = options.histformat[v1][3]

        rebin1 = []
        if hasattr(options.usermodule,
                   'rebin') and v1 in options.usermodule.rebin.keys():
            rebin1 = options.usermodule.rebin[v1]

        for vj, v2 in enumerate(options.variables.split(',')):
            if v2 == v1: continue
            if vj > vi: continue

            n2, low2, high2 = options.histformat[v2][:3]
            xlabel2 = options.histformat[v2][3]

            rebin2 = []
            if hasattr(options.usermodule,
                       'rebin') and v2 in options.usermodule.rebin.keys():
                rebin2 = options.usermodule.rebin[v2]

            bkg_hists = []
            sig_hists = []
            data_hist = None

            if options.bkgs:
                bkg_hists = anaplot.Get2dVariableHistsFromTrees(
                    trees_b,
                    keys_b,
                    v1,
                    v2,
                    weight,
                    n1,
                    low1,
                    high1,
                    n2,
                    low2,
                    high2,
                    normalize=options.normalize,
                    rebin1=rebin1,
                    rebin2=rebin2,
                    scale=lumi_scale_factor)
                anaplot.PrepareBkgHistosForStack(bkg_hists)
                for i, h in enumerate(bkg_hists):
                    canname = '%s_%s_%s' % (v1, v2, keys_b[i])
                    cans.append(ROOT.TCanvas(canname, canname, 500, 500))
                    h.SetMinimum(-0.00001)
                    plotfunc.AddHistogram(cans[-1], h, drawopt='colz')
                    plotfunc.FormatCanvasAxes(cans[-1])
                    plotfunc.SetAxisLabels(cans[-1], xlabel1, xlabel2)
            if options.susy:
                sig_hists = anaplot.Get2dVariableHistsFromTrees(
                    trees_s,
                    keys_s,
                    v1,
                    v2,
                    weight,
                    n1,
                    low1,
                    high1,
                    n2,
                    low2,
                    high2,
                    normalize=options.normalize,
                    rebin1=rebin1,
                    rebin2=rebin2,
                    scale=lumi_scale_factor)
                for i, h in enumerate(sig_hists):
                    canname = '%s_%s_%s' % (v1, v2, keys_s[i])
                    cans.append(ROOT.TCanvas(canname, canname, 500, 500))
                    h.SetMinimum(-0.00001)
                    plotfunc.AddHistogram(cans[-1], h, drawopt='colz')
                    plotfunc.FormatCanvasAxes(cans[-1])
                    plotfunc.SetAxisLabels(cans[-1], xlabel1, xlabel2)

            if options.data:
                data_hist = anaplot.Get2dVariableHistsFromTrees(
                    tree_d,
                    key_d,
                    v1,
                    v2,
                    dweight,
                    n1,
                    low1,
                    high1,
                    n2,
                    low2,
                    high2,
                    normalize=options.normalize,
                    rebin1=rebin1,
                    rebin2=rebin2,
                    scale=lumi_scale_factor)[0]
                data_hist.SetLineWidth(2)
                data_hist.SetLineColor(1)
                data_hist.SetMarkerColor(1)
                canname = '%s_%s_%s' % (v1, v2, 'data')
                cans.append(ROOT.TCanvas(canname, canname, 500, 500))
                h.SetMinimum(-0.00001)
                plotfunc.AddHistogram(cans[-1], h, drawopt='colz')
                plotfunc.FormatCanvasAxes(cans[-1])
                plotfunc.SetAxisLabels(cans[-1], xlabel1, xlabel2)

            cans.append(
                anaplot.DrawHistos(v,
                                   v,
                                   xlabel,
                                   bkg_hists,
                                   sig_hists,
                                   data_hist,
                                   dostack=options.stack,
                                   log=options.log,
                                   ratio=options.ratio,
                                   fb=options.fb))
    if not options.batch:
        raw_input('Press enter to exit')

    if options.save:
        for can in cans:
            can.Print(can.GetName() + '.pdf')

    print 'done.'
    return
Ejemplo n.º 3
0
def main(options, args):

    print 'Loading c++...'
    anaplot.LoadRootCore()
    ROOT.gROOT.SetBatch(False)

    plotfunc.SetupStyle()

    options.keys = []
    if not options.e:
        options.keys += [
            options.key + '_ttt_MuNum', options.key + '_ltt_MuDen'
        ]
    if not options.mu:
        options.keys += [
            options.key + '_ttt_EleNum', options.key + '_ltt_EleDen'
        ]

    print options.keys

    #
    # Draw plots
    #
    print 'Drawing the plots'
    cans = []
    for v in options.variables.split(','):
        if not v: continue
        n, low, high = options.histformat[v][:3]

        lep_pt_dijet_num = None
        lep_pt_dijet_den = None

        for k in options.keys:

            bkg_hists = []
            sig_hists = []
            data_hist = None

            rebin = []
            if hasattr(options.usermodule,
                       'rebin') and v in options.usermodule.rebin.keys():
                rebin = options.usermodule.rebin[v]
                if ('lepPt' in v) and ('Mu' in k):
                    rebin = [0, 9, 15, 20, 30, 100]

            if options.file:
                bkg_processes = options.processes.replace('data', '').replace(
                    ',,', ',')
                bkg_hists = anaplot.GetPassEventBkgHistos(
                    v,
                    k,
                    bkg_processes,
                    options.file,
                    normalize=options.normalize,
                    rebin=rebin,
                    n=n,
                    low=low,
                    high=high)
                anaplot.PrepareBkgHistosForStack(bkg_hists)
                if 'data' in options.processes:
                    data_hist = anaplot.GetPassEventBkgHistos(
                        v,
                        k,
                        'data',
                        options.file,
                        normalize=options.normalize,
                        rebin=rebin,
                        n=n,
                        low=low,
                        high=high)[0]

            xlabel = ROOT.PSL.GetXaxisLabel(v.split('[')[0])
            cans.append(
                anaplot.DrawHistos('%s_%s' % (k, v),
                                   v,
                                   xlabel,
                                   bkg_hists,
                                   sig_hists,
                                   data_hist=data_hist,
                                   dostack=options.stack,
                                   log=options.log,
                                   ratio=options.ratio,
                                   fb=options.fb))
            taxisfunc.SetXaxisRanges(cans[-1], low, high)

    #
    # Specifically get the fake factor in pt:
    #
    cans_divide = []

    v = 'lepPt[lepW_index]'
    rebin_mu = [0, 9, 15, 20, 30, 100]
    rebin_e = [0, 9, 15, 20, 30, 50, 100]

    hists_MuNum = dict()
    hists_MuDen = dict()
    hists_EleNum = dict()
    hists_EleDen = dict()

    mu_options = options.file, options.normalize, rebin_mu
    e_options = options.file, options.normalize, rebin_e

    for p in options.processes.split(','):
        hists_MuNum[p] = anaplot.GetPassEventBkgHistos(
            v, options.key + '_ttt_MuNum', p, *mu_options)[0]
        hists_MuDen[p] = anaplot.GetPassEventBkgHistos(
            v, options.key + '_ltt_MuDen', p, *mu_options)[0]
        hists_EleNum[p] = anaplot.GetPassEventBkgHistos(
            v, options.key + '_ttt_EleNum', p, *e_options)[0]
        hists_EleDen[p] = anaplot.GetPassEventBkgHistos(
            v, options.key + '_ltt_EleDen', p, *e_options)[0]

    z_w = ''
    tmp = ROOT.TFile(options.file)
    syst_check = 'PassEvent_FFTool_forsyst_%s_ttt_MuNum/PassEvent_FFTool_forsyst_%s_ttt_MuNum_zjet_lepPt_antilep0_index' % (
        'wid', 'wid')
    print syst_check
    if tmp.Get(syst_check):
        z_w = 'wid'
    elif tmp.Get(syst_check.replace('wid', 'zid')):
        z_w = 'zid'

    if z_w and options.syst:
        do_z = (z_w == 'zid')
        # try to get SR FF
        hists_MuNum['zjet_sr'] = anaplot.GetPassEventBkgHistos(
            'lepPt_antilep0_index', 'FFTool_forsyst_%s_ttt_MuNum' % (z_w),
            'zjet', *mu_options)[0]
        if True:
            hists_MuDen['zjet_sr'] = anaplot.GetPassEventBkgHistos(
                'lepPt_lepW_index', 'FFTool_forsyst_ltt_MuDen', 'zjet',
                *mu_options)[0]
        if do_z:
            hists_MuDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepleadZ_index',
                                              'FFTool_forsyst_tlt_MuDen',
                                              'zjet', *mu_options)[0])
        if do_z:
            hists_MuDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepsubleadZ_index',
                                              'FFTool_forsyst_ttl_MuDen',
                                              'zjet', *mu_options)[0])

        hists_MuNum['zjet_sr'].Add(
            anaplot.GetPassEventBkgHistos(
                'lepPt_antilep0_index', 'FFTool_forsyst_%s_ttt_MuNum' % (z_w),
                'zgam', *mu_options)[0])
        if True:
            hists_MuDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepW_index',
                                              'FFTool_forsyst_ltt_MuDen',
                                              'zgam', *mu_options)[0])
        if do_z:
            hists_MuDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepleadZ_index',
                                              'FFTool_forsyst_tlt_MuDen',
                                              'zgam', *mu_options)[0])
        if do_z:
            hists_MuDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepsubleadZ_index',
                                              'FFTool_forsyst_ttl_MuDen',
                                              'zgam', *mu_options)[0])

        hists_EleNum['zjet_sr'] = anaplot.GetPassEventBkgHistos(
            'lepPt_antilep0_index', 'FFTool_forsyst_%s_ttt_EleNum' % (z_w),
            'zjet', *e_options)[0]
        if True:
            hists_EleDen['zjet_sr'] = anaplot.GetPassEventBkgHistos(
                'lepPt_lepW_index', 'FFTool_forsyst_ltt_EleDen', 'zjet',
                *e_options)[0]
        if do_z:
            hists_EleDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepleadZ_index',
                                              'FFTool_forsyst_tlt_EleDen',
                                              'zjet', *e_options)[0])
        if do_z:
            hists_EleDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepsubleadZ_index',
                                              'FFTool_forsyst_ttl_EleDen',
                                              'zjet', *e_options)[0])

        hists_EleNum['zjet_sr'].Add(
            anaplot.GetPassEventBkgHistos(
                'lepPt_antilep0_index', 'FFTool_forsyst_%s_ttt_EleNum' % (z_w),
                'zgam', *e_options)[0])
        if True:
            hists_EleDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepW_index',
                                              'FFTool_forsyst_ltt_EleDen',
                                              'zgam', *e_options)[0])
        if do_z:
            hists_EleDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepleadZ_index',
                                              'FFTool_forsyst_tlt_EleDen',
                                              'zgam', *e_options)[0])
        if do_z:
            hists_EleDen['zjet_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepsubleadZ_index',
                                              'FFTool_forsyst_ttl_EleDen',
                                              'zgam', *e_options)[0])

        # now for ttbar
        hists_MuNum['ttbar_sr'] = anaplot.GetPassEventBkgHistos(
            'lepPt_antilep0_index', 'FFTool_forsyst_%s_ttt_MuNum' % (z_w),
            'ttbar', *mu_options)[0]
        if True:
            hists_MuDen['ttbar_sr'] = anaplot.GetPassEventBkgHistos(
                'lepPt_lepW_index', 'FFTool_forsyst_ltt_MuDen', 'ttbar',
                *mu_options)[0]
        if do_z:
            hists_MuDen['ttbar_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepleadZ_index',
                                              'FFTool_forsyst_tlt_MuDen',
                                              'ttbar', *mu_options)[0])
        if do_z:
            hists_MuDen['ttbar_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepsubleadZ_index',
                                              'FFTool_forsyst_ttl_MuDen',
                                              'ttbar', *mu_options)[0])

        hists_EleNum['ttbar_sr'] = anaplot.GetPassEventBkgHistos(
            'lepPt_antilep0_index', 'FFTool_forsyst_%s_ttt_EleNum' % (z_w),
            'ttbar', *e_options)[0]
        if True:
            hists_EleDen['ttbar_sr'] = anaplot.GetPassEventBkgHistos(
                'lepPt_lepW_index', 'FFTool_forsyst_ltt_EleDen', 'ttbar',
                *e_options)[0]
        if do_z:
            hists_EleDen['ttbar_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepleadZ_index',
                                              'FFTool_forsyst_tlt_EleDen',
                                              'ttbar', *e_options)[0])
        if do_z:
            hists_EleDen['ttbar_sr'].Add(
                anaplot.GetPassEventBkgHistos('lepPt_lepsubleadZ_index',
                                              'FFTool_forsyst_ttl_EleDen',
                                              'ttbar', *e_options)[0])

        hists_MuNum['zjet_sr'].SetName('zjet_sr')
        hists_MuNum['zjet_sr'].SetTitle('zjet+z#gamma SR FF')
        hists_EleNum['zjet_sr'].SetName('zjet_sr')
        hists_EleNum['zjet_sr'].SetTitle('zjet+z#gamma SR FF')

        if False:
            hists_MuDen['zjet_sr'].Add(hists_MuDen['ttbar_sr'])
            hists_EleDen['zjet_sr'].Add(hists_EleDen['ttbar_sr'])
            hists_MuNum['zjet_sr'].Add(hists_MuNum['ttbar_sr'])
            hists_EleNum['zjet_sr'].Add(hists_EleNum['ttbar_sr'])
            hists_MuNum['zjet_sr'].SetTitle('zjet+z#gamma+ttbar SR FF')
            hists_EleNum['zjet_sr'].SetTitle('zjet+z#gamma+ttbar SR FF')

        hists_MuNum['zjet_sr'].Divide(hists_MuDen['zjet_sr'])
        hists_EleNum['zjet_sr'].Divide(hists_EleDen['zjet_sr'])

        hists_MuNum['ttbar_sr'].SetName('ttbar_sr')
        hists_MuNum['ttbar_sr'].SetTitle('ttbar SR FF')
        hists_EleNum['ttbar_sr'].SetName('ttbar_sr')
        hists_EleNum['ttbar_sr'].SetTitle('ttbar SR FF')
        hists_MuNum['ttbar_sr'].Divide(hists_MuDen['ttbar_sr'])
        hists_EleNum['ttbar_sr'].Divide(hists_EleDen['ttbar_sr'])

    print hists_MuNum.keys()

    if (options.region == 'dijet') and 'dijet' in hists_EleNum.keys():
        hists_MuNum['dijet'].SetLineColor(ROOT.kBlue + 1)
        hists_MuNum['dijet'].SetMarkerColor(ROOT.kBlue + 1)
        hists_EleNum['dijet'].SetLineColor(ROOT.kBlue + 1)
        hists_EleNum['dijet'].SetMarkerColor(ROOT.kBlue + 1)

        hists_MuNum['dijet'].Divide(hists_MuDen['dijet'])
        hists_EleNum['dijet'].Divide(hists_EleDen['dijet'])

    if (options.region == 'zjet') and 'zjet' in hists_EleNum.keys():

        hists_MuNum['zjet'].SetTitle('zjet (no z#gamma) MC')
        hists_EleNum['zjet'].SetTitle('zjet (no z#gamma) MC')

        if 'zgam' in hists_EleNum.keys():
            print '##############################################################################'
            print '## Adding zgam to your estimate'
            print '##############################################################################'
            hists_MuNum['zjet'].Add(hists_MuNum['zgam'])
            hists_EleNum['zjet'].Add(hists_EleNum['zgam'])

            hists_MuNum['zjet'].SetTitle('zjet+z#gamma MC')
            hists_EleNum['zjet'].SetTitle('zjet+z#gamma MC')

        hists_MuNum['zjet'].SetLineColor(ROOT.kBlue + 1)
        hists_MuNum['zjet'].SetMarkerColor(ROOT.kBlue + 1)
        hists_EleNum['zjet'].SetLineColor(ROOT.kBlue + 1)
        hists_EleNum['zjet'].SetMarkerColor(ROOT.kBlue + 1)

        hists_MuNum['zjet'].Divide(hists_MuDen['zjet'])
        hists_EleNum['zjet'].Divide(hists_EleDen['zjet'])


#     cans.append(ROOT.TCanvas('%s_Mu_ff_dijet'%(v),'%s_Mu_ff'%(v),500,500))
#     plotfunc.AddHistogram(cans[-1],dijet_hist_MuNum)
#     plotfunc.FormatCanvasAxes(cans[-1])
#     taxisfunc.SetYaxisRanges(cans[-1],0,0.5)

#     cans.append(ROOT.TCanvas('%s_Ele_ff_dijet'%(v),'%s_Ele_ff'%(v),500,500))
#     plotfunc.AddHistogram(cans[-1],dijet_hist_EleNum)
#     plotfunc.FormatCanvasAxes(cans[-1])
#     taxisfunc.SetYaxisRanges(cans[-1],0,0.5)

    if 'data' in hists_EleNum.keys():
        hists_EleNum['data'].SetTitle('With VV subtraction')
        hists_EleDen['data'].SetTitle('With VV subtraction')
        hists_MuNum['data'].SetTitle('With VV subtraction')
        hists_MuDen['data'].SetTitle('With VV subtraction')

        if (options.region == 'dijet'):
            hists_EleNum['data_bws'] = hists_EleNum['data'].Clone()
            hists_EleNum['data_bws'].SetName(
                hists_EleNum['data_bws'].GetName() + '_bws')
            hists_EleDen['data_bws'] = hists_EleDen['data'].Clone()
            hists_EleDen['data_bws'].SetName(
                hists_EleDen['data_bws'].GetName() + '_bws')
            hists_MuNum['data_bws'] = hists_MuNum['data'].Clone()
            hists_MuNum['data_bws'].SetName(hists_MuNum['data_bws'].GetName() +
                                            '_bws')
            hists_MuDen['data_bws'] = hists_MuDen['data'].Clone()
            hists_MuDen['data_bws'].SetName(hists_MuDen['data_bws'].GetName() +
                                            '_bws')

            hists_EleNum['data_bws'].SetTitle('No EW subtraction')
            hists_EleDen['data_bws'].SetTitle('No EW subtraction')
            hists_MuNum['data_bws'].SetTitle('No EW subtraction')
            hists_MuDen['data_bws'].SetTitle('No EW subtraction')

            hists_EleNum['data_bzs'] = hists_EleNum['data_bws'].Clone()
            hists_EleNum['data_bzs'].SetName(
                hists_EleNum['data_bzs'].GetName() + '_bzs')
            hists_EleDen['data_bzs'] = hists_EleDen['data_bws'].Clone()
            hists_EleDen['data_bzs'].SetName(
                hists_EleDen['data_bzs'].GetName() + '_bzs')
            hists_MuNum['data_bzs'] = hists_MuNum['data_bws'].Clone()
            hists_MuNum['data_bzs'].SetName(hists_MuNum['data_bzs'].GetName() +
                                            '_bzs')
            hists_MuDen['data_bzs'] = hists_MuDen['data_bws'].Clone()
            hists_MuDen['data_bzs'].SetName(hists_MuDen['data_bzs'].GetName() +
                                            '_bzs')

            hists_EleNum['data_bzs'].SetTitle('With W subtraction')
            hists_EleDen['data_bzs'].SetTitle('With W subtraction')
            hists_MuNum['data_bzs'].SetTitle('With W subtraction')
            hists_MuDen['data_bzs'].SetTitle('With W subtraction')

            for i in ['wje', 'wjm', 'wjt', 'zjee', 'zjmm', 'zjtt']:
                if i in hists_EleNum.keys():
                    print 'Subtracting', i
                    hists_EleNum['data'].Add(hists_EleNum[i], -1)
                    hists_EleDen['data'].Add(hists_EleDen[i], -1)
                    hists_MuNum['data'].Add(hists_MuNum[i], -1)
                    hists_MuDen['data'].Add(hists_MuDen[i], -1)
                    if i not in ['zjee', 'zjmm', 'zjtt']:
                        hists_EleNum['data_bzs'].Add(hists_EleNum[i], -1)
                        hists_EleDen['data_bzs'].Add(hists_EleDen[i], -1)
                        hists_MuNum['data_bzs'].Add(hists_MuNum[i], -1)
                        hists_MuDen['data_bzs'].Add(hists_MuDen[i], -1)

            hists_MuNum['data_bws'].Divide(hists_MuDen['data_bws'])
            hists_EleNum['data_bws'].Divide(hists_EleDen['data_bws'])

            hists_MuNum['data_bzs'].Divide(hists_MuDen['data_bzs'])
            hists_EleNum['data_bzs'].Divide(hists_EleDen['data_bzs'])

        if (options.region == 'zjet'):
            hists_EleNum['data_bvvs'] = hists_EleNum['data'].Clone()
            hists_EleNum['data_bvvs'].SetName(
                hists_EleNum['data_bvvs'].GetName() + '_bvvs')
            hists_EleDen['data_bvvs'] = hists_EleDen['data'].Clone()
            hists_EleDen['data_bvvs'].SetName(
                hists_EleDen['data_bvvs'].GetName() + '_bvvs')
            hists_MuNum['data_bvvs'] = hists_MuNum['data'].Clone()
            hists_MuNum['data_bvvs'].SetName(
                hists_MuNum['data_bvvs'].GetName() + '_bvvs')
            hists_MuDen['data_bvvs'] = hists_MuDen['data'].Clone()
            hists_MuDen['data_bvvs'].SetName(
                hists_MuDen['data_bvvs'].GetName() + '_bvvs')

            hists_EleNum['data_bvvs'].SetTitle('No VV subtraction')
            hists_EleDen['data_bvvs'].SetTitle('No VV subtraction')
            hists_MuNum['data_bvvs'].SetTitle('No VV subtraction')
            hists_MuDen['data_bvvs'].SetTitle('No VV subtraction')

            # 15% systematic on vv subtraction
            hists_EleNum['data_vv15sys'] = hists_EleNum['data'].Clone()
            hists_EleNum['data_vv15sys'].SetName(
                hists_EleNum['data_vv15sys'].GetName() + '_vv15sys')
            hists_EleDen['data_vv15sys'] = hists_EleDen['data'].Clone()
            hists_EleDen['data_vv15sys'].SetName(
                hists_EleDen['data_vv15sys'].GetName() + '_vv15sys')
            hists_MuNum['data_vv15sys'] = hists_MuNum['data'].Clone()
            hists_MuNum['data_vv15sys'].SetName(
                hists_MuNum['data_vv15sys'].GetName() + '_vv15sys')
            hists_MuDen['data_vv15sys'] = hists_MuDen['data'].Clone()
            hists_MuDen['data_vv15sys'].SetName(
                hists_MuDen['data_vv15sys'].GetName() + '_vv15sys')

            hists_EleNum['data_vv15sys'].SetTitle(
                '15% systematic on subtraction')
            hists_EleDen['data_vv15sys'].SetTitle(
                '15% systematic on subtraction')
            hists_MuNum['data_vv15sys'].SetTitle(
                '15% systematic on subtraction')
            hists_MuDen['data_vv15sys'].SetTitle(
                '15% systematic on subtraction')

            for i in ['wz', 'zz']:
                if i in hists_EleNum.keys():
                    print 'Subtracting', i
                    hists_EleNum['data'].Add(hists_EleNum[i], -1)
                    hists_EleDen['data'].Add(hists_EleDen[i], -1)
                    hists_MuNum['data'].Add(hists_MuNum[i], -1)
                    hists_MuDen['data'].Add(hists_MuDen[i], -1)

                    hists_EleNum['data_vv15sys'].Add(hists_EleNum[i],
                                                     -1 + 0.15)
                    hists_EleDen['data_vv15sys'].Add(hists_EleDen[i],
                                                     -1 + 0.15)
                    hists_MuNum['data_vv15sys'].Add(hists_MuNum[i], -1 + 0.15)
                    hists_MuDen['data_vv15sys'].Add(hists_MuDen[i], -1 + 0.15)

            hists_MuNum['data_bvvs'].Divide(hists_MuDen['data_bvvs'])
            hists_EleNum['data_bvvs'].Divide(hists_EleDen['data_bvvs'])

            hists_MuNum['data_vv15sys'].Divide(hists_MuDen['data_vv15sys'])
            hists_EleNum['data_vv15sys'].Divide(hists_EleDen['data_vv15sys'])

        hists_MuNum['data'].Divide(hists_MuDen['data'])
        hists_EleNum['data'].Divide(hists_EleDen['data'])

    f = ROOT.TFile('FakeFactors.root', 'recreate')
    f.cd()
    if 'data' in hists_EleNum.keys():
        if not options.e: hists_MuNum['data'].Write("Muons")
        if not options.mu: hists_EleNum['data'].Write("Electrons")
        if not options.e: hists_MuNum['data_vv15sys'].Write("Muons_wzsyst")
        if not options.mu:
            hists_EleNum['data_vv15sys'].Write("Electrons_wzsyst")
    f.Close()

    g = ROOT.TFile('FakeFactors_mcClosure.root', 'recreate')
    g.cd()
    if (options.region == 'zjet'):
        if not options.e: hists_MuNum['zjet'].Write('Muons')
        if not options.mu: hists_EleNum['zjet'].Write('Electrons')
    if (options.region == 'dijet'):
        if not options.e: hists_MuNum['dijet'].Write('Muons')
        if not options.mu: hists_EleNum['dijet'].Write('Electrons')
    g.Close()

    #
    # systematic band histogram
    #
    from array import array
    band = ROOT.TH1F('band', 'band', 5, array('d', [0, 15, 20, 30, 50, 100]))

    def SetSysts(h_band, systs):
        for i, sys in enumerate(systs):
            h_band.SetBinContent(i + 1, 1)
            h_band.SetBinError(i + 1, sys)

    band.SetFillColor(ROOT.kOrange - 0)
    band.SetMarkerSize(0)
    band.SetMarkerColor(ROOT.kOrange - 0)

    #
    # Muon fake factor plots
    #
    if not options.e:
        if 'zjet_sr' in hists_MuNum.keys():
            cans.append(
                plotfunc.RatioCanvas('lepPt_Mu_ff_systematic',
                                     '%s_Mu_ff_systematic' % (v), 500, 500))
        else:
            cans.append(ROOT.TCanvas('lepPt_Mu_ff', '%s_Mu_ff' % (v), 500,
                                     500))

        if 'data' in hists_EleNum.keys():

            if (options.region == 'dijet'):
                plotfunc.AddHistogram(cans[-1], hists_MuNum['data_bws'])
                plotfunc.AddHistogram(cans[-1], hists_MuNum['data_bzs'])
                if 'dijet' in hists_EleNum.keys():
                    plotfunc.AddHistogram(cans[-1], hists_MuNum['dijet'])

            if (options.region == 'zjet'):
                if not options.syst:
                    plotfunc.AddHistogram(cans[-1], hists_MuNum['data_bvvs'])
                #plotfunc.AddHistogram(cans[-1],hists_MuNum['data_vv15sys'])
                pass
            if not options.syst:
                plotfunc.AddHistogram(cans[-1], hists_MuNum['data'])

        if (options.region == 'zjet'):
            if 'zjet' in hists_MuNum.keys():
                plotfunc.AddHistogram(cans[-1], hists_MuNum['zjet'])
                if 'zjet_sr' in hists_MuNum.keys():
                    if z_w == 'zid':
                        SetSysts(band, [0.0, 0.25, 0.2, 0.25, 0.25])
                    else:
                        SetSysts(band, [0.0, 0.0, 0.2, 0.30, 0.30])
                    plotfunc.AddHistogram(cans[-1].GetPrimitive('pad_bot'),
                                          band, 'E2')
                    plotfunc.AddRatio(cans[-1], hists_MuNum['zjet_sr'],
                                      hists_MuNum['zjet'])
                    #plotfunc.AddRatio(cans[-1],hists_MuNum['ttbar_sr'],hists_MuNum['zjet'])

        if not plotfunc.CanvasEmpty(cans[-1]):
            plotfunc.FormatCanvasAxes(cans[-1])
            #taxisfunc.SetYaxisRanges(cans[-1],0,0.5)
            plotfunc.MakeLegend(cans[-1])
            additionaltext = ('supporting triggers'
                              if options.region == 'dijet' else None)
            plotfunc.FullFormatCanvasDefault(cans[-1],
                                             sqrts=13,
                                             lumi=options.fb,
                                             additionaltext=additionaltext)
            if 'zjet_sr' in hists_MuNum.keys():
                taxisfunc.SetYaxisRanges(cans[-1].GetPrimitive('pad_bot'), -1,
                                         3)
        plotfunc.SetAxisLabels(cans[-1], 'p_{T} [GeV]', 'fake factor')

    #
    # electron fake factor plots
    #
    if not options.mu:
        if 'zjet_sr' in hists_EleNum.keys():
            cans.append(
                plotfunc.RatioCanvas('lepPt_Ele_ff_systematic',
                                     '%s_Ele_ff_systematic' % (v), 500, 500))
        else:
            cans.append(
                ROOT.TCanvas('lepPt_Ele_ff', '%s_Ele_ff' % (v), 500, 500))

        if 'data' in hists_EleNum.keys():

            if (options.region == 'dijet'):
                plotfunc.AddHistogram(cans[-1], hists_EleNum['data_bws'])
                plotfunc.AddHistogram(cans[-1], hists_EleNum['data_bzs'])
                if 'dijet' in hists_EleNum.keys():
                    plotfunc.AddHistogram(cans[-1], hists_EleNum['dijet'])

            if (options.region == 'zjet'):
                if not options.syst:
                    plotfunc.AddHistogram(cans[-1], hists_EleNum['data_bvvs'])
                #plotfunc.AddHistogram(cans[-1],hists_EleNum['data_vv15sys'])

            if not options.syst:
                plotfunc.AddHistogram(cans[-1], hists_EleNum['data'])

        if (options.region == 'zjet'):
            if 'zjet' in hists_EleNum.keys():
                plotfunc.AddHistogram(cans[-1], hists_EleNum['zjet'])
                if 'zjet_sr' in hists_EleNum.keys():
                    if z_w == 'zid':
                        SetSysts(band, [0.0, 0.55, 0.30, 0.40, 0.60])
                    else:
                        SetSysts(band, [0.0, 0.0, 0.25, 0.25, 0.65])
                    plotfunc.AddHistogram(cans[-1].GetPrimitive('pad_bot'),
                                          band, 'E2')
                    plotfunc.AddRatio(cans[-1], hists_EleNum['zjet_sr'],
                                      hists_EleNum['zjet'])
                    #plotfunc.AddRatio(cans[-1],hists_EleNum['ttbar_sr'],hists_EleNum['zjet'])

        if not plotfunc.CanvasEmpty(cans[-1]):
            plotfunc.FormatCanvasAxes(cans[-1])
            #taxisfunc.SetYaxisRanges(cans[-1],0,0.5)
            plotfunc.MakeLegend(cans[-1])
            additionaltext = ('supporting triggers'
                              if options.region == 'dijet' else None)
            plotfunc.FullFormatCanvasDefault(cans[-1],
                                             sqrts=13,
                                             lumi=options.fb,
                                             additionaltext=additionaltext)
            if 'zjet_sr' in hists_EleNum.keys():
                taxisfunc.SetYaxisRanges(cans[-1].GetPrimitive('pad_bot'), -1,
                                         3)
        plotfunc.SetAxisLabels(cans[-1], 'p_{T} [GeV]', 'fake factor')

    raw_input('Pausing. Press enter to exit.')

    if options.save:
        for can in cans:
            can.Print(can.GetName() + '.pdf')
            can.Print(can.GetName() + '.eps')

    print 'done.'
    return
Ejemplo n.º 4
0
def main(options, args):

    plotfunc.SetupStyle()

    for i in options.histformat.keys():
        print '\'%s\':' % (i), options.histformat[i]

    anaplot.LoadRootCore()

    files_b, trees_b, keys_b, cf_b = anaplot.GetTreesFromFiles(options.bkgs)
    files_s, trees_s, keys_s, cf_s = anaplot.GetTreesFromFiles(options.susy)
    file_d, tree_d, key_d, cf_d = anaplot.GetTreesFromFiles(options.data)

    options.fb, lumi_scale_factor = helpers.GetTTreeLumiScaleFactor(
        files_b + files_s, options.fb)

    dweight = ''
    weight = 'TotalWeightNoSF'
    if ''.join(options.cuts):
        weight = weight + '*(%s)' % (' && '.join(options.cuts))
        dweight = '(' + ' && '.join(options.cuts) + ')'
    if hasattr(options.usermodule, 'weight'):
        weight += '*(%s)' % (options.usermodule.weight)
    if hasattr(options.usermodule, 'dataweight'):
        dweight += '*(%s)' % (options.usermodule.dataweight)

    cans = []

    # get the histograms from the files
    for v in options.variables.split(','):
        n, low, high = options.histformat[v][:3]
        xlabel = options.histformat[v][3]

        bkg_hists = []
        sig_hists = []
        data_hist = None

        rebin = []
        if hasattr(options.usermodule,
                   'rebin') and v in options.usermodule.rebin.keys():
            rebin = options.usermodule.rebin[v]

        if options.bkgs:
            bkg_hists = anaplot.GetVariableHistsFromTrees(
                trees_b,
                keys_b,
                v,
                weight,
                n,
                low,
                high,
                normalize=options.normalize,
                rebin=rebin,
                scale=lumi_scale_factor)
            anaplot.PrepareBkgHistosForStack(bkg_hists)
        if options.susy:
            sig_hists = anaplot.GetVariableHistsFromTrees(
                trees_s,
                keys_s,
                v,
                weight,
                n,
                low,
                high,
                normalize=options.normalize,
                rebin=rebin,
                scale=lumi_scale_factor)
        if options.data:
            data_hist = anaplot.GetVariableHistsFromTrees(
                tree_d,
                key_d,
                v,
                dweight,
                n,
                low,
                high,
                normalize=options.normalize,
                rebin=rebin,
                scale=lumi_scale_factor)[0]
            data_hist.SetLineWidth(2)
            data_hist.SetLineColor(1)
            data_hist.SetMarkerColor(1)

        nsignal = 0
        for si, s in enumerate(keys_s):
            mass_map = GetMassesMap()
            for i in mass_map.keys():
                if str(i) in sig_hists[si].GetName():
                    title = '%s,%s' % (mass_map[i]['mc1'], mass_map[i]['mn1'])
                    sig_hists[si].SetTitle(title)
            sig_hists[si].SetLineWidth(2)
            sig_hists[si].SetLineColor(signal_colors[nsignal])
            sig_hists[si].SetMarkerColor(signal_colors[nsignal])
            nsignal += 1

        cans.append(
            anaplot.DrawHistos(v,
                               v,
                               xlabel,
                               bkg_hists,
                               sig_hists,
                               data_hist,
                               dostack=options.stack,
                               log=options.log,
                               ratio=options.ratio,
                               fb=options.fb))

    if not options.batch:
        raw_input('Press enter to exit')

    if options.save:
        for can in cans:
            can.Print(can.GetName() + '.pdf')

    print 'done.'
    return
Ejemplo n.º 5
0
def main(options, args):

    plotfunc.SetupStyle()

    for i in options.histformat.keys():
        print '\'%s\':' % (i), options.histformat[i]

    anaplot.LoadRootCore()

    files_mc1, trees_mc1, keys_mc1, cf_mc1 = anaplot.GetTreesFromFiles(
        options.mc1)
    files_mc2, trees_mc2, keys_mc2, cf_mc2 = anaplot.GetTreesFromFiles(
        options.mc2)

    weight = 'TotalWeightNoSF'
    if ''.join(options.cuts):
        weight = weight + '*(%s)' % (' && '.join(options.cuts))
    if hasattr(options.usermodule, 'weight'):
        weight += '*(%s)' % (options.usermodule.weight)

    cans = []

    # get the histograms from the files
    for v in options.variables.split(','):
        n, low, high = options.histformat[v][:3]
        xlabel = options.histformat[v][3]

        mc_hists = []

        rebin = []
        if hasattr(options.usermodule,
                   'rebin') and v in options.usermodule.rebin.keys():
            rebin = options.usermodule.rebin[v]

        if options.mc1:
            mc_hists.append(
                anaplot.GetVariableHistsFromTrees(trees_mc1,
                                                  keys_mc1,
                                                  v,
                                                  weight,
                                                  n,
                                                  low,
                                                  high,
                                                  normalize=options.normalize,
                                                  rebin=rebin)[0])
            mc_hists[-1].SetTitle(options.label1)
            mc_hists[-1].SetLineWidth(2)
            mc_hists[-1].SetLineColor(1)
            mc_hists[-1].SetMarkerColor(1)
        if options.mc2:
            mc_hists.append(
                anaplot.GetVariableHistsFromTrees(trees_mc2,
                                                  keys_mc2,
                                                  v,
                                                  weight,
                                                  n,
                                                  low,
                                                  high,
                                                  normalize=options.normalize,
                                                  rebin=rebin)[0])
            mc_hists[-1].SetTitle(options.label2)
            mc_hists[-1].SetLineWidth(ROOT.kRed + 1)
            mc_hists[-1].SetLineColor(ROOT.kRed + 1)
            mc_hists[-1].SetMarkerColor(ROOT.kRed + 1)

        cans.append(
            anaplot.DrawHistos(v,
                               v,
                               xlabel,
                               mc_hists,
                               dostack=False,
                               log=options.log,
                               ratio=options.ratio,
                               fb=options.fb))

    if not options.batch:
        raw_input('Press enter to exit')

    if options.save:
        for can in cans:
            can.Print(can.GetName() + '.pdf')

    print 'done.'
    return