Пример #1
0
def plot1D(variable, binnings, cutstring, histos, lumi, pdffile, ymin=1e-1, ymax=1e5, showdata = False):

    contains_data = False
    for label in histos.keys():
        if not "Data" in label:
            histos[label].Scale(lumi)
        else:
            contains_data = True
    
    for label in histos:
        histos[label].SetLineWidth(2)
        shared_utils.histoStyler(histos[label])

    histolist = [histos[histos.keys()[0]]]
    for label in histos:
        if not "Data" in label and not "Signal" in label:
            histolist.append(histos[label])

    legend = shared_utils.mklegend(x1 = 0.6, y1 = 0.4, x2 = 0.9, y2 = 0.8)

    canvas = shared_utils.mkcanvas()
    canvas.SetFillStyle(4000)    
      
    lumi = float("%.2f" % (lumi/1e3))

    if contains_data and showdata:
        hratio, pad1, pad2 = shared_utils.FabDraw(canvas, legend, histolist[0], histolist[1:], lumi = lumi, datamc = 'Data')
    else:
        print "No data"
        hratio, pad1, pad2 = shared_utils.FabDraw(canvas, legend, histolist[-1], histolist, lumi = lumi, datamc = 'Data')
        histolist[-1].SetTitle("")

    for i_label, label in enumerate(histos):
        histos[label].GetYaxis().SetRangeUser(ymin, ymax)

    for label in histos:
        if "Signal" in label:
            histos[label].Draw("same")
            legend.AddEntry(histos[label], histos[label].GetTitle())
       
    hratio.GetYaxis().SetRangeUser(-0.1,2.6)    
    hratio.GetYaxis().SetTitle('Data/MC')

    xlabel = variable
    xlabel = xlabel.replace("leptons_mt", "m_{T}^{lepton} (GeV)")
    xlabel = xlabel.replace("leadinglepton_mt", "m_{T}^{lepton} (GeV)")
    xlabel = xlabel.replace("dilepton_invmass", "m_{ll} (GeV)")
    hratio.GetXaxis().SetTitle(xlabel)

    for ibin in range(1,hratio.GetXaxis().GetNbins()+1):
        if hratio.GetBinContent(ibin)==0:
            hratio.SetBinContent(ibin,-999)
    hratio.SetMarkerColor(kBlack)

    if "/" in pdffile:
        os.system("mkdir -p %s" % "/".join(pdffile.split("/")[:-1]))
    canvas.SaveAs(pdffile + ".pdf")
Пример #2
0
    def makeplot(stacked_histograms, datahist, plotlabel, ratiovalues = False, ratiolabel = False, ratio_limits = False, header = False):

        foldername = outputfolder + "/%s/%s%s%s_%sFR" % (data_period, category.replace("short", "Short").replace("long", "Long"), region, plotlabel, fakeratevariable.replace("_", "").replace(":", "-"))

        if use_prompt_fakesubtraction:
            foldername += "_FakeSubtraction"
        
        canvas = shared_utils.mkcanvas()
        legend = shared_utils.mklegend(x1 = 0.6, y1 = 0.4, x2 = 0.9, y2 = 0.8)
        
        if header:
            legend.SetHeader(header)
        else:
            legend.SetHeader("%s, %s tracks" % (region, category))
    
        ymin = 1e0; ymax = 1e2
        datahist.GetYaxis().SetRangeUser(ymin, ymax)
        for stacked_histogram in stacked_histograms:
            stacked_histogram.GetYaxis().SetRangeUser(ymin, ymax)
            stacked_histogram.GetYaxis().SetLimits(ymin, ymax)
    
        hratio, pads = shared_utils.FabDraw(canvas, legend, datahist, stacked_histograms, datamc = 'Data', lumi = lumi/1e3)
        stacked_histograms[-1].SetTitle("")
        
        if ratio_limits:
            hratio.GetYaxis().SetRangeUser(ratio_limits[0], ratio_limits[1])
        else:
            hratio.GetYaxis().SetRangeUser(-0.1,2.6)
        
        if "Run201" in dataid:
            hratio.GetYaxis().SetTitle('Data/prediction')
        else:
            hratio.GetYaxis().SetTitle('Fake pred./truth')
            
        if ratiolabel:
            hratio.GetYaxis().SetTitle(ratiolabel)
        
        xlabel = variable
        xlabel = xlabel.replace("leptons_mt", "m_{T}^{lepton} (GeV)")
        xlabel = xlabel.replace("leadinglepton_mt", "m_{T}^{lepton} (GeV)")
        hratio.GetXaxis().SetTitle(xlabel)
    
        for ibin in range(1,hratio.GetXaxis().GetNbins()+1):
            if ratiovalues:
                hratio.SetBinContent(ibin, ratiovalues.GetBinContent(ibin))
            else:
                if hratio.GetBinContent(ibin)==0:
                    hratio.SetBinContent(ibin,-999)
        hratio.SetMarkerColor(kBlack)
        
        os.system("mkdir -p " + foldername)
        canvas.SaveAs(foldername + "/" + pdffile + ".png")
Пример #3
0
gROOT.SetBatch(True)
gStyle.SetOptStat(0)
TH1D.SetDefaultSumw2()

tmva_folders = glob.glob("optimize-*/output.root")
rocarea = {}
ks_signal = {}
ks_background = {}

for phase in ["2016", "2017"]:
    for category in ["short", "long"]:

        histos = {}
        colors = range(209, 270)

        canvas = shared_utils.mkcanvas("c1")
        legend = shared_utils.mklegend(x1=.17, y1=.17, x2=.75, y2=.6)
        legend.SetTextSize(0.035)

        rocarea["%s, %s tracks" % (phase, category)] = TH1F(
            "rocarea", ";depth, number of trees;area under ROC", 55, 0, 55)
        ks_signal["%s, %s tracks" % (phase, category)] = TH1F(
            "ks_signal", ";depth, number of trees;KS test (signal)", 55, 0, 55)
        ks_background["%s, %s tracks" % (phase, category)] = TH1F(
            "ks_background", ";depth, number of trees;KS test (background)",
            55, 0, 55)
        shared_utils.histoStyler(rocarea["%s, %s tracks" % (phase, category)])
        shared_utils.histoStyler(ks_signal["%s, %s tracks" %
                                           (phase, category)])
        shared_utils.histoStyler(ks_background["%s, %s tracks" %
                                               (phase, category)])
Пример #4
0
def get_reweighting_factor(histofolder, suffix):

    periods = [
        "Summer16",
        "Fall17",
        "Autumn18",
        "Run2016B",
        "Run2016C",
        "Run2016D",
        "Run2016E",
        "Run2016F",
        "Run2016G",
        "Run2016H",
        "Run2017B",
        "Run2017C",
        "Run2017D",
        "Run2017E",
        "Run2017F",
        "Run2018A",
        "Run2018B",
        "Run2018C",
        "Run2018D",
    ]

    histolabels = [
        #"track_nValidPixelHits",
        #"track_nValidPixelHits_short",
        #"track_nValidPixelHits_long",
        "h_muonPtCand",
        #"h_muonPt2Cand",
        "h_muonPt",
        #"track_pt",
        "track_pt_short",
        "track_pt_long",
    ]

    hists = {}
    for period in periods:
        hists[period] = {}
        for label in histolabels:
            fin = TFile(
                "%s/histograms%s_%s.root" % (histofolder, suffix, period),
                "open")
            print label, histofolder, suffix, period
            hists[period][label] = fin.Get("Histograms/" + label)
            hists[period][label].SetDirectory(0)
            hists[period][label].SetLineWidth(2)
            shared_utils.histoStyler(hists[period][label])
            fin.Close()

    print "all loaded"

    # hweight = histTarget_NPixHits.Clone(); hweight.Divide(histSimulation)
    hweight = {}

    for label in histolabels:

        hweight[label] = {}

        for year in ["2016", "2017", "2018"]:
            canvas = shared_utils.mkcanvas()
            legend = shared_utils.mklegend(x1=0.6, y1=0.6, x2=0.85, y2=0.85)
            colors = range(209, 250)[::3]

            for i, period in enumerate(sorted(periods)):

                if year not in period: continue

                if "Run2016" in period:
                    mc = "Summer16"
                elif "Run2017" in period:
                    mc = "Fall17"
                elif "Run2018" in period:
                    mc = "Autumn18"
                else:
                    continue

                #num = hists[period]["track_nValidPixelHits"].Clone()
                #denom = hists[mc]["track_nValidPixelHits"].Clone()
                num = hists[period][label].Clone()
                denom = hists[mc][label].Clone()

                if num.Integral() > 0:
                    num.Scale(1.0 / num.Integral())
                else:
                    print label, year, period, num.Integral()

                if denom.Integral() > 0:
                    denom.Scale(1.0 / denom.Integral())
                else:
                    print label, year, period, denom.Integral()

                hweight[label][period] = num.Clone()
                hweight[label][period].Divide(denom)
                hweight[label][period].SetName(period + "_" + label)
                shared_utils.histoStyler(hweight[label][period])
                hweight[label][period].SetLineColor(colors.pop(0))
                hweight[label][period].GetYaxis().SetRangeUser(0, 5)
                #hweight[label][period].SetTitle(";number of pixel hits;weight")
                hweight[label][period].SetTitle(";track p_{T} (GeV);weight")

                hweight[label][period].GetXaxis().SetRangeUser(0, 200)

                if i == 0:
                    hweight[label][period].Draw("hist e")
                else:
                    hweight[label][period].Draw("hist e same")

                # Draw overflow:
                last_bin = hweight[label][period].GetNbinsX() + 1
                overflow = hweight[label][period].GetBinContent(last_bin)
                print overflow
                hweight[label][period].AddBinContent((last_bin - 1), overflow)

                legend.SetTextSize(0.035)
                legend.AddEntry(hweight[label][period], period)

            legend.Draw()
            shared_utils.stamp()
            canvas.SaveAs("plots/hweights_%s_%s.pdf" % (label, year))

    # save weights:
    fout = TFile("hweights.root", "recreate")
    for label in histolabels:
        for period in hweight[label]:
            hweight[label][period].Write()
    fout.Close()
Пример #5
0
def combinedplots(channel,
                  variable,
                  outputfolder,
                  folderlabel,
                  cuts_channel,
                  skim_folder,
                  use_or_trigger=True):

    if channel == "SEl" and "leadingmuon" in variable:
        return
    if channel == "SMu" and "leadingelectron" in variable:
        return
    if channel == "MHT" and "leading" in variable:
        return

    numevents = -1

    pdffile = "%s/triggereff_%s_%s.pdf" % (outputfolder, folderlabel,
                                           variable.replace(":", "_"))

    histos = {}

    # select/unselect specific run period:
    period = ""

    for year in [
            2016,
            2017,
            2018,
    ]:

        # switches
        if "switchdenom" in folderlabel:
            glob_sel_num = "Run%s%s*SingleMuon*.root" % (year, period)
            glob_sel_denom = "Run%s%s*SingleMuon*.root" % (year, period)
            glob_smu_num = "Run%s%s*SingleElectron*.root" % (year, period)
            glob_smu_denom = "Run%s%s*SingleElectron*.root" % (year, period)
            if channel == "SEl":
                denom = "triggered_singlemuon==1"
                denom_label = "mu trigger"
                extra_label = "SingleMuon dataset"
            elif channel == "SMu":
                denom = "triggered_singleelectron==1"
                denom_label = "el trigger"
                extra_label = "SingleElectron dataset"

        elif "jethtother" in folderlabel:
            glob_sel_num = "Run%s%s*JetHT*.root" % (year, period)
            glob_sel_denom = "Run%s%s*SingleMuon*.root" % (year, period)
            glob_smu_num = "Run%s%s*JetHT*.root" % (year, period)
            glob_smu_denom = "Run%s%s*SingleElectron*.root" % (year, period)
            if channel == "SEl":
                denom = "triggered_singlemuon==1"
                denom_label = "mu trigger"
                extra_label = "num.: JetHT, denom.: SingleMu"
            elif channel == "SMu":
                denom = "triggered_singleelectron==1"
                denom_label = "el trigger"
                extra_label = "num.: JetHT, denom.: SingleEl"

        elif "jetht" in folderlabel:
            glob_sel_num = "Run%s%s*JetHT*.root" % (year, period)
            glob_sel_denom = "Run%s%s*JetHT*.root" % (year, period)
            glob_smu_num = "Run%s%s*JetHT*.root" % (year, period)
            glob_smu_denom = "Run%s%s*JetHT*.root" % (year, period)
            denom = "triggered_ht==1"
            denom_label = "HT trigger"
            extra_label = "JetHT dataset"

        else:
            glob_sel_num = "Run%s%s*MET*.root" % (year, period)
            glob_sel_denom = "Run%s%s*MET*.root" % (year, period)
            glob_smu_num = "Run%s%s*MET*.root" % (year, period)
            glob_smu_denom = "Run%s%s*MET*.root" % (year, period)
            denom = "triggered_met==1"
            denom_label = "MHT trigger"
            extra_label = "MET dataset"
            use_or_trigger = False

        if channel == "MHT":
            glob_mht_num = "Run%s%s*SingleElectron*.root" % (year, period)
            glob_mht_denom = "Run%s%s*SingleElectron*.root" % (year, period)
            denom = "triggered_singleelectron==1"
            denom_label = "el. trigger"
            extra_label = "SingleElectron dataset"

        if ":" in variable:
            nMinus1 = False
            extra_label += ", trigger efficiency"
        else:
            if variable == "MHT" and "DtTurnon" in folderlabel:
                # don't peek into SR
                nMinus1 = False
            else:
                nMinus1 = True

        nBinsX = binnings[variable][0]
        xmin = binnings[variable][1]
        xmax = binnings[variable][2]

        if ":" in variable:
            nBinsY = binnings[variable][3]
            ymin = binnings[variable][4]
            ymax = binnings[variable][5]
        else:
            nBinsY = False
            ymin = False
            ymax = False

        def get_treff_histogram(globstring, variable, cutA, cutB):
            if year == 2018 and "SingleElectron" in globstring:
                globstring = globstring.replace("SingleElectron", "EGamma")
                extra_label = "Egamma dataset"
                if ":" in variable:
                    extra_label = "Egamma dataset, trigger efficiency"
            histo = plotting.get_all_histos([skim_folder + "/" + globstring],
                                            "Events",
                                            variable,
                                            numevents=numevents,
                                            nMinus1=nMinus1,
                                            cutstring=cuts_channel + cutA +
                                            cutB,
                                            nBinsX=nBinsX,
                                            xmin=xmin,
                                            xmax=xmax,
                                            nBinsY=nBinsY,
                                            ymin=ymin,
                                            ymax=ymax)
            histo.SetDirectory(0)
            return histo

        # denominator:
        if channel == "MHT":
            histos["num_%s_%s_%s" %
                   (variable, channel, year)] = get_treff_histogram(
                       glob_mht_num, variable, denom, " && triggered_met==1")
            histos["denom_%s_%s_%s" %
                   (variable, channel, year)] = get_treff_histogram(
                       glob_mht_denom, variable, denom, "")
        elif channel == "SEl":
            if use_or_trigger:
                histos[
                    "num_%s_%s_%s" %
                    (variable, channel, year)] = get_treff_histogram(
                        glob_sel_num, variable, denom,
                        " && (triggered_met==1 || triggered_singleelectron==1)"
                    )
            else:
                histos["num_%s_%s_%s" %
                       (variable, channel, year)] = get_treff_histogram(
                           glob_sel_num, variable, denom,
                           " && triggered_singleelectron==1")
            histos["denom_%s_%s_%s" %
                   (variable, channel, year)] = get_treff_histogram(
                       glob_sel_denom, variable, denom, "")
        elif channel == "SMu":
            if use_or_trigger:
                histos["num_%s_%s_%s" %
                       (variable, channel, year)] = get_treff_histogram(
                           glob_smu_num, variable, denom,
                           " && (triggered_met==1 || triggered_singlemuon==1)")
            else:
                histos["num_%s_%s_%s" %
                       (variable, channel, year)] = get_treff_histogram(
                           glob_smu_num, variable, denom,
                           " && triggered_singlemuon==1")
            histos["denom_%s_%s_%s" %
                   (variable, channel, year)] = get_treff_histogram(
                       glob_smu_denom, variable, denom, "")

    c1 = shared_utils.mkcanvas("c1")

    #legend = TLegend(0.5, 0.2, 0.88, 0.4)
    legend = TLegend(0.55, 0.2, 0.88, 0.5)
    legend.SetTextSize(0.03)
    legend.SetBorderSize(0)
    legend.SetFillStyle(0)

    if ":" not in variable:
        if isinstance(binnings[variable][0], list):
            histo = TH1F("histo", "",
                         len(binnings[variable][0]) - 1,
                         array('d', binnings[variable][0]))
        else:
            histo = TH1F("histo", "", binnings[variable][0],
                         binnings[variable][1], binnings[variable][2])
    else:
        if isinstance(binnings[variable][0], list):
            histo = TH2F("histo", "",
                         len(binnings[variable][0]) - 1,
                         array('d', binnings[variable][0]),
                         len(binnings[variable][3]) - 1,
                         array('d', binnings[variable][3]))
        else:
            histo = TH2F("histo", "", binnings[variable][0],
                         binnings[variable][1], binnings[variable][2],
                         binnings[variable][3], binnings[variable][4],
                         binnings[variable][5])

    if ":" not in variable:
        htitle = ";%s; trigger efficiency #epsilon" % binnings[variable][3]
    else:
        htitle = ";%s; trigger efficiency #epsilon" % binnings[variable][6]
    histo.SetTitle(htitle)

    shared_utils.histoStyler(histo)

    #FIXME
    gStyle.SetPaintTextFormat("4.2f")

    if ":" not in variable:
        histo.Draw("hist")
        histo.GetYaxis().SetRangeUser(0, 1.1)
        c1.SetLogy(False)
        c1.SetLogz(False)
    else:
        histo.Draw("colz text e")
        histo.GetZaxis().SetRangeUser(0, 1)
        histo.GetZaxis().SetTitle("trigger efficiency #epsilon")
        c1.SetRightMargin(.13)
        size = 0.15
        font = 132
        histo.GetZaxis().SetLabelFont(font)
        histo.GetZaxis().SetTitleFont(font)
        histo.GetZaxis().SetTitleSize(size)
        histo.GetZaxis().SetLabelSize(size)
        histo.GetZaxis().SetTitleOffset(1)
        c1.SetLogy(False)
        c1.SetLogz(False)

    h_effs = {}
    for i_year, year in enumerate([2016, 2017, 2018]):

        denom = histos["denom_%s_%s_%s" % (variable, channel, year)].Clone()
        num = histos["num_%s_%s_%s" % (variable, channel, year)].Clone()

        print "@@", num.GetEntries()
        print "@@", denom.GetEntries()

        if "normalizeDenom" in folderlabel:
            # calc. eff with normalizing to denominator
            numNormEff = num.Clone()
            denomNormEff = denom.Clone()
            if denom.Integral() > 0:
                numNormEff.Scale(1.0 / denom.Integral())
            if denom.Integral() > 0:
                denomNormEff.Scale(1.0 / denom.Integral())
            h_effs["eff_%s" % year] = numNormEff.Clone()
            h_effs["eff_%s" % year].Divide(denomNormEff)
            if "altern." not in extra_label:
                extra_label += "; altern."
        else:
            # default TEfficiency
            h_effs["eff_%s_TEff" % year] = TEfficiency(num.Clone(),
                                                       denom.Clone())

            # convert TEff to normal hist:
            h2dPass = h_effs["eff_%s_TEff" % year].GetPassedHistogram()
            h2dTotal = h_effs["eff_%s_TEff" % year].GetTotalHistogram()
            h_effs["eff_%s" % year] = h2dPass.Clone()
            h_effs["eff_%s" % year].Divide(h2dTotal)
            h_effs["eff_%s" % year].SetDirectory(0)
            shared_utils.histoStyler(h_effs["eff_%s" % year])
            h_effs["eff_%s" % year].SetTitle(htitle)

        if ":" not in variable:
            # 1D histograms:
            h_effs["eff_%s" % year].Draw("same")
            h_effs["eff_%s" % year].SetLineWidth(2)

            if year == 2016:
                print "extra"
                numNorm = num.Clone()
                denomNorm = denom.Clone()
                if num.Integral() > 0:
                    numNorm.Scale(1.0 / num.Integral())
                if denom.Integral():
                    denomNorm.Scale(1.0 / denom.Integral())
                denomNorm.SetLineColor(kBlue)
                denomNorm.SetFillColor(kBlue)
                denomNorm.SetFillStyle(3354)
                denomNorm.Draw("hist f same")
                numNorm.SetLineColor(kTeal)
                numNorm.SetFillColor(kTeal)
                numNorm.SetFillStyle(3345)
                numNorm.Draw("hist f same")

                legend.AddEntry(numNorm, "2016 norm. numerator", "f")
                legend.AddEntry(denomNorm, "2016 norm. denominator", "f")
                #legend.AddEntry(numNorm, "2016 numerator", "f")
                #legend.AddEntry(denomNorm, "2016 denominator", "f")

        else:
            # 2D histograms:
            h_effs["eff_%s" % year].Draw("colz text e same")
            savetoroot(
                h_effs["eff_%s" % year], "h_triggereff_%s_%s_%s" %
                (channel, variable.replace(":", "_"), year), outputfolder,
                folderlabel)

            if "eff_%s_TEff" % year in h_effs:
                savetoroot(
                    h_effs["eff_%s_TEff" % year], "teff_triggereff_%s_%s_%s" %
                    (channel, variable.replace(":", "_"), year), outputfolder,
                    folderlabel)

            stamp_cuts(cuts_channel, channel, variable, use_or_trigger,
                       denom_label, extra_label)
            shared_utils.stamp()
            c1.SaveAs(pdffile.replace(".pdf", "_%s.pdf" % year))
            savetoroot(
                c1, "c_triggereff_%s_%s_%s" %
                (channel, variable.replace(":", "_"), year), outputfolder,
                folderlabel)

            continue

    if ":" in variable:
        return 0

    h_effs["eff_2016"].SetFillColorAlpha(0, 0)
    h_effs["eff_2017"].SetFillColorAlpha(0, 0)
    h_effs["eff_2018"].SetFillColorAlpha(0, 0)

    h_effs["eff_2016"].SetLineColor(kBlue)
    if channel == "MHT":
        legend.AddEntry(h_effs["eff_2016"], "2016")
    else:
        legend.AddEntry(h_effs["eff_2016"], "2016, %s" % channel)
    h_effs["eff_2017"].SetLineColor(kRed)
    if channel == "MHT":
        legend.AddEntry(h_effs["eff_2017"], "2017")
    else:
        legend.AddEntry(h_effs["eff_2017"], "2017, %s" % channel)
    h_effs["eff_2018"].SetLineColor(kGreen + 2)
    if channel == "MHT":
        legend.AddEntry(h_effs["eff_2018"], "2018")
    else:
        legend.AddEntry(h_effs["eff_2018"], "2018, %s" % channel)

    stamp_cuts(cuts_channel, channel, variable, use_or_trigger, denom_label,
               extra_label)
    shared_utils.stamp()

    legend.Draw()

    c1.SetGrid(True)

    c1.SaveAs(pdffile)
    savetoroot(
        h_effs["eff_2016"],
        "h_triggereff_%s_%s_2016" % (channel, variable.replace(":", "_")),
        outputfolder, folderlabel)
    savetoroot(
        h_effs["eff_2017"],
        "h_triggereff_%s_%s_2017" % (channel, variable.replace(":", "_")),
        outputfolder, folderlabel)
    savetoroot(
        h_effs["eff_2018"],
        "h_triggereff_%s_%s_2018" % (channel, variable.replace(":", "_")),
        outputfolder, folderlabel)

    if "eff_2016_TEff" in h_effs:
        print "@@@@@"
        savetoroot(
            h_effs["eff_2016_TEff"], "teff_triggereff_%s_%s_2016" %
            (channel, variable.replace(":", "_")), outputfolder, folderlabel)
        savetoroot(
            h_effs["eff_2017_TEff"], "teff_triggereff_%s_%s_2017" %
            (channel, variable.replace(":", "_")), outputfolder, folderlabel)
        savetoroot(
            h_effs["eff_2018_TEff"], "teff_triggereff_%s_%s_2018" %
            (channel, variable.replace(":", "_")), outputfolder, folderlabel)

    savetoroot(
        c1, "c_triggereff_%s_%s" % (folderlabel, variable.replace(":", "_")),
        outputfolder, folderlabel)
Пример #6
0
def plot2D(variable,
           cutstring,
           histos,
           lumi,
           pdffile,
           ymin=1e-1,
           ymax=1e5,
           showdata=False,
           drawoption="colz"):

    # BDT sideband region plot

    for label in histos:
        histos[label].SetLineWidth(2)
        shared_utils.histoStyler(histos[label])

        size = 0.059
        font = 132
        histos[label].GetZaxis().SetLabelFont(font)
        histos[label].GetZaxis().SetTitleFont(font)
        histos[label].GetZaxis().SetTitleSize(size)
        histos[label].GetZaxis().SetLabelSize(size)
        histos[label].GetZaxis().SetTitleOffset(1.2)

        histos[label].GetXaxis().SetNdivisions(5)

        histos[label].SetMarkerStyle(20)
        histos[label].SetMarkerSize(0.2)
        histos[label].SetMarkerColorAlpha(histos[label].GetFillColor(), 0.5)

        variable = variable.replace("tracks_mva_loose", "MVA score")
        variable = variable.replace("tracks_dxyVtx", "d_{xy} (cm)")
        histos[label].SetTitle(
            ";%s;%s;Events" % (variable.split(":")[1], variable.split(":")[0]))

    legend = shared_utils.mklegend(x1=0.6, y1=0.4, x2=0.9, y2=0.8)

    canvas = shared_utils.mkcanvas()
    canvas.SetLogz(True)
    canvas.SetRightMargin(0.2)

    # combine backgrounds:
    histos["CombinedBg"] = 0
    for label in histos:
        if "Signal" not in label and "CombinedBg" not in label:
            if histos["CombinedBg"] == 0:
                histos["CombinedBg"] = histos[label].Clone()
            else:
                histos["CombinedBg"].Add(histos[label])
    if histos["CombinedBg"] == 0:
        del histos["CombinedBg"]

    #if "/" in pdffile:
    #    os.system("mkdir -p %s" % "/".join(pdffile.split("/")[:-1]))
    #    os.chdir("/".join(pdffile.split("/")[:-1]))

    for label in histos:

        if "CombinedBg" in label or "Signal" in label:

            histos[label].Draw(drawoption)
            shared_utils.stamp()
            canvas.SaveAs(pdffile + "_" +
                          label.replace(":", "_").replace(" ", "_") + ".pdf")

            # draw some funky lcines
            text = TLatex()
            text.SetTextFont(132)
            text.SetTextSize(0.059)

            if "short" in pdffile:
                #upper_line = TLine(0, -0.5, (1 + 0.5)/(0.65/0.01), 1)
                upper_line = TLine(0, -0.5, (0.8 + 0.5) / (0.65 / 0.01), 0.8)
                upper_line.SetLineWidth(2)
                upper_line.Draw("same")
                lower_line = TLine(0.02, -1, 0.02, 1)
                lower_line.SetLineWidth(2)
                lower_line.Draw("same")

                text.DrawLatex(0.005, 0.75, "SR")
                text.DrawLatex(0.03, 0.75, "CR")

            if "long" in pdffile:
                upper_line = TLine(0, -0.05, (1 + 0.05) / (0.7 / 0.01), 1)
                upper_line.SetLineWidth(2)
                upper_line.Draw("same")
                lower_line = TLine(0.02, -1, 0.02, 1)
                lower_line.SetLineWidth(2)
                lower_line.Draw("same")

                text.DrawLatex(0.003, 0.75, "SR")
                text.DrawLatex(0.03, 0.75, "CR")

            canvas.SaveAs(pdffile + "_" +
                          label.replace(":", "_").replace(" ", "_") +
                          "_lines.pdf")
Пример #7
0
def plot1D(variable,
           cutstring,
           histos,
           lumi,
           pdffile,
           xlabel=False,
           ymin=1e-1,
           ymax=1e5,
           showdata=True):

    contains_data = False
    for label in histos.keys():
        if not "Data" in label:
            histos[label].Scale(lumi)
            print "scaling with", lumi
        else:
            contains_data = True
        if "Signal" in label:
            histos[label].SetLineWidth(2)

    if contains_data:
        histolist = [histos[histos.keys()[0]]]
    else:
        histolist = []

    histolistbg = []
    for label in histos:
        if not "Data" in label and not "Signal" in label:
            histolistbg.append(histos[label])
    histolistbg = sorted(histolistbg, key=lambda item: item.Integral())
    histolist += histolistbg

    legend = shared_utils.mklegend(x1=0.6, y1=0.4, x2=0.9, y2=0.8)

    canvas = shared_utils.mkcanvas()
    canvas.SetFillStyle(4000)

    lumi = float("%.2f" % (lumi / 1e3))

    if contains_data and showdata:
        hratio, pads = shared_utils.FabDraw(canvas,
                                            legend,
                                            histolist[0],
                                            histolist[1:],
                                            lumi=lumi,
                                            datamc='Data')
    else:
        print "No data"

        example = histos[histos.keys()[0]].Clone()
        print "example.GetXaxis().GetNbins()", example.GetXaxis().GetNbins()
        print "example.GetXaxis().GetBinLowEdge(0)", example.GetXaxis(
        ).GetBinLowEdge(1)
        print "example.GetXaxis().GetBinLowEdge(example.GetXaxis().GetNbins()+1)", example.GetXaxis(
        ).GetBinLowEdge(example.GetXaxis().GetNbins() + 1)

        empty_histo = TH1D(
            "Data", "Data",
            example.GetXaxis().GetNbins(),
            example.GetXaxis().GetBinLowEdge(1),
            example.GetXaxis().GetBinLowEdge(example.GetXaxis().GetNbins() +
                                             1))
        shared_utils.histoStyler(empty_histo)

        print canvas, legend, empty_histo, histolist, lumi

        hratio, pads = shared_utils.FabDraw(canvas,
                                            legend,
                                            empty_histo,
                                            histolist,
                                            lumi=lumi,
                                            datamc='MC')
        histolist[-1].SetTitle("")

    for i_label, label in enumerate(histos):
        histos[label].GetYaxis().SetRangeUser(ymin, ymax)

    for label in histos:
        if "Signal" in label:
            histos[label].Draw("same")
            legend.AddEntry(histos[label], histos[label].GetTitle())

    hratio.GetYaxis().SetRangeUser(-0.1, 2.6)
    hratio.GetYaxis().SetTitle('Data/MC')

    if xlabel == False:
        xlabel = variable
    hratio.GetXaxis().SetTitle(str(xlabel))

    for ibin in range(1, hratio.GetXaxis().GetNbins() + 1):
        if hratio.GetBinContent(ibin) == 0:
            hratio.SetBinContent(ibin, -999)
    hratio.SetMarkerColor(kBlack)

    if "/" in pdffile:
        os.system("mkdir -p %s" % "/".join(pdffile.split("/")[:-1]))
    canvas.SaveAs(pdffile + ".pdf")
Пример #8
0
def plot_run_periods(variable, prediction_folder, header):
    
    histos = collections.OrderedDict()
    
    for period in ["Summer16", "Run2016B_MET", "Run2016C_MET", "Run2016D_MET", "Run2016E_MET", "Run2016F_MET", "Run2016G_MET", "Run2016H_MET"]:
        iFile = prediction_folder + "/prediction_%s.root" % period
        print iFile
        fin = TFile(iFile, "read")
        
        histos[period] = fin.Get(variable)
        print histos[period].GetEntries()
        histos[period].SetDirectory(0)
        fin.Close()
    
    canvas = shared_utils.mkcanvas()
    canvas.SetLogy(True)
    
    legend = TLegend(0.55, 0.6, 0.88, 0.88)
    legend.SetHeader(header)
    legend.SetTextSize(0.025)
    legend.SetBorderSize(0)    
    
    colors = [kBlack, kRed, kBlue, kGreen, kOrange, kAzure, kMagenta, kYellow, kTeal]
    
    for i_label, label in enumerate(histos):
        
        if i_label == 0:
            histos[label].Draw("h")
        else:
            histos[label].Draw("h same")
    
        shared_utils.histoStyler(histos[label])

        xlabel = variable.split("_")[0]
        if "DeDx" in xlabel:
            xlabel += " (Mev/cm)"

        color = colors.pop(0)
        histos[label].SetLineColor(color)
        histos[label].SetFillColor(0)
        histos[label].SetMarkerSize(0)
        if histos[label].Integral()>0:
            histos[label].Scale(1.0/histos[label].Integral())
        histos[label].SetTitle(";%s;Events" % xlabel)
        histos[label].GetXaxis().SetRangeUser(1.5,7.5)
        histos[label].GetYaxis().SetRangeUser(5e-4,1e-1)
        
        # fit histograms:

        if "DeDx_" in variable:
            if "Summer16" in label:
                fit_x = TF1("fit_x", "gaus", 2.5, 3.6)
            else:
                fit_x = TF1("fit_x", "gaus", 1.6, 2.8)
            fit_x.SetLineColor(kRed)
            fit_x.SetLineWidth(2)
            histos[label].Fit("fit_x", "r")

            legend.AddEntry(histos[label], label + ", (#mu=%1.2f)" % fit_x.GetParameter(1))

        else:

            legend.AddEntry(histos[label], label)


    legend.Draw()
    shared_utils.stamp()
    canvas.SaveAs("fakebg_%s.pdf" % variable)
Пример #9
0
def significances(sg_filelist, bg_filelist, phase, batchname, signalcut=""):

    bdt_short = " && ".join(cutflow.cuts["BDT_short"][:-2])
    bdt_long = " && ".join(cutflow.cuts["BDT_long"][:-2])

    #FIXME phase 1 dE/dx
    if phase == 1:
        bdt_short = bdt_short.replace("tracks_deDxHarmonic2pixel>2.0",
                                      "MHT>=0")
        bdt_long = bdt_long.replace("tracks_deDxHarmonic2pixel>2.0", "MHT>=0")
        lumi = 137000
    else:
        lumi = 35000

    histos = collections.OrderedDict()
    drawoptions = collections.OrderedDict()

    # get histograms:

    def fill_histos(label, variable, shortcuts, longcuts):
        histos["bg_short_%s" % label] = plotting.get_all_histos(
            bg_filelist,
            "Events",
            variable,
            "tracks_is_pixel_track==1 && " + shortcuts,
            nBinsX=200,
            xmin=-1,
            xmax=1)
        histos["bg_long_%s" % label] = plotting.get_all_histos(
            bg_filelist,
            "Events",
            variable,
            "tracks_is_pixel_track==0 && tracks_nMissingOuterHits>=2 && " +
            longcuts,
            nBinsX=200,
            xmin=-1,
            xmax=1)
        histos["sg_short_%s" % label] = plotting.get_all_histos(
            sg_filelist,
            "Events",
            variable,
            "tracks_is_pixel_track==1 && tracks_chiCandGenMatchingDR<0.01 && "
            + shortcuts + signalcut,
            nBinsX=200,
            xmin=-1,
            xmax=1)
        histos["sg_long_%s" % label] = plotting.get_all_histos(
            sg_filelist,
            "Events",
            variable,
            "tracks_is_pixel_track==0 && tracks_nMissingOuterHits>=2 && tracks_chiCandGenMatchingDR<0.01 && "
            + longcuts + signalcut,
            nBinsX=200,
            xmin=-1,
            xmax=1)

    # get common denominator:
    fill_histos(
        "study", "tracks_mva_tight_may20_chi2_pt15",
        "tracks_pt>15 && tracks_matchedCaloEnergy/tracks_p<0.20 && " +
        bdt_short,
        "tracks_pt>40 && tracks_matchedCaloEnergy/tracks_p<0.20 && " +
        bdt_long)
    fill_histos("denom", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15",
                "tracks_pt>40")

    # scale with lumi
    for label in histos:
        shared_utils.histoStyler(histos[label])
        histos[label].Scale(lumi)

    # get efficiencies:
    efficiencies = {}

    for label in histos:

        if "denom" in label: continue

        efficiencies[label] = []

        #denominator = histos[label.replace("study", "denom")].Integral(histos[label].GetXaxis().FindBin(-1), histos[label].GetXaxis().FindBin(1))
        denominator = histos[label.replace("study", "denom")].Integral()
        #denominator = histos[label].Integral(histos[label].GetXaxis().FindBin(-1), histos[label].GetXaxis().FindBin(1))

        for i_score in numpy.arange(-1.0, 1.0, 0.005):
            numerator = histos[label].Integral(
                histos[label].GetXaxis().FindBin(i_score),
                histos[label].GetXaxis().FindBin(1))
            if denominator > 0:
                efficiencies[label].append(
                    [i_score, numerator / denominator, numerator])
            else:
                efficiencies[label].append([i_score, 0, numerator])

    # build TGraphs
    graphs_roc = {}
    graphs_sgeff = {}
    graphs_bgeff = {}
    graphs_significance = {}
    scalingfactor = 1.0
    signlabel = ""

    for label in efficiencies:

        if "bg" in label: continue
        graphs_roc[label] = TGraph()
        graphs_sgeff[label] = TGraph()
        graphs_bgeff[label] = TGraph()
        graphs_significance[label] = TGraph()

        for i in range(len(efficiencies[label])):
            score = efficiencies[label][i][0]
            eff_sg = efficiencies[label][i][1]
            eff_bg = efficiencies[label.replace("sg", "bg")][i][1]
            N_sg = efficiencies[label][i][2]
            N_bg = efficiencies[label.replace("sg", "bg")][i][2]
            graphs_roc[label].SetPoint(graphs_roc[label].GetN(), eff_sg,
                                       1 - eff_bg)
            graphs_sgeff[label].SetPoint(graphs_sgeff[label].GetN(), score,
                                         eff_sg)
            graphs_bgeff[label].SetPoint(graphs_bgeff[label].GetN(), score,
                                         eff_bg)

            try:
                #N = 1000
                #significance = N * eff_sg / math.sqrt(N*eff_sg+N*eff_bg)
                #signlabel = "#epsilon_{sg} / #sqrt{#epsilon_{sg} + #epsilon_{bg}}"
                #significance = 5e4 * N_sg / math.sqrt(5e4*N_sg + N_bg)
                significance = N_sg / math.sqrt(N_sg + N_bg)
                signlabel = "significance = N_{sg} / #sqrt{N_{sg} + N_{bg}}"
                #significance = 1e3 * N_sg / math.sqrt( N_bg + (0.2*N_bg)**2 )
                #signlabel = "significance = 1e3 * N_{sg} / #sqrt{N_{bg} + (0.2*N_{bg})^{2}}"
            except:
                significance = 0

            #scalingfactor = 1.0/1000
            #scalingfactor = 1.0/100
            graphs_significance[label].SetPoint(
                graphs_significance[label].GetN(), score, significance)

    for category in ["short", "long"]:

        # plot significances:
        #####################

        canvas = shared_utils.mkcanvas()

        if category == "short":
            histo = TH2F("empty", "empty", 1, -1, 1, 1, 0, 1)
        else:
            histo = TH2F("empty", "empty", 1, -1, 1, 1, 0, 1)

        shared_utils.histoStyler(histo)
        histo.Draw()
        histo.SetTitle(";BDT response;efficiency, significance")
        legend = shared_utils.mklegend(x1=0.17, y1=0.2, x2=0.65, y2=0.45)

        first = True
        for label in graphs_significance:

            if category not in label: continue

            graphs_significance[label].Draw("same")
            graphStyler(graphs_significance[label])
            graphs_significance[label].SetLineColor(210)

            graphs_sgeff[label].Draw("same")
            graphStyler(graphs_sgeff[label])
            graphs_sgeff[label].SetLineColor(kBlue)
            graphs_sgeff[label].SetFillColor(0)

            graphs_bgeff[label].Draw("same")
            graphStyler(graphs_bgeff[label])
            graphs_bgeff[label].SetLineColor(kRed)
            graphs_bgeff[label].SetFillColor(0)

            legendlabel = label.replace("sg_", "").replace(
                "short_",
                "short tracks ").replace("long_", "long tracks ").replace(
                    "p0", " (phase 0)").replace("p1", " (phase 1)")

        legend.SetHeader("Phase %s, %s tracks" % (phase, category))
        legend.AddEntry(graphs_sgeff["sg_short_study"],
                        "signal efficiency #epsilon_{sg}")
        legend.AddEntry(graphs_bgeff["sg_short_study"],
                        "background efficiency #epsilon_{bg}")

        legend.AddEntry(graphs_significance["sg_short_study"], signlabel)

        legend.Draw()
        shared_utils.stamp()
        canvas.Print("plots/significance_%s_%s_phase%s_completestats.pdf" %
                     (batchname, category, phase))
        canvas.Print("plots/significance_%s_%s_phase%s_completestats.root" %
                     (batchname, category, phase))
Пример #10
0
def roc_and_efficiencies(sg_filelist,
                         bg_filelist,
                         phase,
                         batchname,
                         style="A"):

    mt2_short = " && ".join(cutflow.cuts["MT2_short"])
    mt2_long = " && ".join(cutflow.cuts["MT2_long"])
    exo_short = " && ".join(cutflow.cuts["EXO_short"])
    exo_long = " && ".join(cutflow.cuts["EXO_long"])
    exo_pt15_short = " && ".join(cutflow.cuts["EXO_pt15_short"])
    exo_pt15_long = " && ".join(cutflow.cuts["EXO_pt15_long"])
    exo_noeta_short = " && ".join(cutflow.cuts["EXO_noeta_short"])
    exo_noeta_long = " && ".join(cutflow.cuts["EXO_noeta_long"])
    exo_noetapt_short = " && ".join(cutflow.cuts["EXO_noetapt_short"])
    exo_noetapt_long = " && ".join(cutflow.cuts["EXO_noetapt_long"])
    bdt_short = " && ".join(cutflow.cuts["BDT_short"][:-2])
    bdt_long = " && ".join(cutflow.cuts["BDT_long"][:-2])
    bdt_nojets_short = " && ".join(cutflow.cuts["BDT_noJetVeto_short"][:-2])
    bdt_nojets_long = " && ".join(cutflow.cuts["BDT_noJetVeto_long"][:-2])

    #FIXME phase 1 dE/dx
    if phase == 1:
        bdt_short = bdt_short.replace("tracks_deDxHarmonic2pixel>2.0",
                                      "MHT>=0")
        bdt_long = bdt_long.replace("tracks_deDxHarmonic2pixel>2.0", "MHT>=0")
        bdt_nojets_short = bdt_nojets_short.replace(
            "tracks_deDxHarmonic2pixel>2.0", "MHT>=0")
        bdt_nojets_long = bdt_nojets_long.replace(
            "tracks_deDxHarmonic2pixel>2.0", "MHT>=0")
        lumi = 137000
    else:
        bdt_short = bdt_short.replace("tracks_deDxHarmonic2pixel>2.0",
                                      "MHT>=0")
        bdt_long = bdt_long.replace("tracks_deDxHarmonic2pixel>2.0", "MHT>=0")
        bdt_nojets_short = bdt_nojets_short.replace(
            "tracks_deDxHarmonic2pixel>2.0", "MHT>=0")
        bdt_nojets_long = bdt_nojets_long.replace(
            "tracks_deDxHarmonic2pixel>2.0", "MHT>=0")
        lumi = 35000

    histos = collections.OrderedDict()
    drawoptions = collections.OrderedDict()

    # get histograms:

    def fill_histos(label, variable, shortcuts, longcuts):
        histos["bg_short_%s" % label] = plotting.get_all_histos(
            bg_filelist,
            "Events",
            variable,
            "tracks_is_pixel_track==1 && " + shortcuts,
            nBinsX=200,
            xmin=-1,
            xmax=1)
        histos["bg_long_%s" % label] = plotting.get_all_histos(
            bg_filelist,
            "Events",
            variable,
            "tracks_is_pixel_track==0 && tracks_nMissingOuterHits>=2 && " +
            longcuts,
            nBinsX=200,
            xmin=-1,
            xmax=1)
        histos["sg_short_%s" % label] = plotting.get_all_histos(
            sg_filelist,
            "Events",
            variable,
            "tracks_is_pixel_track==1 && tracks_chiCandGenMatchingDR<0.01 && "
            + shortcuts,
            nBinsX=200,
            xmin=-1,
            xmax=1)
        histos["sg_long_%s" % label] = plotting.get_all_histos(
            sg_filelist,
            "Events",
            variable,
            "tracks_is_pixel_track==0 && tracks_nMissingOuterHits>=2 && tracks_chiCandGenMatchingDR<0.01 && "
            + longcuts,
            nBinsX=200,
            xmin=-1,
            xmax=1)

    # get common denominator:
    #fill_histos("denom", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15", "tracks_pt>40")
    fill_histos("denom", "tracks_mva_nov20_noEdep", "tracks_pt>15",
                "tracks_pt>40")

    #fill_histos("denom", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_short, "tracks_pt>30 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_long)

    # get numerator, ROC curve scans:
    #fill_histos("pt10", "tracks_mva_tight_may20_chi2_pt10", "tracks_pt>10 && " + bdt_short, "tracks_pt>30 && " + bdt_long)
    #drawoptions["pt10"] = ["BDT (track p_{T}>10 GeV)", "same", kRed, 1, True, False]

    #fill_histos("noBLpt15", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && " + bdt_short.replace("tracks_passleptonveto==1", "MHT>=0"), "tracks_pt>30 && " + bdt_long.replace("tracks_passleptonveto==1", "MHT>=0"))
    #drawoptions["noBLpt15"] = ["BDT (track p_{T}>15 GeV, no leptonveto)", "same", kYellow, 1, True, True]
    #
    #fill_histos("noBLpt15b", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && " + bdt_short.replace("tracks_passpionveto==1", "MHT>=0"), "tracks_pt>30 && " + bdt_long.replace("tracks_passpionveto==1", "MHT>=0"))
    #drawoptions["noBLpt15b"] = ["BDT (track p_{T}>15 GeV, no pionveto)", "same", kYellow+1, 1, True, True]
    #
    #fill_histos("noBLpt15c", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && " + bdt_short.replace("tracks_passjetveto==1", "MHT>=0"), "tracks_pt>30 && " + bdt_long.replace("tracks_passjetveto==1", "MHT>=0"))
    #drawoptions["noBLpt15c"] = ["BDT (track p_{T}>15 GeV, no jetveto)", "same", kYellow+2, 1, True, True]
    #
    #fill_histos("noBLpt15d", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && " + bdt_short.replace("tracks_deDxHarmonic2pixel>2.0", "MHT>=0"), "tracks_pt>30 && " + bdt_long.replace("tracks_deDxHarmonic2pixel>2.0", "MHT>=0"))
    #drawoptions["noBLpt15d"] = ["BDT (track p_{T}>15 GeV, no dedx)", "same", kYellow+3, 1, True, True]

    if style == "A":

        fill_histos("pt15noEdep", "tracks_mva_nov20_noEdep",
                    "tracks_pt>15 && " + bdt_short,
                    "tracks_pt>40 && " + bdt_long)
        drawoptions["pt15noEdep"] = [
            "BDT (track p_{T}>15 GeV)", "same", kOrange, 1, True, False
        ]

        fill_histos("pt30noEdep", "tracks_mva_nov20_noEdep",
                    "tracks_pt>40 && " + bdt_short,
                    "tracks_pt>40 && " + bdt_long)
        drawoptions["pt30noEdep"] = [
            "BDT (track p_{T}>40 GeV)", "same", kOrange, 1, False, True
        ]

        fill_histos(
            "pt30noEdep", "tracks_mva_nov20_noEdep", "tracks_pt>40 && " +
            bdt_short.replace("tracks_passjetveto==1 &&", " "),
            "tracks_pt>40 && " +
            bdt_long.replace("tracks_passjetveto==1 &&", " "))
        drawoptions["pt30noEdep"] = [
            "BDT (track p_{T}>40 GeV, no jet veto)", "same", kOrange, 2, False,
            True
        ]

        fill_histos(
            "pt15_ratio12", "tracks_mva_nov20_noEdep",
            "tracks_pt>15 && tracks_matchedCaloEnergy<15 && " + bdt_short,
            "tracks_pt>40 && tracks_matchedCaloEnergy/tracks_p<0.15 && " +
            bdt_long)
        drawoptions["pt15_ratio12"] = [
            "BDT (track p_{T}>15 GeV, with cut on E_{dep}", "same", kRed, 1,
            True, True
        ]

    if style == "B":

        #fill_histos("pt15", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && " + bdt_short, "tracks_pt>40 && " + bdt_long)
        #drawoptions["pt15"] = ["BDT (track p_{T}>15 GeV), trained with E_{dep}", "same", kOrange, 1, True, True]

        fill_histos("pt15noEdep", "tracks_mva_nov20_noEdep",
                    "tracks_pt>15 && " + bdt_short,
                    "tracks_pt>40 && " + bdt_long)
        drawoptions["pt15noEdep"] = [
            "BDT (track p_{T}>15 GeV)", "same", kOrange, 1, True, True
        ]

        fill_histos(
            "pt15_ratio12", "tracks_mva_nov20_noEdep",
            "tracks_pt>15 && tracks_matchedCaloEnergy<15 && " + bdt_short,
            "tracks_pt>40 && tracks_matchedCaloEnergy/tracks_p<0.15 && " +
            bdt_long)
        drawoptions["pt15_ratio12"] = [
            "BDT (track p_{T}>15 GeV, E_{Dep}<15 GeV (<0.15/p))", "same", kRed,
            1, True, True
        ]

    #fill_histos("noBLpt15c", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && " + bdt_short.replace("tracks_passjetveto==1", "MHT>=0"), "tracks_pt>30 && " + bdt_long.replace("tracks_passjetveto==1", "MHT>=0"))
    #drawoptions["noBLpt15c"] = ["BDT (track p_{T}>15 GeV, no jet veto)", "same", kOrange, 2, True, True]

    #fill_histos("pt30", "tracks_mva_tight_may20_chi2", "tracks_pt>30 && " + bdt_short, "tracks_pt>30 && " + bdt_long)
    #drawoptions["pt30"] = ["BDT (track p_{T}>30 GeV)", "same", kOrange, 2, True, False]

    #fill_histos("pt15_nojets", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && " + bdt_nojets_short, "tracks_pt>30 && " + bdt_nojets_long)
    #drawoptions["pt15_nojets"] = ["BDT (track p_{T}>15 GeV, no jet veto)", "same", kOrange, 2, True, True]

    #fill_histos("pt15_ratio12", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && tracks_matchedCaloEnergy/tracks_p<0.12 && " + bdt_short, "tracks_pt>30 && tracks_matchedCaloEnergy/tracks_p<0.12 && " + bdt_long)
    #drawoptions["pt15_ratio12"] = ["BDT (track p_{T}>15 GeV, EDep/p<0.12)", "same", kOrange, 3, True, True]

    #fill_histos("pt15_ratio20", "tracks_mva_nov20_noEdep", "tracks_pt>15 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_short, "tracks_pt>40 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_long)
    #drawoptions["pt15_ratio20"] = ["BDT (track p_{T}>15 GeV, not tr. on E_{dep}, E_{Dep}/p<20%)", "same", kRed, 1, True, True]

    #fill_histos("pt15_ratio30", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && tracks_matchedCaloEnergy/tracks_p<0.30 && " + bdt_short, "tracks_pt>30 && tracks_matchedCaloEnergy/tracks_p<0.30 && " + bdt_long)
    #drawoptions["pt15_ratio30"] = ["BDT (track p_{T}>15 GeV, EDep/p<0.30)", "same", kOrange, 5, True, True]

    #fill_histos("pt15_moreiso", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && tracks_trkRelIso<0.1 && " + bdt_short, "tracks_pt>30 && tracks_trkRelIso<0.1 && " + bdt_long)
    #drawoptions["pt15_moreiso"] = ["BDT (track p_{T}>15 GeV, relIso<0.1)", "same", kGreen-3, 1, True, True]

    #fill_histos("pt30", "tracks_mva_tight_may20_chi2", "tracks_pt>30 && " + bdt_short, "tracks_pt>30 && " + bdt_long)
    #drawoptions["pt30"] = ["BDT (track p_{T}>30 GeV)", "same", kPink+4, 1, True, False]

    # get numerator, working points:
    if phase == 0:
        #fill_histos("wpA", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>30 && tracks_mva_tight_may20_chi2>0.13 && tracks_matchedCaloEnergy/tracks_p<0.12 && " + bdt_short, "tracks_pt>30 && tracks_mva_tight_may20_chi2>0.13 && tracks_matchedCaloEnergy/tracks_p<0.12 && " + bdt_long)
        fill_histos(
            "wpA", "tracks_mva_nov20_noEdep",
            "tracks_pt>15 && tracks_mva_nov20_noEdep>0.1 && tracks_matchedCaloEnergy<15 && "
            + bdt_short,
            "tracks_pt>40 && tracks_mva_nov20_noEdep>0.1 && tracks_matchedCaloEnergy/tracks_p<0.15 && "
            + bdt_long)
        #fill_histos("wpB", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && tracks_mva_tight_may20_chi2_pt15>-0.02 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_short, "tracks_pt>30 && tracks_mva_tight_may20_chi2_pt15>0 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_long)
        #fill_histos("wpC", "tracks_mva_nov20_noEdep", "tracks_pt>15 && tracks_mva_nov20_noEdep>0 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_short, "tracks_pt>40 && tracks_mva_nov20_noEdep>0.05 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_long)
        #fill_histos("wpB_pt150", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && tracks_mva_tight_may20_chi2_pt15>-0.02 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_short, "tracks_pt>150 && tracks_mva_tight_may20_chi2_pt15>0 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_long)
        #fill_histos("wpB", "tracks_mva_tight_may20_chi2", "tracks_pt>15 && tracks_mva_tight_may20_chi2_pt15>0.05 && tracks_matchedCaloEnergy/tracks_p<0.30 && " + bdt_short, "tracks_pt>30 && tracks_mva_tight_may20_chi2_pt15>0.1 && tracks_matchedCaloEnergy/tracks_p<0.30 && " + bdt_long)
    elif phase == 1:
        #fill_histos("wpA", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>30 && tracks_mva_tight_may20_chi2>0 && tracks_matchedCaloEnergy/tracks_p<0.12 && " + bdt_short, "tracks_pt>30 && tracks_mva_tight_may20_chi2>0 && tracks_matchedCaloEnergy/tracks_p<0.12 && " + bdt_long)
        fill_histos(
            "wpA", "tracks_mva_nov20_noEdep",
            "tracks_pt>15 && tracks_mva_nov20_noEdep>0.12 && tracks_matchedCaloEnergy<15 && "
            + bdt_short,
            "tracks_pt>40 && tracks_mva_nov20_noEdep>0.15 && tracks_matchedCaloEnergy/tracks_p<0.15 && "
            + bdt_long)
        #fill_histos("wpB", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && tracks_mva_tight_may20_chi2_pt15>-0.1 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_short, "tracks_pt>30 && tracks_mva_tight_may20_chi2_pt15>-0.14 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_long)
        #fill_histos("wpC", "tracks_mva_nov20_noEdep", "tracks_pt>15 && tracks_mva_nov20_noEdep>-0.1 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_short, "tracks_pt>40 && tracks_mva_nov20_noEdep>0 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_long)
        #fill_histos("wpB_pt150", "tracks_mva_tight_may20_chi2_pt15", "tracks_pt>15 && tracks_mva_tight_may20_chi2_pt15>-0.1 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_short, "tracks_pt>150 && tracks_mva_tight_may20_chi2_pt15>-0.14 && tracks_matchedCaloEnergy/tracks_p<0.20 && " + bdt_long)
        #fill_histos("wpB", "tracks_mva_tight_may20_chi2", "tracks_pt>15 && tracks_mva_tight_may20_chi2_pt15>0.1 && tracks_matchedCaloEnergy/tracks_p<0.30 && " + bdt_short, "tracks_pt>30 && tracks_mva_tight_may20_chi2_pt15>0.1 && tracks_matchedCaloEnergy/tracks_p<0.30 && " + bdt_long)
    drawoptions["wpA"] = ["working point", "same p", kRed, 21, True, True]
    #drawoptions["wpA2"] = ["working point A, pT>15", "same p", kRed, 22, True, False]
    #drawoptions["wpB"] = ["working point B", "same p", kMagenta, 20, True, True]
    #drawoptions["wpC"] = ["working point (loose)", "same p", kRed, 20, True, True]

    if style == "A":
        fill_histos("mt2", "tracks_mva_tight_may20_chi2_pt15", mt2_short,
                    mt2_long)
        drawoptions["mt2"] = [
            "SUS-19-005 tag", "same p", kTeal, 20, True, True
        ]

        #fill_histos("mt2_pt150", "tracks_mva_tight_may20_chi2_pt15", mt2_short, mt2_long + " && tracks_pt>150")
        #drawoptions["mt2_pt150"] = ["SUS-19-005 tag (track p_{T}>150 GeV)", "same p", kTeal, 22, False, True]

        fill_histos("exo", "tracks_mva_tight_may20_chi2_pt15", exo_short,
                    exo_long)
        drawoptions["exo"] = [
            "EXO-19-010 tag", "same p", kAzure, 20, True, True
        ]

        #fill_histos("exo_pt15", "tracks_mva_tight_may20_chi2", exo_pt15_short, exo_pt15_long)
        #drawoptions["exo_pt15"] = ["EXO-19-010 tag (p_{T}>15 GeV)", "same p", kAzure+7, 20, True, True]

        fill_histos("exo_noeta", "tracks_mva_tight_may20_chi2_pt15",
                    exo_noeta_short, exo_noeta_long)
        drawoptions["exo_noeta"] = [
            "EXO-19-010 tag (no #eta cuts)", "same p", kAzure + 5, 20, True,
            True
        ]

        fill_histos("exo_noetapt", "tracks_mva_tight_may20_chi2_pt15",
                    exo_noetapt_short, exo_noetapt_long)
        drawoptions["exo_noetapt"] = [
            "EXO-19-010 tag (no p_{T} and #eta cuts)", "same p", kAzure + 5,
            21, True, True
        ]

    # scale with lumi
    for label in histos:
        shared_utils.histoStyler(histos[label])
        histos[label].Scale(lumi)

    # get efficiencies:
    efficiencies = {}
    for label in histos:

        if "denom" in label: continue

        efficiencies[label] = []

        denom_label = label.split("_")[0] + "_" + label.split(
            "_")[1] + "_denom"
        denominator = histos[denom_label].Integral()

        if "mt2" in label or "exo" in label or "wp" in label:
            numerator = histos[label].Integral()
            if denominator > 0:
                efficiencies[label].append(
                    [0, numerator / denominator, numerator])
            else:
                efficiencies[label].append([0, 0, numerator])
        else:
            for i_score in numpy.arange(-1.0, 1.0, 0.005):
                numerator = histos[label].Integral(
                    histos[label].GetXaxis().FindBin(i_score),
                    histos[label].GetXaxis().FindBin(1))
                if denominator > 0:
                    efficiencies[label].append(
                        [i_score, numerator / denominator, numerator])
                else:
                    efficiencies[label].append([i_score, 0, numerator])

    #efffile = "plots/roc_%s_phase%s.dat" % (batchname, phase)
    #with open(efffile, "w+") as fout:
    #    for label in efficiencies:
    #        fout.write("Label: " + label + "\n**************\n")
    #        for item in efficiencies[label]:
    #            fout.write("%s, %s, %s\n" % (item[0], item[1], item[2]))
    #        fout.write("\n")

    # build TGraphs
    graphs_roc = {}
    graphs_sgeff = {}
    graphs_bgeff = {}
    graphs_significance = {}
    for label in efficiencies:

        if "bg" in label: continue
        graphs_roc[label] = TGraph()
        graphs_sgeff[label] = TGraph()
        graphs_bgeff[label] = TGraph()
        graphs_significance[label] = TGraph()

        for i in range(len(efficiencies[label])):
            score = efficiencies[label][i][0]
            eff_sg = efficiencies[label][i][1]
            eff_bg = efficiencies[label.replace("sg", "bg")][i][1]
            N_sg = efficiencies[label][i][2]
            N_bg = efficiencies[label.replace("sg", "bg")][i][2]
            graphs_roc[label].SetPoint(graphs_roc[label].GetN(), eff_sg,
                                       1 - eff_bg)
            graphs_sgeff[label].SetPoint(graphs_sgeff[label].GetN(), score,
                                         eff_sg)
            graphs_bgeff[label].SetPoint(graphs_bgeff[label].GetN(), score,
                                         eff_bg)

            try:
                significance = N_sg / math.sqrt(N_sg + N_bg)
                #significance = N_sg / math.sqrt( N_bg + (0.1*N_bg)**2 )
            except:
                significance = 0

            graphs_significance[label].SetPoint(
                graphs_significance[label].GetN(), score, significance)

    for category in ["short", "long"]:

        # plot ROC curves:
        ##################

        canvas = shared_utils.mkcanvas()

        if category == "short":
            if phase == 0:
                histo = TH2F("empty", "empty", 1, 0, 1, 1, 0.9, 1)
            else:
                histo = TH2F("empty", "empty", 1, 0, 1, 1, 0.98, 1)
        else:
            if phase == 0:
                histo = TH2F("empty", "empty", 1, 0, 1, 1, 0.97, 1)
            else:
                histo = TH2F("empty", "empty", 1, 0, 1, 1, 0.97, 1)

        shared_utils.histoStyler(histo)
        histo.Draw()
        histo.SetTitle(";#epsilon_{  sg};1 - #epsilon_{  bg}")

        if category == "short":
            legend = shared_utils.mklegend(x1=0.17, y1=0.2, x2=0.65, y2=0.6)
        else:
            legend = shared_utils.mklegend(x1=0.17, y1=0.2, x2=0.65, y2=0.6)

        for label in sorted(graphs_roc):

            if category not in label:
                continue

            graphStyler(graphs_roc[label])

            # e.g. drawoptions["pt30"] = ["BDT (track p_{T}>30 GeV)", "same", kPink+4, 1, True, True]

            optionslabel = "_".join(label.split("_")[2:])

            if category == "short" and not drawoptions[optionslabel][4]:
                continue
            if category == "long" and not drawoptions[optionslabel][5]:
                continue

            legendlabel = drawoptions[optionslabel][0]
            if drawoptions[optionslabel][1] == "same p":
                graphs_roc[label].SetLineColor(kWhite)
                graphs_roc[label].SetMarkerStyle(drawoptions[optionslabel][3])
                graphs_roc[label].SetMarkerColor(drawoptions[optionslabel][2])
            else:
                graphs_roc[label].SetLineColor(drawoptions[optionslabel][2])
                graphs_roc[label].SetLineStyle(drawoptions[optionslabel][3])
                print label, optionslabel, drawoptions[optionslabel][3]
            graphs_roc[label].Draw(drawoptions[optionslabel][1])

            if category == "long":
                legendlabel = legendlabel.replace("p_{T}>15 GeV",
                                                  "p_{T}>40 GeV")

            legend.AddEntry(graphs_roc[label], legendlabel)

        #legend.SetTextSize(0.045)
        legend.SetTextSize(0.04)
        legend.SetHeader("Phase %s, %s tracks" % (phase, category))
        legend.Draw()
        #shared_utils.stamp()

        pdfname = "plots/roc_%s_%s_phase%s_noedep.pdf" % (batchname, category,
                                                          phase)

        if style == "A":
            pdfname = pdfname.replace(".pdf", "_styleA.pdf")

        canvas.Print(pdfname)
Пример #11
0
                            "n_allvertices", "n_{vertices}")

                        size = 0.059
                        font = 132
                        histos[label].GetZaxis().SetLabelFont(font)
                        histos[label].GetZaxis().SetTitleFont(font)
                        histos[label].GetZaxis().SetTitleSize(size)
                        histos[label].GetZaxis().SetLabelSize(size)
                        histos[label].GetZaxis().SetLabelSizeser(3e-3, 2e-1)
                        histos[label].GetZaxis().SetTitleOffset(1.2)
                        histos[label].GetXaxis().SetNdivisions(5)
                        histos[label].SetTitle(";%s;%s;fake rate" %
                                               (myvariable.split(":")[0],
                                                myvariable.split(":")[1]))

                        canvas = shared_utils.mkcanvas()
                        canvas.SetRightMargin(0.2)
                        histos[label].Draw("colz")
                        canvas.SetLogz(True)
                        shared_utils.stamp()

                        text = TLatex()
                        text.SetTextFont(132)
                        text.SetTextSize(0.05)
                        text.SetNDC()
                        text.DrawLatex(
                            0.175, 0.825, "%s, %s tracks" %
                            (dataset, category.replace("_", "")))

                        canvas.SaveAs(folder + "_plots/" + label + ".pdf")
Пример #12
0
def stack_histograms(histos, samples, variable, xlabel, ylabel, folder, normalize = False, ratioplot = False, signal_scaling_factor=1.0, suffix="", logx=False, logy=True, miniylabel="Data/MC", lumi=False, ymin=False, ymax=False, xmin=False, xmax=False, yaxis_label="Events", output_folder = ".", include_data = True, width=900):
 
    if ratioplot:
        canvas = TCanvas("canvas", "canvas", width, 800)
    else:
        canvas = shared_utils.mkcanvas()

    if ratioplot:
        pad1 = TPad("pad1", "pad1", 0, 0.16, 1, 1.0)
        pad1.SetRightMargin(0.05)
        pad1.SetLogy(logy)
        pad1.SetTopMargin(0.07)
        pad1.Draw()
        pad2 = TPad("pad2", "pad2", 0.0, 0.025, 1.0, 0.235)
        pad2.SetBottomMargin(0.25)
        pad2.SetRightMargin(0.05)
        pad2.Draw()
        pad1.cd()

    l = canvas.GetLeftMargin()
    t = canvas.GetTopMargin()
    r = canvas.GetRightMargin()
    b = canvas.GetBottomMargin()

    #canvas.SetTopMargin(0.05)
    #canvas.SetBottomMargin(1.2*b)
    #canvas.SetLeftMargin(1.2*l)
    #canvas.SetRightMargin(0.7*r)
    
    canvas.SetLogx(logx)
    canvas.SetLogy(logy)
   
    legend = TLegend(0.6, 0.65, 0.9, 0.9)
    legend.SetTextSize(0.03)
    minimum_y_value = 1e6

    global_minimum = 1e10
    global_maximum = 1e-10
   
    mcstack = THStack("mcstack-%s" % str(uuid.uuid1()), "")

    samples_for_sorting = []

    # get total lumi value:
    plot_has_data = False
    total_lumi = 0
    for label in sorted(histos):
        if samples[label]["type"] == "data" and include_data:
            plot_has_data = True
            total_lumi += samples[label]["lumi"]
    
    if not lumi and plot_has_data:
        lumi = total_lumi
    elif lumi:
        pass
    else:
        lumi = 1.0

    totals_background = 0
    totals_signal = 0

    # plot backgrounds:
    for label in sorted(histos):

        if samples[label]["type"] == "bg" or samples[label]["type"] == "sg":
            histos[label].Scale(lumi * 1000.0)
            
            totals_background += histos[label].Integral()

        if histos[label].GetMinimum(0) < global_minimum:
            global_minimum = histos[label].GetMinimum(0) 
        if histos[label].GetMaximum() > global_maximum:
            global_maximum = histos[label].GetMaximum()

        if samples[label]["type"] == "bg":
            histos[label].SetFillColor(samples[label]["color"])
            histos[label].SetLineColor(samples[label]["color"])
            histos[label].SetMarkerColor(samples[label]["color"])
            histos[label].SetLineWidth(0)
            samples_for_sorting.append([label, histos[label].Integral()])
        
        if samples[label]["type"] == "sg":
            totals_signal += histos[label].Integral()
            
        if normalize:
            histos[label].Scale(1.0/histos[label].Integral())
            
        #if not ratioplot:
        #    shared_utils.histoStyler(histos[label])
        
                
    # stack histograms with the largest integral to appear on the top of the stack:
    def Sort(sub_li, i_index): 
        return(sorted(sub_li, key = lambda x: x[i_index]))     
        
    print "Stacking"   
    for label in Sort(samples_for_sorting, 1):
        mcstack.Add(histos[label[0]])
        legend.AddEntry(histos[label[0]], label[0])
                                
    mcstack.Draw("hist")
    if ratioplot:
        mcstack.GetXaxis().SetLabelSize(0)   
        mcstack.SetTitle(";;%s" % yaxis_label)
        mcstack.GetYaxis().SetTitleOffset(1.3)
        mcstack.GetXaxis().SetTitleOffset(1.3)
    else:
        mcstack.SetTitle(";%s;%s" % (xlabel, yaxis_label))
        #canvas.SetGridx(True)
        #canvas.SetGridy(True)
        #mcstack.GetXaxis().SetTitleSize(0.13)
        #mcstack.GetYaxis().SetTitleSize(0.13)
        mcstack.GetYaxis().SetTitleOffset(0.38)
        #mcstack.GetYaxis().SetRangeUser(0,2)
        #mcstack.GetYaxis().SetNdivisions(4)
        #mcstack.GetXaxis().SetLabelSize(0.15)
        #mcstack.GetYaxis().SetLabelSize(0.15)
        
        shared_utils.histoStyler(mcstack)


    # plot signal:
    for label in sorted(histos):
        if samples[label]["type"] == "sg":
            histos[label].SetLineColor(samples[label]["color"])
            histos[label].SetMarkerColor(samples[label]["color"])
            histos[label].SetLineWidth(3)
            histos[label].Scale(signal_scaling_factor)
            histos[label].Draw("same hist")
            legend.AddEntry(histos[label], label)

    # plot data:
    if plot_has_data:

        combined_data = 0
        for label in sorted(histos):
            if samples[label]["type"] == "data":
                if combined_data == 0:
                    combined_data = histos[label].Clone()
                else:
                     combined_data.Add(histos[label])        

        combined_data.SetLineColor(kBlack)
        combined_data.SetMarkerColor(kBlack)
        combined_data.SetMarkerColor(kBlack)
        combined_data.SetMarkerStyle(20)
        combined_data.SetMarkerSize(1)
        combined_data.SetLineWidth(3)
        combined_data.Draw("same E & X0")
        legend.AddEntry(combined_data, "Data")

    # set minimum/maximum ranges   
    if global_minimum != 0:
        mcstack.SetMinimum(1e-2 * global_minimum)
    else:
        mcstack.SetMinimum(1e-2)

    if ymin:
        mcstack.SetMinimum(ymin)
   
    if logy:
        global_maximum_scale = 100
    else:
        global_maximum_scale = 1
   
    if not ymax:
        mcstack.SetMaximum(global_maximum_scale * global_maximum)
    else:
        mcstack.SetMaximum(ymax)

    legend.SetBorderSize(0)
    legend.SetFillStyle(0)
    legend.Draw()

    latex=TLatex()
    latex.SetNDC()
    latex.SetTextAngle(0)
    latex.SetTextColor(kBlack)
    
    latex.SetTextFont(62)
    latex.SetTextAlign(31)
    latex.SetTextSize(0.35 * t)

    latex.DrawLatex(0.915, 0.915, "%.1f fb^{-1} (13 TeV)" % lumi)
    
    #latex.SetTextSize(0.35*t)
    #latex.SetTextFont(52)
    #latex.DrawLatex(0.4, 1-0.5*t+0.15*0.5*t, "CMS Work in Progress")
    
    if ratioplot:
        # plot ratio
        pad2.cd()
        
        combined_mc_background = 0
        for label in sorted(histos):
            if samples[label]["type"] == "bg":
                if combined_mc_background == 0:
                    combined_mc_background = histos[label].Clone()
                else:
                     combined_mc_background.Add(histos[label])        
        
        if plot_has_data:
            ratio = combined_data.Clone()
        else:
            ratio = combined_mc_background.Clone()
            
        ratio.Divide(combined_mc_background)
        if xmax:
            ratio.GetXaxis().SetRangeUser(xmin, xmax)
        ratio.Draw("same e0")
        ratio.SetTitle(";%s;%s" % (xlabel, miniylabel))
        pad2.SetGridx(True)
        pad2.SetGridy(True)
        ratio.GetXaxis().SetTitleSize(0.13)
        ratio.GetYaxis().SetTitleSize(0.13)
        ratio.GetYaxis().SetTitleOffset(0.38)
        ratio.GetYaxis().SetRangeUser(0,2)
        ratio.GetYaxis().SetNdivisions(4)
        ratio.GetXaxis().SetLabelSize(0.15)
        ratio.GetYaxis().SetLabelSize(0.15)
    
    shared_utils.stamp()
    
    if len(output_folder)>0:
        os.system("mkdir -p %s" % output_folder)
    canvas.SaveAs("%s/%s%s.pdf" % (output_folder, variable, suffix))
    
    #with open("%s/%s%s.txt" % (output_folder, variable, suffix), "w+") as fo:
    #    fo.write("bg = %s\n" % totals_background)
    #    fo.write("sg = %s\n" % totals_signal)
    
    #canvas.SaveAs("%s/%s%s.root" % (output_folder, variable, suffix))
    print "\n****************\n"
def main(options):

    histofolder = options.histofolder
    suffix = options.suffix
    plotfolder = "plots_%s" % options.suffix

    os.system("mkdir -p %s" % plotfolder)

    if options.mc_reweighted:
        periods = [
            "Run2016B",
            "Run2016C",
            "Run2016D",
            "Run2016E",
            "Run2016F",
            "Run2016G",
            "Run2016H",
            "Run2017B",
            "Run2017C",
            "Run2017D",
            "Run2017E",
            "Run2017F",
            "Run2018A",
            "Run2018B",
            "Run2018C",
            "Run2018D",
            "Summer16rwRun2016B",
            "Summer16rwRun2016C",
            "Summer16rwRun2016D",
            "Summer16rwRun2016E",
            "Summer16rwRun2016F",
            "Summer16rwRun2016G",
            "Summer16rwRun2016H",
            "Fall17rwRun2017B",
            "Fall17rwRun2017C",
            "Fall17rwRun2017D",
            "Fall17rwRun2017E",
            "Fall17rwRun2017F",
            "Autumn18rwRun2018A",
            "Autumn18rwRun2018B",
            "Autumn18rwRun2018C",
            "Autumn18rwRun2018D",
        ]
    else:
        periods = [
            "Run2016B",
            "Run2016C",
            "Run2016D",
            "Run2016E",
            "Run2016F",
            "Run2016G",
            "Run2016H",
            "Run2017B",
            "Run2017C",
            "Run2017D",
            "Run2017E",
            "Run2017F",
            "Run2018A",
            "Run2018B",
            "Run2018C",
            "Run2018D",
            "Summer16",
            "Fall17",
            "Autumn18",
        ]

    exact = "layers_remaining==track_trackerLayersWithMeasurement && "

    cuts = {
        "baseline": {
            "base_cuts": "layers_remaining>=3 && ",
            "taggedextra": "",
            "legendheader": "baseline",
        },
        #"lowdxydz": {
        #              "base_cuts":    "layers_remaining>=3 && ",
        #              "taggedextra":  "track_dxyVtx<0.01 && track_dzVtx<0.01 && ",
        #              "legendheader": "lowdxydz",
        #            },
    }

    if not options.get_from_tree:
        cuts = {
            "baseline": {
                "base_cuts": "",
                "taggedextra": "",
                "legendheader": "baseline",
            },
        }

    for cut_label in cuts:

        if options.weightkinematic:
            cuts[cut_label]["base_cuts"] += " weight_kinematicMLP2>0 && "
        elif options.weighttrackprop:
            cuts[cut_label]["base_cuts"] += " weight_trackpropMLP2>0 && "

        histolabels = {
            "h_tracks_reco": [
                "track_reco", cuts[cut_label]["base_cuts"] + "track_reco==1",
                1, 1, 2
            ],
            "h_tracks_rereco_short": [
                "track_reco",
                cuts[cut_label]["base_cuts"] + cuts[cut_label]["taggedextra"] +
                exact + " track_rereco==1 && track_is_pixel_track==1", 1, 1, 2
            ],
            "h_tracks_rereco_long": [
                "track_reco",
                cuts[cut_label]["base_cuts"] + cuts[cut_label]["taggedextra"] +
                exact + " track_rereco==1 && track_is_pixel_track==0", 1, 1, 2
            ],
            "h_tracks_tagged_short": [
                "track_reco", cuts[cut_label]["base_cuts"] +
                cuts[cut_label]["taggedextra"] + exact +
                " track_preselected==1 && track_mva>0.1 && track_pt>25 && track_is_pixel_track==1 && (track_matchedCaloEnergy<15 || track_matchedCaloEnergy/track_p<0.15)",
                1, 1, 2
            ],
            "h_tracks_tagged_long": [
                "track_reco", cuts[cut_label]["base_cuts"] +
                cuts[cut_label]["taggedextra"] + exact +
                " track_preselected==1 && track_mva>0.1 && track_pt>40 && track_is_pixel_track==0 && (track_matchedCaloEnergy<15 || track_matchedCaloEnergy/track_p<0.15)",
                1, 1, 2
            ],
            #"h_tracks_tagged_short":    ["track_reco", cuts[cut_label]["base_cuts"] + cuts[cut_label]["taggedextra"] + exact + " track_preselected==1 && track_tagged==1 && track_is_pixel_track==1", 1, 1, 2],
            #"h_tracks_tagged_long":     ["track_reco", cuts[cut_label]["base_cuts"] + cuts[cut_label]["taggedextra"] + exact + " track_preselected==1 && track_tagged==1 && track_is_pixel_track==0", 1, 1, 2],
            #h_tracks_tagged_short":    ["track_reco", base_cutstagged + "track_tagged==1 && track_is_pixel_track==1", 1, 1, 2],
            #"h_tracks_tagged_short":   ["track_reco", base_cutstagged + "track_is_pixel_track==1", 1, 1, 2],
            #"h_tracks_tagged_short":   ["track_reco", base_cuts + exact + " abs(track_dxyVtx)<0.005 && abs(track_dzVtx)<0.005 && track_tagged==1 && track_pt>25 && track_is_pixel_track==1", 1, 1, 2],
            #"h_tracks_tagged_short":   ["track_reco", base_cuts + exact + base_cutstagged + " track_is_pixel_track==1", 1, 1, 2],
            #"h_tracks_tagged_short":   ["track_reco", exo + " && track_is_pixel_track==1", 1, 1, 2],
            #"h_tracks_tagged_short":   ["track_reco", base_cuts + "track_pt>25 && track_dxyVtx<0.001 && track_dzVtx<0.001 && track_tagged==1 && track_is_pixel_track==1", 1, 1, 2],
            #"h_tracks_tagged_short":   ["track_reco", "track_rereco==1 && track_is_pixel_track==1 && " + exo, 1, 1, 2],
        }

        categories = ["_short", "_long"]

        # get all histos:
        hists = {}
        for period in periods:

            if "Run201" in period and "rw" not in period:
                is_data = False
            else:
                is_data = True

            hists[period] = {}
            for label in histolabels:
                print period, label

                filename = "%s/histograms%s_%s.root" % (histofolder, suffix,
                                                        period)

                if options.get_from_tree:
                    tree = TChain("Events")
                    tree.Add(filename)

                    # apply weights before getting the histograms from the tree:
                    treecuts = "(%s)" % histolabels[label][1]
                    if not is_data and options.weightkinematic:
                        treecuts += "*weight_kinematicMLP2/(1.0-weight_kinematicMLP2)"
                    if not is_data and options.weighttrackprop:
                        treecuts += "*weight_trackpropMLP2/(1.0-weight_trackpropMLP2)"
                    if options.mc_reweighted:
                        treecuts += "*weight_ptreweighting"

                    hists[period][label] = plotting.get_histogram_from_tree(
                        tree,
                        histolabels[label][0],
                        cutstring=treecuts,
                        nBinsX=histolabels[label][2],
                        xmin=histolabels[label][3],
                        xmax=histolabels[label][4])
                    hists[period][label].SetDirectory(0)
                    hists[period][label].SetLineWidth(2)
                else:
                    fin = TFile(filename, "open")
                    print "%s/histograms%s_%s.root" % (histofolder, suffix,
                                                       period)
                    hists[period][label] = fin.Get("Histograms/" + label)
                    hists[period][label].SetDirectory(0)
                    hists[period][label].SetLineWidth(2)
                    fin.Close()

                shared_utils.histoStyler(hists[period][label])

        # denom. fix:
        for period in periods:
            hists[period]["h_tracks_reco_short"] = hists[period][
                "h_tracks_reco"].Clone()
            hists[period]["h_tracks_reco_long"] = hists[period][
                "h_tracks_reco"].Clone()

        # global SF:
        fitresults = {}
        fitresults["fit_sf"] = {}
        fitresults["fit_uncert"] = {}
        fitresults["fit_sfreco"] = {}
        fitresults["fit_uncertreco"] = {}
        fitresults["fit_sftag"] = {}
        fitresults["fit_uncerttag"] = {}

        # calculated histograms:
        finaleff_global = {}
        finaleff_reco = {}
        finaleff_tag = {}

        finaleff_global_num = {}
        finaleff_reco_num = {}
        finaleff_tag_num = {}
        finaleff_global_denom = {}
        finaleff_reco_denom = {}
        finaleff_tag_denom = {}

        h_sf_global = {}
        h_sf_reco = {}
        h_sf_tag = {}
        g1_global = {}
        g1_reco = {}
        g1_tag = {}

        for category in categories:

            # first, get efficiencies:

            finaleff_global[category] = {}
            finaleff_reco[category] = {}
            finaleff_tag[category] = {}

            finaleff_global_num[category] = {}
            finaleff_reco_num[category] = {}
            finaleff_tag_num[category] = {}

            finaleff_global_denom[category] = {}
            finaleff_reco_denom[category] = {}
            finaleff_tag_denom[category] = {}

            for period in periods:

                finaleff_global_num[category][period] = hists[period][
                    "h_tracks_tagged" + category].Clone()
                finaleff_global_denom[category][period] = hists[period][
                    "h_tracks_reco" + category].Clone()
                finaleff_global[category][period] = finaleff_global_num[
                    category][period].Clone()
                finaleff_global[category][period].Divide(
                    finaleff_global_denom[category][period])

                finaleff_reco_num[category][period] = hists[period][
                    "h_tracks_rereco" + category].Clone()
                finaleff_reco_denom[category][period] = hists[period][
                    "h_tracks_reco" + category].Clone()
                finaleff_reco[category][period] = finaleff_reco_num[category][
                    period].Clone()
                finaleff_reco[category][period].Divide(
                    finaleff_reco_denom[category][period])

                finaleff_tag_num[category][period] = hists[period][
                    "h_tracks_tagged" + category].Clone()
                finaleff_tag_denom[category][period] = hists[period][
                    "h_tracks_rereco" + category].Clone()
                finaleff_tag[category][period] = finaleff_tag_num[category][
                    period].Clone()
                finaleff_tag[category][period].Divide(
                    finaleff_tag_denom[category][period])

            # get SF:

            h_sf_global[category] = {}
            h_sf_reco[category] = {}
            h_sf_tag[category] = {}
            g1_global[category] = {}
            g1_reco[category] = {}
            g1_tag[category] = {}

            for period in periods:

                print category, period
                if "rw" in period: continue
                if "Run201" not in period: continue

                if not options.mc_reweighted:
                    if "Run2016" in period:
                        mcperiod = "Summer16"
                    elif "Run2017" in period:
                        mcperiod = "Fall17"
                    elif "Run2018" in period:
                        mcperiod = "Autumn18"
                else:
                    if "Run2016" in period:
                        mcperiod = "Summer16rw" + period
                    elif "Run2017" in period:
                        mcperiod = "Fall17rw" + period
                    elif "Run2018" in period:
                        mcperiod = "Autumn18rw" + period

                if not options.get_from_tree:

                    print "fitting global SF..."
                    g1_global[category][period] = TF1('g1_global', '[0]', 3,
                                                      20)
                    h_sf_global[category][period] = finaleff_global[category][
                        period].Clone()
                    h_sf_global[category][period].Divide(
                        finaleff_global[category][mcperiod])
                    fit = h_sf_global[category][period].Fit(
                        g1_global[category][period], "", "same", 3, 20)
                    fitresults["fit_sf"][
                        period +
                        category] = g1_global[category][period].GetParameter(0)
                    if "short" in category:
                        fitresults["fit_uncert"][
                            period + category] = h_sf_global[category][
                                period].GetBinError(4)
                    else:
                        fitresults["fit_uncert"][
                            period + category] = h_sf_global[category][
                                period].GetBinError(6)

                    print "fitting reco SF..."
                    g1_reco[category][period] = TF1('g1_reco', '[0]', 3, 20)
                    h_sf_reco[category][period] = finaleff_reco[category][
                        period].Clone()
                    h_sf_reco[category][period].Divide(
                        finaleff_reco[category][mcperiod])
                    fit = h_sf_reco[category][period].Fit(
                        g1_reco[category][period], "", "same", 3, 20)
                    fitresults["fit_sfreco"][
                        period +
                        category] = g1_reco[category][period].GetParameter(0)
                    if "short" in category:
                        fitresults["fit_uncertreco"][
                            period + category] = h_sf_reco[category][
                                period].GetBinError(4)
                    else:
                        fitresults["fit_uncertreco"][
                            period + category] = h_sf_reco[category][
                                period].GetBinError(6)

                    print "fitting tagging SF..."
                    g1_tag[category][period] = TF1('g1_tag', '[0]', 3, 20)
                    h_sf_tag[category][period] = finaleff_tag[category][
                        period].Clone()
                    h_sf_tag[category][period].Divide(
                        finaleff_tag[category][mcperiod])
                    fit = h_sf_tag[category][period].Fit(
                        g1_tag[category][period], "", "same", 3, 20)
                    fitresults["fit_sftag"][
                        period +
                        category] = g1_tag[category][period].GetParameter(0)
                    if "short" in category:
                        fitresults["fit_uncerttag"][
                            period +
                            category] = h_sf_tag[category][period].GetBinError(
                                4)
                    else:
                        fitresults["fit_uncerttag"][
                            period +
                            category] = h_sf_tag[category][period].GetBinError(
                                6)

                else:

                    # using the tree:
                    h_sf_global[category][period] = finaleff_global[category][
                        period].Clone()
                    h_sf_global[category][period].Divide(
                        finaleff_global[category][mcperiod])
                    fitresults["fit_sf"][period + category] = h_sf_global[
                        category][period].GetBinContent(1)
                    fitresults["fit_uncert"][period + category] = h_sf_global[
                        category][period].GetBinError(1)

                    h_sf_reco[category][period] = finaleff_reco[category][
                        period].Clone()
                    h_sf_reco[category][period].Divide(
                        finaleff_reco[category][mcperiod])
                    fitresults["fit_sfreco"][period + category] = h_sf_reco[
                        category][period].GetBinContent(1)
                    fitresults["fit_uncertreco"][
                        period +
                        category] = h_sf_reco[category][period].GetBinError(1)

                    h_sf_tag[category][period] = finaleff_tag[category][
                        period].Clone()
                    h_sf_tag[category][period].Divide(
                        finaleff_tag[category][mcperiod])
                    fitresults["fit_sftag"][
                        period +
                        category] = h_sf_tag[category][period].GetBinContent(1)
                    fitresults["fit_uncerttag"][
                        period +
                        category] = h_sf_tag[category][period].GetBinError(1)

            # Lumi-weighting:
            if options.lumiweighted:

                official_lumis = {
                    "Run2016B": 5.8,
                    "Run2016C": 2.6,
                    "Run2016D": 4.2,
                    "Run2016E": 4.0,
                    "Run2016F": 3.1,
                    "Run2016G": 7.5,
                    "Run2016H": 8.6,
                    "Run2017B": 4.8,
                    "Run2017C": 9.7,
                    "Run2017D": 4.3,
                    "Run2017E": 9.3,
                    "Run2017F": 13.5,
                    "Run2018A": 14,
                    "Run2018B": 7.1,
                    "Run2018C": 6.94,
                    "Run2018D": 31.93,
                }

                for runyear in ["Run2016", "Run2017", "Run2018"]:

                    print "runyear", runyear

                    fitresults["fit_sf"][runyear + category] = 0
                    fitresults["fit_uncert"][runyear + category] = 0
                    fitresults["fit_sfreco"][runyear + category] = 0
                    fitresults["fit_uncertreco"][runyear + category] = 0
                    fitresults["fit_sftag"][runyear + category] = 0
                    fitresults["fit_uncerttag"][runyear + category] = 0

                    sum_lumi = 0.0

                    for lumiyear in official_lumis:
                        if runyear in lumiyear:

                            sum_lumi += official_lumis[lumiyear]

                            fitresults["fit_sf"][
                                runyear + category] += fitresults["fit_sf"][
                                    lumiyear +
                                    category] * official_lumis[lumiyear]
                            fitresults["fit_uncert"][
                                runyear +
                                category] += fitresults["fit_uncert"][
                                    lumiyear +
                                    category] * official_lumis[lumiyear]
                            fitresults["fit_sfreco"][
                                runyear +
                                category] += fitresults["fit_sfreco"][
                                    lumiyear +
                                    category] * official_lumis[lumiyear]
                            fitresults["fit_uncertreco"][
                                runyear +
                                category] += fitresults["fit_uncertreco"][
                                    lumiyear +
                                    category] * official_lumis[lumiyear]
                            fitresults["fit_sftag"][
                                runyear + category] += fitresults["fit_sftag"][
                                    lumiyear +
                                    category] * official_lumis[lumiyear]
                            fitresults["fit_uncerttag"][
                                runyear +
                                category] += fitresults["fit_uncerttag"][
                                    lumiyear +
                                    category] * official_lumis[lumiyear]

                    print runyear, sum_lumi

                    fitresults["fit_sf"][runyear + category] /= sum_lumi
                    fitresults["fit_uncert"][runyear + category] /= sum_lumi
                    fitresults["fit_sfreco"][runyear + category] /= sum_lumi
                    fitresults["fit_uncertreco"][runyear +
                                                 category] /= sum_lumi
                    fitresults["fit_sftag"][runyear + category] /= sum_lumi
                    fitresults["fit_uncerttag"][runyear + category] /= sum_lumi

        # efficiencies:

        print "plot SF..."

        output_rootfile = TFile(
            "%s/allperiods_sf_combined.root" % (plotfolder), "recreate")

        for category in ["short", "long"]:

            canvas = shared_utils.mkcanvas()
            legend = shared_utils.mklegend(x1=0.47, y1=0.65, x2=0.85, y2=0.85)
            legend.SetHeader("%s tracks %s" %
                             (category, cuts[cut_label]["legendheader"]))
            legend.SetTextSize(0.035)

            h_sf_short = {}
            h_sf_long = {}

            for label in ["fit_sf", "fit_sfreco", "fit_sftag"]:

                if options.lumiweighted:
                    h_sf_short[label] = TH1F("h_sf_short[label]", "", 3, 0, 3)
                    h_sf_long[label] = TH1F("h_sf_long[label]", "", 3, 0, 3)
                else:
                    h_sf_short[label] = TH1F("h_sf_short[label]", "", 16, 0,
                                             16)
                    h_sf_long[label] = TH1F("h_sf_long[label]", "", 16, 0, 16)

                shared_utils.histoStyler(h_sf_short[label])
                shared_utils.histoStyler(h_sf_long[label])

                i_short = 0
                i_long = 0
                binlabels_short = []
                binlabels_long = []

                for i, period in enumerate(sorted(fitresults[label])):

                    sf = fitresults[label][period]
                    uncert = fitresults[label.replace("_sf",
                                                      "_uncert")][period]

                    if options.lumiweighted:
                        if period.split("_")[0] == "Run2016" or period.split(
                                "_")[0] == "Run2017" or period.split(
                                    "_")[0] == "Run2018":
                            pass
                        else:
                            continue

                    print category, period

                    if "short" in period:
                        h_sf_short[label].SetBinContent(i_short + 1, sf)
                        h_sf_short[label].SetBinError(i_short + 1, uncert)
                        i_short += 1
                        binlabels_short.append(
                            period.replace("Run", "").replace("_short",
                                                              "").replace(
                                                                  "_long", ""))
                    elif "long" in period:
                        h_sf_long[label].SetBinContent(i_long + 1, sf)
                        h_sf_long[label].SetBinError(i_long + 1, uncert)
                        i_long += 1
                        binlabels_long.append(
                            period.replace("Run", "").replace("_short",
                                                              "").replace(
                                                                  "_long", ""))

                if category == "short":
                    if label == "fit_sf":
                        h_sf_short[label].Draw("hist e")
                    else:
                        h_sf_short[label].Draw("hist e same")

                    outhist = h_sf_short[label].Clone()
                    outhist.SetDirectory(0)
                    outhist.SetName(label + "_" + category)
                    outhist.Write()

                    if "reco" in label:
                        h_sf_short[label].SetTitle(
                            ";;fitted track reconstruction scale factor")
                    elif "tag" in label:
                        h_sf_short[label].SetTitle(
                            ";;fitted track tagging scale factor")
                    else:
                        h_sf_short[label].SetTitle(";;fitted scale factor")
                    h_sf_short[label].GetYaxis().SetRangeUser(0.4, 1.6)
                else:
                    if label == "fit_sf":
                        h_sf_long[label].Draw("hist e")
                    else:
                        h_sf_long[label].Draw("hist e same")

                    outhist = h_sf_long[label].Clone()
                    outhist.SetDirectory(0)
                    outhist.SetName(label + "_" + category)
                    outhist.Write()

                    if "reco" in label:
                        h_sf_long[label].SetTitle(
                            ";;fitted track reconstruction scale factor")
                    elif "tag" in label:
                        h_sf_long[label].SetTitle(
                            ";;fitted track tagging scale factor")
                    else:
                        h_sf_long[label].SetTitle(";;fitted scale factor")
                    h_sf_long[label].GetYaxis().SetRangeUser(0.4, 1.6)

                if label == "fit_sf":
                    #h_sf_short[label].SetLineStyle(1)
                    #h_sf_long[label].SetLineStyle(1)
                    h_sf_short[label].SetLineWidth(3)
                    h_sf_long[label].SetLineWidth(3)
                    h_sf_short[label].SetLineColor(kRed)
                    h_sf_long[label].SetLineColor(kBlue)
                    if category == "short":
                        legend.AddEntry(h_sf_short[label], "combined SF")
                    else:
                        legend.AddEntry(h_sf_long[label], "combined SF")
                if label == "fit_sfreco":
                    #h_sf_short[label].SetLineStyle(2)
                    #h_sf_long[label].SetLineStyle(2)
                    h_sf_short[label].SetLineColor(97)
                    h_sf_long[label].SetLineColor(62)
                    if category == "short":
                        legend.AddEntry(h_sf_short[label], "reconstruction SF")
                    else:
                        legend.AddEntry(h_sf_long[label], "reconstruction SF")
                elif label == "fit_sftag":
                    h_sf_short[label].SetLineStyle(2)
                    h_sf_long[label].SetLineStyle(2)
                    h_sf_short[label].SetLineColor(97)
                    h_sf_long[label].SetLineColor(62)
                    if category == "short":
                        legend.AddEntry(h_sf_short[label], "tagging SF")
                    else:
                        legend.AddEntry(h_sf_long[label], "tagging SF")

                for i, i_binlabel in enumerate(binlabels_short):
                    h_sf_short[label].GetXaxis().SetBinLabel(i + 1, i_binlabel)
                for i, i_binlabel in enumerate(binlabels_long):
                    h_sf_long[label].GetXaxis().SetBinLabel(i + 1, i_binlabel)

                if options.lumiweighted:
                    h_sf_short[label].GetXaxis().SetLabelSize(0.09)
                    h_sf_short[label].GetXaxis().SetTitleSize(0.09)
                    h_sf_long[label].GetXaxis().SetLabelSize(0.09)
                    h_sf_long[label].GetXaxis().SetTitleSize(0.09)
                else:
                    h_sf_short[label].GetXaxis().SetTitleSize(0.045)
                    h_sf_short[label].GetXaxis().SetLabelSize(0.045)
                    h_sf_long[label].GetXaxis().SetTitleSize(0.045)
                    h_sf_long[label].GetXaxis().SetLabelSize(0.045)

            legend.Draw()

            shared_utils.stamp()

            pdfname = "%s/allperiods_sf_combined_%s_%s.pdf" % (
                plotfolder, category, cut_label)

            if options.get_from_tree:
                pdfname = pdfname.replace(".pdf", "_tree.pdf")
            if options.mc_reweighted:
                pdfname = pdfname.replace(".pdf", "_mcreweighted.pdf")
            if options.lumiweighted:
                pdfname = pdfname.replace(".pdf", "_lumiweighted.pdf")
            if options.weightkinematic:
                pdfname = pdfname.replace(".pdf", "_weightkinematic.pdf")
            if options.weighttrackprop:
                pdfname = pdfname.replace(".pdf", "_weighttrackprop.pdf")

            canvas.SaveAs(pdfname)
            #canvas.SaveAs(pdfname.replace(".pdf", ".root"))

            #fout = TFile(pdfname.replace(".pdf", ".root"), "recreate")
            #canvas.Write()
            #h_sf_short.SetName("h_scalefactor_short")
            #h_sf_short.Write()
            #h_sf_long.SetName("h_scalefactor_long")
            #h_sf_long.Write()
            #fout.Close()

        output_rootfile.Close()

        if options.lumiweighted:
            #this_periods = finaleff_global_num[category].keys()
            this_periods = [
                "Run2016",
                "Run2017",
                "Run2018",
            ]
        else:
            this_periods = periods

        # plot underlying efficiencies and SFs:

        if not options.lumiweighted and not options.get_from_tree:

            for i_finaleff, finaleff in enumerate(
                [finaleff_global, finaleff_reco, finaleff_tag]):

                for year in ["2016", "2017", "2018"]:

                    for category in categories:

                        canvas = shared_utils.mkcanvas()
                        legend = shared_utils.mklegend(x1=0.6,
                                                       y1=0.6,
                                                       x2=0.85,
                                                       y2=0.85)
                        legend.SetHeader("%s tracks (%s)" %
                                         (category.replace("_", ""), year))
                        legend.SetTextSize(0.035)

                        colors = [kBlack, 97, 94, 91, 86, 81, 70, 65, 61, 51]

                        for i_period, period in enumerate(this_periods):

                            if "rw" in period: continue

                            if year == "2016":
                                mcperiod = "Summer16"
                            elif year == "2017":
                                mcperiod = "Fall17"
                            elif year == "2018":
                                mcperiod = "Autumn18"

                            if period == mcperiod or year in period:

                                color = colors.pop(0)

                                if i_period == 0:
                                    drawoption = "p e"
                                else:
                                    drawoption = "hist e same"

                                print finaleff[category].keys()

                                if period != mcperiod:
                                    finaleff[category][period].SetMarkerStyle(
                                        20)
                                    finaleff[category][period].SetMarkerColor(
                                        color)

                                finaleff[category][period].Draw(drawoption)
                                finaleff[category][period].SetLineColor(color)
                                finaleff[category][period].SetLineStyle(1)
                                finaleff[category][period].SetTitle(
                                    ";number of remaining tracker layers;efficiency, scale factor"
                                )
                                finaleff[category][period].GetXaxis(
                                ).SetRangeUser(0, 20)
                                finaleff[category][period].GetYaxis(
                                ).SetRangeUser(0.4, 1.6)

                                legend.AddEntry(finaleff[category][period],
                                                period)

                                # include scale factor with fit result...:

                                if "Run" in period and "rw" not in period:

                                    if i_finaleff == 0:
                                        h_sf_global[category][period].Draw(
                                            "e same")
                                        h_sf_global[category][
                                            period].SetLineColor(color)
                                        h_sf_global[category][
                                            period].SetLineWidth(2)
                                        g1_global[category][period].Draw(
                                            "same")
                                        g1_global[category][
                                            period].SetLineColor(color)
                                        g1_global[category][
                                            period].SetLineWidth(2)
                                        #legend.AddEntry(g1_global[category][period], "scale factor")
                                    elif i_finaleff == 1:
                                        h_sf_reco[category][period].Draw(
                                            "e same")
                                        h_sf_reco[category][
                                            period].SetLineColor(color)
                                        h_sf_reco[category][
                                            period].SetLineWidth(2)
                                        g1_reco[category][period].Draw("same")
                                        g1_reco[category][period].SetLineColor(
                                            color)
                                        g1_reco[category][period].SetLineWidth(
                                            2)
                                        #legend.AddEntry(g1_reco[category][period], "scale factor")
                                    elif i_finaleff == 2:
                                        h_sf_tag[category][period].Draw(
                                            "e same")
                                        h_sf_tag[category][
                                            period].SetLineColor(color)
                                        h_sf_tag[category][
                                            period].SetLineWidth(2)
                                        g1_tag[category][period].Draw("same")
                                        g1_tag[category][period].SetLineColor(
                                            color)
                                        g1_tag[category][period].SetLineWidth(
                                            2)
                                        #legend.AddEntry(g1_tag[category][period], "scale factor")

                                    #finaleff_global_num[category][period].Draw("e same")
                                    #finaleff_global_num[category][period].SetLineColor(color)
                                    #finaleff_global_num[category][period].SetLineWidth(2)
                                    #finaleff_global_denom[category][period].Draw("e same")
                                    #finaleff_global_denom[category][period].SetLineColor(color)
                                    #finaleff_global_denom[category][period].SetLineWidth(2)
                                    #finaleff_global_denom[category][period].SetLineStyle(2)

                        legend.Draw()
                        shared_utils.stamp()

                        if i_finaleff == 0:
                            pdfname = "%s/underlying%s%s.pdf" % (
                                plotfolder, category, year)
                        elif i_finaleff == 1:
                            pdfname = "%s/underlying_reco%s%s.pdf" % (
                                plotfolder, category, year)
                        elif i_finaleff == 2:
                            pdfname = "%s/underlying_tag%s%s.pdf" % (
                                plotfolder, category, year)

                        if options.get_from_tree:
                            pdfname = pdfname.replace(".pdf", "_tree.pdf")
                        if options.mc_reweighted:
                            pdfname = pdfname.replace(".pdf",
                                                      "_mcreweighted.pdf")
                        if options.lumiweighted:
                            pdfname = pdfname.replace(".pdf",
                                                      "_lumiweighted.pdf")
                        #if not plot_sf:
                        #    pdfname = pdfname.replace(".pdf", "_numdenom.pdf")

                        canvas.SaveAs(pdfname)