39 |
|
""" |
40 |
|
writeCfg |
41 |
|
|
42 |
< |
- Read in existing, user supplied cfg or pickled 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 modified cfg or pickled pycfg file |
44 |
> |
- Write out pickled pycfg file |
45 |
|
|
46 |
|
required parameters: none |
47 |
|
|
121 |
|
parentFiles = str(elem.getAttribute('ParentFiles')) |
122 |
|
lumis = str(elem.getAttribute('Lumis')) |
123 |
|
|
124 |
< |
# Read Input cfg or python cfg file, FUTURE: Get rid cfg mode |
124 |
> |
# Read Input python config file |
125 |
|
|
126 |
< |
if fileName.endswith('py'): |
127 |
< |
handle = open(fileName, 'r') |
128 |
< |
try: # Nested form for Python < 2.5 |
129 |
< |
try: |
130 |
< |
print "Importing .py file" |
131 |
< |
cfo = imp.load_source("pycfg", fileName, handle) |
132 |
< |
cmsProcess = cfo.process |
133 |
< |
except Exception, ex: |
134 |
< |
msg = "Your pycfg file is not valid python: %s" % str(ex) |
135 |
< |
raise ConfigException(msg) |
136 |
< |
finally: |
137 |
< |
handle.close() |
138 |
< |
else: |
126 |
> |
handle = open(fileName, 'r') |
127 |
> |
try: # Nested form for Python < 2.5 |
128 |
|
try: |
129 |
< |
print "Importing .cfg file" |
130 |
< |
cfo = include(fileName) |
131 |
< |
cmsProcess = cfo |
129 |
> |
print "Importing .py file" |
130 |
> |
cfo = imp.load_source("pycfg", fileName, handle) |
131 |
> |
cmsProcess = cfo.process |
132 |
|
except Exception, ex: |
133 |
< |
msg = "The cfg file is not valid, %s\n" % str(ex) |
133 |
> |
msg = "Your pycfg file is not valid python: %s" % str(ex) |
134 |
|
raise ConfigException(msg) |
135 |
+ |
finally: |
136 |
+ |
handle.close() |
137 |
|
|
138 |
|
cfg = CfgInterface(cmsProcess) |
139 |
|
|
165 |
|
inModule.setSecondaryFileNames(*parentFileNames) |
166 |
|
|
167 |
|
if lumis: |
168 |
< |
lumiRanges = lumis.split(',') |
169 |
< |
inModule.setLumisToProcess(*lumiRanges) |
168 |
> |
if CMSSW_major < 3: # FUTURE: Can remove this check |
169 |
> |
print "Cannot skip lumis for CMSSW 2_x" |
170 |
> |
else: |
171 |
> |
lumiRanges = lumis.split(',') |
172 |
> |
inModule.setLumisToProcess(*lumiRanges) |
173 |
|
|
174 |
|
# Pythia parameters |
175 |
|
if (firstRun): |
176 |
|
inModule.setFirstRun(firstRun) |
177 |
|
|
178 |
< |
incrementSeedList = [] |
179 |
< |
preserveSeedList = [] |
186 |
< |
|
187 |
< |
if incrementSeeds: |
188 |
< |
incrementSeedList = incrementSeeds.split(',') |
189 |
< |
if preserveSeeds: |
190 |
< |
preserveSeedList = preserveSeeds.split(',') |
191 |
< |
|
192 |
< |
# FUTURE: This function tests the CMSSW version and presence of old-style seed specification. |
193 |
< |
# Reduce when we drop support for old versions |
194 |
< |
if cfg.data.services.has_key('RandomNumberGeneratorService'): # There are random #'s to deal with |
178 |
> |
# Check if there are random #'s to deal with |
179 |
> |
if cfg.data.services.has_key('RandomNumberGeneratorService'): |
180 |
|
print "RandomNumberGeneratorService found, will attempt to change seeds" |
181 |
+ |
from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper |
182 |
|
ranGenerator = cfg.data.services['RandomNumberGeneratorService'] |
183 |
< |
ranModules = getattr(ranGenerator, "moduleSeeds", None) |
198 |
< |
oldSource = getattr(ranGenerator, "sourceSeed", None) |
199 |
< |
if ranModules != None or oldSource != None: # Old format present, no matter the CMSSW version |
200 |
< |
print "Old-style random number seeds found, will be changed." |
201 |
< |
if oldSource != None: |
202 |
< |
sourceSeed = int(ranGenerator.sourceSeed.value()) |
203 |
< |
if ('sourceSeed' in preserveSeedList) or ('theSource' in preserveSeedList): |
204 |
< |
pass |
205 |
< |
elif ('sourceSeed' in incrementSeedList) or ('theSource' in incrementSeedList): |
206 |
< |
ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(sourceSeed+nJob)) |
207 |
< |
else: |
208 |
< |
ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(MyRandom.randint(1, _MAXINT))) |
209 |
< |
|
210 |
< |
for seed in incrementSeedList: |
211 |
< |
curSeed = getattr(ranGenerator.moduleSeeds, seed, None) |
212 |
< |
if curSeed: |
213 |
< |
curValue = int(curSeed.value()) |
214 |
< |
setattr(ranGenerator.moduleSeeds, seed, CfgTypes.untracked(CfgTypes.uint32(curValue+nJob))) |
215 |
< |
preserveSeedList.append(seed) |
216 |
< |
|
217 |
< |
if ranModules != None: |
218 |
< |
for seed in ranGenerator.moduleSeeds.parameterNames_(): |
219 |
< |
if seed not in preserveSeedList: |
220 |
< |
curSeed = getattr(ranGenerator.moduleSeeds, seed, None) |
221 |
< |
if curSeed: |
222 |
< |
curValue = int(curSeed.value()) |
223 |
< |
setattr(ranGenerator.moduleSeeds, seed, |
224 |
< |
CfgTypes.untracked(CfgTypes.uint32(MyRandom.randint(1,_MAXINT)))) |
225 |
< |
elif CMSSW_major > 2 or (CMSSW_major == 2 and CMSSW_minor >= 1): # Treatment for seeds, CMSSW 2_1_x and later |
226 |
< |
print "New-style random number seeds found, will be changed." |
227 |
< |
from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper |
228 |
< |
randSvc = RandomNumberServiceHelper(ranGenerator) |
229 |
< |
|
230 |
< |
# Increment requested seed sets |
231 |
< |
for seedName in incrementSeedList: |
232 |
< |
curSeeds = randSvc.getNamedSeed(seedName) |
233 |
< |
newSeeds = [x+nJob for x in curSeeds] |
234 |
< |
randSvc.setNamedSeed(seedName, *newSeeds) |
235 |
< |
preserveSeedList.append(seedName) |
183 |
> |
randSvc = RandomNumberServiceHelper(ranGenerator) |
184 |
|
|
185 |
< |
# Randomize remaining seeds |
186 |
< |
randSvc.populate(*preserveSeedList) |
187 |
< |
else: |
188 |
< |
print "Neither old nor new seed format found!" |
185 |
> |
incrementSeedList = [] |
186 |
> |
preserveSeedList = [] |
187 |
> |
|
188 |
> |
if incrementSeeds: |
189 |
> |
incrementSeedList = incrementSeeds.split(',') |
190 |
> |
if preserveSeeds: |
191 |
> |
preserveSeedList = preserveSeeds.split(',') |
192 |
> |
|
193 |
> |
# Increment requested seed sets |
194 |
> |
for seedName in incrementSeedList: |
195 |
> |
curSeeds = randSvc.getNamedSeed(seedName) |
196 |
> |
newSeeds = [x+nJob for x in curSeeds] |
197 |
> |
randSvc.setNamedSeed(seedName, *newSeeds) |
198 |
> |
preserveSeedList.append(seedName) |
199 |
|
|
200 |
< |
# End version specific code |
200 |
> |
# Randomize remaining seeds |
201 |
> |
randSvc.populate(*preserveSeedList) |
202 |
|
|
203 |
< |
# Write out new config file in one format or the other, FUTURE: Get rid of cfg mode |
203 |
> |
# Write out new config file |
204 |
|
outFile = open(outFileName,"w") |
205 |
< |
if outFileName.endswith('py'): |
206 |
< |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
207 |
< |
outFile.write("import pickle\n") |
208 |
< |
outFile.write("pickledCfg=\"\"\"%s\"\"\"\n" % pickle.dumps(cmsProcess)) |
250 |
< |
outFile.write("process = pickle.loads(pickledCfg)\n") |
251 |
< |
if (debug): |
252 |
< |
print "writeCfg output (May not be exact):" |
253 |
< |
print "import FWCore.ParameterSet.Config as cms" |
254 |
< |
print cmsProcess.dumpPython() |
255 |
< |
else: |
256 |
< |
outFile.write(cfg.data.dumpConfig()) |
257 |
< |
if (debug): |
258 |
< |
print "writeCfg output:" |
259 |
< |
print str(cfg.data.dumpConfig()) |
205 |
> |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
206 |
> |
outFile.write("import pickle\n") |
207 |
> |
outFile.write("pickledCfg=\"\"\"%s\"\"\"\n" % pickle.dumps(cmsProcess)) |
208 |
> |
outFile.write("process = pickle.loads(pickledCfg)\n") |
209 |
|
outFile.close() |
210 |
+ |
if (debug): |
211 |
+ |
print "writeCfg output (May not be exact):" |
212 |
+ |
print "import FWCore.ParameterSet.Config as cms" |
213 |
+ |
print cmsProcess.dumpPython() |
214 |
|
|
215 |
|
|
216 |
|
if __name__ == '__main__' : |