7 |
|
__revision__ = "$Id$" |
8 |
|
__version__ = "$Revision$" |
9 |
|
|
10 |
< |
import sys, getopt |
10 |
> |
import getopt |
11 |
|
import imp |
12 |
|
import os |
13 |
+ |
import pickle |
14 |
+ |
import sys |
15 |
+ |
import xml.dom.minidom |
16 |
+ |
|
17 |
|
from random import SystemRandom |
18 |
|
|
19 |
|
from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface |
39 |
|
""" |
40 |
|
writeCfg |
41 |
|
|
42 |
< |
- Read in existing, user supplied cfg or pycfg file |
43 |
< |
- Modify job specific parameters based on environment variables |
44 |
< |
- Write out modified cfg or pycfg file |
42 |
> |
- Read in existing, user supplied pycfg or pickled pycfg file |
43 |
> |
- Modify job specific parameters based on environment variables and arguments.xml |
44 |
> |
- Write out pickled pycfg file |
45 |
|
|
46 |
|
required parameters: none |
47 |
|
|
89 |
|
print main.__doc__ |
90 |
|
sys.exit() |
91 |
|
|
92 |
< |
# Optional Parameters |
92 |
> |
# Read in Environment, XML and get optional Parameters |
93 |
|
|
90 |
– |
maxEvents = int(os.environ.get('MaxEvents', '0')) |
91 |
– |
skipEvents = int(os.environ.get('SkipEvents','0')) |
92 |
– |
firstEvent = int(os.environ.get('FirstEvent','-1')) |
93 |
– |
compHEPFirstEvent = int(os.environ.get('CompHEPFirstEvent','0')) |
94 |
– |
firstRun = int(os.environ.get('FirstRun', '0')) |
94 |
|
nJob = int(os.environ.get('NJob', '0')) |
96 |
– |
|
97 |
– |
inputFiles = os.environ.get('InputFiles','') |
98 |
– |
parentFiles = os.environ.get('ParentFiles','') |
95 |
|
preserveSeeds = os.environ.get('PreserveSeeds','') |
96 |
|
incrementSeeds = os.environ.get('IncrementSeeds','') |
97 |
|
|
98 |
< |
# Read Input cfg or python cfg file, FUTURE: Get rid cfg mode |
98 |
> |
# Defaults |
99 |
> |
|
100 |
> |
maxEvents = 0 |
101 |
> |
skipEvents = 0 |
102 |
> |
firstEvent = -1 |
103 |
> |
compHEPFirstEvent = 0 |
104 |
> |
firstRun = 0 |
105 |
> |
# FUTURE: Remove firstRun |
106 |
> |
firstLumi = 0 |
107 |
> |
|
108 |
> |
dom = xml.dom.minidom.parse(os.environ['RUNTIME_AREA']+'/arguments.xml') |
109 |
> |
|
110 |
> |
for elem in dom.getElementsByTagName("Job"): |
111 |
> |
if nJob == int(elem.getAttribute("JobID")): |
112 |
> |
if elem.getAttribute("MaxEvents"): |
113 |
> |
maxEvents = int(elem.getAttribute("MaxEvents")) |
114 |
> |
if elem.getAttribute("SkipEvents"): |
115 |
> |
skipEvents = int(elem.getAttribute("SkipEvents")) |
116 |
> |
if elem.getAttribute("FirstEvent"): |
117 |
> |
firstEvent = int(elem.getAttribute("FirstEvent")) |
118 |
> |
if elem.getAttribute("FirstRun"): |
119 |
> |
firstRun = int(elem.getAttribute("FirstRun")) |
120 |
> |
if elem.getAttribute("FirstLumi"): |
121 |
> |
firstLumi = int(elem.getAttribute("FirstLumi")) |
122 |
> |
|
123 |
> |
generator = str(elem.getAttribute('Generator')) |
124 |
> |
inputFiles = str(elem.getAttribute('InputFiles')) |
125 |
> |
parentFiles = str(elem.getAttribute('ParentFiles')) |
126 |
> |
lumis = str(elem.getAttribute('Lumis')) |
127 |
|
|
128 |
< |
if fileName.endswith('py'): |
129 |
< |
handle = open(fileName, 'r') |
130 |
< |
try: # Nested form for Python < 2.5 |
131 |
< |
try: |
108 |
< |
print "Importing .py file" |
109 |
< |
cfo = imp.load_source("pycfg", fileName, handle) |
110 |
< |
cmsProcess = cfo.process |
111 |
< |
except Exception, ex: |
112 |
< |
msg = "Your pycfg file is not valid python: %s" % str(ex) |
113 |
< |
raise ConfigException(msg) |
114 |
< |
finally: |
115 |
< |
handle.close() |
116 |
< |
else: |
128 |
> |
# Read Input python config file |
129 |
> |
|
130 |
> |
handle = open(fileName, 'r') |
131 |
> |
try: # Nested form for Python < 2.5 |
132 |
|
try: |
133 |
< |
print "Importing .cfg file" |
134 |
< |
cfo = include(fileName) |
135 |
< |
cmsProcess = cfo |
133 |
> |
print "Importing .py file" |
134 |
> |
cfo = imp.load_source("pycfg", fileName, handle) |
135 |
> |
cmsProcess = cfo.process |
136 |
|
except Exception, ex: |
137 |
< |
msg = "The cfg file is not valid, %s\n" % str(ex) |
137 |
> |
msg = "Your pycfg file is not valid python: %s" % str(ex) |
138 |
|
raise ConfigException(msg) |
139 |
+ |
finally: |
140 |
+ |
handle.close() |
141 |
|
|
142 |
|
cfg = CfgInterface(cmsProcess) |
143 |
|
|
149 |
|
|
150 |
|
if skipEvents: |
151 |
|
inModule.setSkipEvents(skipEvents) |
152 |
< |
if firstEvent != -1: |
152 |
> |
|
153 |
> |
# Set "skip events" for various generators |
154 |
> |
if generator == 'comphep': |
155 |
> |
cmsProcess.source.CompHEPFirstEvent = CfgTypes.int32(firstEvent) |
156 |
> |
elif generator == 'lhe': |
157 |
> |
cmsProcess.source.skipEvents = CfgTypes.untracked(CfgTypes.uint32(firstEvent)) |
158 |
> |
cmsProcess.source.firstEvent = CfgTypes.untracked(CfgTypes.uint32(firstEvent+1)) |
159 |
> |
elif firstEvent != -1: # (Old? Madgraph) |
160 |
|
cmsProcess.source.firstEvent = CfgTypes.untracked(CfgTypes.uint32(firstEvent)) |
161 |
< |
if compHEPFirstEvent: |
138 |
< |
cmsProcess.source.CompHEPFirstEvent = CfgTypes.int32(compHEPFirstEvent) |
161 |
> |
|
162 |
|
if inputFiles: |
140 |
– |
inputFiles = inputFiles.replace('\\','') |
141 |
– |
inputFiles = inputFiles.replace('"','') |
163 |
|
inputFileNames = inputFiles.split(',') |
164 |
|
inModule.setFileNames(*inputFileNames) |
165 |
|
|
166 |
|
# handle parent files if needed |
167 |
|
if parentFiles: |
147 |
– |
parentFiles = parentFiles.replace('\\','') |
148 |
– |
parentFiles = parentFiles.replace('"','') |
168 |
|
parentFileNames = parentFiles.split(',') |
169 |
|
inModule.setSecondaryFileNames(*parentFileNames) |
170 |
|
|
171 |
+ |
if lumis: |
172 |
+ |
if CMSSW_major < 3: # FUTURE: Can remove this check |
173 |
+ |
print "Cannot skip lumis for CMSSW 2_x" |
174 |
+ |
else: |
175 |
+ |
lumiRanges = lumis.split(',') |
176 |
+ |
inModule.setLumisToProcess(*lumiRanges) |
177 |
+ |
|
178 |
|
# Pythia parameters |
179 |
|
if (firstRun): |
180 |
|
inModule.setFirstRun(firstRun) |
181 |
+ |
if (firstLumi): |
182 |
+ |
inModule.setFirstLumi(firstLumi) |
183 |
|
|
184 |
< |
incrementSeedList = [] |
185 |
< |
preserveSeedList = [] |
158 |
< |
|
159 |
< |
if incrementSeeds: |
160 |
< |
incrementSeedList = incrementSeeds.split(',') |
161 |
< |
if preserveSeeds: |
162 |
< |
preserveSeedList = preserveSeeds.split(',') |
163 |
< |
|
164 |
< |
# FUTURE: This function tests the CMSSW version and presence of old-style seed specification. |
165 |
< |
# Reduce when we drop support for old versions |
166 |
< |
if cfg.data.services.has_key('RandomNumberGeneratorService'): # There are random #'s to deal with |
184 |
> |
# Check if there are random #'s to deal with |
185 |
> |
if cfg.data.services.has_key('RandomNumberGeneratorService'): |
186 |
|
print "RandomNumberGeneratorService found, will attempt to change seeds" |
187 |
+ |
from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper |
188 |
|
ranGenerator = cfg.data.services['RandomNumberGeneratorService'] |
189 |
< |
ranModules = getattr(ranGenerator, "moduleSeeds", None) |
170 |
< |
oldSource = getattr(ranGenerator, "sourceSeed", None) |
171 |
< |
if ranModules != None or oldSource != None: # Old format present, no matter the CMSSW version |
172 |
< |
print "Old-style random number seeds found, will be changed." |
173 |
< |
if oldSource != None: |
174 |
< |
sourceSeed = int(ranGenerator.sourceSeed.value()) |
175 |
< |
if ('sourceSeed' in preserveSeedList) or ('theSource' in preserveSeedList): |
176 |
< |
pass |
177 |
< |
elif ('sourceSeed' in incrementSeedList) or ('theSource' in incrementSeedList): |
178 |
< |
ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(sourceSeed+nJob)) |
179 |
< |
else: |
180 |
< |
ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(MyRandom.randint(1, _MAXINT))) |
181 |
< |
|
182 |
< |
for seed in incrementSeedList: |
183 |
< |
curSeed = getattr(ranGenerator.moduleSeeds, seed, None) |
184 |
< |
if curSeed: |
185 |
< |
curValue = int(curSeed.value()) |
186 |
< |
setattr(ranGenerator.moduleSeeds, seed, CfgTypes.untracked(CfgTypes.uint32(curValue+nJob))) |
187 |
< |
preserveSeedList.append(seed) |
188 |
< |
|
189 |
< |
if ranModules != None: |
190 |
< |
for seed in ranGenerator.moduleSeeds.parameterNames_(): |
191 |
< |
if seed not in preserveSeedList: |
192 |
< |
curSeed = getattr(ranGenerator.moduleSeeds, seed, None) |
193 |
< |
if curSeed: |
194 |
< |
curValue = int(curSeed.value()) |
195 |
< |
setattr(ranGenerator.moduleSeeds, seed, |
196 |
< |
CfgTypes.untracked(CfgTypes.uint32(MyRandom.randint(1,_MAXINT)))) |
197 |
< |
elif CMSSW_major > 2 or (CMSSW_major == 2 and CMSSW_minor >= 1): # Treatment for seeds, CMSSW 2_1_x and later |
198 |
< |
print "New-style random number seeds found, will be changed." |
199 |
< |
from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper |
200 |
< |
randSvc = RandomNumberServiceHelper(ranGenerator) |
201 |
< |
|
202 |
< |
# Increment requested seed sets |
203 |
< |
for seedName in incrementSeedList: |
204 |
< |
curSeeds = randSvc.getNamedSeed(seedName) |
205 |
< |
newSeeds = [x+nJob for x in curSeeds] |
206 |
< |
randSvc.setNamedSeed(seedName, *newSeeds) |
207 |
< |
preserveSeedList.append(seedName) |
189 |
> |
randSvc = RandomNumberServiceHelper(ranGenerator) |
190 |
|
|
191 |
< |
# Randomize remaining seeds |
192 |
< |
randSvc.populate(*preserveSeedList) |
193 |
< |
else: |
194 |
< |
print "Neither old nor new seed format found!" |
191 |
> |
incrementSeedList = [] |
192 |
> |
preserveSeedList = [] |
193 |
> |
|
194 |
> |
if incrementSeeds: |
195 |
> |
incrementSeedList = incrementSeeds.split(',') |
196 |
> |
if preserveSeeds: |
197 |
> |
preserveSeedList = preserveSeeds.split(',') |
198 |
> |
|
199 |
> |
# Increment requested seed sets |
200 |
> |
for seedName in incrementSeedList: |
201 |
> |
curSeeds = randSvc.getNamedSeed(seedName) |
202 |
> |
newSeeds = [x+nJob for x in curSeeds] |
203 |
> |
randSvc.setNamedSeed(seedName, *newSeeds) |
204 |
> |
preserveSeedList.append(seedName) |
205 |
|
|
206 |
< |
# End version specific code |
206 |
> |
# Randomize remaining seeds |
207 |
> |
randSvc.populate(*preserveSeedList) |
208 |
|
|
209 |
< |
# Write out new config file in one format or the other, FUTURE: Get rid of cfg mode |
209 |
> |
# Write out new config file |
210 |
|
outFile = open(outFileName,"w") |
211 |
< |
if outFileName.endswith('py'): |
212 |
< |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
213 |
< |
outFile.write(cmsProcess.dumpPython()) |
214 |
< |
if (debug): |
222 |
< |
print "writeCfg output:" |
223 |
< |
print "import FWCore.ParameterSet.Config as cms" |
224 |
< |
print cmsProcess.dumpPython() |
225 |
< |
else: |
226 |
< |
outFile.write(cfg.data.dumpConfig()) |
227 |
< |
if (debug): |
228 |
< |
print "writeCfg output:" |
229 |
< |
print str(cfg.data.dumpConfig()) |
211 |
> |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
212 |
> |
outFile.write("import pickle\n") |
213 |
> |
outFile.write("pickledCfg=\"\"\"%s\"\"\"\n" % pickle.dumps(cmsProcess)) |
214 |
> |
outFile.write("process = pickle.loads(pickledCfg)\n") |
215 |
|
outFile.close() |
216 |
+ |
if (debug): |
217 |
+ |
print "writeCfg output (May not be exact):" |
218 |
+ |
print "import FWCore.ParameterSet.Config as cms" |
219 |
+ |
print cmsProcess.dumpPython() |
220 |
|
|
221 |
|
|
222 |
|
if __name__ == '__main__' : |