diff --git a/tools/get_normalization_factors.py b/tools/get_normalization_factors.py index ee2bc9d..025d403 100644 --- a/tools/get_normalization_factors.py +++ b/tools/get_normalization_factors.py @@ -31,24 +31,29 @@ with open(opts.samplefile, "rb") as f: reader = csv.reader(f) for row in reader: - sample_dict[row[0]] = row[1] + if len(row) == 0: continue + sample_dict[row[0]] = row[0] ############################################################ +line_template = "{sample},{branch},{fiducial},{inclusive}\n" + # print(files) if not os.path.exists(opts.output): os.makedirs(opts.output) -rffile = opts.output+"/ratefactors.csv" +rffile = opts.output+"/ratefactors_new.csv" fobj_out = open(rffile, "w") -fobj_out.write("name,weight,factor" + "\n") +fobj_out.write("sample,variation,fiducial_xs_norm,inclusive_xs_norm" + "\n") file_ = ROOT.TChain("MVATree", "MVATree") branches = [ "Weight_scale_variation_muR_1p0_muF_1p0", "Weight_scale_variation_muR_2p0_muF_1p0", + "Weight_scale_variation_muR_2p0_muF_2p0", "Weight_scale_variation_muR_0p5_muF_1p0", + "Weight_scale_variation_muR_0p5_muF_0p5", "Weight_scale_variation_muR_1p0_muF_2p0", "Weight_scale_variation_muR_1p0_muF_0p5", "Weight_LHA_306000_nominal", @@ -58,12 +63,29 @@ "Weight_LHA_320900_up", "Weight_LHA_320900_down" ] +branches += ["Weight_pdf_variation_306{:03}".format(i) for i in range(0,103)] +branches += ["Weight_pdf_variation_32{:04}".format(i) for i in range(900,1001)] +branches += [ + "GenWeight_fsr_Def_down", + "GenWeight_fsr_Def_up", + "GenWeight_fsr_Def_nom", + "GenWeight_isr_Def_down", + "GenWeight_isr_Def_up", + "GenWeight_isr_Def_nom", + ] for i in range(len(samples)): sample = samples[i].replace(directory, "").replace("/","") print("\n"*4+"="*50) print (sample) print("\n") + if not sample in sample_dict: continue + # define fiducial selection: + selection = "1." + if sample.startswith("TTTo"): + selection = "1.*(GenEvt_I_TTPlusBB==0)" + if sample.startswith("TTbb"): + selection = "1.*(GenEvt_I_TTPlusBB>=1)" #if not os.path.exists(opts.output+"/"+sample): # os.makedirs(opts.output+"/"+sample) @@ -86,34 +108,60 @@ if not branch_name in file_branches: print("branch {} not in file - skipping.".format(branch_name)) continue + + + ### FIDUCIAL + #canvas=ROOT.TCanvas() + test_var = ROOT.TH1D(sample+branch_name, sample+branch_name, 1, -20, 20) + test_nom = ROOT.TH1D(sample+branch_name+"nom", sample+branch_name+"nom", 1, -20, 20) + file_.Draw( + "1."+">>"+sample+branch_name, + selection+"*Weight_GEN_nom*"+branch_name, + "goff", + ) + file_.Draw( + "1."+">>"+sample+branch_name+"nom", + selection+"*Weight_GEN_nom", + "goff", + ) + try: + if test_nom.Integral() > 0.0 and test_var.Integral() > 0.0: + fiducial_factor = test_nom.Integral() / test_var.Integral() + else: + fiducial_factor = 1.0 + except AttributeError: + fiducial_factor = 1.0 + + ### INCLUSIVE #canvas=ROOT.TCanvas() + test_var = ROOT.TH1D(sample+branch_name+"incl", sample+branch_name, 1, -20, 20) + test_nom = ROOT.TH1D(sample+branch_name+"incl"+"nom", sample+branch_name+"nom", 1, -20, 20) file_.Draw( - "1." + ">>" + sample + branch_name + "(1,0,2)", - "Weight_XS*Weight_GEN_nom*" + branch_name + "*(" + branch_name + ">0.)" + "*(" + branch_name + "<2.)", + "1."+">>"+sample+branch_name+"incl", + "Weight_GEN_nom*"+branch_name, "goff", ) file_.Draw( - "1." + ">>" + sample + branch_name + "nom" + "(1,0,2)", - "Weight_XS*Weight_GEN_nom" + "*(" + branch_name + ">0.)" + "*(" + branch_name + "<2.)", + "1."+">>"+sample+branch_name+"incl"+"nom", + "Weight_GEN_nom", "goff", ) - test_var = ROOT.gDirectory.Get(sample + branch_name) - test_nom = ROOT.gDirectory.Get(sample + branch_name + "nom") try: - if test_nom.Integral() > 0.0 and test_var.Integral(): - factor = test_nom.Integral() / test_var.Integral() + if test_nom.Integral() > 0.0 and test_var.Integral() > 0.0: + inclusive_factor = test_nom.Integral() / test_var.Integral() # print(factor) else: - factor = 1.0 + inclusive_factor = 1.0 except AttributeError: - factor = 1.0 + inclusive_factor = 1.0 - line = '"' + str(sample_dict[sample]) + '"' + "," + str(branch_name) + "," + str(factor) + "\n" + line = line_template.format(**{ + "sample": sample_dict[sample], + "branch": branch_name, + "fiducial": str(fiducial_factor), + "inclusive": str(inclusive_factor)}) print("\t\t"+line) fobj_out.write(line) - # print(1./test.GetMean()) - #canvas.Print(opts.output+"/"+sample+"/"+branch_name+".pdf") - #del canvas file_.Reset() fobj_out.close diff --git a/tools/get_rf_fractions.py b/tools/get_rf_fractions.py new file mode 100644 index 0000000..ebfef8d --- /dev/null +++ b/tools/get_rf_fractions.py @@ -0,0 +1,164 @@ +# script to calculate rate factors for e.g. ME weights to ensure that there is only a shape change and not a normalization/yield change +import ROOT +import glob +import os +import csv +import optparse + +parser = optparse.OptionParser() +parser.add_option("-i",dest="input", + help = "directory with inclusive ntuples. In this directory should be one subdirectory per sample") +parser.add_option("-s",dest="samplefile", + help = "samplefile from runscripts repository") +parser.add_option("-o",dest="output", + help = "output directory") +(opts, args) = parser.parse_args() + +directory = os.path.realpath(opts.input) +print("calculating ratefactors for ntuples in directory: {}".format(directory)) + +# globbing samplies +samples = glob.glob(directory + "/*") +print("samples:") +for s in samples: print("\t"+s) + +files = [] +for i in range(len(samples)): + files.append(glob.glob(samples[i] + "/*nominal*.root")) + +############################################################ +sample_dict = {} +with open(opts.samplefile, "rb") as f: + reader = csv.reader(f) + for row in reader: + if len(row) == 0: continue + sample_dict[row[0]] = row[0] + +############################################################ + +# print(files) +if not os.path.exists(opts.output): + os.makedirs(opts.output) + +rffile = opts.output+"/ratefactor_ratios.csv" +fobj_out = open(rffile, "w") +fobj_out.write("sample,variation,ttB_frac,ttOther_frac,ttC_frac,ttLF_frac" + "\n") + +file_ = ROOT.TChain("MVATree", "MVATree") + +branches = [ + "nominal", + "Weight_scale_variation_muR_1p0_muF_1p0", + "Weight_scale_variation_muR_2p0_muF_1p0", + "Weight_scale_variation_muR_2p0_muF_2p0", + "Weight_scale_variation_muR_0p5_muF_1p0", + "Weight_scale_variation_muR_0p5_muF_0p5", + "Weight_scale_variation_muR_1p0_muF_2p0", + "Weight_scale_variation_muR_1p0_muF_0p5", + "Weight_LHA_306000_nominal", + "Weight_LHA_306000_up", + "Weight_LHA_306000_down", + "Weight_LHA_320900_nominal", + "Weight_LHA_320900_up", + "Weight_LHA_320900_down" +] +branches += ["Weight_pdf_variation_306{:03}".format(i) for i in range(0,103)] +branches += ["Weight_pdf_variation_32{:04}".format(i) for i in range(900,1001)] +branches += [ + "GenWeight_fsr_Def_down", + "GenWeight_fsr_Def_up", + "GenWeight_fsr_Def_nom", + "GenWeight_isr_Def_down", + "GenWeight_isr_Def_up", + "GenWeight_isr_Def_nom", + ] + +for i in range(len(samples)): + sample = samples[i].replace(directory, "").replace("/","") + if not sample in sample_dict: + print("sample {} not in sample list".format(sample)) + continue + print("\n"*4+"="*50) + print (sample) + print("\n") + + # define fiducial selection: + selection_ttbb = "1.*(GenEvt_I_TTPlusBB>=1)" + selection_ttother = "1.*(GenEvt_I_TTPlusBB==0)" + selection_ttlf = "1.*(GenEvt_I_TTPlusBB==0)*(GenEvt_I_TTPlusCC==0)" + selection_ttcc = "1.*(GenEvt_I_TTPlusBB==0)*(GenEvt_I_TTPlusCC==1)" + #if not os.path.exists(opts.output+"/"+sample): + # os.makedirs(opts.output+"/"+sample) + + for k in range(len(files[i])): + file_.Add(files[i][k]) + + print("number of entries: {}".format(file_.GetEntries())) + if file_.GetEntries() < 50000: + print ("not really enough events in tree") + #file_.Reset() + #continue + + file_branches = [b.GetName() for b in list(file_.GetListOfBranches())] + # file_.Draw("Weight_CSV") + # file_.GetListOfBranches().Print() + for branch in branches: + branch_name = branch + branch_weight = "1." if branch_name == "nominal" else branch_name + print ("\tchecking branch {}".format(branch_name)) + + if not branch_name in file_branches and not branch_name == "nominal": + print("branch {} not in file - skipping.".format(branch_name)) + continue + #canvas=ROOT.TCanvas() + frac_ttbb = -1. + frac_ttother = -1. + frac_ttcc = -1. + frac_ttlf = -1. + + if sample.startswith("TT") or sample.startswith("TTbb"): + test_ttbb = ROOT.TH1D(sample+branch_name+"ttbb",sample+branch_name+"ttbb",1,-20,20) + test_ttother = ROOT.TH1D(sample+branch_name+"ttother",sample+branch_name+"ttother",1,-20,20) + test_ttcc = ROOT.TH1D(sample+branch_name+"ttcc",sample+branch_name+"ttcc",1,-20,20) + test_ttlf = ROOT.TH1D(sample+branch_name+"ttlf",sample+branch_name+"ttlf",1,-20,20) + file_.Draw( + "1."+">>"+sample+branch_name+"ttbb", + selection_ttbb+"*Weight_GEN_nom*"+branch_weight , + "goff", + ) + file_.Draw( + "1."+">>"+sample+branch_name+"ttother", + selection_ttother+"*Weight_GEN_nom*"+branch_weight, + "goff", + ) + file_.Draw( + "1."+">>"+sample+branch_name+"ttcc", + selection_ttcc+"*Weight_GEN_nom*"+branch_weight, + "goff", + ) + file_.Draw( + "1."+">>"+sample+branch_name+"ttlf", + selection_ttlf+"*Weight_GEN_nom*"+branch_weight, + "goff", + ) + try: + if test_ttbb.Integral() > 0.0 and test_ttother.Integral() > 0.0: + frac_ttbb = test_ttbb.Integral() / (test_ttbb.Integral()+test_ttother.Integral()) + frac_ttother = test_ttother.Integral() / (test_ttbb.Integral()+test_ttother.Integral()) + frac_ttcc = test_ttcc.Integral() / (test_ttbb.Integral()+test_ttother.Integral()) + frac_ttlf = test_ttlf.Integral() / (test_ttbb.Integral()+test_ttother.Integral()) + # print(factor) + except: + pass + + line = "{},{},{:.6f},{:.6f},{:.6f},{:.6f}\n".format( + sample_dict[sample], branch_name, frac_ttbb, frac_ttother, frac_ttcc, frac_ttlf) + print("\t\t"+line) + fobj_out.write(line) + # print(1./test.GetMean()) + #canvas.Print(opts.output+"/"+sample+"/"+branch_name+".pdf") + #del canvas + file_.Reset() + +fobj_out.close + diff --git a/tools/get_summary_table.py b/tools/get_summary_table.py new file mode 100644 index 0000000..2b7bd2a --- /dev/null +++ b/tools/get_summary_table.py @@ -0,0 +1,219 @@ +# script to calculate rate factors for e.g. ME weights to ensure that there is only a shape change and not a normalization/yield change +import ROOT +import glob +import os +import csv +import optparse + +parser = optparse.OptionParser() +parser.add_option("-i",dest="input", + help = "input fraction file") +parser.add_option("-r",dest="ratefactorfile", + help = "input ratefactor file") +parser.add_option("-y",dest="year", + help = "year of data taking period") +(opts, args) = parser.parse_args() + +directory = os.path.realpath(opts.input) +print("calculating ratefactors for ntuples in directory: {}".format(directory)) + +with open(opts.input, "r") as f: + fraction_file = f.readlines() +sample_dict = {} +for line in fraction_file: + entry = line.replace("\n","").replace("\"","").split(",") + if not entry[0] in sample_dict: + sample_dict[entry[0]] = {} + sample_dict[entry[0]][entry[1]] = [entry[2], entry[3], entry[4], entry[5]] + +with open(opts.ratefactorfile, "r") as f: + rf_file = f.readlines() + +if opts.year == "2017": + translations_5fs = { + "TTbb_Powheg_Openloops_DL": "TTTo2L2Nu_TuneCP5_PSweights_13TeV-powheg-pythia8", + "TTbb_Powheg_Openloops_new_pmx": "TTToSemiLeptonic_TuneCP5_PSweights_13TeV-powheg-pythia8", + "TTbb_4f_TTToHadronic_TuneCP5-Powheg-Openloops-Pythia8": "TTToHadronic_TuneCP5_PSweights_13TeV-powheg-pythia8_new_pmx", + } +elif opts.year == "2018": + translations_5fs = { + "TTbb_4f_TTTo2l2nu_TuneCP5-Powheg-Openloops-Pythia8": "TTTo2L2Nu_TuneCP5_13TeV-powheg-pythia8", + "TTbb_4f_TTToSemiLeptonic_TuneCP5-Powheg-Openloops-Pythia8": "TTToSemiLeptonic_TuneCP5_13TeV-powheg-pythia8", + "TTbb_4f_TTToHadronic_TuneCP5-Powheg-Openloops-Pythia8": "TTToHadronic_TuneCP5_13TeV-powheg-pythia8", + } +elif opts.year == "2016": + translations_5fs = { + "TTbb_4f_TTTo2l2nu_TuneCP5-Powheg-Openloops-Pythia8": "TTTo2L2Nu_TuneCP5_PSweights_13TeV-powheg-pythia8", + "TTbb_4f_TTToSemiLeptonic_TuneCP5-Powheg-Openloops-Pythia8": "TTToSemiLeptonic_TuneCP5_PSweights_13TeV-powheg-pythia8", + "TTbb_4f_TTToHadronic_TuneCP5-Powheg-Openloops-Pythia8": "TTToHadronic_TuneCP5_PSweights_13TeV-powheg-pythia8", + } +elif opts.year == "comb": + translations_5fs = { + "TTbbDL": "TTDL", + "TTbbFH": "TTFH", + "TTbbSL": "TTSL", + } + +else: + sys.exit("need to specify a year for sample matching") + +branches = [ + "nominal", + "Weight_scale_variation_muR_1p0_muF_1p0", + "Weight_scale_variation_muR_2p0_muF_1p0", + "Weight_scale_variation_muR_2p0_muF_2p0", + "Weight_scale_variation_muR_0p5_muF_1p0", + "Weight_scale_variation_muR_0p5_muF_0p5", + "Weight_scale_variation_muR_1p0_muF_2p0", + "Weight_scale_variation_muR_1p0_muF_0p5", + #"Weight_LHA_306000_nominal", + #"Weight_LHA_306000_up", + #"Weight_LHA_306000_down", + #"Weight_LHA_320900_nominal", + #"Weight_LHA_320900_up", + #"Weight_LHA_320900_down" +] +#branches += ["Weight_pdf_variation_306{:03}".format(i) for i in range(0,103)] +#branches += ["Weight_pdf_variation_32{:04}".format(i) for i in range(900,1001)] +branches += [ + "GenWeight_fsr_Def_down", + "GenWeight_fsr_Def_up", + "GenWeight_fsr_Def_nom", + "GenWeight_isr_Def_down", + "GenWeight_isr_Def_up", + "GenWeight_isr_Def_nom", + ] + + +text = """ +================================================ +sample: {samplename} +variation: {varname} +fiducial XS change: {rf_fiducial} +inclusive XS change: {rf_inclusive} +---------------- +fraction of tt+B: {ttbb_frac} +fraction of tt+B in 5FS sample: {ttbb_frac_5fs} +fraction of tt+B nominal: {nom_frac} +---------------- +nom/varied tt+B fractions: {ratio_ttB_nom_vs_varied} +nom/varied tt+B fractions 5FS: {ratio_ttB_nom_vs_varied_5FS} +---------------- +SL weight: {sl_weight} +================================================ +""" +new_file = [] +for line in rf_file: + line = line.replace("\n","").replace("\"","") + entries = line.split(",") + if entries[0] == "sample": + entries.append("fraction_ttB") + entries.append("fraction_ttB_5FS") + entries.append("fraction_ttC") + entries.append("fraction_ttC_5FS") + entries.append("fraction_ttC_nom") + entries.append("fraction_ttC_5FS_nom") + entries.append("fraction_ttLF") + entries.append("fraction_ttLF_5FS") + entries.append("fraction_ttLF_nom") + entries.append("fraction_ttLF_5FS_nom") + entries.append("ratio_ttB_nominal_vs_varied") + entries.append("ratio_ttB_nominal_vs_varied_5FS") + entries.append("ratio_ttB_5FS_vs_this") + entries.append("ratio_ttB_5FS_vs_this_nom") + entries.append("ratio_5FS_vs_this__nominal_vs_varied") + entries.append("final_weight_sl_analysis") + + else: + ttbb_frac = sample_dict[entries[0]][entries[1]][0] + nom_frac = sample_dict[entries[0]]["nominal"][0] + ttcc_frac = sample_dict[entries[0]][entries[1]][2] + nom_cc_frac = sample_dict[entries[0]]["nominal"][2] + ttlf_frac = sample_dict[entries[0]][entries[1]][3] + nom_lf_frac = sample_dict[entries[0]]["nominal"][3] + + rf_fiducial = float(entries[2]) + rf_inclusive = float(entries[3]) + + nonapplicable = False + if entries[0].startswith("TTbb") and entries[1] in branches: + samplename_5fs = translations_5fs[entries[0]] + + ttbb_frac_5fs = sample_dict[samplename_5fs][entries[1]][0] + nom_frac_5fs = sample_dict[samplename_5fs]["nominal"][0] + ttcc_frac_5fs = sample_dict[samplename_5fs][entries[1]][2] + nom_cc_frac_5fs = sample_dict[samplename_5fs]["nominal"][2] + ttlf_frac_5fs = sample_dict[samplename_5fs][entries[1]][3] + nom_lf_frac_5fs = sample_dict[samplename_5fs]["nominal"][3] + elif entries[0].startswith("TTTo"): + ttbb_frac_5fs = sample_dict[entries[0]][entries[1]][0] + nom_frac_5fs = sample_dict[entries[0]]["nominal"][0] + ttcc_frac_5fs = sample_dict[entries[0]][entries[1]][2] + nom_cc_frac_5fs = sample_dict[entries[0]]["nominal"][2] + ttlf_frac_5fs = sample_dict[entries[0]][entries[1]][3] + nom_lf_frac_5fs = sample_dict[entries[0]]["nominal"][3] + else: + ttbb_frac_5fs = -1. + nom_frac_5fs = -1. + ttcc_frac_5fs = -1. + nom_cc_frac_5fs = -1. + ttlf_frac_5fs = -1. + nom_lf_frac_5fs = -1. + nonapplicable = True + + ratio_ttB_nom_vs_varied = float(nom_frac)/float(ttbb_frac) + ratio_ttB_nom_vs_varied_5FS = float(nom_frac_5fs)/float(ttbb_frac_5fs) + ratio_ttB_5FS_vs_this = float(ttbb_frac_5fs)/float(ttbb_frac) + ratio_ttB_5FS_vs_this_nom = float(nom_frac_5fs)/float(nom_frac) + ratio_5FS_this_nominal_vs_varied = ratio_ttB_5FS_vs_this_nom/ratio_ttB_5FS_vs_this + + if nonapplicable: + ratio_ttB_nom_vs_varied_5FS = -1. + ratio_ttB_5FS_vs_this = -1. + ratio_ttB_5FS_vs_this_nom = -1. + ratio_5FS_this_nominal_vs_varied = -1. + + entries.append(str(ttbb_frac)) + entries.append(str(ttbb_frac_5fs)) + entries.append(str(ttcc_frac)) + entries.append(str(ttcc_frac_5fs)) + entries.append(str(nom_cc_frac)) + entries.append(str(nom_cc_frac_5fs)) + entries.append(str(ttlf_frac)) + entries.append(str(ttlf_frac_5fs)) + entries.append(str(nom_lf_frac)) + entries.append(str(nom_lf_frac_5fs)) + entries.append(str(ratio_ttB_nom_vs_varied)) + entries.append(str(ratio_ttB_nom_vs_varied_5FS)) + entries.append(str(ratio_ttB_5FS_vs_this)) + entries.append(str(ratio_ttB_5FS_vs_this_nom)) + entries.append(str(ratio_5FS_this_nominal_vs_varied)) + + sl_weight = 1. + if entries[0].startswith("TTbb"): + if entries[1] in branches: + sl_weight = rf_fiducial / ratio_ttB_nom_vs_varied_5FS + else: + sl_weight = rf_fiducial + else: + sl_weight = rf_inclusive + + entries.append(str(sl_weight)) + print(text.format( + **{"samplename": entries[0], "varname": entries[1], + "ttbb_frac": ttbb_frac, + "nom_frac": nom_frac, + "ttbb_frac_5fs": ttbb_frac_5fs, + "ratio_ttB_nom_vs_varied": ratio_ttB_nom_vs_varied, + "ratio_ttB_nom_vs_varied_5FS": ratio_ttB_nom_vs_varied_5FS, + "rf_fiducial": rf_fiducial, + "rf_inclusive": rf_inclusive, + "sl_weight": sl_weight})) + + + new_file.append(",".join(entries)) + +outf = opts.ratefactorfile.replace(".csv","_with_correction.csv") +with open(outf, "w") as f: + f.write("\n".join(new_file)) +print("wrote new file to {}".format(outf)) diff --git a/tools/get_ttbb_fractions.py b/tools/get_ttbb_fractions.py new file mode 100644 index 0000000..6f23b58 --- /dev/null +++ b/tools/get_ttbb_fractions.py @@ -0,0 +1,151 @@ +# script to calculate rate factors for e.g. ME weights to ensure that there is only a shape change and not a normalization/yield change +import ROOT +import glob +import os +import csv +import optparse + +parser = optparse.OptionParser() +parser.add_option("-i",dest="input", + help = "input fraction file") +parser.add_option("-r",dest="ratefactorfile", + help = "input ratefactor file") +parser.add_option("-y",dest="year", + help = "year of data taking period") +(opts, args) = parser.parse_args() + +directory = os.path.realpath(opts.input) +print("calculating ratefactors for ntuples in directory: {}".format(directory)) + +with open(opts.input, "r") as f: + fraction_file = f.readlines() +sample_dict = {} +for line in fraction_file: + entry = line.replace("\n","").replace("\"","").split(",") + if not entry[0] in sample_dict: + sample_dict[entry[0]] = {} + sample_dict[entry[0]][entry[1]] = [entry[2], entry[3], entry[4], entry[5]] + +with open(opts.ratefactorfile, "r") as f: + rf_file = f.readlines() + +if opts.year == "2017": + translations_5fs = { + "TTbb_Powheg_Openloops_DL": "TTTo2L2Nu_TuneCP5_PSweights_13TeV-powheg-pythia8", + "TTbb_Powheg_Openloops_new_pmx": "TTToSemiLeptonic_TuneCP5_PSweights_13TeV-powheg-pythia8", + "TTbb_4f_TTToHadronic_TuneCP5-Powheg-Openloops-Pythia8": "TTToHadronic_TuneCP5_PSweights_13TeV-powheg-pythia8_new_pmx", + } +elif opts.year == "2018": + translations_5fs = { + "TTbb_4f_TTTo2l2nu_TuneCP5-Powheg-Openloops-Pythia8": "TTTo2L2Nu_TuneCP5_13TeV-powheg-pythia8", + "TTbb_4f_TTToSemiLeptonic_TuneCP5-Powheg-Openloops-Pythia8": "TTToSemiLeptonic_TuneCP5_13TeV-powheg-pythia8", + "TTbb_4f_TTToHadronic_TuneCP5-Powheg-Openloops-Pythia8": "TTToHadronic_TuneCP5_13TeV-powheg-pythia8", + } +elif opts.year == "2016": + translations_5fs = { + "TTbb_4f_TTTo2l2nu_TuneCP5-Powheg-Openloops-Pythia8": "TTTo2L2Nu_TuneCP5_PSweights_13TeV-powheg-pythia8", + "TTbb_4f_TTToSemiLeptonic_TuneCP5-Powheg-Openloops-Pythia8": "TTToSemiLeptonic_TuneCP5_PSweights_13TeV-powheg-pythia8", + "TTbb_4f_TTToHadronic_TuneCP5-Powheg-Openloops-Pythia8": "TTToHadronic_TuneCP5_PSweights_13TeV-powheg-pythia8", + } +elif opts.year == "comb": + translations_5fs = { + "TTbbDL": "TTDL", + "TTbbFH": "TTFH", + "TTbbSL": "TTSL", + } +else: + sys.exit("need to specify a year for sample matching") + +branches = [ + "nominal", + #"Weight_scale_variation_muR_1p0_muF_1p0", + "Weight_scale_variation_muR_2p0_muF_1p0", + #"Weight_scale_variation_muR_2p0_muF_2p0", + "Weight_scale_variation_muR_0p5_muF_1p0", + #"Weight_scale_variation_muR_0p5_muF_0p5", + "Weight_scale_variation_muR_1p0_muF_2p0", + "Weight_scale_variation_muR_1p0_muF_0p5", + #"Weight_LHA_306000_nominal", + #"Weight_LHA_306000_up", + #"Weight_LHA_306000_down", + #"Weight_LHA_320900_nominal", + #"Weight_LHA_320900_up", + #"Weight_LHA_320900_down" +] +#branches += ["Weight_pdf_variation_306{:03}".format(i) for i in range(0,103)] +#branches += ["Weight_pdf_variation_32{:04}".format(i) for i in range(900,1001)] +branches += [ + "GenWeight_fsr_Def_down", + "GenWeight_fsr_Def_up", + #"GenWeight_fsr_Def_nom", + "GenWeight_isr_Def_down", + "GenWeight_isr_Def_up", + #"GenWeight_isr_Def_nom", + ] + + +new_file = [] +for line in rf_file: + line = line.replace("\n","").replace("\"","") + entries = line.split(",") + if entries[0] == "sample": + entries.append("fraction_ttB_4FS") + entries.append("fraction_ttB_5FS") + entries.append("fraction_ttB_4FS_nom") + entries.append("fraction_ttB_5FS_nom") + entries.append("fraction_ttC_4FS") + entries.append("fraction_ttC_5FS") + entries.append("fraction_ttC_4FS_nom") + entries.append("fraction_ttC_5FS_nom") + entries.append("fraction_ttLF_4FS") + entries.append("fraction_ttLF_5FS") + entries.append("fraction_ttLF_4FS_nom") + entries.append("fraction_ttLF_5FS_nom") + entries.append("ratio_nominal_vs_varied_4FS") + entries.append("ratio_nominal_vs_varied_5FS") + + else: + ttbb_frac = sample_dict[entries[0]][entries[1]][0] + nom_frac = sample_dict[entries[0]]["nominal"][0] + ttcc_frac = sample_dict[entries[0]][entries[1]][2] + nom_cc_frac = sample_dict[entries[0]]["nominal"][2] + ttlf_frac = sample_dict[entries[0]][entries[1]][3] + nom_lf_frac = sample_dict[entries[0]]["nominal"][3] + + rf_fiducial = float(entries[2]) + rf_inclusive = float(entries[3]) + + nonapplicable = False + if entries[0].startswith("TTbb") and entries[1] in branches: + samplename_5fs = translations_5fs[entries[0]] + + ttbb_frac_5fs = sample_dict[samplename_5fs][entries[1]][0] + nom_frac_5fs = sample_dict[samplename_5fs]["nominal"][0] + ttcc_frac_5fs = sample_dict[samplename_5fs][entries[1]][2] + nom_cc_frac_5fs = sample_dict[samplename_5fs]["nominal"][2] + ttlf_frac_5fs = sample_dict[samplename_5fs][entries[1]][3] + nom_lf_frac_5fs = sample_dict[samplename_5fs]["nominal"][3] + else: + continue + + entries.append(str(ttbb_frac)) + entries.append(str(ttbb_frac_5fs)) + entries.append(str(nom_frac)) + entries.append(str(nom_frac_5fs)) + entries.append(str(ttcc_frac)) + entries.append(str(ttcc_frac_5fs)) + entries.append(str(nom_cc_frac)) + entries.append(str(nom_cc_frac_5fs)) + entries.append(str(ttlf_frac)) + entries.append(str(ttlf_frac_5fs)) + entries.append(str(nom_lf_frac)) + entries.append(str(nom_lf_frac_5fs)) + entries.append(str(float(nom_frac)/float(ttbb_frac))) + entries.append(str(float(nom_frac_5fs)/float(ttbb_frac_5fs))) + + new_file.append(",".join(entries)) + +outf = opts.ratefactorfile.replace(".csv","ttbb.csv") +with open(outf, "w") as f: + f.write("\n".join(new_file)) +print("wrote new file to {}".format(outf))