1 from __future__
import print_function
9 from PhysicsTools.HeppyCore.framework.config
import CFG
10 from PhysicsTools.Heppy.utils.edmUtils
import edmFileLs
12 def __init__(self,configFile,command="cmsRun", addOrigAsSecondary=True, prefetch=False, options={}) :
21 tmpdir = os.environ[
'TMPDIR']
if 'TMPDIR' in os.environ
else "/tmp"
22 rndchars =
"".
join([hex(ord(i))[2:]
for i
in os.urandom(8)])
23 localfile =
"%s/%s-%s.root" % (tmpdir, os.path.basename(fname).
replace(
".root",
""), rndchars)
25 print(
"Fetching %s to local path %s " % (fname,localfile))
26 start = timeit.default_timer()
27 subprocess.check_output([
"xrdcp",
"-f",
"-N",fname,localfile])
28 print(
"Time used for transferring the file locally: %s s" % (timeit.default_timer() - start))
29 return (localfile,
True)
31 print(
"Could not save file locally, will run from remote")
32 if os.path.exists(localfile): os.remove(localfile)
36 component._preprocessor_tempFiles = []
37 for fn
in component.files:
38 if self.
prefetch and fn.startswith(
"root://"):
40 newfiles.append(newfile)
42 component._preprocessor_tempFiles.append(newfile)
45 component.files = newfiles
47 for fname
in component._preprocessor_tempFiles:
48 print(
"Removing local cache file ",fname)
50 component._preprocessor_tempFiles = []
51 def run(self,component,wd,firstEvent,nEvents):
52 if firstEvent != 0:
raise RuntimeError(
"The preprocessor can't skip events at the moment")
53 fineSplitIndex, fineSplitFactor = getattr(component,
'fineSplit', (1,1))
54 if fineSplitFactor > 1:
55 if len(component.files) != 1:
56 raise RuntimeError(
"Any component with fineSplit > 1 is supposed to have just a single file, while %s has %s" % (component.name, component.files))
57 evtsInFile =
edmFileLs(component.files[0])[
'events']
58 if nEvents
in (
None, -1)
or nEvents > evtsInFile: nEvents = evtsInFile
60 firstEvent = fineSplitIndex * nEvents
63 component.fineSplit = (1,1)
67 cmsswConfig = imp.load_source(
"cmsRunProcess",os.path.expandvars(self.
configFile))
69 for fn
in component.files :
70 if not re.match(
"file:.*",fn)
and not re.match(
"root:.*",fn) :
84 if hasattr(cmsswConfig,
"initialize"):
86 cmsswConfig.process = cmsswConfig.initialize()
88 cmsswConfig.process = cmsswConfig.initialize(**self.
options)
93 print(
"WARNING: cmsswPreprocessor received options but can't pass on to cmsswConfig")
95 cmsswConfig.process.source.fileNames = inputfiles
98 cmsswConfig.process.maxEvents.input = 1
if (fineSplitFactor>1
and nEvents==0)
else nEvents
99 cmsswConfig.process.source.skipEvents = cmsswConfig.cms.untracked.uint32(0
if (fineSplitFactor>1
and nEvents==0)
else firstEvent)
102 outfilename=wd+
"/cmsswPreProcessing.root"
104 for module
in cmsswConfig.process.endpaths.viewvalues():
105 for outName
in module.moduleNames():
106 out = getattr(cmsswConfig.process,outName)
107 if not hasattr(out,
"fileName"):
continue
108 out.fileName = outfilename
110 if not hasattr(component,
"options"):
111 component.options = CFG(name=
"postCmsrunOptions")
118 component.options.secondaryInputFiles= component.files
119 component.options.inputFiles=[outfilename]
120 component.files=[outfilename]
122 configfile=wd+
"/cmsRun_config.py"
123 f = open(configfile,
'w')
124 f.write(cmsswConfig.process.dumpPython())
126 runstring=
"%s %s >& %s/cmsRun.log" % (self.
command,configfile,wd)
127 print(
"Running pre-processor: %s " %runstring)
128 ret=os.system(runstring)
130 print(
"CMSRUN failed")