ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
send_sig_wspaces.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 import errno
8 import glob
9 import numpy
10 import subprocess
11 
12 def ensureDir(path):
13  try:
14  os.makedirs(path)
15  except OSError as e:
16  if e.errno == errno.EEXIST and os.path.isdir(path):
17  pass
18  else:
19  raise
20 
21 def fullPath(path):
22  return os.path.realpath(os.path.abspath(os.path.expanduser(path)))
23 
24 def SendSignalWorkspaces(input_dir, output_dir, num_jobs, injection_strength, injection_model):
25  input_dir = fullPath(input_dir)
26  output_dir = fullPath(output_dir)
27 
28  run_dir = os.path.join(output_dir, "run")
29  ensureDir(run_dir)
30 
31  cmssw_dir = os.path.join(os.environ["CMSSW_BASE"],"src")
32 
33  if num_jobs < 1:
34  num_jobs = 1
35 
36  input_files = [ fullPath(f) for f in glob.glob(os.path.join(input_dir, "*SMS*")) ]
37  num_files = len(input_files)
38  input_files = numpy.array_split(numpy.array(input_files), num_jobs)
39 
40  num_submitted = 0
41 
42  for sublist in input_files:
43  if len(sublist) == 0:
44  continue
45  job_files = sublist.tolist()
46  run_path = os.path.join(run_dir,"wspace_sig_{}.sh".format(num_submitted))
47  with open(run_path, "w") as run_file:
48  os.fchmod(run_file.fileno(), 0755)
49  run_file.write("#! /bin/bash\n\n")
50 
51  run_file.write("DIRECTORY=`pwd`\n")
52  run_file.write("cd {}\n".format(cmssw_dir))
53  run_file.write(". /net/cms2/cms2r0/babymaker/cmsset_default.sh\n")
54  run_file.write("eval `scramv1 runtime -sh`\n")
55  run_file.write("cd $DIRECTORY\n\n")
56 
57  for ifile in range(len(job_files)):
58  f = job_files[ifile]
59  cmd = "./run/wspace_sig.exe -f {} -o {} --sig_strength {} -u all -l 35.9 -p".format(
60  f, output_dir, (injection_strength if injection_strength >= 0. else 0.))
61  if injection_strength >= 0.:
62  cmd += " --unblind none"
63  if injection_model != "":
64  cmd += " --inject "+injection_model
65  run_file.write("echo Starting to process file {} of {}\n".format(ifile+1, len(job_files)))
66  run_file.write(cmd+"\n\n")
67 
68  subprocess.check_call(["JobSubmit.csh",run_path])
69  num_submitted += 1
70 
71  print("\nSubmitted {} files in {} jobs. Output will be sent to {}.\n".format(
72  num_files, num_submitted, output_dir))
73 
74 if __name__ == "__main__":
75  parser = argparse.ArgumentParser(description = "Submits batch jobs to produce workspaces for each signal mass point",
76  formatter_class = argparse.ArgumentDefaultsHelpFormatter)
77  parser.add_argument("output_dir", nargs="?", default = "/net/cms2/cms2r0/babymaker/wspaces/2016_08_10/T1tttt",
78  help="Directory in which to store output workspaces.")
79  parser.add_argument("input_dir", nargs="?", default = "/net/cms29/cms29r0/babymaker/babies/2016_08_10/T1tttt/skim_abcd",
80  help="Directory containing input ntuples.")
81  parser.add_argument("--num_jobs","-n", type=int, default=50,
82  help="Maximum number of jobs into which to split the mass points")
83  parser.add_argument("--injection_strength", type=float, default=-1.,
84  help="Amount of signal to inject. Negative values turn off signal injection. Note that signal injection replaces the data with MC yields, even at injection strength of 0.")
85  parser.add_argument("--injection_model", default="",
86  help="Path to signal ntuple to use for signal injection. If unspecified, uses the same signal model as used to construct the likelihood function")
87  args = parser.parse_args()
88 
89  SendSignalWorkspaces(args.input_dir, args.output_dir, args.num_jobs, args.injection_strength, args.injection_model)
def SendSignalWorkspaces(input_dir, output_dir, num_jobs, injection_strength, injection_model)