48 global tclparams, pythiaparams
56 param_pattern = re.compile(
"^set\s+(.*?)\s+(.*)")
58 line = line.rstrip(
'\n')
59 match = param_pattern.match(line)
61 tclparams[match.group(1)] = str(match.group(2))
63 print(
"Customization TCL Parameters are:")
69 param_pattern = re.compile(
"^\!\s*(.*?)\s*=\s*(.*)")
71 line = line.rstrip(
'\n')
73 if line.find(
'END HEADER') != -1:
76 match = param_pattern.match(line)
78 pythiaparams[match.group(1)] = str(match.group(2))
80 print(
"Customization Pythia8 Parameters are:")
86 tclfile =
open(filename,
'w')
88 for param
in args.params:
89 value = args.params[param]
90 if (param
not in tclparams.keys())
and (param
not in pythiaparams.keys()):
91 print(
"WARNING: you tried to set %s, which is not an available parameter!" % (param))
93 tclparams[param] = str(value)
95 for param
in tclparams:
96 line =
"set %s %s\n" % (param, tclparams[param])
103 for param
in args.params:
104 value = args.params[param]
105 if (param
not in pythiaparams.keys())
and (param
not in tclparams.keys()):
106 print(
"WARNING: you tried to set %s, which is not an available parameter!" % (param))
108 pythiaparams[param] = str(value)
111 with
open(args.commands,
"rt")
as input_template:
112 with
open(output_filename,
"wt")
as output_template:
113 for line
in input_template:
114 for param
in pythiaparams:
115 value = str(pythiaparams[param])
116 line = line.replace(param, value)
118 output_template.write(line)
122 with
open(tclfile,
"rt")
as input_template:
123 with
open(
"%s/%s" % (taskdir, tclfile),
"wt")
as output_template:
124 for line
in input_template:
125 line = line.replace(
"customizations.tcl",
"%s/customizations.tcl" % (taskdir))
127 output_template.write(line)
131 parser = argparse.ArgumentParser()
133 parser.add_argument(
"-n",
"--name", type=str,
134 help=
"name for this study set (e.g. bfield)")
135 parser.add_argument(
"-t",
"--template", type=str,
136 help=
"template TCL file for study")
137 parser.add_argument(
"-c",
"--commands", type=str,
138 help=
"template command file for study")
139 parser.add_argument(
"-p",
"--params", type=ast.literal_eval, default={},
140 help=
"environment variables to set for study")
143 parser.add_argument(
"-f",
"--force", default=
False, action=
'store_true',
144 help=
"force-overwrite existing output")
147 args = parser.parse_args()
151 if not os.path.exists(args.name):
153 os.makedirs(args.name)
155 print(
"%s already exists... continuing..." % (args.name))
158 SLURM_ARRAY_TASK_ID=
"0"
161 SLURM_ARRAY_TASK_ID=os.environ[
"SLURM_ARRAY_TASK_ID"]
163 print(
"Please set the SLURM_ARRAY_TASK_ID environment variable to a number (e.g. 0) before running this script.")
167 print(
"Task ID requested: %d" % (int(SLURM_ARRAY_TASK_ID)))
171 value_index = int(SLURM_ARRAY_TASK_ID)
185 if "PARAM_RANDOM_SEED" not in args.params:
186 random_seed =
abs(
hash(args.name)) % (10 ** 8) + value_index
187 pythiaparams[
"PARAM_RANDOM_SEED"] = random_seed
191 taskdir=
"%s/%d" % (args.name, value_index)
192 tclfile =
"%s/customizations.tcl" % (taskdir)
193 cmndfile =
"%s/%s" % (taskdir, args.commands)
195 if os.path.exists(taskdir)
and not args.force:
196 print(
"Skipping this task directory --- it already exists. Cleanup before overwriting!")
199 if not os.path.exists(taskdir):
207 copy_files = [args.template]
208 for a_file
in copy_files:
209 subprocess.call(
"cp %s %s" % (a_file, taskdir), shell=
True)
212 rndm_file =
open(taskdir+
"/random_seed.dat",
"w")
213 rndm_file.write(str(random_seed))
217 subprocess.call(
"DelphesPythia8 {0[taskdir]}/{0[template]} {0[taskdir]}/{0[commands]} {0[taskdir]}/out.root".format({
'taskdir': taskdir,
'template': args.template,
'commands': args.commands}), shell=
True)