def printCutFlow(hist, analysis, outputFormat='Latex'): scale_ttbar = 164.4 / 157.5 used_data = 'ElectronHad' lepton = 'Electron/electron' if 'Mu' in analysis: used_data = 'SingleMu' lepton = 'Muon/muon' hist_1mBtag = 'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/' + lepton + '_AbsEta_1orMoreBtag' hist_2mBtag = 'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/' + lepton + '_AbsEta_2orMoreBtags' hist_names = [hist, #due to b-tag scale factors these are not as simple any more hist_1mBtag, hist_2mBtag ] inputfiles = {} for sample in FILES.samplesToLoad: inputfiles[sample] = FILES.files[sample] hists = FileReader.getHistogramsFromFiles(hist_names, inputfiles) for sample in hists.keys(): for histname in hists[sample].keys(): hists[sample][histname].Sumw2() if analysis == 'EPlusJets': hists['QCD'] = plotting.sumSamples(hists, plotting.qcd_samples) else: hists['QCD'] = hists['QCD_Pt-20_MuEnrichedPt-15'] hists['SingleTop'] = plotting.sumSamples(hists, plotting.singleTop_samples) hists['Di-Boson'] = plotting.sumSamples(hists, plotting.diboson_samples) hists['W+Jets'] = plotting.sumSamples(hists, plotting.wplusjets_samples) # hists['SumMC'] = plotting.sumSamples(hists, plotting.allMC_samples) header = "| Step | TTJet | W+jets | DY + Jets | single top | Di-boson | QCD | Sum MC | Data |" row = " | %s | %d +- %d | %d +- %d | %d +- %d | %d +- %d | %d +- %d | %d +- %d | %d +- %d | %d | " if outputFormat == 'Latex': header = "Selection step & \\ttbar & W + Jets & Z + Jets & Single-top & Di-boson & QCD~ & Sum MC & Data\\\\" row = " %s & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & %d \\\\ " print header numbers, errors = getEventNumbers(hists, hist, hist_1mBtag, hist_2mBtag)# + '_0orMoreBtag') for step in range(len(cuts)): nums = numbers[step] errs = errors[step] nums['TTJet'] = nums['TTJet'] * scale_ttbar errs['TTJet'] = errs['TTJet'] * scale_ttbar if analysis == 'EPlusJets' and step >= len(cuts) - 3:#have only estimates for >= 4 jet and beyond histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_0orMoreBtag' if step == len(cuts) - 2: histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_1orMoreBtag' if step == len(cuts) - 1: histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_2orMoreBtags' estimate = QCDRateEstimation.estimateQCDWithRelIso(FILES.files, histForEstimation) nums['QCD'], errs['QCD'] = estimate['estimate'], estimate['absoluteError'] if analysis == 'MuPlusJets' and step >= len(cuts) - 3:#have only estimates for >= 4 jet and beyond scale = 1.21 nums['QCD'], errs['QCD'] = nums['QCD'] * scale, errs['QCD'] * scale sumMC = nums['TTJet'] + nums['W+Jets'] + nums['DYJetsToLL'] + nums['SingleTop'] + nums['QCD'] + nums['Di-Boson'] sumMC_err = sqrt(errs['TTJet'] ** 2 + errs['W+Jets'] ** 2 + errs['DYJetsToLL'] ** 2 + errs['SingleTop'] ** 2 + errs['QCD'] ** 2 + errs['Di-Boson'] ** 2) print row % (cuts[step], nums['TTJet'], errs['TTJet'], nums['W+Jets'], errs['W+Jets'], nums['DYJetsToLL'], errs['DYJetsToLL'], nums['SingleTop'], errs['SingleTop'], nums['Di-Boson'], errs['Di-Boson'], nums['QCD'], errs['QCD'], sumMC, sumMC_err, nums[used_data])
'electron_phi':'Events/(0.5)', "electron_pfIsolation_03_0orMoreBtag":'Events/(0.05)', "electron_pfIsolation_04_0orMoreBtag":'Events/(0.05)', "electron_pfIsolation_05_0orMoreBtag":'Events/(0.05)', "electron_dPhi_in":'Events/(0.01)', "electron_dEta_in":'Events/(0.001)', "electron_HadOverEM":'Events/(0.01)', "electron_mvaTrigV0":'Events/(0.05)', "electron_mvaNonTrigV0":'Events/(0.05)', "electron_dB":'Events/(0.001 cm)', 'electron_sigma_ietaieta':'Events/(0.001)' } histograms = ['HLTQCDAnalyser_inclusive/' + trigger + '/' + variable for variable in variables for trigger in triggers] hists = FileReader.getHistogramsFromFiles(histograms, files) plotting.setStyle() for variable in variables: hists = plotting.rebin(hists, rebins[variable], '*' + variable) hists = plotting.setXRange(hists, limits=limits[variable], histname='*' + variable) hists = plotting.setYTitle(hists, title=titles[variable], histname='*' + variable) labels = [ 'CaloIdVT_CaloIsoT_TrkIdT_TrkIsoT', 'CaloIdVT_CaloIsoVL_TrkIdVL_TrkIsoT', 'CaloIdVL_CaloIsoT_TrkIdVL_TrkIsoT', 'CaloIdVT_TrkIdT' ] styles = [ {'color': kBlack, 'fill': 1001},
def sumExclusiveHistogramsInFile(filename): testfile = File(filename, 'read') newHistograms = {} histExists = newHistograms.has_key nTimes = 1000000000 nthTime = 0 nHistograms = 0 listOfAllHistograms = [] addToList = listOfAllHistograms.append for folder, emptyThing, histograms in testfile: for histogram in histograms: nHistograms += 1 currentPath = folder + '/' + histogram addToList(currentPath) print 'Loading', nHistograms, 'histograms' allHistograms = fileReader.getHistogramsFromFiles(listOfAllHistograms, {'Test': filename}, True)['Test'] print 'Loaded', nHistograms, 'histograms' nHistograms = 0 for histogramName, histogram in allHistograms.iteritems(): nthTime += 1 if nthTime >= nTimes: continue nHistograms += 1 isBtagBinnedHist = False if nHistograms % 5000 == 0: print 'Done', nHistograms, 'histograms' currentHist = histogramName for btag_bin in btag_bins_available: if btag_bin in histogramName: isBtagBinnedHist = True currentHist = histogramName.replace(btag_bin, '') if not isBtagBinnedHist: continue currentPath = currentHist if histExists(currentHist): #already have it continue inclBinHistograms = {} for exclBin in range(len(btag_bins_inclusive)): # print 'Starting with', currentPath + btag_bins_available[exclBin] newhist = allHistograms[currentPath + btag_bins_available[exclBin]] for availBin in range(exclBin + 1, len(btag_bins_available)): # print '>>>>> Adding:', currentPath + btag_bins_available[availBin] addThis = allHistograms[currentPath + btag_bins_available[availBin]] newhist.Add(addThis) inclBinHistograms[currentPath + btag_bins_inclusive[exclBin]] = newhist newHistograms[currentPath] = inclBinHistograms testfile.Close() print 'Done', nHistograms, 'histograms' rootFile = TFile.Open(filename, 'UPDATE') cd = rootFile.Cd rootFile.cd() for histFullPath, histogramDict in newHistograms.iteritems(): currentHist = histFullPath.split('/')[-1] path = histFullPath.replace(currentHist, '') cd('/' + path) for histname, histogram in histogramDict.iteritems(): currentHist = histname.split('/')[-1] histogram.Write(currentHist) rootFile.Write() rootFile.Close() del allHistograms del rootFile del testfile del newHistograms
def sumExclusiveHistogramsInFile(filename): testfile = File(filename, 'read') newHistograms = {} histExists = newHistograms.has_key nTimes = 1000000000 nthTime = 0 nHistograms = 0 listOfAllHistograms = [] addToList = listOfAllHistograms.append for folder, emptyThing, histograms in testfile: for histogram in histograms: nHistograms += 1 currentPath = folder + '/' + histogram addToList(currentPath) print 'Loading', nHistograms, 'histograms' allHistograms = fileReader.getHistogramsFromFiles(listOfAllHistograms, {'Test':filename}, True)['Test'] print 'Loaded', nHistograms, 'histograms' nHistograms = 0 for histogramName, histogram in allHistograms.iteritems(): nthTime += 1 if nthTime >= nTimes: continue nHistograms += 1 isBtagBinnedHist = False if nHistograms % 5000 == 0: print 'Done', nHistograms, 'histograms' currentHist = histogramName for btag_bin in btag_bins_available: if btag_bin in histogramName: isBtagBinnedHist = True currentHist = histogramName.replace(btag_bin, '') if not isBtagBinnedHist: continue currentPath = currentHist if histExists(currentHist):#already have it continue inclBinHistograms = {} for exclBin in range(len(btag_bins_inclusive)): # print 'Starting with', currentPath + btag_bins_available[exclBin] newhist = allHistograms[currentPath + btag_bins_available[exclBin]] for availBin in range(exclBin + 1, len(btag_bins_available)): # print '>>>>> Adding:', currentPath + btag_bins_available[availBin] addThis = allHistograms[currentPath + btag_bins_available[availBin]] newhist.Add(addThis) inclBinHistograms[currentPath + btag_bins_inclusive[exclBin]] = newhist newHistograms[currentPath] = inclBinHistograms testfile.Close() print 'Done', nHistograms, 'histograms' rootFile = TFile.Open(filename, 'UPDATE') cd = rootFile.Cd rootFile.cd() for histFullPath, histogramDict in newHistograms.iteritems(): currentHist = histFullPath.split('/')[-1] path = histFullPath.replace(currentHist, '') cd('/' + path) for histname, histogram in histogramDict.iteritems(): currentHist = histname.split('/')[-1] histogram.Write(currentHist) rootFile.Write() rootFile.Close() del allHistograms del rootFile del testfile del newHistograms
"electron_pfIsolation_05_0orMoreBtag": 'Events/(0.05)', "electron_dPhi_in": 'Events/(0.01)', "electron_dEta_in": 'Events/(0.001)', "electron_HadOverEM": 'Events/(0.01)', "electron_mvaTrigV0": 'Events/(0.05)', "electron_mvaNonTrigV0": 'Events/(0.05)', "electron_dB": 'Events/(0.001 cm)', 'electron_sigma_ietaieta': 'Events/(0.001)' } histograms = [ 'HLTQCDAnalyser_inclusive/' + trigger + '/' + variable for variable in variables for trigger in triggers ] hists = FileReader.getHistogramsFromFiles(histograms, files) plotting.setStyle() for variable in variables: hists = plotting.rebin(hists, rebins[variable], '*' + variable) hists = plotting.setXRange(hists, limits=limits[variable], histname='*' + variable) hists = plotting.setYTitle(hists, title=titles[variable], histname='*' + variable) labels = [ 'CaloIdVT_CaloIsoT_TrkIdT_TrkIsoT', 'CaloIdVT_CaloIsoVL_TrkIdVL_TrkIsoT', 'CaloIdVL_CaloIsoT_TrkIdVL_TrkIsoT', 'CaloIdVT_TrkIdT' ]
def printCutFlow(hist, analysis, outputFormat='Latex'): scale_ttbar = 164.4 / 157.5 used_data = 'ElectronHad' lepton = 'Electron/electron' if 'Mu' in analysis: used_data = 'SingleMu' lepton = 'Muon/muon' hist_1mBtag = 'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/' + lepton + '_AbsEta_1orMoreBtag' hist_2mBtag = 'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/' + lepton + '_AbsEta_2orMoreBtags' hist_names = [ hist, #due to b-tag scale factors these are not as simple any more hist_1mBtag, hist_2mBtag ] inputfiles = {} for sample in FILES.samplesToLoad: inputfiles[sample] = FILES.files[sample] hists = FileReader.getHistogramsFromFiles(hist_names, inputfiles) for sample in hists.keys(): for histname in hists[sample].keys(): hists[sample][histname].Sumw2() if analysis == 'EPlusJets': hists['QCD'] = plotting.sumSamples(hists, plotting.qcd_samples) else: hists['QCD'] = hists['QCD_Pt-20_MuEnrichedPt-15'] hists['SingleTop'] = plotting.sumSamples(hists, plotting.singleTop_samples) hists['Di-Boson'] = plotting.sumSamples(hists, plotting.diboson_samples) hists['W+Jets'] = plotting.sumSamples(hists, plotting.wplusjets_samples) # hists['SumMC'] = plotting.sumSamples(hists, plotting.allMC_samples) header = "| Step | TTJet | W+jets | DY + Jets | single top | Di-boson | QCD | Sum MC | Data |" row = " | %s | %d +- %d | %d +- %d | %d +- %d | %d +- %d | %d +- %d | %d +- %d | %d +- %d | %d | " if outputFormat == 'Latex': header = "Selection step & \\ttbar & W + Jets & Z + Jets & Single-top & Di-boson & QCD~ & Sum MC & Data\\\\" row = " %s & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & $%d \pm %d$ & %d \\\\ " print header numbers, errors = getEventNumbers(hists, hist, hist_1mBtag, hist_2mBtag) # + '_0orMoreBtag') for step in range(len(cuts)): nums = numbers[step] errs = errors[step] nums['TTJet'] = nums['TTJet'] * scale_ttbar errs['TTJet'] = errs['TTJet'] * scale_ttbar if analysis == 'EPlusJets' and step >= len( cuts) - 3: #have only estimates for >= 4 jet and beyond histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_0orMoreBtag' if step == len(cuts) - 2: histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_1orMoreBtag' if step == len(cuts) - 1: histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_2orMoreBtags' estimate = QCDRateEstimation.estimateQCDWithRelIso( FILES.files, histForEstimation) nums['QCD'], errs['QCD'] = estimate['estimate'], estimate[ 'absoluteError'] if analysis == 'MuPlusJets' and step >= len( cuts) - 3: #have only estimates for >= 4 jet and beyond scale = 1.21 nums['QCD'], errs['QCD'] = nums['QCD'] * scale, errs['QCD'] * scale sumMC = nums['TTJet'] + nums['W+Jets'] + nums['DYJetsToLL'] + nums[ 'SingleTop'] + nums['QCD'] + nums['Di-Boson'] sumMC_err = sqrt(errs['TTJet']**2 + errs['W+Jets']**2 + errs['DYJetsToLL']**2 + errs['SingleTop']**2 + errs['QCD']**2 + errs['Di-Boson']**2) print row % (cuts[step], nums['TTJet'], errs['TTJet'], nums['W+Jets'], errs['W+Jets'], nums['DYJetsToLL'], errs['DYJetsToLL'], nums['SingleTop'], errs['SingleTop'], nums['Di-Boson'], errs['Di-Boson'], nums['QCD'], errs['QCD'], sumMC, sumMC_err, nums[used_data])