def plot(file, var, region): signalRegion = False stack = StackMaker(config, var, region, signalRegion) histosL = [] overlayL = [] print file datas = [] datasL = [] for th1 in get_th1(file): print th1.GetBinLowEdge(0) if 'VH' in th1.GetName(): overlayL.append(th1) th1.SetLineWidth(1) if 'data_obs' in th1.GetName(): datasL.append(th1) else: histosL.append(th1) print 'histoL' print histosL typs = [] typsL = [] overlay_typs = [] #append the name just once for histo in histosL: typsL.append(histo.GetName()) print histo.GetName() if 'VH' in histo.GetName(): overlay_typs.append(histo.GetName()) print typsL print 'Overlay list' print overlayL overlay_histo_dict = HistoMaker.orderandadd([{ overlay_typs[i]: overlayL[i] } for i in range(len(overlayL))], ['VH', 'VV']) overlayL2 = [] stack.histos = histosL stack.typs = typsL stack.datas = datasL stack.datanames = region for key in overlay_histo_dict: overlayL2.append(overlay_histo_dict[key]) appendix = '' stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_unweighted.' + opts.format) stack.lumi = 18940 stack.doPlot() print 'i am done!\n'
def plot(file,var,region): signalRegion = False stack = StackMaker(config,var,region,signalRegion) histosL = [] overlayL = [] print file datas = [] datasL = [] for th1 in get_th1(file): print th1.GetBinLowEdge(0) if 'VH' in th1.GetName(): overlayL.append(th1) th1.SetLineWidth(1) if 'data_obs' in th1.GetName(): datasL.append(th1) else: histosL.append(th1) print 'histoL' print histosL typs = [] typsL = [] overlay_typs=[] #append the name just once for histo in histosL: typsL.append(histo.GetName()) print histo.GetName() if 'VH' in histo.GetName(): overlay_typs.append(histo.GetName()) print typsL print 'Overlay list' print overlayL overlay_histo_dict = HistoMaker.orderandadd([{overlay_typs[i]:overlayL[i]} for i in range(len(overlayL))],['VH','VV']) overlayL2=[] stack.histos = histosL stack.typs = typsL stack.datas = datasL stack.datanames=region for key in overlay_histo_dict: overlayL2.append(overlay_histo_dict[key]) appendix = '' stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_unweighted.'+opts.format) stack.lumi = 18940 stack.doPlot() print 'i am done!\n'
def doPlot(): #print "Read Ploting Region information" #print "===============================\n" if var_to_plot_: vars = [var_to_plot_] else: vars = (config.get(section, 'vars')).split(',')#get the variables to be ploted in each region print "The variables are", vars, "\n" data = eval(config.get(section,'Datas'))# read the data corresponding to each CR (section) mc = eval(config.get('Plot_general','samples'))# read the list of mc samples total_lumi = eval(config.get('Plot_general','lumi')) #print 'total lumi is', total_lumi print "The list of mc samples is", mc remove_sys_ = eval(config.get('Plot_general','remove_sys')) #print "Check if is Signal Region" #print "=========================\n" SignalRegion = False if config.has_option(section,'Signal'): mc.append(config.get(section,'Signal')) SignalRegion = True #print "After addind the signal, the mc is", mc #print "Getting information on data and mc samples" #print "==========================================\n" #print "Getting data sample" datasamples = info.get_samples(data) #print "datasamples is\n", datasamples #print "Getting mc sample" mcsamples = info.get_samples(mc) #print "mc sample is\n" #for sample in mcsamples: #print "sample name", sample.name GroupDict = eval(config.get('Plot_general','Group'))#Contained in plots. Listed in general, under Group [Samples] group. This is a dictionnary that descriebes what samples should share the same color under the stack plot. #GETALL AT ONCE options = [] Stacks = [] #print "Start Loop over the list of variables(to fill the StackMaker )" print "==============================================================\n" for i in range(len(vars)):# loop over the list of variables to be ploted in each reagion #print "The variable is ", vars[i], "\n" Stacks.append(StackMaker(config,vars[i],region,SignalRegion, None, '_'+subcut_plotname))# defined in myutils DoubleStackMaker. The StackMaker merge together all the informations necessary to perform the plot (plot region, variables, samples and signal region ). "options" contains the variables information, including the cuts. options.append(Stacks[i].options) #print "Finished Loop over the list of variables(to fill the StackMaker )" #print "================================================================\n" #Prepare cached files in the temporary (tmpSamples) folder. #def __init__(self, samples, path, config, optionsList, GroupDict=None, filelist=None, mergeplot=False, sample_to_merge=None, mergecaching=False): Plotter=HistoMaker(samples=mcsamples+datasamples, path=path, config=config, optionsList=options, GroupDict=GroupDict, filelist=filelist, mergeplot=opts.mergeplot, sample_to_merge=sample_to_merge_, mergeCachingPart=mergeCachingPart, plotMergeCached = opts.mergecachingplot, branch_to_keep=None, dccut=None,remove_sys=remove_sys_) if len(filelist)>0 or opts.mergeplot: print('ONLY CACHING PERFORMED, EXITING'); sys.exit(1) #print '\nProducing Plot of %s\n'%vars[v] Lhistos = [[] for _ in range(0,len(vars))] Ltyps = [[] for _ in range(0,len(vars))] Ldatas = [[] for _ in range(0,len(vars))] Ldatatyps = [[] for _ in range(0,len(vars))] Ldatanames = [[] for _ in range(0,len(vars))] Ljobnames = [[] for _ in range(0,len(vars))] #print "Summing up the Luminosity" #print "=========================\n" #! Sums up the luminosity of the data: lumicounter=0. lumi=0. if datasamples == []: lumi = total_lumi else: print "Will run over datasamples to sum up the lumi" for job in datasamples: print "Datasample is", job lumi+=float(job.lumi) lumicounter+=1. if lumicounter > 0: lumi=lumi/lumicounter print "The lumi is", lumi, "\n" Plotter.lumi=lumi mass = Stacks[0].mass #print 'mcsamples',mcsamples inputs=[] for job in mcsamples: # print 'job.name' # cutOverWrite = None # if addBlindingCut: # cutOverWrite = config.get('Cuts',region)+' & ' + addBlindingCut inputs.append((Plotter,"get_histos_from_tree",(job,True, subcut_, '1'))) #print 'inputs are', inputs # if('pisa' in config.get('Configuration','whereToLaunch')): multiprocess=int(config.get('Configuration','nprocesses')) # multiprocess=0 start_time = time.time() outputs = [] if multiprocess>1: from multiprocessing import Pool from myutils import GlobalFunction p = Pool(multiprocess) #print 'launching get_histos_from_tree with ',multiprocess,' processes' outputs = p.map(GlobalFunction, inputs) else: #print 'launching get_histos_from_tree with ',multiprocess,' processes' for input_ in inputs: outputs.append(getattr(input_[0],input_[1])(*input_[2])) #ie. Plotter.get_histos_from_tree(job) #print 'get_histos_from_tree DONE' Overlaylist = [] print "All histograms retrived. DONE in ", str(time.time() - start_time)," s." start_time = time.time() for i,job in enumerate(mcsamples): hDictList = outputs[i] if job.name in mass: histoList = [] for v in range(0,len(vars)): histoCopy = deepcopy(hDictList[v].values()[0]) histoCopy.SetTitle(job.name) histoList.append(histoCopy) Overlaylist.append(histoList) for v in range(0,len(vars)): Lhistos[v].append(hDictList[v].values()[0]) Ltyps[v].append(hDictList[v].keys()[0]) Ljobnames[v].append(job.name) #print "len(vars)=",len(vars) #print "Ltyps is", Ltyps ##invert Overlaylist[variable][job] -> Overlaylist[job][variable] #print "len(Overlaylist) before: ",len(Overlaylist) #print "Overlaylist",Overlaylist # newOverlaylist = [[None]*len(Overlaylist)]*len(vars) # for i,OverlaySameSample in enumerate(Overlaylist): # for j,Overlay in enumerate(OverlaySameSample): # newOverlaylist[j][i] = Overlay # Overlaylist = newOverlaylist Overlaylist = [list(a) for a in zip(*Overlaylist)] #print "len(Overlaylist) after: ",len(Overlaylist) #print "Overlaylist",Overlaylist ##merge overlays in groups for i in range(len(Overlaylist)): newhistos = {} #print "len(Overlaylist[i]):",Overlaylist[i] for histo in Overlaylist[i]: #print "histo.GetName()",histo.GetName(), #print "histo.GetTitle()",histo.GetTitle(), group = GroupDict[histo.GetTitle()] if not group in newhistos.keys(): histo.SetTitle(group) newhistos[group]=histo else: #print "Before newhistos[group].Integral()",newhistos[group].Integral(), newhistos[group].Add(histo) #print "After newhistos[group].Integral()",newhistos[group].Integral() Overlaylist[i] = newhistos.values() # ### ORIGINAL ### # print 'mcsamples',mcsamples # for job in mcsamples: # print 'job.name',job.name # #hTempList, typList = Plotter.get_histos_from_tree(job) # if addBlindingCut: # hDictList = Plotter.get_histos_from_tree(job,config.get('Cuts',region)+' & ' + addBlindingCut) # else: # print 'going to get_histos_from_tree' # hDictList = Plotter.get_histos_from_tree(job) # if job.name == mass: # print 'job.name', job.name # Overlaylist= deepcopy([hDictList[v].values()[0] for v in range(0,len(vars))]) # for v in range(0,len(vars)): # Lhistos[v].append(hDictList[v].values()[0]) # Ltyps[v].append(hDictList[v].keys()[0]) # Ljobnames[v].append(job.name) #print "DATA samples\n" #! Get the data histograms for job in datasamples: if addBlindingCut: if subcut_: dDictList = Plotter.get_histos_from_tree(job,True, config.get('Cuts',region)+' & ' + addBlindingCut +' & ' + subcut_) else: dDictList = Plotter.get_histos_from_tree(job, True, config.get('Cuts',region)+' & ' + addBlindingCut, '1') else: if subcut_: dDictList = Plotter.get_histos_from_tree(job, True, config.get('Cuts',region)+' & ' + subcut_) else: dDictList = Plotter.get_histos_from_tree(job, True, None, '1') #! add the variables list for each job (Samples) for v in range(0,len(vars)): Ldatas[v].append(dDictList[v].values()[0]) Ldatatyps[v].append(dDictList[v].keys()[0]) Ldatanames[v].append(job.name) for v in range(0,len(vars)): #print "Ltyps[v]:",Ltyps[v] #print "Lhistos[v]:",Lhistos[v] #print "Ldatas[v]:",Ldatas[v] #print "Ldatatyps[v]:",Ldatatyps[v] #print "Ldatanames[v]:",Ldatanames[v] #print "lumi:",lumi histos = Lhistos[v] typs = Ltyps[v] Stacks[v].histos = Lhistos[v] Stacks[v].typs = Ltyps[v] Stacks[v].datas = Ldatas[v] Stacks[v].datatyps = Ldatatyps[v] Stacks[v].datanames= Ldatanames[v] try: if SignalRegion: Stacks[v].overlay = Overlaylist[v] ## from except Exception as e: print "NO OVERLAY LIST:", e Stacks[v].lumi = lumi Stacks[v].jobnames= Ljobnames[v] Stacks[v].normalize = eval(config.get(section,'Normalize')) #print 'Stack[v].subcut is', Stacks[v].subcut #if subcut_plotname: Stacks[v].subcut = '_'+subcut_plotname #print 'again, Stack[v].subcut is', Stacks[v].subcut Stacks[v].doPlot() ##FIXME## # Stacks[v].histos = Lhistos[v] # Stacks[v].typs = Ltyps[v] # Stacks[v].datas = Ldatas[v] # Stacks[v].datatyps = Ldatatyps[v] # Stacks[v].datanames= Ldatanames[v] # Stacks[v].normalize = True # Stacks[v].options['pdfName'] = Stacks[v].options['pdfName'].replace('.pdf','_norm.pdf') # Stacks[v].doPlot() print 'i am done!\n' print "Last part DONE in ", str(time.time() - start_time)," s." print "tree_stack done in ", str(time.time() - begin_time)," s." print 'DOPLOT END'
def doPlot(): #print "Read Ploting Region information" #print "===============================\n" vars = (config.get(section, 'vars')).split( ',') #get the variables to be ploted in each region #print "The variables are", vars, "\n" data = eval(config.get( section, 'Datas')) # read the data corresponding to each CR (section) mc = eval(config.get('Plot_general', 'samples')) # read the list of mc samples total_lumi = eval(config.get('Plot_general', 'lumi')) #print 'total lumi is', total_lumi print "The list of mc samples is", mc #print "Check if is Signal Region" #print "=========================\n" SignalRegion = False if config.has_option(section, 'Signal'): mc.append(config.get(section, 'Signal')) SignalRegion = True #print "After addind the signal, the mc is", mc #print "Getting information on data and mc samples" #print "==========================================\n" #print "Getting data sample" datasamples = info.get_samples(data) #print "datasamples is\n", datasamples #print "Getting mc sample" mcsamples = info.get_samples(mc) #print "mc sample is\n" #for sample in mcsamples: #print "sample name", sample.name GroupDict = eval( config.get('Plot_general', 'Group') ) #Contained in plots. Listed in general, under Group [Samples] group. This is a dictionnary that descriebes what samples should share the same color under the stack plot. #GETALL AT ONCE options = [] Stacks = [] #print "Start Loop over the list of variables(to fill the StackMaker )" print "==============================================================\n" for i in range( len(vars) ): # loop over the list of variables to be ploted in each reagion #print "The variable is ", vars[i], "\n" Stacks.append( StackMaker(config, vars[i], region, SignalRegion) ) # defined in myutils DoubleStackMaker. The StackMaker merge together all the informations necessary to perform the plot (plot region, variables, samples and signal region ). "options" contains the variables information, including the cuts. options.append(Stacks[i].options) #print "Finished Loop over the list of variables(to fill the StackMaker )" #print "================================================================\n" #print 'loop options',options # print 'options',options #Prepare cached files in the temporary (tmpSamples) folder. Plotter = HistoMaker(mcsamples + datasamples, path, config, options, GroupDict, filelist, opts.mergeplot) if len(filelist) > 0 or opts.mergeplot: print('ONLY CACHING PERFORMED, EXITING') sys.exit(1) #print '\nProducing Plot of %s\n'%vars[v] Lhistos = [[] for _ in range(0, len(vars))] Ltyps = [[] for _ in range(0, len(vars))] Ldatas = [[] for _ in range(0, len(vars))] Ldatatyps = [[] for _ in range(0, len(vars))] Ldatanames = [[] for _ in range(0, len(vars))] Ljobnames = [[] for _ in range(0, len(vars))] #print "Summing up the Luminosity" #print "=========================\n" #! Sums up the luminosity of the data: lumicounter = 0. lumi = 0. if datasamples == []: lumi = total_lumi else: print "Will run over datasamples to sum up the lumi" for job in datasamples: print "Datasample is", job lumi += float(job.lumi) lumicounter += 1. if lumicounter > 0: lumi = lumi / lumicounter print "The lumi is", lumi, "\n" Plotter.lumi = lumi mass = Stacks[0].mass #print 'mcsamples',mcsamples inputs = [] for job in mcsamples: # print 'job.name' # cutOverWrite = None # if addBlindingCut: # cutOverWrite = config.get('Cuts',region)+' & ' + addBlindingCut inputs.append((Plotter, "get_histos_from_tree", (job, True))) #print 'inputs are', inputs # if('pisa' in config.get('Configuration','whereToLaunch')): multiprocess = int(config.get('Configuration', 'nprocesses')) # multiprocess=0 outputs = [] if multiprocess > 1: from multiprocessing import Pool from myutils import GlobalFunction p = Pool(multiprocess) #print 'launching get_histos_from_tree with ',multiprocess,' processes' outputs = p.map(GlobalFunction, inputs) else: #print 'launching get_histos_from_tree with ',multiprocess,' processes' for input_ in inputs: outputs.append(getattr( input_[0], input_[1])(*input_[2])) #ie. Plotter.get_histos_from_tree(job) #print 'get_histos_from_tree DONE' Overlaylist = [] for i, job in enumerate(mcsamples): #print 'job.name',job.name,"mass==",mass #hTempList, typList = Plotter.get_histos_from_tree(job) hDictList = outputs[i] if job.name in mass: #print 'job.name == mass' histoList = [] for v in range(0, len(vars)): histoCopy = deepcopy(hDictList[v].values()[0]) histoCopy.SetTitle(job.name) histoList.append(histoCopy) Overlaylist.append(histoList) # Overlaylist.append(deepcopy([hDictList[v].values()[0] for v in range(0,len(vars))])) # >>>>>>> silviodonato/master for v in range(0, len(vars)): Lhistos[v].append(hDictList[v].values()[0]) Ltyps[v].append(hDictList[v].keys()[0]) Ljobnames[v].append(job.name) #print "len(vars)=",len(vars) #print "Ltyps is", Ltyps ##invert Overlaylist[variable][job] -> Overlaylist[job][variable] #print "len(Overlaylist) before: ",len(Overlaylist) #print "Overlaylist",Overlaylist # newOverlaylist = [[None]*len(Overlaylist)]*len(vars) # for i,OverlaySameSample in enumerate(Overlaylist): # for j,Overlay in enumerate(OverlaySameSample): # newOverlaylist[j][i] = Overlay # Overlaylist = newOverlaylist Overlaylist = [list(a) for a in zip(*Overlaylist)] #print "len(Overlaylist) after: ",len(Overlaylist) #print "Overlaylist",Overlaylist ##merge overlays in groups for i in range(len(Overlaylist)): newhistos = {} #print "len(Overlaylist[i]):",Overlaylist[i] for histo in Overlaylist[i]: #print "histo.GetName()",histo.GetName(), #print "histo.GetTitle()",histo.GetTitle(), group = GroupDict[histo.GetTitle()] if not group in newhistos.keys(): histo.SetTitle(group) newhistos[group] = histo else: #print "Before newhistos[group].Integral()",newhistos[group].Integral(), newhistos[group].Add(histo) #print "After newhistos[group].Integral()",newhistos[group].Integral() Overlaylist[i] = newhistos.values() # ### ORIGINAL ### # print 'mcsamples',mcsamples # for job in mcsamples: # print 'job.name',job.name # #hTempList, typList = Plotter.get_histos_from_tree(job) # if addBlindingCut: # hDictList = Plotter.get_histos_from_tree(job,config.get('Cuts',region)+' & ' + addBlindingCut) # else: # print 'going to get_histos_from_tree' # hDictList = Plotter.get_histos_from_tree(job) # if job.name == mass: # print 'job.name', job.name # Overlaylist= deepcopy([hDictList[v].values()[0] for v in range(0,len(vars))]) # for v in range(0,len(vars)): # Lhistos[v].append(hDictList[v].values()[0]) # Ltyps[v].append(hDictList[v].keys()[0]) # Ljobnames[v].append(job.name) #print "DATA samples\n" #! Get the data histograms for job in datasamples: if addBlindingCut: dDictList = Plotter.get_histos_from_tree( job, config.get('Cuts', region) + ' & ' + addBlindingCut) else: dDictList = Plotter.get_histos_from_tree(job) #! add the variables list for each job (Samples) for v in range(0, len(vars)): Ldatas[v].append(dDictList[v].values()[0]) Ldatatyps[v].append(dDictList[v].keys()[0]) Ldatanames[v].append(job.name) for v in range(0, len(vars)): #print "Ltyps[v]:",Ltyps[v] #print "Lhistos[v]:",Lhistos[v] #print "Ldatas[v]:",Ldatas[v] #print "Ldatatyps[v]:",Ldatatyps[v] #print "Ldatanames[v]:",Ldatanames[v] #print "lumi:",lumi histos = Lhistos[v] typs = Ltyps[v] Stacks[v].histos = Lhistos[v] Stacks[v].typs = Ltyps[v] Stacks[v].datas = Ldatas[v] Stacks[v].datatyps = Ldatatyps[v] Stacks[v].datanames = Ldatanames[v] if SignalRegion: Stacks[v].overlay = Overlaylist[v] ## from Stacks[v].lumi = lumi Stacks[v].jobnames = Ljobnames[v] Stacks[v].normalize = eval(config.get(section, 'Normalize')) Stacks[v].doPlot() ##FIXME## # Stacks[v].histos = Lhistos[v] # Stacks[v].typs = Ltyps[v] # Stacks[v].datas = Ldatas[v] # Stacks[v].datatyps = Ldatatyps[v] # Stacks[v].datanames= Ldatanames[v] # Stacks[v].normalize = True # Stacks[v].options['pdfName'] = Stacks[v].options['pdfName'].replace('.pdf','_norm.pdf') # Stacks[v].doPlot() print 'i am done!\n' print 'DOPLOT END'
def plot(fileList): signalRegion = True region = 'plot' var = 'Hmass' stack = StackMaker(config,var,region,signalRegion) histosL = [] overlayL = [] #7-9 for the higgs #5-6 for the VV binmin=7 binmax=9 max_sb = 0 max_ssb = 0 for file in fileList: if max_sb < get_s_over_b(file,binmin,binmax): max_sb = get_s_over_b(file,binmin,binmax) if max_ssb < get_s_over_sb(file,binmin,binmax): max_ssb = get_s_over_sb(file,binmin,binmax) print max_ssb print max_sb for file in fileList: print file print get_s_over_b(file,binmin,binmax) if eval(opts.rescale) == False: max_sb = 1. max_ssb = 1. for th1 in get_th1(file): #th1.Sumw2() if 's/b' in opts.fom: th1.Scale(get_s_over_b(file,binmin,binmax)/max_sb) if 's/s+b' in opts.fom: th1.Scale(get_s_over_sb(file,binmin,binmax)/max_ssb) if 'VV' in th1.GetName(): th1.SetName('VV') if 'Zj1b' in th1.GetName(): th1.SetName('Zj2b') if 'Wj1b' in th1.GetName(): th1.SetName('Wj2b') # new stack for the overlay plot if 'VH' in th1.GetName() or 'VV' in th1.GetName(): overlayL.append(th1) histosL.append(th1) print 'histoL' print histosL typs = [] typsL = [] datas = [] datasL = [] overlay_typs=[] #append the name just once for histo in histosL: typsL.append(histo.GetName()) if 'data' in histo.GetName(): datasL.append(histo) if 'VH' in histo.GetName() or 'VV' in histo.GetName(): overlay_typs.append(histo.GetName()) #datasL.append(datas) #typsL.append(typs) print typsL print 'Overlay list' print overlayL overlay_histo_dict = HistoMaker.orderandadd([{overlay_typs[i]:overlayL[i]} for i in range(len(overlayL))],['VH','VV']) overlayL2=[] stack.histos = histosL stack.typs = typsL stack.datas = datasL # stack.datatyps = Ldatatyps[v] stack.datanames='data_obs' for key in overlay_histo_dict: overlayL2.append(overlay_histo_dict[key]) mjj_sub = eval(opts.sub) if not mjj_sub: stack.overlay = overlayL2 appendix = '' if(eval(opts.rescale) == True): appendix = '_rescaled_' if 's/s+b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_combined78tev_postFit_s_over_sb'+appendix+'.'+opts.format) elif 's/b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_combined78tev_postFit_s_over_b'+appendix+'.'+opts.format) else: stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_unweighted.'+opts.format) # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_highPt_7tev.pdf') # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_combined_postFit_s_over_b_Hpt_weight_1.pdf' stack.lumi = 18940 if mjj_sub == False: stack.doPlot() elif mjj_sub == True: stack.options['pdfName'] = stack.options['pdfName'].replace('.'+opts.format,'_subtracted.'+opts.format) stack.doSubPlot(['VH','VV']) print 'i am done!\n'
## This is now incorporated in main SYS loop(line 243) #UEPS #for weightF_sys in weightF_systematics: #for _weight in [config.get('Weights','%s_UP' %(weightF_sys)),config.get('Weights','%s_DOWN' %(weightF_sys))]: # _cut = treecut # _treevar = treevar # _name = title # appendList() print '\n-----> Run MC HistoMaker...' mc_hMaker = HistoMaker(all_samples,path,config,optionsList,GroupDict) print '\n-----> Run Data HistoMaker...' data_hMaker = HistoMaker(data_samples,path,config,[optionsList[0]]) #Calculate lumi lumi = 0. nData = 0 for job in data_samples: nData += 1 lumi += float(job.lumi) if nData > 1: lumi = lumi/float(nData) mc_hMaker.lumi = lumi
def plot(fileList): signalRegion = True #region = 'plot' region = opts.region var = 'HCSVmass' stack = StackMaker(config, var, region, signalRegion) histosL = [] overlayL = [] #7-9 for the higgs #5-6 for the VV binmin = 7 binmax = 9 max_sb = 0 max_ssb = 0 for file in fileList: if max_sb < get_s_over_b(file, binmin, binmax): max_sb = get_s_over_b(file, binmin, binmax) if max_ssb < get_s_over_sb(file, binmin, binmax): max_ssb = get_s_over_sb(file, binmin, binmax) print max_ssb print max_sb for file in fileList: print file print get_s_over_b(file, binmin, binmax) if eval(opts.rescale) == False: max_sb = 1. max_ssb = 1. for th1 in get_th1(file): #th1.Sumw2() if 's/b' in opts.fom: th1.Scale(get_s_over_b(file, binmin, binmax) / max_sb) if 's/s+b' in opts.fom: th1.Scale(get_s_over_sb(file, binmin, binmax) / max_ssb) if 'VV' in th1.GetName(): th1.SetName('VV') if 'Zj1b' in th1.GetName(): th1.SetName('Zj2b') if 'Wj1b' in th1.GetName(): th1.SetName('Wj2b') # new stack for the overlay plot if 'VH' in th1.GetName() or 'VV' in th1.GetName(): overlayL.append(th1) histosL.append(th1) print 'histoL' print histosL typs = [] typsL = [] datas = [] datasL = [] overlay_typs = [] #append the name just once for histo in histosL: typsL.append(histo.GetName()) if 'data' in histo.GetName(): datasL.append(histo) if 'VH' in histo.GetName() or 'VV' in histo.GetName(): overlay_typs.append(histo.GetName()) #datasL.append(datas) #typsL.append(typs) print typsL print 'Overlay list' print overlayL overlay_histo_dict = HistoMaker.orderandadd([{ overlay_typs[i]: overlayL[i] } for i in range(len(overlayL))], ['VH', 'VV']) overlayL2 = [] stack.histos = histosL stack.typs = typsL stack.datas = datasL # stack.datatyps = Ldatatyps[v] stack.datanames = 'data_obs' for key in overlay_histo_dict: overlayL2.append(overlay_histo_dict[key]) mjj_sub = eval(opts.sub) if not mjj_sub: stack.overlay = overlayL2 appendix = '' if (eval(opts.rescale) == True): appendix = '_rescaled_' if 's/s+b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_combined78tev_postFit_s_over_sb' + appendix + '.' + opts.format) elif 's/b' in opts.fom: stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_combined78tev_postFit_s_over_b' + appendix + '.' + opts.format) else: stack.options['pdfName'] = stack.options['pdfName'].replace( '.pdf', '_unweighted.' + opts.format) # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_highPt_7tev.pdf') # stack.options['pdfName'] = stack.options['pdfName'].replace('.pdf','_combined_postFit_s_over_b_Hpt_weight_1.pdf' stack.lumi = 18940 if mjj_sub == False: stack.doPlot() elif mjj_sub == True: stack.options['pdfName'] = stack.options['pdfName'].replace( '.' + opts.format, '_subtracted.' + opts.format) stack.doSubPlot(['VH', 'VV']) print 'i am done!\n'
_treevar = treevar _name = title _countHisto = "CountWeightedLHEWeightScale" _countbin = lhe_num appendList() _countHisto = "CountWeighted" _countbin = 0 #print '===================\n' #print 'The option list is', optionsList print 'Preparations for Histograms (HistoMakeri)' print '=========================================\n' mc_hMaker = HistoMaker(all_samples, path, config, optionsList, GroupDict) data_hMaker = HistoMaker(data_samples, path, config, [optionsList[0]]) print 'Calculate luminosity' print '====================\n' #Calculate lumi lumi = 0. nData = 0 for job in data_samples: nData += 1 lumi += float(job.lumi) if nData > 1: lumi = lumi / float(nData) mc_hMaker.lumi = lumi
def doPlot(): vars = (config.get(section, 'vars')).split(',') data = config.get(section, 'Datas') mc = eval(config.get('Plot_general', 'samples')) print ' with Vars:', vars print ' Using MC samples: ', mc print ' and Data samples: ', data # Is set in plots. Can be the mass of ZH/ggZH. SignalRegion = False if config.has_option(section, 'Signal'): mc.append(config.get(section, 'Signal')) SignalRegion = True #print '\n\n\t\t=====SignalRegion:', SignalRegion datasamples = info.get_samples(data) mcsamples = info.get_samples(mc) GroupDict = eval(config.get('Plot_general', 'Group')) #GETALL AT ONCE options = [] Stacks = [] for i in range(len(vars)): Stacks.append(StackMaker(config, vars[i], region, SignalRegion)) options.append(Stacks[i].options) # Init the HistoMaker class Plotter = HistoMaker(mcsamples + datasamples, path, config, options, GroupDict) #print '\nProducing Plot of %s\n'%vars[v] Lhistos = [[] for _ in range(0, len(vars))] Ltyps = [[] for _ in range(0, len(vars))] Ldatas = [[] for _ in range(0, len(vars))] Ldatatyps = [[] for _ in range(0, len(vars))] Ldatanames = [[] for _ in range(0, len(vars))] #Find out Lumi: lumicounter = 0. lumi = 0. for job in datasamples: lumi += float(job.lumi) lumicounter += 1. if lumicounter > 0: lumi = lumi / lumicounter print '\n\t\t Luminosity Auto Calculation: ', lumi # temp hack #lumi = 3000. Plotter.lumi = lumi mass = Stacks[0].mass # Define the counters for statistics counting mass_count = Counter() bdt_count = Counter() for job in mcsamples: # hDictList returns list of TH1Fs of the specified vars to plot if addBlindingCut: hDictList = Plotter.get_histos_from_tree( job, config.get('Cuts', region) + ' & ' + addBlindingCut) else: hDictList = Plotter.get_histos_from_tree(job) #print '\n\n\t\tJob name: ', job.name if job.name == 'ZH125': #or job.name == 'ggZH125': Overlaylist = deepcopy( [hDictList[v].values()[0] for v in range(0, len(vars))]) for v in range(0, len(vars)): Lhistos[v].append(hDictList[v].values()[0]) Ltyps[v].append(hDictList[v].keys()[0]) hist = hDictList[v].values()[0] # end variable loop # end MC sample loop # Loop over data for job in datasamples: # temp hack to turn off data #break #hTemp, typ = Plotter.get_histos_from_tree(job) if addBlindingCut: dDictList = Plotter.get_histos_from_tree( job, config.get('Cuts', region) + ' & ' + addBlindingCut) else: dDictList = Plotter.get_histos_from_tree(job) for v in range(0, len(vars)): Ldatas[v].append(dDictList[v].values()[0]) Ldatatyps[v].append(dDictList[v].keys()[0]) Ldatanames[v].append(job.name) # produce the final plots and save for v in range(0, len(vars)): histos = Lhistos[v] typs = Ltyps[v] Stacks[v].histos = Lhistos[v] Stacks[v].typs = Ltyps[v] Stacks[v].datas = Ldatas[v] Stacks[v].datatyps = Ldatatyps[v] Stacks[v].datanames = Ldatanames[v] if SignalRegion: Stacks[v].overlay = Overlaylist[v] Stacks[v].lumi = lumi Stacks[v].doPlot() Stacks[v].overlay = [] Stacks[v].histos = Lhistos[v] Stacks[v].typs = Ltyps[v] Stacks[v].datas = Ldatas[v] Stacks[v].datatyps = Ldatatyps[v] Stacks[v].datanames = Ldatanames[v] Stacks[v].normalize = True Stacks[v].options['pdfName'] = Stacks[v].options['pdfName'].replace( '.pdf', '_norm.pdf') #Stacks[v].doPlot() print 'i am done!\n'
def doPlot(): vars = (config.get(section, 'vars')).split(',') data = config.get(section,'Datas') mc = eval(config.get('Plot_general','samples')) SignalRegion = False if config.has_option(section,'Signal'): mc.append(config.get(section,'Signal')) SignalRegion = True datasamples = info.get_samples(data) mcsamples = info.get_samples(mc) GroupDict = eval(config.get('Plot_general','Group')) #GETALL AT ONCE options = [] Stacks = [] for i in range(len(vars)): Stacks.append(StackMaker(config,vars[i],region,SignalRegion)) options.append(Stacks[i].options) print options Plotter=HistoMaker(mcsamples+datasamples,path,config,options,GroupDict) #print '\nProducing Plot of %s\n'%vars[v] Lhistos = [[] for _ in range(0,len(vars))] Ltyps = [[] for _ in range(0,len(vars))] Ldatas = [[] for _ in range(0,len(vars))] Ldatatyps = [[] for _ in range(0,len(vars))] Ldatanames = [[] for _ in range(0,len(vars))] #Find out Lumi: lumicounter=0. lumi=0. for job in datasamples: lumi+=float(job.lumi) lumicounter+=1. if lumicounter > 0: lumi=lumi/lumicounter Plotter.lumi=lumi mass = Stacks[0].mass for job in mcsamples: #hTempList, typList = Plotter.get_histos_from_tree(job) if addBlindingCut: hDictList = Plotter.get_histos_from_tree(job,config.get('Cuts',region)+' & ' + addBlindingCut) else: hDictList = Plotter.get_histos_from_tree(job) if job.name == mass: print job.name Overlaylist= deepcopy([hDictList[v].values()[0] for v in range(0,len(vars))]) for v in range(0,len(vars)): Lhistos[v].append(hDictList[v].values()[0]) Ltyps[v].append(hDictList[v].keys()[0]) for job in datasamples: #hTemp, typ = Plotter.get_histos_from_tree(job) if addBlindingCut: dDictList = Plotter.get_histos_from_tree(job,config.get('Cuts',region)+' & ' + addBlindingCut) else: dDictList = Plotter.get_histos_from_tree(job) for v in range(0,len(vars)): Ldatas[v].append(dDictList[v].values()[0]) Ldatatyps[v].append(dDictList[v].keys()[0]) Ldatanames[v].append(job.name) for v in range(0,len(vars)): histos = Lhistos[v] typs = Ltyps[v] Stacks[v].histos = Lhistos[v] Stacks[v].typs = Ltyps[v] Stacks[v].datas = Ldatas[v] Stacks[v].datatyps = Ldatatyps[v] Stacks[v].datanames= Ldatanames[v] #if SignalRegion: # Stacks[v].overlay = Overlaylist[v] Stacks[v].lumi = lumi Stacks[v].doPlot() Stacks[v].histos = Lhistos[v] Stacks[v].typs = Ltyps[v] Stacks[v].datas = Ldatas[v] Stacks[v].datatyps = Ldatatyps[v] Stacks[v].datanames= Ldatanames[v] Stacks[v].normalize = True Stacks[v].options['pdfName'] = Stacks[v].options['pdfName'].replace('.pdf','_norm.pdf') Stacks[v].doPlot() print 'i am done!\n'
def doPlot(): vars = (config.get(section, "vars")).split(",") data = config.get(section, "Datas") mc = eval(config.get("Plot_general", "samples")) print " with Vars:", vars print " Using MC samples: ", mc print " and Data samples: ", data # Is set in plots. Can be the mass of ZH/ggZH. SignalRegion = False if config.has_option(section, "Signal"): mc.append(config.get(section, "Signal")) SignalRegion = True # print '\n\n\t\t=====SignalRegion:', SignalRegion datasamples = info.get_samples(data) mcsamples = info.get_samples(mc) GroupDict = eval(config.get("Plot_general", "Group")) # GETALL AT ONCE options = [] Stacks = [] for i in range(len(vars)): Stacks.append(StackMaker(config, vars[i], region, SignalRegion)) options.append(Stacks[i].options) # Init the HistoMaker class Plotter = HistoMaker(mcsamples + datasamples, path, config, options, GroupDict) # print '\nProducing Plot of %s\n'%vars[v] Lhistos = [[] for _ in range(0, len(vars))] Ltyps = [[] for _ in range(0, len(vars))] Ldatas = [[] for _ in range(0, len(vars))] Ldatatyps = [[] for _ in range(0, len(vars))] Ldatanames = [[] for _ in range(0, len(vars))] # Find out Lumi: lumicounter = 0.0 lumi = 0.0 for job in datasamples: lumi += float(job.lumi) lumicounter += 1.0 if lumicounter > 0: lumi = lumi / lumicounter print "\n\t\t Luminosity Auto Calculation: ", lumi # temp hack # lumi = 3000. Plotter.lumi = lumi mass = Stacks[0].mass # Define the counters for statistics counting mass_count = Counter() bdt_count = Counter() for job in mcsamples: # hDictList returns list of TH1Fs of the specified vars to plot if addBlindingCut: hDictList = Plotter.get_histos_from_tree(job, config.get("Cuts", region) + " & " + addBlindingCut) else: hDictList = Plotter.get_histos_from_tree(job) # print '\n\n\t\tJob name: ', job.name if job.name == "ZH125": # or job.name == 'ggZH125': Overlaylist = deepcopy([hDictList[v].values()[0] for v in range(0, len(vars))]) for v in range(0, len(vars)): Lhistos[v].append(hDictList[v].values()[0]) Ltyps[v].append(hDictList[v].keys()[0]) hist = hDictList[v].values()[0] # end variable loop # end MC sample loop # Loop over data for job in datasamples: # temp hack to turn off data # break # hTemp, typ = Plotter.get_histos_from_tree(job) if addBlindingCut: dDictList = Plotter.get_histos_from_tree(job, config.get("Cuts", region) + " & " + addBlindingCut) else: dDictList = Plotter.get_histos_from_tree(job) for v in range(0, len(vars)): Ldatas[v].append(dDictList[v].values()[0]) Ldatatyps[v].append(dDictList[v].keys()[0]) Ldatanames[v].append(job.name) # produce the final plots and save for v in range(0, len(vars)): histos = Lhistos[v] typs = Ltyps[v] Stacks[v].histos = Lhistos[v] Stacks[v].typs = Ltyps[v] Stacks[v].datas = Ldatas[v] Stacks[v].datatyps = Ldatatyps[v] Stacks[v].datanames = Ldatanames[v] if SignalRegion: Stacks[v].overlay = Overlaylist[v] Stacks[v].lumi = lumi Stacks[v].doPlot() Stacks[v].overlay = [] Stacks[v].histos = Lhistos[v] Stacks[v].typs = Ltyps[v] Stacks[v].datas = Ldatas[v] Stacks[v].datatyps = Ldatatyps[v] Stacks[v].datanames = Ldatanames[v] Stacks[v].normalize = True Stacks[v].options["pdfName"] = Stacks[v].options["pdfName"].replace(".pdf", "_norm.pdf") Stacks[v].doPlot() print "i am done!\n"
def doPlot(): vars = (config.get(section, 'vars')).split(',') # print 'vars',vars data = config.get(section,'Datas') mc = eval(config.get('Plot_general','samples')) SignalRegion = False if config.has_option(section,'Signal'): mc.append(config.get(section,'Signal')) SignalRegion = True datasamples = info.get_samples(data) mcsamples = info.get_samples(mc) GroupDict = eval(config.get('Plot_general','Group')) #GETALL AT ONCE options = [] Stacks = [] for i in range(len(vars)): Stacks.append(StackMaker(config,vars[i],region,SignalRegion)) options.append(Stacks[i].options) print 'loop options',options # print 'options',options Plotter=HistoMaker(mcsamples+datasamples,path,config,options,GroupDict) #print '\nProducing Plot of %s\n'%vars[v] Lhistos = [[] for _ in range(0,len(vars))] Ltyps = [[] for _ in range(0,len(vars))] Ldatas = [[] for _ in range(0,len(vars))] Ldatatyps = [[] for _ in range(0,len(vars))] Ldatanames = [[] for _ in range(0,len(vars))] Ljobnames = [[] for _ in range(0,len(vars))] #Find out Lumi: lumicounter=0. lumi=0. for job in datasamples: lumi+=float(job.lumi) lumicounter+=1. if lumicounter > 0: lumi=lumi/lumicounter Plotter.lumi=lumi mass = Stacks[0].mass # multiprocess=16 # if multiprocess>0: # from multiprocessing import Pool # p = Pool(multiprocess) ## import pathos.multiprocessing as mp ## p = mp.ProcessingPool(multiprocess) # myinputs = [] # for job in self.__sampleList: # myoptions = self.putOptions() # myinputs.append((myoptions,job)) # # outputs = p.map(trim_treeMT, myinputs) print 'mcsamples',mcsamples inputs=[] for job in mcsamples: # print 'job.name' cutOverWrite = None if addBlindingCut: cutOverWrite = config.get('Cuts',region)+' & ' + addBlindingCut inputs.append((Plotter,"get_histos_from_tree",(job,cutOverWrite))) multiprocess=64 outputs = [] if multiprocess>0: from multiprocessing import Pool from myutils import GlobalFunction p = Pool(multiprocess) print 'launching get_histos_from_tree with ',multiprocess,' processes' outputs = p.map(GlobalFunction, inputs) else: print 'launching get_histos_from_tree with ',multiprocess,' processes' for input_ in inputs: outputs.append(getattr(input_[0],input_[1])(*input_[2])) #ie. Plotter.get_histos_from_tree(job,cutOverWrite) print 'get_histos_from_tree DONE' Overlaylist = [] for i,job in enumerate(mcsamples): print 'job.name',job.name,"mass==",mass #hTempList, typList = Plotter.get_histos_from_tree(job) hDictList = outputs[i] if job.name in mass: print 'job.name == mass' histoList = [] for v in range(0,len(vars)): histoCopy = deepcopy(hDictList[v].values()[0]) histoCopy.SetTitle(job.name) histoList.append(histoCopy) Overlaylist.append(histoList) # Overlaylist.append(deepcopy([hDictList[v].values()[0] for v in range(0,len(vars))])) for v in range(0,len(vars)): Lhistos[v].append(hDictList[v].values()[0]) Ltyps[v].append(hDictList[v].keys()[0]) Ljobnames[v].append(job.name) print "len(vars)=",len(vars) ##invert Overlaylist[variable][job] -> Overlaylist[job][variable] print "len(Overlaylist) before: ",len(Overlaylist) print "Overlaylist",Overlaylist # newOverlaylist = [[None]*len(Overlaylist)]*len(vars) # for i,OverlaySameSample in enumerate(Overlaylist): # for j,Overlay in enumerate(OverlaySameSample): # newOverlaylist[j][i] = Overlay # Overlaylist = newOverlaylist Overlaylist = [list(a) for a in zip(*Overlaylist)] print "len(Overlaylist) after: ",len(Overlaylist) print "Overlaylist",Overlaylist ##merge overlays in groups for i in range(len(Overlaylist)): newhistos = {} print "len(Overlaylist[i]):",Overlaylist[i] for histo in Overlaylist[i]: print "histo.GetName()",histo.GetName(), print "histo.GetTitle()",histo.GetTitle(), group = GroupDict[histo.GetTitle()] if not group in newhistos.keys(): histo.SetTitle(group) newhistos[group]=histo else: print "Before newhistos[group].Integral()",newhistos[group].Integral(), newhistos[group].Add(histo) print "After newhistos[group].Integral()",newhistos[group].Integral() Overlaylist[i] = newhistos.values() # ### ORIGINAL ### # print 'mcsamples',mcsamples # for job in mcsamples: # print 'job.name',job.name # #hTempList, typList = Plotter.get_histos_from_tree(job) # if addBlindingCut: # hDictList = Plotter.get_histos_from_tree(job,config.get('Cuts',region)+' & ' + addBlindingCut) # else: # print 'going to get_histos_from_tree' # hDictList = Plotter.get_histos_from_tree(job) # if job.name == mass: # print 'job.name', job.name # Overlaylist= deepcopy([hDictList[v].values()[0] for v in range(0,len(vars))]) # for v in range(0,len(vars)): # Lhistos[v].append(hDictList[v].values()[0]) # Ltyps[v].append(hDictList[v].keys()[0]) # Ljobnames[v].append(job.name) print 'datasamples',datasamples for job in datasamples: #hTemp, typ = Plotter.get_histos_from_tree(job) if addBlindingCut: dDictList = Plotter.get_histos_from_tree(job,config.get('Cuts',region)+' & ' + addBlindingCut) else: dDictList = Plotter.get_histos_from_tree(job) for v in range(0,len(vars)): Ldatas[v].append(dDictList[v].values()[0]) Ldatatyps[v].append(dDictList[v].keys()[0]) Ldatanames[v].append(job.name) for v in range(0,len(vars)): print "Ltyps[v]:",Ltyps[v] print "Lhistos[v]:",Lhistos[v] print "Ldatas[v]:",Ldatas[v] print "Ldatatyps[v]:",Ldatatyps[v] print "Ldatanames[v]:",Ldatanames[v] print "lumi:",lumi histos = Lhistos[v] typs = Ltyps[v] Stacks[v].histos = Lhistos[v] Stacks[v].typs = Ltyps[v] Stacks[v].datas = Ldatas[v] Stacks[v].datatyps = Ldatatyps[v] Stacks[v].datanames= Ldatanames[v] #if SignalRegion: Stacks[v].overlay = Overlaylist[v] ## from Stacks[v].lumi = lumi Stacks[v].jobnames= Ljobnames[v] Stacks[v].doPlot() ##FIXME## # Stacks[v].histos = Lhistos[v] # Stacks[v].typs = Ltyps[v] # Stacks[v].datas = Ldatas[v] # Stacks[v].datatyps = Ldatatyps[v] # Stacks[v].datanames= Ldatanames[v] # Stacks[v].normalize = True # Stacks[v].options['pdfName'] = Stacks[v].options['pdfName'].replace('.pdf','_norm.pdf') # Stacks[v].doPlot() print 'i am done!\n'
_treevar = treevar # append appendList() # UEPS for weightF_sys in weightF_systematics: for _weight in [config.get("Weights", "%s_UP" % (weightF_sys)), config.get("Weights", "%s_DOWN" % (weightF_sys))]: _cut = treecut _treevar = treevar _name = title appendList() # for option in optionsList: # print option['cut'] mc_hMaker = HistoMaker(all_samples, path, config, optionsList, GroupDict) data_hMaker = HistoMaker(data_samples, path, config, [optionsList[0]]) # Calculate lumi lumi = 0.0 nData = 0 for job in data_samples: nData += 1 lumi += float(job.lumi) if nData > 1: lumi = lumi / float(nData) mc_hMaker.lumi = lumi data_hMaker.lumi = lumi if addBlindingCut: