Exemple #1
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
Exemple #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'
    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
def main(options, args):

    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)
    all_cutflows = cf_b + cf_s + cf_d

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

    for i in all_cutflows:
        for j in i:
            j.Scale(lumi_scale_factor)

    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)

    cuts = []
    samples = []
    data_index = -1
    if options.bkgs:
        samples += list(
            a.replace('.root', '') for a in options.bkgs.split(','))
    samples += ['Total Bkg.']
    totbkg_index = len(samples) - 1
    if options.susy:
        samples += list(
            a.replace('.root', '') for a in options.susy.split(','))
    if options.data:
        samples += ['data']
        data_index = len(samples) - 1
    samples += ['s/b']
    s_over_b_index = len(samples) - 1
    samples += ['purity']
    purity_index = len(samples) - 1
    print samples

    #
    # Figure out what all the cuts were
    #
    firsthistlist = all_cutflows[0]
    for i in firsthistlist:
        for y in range(i.GetNbinsY(), 0, -1):
            cuts.append(i.GetYaxis().GetBinLabel(y))

    if hasattr(options.usermodule, 'cutflow'):
        for i in options.usermodule.cutflow:
            cuts.insert(0, i[0])

    cuts.reverse()
    print cuts

    #
    # Make a histogram to fill
    #
    cutflow_hist = ROOT.TH2F('Cutflow', 'Cutflow', len(samples), 0,
                             len(samples), len(cuts), 0, len(cuts))
    for i in range(len(samples)):
        cutflow_hist.GetXaxis().SetBinLabel(i + 1, samples[i])
    for i in range(len(cuts)):
        cutflow_hist.GetYaxis().SetBinLabel(i + 1, cuts[i])

    #
    # Grab the bin content from the "all" histograms that were saved in each file. (bkgs)
    #
    current_sample = 1
    for i, histlist in enumerate(cf_b):
        current_cut = len(cuts)
        if hasattr(options.usermodule, 'cutflow'):
            tree_cuts_list = list('(' + a[1] + ')'
                                  for a in options.usermodule.cutflow)
            for j in options.usermodule.cutflow:
                tmp_weight = weight + '*(%s)' % (' && '.join(tree_cuts_list))
                trees_b[keys_b[i]].Draw('GRL>>h1(2,0,2)', tmp_weight, 'egoff')
                tree_cuts_list.pop()
                hist = ROOT.gDirectory.Get('h1')
                hist.Scale(lumi_scale_factor)
                cutflow_hist.SetBinContent(current_sample, current_cut,
                                           hist.GetBinContent(2))
                cutflow_hist.SetBinError(current_sample, current_cut,
                                         hist.GetBinError(2))
                totbkg_content = cutflow_hist.GetBinContent(
                    totbkg_index + 1, current_cut) + hist.GetBinContent(2)
                totbkg_error = math.sqrt(
                    cutflow_hist.GetBinError(totbkg_index +
                                             1, current_cut)**2 +
                    hist.GetBinError(2)**2)
                cutflow_hist.SetBinContent(totbkg_index + 1, current_cut,
                                           totbkg_content)
                cutflow_hist.SetBinError(totbkg_index + 1, current_cut,
                                         totbkg_error)
                ROOT.gROOT.ProcessLine('delete h1')
                current_cut -= 1
        for hist in histlist:
            for x in range(hist.GetNbinsX()):
                if hist.GetXaxis().GetBinLabel(x + 1) == 'all':
                    allbin = x + 1
                    break
            for y in range(hist.GetNbinsY(), 0, -1):
                cutflow_hist.SetBinContent(current_sample, current_cut,
                                           hist.GetBinContent(allbin, y))
                cutflow_hist.SetBinError(current_sample, current_cut,
                                         hist.GetBinError(allbin, y))
                totbkg_content = cutflow_hist.GetBinContent(
                    totbkg_index + 1, current_cut) + hist.GetBinContent(
                        allbin, y)
                totbkg_error = math.sqrt(
                    cutflow_hist.GetBinError(totbkg_index +
                                             1, current_cut)**2 +
                    hist.GetBinError(allbin, y)**2)
                cutflow_hist.SetBinContent(totbkg_index + 1, current_cut,
                                           totbkg_content)
                cutflow_hist.SetBinError(totbkg_index + 1, current_cut,
                                         totbkg_error)
                current_cut -= 1
        current_sample += 1

    current_sample += 1

    #
    # susy (or wz!)
    #
    for i, histlist in enumerate(cf_s):
        current_cut = len(cuts)
        if hasattr(options.usermodule, 'cutflow'):
            tree_cuts_list = list('(' + a[1] + ')'
                                  for a in options.usermodule.cutflow)
            for j in options.usermodule.cutflow:
                #print tree_cuts_list
                tmp_weight = weight + '*(%s)' % (' && '.join(tree_cuts_list))
                trees_s[keys_s[i]].Draw('GRL>>h1(2,0,2)', tmp_weight, 'egoff')
                tree_cuts_list.pop()
                hist = ROOT.gDirectory.Get('h1')
                hist.Scale(lumi_scale_factor)
                signal_n = hist.GetBinContent(2)
                cutflow_hist.SetBinContent(current_sample, current_cut,
                                           signal_n)
                if signal_n > 0:
                    cutflow_hist.SetBinError(current_sample, current_cut,
                                             100. / float(math.sqrt(signal_n)))
                tot_bkg = cutflow_hist.GetBinContent(totbkg_index + 1,
                                                     current_cut)
                if tot_bkg:
                    cutflow_hist.SetBinContent(s_over_b_index + 1, current_cut,
                                               signal_n / float(tot_bkg))
                if tot_bkg + signal_n:
                    cutflow_hist.SetBinContent(
                        purity_index + 1, current_cut,
                        100. * signal_n / float(tot_bkg + signal_n))
                ROOT.gROOT.ProcessLine('delete h1')
                current_cut -= 1
        for hist in histlist:
            for x in range(hist.GetNbinsX()):
                if hist.GetXaxis().GetBinLabel(x + 1) == 'all':
                    allbin = x + 1
                    break
            for y in range(hist.GetNbinsY(), 0, -1):
                signal_n = hist.GetBinContent(allbin, y)
                cutflow_hist.SetBinContent(current_sample, current_cut,
                                           signal_n)
                cutflow_hist.SetBinError(current_sample, current_cut,
                                         100. / float(math.sqrt(signal_n)))
                tot_bkg = cutflow_hist.GetBinContent(totbkg_index + 1,
                                                     current_cut)
                if tot_bkg:
                    cutflow_hist.SetBinContent(s_over_b_index + 1, current_cut,
                                               signal_n / float(tot_bkg))
                if tot_bkg + signal_n:
                    cutflow_hist.SetBinContent(
                        purity_index + 1, current_cut,
                        100. * signal_n / float(tot_bkg + signal_n))
                current_cut -= 1
        current_sample += 1

    #
    # data
    #
    for i, histlist in enumerate(cf_d):
        current_cut = len(cuts)
        if hasattr(options.usermodule, 'cutflow'):
            tree_cuts_list = list('(' + a[1] + ')'
                                  for a in options.usermodule.cutflow)
            for j in options.usermodule.cutflow:
                tmp_weight = weight + '*(%s)' % (' && '.join(tree_cuts_list))
                tree_d[key_d[i]].Draw('GRL>>h1(2,0,2)', tmp_weight, 'egoff')
                tree_cuts_list.pop()
                hist = ROOT.gDirectory.Get('h1')
                hist.Scale(lumi_scale_factor)
                cutflow_hist.SetBinContent(current_sample, current_cut,
                                           hist.GetBinContent(2))
                cutflow_hist.SetBinError(current_sample, current_cut,
                                         hist.GetBinError(2))
                ROOT.gROOT.ProcessLine('delete h1')
                current_cut -= 1
        for hist in histlist:
            for x in range(hist.GetNbinsX()):
                if (hist.GetXaxis().GetBinLabel(x + 1) == 'data'):
                    allbin = x + 1
                    break
            for y in range(hist.GetNbinsY(), 0, -1):
                cutflow_hist.SetBinContent(current_sample, current_cut,
                                           hist.GetBinContent(allbin, y))
                cutflow_hist.SetBinError(current_sample, current_cut,
                                         hist.GetBinError(allbin, y))
                current_cut -= 1
        current_sample += 1

    PrintCutflow('Cutflow',
                 cutflow_hist,
                 samp_list=','.join(samples),
                 latex=options.latex)

    return
Exemple #4
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