File indexing completed on 2024-09-28 07:03:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 import subprocess
0038 import math
0039 import os
0040 import sys
0041 import shutil
0042 import glob
0043 import re
0044 import ast
0045
0046 import argparse
0047
0048 global tclparams, pythiaparams
0049 tclparams = {}
0050 pythiaparams = {}
0051
0052
0053 def LoadParamsTcl(tclfile):
0054 tcl = open(tclfile)
0055
0056 param_pattern = re.compile("^set\s+(.*?)\s+(.*)")
0057 for line in tcl:
0058 line = line.rstrip('\n')
0059 match = param_pattern.match(line)
0060 if match != None:
0061 tclparams[match.group(1)] = str(match.group(2))
0062
0063 print("Customization TCL Parameters are:")
0064 print(tclparams)
0065
0066 def LoadParamsPythia(cmndfile):
0067 cmnd = open(cmndfile)
0068
0069 param_pattern = re.compile("^\!\s*(.*?)\s*=\s*(.*)")
0070 for line in cmnd:
0071 line = line.rstrip('\n')
0072
0073 if line.find('END HEADER') != -1:
0074 break
0075
0076 match = param_pattern.match(line)
0077 if match != None:
0078 pythiaparams[match.group(1)] = str(match.group(2))
0079
0080 print("Customization Pythia8 Parameters are:")
0081 print(pythiaparams)
0082
0083
0084 def WriteTclFile(filename):
0085 global args
0086 tclfile = open(filename, 'w')
0087
0088 for param in args.params:
0089 value = args.params[param]
0090 if (param not in tclparams.keys()) and (param not in pythiaparams.keys()):
0091 print("WARNING: you tried to set %s, which is not an available parameter!" % (param))
0092 continue
0093 tclparams[param] = str(value)
0094
0095 for param in tclparams:
0096 line = "set %s %s\n" % (param, tclparams[param])
0097 tclfile.write(line)
0098 tclfile.close()
0099
0100 def WritePythiaFile(output_filename):
0101 global args
0102
0103 for param in args.params:
0104 value = args.params[param]
0105 if (param not in pythiaparams.keys()) and (param not in tclparams.keys()):
0106 print("WARNING: you tried to set %s, which is not an available parameter!" % (param))
0107 continue
0108 pythiaparams[param] = str(value)
0109
0110
0111 with open(args.commands, "rt") as input_template:
0112 with open(output_filename, "wt") as output_template:
0113 for line in input_template:
0114 for param in pythiaparams:
0115 value = str(pythiaparams[param])
0116 line = line.replace(param, value)
0117
0118 output_template.write(line)
0119
0120
0121 def TransferTclFile(tclfile, taskdir):
0122 with open(tclfile, "rt") as input_template:
0123 with open("%s/%s" % (taskdir, tclfile), "wt") as output_template:
0124 for line in input_template:
0125 line = line.replace("customizations.tcl", "%s/customizations.tcl" % (taskdir))
0126
0127 output_template.write(line)
0128
0129
0130
0131 parser = argparse.ArgumentParser()
0132
0133 parser.add_argument("-n", "--name", type=str,
0134 help="name for this study set (e.g. bfield)")
0135 parser.add_argument("-t", "--template", type=str,
0136 help="template TCL file for study")
0137 parser.add_argument("-c", "--commands", type=str,
0138 help="template command file for study")
0139 parser.add_argument("-p", "--params", type=ast.literal_eval, default={},
0140 help="environment variables to set for study")
0141
0142
0143 parser.add_argument("-f", "--force", default=False, action='store_true',
0144 help="force-overwrite existing output")
0145
0146 global args
0147 args = parser.parse_args()
0148
0149
0150
0151 if not os.path.exists(args.name):
0152 try:
0153 os.makedirs(args.name)
0154 except OSError:
0155 print("%s already exists... continuing..." % (args.name))
0156
0157
0158 SLURM_ARRAY_TASK_ID="0"
0159
0160 try:
0161 SLURM_ARRAY_TASK_ID=os.environ["SLURM_ARRAY_TASK_ID"]
0162 except:
0163 print("Please set the SLURM_ARRAY_TASK_ID environment variable to a number (e.g. 0) before running this script.")
0164 sys.exit()
0165 pass
0166
0167 print("Task ID requested: %d" % (int(SLURM_ARRAY_TASK_ID)))
0168
0169
0170
0171 value_index = int(SLURM_ARRAY_TASK_ID)
0172
0173
0174
0175
0176
0177
0178 LoadParamsTcl("customizations.tcl")
0179 LoadParamsPythia(args.commands)
0180
0181
0182
0183
0184 random_seed = 0
0185 if "PARAM_RANDOM_SEED" not in args.params:
0186 random_seed = abs(hash(args.name)) % (10 ** 8) + value_index
0187 pythiaparams["PARAM_RANDOM_SEED"] = random_seed
0188
0189
0190
0191 taskdir="%s/%d" % (args.name, value_index)
0192 tclfile = "%s/customizations.tcl" % (taskdir)
0193 cmndfile = "%s/%s" % (taskdir, args.commands)
0194
0195 if os.path.exists(taskdir) and not args.force:
0196 print("Skipping this task directory --- it already exists. Cleanup before overwriting!")
0197 print(taskdir)
0198 else:
0199 if not os.path.exists(taskdir):
0200 os.makedirs(taskdir)
0201
0202
0203 WriteTclFile(tclfile)
0204 WritePythiaFile(cmndfile)
0205
0206
0207 copy_files = [args.template]
0208 for a_file in copy_files:
0209 subprocess.call("cp %s %s" % (a_file, taskdir), shell=True)
0210
0211
0212 rndm_file = open(taskdir+"/random_seed.dat", "w")
0213 rndm_file.write(str(random_seed))
0214 rndm_file.close()
0215
0216
0217 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)