示例#1
0
文件: plot.py 项目: zucchett/SFrame
def plotAll():
    gROOT.SetBatch(True)

    hists = {}

    #    file = TFile(NTUPLEDIR + "DYJetsToLL.root", 'READ')
    #    file.cd()
    #    # Looping over file content
    #    for key in file.GetListOfKeys():
    #        obj = key.ReadObj()
    #        # Histograms
    #        if obj.IsA().InheritsFrom('TH1'): continue
    #        #    hists.append(obj.GetName())
    #        # Tree
    #        elif obj.IsA().InheritsFrom('TTree'): continue
    #        # Directories
    #        if obj.IsFolder() and obj.GetName() in ['0l', '1e', '1m', '2e', '2m', '1e1m']:
    #            subdir = obj.GetName()
    #            hists[subdir] = []
    #            file.cd(subdir)
    #            for subkey in file.GetDirectory(subdir).GetListOfKeys():
    #                subobj = subkey.ReadObj()
    #                if subobj.IsA().InheritsFrom('TH1'):
    #                    hists[subdir].append(subobj.GetName())
    #            file.cd('..')
    #    file.Close()
    #
    #    for c, l in hists.iteritems():
    #        #if not os.path.exists("plots/"+c):
    #        #    os.mkdir("plots/"+c)
    #        for h in l:
    #            plot(h, c)

    for r in selection.keys():
        #for v in ['Lepton1_pt', 'Lepton1_pfIso', 'Lepton2_pt', 'Lepton2_pfIso', 'Jet1_pt', 'Jet1_csv', 'Jet2_pt', 'Jet2_csv', 'Jet3_pt', 'Jet3_csv', 'Jet4_pt', 'Jet4_csv', 'JetF_pt', 'mZ', 'V_pt', 'mT', 'mT2', 'MinDPhi', 'MinDPhi12', 'MET_pt', 'MET_sign', 'FakeMET_pt', 'nPV', 'nJets', 'nForwardJets', 'nBTagJets', 'nElectrons', 'nMuons', 'nTaus', 'HT', 'ST']:
        for v in [
                'MET_pt', 'MET_sign', 'MinDPhi12', 'mTb', 'mT',
                'ratio_jet1pt_HT', 'mT2'
        ]:
            #for v in ['MET_pt','MET_sign', 'FakeMET_pt']:
            #            plot(v, r)
            p = multiprocessing.Process(target=plot, args=(
                v,
                r,
            ))
            jobs.append(p)
            p.start()
        p.join()
示例#2
0
def plot(var, cut, nm1=False):
    ### Preliminary Operations ###
    treeRead = True
    channel = cut
    BTagAK4deepup = False
    BTagAK4deepdown = False
    BTagAK4deep = False
    BTagAK8deepup = False
    BTagAK8deepdown = False
    BTagAK8deep = False
    if 'inc' in cut:
        eventWeightLuminame = 'eventWeightLumi_nobtag'
    else:
        eventWeightLuminame = 'eventWeightLumi'
    if "SB" in cut or "SR" in cut:
        channel_name = cut[:-2]
    else:
        channel_name = cut
    isBlind = BLIND and 'SR' in channel

    if year in ['2016', '2017', '2018']:
        NTUPLEDIR = "/work/pbaertsc/heavy_resonance/Ntuples%s/" % (year)
    else:
        NTUPLEDIR = "/work/pbaertsc/heavy_resonance/"
    #showSignal = False if 'SB' in cut or 'TR' in cut else True #'SR' in channel or channel=='qqqq'#or len(channel)==5
    if var in [
            'dijet_VBF_mass', 'deltaR_VBF', 'deltaR_HVBFjet1',
            'deltaR_HVBFjet2'
    ]:
        showSignal = False
    else:
        showSignal = True
    if "cutflow" in var:
        showSignal = False
    if var in [
            'X_mass_jesUp', 'X_mass_jesDown', 'X_mass_jerUp', 'X_mass_jerDown',
            'X_mass_nom', 'X_mass_MET_jesUp', 'X_mass_MET_jesDown',
            'X_mass_MET_jerUp', 'X_mass_MET_jerDown', 'X_mass_MET_nom',
            'H_mass_jmsUp', 'H_mass_jmsDown', 'H_mass_jmrUp', 'H_mass_jmrDown',
            'H_mass_nom'
    ]:
        back = ["VV"]
    else:
        back = [
            "VV", "ST", "TTbarSL", "WJetsToLNu_HT", "DYJetsToNuNu_HT",
            "DYJetsToLL_HT"
        ]
    stype = "HVT model B"
    if channel.endswith('up'):
        TopBTagAK4deepup = True
        channel = channel[:-2]
        cut = channel
    if channel.endswith('down'):
        TopBTagAK4deepdown = True
        channel = channel[:-4]
        cut = channel
    if treeRead:
        for k in sorted(selection.keys(), key=len, reverse=True):
            if k in cut: cut = cut.replace(k, selection[k])

    # Determine Primary Dataset
    pd = []

    if var == 'BTagAK4Weightdeep_up':
        var = 'X_mass'
        BTagAK4deepup = True
    elif var == 'BTagAK4Weight_deep_down':
        var = 'X_mass'
        BTagAK4deepdown = True
    elif var == 'BTagAK4Weight_deep':
        var = 'X_mass'
        BTagAK4deep = True
    elif var == 'BTagAK8Weight_deep_up':
        var = 'X_mass'
        BTagAK8deepup = True
    elif var == 'BTagAK8Weight_deep_down':
        var = 'X_mass'
        BTagAK8deepdown = True
    elif var == 'BTagAK8Weight_deep':
        var = 'X_mass'
        BTagAK8deep = True

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}
    ### Create and fill MC histograms ###
    for i, s in enumerate(back + sign + sign_VBF):
        if treeRead:  # Project from tree
            tree[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree[s].Add(NTUPLEDIR + ss + ".root")
            if variable[var]['nbins'] > 0:
                if 'X_mass' in var:
                    min_value = 0.0
                    max_value = 6000.
                else:
                    min_value = variable[var]['min']
                    max_value = variable[var]['max']
                title = variable[var]['title']
                hist[s] = TH1F(
                    s, ";" + title + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], min_value, max_value)
            else:
                hist[s] = TH1F(s, ";" + variable[var]['title'],
                               len(variable[var]['bins']) - 1,
                               array('f', variable[var]['bins']))
            hist[s].Sumw2()
            cutstring = "%s" % eventWeightLuminame + ("*(" + cut + ")")
            if var == 'LeptonWeightUp':
                cutstring = "(%s * LeptonWeightUp/LeptonWeight)" % eventWeightLuminame + (
                    "*(" + cut + ")")
            elif var == 'LeptonWeightDown':
                cutstring = "(%s * LeptonWeightDown/LeptonWeight)" % eventWeightLuminame + (
                    "*(" + cut + ")")
            elif var == 'TriggerWeightUp':
                cutstring = "(%s * TriggerWeightUp/TriggerWeight)" % eventWeightLuminame + (
                    "*(" + cut + ")")
            elif var == 'TriggerWeightDown':
                cutstring = "(%s * TriggerWeightDown/TriggerWeight)" % eventWeightLuminame + (
                    "*(" + cut + ")")
            #division by BTagAk8Weight_deep is because the weighted samples are used
            elif BTagAK4deep:
                cutstring = "(%s * BTagAK4Weight_deep)" % eventWeightLuminame + (
                    "*(" + cut + ")")
            elif BTagAK4deepup:
                cutstring = "(%s * BTagAK4Weight_deep_up)" % eventWeightLuminame + (
                    "*(" + cut + ")")
            elif BTagAK4deepdown:
                cutstring = "(%s * BTagAK4Weight_deep_down)" % eventWeightLuminame + (
                    "*(" + cut + ")")
            elif BTagAK8deepup:
                cutstring = "(%s * BTagAK8Weight_deep_up/BTagAK8Weight_deep)" % eventWeightLuminame + (
                    "*(" + cut + ")")
            elif BTagAK8deepdown:
                cutstring = "(%s * BTagAK8Weight_deep_down/BTagAK8Weight_deep)" % eventWeightLuminame + (
                    "*(" + cut + ")")
            tree[s].Project(s, var, cutstring)
            if not tree[s].GetTree() == None:
                hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())
            #print tree[s].GetTree().GetEntriesFast()
        hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])

    hist['BkgSum'] = hist['data_obs'].Clone(
        "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s])
    # Set histogram style

    for i, s in enumerate(back + sign + sign_VBF + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow
    for i, s in enumerate(sign + sign_VBF):
        hist[s].SetLineWidth(3)
    for i, s in enumerate(sign + sign_VBF):
        sample[s][
            'plot'] = True  #sample[s]['plot'] and s.startswith(channel[:2])

    # Create stack
    bkg = THStack("Bkg",
                  ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events")
    for i, s in enumerate(back):
        bkg.Add(hist[s])
    return hist
示例#3
0
文件: plot.py 项目: zucchett/SFrame
def plot(var, cut, norm=False, nm1=False):
    ### Preliminary Operations ###
    doBinned = False
    if options.mode == "binned": doBinned = True

    fileRead = os.path.exists("combinedCards_" + options.name +
                              "/fitDiagnostics_" + options.file + ".root")
    treeRead = not any(
        x == cut
        for x in ['0l', '1e', '1m', '2e', '2m', '1e1m', 'Gen', 'Trigger'
                  ])  #(var in variable.keys()) # Read from tree

    #signal definition
    if fileRead:
        sign = ['ttDM_MChi1_MPhi200_scalar',
                'tDM_MChi1_MPhi200_scalar']  #for postfit plot
    if not fileRead and not options.limit:
        sign = ['ttDM_MChi1_MPhi100_scalar',
                'tDM_MChi1_MPhi100_scalar']  #for normal plotting
    #bkg definition
    if fileRead or options.limit:
        back = [
            "QCD", "DYJetsToNuNu_HT", "DYJetsToLL_HT", "VV", "ST",
            "WJetsToLNu_HT", "TTbarSL"
        ]  #for postfit or limit
    if (cut).find('>250') or (cut).startswith('AH'):  #for hadronic selections
        back = [
            "QCD", "DYJetsToLL_HT", "VV", "ST", "WJetsToLNu_HT", "TTbarV",
            "TTbar2L", "TTbar1L", "DYJetsToNuNu_HT"
        ]
        if fileRead or options.limit:
            back = [
                "QCD", "DYJetsToLL_HT", "VV", "ST", "WJetsToLNu_HT", "TTbarSL",
                "DYJetsToNuNu_HT"
            ]  #for postfit or limit

    binLow = ""
    binHigh = ""
    binName = ""
    if "binned" in cut:
        binLow = cut[cut.find("LowVal") + 6:cut.find("HighVal") - 1]
        binHigh = cut[cut.find("HighVal") + 7:]
        binName = "bin_" + binLow + "_" + binHigh
        cut = cut[:cut.find("binned")]
    useformula = False
    if 'formula' in variable[var]:
        useformula = True
    channel = cut
    plotdir = cut
    plotname = var
    weight = "eventWeightLumi"  #*(2.2/35.9)
    isBlind = BLIND and ('SR' in channel or 'ps' in channel)
    if fileRead:
        isBlind = False
        options.saveplots = True
        RESIDUAL = True
    elif isBlind:
        RATIO = 0
        SIGNAL = 20
    else:
        RATIO = 4
        SIGNAL = 1
        RESIDUAL = False
    showSignal = True  #('SR' in channel)
    cutSplit = cut.split()
    for s in cutSplit:
        if s in selection.keys():
            plotdir = s
            cut = cut.replace(s, selection[s])
            if not binLow == "":
                cut = cut + " && " + var + " > " + binLow + " && " + var + " < " + binHigh
#if treeRead and cut in selection: cut  = cut.replace(cut, selection[cut])

# Determine Primary Dataset
    pd = []
    if any(w in cut
           for w in ['1l', '1m', '2m', 'isWtoMN', 'isZtoMM', 'isTtoEM']):
        pd += [x for x in sample['data_obs']['files'] if 'SingleMuon' in x]
    if any(w in cut for w in ['1l', '1e', '2e', 'isWtoEN', 'isZtoEE']):
        pd += [x for x in sample['data_obs']['files'] if 'SingleElectron' in x]
    if any(w in cut for w in ['0l', 'isZtoNN']):
        pd += [x for x in sample['data_obs']['files'] if 'MET' in x]
    if len(pd) == 0:
        raw_input("Warning: Primary Dataset not recognized, continue?")

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut
    print "  cut    :", weight

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}

    ### Create and fill MC histograms ###
    for i, s in enumerate(data + back + sign):
        if fileRead:
            var = 'MET_pt'
            if channel.startswith('SL'): var = 'MET_sign'
            if channel.endswith('ZR'): var = 'FakeMET_pt'
            plotname = var

            hist[s] = TH1F(
                s, ";" + variable[var]['title'] + ";Events;" +
                ('log' if variable[var]['log'] else ''),
                variable[var]['nbins'], variable[var]['min'],
                variable[var]['max'])

            if doBinned:
                bins = np.array([])
                if 'bins' in variable[var].keys():
                    bins = np.array(variable[var]['bins'])
                else:
                    binsize = (variable[var]['max'] -
                               variable[var]['min']) / variable[var]['nbins']
                    bins = np.arange(variable[var]['min'],
                                     variable[var]['max'] + binsize, binsize)
                bins = np.append(bins, 10000)

                for i in range(0, len(bins) - 1):
                    rbin = str(bins[i]) + "_" + str(bins[i + 1])
                    fileName = "combinedCards_" + options.name + "/fitDiagnostics_" + options.file + ".root" if not any(
                        t in s for t in ['data', 'tDM']
                    ) else "rootfiles_" + options.name + "/" + channel + "bin_" + rbin + ".root"
                    histName = "shapes_fit_b/" + channel + "bin_" + rbin + "/" + s if not any(
                        t in s for t in ['data', 'tDM']) else s
                    file[s] = TFile(fileName, "READ")
                    tmphist = file[s].Get(histName)

                    if 'data' not in s: hist[s].SetMarkerSize(0)
                    if tmphist:
                        hist[s].SetBinContent(i + 1, tmphist.GetBinContent(1))
                        hist[s].SetBinError(i + 1, tmphist.GetBinError(1))
                    else:
                        hist[s].SetBinContent(i + 1, 0.)
                        hist[s].SetBinError(i + 1, 0.)
            else:
                fileName = "combinedCards_" + options.name + "/fitDiagnostics_" + options.file + ".root" if not s == 'data_obs' else "rootfiles_" + options.name + "/" + channel + binName + ".root"
                histName = "shapes_fit_b/" + channel + "/" + s if not s == 'data_obs' else s
                file[s] = TFile(fileName, "READ")
                tmphist = file[s].Get(histName)

                if tmphist == None:
                    tmphist = hist[back[0]].Clone(s)
                    tmphist.Reset("MICES")
                    print "Histogram", histName, "not found in file", fileName

                if s == 'data_obs': hist[s] = tmphist
                else:
                    hist[s] = hist['data_obs'].Clone(s)
                    hist[s].SetMarkerSize(0)
                    for i in range(tmphist.GetNbinsX() + 1):
                        hist[s].SetBinContent(i + 1,
                                              tmphist.GetBinContent(i + 1))

        elif treeRead:  # Project from tree
            tree[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree[s].Add(NTUPLEDIR + ss + ".root")
            if not binLow == "":
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events;" +
                    ('log' if variable[var]['log'] else ''), 1, float(binLow),
                    float(binHigh))
            elif binLow == "" and variable[var]['nbins'] > 0:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], variable[var]['min'],
                    variable[var]['max'])
            else:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    len(variable[var]['bins']) - 1,
                    array('f', variable[var]['bins']))
            hist[s].Sumw2()
            redFactorString = ""
            redFactorValue = ""
            #if isBlind and 'data' in s:
            if isBlind and 'data' in s and options.limit:
                redFactorString = " && Entry$ % 15 == 1"
            #if isBlind and 'data' not in s:
            if isBlind and 'data' not in s and options.limit:
                redFactorValue = " / 15"
            cutstring = "(" + weight + redFactorValue + ")" + (
                "*(" + cut + redFactorString + ")" if len(cut) > 0 else "")
            if '-' in s:
                cutstring = cutstring.replace(
                    cut, cut + "&& nBQuarks==" + s.split('-')[1][0])
            if useformula == True:
                tree[s].Project(s, variable[var]['formula'], cutstring)
            else:
                tree[s].Project(s, var, cutstring)
            if not tree[s].GetTree() == None:
                hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())
        else:  # Histogram written to file
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    file[ss] = TFile(NTUPLEDIR + ss + ".root", "R")
                    if file[ss].IsZombie():
                        print "WARNING: file", NTUPLEDIR + ss + ".root", "does not exist"
                        continue
                    tmphist = file[ss].Get(cut + "/" + var)
                    if tmphist == None: continue
                    if not s in hist.keys(): hist[s] = tmphist
                    else: hist[s].Add(tmphist)
        if hist[s].Integral() < 0: hist[s].Scale(0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])
        #if 'WJetsToLNu' in s and 'SL' in channel and 'WR' in channel: hist[s].Scale(1.30)
        #if 'TTbar' in s and 'SL' in channel and 'TR' in channel: hist[s].Scale(0.91)

    hist['BkgSum'] = hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s], 1)

    if fileRead:
        hist['PreFit'] = hist['BkgSum'].Clone("PreFit")
        if doBinned:
            for i in range(0, len(bins) - 1):
                rbin = str(bins[i]) + "_" + str(bins[i + 1])
                tmphist = file[back[0]].Get("shapes_prefit/" + channel +
                                            "bin_" + rbin + "/" +
                                            "total_background")

                if tmphist:
                    hist['PreFit'].SetBinContent(i + 1,
                                                 tmphist.GetBinContent(1))
                else:
                    hist['PreFit'].SetBinContent(i + 1, 0.)
        else:
            tmphist = file[back[0]].Get("shapes_prefit/" + channel + "/" +
                                        "total_background")
            for i in range(tmphist.GetNbinsX() + 1):
                hist['PreFit'].SetBinContent(i + 1,
                                             tmphist.GetBinContent(i + 1))
        addOverflow(hist['PreFit'], False)
        hist['PreFit'].SetLineStyle(2)
        hist['PreFit'].SetLineColor(617)  #923
        hist['PreFit'].SetLineWidth(3)
        hist['PreFit'].SetFillStyle(0)
    hist['BkgSum'].SetFillStyle(3002)
    hist['BkgSum'].SetFillColor(1)

    # Create data and Bkg sum histograms
    #    if options.blind or 'SR' in channel:
    #        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
    #        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist[data[0]].SetMarkerStyle(20)
    hist[data[0]].SetMarkerSize(1.25)

    for i, s in enumerate(data + back + sign + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    #for i, s in enumerate(sign): sample[s]['plot'] = True#sample[s]['plot'] and s.startswith(channel[:2])

    if norm:
        for i, s in enumerate(sign):
            hist[s].Scale(hist['BkgSum'].Integral() / hist[s].Integral())
#        for i, s in enumerate(back):
#            hist[s].SetFillStyle(3005)
#            hist[s].SetLineWidth(2)
#        #for i, s in enumerate(sign):
#        #    hist[s].SetFillStyle(0)
#        if not var=="Events":
#            sfnorm = hist[data[0]].Integral()/hist['BkgSum'].Integral()
#            print "Applying SF:", sfnorm
#            for i, s in enumerate(back+['BkgSum']): hist[s].Scale(sfnorm)

    if SIGNAL > 1:
        if not var == "Events":
            for i, s in enumerate(sign):
                hist[s].Scale(SIGNAL)

    # Create stack
    bkg = THStack("Bkg",
                  ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    #leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg = TLegend(0.45, 0.63, 0.93, 0.92)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetNColumns(3)
    leg.SetTextFont(42)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if 'PreFit' not in hist:
        leg.AddEntry(hist['BkgSum'], sample['BkgSum']['label'], "f")
    else:
        leg.AddEntry(hist['BkgSum'], 'MC unc.', "l")
        leg.AddEntry(hist['PreFit'], sample['PreFit']['label'], "l")
    if showSignal:
        for i, s in enumerate(sign):
            if SIGNAL > 1:
                if sample[s]['plot']:
                    leg.AddEntry(hist[s],
                                 '%s (x%d)' % (sample[s]['label'], SIGNAL),
                                 "l")
            else:
                if sample[s]['plot']:
                    leg.AddEntry(hist[s], sample[s]['label'], "l")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        if RESIDUAL:
            c1.Divide(1, 3)
            setFitTopPad(c1.GetPad(1), RATIO)
            setFitBotPad(c1.GetPad(2), RATIO)
            setFitResPad(c1.GetPad(3), RATIO)
        else:
            c1.Divide(1, 2)
            setTopPad(c1.GetPad(1), RATIO)
            setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    log = ("log" in hist['BkgSum'].GetZaxis().GetTitle())
    if log: c1.GetPad(bool(RATIO)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if not isBlind and len(data) > 0:
        graph = fixData(hist[data[0]], USEGARWOOD)
        graph.Draw("SAME, PE")
    #data_graph.Draw("SAME, PE")
    if 'PreFit' in hist: hist['PreFit'].Draw("SAME, HIST")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                hist[s].Draw(
                    "SAME, HIST"
                )  # signals Normalized, hist[s].Integral()*sample[s]['weight']
    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1.075)
    bkg.SetMaximum((5. if log else 1.25) * max(
        bkg.GetMaximum(),
        hist[data[0]].GetBinContent(hist[data[0]].GetMaximumBin()) +
        hist[data[0]].GetBinError(hist[data[0]].GetMaximumBin())))
    if len(sign) > 0 and bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(),
                                                hist[sign[-1]].GetMaximum()):
        bkg.SetMaximum(
            max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()) *
            1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist[data[0]].GetMinimum()), 5.e-1) if log else 0.)
    if log:
        #bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e3)
        bkg.GetYaxis().SetMoreLogLabels(True)
    else:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e3)

    leg.Draw()
    if fileRead and 'SR' in channel:
        drawCMS(LUMI / 15., "Preliminary")
    else:
        drawCMS(LUMI, "Preliminary")
    drawRegion(channel, True)
    drawAnalysis("DM" + channel[:2])
    drawOverflow()

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        if RESIDUAL: setFitBotStyle(err)
        else: setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist[data[0]].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        if RESIDUAL: setFitBotStyle(res)
        else: setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        if 'PreFit' in hist:
            respre = hist[data[0]].Clone("ResiduesPreFit")
            respre.Divide(hist['PreFit'])
            respre.SetLineStyle(2)
            respre.SetLineColor(617)  #923
            respre.SetLineWidth(3)
            respre.SetFillStyle(0)
            respre.Draw("SAME, HIST")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])

    c1.Update()

    if RATIO and RESIDUAL:
        c1.cd(3)
        c1.SetGrid(1, 0)
        resFit = hist[data[0]].Clone("Residues")
        resFit.Reset("MICES")
        resFit.SetTitle("")
        #resFit.GetYaxis().SetTitle("Residuals")
        resFit.GetYaxis().SetTitle(
            "#frac{Data - Bkg}{#sqrt{#sigma_{Data}^{2}+#sigma_{Bkg}^{2}}}")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                resFit.SetBinContent(
                    i, (hist[data[0]].GetBinContent(i) -
                        hist['BkgSum'].GetBinContent(i)) / (math.sqrt(
                            math.pow(hist['BkgSum'].GetBinError(i), 2) +
                            math.pow(hist[data[0]].GetBinError(i), 2))))
        setFitResStyle(resFit)
        resFit.SetLineColor(15)
        resFit.SetFillColor(15)
        resFit.SetFillStyle(1001)
        resFit.Draw("HIST")

        resFitLine = resFit.Clone("resFitLine")
        resFitLine.SetLineWidth(1)
        resFitLine.SetFillStyle(0)
        resFitLine.Draw("SAME, HIST")

    c1.Update()

    if gROOT.IsBatch(
    ) and options.saveplots:  # and (treeRead and channel in selection.keys()):
        AddString = ""
        if not os.path.exists("plots_" + options.name + "/" + plotdir):
            os.makedirs("plots_" + options.name + "/" + plotdir)
        if fileRead:
            if RESIDUAL: AddString = "_PostFit_Residual"
            else: AddString = "_PostFit"
        #c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+AddString+".png")
        c1.Print("plots_" + options.name + "/" + plotdir + "/" + plotname +
                 binName + AddString + ".pdf")

    # Print table
    printTable(hist, sign)

    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")

    if gROOT.IsBatch() and not fileRead and (
            var == 'MET_pt' or
        (channel.startswith('SL') and var == 'MET_sign') or
        (channel.endswith('ZR') and var == 'FakeMET_pt')):
        saveHist(hist, channel + binName)
示例#4
0
def plot(var, cut, norm=False, nm1=False):
    ### Preliminary Operations ###
    fileRead = os.path.exists(options.file)
    treeRead = not any(x==cut for x in ['0l', '1e', '1m', '2e', '2m', '1e1m', 'Gen', 'Trigger'])#(var in variable.keys()) # Read from tree
    binLow = ""
    binHigh = ""
    binName = ""
    if "binned" in cut:
        binLow = cut[cut.find("LowVal")+6:cut.find("HighVal")-1]
        binHigh = cut[cut.find("HighVal")+7:]
        binName = "bin_"+binLow+"_"+binHigh
        cut = cut[:cut.find("binned")]
    useformula = True
    if 'formula' in variable[var]:
        print variable[var]['formula']
        useformula = True
    channel = cut
    plotdir = cut
    plotname = var
    weight = "eventWeightLumi" #*(2.2/35.9)
    isBlind = BLIND and 'SR' in channel
    showSignal = True#('SR' in channel)
    cutSplit = cut.split()
    for s in cutSplit:
        if s in selection.keys():
            plotdir = s
            cut  = cut.replace(s, selection[s])
            if not binLow == "":
                cut = cut + " && " + var + " > " + binLow + " && " + var + " < " + binHigh
   #if treeRead and cut in selection: cut  = cut.replace(cut, selection[cut])
    
    # Determine Primary Dataset
    pd = []
    print cut
    if any(w in cut for w in ['1l', '1m', '2m', 'isWtoMN', 'isZtoMM', 'isTtoEM']): pd += [x for x in sample['data_obs']['files'] if 'SingleMuon' in x]
    if any(w in cut for w in ['1l', '1e', '2e', 'isWtoEN', 'isZtoEE']): pd += [x for x in sample['data_obs']['files'] if 'SingleElectron' in x]
    if any(w in cut for w in ['0l', 'isZtoNN']): pd += [x for x in sample['data_obs']['files'] if 'MET' in x]
    if len(pd)==0: raw_input("Warning: Primary Dataset not recognized, continue?")
    
    print "Plotting from", ("tree" if treeRead else "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut
    print "  cut    :", weight
    
    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}
    
    ### Create and fill MC histograms ###
    for i, s in enumerate(data+back+sign):
        if fileRead:
            fileName = options.file if not s=='data_obs' else "rootfiles_"+options.name+"/"+channel+binName+".root"
            histName = "shapes_fit_b/"+channel+"/"+s if not s=='data_obs' else s
            file[s] = TFile(fileName, "READ")
            tmphist = file[s].Get(histName)
            if tmphist==None:
                tmphist = hist[back[0]].Clone(s)
                tmphist.Reset("MICES")
                print "Histogram", histName, "not found in file", fileName
            if s=='data_obs': hist[s] = tmphist
            else:
                hist[s] = hist['data_obs'].Clone(s)
                #hist[s].Reset("MICES")
                hist[s].SetMarkerSize(0)
                for i in range(tmphist.GetNbinsX()+1): hist[s].SetBinContent(i+1, tmphist.GetBinContent(i+1))
        elif treeRead: # Project from tree
            tree[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree[s].Add(NTUPLEDIR + ss + ".root")
            if not binLow == "":
                hist[s] = TH1F(s, ";"+variable[var]['title']+";Events;"+('log' if variable[var]['log'] else ''), 1, float(binLow), float(binHigh))
            elif binLow == "" and  variable[var]['nbins']>0: 
                hist[s] = TH1F(s, ";"+variable[var]['title']+";Events;"+('log' if variable[var]['log'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max'])
            else: 
                hist[s] = TH1F(s, ";"+variable[var]['title']+";Events;"+('log' if variable[var]['log'] else ''), len(variable[var]['bins'])-1, array('f', variable[var]['bins']))
            hist[s].Sumw2()
            redFactorString = ""
            redFactorValue = ""
            if isBlind and 'data' in s:
                redFactorString = " && Entry$ % 15 == 0"
            if isBlind and 'data' not in s:
                redFactorValue = " / 15"
            cutstring = "("+weight+redFactorValue+")" + ("*("+cut+redFactorString+")" if len(cut)>0 else "")
            if '-' in s: cutstring = cutstring.replace(cut, cut + "&& nBQuarks==" + s.split('-')[1][0])
            if useformula == True:
                tree[s].Project(s, variable[var]['formula'], cutstring)
            else:
                tree[s].Project(s, var, cutstring)         
            if not tree[s].GetTree()==None: hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())
        else: # Histogram written to file
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    file[ss] = TFile(NTUPLEDIR + ss + ".root", "R")
                    if file[ss].IsZombie():
                        print "WARNING: file", NTUPLEDIR + ss + ".root", "does not exist"
                        continue
                    tmphist = file[ss].Get(cut+"/"+var)
                    if tmphist==None: continue
                    if not s in hist.keys(): hist[s] = tmphist
                    else: hist[s].Add(tmphist)
        if hist[s].Integral() < 0: hist[s].Scale(0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])
        #if 'WJetsToLNu' in s and 'SL' in channel and 'WR' in channel: hist[s].Scale(1.30)
        #if 'TTbar' in s and 'SL' in channel and 'TR' in channel: hist[s].Scale(0.91)
    
    
    hist['BkgSum'] = hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    for i, s in enumerate(back): hist['BkgSum'].Add(hist[s], 1)
    if fileRead:
        #hist['BkgSum'] = file[back[0]].Get("shapes_fit_b/"+channel+"/"+"total_background")
        tmphist = file[back[0]].Get("shapes_prefit/"+channel+"/"+"total_background")
        hist['PreFit'] = hist['BkgSum'].Clone("PreFit")
        for i in range(tmphist.GetNbinsX()+1): hist['PreFit'].SetBinContent(i+1, tmphist.GetBinContent(i+1))
        hist['PreFit'].SetLineStyle(2)
        hist['PreFit'].SetLineColor(923)
        hist['PreFit'].SetLineWidth(3)
        hist['PreFit'].SetFillStyle(0)
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    

    # Create data and Bkg sum histograms
#    if options.blind or 'SR' in channel:
#        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
#        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist[data[0]].SetMarkerStyle(20)
    hist[data[0]].SetMarkerSize(1.25)
    
    for i, s in enumerate(data+back+sign+['BkgSum']): addOverflow(hist[s], False) # Add overflow
    for i, s in enumerate(sign): hist[s].SetLineWidth(3)
    #for i, s in enumerate(sign): sample[s]['plot'] = True#sample[s]['plot'] and s.startswith(channel[:2])
    
    
    if norm:
        for i, s in enumerate(sign):
            hist[s].Scale(hist['BkgSum'].Integral()/hist[s].Integral())
#        for i, s in enumerate(back):
#            hist[s].SetFillStyle(3005)
#            hist[s].SetLineWidth(2)
#        #for i, s in enumerate(sign):
#        #    hist[s].SetFillStyle(0)
#        if not var=="Events":
#            sfnorm = hist[data[0]].Integral()/hist['BkgSum'].Integral()
#            print "Applying SF:", sfnorm
#            for i, s in enumerate(back+['BkgSum']): hist[s].Scale(sfnorm)
    
    # Create stack
    bkg = THStack("Bkg", ";"+hist['BkgSum'].GetXaxis().GetTitle()+";Events")
    for i, s in enumerate(back): bkg.Add(hist[s])
    
    # Legend
    leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0) #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(['BkgSum']+back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if 'PreFit' in hist: leg.AddEntry(hist['PreFit'], sample['PreFit']['label'], "l")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']: leg.AddEntry(hist[s], sample[s]['label'], "fl")
        
    leg.SetY1(0.9-leg.GetNRows()*0.05)
    
    
    # --- Display ---
    c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800 if RATIO else 600)
    
    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)
    
    log = ("log" in hist['BkgSum'].GetZaxis().GetTitle())
    if log: c1.GetPad(bool(RATIO)).SetLogy()
        
    # Draw
    bkg.Draw("HIST") # stack
    hist['BkgSum'].Draw("SAME, E2") # sum of bkg
    if not isBlind and len(data) > 0: hist[data[0]].Draw("SAME, PE") # data
    #data_graph.Draw("SAME, PE")
    if 'PreFit' in hist: hist['PreFit'].Draw("SAME, HIST")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']: hist[s].Draw("SAME, HIST") # signals Normalized, hist[s].Integral()*sample[s]['weight']
    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075)
    bkg.SetMaximum((5. if log else 1.25)*max(bkg.GetMaximum(), hist[data[0]].GetBinContent(hist[data[0]].GetMaximumBin())+hist[data[0]].GetBinError(hist[data[0]].GetMaximumBin())))
    if len(sign) > 0 and bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25)
    bkg.SetMinimum(max(min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin()), hist[data[0]].GetMinimum()), 5.e-1)  if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetMoreLogLabels(True)
    
    leg.Draw()
    drawCMS(LUMI, "Preliminary")
    drawRegion(channel, True)
    drawAnalysis("DM"+channel[:2])
    drawOverflow()
    
    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)
       
    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX()+1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(i, hist['BkgSum'].GetBinError(i)/hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist[data[0]].Clone("Residues")
        for i in range(0, res.GetNbinsX()+1):
            if hist['BkgSum'].GetBinContent(i) > 0: 
                res.SetBinContent(i, res.GetBinContent(i)/hist['BkgSum'].GetBinContent(i))
                res.SetBinError(i, res.GetBinError(i)/hist['BkgSum'].GetBinContent(i))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        if 'PreFit' in hist:
            respre = hist['PreFit'].Clone("ResiduesPreFit")
            respre.Divide(hist['BkgSum'])
            respre.Draw("SAME, HIST")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(1))==0: # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])
    
    c1.Update()
        
    if gROOT.IsBatch() and options.saveplots: # and (treeRead and channel in selection.keys()):
        if not os.path.exists("plots_"+options.name+"/"+plotdir): os.makedirs("plots_"+options.name+"/"+plotdir)
        c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+".png")
        c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+".pdf")
    
    # Print table
    printTable(hist, sign)
    
    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")

    if gROOT.IsBatch() and not fileRead and (var == 'MET_pt' or (channel.startswith('SL') and var == 'MET_sign') or (channel.endswith('ZR') and var == 'FakeMET_pt')):
        saveHist(hist, channel+binName)
示例#5
0
def plot(var, cut, norm=False, nm1=False):
    ### Preliminary Operations ###
    fileRead = os.path.exists(options.file)
    treeRead = not any(
        x == cut
        for x in ['0l', '1e', '1m', '2e', '2m', '1e1m', 'Gen', 'Trigger'
                  ])  # Read from tree
    channel = cut
    plotdir = cut
    plotname = var
    weight = "eventWeightLumi"  #*(2.2/35.9)
    isBlind = BLIND and 'SR' in channel
    showSignal = True  #('SR' in channel)
    cutSplit = cut.split()
    for s in cutSplit:
        if s in selection.keys():
            plotdir = s
            cut = cut.replace(s, selection[s])
    #if treeRead and cut in selection: cut  = cut.replace(cut, selection[cut])

    # Determine Primary Dataset
    pd = []
    #print cut
    if any(w in cut
           for w in ['1l', '1m', '2m', 'isWtoMN', 'isZtoMM', 'isTtoEM']):
        pd += [x for x in sample['data_obs']['files'] if 'SingleMuon' in x]
    if any(w in cut for w in ['1l', '1e', '2e', 'isWtoEN', 'isZtoEE']):
        pd += [x for x in sample['data_obs']['files'] if 'SingleElectron' in x]
    if any(w in cut for w in ['0l', 'isZtoNN']):
        pd += [x for x in sample['data_obs']['files'] if 'MET' in x]
    if len(pd) == 0:
        raw_input("Warning: Primary Dataset not recognized, continue?")

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut
    print "  cut    :", weight

    print "preparing cut grid"
    # fillCutValues(0.,0.2,4,1,
    #               0.,0.4,4,1,
    #               0.,0.5,5,1,
    #               0.,1.,5,1,
    #               0.,0.2,4,1
    #               )

    # fillCutValues(0.,3.2,4,2,
    #               0.,3.2,4,1,
    #               0.,3.2,4,2,
    #               0.,3.2,4,1,
    #               0.,1.,5,2
    #               )

    # fillCutValues(0.,3.2,4,1,
    #               0.,3.2,4,1,
    #               0.,3.2,4,2,
    #               0.,3.2,4,2,
    #               0.,400.,4,2
    #               )

    fillCutValues(100., 400, 30, 1, 0., 0., 1, 1, 0., 0., 1, 1, 0., 0., 1, 1,
                  0., 0., 1, 1)

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}

    #variables = ["mTb","mT2","mT","MinLepMetDPhi","MinLepJetDPhi"]
    #'mTb','topMass','WMass','HT','Dphi_topb','Dphi_Wb','Dphi_topMET','Dphi_WMET'
    #variables = ["mTb","WdRMass","topdRMass","MET_pt","MET_pt"]
    #variables = ["Dphi_topb","Dphi_Wb","Dphi_topMET","Dphi_WMET","MET_pt"]

    #variables = ["aplanarityFT","CFT","circularityFT","sphericity_AZFT","DFT"]
    #variables = ["Dphi_topb","Dphi_topMET","Dphi_Wb","Dphi_WMET","Jet1_pt/HT"]
    #variables = ["MaxBJetMetDPhi","MinBJetMetDPhi","minDphiJet1BJet","minDphiJet2BJet","pt_bjet"]
    #variables = ["cosTheta_bb","topdRMass","topdRMass","WdRMass","WdRMass"]
    #variables = ["Jet1_pt/HT","MET_pt","MET_pt","MET_pt","MET_pt"]

    #variables = ["MinDPhi12","MET_pt","MET_pt","MET_pt","MET_pt"]
    variables = ["mT2", "MET_pt", "MET_pt", "MET_pt", "MET_pt"]
    #variables = ["MET_pt","MET_pt","MET_pt","MET_pt","MET_pt"]

    ### Create and fill MC histograms ###
    for i, s in enumerate(data + back + sign):
        print "reading tree"
        tree[s] = TChain("tree")
        for j, ss in enumerate(sample[s]['files']):
            if not 'data' in s or ('data' in s and ss in pd):
                tree[s].Add(NTUPLEDIR + ss + ".root")

        if variable[var]['nbins'] > 0:
            hist[s] = TH1F(
                s, ";" + variable[var]['title'] + ";Events;" +
                ('log' if variable[var]['log'] else ''),
                variable[var]['nbins'], variable[var]['min'],
                variable[var]['max'])
        else:
            hist[s] = TH1F(
                s, ";" + variable[var]['title'] + ";Events;" +
                ('log' if variable[var]['log'] else ''),
                len(variable[var]['bins']) - 1,
                array('f', variable[var]['bins']))

        hist[s].Sumw2()
        cutstring = "(" + weight + ")" + ("*(" + cut +
                                          ")" if len(cut) > 0 else "")
        if '-' in s:
            cutstring = cutstring.replace(
                cut, cut + "&& nBQuarks==" + s.split('-')[1][0])

        if s.startswith("tDM") or s.startswith("ttDM"):
            isSignal = True
            fillSigBkgArrays(isSignal, variables, tree[s], weight, s, cut)
        elif not (s.startswith("data")):
            isSignal = False
            fillSigBkgArrays(isSignal, variables, tree[s], weight, s, cut)

    print "searching for best cuts"
    searchBestCut()
示例#6
0
def plot(var, cut, signal):
    sign = signal
    ### Preliminary Operations ###
    treeRead = not 'cutflow' in var
    #treeRead = False
    channel = cut
    if 'inc' in cut:
        eventWeightLuminame = 'eventWeightLumi_nobtag'
    else:
        eventWeightLuminame = 'eventWeightLumi'
    if "SB" in cut or "SR" in cut:
        channel_name = cut[:-2]
    else:
        channel_name = cut
    if year in ['2016', '2017', '2018']:
        NTUPLEDIR = "/work/pbaertsc/heavy_resonance/Ntuples%s/" % (year)
    else:
        NTUPLEDIR = "/work/pbaertsc/heavy_resonance/"
    #showSignal = False if 'SB' in cut or 'TR' in cut else True #'SR' in channel or channel=='qqqq'#or len(channel)==5
    if var in [
            'dijet_VBF_mass', 'deltaR_VBF', 'deltaR_HVBFjet1',
            'deltaR_HVBFjet2'
    ]:
        showSignal = False
    else:
        showSignal = True
    if treeRead:
        for k in sorted(selection.keys(), key=len, reverse=True):
            if k in cut: cut = cut.replace(k, selection[k])

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  cut    :", cut

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}
    ### Create and fill MC histograms ###
    tree[sign] = TChain("tree")
    for j, ss in enumerate(sample[sign]['files']):
        tree[sign].Add(NTUPLEDIR + ss + ".root")
    if variable[var]['nbins'] > 0:
        min_value = variable[var]['min']
        max_value = variable[var]['max']
        title = variable[var]['title']
        hist[sign] = TH1F(
            sign,
            ";" + title + ";Events;" + ('log' if variable[var]['log'] else ''),
            variable[var]['nbins'], min_value, max_value)
    else:
        hist[s] = TH1F(sign, ";" + variable[var]['title'],
                       len(variable[var]['bins']) - 1,
                       array('f', variable[var]['bins']))
    hist[sign].Sumw2()
    cutstring = "%s" % eventWeightLuminame + ("*(" + cut + ")")
    tree[sign].Project(sign, var, cutstring)
    if not tree[sign].GetTree() == None:
        hist[sign].SetOption("%s" % tree[sign].GetTree().GetEntriesFast())
    hist[sign].SetFillColor(sample[sign]['fillcolor'])
    hist[sign].SetFillStyle(sample[sign]['fillstyle'])
    hist[sign].SetLineColor(sample[sign]['linecolor'])
    hist[sign].SetLineStyle(sample[sign]['linestyle'])

    hist[sign].SetLineWidth(3)
    sample[sign]['plot'] = True

    # Legend
    leg = TLegend(0.6, 0.6, 0.9, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.AddEntry(hist[sign], sample[sign]['label'], "fl")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800)

    c1.SetTopMargin(0.06)
    c1.SetRightMargin(0.05)
    c1.SetTicks(1, 1)
    hist[sign].Draw("HIST")

    leg.Draw()
    drawCMS(LUMI, year, "Preliminary")
    drawRegion('XVH' + channel, True)
    drawAnalysis(channel)

    c1.Update()

    if gROOT.IsBatch():
        varname = var.replace('.', '_').replace('()', '')
        if not os.path.exists(OUTPUTDIR + channel_name):
            os.makedirs(OUTPUTDIR + channel_name)
        c1.Print("%s" % OUTPUTDIR + channel_name + "/" + varname + sign +
                 ".png")
        c1.Print("%s" % OUTPUTDIR + channel_name + "/" + varname + sign +
                 ".pdf")
def systematics(sys, isShape=False, antiCorr=False):
    treeRead = True
    file = {}
    hist = {}
    tree = {}
    histUp = {}
    histDown = {}
    up = {}
    down = {}
    gUp = TGraph()
    gDown = TGraph()
    gUp.SetLineWidth(3)
    gDown.SetLineWidth(3)
    gUp.SetLineColor(632)
    gDown.SetLineColor(602)
    BTagAK4deepup = False
    BTagAK4deepdown = False
    BTagAK4deep = False
    BTagAK8deepup = False
    BTagAK8deepdown = False
    BTagAK8deep = False
    #    g.SetMarkerStyle(20)
    #    g.SetMarkerColor(418)
    #    g.SetMarkerSize(1.25)
    var = sys
    cut = 'nnbbSR'
    if var == 'BTagAK4Weight_deep_up':
        var = 'X_mass'
        BTagAK4deepup = True
    elif var == 'BTagAK4Weight_deep_down':
        var = 'X_mass'
        BTagAK4deepdown = True
    elif var == 'BTagAK4Weight_deep':
        var = 'X_mass'
        BTagAK4deep = True
    elif var == 'BTagAK8Weight_deep_up':
        var = 'X_mass'
        BTagAK8deepup = True
    elif var == 'BTagAK8Weight_deep_down':
        var = 'X_mass'
        BTagAK8deepdown = True
    elif var == 'BTagAK8Weight_deep':
        var = 'X_mass'
        BTagAK8deep = True
    upAvg, downAvg, upMin, downMin, upMax, downMax = 0., 0., 2., 2., 0., 0.
    for k in sorted(selection.keys(), key=len, reverse=True):
        if k in cut:
            cut = cut.replace(k, selection[k])
    for i, s in enumerate(sign):
        if '_MZ' in s: m = int((s.split('_MZ')[1]).split('_MA')[0])
        elif '_M' in s: m = int(s.split('_M')[1])
        else: m = 0
        if treeRead:  # Project from tree
            tree[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree[s].Add(NTUPLEDIR + ss + ".root")
            if variable[var]['nbins'] > 0:
                min_value = variable[var]['min']
                max_value = variable[var]['max']
                title = variable[var]['title']
                if 'isZtoNN' in cut:
                    if var == 'MET':
                        min_value = 200
                        max_value = 2000
                    elif var == 'DPhi':
                        title = "#Delta #varphi (AK8 jet-#slash{E}_{T})"
                    elif var == 'VH_deltaR':
                        title = "#Delta R (#slash{E}_{T}, AK8 jet)"
                hist[s] = TH1F(
                    s, ";" + title + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], min_value, max_value)
            else:
                hist[s] = TH1F(s, ";" + variable[var]['title'],
                               len(variable[var]['bins']) - 1,
                               array('f', variable[var]['bins']))
            hist[s].Sumw2()
            cutstring = "(eventWeightLumi)" + ("*(" + cut + ")")
            if var == 'LeptonWeightUp':
                cutstring = "(eventWeightLumi * LeptonWeightUp/LeptonWeight)" + (
                    "*(" + cut + ")")
            elif var == 'LeptonWeightDown':
                cutstring = "(eventWeightLumi * LeptonWeightDown/LeptonWeight)" + (
                    "*(" + cut + ")")
            elif var == 'TriggerWeightUp':
                cutstring = "(eventWeightLumi * TriggerWeightUp/TriggerWeight)" + (
                    "*(" + cut + ")")
            elif var == 'TriggerWeightDown':
                cutstring = "(eventWeightLumi * TriggerWeightDown/TriggerWeight)" + (
                    "*(" + cut + ")")
            #division by BTagAk4Weight_deep is because the weighted samples are used
            elif BTagAK4deepup:
                cutstring = "(eventWeightLumi * BTagAK4Weight_deep_up/BTagAK4Weight_deep)" + (
                    "*(" + cut + ")")
            elif BTagAK4deepdown:
                cutstring = "(eventWeightLumi * BTagAK4Weight_deep_down/BTagAK4Weight_deep)" + (
                    "*(" + cut + ")")
            elif BTagAK8deep:
                cutstring = "(eventWeightLumi * BTagAK8Weight_deep)" + (
                    "*(" + cut + ")")
            elif BTagAK8deepup:
                cutstring = "(eventWeightLumi * BTagAK8Weight_deep_up)" + (
                    "*(" + cut + ")")
            elif BTagAK8deepdown:
                cutstring = "(eventWeightLumi * BTagAK8Weight_deep_down)" + (
                    "*(" + cut + ")")
            tree[s].Project(s, var, cutstring)
            if not tree[s].GetTree() == None:
                hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())
        """
        for j, ss in enumerate(sample[s]['files']):
            file[ss] = TFile(NTUPLEDIR + ss + ".root", "READ")
            tmp = file[ss].Get("Sys/"+sys)
            if tmp == None: continue
            if not s in hist.keys(): hist[s] = tmp
            else:
                if antiCorr:
                    for x in range(1, hist[s].GetNbinsX()+1): hist[s].SetBinContent(x, hist[s].GetBinContent(x)+tmp.GetBinContent(tmp.GetNbinsX()+1-x))
                else: hist[s].Add( tmp )
            
            if isShape:
                tmp = file[ss].Get("Sys/"+sys+"_up")
                print "Sys/"+sys+"_up"
                if tmp == None: continue
                if not s in histUp.keys(): histUp[s] = tmp
                else: histUp[s].Add( tmp )
                #
                tmp = file[ss].Get("Sys/"+sys+"_down")
                if tmp == None: continue
                if not s in histDown.keys(): histDown[s] = tmp
                else: histDown[s].Add( tmp )
            
        if 'accept' in sys:
            norm = None
            for j, ss in enumerate(sample[s]['files']):
                tmp = file[ss].Get("Sys/PDF_scale")
                if tmp == None: continue
                if norm == None: norm = tmp
                else: norm.Add( tmp )
            hist[s].Divide(norm)
        """
        if (isShape):
            shape = TF1("shape", "gaus", 0, 5000)
            shapeUp = TF1("shapeUp", "gaus", 0, 5000)
            shapeDown = TF1("shapeDown", "gaus", 0, 5000)
            hist[s].Fit(shape, "Q0", "")
            histUp[s].Fit(shapeUp, "Q0", "")
            histDown[s].Fit(shapeDown, "Q0", "")
            if 'scale' in sys or 'unc' in sys:
                up[s] = histUp[s].GetMean() / hist[s].GetMean()
                down[s] = histDown[s].GetMean() / hist[s].GetMean()
#                up[s] = shapeUp.GetParameter(1)/shape.GetParameter(1)
#                down[s] = shapeDown.GetParameter(1)/shape.GetParameter(1)
            elif 'res' in sys:
                up[s] = histUp[s].GetRMS() / hist[s].GetRMS()
                down[s] = histDown[s].GetRMS() / hist[s].GetRMS()


#                up[s] = shapeUp.GetParameter(2)/shape.GetParameter(2)
#                down[s] = shapeDown.GetParameter(2)/shape.GetParameter(2)
        else:
            up[s] = hist[s].GetBinContent(
                hist[s].FindBin(+1)) / hist[s].GetBinContent(
                    hist[s].FindBin(0))
            down[s] = hist[s].GetBinContent(
                hist[s].FindBin(-1)) / hist[s].GetBinContent(
                    hist[s].FindBin(0))
        gUp.SetPoint(i, m, up[s])
        gDown.SetPoint(i, m, down[s])
        #if mass < 1000: continue
        upAvg += up[s]
        downAvg += down[s]
        if abs(up[s]) > upMax: upMax = abs(up[s])
        if abs(up[s]) < upMin: upMin = abs(up[s])
        if abs(down[s]) > downMax: downMax = abs(down[s])
        if abs(down[s]) < downMin: downMin = abs(down[s])

    upAvg /= len(sign)
    downAvg /= len(sign)

    print " ---", sys, "--- | up: %.3f, down: %.3f, average: %.3f" % (
        upAvg, downAvg, abs(upAvg - 1 + 1. - downAvg) /
        2.), "|", "^{%.1f-%.1f}_{%.1f-%.1f}" % (100. * (1. - upMin), 100. *
                                                (1. - upMax), 100. *
                                                (downMin - 1.), 100. *
                                                (downMax - 1.))

    c1 = TCanvas("c1", "Signals", 800, 600)
    c1.cd()
    c1.GetPad(0).SetTicky(2)

    gUp.Draw("AL")
    gDown.Draw("SAME, L")
    gUp.GetYaxis().SetRangeUser(0.65, 1.35)
    gUp.GetXaxis().SetTitle("m_{X} (GeV)")
    gUp.GetYaxis().SetTitle("Uncertainty")

    leg = TLegend(0.5, 0.90 - 0.20, 0.9, 0.90)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetHeader(sys.replace('_', ' '))
    leg.AddEntry(gUp, "+1 s. d. (%.1f%%)" % (100. * (upAvg - 1.)), "l")
    leg.AddEntry(gDown, " -1 s. d. (%.1f%%)" % (100. * (1. - downAvg)), "l")
    leg.Draw()

    drawCMS(-1, "Simulation", False)
    c1.Update()

    filename = sys
    if sys.startswith('W_mass') or sys.startswith('Z_mass'):
        filename += "_" + sign[0][:3]
    #    c1.Print("plots/Systematics/"+filename+".png")
    #    c1.Print("plots/Systematics/"+filename+".pdf")
    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
    if 'doubleB' in sys or 'subjet' in sys or 'mass' in sys:
        print sys + " = {",
        for m in [
                800, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800,
                2000, 2500, 3000, 3500, 4000, 4500
        ]:
            print "%d : [%.3f, %.3f], " % (m, gUp.Eval(m), gDown.Eval(m)),
        print "}"
    if 'extr' in sys or 'tagging' in sys:
        print sys + " = {",
        for m in [800, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]:
            print "%d : [%.3f, %.3f], " % (m, gUp.Eval(m), gDown.Eval(m)),
        print "}"
    if 'QCD_scale' in sys or 'PDF_scale' in sys:
        print sys + " = {",
        for m in range(800, 4500 + 1, 10):
            print "%d : [%.3f, %.3f], " % (m, gUp.Eval(m), gDown.Eval(m)),
        print "}"
def plot(var, cut):
    ### Preliminary Operations ###
    treeRead = not 'cutflow' in var
    #treeRead = False
    channel = cut
    if 'inc' in cut:
        eventWeightLuminame = 'eventWeightLumi_nobtag'
    else:
        eventWeightLuminame = 'eventWeightLumi'
    if "SB" in cut or "SR" in cut:
        channel_name = cut[:-2]
    else:
        channel_name = cut
    cut = 'mmincSB_noisocut'
    isBlind = BLIND and 'SR' in channel
    if year in ['2016', '2017', '2018']:
        NTUPLEDIR = "/work/pbaertsc/heavy_resonance/Ntuples%s/" % (year)
    else:
        NTUPLEDIR = "/work/pbaertsc/heavy_resonance/"
    stype = "HVT model B"

    if treeRead:
        for k in sorted(selection.keys(), key=len, reverse=True):
            if k in cut: cut = cut.replace(k, selection[k])

    # Determine Primary Dataset
    pd = []
    if any(w in cut for w in ['mn', 'mm', 'isZtoMM', 'isWtoMN']):
        pd += [x for x in sample['data_obs']['files'] if 'SingleMuon' in x]
    if any(w in cut
           for w in ['en', 'ee', 'isWtoEN', 'isZtoEE', 'emqq', 'isTtoEM']):
        pd += [
            x for x in sample['data_obs']['files']
            if ('SingleElectron' in x or 'EGamma' in x)
        ]
    if any(w in cut for w in ['nn', 'isZtoNN']):
        pd += [x for x in sample['data_obs']['files'] if 'MET' in x]
    if len(pd) == 0:
        raw_input("Warning: Primary Dataset not recognized, continue?")
    if var == "PrefireWeight":
        pd = []

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree_noisocut = {}
    tree_isocut = {}
    hist_noisocut = {}
    hist_isocut = {}
    cutstring_noisocut = "%s" % eventWeightLuminame + ("*(Mu1_highPtId==2 &&" +
                                                       cut + ")")
    cutstring_isocut = "%s" % eventWeightLuminame + (
        "*(Mu1_relIso!=-1. && Mu1_relIso < 0.1 && Mu1_highPtId==2 &&" + cut +
        ")")
    #cutstring_noisocut = "%s" % eventWeightLuminame + ("*(Mu1_highPtId==1 &&"+cut+")")
    #cutstring_isocut = "%s" % eventWeightLuminame + ("*(Mu1_relIso!=-1. && Mu1_relIso < 0.1 && Mu1_highPtId==1 &&"+cut+")")
    """
    if var == 'Mu1_pt':
        cutstring_noisocut = "%s" % eventWeightLuminame + ("*(Mu1_highPtId==2 &&"+cut+")") 
        cutstring_isocut = "%s" % eventWeightLuminame + ("*(Mu1_relIso!=-1. && Mu1_relIso < 0.1 && Mu1_highPtId==2 &&"+cut+")") 
    elif var == 'Mu2_pt':
        cutstring_noisocut = "%s" % eventWeightLuminame + ("*(Mu2_highPtId==1 &&"+cut+")") 
        cutstring_isocut = "%s" % eventWeightLuminame + ("*(Mu2_relIso!=-1. && Mu2_relIso < 0.1 && Mu2_highPtId==1 &&"+cut+")") 
    """
    ### Create and fill MC histograms ###
    for i, s in enumerate(sign):
        if treeRead:  # Project from tree
            tree_noisocut[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree_noisocut[s].Add(NTUPLEDIR + ss + ".root")
            if variable[var]['nbins'] > 0:
                min_value = variable[var]['min']
                max_value = variable[var]['max']
                title = variable[var]['title']
                hist_noisocut[s] = TH1F(
                    s, ";" + title + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], min_value, max_value)
            else:
                hist_noisocut[s] = TH1F(s, ";" + variable[var]['title'],
                                        len(variable[var]['bins']) - 1,
                                        array('f', variable[var]['bins']))
            hist_noisocut[s].Sumw2()
            tree_noisocut[s].Project(s, var, cutstring_noisocut)
        hist_noisocut[s].Scale(
            sample[s]['weight'] if hist_noisocut[s].Integral() >= 0 else 0)

    for i, s in enumerate(sign):
        if treeRead:  # Project from tree
            tree_isocut[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree_isocut[s].Add(NTUPLEDIR + ss + ".root")
            if variable[var]['nbins'] > 0:
                min_value = variable[var]['min']
                max_value = variable[var]['max']
                title = variable[var]['title']
                hist_isocut[s] = TH1F(
                    s, ";" + title + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], min_value, max_value)
            else:
                hist_isocut[s] = TH1F(s, ";" + variable[var]['title'],
                                      len(variable[var]['bins']) - 1,
                                      array('f', variable[var]['bins']))
            hist_isocut[s].Sumw2()
            tree_isocut[s].Project(s, var, cutstring_isocut)
        hist_isocut[s].Scale(
            sample[s]['weight'] if hist_isocut[s].Integral() >= 0 else 0)

    for i, s in enumerate(sign):
        addOverflow(hist_noisocut[s], False)  # Add overflow
    for i, s in enumerate(sign):
        addOverflow(hist_isocut[s], False)  # Add overflow
    for i, s in enumerate(sign):
        hist_noisocut[s].SetLineWidth(3)
    for i, s in enumerate(sign):
        hist_isocut[s].SetLineWidth(3)
    for i, s in enumerate(sign):
        sample[s][
            'plot'] = True  #sample[s]['plot'] and s.startswith(channel[:2])

    # Create stack
    SigSum_noisocut = hist_noisocut[sign[0]].Clone("SigSum_noisocut")
    SigSum_noisocut.Reset("MICES")
    for i, s in enumerate(sign):
        SigSum_noisocut.Add(hist_noisocut[s], 1)
    SigSum_isocut = hist_isocut[sign[0]].Clone("SigSum_isocut")
    SigSum_isocut.Reset("MICES")
    for i, s in enumerate(sign):
        SigSum_isocut.Add(hist_isocut[s], 1)

    SigSum_noisocut.Divide(SigSum_isocut)

    SigSum_noisocut.GetYaxis().SetRangeUser(0.98, 1.05)
    c1 = TCanvas("c1", "Efficiency Mu Iso", 800, 800)
    SigSum_noisocut.Draw("HIST")
    drawCMS(LUMI, year, "Preliminary")
    drawRegion('XVH' + channel, True)
    drawAnalysis(channel)
    c1.Print("plotsSF/MuIsoEff_highPtId.png")
    c1.Print("plotsSF/MuIsoEff_highPtId.pdf")