def doCounters(datasetsEmb, datasetsSig, datasetName):
    lumi = datasetsEmb.getLuminosity()

    # Counters
    eventCounterEmb = tauEmbedding.EventCounterMany(datasetsEmb)
    eventCounterSig = counter.EventCounter(datasetsSig)

    def isNotThis(name):
        return name != datasetName

    eventCounterEmb.removeColumns(filter(isNotThis, datasetsEmb.getAllDatasetNames()))
    eventCounterSig.removeColumns(filter(isNotThis, datasetsSig.getAllDatasetNames()))
    eventCounterSig.normalizeMCToLuminosity(lumi)

    tdCount = dataset.TreeDraw("dummy", weight=tauEmbedding.signalNtuple.weightBTagging)
    tdCountMET = tdCount.clone(weight=tauEmbedding.signalNtuple.weight, selection=tauEmbedding.signalNtuple.metCut)
    tdCountBTagging = tdCount.clone(selection=And(tauEmbedding.signalNtuple.metCut, tauEmbedding.signalNtuple.bTaggingCut))
    tdCountDeltaPhi160 = tdCount.clone(selection=And(tauEmbedding.signalNtuple.metCut, tauEmbedding.signalNtuple.bTaggingCut, tauEmbedding.signalNtuple.deltaPhi160Cut))
    tdCountDeltaPhi130 = tdCount.clone(selection=And(tauEmbedding.signalNtuple.metCut, tauEmbedding.signalNtuple.bTaggingCut, tauEmbedding.signalNtuple.deltaPhi130Cut))
    def addRow(name, td):
        tdEmb = td.clone(tree=analysisEmb+"/tree")
        tdSig = td.clone(tree=analysisSig+"/tree")
        eventCounterEmb.mainCounterAppendRow(name, tdEmb)
        eventCounterSig.getMainCounter().appendRow(name, tdSig)

    addRow("JetsForEffs", tdCount.clone(weight=tauEmbedding.signalNtuple.weight))
    addRow("METForEffs", tdCountMET)
    addRow("BTagging (SF)", tdCountBTagging)
    addRow("DeltaPhi < 160", tdCountDeltaPhi160)
    addRow("BTagging (SF) again", tdCountBTagging)
    addRow("DeltaPhi < 130", tdCountDeltaPhi130)

    table = counter.CounterTable()
    col = eventCounterEmb.getMainCounterTable().getColumn(name=datasetName)
    table.appendColumn(counter.efficiencyColumn("Embedded eff", col))
    col = eventCounterSig.getMainCounterTable().getColumn(name=datasetName)
    table.appendColumn(counter.efficiencyColumn("Normal eff", col))

    table.keepOnlyRows(["btagging", "METForEffs", "BTagging (SF)", "DeltaPhi < 160", "DeltaPhi < 130"])
    row = table.getRow(name="btagging")
    table.removeRow(name="btagging")
    table.insertRow(1, row)
    table.renameRows({
            "METForEffs": "MET > 50",
            "btagging": "b tagging",
            "BTagging (SF)": "b tagging with SF (w.r.t MET)",
            "DeltaPhi < 130": "DeltaPhi < 130 (w.r.t. btagSF)"
            })

    effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f', withPrecision=2))

#    print table.format(effFormat)

    fname = "counters_selections_%s.txt"%datasetName
    f = open(fname, "w")
    f.write(table.format(effFormat))
    f.close()
    print "Printed selection counters to", fname
def doTauCounters(datasetsEmb,
                  datasetsSig,
                  datasetName,
                  ntupleCacheEmb,
                  ntupleCacheSig,
                  normalizeEmb=True):
    lumi = datasetsEmb.getLuminosity()

    # Take unweighted counters for embedded, to get a handle on the muon isolation efficiency
    eventCounterEmb = tauEmbedding.EventCounterMany(
        datasetsEmb,
        counters="/" + tauAnalysisEmb + "Counters",
        normalize=normalizeEmb)
    eventCounterSig = counter.EventCounter(datasetsSig,
                                           counters="/" + tauAnalysisEmb +
                                           "Counters")

    def isNotThis(name):
        return name != datasetName

    eventCounterEmb.removeColumns(
        filter(isNotThis, datasetsEmb.getAllDatasetNames()))
    eventCounterSig.removeColumns(
        filter(isNotThis, datasetsSig.getAllDatasetNames()))

    eventCounterEmb.mainCounterAppendRows(
        ntupleCacheEmb.histogram("counters/weighted/counter"))
    eventCounterSig.getMainCounter().appendRows(
        ntupleCacheSig.histogram("counters/weighted/counter"))

    eventCounterSig.normalizeMCToLuminosity(lumi)

    table = counter.CounterTable()
    col = eventCounterEmb.getMainCounterTable().getColumn(name=datasetName)
    col.setName("Embedded")
    table.appendColumn(col)
    col = eventCounterSig.getMainCounterTable().getColumn(name=datasetName)
    col.setName("Normal")
    table.appendColumn(col)

    lastCountEmb = table.getCount(colName="Embedded",
                                  irow=table.getNrows() - 1)
    lastCountNormal = table.getCount(colName="Normal",
                                     irow=table.getNrows() - 1)

    postfix = ""
    if not normalizeEmb:
        postfix = "_notEmbNormalized"

    effFormat = counter.TableFormatLaTeX(
        counter.CellFormatTeX(valueFormat="%.4f", withPrecision=2))
    countFormat = counter.TableFormatText(
        counter.CellFormatText(valueFormat="%.4f"),
        #columnSeparator="  ;"
    )

    fname = "counters_tau_" + datasetName + postfix + ".txt"
    f = open(fname, "w")
    f.write(table.format(countFormat))
    f.write("\n")

    try:
        ratio = lastCountNormal.clone()
        ratio.divide(lastCountEmb)
        f.write("Normal/embedded = %.4f +- %.4f\n\n" %
                (ratio.value(), ratio.uncertainty()))
    except ZeroDivisionError:
        pass

    f.close()
    print "Printed tau counters to", fname

    if not normalizeEmb:
        return

    tableEff = counter.CounterTable()
    tableEff.appendColumn(
        counter.efficiencyColumn("Embedded eff",
                                 table.getColumn(name="Embedded")))
    tableEff.appendColumn(
        counter.efficiencyColumn("Normal eff", table.getColumn(name="Normal")))

    embeddingMuonIsolationEff = tableEff.getCount(
        rowName="tauEmbeddingMuonsCount", colName="Embedded eff")
    embeddingTauIsolationEff = tableEff.getCount(rowName="Isolation",
                                                 colName="Embedded eff")
    embeddingTotalIsolationEff = embeddingMuonIsolationEff.clone()
    embeddingTotalIsolationEff.multiply(embeddingTauIsolationEff)

    # Remove unnecessary rows
    rowNames = [
        #        "All events",
        "Decay mode finding",
        "Eta cut",
        "Pt cut",
        "Leading track pt",
        "Against electron",
        "Against muon",
        "Isolation",
        "One prong",
        "Rtau",
    ]
    tableEff.keepOnlyRows(rowNames)
    rowIndex = tableEff.getRowNames().index("Isolation")
    tableEff.insertRow(
        rowIndex,
        counter.CounterRow("Mu isolation (emb)",
                           ["Embedded eff", "Normal eff"],
                           [embeddingMuonIsolationEff, None]))
    tableEff.insertRow(
        rowIndex + 1,
        counter.CounterRow("Tau isolation (emb)",
                           ["Embedded eff", "Normal eff"],
                           [embeddingTauIsolationEff, None]))
    tableEff.setCount2(embeddingTotalIsolationEff,
                       rowName="Isolation",
                       colName="Embedded eff")
    #tableEff.setCount2(None, rowName="pT > 15", colName="Normal eff")

    #print table.format(effFormat)
    fname = "counters_tau_" + datasetName + "_eff.txt"
    f = open(fname, "w")
    f.write(tableEff.format(effFormat))
    f.write("\n")
    f.close()
    print "Printed tau efficiencies to", fname
def doCounters(datasetsEmb2, datasetsSig2, datasetName):
    lumi = datasetsEmb2.getDataset("Data").getLuminosity()

    datasetsEmb = datasetsEmb2.deepCopy()
    datasetsSig = datasetsSig2.deepCopy()

    datasetsEmb.remove(
        filter(lambda name: name != datasetName,
               datasetsEmb.getAllDatasetNames()))
    datasetsSig.remove(
        filter(lambda name: name != datasetName,
               datasetsSig.getAllDatasetNames()))
    # Counters
    eventCounterEmb = counter.EventCounter(datasetsEmb,
                                           counters=analysisEmb + "Counters")
    eventCounterSig = counter.EventCounter(datasetsSig,
                                           counters=analysisSig + "Counters")
    eventCounterEmb.normalizeMCToLuminosity(lumi)
    eventCounterSig.normalizeMCToLuminosity(lumi)

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.4f'))

    counterEmb = eventCounterEmb.getMainCounter()
    counterSig = eventCounterSig.getMainCounter()
    treeDraw = dataset.TreeDraw("dummy")
    tdEmb = treeDraw.clone(tree=analysisEmb + "/tree")
    tdSig = treeDraw.clone(tree=analysisSig + "/tree")
    selectionsCumulative = []
    tauSelectionsCumulative = []

    def sel(name, selection):
        selectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        counterEmb.appendRow(name, tdEmb.clone(selection=sel))
        counterSig.appendRow(name, tdSig.clone(selection=sel))

    def tauSel(name, selection):
        tauSelectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        counterEmb.appendRow(name, tdEmb.clone(selection=sel))
        counterSig.appendRow(name, tdSig.clone(selection=sel))


#    sel("Primary vertex", tauPlot.pvSelection)

    sel(">= 1 tau candidate", "Length$(taus_p4) >= 1")
    tauSel("Decay mode finding", tauPlot.decayModeFinding)
    tauSel("pT > 15", "(taus_p4.Pt() > 15)")
    tauSel("pT > 40", tauPlot.tauPtCut)
    tauSel("eta < 2.1", tauPlot.tauEtaCut)
    tauSel("leading track pT > 20", tauPlot.tauLeadPt)
    tauSel("ECAL fiducial", tauPlot.ecalFiducial)
    tauSel("againstElectron", tauPlot.electronRejection)
    tauSel("againstMuon", tauPlot.muonRejection)
    tauSel("isolation", tauPlot.tightIsolation)
    tauSel("oneProng", tauPlot.oneProng)
    tauSel("Rtau", tauPlot.rtau)
    sel("3 jets", tauPlot.jetEventSelection)
    sel("MET", tauPlot.metSelection)
    sel("btag", tauPlot.btagEventSelection)

    table = counter.CounterTable()
    col = counterEmb.getTable().getColumn(name=datasetName)
    col.setName("Embedded")
    table.appendColumn(col)
    col = counterSig.getTable().getColumn(name=datasetName)
    col.setName("Normal")
    table.appendColumn(col)

    col = table.getColumn(name="Embedded")
    table.insertColumn(1, counter.efficiencyColumn(col.getName() + " eff",
                                                   col))
    col = table.getColumn(name="Normal")
    table.appendColumn(counter.efficiencyColumn(col.getName() + " eff", col))

    print "%s counters" % datasetName
    print table.format(effFormat)
Example #4
0
def doPlots(datasetsEmb, datasetsSig, datasetName):
    lumi = datasetsEmb.getDataset("Data").getLuminosity()

    plots._legendLabels[datasetName+"_Embedded"] = "Embedded "+plots._legendLabels[datasetName]
    plots._legendLabels[datasetName+"_Normal"]   = "Normal "+plots._legendLabels[datasetName]

    def createPlot(name):
        name2Emb = name
        name2Sig = name
        if isinstance(name, basestring):
            name2Emb = analysisEmb+"/"+name
            name2Sig = analysisSig+"/"+name
        else:
            name2Emb = name.clone(tree=analysisEmb+"/tree")
            name2Sig = name.clone(tree=analysisSig+"/tree")
        emb = datasetsEmb.getDataset(datasetName).getDatasetRootHisto(name2Emb)
        emb.setName("Embedded")
        sig = datasetsSig.getDataset(datasetName).getDatasetRootHisto(name2Sig)
        sig.setName("Normal")
        p = plots.ComparisonPlot(emb, sig)
        p.histoMgr.normalizeMCToLuminosity(lumi)
        p.histoMgr.setHistoLegendLabelMany({
                "Embedded": "Embedded "+plots._legendLabels[datasetName],
                "Normal":   "Normal "+plots._legendLabels[datasetName],
                })
        p.histoMgr.forEachHisto(styles.generator())

        return p

    opts2 = {"ymin": 0, "ymax": 2}
    def drawControlPlot(path, xlabel, **kwargs):
        drawPlot(createPlot("ControlPlots/"+path), "mcembsig_"+datasetName+"_"+path, xlabel, opts2=opts2, **kwargs)

    # Control plots
    drawControlPlot("SelectedTau_pT_AfterStandardSelections", "#tau-jet p_{T} (GeV/c)", opts={"xmax": 250}, rebin=2, cutBox={"cutValue": 40, "greaterThan": 40})
    drawControlPlot("SelectedTau_eta_AfterStandardSelections", "#tau-jet #eta", opts={"xmin": -2.2, "xmax": 2.2}, ylabel="Events / %.1f", rebin=4, log=False, moveLegend={"dy":-0.6, "dx":-0.2})
    drawControlPlot("SelectedTau_phi_AfterStandardSelections", "#tau-jet #phi", rebin=10, ylabel="Events / %.2f", log=False)
    drawControlPlot("SelectedTau_LeadingTrackPt_AfterStandardSelections", "#tau-jet ldg. charged particle p_{T} (GeV/c)", opts={"xmax": 300}, rebin=2, cutBox={"cutValue": 20, "greaterThan": True})
    drawControlPlot("SelectedTau_Rtau_AfterStandardSelections", "R_{#tau} = p^{ldg. charged particle}/p^{#tau jet}", opts={"xmin": 0.65, "xmax": 1.05, "ymin": 1e-1, "ymaxfactor": 5}, rebin=5, ylabel="Events / %.2f", moveLegend={"dx":-0.3}, cutBox={"cutValue":0.7, "greaterThan":True})
    drawControlPlot("SelectedTau_p_AfterStandardSelections", "#tau-jet p (GeV/c)", rebin=2)
    drawControlPlot("SelectedTau_LeadingTrackP_AfterStandardSelections", "#tau-jet ldg. charged particle p (GeV/c)", rebin=2)
    #drawControlPlot("IdentifiedElectronPt_AfterStandardSelections", "Electron p_{T} (GeV/c)")
    #drawControlPlot("IdentifiedMuonPt_AfterStandardSelections", "Muon p_{T} (GeV/c)")
    drawControlPlot("Njets_AfterStandardSelections", "Number of jets", ylabel="Events")
    drawControlPlot("MET", "Uncorredted PF E_{T}^{miss} (GeV)", rebin=5, opts={"xmax": 400}, cutLine=50)
    drawControlPlot("NBjets", "Number of selected b jets", opts={"xmax": 6}, ylabel="Events", moveLegend={"dx":-0.3, "dy":-0.5}, cutLine=1)

    treeDraw = dataset.TreeDraw("dummy", weight="weightPileup")

    tdDeltaPhi = treeDraw.clone(varexp="acos( (tau_p4.Px()*met_p4.Px()+tau_p4.Py()*met_p4.Py())/(tau_p4.Pt()*met_p4.Et()) )*57.3 >>tmp(18, 0, 180)")
    tdMt = treeDraw.clone(varexp="sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) >>tmp(20,0,200)")

    # DeltaPhi
    xlabel = "#Delta#phi(#tau, MET) (^{#circ})"
    def customDeltaPhi(h):
        yaxis = h.getFrame().GetYaxis()
        yaxis.SetTitleOffset(0.8*yaxis.GetTitleOffset())
    drawPlot(createPlot(tdDeltaPhi.clone()), "mcembsig_"+datasetName+"_deltaPhi_1AfterTauID", xlabel, log=False, opts2=opts2, ylabel="Events / %.0f^{#circ}", function=customDeltaPhi, moveLegend={"dx":-0.22}, cutLine=[130, 160])

    # mT
    xlabel = "m_{T} (#tau jet, E_{T}^{miss}) (GeV/c^{2})"
    drawPlot(createPlot(tdMt.clone()), "mcembsig_"+datasetName+"_transverseMass_1AfterTauID", xlabel, opts2=opts2, ylabel="Events / %.0f GeV/c^{2}")


    # After all cuts
    metCut = "(met_p4.Et() > 50)"
    bTaggingCut = "passedBTagging"
    selection = "&&".join([metCut, bTaggingCut])
    drawPlot(createPlot(treeDraw.clone(varexp="tau_p4.Pt() >>tmp(20,0,200)", selection=selection)), "mcembsig_"+datasetName+"_selectedTauPt_3AfterBTagging", "#tau-jet p_{T} (GeV/c)", opts2={"ymin": 0, "ymax": 2})
    drawPlot(createPlot(treeDraw.clone(varexp="met_p4.Pt() >>tmp(16,0,400)", selection=selection)), "mcembsig_"+datasetName+"_MET_3AfterBTagging", "E_{T}^{miss} (GeV)", ylabel="Events / %.0f GeV", opts2={"ymin": 0, "ymax": 2})
    drawPlot(createPlot(tdMt.clone(selection=selection)), "mcembsig_"+datasetName+"_transverseMass_3AfterBTagging", xlabel, opts2={"ymin": 0, "ymax": 2}, ylabel="Events / %.0f GeV/c^{2}")
                        


    eventCounterEmb = counter.EventCounter(datasetsEmb, counters=analysisEmb+"Counters")
    eventCounterSig = counter.EventCounter(datasetsSig, counters=analysisSig+"Counters")
    eventCounterEmb.normalizeMCToLuminosity(lumi)
    eventCounterSig.normalizeMCToLuminosity(lumi)

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f'))

    for function, cname in [
        (lambda c: c.getMainCounterTable(), "Main"),
        (lambda c: c.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight"), "Tau")
        ]:
        tableEmb = function(eventCounterEmb)
        tableSig = function(eventCounterSig)

        table = counter.CounterTable()
        col = tableEmb.getColumn(name=datasetName)
        col.setName("Embedded")
        table.appendColumn(col)
        col = tableSig.getColumn(name=datasetName)
        col.setName("Normal")
        table.appendColumn(col)

        print "%s counters" % cname
        print table.format()

        if cname == "Main":
            #map(lambda t: t.keepOnlyRows([
            table.keepOnlyRows([
                        "All events",
                        "Trigger and HLT_MET cut",
                        "taus == 1",
                        #"trigger scale factor",
                        "electron veto",
                        "muon veto",
                        "MET",
                        "njets",
                        "btagging",
                        "btagging scale factor",
                        "JetsForEffs",
                        "METForEffs",
                        "BTagging",
                        "DeltaPhi < 160",
                        "DeltaPhi < 130"
                        ])#, [tableEmb, tableSig])
        else:
            #map(lambda t: t.keepOnlyRows([
            table.keepOnlyRows([
                        "AllTauCandidates",
                        "DecayModeFinding",
                        "TauJetPt",
                        "TauJetEta",
                        #"TauLdgTrackExists",
                        "TauLdgTrackPtCut",
                        "TauECALFiducialCutsCracksAndGap",
                        "TauAgainstElectronCut",
                        "TauAgainstMuonCut",
                        #"EMFractionCut",
                        "HPS",
                        "TauOneProngCut",
                        "TauRtauCut",
                        ])#, [tableEmb, tableSig])

        col = table.getColumn(name="Embedded")
        table.insertColumn(1, counter.efficiencyColumn(col.getName()+" eff", col))
        col = table.getColumn(name="Normal")
        table.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))

        print "%s counters" % cname
        print table.format(effFormat)
def doCounters(datasetsEmb, datasetsSig, datasetName):
    lumi = datasetsEmb.getLuminosity()

    # Counters
    eventCounterEmb = tauEmbedding.EventCounterMany(datasetsEmb, counters=analysisEmb+"Counters/weighted")
    eventCounterSig = counter.EventCounter(datasetsSig, counters=analysisSig+"Counters/weighted")

    def isNotThis(name):
        return name != datasetName


    #eventCounterSig.normalizeMCToLuminosity(lumi)
    #print eventCounterSig.getMainCounterTable().format()
    #return

    eventCounterEmb.removeColumns(filter(isNotThis, datasetsEmb.getAllDatasetNames()))
    eventCounterSig.removeColumns(filter(isNotThis, datasetsSig.getAllDatasetNames()))
    eventCounterSig.normalizeMCToLuminosity(lumi)

    tdCount = dataset.TreeDraw("dummy", weight=weightBTagging)
    tdCountMET = tdCount.clone(weight=weight, selection=metCut)
    tdCountBTagging = tdCount.clone(selection=And(metCut, bTaggingCut))
    tdCountDeltaPhi160 = tdCount.clone(selection=And(metCut, bTaggingCut, deltaPhi160Cut))
    tdCountDeltaPhi130 = tdCount.clone(selection=And(metCut, bTaggingCut, deltaPhi130Cut))
    def addRow(name, td):
        tdEmb = td.clone(tree=analysisEmb+"/tree")
        tdSig = td.clone(tree=analysisSig+"/tree")
        eventCounterEmb.mainCounterAppendRow(name, tdEmb)
        eventCounterSig.getMainCounter().appendRow(name, tdSig)

    addRow("JetsForEffs", tdCount.clone(weight=weight))
    addRow("METForEffs", tdCountMET)
    addRow("BTagging (SF)", tdCountBTagging)
    addRow("DeltaPhi < 160", tdCountDeltaPhi160)
    addRow("BTagging (SF) again", tdCountBTagging)
    addRow("DeltaPhi < 130", tdCountDeltaPhi130)

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f'))

    f = open("counters_%s.txt"%datasetName, "w")

    for function, cname in [
        (lambda c: c.getMainCounterTable(), "Main"),
        (lambda c: c.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight"), "Tau")
        ]:
        tableEmb = function(eventCounterEmb)
        tableSig = function(eventCounterSig)

        table = counter.CounterTable()
        col = tableEmb.getColumn(name=datasetName)
        col.setName("Embedded")
        table.appendColumn(col)
        col = tableSig.getColumn(name=datasetName)
        col.setName("Normal")
        table.appendColumn(col)

        f.write("%s counters\n" % cname)
        f.write(table.format())
        f.write("\n")

        if cname == "Main":
            #map(lambda t: t.keepOnlyRows([
            table.keepOnlyRows([
                        "All events",
                        "Trigger and HLT_MET cut",
                        "taus == 1",
                        #"trigger scale factor",
                        "electron veto",
                        "muon veto",
                        "MET",
                        "njets",
                        "btagging",
                        "btagging scale factor",
                        "JetsForEffs",
                        "METForEffs",
                        "BTagging (SF)",
                        "DeltaPhi < 160",
                        "BTagging (SF) again",
                        "DeltaPhi < 130"
                        ])#, [tableEmb, tableSig])
        else:
            #map(lambda t: t.keepOnlyRows([
            table.keepOnlyRows([
                        "AllTauCandidates",
                        "DecayModeFinding",
                        "TauJetPt",
                        "TauJetEta",
                        #"TauLdgTrackExists",
                        "TauLdgTrackPtCut",
                        "TauECALFiducialCutsCracksAndGap",
                        "TauAgainstElectronCut",
                        "TauAgainstMuonCut",
                        #"EMFractionCut",
                        "HPS",
                        "TauOneProngCut",
                        "TauRtauCut",
                        ])#, [tableEmb, tableSig])

        col = table.getColumn(name="Embedded")
        table.insertColumn(1, counter.efficiencyColumn(col.getName()+" eff", col))
        col = table.getColumn(name="Normal")
        table.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))

        f.write("%s counters\n" % cname)
        f.write(table.format(effFormat))
        f.write("\n\n")
    f.close()
def doCounters(datasetsEmb):
    isCorrected = isinstance(datasetsEmb, result.DatasetsDYCorrection)
    if isCorrected:
        eventCounter = result.EventCounterDYCorrection(datasetsEmb, counters=analysisEmb+counters)
    else:
        scaleNormalization = analysisEmb != "signalAnalysis"
        eventCounter = result.EventCounterMany(datasetsEmb, counters=analysisEmb+counters, scaleNormalization=scaleNormalization)

    # Add counts
    sels = []
    tdCount = treeDraw.clone(weight=weightBTagging)
    tdCountMET = tdCount.clone(weight=weight, selection="&&".join(sels+[metCut]))
    tdCountBTagging = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut]))
    tdCountDeltaPhi160 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi160Cut]))
    tdCountDeltaPhi130 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi130Cut]))
    tdCountDeltaPhi90 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi90Cut]))
    eventCounter.mainCounterAppendRow("JetsForEffs", tdCount.clone(weight=weight, selection="&&".join(sels)))
    eventCounter.mainCounterAppendRow("METForEffs", tdCountMET)
    eventCounter.mainCounterAppendRow("BTagging", tdCountBTagging)
    eventCounter.mainCounterAppendRow("DeltaPhi < 160", tdCountDeltaPhi160)
    eventCounter.mainCounterAppendRow("DeltaPhi < 130", tdCountDeltaPhi130)
    eventCounter.mainCounterAppendRow("DeltaPhi < 90", tdCountDeltaPhi90)

    if not isCorrected:
        td1 = tdCount.clone(selection=metCut+"&&"+bTaggingCut+"&& (tecalometNoHF_p4.Pt() > 60)")
        td2 = tdCount.clone(selection=metCut+"&&"+bTaggingCut+"&& (tecalomet_p4.Pt() > 60)")
        td3 = dataset.TreeDrawCompound(td1, {
                "SingleMu_Mu_170722-172619_Aug05": td2,
                "SingleMu_Mu_172620-173198_Prompt": td2,
                "SingleMu_Mu_173236-173692_Prompt": td2,
                })
        eventCounter.mainCounterAppendRow("BTagging+CaloMetNoHF", td1)
        eventCounter.mainCounterAppendRow("BTagging+CaloMet", td2)
        eventCounter.mainCounterAppendRow("BTagging+CaloMet(NoHF)", td3)

    #(mainTable, mainTableChi2) = eventCounter.getMainCounterTableFit()
    mainTable = eventCounter.getMainCounterTable()

    ewkDatasets = ["WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson"]
    allDatasets = None
    if "QCD_Pt20_MuEnriched" in datasetsEmb.getAllDatasetNames():
        allDatasets = ["QCD_Pt20_MuEnriched"]+ewkDatasets
    def ewkSum(table):
        table.insertColumn(1, counter.sumColumn("EWKMCsum", [table.getColumn(name=name) for name in ewkDatasets]))
        if allDatasets != None:
            table.insertColumn(2, counter.sumColumn("MCSum", [table.getColumn(name=name) for name in allDatasets]))
    ewkSum(mainTable)
    cellFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.3f'))
    #print mainTableChi2.format(cellFormat)
    print mainTable.format(cellFormat)

    tauTable = eventCounter.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight")
    ewkSum(tauTable)
    print tauTable.format(cellFormat)

    # Efficiencies
    mainTable.keepOnlyRows([
            "All events",
            "Trigger and HLT_MET cut",
            "taus == 1",
#            "trigger scale factor",
            "electron veto",
            "muon veto",
            "MET",
            "njets",
            "btagging",
            "btagging scale factor",
            "JetsForEffs",
            "METForEffs",
            "BTagging",
            "DeltaPhi < 160",
            "DeltaPhi < 130"
            ])
    tauTable.keepOnlyRows([
            "AllTauCandidates",
            "DecayModeFinding",
            "TauJetPt",
            "TauJetEta",
            "TauLdgTrackExists",
            "TauLdgTrackPtCut",
            "TauECALFiducialCutsCracksAndGap",
            "TauAgainstElectronCut",
            "TauAgainstMuonCut",
            #"EMFractionCut",
            "HPS",
            "TauOneProngCut",
            "TauRtauCut",
            ])

    effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    for name, table in [("Main", mainTable), ("Tau ID", tauTable)]:
        effTable = counter.CounterTable()
        col = table.getColumn(name="Data")
        effTable.appendColumn(col)
        effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))
        col = table.getColumn(name="EWKMCsum")
        effTable.appendColumn(col)
        effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))
        print "%s counter efficiencies" % name
        print effTable.format(effFormat)


    if isCorrected:
        return

    print "Trigger uncertainties"
    bins = [40, 50, 60, 80]
    tauPtPrototype = ROOT.TH1F("tauPtTrigger", "Tau pt", len(bins)-1, array.array("d", bins))
    runs = [
        "(160431 <= run && run <= 167913)",
        "(170722 <= run && run <= 173198)",
        "(173236 <= run && run <= 173692)",
        #"(160431 <= run && run <= 173692)",
        ]
    for name, td in [
        ("BTagging", tdCountBTagging),
        ("DeltaPhi160", tdCountDeltaPhi160),
        ("DeltaPhi130", tdCountDeltaPhi130),
        ("DeltaPhi90", tdCountDeltaPhi90)
        ]:
        t = td.clone(varexp="tau_p4.Pt() >>tauPtTrigger")
        
        NallSum = 0
        NSum = 0
        absUncSquareSum = 0

        for runRegion in runs:
            #neventsPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTrigger")))
            #uncertaintyPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTriggerAbsUnc*weightTriggerAbsUnc/(weightTrigger*weightTrigger)")))
            tmp = t.clone(selection=t.selection+"&&"+runRegion)
            (th1all, gr) = datasetsEmb.getHistogram("Data", tmp.clone(weight="")) # Nall
            (th1, gr) = datasetsEmb.getHistogram("Data", tmp.clone(weight="weightTrigger")) # Nevents
            (th12, gr) = datasetsEmb.getHistogram("Data", tmp.clone(weight="weightTriggerAbsUnc")) # uncertainty

            Nall = th1all.Integral(0, th1all.GetNbinsX()+1)
            N = th1.Integral(0, th1.GetNbinsX()+1)
            #absSum2 = th12.Integral(0, th12.GetNbinsX()+1)
            #absUnc = math.sqrt(absSum2)
            #absUnc = th12.Integral(0, 2)
            NallSum += Nall
            NSum += N
            absUnc = tauEmbeddingPlot.squareSum(th12)
            absUncSquareSum += absUnc
            absUnc = math.sqrt(absUnc)
            relUnc = 0
            if N > 0:
                relUnc = absUnc/N

            print "%-15s for runs %s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (name, runRegion, Nall, N, absUnc, relUnc)


        absUnc = math.sqrt(absUncSquareSum)
        relUnc = absUnc/NSum

        print "%-15s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (name, NallSum, NSum, absUnc, relUnc)
        print
Example #7
0
def doCounters(datasetsEmb, datasetsSig, datasetName):
    lumi = datasetsEmb.getLuminosity()

    # Counters
    eventCounterEmb = tauEmbedding.EventCounterMany(datasetsEmb)
    eventCounterSig = counter.EventCounter(datasetsSig)

    def isNotThis(name):
        return name != datasetName

    eventCounterEmb.removeColumns(
        filter(isNotThis, datasetsEmb.getAllDatasetNames()))
    eventCounterSig.removeColumns(
        filter(isNotThis, datasetsSig.getAllDatasetNames()))
    eventCounterSig.normalizeMCToLuminosity(lumi)

    tdCount = dataset.TreeDraw("dummy",
                               weight=tauEmbedding.signalNtuple.weightBTagging)
    tdCountMET = tdCount.clone(weight=tauEmbedding.signalNtuple.weight,
                               selection=tauEmbedding.signalNtuple.metCut)
    tdCountBTagging = tdCount.clone(
        selection=And(tauEmbedding.signalNtuple.metCut,
                      tauEmbedding.signalNtuple.bTaggingCut))
    tdCountDeltaPhi160 = tdCount.clone(selection=And(
        tauEmbedding.signalNtuple.metCut, tauEmbedding.signalNtuple.
        bTaggingCut, tauEmbedding.signalNtuple.deltaPhi160Cut))
    tdCountDeltaPhi130 = tdCount.clone(selection=And(
        tauEmbedding.signalNtuple.metCut, tauEmbedding.signalNtuple.
        bTaggingCut, tauEmbedding.signalNtuple.deltaPhi130Cut))

    def addRow(name, td):
        tdEmb = td.clone(tree=analysisEmb + "/tree")
        tdSig = td.clone(tree=analysisSig + "/tree")
        eventCounterEmb.mainCounterAppendRow(name, tdEmb)
        eventCounterSig.getMainCounter().appendRow(name, tdSig)

    addRow("JetsForEffs",
           tdCount.clone(weight=tauEmbedding.signalNtuple.weight))
    addRow("METForEffs", tdCountMET)
    addRow("BTagging (SF)", tdCountBTagging)
    addRow("DeltaPhi < 160", tdCountDeltaPhi160)
    addRow("BTagging (SF) again", tdCountBTagging)
    addRow("DeltaPhi < 130", tdCountDeltaPhi130)

    table = counter.CounterTable()
    col = eventCounterEmb.getMainCounterTable().getColumn(name=datasetName)
    table.appendColumn(counter.efficiencyColumn("Embedded eff", col))
    col = eventCounterSig.getMainCounterTable().getColumn(name=datasetName)
    table.appendColumn(counter.efficiencyColumn("Normal eff", col))

    table.keepOnlyRows([
        "btagging", "METForEffs", "BTagging (SF)", "DeltaPhi < 160",
        "DeltaPhi < 130"
    ])
    row = table.getRow(name="btagging")
    table.removeRow(name="btagging")
    table.insertRow(1, row)
    table.renameRows({
        "METForEffs": "MET > 50",
        "btagging": "b tagging",
        "BTagging (SF)": "b tagging with SF (w.r.t MET)",
        "DeltaPhi < 130": "DeltaPhi < 130 (w.r.t. btagSF)"
    })

    effFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.4f', withPrecision=2))

    #    print table.format(effFormat)

    fname = "counters_selections_%s.txt" % datasetName
    f = open(fname, "w")
    f.write(table.format(effFormat))
    f.close()
    print "Printed selection counters to", fname
def doCounters(datasetsEmb, datasetsSig, datasetName):
    lumi = datasetsEmb.getLuminosity()

    # Counters
    eventCounterEmb = result.EventCounterMany(datasetsEmb,
                                              counters=analysisEmb +
                                              "Counters/weighted")
    eventCounterSig = counter.EventCounter(datasetsSig,
                                           counters=analysisSig +
                                           "Counters/weighted")

    def isNotThis(name):
        return name != datasetName

    eventCounterEmb.removeColumns(
        filter(isNotThis, datasetsEmb.getAllDatasetNames()))
    eventCounterSig.removeColumns(
        filter(isNotThis, datasetsSig.getAllDatasetNames()))
    eventCounterSig.normalizeMCToLuminosity(lumi)

    tdCount = dataset.TreeDraw("dummy", weight=weightBTagging)
    tdCountMET = tdCount.clone(weight=weight, selection=metCut)
    tdCountBTagging = tdCount.clone(selection=And(metCut, bTaggingCut))
    tdCountDeltaPhi160 = tdCount.clone(
        selection=And(metCut, bTaggingCut, deltaPhi160Cut))
    tdCountDeltaPhi130 = tdCount.clone(
        selection=And(metCut, bTaggingCut, deltaPhi130Cut))

    def addRow(name, td):
        tdEmb = td.clone(tree=analysisEmb + "/tree")
        tdSig = td.clone(tree=analysisSig + "/tree")
        eventCounterEmb.mainCounterAppendRow(name, tdEmb)
        eventCounterSig.getMainCounter().appendRow(name, tdSig)

    addRow("JetsForEffs", tdCount.clone(weight=weight))
    addRow("METForEffs", tdCountMET)
    addRow("BTagging (SF)", tdCountBTagging)
    addRow("DeltaPhi < 160", tdCountDeltaPhi160)
    addRow("BTagging (SF) again", tdCountBTagging)
    addRow("DeltaPhi < 130", tdCountDeltaPhi130)

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.4f'))

    f = open("counters_%s.txt" % datasetName, "w")

    for function, cname in [
        (lambda c: c.getMainCounterTable(), "Main"),
        (lambda c: c.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight"),
         "Tau")
    ]:
        tableEmb = function(eventCounterEmb)
        tableSig = function(eventCounterSig)

        table = counter.CounterTable()
        col = tableEmb.getColumn(name=datasetName)
        col.setName("Embedded")
        table.appendColumn(col)
        col = tableSig.getColumn(name=datasetName)
        col.setName("Normal")
        table.appendColumn(col)

        f.write("%s counters\n" % cname)
        f.write(table.format())
        f.write("\n")

        if cname == "Main":
            #map(lambda t: t.keepOnlyRows([
            table.keepOnlyRows([
                "All events",
                "Trigger and HLT_MET cut",
                "taus == 1",
                #"trigger scale factor",
                "electron veto",
                "muon veto",
                "MET",
                "njets",
                "btagging",
                "btagging scale factor",
                "JetsForEffs",
                "METForEffs",
                "BTagging (SF)",
                "DeltaPhi < 160",
                "BTagging (SF) again",
                "DeltaPhi < 130"
            ])  #, [tableEmb, tableSig])
        else:
            #map(lambda t: t.keepOnlyRows([
            table.keepOnlyRows([
                "AllTauCandidates",
                "DecayModeFinding",
                "TauJetPt",
                "TauJetEta",
                #"TauLdgTrackExists",
                "TauLdgTrackPtCut",
                "TauECALFiducialCutsCracksAndGap",
                "TauAgainstElectronCut",
                "TauAgainstMuonCut",
                #"EMFractionCut",
                "HPS",
                "TauOneProngCut",
                "TauRtauCut",
            ])  #, [tableEmb, tableSig])

        col = table.getColumn(name="Embedded")
        table.insertColumn(
            1, counter.efficiencyColumn(col.getName() + " eff", col))
        col = table.getColumn(name="Normal")
        table.appendColumn(
            counter.efficiencyColumn(col.getName() + " eff", col))

        f.write("%s counters\n" % cname)
        f.write(table.format(effFormat))
        f.write("\n\n")
    f.close()
def main():
    datasets = dataset.getDatasetsFromMulticrabCfg(counters=baseline+"Counters")

    datasets.remove([
#            "SingleMu_Mu_160431-163261_May10",
#            "SingleMu_Mu_163270-163869_May10",
#            "SingleMu_Mu_165088-166150_Prompt",
#            "SingleMu_Mu_166161-166164_Prompt",
#            "SingleMu_Mu_166346-166346_Prompt",
#            "SingleMu_Mu_166374-167043_Prompt",
#            "SingleMu_Mu_167078-167913_Prompt",

#            "SingleMu_Mu_170722-172619_Aug05",
#            "SingleMu_Mu_172620-173198_Prompt",
#            "SingleMu_Mu_173236-173692_Prompt",
        ])
    datasets.loadLuminosities()
    plots.mergeRenameReorderForDataMC(datasets)

    values = {}

    analyses = [
        ("Baseline", baseline),
        ("Plus", plus),
        ("Minus", minus)
        ]

    mainTable = counter.CounterTable()
    tauTable = counter.CounterTable()
    cntr = "Counters"
    if normalize:
        cntr += "/weighted"
    for name, analysis in analyses:
        eventCounter = counter.EventCounter(datasets, counters=analysis+cntr)

        eventCounter.normalizeMCByLuminosity()
        if normalize:
            tauEmbedding.scaleNormalization(eventCounter)
        col = eventCounter.getMainCounterTable().getColumn(name="Data")
        col.setName(name)
        mainTable.appendColumn(col)
        effcol = counter.efficiencyColumn(col.getName()+" eff", col)
        mainTable.appendColumn(effcol)
        values[name+"Main"] = effcol

        value = col.getCount(col.getRowNames().index(count)).value()
        values[name] = value

        col = eventCounter.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight").getColumn(name="Data")
        col.setName(name)
        tauTable.appendColumn(col)
        effcol = counter.efficiencyColumn(col.getName()+" eff", col)
        tauTable.appendColumn(effcol)
        values[name+"Tau"] = effcol
    
    mainTable.appendColumn(counter.divideColumn("Plus eff/Baseline", values["PlusMain"], values["BaselineMain"]))
    mainTable.appendColumn(counter.divideColumn("Minus eff/Baseline", values["MinusMain"], values["BaselineMain"]))
    tauTable.appendColumn(counter.divideColumn("Plus eff/Baseline", values["PlusTau"], values["BaselineTau"]))
    tauTable.appendColumn(counter.divideColumn("Minus eff/Baseline", values["MinusTau"], values["BaselineTau"]))

    print mainTable.format()
    print tauTable.format()

    plusDiff = abs(values["Baseline"] - values["Plus"])
    minusDiff = abs(values["Baseline"] - values["Minus"])
    maxDiff = max(plusDiff, minusDiff)
    rel = maxDiff / values["Baseline"]

    print "Count %s, baseline %.3f, plus %.3f, minus %.3f" % (count, values["Baseline"], values["Plus"], values["Minus"])
    print "Plus diff %.3f, minus diff %.3f" % (plusDiff, minusDiff)
    print "Relative uncertainty from tau energy scale %.6f" % (rel)


    style = tdrstyle.TDRStyle()
    histograms.createLegend.moveDefaults(dx=-0.32, dh=-0.15)

    metCut = "(met_p4.Et() > 50)"
    bTaggingCut = "passedBTagging"
    tdMt = dataset.TreeDraw("dummy",
                            weight="weightPileup*weightTrigger*weightBTagging",
                            selection=metCut+"&&"+bTaggingCut,
                            varexp="sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) >>tmp(40,0,400)"
                            )

    output = ROOT.TFile.Open("mt_variated_ewk.root", "RECREATE")

    #doPlot(datasets, analyses, tdMt, "mt_variated")
    doPlot(datasets, analyses, "transverseMass", "mt_variated_btagging", output, "Dphi180")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi160", "mt_variated_deltaPhi160", output, "Dphi160")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi130", "mt_variated_deltaPhi130", output, "Dphi130")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi90", "mt_variated_deltaPhi90", output, "Dphi90")

    output.Close()
def doCounters(datasetsEmb):
    isCorrected = isinstance(datasetsEmb, result.DatasetsDYCorrection)
    if isCorrected:
        eventCounter = result.EventCounterDYCorrection(datasetsEmb,
                                                       counters=analysisEmb +
                                                       counters)
    else:
        scaleNormalization = analysisEmb != "signalAnalysis"
        eventCounter = result.EventCounterMany(
            datasetsEmb,
            counters=analysisEmb + counters,
            scaleNormalization=scaleNormalization)

    # Add counts
    sels = []
    tdCount = treeDraw.clone(weight=weightBTagging)
    tdCountMET = tdCount.clone(weight=weight,
                               selection="&&".join(sels + [metCut]))
    tdCountBTagging = tdCount.clone(selection="&&".join(sels +
                                                        [metCut, bTaggingCut]))
    tdCountDeltaPhi160 = tdCount.clone(
        selection="&&".join(sels + [metCut, bTaggingCut, deltaPhi160Cut]))
    tdCountDeltaPhi130 = tdCount.clone(
        selection="&&".join(sels + [metCut, bTaggingCut, deltaPhi130Cut]))
    tdCountDeltaPhi90 = tdCount.clone(
        selection="&&".join(sels + [metCut, bTaggingCut, deltaPhi90Cut]))
    eventCounter.mainCounterAppendRow(
        "JetsForEffs", tdCount.clone(weight=weight, selection="&&".join(sels)))
    eventCounter.mainCounterAppendRow("METForEffs", tdCountMET)
    eventCounter.mainCounterAppendRow("BTagging", tdCountBTagging)
    eventCounter.mainCounterAppendRow("DeltaPhi < 160", tdCountDeltaPhi160)
    eventCounter.mainCounterAppendRow("DeltaPhi < 130", tdCountDeltaPhi130)
    eventCounter.mainCounterAppendRow("DeltaPhi < 90", tdCountDeltaPhi90)

    if not isCorrected:
        td1 = tdCount.clone(selection=metCut + "&&" + bTaggingCut +
                            "&& (tecalometNoHF_p4.Pt() > 60)")
        td2 = tdCount.clone(selection=metCut + "&&" + bTaggingCut +
                            "&& (tecalomet_p4.Pt() > 60)")
        td3 = dataset.TreeDrawCompound(
            td1, {
                "SingleMu_Mu_170722-172619_Aug05": td2,
                "SingleMu_Mu_172620-173198_Prompt": td2,
                "SingleMu_Mu_173236-173692_Prompt": td2,
            })
        eventCounter.mainCounterAppendRow("BTagging+CaloMetNoHF", td1)
        eventCounter.mainCounterAppendRow("BTagging+CaloMet", td2)
        eventCounter.mainCounterAppendRow("BTagging+CaloMet(NoHF)", td3)

    #(mainTable, mainTableChi2) = eventCounter.getMainCounterTableFit()
    mainTable = eventCounter.getMainCounterTable()

    ewkDatasets = ["WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson"]
    allDatasets = None
    if "QCD_Pt20_MuEnriched" in datasetsEmb.getAllDatasetNames():
        allDatasets = ["QCD_Pt20_MuEnriched"] + ewkDatasets

    def ewkSum(table):
        table.insertColumn(
            1,
            counter.sumColumn(
                "EWKMCsum",
                [table.getColumn(name=name) for name in ewkDatasets]))
        if allDatasets != None:
            table.insertColumn(
                2,
                counter.sumColumn(
                    "MCSum",
                    [table.getColumn(name=name) for name in allDatasets]))

    ewkSum(mainTable)
    cellFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.3f'))
    #print mainTableChi2.format(cellFormat)
    print mainTable.format(cellFormat)

    tauTable = eventCounter.getSubCounterTable(
        "TauIDPassedEvt::tauID_HPSTight")
    ewkSum(tauTable)
    print tauTable.format(cellFormat)

    # Efficiencies
    mainTable.keepOnlyRows([
        "All events",
        "Trigger and HLT_MET cut",
        "taus == 1",
        #            "trigger scale factor",
        "electron veto",
        "muon veto",
        "MET",
        "njets",
        "btagging",
        "btagging scale factor",
        "JetsForEffs",
        "METForEffs",
        "BTagging",
        "DeltaPhi < 160",
        "DeltaPhi < 130"
    ])
    tauTable.keepOnlyRows([
        "AllTauCandidates",
        "DecayModeFinding",
        "TauJetPt",
        "TauJetEta",
        "TauLdgTrackExists",
        "TauLdgTrackPtCut",
        "TauECALFiducialCutsCracksAndGap",
        "TauAgainstElectronCut",
        "TauAgainstMuonCut",
        #"EMFractionCut",
        "HPS",
        "TauOneProngCut",
        "TauRtauCut",
    ])

    effFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    for name, table in [("Main", mainTable), ("Tau ID", tauTable)]:
        effTable = counter.CounterTable()
        col = table.getColumn(name="Data")
        effTable.appendColumn(col)
        effTable.appendColumn(
            counter.efficiencyColumn(col.getName() + " eff", col))
        col = table.getColumn(name="EWKMCsum")
        effTable.appendColumn(col)
        effTable.appendColumn(
            counter.efficiencyColumn(col.getName() + " eff", col))
        print "%s counter efficiencies" % name
        print effTable.format(effFormat)

    if isCorrected:
        return

    print "Trigger uncertainties"
    bins = [40, 50, 60, 80]
    tauPtPrototype = ROOT.TH1F("tauPtTrigger", "Tau pt",
                               len(bins) - 1, array.array("d", bins))
    runs = [
        "(160431 <= run && run <= 167913)",
        "(170722 <= run && run <= 173198)",
        "(173236 <= run && run <= 173692)",
        #"(160431 <= run && run <= 173692)",
    ]
    for name, td in [("BTagging", tdCountBTagging),
                     ("DeltaPhi160", tdCountDeltaPhi160),
                     ("DeltaPhi130", tdCountDeltaPhi130),
                     ("DeltaPhi90", tdCountDeltaPhi90)]:
        t = td.clone(varexp="tau_p4.Pt() >>tauPtTrigger")

        NallSum = 0
        NSum = 0
        absUncSquareSum = 0

        for runRegion in runs:
            #neventsPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTrigger")))
            #uncertaintyPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTriggerAbsUnc*weightTriggerAbsUnc/(weightTrigger*weightTrigger)")))
            tmp = t.clone(selection=t.selection + "&&" + runRegion)
            (th1all,
             gr) = datasetsEmb.getHistogram("Data",
                                            tmp.clone(weight=""))  # Nall
            (th1, gr) = datasetsEmb.getHistogram(
                "Data", tmp.clone(weight="weightTrigger"))  # Nevents
            (th12, gr) = datasetsEmb.getHistogram(
                "Data", tmp.clone(weight="weightTriggerAbsUnc"))  # uncertainty

            Nall = th1all.Integral(0, th1all.GetNbinsX() + 1)
            N = th1.Integral(0, th1.GetNbinsX() + 1)
            #absSum2 = th12.Integral(0, th12.GetNbinsX()+1)
            #absUnc = math.sqrt(absSum2)
            #absUnc = th12.Integral(0, 2)
            NallSum += Nall
            NSum += N
            absUnc = tauEmbeddingPlot.squareSum(th12)
            absUncSquareSum += absUnc
            absUnc = math.sqrt(absUnc)
            relUnc = 0
            if N > 0:
                relUnc = absUnc / N

            print "%-15s for runs %s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (
                name, runRegion, Nall, N, absUnc, relUnc)

        absUnc = math.sqrt(absUncSquareSum)
        relUnc = absUnc / NSum

        print "%-15s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (
            name, NallSum, NSum, absUnc, relUnc)
        print
Example #11
0
def doCounters(datasets, mcLumi=None):
    createPlot = lambda name: createPlotCommon(name, datasets, mcLumi)
    eventCounter = counter.EventCounter(datasets, counters=countersWeighted)

    sels = [
        #        "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 20)",
        #        "(20 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 80)",
        #        "(80 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 120)",
        #        "(120 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))",
    ]
    tdCount = treeDraw.clone(weight=weightBTagging)
    tdCountMET = tdCount.clone(weight=weight,
                               selection="&&".join(sels + [metCut]))
    tdCountBTagging = tdCount.clone(selection="&&".join(sels +
                                                        [metCut, bTaggingCut]))
    tdCountDeltaPhi160 = tdCount.clone(
        selection="&&".join(sels + [metCut, bTaggingCut, deltaPhi160Cut]))
    tdCountDeltaPhi130 = tdCount.clone(
        selection="&&".join(sels + [metCut, bTaggingCut, deltaPhi130Cut]))
    tdCountDeltaPhi90 = tdCount.clone(
        selection="&&".join(sels + [metCut, bTaggingCut, deltaPhi90Cut]))
    #    eventCounter.getMainCounter().appendRow("JetsForEffs", tdCount.clone(weight=weight, selection="&&".join(sels)))
    #    eventCounter.getMainCounter().appendRow("METForEffs", tdCountMET)
    #    eventCounter.getMainCounter().appendRow("BTagging", tdCountBTagging)
    #    eventCounter.getMainCounter().appendRow("DeltaPhi < 160", tdCountDeltaPhi160)
    #    eventCounter.getMainCounter().appendRow("DeltaPhi < 130", tdCountDeltaPhi130)
    #    eventCounter.getMainCounter().appendRow("DeltaPhi < 90", tdCountDeltaPhi90)

    td1 = tdCount.clone(selection=metCut + "&&" + bTaggingCut +
                        "&& (tecalometNoHF_p4.Pt() > 60)")
    td2 = tdCount.clone(selection=metCut + "&&" + bTaggingCut +
                        "&& (tecalomet_p4.Pt() > 60)")
    td3 = dataset.TreeDrawCompound(
        td1, {
            "SingleMu_Mu_170722-172619_Aug05": td2,
            "SingleMu_Mu_172620-173198_Prompt": td2,
            "SingleMu_Mu_173236-173692_Prompt": td2,
        })
    #    eventCounter.getMainCounter().appendRow("BTagging+CaloMetNoHF", td1)
    #    eventCounter.getMainCounter().appendRow("BTagging+CaloMet", td2)
    #    eventCounter.getMainCounter().appendRow("BTagging+CaloMet(NoHF)", td3)

    if mcLumi != None:
        eventCounter.normalizeMCToLuminosity(mcLumi)
    else:
        eventCounter.normalizeMCByLuminosity()
    tauEmbedding.scaleNormalization(eventCounter)

    ewkDatasets = ["WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson"]

    table = eventCounter.getMainCounterTable()
    mainTable = table
    #muonAnalysis.addSumColumn(table)
    #mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets]))
    #muonAnalysis.addDataMcRatioColumn(table)
    if datasets.hasDataset("EWKSignal"):
        mainTable.insertColumn(
            7,
            counter.divideColumn(
                "SignalFraction",
                mainTable.getColumn(name="TTToHplus_" + keepSignal),
                mainTable.getColumn(name="EWKSignal")))

    datasets.printInfo()
    print "============================================================"
    print "Main counter (%s)" % eventCounter.getNormalizationString()
    cellFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.3f'))
    print table.format(cellFormat)

    tauTable = eventCounter.getSubCounterTable(
        "TauIDPassedEvt::TauSelection_HPS")
    #muonAnalysis.addSumColumn(tauTable)
    tauTable.insertColumn(
        2,
        counter.sumColumn(
            "EWKMCsum",
            [tauTable.getColumn(name=name) for name in ewkDatasets]))
    print tauTable.format(cellFormat)

    #    print eventCounter.getSubCounterTable("TauIDPassedJets::tauID_HPSTight").format()
    #    table = eventCounter.getSubCounterTable("Trigger")
    #    muonAnalysis.addSumColumn(table)
    #    print table.format(cellFormat)

    mainTable.keepOnlyRows([
        "All events",
        "Trigger and HLT_MET cut",
        "taus == 1",
        #            "trigger scale factor",
        "electron veto",
        "muon veto",
        "MET",
        "njets",
        "btagging",
        "btagging scale factor",
        "JetsForEffs",
        "METForEffs",
        "BTagging",
        "DeltaPhi < 160",
        "DeltaPhi < 130"
    ])
    tauTable.keepOnlyRows([
        "AllTauCandidates",
        "DecayModeFinding",
        "TauJetPt",
        "TauJetEta",
        "TauLdgTrackExists",
        "TauLdgTrackPtCut",
        "TauECALFiducialCutsCracksAndGap",
        "TauAgainstElectronCut",
        "TauAgainstMuonCut",
        #"EMFractionCut",
        "HPS",
        "TauOneProngCut",
        "TauRtauCut",
    ])

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    for name, table in [("Main", mainTable), ("Tau ID", tauTable)]:
        effTable = counter.CounterTable()
        col = table.getColumn(name="Data")
        effTable.appendColumn(col)
        effTable.appendColumn(
            counter.efficiencyColumn(col.getName() + " eff", col))
        col = table.getColumn(name="EWKMCsum")
        effTable.appendColumn(col)
        effTable.appendColumn(
            counter.efficiencyColumn(col.getName() + " eff", col))
        print "%s counter efficiencies" % name
        print effTable.format(effFormat)

    print "Trigger uncertainties"
    bins = [40, 50, 60, 80]
    tauPtPrototype = ROOT.TH1F("tauPtTrigger", "Tau pt",
                               len(bins) - 1, array.array("d", bins))
    runs = [
        "(160431 <= run && run <= 167913)",
        "(170722 <= run && run <= 173198)",
        "(173236 <= run && run <= 173692)",
        #"(160431 <= run && run <= 173692)",
    ]
    for name, td in [("BTagging", tdCountBTagging),
                     ("DeltaPhi160", tdCountDeltaPhi160),
                     ("DeltaPhi130", tdCountDeltaPhi130),
                     ("DeltaPhi90", tdCountDeltaPhi90)]:
        t = td.clone(varexp="tau_p4.Pt() >>tauPtTrigger")

        NallSum = 0
        NSum = 0
        absUncSquareSum = 0

        for runRegion in runs:
            #neventsPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTrigger")))
            #uncertaintyPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTriggerAbsUnc*weightTriggerAbsUnc/(weightTrigger*weightTrigger)")))
            tmp = t.clone(selection=t.selection + "&&" + runRegion)
            nallPlot = createPlot(tmp.clone(weight=""))
            neventsPlot = createPlot(tmp.clone(weight="weightTrigger"))
            uncertaintyPlot = createPlot(
                tmp.clone(weight="weightTriggerAbsUnc"))
            th1all = nallPlot.histoMgr.getHisto("Data").getRootHisto()
            th1 = neventsPlot.histoMgr.getHisto("Data").getRootHisto()
            th12 = uncertaintyPlot.histoMgr.getHisto("Data").getRootHisto()

            Nall = th1all.Integral(0, th1all.GetNbinsX() + 1)
            N = th1.Integral(0, th1.GetNbinsX() + 1)
            #absSum2 = th12.Integral(0, th12.GetNbinsX()+1)
            #absUnc = math.sqrt(absSum2)
            #absUnc = th12.Integral(0, 2)
            NallSum += Nall
            NSum += N
            absUnc = tauEmbedding.squareSum(th12)
            absUncSquareSum += absUnc
            absUnc = math.sqrt(absUnc)
            relUnc = 0
            if N > 0:
                relUnc = absUnc / N

            print "%-15s for runs %s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (
                name, runRegion, Nall, N, absUnc, relUnc)

        absUnc = math.sqrt(absUncSquareSum)
        relUnc = absUnc / NSum

        print "%-15s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (
            name, NallSum, NSum, absUnc, relUnc)
        print
def doCounters(datasetsEmb2, datasetsSig2, datasetName):
    lumi = datasetsEmb2.getDataset("Data").getLuminosity()

    datasetsEmb = datasetsEmb2.deepCopy()
    datasetsSig = datasetsSig2.deepCopy()

    datasetsEmb.remove(filter(lambda name: name != datasetName, datasetsEmb.getAllDatasetNames()))
    datasetsSig.remove(filter(lambda name: name != datasetName, datasetsSig.getAllDatasetNames()))
    # Counters
    eventCounterEmb = counter.EventCounter(datasetsEmb, counters=analysisEmb+"Counters")
    eventCounterSig = counter.EventCounter(datasetsSig, counters=analysisSig+"Counters")
    eventCounterEmb.normalizeMCToLuminosity(lumi)
    eventCounterSig.normalizeMCToLuminosity(lumi)

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f'))

    counterEmb = eventCounterEmb.getMainCounter()
    counterSig = eventCounterSig.getMainCounter()
    treeDraw = dataset.TreeDraw("dummy")
    tdEmb = treeDraw.clone(tree=analysisEmb+"/tree")
    tdSig = treeDraw.clone(tree=analysisSig+"/tree")
    selectionsCumulative = []
    tauSelectionsCumulative = []
    def sel(name, selection):
        selectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        counterEmb.appendRow(name, tdEmb.clone(selection=sel))
        counterSig.appendRow(name, tdSig.clone(selection=sel))
    def tauSel(name, selection):
        tauSelectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        counterEmb.appendRow(name, tdEmb.clone(selection=sel))
        counterSig.appendRow(name, tdSig.clone(selection=sel))

#    sel("Primary vertex", tauPlot.pvSelection)
    sel(">= 1 tau candidate", "Length$(taus_p4) >= 1")
    tauSel("Decay mode finding", tauPlot.decayModeFinding)
    tauSel("pT > 15", "(taus_p4.Pt() > 15)")
    tauSel("pT > 40", tauPlot.tauPtCut)
    tauSel("eta < 2.1", tauPlot.tauEtaCut)
    tauSel("leading track pT > 20", tauPlot.tauLeadPt)
    tauSel("ECAL fiducial", tauPlot.ecalFiducial)
    tauSel("againstElectron", tauPlot.electronRejection)
    tauSel("againstMuon", tauPlot.muonRejection)
    tauSel("isolation", tauPlot.tightIsolation)
    tauSel("oneProng", tauPlot.oneProng)
    tauSel("Rtau", tauPlot.rtau)
    sel("3 jets", tauPlot.jetEventSelection)
    sel("MET", tauPlot.metSelection)
    sel("btag", tauPlot.btagEventSelection)



    table = counter.CounterTable()
    col = counterEmb.getTable().getColumn(name=datasetName)
    col.setName("Embedded")
    table.appendColumn(col)
    col = counterSig.getTable().getColumn(name=datasetName)
    col.setName("Normal")
    table.appendColumn(col)

    col = table.getColumn(name="Embedded")
    table.insertColumn(1, counter.efficiencyColumn(col.getName()+" eff", col))
    col = table.getColumn(name="Normal")
    table.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))

    print "%s counters" % datasetName
    print table.format(effFormat)
def main():
    datasets = dataset.getDatasetsFromMulticrabCfg(counters=baseline +
                                                   "Counters",
                                                   weightedCounters=False)

    datasets.remove([
        #            "SingleMu_Mu_160431-163261_May10",
        #            "SingleMu_Mu_163270-163869_May10",
        #            "SingleMu_Mu_165088-166150_Prompt",
        #            "SingleMu_Mu_166161-166164_Prompt",
        #            "SingleMu_Mu_166346-166346_Prompt",
        #            "SingleMu_Mu_166374-167043_Prompt",
        #            "SingleMu_Mu_167078-167913_Prompt",

        #            "SingleMu_Mu_170722-172619_Aug05",
        #            "SingleMu_Mu_172620-173198_Prompt",
        #            "SingleMu_Mu_173236-173692_Prompt",
    ])
    datasets.loadLuminosities()
    plots.mergeRenameReorderForDataMC(datasets)

    values = {}

    analyses = [("Baseline", baseline), ("Plus", plus), ("Minus", minus)]

    mainTable = counter.CounterTable()
    tauTable = counter.CounterTable()
    cntr = "Counters"
    if normalize:
        cntr += "/weighted"
    for name, analysis in analyses:
        eventCounter = counter.EventCounter(datasets, counters=analysis + cntr)

        eventCounter.normalizeMCByLuminosity()
        if normalize:
            tauEmbedding.scaleNormalization(eventCounter)
        col = eventCounter.getMainCounterTable().getColumn(name="Data")
        col.setName(name)
        mainTable.appendColumn(col)
        effcol = counter.efficiencyColumn(col.getName() + " eff", col)
        mainTable.appendColumn(effcol)
        values[name + "Main"] = effcol

        value = col.getCount(col.getRowNames().index(count)).value()
        values[name] = value

        col = eventCounter.getSubCounterTable(
            "TauIDPassedEvt::tauID_HPSTight").getColumn(name="Data")
        col.setName(name)
        tauTable.appendColumn(col)
        effcol = counter.efficiencyColumn(col.getName() + " eff", col)
        tauTable.appendColumn(effcol)
        values[name + "Tau"] = effcol

    mainTable.appendColumn(
        counter.divideColumn("Plus eff/Baseline", values["PlusMain"],
                             values["BaselineMain"]))
    mainTable.appendColumn(
        counter.divideColumn("Minus eff/Baseline", values["MinusMain"],
                             values["BaselineMain"]))
    tauTable.appendColumn(
        counter.divideColumn("Plus eff/Baseline", values["PlusTau"],
                             values["BaselineTau"]))
    tauTable.appendColumn(
        counter.divideColumn("Minus eff/Baseline", values["MinusTau"],
                             values["BaselineTau"]))

    print mainTable.format()
    print tauTable.format()

    plusDiff = abs(values["Baseline"] - values["Plus"])
    minusDiff = abs(values["Baseline"] - values["Minus"])
    maxDiff = max(plusDiff, minusDiff)
    rel = maxDiff / values["Baseline"]

    print "Count %s, baseline %.3f, plus %.3f, minus %.3f" % (
        count, values["Baseline"], values["Plus"], values["Minus"])
    print "Plus diff %.3f, minus diff %.3f" % (plusDiff, minusDiff)
    print "Relative uncertainty from tau energy scale %.6f" % (rel)

    style = tdrstyle.TDRStyle()
    histograms.createLegend.moveDefaults(dx=-0.32, dh=-0.15)

    metCut = "(met_p4.Et() > 50)"
    bTaggingCut = "passedBTagging"
    tdMt = dataset.TreeDraw(
        "dummy",
        weight="weightPileup*weightTrigger*weightBTagging",
        selection=metCut + "&&" + bTaggingCut,
        varexp=
        "sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) >>tmp(40,0,400)"
    )

    output = ROOT.TFile.Open("mt_variated_ewk.root", "RECREATE")

    #doPlot(datasets, analyses, tdMt, "mt_variated")
    doPlot(datasets, analyses, "transverseMass", "mt_variated_btagging",
           output, "Dphi180")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi160",
           "mt_variated_deltaPhi160", output, "Dphi160")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi130",
           "mt_variated_deltaPhi130", output, "Dphi130")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi90",
           "mt_variated_deltaPhi90", output, "Dphi90")

    output.Close()
def doCounters(datasets, mcLumi=None):
    createPlot = lambda name: createPlotCommon(name, datasets, mcLumi)
    eventCounter = counter.EventCounter(datasets, counters=countersWeighted)
   

    sels = [
#        "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 20)",
#        "(20 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 80)",
#        "(80 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 120)",
#        "(120 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))",
        ]
    tdCount = treeDraw.clone(weight=weightBTagging)
    tdCountMET = tdCount.clone(weight=weight, selection="&&".join(sels+[metCut]))
    tdCountBTagging = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut]))
    tdCountDeltaPhi160 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi160Cut]))
    tdCountDeltaPhi130 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi130Cut]))
    tdCountDeltaPhi90 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi90Cut]))
    eventCounter.getMainCounter().appendRow("JetsForEffs", tdCount.clone(weight=weight, selection="&&".join(sels)))
    eventCounter.getMainCounter().appendRow("METForEffs", tdCountMET)
    eventCounter.getMainCounter().appendRow("BTagging", tdCountBTagging)
    eventCounter.getMainCounter().appendRow("DeltaPhi < 160", tdCountDeltaPhi160)
    eventCounter.getMainCounter().appendRow("DeltaPhi < 130", tdCountDeltaPhi130)
    eventCounter.getMainCounter().appendRow("DeltaPhi < 90", tdCountDeltaPhi90)

    td1 = tdCount.clone(selection=metCut+"&&"+bTaggingCut+"&& (tecalometNoHF_p4.Pt() > 60)")
    td2 = tdCount.clone(selection=metCut+"&&"+bTaggingCut+"&& (tecalomet_p4.Pt() > 60)")
    td3 = dataset.TreeDrawCompound(td1, {
            "SingleMu_Mu_170722-172619_Aug05": td2,
            "SingleMu_Mu_172620-173198_Prompt": td2,
            "SingleMu_Mu_173236-173692_Prompt": td2,
            })
    eventCounter.getMainCounter().appendRow("BTagging+CaloMetNoHF", td1)
    eventCounter.getMainCounter().appendRow("BTagging+CaloMet", td2)
    eventCounter.getMainCounter().appendRow("BTagging+CaloMet(NoHF)", td3)

    if mcLumi != None:
        eventCounter.normalizeMCToLuminosity(mcLumi)
    else:
        eventCounter.normalizeMCByLuminosity()
    tauEmbedding.scaleNormalization(eventCounter)

    ewkDatasets = [
        "WJets", "TTJets",
        "DYJetsToLL", "SingleTop", "Diboson"
        ]

    table = eventCounter.getMainCounterTable()
    mainTable = table
    muonAnalysis.addSumColumn(table)
    mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets]))
#    table = eventCounter.getSubCounterTable("Trigger")
    #    muonAnalysis.reorderCounterTable(table)
    muonAnalysis.addDataMcRatioColumn(table)
    if datasets.hasDataset("EWKSignal"):
        mainTable.insertColumn(7, counter.divideColumn("SignalFraction", mainTable.getColumn(name="TTToHplus_"+keepSignal), mainTable.getColumn(name="EWKSignal")))

    datasets.printInfo()
    print "============================================================"
    print "Main counter (%s)" % eventCounter.getNormalizationString()
    cellFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.3f'))
    print table.format(cellFormat)

    tauTable = eventCounter.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight")
    #muonAnalysis.addSumColumn(tauTable)
    tauTable.insertColumn(2, counter.sumColumn("EWKMCsum", [tauTable.getColumn(name=name) for name in ewkDatasets]))
    print tauTable.format(cellFormat)

#    print eventCounter.getSubCounterTable("TauIDPassedJets::tauID_HPSTight").format()
#    table = eventCounter.getSubCounterTable("Trigger")
#    muonAnalysis.addSumColumn(table)
#    print table.format(cellFormat)

    mainTable.keepOnlyRows([
            "All events",
            "Trigger and HLT_MET cut",
            "taus == 1",
#            "trigger scale factor",
            "electron veto",
            "muon veto",
            "MET",
            "njets",
            "btagging",
            "btagging scale factor",
            "JetsForEffs",
            "METForEffs",
            "BTagging",
            "DeltaPhi < 160",
            "DeltaPhi < 130"
            ])
    tauTable.keepOnlyRows([
            "AllTauCandidates",
            "DecayModeFinding",
            "TauJetPt",
            "TauJetEta",
            "TauLdgTrackExists",
            "TauLdgTrackPtCut",
            "TauECALFiducialCutsCracksAndGap",
            "TauAgainstElectronCut",
            "TauAgainstMuonCut",
            #"EMFractionCut",
            "HPS",
            "TauOneProngCut",
            "TauRtauCut",
            ])

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    for name, table in [("Main", mainTable), ("Tau ID", tauTable)]:
        effTable = counter.CounterTable()
        col = table.getColumn(name="Data")
        effTable.appendColumn(col)
        effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))
        col = table.getColumn(name="EWKMCsum")
        effTable.appendColumn(col)
        effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))
        print "%s counter efficiencies" % name
        print effTable.format(effFormat)


    print "Trigger uncertainties"
    bins = [40, 50, 60, 80]
    tauPtPrototype = ROOT.TH1F("tauPtTrigger", "Tau pt", len(bins)-1, array.array("d", bins))
    runs = [
        "(160431 <= run && run <= 167913)",
        "(170722 <= run && run <= 173198)",
        "(173236 <= run && run <= 173692)",
        #"(160431 <= run && run <= 173692)",
        ]
    for name, td in [
        ("BTagging", tdCountBTagging),
        ("DeltaPhi160", tdCountDeltaPhi160),
        ("DeltaPhi130", tdCountDeltaPhi130),
        ("DeltaPhi90", tdCountDeltaPhi90)
        ]:
        t = td.clone(varexp="tau_p4.Pt() >>tauPtTrigger")
        
        NallSum = 0
        NSum = 0
        absUncSquareSum = 0

        for runRegion in runs:
            #neventsPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTrigger")))
            #uncertaintyPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTriggerAbsUnc*weightTriggerAbsUnc/(weightTrigger*weightTrigger)")))
            tmp = t.clone(selection=t.selection+"&&"+runRegion)
            nallPlot = createPlot(tmp.clone(weight=""))
            neventsPlot = createPlot(tmp.clone(weight="weightTrigger"))
            uncertaintyPlot = createPlot(tmp.clone(weight="weightTriggerAbsUnc"))
            th1all = nallPlot.histoMgr.getHisto("Data").getRootHisto()
            th1 = neventsPlot.histoMgr.getHisto("Data").getRootHisto()
            th12 = uncertaintyPlot.histoMgr.getHisto("Data").getRootHisto()

            Nall = th1all.Integral(0, th1all.GetNbinsX()+1)
            N = th1.Integral(0, th1.GetNbinsX()+1)
            #absSum2 = th12.Integral(0, th12.GetNbinsX()+1)
            #absUnc = math.sqrt(absSum2)
            #absUnc = th12.Integral(0, 2)
            NallSum += Nall
            NSum += N
            absUnc = squareSum(th12)
            absUncSquareSum += absUnc
            absUnc = math.sqrt(absUnc)
            relUnc = 0
            if N > 0:
                relUnc = absUnc/N

            print "%-15s for runs %s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (name, runRegion, Nall, N, absUnc, relUnc)


        absUnc = math.sqrt(absUncSquareSum)
        relUnc = absUnc/NSum

        print "%-15s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (name, NallSum, NSum, absUnc, relUnc)
        print
def doCounters(datasetsEmb, datasetsSig, datasetName, normalizeEmb=True):
    lumi = datasetsEmb.getLuminosity()

    # Counters
    eventCounterEmb = tauEmbedding.EventCounterMany(
        datasetsEmb,
        normalize=normalizeEmb)  #, counters=analysisEmb+"/counters")
    eventCounterSig = counter.EventCounter(datasetsSig)

    def isNotThis(name):
        return name != datasetName

    eventCounterEmb.removeColumns(
        filter(isNotThis, datasetsEmb.getAllDatasetNames()))
    eventCounterSig.removeColumns(
        filter(isNotThis, datasetsSig.getAllDatasetNames()))
    eventCounterSig.normalizeMCToLuminosity(lumi)

    tdCount = dataset.TreeDraw("dummy",
                               weight=tauEmbedding.signalNtuple.weightBTagging)
    tdCountMET = tdCount.clone(weight=tauEmbedding.signalNtuple.weight,
                               selection=tauEmbedding.signalNtuple.metCut)
    tdCountBTagging = tdCount.clone(
        selection=And(tauEmbedding.signalNtuple.metCut,
                      tauEmbedding.signalNtuple.bTaggingCut))
    tdCountDeltaPhi160 = tdCount.clone(selection=And(
        tauEmbedding.signalNtuple.metCut, tauEmbedding.signalNtuple.
        bTaggingCut, tauEmbedding.signalNtuple.deltaPhi160Cut))
    tdCountDeltaPhi130 = tdCount.clone(selection=And(
        tauEmbedding.signalNtuple.metCut, tauEmbedding.signalNtuple.
        bTaggingCut, tauEmbedding.signalNtuple.deltaPhi130Cut))

    def addRow(name, td):
        tdEmb = td.clone(tree=analysisEmb + "/tree")
        tdSig = td.clone(tree=analysisSig + "/tree")
        eventCounterEmb.mainCounterAppendRow(name, tdEmb)
        eventCounterSig.getMainCounter().appendRow(name, tdSig)

    # addRow("JetsForEffs", tdCount.clone(weight=tauEmbedding.signalNtuple.weight))
    # addRow("METForEffs", tdCountMET)
    # addRow("BTagging (SF)", tdCountBTagging)
    # addRow("DeltaPhi < 160", tdCountDeltaPhi160)
    # addRow("BTagging (SF) again", tdCountBTagging)
    # addRow("DeltaPhi < 130", tdCountDeltaPhi130)

    table = counter.CounterTable()
    col = eventCounterEmb.getMainCounterTable().getColumn(name=datasetName)
    col.setName("Embedded")
    table.appendColumn(col)
    col = eventCounterSig.getMainCounterTable().getColumn(name=datasetName)
    col.setName("Normal")
    table.appendColumn(col)

    tableTau = counter.CounterTable()
    tmp = "TauIDPassedEvt::TauSelection_HPS"
    col = eventCounterEmb.getSubCounterTable(tmp).getColumn(name=datasetName)
    col.setName("Embedded")
    tableTau.appendColumn(col)
    col = eventCounterSig.getSubCounterTable(tmp).getColumn(name=datasetName)
    col.setName("Normal")
    tableTau.appendColumn(col)

    postfix = ""
    if not normalizeEmb:
        postfix = "_notEmbNormalized"

    fname = "counters_selections_%s%s.txt" % (datasetName, postfix)
    f = open(fname, "w")
    f.write(table.format())
    f.write("\n")
    f.write(tableTau.format())
    f.close()
    print "Printed selection counters to", fname

    if not normalizeEmb:
        return

    # Calculate efficiencies
    table.keepOnlyRows([
        "njets", "MET", "btagging", "btagging scale factor",
        "DeltaPhi(Tau,MET) upper limit"
    ])
    # btag SF efficiency w.r.t. MET
    row = table.getRow(name="MET")
    row.setName("METForEff")
    table.insertRow(3, row)

    tableEff = counter.CounterTable()
    tableEff.appendColumn(
        counter.efficiencyColumn("Embedded eff",
                                 table.getColumn(name="Embedded")))
    tableEff.appendColumn(
        counter.efficiencyColumn("Normal eff", table.getColumn(name="Normal")))
    tableEff.removeRow(name="METForEff")

    effFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.4f', withPrecision=2))

    #    print table.format(effFormat)

    fname = "counters_selections_%s_eff.txt" % datasetName
    f = open(fname, "w")
    f.write(tableEff.format(effFormat))
    f.close()
    print "Printed selection efficiencies to", fname
def doCounters(datasetsEmb, datasetsSig, datasetName):
    lumi = datasetsEmb.getLuminosity()
    treeDraw = dataset.TreeDraw("dummy", weight=weight)

    # Counters
    eventCounterEmb = tauEmbedding.EventCounterMany(datasetsEmb, counters=analysisEmb+"Counters")
    eventCounterSig = counter.EventCounter(datasetsSig, counters=analysisSig+"Counters")

    def isNotThis(name):
        return name != datasetName

    eventCounterEmb.removeColumns(filter(isNotThis, datasetsEmb.getAllDatasetNames()))
    eventCounterSig.removeColumns(filter(isNotThis, datasetsSig.getAllDatasetNames()))
    eventCounterSig.normalizeMCToLuminosity(lumi)

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f'))

    tdEmb = treeDraw.clone(tree=analysisEmb+"/tree")
    tdSig = treeDraw.clone(tree=analysisSig+"/tree")
    selectionsCumulative = []
    tauSelectionsCumulative = []
    def sel(name, selection):
        selectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        eventCounterEmb.mainCounterAppendRow(name, tdEmb.clone(selection=sel))
        eventCounterSig.getMainCounter().appendRow(name, tdSig.clone(selection=sel))
    def tauSel(name, selection):
        tauSelectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        eventCounterEmb.mainCounterAppendRow(name, tdEmb.clone(selection=sel))
        eventCounterSig.getMainCounter().appendRow(name, tdSig.clone(selection=sel))

#    sel("Primary vertex", tauEmbedding.tauNtuple.pvSelection)
    sel(">= 1 tau candidate", "Length$(taus_p4) >= 1")
    tauSel("Decay mode finding", tauEmbedding.tauNtuple.decayModeFinding)
    tauSel("pT > 15", tauEmbedding.tauNtuple.tauPtPreCut)
    tauSel("pT > 40", tauEmbedding.tauNtuple.tauPtCut)
    tauSel("eta < 2.1", tauEmbedding.tauNtuple.tauEtaCut)
    tauSel("leading track pT > 20", tauEmbedding.tauNtuple.tauLeadPt)
    tauSel("ECAL fiducial", tauEmbedding.tauNtuple.ecalFiducial)
    tauSel("againstElectron", tauEmbedding.tauNtuple.electronRejection)
    tauSel("againstMuon", tauEmbedding.tauNtuple.muonRejection)
    tauSel("isolation", tauEmbedding.tauNtuple.tightIsolation)
    tauSel("oneProng", tauEmbedding.tauNtuple.oneProng)
    tauSel("Rtau", tauEmbedding.tauNtuple.rtau)
    sel("3 jets", tauEmbedding.tauNtuple.jetEventSelection)
    sel("MET", tauEmbedding.tauNtuple.metSelection)
    sel("btag", tauEmbedding.tauNtuple.btagEventSelection)

    table = counter.CounterTable()
    col = eventCounterEmb.getMainCounterTable().getColumn(name=datasetName)
    col.setName("Embedded")
    table.appendColumn(col)
    col = eventCounterSig.getMainCounterTable().getColumn(name=datasetName)
    col.setName("Normal")
    table.appendColumn(col)

    col = table.getColumn(name="Embedded")
    table.insertColumn(1, counter.efficiencyColumn(col.getName()+" eff", col))
    col = table.getColumn(name="Normal")
    table.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))

    print "%s counters" % datasetName
    print table.format(effFormat)

    f = open("counters_"+datasetName+".txt", "w")
    f.write(table.format(effFormat))
    f.write("\n")
    f.close()
def doCounters(datasets):
    # Counters
    eventCounter = counter.EventCounter(datasets, counters=counters)
    mainCounter = eventCounter.getMainCounter()

    selectionsCumulative = []
    tauSelectionsCumulative = []
    td = treeDraw.clone(weight="")

    def sel(name, selection):
        selectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        mainCounter.appendRow(name, td.clone(selection="&&".join(sel)))

    def tauSel(name, selection):
        tauSelectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        mainCounter.appendRow(name, td.clone(selection="&&".join(sel)))

    sel("Primary vertex", pvSelection)

    sel(">= 1 tau candidate", "Length$(taus_p4) >= 1")
    tauSel("Decay mode finding", decayModeFinding)
    tauSel("pT > 15", "(taus_p4.Pt() > 15)")
    tauSel("pT > 40", tauPtCut)  #
    tauSel("eta < 2.1", tauEtaCut)
    tauSel("leading track pT > 20", tauLeadPt)
    tauSel("ECAL fiducial", ecalFiducial)
    tauSel("againstElectron", electronRejection)  #
    tauSel("againstMuon", muonRejection)
    tauSel("isolation", tightIsolation)  #
    tauSel("oneProng", oneProng)  #
    tauSel("Rtau", rtau)  #

    sel("3 jets", jetEventSelection)
    sel("MET", metSelection)
    sel("btag", btagEventSelection)
    sel("deltaPhi<160", deltaPhi160Selection)

    fullSelection = "&&".join(
        selectionsCumulative +
        ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)])
    fullSelectionCaloMetNoHF = fullSelection + "&&" + caloMetNoHF
    fullSelectionCaloMet = fullSelection + "&&" + caloMet
    #print fullSelection
    f = open("pickEvents.txt", "w")

    def printPickEvent(tree):
        f.write("%d:%d:%d\n" % (tree.run, tree.lumi, tree.event))

    ts = dataset.TreeScan(td.tree,
                          function=printPickEvent,
                          selection=fullSelection)
    ts2 = dataset.TreeScan(td.tree,
                           function=printPickEvent,
                           selection=fullSelectionCaloMetNoHF)
    ts3 = dataset.TreeScan(td.tree,
                           function=printPickEvent,
                           selection=fullSelectionCaloMet)
    ts4 = dataset.TreeDrawCompound(
        ts2, {
            "SingleMu_Mu_170722-172619_Aug05": ts3,
            "SingleMu_Mu_172620-173198_Prompt": ts3,
            "SingleMu_Mu_173236-173692_Prompt": ts3,
        })
    datasets.getDataset("Data").getDatasetRootHisto(ts4)
    f.close()

    ewkDatasets = [
        "WJets",
        "TTJets",
        #        "DYJetsToLL", "SingleTop", "Diboson"
    ]

    eventCounter.normalizeMCByLuminosity()
    mainTable = eventCounter.getMainCounterTable()
    #mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets]))
    cellFormat = counter.TableFormatText(
        counter.CellFormatText(valueFormat='%.3f',
                               #valueOnly=True
                               ),
        #                                         columnSeparator = ";",
    )
    print mainTable.format(cellFormat)

    return

    effFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.4f'))
    effTable = counter.CounterTable()
    col = table.getColumn(name="Data")
    effTable.appendColumn(col)
    effTable.appendColumn(counter.efficiencyColumn(col.getName() + " eff",
                                                   col))
    col = table.getColumn(name="EWKMCsum")
    effTable.appendColumn(col)
    effTable.appendColumn(counter.efficiencyColumn(col.getName() + " eff",
                                                   col))
    print effTable.format(effFormat)
def doTauCounters(datasetsEmb, datasetsSig, datasetName):
    lumi = datasetsEmb.getLuminosity()
    treeDraw = dataset.TreeDraw("dummy", weight=tauEmbedding.tauNtuple.weight[tauEmbedding.era])

    eventCounterEmb = tauEmbedding.EventCounterMany(datasetsEmb)
    eventCounterSig = counter.EventCounter(datasetsSig)

    def isNotThis(name):
        return name != datasetName

    eventCounterEmb.removeColumns(filter(isNotThis, datasetsEmb.getAllDatasetNames()))
    eventCounterSig.removeColumns(filter(isNotThis, datasetsSig.getAllDatasetNames()))
    eventCounterSig.normalizeMCToLuminosity(lumi)

    effFormat = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat="%.4f", withPrecision=2))

    tdEmb = treeDraw.clone(tree=tauAnalysisEmb+"/tree")
    tdSig = treeDraw.clone(tree=tauAnalysisSig+"/tree")
    selectionsCumulative = []
    tauSelectionsCumulative = []
    rowNames = []
    def sel(name, selection):
        selectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        eventCounterEmb.mainCounterAppendRow(name, tdEmb.clone(selection=sel))
        eventCounterSig.getMainCounter().appendRow(name, tdSig.clone(selection=sel))
        rowNames.append(name)
    def tauSel(name, selection):
        tauSelectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        eventCounterEmb.mainCounterAppendRow(name, tdEmb.clone(selection=sel))
        eventCounterSig.getMainCounter().appendRow(name, tdSig.clone(selection=sel))
        rowNames.append(name)

    sel(">= 1 tau candidate", "Length$(taus_p4) >= 1")
    tauSel("Decay mode finding", tauEmbedding.tauNtuple.decayModeFinding)
    tauSel("pT > 15", "taus_p4.Pt() > 15")
    tauSel("pT > 40", tauEmbedding.tauNtuple.tauPtCut)
    tauSel("eta < 2.1", tauEmbedding.tauNtuple.tauEtaCut)
    tauSel("leading track pT > 20", tauEmbedding.tauNtuple.tauLeadPt)
    tauSel("ECAL fiducial", tauEmbedding.tauNtuple.ecalFiducial)
    tauSel("againstElectron", tauEmbedding.tauNtuple.electronRejection)
    tauSel("againstMuon", tauEmbedding.tauNtuple.muonRejection)
    tauSel("isolation", tauEmbedding.tauNtuple.tightIsolation)
    tauSel("oneProng", tauEmbedding.tauNtuple.oneProng)
    tauSel("Rtau", tauEmbedding.tauNtuple.rtau)
    sel("MET", tauEmbedding.tauNtuple.metSelection)

    table = counter.CounterTable()
    col = eventCounterEmb.getMainCounterTable().getColumn(name=datasetName)
    table.appendColumn(counter.efficiencyColumn("Embedded eff", col))
    col = eventCounterSig.getMainCounterTable().getColumn(name=datasetName)
    table.appendColumn(counter.efficiencyColumn("Normal eff", col))

    embeddingMuonIsolationEff = table.getCount(rowName="tauEmbeddingMuonsCount", colName="Embedded eff")
    embeddingTauIsolationEff = table.getCount(rowName="isolation", colName="Embedded eff")
    embeddingTotalIsolationEff = embeddingMuonIsolationEff.clone()
    embeddingTotalIsolationEff.multiply(embeddingTauIsolationEff)

    # Remove unnecessary rows
    del rowNames[0]
    table.keepOnlyRows(rowNames)
    rowIndex = table.getRowNames().index("isolation")
    table.insertRow(rowIndex, counter.CounterRow("Mu isolation (emb)", ["Embedded eff", "Normal eff"],
                                                 [embeddingMuonIsolationEff, None]))
    table.insertRow(rowIndex+1, counter.CounterRow("Tau isolation (emb)", ["Embedded eff", "Normal eff"],
                                                   [embeddingTauIsolationEff, None]))
    table.setCount2(embeddingTotalIsolationEff, rowName="isolation", colName="Embedded eff")
    table.setCount2(None, rowName="pT > 15", colName="Normal eff")

    #print table.format(effFormat)
    fname = "counters_tau_"+datasetName+".txt"
    f = open(fname, "w")
    f.write(table.format(effFormat))
    f.write("\n")
    f.close()
    print "Printed tau counters to", fname
Example #19
0
def doCounters(datasets):
    # Counters
    eventCounter = counter.EventCounter(datasets, counters=counters)
    mainCounter = eventCounter.getMainCounter();

    selectionsCumulative = []
    tauSelectionsCumulative = []
    td = treeDraw.clone(weight="")
    def sel(name, selection):
        selectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        mainCounter.appendRow(name, td.clone(selection="&&".join(sel)))
    def tauSel(name, selection):
        tauSelectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        mainCounter.appendRow(name, td.clone(selection="&&".join(sel)))

    sel("Primary vertex", pvSelection)

    sel(">= 1 tau candidate", "Length$(taus_p4) >= 1")
    tauSel("Decay mode finding", decayModeFinding)
    tauSel("pT > 15", "(taus_p4.Pt() > 15)")
    tauSel("pT > 40", tauPtCut) #
    tauSel("eta < 2.1", tauEtaCut)
    tauSel("leading track pT > 20", tauLeadPt)
    tauSel("ECAL fiducial", ecalFiducial)
    tauSel("againstElectron", electronRejection) #
    tauSel("againstMuon", muonRejection)
    tauSel("isolation", tightIsolation) #
    tauSel("oneProng", oneProng) #
    tauSel("Rtau", rtau) #

    sel("3 jets", jetEventSelection)
    sel("MET", metSelection)
    sel("btag", btagEventSelection)
    sel("deltaPhi<160", deltaPhi160Selection)

    fullSelection = "&&".join(selectionsCumulative+["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)])
    fullSelectionCaloMetNoHF = fullSelection+"&&"+caloMetNoHF
    fullSelectionCaloMet = fullSelection+"&&"+caloMet
    #print fullSelection
    f = open("pickEvents.txt", "w")
    def printPickEvent(tree):
        f.write("%d:%d:%d\n" % (tree.run, tree.lumi, tree.event))

    ts = dataset.TreeScan(td.tree, function=printPickEvent, selection=fullSelection)
    ts2 = dataset.TreeScan(td.tree, function=printPickEvent, selection=fullSelectionCaloMetNoHF)
    ts3 = dataset.TreeScan(td.tree, function=printPickEvent, selection=fullSelectionCaloMet)
    ts4 = dataset.TreeDrawCompound(ts2, {
            "SingleMu_Mu_170722-172619_Aug05": ts3,
            "SingleMu_Mu_172620-173198_Prompt": ts3,
            "SingleMu_Mu_173236-173692_Prompt": ts3,
            })
    datasets.getDataset("Data").getDatasetRootHisto(ts4)
    f.close()


    ewkDatasets = [
        "WJets", "TTJets",
#        "DYJetsToLL", "SingleTop", "Diboson"
        ]

    eventCounter.normalizeMCByLuminosity()
    mainTable = eventCounter.getMainCounterTable()
    #mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets]))
    cellFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.3f',
                                                                #valueOnly=True
                                                                ),
#                                         columnSeparator = ";",
                                         )
    print mainTable.format(cellFormat)

    return

    effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f'))
    effTable = counter.CounterTable()
    col = table.getColumn(name="Data")
    effTable.appendColumn(col)
    effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))
    col = table.getColumn(name="EWKMCsum")
    effTable.appendColumn(col)
    effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))
    print effTable.format(effFormat)
Example #20
0
def doTauCounters(datasetsEmb, datasetsSig, datasetName):
    lumi = datasetsEmb.getLuminosity()
    treeDraw = dataset.TreeDraw(
        "dummy", weight=tauEmbedding.tauNtuple.weight[tauEmbedding.era])

    eventCounterEmb = tauEmbedding.EventCounterMany(datasetsEmb)
    eventCounterSig = counter.EventCounter(datasetsSig)

    def isNotThis(name):
        return name != datasetName

    eventCounterEmb.removeColumns(
        filter(isNotThis, datasetsEmb.getAllDatasetNames()))
    eventCounterSig.removeColumns(
        filter(isNotThis, datasetsSig.getAllDatasetNames()))
    eventCounterSig.normalizeMCToLuminosity(lumi)

    effFormat = counter.TableFormatLaTeX(
        counter.CellFormatTeX(valueFormat="%.4f", withPrecision=2))

    tdEmb = treeDraw.clone(tree=tauAnalysisEmb + "/tree")
    tdSig = treeDraw.clone(tree=tauAnalysisSig + "/tree")
    selectionsCumulative = []
    tauSelectionsCumulative = []
    rowNames = []

    def sel(name, selection):
        selectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        eventCounterEmb.mainCounterAppendRow(name, tdEmb.clone(selection=sel))
        eventCounterSig.getMainCounter().appendRow(name,
                                                   tdSig.clone(selection=sel))
        rowNames.append(name)

    def tauSel(name, selection):
        tauSelectionsCumulative.append(selection)
        sel = selectionsCumulative[:]
        if len(tauSelectionsCumulative) > 0:
            sel += ["Sum$(%s) >= 1" % "&&".join(tauSelectionsCumulative)]
        sel = "&&".join(sel)
        eventCounterEmb.mainCounterAppendRow(name, tdEmb.clone(selection=sel))
        eventCounterSig.getMainCounter().appendRow(name,
                                                   tdSig.clone(selection=sel))
        rowNames.append(name)

    sel(">= 1 tau candidate", "Length$(taus_p4) >= 1")
    tauSel("Decay mode finding", tauEmbedding.tauNtuple.decayModeFinding)
    tauSel("pT > 15", "taus_p4.Pt() > 15")
    tauSel("pT > 40", tauEmbedding.tauNtuple.tauPtCut)
    tauSel("eta < 2.1", tauEmbedding.tauNtuple.tauEtaCut)
    tauSel("leading track pT > 20", tauEmbedding.tauNtuple.tauLeadPt)
    tauSel("ECAL fiducial", tauEmbedding.tauNtuple.ecalFiducial)
    tauSel("againstElectron", tauEmbedding.tauNtuple.electronRejection)
    tauSel("againstMuon", tauEmbedding.tauNtuple.muonRejection)
    tauSel("isolation", tauEmbedding.tauNtuple.tightIsolation)
    tauSel("oneProng", tauEmbedding.tauNtuple.oneProng)
    tauSel("Rtau", tauEmbedding.tauNtuple.rtau)
    sel("MET", tauEmbedding.tauNtuple.metSelection)

    table = counter.CounterTable()
    col = eventCounterEmb.getMainCounterTable().getColumn(name=datasetName)
    table.appendColumn(counter.efficiencyColumn("Embedded eff", col))
    col = eventCounterSig.getMainCounterTable().getColumn(name=datasetName)
    table.appendColumn(counter.efficiencyColumn("Normal eff", col))

    embeddingMuonIsolationEff = table.getCount(
        rowName="tauEmbeddingMuonsCount", colName="Embedded eff")
    embeddingTauIsolationEff = table.getCount(rowName="isolation",
                                              colName="Embedded eff")
    embeddingTotalIsolationEff = embeddingMuonIsolationEff.clone()
    embeddingTotalIsolationEff.multiply(embeddingTauIsolationEff)

    # Remove unnecessary rows
    del rowNames[0]
    table.keepOnlyRows(rowNames)
    rowIndex = table.getRowNames().index("isolation")
    table.insertRow(
        rowIndex,
        counter.CounterRow("Mu isolation (emb)",
                           ["Embedded eff", "Normal eff"],
                           [embeddingMuonIsolationEff, None]))
    table.insertRow(
        rowIndex + 1,
        counter.CounterRow("Tau isolation (emb)",
                           ["Embedded eff", "Normal eff"],
                           [embeddingTauIsolationEff, None]))
    table.setCount2(embeddingTotalIsolationEff,
                    rowName="isolation",
                    colName="Embedded eff")
    table.setCount2(None, rowName="pT > 15", colName="Normal eff")

    #print table.format(effFormat)
    fname = "counters_tau_" + datasetName + ".txt"
    f = open(fname, "w")
    f.write(table.format(effFormat))
    f.write("\n")
    f.close()
    print "Printed tau counters to", fname