def main(): parser = optparse.OptionParser(usage=usage) parser.add_option('-i', '--input-dir', default='./out/fakerate') parser.add_option('-o', '--output-dir', default='./out/fakerate/efficiencies') parser.add_option('-l', '--lepton', default='el', help='either el or mu') parser.add_option('-m', '--mode', help='emu') parser.add_option('-t', '--tag', help='tag used to select the input files (e.g. Apr_04)') parser.add_option('-f', '--fill-histos', action='store_true', default=False, help='force fill (default only if needed)') parser.add_option('-T', '--tight-def', help='on-the-fly tight def, one of defs in fakeUtils.py: fakeu.lepIsTight_std, etc.') parser.add_option('-v', '--verbose', action='store_true', default=False) (options, args) = parser.parse_args() inputDir = options.input_dir outputDir = options.output_dir lepton = options.lepton mode = options.mode tag = options.tag verbose = options.verbose if not tag : parser.error('tag is a required option') if lepton not in ['el', 'mu'] : parser.error("invalid lepton '%s'"%lepton) validModes = ['emu'] if mode not in validModes : parser.error("invalid mode %s"%mode) tupleStem, treeName = filter(lambda _: _[0]==mode, fakeu.tupleStemsAndNames)[0] templateInputFilename = "*_%(stem)s_tuple_%(tag)s.root" % {'tag':tag, 'stem':tupleStem} templateOutputFilename = "%(stem)s_%(l)s_eff.root" % {'stem':tupleStem.replace('tuple','histos'), 'l':lepton} outputFileName = os.path.join(outputDir, templateOutputFilename) cacheFileName = outputFileName.replace('.root', '_'+mode+'_cache.root') doFillHistograms = options.fill_histos or not os.path.exists(cacheFileName) onthefly_tight_def = eval(options.tight_def) if options.tight_def else None # eval will take care of aborting on typos optionsToPrint = ['inputDir', 'outputDir', 'mode', 'tag', 'doFillHistograms', 'cacheFileName', 'onthefly_tight_def'] if verbose : print "working from %s"%os.getcwd() print "being called as : %s"%' '.join(os.sys.argv) print "options parsed:\n"+'\n'.join(["%s : %s"%(o, eval(o)) for o in optionsToPrint]) print 'input filenames: ',os.path.join(inputDir, templateInputFilename) # collect inputs tupleFilenames = glob.glob(os.path.join(inputDir, templateInputFilename)) samples = setSameGroupForAllData(fastSamplesFromFilenames(tupleFilenames, verbose)) samplesPerGroup = collections.defaultdict(list) filenamesPerGroup = collections.defaultdict(list) mkdirIfNeeded(outputDir) for s, f in zip(samples, tupleFilenames) : samplesPerGroup[s.group].append(s) filenamesPerGroup[s.group].append(f) vars = ['pt', 'pt_eta'] groups = [g for g in samplesPerGroup.keys() if g is not 'higgs'] if lepton=='el' : groups = [g for g in groups if g is not 'heavyflavor'] sourcesThisMode = ['real', 'conv', 'heavy', 'light', 'unknown'] if lepton=='el' else ['real', 'heavy', 'light', 'unknown'] #fill histos if doFillHistograms : start_time = time.clock() num_processed_entries = 0 histosPerGroupPerSource = bookHistosPerSamplePerSource(vars, groups, sourcesThisMode, mode=mode) for group in groups: filenames = filenamesPerGroup[group] sources = histosPerGroupPerSource.keys() histosThisGroupPerSource = dict((s, histosPerGroupPerSource[s][group]) for s in sources) histosAnyGroupPerSource = dict((s, histosPerGroupPerSource[s]['anygroup']) for s in sources) if group!='data' else {} chain = r.TChain(treeName) [chain.Add(fn) for fn in filenames] if verbose: print "%s : %d entries"%(group, chain.GetEntries()) is_data = group in ['data'] print 'is_data ',is_data num_processed_entries += fillHistos(chain=chain, histosPerSource=histosThisGroupPerSource, histosPerSourceAnygroup=histosAnyGroupPerSource, lepton=lepton, onthefly_tight_def=onthefly_tight_def, verbose=verbose) writeHistos(cacheFileName, histosPerGroupPerSource, verbose) end_time = time.clock() delta_time = end_time - start_time one_minute = 60 if verbose: print ("processed {0:d} entries ".format(num_processed_entries) +"in "+("{0:d} min ".format(int(delta_time/60)) if delta_time>one_minute else "{0:.1f} s ".format(delta_time)) +"({0:.1f} kHz)".format(num_processed_entries/delta_time)) # plot histos histosPerGroupPerSource = fetchHistos(cacheFileName, histoNamesPerSamplePerSource(vars, groups, sourcesThisMode, mode), verbose) # effs = computeEfficiencies(histosPerGroupPerSource) # still [var][gr][source][l/t] for v in vars: varIs1D, varIs2D = v=='pt', v=='pt_eta' densThisSourceThisVar = dictSum(dict((s, histosPerGroupPerSource[v]['anygroup'][s]['loose']) for s in sourcesThisMode), {'data' : histosPerGroupPerSource[v]['data']['unknown']['loose']}) numsThisSourceThisVar = dictSum(dict((s, histosPerGroupPerSource[v]['anygroup'][s]['tight']) for s in sourcesThisMode), {'data' : histosPerGroupPerSource[v]['data']['unknown']['tight']}) if varIs1D: lT, lX, lY = '#varepsilon(T|L)', 'p_{T} [GeV]', '#varepsilon(T|L)' cname = 'stack_loose_'+lepton lT, lY = 'loose '+lepton+', denominator to #varepsilon(T|L)', '#varepsilon(T|L)' title = lT+' '+'anysource'+' '+lepton+';'+lX+';'+lY plotStackedHistosWithData(densThisSourceThisVar, outputDir, cname, title, colors=fakeu.colorsFillSources(), verbose=verbose) cname = 'stack_tight_'+lepton lT, lY = 'tight '+lepton+', numerator to #varepsilon(T|L)', '#varepsilon(T|L)' title = lT+' '+'anysource'+' '+lepton+';'+lX+';'+lY plotStackedHistosWithData(numsThisSourceThisVar, outputDir, cname, title, colors=fakeu.colorsFillSources(), verbose=verbose) for s in sourcesThisMode: for v in vars: groups = first(histosPerGroupPerSource).keys() varIs1D, varIs2D = v=='pt', v=='pt_eta' # effsThisSourceThisVar = dict((g, effs[v][g][s]) for g in groups) densThisSourceThisVar = dictSum(dict((g, histosPerGroupPerSource[v][g][s]['loose']) for g in groups if g not in ['anygroup','data']), {'data' : histosPerGroupPerSource[v]['data']['unknown']['loose']}) numsThisSourceThisVar = dictSum(dict((g, histosPerGroupPerSource[v][g]['unknown']['tight']) for g in groups if g not in ['anygroup','data']), {'data' : histosPerGroupPerSource[v]['data']['unknown']['tight']}) if varIs1D: # cname = 'eff_'+lepton+'_'+s lT, lX, lY = '#varepsilon(T|L)', 'p_{T} [GeV]', '#varepsilon(T|L)' # title = lT+' '+s+' '+lepton+';'+lX+';'+lY # zoomIn = True # fakeu.plot1dEfficiencies(effsThisSourceThisVar, cname, outputDir, title, zoomIn) cname = 'stack_loose_'+lepton+'_'+s lT, lY = 'loose '+lepton+', denominator to #varepsilon(T|L)', '#varepsilon(T|L)' title = lT+' '+s+' '+lepton+';'+lX+';'+lY plotStackedHistosWithData(densThisSourceThisVar, outputDir, cname, title, colors=SampleUtils.colors, verbose=verbose) cname = 'stack_tight_'+lepton+'_'+s lT, lY = 'tight '+lepton+', numerator to #varepsilon(T|L)', '#varepsilon(T|L)' title = lT+' '+s+' '+lepton+';'+lX+';'+lY plotStackedHistosWithData(numsThisSourceThisVar, outputDir, cname, title, colors=SampleUtils.colors, verbose=verbose) # elif varIs2D: # cname = 'eff_'+lepton+'_'+s # lT, lX, lY = '#varepsilon(T|L)', 'p_{T} [GeV]', '#eta' # title = lT+' '+s+' '+lepton+';'+lX+';'+lY # fakeu.plot2dEfficiencies(effsThisSourceThisVar, cname, outputDir, title, zoomIn=zoomIn) # writeHistos(outputFileName, effs, verbose) if verbose : print "saved scale factors to %s" % outputFileName
def main(): parser = optparse.OptionParser(usage=usage) parser.add_option('-i', '--input-dir', default='./out/fakerate') parser.add_option('-o', '--output-dir', default='./out/fakerate/efficiencies') parser.add_option('-l', '--lepton', default='el', help='either el or mu') parser.add_option('-m', '--mode', help='real, conv, hflf') parser.add_option('-t', '--tag', help='tag used to select the input files (e.g. Apr_04)') parser.add_option('-f', '--fill-histos', action='store_true', default=False, help='force fill (default only if needed)') parser.add_option('-T', '--tight-def', help='on-the-fly tight def, one of defs in fakeUtils.py: fakeu.lepIsTight_std, etc.') parser.add_option('-v', '--verbose', action='store_true', default=False) (options, args) = parser.parse_args() inputDir = options.input_dir outputDir = options.output_dir lepton = options.lepton mode = options.mode tag = options.tag verbose = options.verbose if not tag : parser.error('tag is a required option') if lepton not in ['el', 'mu'] : parser.error("invalid lepton '%s'"%lepton) validModesEl = ['real', 'hflf'] + ['conv'] validModesMu = ['real', 'hflf'] if mode not in (validModesEl if lepton=='el' else validModesMu) : parser.error("invalid mode %s"%mode) tupleStem, treeName = {'conv' : ('mcconv_tuple', 'ConversionExtractionRegion'), 'hflf' : ('mcqcd_tuple', 'HfLfExtractionRegion'), 'real' : ('mcreal_tuple', 'RealExtractionRegion') }[mode] templateInputFilename = "*_%(stem)s_%(tag)s.root" % {'tag':tag, 'stem':tupleStem} templateOutputFilename = "%(stem)s_%(l)s_eff.root" % {'stem':tupleStem.replace('tuple','histos'), 'l':lepton} outputFileName = os.path.join(outputDir, templateOutputFilename) cacheFileName = outputFileName.replace('.root', '_'+mode+'_cache.root') doFillHistograms = options.fill_histos or not os.path.exists(cacheFileName) onthefly_tight_def = eval(options.tight_def) if options.tight_def else None # eval will take care of aborting on typos optionsToPrint = ['inputDir', 'outputDir', 'mode', 'tag', 'doFillHistograms', 'cacheFileName', 'onthefly_tight_def'] if verbose : print "working from %s"%os.getcwd() print "being called as : %s"%' '.join(os.sys.argv) print "options parsed:\n"+'\n'.join(["%s : %s"%(o, eval(o)) for o in optionsToPrint]) # collect inputs print 'input filenames: ',os.path.join(inputDir, templateInputFilename) tupleFilenames = glob.glob(os.path.join(inputDir, templateInputFilename)) samples = setSameGroupForAllData(fastSamplesFromFilenames(tupleFilenames, verbose)) samplesPerGroup = collections.defaultdict(list) filenamesPerGroup = collections.defaultdict(list) mkdirIfNeeded(outputDir) for s, f in zip(samples, tupleFilenames) : samplesPerGroup[s.group].append(s) filenamesPerGroup[s.group].append(f) vars = ['pt', 'pt_eta'] groups = [g for g in samplesPerGroup.keys() if not isDataSample(g) and not g=='higgs'] if lepton=='el' : groups = [g for g in groups if g!='heavyflavor'] sourcesThisMode = {'real' : ['real'], # use same convention as in FakeLeptonSources.h 'conv' : ['conv'], 'hflf' : ['heavy', 'light', 'qcd'] }[mode] #fill histos if doFillHistograms : start_time = time.clock() num_processed_entries = 0 histosPerGroupPerSource = bookHistosPerSamplePerSource(vars, groups, sourcesThisMode, mode=mode) for group in groups: filenames = filenamesPerGroup[group] histosThisGroupPerSource = dict((v, histosPerGroupPerSource[v][group]) for v in histosPerGroupPerSource.keys()) histosAnyGroupPerSource = dict((v, histosPerGroupPerSource[v]['anygroup']) for v in histosPerGroupPerSource.keys()) chain = r.TChain(treeName) [chain.Add(fn) for fn in filenames] if verbose: print "%s : %d entries"%(group, chain.GetEntries()) num_processed_entries += fillHistos(chain, histosThisGroupPerSource, histosAnyGroupPerSource, lepton, mode, onthefly_tight_def=onthefly_tight_def, verbose=verbose) writeHistos(cacheFileName, histosPerGroupPerSource, verbose) end_time = time.clock() delta_time = end_time - start_time if verbose: print ("processed {0:d} entries ".format(num_processed_entries) +"in "+("{0:d} min ".format(int(delta_time/60)) if delta_time>60 else "{0:.1f} s ".format(delta_time)) +"({0:.1f} kHz)".format(num_processed_entries/delta_time)) # compute efficiencies histosPerGroupPerSource = fetchHistos(cacheFileName, histoNamesPerSamplePerSource(vars, groups, sourcesThisMode, mode), verbose) effs = computeEfficiencies(histosPerGroupPerSource) # still [var][gr][source][l/t] for s in sourcesThisMode: for v in vars: groups = first(effs).keys() varIs1D, varIs2D = v=='pt', v=='pt_eta' effsThisSourceThisVar = dict((g, effs[v][g][s]) for g in groups) densThisSourceThisVar = dict((g, histosPerGroupPerSource[v][g][s]['loose']) for g in groups if g!='anygroup') numsThisSourceThisVar = dict((g, histosPerGroupPerSource[v][g][s]['tight']) for g in groups if g!='anygroup') if varIs1D: cname = 'eff_'+lepton+'_'+s lT, lX, lY = '#varepsilon(T|L)', 'p_{T} [GeV]', '#varepsilon(T|L)' title = lT+' '+s+' '+lepton+';'+lX+';'+lY zoomIn = True fakeu.plot1dEfficiencies(effsThisSourceThisVar, cname, outputDir, title, zoomIn) cname = 'stack_loose_'+lepton+'_'+s lT, lY = 'loose '+lepton+', denominator to #varepsilon(T|L)', '#varepsilon(T|L)' title = lT+' '+s+' '+lepton+';'+lX+';'+lY plotParametrizedFractions.plotStackedHistos(densThisSourceThisVar, cname, outputDir, title) cname = 'stack_tight_'+lepton+'_'+s lT, lY = 'tight '+lepton+', numerator to #varepsilon(T|L)', '#varepsilon(T|L)' title = lT+' '+s+' '+lepton+';'+lX+';'+lY plotParametrizedFractions.plotStackedHistos(numsThisSourceThisVar, cname, outputDir, title) elif varIs2D: cname = 'eff_'+lepton+'_'+s lT, lX, lY = '#varepsilon(T|L)', 'p_{T} [GeV]', '#eta' title = lT+' '+s+' '+lepton+';'+lX+';'+lY fakeu.plot2dEfficiencies(effsThisSourceThisVar, cname, outputDir, title, zoomIn=zoomIn) writeHistos(outputFileName, effs, verbose) if verbose : print "saved scale factors to %s" % outputFileName
def main(): parser = optparse.OptionParser(usage=usage) parser.add_option('-i', '--input-dir', default='./out/fakerate') parser.add_option('-o', '--output-dir', default='./out/tight_variables_plots', help='dir for plots') parser.add_option('-l', '--lepton', default='el', help='either el or mu') parser.add_option('-r', '--region', help='one of the regions for which we saved the fake ntuples') parser.add_option('-t', '--tag', help='tag used to select the input files (e.g. Apr_04)') parser.add_option('-f', '--fill-histos', action='store_true', default=False, help='force fill (default only if needed)') parser.add_option('-v', '--verbose', action='store_true', default=False) (options, args) = parser.parse_args() inputDir = options.input_dir outputDir = options.output_dir lepton = options.lepton region = options.region tag = options.tag verbose = options.verbose if not tag : parser.error('tag is a required option') if lepton not in ['el', 'mu'] : parser.error("invalid lepton '%s'"%lepton) filestems, treenames = utils.verticalSlice(fakeu.tupleStemsAndNames) regions = filestems assert region in regions,"invalid region '%s', must be one of %s"%(region, str(regions)) templateInputFilename = "*_%(region)s_tuple_%(tag)s.root" % {'tag':tag, 'region':region} templateOutputFilename = "%(region)s_%(l)s_tight_plots.root" % {'region':region, 'l':lepton} treeName = treenames[regions.index(region)] outputDir = outputDir+'/'+region+'/'+lepton # split the output in subdirectories, so we don't overwrite things mkdirIfNeeded(outputDir) outputFileName = os.path.join(outputDir, templateOutputFilename) cacheFileName = outputFileName.replace('.root', '_'+region+'_cache.root') doFillHistograms = options.fill_histos or not os.path.exists(cacheFileName) optionsToPrint = ['inputDir', 'outputDir', 'region', 'tag', 'doFillHistograms'] if verbose : print "working from %s"%os.getcwd() print "being called as : %s"%' '.join(os.sys.argv) print "options:\n"+'\n'.join(["%s : %s"%(o, eval(o)) for o in optionsToPrint]) # collect inputs if verbose : print 'input files ',os.path.join(inputDir, templateInputFilename) tupleFilenames = glob.glob(os.path.join(inputDir, templateInputFilename)) samples = setSameGroupForAllData(fastSamplesFromFilenames(tupleFilenames, verbose)) if not samples : samples = [guessSampleFromFilename(f) for f in tupleFilenames] # if the fast guess didn't work, try the slow one samplesPerGroup = collections.defaultdict(list) filenamesPerGroup = collections.defaultdict(list) for s, f in zip(samples, tupleFilenames) : samplesPerGroup[s.group].append(s) filenamesPerGroup[s.group].append(f) vars = ['pt','eta','d0sig','z0SinTheta','etCone','ptCone','etConeCorr','ptConeCorr'] vars += ['relEtConeStd', 'relPtConeStd', 'relEtConeMod', 'relPtConeMod'] groups = samplesPerGroup.keys() sources = leptonSources #fill histos if doFillHistograms : lepLabel = "(probe %s)"%lepton histosPerGroup = bookHistosPerGroup(vars, groups, lepLabel=lepLabel) histosPerSource = bookHistosPerSource(vars, sources, lepLabel=lepLabel) for group in groups: isData = isDataSample(group) filenames = filenamesPerGroup[group] histosThisGroup = histosPerGroup[group] chain = r.TChain(treeName) [chain.Add(fn) for fn in filenames] print "%s : %d entries"%(group, chain.GetEntries()) fillHistos(chain, histosThisGroup, histosPerSource, isData, lepton, group, verbose) writeHistos(cacheFileName, {'perGroup':histosPerGroup, 'perSource':histosPerSource}, verbose) # compute scale factors histosPerGroup = fetchHistos(cacheFileName, histoNames(vars, groups), verbose) histosPerSource = fetchHistos(cacheFileName, histoNames(vars, sources), verbose) plotStackedHistos(histosPerGroup, outputDir+'/by_group', region, colors=SampleUtils.colors, verbose=verbose) plotStackedHistos(histosPerSource, outputDir+'/by_source', region, colors=fakeu.colorsFillSources(), verbose=verbose) plotIsoComparison(histosPerSource, outputDir+'/', region, lepton, verbose)
def main(): parser = optparse.OptionParser(usage=usage) parser.add_option('-i', '--input-dir', default='./out/fakerate') parser.add_option('-o', '--output-dir', default='./out/fake_el_scale_factor', help='dir for plots') parser.add_option('-l', '--lepton', default='el', help='either el or mu') parser.add_option('-r', '--region', help='where we want the compositions,' ' i.e. one of the regions for which we saved the fake nutples' ' (eg. ssinc1j_tuple*, emu_tuple*') parser.add_option('-s', '--syst-fudge', help='scale down main group (el:wjets, mu:bb/cc) to evaluate fraction syst unc') parser.add_option('-t', '--tag', help='tag used to select the input files (e.g. Apr_04)') parser.add_option('-f', '--fill-histos', action='store_true', default=False, help='force fill (default only if needed)') parser.add_option('-v', '--verbose', action='store_true', default=False) (options, args) = parser.parse_args() inputDir = options.input_dir outputDir = options.output_dir lepton = options.lepton systfudge = options.syst_fudge region = options.region tag = options.tag verbose = options.verbose if not tag : parser.error('tag is a required option') if not region : parser.error('region is a required option') if lepton not in ['el', 'mu'] : parser.error("invalid lepton '%s'"%lepton) outputDir = outputDir+'/'+lepton # split the output in subdirectories, so we don't overwrite things templateInputFilename = "*_%(region)s_tuple_%(tag)s.root" % {'tag':tag, 'region':region} templateOutputFilename = "%(l)s_composition_histos.root" % {'l':lepton} treeName = dict(fakeu.tupleStemsAndNames)[region] outputFileName = os.path.join(outputDir, templateOutputFilename) cacheFileName = outputFileName.replace('.root', '_cache.root') doFillHistograms = options.fill_histos or not os.path.exists(cacheFileName) optionsToPrint = ['inputDir', 'outputDir', 'tag', 'doFillHistograms', 'systfudge'] if verbose : print "working from %s"%os.getcwd() print "being called as : %s"%' '.join(os.sys.argv) print "options parsed:\n"+'\n'.join(["%s : %s"%(o, eval(o)) for o in optionsToPrint]) # collect inputs print '----> input files ',os.path.join(inputDir, templateInputFilename) tupleFilenames = glob.glob(os.path.join(inputDir, templateInputFilename)) samples = setSameGroupForAllData(fastSamplesFromFilenames(tupleFilenames, verbose)) samplesPerGroup = collections.defaultdict(list) filenamesPerGroup = collections.defaultdict(list) mkdirIfNeeded(outputDir) for s, f in zip(samples, tupleFilenames) : samplesPerGroup[s.group].append(s) filenamesPerGroup[s.group].append(f) vars = ['pt', 'eta', 'pt_eta', 'mt', 'mdeltar'] groups = samplesPerGroup.keys() if lepton=='el' : groups = [g for g in groups if g!='heavyflavor'] selections = [region] #fill histos if doFillHistograms : start_time = time.clock() num_processed_entries = 0 histosPerGroupPerSource = bookHistosPerSamplePerSource(vars, groups, leptonSources, selections) for group in groups: isData = isDataSample(group) filenames = filenamesPerGroup[group] histosThisGroupPerSource = histosPerGroupPerSource[group] chain = r.TChain(treeName) [chain.Add(fn) for fn in filenames] print "%s : %d entries (%d files)"%(group, chain.GetEntries(), chain.GetListOfFiles().GetEntries()) num_processed_entries += fillHistos(chain, histosThisGroupPerSource, isData, lepton, group, region, verbose) writeHistos(cacheFileName, histosPerGroupPerSource, verbose) end_time = time.clock() delta_time = end_time - start_time if verbose: print ("processed {0:d} entries ".format(num_processed_entries) +"in "+("{0:d} min ".format(int(delta_time/60)) if delta_time>60 else "{0:.1f} s ".format(delta_time)) +"({0:.1f} kHz)".format(num_processed_entries/delta_time)) # compute and plot fractions histosPerGroupPerSource = fetchHistos(cacheFileName, histoNamesPerSamplePerSource(vars, groups, leptonSources, selections)) histosCompositions = dict() for sel in selections: histosCompositions[sel] = dict() for var in vars: hs, groups = histosPerGroupPerSource, histosPerGroupPerSource.keys() groups = [g for g in groups if g!='data' and g!='higgs'] histosHeavy = dict((g, hs[g][sel]['heavy'][var]['loose']) for g in groups) histosLight = dict((g, hs[g][sel]['light'][var]['loose']) for g in groups) histosConv = dict((g, hs[g][sel]['conv' ][var]['loose']) for g in groups) normalizeHistos = plotParametrizedFractions.normalizeHistos plotStackedHistos = plotParametrizedFractions.plotStackedHistos frameTitle = 'hf '+lepton+': '+sel+' loose;'+var canvasName = lepton+'_hf'+sel+'_'+var+'_den' plotStackedHistos(histosHeavy, canvasName, outputDir, frameTitle) frameTitle = 'lf '+lepton+': '+sel+' loose;'+var canvasName = lepton+'_lf'+sel+'_'+var+'_den' plotStackedHistos(histosHeavy, canvasName, outputDir, frameTitle) frameTitle = 'conv '+lepton+': '+sel+' loose;'+var canvasName = lepton+'_conv'+sel+'_'+var+'_den' plotStackedHistos(histosConv, canvasName, outputDir, frameTitle) # normalize and draw fractions (den only) histos = dict([(k+'_heavy', h) for k,h in histosHeavy.iteritems()] + [(k+'_light', h) for k,h in histosLight.iteritems()] + [(k+'_conv', h) for k,h in histosConv.iteritems()]) if systfudge: fudgeCompositions(histosHeavy, histosLight, histosConv if lepton=='el' else None) normalizeHistos(histos) anygroupCompositions = buildCompositionsAddingGroups({'heavy':histosHeavy, 'light':histosLight, 'conv':histosConv}) histosCompositions[sel][var] = {'bygroup':histos, 'anygroup': anygroupCompositions} is1Dhisto = var!='pt_eta' # can only stack 1D plots if is1Dhisto: histosBySource = {'heavy':histosHeavy, 'light':histosLight, 'conv':histosConv} frameTitle = lepton+': '+sel+';'+var canvasBaseName = lepton+'_fake'+sel+'_'+var+'_frac' plotFractionsStacked(histosBySource, canvasBaseName+'_stack', outputDir, frameTitle) writeHistos(outputFileName, histosCompositions, verbose)
def main(): parser = optparse.OptionParser(usage=usage) parser.add_option('-i', '--input-dir', default='./out/fakerate') parser.add_option('-o', '--output-dir', default='./out/fake_scale_factor', help='dir for plots') parser.add_option('-l', '--lepton', default='el', help='either el or mu') parser.add_option('-r', '--region', help='one of the regions for which we saved the fake ntuples') parser.add_option('-t', '--tag', help='tag used to select the input files (e.g. Apr_04)') parser.add_option('-T', '--tight-def', help='on-the-fly tight def, one of defs in fakeUtils.py: fakeu.lepIsTight_std, etc.') parser.add_option('-f', '--fill-histos', action='store_true', default=False, help='force fill (default only if needed)') parser.add_option('-v', '--verbose', action='store_true', default=False) (options, args) = parser.parse_args() inputDir = options.input_dir outputDir = options.output_dir lepton = options.lepton region = options.region tag = options.tag verbose = options.verbose if not tag : parser.error('tag is a required option') if lepton not in ['el', 'mu'] : parser.error("invalid lepton '%s'"%lepton) filestems, treenames = utils.verticalSlice(fakeu.tupleStemsAndNames) regions = filestems assert region in regions,"invalid region '%s', must be one of %s"%(region, str(regions)) templateInputFilename = "*_%(region)s_tuple_%(tag)s.root" % {'tag':tag, 'region':region} templateOutputFilename = "%(region)s_%(l)s_scale_histos.root" % {'region':region, 'l':lepton} treeName = treenames[regions.index(region)] outputDir = outputDir+'/'+region+'/'+lepton # split the output in subdirectories, so we don't overwrite things mkdirIfNeeded(outputDir) outputFileName = os.path.join(outputDir, templateOutputFilename) cacheFileName = outputFileName.replace('.root', '_'+region+'_cache.root') doFillHistograms = options.fill_histos or not os.path.exists(cacheFileName) onthefly_tight_def = eval(options.tight_def) if options.tight_def else None # eval will take care of aborting on typos optionsToPrint = ['inputDir', 'outputDir', 'region', 'tag', 'doFillHistograms', 'onthefly_tight_def'] if verbose : print "working from %s"%os.getcwd() print "being called as : %s"%' '.join(os.sys.argv) print "options:\n"+'\n'.join(["%s : %s"%(o, eval(o)) for o in optionsToPrint]) # collect inputs if verbose : print 'input files ',os.path.join(inputDir, templateInputFilename) tupleFilenames = glob.glob(os.path.join(inputDir, templateInputFilename)) samples = setSameGroupForAllData(fastSamplesFromFilenames(tupleFilenames, verbose)) samplesPerGroup = collections.defaultdict(list) filenamesPerGroup = collections.defaultdict(list) mkdirIfNeeded(outputDir) for s, f in zip(samples, tupleFilenames) : samplesPerGroup[s.group].append(s) filenamesPerGroup[s.group].append(f) vars = ['mt0', 'mt1', 'pt0', 'pt1', 'eta1'] groups = samplesPerGroup.keys() #fill histos if doFillHistograms : start_time = time.clock() num_processed_entries = 0 histosPerGroup = bookHistos(vars, groups, region=region) histosPerSource = bookHistosPerSource(vars, leptonSources, region=region) histosPerGroupPerSource = bookHistosPerSamplePerSource(vars, groups, leptonSources, region=region) for group in groups: isData = isDataSample(group) filenames = filenamesPerGroup[group] if verbose: print " --- group : %s ---".format(group) print '\n\t'.join(filenames) histosThisGroup = histosPerGroup[group] histosThisGroupPerSource = dict((v, histosPerGroupPerSource[v][group]) for v in histosPerGroupPerSource.keys()) chain = r.TChain(treeName) [chain.Add(fn) for fn in filenames] if verbose: print "%s : %d entries"%(group, chain.GetEntries()) num_processed_entries += fillHistos(chain, histosThisGroup, histosPerSource, histosThisGroupPerSource, lepton, group, region, onthefly_tight_def=onthefly_tight_def, verbose=verbose) writeHistos(cacheFileName, histosPerGroup, histosPerSource, histosPerGroupPerSource, verbose) end_time = time.clock() delta_time = end_time - start_time if verbose: print ("processed {0:d} entries ".format(num_processed_entries) +"in "+("{0:d} min ".format(int(delta_time/60)) if delta_time>60 else "{0:.1f} s ".format(delta_time)) +"({0:.1f} kHz)".format(num_processed_entries/delta_time)) # compute scale factors histosPerGroup = fetchHistos(cacheFileName, histoNames(vars, groups, region), verbose) histosPerSource = fetchHistos(cacheFileName, histoNamesPerSource(vars, leptonSources, region), verbose) histosPerSamplePerSource = fetchHistos(cacheFileName, histoNamesPerSamplePerSource(vars, groups, leptonSources, region), verbose) plotStackedHistos(histosPerGroup, outputDir+'/by_group', region, verbose) plotStackedHistosSources(histosPerSource, outputDir+'/by_source', region, verbose) plotPerSourceEff(histosPerVar=histosPerSource, outputDir=outputDir+'/by_source', lepton=lepton, region=region, verbose=verbose) for g in groups: hps = dict((v, histosPerSamplePerSource[v][g])for v in vars) plotPerSourceEff(histosPerVar=hps, outputDir=outputDir, lepton=lepton, region=region, sample=g, verbose=verbose) hn_sf_eta = histoname_sf_vs_eta (lepton) hn_sf_pt = histoname_sf_vs_pt (lepton) hn_da_eta = histoname_data_fake_eff_vs_eta(lepton) hn_da_pt = histoname_data_fake_eff_vs_pt (lepton) objs_eta = subtractRealAndComputeScaleFactor(histosPerGroup, 'eta1', hn_sf_eta, hn_da_eta, outputDir, region, verbose) objs_pt = subtractRealAndComputeScaleFactor(histosPerGroup, 'pt1', hn_sf_pt, hn_da_pt, outputDir, region, verbose) rootUtils.writeObjectsToFile(outputFileName, dictSum(objs_eta, objs_pt), verbose) if verbose : print "saved scale factors to %s" % outputFileName