ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
update_bkg_systs.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 
5 import argparse
6 import os
7 
8 def fullPath(path):
9  return os.path.realpath(os.path.abspath(os.path.expanduser(path)))
10 
11 def ensureDir(path):
12  try:
13  os.makedirs(path)
14  except OSError:
15  if not os.path.isdir(path):
16  raise
17 
18 def PrintBkgSysts(output_file):
19  print("SYSTEMATIC dilep_lownj", file=output_file)
20  print(" PROCESSES ttbar,other", file=output_file)
21  print(" r2_lowmet_lownj_1b 0.06", file=output_file)
22  print(" r2_lowmet_lownj_2b 0.06", file=output_file)
23  print(" r2_lowmet_lownj_3b 0.06", file=output_file)
24  print(" r2_medmet_lownj_1b 0.06", file=output_file)
25  print(" r2_medmet_lownj_2b 0.06", file=output_file)
26  print(" r2_medmet_lownj_3b 0.06", file=output_file)
27  print(" r2_highmet_lownj_1b 0.06", file=output_file)
28  print(" r2_highmet_lownj_2b 0.06", file=output_file)
29  print(" r2_highmet_lownj_3b 0.06", file=output_file)
30  print("", file=output_file)
31  print("SYSTEMATIC dilep_highnj", file=output_file)
32  print(" PROCESSES ttbar,other", file=output_file)
33  print(" r2_lowmet_highnj_1b 0.16", file=output_file)
34  print(" r2_lowmet_highnj_2b 0.16", file=output_file)
35  print(" r2_lowmet_highnj_3b 0.16", file=output_file)
36  print(" r2_medmet_highnj_1b 0.16", file=output_file)
37  print(" r2_medmet_highnj_2b 0.16", file=output_file)
38  print(" r2_medmet_highnj_3b 0.16", file=output_file)
39  print(" r2_highmet_highnj_1b 0.16", file=output_file)
40  print(" r2_highmet_highnj_2b 0.16", file=output_file)
41  print(" r2_highmet_highnj_3b 0.16", file=output_file)
42  print("", file=output_file)
43  print("SYSTEMATIC fivejet_lowmet", file=output_file)
44  print(" PROCESSES ttbar,other", file=output_file)
45  print(" r2_lowmet_lownj_1b 0.15", file=output_file)
46  print(" r2_lowmet_lownj_2b 0.15", file=output_file)
47  print(" r2_lowmet_lownj_3b 0.15", file=output_file)
48  print(" r2_lowmet_highnj_1b 0.15", file=output_file)
49  print(" r2_lowmet_highnj_2b 0.15", file=output_file)
50  print(" r2_lowmet_highnj_3b 0.15", file=output_file)
51  print("", file=output_file)
52  print("SYSTEMATIC fivejet_highmet", file=output_file)
53  print(" PROCESSES ttbar,other", file=output_file)
54  print(" r2_medmet_lownj_1b 0.37", file=output_file)
55  print(" r2_medmet_lownj_2b 0.37", file=output_file)
56  print(" r2_medmet_lownj_3b 0.37", file=output_file)
57  print(" r2_medmet_highnj_1b 0.37", file=output_file)
58  print(" r2_medmet_highnj_2b 0.37", file=output_file)
59  print(" r2_medmet_highnj_3b 0.37", file=output_file)
60  print(" r2_highmet_lownj_1b 0.37", file=output_file)
61  print(" r2_highmet_lownj_2b 0.37", file=output_file)
62  print(" r2_highmet_lownj_3b 0.37", file=output_file)
63  print(" r2_highmet_highnj_1b 0.37", file=output_file)
64  print(" r2_highmet_highnj_2b 0.37", file=output_file)
65  print(" r2_highmet_highnj_3b 0.37", file=output_file)
66  print("", file=output_file)
67 
68 def UpdateOneBkgSyst(input_path, output_path, overwrite):
69  print(str.join(" -> ",[input_path,output_path]))
70  if os.path.exists(output_path) and not overwrite:
71  raise Exception(str.join(" ", ["Output file",output_path,"already exists"]))
72 
73  with open(input_path, "r") as input_file, open(output_path, "w") as output_file:
74  PrintBkgSysts(output_file)
75  syst_line = ""
76  proc_line = ""
77  printed = False
78  for line in input_file:
79  if "SYSTEMATIC" in line:
80  syst_line = line
81  printed = False
82  elif "PROCESSES" in line:
83  proc_line = line
84  printed = False
85  elif "signal" in proc_line and syst_line != "" and proc_line != "":
86  if not printed:
87  print(syst_line, file=output_file, end="")
88  print(proc_line, file=output_file, end="")
89  printed = True
90  print(line, file=output_file, end="")
91 
92 def UpdateBkgSysts(input_dirs, output_dir, recurse, overwrite):
93  input_dirs = [ fullPath(d) for d in input_dirs ]
94  output_dir = fullPath(output_dir)
95 
96  for input_dir in input_dirs:
97  for root, dirs, files in os.walk(input_dir):
98  if root != input_dir and not recurse:
99  break
100  subdir = os.path.relpath(root, input_dir)
101  output_subdir = fullPath(os.path.join(output_dir, subdir))
102  ensureDir(output_subdir)
103  for base_file in files:
104  if os.path.splitext(base_file)[1] != ".txt":
105  continue
106  input_file = os.path.join(root, base_file)
107  output_file = os.path.join(output_subdir, base_file)
108  UpdateOneBkgSyst(input_file, output_file, overwrite)
109 
110 if __name__ == "__main__":
111  parser = argparse.ArgumentParser(description="Update systematics files in specified directories to use new background systematics.",
112  formatter_class=argparse.ArgumentDefaultsHelpFormatter)
113  parser.add_argument("input_dirs", nargs="+", help="Directories containing systematics files with old background systematics")
114  parser.add_argument("output_dir", help="Directory in which to place updated systematics files")
115  parser.add_argument("-r","--recurse", action="store_true", help="Recurse through subdirectories")
116  parser.add_argument("--overwrite", action="store_true", help="Allow overwriting of existing output files")
117  args = parser.parse_args()
118 
119  UpdateBkgSysts(args.input_dirs, args.output_dir, args.recurse, args.overwrite)
def UpdateBkgSysts(input_dirs, output_dir, recurse, overwrite)
def UpdateOneBkgSyst(input_path, output_path, overwrite)
def PrintBkgSysts(output_file)