def merge_hists(hists_d, merge_groups): out_d = dict() for merge_name, items in merge_groups.items(): hist = hists_d[items[0]].Clone() for item in items[1:]: hist.Add(hists_d[item]) out_d[merge_name] = hist out_d[merge_name].SetTitle("%s" % merge_name) return out_d
def get_sample_dict(path, sample_d): out_d = dict() for (name, samples) in sample_d.items(): files = [] for samp in samples: fn = path + "/" + samp + ".root" try: fi = ROOT.TFile(fn) except Exception as e: logging.getLogger().warning("Could not open sample %s: %s" % (fn, str(e))) fi = None if fi: files.append(fi) out_d[name] = files return out_d
import ROOT from odict import OrderedDict as dict import string import logging #Here the latter items will become topmost in stacks merge_cmds = dict() merge_cmds["data"] = ["SingleMu"] merge_cmds["diboson"] = ["WW", "WZ", "ZZ"] merge_cmds["W(#rightarrow l #nu) + jets"] = [ "W1Jets_exclusive", "W2Jets_exclusive", "W3Jets_exclusive", "W4Jets_exclusive" ] merge_cmds["DY-jets"] = ["DYJets"] merge_cmds["t#bar{t} (#rightarrow lq, ll)"] = [ "TTJets_FullLept", "TTJets_SemiLept" ] #merge_cmds["t#bar{t}"] = ["TTJets_MassiveBinDECAY"] merge_cmds["tW-channel"] = ["T_tW", "Tbar_tW"] merge_cmds["s-channel"] = ["T_s", "Tbar_s"] merge_cmds["t-channel"] = ["T_t", "Tbar_t"] def lumi_textbox(lumi, pos="top-left"): """ This method creates and draws the "CMS Preliminary" luminosity box, displaying the lumi in 1/fb and the COM energy. **Mandatory arguments: lumi - the integrated luminosity in 1/pb
infile_SL = set_file_name("histograms_presel_2b_SL", mctrig, topw, noWeight, dosys=args.doSys) standalone = True f_SL = ROOT.TFile(indir + infile_SL) pars_SL = f_SL.Get("pars") # read lumi normalization of mc histograms hist_lumi_SL = pars_SL.GetBinContent(pars_SL.GetXaxis().FindBin("Lumi") ) #lf = 19.6/hist_lumi_SL # scale lumi to new value if needed, default 1 lf=1 Lumi = hist_lumi_SL*lf from odict import OrderedDict as dict processes_SM = dict() processes_SM["ttH125"] = f_SL.Get("ttH125/cut_flow_smu_ttH125") processes_SM["ttjj"] = f_SL.Get("ttjj/cut_flow_smu_ttjj") processes_SM["ttb"] = f_SL.Get("ttb/cut_flow_smu_ttb") processes_SM["ttbb"] = f_SL.Get("ttbb/cut_flow_smu_ttbb") processes_SM["TTV"] = f_SL.Get("TTV/cut_flow_smu_TTV") processes_SM["SingleT"] = f_SL.Get("SingleT/cut_flow_smu_SingleT") processes_SM["EWK"] = f_SL.Get("EWK/cut_flow_smu_EWK") processes_SM["DiBoson"] = f_SL.Get("DiBoson/cut_flow_smu_DiBoson") sumBkg_SM = processes_SM["ttjj"].Clone("sumBkg") # Get a cut-flow histogram for sum Bkg for proc, cf_hist in processes_SM.iteritems(): if not proc == "ttH125" and not proc=="ttjj": sumBkg_SM.Add(cf_hist) data_SM = f_SL.Get("singleMu_data/cut_flow_smu_singleMu_data") # get cut-flow of data
import re from odict import OrderedDict as dict tracking_cfg_parameters = dict() from sub_fun import read_template, write_outfile, create_cmssw_cfg_from_template, create_batch_submission_script, create_varstrings from infilelists import infilelist_Zee # Chi2 scan tracking_cfg_parameters["maxCand"] = [5, 5, 5, 5, 5, 5 ] tracking_cfg_parameters["maxChi2"] = [10, 30, 50, 100, 300, 2000] tracking_cfg_parameters["nSigma"] = [3, 3, 3, 3, 3, 3 ] # Max cand scan tracking_cfg_parameters["maxCand"] += [3, 4, 6, 7] tracking_cfg_parameters["maxChi2"] += [2000, 2000, 2000, 2000] tracking_cfg_parameters["nSigma"] += [3, 3, 3, 3] # nSigma scan tracking_cfg_parameters["maxCand"] += [5, 5, 5] tracking_cfg_parameters["maxChi2"] += [2000, 2000, 2000] tracking_cfg_parameters["nSigma"] += [2, 4, 5] print "Creating " + str(len(tracking_cfg_parameters["maxCand"]) )+ " jobs" varstrings = create_varstrings(tracking_cfg_parameters, iter = len(tracking_cfg_parameters["maxCand"]), skip_default = False) filelists = { # "Flat_Pt": "", # "Pt10": "", # "Pt100": "",
def legend(hists, pos="top-right", **kwargs): """ ***Mandatory arguments: hists - list of TH1* type objects The title of the histogram(TH1.GetTitle()) is used as the name in the legend. The legend will be in the order of the histograms. ***Optional arguments: names - names of the processes displayed at the legend instead of the histogram titles. Order 1. data 2. MC samples in the reverse order to appear in the legend pos - a string specifying the desired position on the canvas width, height - the size of the legend in relative coordinates styles - an optional list of characters form the set of {p,f} specifying whether to draw a filled legend marker (MC) or a point (data) If the length of style characters does not equal the length of the histogram list, the last character is duplicated to have styles for all histograms. For example: hists=[hist_data, hist_mc1, hist_mc2, hist_mc3] styles=["p","f"] becomes styles["p", "f", "f", "f"] and the data histogram will be drawn as "p", the mc histogram as "f" returns - instance of TLegend. You must keep a reference to the instance to avoid losing the legend, e.g. you must do ... h = TH1F(...) leg = legend([h], ...) <<< note the leg=.. ... Instead of ... legend([h], ...) """ #Names of the processes as displayed on a legend names = kwargs.get("names") #The relative size of the text in the legend text_size = kwargs.get("text_size", 0.03) #If you have long names you should make the width larger width = kwargs.get("width", 0.21) #The height is determined automatically with some sensible settings from #the number of legend items height = kwargs.get("height", 0.03 * len(hists)) #The style characters for drawing the legend styles = kwargs.get("styles", ["p", "f"]) #Fill the style character array to correct length if len(styles) < len(hists): styles += [styles[-1]] * (len(hists) - len(styles)) #Check that each histo has a style character if len(styles) != len(hists): raise ValueError( "styles must have the same number of objects as hists") #We'll be popping form back, so need to reverse styles.reverse() #Some 'reasonable' hard-coded positions #[bottom_left_x, bottom_left_y, top_right_x, top_right_y] #FIXME: fine-tune and make your own if pos == "top-right": leg_coords = [-1, -1, 0.93, 0.91] if pos == "top-left": leg_coords = [-1, -1, 0.45, 0.91] #Calculate the bottom-left coordinate from top right using the width and height if leg_coords[0] == -1: leg_coords[0] = leg_coords[2] - width if leg_coords[1] == -1: leg_coords[1] = leg_coords[3] - height #Expand the array using the wildcard leg = ROOT.TLegend(*leg_coords) if "names" in kwargs: rnames = names[::-1] data = rnames.pop() rnames.insert(0, data) from odict import OrderedDict as dict name_hist_pairs = dict(zip(rnames, hists)) for name in name_hist_pairs: leg_style = styles.pop() leg.AddEntry(name_hist_pairs[name], name, leg_style) else: for hist in hists: leg_style = styles.pop() leg.AddEntry(hist, hist.GetTitle(), leg_style) leg.Draw() leg.SetFillColor(ROOT.kWhite) leg.SetLineColor(ROOT.kWhite) leg.SetTextSize(text_size) #Need to return the legend to keep a reference return leg
f_DL = ROOT.TFile(indir + infile_DL) pars_SL = f_SL.Get("pars") # read lumi normalization of mc histograms pars_DL = f_DL.Get("pars") hist_lumi_SL = pars_SL.GetBinContent(pars_SL.GetXaxis().FindBin("Lumi") ) hist_lumi_DL = pars_DL.GetBinContent(pars_DL.GetXaxis().FindBin("Lumi") ) if hist_lumi_SL != hist_lumi_DL: print "WARNING: lumi_SL != lumi_DL, using lumi_SL" lf = 1 #19.5/hist_lumi # scale lumi to new value if needed, default 1 Lumi = hist_lumi_SL*lf from odict import OrderedDict as dict processes_SL = dict() processes_SL["ttH125"] = f_SL.Get("ttH125/cut_flow_ttH125") processes_SL["ttjj"] = f_SL.Get("ttjj/cut_flow_ttjj") processes_SL["ttb"] = f_SL.Get("ttb/cut_flow_ttb") processes_SL["ttbb"] = f_SL.Get("ttbb/cut_flow_ttbb") processes_SL["TTV"] = f_SL.Get("TTV/cut_flow_TTV") processes_SL["SingleT"] = f_SL.Get("SingleT/cut_flow_SingleT") processes_SL["EWK"] = f_SL.Get("EWK/cut_flow_EWK") processes_SL["DiBoson"] = f_SL.Get("DiBoson/cut_flow_DiBoson") processes_DL = dict() processes_DL["ttH125"] = f_DL.Get("ttH125/cut_flow_ttH125") processes_DL["ttjj"] = f_DL.Get("ttjj/cut_flow_ttjj") processes_DL["ttb"] = f_DL.Get("ttb/cut_flow_ttb") processes_DL["ttbb"] = f_DL.Get("ttbb/cut_flow_ttbb") processes_DL["TTV"] = f_DL.Get("TTV/cut_flow_TTV")
data = data_mu.Clone("data") data.Add(data_el) if not (hist_to_plot[:3] == "num"): data.Rebin(nrebin) if mode=="DL": data_mu = h.Get("diMu_data/" + hist_to_plot + "_diMu_data") data_el = h.Get("diEl_data/" + hist_to_plot + "_diEl_data") data = data_mu.Clone("data") data.Add(data_el) if not(hist_to_plot[:3] == "num"): data.Rebin(nrebin) from odict import OrderedDict as dict mc = dict() mc["EWK"] = h.Get("EWK/" + hist_to_plot + "_EWK") # mc["SingleTop"] = h.Get("SingleT/" + hist_to_plot + "_SingleT") mc["DiBoson"] = h.Get("DiBoson/" + hist_to_plot + "_DiBoson") mc["TTV"] = h.Get("TTV/" + hist_to_plot + "_TTV") if inclusive_ttjets: mc["TTJets"] = h.Get("TTJets/" + hist_to_plot + "_TTJets") else: mc["ttjj"] = h.Get("ttjj/" + hist_to_plot + "_ttjj") mc["ttb"] = h.Get("ttb/" + hist_to_plot + "_ttb") mc["ttbb"] = h.Get("ttbb/" + hist_to_plot + "_ttbb") mc["TTH125"] = h.Get("ttH125/" + hist_to_plot + "_ttH125") for key in mc:
def legend(hists, pos="top-right", **kwargs): """ ***Mandatory arguments: hists - list of TH1* type objects The title of the histogram(TH1.GetTitle()) is used as the name in the legend. The legend will be in the order of the histograms. ***Optional arguments: names - names of the processes displayed at the legend instead of the histogram titles. Order 1. data 2. MC samples in the reverse order to appear in the legend pos - a string specifying the desired position on the canvas width, height - the size of the legend in relative coordinates styles - an optional list of characters form the set of {p,f} specifying whether to draw a filled legend marker (MC) or a point (data) If the length of style characters does not equal the length of the histogram list, the last character is duplicated to have styles for all histograms. For example: hists=[hist_data, hist_mc1, hist_mc2, hist_mc3] styles=["p","f"] becomes styles["p", "f", "f", "f"] and the data histogram will be drawn as "p", the mc histogram as "f" returns - instance of TLegend. You must keep a reference to the instance to avoid losing the legend, e.g. you must do ... h = TH1F(...) leg = legend([h], ...) <<< note the leg=.. ... Instead of ... legend([h], ...) """ # Names of the processes as displayed on a legend names = kwargs.get("names") # The relative size of the text in the legend text_size = kwargs.get("text_size", 0.03) # If you have long names you should make the width larger width = kwargs.get("width", 0.21) # The height is determined automatically with some sensible settings from # the number of legend items height = kwargs.get("height", 0.03 * len(hists)) # The style characters for drawing the legend styles = kwargs.get("styles", ["p", "f"]) # Fill the style character array to correct length if len(styles) < len(hists): styles += [styles[-1]] * (len(hists) - len(styles)) # Check that each histo has a style character if len(styles) != len(hists): raise ValueError("styles must have the same number of objects as hists") # We'll be popping form back, so need to reverse styles.reverse() # Some 'reasonable' hard-coded positions # [bottom_left_x, bottom_left_y, top_right_x, top_right_y] # FIXME: fine-tune and make your own if pos == "top-right": leg_coords = [-1, -1, 0.93, 0.91] if pos == "top-left": leg_coords = [-1, -1, 0.45, 0.91] # Calculate the bottom-left coordinate from top right using the width and height if leg_coords[0] == -1: leg_coords[0] = leg_coords[2] - width if leg_coords[1] == -1: leg_coords[1] = leg_coords[3] - height # Expand the array using the wildcard leg = ROOT.TLegend(*leg_coords) if "names" in kwargs: rnames = names[::-1] data = rnames.pop() rnames.insert(0, data) from odict import OrderedDict as dict name_hist_pairs = dict(zip(rnames, hists)) for name in name_hist_pairs: leg_style = styles.pop() leg.AddEntry(name_hist_pairs[name], name, leg_style) else: for hist in hists: leg_style = styles.pop() leg.AddEntry(hist, hist.GetTitle(), leg_style) leg.Draw() leg.SetFillColor(ROOT.kWhite) leg.SetLineColor(ROOT.kWhite) leg.SetTextSize(text_size) # Need to return the legend to keep a reference return leg
import ROOT from odict import OrderedDict as dict import string import logging #Here the latter items will become topmost in stacks merge_cmds = dict() merge_cmds["data"] = ["SingleMu"] merge_cmds["diboson"] = ["WW", "WZ", "ZZ"] merge_cmds["W(#rightarrow l #nu) + jets"] = ["W1Jets_exclusive", "W2Jets_exclusive", "W3Jets_exclusive", "W4Jets_exclusive"] merge_cmds["DY-jets"] = ["DYJets"] merge_cmds["t#bar{t} (#rightarrow lq, ll)"] = ["TTJets_FullLept", "TTJets_SemiLept"] #merge_cmds["t#bar{t}"] = ["TTJets_MassiveBinDECAY"] merge_cmds["tW-channel"] = ["T_tW", "Tbar_tW"] merge_cmds["s-channel"] = ["T_s", "Tbar_s"] merge_cmds["t-channel"] = ["T_t", "Tbar_t"] def lumi_textbox(lumi, pos="top-left"): """ This method creates and draws the "CMS Preliminary" luminosity box, displaying the lumi in 1/fb and the COM energy. **Mandatory arguments: lumi - the integrated luminosity in 1/pb **Optional arguments: pos - a string specifying the position of the lumi box. **returns: A TPaveText instance with the lumi information """
if mode == "SL": cut_flow_base = "cut_flow_smu" elif mode == "DL": cut_flow_base = "cut_flow_di_mu" elif args.lep == "ele": if mode == "SL": cut_flow_base = "cut_flow_sele" elif mode == "DL": cut_flow_base = "cut_flow_di_ele" else: cut_flow_base = "cut_flow" print "reading cut flow from histograms: " + cut_flow_base from odict import OrderedDict as dict processes = dict() processes["ttH125"] = f.Get("ttH125/" + cut_flow_base + "_ttH125") processes["ttjj"] = f.Get("ttjj/" + cut_flow_base + "_ttjj") processes["ttb"] = f.Get("ttb/" + cut_flow_base + "_ttb") processes["ttbb"] = f.Get("ttbb/" + cut_flow_base + "_ttbb") processes["TTV"] = f.Get("TTV/" + cut_flow_base + "_TTV") processes["SingleT"] = f.Get("SingleT/" + cut_flow_base + "_SingleT") processes["EWK"] = f.Get("EWK/" + cut_flow_base + "_EWK") processes["DiBoson"] = f.Get("DiBoson/" + cut_flow_base + "_DiBoson") #evt_weight = {} #for process in processes: # evt_weight[process] = (f.Get(process + "/weights_" + process) ).GetBinContent(1) evt_weight = get_evt_weight(f,processes)