1 |
|
#!/usr/bin/env python |
2 |
|
|
3 |
< |
import sys, getopt, string |
3 |
> |
""" |
4 |
> |
Re-write config file and optionally convert to python |
5 |
> |
""" |
6 |
> |
|
7 |
> |
__revision__ = "$Id$" |
8 |
> |
__version__ = "$Revision$" |
9 |
> |
|
10 |
> |
import getopt |
11 |
|
import imp |
12 |
|
import os |
13 |
< |
import random |
13 |
> |
import pickle |
14 |
> |
import sys |
15 |
> |
import xml.dom.minidom |
16 |
> |
|
17 |
|
from random import SystemRandom |
8 |
– |
_inst = SystemRandom() |
18 |
|
|
19 |
|
from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface |
20 |
< |
from FWCore.ParameterSet.DictTypes import SortedKeysDict |
21 |
< |
from FWCore.ParameterSet.Modules import Service |
13 |
< |
from FWCore.ParameterSet.Types import * |
14 |
< |
from FWCore.ParameterSet.Config import include |
20 |
> |
from FWCore.ParameterSet.Config import include |
21 |
> |
import FWCore.ParameterSet.Types as CfgTypes |
22 |
|
|
23 |
< |
import FWCore.ParameterSet.Types as CfgTypes |
17 |
< |
import FWCore.ParameterSet.Modules as CfgModules |
23 |
> |
MyRandom = SystemRandom() |
24 |
|
|
25 |
+ |
class ConfigException(Exception): |
26 |
+ |
""" |
27 |
+ |
Exceptions raised by writeCfg |
28 |
+ |
""" |
29 |
+ |
|
30 |
+ |
def __init__(self, msg): |
31 |
+ |
Exception.__init__(self, msg) |
32 |
+ |
self._msg = msg |
33 |
+ |
return |
34 |
+ |
|
35 |
+ |
def __str__(self): |
36 |
+ |
return self._msg |
37 |
|
|
38 |
|
def main(argv) : |
39 |
< |
""" |
40 |
< |
writeCfg |
39 |
> |
""" |
40 |
> |
writeCfg |
41 |
> |
|
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 |
> |
|
48 |
> |
optional parameters: |
49 |
> |
--help : help |
50 |
> |
--debug : debug statements |
51 |
> |
|
52 |
> |
""" |
53 |
> |
|
54 |
> |
# defaults |
55 |
> |
inputFileNames = None |
56 |
> |
parentFileNames = None |
57 |
> |
debug = False |
58 |
> |
_MAXINT = 900000000 |
59 |
|
|
60 |
< |
- Read in existing, user supplied cfg or pycfg file |
61 |
< |
- Modify job specific parameters based on environment variables |
62 |
< |
- Write out modified cfg or pycfg file |
63 |
< |
|
64 |
< |
required parameters: none |
29 |
< |
|
30 |
< |
optional parameters: |
31 |
< |
--help : help |
32 |
< |
--debug : debug statements |
33 |
< |
|
34 |
< |
""" |
35 |
< |
|
36 |
< |
# defaults |
37 |
< |
maxEvents = 0 |
38 |
< |
skipEvents = 0 |
39 |
< |
inputFileNames = None |
40 |
< |
firstRun = 0 |
41 |
< |
sourceSeed = 0 |
42 |
< |
vtxSeed = 0 |
43 |
< |
g4Seed = 0 |
44 |
< |
mixSeed = 0 |
45 |
< |
debug = False |
46 |
< |
|
47 |
< |
try: |
48 |
< |
opts, args = getopt.getopt(argv, "", ["debug", "help","inputFiles=","maxEvents=","skipEvents="]) |
49 |
< |
except getopt.GetoptError: |
50 |
< |
print main.__doc__ |
51 |
< |
sys.exit(2) |
52 |
< |
|
53 |
< |
# Parse command line parameters |
54 |
< |
for opt, arg in opts : |
55 |
< |
if opt == "--help" : |
56 |
< |
print main.__doc__ |
57 |
< |
sys.exit() |
58 |
< |
elif opt == "--debug" : |
59 |
< |
debug = True |
60 |
< |
elif opt == "--maxEvents": |
61 |
< |
maxEvents = int(arg) |
62 |
< |
elif opt == "--skipEvents": |
63 |
< |
skipEvents = int(arg) |
64 |
< |
elif opt == "--inputFiles": |
65 |
< |
inputFiles = arg |
66 |
< |
inputFiles = inputFiles.replace('\\','') |
67 |
< |
inputFiles = inputFiles.replace('"','') |
68 |
< |
inputFileNames = inputFiles.split(',') |
69 |
< |
elif opt == "--firstRun": |
70 |
< |
firstRun = int(arg) |
71 |
< |
elif opt == "--sourceSeed": |
72 |
< |
sourceSeed = int(arg) |
73 |
< |
elif opt == "--vtxSeed": |
74 |
< |
vtxSeed = int(arg) |
75 |
< |
elif opt == "--g4Seed": |
76 |
< |
g4Seed = int(arg) |
77 |
< |
elif opt == "--mixSeed": |
78 |
< |
mixSeed = int(arg) |
60 |
> |
try: |
61 |
> |
opts, args = getopt.getopt(argv, "", ["debug", "help"]) |
62 |
> |
except getopt.GetoptError: |
63 |
> |
print main.__doc__ |
64 |
> |
sys.exit(2) |
65 |
|
|
66 |
< |
# Parse remaining parameters |
66 |
> |
try: |
67 |
> |
CMSSW = os.environ['CMSSW_VERSION'] |
68 |
> |
parts = CMSSW.split('_') |
69 |
> |
CMSSW_major = int(parts[1]) |
70 |
> |
CMSSW_minor = int(parts[2]) |
71 |
> |
CMSSW_patch = int(parts[3]) |
72 |
> |
except (KeyError, ValueError): |
73 |
> |
msg = "Your environment doesn't specify the CMSSW version or specifies it incorrectly" |
74 |
> |
raise ConfigException(msg) |
75 |
> |
|
76 |
> |
# Parse command line options |
77 |
> |
for opt, arg in opts : |
78 |
> |
if opt == "--help" : |
79 |
> |
print main.__doc__ |
80 |
> |
sys.exit() |
81 |
> |
elif opt == "--debug" : |
82 |
> |
debug = True |
83 |
|
|
84 |
< |
fileName = args[0]; |
85 |
< |
outFileName = args[1]; |
84 |
> |
# Parse remaining parameters |
85 |
> |
try: |
86 |
> |
fileName = args[0] |
87 |
> |
outFileName = args[1] |
88 |
> |
except IndexError: |
89 |
> |
print main.__doc__ |
90 |
> |
sys.exit() |
91 |
> |
|
92 |
> |
# Read in Environment, XML and get optional Parameters |
93 |
> |
|
94 |
> |
nJob = int(os.environ.get('NJob', '0')) |
95 |
> |
preserveSeeds = os.environ.get('PreserveSeeds','') |
96 |
> |
incrementSeeds = os.environ.get('IncrementSeeds','') |
97 |
> |
|
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 |
< |
# Input cfg or python cfg file |
128 |
> |
# Read Input python config file |
129 |
|
|
87 |
– |
if (fileName.endswith('py') or fileName.endswith('pycfg') ): |
130 |
|
handle = open(fileName, 'r') |
131 |
|
try: # Nested form for Python < 2.5 |
132 |
< |
try: |
133 |
< |
cfo = imp.load_source("pycfg", fileName, handle) |
134 |
< |
except Exception, ex: |
135 |
< |
msg = "Your pycfg file is not valid python: %s" % str(ex) |
136 |
< |
raise "Error: ",msg |
132 |
> |
try: |
133 |
> |
print "Importing .py file" |
134 |
> |
cfo = imp.load_source("pycfg", fileName, handle) |
135 |
> |
cmsProcess = cfo.process |
136 |
> |
except Exception, ex: |
137 |
> |
msg = "Your pycfg file is not valid python: %s" % str(ex) |
138 |
> |
raise ConfigException(msg) |
139 |
|
finally: |
140 |
|
handle.close() |
141 |
< |
cfg = CfgInterface(cfo.process) |
142 |
< |
else: |
143 |
< |
try: |
144 |
< |
cfo = include(fileName) |
145 |
< |
cfg = CfgInterface(cfo) |
146 |
< |
except Exception, ex: |
147 |
< |
msg = "The cfg file is not valid, %s\n" % str(ex) |
148 |
< |
raise "Error: ",msg |
149 |
< |
|
150 |
< |
# Set parameters for job |
151 |
< |
|
152 |
< |
inModule = cfg.inputSource |
153 |
< |
|
154 |
< |
cfg.maxEvents.setMaxEventsInput(maxEvents) |
155 |
< |
|
156 |
< |
inModule.setSkipEvents(skipEvents) |
157 |
< |
if (inputFileNames): |
158 |
< |
inModule.setFileNames(*inputFileNames) |
159 |
< |
|
160 |
< |
# Pythia parameters |
161 |
< |
if (firstRun): |
162 |
< |
inModule.setFirstRun(firstRun) |
163 |
< |
if (sourceSeed) : |
164 |
< |
ranGenerator = cfg.data.services['RandomNumberGeneratorService'] |
165 |
< |
ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(sourceSeed)) |
166 |
< |
if (vtxSeed) : |
167 |
< |
ranModules = ranGenerator.moduleSeeds |
168 |
< |
ranModules.VtxSmeared = CfgTypes.untracked(CfgTypes.uint32(vtxSeed)) |
169 |
< |
if (g4Seed) : |
170 |
< |
ranModules = ranGenerator.moduleSeeds |
171 |
< |
ranModules.g4SimHits = CfgTypes.untracked(CfgTypes.uint32(g4Seed)) |
172 |
< |
if (mixSeed) : |
173 |
< |
ranModules = ranGenerator.moduleSeeds |
174 |
< |
ranModules.mix = CfgTypes.untracked(CfgTypes.uint32(mixSeed)) |
175 |
< |
|
176 |
< |
# Randomize all seeds |
177 |
< |
|
178 |
< |
print "There are ",cfg.seedCount()," seeds" |
179 |
< |
seedList = [random.randint(100,200) for i in range(cfg.seedCount)] |
180 |
< |
seedTuple = seedList.tuple() |
181 |
< |
|
182 |
< |
cfg.insertSeeds(*seedTuple) |
183 |
< |
|
184 |
< |
# Write out new config file |
185 |
< |
|
186 |
< |
outFile = open(outFileName,"w") |
187 |
< |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
188 |
< |
outFile.write(cfo.dumpPython()) |
189 |
< |
outFile.close() |
190 |
< |
|
191 |
< |
if (debug): |
192 |
< |
print "writeCfg output:" |
193 |
< |
print "import FWCore.ParameterSet.Config as cms" |
194 |
< |
print cfo.dumpPython() |
141 |
> |
|
142 |
> |
cfg = CfgInterface(cmsProcess) |
143 |
> |
|
144 |
> |
# Set parameters for job |
145 |
> |
print "Setting parameters" |
146 |
> |
inModule = cfg.inputSource |
147 |
> |
if maxEvents: |
148 |
> |
cfg.maxEvents.setMaxEventsInput(maxEvents) |
149 |
> |
|
150 |
> |
if skipEvents: |
151 |
> |
inModule.setSkipEvents(skipEvents) |
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 |
> |
|
162 |
> |
if inputFiles: |
163 |
> |
inputFileNames = inputFiles.split(',') |
164 |
> |
inModule.setFileNames(*inputFileNames) |
165 |
> |
|
166 |
> |
# handle parent files if needed |
167 |
> |
if parentFiles: |
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 |
> |
# 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 |
> |
randSvc = RandomNumberServiceHelper(ranGenerator) |
190 |
> |
|
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 |
> |
# Randomize remaining seeds |
207 |
> |
randSvc.populate(*preserveSeedList) |
208 |
> |
|
209 |
> |
# Write out new config file |
210 |
> |
outFile = open(outFileName,"w") |
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__' : |
223 |
|
exit_status = main(sys.argv[1:]) |
224 |
|
sys.exit(exit_status) |
156 |
– |
|