def adjustShapes(analysis, shapeNames): """ Add additional shape uncertainty names as needed. (e.g. for scale factor stat uncertainties) """ auxSFs = sig.getAllAuxSFs(analysis) newShapes = {} for shape in shapeNames: sys = shape[0] if sys.startswith('sfstat'): procs = shape[1] for proc in procs: if proc not in auxSFs: continue for auxSF in auxSFs[proc]: newShapeName = sys + auxSF # ex: sfstatttjetsNJetsTTJets if newShapeName not in newShapes: newShapes[newShapeName] = [proc] else: newShapes[newShapeName].append(proc) newShapes = [(name, procs) for name, procs in newShapes.iteritems()] if 'QCD' in analysis.samples: newShapes.append(('qcdbtagsys', ['QCD'])) print "Using additional shape uncertainties:" print newShapes return shapeNames + newShapes
def uncygwin(path, verbose=False): sys = platform.system() if sys.startswith('CYGWIN'): if path.startswith('/cygdrive/'): path = path[10].upper() + ':' + path[11:] elif path.startswith('/'): path = 'C:/cygwin' + path return path else: return path
def __init__(self, *args, **kwargs): Device.__init__(self, *args, **kwargs['dconfig']) self._psychopy_monitor = None self._coord2pix = None self._pix2coord = None if sys.startswith('linux'): self._xwindow = None if Display._computer_display_runtime_info_list is None: Display._computer_display_runtime_info_list=\ Display._createAllRuntimeInfoDicts() self._addRuntimeInfoToDisplayConfig()
### analysis cuts for data if mc: sys_e = [ "pt", "es", "esup", "esdown", "jarup", "jardown", "pu_w_up", "pu_w_down" ] else: sys_e = ["pt", "eup", "edown"] hist_l = [] for sys in sys_e: if mc: e_w = "*(%s_pu_w)" % (sys) if sys.startswith("pu_w"): e_w = "*(pt_%s)" % sys else: e_w = "*(1.0)" if sys.startswith("pu_w"): s_name = sys sys = "pt" else: s_name = sys tr_beta = in_rf.Get("%s_beta" % sys) ## event selection r_mass_cut = "(%s_raw_mass > 220)*" % sys #r_mass_cut = "(%s_raw_mass > 250)*"%sys dr_cut = "(%s_del_r>0.5)*(%s_del_r<1.5)*" % (sys, sys) dr12_cut = "(%s_del_r12<1.0)*" % (sys) eta_cut = "(abs(%s_jet1_eta)<2.5)*" % sys
def auto_load(): """ Find libclang and load it """ if sys.startswith('linux'): pass
def produce_scalevars(scaletemplate=None, updown="up", tagger=tagger, run_name=args.name, debug=args.debug, keepsys=True, mcjp=False): fin = R.TFile("colfin/collated{}.root".format(run_name)) if scaletemplate != None: fout = R.TFile( "colfin/collated_norm{}.root".format(run_name + "_" + scaletemplate + "_" + updown), 'RECREATE') else: if not mcjp: fout = R.TFile("colfin/collated_norm{}.root".format(run_name), 'RECREATE') else: fout = R.TFile("colfin/collated_norm{}_MCJP.root".format(run_name), 'RECREATE') fout.cd() for LTSVvar in ["JP", "JPhasSV", "JPnoSV", "tau1VertexMassCorr"]: for ptbin in pt_bins: for WP in WPs: for passfail in ["all", "{}_pass", "{}_fail"]: if passfail == "all" and WPs.index(WP) > 0: continue passfail = passfail.format(tagger + "_" + WP) if keepsys: systlist = [""] + [ s.replace("UP", "").replace("DOWN", "") + "up" for s in systnames if len(s) > 0 ] + [ s.replace("UP", "").replace("DOWN", "") + "down" for s in systnames if len(s) > 0 ] # Must include up/down systlist = systlist + [ "JESup", "JESdown", "GBBdown", "GCCdown", "GBBup", "GCCup" ] systlist = [ sys for sys in systlist if not sys.startswith("MCJP") ] else: systlist = [""] hout_name = "{}" + LTSVvar + "_" + passfail.replace( "_", "") + "_" + ptbin + "_{}_opt{}" # Write Data if not mcjp: hout = fin.Get( hout_name.format("UNWEIGHTED__DATA__FatJet_", "data", "")) hout.Write() # Write mcjp data else: hin = fin.Get( hout_name.format("UNWEIGHTED__DATA__FatJet_", "data", "_MCJPup")) #print(hout_name.format("UNWEIGHTED__DATA__FatJet_", "data", "MCJPup")) #print(hin) hout = hin.Clone( hout_name.format("UNWEIGHTED__DATA__FatJet_", "data", "")) hout.Write() # Write MC with systematics for systname in systlist: if len(systname) > 0: systname = "_" + systname if debug: print(systname) if debug: print( hout_name.format("UNWEIGHTED__DATA__FatJet_", "data", systname)) int_data = fin.Get( hout_name.format("UNWEIGHTED__DATA__FatJet_", "data", systname)).Integral() int_flavs, int_old = [], [] for flavor in ["b", "bfromg", "c", "cfromg", "l"]: int_h = fin.Get( hout_name.format("UNWEIGHTED__QCDMu+__FatJet_", flavor, systname)).Integral() if flavor == scaletemplate and updown == "up": int_flavs.append(int_h * 1.5) elif flavor == scaletemplate and updown == "down": int_flavs.append(int_h * 0.5) else: int_flavs.append(int_h) int_old.append(int_h) # Debug int_qcd = np.sum(int_flavs) norm = 1 #int_data/(int_qcd+0.0000000000000000000000000000000001 int_flavs_corr = [] for flavor in [ "b", "bfromg", "c", "cfromg", "l", "b_cfromg", "b_bfromg", "c_l", "b_cfromg_c_l", "b_bfromg_c_l" ]: hout = fin.Get( hout_name.format("UNWEIGHTED__QCDMu+__FatJet_", flavor, systname)) if scaletemplate != None and scaletemplate in flavor.split( "_"): if updown == "up": hout.Scale(1.5) elif updown == "down": hout.Scale(0.5) else: pass hout.Scale(norm) int_flavs_corr.append(hout.Integral()) hout.Write() #for merge, temps in zip (["b_cfromg", "c_l", "b_cfromg_c_l"], [["b","cfromg"], ["c","l"], ["b", "c", "cfromg", "l"]]): fout.Close() print("Normed data/MC templates, while scaling {} 50% {}.".format( scaletemplate, updown)) print("Output written to: {}".format(fout))
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on May 22, 2015 @author: MHuang1 ''' import sys,os print 'sys.argv[0] =', sys.argv[0] pathname = os.path.dirname(sys.argv[0]) print 'path =', pathname print 'full path =', os.path.abspath(pathname) '''sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径;比如在CMD命令行输入 “python test.py -help”,那么sys.argv[0]就代表“test.py”。 sys.startswith() 是用来判断一个对象是以什么开头的,比如在python命令行输入“'abc'.startswith('ab')”就会返回True ''' def open_file(filename): f = file(filename) while True: line = f.readline() if len(line) == 0: break print line f.close() #open_file(os.path.abspath(sys.argv[0])) if len(sys.argv) < 2:
pt_bin = ["low_pt", "medium_pt", "high_pt"] pt_bin_cut = [l_pt, m_pt, h_pt] beta_l = ["beta", "del_eta", "del_phi", "del_r", "raw_mass", "del_r12"] beta_bin = [[18, 0, pi], [30, -3, 3], [30, -3, 3], [30, 0, 3], [50, 0, 5000], [30, 0, 3]] jet_l = ["pt", "eta", "phi"] jet_bin = [[[30, 0, 2500], [30, -3, 3], [30, -pi, pi]], [[30, 0, 2500], [30, -3, 3], [30, -pi, pi]], [[30, 0, 2500], [30, -3, 3], [30, -pi, pi]]] ev_l = ["njet", "metSig", "nvtx"] ev_bin = [[30, 0, 30], [100, 0, 1], [50, 0, 50]] for eta_i, eta_loop in enumerate(eta_bin): for pt_i, pt_loop in enumerate(pt_bin): cut = d_cut + eta_bin_cut[eta_i] + pt_bin_cut[pt_i] if sys.startswith("pu"): tr = in_rf.Get("cc/nom").Clone(eta_loop + pt_loop) if sys == "pu_u": e_w = "(1.0*pileupWeight_up)" else: e_w = "(1.0*pileupWeight_dn)" else: tr = in_rf.Get("cc/%s" % sys).Clone(eta_loop + pt_loop) tr.Draw(">>eventList", cut) el = ROOT.gDirectory.Get("eventList") tr.SetEventList(el) for i, beta_loop in enumerate(beta_l): name = eta_loop + "_" + pt_loop + "_" + sys + "_" + beta_loop title = name bin_set = beta_bin[i]
hist2.GetXaxis().SetTitle("reco_beta") hist2.GetYaxis().SetTitle("gen_beta") hist2.Sumw2() tr.Project(name,br_gen+":"+br_reco,cut) return hist2 ### analysis cuts for data if mc: sys_e = ["pt", "es", "esup", "esdown", "jarup", "jardown", "pu_w_up", "pu_w_down"] else: sys_e = ["pt","eup","edown"] hist_l = [] for sys in sys_e: if mc: e_w = "*(%s_pu_w)"%(sys) if sys.startswith("pu_w"): e_w = "*(pt_%s)"%sys else: e_w = "*(1.0)" if sys.startswith("pu_w"): s_name = sys sys = "pt" else: s_name = sys tr_beta = in_rf.Get("%s_beta"%sys) ## event selection r_mass_cut = "(%s_raw_mass > 220)*"%sys #r_mass_cut = "(%s_raw_mass > 250)*"%sys dr_cut = "(%s_del_r>0.5)*(%s_del_r<1.5)*"%(sys,sys) dr12_cut = "(%s_del_r12<1.0)*"%(sys) eta_cut = "(abs(%s_jet1_eta)<2.5)*"%sys
if isNumber(word): sysVersion = word else: ws = word.split('v') for w in ws: if isNumber(w): sysVersion = w #print sys, sysVersion for tech in roadMapDates: #Check how many words match #Note: first word must match, as should the version (if present) words = tech.split() matchCount = 0 if sysVersion == "": matchVersion = True else: matchVersion = False if sys.startswith(words[0]): for word in words: if isNumber(word) and sysVersion == word: matchVersion = True if word in sys: matchCount += 1 if matchCount > 2 and matchVersion: #print "Found match: %s is %s" % (tech, sys) date = roadMapDates[tech] matches[sysSoftware[sys]] = (sys, tech, date[0], date[1]) matched = True break else: date = roadMapDates[sysNew] matches[sysSoftware[sys]] = (sys, sysNew, date[0], date[1]) matched = True #print "Found exact match: %s is %s" % (sys, sys)
for j, pt in enumerate(pt_bin_cut): for k, dr in enumerate(dr_bin_cut): for l, pt3 in enumerate(pt3_bin_cut): cut_l.append(d_cut+eta+pt+dr+pt3+"(1.0)") cut_name.append(eta_bin[i]+"_"+pt_bin[j]+"_"+dr_bin[k]+"_"+pt3_bin[l]) if mc: sys_e = ["nom", "jer","jer_u", "jer_d", "jar", "pu_u", "pu_d"] sys_e = ["nom"] else: sys_e = ["nom", "jes_u", "jes_d"] hist_l = [] for sys in sys_e: if mc: e_w = "({}*pileupWeight)".format(mc_ss) if sys.startswith("pu"): if sys == "pu_u": e_w = "(1.0*pileupWeight_up)" else: e_w = "(1.0*pileupWeight_dn)" else: e_w = "(1.0)" for i, cut in enumerate(cut_l): if sys.startswith("pu"): tr = in_rf.Get("cc/nom").Clone(cut_name[i]) else: tr = in_rf.Get("cc/%s"%sys).Clone(cut_name[i]) #tr.Draw(">>eventList", cut) #el = ROOT.gDirectory.Get("eventList") #tr.SetEventList(el)
def systematics(sys, isShape=False, antiCorr=False): treeRead = True file = {} hist = {} tree = {} histUp = {} histDown = {} up = {} down = {} gUp = TGraph() gDown = TGraph() gUp.SetLineWidth(3) gDown.SetLineWidth(3) gUp.SetLineColor(632) gDown.SetLineColor(602) BTagAK4deepup = False BTagAK4deepdown = False BTagAK4deep = False BTagAK8deepup = False BTagAK8deepdown = False BTagAK8deep = False # g.SetMarkerStyle(20) # g.SetMarkerColor(418) # g.SetMarkerSize(1.25) var = sys cut = 'nnbbSR' if var == 'BTagAK4Weight_deep_up': var = 'X_mass' BTagAK4deepup = True elif var == 'BTagAK4Weight_deep_down': var = 'X_mass' BTagAK4deepdown = True elif var == 'BTagAK4Weight_deep': var = 'X_mass' BTagAK4deep = True elif var == 'BTagAK8Weight_deep_up': var = 'X_mass' BTagAK8deepup = True elif var == 'BTagAK8Weight_deep_down': var = 'X_mass' BTagAK8deepdown = True elif var == 'BTagAK8Weight_deep': var = 'X_mass' BTagAK8deep = True upAvg, downAvg, upMin, downMin, upMax, downMax = 0., 0., 2., 2., 0., 0. for k in sorted(selection.keys(), key=len, reverse=True): if k in cut: cut = cut.replace(k, selection[k]) for i, s in enumerate(sign): if '_MZ' in s: m = int((s.split('_MZ')[1]).split('_MA')[0]) elif '_M' in s: m = int(s.split('_M')[1]) else: m = 0 if treeRead: # Project from tree tree[s] = TChain("tree") for j, ss in enumerate(sample[s]['files']): if not 'data' in s or ('data' in s and ss in pd): tree[s].Add(NTUPLEDIR + ss + ".root") if variable[var]['nbins'] > 0: min_value = variable[var]['min'] max_value = variable[var]['max'] title = variable[var]['title'] if 'isZtoNN' in cut: if var == 'MET': min_value = 200 max_value = 2000 elif var == 'DPhi': title = "#Delta #varphi (AK8 jet-#slash{E}_{T})" elif var == 'VH_deltaR': title = "#Delta R (#slash{E}_{T}, AK8 jet)" hist[s] = TH1F( s, ";" + title + ";Events;" + ('log' if variable[var]['log'] else ''), variable[var]['nbins'], min_value, max_value) else: hist[s] = TH1F(s, ";" + variable[var]['title'], len(variable[var]['bins']) - 1, array('f', variable[var]['bins'])) hist[s].Sumw2() cutstring = "(eventWeightLumi)" + ("*(" + cut + ")") if var == 'LeptonWeightUp': cutstring = "(eventWeightLumi * LeptonWeightUp/LeptonWeight)" + ( "*(" + cut + ")") elif var == 'LeptonWeightDown': cutstring = "(eventWeightLumi * LeptonWeightDown/LeptonWeight)" + ( "*(" + cut + ")") elif var == 'TriggerWeightUp': cutstring = "(eventWeightLumi * TriggerWeightUp/TriggerWeight)" + ( "*(" + cut + ")") elif var == 'TriggerWeightDown': cutstring = "(eventWeightLumi * TriggerWeightDown/TriggerWeight)" + ( "*(" + cut + ")") #division by BTagAk4Weight_deep is because the weighted samples are used elif BTagAK4deepup: cutstring = "(eventWeightLumi * BTagAK4Weight_deep_up/BTagAK4Weight_deep)" + ( "*(" + cut + ")") elif BTagAK4deepdown: cutstring = "(eventWeightLumi * BTagAK4Weight_deep_down/BTagAK4Weight_deep)" + ( "*(" + cut + ")") elif BTagAK8deep: cutstring = "(eventWeightLumi * BTagAK8Weight_deep)" + ( "*(" + cut + ")") elif BTagAK8deepup: cutstring = "(eventWeightLumi * BTagAK8Weight_deep_up)" + ( "*(" + cut + ")") elif BTagAK8deepdown: cutstring = "(eventWeightLumi * BTagAK8Weight_deep_down)" + ( "*(" + cut + ")") tree[s].Project(s, var, cutstring) if not tree[s].GetTree() == None: hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast()) """ for j, ss in enumerate(sample[s]['files']): file[ss] = TFile(NTUPLEDIR + ss + ".root", "READ") tmp = file[ss].Get("Sys/"+sys) if tmp == None: continue if not s in hist.keys(): hist[s] = tmp else: if antiCorr: for x in range(1, hist[s].GetNbinsX()+1): hist[s].SetBinContent(x, hist[s].GetBinContent(x)+tmp.GetBinContent(tmp.GetNbinsX()+1-x)) else: hist[s].Add( tmp ) if isShape: tmp = file[ss].Get("Sys/"+sys+"_up") print "Sys/"+sys+"_up" if tmp == None: continue if not s in histUp.keys(): histUp[s] = tmp else: histUp[s].Add( tmp ) # tmp = file[ss].Get("Sys/"+sys+"_down") if tmp == None: continue if not s in histDown.keys(): histDown[s] = tmp else: histDown[s].Add( tmp ) if 'accept' in sys: norm = None for j, ss in enumerate(sample[s]['files']): tmp = file[ss].Get("Sys/PDF_scale") if tmp == None: continue if norm == None: norm = tmp else: norm.Add( tmp ) hist[s].Divide(norm) """ if (isShape): shape = TF1("shape", "gaus", 0, 5000) shapeUp = TF1("shapeUp", "gaus", 0, 5000) shapeDown = TF1("shapeDown", "gaus", 0, 5000) hist[s].Fit(shape, "Q0", "") histUp[s].Fit(shapeUp, "Q0", "") histDown[s].Fit(shapeDown, "Q0", "") if 'scale' in sys or 'unc' in sys: up[s] = histUp[s].GetMean() / hist[s].GetMean() down[s] = histDown[s].GetMean() / hist[s].GetMean() # up[s] = shapeUp.GetParameter(1)/shape.GetParameter(1) # down[s] = shapeDown.GetParameter(1)/shape.GetParameter(1) elif 'res' in sys: up[s] = histUp[s].GetRMS() / hist[s].GetRMS() down[s] = histDown[s].GetRMS() / hist[s].GetRMS() # up[s] = shapeUp.GetParameter(2)/shape.GetParameter(2) # down[s] = shapeDown.GetParameter(2)/shape.GetParameter(2) else: up[s] = hist[s].GetBinContent( hist[s].FindBin(+1)) / hist[s].GetBinContent( hist[s].FindBin(0)) down[s] = hist[s].GetBinContent( hist[s].FindBin(-1)) / hist[s].GetBinContent( hist[s].FindBin(0)) gUp.SetPoint(i, m, up[s]) gDown.SetPoint(i, m, down[s]) #if mass < 1000: continue upAvg += up[s] downAvg += down[s] if abs(up[s]) > upMax: upMax = abs(up[s]) if abs(up[s]) < upMin: upMin = abs(up[s]) if abs(down[s]) > downMax: downMax = abs(down[s]) if abs(down[s]) < downMin: downMin = abs(down[s]) upAvg /= len(sign) downAvg /= len(sign) print " ---", sys, "--- | up: %.3f, down: %.3f, average: %.3f" % ( upAvg, downAvg, abs(upAvg - 1 + 1. - downAvg) / 2.), "|", "^{%.1f-%.1f}_{%.1f-%.1f}" % (100. * (1. - upMin), 100. * (1. - upMax), 100. * (downMin - 1.), 100. * (downMax - 1.)) c1 = TCanvas("c1", "Signals", 800, 600) c1.cd() c1.GetPad(0).SetTicky(2) gUp.Draw("AL") gDown.Draw("SAME, L") gUp.GetYaxis().SetRangeUser(0.65, 1.35) gUp.GetXaxis().SetTitle("m_{X} (GeV)") gUp.GetYaxis().SetTitle("Uncertainty") leg = TLegend(0.5, 0.90 - 0.20, 0.9, 0.90) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) leg.SetHeader(sys.replace('_', ' ')) leg.AddEntry(gUp, "+1 s. d. (%.1f%%)" % (100. * (upAvg - 1.)), "l") leg.AddEntry(gDown, " -1 s. d. (%.1f%%)" % (100. * (1. - downAvg)), "l") leg.Draw() drawCMS(-1, "Simulation", False) c1.Update() filename = sys if sys.startswith('W_mass') or sys.startswith('Z_mass'): filename += "_" + sign[0][:3] # c1.Print("plots/Systematics/"+filename+".png") # c1.Print("plots/Systematics/"+filename+".pdf") if not gROOT.IsBatch(): raw_input("Press Enter to continue...") if 'doubleB' in sys or 'subjet' in sys or 'mass' in sys: print sys + " = {", for m in [ 800, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 2000, 2500, 3000, 3500, 4000, 4500 ]: print "%d : [%.3f, %.3f], " % (m, gUp.Eval(m), gDown.Eval(m)), print "}" if 'extr' in sys or 'tagging' in sys: print sys + " = {", for m in [800, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]: print "%d : [%.3f, %.3f], " % (m, gUp.Eval(m), gDown.Eval(m)), print "}" if 'QCD_scale' in sys or 'PDF_scale' in sys: print sys + " = {", for m in range(800, 4500 + 1, 10): print "%d : [%.3f, %.3f], " % (m, gUp.Eval(m), gDown.Eval(m)), print "}"