def makeControlPlot(systhistos, syst, tag, seltag, opt): hists = tuple([systhistos[(tag, syst, c)] for c in COMBINATIONS.keys()]) h_tot, h_cor, h_wro, h_unm = hists h_tot.Scale(1. / h_tot.Integral()) ctrlplot = RatioPlot('ctrlplot_%s' % tag) ctrlplot.add(h_cor, 'Correct') ctrlplot.add(h_wro, 'Wrong') ctrlplot.add(h_unm, 'Unmatched') ctrlplot.reference = h_tot ctrlplot.tag = '' ctrlplot.subtag = seltag ctrlplot.ratiotitle = 'Ratio wrt Total' ctrlplot.ratiorange = (0., 3.0) ctrlplot.colors = [ROOT.kBlue - 3, ROOT.kRed - 4, ROOT.kOrange - 3] ctrlplot.show("control_%s_%s" % (syst, tag), opt.outDir) ctrlplot.reset()
def makeControlPlot(systhistos, syst, tag, seltag, opt): hists = tuple([systhistos[(tag, syst, c)] for c in COMBINATIONS.keys()]) h_tot, h_cor, h_wro, h_unm = hists h_tot.Scale(1./h_tot.Integral()) ctrlplot = RatioPlot('ctrlplot_%s'%tag) ctrlplot.add(h_cor, 'Correct') ctrlplot.add(h_wro, 'Wrong') ctrlplot.add(h_unm, 'Unmatched') ctrlplot.reference = h_tot ctrlplot.tag = '' ctrlplot.subtag = seltag ctrlplot.ratiotitle = 'Ratio wrt Total' ctrlplot.ratiorange = (0., 3.0) ctrlplot.colors = [ROOT.kBlue-3, ROOT.kRed-4, ROOT.kOrange-3] ctrlplot.show("control_%s_%s"%(syst,tag), os.path.join(opt.outDir, 'syst_plots')) ctrlplot.reset()
def main(args, opt): os.system('mkdir -p %s'%opt.outDir) systfiles = {} # procname -> filename try: for fname in os.listdir(os.path.join(args[0],'syst')): if not os.path.splitext(fname)[1] == '.root': continue for syst,_,systfile,_ in SYSTSFROMFILES: if fname in systfile: systfiles[syst] = [os.path.join(args[0], 'syst', fname)] # Get the split nominal files systfiles['nominal'] = [] for fname in os.listdir(os.path.join(args[0],'Chunks')): if not os.path.splitext(fname)[1] == '.root': continue isdata,procname,splitno = resolveFilename(fname) if not procname == 'TTJets_MSDecays_172v5': continue if not splitno: continue # file is split systfiles['nominal'].append(os.path.join(args[0],'Chunks',fname)) if len(systfiles['nominal']) < 20: print "ERROR >>> Missing files for split nominal sample?" return -1 except IndexError: print "Please provide a valid input directory" exit(-1) hname_to_keys = {} # hname -> (tag, syst, comb) tasklist = {} # treefile -> tasklist for fsyst in systfiles.keys(): if not fsyst in tasklist: tasklist[fsyst] = [] for tag,sel,_ in SELECTIONS: if fsyst == 'nominal': for syst,_,weight,combs in SYSTSFROMWEIGHTS: tasks = makeSystTask(tag, sel, syst, hname_to_keys, weight=weight, combs=combs) tasklist[fsyst] += tasks tasks = [] for comb,combsel in COMBINATIONS.iteritems(): for var,nbins,xmin,xmax,titlex in CONTROLVARS: hname = "%s_%s_%s" % (var, comb, tag) finalsel = "%s*(%s&&%s)"%(COMMONWEIGHT, sel, combsel) tasks.append((hname, var, finalsel, nbins, xmin, xmax, titlex)) hname_to_keys[hname] = (tag, var, comb) tasklist[fsyst] += tasks tasks = [] for name, nus in [('nu', 1), ('nonu', 0), ('nuunm', -1)]: hname = "SVLMass_%s_%s_%s" % ('tot', tag, name) finalsel = "%s*(%s&&BHadNeutrino==%d)"%( COMMONWEIGHT, sel, nus) tasks.append((hname, 'SVLMass', finalsel, NBINS, XMIN, XMAX, MASSXAXISTITLE)) hname_to_keys[hname] = (tag, name, 'tot') tasklist[fsyst] += tasks else: tasks = makeSystTask(tag, sel, fsyst, hname_to_keys) tasklist[fsyst] += tasks if not opt.cache: # print ' Will process the following tasks:' # for filename,tasks in sorted(tasklist.iteritems()): # print filename # for task in tasks: # print task # raw_input("Press any key to continue...") runTasks(systfiles, tasklist, opt, 'syst_histos') systhistos = {} # (tag, syst, comb) -> histo systhistos = gatherHistosFromFiles(tasklist, systfiles, os.path.join(opt.outDir, 'syst_histos'), hname_to_keys) cachefile = open(".svlsysthistos.pck", 'w') pickle.dump(systhistos, cachefile, pickle.HIGHEST_PROTOCOL) cachefile.close() # print "Wrote syst histos to cache file" # raw_input("press key") cachefile = open(".svlsysthistos.pck", 'r') systhistos = pickle.load(cachefile) print '>>> Read syst histos from cache (.svlsysthistos.pck)' cachefile.close() ROOT.gStyle.SetOptTitle(0) ROOT.gStyle.SetOptStat(0) ROOT.gROOT.SetBatch(1) for var,_,_,_,_ in CONTROLVARS: for sel,tag in [ #('inclusive', 'Fully Inclusive'), #('inclusive_mrank1', 'Mass ranked, leading p_{T}'), #('inclusive_mrank1dr', 'Mass ranked, #DeltaR<2, leading p_{T}'), #('inclusive_drrank1dr', '#DeltaR ranked, #DeltaR<2, leading p_{T}'), ('inclusive_optmrank', 'Optimized mass rank')]: try: makeControlPlot(systhistos, var, sel, tag, opt) except KeyError: print 'control plots for %s selection not found' % sel for tag,_,_ in SELECTIONS: if not 'inclusive' in tag : continue print "... processing %s"%tag # Make plot of mass with and without neutrino: # for comb in COMBINATIONS.keys(): # plot = RatioPlot('neutrino_%s'%tag) # plot.rebin = 2 # plot.add(systhistos[(tag,'nonu', 'tot')], 'Without neutrino') # plot.add(systhistos[(tag,'nu', 'tot')], 'With neutrino') # plot.add(systhistos[(tag,'nuunm','tot')], 'Unmatched') # plot.reference = systhistos[(tag,'nominal','tot')] # plot.tag = "Mass shape with and without neutrinos" # plot.subtag = SELNAMES[tag] + COMBNAMES['tot'] # plot.ratiotitle = 'Ratio wrt Total' # plot.ratiorange = (0.7, 1.3) # plot.colors = [ROOT.kBlue-3, ROOT.kRed-4, ROOT.kOrange-3] # plot.show("neutrino_%s_%s"%(tag,'tot'), # os.path.join(opt.outDir, 'syst_plots')) # plot.reset() for name, title, systs, colors, comb in SYSTPLOTS: print name, title, systs, colors, comb plot = RatioPlot('%s_%s'%(name,comb)) plot.rebin = 2 for syst in systs: try: plot.add(systhistos[(tag,syst,comb)], SYSTNAMES[syst]) except: print 'failed to add',(tag,syst,comb),syst plot.tag = title subtag = SELNAMES[tag] + COMBNAMES[comb] plot.subtag = subtag plot.ratiotitle = 'Ratio wrt %s' % SYSTNAMES[systs[0]] plot.ratiorange = (0.85, 1.15) plot.colors = colors filename = "%s_%s"%(name,tag) if comb != 'tot': filename += '_%s'%comb plot.show(filename, os.path.join(opt.outDir,'syst_plots')) plot.reset() # Make top pt plot with both correct and wrong plot = RatioPlot('toppt_paper_cor_wro') plot.canvassize = (600,600) plot.tag = 'Top p_{T} mis-modeling' plot.rebin = 2 plot.subtag = 'Inclusive channels' plot.ratiotitle = '1 / Nominal' plot.ratiorange = (0.85, 1.15) plot.legpos = (0.55, 0.30) plot.colors = [ROOT.kGreen+2, ROOT.kGreen-6, ROOT.kRed+2, ROOT.kRed-6] plot.add(systhistos[(tag,'nominal','cor')], 'Nominal (correct)', includeInRatio=False) plot.add(systhistos[(tag,'toppt','cor')], 'p_{T} weighted (correct)', includeInRatio=True) plot.add(systhistos[(tag,'nominal','wro')], 'Nominal (wrong)', includeInRatio=False) plot.add(systhistos[(tag,'toppt','wro')], 'p_{T} weighted (wrong)', includeInRatio=True) plot.reference = [systhistos[(tag,'nominal','cor')], systhistos[(tag,'nominal','wro')]] plot.show('toppt_cor_wro_forpaper_%s'%tag, os.path.join(opt.outDir,'syst_plots')) plot.reset() # Make b fragmentation plot for paper plot = RatioPlot('bfrag_paper') plot.canvassize = (600,600) plot.tag = 'b fragmentation' plot.rebin = 2 plot.subtag = 'Inclusive channels' plot.ratiotitle = '1 / Z2* rb LEP' plot.ratiorange = (0.85, 1.15) plot.legpos = (0.65, 0.15) plot.colors = [ROOT.kMagenta, ROOT.kMagenta+2, ROOT.kMagenta-9, ROOT.kAzure+7] plot.add(systhistos[(tag,'nominal', 'tot')], 'Z2* rb LEP', includeInRatio=False) plot.add(systhistos[(tag,'bfragdn', 'tot')], 'Z2* rb LEP soft') plot.add(systhistos[(tag,'bfragup', 'tot')], 'Z2* rb LEP hard') plot.add(systhistos[(tag,'bfragz2s','tot')], 'Z2* nominal') plot.reference = [systhistos[(tag,'nominal','tot')]] plot.show('bfrag_paper_%s'%tag, os.path.join(opt.outDir,'syst_plots')) plot.reset() # for tag,sel,seltag in SELECTIONS: # print 70*'-' # print '%-10s: %s' % (tag, sel) # fcor, fwro, funm = {}, {}, {} # for mass in sorted(massfiles.keys()): # # mass = 172.5 # hists = masshistos[(tag, mass)] # n_tot, n_cor, n_wro, n_unm = (x.GetEntries() for x in hists) # fcor[mass] = 100.*(n_cor/float(n_tot)) # fwro[mass] = 100.*(n_wro/float(n_tot)) # funm[mass] = 100.*(n_unm/float(n_tot)) # print (' %5.1f GeV: %7d entries \t' # '(%4.1f%% corr, %4.1f%% wrong, %4.1f%% unmatched)' % # (mass, n_tot, fcor[mass], fwro[mass], funm[mass])) # oname = os.path.join(opt.outDir, 'fracvsmt_%s'%tag) # plotFracVsTopMass(fcor, fwro, funm, tag, seltag, oname) # print 112*'-' # print 'Estimated systematics (from a crude chi2 fit)' # print '%20s | %-15s | %-15s | %-15s | %-15s | %-15s' % ( # 'selection', 'bfrag', 'scale', # 'toppt', 'matching', 'uecr') # for tag,_,_ in SELECTIONS: # sys.stdout.write("%20s | " % tag) # for syst in ['bfrag', 'scale', 'toppt', 'matching', 'uecr']: # err, chi2 = systematics[(tag,syst)] # sys.stdout.write('%4.1f (%4.1f GeV)' % (chi2*1e5, err)) # # sys.stdout.write('%4.1f (%4.1f GeV)' % (chi2, err)) # sys.stdout.write(' | ') # sys.stdout.write('\n') # print 112*'-' return 0
def main(args, opt): os.system('mkdir -p %s' % opt.outDir) systfiles = {} # procname -> filename try: for fname in os.listdir(os.path.join(args[0], 'syst')): if not os.path.splitext(fname)[1] == '.root': continue for syst, _, systfile, _ in SYSTSFROMFILES: if fname in systfile: systfiles[syst] = [os.path.join(args[0], 'syst', fname)] # Get the split nominal files systfiles['nominal'] = [] for fname in os.listdir(os.path.join(args[0], 'Chunks')): if not os.path.splitext(fname)[1] == '.root': continue isdata, procname, splitno = resolveFilename(fname) if not procname == 'TTJets_MSDecays_172v5': continue if not splitno: continue # file is split systfiles['nominal'].append(os.path.join(args[0], 'Chunks', fname)) if len(systfiles['nominal']) < 20: print "ERROR >>> Missing files for split nominal sample?" return -1 except IndexError: print "Please provide a valid input directory" exit(-1) hname_to_keys = {} # hname -> (tag, syst, comb) tasklist = {} # treefile -> tasklist for fsyst in systfiles.keys(): if not fsyst in tasklist: tasklist[fsyst] = [] for tag, sel, _ in SELECTIONS: if fsyst == 'nominal': for syst, _, weight, combs in SYSTSFROMWEIGHTS: tasks = makeSystTask(tag, sel, syst, hname_to_keys, weight=weight, combs=combs) tasklist[fsyst] += tasks tasks = [] for comb, combsel in COMBINATIONS.iteritems(): for var, nbins, xmin, xmax, titlex in CONTROLVARS: hname = "%s_%s_%s" % (var, comb, tag) finalsel = "%s*(%s&&%s)" % (COMMONWEIGHT, sel, combsel) tasks.append( (hname, var, finalsel, nbins, xmin, xmax, titlex)) hname_to_keys[hname] = (tag, var, comb) tasklist[fsyst] += tasks tasks = [] for name, nus in [('nu', 1), ('nonu', 0), ('nuunm', -1)]: hname = "SVLMass_%s_%s_%s" % ('tot', tag, name) finalsel = "%s*(%s&&BHadNeutrino==%d)" % (COMMONWEIGHT, sel, nus) tasks.append((hname, 'SVLMass', finalsel, NBINS, XMIN, XMAX, MASSXAXISTITLE)) hname_to_keys[hname] = (tag, name, 'tot') tasklist[fsyst] += tasks else: tasks = makeSystTask(tag, sel, fsyst, hname_to_keys) tasklist[fsyst] += tasks if not opt.cache: # print ' Will process the following tasks:' # for filename,tasks in sorted(tasklist.iteritems()): # print filename # for task in tasks: # print task # raw_input("Press any key to continue...") runTasks(systfiles, tasklist, opt, 'syst_histos') systhistos = {} # (tag, syst, comb) -> histo systhistos = gatherHistosFromFiles( tasklist, systfiles, os.path.join(opt.outDir, 'syst_histos'), hname_to_keys) cachefile = open(".svlsysthistos.pck", 'w') pickle.dump(systhistos, cachefile, pickle.HIGHEST_PROTOCOL) cachefile.close() # print "Wrote syst histos to cache file" # raw_input("press key") cachefile = open(".svlsysthistos.pck", 'r') systhistos = pickle.load(cachefile) print '>>> Read syst histos from cache (.svlsysthistos.pck)' cachefile.close() ROOT.gStyle.SetOptTitle(0) ROOT.gStyle.SetOptStat(0) ROOT.gROOT.SetBatch(1) for var, _, _, _, _ in CONTROLVARS: for sel, tag in [ #('inclusive', 'Fully Inclusive'), #('inclusive_mrank1', 'Mass ranked, leading p_{T}'), #('inclusive_mrank1dr', 'Mass ranked, #DeltaR<2, leading p_{T}'), #('inclusive_drrank1dr', '#DeltaR ranked, #DeltaR<2, leading p_{T}'), ('inclusive_optmrank', 'Optimized mass rank') ]: try: makeControlPlot(systhistos, var, sel, tag, opt) except KeyError: print 'control plots for %s selection not found' % sel for tag, _, _ in SELECTIONS: if not 'inclusive' in tag: continue print "... processing %s" % tag # Make plot of mass with and without neutrino: # for comb in COMBINATIONS.keys(): # plot = RatioPlot('neutrino_%s'%tag) # plot.rebin = 2 # plot.add(systhistos[(tag,'nonu', 'tot')], 'Without neutrino') # plot.add(systhistos[(tag,'nu', 'tot')], 'With neutrino') # plot.add(systhistos[(tag,'nuunm','tot')], 'Unmatched') # plot.reference = systhistos[(tag,'nominal','tot')] # plot.tag = "Mass shape with and without neutrinos" # plot.subtag = SELNAMES[tag] + COMBNAMES['tot'] # plot.ratiotitle = 'Ratio wrt Total' # plot.ratiorange = (0.7, 1.3) # plot.colors = [ROOT.kBlue-3, ROOT.kRed-4, ROOT.kOrange-3] # plot.show("neutrino_%s_%s"%(tag,'tot'), # os.path.join(opt.outDir, 'syst_plots')) # plot.reset() for name, title, systs, colors, comb in SYSTPLOTS: print name, title, systs, colors, comb plot = RatioPlot('%s_%s' % (name, comb)) plot.rebin = 2 for syst in systs: try: plot.add(systhistos[(tag, syst, comb)], SYSTNAMES[syst]) except: print 'failed to add', (tag, syst, comb), syst plot.tag = title subtag = SELNAMES[tag] + COMBNAMES[comb] plot.subtag = subtag plot.ratiotitle = 'Ratio wrt %s' % SYSTNAMES[systs[0]] plot.ratiorange = (0.85, 1.15) plot.colors = colors filename = "%s_%s" % (name, tag) if comb != 'tot': filename += '_%s' % comb plot.show(filename, os.path.join(opt.outDir, 'syst_plots')) plot.reset() # Make top pt plot with both correct and wrong plot = RatioPlot('toppt_paper_cor_wro') plot.canvassize = (600, 600) plot.tag = 'Top quark p_{T} mismodeling' plot.rebin = 2 plot.subtag = 'Inclusive channels' plot.tagpos = (0.92, 0.85) plot.subtagpos = (0.92, 0.78) plot.titlex = 'm_{svl} [GeV]' plot.ratiotitle = '1 / Nominal' # plot.ratiorange = (0.85, 1.15) plot.ratiorange = (0.92, 1.08) plot.legpos = (0.55, 0.38) plot.ratioydivisions = 405 plot.colors = [ ROOT.kGreen + 2, ROOT.kGreen - 6, ROOT.kRed + 2, ROOT.kRed - 6 ] plot.add(systhistos[(tag, 'nominal', 'cor')], 'Nominal (correct)', includeInRatio=False) plot.add(systhistos[(tag, 'toppt', 'cor')], 'p_{T} weighted (correct)', includeInRatio=True) plot.add(systhistos[(tag, 'nominal', 'wro')], 'Nominal (wrong)', includeInRatio=False) plot.add(systhistos[(tag, 'toppt', 'wro')], 'p_{T} weighted (wrong)', includeInRatio=True) plot.reference = [ systhistos[(tag, 'nominal', 'cor')], systhistos[(tag, 'nominal', 'wro')] ] plot.show('toppt_cor_wro_forpaper_%s' % tag, os.path.join(opt.outDir, 'syst_plots')) plot.reset() # Make b fragmentation plot for paper plot = RatioPlot('bfrag_paper') plot.canvassize = (600, 600) plot.tag = 'b fragmentation' plot.titlex = 'm_{svl} [GeV]' plot.rebin = 2 plot.subtag = 'Inclusive channels' plot.tagpos = (0.92, 0.85) plot.subtagpos = (0.92, 0.78) plot.ratiotitle = '1 / Z2* #it{r}_{b} LEP' # plot.ratiorange = (0.85, 1.15) plot.ratiorange = (0.92, 1.08) plot.legpos = (0.65, 0.20) plot.ratioydivisions = 405 plot.colors = [ ROOT.kMagenta, ROOT.kMagenta + 2, ROOT.kMagenta - 9, ROOT.kAzure + 7 ] plot.add(systhistos[(tag, 'nominal', 'tot')], 'Z2* #it{r}_{b} LEP', includeInRatio=False) plot.add(systhistos[(tag, 'bfragdn', 'tot')], 'Z2* #it{r}_{b} LEP soft') plot.add(systhistos[(tag, 'bfragup', 'tot')], 'Z2* #it{r}_{b} LEP hard') plot.add(systhistos[(tag, 'bfragz2s', 'tot')], 'Z2* nominal') plot.reference = [systhistos[(tag, 'nominal', 'tot')]] plot.show('bfrag_paper_%s' % tag, os.path.join(opt.outDir, 'syst_plots')) plot.reset() # for tag,sel,seltag in SELECTIONS: # print 70*'-' # print '%-10s: %s' % (tag, sel) # fcor, fwro, funm = {}, {}, {} # for mass in sorted(massfiles.keys()): # # mass = 172.5 # hists = masshistos[(tag, mass)] # n_tot, n_cor, n_wro, n_unm = (x.GetEntries() for x in hists) # fcor[mass] = 100.*(n_cor/float(n_tot)) # fwro[mass] = 100.*(n_wro/float(n_tot)) # funm[mass] = 100.*(n_unm/float(n_tot)) # print (' %5.1f GeV: %7d entries \t' # '(%4.1f%% corr, %4.1f%% wrong, %4.1f%% unmatched)' % # (mass, n_tot, fcor[mass], fwro[mass], funm[mass])) # oname = os.path.join(opt.outDir, 'fracvsmt_%s'%tag) # plotFracVsTopMass(fcor, fwro, funm, tag, seltag, oname) # print 112*'-' # print 'Estimated systematics (from a crude chi2 fit)' # print '%20s | %-15s | %-15s | %-15s | %-15s | %-15s' % ( # 'selection', 'bfrag', 'scale', # 'toppt', 'matching', 'uecr') # for tag,_,_ in SELECTIONS: # sys.stdout.write("%20s | " % tag) # for syst in ['bfrag', 'scale', 'toppt', 'matching', 'uecr']: # err, chi2 = systematics[(tag,syst)] # sys.stdout.write('%4.1f (%4.1f GeV)' % (chi2*1e5, err)) # # sys.stdout.write('%4.1f (%4.1f GeV)' % (chi2, err)) # sys.stdout.write(' | ') # sys.stdout.write('\n') # print 112*'-' return 0