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 pickle |
14 |
+ |
import sys |
15 |
+ |
import xml.dom.minidom |
16 |
+ |
|
17 |
+ |
from random import SystemRandom |
18 |
|
|
19 |
|
from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface |
20 |
< |
from FWCore.ParameterSet.DictTypes import SortedKeysDict |
21 |
< |
from FWCore.ParameterSet.Modules import Service |
22 |
< |
from FWCore.ParameterSet.Types import * |
23 |
< |
from FWCore.ParameterSet.Config import include |
20 |
> |
from FWCore.ParameterSet.Config import include |
21 |
> |
import FWCore.ParameterSet.Types as CfgTypes |
22 |
> |
|
23 |
> |
MyRandom = SystemRandom() |
24 |
|
|
25 |
< |
import FWCore.ParameterSet.Types as CfgTypes |
26 |
< |
import FWCore.ParameterSet.Modules as CfgModules |
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 cfg or pycfg file |
43 |
< |
- Modify job specific parameters based on environment variables |
44 |
< |
- Write out modified cfg or pycfg file |
45 |
< |
|
46 |
< |
required parameters: none |
47 |
< |
|
48 |
< |
optional parameters: |
49 |
< |
--help : help |
50 |
< |
--debug : debug statements |
51 |
< |
|
52 |
< |
""" |
53 |
< |
|
54 |
< |
# defaults |
55 |
< |
maxEvents = 0 |
56 |
< |
skipEvents = 0 |
57 |
< |
inputFileNames = None |
58 |
< |
debug = False |
38 |
< |
|
39 |
< |
try: |
40 |
< |
opts, args = getopt.getopt(argv, "", ["debug", "help","inputFiles=","maxEvents=","skipEvents="]) |
41 |
< |
except getopt.GetoptError: |
42 |
< |
print main.__doc__ |
43 |
< |
sys.exit(2) |
44 |
< |
|
45 |
< |
# Parse command line parameters |
46 |
< |
for opt, arg in opts : |
47 |
< |
if opt == "--help" : |
48 |
< |
print main.__doc__ |
49 |
< |
sys.exit() |
50 |
< |
elif opt == "--debug" : |
51 |
< |
debug = True |
52 |
< |
elif opt == "--maxEvents": |
53 |
< |
maxEvents = int(arg) |
54 |
< |
elif opt == "--skipEvents": |
55 |
< |
skipEvents = int(arg) |
56 |
< |
elif opt == "--inputFiles": |
57 |
< |
inputFiles = arg |
58 |
< |
inputFiles = inputFiles.replace('\\','') |
59 |
< |
inputFiles = inputFiles.replace('"','') |
60 |
< |
inputFileNames = inputFiles.split(',') |
42 |
> |
- Read in existing, user supplied cfg 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 |
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 |
< |
# Parse remaining parameters |
60 |
> |
try: |
61 |
> |
opts, args = getopt.getopt(argv, "", ["debug", "help"]) |
62 |
> |
except getopt.GetoptError: |
63 |
> |
print main.__doc__ |
64 |
> |
sys.exit(2) |
65 |
> |
|
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 |
> |
|
106 |
> |
dom = xml.dom.minidom.parse(os.environ['RUNTIME_AREA']+'/arguments.xml') |
107 |
> |
|
108 |
> |
for elem in dom.getElementsByTagName("Job"): |
109 |
> |
if nJob == int(elem.getAttribute("JobID")): |
110 |
> |
if elem.getAttribute("MaxEvents"): |
111 |
> |
maxEvents = int(elem.getAttribute("MaxEvents")) |
112 |
> |
if elem.getAttribute("SkipEvents"): |
113 |
> |
skipEvents = int(elem.getAttribute("SkipEvents")) |
114 |
> |
if elem.getAttribute("FirstEvent"): |
115 |
> |
firstEvent = int(elem.getAttribute("FirstEvent")) |
116 |
> |
if elem.getAttribute("FirstRun"): |
117 |
> |
firstRun = int(elem.getAttribute("FirstRun")) |
118 |
> |
|
119 |
> |
generator = str(elem.getAttribute('Generator')) |
120 |
> |
inputFiles = str(elem.getAttribute('InputFiles')) |
121 |
> |
parentFiles = str(elem.getAttribute('ParentFiles')) |
122 |
> |
lumis = str(elem.getAttribute('Lumis')) |
123 |
|
|
124 |
< |
# Input cfg or python cfg file |
124 |
> |
# Read Input python config file |
125 |
|
|
69 |
– |
if (fileName.endswith('py') or fileName.endswith('pycfg') ): |
126 |
|
handle = open(fileName, 'r') |
127 |
|
try: # Nested form for Python < 2.5 |
128 |
< |
try: |
129 |
< |
cfo = imp.load_source("pycfg", fileName, handle) |
130 |
< |
except Exception, ex: |
131 |
< |
msg = "Your pycfg file is not valid python: %s" % str(ex) |
132 |
< |
raise "Error: ",msg |
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() |
79 |
– |
cfg = CfgInterface(cfo.process) |
80 |
– |
else: |
81 |
– |
try: |
82 |
– |
cfo = include(fileName) |
83 |
– |
cfg = CfgInterface(cfo) |
84 |
– |
except Exception, ex: |
85 |
– |
msg = "The cfg file is not valid, %s\n" % str(ex) |
86 |
– |
raise "Error: ",msg |
87 |
– |
|
88 |
– |
# Set parameters for job |
89 |
– |
|
90 |
– |
inModule = cfg.inputSource |
91 |
– |
|
92 |
– |
cfg.maxEvents.setMaxEventsInput(maxEvents) |
137 |
|
|
138 |
< |
inModule.setSkipEvents(skipEvents) |
95 |
< |
if (inputFileNames): |
96 |
< |
inModule.setFileNames(*inputFileNames) |
138 |
> |
cfg = CfgInterface(cmsProcess) |
139 |
|
|
140 |
< |
# Write out new config file |
141 |
< |
|
142 |
< |
outFile = open(outFileName,"w") |
143 |
< |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
144 |
< |
outFile.write(cfo.dumpPython()) |
145 |
< |
outFile.close() |
146 |
< |
|
147 |
< |
if (debug): |
148 |
< |
print "writeCfg output:" |
149 |
< |
print "import FWCore.ParameterSet.Config as cms" |
150 |
< |
print cfo.dumpPython() |
140 |
> |
# Set parameters for job |
141 |
> |
print "Setting parameters" |
142 |
> |
inModule = cfg.inputSource |
143 |
> |
if maxEvents: |
144 |
> |
cfg.maxEvents.setMaxEventsInput(maxEvents) |
145 |
> |
|
146 |
> |
if skipEvents: |
147 |
> |
inModule.setSkipEvents(skipEvents) |
148 |
> |
|
149 |
> |
# Set "skip events" for various generators |
150 |
> |
if generator == 'comphep': |
151 |
> |
cmsProcess.source.CompHEPFirstEvent = CfgTypes.int32(firstEvent) |
152 |
> |
elif generator == 'lhe': |
153 |
> |
cmsProcess.source.skipEvents = CfgTypes.untracked(CfgTypes.uint32(firstEvent)) |
154 |
> |
cmsProcess.source.firstEvent = CfgTypes.untracked(CfgTypes.uint32(firstEvent+1)) |
155 |
> |
elif firstEvent != -1: # (Old? Madgraph) |
156 |
> |
cmsProcess.source.firstEvent = CfgTypes.untracked(CfgTypes.uint32(firstEvent)) |
157 |
> |
|
158 |
> |
if inputFiles: |
159 |
> |
inputFileNames = inputFiles.split(',') |
160 |
> |
inModule.setFileNames(*inputFileNames) |
161 |
> |
|
162 |
> |
# handle parent files if needed |
163 |
> |
if parentFiles: |
164 |
> |
parentFileNames = parentFiles.split(',') |
165 |
> |
inModule.setSecondaryFileNames(*parentFileNames) |
166 |
> |
|
167 |
> |
if lumis: |
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 |
> |
# 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 |
> |
randSvc = RandomNumberServiceHelper(ranGenerator) |
184 |
> |
|
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 |
> |
# Randomize remaining seeds |
201 |
> |
randSvc.populate(*preserveSeedList) |
202 |
> |
|
203 |
> |
# Write out new config file |
204 |
> |
outFile = open(outFileName,"w") |
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__' : |
217 |
|
exit_status = main(sys.argv[1:]) |
218 |
|
sys.exit(exit_status) |
114 |
– |
|