示例#1
0
def doCounters(datasetsEmb):
    eventCounter = tauEmbedding.EventCounterMany(datasetsEmb, normalize=True)

    #row = "btagging scale factor"
    row = "deltaPhiTauMET<160"
    #row = "deltaPhiTauMET<130"
    table = eventCounter.getMainCounterTable()
    table.keepOnlyRows([row])

    result = counter.CounterTable()
    def addRow(name, newktt, nsignal):
        fraction = None
        if nsignal != None:
            fraction = nsignal.clone()
            total = nsignal.clone()
            total.add(newktt)
            fraction.divide(total)
            fraction.multiply(dataset.Count(100))
        result.appendRow(counter.CounterRow(name, ["EWK+tt events", "Signal events", "Signal fraction (\%)"], [newktt, nsignal, fraction]))
    addRow("No signal", table.getCount(irow=0, colName="EWKMC"), None)
    ewkWithSignal = table.getCount(irow=0, colName="EWKScaled")
    for mass in [80, 90, 100, 120, 140, 150, 155, 160]:
        addRow("H+ M%d"%mass, ewkWithSignal, table.getCount(irow=0, colName="TTToHplus_M%d"%mass))

    #cellFormat = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat='%.3f'))
    cellFormat = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat='%.4f', withPrecision=2))
    print result.format(cellFormat)
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)

    tableEmb = eventCounterEmb.getMainCounterTable()
    tableSig = eventCounterSig.getMainCounterTable()

    table = counter.CounterTable()
    col = tableEmb.getColumn(name=datasetName)
    col.setName(datasetName+" emb")
    table.appendColumn(col)
    col = tableSig.getColumn(name=datasetName)
    col.setName(datasetName+" norm")
    table.appendColumn(col)

    table.keepOnlyRows([
            "njets",
            "MET",
            "btagging",
            "btagging scale factor",
            "deltaPhiTauMET<160",
            "deltaPhiTauMET<130",
            ])
    table.renameRows({"njets": "tau ID"})

    return table
示例#3
0
def doCounters(dirEmbs):
    datasetsEmb = tauEmbedding.DatasetsMany(dirEmbs, analysisEmb+"Counters", normalizeMCByLuminosity=True)
    datasetsEmb.forEach(plots.mergeRenameReorderForDataMC)
    datasetsEmb.setLumiFromData()

    datasetsEmb.remove(filter(lambda name: "HplusTB" in name, datasetsEmb.getAllDatasetNames()))
    datasetsEmb.remove(filter(lambda name: "TTToHplus" in name, datasetsEmb.getAllDatasetNames()))

    eventCounter = tauEmbedding.EventCounterMany(datasetsEmb)

    mainTable = eventCounter.getMainCounterTable()

    ewkDatasets = ["WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson"]
    def ewkSum(table):
        table.insertColumn(1, counter.sumColumn("EWKMCsum", [table.getColumn(name=name) for name in ewkDatasets]))
    ewkSum(mainTable)

    datasetsEmb.close()
    return mainTable.getRow(name="deltaPhiTauMET<160")
示例#4
0
def doCounters(datasetsEmb):
    eventCounter = tauEmbedding.EventCounterMany(datasetsEmb,
                                                 counters=analysisEmb +
                                                 counters,
                                                 normalize=True)

    mainTable = eventCounter.getMainCounterTable()

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

    def ewkSum(table):
        table.insertColumn(
            1,
            counter.sumColumn(
                "EWKMCsum",
                [table.getColumn(name=name) for name in ewkDatasets]))

    ewkSum(mainTable)
    cellFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.3f'))
    print mainTable.format(cellFormat)
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 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
示例#7
0
def doCounters(datasetsEmb):

    # All embedded events
    eventCounterAll = counter.EventCounter(
        datasetsEmb.getFirstDatasetManager(),
        counters=analysisEmbAll + counters)
    eventCounterAll.normalizeMCByLuminosity()
    tableAll = eventCounterAll.getMainCounterTable()
    tableAll.keepOnlyRows([
        "All events",
    ])
    tableAll.renameRows({"All events": "All embedded events"})

    # Mu eff + Wtau mu
    eventCounterMuEff = counter.EventCounter(
        datasetsEmb.getFirstDatasetManager(),
        counters=analysisEmbNoTauEff + counters)
    eventCounterMuEff.normalizeMCByLuminosity()
    tauEmbedding.scaleNormalization(eventCounterMuEff)
    tableMuEff = eventCounterMuEff.getMainCounterTable()
    tableMuEff.keepOnlyRows(["All events"])
    tableMuEff.renameRows({"All events": "mu eff + Wtaumu"})

    # Event counts after embedding normalization, before tau trigger eff,
    # switch to calculate uncertainties of the mean of 10 trials
    eventCounterNoTauEff = tauEmbedding.EventCounterMany(
        datasetsEmb, counters=analysisEmbNoTauEff + counters)
    tableNoTauEff = eventCounterNoTauEff.getMainCounterTable()
    tableNoTauEff.keepOnlyRows([
        "Trigger and HLT_MET cut",
        "njets",
    ])
    tableNoTauEff.renameRows({
        "Trigger and HLT_MET cut": "caloMET > 60",
        "njets": "tau ID"
    })

    # Event counts after tau trigger eff
    eventCounter = tauEmbedding.EventCounterMany(datasetsEmb,
                                                 counters=analysisEmb +
                                                 counters)
    table = eventCounter.getMainCounterTable()
    table.keepOnlyRows([
        "njets", "MET", "btagging scale factor", "deltaPhiTauMET<160",
        "deltaPhiTauMET<130"
    ])
    table.renameRows({
        "njets": "Tau trigger efficiency",
        "btagging scale factor": "b tagging"
    })

    # Combine the rows to one table
    result = counter.CounterTable()
    for tbl in [tableAll, tableMuEff, tableNoTauEff, table]:
        for iRow in xrange(tbl.getNrows()):
            result.appendRow(tbl.getRow(index=iRow))

    addMcSum(result)
    cellFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.4f', withPrecision=2))

    print result.format(cellFormat)
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()
示例#9
0
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 = tauEmbedding.EventCounterMany(
            datasetsEmb,
            counters=analysisEmb + counters,
            normalize=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 = 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
示例#11
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
示例#12
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