def makeBinHistos(var): for s in samples.itervalues(): hfilename = "%s/%s_%s.root" % (outhistos, s.label, opt.channel) hfile = ROOT.TFile(hfilename, "UPDATE") print "file ", hfile hin = hfile.Get(var) if not isinstance(hin, ROOT.TH1): raise RuntimeError('Failed to load histogram %s from %s' % (hin, var)) else: ibin = 7 #print "Sample: ",s.label #print "Variable: ",hin.GetName() #print "Integral: ",hin.Integral() #print "Number of bins: ",hin.GetXaxis().GetNbins() # nh = shift(hin, ib, "up") #nh = shift(hin, ibin, "down") mcStat_down = [ shift(hin, ib, "Down", s.label, opt.channel, var) for ib in xrange(1, hin.GetXaxis().GetNbins() + 1) ] mcStat_up = [ shift(hin, ib, "Up", s.label, opt.channel, var) for ib in xrange(1, hin.GetXaxis().GetNbins() + 1) ] hfile.cd() [nh.Write() for nh in mcStat_down] [nh.Write() for nh in mcStat_up] hfile.Close()
def createSyst(syst, newsyst, versus, verbose = False): verbose = True for s in samples.itervalues(): # if(not s.label.startswith("Z")):continue for ch in channels: src = s.label + "_" + ch + ".root" dst = [s.label + "_" + ch + "_" + nSyst + versus + ".root" for nSyst in newsyst] if(verbose): for d in dst: print "Copying 1 " + opt.path + src + " to "+ opt.path + d [shutil.copyfile(opt.path + src, opt.path + d) for d in dst] srcSys = s.label + "_" + ch + "_" + syst + versus + ".root" if(s.label.startswith("WJets")): dstSys = s.label + "_" + ch + "_" + newsyst[0]+ versus + ".root" if verbose: print "Copying W ++ " + opt.path + srcSys + " to "+ opt.path + dstSys shutil.copyfile(opt.path + srcSys, opt.path + dstSys) elif(s.label.startswith("ZToNuNu") or s.label.startswith("DY")): dstSys = s.label + "_" + ch + "_" + newsyst[1]+ versus + ".root" if verbose: print "Copying Z ++ " + opt.path + srcSys + " to "+ opt.path + dstSys shutil.copyfile(opt.path + srcSys, opt.path + dstSys)
def makeBinHistos(var): for s in samples.itervalues(): hfilename = "%s/%s_%s.root" % (outhistos,s.label,opt.channel) hfile = ROOT.TFile(hfilename, "UPDATE") print "file ", hfile hin = hfile.Get(var) if not isinstance(hin, ROOT.TH1): raise RuntimeError('Failed to load histogram %s from %s' % (hin, var)) else: ibin = 7 #print "Sample: ",s.label #print "Variable: ",hin.GetName() #print "Integral: ",hin.Integral() #print "Number of bins: ",hin.GetXaxis().GetNbins() # nh = shift(hin, ib, "up") #nh = shift(hin, ibin, "down") mcStat_down = [ shift(hin, ib, "Down",s.label, opt.channel, var) for ib in xrange(1, hin.GetXaxis().GetNbins()+1) ] mcStat_up = [ shift(hin, ib, "Up", s.label, opt.channel, var) for ib in xrange(1, hin.GetXaxis().GetNbins()+1) ] hfile.cd() [nh.Write() for nh in mcStat_down] [nh.Write() for nh in mcStat_up] hfile.Close()
def createSyst(versus, verbose=True): for s in samples.itervalues(): for ch in channels: src = s.label + "_" + ch + ".root" dst = [ s.label + "_" + ch + "_" + nSyst + versus + ".root" for nSyst in newsyst ] if (verbose): for d in dst: print "Copying " + opt.path + src + " to " + opt.path + d [shutil.copyfile(opt.path + src, opt.path + d) for d in dst] srcSys = s.label + "_" + ch + "_" + syst + versus + ".root" dstSys = s.label + "_" + ch + "_" + syst + s.label + versus + ".root" if s.label.startswith("DM"): dstSys = s.label + "_" + ch + "_" + syst + "DMtt" + versus + ".root" if (s.label.startswith("ZToNuNu") or s.label.startswith("DY")): dstSys = s.label + "_" + ch + "_" + syst + "ZJets" + versus + ".root" if verbose: print "Copying " + opt.path + srcSys + " to " + opt.path + dstSys shutil.copyfile(opt.path + srcSys, opt.path + dstSys)
def createSyst(syst, newsyst, versus, verbose=False): verbose = True for s in samples.itervalues(): # if(not s.label.startswith("Z")):continue for ch in channels: src = s.label + "_" + ch + ".root" dst = [ s.label + "_" + ch + "_" + nSyst + versus + ".root" for nSyst in newsyst ] if (verbose): for d in dst: print "Copying 1 " + opt.path + src + " to " + opt.path + d [shutil.copyfile(opt.path + src, opt.path + d) for d in dst] srcSys = s.label + "_" + ch + "_" + syst + versus + ".root" if (s.label.startswith("WJets")): dstSys = s.label + "_" + ch + "_" + newsyst[ 0] + versus + ".root" if verbose: print "Copying W ++ " + opt.path + srcSys + " to " + opt.path + dstSys shutil.copyfile(opt.path + srcSys, opt.path + dstSys) elif (s.label.startswith("ZToNuNu") or s.label.startswith("DY")): dstSys = s.label + "_" + ch + "_" + newsyst[ 1] + versus + ".root" if verbose: print "Copying Z ++ " + opt.path + srcSys + " to " + opt.path + dstSys shutil.copyfile(opt.path + srcSys, opt.path + dstSys)
def symmetrize(syst): for s in samples.itervalues(): for ch in channels: nom = opt.path + s.label + "_" + ch + ".root" up = opt.path + s.label + "_" + ch + "_"+syst+"Up.root" down = opt.path + s.label + "_" + ch + "_"+syst+"Down.root" f_nom = ROOT.TFile(nom) f_up = ROOT.TFile(up) f_down = ROOT.TFile(down) vars = varsFh regsOff=regsFh_SlUnc if (ch=="semileptonic"): vars = varsSl regsOff=regsSl_FhUnc for v, region in vars.iteritems(): sysUp = opt.path + s.label + "_" + ch +"_"+ syst + region + "Up.root" sysDown =opt.path + s.label + "_" + ch +"_"+ syst + region + "Down.root" #print "++ New File Up: ", sysUp #print "++ New File Down: ", sysDown print "+ Copying " + nom + " to "+ sysUp shutil.copyfile(nom, sysUp) print "+ Copying " + nom + " to "+ sysDown shutil.copyfile(nom, sysDown) h_up = f_up.Get(v) raiseError(h_up, v, up) h_down = f_down.Get(v) raiseError(h_down, v, down) nf_up = ROOT.TFile(sysUp, "UPDATE") nf_up.cd() nh_up = copy.deepcopy(h_up) nh_up.Write() nf_up.Close() nf_down = ROOT.TFile(sysDown, "UPDATE") nf_down.cd() nh_down = copy.deepcopy(h_down) nh_down.Write() nf_down.Close() for region in regsOff: sysUp = opt.path + s.label + "_" + ch +"_"+ syst + region + "Up.root" sysDown =opt.path + s.label + "_" + ch +"_"+ syst + region + "Down.root" print "+ Copying " + nom + " to "+ sysUp shutil.copyfile(nom, sysUp) print "+ Copying " + nom + " to "+ sysDown shutil.copyfile(nom, sysDown)
def applyXSecSys(): syst = "QCD_xsec" unc_xsec = 0.5 for s in samples.itervalues(): for ch in channels: nom = opt.path + s.label + "_" + ch + ".root" f_nom = ROOT.TFile(nom) sysUp = opt.path + s.label + "_" + ch + "_" + syst + "Up.root" sysDown = opt.path + s.label + "_" + ch + "_" + syst + "Down.root" shutil.copyfile(nom, sysUp) shutil.copyfile(nom, sysDown) if (ch == "fullhadronic"): histos_up = [] histos_down = [] for v, region in varsFh.iteritems(): h = f_nom.Get(v) raiseError(h, v, nom) nh_up = copy.deepcopy(h) nh_down = copy.deepcopy(h) if (s.label == "QCD"): nh_up.Scale(1.0 + unc_xsec) histos_up.append(nh_up) if (s.label == "QCD"): nh_down.Scale(1.0 - unc_xsec) histos_down.append(nh_down) nf_up = ROOT.TFile(sysUp, "UPDATE") nf_up.cd() [h.Write() for h in histos_up] nf_up.Close() nf_down = ROOT.TFile(sysDown, "UPDATE") nf_down.cd() [h.Write() for h in histos_down] nf_down.Close()
def applySys(): syst = "Trigger" for s in samples.itervalues(): for ch in channels: nom = opt.path + s.label + "_" + ch + ".root" f_nom = ROOT.TFile(nom) sysUp = opt.path + s.label + "_" + ch + "_" + syst + "Up.root" sysDown = opt.path + s.label + "_" + ch + "_" + syst + "Down.root" shutil.copyfile(nom, sysUp) shutil.copyfile(nom, sysDown) if (ch == "cat"): histos_up = [] histos_down = [] for v, region in varsFh.iteritems(): h = f_nom.Get(v) raiseError(h, v, nom) nh_up = copy.deepcopy(h) nh_down = copy.deepcopy(h) nh_up.Scale(1.02) histos_up.append(nh_up) nh_down.Scale(0.98) histos_down.append(nh_down) nf_up = ROOT.TFile(sysUp, "UPDATE") nf_up.cd() [h.Write() for h in histos_up] nf_up.Close() nf_down = ROOT.TFile(sysDown, "UPDATE") nf_down.cd() [h.Write() for h in histos_down] nf_down.Close()
def createSyst(versus, verbose = True): for s in samples.itervalues(): for ch in channels: src = s.label + "_" + ch + ".root" dst = [s.label + "_" + ch + "_" + nSyst + versus + ".root" for nSyst in newsyst] if(verbose): for d in dst: print "Copying " + opt.path + src + " to "+ opt.path + d [shutil.copyfile(opt.path + src, opt.path + d) for d in dst] srcSys = s.label + "_" + ch + "_" + syst + versus + ".root" dstSys = s.label + "_" + ch + "_" + syst + s.label + versus + ".root" if s.label.startswith("DM"): dstSys = s.label + "_" + ch + "_" + syst + "DMtt"+ versus +".root" if (s.label.startswith("ZToNuNu") or s.label.startswith("DY")): dstSys = s.label + "_" + ch + "_" + syst + "ZJets"+ versus +".root" if verbose: print "Copying " + opt.path + srcSys + " to "+ opt.path + dstSys shutil.copyfile(opt.path + srcSys, opt.path + dstSys)
def symmetrize(syst): for s in samples.itervalues(): for ch in channels: print "+channel ", ch, " samples ", s.label nom = opt.path + s.label + "_" + ch + ".root" jesUp = opt.path + s.label + "_" + ch + "_"+syst+"Up.root" jesDown = opt.path + s.label + "_" + ch + "_"+syst+"Down.root" jesUpSym = opt.path + s.label + "_" + ch + "_sym"+syst+"Up.root" jesDownSym = opt.path + s.label + "_" + ch + "_sym"+syst+"Down.root" f_nom = ROOT.TFile(nom) f_jup = ROOT.TFile(jesUp) f_jdown = ROOT.TFile(jesDown) vars = VariablesFh if (ch=="semileptonic"): vars = VariablesSl histos_up = [] histos_down = [] for v in vars: h_nom = f_nom.Get(v) h_jup = f_jup.Get(v) h_jdown = f_jdown.Get(v) raiseError(h_nom, v, f_nom) raiseError(h_jup, v, f_jup) raiseError(h_jdown, v, f_jdown) ## nh_nom = copy.deepcopy(h_nom) ## nh_up = copy.deepcopy(h_jup) ## nh_down = copy.deepcopy(h_jdown) ## #nh_nom.Scale(2) ## nh_up.Add(nh_down, -1) ## nh_up.Scale(0.5) ## #print "+ Nom Integral: ", nh_up.Integral() ## nh_up.Smooth() ## #print "++ Nom Integral after smoothing: ", nh_up.Integral() ## sym_jup = copy.deepcopy(h_nom) ## sym_jup.Add(nh_up) ## sym_jdown = copy.deepcopy(h_nom) ## sym_jdown.Add(nh_up,-1) #if(opt.debug and s.label.startswith("TT") and ch=="semileptonic"): if(opt.debug ): print "Variable: ",v print "+ JesUp: ", h_jup.Integral() print "+ Nom: ", h_nom.Integral() print "+ JesDown: ", h_jdown.Integral() ## print "++ New jesUp: ", sym_jup.Integral() ## print "++ Nom: ", h_nom.Integral() ## print "++ New jesDown: ", sym_jdown.Integral() ## print "-"*30 sym_new_jup = copy.deepcopy(h_nom) sym_new_jdown = copy.deepcopy(h_nom) k_smooth = copy.deepcopy(h_nom) print " --> Symmetrization " for b in xrange(h_nom.GetNbinsX()): print "bin ", b + 1 up = h_jup.GetBinContent(b+1) nom = h_nom.GetBinContent(b+1) down = h_jdown.GetBinContent(b+1) #print "up ", up, " down ", down #k = ROOT.TMath.Abs(up-down)/2 k = max(ROOT.TMath.Abs(up-nom),ROOT.TMath.Abs(nom-down)) print " k_smooth ", nom-k, " for bin ", b k_smooth.SetBinContent(b+1, k) #k_smooth.Smooth(nsmooth) print " --> Smoothing " for b in xrange(k_smooth.GetNbinsX()): print "k_smooth post bin ", b+1, " value= ", k_smooth.GetBinContent(b+1) sym_new_jup.Add(k_smooth) sym_new_jdown.Add(k_smooth, -1) for b in xrange(sym_new_jdown.GetNbinsX()): if(sym_new_jdown.GetBinContent(b+1)<0): nom = h_nom.GetBinContent(b+1) sym_new_jdown.SetBinContent(b+1, (ROOT.TMath.Power(10,-6)*nom)) print " --> CHECK nom = ", nom, " down ", sym_new_jdown.GetBinContent(b+1) histos_up.append( sym_new_jup) histos_down.append( sym_new_jdown) print "++ New-2 jesUp: ", sym_new_jup.Integral() print "++ Nom: ", h_nom.Integral() print "++ New-2 jesDown: ", sym_new_jdown.Integral() print "-"*30 f_symjup = ROOT.TFile(jesUpSym,"RECREATE") f_symjup.cd() [h.Write() for h in histos_up] f_symjup .Close() f_symjdown = ROOT.TFile(jesDownSym, "RECREATE") f_symjdown.cd() [h.Write() for h in histos_down] f_symjdown .Close()
# regions = ["2j0t"] rf = opt.fileSF # rf="./mlfit.root" print "rootfile is ", rf postFit = PFUtils(rootfile=rf, regions=regions, channel=opt.channel, sys=opt.sys, format=opt.getSF) # print postFit._rootfile #regions = # import postFitUtils as postFitSF keylist = list(settings.keys()) sampleslist = [] for s in samples.itervalues(): sampleslist.append(s.label) # list(samples.keys()) print "Printing samples ==== ", sampleslist sampleslist.remove('Data') print sampleslist importedSFs = postFit.importScaleFactors(variables=keylist, samples=sampleslist, defaultSF="no_region") print importedSFs #parser.add_option('', '--getSF', dest='getSF', type='string', default = '', help='type of scale factors from the fit') #parser.add_option('', '--fileSF', dest='fileSF', type='string', default = './mlfit.root', help='file for the scale factors') for var, (title, scale, rebin, usrrng) in settings.iteritems():
print "Rebin: ", rebin #THStack for plotting stack_bkg = Stack(var,title) stack_sig = Stack(var,title) stack_data = Stack(var,title) stack_sh = Stack(var,title) leg = Legend() leg_sh = Legend() # leg_sign = ROOT.TLegend(0.7,0.7,0.9,0.9) h1 = None for s in samples.itervalues(): nEntries = 0 #print "+ sample" , s.label if(hasattr(s, "components")): #samples with components histos = [] notFound = [] for c in s.components: #print "comp ", c.label if c.label.startswith("MET") and opt.channel == "semileptonic": continue elif ((c.label.startswith("SingleMu") or c.label.startswith("SingleEl")) and (("CR3" not in var) and ("CR6" not in var) and ("CR7" not in var) and ("CRtt" not in var) and ("SR_1lep" not in var)) and (opt.channel == "fullhadronic")): print "Fullhadronic channel, skipping dataset: " + c.label +" for variable " + var
def symmetrize(syst): for s in samples.itervalues(): for ch in channels: print "+channel ", ch, " samples ", s.label nom = opt.path + s.label + "_" + ch + ".root" jesUp = opt.path + s.label + "_" + ch + "_"+syst+"Up.root" jesDown = opt.path + s.label + "_" + ch + "_"+syst+"Down.root" jesUpSym = opt.path + s.label + "_" + ch + "_sym"+syst+"Up.root" jesDownSym = opt.path + s.label + "_" + ch + "_sym"+syst+"Down.root" f_nom = ROOT.TFile(nom) f_jup = ROOT.TFile(jesUp) f_jdown = ROOT.TFile(jesDown) vars = VariablesFh if (ch=="semileptonic"): vars = VariablesSl histos_up = [] histos_down = [] for v in vars: h_nom = f_nom.Get(v) h_jup = f_jup.Get(v) h_jdown = f_jdown.Get(v) raiseError(h_nom, v, f_nom) raiseError(h_jup, v, f_jup) raiseError(h_jdown, v, f_jdown) ## nh_nom = copy.deepcopy(h_nom) ## nh_up = copy.deepcopy(h_jup) ## nh_down = copy.deepcopy(h_jdown) ## #nh_nom.Scale(2) ## nh_up.Add(nh_down, -1) ## nh_up.Scale(0.5) ## #print "+ Nom Integral: ", nh_up.Integral() ## nh_up.Smooth() ## #print "++ Nom Integral after smoothing: ", nh_up.Integral() ## sym_jup = copy.deepcopy(h_nom) ## sym_jup.Add(nh_up) ## sym_jdown = copy.deepcopy(h_nom) ## sym_jdown.Add(nh_up,-1) #if(opt.debug and s.label.startswith("TT") and ch=="semileptonic"): if(opt.debug ): print "Variable: ",v print "+ JesUp: ", h_jup.Integral() print "+ Nom: ", h_nom.Integral() print "+ JesDown: ", h_jdown.Integral() ## print "++ New jesUp: ", sym_jup.Integral() ## print "++ Nom: ", h_nom.Integral() ## print "++ New jesDown: ", sym_jdown.Integral() ## print "-"*30 sym_new_jup = copy.deepcopy(h_nom) sym_new_jdown = copy.deepcopy(h_nom) k_smooth = copy.deepcopy(h_nom) print " --> Symmetrization " for b in xrange(h_nom.GetNbinsX()): print "bin ", b + 1 up = h_jup.GetBinContent(b+1) nom = h_nom.GetBinContent(b+1) down = h_jdown.GetBinContent(b+1) #print "up ", up, " down ", down #k = ROOT.TMath.Abs(up-down)/2 k = max(ROOT.TMath.Abs(up-nom),ROOT.TMath.Abs(nom-down)) print " k_smooth ", nom-k, " for bin ", b k_smooth.SetBinContent(b+1, k) #k_smooth.Smooth(nsmooth) print " --> Smoothing " for b in xrange(k_smooth.GetNbinsX()): print "k_smooth post bin ", b+1, " value= ", k_smooth.GetBinContent(b+1) sym_new_jup.Add(k_smooth) sym_new_jdown.Add(k_smooth, -1) for b in xrange(sym_new_jdown.GetNbinsX()): if(sym_new_jdown.GetBinContent(b+1)<0): nom = h_nom.GetBinContent(b+1) sym_new_jdown.SetBinContent(b+1, (ROOT.TMath.Power(10,-6)*nom)) print " --> CHECK nom = ", nom, " down ", sym_new_jdown.GetBinContent(b+1) histos_up.append( sym_new_jup) histos_down.append( sym_new_jdown) print "++ New-2 jesUp: ", sym_new_jup.Integral() print "++ Nom: ", h_nom.Integral() print "++ New-2 jesDown: ", sym_new_jdown.Integral() print "-"*30 f_symjup = ROOT.TFile(jesUpSym,"RECREATE") f_symjup.cd() [h.Write() for h in histos_up] f_symjup .Close() f_symjdown = ROOT.TFile(jesDownSym, "RECREATE") f_symjdown.cd() [h.Write() for h in histos_down] f_symjdown .Close()
def symmetrize(syst): for s in samples.itervalues(): for ch in channels: nom = opt.path + s.label + "_" + ch + ".root" jesUp = opt.path + s.label + "_" + ch + "_" + syst + "Up.root" jesDown = opt.path + s.label + "_" + ch + "_" + syst + "Down.root" jesUpSym = opt.path + s.label + "_" + ch + "_sym" + syst + "Up.root" jesDownSym = opt.path + s.label + "_" + ch + "_sym" + syst + "Down.root" #shutil.copyfile(nom, jesUp) shutil.copyfile(nom, jesDown) f_nom = ROOT.TFile(nom) f_jup = ROOT.TFile(jesUp) f_jdown = ROOT.TFile(jesDown) vars = VariablesFh if (ch == "semileptonic"): vars = VariablesSl histos_up = [] histos_down = [] for v in vars: h_nom = f_nom.Get(v) h_jup = f_jup.Get(v) h_jdown = f_jdown.Get(v) raiseError(h_nom, v, f_nom) raiseError(h_jup, v, f_jup) raiseError(h_jdown, v, f_jdown) ## nh_nom = copy.deepcopy(h_nom) ## nh_up = copy.deepcopy(h_jup) ## nh_down = copy.deepcopy(h_jdown) ## #nh_nom.Scale(2) ## nh_up.Add(nh_down, -1) ## nh_up.Scale(0.5) ## #print "+ Nom Integral: ", nh_up.Integral() ## nh_up.Smooth() ## #print "++ Nom Integral after smoothing: ", nh_up.Integral() ## sym_jup = copy.deepcopy(h_nom) ## sym_jup.Add(nh_up) ## sym_jdown = copy.deepcopy(h_nom) ## sym_jdown.Add(nh_up,-1) #if(opt.debug and s.label.startswith("TT") and ch=="semileptonic"): if (opt.debug): print "Variable: ", v print "+ JesUp: ", h_jup.Integral() print "+ Nom: ", h_nom.Integral() print "+ JesDown: ", h_jdown.Integral() ## print "++ New jesUp: ", sym_jup.Integral() ## print "++ Nom: ", h_nom.Integral() ## print "++ New jesDown: ", sym_jdown.Integral() ## print "-"*30 sym_new_jup = copy.deepcopy(h_nom) sym_new_jdown = copy.deepcopy(h_nom) k_smooth = copy.deepcopy(h_nom) for b in xrange(h_nom.GetNbinsX()): # print b + 1 up = h_jup.GetBinContent(b + 1) nom = h_nom.GetBinContent(b + 1) down = h_jdown.GetBinContent(b + 1) k = ROOT.TMath.Abs(up - nom) k_smooth.SetBinContent(b + 1, k) #sym_new_jup.Add(k_smooth) sym_new_jdown.Add(k_smooth, -1) for b in xrange(sym_new_jdown.GetNbinsX()): if (sym_new_jdown.GetBinContent(b + 1) < 0): print "+ readapting histo down " sym_new_jdown.SetBinContent(b + 1, 0) histos_up.append(sym_new_jup) histos_down.append(sym_new_jdown) print "++ New-2 jesUp: ", sym_new_jup.Integral() print "++ Nom: ", h_nom.Integral() print "++ New-2 jesDown: ", sym_new_jdown.Integral() print "-" * 30 if (s.label.startswith("WJets") or s.label.startswith("DY") or s.label.startswith("Z")): f_symjdown = ROOT.TFile(jesDown, "RECREATE") f_symjdown.cd() [h.Write() for h in histos_down] f_symjdown.Close()