Exemplo n.º 1
0
def getEfficiency(passes, passesError, total, totalError):
    passesHist = TH1D("passes", "", 1, 0.0, 1.0)
    totalHist = TH1D("total", "", 1, 0.0, 1.0)

    passesHist.SetBinContent(1, passes)
    passesHist.SetBinError(1, passesError)
    totalHist.SetBinContent(1, total)
    totalHist.SetBinError(1, totalError)

    g = TGraphAsymmErrors(passesHist, totalHist)
    x = Double(0.0)
    y = Double(0.0)
    g.GetPoint(0, x, y)

    return (y, g.GetErrorYlow(0), g.GetErrorYhigh(0))
def getEfficiency(nominatorHisto, denominatorHisto, cutString):
    eff = TGraphAsymmErrors(nominatorHisto, denominatorHisto, "cp")
    effValue = ROOT.Double(0.)
    blubb = ROOT.Double(0.)
    intttt = eff.GetPoint(0, blubb, effValue)

    n = {
        "Nominator": nominatorHisto.Integral(),
        "Denominator": denominatorHisto.Integral(),
        "Efficiency": nominatorHisto.Integral() / denominatorHisto.Integral(),
        "UncertaintyUp": eff.GetErrorYhigh(0),
        "UncertaintyDown": eff.GetErrorYlow(0),
    }
    #	print cut, n
    n["cut"] = cutString
    return n
def plotDataOverMCEff(hist_mc_tight,
                      hist_mc_loose,
                      hist_data_tight,
                      hist_data_loose,
                      plot_name='fakerate.pdf'):

    g = TGraphAsymmErrors(hist_mc_tight)
    g.Divide(hist_mc_tight, hist_mc_loose)
    g.GetYaxis().SetTitle('Fake rate')
    g.GetXaxis().SetTitle(hist_mc_tight.GetXaxis().GetTitle())
    g.GetYaxis().SetTitleOffset(1.2)
    g.GetYaxis().SetTitleOffset(1.3)

    g.SetLineColor(2)
    g.SetMarkerColor(2)

    g_data = TGraphAsymmErrors(hist_data_tight)
    g_data.Divide(hist_data_tight, hist_data_loose)
    g_data.GetYaxis().SetTitle('Fake rate')
    g_data.GetXaxis().SetTitle(hist_data_tight.GetXaxis().GetTitle())
    g_data.GetYaxis().SetTitleOffset(1.2)
    g_data.GetYaxis().SetTitleOffset(1.3)
    g_data.SetMarkerColor(1)

    g_vals = g.GetY()
    g_data_vals = g_data.GetY()

    g_ratio = g_data.Clone('ratio')

    for i in xrange(g_data.GetN()):
        ratio = g_data_vals[i] / g_vals[i] if g_vals[i] else 0.
        g_ratio.SetPoint(i, g.GetX()[i], ratio)

        rel_y_low = math.sqrt((g_data.GetErrorYlow(i) / g_data_vals[i])**2 + (
            g.GetErrorYlow(i) /
            g_vals[i])**2) if g_data_vals[i] > 0. and g_vals[i] > 0. else 0.

        g_ratio.SetPointEYlow(i, rel_y_low * ratio)

        rel_y_high = math.sqrt(
            (g_data.GetErrorYhigh(i) / g_data_vals[i])**2 +
            (g.GetErrorYhigh(i) /
             g_vals[i])**2) if g_data_vals[i] > 0. and g_vals[i] > 0. else 0.

        g_ratio.SetPointEYhigh(i, rel_y_high * ratio)

    # Gymnastics to get same label sizes etc in ratio and main plot
    ytp_ratio = 2.
    xtp_ratio = 2.

    # hr.GetYaxis().SetNdivisions(4)

    g_ratio.GetYaxis().SetTitleSize(g.GetYaxis().GetTitleSize() * xtp_ratio)
    g_ratio.GetXaxis().SetTitleSize(g.GetXaxis().GetTitleSize() * ytp_ratio)

    g_ratio.GetYaxis().SetTitleOffset(g.GetYaxis().GetTitleOffset() /
                                      xtp_ratio)
    g_ratio.GetXaxis().SetTitleOffset(
        g.GetXaxis().GetTitleOffset())  # / ytp_ratio)

    g_ratio.GetYaxis().SetLabelSize(g.GetYaxis().GetLabelSize() * xtp_ratio)
    g_ratio.GetXaxis().SetLabelSize(g.GetXaxis().GetLabelSize() * ytp_ratio)

    g_data.GetXaxis().SetLabelColor(0)
    g_data.GetXaxis().SetLabelSize(0)
    g.GetXaxis().SetLabelColor(0)
    g.GetXaxis().SetLabelSize(0)

    g_ratio.GetXaxis().SetTitle(g.GetXaxis().GetTitle())

    # maxy = 1.1 * min(g.GetMaximum(), g_data.GetMaximum(), 0.2)
    g.GetYaxis().SetRangeUser(0.001, 0.2)

    cv, pad, padr = HistDrawer.buildCanvas()

    pad.cd()

    g.Draw('AP')
    g_data.Draw('P')

    legend = TLegend(0.23, 0.73, 0.43, 0.91)
    legend.SetFillColor(0)
    legend.SetFillStyle(0)
    legend.SetLineColor(0)
    legend.SetLineWidth(0)

    legend.AddEntry(g.GetName(), 'MC', 'lep')
    legend.AddEntry(g_data.GetName(), 'Observed', 'lep')

    legend.Draw()

    padr.cd()
    g_ratio.GetYaxis().SetRangeUser(0.51, 1.49)
    g_ratio.GetYaxis().SetTitle('Obs/MC')
    g_ratio.Draw('AP')

    drawRatioLines(g_ratio)

    cv.Print(plot_name)
Exemplo n.º 4
0
def limit(method, channel):
    particle = channel[1:2]
    particleP = particle + "'" if channel.startswith('X') else channel[0]
    THEORY = ['A1', 'B3'] if channel.startswith('X') else []

    suffix = ""
    if method == "hvt": suffix = "_HVT"
    if method == "cls": suffix = "_CLs"
    if method == "monoH": suffix = "_monoH"

    filename = "./combine/" + method + "/" + channel + "_M%d.txt"
    mass, val = fillValues(filename)

    Obs0s = TGraph()
    Exp0s = TGraph()
    Exp1s = TGraphAsymmErrors()
    Exp2s = TGraphAsymmErrors()
    Sign = TGraph()
    pVal = TGraph()
    Best = TGraphAsymmErrors()
    Theory = {}

    for i, m in enumerate(mass):
        if not m in val:
            print "Key Error:", m, "not in value map"
            continue
        n = Exp0s.GetN()
        Obs0s.SetPoint(n, m, val[m][0])
        Exp0s.SetPoint(n, m, val[m][3])
        Exp1s.SetPoint(n, m, val[m][3])
        Exp1s.SetPointError(n, 0., 0., val[m][3] - val[m][2],
                            val[m][4] - val[m][3])
        Exp2s.SetPoint(n, m, val[m][3])
        Exp2s.SetPointError(n, 0., 0., val[m][3] - val[m][1],
                            val[m][5] - val[m][3])
        if len(val[m]) > 6: Sign.SetPoint(n, m, val[m][6])
        if len(val[m]) > 7: pVal.SetPoint(n, m, val[m][7])
        if len(val[m]) > 8: Best.SetPoint(n, m, val[m][8])
        if len(val[m]) > 10:
            Best.SetPointError(n, 0., 0., abs(val[m][9]), val[m][10])

    for t in THEORY:
        Theory[t] = TGraphAsymmErrors()
        for m in sorted(HVT[t]['Z']['XS'].keys()):
            if m < mass[0] or m > mass[-1]: continue
            XsW, XsW_Up, XsW_Down, XsZ, XsZ_Up, XsZ_Down = 0., 0., 0., 0., 0., 0.
            XsZ = 1000. * HVT[t]['Z']['XS'][m] * HVT[t]['Z']['BR'][m]
            XsZ_Up = XsZ * (1. + math.hypot(HVT[t]['Z']['QCD'][m][0] - 1.,
                                            HVT[t]['Z']['PDF'][m][0] - 1.))
            XsZ_Down = XsZ * (1. - math.hypot(1. - HVT[t]['Z']['QCD'][m][0],
                                              1. - HVT[t]['Z']['PDF'][m][0]))

            n = Theory[t].GetN()
            Theory[t].SetPoint(n, m, XsW + XsZ)
            Theory[t].SetPointError(n, 0., 0.,
                                    (XsW - XsW_Down) + (XsZ - XsZ_Down),
                                    (XsW_Up - XsW) + (XsZ_Up - XsZ))

            Theory[t].SetLineColor(theoryLineColor[t])
            Theory[t].SetFillColor(theoryFillColor[t])
            Theory[t].SetFillStyle(theoryFillStyle[t])
            Theory[t].SetLineWidth(2)
            #Theory[t].SetLineStyle(7)

    Exp2s.SetLineWidth(2)
    Exp2s.SetLineStyle(1)
    Obs0s.SetLineWidth(3)
    Obs0s.SetMarkerStyle(0)
    Obs0s.SetLineColor(1)
    Exp0s.SetLineStyle(2)
    Exp0s.SetLineWidth(3)
    Exp1s.SetFillColor(417)  #kGreen+1
    Exp1s.SetLineColor(417)  #kGreen+1
    Exp2s.SetFillColor(800)  #kOrange
    Exp2s.SetLineColor(800)  #kOrange
    Exp2s.GetXaxis().SetTitle("m_{" + particleP + "} (GeV)")
    Exp2s.GetXaxis().SetTitleSize(Exp2s.GetXaxis().GetTitleSize() * 1.25)
    Exp2s.GetXaxis().SetNoExponent(True)
    Exp2s.GetXaxis().SetMoreLogLabels(True)
    Exp2s.GetYaxis().SetTitle("#sigma(" + particleP + ") #bf{#it{#Beta}}(" +
                              particleP + " #rightarrow " + particle +
                              "h) (fb)")
    Exp2s.GetYaxis().SetTitleOffset(1.5)
    Exp2s.GetYaxis().SetNoExponent(True)
    Exp2s.GetYaxis().SetMoreLogLabels()

    Sign.SetLineWidth(2)
    Sign.SetLineColor(629)
    Sign.GetXaxis().SetTitle("m_{" + particleP + "} (GeV)")
    Sign.GetXaxis().SetTitleSize(Sign.GetXaxis().GetTitleSize() * 1.1)
    Sign.GetYaxis().SetTitle("Significance")

    pVal.SetLineWidth(2)
    pVal.SetLineColor(629)
    pVal.GetXaxis().SetTitle("m_{" + particleP + "} (GeV)")
    pVal.GetXaxis().SetTitleSize(pVal.GetXaxis().GetTitleSize() * 1.1)
    pVal.GetYaxis().SetTitle("local p-Value")

    Best.SetLineWidth(2)
    Best.SetLineColor(629)
    Best.SetFillColor(629)
    Best.SetFillStyle(3003)
    Best.GetXaxis().SetTitle("m_{" + particleP + "} (GeV)")
    Best.GetXaxis().SetTitleSize(Best.GetXaxis().GetTitleSize() * 1.1)
    Best.GetYaxis().SetTitle("Best Fit (pb)")

    c1 = TCanvas("c1", "Exclusion Limits", 800, 600)
    c1.cd()
    #SetPad(c1.GetPad(0))
    c1.GetPad(0).SetTopMargin(0.06)
    c1.GetPad(0).SetRightMargin(0.05)
    c1.GetPad(0).SetLeftMargin(0.12)
    c1.GetPad(0).SetTicks(1, 1)
    #c1.GetPad(0).SetGridx()
    #c1.GetPad(0).SetGridy()
    c1.GetPad(0).SetLogy()
    Exp2s.Draw("A3")
    Exp1s.Draw("SAME, 3")
    for t in THEORY:
        Theory[t].Draw("SAME, L3")
        Theory[t].Draw("SAME, L3X0Y0")
    Exp0s.Draw("SAME, L")
    if not options.blind: Obs0s.Draw("SAME, L")
    #setHistStyle(Exp2s)
    Exp2s.GetXaxis().SetTitleSize(0.050)
    Exp2s.GetYaxis().SetTitleSize(0.050)
    Exp2s.GetXaxis().SetLabelSize(0.045)
    Exp2s.GetYaxis().SetLabelSize(0.045)
    Exp2s.GetXaxis().SetTitleOffset(0.90)
    Exp2s.GetYaxis().SetTitleOffset(1.25)
    Exp2s.GetYaxis().SetMoreLogLabels(True)
    Exp2s.GetYaxis().SetNoExponent(True)
    Exp2s.GetYaxis().SetRangeUser(0.01, 5.e3)
    #else: Exp2s.GetYaxis().SetRangeUser(0.1, 1.e2)
    Exp2s.GetXaxis().SetRangeUser(
        mass[0], min(mass[-1],
                     MAXIMUM[channel] if channel in MAXIMUM else 1.e6))
    drawAnalysis(channel)
    drawRegion(channel, True)
    drawCMS(LUMI, YEAR, "Preliminary")  #Preliminary

    # legend
    top = 0.9
    nitems = 4 + len(THEORY)

    leg = TLegend(0.55, top - nitems * 0.3 / 5., 0.98, top)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetHeader("95% CL upper limits")
    leg.AddEntry(Obs0s, "Observed", "l")
    leg.AddEntry(Exp0s, "Expected", "l")
    leg.AddEntry(Exp1s, "#pm 1 std. deviation", "f")
    leg.AddEntry(Exp2s, "#pm 2 std. deviation", "f")
    for t in THEORY:
        leg.AddEntry(Theory[t], theoryLabel[t], "fl")
    leg.Draw()
    latex = TLatex()
    latex.SetNDC()
    latex.SetTextSize(0.045)
    latex.SetTextFont(42)
    #latex.DrawLatex(0.66, leg.GetY1()-0.045, particleP+" #rightarrow "+particle+"h")

    leg2 = TLegend(0.12, 0.225 - 2 * 0.25 / 5., 0.65, 0.225)
    leg2.SetBorderSize(0)
    leg2.SetFillStyle(0)  #1001
    leg2.SetFillColor(0)
    c1.GetPad(0).RedrawAxis()
    """
    if True and channel.endswith('sl'):
        mass, val = fillValues("./combine/alpha/X"+particle+"Hbonly_M%d.txt")
        Exp, Obs = TGraphAsymmErrors(), TGraphAsymmErrors()
        for i, m in enumerate(mass):
            if not m in val: continue
            Exp.SetPoint(Exp.GetN(), m, val[m][3])
            Obs.SetPoint(Obs.GetN(), m, val[m][0])
        Exp.SetLineWidth(3)
        Exp.SetLineColor(602) #602
        Exp.SetLineStyle(5)
        Obs.SetLineWidth(3)
        Obs.SetLineColor(602)
        Exp.Draw("SAME, L")
        Obs.Draw("SAME, L")
        
        mass15, val15 = fillValues("./combine/Vh_2015/X"+particle+"h_M%d.txt")
        Exp15, Obs15 = TGraphAsymmErrors(), TGraphAsymmErrors()
        for i, m in enumerate(mass15):
            if not m in val: continue
            Exp15.SetPoint(Exp15.GetN(), m, val15[m][3]*multF*Theory['B3'].GetY()[i]*(0.625 if particle=='V' and m>3000 else 1.))
            Obs15.SetPoint(Obs15.GetN(), m, val15[m][0]*multF*Theory['B3'].GetY()[i]*(0.625 if particle=='V' and m>3000 else 1.))
        Exp15.SetLineWidth(3)
        Exp15.SetLineColor(856) #602
        Exp15.SetLineStyle(6)
        Obs15.SetLineWidth(3)
        Obs15.SetLineColor(856)
        Exp15.Draw("SAME, L")
        #Obs15.Draw("SAME, L")
        
        leg2.AddEntry(Exp, "1+2 b-tag", "l")
    """
    if True and channel == 'AZh':
        massLL, valLL = fillValues("./combine/AZh/AZhll_M%d.txt")
        ExpLL, ObsLL = TGraphAsymmErrors(), TGraphAsymmErrors()
        for i, m in enumerate(massLL):
            if not m in val: continue
            ExpLL.SetPoint(ExpLL.GetN(), m, valLL[m][3] * multF)
            ObsLL.SetPoint(ObsLL.GetN(), m, valLL[m][0] * multF)
        ExpLL.SetLineWidth(3)
        ExpLL.SetLineColor(833)  #602
        ExpLL.SetLineStyle(5)
        ObsLL.SetLineWidth(3)
        ObsLL.SetLineColor(833)
        ExpLL.Draw("SAME, L")
        #ObsLL.Draw("SAME, L")

        massNN, valNN = fillValues("./combine/AZh/AZhnn_M%d.txt")
        ExpNN, ObsNN = TGraphAsymmErrors(), TGraphAsymmErrors()
        for i, m in enumerate(massNN):
            if not m in val: continue
            ExpNN.SetPoint(ExpNN.GetN(), m, valNN[m][3] * multF)
            ObsNN.SetPoint(ObsNN.GetN(), m, valNN[m][0] * multF)
        ExpNN.SetLineWidth(3)
        ExpNN.SetLineColor(855)  #602
        ExpNN.SetLineStyle(6)
        ObsNN.SetLineWidth(3)
        ObsNN.SetLineColor(855)
        ExpNN.Draw("SAME, L")
        #ObsNN.Draw("SAME, L")

        leg2.AddEntry(ExpLL,
                      "Expected, A #rightarrow Zh #rightarrow llb#bar{b}", "l")
        leg2.AddEntry(ExpNN,
                      "Expected, A #rightarrow Zh #rightarrow #nu#nub#bar{b}",
                      "l")

    if method == 'combo':
        massAH, valAH = fillValues("./combine/dijet/X" + particle +
                                   "Hah_M%d.txt")
        ExpAH = TGraphAsymmErrors()
        for i, m in enumerate(massAH):
            if not m in val: continue
            ExpAH.SetPoint(ExpAH.GetN(), m, valAH[m][3] * multF)
        ExpAH.SetLineWidth(2)
        ExpAH.SetLineColor(602)  #602
        ExpAH.SetLineStyle(4)
        ExpAH.Draw("SAME, L")

        massSL, valSL = fillValues("./combine/alpha/X" + particle +
                                   "Hsl_M%d.txt")
        ExpSL = TGraphAsymmErrors()
        for i, m in enumerate(massSL):
            if not m in val: continue
            ExpSL.SetPoint(ExpSL.GetN(), m, valSL[m][3] * multF)
        ExpSL.SetLineWidth(3)
        ExpSL.SetLineColor(860 - 9)  #602
        ExpSL.SetLineStyle(7)
        ExpSL.Draw("SAME, L")

        leg2.AddEntry(ExpAH, "B2G-17-002", "l")
        leg2.AddEntry(ExpSL, "B2G-17-004", "l")

    leg2.Draw()
    if not options.blind: Obs0s.Draw("SAME, L")
    c1.GetPad(0).Update()

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

    c1.Print("plotsLimit/Exclusion/" + channel + suffix + ".png")
    c1.Print("plotsLimit/Exclusion/" + channel + suffix + ".pdf")
    if 'ah' in channel or 'sl' in channel:
        c1.Print("plotsLimit/Exclusion/" + channel + suffix + ".C")
        c1.Print("plotsLimit/Exclusion/" + channel + suffix + ".root")

    for t in THEORY:
        print "Model", t, ":",
        for m in range(mass[0], mass[-1], 1):
            if not (Theory[t].Eval(m) > Obs0s.Eval(m)) == (
                    Theory[t].Eval(m + 1) > Obs0s.Eval(m + 1)):
                print m,
        print ""

    print "p1s[",
    for i in range(Exp0s.GetN()):
        print Exp0s.GetY()[i] + Exp1s.GetErrorYhigh(i), ",",
    print "],"
    print "m1s[",
    for i in range(Exp0s.GetN()):
        print Exp0s.GetY()[i] - Exp1s.GetErrorYlow(i), ",",
    print "],"
    print "[",
    for i in range(Exp0s.GetN()):
        print Exp0s.GetY()[i], ",",
    print "]"

    #if not 'ah' in channel and not 'sl' in channel: return

    # ---------- Significance ----------
    c2 = TCanvas("c2", "Significance", 800, 600)
    c2.cd()
    c2.GetPad(0).SetTopMargin(0.06)
    c2.GetPad(0).SetRightMargin(0.05)
    c2.GetPad(0).SetTicks(1, 1)
    c2.GetPad(0).SetGridx()
    c2.GetPad(0).SetGridy()
    Sign.GetYaxis().SetRangeUser(0., 5.)
    Sign.Draw("AL3")
    drawCMS(LUMI, YEAR, "Preliminary")
    drawAnalysis(channel[1:3])
    c2.Print("plotsLimit/Significance/" + channel + suffix + ".png")
    c2.Print("plotsLimit/Significance/" + channel + suffix + ".pdf")
    #    c2.Print("plotsLimit/Significance/"+channel+suffix+".root")
    #    c2.Print("plotsLimit/Significance/"+channel+suffix+".C")

    # ---------- p-Value ----------
    c3 = TCanvas("c3", "p-Value", 800, 600)
    c3.cd()
    c3.GetPad(0).SetTopMargin(0.06)
    c3.GetPad(0).SetRightMargin(0.05)
    c3.GetPad(0).SetTicks(1, 1)
    c3.GetPad(0).SetGridx()
    c3.GetPad(0).SetGridy()
    c3.GetPad(0).SetLogy()
    pVal.Draw("AL3")
    pVal.GetYaxis().SetRangeUser(2.e-7, 0.5)

    ci = [
        1., 0.317310508, 0.045500264, 0.002699796, 0.00006334, 0.000000573303,
        0.000000001973
    ]
    line = TLine()
    line.SetLineColor(922)
    line.SetLineStyle(7)
    text = TLatex()
    text.SetTextColor(922)
    text.SetTextSize(0.025)
    text.SetTextAlign(12)
    for i in range(1, len(ci) - 1):
        line.DrawLine(pVal.GetXaxis().GetXmin(), ci[i] / 2,
                      pVal.GetXaxis().GetXmax(), ci[i] / 2)
        text.DrawLatex(pVal.GetXaxis().GetXmax() * 1.01, ci[i] / 2,
                       "%d #sigma" % i)

    drawCMS(LUMI, YEAR, "Preliminary")
    drawAnalysis(channel[1:3])
    c3.Print("plotsLimit/pValue/" + channel + suffix + ".png")
    c3.Print("plotsLimit/pValue/" + channel + suffix + ".pdf")
    #    c3.Print("plotsLimit/pValue/"+channel+suffix+".root")
    #    c3.Print("plotsLimit/pValue/"+channel+suffix+".C")

    # --------- Best Fit ----------
    c4 = TCanvas("c4", "Best Fit", 800, 600)
    c4.cd()
    c4.GetPad(0).SetTopMargin(0.06)
    c4.GetPad(0).SetRightMargin(0.05)
    c4.GetPad(0).SetTicks(1, 1)
    c4.GetPad(0).SetGridx()
    c4.GetPad(0).SetGridy()
    Best.Draw("AL3")
    drawCMS(LUMI, YEAR, "Preliminary")
    drawAnalysis(channel[1:3])
    c4.Print("plotsLimit/BestFit/" + channel + suffix + ".png")
    c4.Print("plotsLimit/BestFit/" + channel + suffix + ".pdf")
    #    c4.Print("plotsLimit/BestFit/"+channel+suffix+".root")
    #    c4.Print("plotsLimit/BestFit/"+channel+suffix+".C")

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

    if 'ah' in channel:
        outFile = TFile("bands.root", "RECREATE")
        outFile.cd()
        pVal.Write("graph")
        Best.Write("best")
        outFile.Close()
Exemplo n.º 5
0
         ',', "_").replace(' ', "") + "_rebin1_" + u + "Down"
     print histname2
     histname3 = 'QCD#chi' + str(massbins[massbin]).strip("()").replace(
         ',', "_").replace(' ', "") + "_rebin1"
     print histname3
     up = fsys.Get(histname1)
     down = fsys.Get(histname2)
     central = fsys.Get(histname3)
     uncertainties += [[up, down, central]]
 h2new = h14.Clone("down" + str(massbins[massbin]))
 h3new = h14.Clone("up" + str(massbins[massbin]))
 chi2 = 0
 for b in range(h14.GetXaxis().GetNbins()):
     if b == 0:  # or b==h14G.GetXaxis().GetNbins()-1:
         print massbins[massbin], b, "stat", h14G.GetErrorYlow(
             b) / h14G.GetY()[b], h14G.GetErrorYhigh(b) / h14G.GetY()[b]
     exp_sumdown = 0
     exp_sumup = 0
     theory_sumdown = 0
     theory_sumup = 0
     for up, down, central in uncertainties:
         if b == 0:  # or b==h14G.GetXaxis().GetNbins()-1:
             print massbins[massbin], b, uncertaintynames[
                 uncertainties.index([up, down, central])], abs(
                     up.GetBinContent(b + 1) - central.GetBinContent(
                         b + 1)) / central.GetBinContent(b + 1), abs(
                             down.GetBinContent(b + 1) -
                             central.GetBinContent(b + 1)
                         ) / central.GetBinContent(b + 1)
         addup = pow(
             max(
Exemplo n.º 6
0
class HLTRate:
  _denominator = 0
  _crossSectionInPb = 0.0
  _nFiles = 0
  _iFile = 0
  _label = ""

  _denominatorHist = None
  _numeratorHist = None
  _rateGraph = None

  def addFile (self, fileName):
    if not fileName:
      return
    if not os.path.isfile (fileName):
      print "\"" + fileName + "\" does not exist or is not a file!"
      return
    if not re.match (r".*\/cmsRun_.*\.log\.tar\.gz", fileName):
      print "\"" + fileName + "\" does not look like a CRAB log file!"
      return

    print "  (" + str (self._iFile) + " / " + str (self._nFiles) + ") Processing file \"" + re.sub (r".*\/([^/]+)$", r"\1", fileName) + "\"..."

    n = re.sub (r".*\/cmsRun_(.*)\.log\.tar\.gz", r"\1", fileName)

    print "    Extracting log file..."

    tin = tarfile.open (fileName)
    fin = tin.extractfile ("cmsRun-stdout-" + n + ".log")

    print "    Parsing log file..."

    for line in fin:
      line = line.rstrip ()
      if not re.match (r"HLT-Report.*", line):
        continue
      if re.match (r".*Events total = .* wasrun = .* passed = .* errors = .*", line):
        self._denominator += int (re.sub (r".*Events total = (.*) wasrun = .* passed = .* errors = .*", r"\1", line))
      if re.match (r".*ETM[^_]*_MET75_IsoTrk50.*", line):
        seed = re.sub (r".*ETM([^_]*)_MET75_IsoTrk50.*", r"\1", line)
        if seed not in numerator[self._label]:
          numerator[self._label][seed] = 0
        numerator[self._label][seed] += int (re.sub (r"HLT-Report *[^ ]* *[^ ]* *[^ ]* *[^ ]* *([^ ]*) *[^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]*", r"\1", line))

    fin.close ()
    tin.close ()

  def addDir (self, dirName):
    if not dirName:
      return
    if not os.path.isdir (dirName):
      print "\"" + dirName + "\" does not exist or is not a directory!"
      return

    print "Processing directory \"" + dirName + "\"..."

    logFiles = glob.glob (dirName + "/cmsRun_*.log.tar.gz")
    self._nFiles = len (logFiles)
    self._iFile = 0
    for logFile in logFiles:
      self._iFile += 1
      self.addFile (logFile)

  def setCrossSectionInPb (self, crossSectionInPb):
    self._crossSectionInPb = crossSectionInPb

  def setLabel (self, label):
    self._label = label
    numerator[self._label] = {}

  def __init__ (self, label = "", dirName = "", crossSectionInPb = 0.0):
    self.setLabel (label)
    self.addDir (dirName)
    self.setCrossSectionInPb (crossSectionInPb)

  def getRateInPb (self, seed):
    if seed not in numerator[self._label]:
      print "ETM" + seed + " not found in results!"
      return

    self._denominatorHist = TH1D ("total", "", 1, -0.5, 0.5)
    self._numeratorHist = TH1D ("pass", "", 1, -0.5, 0.5)

    self._denominatorHist.SetBinContent (1, self._denominator)
    self._denominatorHist.SetBinError (1, math.sqrt (self._denominator))
    self._numeratorHist.SetBinContent (1, numerator[self._label][seed])
    self._numeratorHist.SetBinError (1, math.sqrt (numerator[self._label][seed]))

    self._rateGraph = TGraphAsymmErrors (self._numeratorHist, self._denominatorHist)
    x = Double (0.0)
    y = Double (0.0)
    self._rateGraph.GetPoint (0, x, y)
    eLow = self._rateGraph.GetErrorYlow (0)
    eHigh = self._rateGraph.GetErrorYhigh (0)

    y *= self._crossSectionInPb
    eLow *= self._crossSectionInPb
    eHigh *= self._crossSectionInPb

    return (y, eLow, eHigh)

  def getSeeds (self):
    return map (str, sorted (map (int, numerator[self._label].keys ())))
Exemplo n.º 7
0
def plotDataOverMCEff(hist_mc_tight, hist_mc_loose, hist_data_tight, hist_data_loose, plot_name='fakerate.pdf', mc_leg='MC', obs_leg='Observed', ratio_leg='Obs/MC'):

    g = TGraphAsymmErrors(hist_mc_tight)
    g.Divide(hist_mc_tight, hist_mc_loose)
    g.GetYaxis().SetTitle('Misidentification rate')
    g.GetXaxis().SetTitle(hist_mc_tight.GetXaxis().GetTitle())
    g.GetYaxis().SetTitleOffset(1.2)
    g.GetYaxis().SetTitleOffset(1.3)

    g.SetLineColor(2)
    g.SetMarkerColor(2)

    g_data = TGraphAsymmErrors(hist_data_tight)
    g_data.Divide(hist_data_tight, hist_data_loose)

    # if g_data.GetN() != hist_data_tight.GetNbinsX():
    #     import pdb; pdb.set_trace()

    g_data.GetYaxis().SetTitle('Misidentification rate')
    g_data.GetXaxis().SetTitle(hist_data_tight.GetXaxis().GetTitle())
    g_data.GetYaxis().SetTitleOffset(1.2)
    g_data.GetYaxis().SetTitleOffset(1.3)
    g_data.SetMarkerColor(1)

    g_vals = g.GetY()
    g_data_vals = g_data.GetY()

    g_ratio = g_data.Clone('ratio')

    for i in xrange(g_data.GetN()):
        ratio = g_data_vals[i]/g_vals[i] if g_vals[i] else 0.
        g_ratio.SetPoint(i, g.GetX()[i], ratio)

        rel_y_low = math.sqrt((g_data.GetErrorYlow(i)/g_data_vals[i])**2 + (g.GetErrorYlow(i)/g_vals[i])**2) if g_data_vals[i] > 0. and g_vals[i] > 0. else 0.

        g_ratio.SetPointEYlow(i, rel_y_low * ratio)

        rel_y_high = math.sqrt((g_data.GetErrorYhigh(i)/g_data_vals[i])**2 + (g.GetErrorYhigh(i)/g_vals[i])**2) if g_data_vals[i] > 0. and g_vals[i] > 0. else 0.

        g_ratio.SetPointEYhigh(i, rel_y_high * ratio)

    # Gymnastics to get same label sizes etc in ratio and main plot
    ytp_ratio = 2.
    xtp_ratio = 2.

    # hr.GetYaxis().SetNdivisions(4)

    g_ratio.GetYaxis().SetTitleSize(g.GetYaxis().GetTitleSize() * xtp_ratio)
    g_ratio.GetXaxis().SetTitleSize(g.GetXaxis().GetTitleSize() * ytp_ratio)

    g_ratio.GetYaxis().SetTitleOffset(g.GetYaxis().GetTitleOffset() / xtp_ratio)
    g_ratio.GetXaxis().SetTitleOffset(g.GetXaxis().GetTitleOffset())  # / ytp_ratio)

    g_ratio.GetYaxis().SetLabelSize(g.GetYaxis().GetLabelSize() * xtp_ratio)
    g_ratio.GetXaxis().SetLabelSize(g.GetXaxis().GetLabelSize() * ytp_ratio)

    g_data.GetXaxis().SetLabelColor(0)
    g_data.GetXaxis().SetLabelSize(0)
    g.GetXaxis().SetLabelColor(0)
    g.GetXaxis().SetLabelSize(0)

    g_ratio.GetXaxis().SetTitle(g.GetXaxis().GetTitle())

    maxy = 1.3 * max(g.GetMaximum(), g_data.GetMaximum(), 0.05)
    g.GetYaxis().SetRangeUser(0.0011, maxy)

    cv, pad, padr = HistDrawer.buildCanvas()

    pad.cd()

    g.Draw('AP')
    g_data.Draw('P')

    legend = TLegend(0.23, 0.73, 0.43, 0.91)
    legend.SetFillColor(0)
    legend.SetFillStyle(0)
    legend.SetLineColor(0)
    legend.SetLineWidth(0)

    legend.AddEntry(g.GetName(), mc_leg, 'lep')
    legend.AddEntry(g_data.GetName(), obs_leg, 'lep')

    legend.Draw()

    padr.cd()
    g_ratio.GetYaxis().SetRangeUser(0.01, 1.99)
    g_ratio.GetYaxis().SetTitle(ratio_leg)
    g_ratio.Draw('AP')

    drawRatioLines(g_ratio)

    cv.Print(plot_name)

    g.GetYaxis().SetRangeUser(0.0001, 1)
    pad.SetLogy(True)
    cv.Print(plot_name.replace('.', '_log.'))
    f = ROOT.TFile(plot_name.replace('.', '_log.').replace('.pdf', '.root'), 'RECREATE')
    g.Write()
    g_data.Write()
    cv.Write()
    f.Close()
Exemplo n.º 8
0
  def fillAcceptance(self):
    '''
    This is to calculate the acceptance of the sample
    '''
    if debug: print('Info in Sample.fillAcceptance()')
    #f = TFile.Open(self.infileName)
    #t = f.Get(self.treeName)
    #if not t:
    #  raise RuntimeError( 'ERROR: no tree in file %s' % self.infileName)
    chain = TChain(self.treeName)
    for fn in self.infileNames:
      chain.Add(fn)

    self.effnum  = ROOT.TH1F('effnum', 'effnum', 1, 0, 13000) 
    self.effden  = ROOT.TH1F('effden', 'effden', 1, 0, 13000)
    self.effnumB = dict([(b, ROOT.TH1F('effnum_%s' % b, 'effnum_%s' % b, 1, 0, 13000)) for b in self.Bspecies])
    self.effdenB = dict([(b, ROOT.TH1F('effden_%s' % b, 'effden_%s' % b, 1, 0, 13000)) for b in self.Bspecies])

    #if doInclusive:
    cutsnum = '(l0_pt>{mp} && abs(l0_eta)<1.5'.format(mp=muTrigPt)
    #else: 
    #cutsnum = '(l0_pt>{mp} && abs(l0_eta)<1.5 && k_pt>1 && abs(k_eta)<2.5 && pi_pt>1 && abs(pi_eta)<2.5'.format(mp=muTrigPt) 
    if doSkipDispl:
      cutsnum += ''
    else:
      cutsnum += '&& Lxy < 500'
    if not doDisplZ:
      pass
    else:
      cutsnum += '&& Lz < 20'
    if doSkipHNLptEta:
      cutsnum += ''
    else:
      cutsnum += '&& l1_pt>3 && abs(l1_eta)<2.5 && pi1_pt>0.8 && abs(pi1_eta)<2.5'
    
    cutsnum += ')'


    cutsden = '(l0_pt>{mp} && abs(l0_eta)<1.5)'.format(mp=muTrigPt)

    
    ###### fill the total acceptance
    chain.Draw('hnl_pt>>effnum', cutsnum+'*'+self.evt_w, 'goff')
    chain.Draw('hnl_pt>>effden', cutsden, 'goff')
 
    if TEfficiency.CheckConsistency(self.effnum,self.effden): 
      peff = TEfficiency(self.effnum,self.effden)
     
      # check usage of TGraphAsymmetricErrors
      self.acc = peff.GetEfficiency(1)
      self.acc_errup = peff.GetEfficiencyErrorUp(1)
      self.acc_errdn = peff.GetEfficiencyErrorLow(1)

    tgra = TGraphAsymmErrors()
    tgra.BayesDivide(self.effnum, self.effden)
    self.acc_tg = tgra.GetY()[0]
    self.acc_errup_tg = tgra.GetErrorYhigh(0)
    self.acc_errdn_tg = tgra.GetErrorYlow(0)

    # for debugging purposes
    self.num = self.effnum.GetEntries()
    if self.num==0: print('**** 0 entries for mass={}'.format(self.mass))
    self.den = self.effden.GetEntries()

    ###### fill the partial acceptances 
    for ib,b in enumerate(self.Bspecies):
      bsel   = '(abs(b_pdgid)=={bid})'.format(bid=self.BpdgIds[ib])
      selnum = '(' + cutsnum + '&&' + bsel + ')' 
      selden = '(' + cutsden + '&&' + bsel + ')'
      chain.Draw('hnl_pt>>effnum_{b}'.format(b=b), selnum + '*' + self.evt_w, 'goff')
      chain.Draw('hnl_pt>>effden_{b}'.format(b=b), selden, 'goff')

      if TEfficiency.CheckConsistency(self.effnumB[b],self.effdenB[b]):
        peff = TEfficiency(self.effnumB[b],self.effdenB[b])
        
        self.accB[b]       = peff.GetEfficiency(1)
        self.accB_errup[b] = peff.GetEfficiencyErrorUp(1)
        self.accB_errdn[b] = peff.GetEfficiencyErrorLow(1)

      else:
        self.accB[b] = 0
        self.accB_errup[b] = 0
        self.accB_errdn[b] = 0