Beispiel #1
0
def produce_limit_plot(pattern="T1qqqqLL_allbins",
                       draw_xsecs=True,
                       smoothing=False):

    xsecGlu = getxsecGlu()

    hexp = TH2F('hexp', 'hexp', 113, -12.5, 2812.5, 113, -12.5, 2812.5)
    hexpdown = TH2F('hexpdown', 'hexpdown', 113, -12.5, 2812.5, 113, -12.5,
                    2812.5)
    hexpup = TH2F('hexpup', 'hexpup', 113, -12.5, 2812.5, 113, -12.5, 2812.5)
    hobs = TH2F('hobs', 'hobs', 113, -12.5, 2812.5, 113, -12.5, 2812.5)

    vmx = []
    vmy = []
    vxsec = []
    vobs = []
    vobsup = []
    vobsdown = []
    vexp = []
    vup = []
    vdown = []
    vlim = []

    for signal_point_file in glob.glob(pattern + '/higgsCombine*root'):

        print signal_point_file
        mGo = int(signal_point_file.split("Signalg")[-1].split("_chi")[0])
        mLSP = int(
            signal_point_file.split("_chi")[-1].split(".AsymptoticLimits")[0])
        print 'mGo', mGo
        print 'mLSP', mLSP

        f = TFile.Open(signal_point_file, 'read')
        t = f.Get('limit')

        try:
            entries = t.GetEntries()
        except:
            continue

        if "T1qqqq" in pattern:
            xsec = xsecGlu[mGo][0]
            theorySys = xsecGlu[mGo][1]
        elif "T2bt" in pattern:
            xsec = get_sbottom_antisbottom_cross_section(mGo)
            theorySys = get_sbottom_antisbottom_cross_section(mGo)

        rExp = 0
        rObs = 0
        rExp1SigmaDown = 0
        rExp1SigmaUp = 0
        factor = 1.0
        if mGo < 1400:
            factor = 100.0

        for entry in t:
            q = entry.quantileExpected
            if q == 0.5: rExp = entry.limit / factor
            if q == -1: rObs = entry.limit / factor
            if q < 0.4 and q > 0.14: rExp1SigmaDown = entry.limit / factor
            if q < 0.14: rExp2SigmaDown = entry.limit / factor
            if q > 0.6 and q < 0.9: rExp1SigmaUp = entry.limit / factor
            if q > 0.9: rExp2SigmaUp = entry.limit / factor

        hexp.Fill(mGo, mLSP, rExp)
        hexpdown.Fill(mGo, mLSP, rExp1SigmaDown)
        hexpup.Fill(mGo, mLSP, rExp1SigmaUp)
        hobs.Fill(mGo, mLSP, rObs)
        vmx.append(mGo)
        vmy.append(mLSP)
        vxsec.append(xsec)
        vlim.append(xsec * rObs)
        vobs.append(rObs)
        vobsup.append(rObs * (1 + theorySys / 100.0))
        vobsdown.append(rObs * (1 - theorySys / 100.0))
        vexp.append(rExp)
        vup.append(rExp1SigmaUp)
        vdown.append(rExp1SigmaDown)
        f.Close()

    #hexp.SaveAs(pattern + '/testexp_' + pattern + '.root')
    #hobs.SaveAs(pattern + '/testobs_' + pattern + '.root')
    aexp = array("d", vexp)
    alim = array("d", vlim)
    aup = array("d", vup)
    adown = array("d", vdown)
    aobs = array("d", vobs)
    aobsup = array("d", vobsup)
    aobsdown = array("d", vobsdown)
    amx = array("d", vmx)
    amy = array("d", vmy)

    glim = TGraph2D("glim", "Cross-section limt", len(vlim), amx, amy, alim)
    gexp = TGraph2D("gexp", "Expected Limit", len(vexp), amx, amy, aexp)
    gup = TGraph2D("gup", "Expected Limit 1sigma up", len(vup), amx, amy, aup)
    gdown = TGraph2D("gdown", "Expected Limit 1sigma down", len(vdown), amx,
                     amy, adown)
    gobs = TGraph2D("gobs", "Observed Limit", len(vobs), amx, amy, aobs)
    gobsup = TGraph2D("gobsup", "theory 1sigma up", len(vobsup), amx, amy,
                      aobsup)
    gobsdown = TGraph2D("gobsdown", "theory 1sigma down", len(vobsdown), amx,
                        amy, aobsdown)
    c = TCanvas("c", "c", 900, 800)
    c.SetLeftMargin(0.15)
    c.SetRightMargin(0.15)
    c.SetTopMargin(0.1)
    c.SetLogz(True)

    xmin = min(vmx)
    xmax = max(vmx)
    ymin = min(vmy)
    ymax = max(vmy)
    bin_size = 12.5
    nxbins = max(1, min(500, (math.ceil((xmax - xmin) / bin_size))))
    nybins = max(1, min(500, (math.ceil((ymax - ymin) / bin_size))))
    glim.SetNpx(int(nxbins))
    glim.SetNpy(int(nybins))

    cexp = TryToGetContours(gexp, 2, 1, smoothing=smoothing)
    cup = TryToGetContours(gup, 2, 2, smoothing=smoothing)
    cdown = TryToGetContours(gdown, 2, 2, smoothing=smoothing)
    cobs = TryToGetContours(gobs, 1, 1, smoothing=smoothing)
    cobsup = TryToGetContours(gobsup, 1, 2, smoothing=smoothing)
    cobsdown = TryToGetContours(gobsdown, 1, 2, smoothing=smoothing)

    hlim = glim.GetHistogram()

    if "T1qqqq" in pattern:
        hlim.SetTitle(";m_{gluino} [GeV];m_{LSP} [GeV]")
    elif "T2bt" in pattern:
        hlim.SetTitle(";m_{stop} [GeV];m_{LSP} [GeV]")

    hlim.Draw("colz")
    #hlim.Draw("text")
    if "T2bt" in pattern:
        hlim.GetXaxis().SetRangeUser(0, 2000)
        hlim.GetYaxis().SetRangeUser(50, 2000)
    cexp.Draw("same")
    cup.Draw("same")
    cdown.Draw("same")
    #cobs.Draw("same")
    #cobsup.Draw("same")
    #cobsdown.Draw("same")
    flimit = TFile(pattern + "/limit_scan.root", "recreate")

    cobs.SetTitle("Observed Limit")
    cobsup.SetTitle("Observed -1#sigma Limit")
    cobsdown.SetTitle("Observed +1#sigma Limit")
    cexp.SetTitle("Expected  Limit")
    cup.SetTitle("Expected -1#sigma Limit")
    cdown.SetTitle("Expected +1#sigma Limit")

    hlim.Write("T1ttttObservedExcludedXsec")
    cobs.Write("T1ttttObservedLimit")
    cobsup.Write("T1ttttObservedLimitDown")
    cobsdown.Write("T1ttttObservedLimitUp")
    cexp.Write("T1ttttExpectedLimit")
    cup.Write("T1ttttExpectedLimitDown")
    cdown.Write("T1ttttExpectedLimitUp")

    shared_utils.stamp()

    label = pattern.split("/")[-1].replace("_", ", ")

    latex_label = TLatex()
    latex_label.SetNDC()
    latex_label.SetTextColor(kBlack)
    latex_label.SetTextAlign(11)
    latex_label.SetTextSize(0.035)
    latex_label.DrawLatex(0.2, 0.8, label)

    c.SaveAs("../" + pattern.split("/")[-1] + '.pdf')
Beispiel #2
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()
                ndepth = tmva_folder.split("ndepth")[1].split("-")[0]
                legend.AddEntry(histos[tmva_folder],
                                "depth=%s, %s trees" % (ndepth, ntrees))
                area_under_curve = histos[tmva_folder].Integral() / 100.0
                rocarea["%s, %s tracks" % (phase, category)].SetBinContent(
                    iBin, area_under_curve)
                binLabels[iBin] = "%s, %s" % (ndepth, ntrees)

        for i, label in enumerate(histos):
            if i == 0:
                histos[label].Draw("hist")
            else:
                histos[label].Draw("hist same")

        legend.Draw()
        shared_utils.stamp(showlumi=False)
        canvas.SaveAs("optimize_%s_%s.pdf" % (phase, category))

# ROC plot:
canvas = shared_utils.mkcanvas("c1")
colors = [kBlue, kRed, kOrange, kMagenta]
legend = shared_utils.mklegend(x1=.2, y1=.2, x2=.5, y2=.4)
legend.SetTextSize(0.035)

for i, label in enumerate(rocarea):
    if i == 0:
        rocarea[label].Draw("hist")
    else:
        rocarea[label].Draw("hist same")

    rocarea[label].GetYaxis().SetRangeUser(0.85, 1)
Beispiel #4
0
def get_fakerate(path,
                 variable,
                 rootfile,
                 foldername,
                 base_cuts,
                 numerator_cuts,
                 denominator_cuts,
                 selected_sample,
                 extra_text,
                 binning,
                 threads,
                 nBinsX=False,
                 xmin=False,
                 xmax=False,
                 nBinsY=False,
                 ymin=False,
                 ymax=False,
                 xlabel=False,
                 ylabel=False,
                 do_interpolation=False):

    print "Getting fake rate for", variable, selected_sample, extra_text

    if ":" in variable:
        plot2D = True
    else:
        plot2D = False

    if not plot2D:
        if binning[0] == "variable":
            nbins = len(binning[1]) - 1
            histos[label] = histos[label].Rebin(nbins, label,
                                                array.array('d', binning[1]))
        else:
            nBinsX = binning[0]
            xmin = binning[1]
            xmax = binning[2]
    else:
        if binning[0] == "variable":
            nbinsX = len(binning[1]) - 1
            nbinsY = len(binning[2]) - 1
            histos[label] = histos[label].RebinX(nbinsX, label,
                                                 array.array('d', binning[1]))
            histos[label] = histos[label].RebinY(nbinsY, label,
                                                 array.array('d', binning[2]))
        else:
            nBinsX = binning[0]
            xmin = binning[1]
            xmax = binning[2]
            nBinsY = binning[3]
            ymin = binning[4]
            ymax = binning[5]

    if plot2D:
        fakes_numerator = plotting.get_histogram(
            variable,
            base_cuts + numerator_cuts,
            nBinsX=nBinsX,
            xmin=xmin,
            xmax=xmax,
            nBinsY=nBinsY,
            ymin=ymin,
            ymax=ymax,
            path=path,
            selected_sample=selected_sample,
            threads=threads)
        fakes_denominator = plotting.get_histogram(
            variable,
            base_cuts + denominator_cuts,
            nBinsX=nBinsX,
            xmin=xmin,
            xmax=xmax,
            nBinsY=nBinsY,
            ymin=ymin,
            ymax=ymax,
            path=path,
            selected_sample=selected_sample,
            threads=threads)
    else:
        fakes_numerator = plotting.get_histogram(
            variable,
            base_cuts + numerator_cuts,
            nBinsX=nBinsX,
            xmin=xmin,
            xmax=xmax,
            path=path,
            selected_sample=selected_sample,
            threads=threads)
        fakes_denominator = plotting.get_histogram(
            variable,
            base_cuts + denominator_cuts,
            nBinsX=nBinsX,
            xmin=xmin,
            xmax=xmax,
            path=path,
            selected_sample=selected_sample,
            threads=threads)

    try:
        print fakes_numerator.GetEntries()
        print fakes_denominator.GetEntries()
    except:
        print "Error while getting histogram!"
        return

    fout = TFile(rootfile, "update")
    fout.cd()
    gDirectory.mkdir(foldername)
    fout.cd(foldername)

    fake_rate = fakes_numerator.Clone()
    fake_rate.Divide(fakes_denominator)
    fake_rate.SetName("fakerate_%s" % (variable.replace(":", "_")))

    labels = {
        "HT": "H_{T} (GeV)",
        "MHT": "missing H_{T} (GeV)",
        "HT_cleaned": "cleaned H_{T} (GeV)",
        "MHT_cleaned": "cleaned missing H_{T} (GeV)",
        "n_allvertices": "number of vertices"
    }
    if not xlabel:
        xlabel = variable.split(":")[0]
        if xlabel in labels:
            xlabel = labels[xlabel]
    if not ylabel and plot2D:
        ylabel = variable.split(":")[1]
        if ylabel in labels:
            ylabel = labels[ylabel]

    if plot2D:
        fake_rate.SetTitle(";%s; %s; fake rate" % (ylabel, xlabel))
    else:
        fake_rate.SetTitle(";%s; fake rate" % xlabel)

    shared_utils.histoStyler(fake_rate)
    shared_utils.stamp()
    fake_rate.SetName("fakerate_%s" %
                      (variable.replace(":", "_").replace("_cleaned", "")))
    fake_rate.SetDirectory(gDirectory)
    fake_rate.Write()

    # also save interpolated histogram:
    if plot2D and do_interpolation:
        fake_rate_interpolated = get_interpolated_histogram(fake_rate)
        fake_rate_interpolated.SetName("fakerate_%s_interpolated" %
                                       (variable.replace(":", "_")))
        fake_rate_interpolated.SetDirectory(gDirectory)
        fake_rate_interpolated.Write()

    fout.Close()
Beispiel #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)
Beispiel #6
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)
Beispiel #7
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")
Beispiel #8
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))
                        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")

                if ":" not in variable:
                    for category in ["long", "short"]:

                        if category == "combined" and variable != "tracks_is_pixel_track":
Beispiel #10
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)
Beispiel #12
0
def plot_cutflow(files, header, is_signal, prefix):

    if is_signal:
        signal_cutstring = " && tracks_chiCandGenMatchingDR<0.01"
    else:
        signal_cutstring = ""

    histos = {}
    for label in cuts:
        histos[label] = TH1D(label, label, 20, 0, 20)

    # get consecutive cuts:
    cuts_consecutive = {}
    for label in cuts:
        cuts_consecutive[label] = []
        for i, cut in enumerate(cuts[label]):
            cuts_consecutive[label].append(" && ".join(cuts[label][:i + 1]))

    # get nev:
    counts = {}
    for label in cuts_consecutive:
        counts[label] = []
        for i, cut in enumerate(cuts_consecutive[label]):

            if "p1" in prefix:
                #FIXME phase 1 dE/dx
                cut = cut.replace("tracks_deDxHarmonic2pixel>2.0", "MHT>=0")

            h_tmp = plotting.get_all_histos(files,
                                            "Events",
                                            "tracks_is_pixel_track",
                                            cut + signal_cutstring,
                                            nBinsX=2,
                                            xmin=0,
                                            xmax=2)
            if h_tmp:
                count = h_tmp.Integral()
            else:
                count = 0
            counts[label].append(count)
            histos[label].Fill(i, count)

    # normalize histos:
    for label in histos:
        normalization = histos[label].GetBinContent(1)
        if normalization > 0:
            histos[label].Scale(1.0 / normalization)

    # set alphanumeric x-axis labels:
    for label in histos:
        for i in range(histos[label].GetNbinsX()):
            if i <= len(cuts[label]):

                binlabel = cuts[label][i - 1]
                if "tracks_is_pixel_track" in binlabel: binlabel = "category"
                elif "tracks_ptErrOverPt2" in binlabel:
                    binlabel = "#Delta p_{T}"
                elif "tracks_neutralPtSum/tracks_pt" in binlabel:
                    binlabel = "nt. #Sigma p_{T}/pT"
                elif "tracks_neutralPtSum" in binlabel:
                    binlabel = "nt. pTSum"
                elif "tracks_chargedPtSum/tracks_pt" in binlabel:
                    binlabel = "ch. #Sigma p_{T}/pT"
                elif "tracks_chargedPtSum" in binlabel:
                    binlabel = "ch. #Sigma p_{T}"
                elif "tracks_pt" in binlabel:
                    binlabel = "p_{T}"
                elif "tracks_passmask" in binlabel:
                    binlabel = "mask"
                elif "tracks_trackQualityHighPurity" in binlabel:
                    binlabel = "purity"
                elif "tracks_eta" in binlabel:
                    binlabel = "#eta"
                elif "tracks_dzVtx" in binlabel:
                    binlabel = "d_{z}"
                elif "tracks_dxyVtx" in binlabel:
                    binlabel = "d_{xy}"
                elif "tracks_trkRelIso" in binlabel:
                    binlabel = "relIso"
                elif "tracks_trackerLayersWithMeasurement" in binlabel:
                    binlabel = "layers"
                elif "tracks_nValidTrackerHits" in binlabel:
                    binlabel = "tracker hits"
                elif "tracks_nMissingInnerHits" in binlabel:
                    binlabel = "miss. inner hits"
                elif "tracks_nValidPixelHits" in binlabel:
                    binlabel = "pixel hits"
                elif "tracks_passPFCandVeto" in binlabel:
                    binlabel = "PFCand"
                elif "tracks_passleptonveto" in binlabel:
                    binlabel = "lepton veto"
                elif "tracks_passpionveto" in binlabel:
                    binlabel = "pion veto"
                elif "tracks_passjetveto" in binlabel:
                    binlabel = "jet veto"
                elif "tracks_deDxHarmonic2pixel" in binlabel:
                    binlabel = "dE/dx"
                elif "tracks_mva_tight_may20_chi2" in binlabel:
                    binlabel = "BDT"
                elif "tracks_matchedCaloEnergy/tracks_p" in binlabel:
                    binlabel = "EDep/track p"
                elif "tracks_nMissingOuterHits" in binlabel:
                    binlabel = "miss. outer hits"
                elif "tracks_nMissingMiddleHits" in binlabel:
                    binlabel = "miss. middle hits"
                elif "tracks_exo_leptoniso" in binlabel:
                    binlabel = "lepton iso"
                elif "tracks_exo_trackiso" in binlabel:
                    binlabel = "track iso"
                elif "tracks_exo_jetiso" in binlabel:
                    binlabel = "jet iso"
                elif "tracks_matchedCaloEnergy" in binlabel:
                    binlabel = "EDep"
                elif "tracks_mt2_leptoniso" in binlabel:
                    binlabel = "lepton iso"
                elif "tracks_mt2_trackiso" in binlabel:
                    binlabel = "track iso"
                elif "tracks_pixelLayersWithMeasurement" in binlabel:
                    binlabel = "pixel layers"

                histos[label].GetXaxis().SetBinLabel(i, binlabel)

    for label in histos:

        if "long" in label: continue

        canvas = TCanvas("c1", "c1", 1000, 630)
        canvas.SetBottomMargin(.16)
        canvas.SetLeftMargin(.14)
        canvas.SetGrid()

        if "bg" in prefix:
            canvas.SetLogy()

        if is_signal:
            legend = shared_utils.mklegend(x1=0.17, y1=0.17, x2=0.4, y2=0.4)
        else:
            legend = shared_utils.mklegend(x1=0.6, y1=0.7, x2=0.9, y2=0.9)
        legend.SetTextSize(0.03)

        shared_utils.histoStyler(histos[label])
        histos[label].Draw("hist")
        histos[label].SetLineColor(kRed)
        histos[label].SetTitle(";;percentage of tracks remaining")
        legend.AddEntry(histos[label], "short tracks")

        if "bg" in prefix:
            histos[label].GetYaxis().SetRangeUser(1e-4, 2e0)
        else:
            histos[label].GetYaxis().SetRangeUser(0, 1.1)

        label_long = label.replace("short", "long")
        shared_utils.histoStyler(histos[label_long])
        histos[label_long].Draw("hist same")
        histos[label_long].SetLineColor(kBlue)
        histos[label_long].SetTitle(";cut stage;tracks")
        legend.AddEntry(histos[label_long], "long tracks")

        legend.SetTextSize(0.045)
        legend.SetHeader(header + ", " +
                         label.replace("_short", "").replace("_", " ") +
                         " tag")
        legend.Draw()

        shared_utils.stamp()

        batchname = files[0].split("/")[2]
        canvas.Print("plots/cutflow_" + batchname + "_" + prefix + "_" +
                     label.replace("_short", "") + ".pdf")