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.4 by ewv, Fri Apr 4 15:18:42 2008 UTC vs.
Revision 1.7 by ewv, Tue May 6 01:09:58 2008 UTC

# Line 49 | Line 49 | def main(argv) :
49    g4Seed         = 0
50    mixSeed        = 0
51    debug          = False
52 +  _MAXINT        = 900000000
53 +  maxSeeds       = 4         # Kludge, maximum # of seeds that any engine takes
54  
55    try:
56      opts, args = getopt.getopt(argv, "", ["debug", "help"])
# Line 85 | Line 87 | def main(argv) :
87  
88   # Optional Parameters
89  
90 <  maxEvents  = int(os.environ.get('MaxEvents','0'))
90 >  maxEvents  = int(os.environ.get('MaxEvents', '0'))
91    skipEvents = int(os.environ.get('SkipEvents','0'))
92 <  inputFiles = os.environ.get('InputFiles','')
93 <  firstRun   = int(os.environ.get('FirstRun','0'))
94 <  nJob       = int(os.environ.get('NJob','0'))
95 <  preserveSeeds = os.environ.get('PreserveSeeds','')
92 >  firstRun   = int(os.environ.get('FirstRun',  '0'))
93 >  nJob       = int(os.environ.get('NJob',      '0'))
94 >
95 >  inputFiles     = os.environ.get('InputFiles','')
96 >  preserveSeeds  = os.environ.get('PreserveSeeds','')
97    incrementSeeds = os.environ.get('IncrementSeeds','')
98  
99 < # Read Input cfg or python cfg file
99 > # Read Input cfg or python cfg file, FUTURE: Get rid cfg mode
100  
101 <  if (fileName.endswith('py') or fileName.endswith('pycfg') ):
101 >  if fileName.endswith('py'):
102      handle = open(fileName, 'r')
103      try:   # Nested form for Python < 2.5
104        try:
105          cfo = imp.load_source("pycfg", fileName, handle)
106 +        cmsProcess = cfo.process
107        except Exception, ex:
108          msg = "Your pycfg file is not valid python: %s" % str(ex)
109          raise "Error: ",msg
110      finally:
111          handle.close()
108    cfg = CfgInterface(cfo.process)
112    else:
113      try:
114        cfo = include(fileName)
115 <      cfg = CfgInterface(cfo)
115 >      cmsProcess = cfo
116      except Exception, ex:
117        msg =  "The cfg file is not valid, %s\n" % str(ex)
118        raise "Error: ",msg
119 <  inModule = cfg.inputSource
119 >  cfg = CfgInterface(cmsProcess)
120  
121    # Set parameters for job
122 +  inModule = cfg.inputSource
123    if maxEvents:
124      cfg.maxEvents.setMaxEventsInput(maxEvents)
125  
# Line 128 | Line 132 | def main(argv) :
132      inputFileNames = inputFiles.split(',')
133      inModule.setFileNames(*inputFileNames)
134  
131  # FUTURE: This function tests the CMSSW version. Can be simplified as we drop support for old versions
135    # Pythia parameters
136    if (firstRun):
137      inModule.setFirstRun(firstRun)
# Line 141 | Line 144 | def main(argv) :
144    if preserveSeeds:
145      preserveSeedList  = preserveSeeds.split(',')
146  
147 <  if CMSSW_major < 3: # True for now, should be < 2 when really ready
148 <  # Treatment for seeds, CMSSW < 2_0_x
147 >  # FUTURE: This function tests the CMSSW version. Can be simplified as we drop support for old versions
148 >  if CMSSW_major < 2 or (CMSSW_major == 2 and CMSSW_minor == 0): # Treatment for seeds, CMSSW < 2_1_x
149      if cfg.data.services.has_key('RandomNumberGeneratorService'):
150        ranGenerator = cfg.data.services['RandomNumberGeneratorService']
151        ranModules   = ranGenerator.moduleSeeds
152  
150      _MAXINT = 900000000
151
153        sourceSeed = int(ranGenerator.sourceSeed.value())
154        if 'sourceSeed' in preserveSeedList:
155          pass
# Line 164 | Line 165 | def main(argv) :
165            setattr(ranGenerator.moduleSeeds,seed,CfgTypes.untracked(CfgTypes.uint32(curValue+nJob)))
166            preserveSeedList.append(seed)
167  
168 <      try:
168 <        seedList = ranGenerator.moduleSeeds.parameters().keys()
169 <      except: # Needed for 1_6_7. Above line is good for 1_6_10
170 <        seedList = ranGenerator.moduleSeeds.parameterNames_()
171 <
172 <      for seed in seedList:
168 >      for seed in ranGenerator.moduleSeeds.parameterNames_():
169          if seed not in preserveSeedList:
170            curSeed = getattr(ranGenerator.moduleSeeds,seed,None)
171            if curSeed:
172              curValue = int(curSeed.value())
173              setattr(ranGenerator.moduleSeeds,seed,CfgTypes.untracked(CfgTypes.uint32(_inst.randint(1,_MAXINT))))
174 <  else:
175 <    # Treatment for  seeds, CMSSW => 2_0_x
176 < #from RandomService import RandomSeedService
181 <
182 <
183 <    # This code not currently working because randSvc is not part of the actual configuration file
174 >  else: # Treatment for  seeds, CMSSW 2_1_x and later
175 >    if cfg.data.services.has_key('RandomNumberGeneratorService'):
176 >      from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper
177  
185    # Translate old format to new format first
186    randSvc = RandomSeedService()
187    try:
178        ranGenerator = cfg.data.services['RandomNumberGeneratorService']
179 <      ranModules   = ranGenerator.moduleSeeds
180 <      for seed in ranGenerator.moduleSeeds.parameters().keys():
181 <        curSeed = getattr(ranGenerator.moduleSeeds,seed,None)
182 <        curValue = int(curSeed.value())
183 <        setattr(randSvc,seed,CfgTypes.PSet())
184 <        curPSet = getattr(randSvc,seed,None)
185 <        curPSet.initialSeed = CfgTypes.untracked(CfgTypes.uint32(curValue))
186 <      del ranGenerator.moduleSeeds # Get rid of seeds in old format
187 < # Doesn't work, filter is false      randSvc.populate()
179 >      randSvc = RandomNumberServiceHelper(ranGenerator)
180 >      # sourceSeed is different from the rest, John says will become "theSource"
181 >      if 'sourceSeed' in preserveSeedList:
182 >        pass
183 >      elif 'sourceSeed' in incrementSeedList:
184 >        sourceSeed = int(ranGenerator.sourceSeed.value())
185 >        ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(sourceSeed+nJob))
186 >      else:
187 >        ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(_inst.randint(1,_MAXINT)))
188  
189 <    except:
190 <      print "Problems converting old seeds to new format"
189 >      # Increment requested seed sets
190 >      for seedName in incrementSeedList:
191 >        curSeeds = randSvc.getNamedSeed(seedName)
192 >        newSeeds = [x+nJob for x in curSeeds]
193 >        randSvc.setNamedSeed(seedName,*newSeeds)
194 >        preserveSeedList.append(seedName)
195  
196 <  # Write out new config file in one format or the other
196 >      # Randomize remaining seeds
197 >      randSvc.populate(*preserveSeedList)
198  
199 +  # End version specific code
200 +
201 +  # Write out new config file in one format or the other, FUTURE: Get rid of cfg mode
202    outFile = open(outFileName,"w")
203 <  if (outFileName.endswith('py') or outFileName.endswith('pycfg') ):
203 >  if outFileName.endswith('py'):
204      outFile.write("import FWCore.ParameterSet.Config as cms\n")
205 <    outFile.write(cfo.dumpPython())
205 >    outFile.write(cmsProcess.dumpPython())
206      if (debug):
207        print "writeCfg output:"
208        print "import FWCore.ParameterSet.Config as cms"
209 <      print cfo.dumpPython()
209 >      print cmsProcess.dumpPython()
210    else:
211      outFile.write(str(cfg))
212      if (debug):
# Line 220 | Line 218 | def main(argv) :
218   if __name__ == '__main__' :
219      exit_status = main(sys.argv[1:])
220      sys.exit(exit_status)
223

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines