8 from PhysicsTools.HeppyCore.framework.config
import CFG
9 from PhysicsTools.Heppy.utils.edmUtils
import edmFileLs
11 def __init__(self,configFile,command="cmsRun", addOrigAsSecondary=True, prefetch=False, options={}) :
20 tmpdir = os.environ[
'TMPDIR']
if 'TMPDIR' in os.environ
else "/tmp"
21 rndchars =
"".
join([hex(ord(i))[2:]
for i
in os.urandom(8)])
22 localfile =
"%s/%s-%s.root" % (tmpdir, os.path.basename(fname).
replace(
".root",
""), rndchars)
24 print "Fetching %s to local path %s " % (fname,localfile)
25 start = timeit.default_timer()
26 subprocess.check_output([
"xrdcp",
"-f",
"-N",fname,localfile])
27 print "Time used for transferring the file locally: %s s" % (timeit.default_timer() - start)
28 return (localfile,
True)
30 print "Could not save file locally, will run from remote"
31 if os.path.exists(localfile): os.remove(localfile)
35 component._preprocessor_tempFiles = []
36 for fn
in component.files:
37 if self.
prefetch and fn.startswith(
"root://"):
39 newfiles.append(newfile)
41 component._preprocessor_tempFiles.append(newfile)
44 component.files = newfiles
46 for fname
in component._preprocessor_tempFiles:
47 print "Removing local cache file ",fname
49 component._preprocessor_tempFiles = []
50 def run(self,component,wd,firstEvent,nEvents):
51 if firstEvent != 0:
raise RuntimeError(
"The preprocessor can't skip events at the moment")
52 fineSplitIndex, fineSplitFactor = getattr(component,
'fineSplit', (1,1))
53 if fineSplitFactor > 1:
54 if len(component.files) != 1:
55 raise RuntimeError(
"Any component with fineSplit > 1 is supposed to have just a single file, while %s has %s" % (component.name, component.files))
56 evtsInFile =
edmFileLs(component.files[0])[
'events']
57 if nEvents
in (
None, -1)
or nEvents > evtsInFile: nEvents = evtsInFile
58 nEvents = int(ceil(nEvents/float(fineSplitFactor)))
59 firstEvent = fineSplitIndex * nEvents
62 component.fineSplit = (1,1)
66 cmsswConfig = imp.load_source(
"cmsRunProcess",os.path.expandvars(self.
configFile))
68 for fn
in component.files :
69 if not re.match(
"file:.*",fn)
and not re.match(
"root:.*",fn) :
83 if hasattr(cmsswConfig,
"initialize"):
85 cmsswConfig.process = cmsswConfig.initialize()
87 cmsswConfig.process = cmsswConfig.initialize(**self.
options)
92 print "WARNING: cmsswPreprocessor received options but can't pass on to cmsswConfig"
94 cmsswConfig.process.source.fileNames = inputfiles
97 cmsswConfig.process.maxEvents.input = 1
if (fineSplitFactor>1
and nEvents==0)
else nEvents
98 cmsswConfig.process.source.skipEvents = cmsswConfig.cms.untracked.uint32(0
if (fineSplitFactor>1
and nEvents==0)
else firstEvent)
101 outfilename=wd+
"/cmsswPreProcessing.root"
103 for module
in cmsswConfig.process.endpaths.viewvalues():
104 for outName
in module.moduleNames():
105 out = getattr(cmsswConfig.process,outName)
106 if not hasattr(out,
"fileName"):
continue
107 out.fileName = outfilename
109 if not hasattr(component,
"options"):
110 component.options = CFG(name=
"postCmsrunOptions")
117 component.options.secondaryInputFiles= component.files
118 component.options.inputFiles=[outfilename]
119 component.files=[outfilename]
121 configfile=wd+
"/cmsRun_config.py"
122 f = open(configfile,
'w')
123 f.write(cmsswConfig.process.dumpPython())
125 runstring=
"%s %s >& %s/cmsRun.log" % (self.
command,configfile,wd)
126 print "Running pre-processor: %s " %runstring
127 ret=os.system(runstring)
129 print "CMSRUN failed"
def prefetchOneXrootdFile
static std::string join(char **cmd)