ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/writeCfg.py
(Generate patch)

Comparing COMP/CRAB/python/writeCfg.py (file contents):
Revision 1.22 by ewv, Wed Jul 29 21:20:03 2009 UTC vs.
Revision 1.26.2.1 by ewv, Mon Apr 26 16:13:42 2010 UTC

# Line 17 | Line 17 | import xml.dom.minidom
17   from random import SystemRandom
18  
19   from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface
20 from FWCore.ParameterSet.Config                       import include
20   import FWCore.ParameterSet.Types as CfgTypes
21  
22   MyRandom  = SystemRandom()
# Line 39 | Line 38 | def main(argv) :
38      """
39      writeCfg
40  
41 <    - Read in existing, user supplied cfg or pickled pycfg file
41 >    - Read in existing, user supplied pycfg or pickled pycfg file
42      - Modify job specific parameters based on environment variables and arguments.xml
43 <    - Write out modified cfg or pickled pycfg file
43 >    - Write out pickled pycfg file
44  
45      required parameters: none
46  
# Line 102 | Line 101 | def main(argv) :
101      firstEvent = -1
102      compHEPFirstEvent = 0
103      firstRun   = 0
104 +    # FUTURE: Remove firstRun
105 +    firstLumi  = 0
106  
107      dom = xml.dom.minidom.parse(os.environ['RUNTIME_AREA']+'/arguments.xml')
108  
# Line 115 | Line 116 | def main(argv) :
116                  firstEvent = int(elem.getAttribute("FirstEvent"))
117              if elem.getAttribute("FirstRun"):
118                  firstRun = int(elem.getAttribute("FirstRun"))
119 +            if elem.getAttribute("FirstLumi"):
120 +                firstLumi = int(elem.getAttribute("FirstLumi"))
121  
122              generator      = str(elem.getAttribute('Generator'))
123              inputFiles     = str(elem.getAttribute('InputFiles'))
124              parentFiles    = str(elem.getAttribute('ParentFiles'))
125              lumis          = str(elem.getAttribute('Lumis'))
126  
127 <  # Read Input cfg or python cfg file, FUTURE: Get rid cfg mode
127 >  # Read Input python config file
128  
129 <    if fileName.endswith('py'):
130 <        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:
129 >    handle = open(fileName, 'r')
130 >    try:   # Nested form for Python < 2.5
131          try:
132 <            print "Importing .cfg file"
133 <            cfo = include(fileName)
134 <            cmsProcess = cfo
132 >            print "Importing .py file"
133 >            cfo = imp.load_source("pycfg", fileName, handle)
134 >            cmsProcess = cfo.process
135          except Exception, ex:
136 <            msg =  "The cfg file is not valid, %s\n" % str(ex)
136 >            msg = "Your pycfg file is not valid python: %s" % str(ex)
137              raise ConfigException(msg)
138 +    finally:
139 +        handle.close()
140  
141      cfg = CfgInterface(cmsProcess)
142  
# Line 174 | Line 168 | def main(argv) :
168          inModule.setSecondaryFileNames(*parentFileNames)
169  
170      if lumis:
171 <        lumiRanges = lumis.split(',')
172 <        inModule.setLumisToProcess(*lumiRanges)
171 >        if CMSSW_major < 3: # FUTURE: Can remove this check
172 >            print "Cannot skip lumis for CMSSW 2_x"
173 >        else:
174 >            lumiRanges = lumis.split(',')
175 >            inModule.setLumisToProcess(*lumiRanges)
176  
177      # Pythia parameters
178      if (firstRun):
179          inModule.setFirstRun(firstRun)
180 +    if (firstLumi):
181 +        inModule.setFirstLumi(firstLumi)
182  
183 <    incrementSeedList = []
184 <    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
183 >    # Check if there are random #'s to deal with
184 >    if cfg.data.services.has_key('RandomNumberGeneratorService'):
185          print "RandomNumberGeneratorService found, will attempt to change seeds"
186 +        from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper
187          ranGenerator = cfg.data.services['RandomNumberGeneratorService']
188 <        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)
188 >        randSvc = RandomNumberServiceHelper(ranGenerator)
189  
190 <            # Randomize remaining seeds
191 <            randSvc.populate(*preserveSeedList)
192 <        else:
193 <            print "Neither old nor new seed format found!"
190 >        incrementSeedList = []
191 >        preserveSeedList  = []
192 >
193 >        if incrementSeeds:
194 >            incrementSeedList = incrementSeeds.split(',')
195 >        if preserveSeeds:
196 >            preserveSeedList  = preserveSeeds.split(',')
197 >
198 >        # Increment requested seed sets
199 >        for seedName in incrementSeedList:
200 >            curSeeds = randSvc.getNamedSeed(seedName)
201 >            newSeeds = [x+nJob for x in curSeeds]
202 >            randSvc.setNamedSeed(seedName, *newSeeds)
203 >            preserveSeedList.append(seedName)
204  
205 <      # End version specific code
205 >        # Randomize remaining seeds
206 >        randSvc.populate(*preserveSeedList)
207  
208 <    # Write out new config file in one format or the other, FUTURE: Get rid of cfg mode
208 >    # Write out new config file
209      outFile = open(outFileName,"w")
210 <    if outFileName.endswith('py'):
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))
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())
210 >    outFile.write("import FWCore.ParameterSet.Config as cms\n")
211 >    outFile.write("import pickle\n")
212 >    outFile.write("pickledCfg=\"\"\"%s\"\"\"\n" % pickle.dumps(cmsProcess))
213 >    outFile.write("process = pickle.loads(pickledCfg)\n")
214      outFile.close()
215 +    if (debug):
216 +        print "writeCfg output (May not be exact):"
217 +        print "import FWCore.ParameterSet.Config as cms"
218 +        print cmsProcess.dumpPython()
219  
220  
221   if __name__ == '__main__' :

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines