def prepareDYScaleFactors(filename, plotfile, inputdir, options, dytag="DY+Jets", unmergedfile="MC8TeV_TTJets_MSDecays_172v5.root"): """ Prepares a dictionary of (key, process title) -> scale factor, to be used in the runPlotter script to scale DY by a data-derived scale factor. A list of keys to be scaled is generated from a plotfile (for merged histograms), and from one file of an input directory (for unmerged ones). """ ## First calculate the actual scalefactors DYSFs = extractFactors(filename, options=options) ## Get a list of all keys that are to be scaled allkeys = [] ## Need the list of processes tagsToFilter = ['_ee', '_mm', '_mumu'] ## From merged plots: allkeys += getAllPlotsFrom(openTFile(plotfile), tagsToFilter=tagsToFilter, filterByProcsFromJSON=options.json) ## From unmerged plots: allkeys += getAllPlotsFrom(openTFile(os.path.join(inputdir, unmergedfile)), tagsToFilter=tagsToFilter) ## Prepare dictionary of (key,tag) -> scale factor scaleFactors = {} if options.verbose > 0: print 'Will scale the following histograms for %s:' % dytag for key in allkeys: if '_ee' in key: scaleFactors[(key, dytag)] = DYSFs['ee'] if options.verbose > 0: print ' %-25s: %5.3f' % (key, DYSFs['ee']) else: scaleFactors[(key, dytag)] = DYSFs['mm'] if options.verbose > 0: print ' %-25s: %5.3f' % (key, DYSFs['mm']) return scaleFactors
def main(args, opt): try: cachefile = open(".eventyields.pck", 'r') yields = pickle.load(cachefile) cachefile.close() return printYields(yields) except EOFError: print "Error loading pickle file, please delete it and try again" return -1 except IOError: pass try: tfile = openTFile(args[0]) except ReferenceError: print "Please provide a valid input file" return -1 ROOT.gStyle.SetOptTitle(0) ROOT.gStyle.SetOptStat(0) ROOT.gROOT.SetBatch(1) allkeys = getAllPlotsFrom(tfile, tagsToFilter=[opt.dist]) channels = ['e', 'ee', 'em', 'm', 'mm'] print 30 * '-' yields = {} for chan in channels: for key in allkeys: histname = "%s_%s" % (opt.dist, chan) if not key.endswith(histname): continue hist = tfile.Get(key) err = ROOT.Double(0.0) firstBin = opt.firstBin lastBin = hist.GetXaxis().GetNbins( ) if opt.lastBin < opt.firstBin else opt.lastBin integral = hist.IntegralAndError(firstBin, lastBin, err) err = float(err) proc = key.rsplit('/', 1)[1] proc = proc.replace('_%s' % histname, '') yields[(chan, proc)] = (integral, err) cachefile = open(".eventyields.pck", 'w') pickle.dump(yields, cachefile, pickle.HIGHEST_PROTOCOL) cachefile.close() return printYields(yields)
def extractFactors(inputFile, options): try: cachefile = open('.svldyscalefactors.pck','r') scaleFactors = pickle.load(cachefile) cachefile.close() print ('>>> Read Drell-Yan scale factors from cache ' '(.svldyscalefactors.pck)') return scaleFactors except IOError: pass try: tfile = openTFile(inputFile) except ReferenceError: print "Please provide a valid input file" allkeys = getAllPlotsFrom(tfile, tagsToFilter=HISTSTOPROCESS) scaleFactors = {} for histname in HISTSTOPROCESS: tot_data = 0 tot_background = 0 tot_signal = 0 for key in allkeys: if not key.endswith(histname): continue integral = tfile.Get(key).Integral() if 'Data8TeV' in key: tot_data += integral elif SIGNAL in key: tot_signal += integral else: tot_background += integral channame = histname.rsplit('_',1)[1] if options.verbose>0: print 30*'-' print histname print ("Total data: %6.1f, total bg: %6.1f, total signal: %6.1f " % (tot_data, tot_background, tot_signal)) # tot_background + SF * tot_signal = tot_data SF = (tot_data-tot_background)/tot_signal if options.verbose>0: print " -> scale factor: %5.3f" % SF scaleFactors[channame] = SF if options.verbose>0: print 30*'-' cachefile = open(".svldyscalefactors.pck", 'w') pickle.dump(scaleFactors, cachefile, pickle.HIGHEST_PROTOCOL) cachefile.close() return scaleFactors
def main(args, opt): try: cachefile = open(".eventyields.pck", 'r') yields = pickle.load(cachefile) cachefile.close() return printYields(yields) except EOFError: print "Error loading pickle file, please delete it and try again" return -1 except IOError: pass try: tfile = openTFile(args[0]) except ReferenceError: print "Please provide a valid input file" return -1 ROOT.gStyle.SetOptTitle(0) ROOT.gStyle.SetOptStat(0) ROOT.gROOT.SetBatch(1) allkeys = getAllPlotsFrom(tfile, tagsToFilter=[opt.dist]) channels = ['e', 'ee', 'em', 'm', 'mm'] print 30*'-' yields = {} for chan in channels: for key in allkeys: histname = "%s_%s"%(opt.dist,chan) if not key.endswith(histname): continue hist = tfile.Get(key) err = ROOT.Double(0.0) firstBin=opt.firstBin lastBin=hist.GetXaxis().GetNbins() if opt.lastBin<opt.firstBin else opt.lastBin integral = hist.IntegralAndError(firstBin,lastBin, err) err = float(err) proc = key.rsplit('/',1)[1] proc = proc.replace('_%s'%histname, '') yields[(chan,proc)] = (integral, err) cachefile = open(".eventyields.pck", 'w') pickle.dump(yields, cachefile, pickle.HIGHEST_PROTOCOL) cachefile.close() return printYields(yields)
def extractLJNTrkWeights(inputFile=None, verbose=0): try: tfile = openTFile(inputFile) except ReferenceError: print "[extractLJNTrkWeights] Please provide a valid input file" allkeys = getAllPlotsFrom(tfile, tagsToFilter=HISTSTOPROCESS) ofile = ROOT.TFile.Open('ljntkweights.root','recreate') ofile.cd() for histname in HISTSTOPROCESS: tot_data, tot_mc = None, None for key in allkeys: if not key.endswith(histname): continue hist = tfile.Get(key) integral = hist.Integral() if 'Data8TeV' in key: if not tot_data: tot_data = hist.Clone("SVNtrk_data") tot_data.SetDirectory(0) else: print "WARNING: Found two data histograms" tot_data = hist.Clone("SVNtrk_data") tot_data.SetDirectory(0) if 'MC8TeV_TTJets_MSDecays_172v5' in key: if not tot_mc: tot_mc = hist.Clone("SVNtrk_mc") tot_mc.SetDirectory(0) else: tot_mc.Add(hist) ## Normalize them tot_data.Scale(1.0/tot_data.Integral()) tot_mc.Scale(1.0/tot_mc.Integral()) ratio = tot_data.Clone("%s_weights"%histname) ratio.Divide(tot_mc) ofile.cd() ratio.Write(ratio.GetName()) ofile.Write() ofile.Close() print ">>> Wrote light jet ntrk weights to ljntkweights.root"
def extractLJNTrkWeights(inputFile=None, verbose=0): try: tfile = openTFile(inputFile) except ReferenceError: print "[extractLJNTrkWeights] Please provide a valid input file" allkeys = getAllPlotsFrom(tfile, tagsToFilter=HISTSTOPROCESS) ofile = ROOT.TFile.Open('ljntkweights.root', 'recreate') ofile.cd() for histname in HISTSTOPROCESS: tot_data, tot_mc = None, None for key in allkeys: if not key.endswith(histname): continue hist = tfile.Get(key) integral = hist.Integral() if 'Data8TeV' in key: if not tot_data: tot_data = hist.Clone("SVNtrk_data") tot_data.SetDirectory(0) else: print "WARNING: Found two data histograms" tot_data = hist.Clone("SVNtrk_data") tot_data.SetDirectory(0) if 'MC8TeV_TTJets_MSDecays_172v5' in key: if not tot_mc: tot_mc = hist.Clone("SVNtrk_mc") tot_mc.SetDirectory(0) else: tot_mc.Add(hist) ## Normalize them tot_data.Scale(1.0 / tot_data.Integral()) tot_mc.Scale(1.0 / tot_mc.Integral()) ratio = tot_data.Clone("%s_weights" % histname) ratio.Divide(tot_mc) ofile.cd() ratio.Write(ratio.GetName()) ofile.Write() ofile.Close() print ">>> Wrote light jet ntrk weights to ljntkweights.root"
def compareNtrk(inputFile=None, verbose=0): xsecweights=readXSecWeights() try: tfile = openTFile(inputFile) except ReferenceError: print "Please provide a valid input file" for reg in ['regA','regB','regC']: for ch in ['e','m','mm','em','ee']: histToProcess='SVNtrk_%s_optmrank' % ch allkeys = getAllPlotsFrom(tfile, tagsToFilter=[histToProcess]) data=None mc=None for key in allkeys: if not (reg in key) : continue weight=1.0 for xsecKey in xsecweights: procName=xsecKey.replace('MC8TeV','')+'_' if procName in key : weight =xsecweights[xsecKey]*LUMI if '2012' in key: try: data.Add( tfile.Get(key), weight ) except: data=tfile.Get(key).Clone('data') data.Scale(weight) else: try: mc.Add( tfile.Get(key), weight ) except: mc=tfile.Get(key).Clone('mc') mc.Scale(weight) varPlot = Plot('%s_%s_ntrks'%(ch,reg),False) varPlot.add(mc, 'expected', ROOT.kGray, False) varPlot.add(data, 'data', 1, True) varPlot.show('~/www/tmp') varPlot.reset()
def compareNtrk(inputFile=None, verbose=0): xsecweights = readXSecWeights() try: tfile = openTFile(inputFile) except ReferenceError: print "Please provide a valid input file" for reg in ['regA', 'regB', 'regC']: for ch in ['e', 'm', 'mm', 'em', 'ee']: histToProcess = 'SVNtrk_%s_optmrank' % ch allkeys = getAllPlotsFrom(tfile, tagsToFilter=[histToProcess]) data = None mc = None for key in allkeys: if not (reg in key): continue weight = 1.0 for xsecKey in xsecweights: procName = xsecKey.replace('MC8TeV', '') + '_' if procName in key: weight = xsecweights[xsecKey] * LUMI if '2012' in key: try: data.Add(tfile.Get(key), weight) except: data = tfile.Get(key).Clone('data') data.Scale(weight) else: try: mc.Add(tfile.Get(key), weight) except: mc = tfile.Get(key).Clone('mc') mc.Scale(weight) varPlot = Plot('%s_%s_ntrks' % (ch, reg), False) varPlot.add(mc, 'expected', ROOT.kGray, False) varPlot.add(data, 'data', 1, True) varPlot.show('~/www/tmp') varPlot.reset()
def extractNTrkWeights(inputFile=None, verbose=0): try: cachefile = open('.svntrkweights.pck', 'r') ntkWeights = pickle.load(cachefile) cachefile.close() print('>>> Read SV ntrk weights from cache ' '(.svntrkweights.pck)') return ntkWeights except IOError: pass try: tfile = openTFile(inputFile) except ReferenceError: print "[extractNTrkWeights] Please provide a valid input file" allkeys = getAllPlotsFrom(tfile, tagsToFilter=HISTSTOPROCESS) ntkWeights = {} for histname in HISTSTOPROCESS: tot_data, tot_mc = None, None for key in allkeys: if not key.endswith(histname): continue hist = tfile.Get(key) integral = hist.Integral() if 'Data8TeV' in key: if not tot_data: tot_data = hist.Clone("SVNtrk_data") else: print "WARNING: Found two data histograms" tot_data = hist.Clone("SVNtrk_data") else: if not tot_mc: tot_mc = hist.Clone("SVNtrk_mc") else: tot_mc.Add(hist) ratio = tot_data.Clone("ratio") ratio.Divide(tot_mc) key = 'inclusive' if len(histname.split('_')) > 2: key = histname.rsplit('_', 1)[1] if not key in ntkWeights: ntkWeights[key] = {} if verbose > 0: print '---------------------------' print histname for x in xrange(1, ratio.GetNbinsX() + 1): if verbose > 0: print("Ntrk=%1d: Ndata: %6d / Nmc: %8.1f , weight = %5.3f" % (x + 1, tot_data.GetBinContent(x), tot_mc.GetBinContent(x), ratio.GetBinContent(x))) ntkWeights[key][x + 1] = ratio.GetBinContent(x) cachefile = open(".svntrkweights.pck", 'w') pickle.dump(ntkWeights, cachefile, pickle.HIGHEST_PROTOCOL) cachefile.close() print ">>> Dumped SV ntrk weights to .svntrkweights.pck" return ntkWeights
def extractNTrkWeights(inputFile=None, verbose=0): try: cachefile = open('.svntrkweights.pck','r') ntkWeights = pickle.load(cachefile) cachefile.close() print ('>>> Read SV ntrk weights from cache ' '(.svntrkweights.pck)') return ntkWeights except IOError: pass try: tfile = openTFile(inputFile) except ReferenceError: print "[extractNTrkWeights] Please provide a valid input file" allkeys = getAllPlotsFrom(tfile, tagsToFilter=HISTSTOPROCESS) ntkWeights = {} for histname in HISTSTOPROCESS: tot_data, tot_mc = None, None for key in allkeys: if not key.endswith(histname): continue hist = tfile.Get(key) integral = hist.Integral() if 'Data8TeV' in key: if not tot_data: tot_data = hist.Clone("SVNtrk_data") else: print "WARNING: Found two data histograms" tot_data = hist.Clone("SVNtrk_data") else: if not tot_mc: tot_mc = hist.Clone("SVNtrk_mc") else: tot_mc.Add(hist) ratio = tot_data.Clone("ratio") ratio.Divide(tot_mc) key = 'inclusive' if len(histname.split('_'))>2: key = histname.rsplit('_',1)[1] if not key in ntkWeights: ntkWeights[key] = {} if verbose>0: print '---------------------------' print histname for x in xrange(1,ratio.GetNbinsX()+1): if verbose>0: print ("Ntrk=%1d: Ndata: %6d / Nmc: %8.1f , weight = %5.3f" % (x+1, tot_data.GetBinContent(x), tot_mc.GetBinContent(x), ratio.GetBinContent(x))) ntkWeights[key][x+1] = ratio.GetBinContent(x) cachefile = open(".svntrkweights.pck", 'w') pickle.dump(ntkWeights, cachefile, pickle.HIGHEST_PROTOCOL) cachefile.close() print ">>> Dumped SV ntrk weights to .svntrkweights.pck" return ntkWeights