Esempio n. 1
0
def printCounters(datasets):
    print "============================================================"
    print "Dataset info: "
    datasets.printInfo()

    eventCounter = counter.EventCounter(datasets)
    selection = "Sum$(%s) >= 1" % muonKinematics
    selection = "Sum$(%s && %s) >= 1" % (muonKinematics, muondB)
    selection = "Sum$(%s && %s && %s) >= 1" % (muonKinematics, muondB,
                                               muonIsolation)
    selection = "Sum$(%s && %s && %s) == 1" % (muonKinematics, muondB,
                                               muonIsolation)
    selection += "&&" + muonVeto
    selection += "&&" + electronVeto
    selection += "&&" + jetSelection
    eventCounter.getMainCounter().appendRow(
        "Selected control sample", treeDraw.clone(selection=selection))

    eventCounter.normalizeMCByLuminosity()

    table = eventCounter.getMainCounterTable()
    mcDatasets = filter(lambda n: n != "Data", table.getColumnNames())
    table.insertColumn(
        1,
        counter.sumColumn("MCSum",
                          [table.getColumn(name=name) for name in mcDatasets]))

    table.keepOnlyRows("Selected control sample")
    # reorder columns
    qcd = table.getColumn(name="QCD_Pt20_MuEnriched")
    table.removeColumn(table.getColumnNames().index("QCD_Pt20_MuEnriched"))
    table.insertColumn(5, qcd)

    table.transpose()

    # result = counter.CounterTable()
    # def addRow(name, value):
    #     result.appendRow(counter.CounterRow(name, ["Number of events"], [value]))
    # for name in ["Data", "MCSum", "WJets", "TTJets", "DYJetsToLL", "QCD_Pt20_MuEnriched", "SingleTop", "Diboson"]:
    #     addRow(name, table.getColumn(name=name).getCount(0))

    cellFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.1f'))
    print table.format(cellFormat)
Esempio n. 2
0
def doCounters(datasetsEmb):
    eventCounter = result.EventCounterMany(datasetsEmb,
                                           counters=analysisEmb + counters,
                                           scaleNormalization=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)
Esempio n. 3
0
def doCounters(datasetsEmb, datasetsSig):
    rows = ["njets", "MET", "btagging scale factor", "deltaPhiTauMET<160", "deltaPhiTauMET<130"]
    residuals = ["DYJetsToLL residual", "WW residual"]

    # Normal MC
    eventCounterNormal = counter.EventCounter(datasetsSig)
    eventCounterNormal.normalizeMCToLuminosity(datasetsEmb.getLuminosity())
    tableNormal = eventCounterNormal.getMainCounterTable()
    tableNormal.keepOnlyRows(rows)

    # Embedded data and MC, residual MC
    eventCounter = tauEmbedding.EventCounterResidual(datasetsEmb)
    table = eventCounter.getMainCounterTable()
    table.keepOnlyRows(rows)

    # Build the result
    result = counter.CounterTable()

    c = table.getColumn(name="Data")
    c.setName("Embedded data")
    result.appendColumn(c)
    #result.appendColumn(table.getColumn(name="EWKMC"))
    for name in residuals:
        result.appendColumn(table.getColumn(name=name))

    result.appendColumn(counter.sumColumn("Emb. data + res. MC", [table.getColumn(name=name) for name in ["Data"]+residuals]))
    result.appendColumn(counter.sumColumn("Emb. MC + res. MC", [table.getColumn(name=name) for name in ["EWKMC"]+residuals]))

    c = tableNormal.getColumn(name="EWKMC")
    c.setName("Normal MC")
    result.appendColumn(c)

    # Final formatting
    result.renameRows({"njets": "tau-jet identification",
                      "btagging scale factor": "b tagging"
                      })

    cellFormat = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat='%.4f', withPrecision=2))
    print result.format(cellFormat)
def doCountersOld(datasetsEmb, counterName="counter"):
    datasetNames = datasetsEmb.getAllDatasetNames()

    table = counter.CounterTable()
    for name in datasetNames:
        table.appendColumn(
            datasetsEmb.getCounter(
                name, analysisEmb + "Counters/weighted/" + counterName))

    ewkDatasets = ["WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson"]
    table.insertColumn(
        2,
        counter.sumColumn("EWKMCsum",
                          [table.getColumn(name=name)
                           for name in ewkDatasets]))

    print "============================================================"
    if isinstance(datasetsEmb, result.DatasetsDYCorrection):
        print "DY correction applied"
    cellFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.3f'))
    print table.format(cellFormat)
Esempio n. 5
0
def doCounters(datasets):
    # Create EventCounter object, holds all counters of all datasets
    eventCounter = counter.EventCounter(datasets)

    # Normalize counters
    if mcOnly:
        eventCounter.normalizeMCToLuminosity(mcOnlyLumi)
    else:
        eventCounter.normalizeMCByLuminosity()

    # Create LaTeX format, automatically adjust value precision by uncertainty
    latexFormat = counter.TableFormatLaTeX(
        counter.CellFormatTeX(valueFormat="%.4f", withPrecision=2))
    plainFormat = counter.TableFormatText(
        counter.CellFormatText(valueOnly=True))

    #table = eventCounter.getMainCounterTable()
    #print table.format()

    table = eventCounter.getSubCounterTable("FullHiggsMassCalculator")
    #table.renameRows(counterLabels)
    print table.format(latexFormat)
def doCountersResidual(datasetsResidual):
    eventCounter = result.EventCounterResidual(datasetsResidual,
                                               counters=analysisEmb + counters)

    mainTable = eventCounter.getMainCounterTable()

    names = ["Data", "DYJetsToLL residual", "WW residual"]
    mainTable.insertColumn(
        1,
        counter.sumColumn("Data+residual",
                          [mainTable.getColumn(name=name) for name in names]))

    if "EWKMC" in datasetsResidual.getAllDatasetNames():
        names = ["EWKMC", "DYJetsToLL residual", "WW residual"]
        mainTable.insertColumn(
            3,
            counter.sumColumn(
                "EWKMC+residual",
                [mainTable.getColumn(name=name) for name in names]))

    cellFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.3f'))
    print mainTable.format(cellFormat)
def printCountersOld(datasets, datasetsMC, analysisPrefix, normalizeToLumi=None):
    print "============================================================"
    print "Dataset info: "
    datasets.printInfo()

    eventCounter = makeEventCounter(datasets)
    if normalizeToLumi == None:
        eventCounter.normalizeMCByLuminosity()
    else:
        eventCounter.normalizeMCToLuminosity(normalizeToLumi)
    
    mainCounterMap = {
        "allEvents": "All events",
        "passedTrigger": "Triggered",
        "passedScrapingVeto": "Scaping veto",
        "passedHBHENoiseFilter": "HBHE noise filter",
        "passedPrimaryVertexFilter": "PV filter",
        analysisPrefix+"countAll": "All events",
        analysisPrefix+"countTrigger": "Triggered",
        analysisPrefix+"countPrimaryVertex": "Good primary vertex",
        analysisPrefix+"countGlobalTrackerMuon": "Global \& tracker muon",
        analysisPrefix+"countMuonKin": "Muon \pT, $\eta$ cuts",
        analysisPrefix+"countMuonQuality": "Muon quality cuts",
        analysisPrefix+"countMuonIP": "Muon transverse IP",
        analysisPrefix+"countMuonVertexDiff": "Muon dz",
        analysisPrefix+"countJetMultiplicityCut": "Njets",
        analysisPrefix+"countMETCut": "MET cut"
        }
    
    latexFormat = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat="%.0f"))
    latexFormat2 = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat="%.1f"))
    #latexFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat="%.0f", valueOnly=True))
    
    print "============================================================"
    print "Main counter (%s)" % eventCounter.getNormalizationString()
    #eventCounter.getMainCounter().printCounter()
    table = eventCounter.getMainCounterTable()

#    addSumColumn(table)
#    addTtwFractionColumn(table)
#    addPurityColumn(table)
#    addDyFractionColumn(table)
#    addQcdFractionColumn(table)

#    reorderCounterTable(table)

#    print table.format()
    print table.format(latexFormat)
#    print table.format(latexFormat2)
    return
    
    #print "------------------------------------------------------------"
    #print counterEfficiency(eventCounter.getMainCounterTable()).format(FloatDecimalFormat(4))
    
    # mainTable = eventCounter.getMainCounterTable()
    # effTable = counterEfficiency(mainTable)
    # for icol in xrange(0, effTable.getNcolumns()):
    #     column = effTable.getColumn(icol)
    #     column.setName(column.getName()+" eff")
    #     mainTable.insertColumn(icol*2+1, column)
    
    # print "------------------------------------------------------------"
    # printCounter(mainTable, FloatDecimalFormat(4))
    
    
    eventCounter = makeEventCounter(datasetsMC)
    print "============================================================"
    print "Main counter (%s)" % eventCounter.getNormalizationString()
    print eventCounter.getMainCounterTable().format(counter.TableFormatText(counter.CellFormatText(valueOnly=True, valueFormat="%.0f")))
    
    
    # Make the Data column entries comparable to the MC
    table.renameRows(mainCounterMap)
    dataCol = table.getColumn(0)
    table.removeColumn(0)
    dataCol.removeRow(2) # scraping
    dataCol.removeRow(2) # HBHE
    dataCol.removeRow(2) # pv filter
    dataCol.removeRow(2) # all events
    dataCol.removeRow(2) # triggered
    table.insertColumn(0, dataCol)
    addDataMcRatioColumn(table)
    
    # LaTeX tables for note
    latexFormat.setColumnFormat(counter.CellFormatTeX(valueFormat="%.3f"), name="Data/MCsum")
    latexFormat.setColumnFormat(counter.CellFormatTeX(valueFormat="%.1f"), name="SingleTop")
    
    tableDataMc = counter.CounterTable()
    tableDataMc.appendColumn(table.getColumn(name="Data"))
    tableDataMc.appendColumn(table.getColumn(name="MCsum"))
    tableDataMc.appendColumn(table.getColumn(name="Data/MCsum"))
    print tableDataMc.format(latexFormat)
    
    tableMc = counter.CounterTable()
    #tableMc.appendColumn(table.getColumn(name="MCsum"))
    for mcName in datasets.getMCDatasetNames():
        tableMc.appendColumn(table.getColumn(name=mcName))
    print tableMc.format(latexFormat)
    
    tableRatio = counter.CounterTable()
    for cname in ["TTJets/(TTJets+WJets)", "Purity", "QCD/MCsum", "DY/MCsum"]:
        tableRatio.appendColumn(table.getColumn(name=cname))
        latexFormat.setColumnFormat(counter.CellFormatTeX(valueFormat="%.2f", valueOnly=True), name=cname)
    print tableRatio.format(latexFormat)
Esempio n. 8
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
Esempio n. 9
0
    ttColumn = table.indexColumn("TTJets")
    wColumn = table.indexColumn("WJets")
    return counter.sumColumn("TTJets+WJets",
                             [table.getColumn(i) for i in [ttColumn, wColumn]])


def addPurityColumn(table):
    mcSumColumn = table.indexColumn("MCsum")
    purity = counter.divideColumn("TT+W purity", ttwSum(table),
                                  table.getColumn(mcSumColumn))
    purity.multiply(100)  # -> %
    table.appendColumn(purity)


latexFormat = counter.TableFormatConTeXtTABLE(
    counter.CellFormatTeX(valueFormat="%.1f", valueOnly=True))
latexFormat.setColumnFormat(counter.CellFormatTeX(valueFormat="%.2f",
                                                  valueOnly=True),
                            name="TTJets/(TTJets+WJets)")

expectedTable = ecExpected.getMainCounterTable()
observedTable = tmp.getMainCounterTable()

for table in [expectedTable, observedTable]:
    addTtwFractionColumn(table)

    table.renameRows(mainCounterMap)

    print table.format(latexFormat)

observedTable = tmp.getMainCounterTable()
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(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 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()
Esempio n. 13
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(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
Esempio n. 15
0
    fraction = counter.divideColumn("TTJets/(TTJets+WJets)", table.getColumn(ttColumn), ttwSum(table))
    fraction.multiply(100) # -> %
    table.appendColumn(fraction)

def ttwSum(table):
    ttColumn = table.indexColumn("TTJets")
    wColumn = table.indexColumn("WJets")
    return counter.sumColumn("TTJets+WJets", [table.getColumn(i) for i in [ttColumn, wColumn]])

def addPurityColumn(table):
    mcSumColumn = table.indexColumn("MCsum")
    purity = counter.divideColumn("TT+W purity", ttwSum(table), table.getColumn(mcSumColumn))
    purity.multiply(100) # -> %
    table.appendColumn(purity)

latexFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat="%.1f", valueOnly=True))
latexFormat.setColumnFormat(counter.CellFormatTeX(valueFormat="%.2f", valueOnly=True), name="TTJets/(TTJets+WJets)")

expectedTable = ecExpected.getMainCounterTable()
observedTable = tmp.getMainCounterTable()

for table in [expectedTable, observedTable]:
    addTtwFractionColumn(table)

    table.renameRows(mainCounterMap)

    print table.format(latexFormat)

observedTable = tmp.getMainCounterTable()
addSumColumn(observedTable)
addPurityColumn(observedTable)
Esempio n. 16
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()
    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()
Esempio n. 18
0
def doCounters(datasets, MyCuts, MyCutsName):

    ### Define the counters to be used
    #eventCounter = counter.EventCounter(datasets, counters=myAnalysis + myDataEra + "/counters")
    eventCounter = counter.EventCounter(datasets)

    ### Normalise the MC sample to a luminosity before creating a table
    eventCounter.normalizeMCToLuminosity(GetLumi(datasets))

    myRowNames = []
    for iCut, iCutName in zip(MyCuts, MyCutsName):
        #print "*** Cut: %s\n*** CutName: %s" % (iCut, iCutName)
        print "*** Processing TCut with:\n    Name = \"%s\" \n    Expr = \"%s\"" % (
            iCutName, iCut)

        ### Define the event "weight" to be used
        EvtWeight = GetEventWeight(iCut)

        ### Define the TTree to be used
        treePath = "tree"  # treePath = myAnalysis+"/tree"
        treeDraw = dataset.TreeDraw(treePath, weight=EvtWeight, selection=iCut)

        ### Append custom rows to the event counter. An asterisk denotes that the counter row was added. Informative and makes things easier
        myRowName = iCutName  #"*" + iCutName
        myRowNames.append(myRowName)
        eventCounter.getMainCounter().appendRow(myRowName, treeDraw)

    ### Get table with all default rows removed and manage the table format
    myTable = GetCustomTable(eventCounter, myRowNames)

    if getBool("bMergeEwk"):
        DataMinusEwkMc = counter.subtractColumn(
            "DataMinusEwkMc", myTable.getColumn(name="Data"),
            myTable.getColumn(name="EWK MC"))
        QcdPurity = counter.divideColumn("QCD Purity", DataMinusEwkMc,
                                         myTable.getColumn(name="Data"))
        myTable.appendColumn(QcdPurity)

    ### See http://docs.python.org/2/library/string.html for string format
    cellTextFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True))  #%.2e
    cellLaTeXFormat = counter.TableFormatLaTeX(
        counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True))  #%.2e
    purityFormat = counter.CellFormatTeX(valueFormat='%.2f', valueOnly=True)

    ### Customise the "QCD Purity" column
    cellTextFormat.setColumnFormat(purityFormat,
                                   name="QCD Purity")  #does nothing
    cellLaTeXFormat.setColumnFormat(purityFormat,
                                    name="QCD Purity")  #does nothing

    # between construction of table format and table format

    ### Print the final table with the desired format
    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellTextFormat)

    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellLaTeXFormat)

    return
Esempio n. 19
0
def main():
    dirEmbs = ["."] + [os.path.join("..", d) for d in tauEmbedding.dirEmbs[1:]]
#    dirEmbs = dirEmbs[:2]

    tauEmbedding.normalize=True
    tauEmbedding.era = "Run2011A"
 
    table = counter.CounterTable()
    for i in xrange(len(dirEmbs)):
        tmp = dirEmbs[:]
        del tmp[i]
        row = doCounters(tmp)
        row.setName("Removed embedding %d"%i)
        table.appendRow(row)

    arows = []
    arows.append(counter.meanRow(table))
    arows.append(counter.maxRow(table))
    arows.append(counter.minRow(table))
    arows.append(counter.subtractRow("Max-mean", arows[1], arows[0]))
    arows.append(counter.subtractRow("Mean-min", arows[0], arows[2]))
    for r in arows:
        table.appendRow(r)

    cellFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.3f'))
    print "DeltaPhi < 160"
    print
    print table.format(cellFormat)
    print
    print

    # Format the table as in AN
    ftable = counter.CounterTable()
    def addRow(name):
        col = table.getColumn(name=name)

        minimum = col.getCount(name="Min")
        maximum = col.getCount(name="Max")

        # Maximum deviation from average
        dev1 = col.getCount(name="Max-mean")
        dev2 = col.getCount(name="Mean-min")
        if dev2.value() > dev1.value():
            dev1 = dev2

        dev1.divide(col.getCount(name="Mean"))
        dev1.multiply(dataset.Count(100))

        ftable.appendRow(counter.CounterRow(name,
                                            ["Minimum", "Maximum", "Largest deviation from average (%)"],
                                            [minimum, maximum, dev1]))

    addRow("Data")
    addRow("EWKMCsum")
    addRow("TTJets")
    addRow("WJets")
    addRow("DYJetsToLL")
    addRow("SingleTop")
    addRow("Diboson")

    cellFormat2 = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat="%.4f", withPrecision=2))
    cellFormat2.setColumnFormat(counter.CellFormatTeX(valueFormat="%.1f", valueOnly=True), index=2)
    print ftable.format(cellFormat2)
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
Esempio n. 21
0
print "Main counter (MC normalized by collision data luminosity)"
print eventCounter.getMainCounterTable().format()

# Example how to print all subcounter names
for subCounterName in eventCounter.getSubCounterNames():
    print "============================================================"
    print "Subcounter %s (MC normalized by collision data luminosity)" % subCounterName
    print eventCounter.getSubCounterTable(subCounterName).format()

print "============================================================"
print "Main counter (examples of the same table)"

# Change the value format (printf style)
print eventCounter.getMainCounterTable().format(
    counter.TableFormatText(counter.CellFormatText(valueFormat="%.1f")))

# No uncertainties
print eventCounter.getMainCounterTable().format(
    counter.TableFormatText(
        counter.CellFormatText(valueFormat="%.0e", valueOnly=True)))

# LaTeX table (tabular), default format
print eventCounter.getMainCounterTable().format(counter.TableFormatLaTeX())

# LaTeX table, change value and uncertainty formats
print eventCounter.getMainCounterTable().format(
    counter.TableFormatLaTeX(
        counter.CellFormatTeX(valueFormat="%.2e",
                              uncertaintyFormat="%.1e",
                              uncertaintyPrecision=1)))
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)
Esempio n. 23
0
eventCounter.normalizeMCToLuminosity(33.69)

# Example how to print the main counter with the default formatting
print "============================================================"
print "Main counter (MC normalized by collision data luminosity)"
print eventCounter.getMainCounterTable().format()

# Example how to print all subcounter names
for subCounterName in eventCounter.getSubCounterNames():
    print "============================================================"
    print "Subcounter %s (MC normalized by collision data luminosity)" % subCounterName
    print eventCounter.getSubCounterTable(subCounterName).format()


print "============================================================"
print "Main counter (examples of the same table)"

# Change the value format (printf style)
print eventCounter.getMainCounterTable().format(counter.TableFormatText(counter.CellFormatText(valueFormat="%.1f")))

# No uncertainties
print eventCounter.getMainCounterTable().format(counter.TableFormatText(counter.CellFormatText(valueFormat="%.0e", valueOnly=True)))

# LaTeX table (tabular), default format
print eventCounter.getMainCounterTable().format(counter.TableFormatLaTeX())

# LaTeX table, change value and uncertainty formats
print eventCounter.getMainCounterTable().format(counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat="%.2e", uncertaintyFormat="%.1e", uncertaintyPrecision=1)))


Esempio n. 24
0
def main():
    dirEmbs = result.dirEmbs[:]
    if onlyWjets:
        dirEmbs.extend(dirEmbsWjets)
    dirEmbs = ["."] + [os.path.join("..", d) for d in dirEmbs[1:]]
    #    dirEmbs = dirEmbs[0:2]

    style = tdrstyle.TDRStyle()

    tauEmbedding.normalize = normalize
    tauEmbedding.era = "Run2011A"

    ts = dataset.TreeScan(analysisEmb + "/tree",
                          function=None,
                          selection=And(metCut, bTaggingCut, deltaPhi160Cut))

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

    table = counter.CounterTable()

    for i, d in enumerate(dirEmbs):
        datasets = dataset.getDatasetsFromMulticrabCfg(
            cfgfile=d + "/multicrab.cfg", counters=analysisEmb + "Counters")
        if onlyWjets:
            datasets.remove(
                filter(lambda n: n != "WJets_TuneZ2_Summer11",
                       datasets.getAllDatasetNames()))
        else:
            if mcEvents:
                datasets.remove(
                    filter(
                        lambda n: n != "WJets_TuneZ2_Summer11" and n !=
                        "TTJets_TuneZ2_Summer11" and not "SingleMu" in n,
                        datasets.getAllDatasetNames()))
            datasets.loadLuminosities()
        datasets.remove(
            filter(lambda name: "HplusTB" in name,
                   datasets.getAllDatasetNames()))
        datasets.remove(
            filter(lambda name: "TTToHplus" in name,
                   datasets.getAllDatasetNames()))
        tauEmbedding.updateAllEventsToWeighted(datasets)
        plots.mergeRenameReorderForDataMC(datasets)

        # for ds in datasets.getAllDatasets():
        #     f = open("pickEvents_%s_%d.txt" % (ds.getName(), i), "w")
        #     ds.getDatasetRootHisto(ts.clone(function=lambda tree: printPickEvent(f, tree)))
        #     f.close()

        row = doCounters(datasets)
        row.setName("Embedding %d" % i)
        table.appendRow(row)

    doPlots(table)

    arows = []
    arows.append(counter.meanRow(table))
    arows.extend(counter.meanRowFit(table))
    arows.append(counter.maxRow(table))
    arows.append(counter.minRow(table))
    for r in arows:
        table.appendRow(r)

#    csvSplitter = counter.TableSplitter([" \pm "])
#    cellFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.3f'), columnSeparator=",")
    cellFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.3f'))
    print "DeltaPhi < 160"
    print
    print table.format(cellFormat)
Esempio n. 25
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 do(onlyWjets, mcEvents, normalize, formatCounters, formatPlots):
    dirEmbs = tauEmbedding.dirEmbs[:]
    if onlyWjets:
        dirEmbs.extend(dirEmbsWjets)
    dirEmbs = ["."] + [os.path.join("..", d) for d in dirEmbs[1:]]
#    dirEmbs = dirEmbs[0:2]

    # Read luminosity
    datasets = dataset.getDatasetsFromMulticrabCfg(cfgfile=dirEmbs[0]+"/multicrab.cfg", counters=analysisEmb+"Counters", weightedCounters=False)
    datasets.loadLuminosities()
    plots.mergeRenameReorderForDataMC(datasets)
    lumi = datasets.getDataset("Data").getLuminosity()


    style = tdrstyle.TDRStyle()
    histograms.cmsTextMode = histograms.CMSMode.SIMULATION
    histograms.cmsText[histograms.cmsTextMode] = "Simulation"

    tauEmbedding.normalize=normalize
    tauEmbedding.era = "Run2011A"

    table = counter.CounterTable()
    for i, d in enumerate(dirEmbs):
        datasets = dataset.getDatasetsFromMulticrabCfg(cfgfile=d+"/multicrab.cfg", counters=analysisEmb+"Counters", weightedCounters=False)
        if onlyWjets:
            datasets.remove(filter(lambda n: n != "WJets_TuneZ2_Summer11", datasets.getAllDatasetNames()))
        else:
            if mcEvents:
                datasets.remove(filter(lambda n: n != "WJets_TuneZ2_Summer11" and n != "TTJets_TuneZ2_Summer11" and not "SingleMu" in n, datasets.getAllDatasetNames()))
            datasets.loadLuminosities()
        datasets.remove(filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
        datasets.remove(filter(lambda name: "TTToHplus" in name, datasets.getAllDatasetNames()))
        tauEmbedding.updateAllEventsToWeighted(datasets)
        plots.mergeRenameReorderForDataMC(datasets)

        row = doCounters(datasets, onlyWjets, mcEvents, normalize, lumi)
        row.setName("Embedding %d" % i)
        table.appendRow(row)

    if formatPlots:
        doPlots(table, onlyWjets, mcEvents, normalize, lumi)

    if not formatCounters:
        return

    arows = []
    arows.append(counter.meanRow(table))
    arows.extend(counter.meanRowFit(table))
    arows.append(counter.maxRow(table))
    arows.append(counter.minRow(table))
    for r in arows:
        table.appendRow(r)

    print table.format()

    ftable = counter.CounterTable()
    def addRow(name):
        col = table.getColumn(name=name)

        minimum = col.getCount(name="Min")
        maximum = col.getCount(name="Max")
        mean = col.getCount(name="Mean")

        ftable.appendRow(counter.CounterRow(name,
                                            ["Mean", "Minimum", "Maximum"],
                                            [mean, minimum, maximum]))
    addRow("Data")
    addRow("EWKMCsum")
    addRow("TTJets")
    addRow("WJets")
    addRow("DYJetsToLL")
    addRow("SingleTop")
    addRow("Diboson")

    cellFormat2 = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat="%.4f", withPrecision=2))
    print ftable.format(cellFormat2)
Esempio n. 27
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