6 # Run event generation and produce an EVNT file.
9 import os, sys, time, shutil
13 from PyJobTransforms.trfArgs
import addAthenaArguments
14 from PyJobTransforms.trfDecorators
import stdTrfExceptionHandler, sigUsrStackTrace
15 from EvgenJobTransforms.evgenTrfArgs
import addStdEvgenArgs
19 ListOfDefaultPositionalKeys=[
'--AMIConfig',
'--AMITag',
'--argJSON',
'--asetup',
'--athena',
'--athenaMPMergeTargetSize',
'--athenaopts',
'--attempt',
'--checkEventCount',
'--command',
'--dumpJSON',
'--dumpPickle',
'--ecmEnergy',
'--env',
'--eventAcceptanceEfficiency',
'--evgenJobOpts',
'--execOnly',
'--fileValidation',
'--firstEvent',
'--ignoreErrors',
'--ignoreFiles',
'--ignorePatterns',
'--imf',
'--inputEVNT_PreFile',
'--inputFileValidation',
'--inputGenConfFile',
'--inputGeneratorFile',
'--jobConfig',
'--jobid',
'--maxEvents',
'--orphanKiller',
'--outputEVNTFile',
'--outputEVNT_PreFile',
'--outputEvtFile',
'--outputFileValidation',
'--outputNTUP_TRUTHFile',
'--outputTXTFile',
'--parallelFileValidation',
'--postExec',
'--postInclude',
'--preExec',
'--preInclude',
'--wprintEvts',
'--randomSeed',
'--reportName',
'--reportType',
'--rivetAnas',
'--runNumber',
'--showGraph',
'--showPath',
'--showSteps',
'--skipEvents',
'--skipFileValidation',
'--skipInputFileValidation',
'--skipOutputFileValidation',
'--steering',
'--taskid',
'--tcmalloc',
'--valgrind',
'--valgrindbasicopts',
'--valgrindextraopts',
'--lheOnly',
'--localPath',
'--cleanOut',
'--saveList']
22 "Specialised trf executor class for event generation jobs"
23 def __init__(self, name="generate", skeleton=None, skeletonCA=None, substep=None, inData=set(), outData=
set()):
24 athenaExecutor.__init__(self, name=name, skeletonFile=skeleton, skeletonCA=skeletonCA,
25 substep=substep, inputEventTest=
False, tryDropAndReload=
False,
26 inData=inData, outData=outData)
29 "Get input tarball, unpack and set up env if an evgenJobOpts arg was provided."
32 super(EvgenExecutor, self).
preExecute(input, output)
34 def expand_if_archive(filename):
35 "Function to expand a file if it is a zip archive or tarball"
36 if ".tar" in filename:
38 with tarfile.open(filename)
as tf:
40 elif filename.endswith(
".zip"):
42 zf = zipfile.ZipFile(filename)
47 return [name
for name
in os.listdir(a_dir)
48 if os.path.isdir(os.path.join(a_dir, name))]
52 os.environ[
'LOCAL_INSTALL_DIR'] = (os.environ[
'JOBOPTSEARCHPATH']).
split(
":")[0]
53 if os.path.exists(
'/cvmfs/atlas.cern.ch/repo/sw/Generators/MCJobOptions/common'):
54 CommonCvmfsDir =
'/cvmfs/atlas.cern.ch/repo/sw/Generators/MCJobOptions/common'
55 os.environ[
"JOBOPTSEARCHPATH"] = CommonCvmfsDir+
":"+os.environ[
"JOBOPTSEARCHPATH"]
56 if os.path.exists(
'/cvmfs/atlas.cern.ch/repo/sw/Generators/MCJobOptions/common/MadGraphControl/dat/'):
57 datCvmfsDir =
'/cvmfs/atlas.cern.ch/repo/sw/Generators/MCJobOptions/common/MadGraphControl/dat/'
58 os.environ[
"DATAPATH"] = datCvmfsDir+
":"+os.environ[
"DATAPATH"]
60 dsidpar = (self._trf.argdict[
"jobConfig"].value).
values()
61 dsidparam =
list(dsidpar)[0][0]
63 if os.path.exists(
'/cvmfs/atlas.cern.ch/repo/sw/Generators/MCJobOptions/'):
64 BaseCvmfsPath =
"/cvmfs/atlas.cern.ch/repo/sw/Generators/MCJobOptions/"
66 if dsidparam.isdigit()
and (len(dsidparam)==6
or len(dsidparam)==7):
68 Jodir = dsidparam[:3]+
'xxx'
70 Jodir = dsidparam[0] +
'/' + dsidparam[:4]+
'xxx'
72 cwd_ful = os.path.join(cwdir, dsidparam)
73 if (os.path.isdir(cwd_ful)):
74 os.environ[
"JOBOPTSEARCHPATH"] = cwd_ful+
":"+os.environ[
"JOBOPTSEARCHPATH"]
75 os.environ[
"DATAPATH"] = cwd_ful+
":"+os.environ[
"DATAPATH"]
77 cwd_Jodir = os.path.join(cwdir,Jodir)
78 cwd_Jodir_ful = os.path.join(cwd_Jodir,dsidparam)
79 if (os.path.isdir(cwd_Jodir_ful)):
80 os.environ[
"JOBOPTSEARCHPATH"] = cwd_Jodir_ful+
":"+os.environ[
"JOBOPTSEARCHPATH"]
81 os.environ[
"DATAPATH"] = cwd_Jodir_ful+
":"+os.environ[
"DATAPATH"]
83 if (os.path.isdir(BaseCvmfsPath)):
84 JoCvmfsPath = os.path.join(BaseCvmfsPath, Jodir)
85 JoCvmfsPath_ful = os.path.join(JoCvmfsPath, dsidparam)
86 os.environ[
"JOBOPTSEARCHPATH"] = JoCvmfsPath_ful+
":"+os.environ[
"JOBOPTSEARCHPATH"]
87 os.environ[
"DATAPATH"] = JoCvmfsPath_ful+
":"+os.environ[
"DATAPATH"]
89 msg.error(
"No access to JOs cvmfs location. JOs should be placed in working directory or full path should be given.")
90 raise RuntimeError(
"JOs not found - no cvmfs access")
92 if dsidparam.startswith(
"Test"):
93 for token
in (os.environ[
'JOBOPTSEARCHPATH']).
split(
":"):
94 if "jobOptions" in token:
97 search_token +=
"/EvgenJobTransforms/EvgenTest/" + dsidparam.split(
"Test")[-1]
98 os.environ[
"JOBOPTSEARCHPATH"] = search_token+
":"+os.environ[
"JOBOPTSEARCHPATH"]
99 os.environ[
"DATAPATH"] = search_token+
":"+os.environ[
"DATAPATH"]
100 elif (os.path.isdir(dsidparam)):
101 os.environ[
"JOBOPTSEARCHPATH"] = dsidparam+
":"+os.environ[
"JOBOPTSEARCHPATH"]
102 os.environ[
"DATAPATH"] = dsidparam+
":"+os.environ[
"DATAPATH"]
104 msg.error(
"JOs not found, please check = '%s'" % dsidparam)
105 raise RuntimeError(
"JOs not found")
107 msg.info(
"Using JOBOPTSEARCHPATH = '%s'" % os.environ[
"JOBOPTSEARCHPATH"])
108 msg.info(
"Using DATAPATH = '%s'" % os.environ[
"DATAPATH"])
110 if "evgenJobOpts" in self._trf.argdict:
111 tarball = self._trf.argdict[
"evgenJobOpts"].value
114 if tarball.startswith(
"http"):
116 tarball = os.basename(tarball)
118 url =
"http://cern.ch/atlas-computing/links/kitsDirectory/EvgenJobOpts/" + tarball
120 if not os.path.exists(tarball):
121 from EvgenJobTransforms.download
import downloadUsingProxy
124 raise EnvironmentError(
'Error downloading tarball %s. Downloader reports: %s' % (tarball, output))
125 msg.info(
'Evgen tarball download success: %s' % output)
127 expand_if_archive(tarball)
132 FIRST_DIR = (os.environ[
'JOBOPTSEARCHPATH']).
split(
":")[0]
133 configFiles = [f
for f
in os.listdir(FIRST_DIR)
if (
"GRID" in f)]
135 if len(configFiles) == 1:
136 confFile = os.path.join(FIRST_DIR, configFiles[0])
137 elif len(configFiles) >1:
138 msg.info(
"more then one gridpack ! ")
139 if "--ecmEnergy" in str(sys.argv[1:]):
140 split_args=
str(sys.argv[1:]).
split(
"ecmEnergy=",1)[1]
141 ener_GeV=split_args.split(
",")[0].strip(
"\'")
143 msg.info(
"Should be used gridpack for energy "+energy)
146 for x
in configFiles:
147 gridS=
"mc_"+energy+
"TeV"
148 msg.info(
"Gridpack should start from "+gridS)
149 if x.startswith(gridS):
150 confFile = os.path.join(FIRST_DIR, x)
151 msg.info(
"using gridpack = "+confFile)
153 msg.error(
"No *GRID* config files, for requested energy = '%s' please check = '%s'" %(energy,dsidparam))
155 if confFile
is not None:
156 expand_if_archive(confFile)
158 msg.info(
"Configuration input gridpack found " + confFile)
161 loc_files = os.listdir(os.getcwd())
162 for loc_file
in loc_files:
163 if "GRID" not in loc_file:
164 expand_if_archive(loc_file)
167 if "inputGeneratorFile" in self._trf.argdict:
169 myinputfiles = self._trf.argdict[
"inputGeneratorFile"].value
170 genInputFiles = myinputfiles.split(
',')
171 for file
in genInputFiles:
172 expand_if_archive(file)
173 if "inputGenConfFile" in self._trf.argdict:
174 expand_if_archive(self._trf.argdict[
"inputGenConfFile"].value)
177 files = os.listdir(tmp_dir)
183 dest = main_dir+
"/"+f
190 msg.info(
"Transform arguments %s" % sys.argv[1:])
191 if "--outputEVNTFile" in str(sys.argv[1:]):
192 exeSet.add(
EvgenExecutor(name=
"generate", skeleton=
"EvgenJobTransforms/skel.GENtoEVGEN.py", skeletonCA=
"EvgenJobTransforms.GENtoEVGEN_Skeleton", inData=[
"inNULL"], outData=[
"YODA",
"EVNT",
"EVNT_Pre",
"TXT"]))
193 msg.info(
"Output EVNT file")
194 elif "--outputYODAFile" in str(sys.argv[1:]):
195 exeSet.add(
EvgenExecutor(name=
"generate", skeleton=
"EvgenJobTransforms/skel.GENtoEVGEN.py", inData=[
"inNULL"], outData=[
"YODA",
"TXT"]))
196 msg.info(
"Output EVNT file")
197 elif "--outputTXTFile" in str(sys.argv[1:]):
198 exeSet.add(
EvgenExecutor(name=
"generate", skeleton=
"EvgenJobTransforms/skel.GENtoTXT.py", inData=[
"inNULL"], outData=[
"TXT"]))
199 msg.info(
"Output TXT file")
201 msg.error(
"Output cannot be recognised")
203 exeSet.add(
EvgenExecutor(name=
"afterburn", skeleton=
"EvgenJobTransforms/skel.ABtoEVGEN.py", inData=[
"EVNT_Pre"], outData=[
"EVNT"]))
204 exeSet.add(athenaExecutor(name =
"AODtoDPD", skeletonFile =
"PATJobTransforms/skeleton.AODtoDPD_tf.py",
205 substep =
"a2d", inData = [
"EVNT"], outData = [
"NTUP_TRUTH"], perfMonFile =
"ntuple_AODtoDPD.pmon.gz"))
206 exeSet.add(athenaExecutor(name =
'EVNTtoHEPMC', skeletonCA =
'EvgenJobTransforms.EVNTtoHEPMC_Skeleton',
207 substep =
"a2d", perfMonFile =
'ntuple.pmon.gz', inData=[
'EVNT'], outData=[
'Evt']))
214 @stdTrfExceptionHandler
217 msg.info(
"This is %s" % sys.argv[0])
219 main_dir = os.getcwd()
221 trf.parseCmdLineArgs(sys.argv[1:])
222 if ((
"cleanOut" in trf.argdict)
and (trf.argdict[
"cleanOut"].value != 0)):
223 name_tmpdir =
"tmprun"
224 tmp_dir = os.path.join(main_dir, name_tmpdir)
225 if os.path.isdir(tmp_dir):
226 shutil.rmtree(tmp_dir, ignore_errors=
True)
229 tmp_dir = os.getcwd()
230 whitelist_in = [
'MC',
'group',
'TXT']
235 msg.info(
"%s stopped at %s, trf exit code %d" % (sys.argv[0], time.asctime(), trf.exitCode))
240 if ((
"cleanOut" in trf.argdict)
and (trf.argdict[
"cleanOut"].value!=0)):
241 whitelist_out = [
'log.generate',
'.root']
242 if "outputTXTFile" in trf.argdict:
243 whitelist_out.append(
'TXT')
244 if "saveList" in trf.argdict:
245 saveList_dic= trf.argdict[
"saveList"].value
246 saveList_str=
str(saveList_dic)
247 saveList_str=saveList_str[10:-3]
248 saveList= saveList_str.split(
",")
249 for item
in saveList:
250 test_ex = os.path.join(main_dir,
str(item))
251 if os.path.isdir(test_ex):
252 shutil.rmtree(test_ex, ignore_errors=
True)
253 elif os.path.isfile(test_ex):
255 if not saveList[0].isdigit():
256 whitelist_out=whitelist_out+saveList
260 if "saveList" not in trf.argdict:
261 shutil.rmtree(tmp_dir, ignore_errors=
True)
262 elif not saveList[0].isdigit():
263 shutil.rmtree(tmp_dir, ignore_errors=
True)
265 elif (
"inputGeneratorFile" in trf.argdict):
266 myinputfiles = trf.argdict[
"inputGeneratorFile"].value
267 genInputFiles = myinputfiles.split(
',')
268 numberOfFiles = len(genInputFiles)
269 merge_file =
'merged_lhef._0.events'
270 if((numberOfFiles>1)
and (os.path.exists(merge_file))):
271 os.remove(merge_file)
273 if ((
"lheOnly" in trf.argdict )
and (trf.argdict[
"lheOnly"].value == 1)):
274 outputName =
''.
join(trf.argdict[
"outputEVNTFile"].value)
275 os.remove(outputName)
276 sys.exit(trf.exitCode)
282 if __name__ ==
"__main__":