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]))
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 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 _correctColumn(self, table, name, correction): columnNames = table.getColumnNames() i = columnNames.index(name) col = table.getColumn(index=i) table.removeColumn(i) col = counter.sumColumn(name, [col, correction]) table.insertColumn(i, col)
def addSumColumn(table): #dataColumn = table.indexColumn("Data") #indices = filter(lambda x: x != dataColumn, xrange(0, table.getNcolumns())) #columns = [table.getColumn(i) for i in indices] columns = [table.getColumn(i) for i in xrange(0, table.getNcolumns())] table.insertColumn(1, counter.sumColumn("MCsum", columns))
def doCounters(datasets): eventCounter = counter.EventCounter(datasets) # append row from the tree to the main counter eventCounter.getMainCounter().appendRow( "MET > 70", treeDraw.clone(selection="met_p4.Et() > 70")) ewkDatasets = ["WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson"] eventCounter.normalizeMCByLuminosity() # eventCounter.normalizeMCToLuminosity(73) print "============================================================" print "Main counter (MC normalized by collision data luminosity)" mainTable = eventCounter.getMainCounterTable() mainTable.insertColumn( 2, counter.sumColumn( "EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets])) print mainTable.format() # print eventCounter.getSubCounterTable("GlobalMuon_ID").format() print eventCounter.getSubCounterTable("tauIDTauSelection").format() print eventCounter.getSubCounterTable( "TauIDPassedEvt::tauID_HPSTight").format() # print eventCounter.getSubCounterTable("TauIDPassedJets::tauID_HPSTight").format() print eventCounter.getSubCounterTable("b-tagging").format() print eventCounter.getSubCounterTable("Jet selection").format() print eventCounter.getSubCounterTable("Jet main").format()
def doCounters(datasets): eventCounter = counter.EventCounter(datasets) # append row from the tree to the main counter eventCounter.getMainCounter().appendRow("MET > 70", treeDraw.clone(selection="met_p4.Et() > 70")) ewkDatasets = [ "WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson" ] eventCounter.normalizeMCByLuminosity() # eventCounter.normalizeMCToLuminosity(73) print "============================================================" print "Main counter (MC normalized by collision data luminosity)" mainTable = eventCounter.getMainCounterTable() mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets])) print mainTable.format() # print eventCounter.getSubCounterTable("GlobalMuon_ID").format() print eventCounter.getSubCounterTable("tauIDTauSelection").format() print eventCounter.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight").format() # print eventCounter.getSubCounterTable("TauIDPassedJets::tauID_HPSTight").format() print eventCounter.getSubCounterTable("b-tagging").format() print eventCounter.getSubCounterTable("Jet selection").format() print eventCounter.getSubCounterTable("Jet main").format()
def printCounters(datasets, selectionName, ntupleCache, selectorName, onlyDataset=None): global printed if not printed: print "============================================================" print "Dataset info: " datasets.printInfo() printed = True if not doWeighted: eventCounter = counter.EventCounter(datasets, counters=counters) counterPath = "counters/counter" else: eventCounter = counter.EventCounter(datasets) counterPath = "counters/weighted/counter" if onlyDataset != None: eventCounter.removeColumns( filter(lambda n: n != onlyDataset, datasets.getAllDatasetNames())) eventCounter.getMainCounter().appendRows( ntupleCache.histogram(counterPath, selectorName)) if mergeMC: if mcOnly: eventCounter.normalizeMCToLuminosity(mcLuminosity) else: eventCounter.normalizeMCByLuminosity() table = eventCounter.getMainCounterTable() mcDatasets = filter(lambda n: n != "Data", table.getColumnNames()) if len(mcDatasets) != 0: col = 1 if mcOnly: col = 0 table.insertColumn( col, counter.sumColumn( "MCSum", [table.getColumn(name=name) for name in mcDatasets])) cellFormat = counter.TableFormatText( counter.CellFormatText(valueFormat='%.3f')) # cellFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.1f')) output = table.format(cellFormat) print print "########################################" print "Selection", selectionName print output prefix = era + "_" + selectionName + "_counters" if not doWeighted: prefix += "_nonweighted" f = open(prefix + ".txt", "w") f.write(output) f.close()
def addMcSum(t): allDatasets = [ "QCD_Pt20_MuEnriched", "WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson" ] t.insertColumn( 1, counter.sumColumn("MCSum", [t.getColumn(name=name) for name in allDatasets]))
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 doCounters(myDsetMgr, mySuffix, isSystematicVariation): eventCounter = counter.EventCounter(myDsetMgr) # append row from the tree to the main counter # eventCounter.getMainCounter().appendRow("MET > 70", treeDraw.clone(selection="met_p4.Et() > 70")) ewkDatasets = [ "WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson" ] if myDsetMgr.hasDataset("W1Jets"): ewkDatasets.extend(["W1Jets", "W2Jets", "W3Jets", "W4Jets"]) if mcOnly: eventCounter.normalizeMCToLuminosity(mcOnlyLumi) else: eventCounter.normalizeMCByLuminosity() print "============================================================" print mySuffix print "============================================================" out = open(os.path.join(mySuffix, "counters.txt"), "w") def printAndSave(line): print line out.write(line) out.write("\n") printAndSave("Main counter (MC normalized by collision data luminosity)") mainTable = eventCounter.getMainCounterTable() mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets])) # Default # cellFormat = counter.TableFormatText() # No uncertainties cellFormat = counter.TableFormatText(cellFormat=counter.CellFormatText(valueOnly=False)) printAndSave(mainTable.format(cellFormat)) if not isSystematicVariation: # printAndSave(eventCounter.getSubCounterTable("tauIDTauSelection").format()) printAndSave(eventCounter.getSubCounterTable("TauIDPassedEvt::TauSelection_HPS").format(cellFormat)) printAndSave(eventCounter.getSubCounterTable("TauIDPassedJets::TauSelection_HPS").format(cellFormat)) printAndSave(eventCounter.getSubCounterTable("b-tagging").format(cellFormat)) printAndSave(eventCounter.getSubCounterTable("Jet selection").format(cellFormat)) printAndSave(eventCounter.getSubCounterTable("Jet main").format(cellFormat)) printAndSave(eventCounter.getSubCounterTable("VetoTauSelection").format(cellFormat)) printAndSave(eventCounter.getSubCounterTable("MuonSelection").format(cellFormat)) printAndSave(eventCounter.getSubCounterTable("MCinfo for selected events").format(cellFormat)) printAndSave(eventCounter.getSubCounterTable("ElectronSelection").format(cellFormat)) # printAndSave(eventCounter.getSubCounterTable("top").format(cellFormat)) out.close()
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 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)
def doCounters(myDsetMgr, mySuffix): eventCounter = counter.EventCounter(myDsetMgr) # append row from the tree to the main counter # eventCounter.getMainCounter().appendRow("MET > 70", treeDraw.clone(selection="met_p4.Et() > 70")) ewkDatasets = [ "WJets", "W1Jets", "W2Jets", "W3Jets", "W4Jets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson" ] if mcOnly: eventCounter.normalizeMCToLuminosity(mcOnlyLumi) else: eventCounter.normalizeMCByLuminosity() print "============================================================" print mySuffix print "============================================================" print "Main counter (MC normalized by collision data luminosity)" mainTable = eventCounter.getMainCounterTable() mainTable.insertColumn( 2, counter.sumColumn( "EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets])) # Default # cellFormat = counter.TableFormatText() # No uncertainties cellFormat = counter.TableFormatText(cellFormat=counter.CellFormatText( valueOnly=False)) print mainTable.format(cellFormat) # print eventCounter.getSubCounterTable("tauIDTauSelection").format() print eventCounter.getSubCounterTable( "TauIDPassedEvt::TauSelection_HPS").format(cellFormat) print eventCounter.getSubCounterTable( "TauIDPassedJets::TauSelection_HPS").format(cellFormat) print eventCounter.getSubCounterTable("b-tagging").format(cellFormat) print eventCounter.getSubCounterTable("Jet selection").format(cellFormat) print eventCounter.getSubCounterTable("Jet main").format(cellFormat) print eventCounter.getSubCounterTable("VetoTauSelection").format( cellFormat) print eventCounter.getSubCounterTable("MuonSelection").format(cellFormat) print eventCounter.getSubCounterTable("MCinfo for selected events").format( cellFormat) print eventCounter.getSubCounterTable("ElectronSelection").format( cellFormat)
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)
def printCounters(datasets): eventCounter = counter.EventCounter(datasets) eventCounter.normalizeMCByLuminosity() ewkDatasets = [ "WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson" ] print "============================================================" print "Main counter (MC normalized by collision data luminosity)" mainTable = eventCounter.getMainCounterTable() mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets])) # Default # cellFormat = counter.TableFormatText() # No uncertainties cellFormat = counter.TableFormatText(cellFormat=counter.CellFormatText(valueOnly=True)) print mainTable.format(cellFormat) print eventCounter.getSubCounterTable("MCinfo for selected events").format(cellFormat)
def getCounter(self, datasetName, name): if not datasetName in ["Data", "EWKMC", "DYJetsToLL"]: (embDataHisto, tmp) = self.datasetsEmb.getHistogram(datasetName, name) return counter.HistoCounter(datasetName, embDataHisto) # Ugly hack sigName = name if isinstance(sigName, basestring): sigName = sigName.replace(self.analysisEmb, self.analysisSig) else: sigName = sigName.clone( tree=sigName.tree.replace(self.analysisEmb, self.analysisSig)) # Get properly normalized embedded data, embedded DY and normal DY histograms (embDataHisto, tmp) = self.datasetsEmb.getHistogram(datasetName, name) (embDyHisto, tmp) = self.datasetsEmb.getHistogram("DYJetsToLL", name) sigDyHisto = self.datasetsSig.getDataset( "DYJetsToLL").getDatasetRootHisto(sigName) # DatasetRootHisto sigDyHisto.normalizeToLuminosity(self.datasetsEmb.getLuminosity()) sigDyHisto = sigDyHisto.getHistogram() # ROOT.TH1 embDataCounter = counter.HistoCounter("EmbData", embDataHisto) embDyCounter = counter.HistoCounter("EmbDy", embDyHisto) sigDyCounter = counter.HistoCounter("SigDy", sigDyHisto) table = counter.CounterTable() table.appendColumn(embDataCounter) table.appendColumn(embDyCounter) table.appendColumn(sigDyCounter) table.removeNonFullRows() column = table.getColumn(name="EmbData") embDyColumn = table.getColumn(name="EmbDy") sigDyColumn = table.getColumn(name="SigDy") dyCorrection = counter.subtractColumn("Correction", sigDyColumn, embDyColumn) column = counter.sumColumn(datasetName, [column, dyCorrection]) return column
def doCounters(myDsetMgr, mySuffix): eventCounter = counter.EventCounter(myDsetMgr) # append row from the tree to the main counter # eventCounter.getMainCounter().appendRow("MET > 70", treeDraw.clone(selection="met_p4.Et() > 70")) ewkDatasets = [ "WJets", "W1Jets", "W2Jets", "W3Jets", "W4Jets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson" ] if mcOnly: eventCounter.normalizeMCToLuminosity(mcOnlyLumi) else: eventCounter.normalizeMCByLuminosity() print "============================================================" print mySuffix print "============================================================" print "Main counter (MC normalized by collision data luminosity)" mainTable = eventCounter.getMainCounterTable() mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets])) # Default # cellFormat = counter.TableFormatText() # No uncertainties cellFormat = counter.TableFormatText(cellFormat=counter.CellFormatText(valueOnly=False)) print mainTable.format(cellFormat) # print eventCounter.getSubCounterTable("tauIDTauSelection").format() print eventCounter.getSubCounterTable("TauIDPassedEvt::TauSelection_HPS").format(cellFormat) print eventCounter.getSubCounterTable("TauIDPassedJets::TauSelection_HPS").format(cellFormat) print eventCounter.getSubCounterTable("b-tagging").format(cellFormat) print eventCounter.getSubCounterTable("Jet selection").format(cellFormat) print eventCounter.getSubCounterTable("Jet main").format(cellFormat) print eventCounter.getSubCounterTable("VetoTauSelection").format(cellFormat) print eventCounter.getSubCounterTable("MuonSelection").format(cellFormat) print eventCounter.getSubCounterTable("MCinfo for selected events").format(cellFormat) print eventCounter.getSubCounterTable("ElectronSelection").format(cellFormat)
def printCounters(datasets): eventCounter = counter.EventCounter(datasets) eventCounter.normalizeMCByLuminosity() ewkDatasets = ["WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson"] print "============================================================" print "Main counter (MC normalized by collision data luminosity)" mainTable = eventCounter.getMainCounterTable() mainTable.insertColumn( 2, counter.sumColumn( "EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets])) # Default # cellFormat = counter.TableFormatText() # No uncertainties cellFormat = counter.TableFormatText(cellFormat=counter.CellFormatText( valueOnly=True)) print mainTable.format(cellFormat) print eventCounter.getSubCounterTable("MCinfo for selected events").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)
def getCounter(self, datasetName, name): if not datasetName in ["Data", "EWKMC", "DYJetsToLL"]: (embDataHisto, tmp) = self.datasetsEmb.getHistogram(datasetName, name) return counter.HistoCounter(datasetName, embDataHisto) # Ugly hack sigName = name if isinstance(sigName, basestring): sigName = sigName.replace(self.analysisEmb, self.analysisSig) else: sigName = sigName.clone(tree=sigName.tree.replace(self.analysisEmb, self.analysisSig)) # Get properly normalized embedded data, embedded DY and normal DY histograms (embDataHisto, tmp) = self.datasetsEmb.getHistogram(datasetName, name) (embDyHisto, tmp) = self.datasetsEmb.getHistogram("DYJetsToLL", name) sigDyHisto = self.datasetsSig.getDataset("DYJetsToLL").getDatasetRootHisto(sigName) # DatasetRootHisto sigDyHisto.normalizeToLuminosity(self.datasetsEmb.getLuminosity()) sigDyHisto = sigDyHisto.getHistogram() # ROOT.TH1 embDataCounter = counter.HistoCounter("EmbData", embDataHisto) embDyCounter = counter.HistoCounter("EmbDy", embDyHisto) sigDyCounter = counter.HistoCounter("SigDy", sigDyHisto) table = counter.CounterTable() table.appendColumn(embDataCounter) table.appendColumn(embDyCounter) table.appendColumn(sigDyCounter) table.removeNonFullRows() column = table.getColumn(name="EmbData") embDyColumn = table.getColumn(name="EmbDy") sigDyColumn = table.getColumn(name="SigDy") dyCorrection = counter.subtractColumn("Correction", sigDyColumn, embDyColumn) column = counter.sumColumn(datasetName, [column, dyCorrection]) return column
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)
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 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 ewkSum(table): table.insertColumn(1, counter.sumColumn("EWKMCsum", [table.getColumn(name=name) for name in ewkDatasets]))
def signalSum(table): ttColumn = table.indexColumn("TTJets") wColumn = table.indexColumn("WJets") stColumn = table.indexColumn("SingleTop") return counter.sumColumn("TTJets+WJets+SingleTop", [table.getColumn(i) for i in [ttColumn, wColumn]])
def addMcSum(t): allDatasets = ["QCD_Pt20_MuEnriched", "WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson"] t.insertColumn(1, counter.sumColumn("MCSum", [t.getColumn(name=name) for name in allDatasets]))
def doCounters(datasets, mcLumi=None): createPlot = lambda name: createPlotCommon(name, datasets, mcLumi) eventCounter = counter.EventCounter(datasets, counters=countersWeighted) sels = [ # "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 20)", # "(20 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 80)", # "(80 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 120)", # "(120 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", ] tdCount = treeDraw.clone(weight=weightBTagging) tdCountMET = tdCount.clone(weight=weight, selection="&&".join(sels+[metCut])) tdCountBTagging = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut])) tdCountDeltaPhi160 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi160Cut])) tdCountDeltaPhi130 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi130Cut])) tdCountDeltaPhi90 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi90Cut])) eventCounter.getMainCounter().appendRow("JetsForEffs", tdCount.clone(weight=weight, selection="&&".join(sels))) eventCounter.getMainCounter().appendRow("METForEffs", tdCountMET) eventCounter.getMainCounter().appendRow("BTagging", tdCountBTagging) eventCounter.getMainCounter().appendRow("DeltaPhi < 160", tdCountDeltaPhi160) eventCounter.getMainCounter().appendRow("DeltaPhi < 130", tdCountDeltaPhi130) eventCounter.getMainCounter().appendRow("DeltaPhi < 90", tdCountDeltaPhi90) td1 = tdCount.clone(selection=metCut+"&&"+bTaggingCut+"&& (tecalometNoHF_p4.Pt() > 60)") td2 = tdCount.clone(selection=metCut+"&&"+bTaggingCut+"&& (tecalomet_p4.Pt() > 60)") td3 = dataset.TreeDrawCompound(td1, { "SingleMu_Mu_170722-172619_Aug05": td2, "SingleMu_Mu_172620-173198_Prompt": td2, "SingleMu_Mu_173236-173692_Prompt": td2, }) eventCounter.getMainCounter().appendRow("BTagging+CaloMetNoHF", td1) eventCounter.getMainCounter().appendRow("BTagging+CaloMet", td2) eventCounter.getMainCounter().appendRow("BTagging+CaloMet(NoHF)", td3) if mcLumi != None: eventCounter.normalizeMCToLuminosity(mcLumi) else: eventCounter.normalizeMCByLuminosity() tauEmbedding.scaleNormalization(eventCounter) ewkDatasets = [ "WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson" ] table = eventCounter.getMainCounterTable() mainTable = table muonAnalysis.addSumColumn(table) mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets])) # table = eventCounter.getSubCounterTable("Trigger") # muonAnalysis.reorderCounterTable(table) muonAnalysis.addDataMcRatioColumn(table) if datasets.hasDataset("EWKSignal"): mainTable.insertColumn(7, counter.divideColumn("SignalFraction", mainTable.getColumn(name="TTToHplus_"+keepSignal), mainTable.getColumn(name="EWKSignal"))) datasets.printInfo() print "============================================================" print "Main counter (%s)" % eventCounter.getNormalizationString() cellFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.3f')) print table.format(cellFormat) tauTable = eventCounter.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight") #muonAnalysis.addSumColumn(tauTable) tauTable.insertColumn(2, counter.sumColumn("EWKMCsum", [tauTable.getColumn(name=name) for name in ewkDatasets])) print tauTable.format(cellFormat) # print eventCounter.getSubCounterTable("TauIDPassedJets::tauID_HPSTight").format() # table = eventCounter.getSubCounterTable("Trigger") # muonAnalysis.addSumColumn(table) # print table.format(cellFormat) mainTable.keepOnlyRows([ "All events", "Trigger and HLT_MET cut", "taus == 1", # "trigger scale factor", "electron veto", "muon veto", "MET", "njets", "btagging", "btagging scale factor", "JetsForEffs", "METForEffs", "BTagging", "DeltaPhi < 160", "DeltaPhi < 130" ]) tauTable.keepOnlyRows([ "AllTauCandidates", "DecayModeFinding", "TauJetPt", "TauJetEta", "TauLdgTrackExists", "TauLdgTrackPtCut", "TauECALFiducialCutsCracksAndGap", "TauAgainstElectronCut", "TauAgainstMuonCut", #"EMFractionCut", "HPS", "TauOneProngCut", "TauRtauCut", ]) #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f')) effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f')) #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f')) for name, table in [("Main", mainTable), ("Tau ID", tauTable)]: effTable = counter.CounterTable() col = table.getColumn(name="Data") effTable.appendColumn(col) effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col)) col = table.getColumn(name="EWKMCsum") effTable.appendColumn(col) effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col)) print "%s counter efficiencies" % name print effTable.format(effFormat) print "Trigger uncertainties" bins = [40, 50, 60, 80] tauPtPrototype = ROOT.TH1F("tauPtTrigger", "Tau pt", len(bins)-1, array.array("d", bins)) runs = [ "(160431 <= run && run <= 167913)", "(170722 <= run && run <= 173198)", "(173236 <= run && run <= 173692)", #"(160431 <= run && run <= 173692)", ] for name, td in [ ("BTagging", tdCountBTagging), ("DeltaPhi160", tdCountDeltaPhi160), ("DeltaPhi130", tdCountDeltaPhi130), ("DeltaPhi90", tdCountDeltaPhi90) ]: t = td.clone(varexp="tau_p4.Pt() >>tauPtTrigger") NallSum = 0 NSum = 0 absUncSquareSum = 0 for runRegion in runs: #neventsPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTrigger"))) #uncertaintyPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTriggerAbsUnc*weightTriggerAbsUnc/(weightTrigger*weightTrigger)"))) tmp = t.clone(selection=t.selection+"&&"+runRegion) nallPlot = createPlot(tmp.clone(weight="")) neventsPlot = createPlot(tmp.clone(weight="weightTrigger")) uncertaintyPlot = createPlot(tmp.clone(weight="weightTriggerAbsUnc")) th1all = nallPlot.histoMgr.getHisto("Data").getRootHisto() th1 = neventsPlot.histoMgr.getHisto("Data").getRootHisto() th12 = uncertaintyPlot.histoMgr.getHisto("Data").getRootHisto() Nall = th1all.Integral(0, th1all.GetNbinsX()+1) N = th1.Integral(0, th1.GetNbinsX()+1) #absSum2 = th12.Integral(0, th12.GetNbinsX()+1) #absUnc = math.sqrt(absSum2) #absUnc = th12.Integral(0, 2) NallSum += Nall NSum += N absUnc = squareSum(th12) absUncSquareSum += absUnc absUnc = math.sqrt(absUnc) relUnc = 0 if N > 0: relUnc = absUnc/N print "%-15s for runs %s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (name, runRegion, Nall, N, absUnc, relUnc) absUnc = math.sqrt(absUncSquareSum) relUnc = absUnc/NSum print "%-15s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (name, NallSum, NSum, absUnc, relUnc) print
def 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]))