def plotsflow(self, name, varexp, options='', bins=None, extra=None, postprocess=None): ''' Create a flow of plots Always call View.plot ''' # make the entries views = self._ensureviews() # add the weight and get the yields if not views: return odict.OrderedDict() plots = odict.OrderedDict([(n, v.plot('%s_%s' % (name, n), varexp, extra, options, bins)) for n, v in views.iteritems()]) # make a list with all processors procs = self._filters if not postprocess else (self._filters + [postprocess]) # apply post creation filters for p in plots.itervalues(): for proc in procs: proc(p) return plots
def yieldsflow(self, extra=None): # make the entries views = self._ensureviews() if not views: return odict.OrderedDict() return odict.OrderedDict([(n, v.yields(extra)) for n, v in views.iteritems()])
class wwcutsB: wwcommon = odict.OrderedDict([ ('trigger', 'trigger==1.'), ('min #slash{E}_{T}', 'pfmet>20.'), ('m_{ll}', 'mll>12'), # ema7 ('Z_{veto}', '(zveto==1||!sameflav)'), ('min proj #slash{E}_{T}', 'mpmet>20.'), # ema9 ('Soft #mu veto', 'bveto_mu==1'), ('Extra Lepton', 'nextra==0'), ('B veto', '(bveto_ip==1 && (nbjettche==0 || njet>3) )'), ('p_{T}^{ll}', 'ptll>45.'), # ema 14 ('Extra Jet', 'njet<4'), ]) wwmin = odict.OrderedDict([ ('trigger', 'trigger==1.'), ('min #slash{E}_{T}', 'pfmet>20.'), ('m_{ll}', 'mll>12'), # ema7 ('min proj #slash{E}_{T}', 'mpmet>20.'), # ema9 ('Soft #mu veto', 'bveto_mu==1'), ('Extra Lepton', 'nextra==0'), ('Extra Jet', 'njet<4'), ]) #dy cuts dphilljjlo = '(njet==0 || njet==1 || (dphilljetjet<pi/180.*165. || !sameflav ) )' dphilljjhi = '((njet<=1 && dphiveto) || (njet>1 && dphilljetjet<pi/180.*165.) || !sameflav )' # met cuts lo: <=140 GeV, hi > 140 GeV metlo = '( !sameflav || ( (njet!=0 || dymva1>0.60) && (njet!=1 || dymva1>0.30) && ( njet==0 || njet==1 || (pfmet > 45.0)) ) )' methi = '( !sameflav || ( (njet!=0 || mpmet>45.0) && (njet!=1 || mpmet>45.0) && ( njet==0 || njet==1 || (pfmet > 45.0)) ) )' wwlo = wwcommon.copy() wwlo.update([ ('d#Phi_{lljj}', dphilljjlo), ('dymva', metlo), ]) wwhi = wwcommon.copy() wwhi.update([ ('d#Phi_{lljj}', dphilljjhi), ('dymva', methi), ]) wwcommon.update([ ('d#Phi_{lljj} OR', '( ' + dphilljjlo + ' || ' + dphilljjhi + ' )'), ('dymva OR', '( ' + metlo + ' || ' + methi + ' )'), ]) wwmin.update([ ('d#Phi_{lljj} OR', '( ' + dphilljjlo + ' || ' + dphilljjhi + ' )'), ('dymva OR', '( ' + metlo + ' || ' + methi + ' )'), ]) zerojet = 'njet == 0' onejet = 'njet == 1' vbf = '(njet >= 2 && njet <= 3 && (jetpt3 <= 30 || !(jetpt3 > 30 && ( (jeteta1-jeteta3 > 0 && jeteta2-jeteta3 < 0) || (jeteta2-jeteta3 > 0 && jeteta1-jeteta3 < 0))))) '
def views(self, cuts): if not cuts: return odict.OrderedDict() views = odict.OrderedDict() last = TreeView(self) for i, (n, c) in enumerate(cuts.iteritems()): m = last.spawn(c, 'elist%d' % (i + nv)) last = m views[n] = m return views
def __init__(self, properties=_defaults): super(HWWPlot, self).__init__() self._properties = properties self._autosort = False self._order = None self._data = None self._sigs = odict.OrderedDict() self._bkgs = odict.OrderedDict() self._errors = None self._verbose = False
def views(self, cuts): # put the treeviews in a temporary container allviews = [o.views(cuts) for o in self._objs] # but what we need are the alliters = [v.itervalues() for v in allviews] import itertools chainviews = odict.OrderedDict() # make an iterator with everything inside # cut,tview1,tview2,...,tviewN # to repack them as # cut,cview for it in itertools.izip(cuts, *alliters): # create a new view cv = ChainView() # add all the treeviews cv.add(*it[1:]) #add it to the list of views chainviews[it[0]] = cv return chainviews
def splityields(self, regions, extra=None): plots = odict.OrderedDict() for rname, rcut in regions.iteritems(): cut = rcut if not extra else '(%s) && (%s)' % (rname, extra) plots[rname] = self.yields(cut) return plots
def yieldsflow(self, cuts, options=''): '''Does it make sense to have a double step? In a way yes, because otherwise one would have to loop over all the events for each step ''' views = self.views(cuts) return odict.OrderedDict([(n, v.yields(options=options)) for n, v in views.iteritems()])
class wwcutsB: wwcommon = odict.OrderedDict([ ('trigger', 'trigger==1.'), ('min #slash{E}_{T}', 'pfmet>20.'), ('m_{ll}', 'mll>12'), # ema7 ('Z_{veto}', '(zveto==1||!sameflav)'), ('min proj #slash{E}_{T}', 'mpmet>20.'), # ema9 ('Soft #mu veto', 'bveto_mu==1'), ('Extra Lepton', 'nextra==0'), ('B veto', '(bveto_ip==1 && (nbjettche==0 || njet>3) )'), ('p_{T}^{ll}', 'ptll>%f' % ptllCut), # ema 14 #('Extra Jet', 'njet<4'), ]) wwmin = odict.OrderedDict([ ('trigger', 'trigger==1.'), ('min #slash{E}_{T}', 'pfmet>20.'), ('m_{ll}', 'mll>12'), # ema7 ('min proj #slash{E}_{T}', 'mpmet>20.'), # ema9 ('Soft #mu veto', 'bveto_mu==1'), ('Extra Lepton', 'nextra==0'), #('Extra Jet', 'njet<4'), ]) #dy cuts dphilljj = '(njet==0 || njet==1 || (dphilljetjet<pi/180.*165. || !sameflav ) )' # met cuts met = '( !sameflav || ( (njet!=0 || dymva1>0.88) && (njet!=1 || dymva1>0.84) && ( njet==0 || njet==1 || (pfmet > 45.0)) ) )' mpmet = '( !sameflav || ( (njet>1 || (mpmet>45 && dphiveto)) && ( njet==0 || njet==1 || (pfmet > 45.0)) ) )' wwcommon.update([ ('d#Phi_{lljj}', dphilljj), ('dymva', met), ]) wwmin.update([ ('d#Phi_{lljj}', dphilljj), ]) zerojet = 'njet == 0' onejet = 'njet == 1' vbf = '(njet >= 2 && njet <= 3 && (jetpt3 <= 30 || !(jetpt3 > 30 && ( (jeteta1-jeteta3 > 0 && jeteta2-jeteta3 < 0) || (jeteta2-jeteta3 > 0 && jeteta1-jeteta3 < 0))))) '
def makeanalysers(latinos, path, flow, lumi): analysers = odict.OrderedDict() for n in latinos.iterkeys(): samples a = TreeAnalyser(latinos.latinos2samples(n, path), flow) a.lumi = lumi if n != 'Data' else 1 analysers[n] = a return analysers
def _makenumaps(self): # make a map of the variables (nuisances+r) associated to each process per bin # used for filtering later # this is list comprehension madness # slimmed to nuisance->processes map # the forula is v != 0 ( and not v > 0) to include cases where v is a list (asym errors) nu2procs = odict.OrderedDict([ (n, [p for p, v in e[self._bin].iteritems() if v != 0.]) for (n, nf, pf, a, e) in self._DC.systs ]) # init the new array proc2nus = odict.OrderedDict([(p, []) for p in self._processes]) for (n, nf, pf, a, e) in self._DC.systs: try: for p, val in e[self._bin].iteritems(): # if the variation is not 0, append the nuisance to the process # could be a non-zer0 float or a list. What if it is none? if val != 0: proc2nus[p].append(n) except KeyError: self._log.debug('No processes in bin %s for nuisance %s' % (self._bin, n)) continue # filter out the self._nu2procs = {} for n, ps in nu2procs.iteritems(): if len(ps) != 0: self._nu2procs[n] = ps else: self._log.debug('Nuisance %s does not affect bin %s', n, self._bin) # add the signal strength self._nu2procs['r'] = self._DC.signals self._proc2nus = proc2nus # add the signal strength to the parameters for s in self._DC.signals: if s in self._proc2nus: self._proc2nus[s].append('r')
def _ensureviews(self, force=False): if force: self._deleteentries() if not self._views: self._views = odict.OrderedDict() self._modified = True if self._modified: self._log.debug('modified cuts!') self._purgeviews(self._cuts, self._views) self._growviews(self._cuts, self._views) self._modified = False return self._views
def hww0j1jcb(mass): mcuts = wwnamedcuts.mcuts i = wwnamedcuts.masses.index(mass) cuts = odict.OrderedDict() cuts['mll_max'] = 'mll < %.1f' % mcuts['mllmax'][i] cuts['pt1_min'] = 'pt1 > %.1f' % mcuts['pt1min'][i] cuts['pt2_min'] = 'pt2 > %.1f' % mcuts['pt2min'][i] cuts['dphill'] = 'dphill < %s' % mcuts['dphimax'][i] cuts['mth'] = '(mth > %.1f && mth < %.1f)' % (mcuts['mtmin'][i], mcuts['mtmax'][i]) return cuts
def plotsflow(self, name, varexp, cuts, options='', bins=None, *args, **kwargs): views = self.views(cuts) return odict.OrderedDict([(n, v.plot('%s_%s' % (name, n), varexp, cuts, options, bins)) for n, v in views.iteritems()])
def splitplot(self, name, varexp, regions, options='', bins=None, extra=None, postprocess=None): plots = odict.OrderedDict() for rname, rcut in regions.iteritems(): cut = rcut if not extra else '(%s) && (%s)' % (rname, extra) plots[rname] = self.plot(name, varexp, options, bins, cut, postprocess) return plots
def main(): usage = 'usage: %prog tag -i dir -o dir' parser = optparse.OptionParser(usage) parser.add_option('-i', '--input', dest='inputdir', help='Input directory') parser.add_option('-o', '--output', dest='outputdir', help='Output directory') (opt, args) = parser.parse_args() if opt.inputdir is None: parser.error('No input directory defined') if not args: parser.error('No tag defined: ' + ' '.join(allowedtags)) tag = args[0] dir = opt.inputdir outdir = opt.outputdir if outdir: if outdir[:-1] != '/': outdir += '/' os.system('mkdir -p ' + outdir) filenames = glob.glob(dir + '/hww*' + '*' + tag + '*.txt') filenames.sort() if tag == 'sf_0j': bindict = bindict_sf_0j plottag = plottag_sf_0j if tag == 'sf_1j': bindict = bindict_sf_1j plottag = plottag_sf_1j if tag == 'of_0j': bindict = bindict_of_0j plottag = plottag_of_0j if tag == 'of_1j': bindict = bindict_of_1j plottag = plottag_of_1j if tag == 'comb_0j': bindict = bindict_0j plottag = plottag_0j if tag == 'comb_1j': bindict = bindict_1j plottag = plottag_1j if tag == 'allcomb': bindict = bindict_all plottag = plottag_all if tag == 'comb_0j1j': bindict = bindict_comb_0j1j plottag = plottag_comb_0j1j if tag == 'comb_of': bindict = bindict_comb_of plottag = plottag_comb_of # run the maxlikelihood thing for file in filenames: maxlikelihoodfit(file, outdir) fits = odict.OrderedDict() norms = odict.OrderedDict() for file in filenames: if not 'comb' in tag and '0j1j' in file: continue ## if '0j1j' in file: ## continue if '0j1j2j' in file: continue mass = file.split('.')[-3].replace('mH', '') print mass # run the normalization to txt macro f = file.split('/')[-1].replace('.txt', '_mlfit.root') print f fit = mlfitnormstotext(f, outdir, tag) print 'kkk fit' print fit # read the datacards norm = readdatacard(file) fits[mass] = fit norms[mass] = norm print norm # print norms text = open(tag + '_normalization.txt', 'w') print '=========================================================' print 'channel: ' + tag for mass in norms: print '-------------------------------------------------------' print '***' + mass + '***' print 'process'.ljust(15) + 'nominal'.ljust( 15) + 'fit: s+b (ratio)'.ljust(20) + 'fit: b (ratio)'.ljust(20) ## for proc in norms[mass]: for proc in fits[mass]: ## if proc == 'DYTT': ## continue ## if proc == 'wzttH': ## continue n = norms[mass][proc] sb = fits[mass][proc][0] b = fits[mass][proc][1] if not float(n) == 0.: r_sb = float(sb) / float(n) r_b = float(b) / float(n) else: r_sb = 0 r_b = 0 ## print proc.ljust(15)+norms[mass][proc].ljust(15)+(fits[mass][proc][0]+' '+str(float(fits[mass][proc][0])/float(norms[mass][proc])).ljust(15)+fits[mass][proc][1].ljust(15) ## print proc.ljust(15)+n.ljust(15)+(sb+' ('+str('%.2f' % r_sb)+')').ljust(20)+(b+' ('+str('%.2f' % r_b)+')').ljust(20) print >> text, '=========================================================' print >> text, 'channel: ' + tag for mass in norms: print >> text, '-------------------------------------------------------' print >> text, '***' + mass + '***' print >> text, 'process'.ljust(15) + 'nominal'.ljust( 15) + 'fit: s+b (ratio)'.ljust(20) + 'fit: b (ratio)'.ljust(20) ## for proc in norms[mass]: for proc in fits[mass]: ## if proc == 'DYTT': ## continue ## if proc == 'wzttH': ## continue n = norms[mass][proc] sb = fits[mass][proc][0] b = fits[mass][proc][1] if not float(n) == 0.: r_sb = float(sb) / float(n) r_b = float(b) / float(n) else: r_sb = 0 r_b = 0 # print >> text, proc.ljust(15)+norms[mass][proc].ljust(15)+(fits[mass][proc][0]+' '+str(float(fits[mass][proc][0])/float(norms[mass][proc])).ljust(15)+fits[mass][proc][1].ljust(15) print >> text, proc.ljust(15) + n.ljust(15) + ( sb + ' (' + str('%.2f' % r_sb) + ')').ljust(20) + (b + ' (' + str('%.2f' % r_b) + ')').ljust(20) for ptag in plottag: print 'ptag', ptag ## make 2-dim histograms c_sb = ROOT.TCanvas() c_b = ROOT.TCanvas() c_sb.SetLeftMargin(0.15) c_b.SetLeftMargin(0.15) n_mass = len(norms) n_proc = 12 ## n_proc = len(bindict) h_sb = ROOT.TH2D('h', 'h', n_mass, 0., n_mass, n_proc, 0., n_proc) h_sb.GetXaxis().SetTitle('mass') ## h_sb.GetYaxis().SetTitle('process') h_sb.SetMaximum(1.5) h_sb.SetMinimum(-0.00001) title_sb = 'N_{fit_{S+B}} / N_{nominal} - ' + tag h_sb.SetTitle(title_sb) h_b = ROOT.TH2D('h_b', 'h_b', n_mass, 0., n_mass, n_proc, 0., n_proc) h_b.GetXaxis().SetTitle('mass') ## h_b.GetYaxis().SetTitle('process') h_b.SetMaximum(1.5) h_b.SetMinimum(-0.00001) title_b = 'N_{fit_{B}} / N_{nominal} - ' + tag h_b.SetTitle(title_b) i = 1 for mass in norms: print '++++++++++++++++' print mass print norms[mass] print fits[mass] h_sb.GetXaxis().SetBinLabel(i, mass) h_b.GetXaxis().SetBinLabel(i, mass) for proc in fits[mass]: if ptag not in proc: continue n = norms[mass][proc] sb = fits[mass][proc][0] b = fits[mass][proc][1] r_sb = float(sb) / float(n) r_b = float(b) / float(n) j = bindict[proc] ## h_sb.GetYaxis().SetBinLabel(j,proc) ## h_b.GetYaxis().SetBinLabel(j,proc) print proc, j, mass, r_sb h_sb.SetBinContent(i, j, r_sb) h_b.SetBinContent(i, j, r_b) i += 1 for proc in bindict: if ptag not in proc: continue j = bindict[proc] h_sb.GetYaxis().SetBinLabel(j, procDict[proc]) h_b.GetYaxis().SetBinLabel(j, procDict[proc]) ## h_sb.GetYaxis().SetBinLabel(j,proc) ## h_b.GetYaxis().SetBinLabel(j,proc) ROOT.gROOT.SetBatch(True) ROOT.gStyle.SetPalette(1) ROOT.gStyle.SetOptStat(0) ROOT.gStyle.SetOptTitle(1) ROOT.gStyle.SetPaintTextFormat('3.3g') c_sb.cd() h_sb.Draw('COLZ TEXT') c_sb.Print(tag + '_normalization_sbFit_' + ptag + '.pdf') c_b.cd() h_b.Draw('COLZ TEXT') c_b.Print(tag + '_normalization_bFit_' + ptag + '.pdf') rf = ROOT.TFile(tag + '_normalization_' + ptag + '.root', 'RECREATE') rf.cd() c_sb.Write() c_b.Write() ROOT.gStyle.Write('theStyle') rf.Write() rf.Close()
def entriesflow(self, cut=None): '''TODO: use the entrylist''' self._ensureviews() return odict.OrderedDict([(n, v.entries(cut)) for n, v in self._views.iteritems()])
def fitAndPlot(dcpath, opts): ''' 1. read the datacard 2. convert to ws 3. run combine 4. open get the mlfit rootfile 1-4 don't need to know the content of the card, only to check that there are shapes inside ''' remass = re.compile('mH(\d*)') m = remass.search(dcpath) if not m: raise ValueError('Mass not found! Name your datacards properly!') print 'Mass', m.group(1) opt.mass = int(m.group(1)) shapepath = os.path.join(os.getenv('CMSSW_BASE'), 'src/HWWAnalysis/ShapeAnalysis') print 'Shape directory is', shapepath ROOT.gInterpreter.ExecuteMacro(shapepath + '/macros/LatinoStyle2.C') # 1. load the datacard dcfile = open(dcpath, 'r') class DCOptions: pass options = DCOptions() options.stat = False options.bin = True options.noJMax = False options.nuisancesToExclude = [] options.nuisancesToRescale = [] options.fileName = dcpath options.out = None options.cexpr = False options.fixpars = False options.libs = [] options.verbose = 0 options.poisson = 0 options.mass = opt.mass DC = parseCard(dcfile, options) if not DC.hasShapes: print 'This datacard has no shapes!' print dcpath sys.exit(-1) # if len(DC.bins) != 1: # raise ValueError('Only 1 bin datacards supported at the moment: '+', '.join(DC.bins)) # 2. convert to ws wspath = os.path.splitext(dcpath)[0] + '_workspace.root' logging.debug('Working with workspace %s', wspath) mkws = (not os.path.exists(wspath) or os.path.getmtime(wspath) < os.path.getmtime(dcpath) or opts.clean) if mkws: # workspace + parameters = shapes print 'Making the workspace...', sys.stdout.flush() os.system('text2workspace.py %s -o %s' % (dcpath, wspath)) print 'done.' ROOT.gSystem.Load('libHiggsAnalysisCombinedLimit') wsfile = ROOT.TFile.Open(wspath) if not wsfile.__nonzero__(): raise IOError('Could not open ' + wspath) w = wsfile.Get('w') w.saveSnapshot('clean', w.allVars()) # run combine if requested if opt.usefit: mlfpath = opt.usefit print '-' * 80 print 'Using results in', mlfpath print '-' * 80 if not os.path.exists(mlfpath): raise IOError('Fit result file %s not found.' % mlfpath) else: # 3.0 prepare the temp direcotry import tempfile mlfdir = opt.tmpdir if opt.tmpdir else tempfile.mkdtemp( prefix='mlfit_') hwwtools.ensuredir(mlfdir) # 3.1 go to the tmp dir here = os.getcwd() os.chdir(mlfdir) # 3.2 mlcmd = 'combine -M MaxLikelihoodFit --saveNormalizations ' + os.path.join( here, wspath) logging.debug(mlcmd) print 'Fitting the workspace...', sys.stdout.flush() if opts.fit: os.system(mlcmd) os.chdir(here) print 'done.' # 3.3 set the max-like fit results path mlfpath = os.path.join(mlfdir, 'mlfit.root') # 4. open the output and get the normalizations mlffile = ROOT.TFile.Open(mlfpath) if not mlffile.__nonzero__(): raise IOError('Could not open ' + mlfpath) model_s = w.pdf('model_s') model_b = w.pdf('model_b') res_s = mlffile.Get('fit_s') res_b = mlffile.Get('fit_b') sig_fit = ( model_s, res_s.floatParsFinal(), mlffile.Get('norm_fit_s'), ) bkg_fit = ( model_b, res_b.floatParsFinal(), mlffile.Get('norm_fit_b'), ) print 'List of bins found', ', '.join(DC.bins) bin = DC.bins[0] modes = odict.OrderedDict([ ('init', (model_s, res_s.floatParsInit(), None)), #(None, None, model_s) ('bkg', bkg_fit), ('sig', sig_fit), ]) # experimental MB = ShapeBuilder(DC, options) allshapes = {} nuisancemap = {} for mode, fit in modes.iteritems(): print 'Analysing model:', mode logging.debug('Plotting %s', fit) allshapes[mode] = {} for bin in DC.bins: print ' - Bin:', bin coroner = Coroner(bin, DC, MB, w, fit) coroner.errmode = opt.errmode shapes, errs = coroner.perform() nuisancemap[bin] = coroner.nuisances() if opts.output: printshapes(shapes, errs, mode, opts, bin, DC.signals, DC.processes) allshapes[mode][bin] = (shapes, errs) if opts.dump: logging.debug('Dumping histograms to %s', opts.dump) dumpdir = os.path.dirname(opt.dump) # open rootfile if dumpdir: hwwtools.ensuredir(dumpdir) dump = ROOT.TFile.Open(opts.dump, 'recreate') here = ROOT.gDirectory.func() dump.cd() idir = dump.mkdir('info') idir.cd() # save the list of nuisances nuisances = ROOT.TObjArray() #ROOT.std.vector('string')() for (n, nf, pf, a, e) in DC.systs: nuisances.Add(ROOT.TObjString(n)) nuisances.Write('nuisances', ROOT.TObject.kSingleKey) # save the list of processes processes = ROOT.TObjArray() #ROOT.std.vector('string')() for p in DC.processes: processes.Add(ROOT.TObjString(p)) processes.Write('processes', ROOT.TObject.kSingleKey) # save the list of signals signals = ROOT.TObjArray() #ROOT.std.vector('string')() for s in DC.signals: signals.Add(ROOT.TObjString(s)) signals.Write('signals', ROOT.TObject.kSingleKey) # save the list of nuisances per bin nuisbybin = ROOT.TMap() for bin, nuis in nuisancemap.iteritems(): tnuis = ROOT.TObjArray() for n in nuis: tnuis.Add(ROOT.TObjString(n)) nuisbybin.Add(ROOT.TObjString(bin), tnuis) nuisbybin.Write('map_binnuisances', ROOT.TObject.kSingleKey) for mode, allbins in allshapes.iteritems(): # make the main directory mdir = dump.mkdir(mode) mdir.cd() # info directory idir = mdir.mkdir('info') idir.cd() # save the fit parameters model, pars, norms = modes[mode] pars.Write('parameters') # save the list of signals # save the bin plots for bin, (shapes, errs) in allbins.iteritems(): # bin directory bdir = mdir.mkdir(bin) bdir.cd() for s in shapes.itervalues(): # print s s.Write() for p, nugs in errs.iteritems(): dp = bdir.mkdir(p) dp.cd() for g in nugs.itervalues(): g.Write() bdir.cd() try: modelall = errs['model']['all'].Clone('model_errs') modelall.SetTitle('model_errs') modelall.Write() except: logging.warn('Error graph model:err not found') mdir.cd() dump.Write() dump.Close() here.cd()
#print args, len(args) if len(args) is not 2: print ' Usage: %prog <path>' sys.exit(0) f = ROOT.TFile(args[1]) fStates = ['mm','me','em','ee','ll'] d = {} prefix='yieldAnalyzer/' # prefix='diLepSel/' for s in fStates: d[s] = odict.OrderedDict() name = prefix+s+'Yield' counters = f.Get(name) counters.Scale(kfact) if not counters.__nonzero__(): raise NameError('histogram '+name+' not found in '+args[1]) lastBin = counters.GetNbinsX() d[s]['entries'] = '%.2f' % counters.GetBinContent(1) for i in range(2,lastBin+1): ax = counters.GetXaxis() labelAbs = ax.GetBinLabel(i) labelRel = ax.GetBinLabel(i)+'/'+ax.GetBinLabel(i-1) entries = counters.GetBinContent(1)
def makeEfficiencyTable(self, name): print name # save the old dir oldDir = ROOT.gDirectory #and go to the new one path = os.path.dirname(name) # but don't write the plots sentry = utils.TH1AddDirSentry() data = self.getDataHistograms(name) mc = self.getMCHistograms(name) self.normalize(mc) #, self.mcSamples) mc = self.sum(mc) #,self.mcSamples) data = self.sum(data) (data0, sample0) = data[0] nbins = data0.GetNbinsX() print data0.GetBinContent(data0.GetNbinsX()) ## for (h,s) in mc: ## print s.legend,h.GetBinContent(h.GetNbinsX()) tableValue = odict.OrderedDict() efficiency = odict.OrderedDict() tableEntry = odict.OrderedDict() print '________________________________________________________________________________________________________________________________________________________________________________________________' print '| cut:'.ljust(18), sample0.legend.ljust(18), '|'.join( s.legend.ljust(18) for (h, s) in mc) print '________________________________________________________________________________________________________________________________________________________________________________________________' for i in range(nbins): j = i + 1 axis = data0.GetXaxis() cut = axis.GetBinLabel(j) tableValue[cut] = odict.OrderedDict() efficiency[cut] = odict.OrderedDict() tableEntry[cut] = odict.OrderedDict() tableEntry[cut]['cut'] = cut preEntry = data0.GetBinContent(i) entry = data0.GetBinContent(j) tableValue[cut][sample0.path] = str('%.1f' % (entry)) if (preEntry == 0): efficiency[cut][sample0.path] = str(0.0) else: efficiency[cut][sample0.path] = str('%.1f' % (100 * (entry / preEntry))) tableEntry[cut][sample0.path] = tableValue[cut][ sample0.path] + ' (' + efficiency[cut][sample0.path] + '\%)' # get the MC for (h, s) in mc: preEntry = h.GetBinContent(i) entry = h.GetBinContent(j) tableValue[cut][s.path] = str('%.1f' % (entry)) if (preEntry == 0): efficiency[cut][s.path] = str(0.0) else: efficiency[cut][s.path] = str('%.1f' % (100 * (entry / preEntry))) tableEntry[cut][s.path] = tableValue[cut][ s.path] + ' (' + efficiency[cut][s.path] + '\%)' ## print cut.ljust(10),'|',tableValue[cut][sample0.path].ljust(10),'|',' | '.join( [ tableValue[cut][s.path].ljust(10) for (h,s) in mc]) print '|' + cut.ljust(18), '|', tableEntry[cut][ sample0.path].ljust(18).replace('\%', '%'), '|', ' | '.join([ tableEntry[cut][s.path].ljust(18).replace('\%', '%') for (h, s) in mc ]) + '|' ## print cut.ljust(20),efficiency[cut][sample0.path].ljust(20),' | '.join( [ efficiency[cut][s.path] for (h,s) in mc]) print '________________________________________________________________________________________________________________________________________________________________________________________________' print r'\documentclass[a4paper]{article}' print r'\begin{document}' print r'\begin{tabular}{|' + 'c|' * (len(mc) + 2) + '}' print r'\hline' print ' & ', sample0.legend, ' & $', ' & $'.join( s.legend.replace('+-', ' $\\pm$ ').replace('%', '\%').replace( '#', '\\') + '$' for (h, s) in mc), r'\\' print r'\hline' for line in tableEntry.iterkeys(): print tableEntry[line]['cut'], ' & ', tableEntry[line][ sample0.path], ' & ', ' & '.join( [tableEntry[line][s.path] for (h, s) in mc]), r'\\' print r'\hline' print r'\end{tabular}' print r'\end{document}'
class wwnamedcuts: wwcommon = odict.OrderedDict([ ('trigger', 'trigger == 1'), ('pt1', 'pt1>20'), ('pt2', 'pt2>10'), ('os', '(ch1*ch2)<0.5'), # don't use 0 ('trigger', 'trigger==1.'), ('pfmet', 'pfmet>20.'), ('mllmin', 'mll>12'), # ema7 ('zveto', 'zveto==1 || !sameflav'), ('mpmet', 'mpmet>20.'), # ema9 ('bveto_mu', 'bveto_mu==1'), ('nextra', 'nextra==0'), ('bveto_ip', 'bveto_ip==1 && nbjettche==0'), ('ptll', 'ptll>%.2f' % ptllCut), # ema 14 ('dphilljj', 'njet==0 || njet==1 || (dphilljetjet<pi/180.*165. || !sameflav )'), ('met', '!sameflav || ( (njet!=0 || dymva1>0.88) && (njet!=1 || dymva1>0.84) && ( njet==0 || njet==1 || pfmet > 45.0) )' ), ]) # commom cuts vbfcommon = wwcommon.copy() vbfcommon.update([ ('lepcnt1', 'abs(eta1 - (jeteta1+jeteta2)/2)/detajj < 0.5'), ('lepcnt2', 'abs(eta2 - (jeteta1+jeteta2)/2)/detajj < 0.5'), ]) vbfcutbased = vbfcommon.copy() vbfcutbased.update([ ('detajj', 'detajj>3.5'), ('mjj', 'mjj>500'), ]) vbfloshape = vbfcommon.copy() vbfloshape.update([ ('minpt1', 'pt1>20'), ('minpt2', 'pt2>0'), ('mth', 'mth>30 && mth<280'), ('mllmax', 'mll<200'), ]) vbfhishape = vbfcommon.copy() vbfhishape.update([ ('minpt1', 'pt1>50'), ('minpt2', 'pt2>0'), ('mth', 'mth>30 && mth<680'), ('mllmax', 'mll<600'), ]) zerojet = 'njet == 0' onejet = 'njet == 1' vbf = 'njet >= 2 && njet <= 3 && (jetpt3 <= 30 || !(jetpt3 > 30 && ( ( jeteta1 < jeteta3 && jeteta3 < jeteta2 ) || ( jeteta2 < jeteta3 && jeteta3 < jeteta1 ) )) ) ' # normal index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 masses = [ 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 170, 180, 190, 200, 250, 300, 350, 400, 450, 500, 550, 600 ] mcuts = {} mcuts['mtmin_vh'] = [ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 60, 60, 60, 60, 60, 100, 100, 100, 100, 100, 100 ] mcuts['mllmax_vh'] = [ 70, 70, 70, 80, 80, 90, 90, 100, 100, 100, 100, 100, 110, 120, 130, 250, 300, 350, 400, 450, 500, 550, 600 ] mcuts['mllmax_bdt'] = [ 70, 70, 70, 80, 80, 90, 90, 100, 100, 100, 100, 100, 110, 120, 130, 250, 300, 350, 400, 450, 500, 550, 600 ] mcuts['pt1min'] = [ 20, 20, 20, 23, 25, 25, 25, 25, 27, 27, 30, 34, 36, 38, 40, 55, 70, 80, 90, 110, 120, 130, 140 ] mcuts['pt2min'] = [ 10, 10, 10, 10, 10, 12, 15, 15, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25 ] mcuts['mllmax'] = [ 40, 40, 40, 43, 45, 45, 45, 45, 50, 50, 50, 50, 60, 80, 90, 150, 200, 250, 300, 350, 400, 450, 500 ] mcuts['dphimax'] = [ 115, 115, 115, 100, 90, 90, 90, 90, 90, 90, 60, 60, 70, 90, 100, 140, 175, 175, 175, 175, 175, 175, 175 ] mcuts['mtmin'] = [ 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 90, 110, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120 ] mcuts['mtmax'] = [ 110, 110, 120, 123, 125, 128, 130, 140, 150, 155, 160, 170, 180, 190, 200, 250, 300, 350, 400, 450, 500, 550, 600 ] # [ 110 , 115 , 120 , 125 , 130 , 135 , 140 , 145 , 150 , 155 , 160 , 170 , 180 , 190 , 200 , 250 , 300 , 350 , 400 , 450 , 500 , 550 , 600 ] # automatic conversion to radiants mcuts['dphimax'] = [str(phi) + '*pi/180' for phi in mcuts['dphimax']] del phi @staticmethod def hww0j1jcb(mass): mcuts = wwnamedcuts.mcuts i = wwnamedcuts.masses.index(mass) cuts = odict.OrderedDict() cuts['mll_max'] = 'mll < %.1f' % mcuts['mllmax'][i] cuts['pt1_min'] = 'pt1 > %.1f' % mcuts['pt1min'][i] cuts['pt2_min'] = 'pt2 > %.1f' % mcuts['pt2min'][i] cuts['dphill'] = 'dphill < %s' % mcuts['dphimax'][i] cuts['mth'] = '(mth > %.1f && mth < %.1f)' % (mcuts['mtmin'][i], mcuts['mtmax'][i]) return cuts @staticmethod def vbfcb(mass): mcuts = wwnamedcuts.mcuts mthmin = 30. i = wwnamedcuts.masses.index(mass) cuts = odict.OrderedDict() cuts['mll_max'] = 'mll < %s' % mcuts['mllmax'][i] cuts['pt1_min'] = 'pt1 > %.1f' % mcuts['pt1min'][i] cuts['pt2_min'] = 'pt2 > %.1f' % mcuts['pt2min'][i] cuts['dphill'] = 'dphill < %s' % mcuts['dphimax'][i] cuts['mth'] = '(mth > %.1f && mth < %.1f)' % (mthmin, mcuts['mtmax'][i]) return cuts @staticmethod def hww0j1jcbfull(mass): cuts = wwnamedcuts.wwcommon.copy() cuts.update(wwnamedcuts.hww0j1jonly(mass)) return cuts @staticmethod def vbfcbfull(mass): cuts = wwnamedcuts.vbfcutbased.copy() cuts.update(wwnamedcuts.vbfcb(mass)) return cuts
_defaults = odict.OrderedDict([ ('ggH', { 'color': ROOT.kRed + 1, 'label': 'ggH', }), ('vbfH', { 'color': ROOT.kRed + 2, 'label': 'qqH', }), ('wzttH', { 'color': ROOT.kRed + 3, 'label': 'VH', }), ('VH', { 'color': ROOT.kRed + 3, 'label': 'VH', }), ('wH', { 'color': ROOT.kRed - 3, 'label': 'wH', }), ('zH', { 'color': ROOT.kRed - 4, 'label': 'zH', }), ('VV', { 'color': ROOT.kAzure - 2, 'label': 'WZ/ZZ', }), ('DYTT', { 'color': ROOT.kGreen + 2, 'label': 'DY+jets', }), ('DYLL', { 'color': ROOT.kGreen + 3, 'label': 'DY+jets', }), ('DYee', { 'color': ROOT.kGreen + 3, 'label': 'DY+jets', }), ('DYmm', { 'color': ROOT.kGreen + 3, 'label': 'DY+jets', }), ('Vg', { 'color': ROOT.kMagenta + 1, 'label': 'V+#gamma', }), ('VgS', { 'color': ROOT.kMagenta + 2, 'label': 'V+#gamma*', }), ('WJet', { 'color': ROOT.kGray + 1, 'label': 'W+jets', }), ('Top', { 'color': ROOT.kYellow, 'label': 'top', }), ('ttbar', { 'color': ROOT.kYellow - 4, 'label': 't#bar{t}', }), ('tW', { 'color': ROOT.kOrange - 2, 'label': 'tW', }), ('WW', { 'color': ROOT.kAzure - 9, 'label': 'WW', }), ('ggWW', { 'color': ROOT.kAzure - 7, 'label': 'WW', }), ])