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 |
|
|
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 |
|
|
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 |
< |
# Read Input cfg or python cfg file, FUTURE: Get rid cfg mode |
128 |
> |
# Read Input python config file |
129 |
|
|
130 |
< |
if fileName.endswith('py'): |
131 |
< |
handle = open(fileName, 'r') |
127 |
< |
try: # Nested form for Python < 2.5 |
128 |
< |
try: |
129 |
< |
print "Importing .py file" |
130 |
< |
cfo = imp.load_source("pycfg", fileName, handle) |
131 |
< |
cmsProcess = cfo.process |
132 |
< |
except Exception, ex: |
133 |
< |
msg = "Your pycfg file is not valid python: %s" % str(ex) |
134 |
< |
raise ConfigException(msg) |
135 |
< |
finally: |
136 |
< |
handle.close() |
137 |
< |
else: |
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 |
|
|
154 |
|
if generator == 'comphep': |
155 |
|
cmsProcess.source.CompHEPFirstEvent = CfgTypes.int32(firstEvent) |
156 |
|
elif generator == 'lhe': |
157 |
< |
cmsProcess.LHESource.skipEvents = CfgTypes.untracked(CfgTypes.uint32(firstEvent)) |
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 |
|
|
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 = [] |
180 |
< |
|
181 |
< |
if incrementSeeds: |
182 |
< |
incrementSeedList = incrementSeeds.split(',') |
183 |
< |
if preserveSeeds: |
184 |
< |
preserveSeedList = preserveSeeds.split(',') |
185 |
< |
|
186 |
< |
# FUTURE: This function tests the CMSSW version and presence of old-style seed specification. |
187 |
< |
# Reduce when we drop support for old versions |
188 |
< |
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) |
192 |
< |
oldSource = getattr(ranGenerator, "sourceSeed", None) |
193 |
< |
if ranModules != None or oldSource != None: # Old format present, no matter the CMSSW version |
194 |
< |
print "Old-style random number seeds found, will be changed." |
195 |
< |
if oldSource != None: |
196 |
< |
sourceSeed = int(ranGenerator.sourceSeed.value()) |
197 |
< |
if ('sourceSeed' in preserveSeedList) or ('theSource' in preserveSeedList): |
198 |
< |
pass |
199 |
< |
elif ('sourceSeed' in incrementSeedList) or ('theSource' in incrementSeedList): |
200 |
< |
ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(sourceSeed+nJob)) |
201 |
< |
else: |
202 |
< |
ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(MyRandom.randint(1, _MAXINT))) |
203 |
< |
|
204 |
< |
for seed in incrementSeedList: |
205 |
< |
curSeed = getattr(ranGenerator.moduleSeeds, seed, None) |
206 |
< |
if curSeed: |
207 |
< |
curValue = int(curSeed.value()) |
208 |
< |
setattr(ranGenerator.moduleSeeds, seed, CfgTypes.untracked(CfgTypes.uint32(curValue+nJob))) |
209 |
< |
preserveSeedList.append(seed) |
210 |
< |
|
211 |
< |
if ranModules != None: |
212 |
< |
for seed in ranGenerator.moduleSeeds.parameterNames_(): |
213 |
< |
if seed not in preserveSeedList: |
214 |
< |
curSeed = getattr(ranGenerator.moduleSeeds, seed, None) |
215 |
< |
if curSeed: |
216 |
< |
curValue = int(curSeed.value()) |
217 |
< |
setattr(ranGenerator.moduleSeeds, seed, |
218 |
< |
CfgTypes.untracked(CfgTypes.uint32(MyRandom.randint(1,_MAXINT)))) |
219 |
< |
elif CMSSW_major > 2 or (CMSSW_major == 2 and CMSSW_minor >= 1): # Treatment for seeds, CMSSW 2_1_x and later |
220 |
< |
print "New-style random number seeds found, will be changed." |
221 |
< |
from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper |
222 |
< |
randSvc = RandomNumberServiceHelper(ranGenerator) |
223 |
< |
|
224 |
< |
# Increment requested seed sets |
225 |
< |
for seedName in incrementSeedList: |
226 |
< |
curSeeds = randSvc.getNamedSeed(seedName) |
227 |
< |
newSeeds = [x+nJob for x in curSeeds] |
228 |
< |
randSvc.setNamedSeed(seedName, *newSeeds) |
229 |
< |
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 |
> |
pklFileName = outFileName + '.pkl' |
211 |
|
outFile = open(outFileName,"w") |
212 |
< |
if outFileName.endswith('py'): |
213 |
< |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
214 |
< |
outFile.write("import pickle\n") |
243 |
< |
outFile.write("pickledCfg=\"\"\"%s\"\"\"\n" % pickle.dumps(cmsProcess)) |
244 |
< |
outFile.write("process = pickle.loads(pickledCfg)\n") |
245 |
< |
if (debug): |
246 |
< |
print "writeCfg output (May not be exact):" |
247 |
< |
print "import FWCore.ParameterSet.Config as cms" |
248 |
< |
print cmsProcess.dumpPython() |
249 |
< |
else: |
250 |
< |
outFile.write(cfg.data.dumpConfig()) |
251 |
< |
if (debug): |
252 |
< |
print "writeCfg output:" |
253 |
< |
print str(cfg.data.dumpConfig()) |
212 |
> |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
213 |
> |
outFile.write("import pickle\n") |
214 |
> |
outFile.write("process = pickle.load(open('%s', 'rb'))\n" % pklFileName) |
215 |
|
outFile.close() |
216 |
|
|
217 |
+ |
pklFile = open(pklFileName,"wb") |
218 |
+ |
myPickle = pickle.Pickler(pklFile) |
219 |
+ |
myPickle.dump(cmsProcess) |
220 |
+ |
pklFile.close() |
221 |
+ |
|
222 |
+ |
if (debug): |
223 |
+ |
print "writeCfg output (May not be exact):" |
224 |
+ |
print "import FWCore.ParameterSet.Config as cms" |
225 |
+ |
print cmsProcess.dumpPython() |
226 |
+ |
|
227 |
|
|
228 |
|
if __name__ == '__main__' : |
229 |
|
exit_status = main(sys.argv[1:]) |