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.10 by ewv, Wed May 21 19:27:30 2008 UTC vs.
Revision 1.15 by ewv, Mon Dec 8 21:57:20 2008 UTC

# Line 1 | Line 1
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 sys, getopt
11   import imp
12   import os
6 import random
13   from random import SystemRandom
8 _inst  = SystemRandom()
14  
15   from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface
16 < from FWCore.ParameterSet.DictTypes import SortedKeysDict
17 < from FWCore.ParameterSet.Modules   import Service
13 < from FWCore.ParameterSet.Types     import *
14 < from FWCore.ParameterSet.Config    import include
16 > from FWCore.ParameterSet.Config                       import include
17 > import FWCore.ParameterSet.Types as CfgTypes
18  
19 < import FWCore.ParameterSet.Types   as CfgTypes
17 < import FWCore.ParameterSet.Modules as CfgModules
19 > MyRandom  = SystemRandom()
20  
21   class ConfigException(Exception):
22 +    """
23 +    Exceptions raised by writeCfg
24 +    """
25 +
26      def __init__(self, msg):
27          Exception.__init__(self, msg)
28          self._msg = msg
# Line 26 | Line 32 | class ConfigException(Exception):
32          return self._msg
33  
34   def main(argv) :
35 <  """
36 <  writeCfg
35 >    """
36 >    writeCfg
37  
38 <  - Read in existing, user supplied cfg or pycfg file
39 <  - Modify job specific parameters based on environment variables
40 <  - Write out modified cfg or pycfg file
41 <
42 <  required parameters: none
43 <
44 <  optional parameters:
45 <  --help             :       help
46 <  --debug            :       debug statements
47 <
48 <  """
49 <
50 <  # defaults
51 <  inputFileNames = None
52 <  firstRun       = 0
53 <  sourceSeed     = 0
54 <  vtxSeed        = 0
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"])
57 <  except getopt.GetoptError:
58 <    print main.__doc__
59 <    sys.exit(2)
60 <
61 <  try:
62 <    CMSSW  = os.environ['CMSSW_VERSION']
63 <    parts = CMSSW.split('_')
64 <    CMSSW_major = int(parts[1])
65 <    CMSSW_minor = int(parts[2])
66 <    CMSSW_patch = int(parts[3])
67 <  except KeyError, ValueError:
68 <    msg = "Your environment doesn't specify the CMSSW version or specifies it incorrectly"
69 <    raise ConfigException(msg)
70 <
71 <  # Parse command line options
72 <  for opt, arg in opts :
73 <    if opt  == "--help" :
74 <      print main.__doc__
75 <      sys.exit()
76 <    elif opt == "--debug" :
77 <      debug = True
78 <
79 <  # Parse remaining parameters
80 <
81 <  try:
82 <    fileName    = args[0];
83 <    outFileName = args[1];
84 <  except IndexError:
85 <    print main.__doc__
86 <    sys.exit()
87 <
88 < # Optional Parameters
89 <
90 <  maxEvents  = int(os.environ.get('MaxEvents', '0'))
91 <  skipEvents = int(os.environ.get('SkipEvents','0'))
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, FUTURE: Get rid cfg mode
100 <
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()
112 <  else:
113 <    try:
114 <      print "Importing .cfg file"
115 <      cfo = include(fileName)
116 <      cmsProcess = cfo
117 <    except Exception, ex:
118 <      msg =  "The cfg file is not valid, %s\n" % str(ex)
119 <      raise "Error: ",msg
120 <  print "Getting interface"
121 <  cfg = CfgInterface(cmsProcess)
122 <
123 <  # Set parameters for job
124 <  print "Setting parameters"
125 <  inModule = cfg.inputSource
126 <  if maxEvents:
127 <    cfg.maxEvents.setMaxEventsInput(maxEvents)
128 <
129 <  if skipEvents:
130 <    inModule.setSkipEvents(skipEvents)
131 <
132 <  if inputFiles:
133 <    inputFiles = inputFiles.replace('\\','')
134 <    inputFiles = inputFiles.replace('"','')
135 <    inputFileNames = inputFiles.split(',')
136 <    inModule.setFileNames(*inputFileNames)
137 <
138 <  # Pythia parameters
139 <  if (firstRun):
140 <    inModule.setFirstRun(firstRun)
141 <
142 <  incrementSeedList = []
143 <  preserveSeedList  = []
144 <
145 <  if incrementSeeds:
146 <    incrementSeedList = incrementSeeds.split(',')
147 <  if preserveSeeds:
148 <    preserveSeedList  = preserveSeeds.split(',')
149 <
150 <  # FUTURE: This function tests the CMSSW version and presence of old-style seed specification. Reduce when we drop support for old versions
151 <  if cfg.data.services.has_key('RandomNumberGeneratorService'): # There are random #'s to deal with
152 <    print "RandomNumberGeneratorService found, will attempt to change seeds"
153 <    ranGenerator = cfg.data.services['RandomNumberGeneratorService']
154 <    ranModules   = getattr(ranGenerator,"moduleSeeds",None)
155 <    if ranModules != None:              # Old format present, no matter the CMSSW version
156 <      print "Old-style random number seeds found, will be changed."
157 <      sourceSeed = int(ranGenerator.sourceSeed.value())
158 <      if ('sourceSeed' in preserveSeedList) or ('theSource' in preserveSeedList):
159 <        pass
160 <      elif ('sourceSeed' in incrementSeedList) or ('theSource' in incrementSeedList):
161 <        ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(sourceSeed+nJob))
162 <      else:
163 <        ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(_inst.randint(1,_MAXINT)))
164 <
165 <      for seed in incrementSeedList:
166 <        curSeed = getattr(ranGenerator.moduleSeeds,seed,None)
167 <        if curSeed:
168 <          curValue = int(curSeed.value())
169 <          setattr(ranGenerator.moduleSeeds,seed,CfgTypes.untracked(CfgTypes.uint32(curValue+nJob)))
170 <          preserveSeedList.append(seed)
171 <
172 <      for seed in ranGenerator.moduleSeeds.parameterNames_():
173 <        if seed not in preserveSeedList:
174 <          curSeed = getattr(ranGenerator.moduleSeeds,seed,None)
175 <          if curSeed:
176 <            curValue = int(curSeed.value())
177 <            setattr(ranGenerator.moduleSeeds,seed,CfgTypes.untracked(CfgTypes.uint32(_inst.randint(1,_MAXINT))))
178 <    elif CMSSW_major > 2 or (CMSSW_major == 2 and CMSSW_minor >= 1): # Treatment for  seeds, CMSSW 2_1_x and later
179 <      print "New-style random number seeds found, will be changed."
180 <      from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper
181 <      randSvc = RandomNumberServiceHelper(ranGenerator)
182 <
183 <      # Increment requested seed sets
184 <      for seedName in incrementSeedList:
185 <        curSeeds = randSvc.getNamedSeed(seedName)
186 <        newSeeds = [x+nJob for x in curSeeds]
187 <        randSvc.setNamedSeed(seedName,*newSeeds)
188 <        preserveSeedList.append(seedName)
38 >    - Read in existing, user supplied cfg or pycfg file
39 >    - Modify job specific parameters based on environment variables
40 >    - Write out modified cfg or pycfg file
41 >
42 >    required parameters: none
43 >
44 >    optional parameters:
45 >    --help             :       help
46 >    --debug            :       debug statements
47 >
48 >    """
49 >
50 >    # defaults
51 >    inputFileNames  = None
52 >    parentFileNames = None
53 >    debug           = False
54 >    _MAXINT         = 900000000
55  
56 <      # Randomize remaining seeds
57 <      randSvc.populate(*preserveSeedList)
58 <    else:
59 <      print "Neither old nor new seed format found!"
56 >    try:
57 >        opts, args = getopt.getopt(argv, "", ["debug", "help"])
58 >    except getopt.GetoptError:
59 >        print main.__doc__
60 >        sys.exit(2)
61  
62 <    # End version specific code
62 >    try:
63 >        CMSSW  = os.environ['CMSSW_VERSION']
64 >        parts = CMSSW.split('_')
65 >        CMSSW_major = int(parts[1])
66 >        CMSSW_minor = int(parts[2])
67 >        CMSSW_patch = int(parts[3])
68 >    except (KeyError, ValueError):
69 >        msg = "Your environment doesn't specify the CMSSW version or specifies it incorrectly"
70 >        raise ConfigException(msg)
71 >
72 >    # Parse command line options
73 >    for opt, arg in opts :
74 >        if opt  == "--help" :
75 >            print main.__doc__
76 >            sys.exit()
77 >        elif opt == "--debug" :
78 >            debug = True
79  
80 <  # Write out new config file in one format or the other, FUTURE: Get rid of cfg mode
81 <  outFile = open(outFileName,"w")
82 <  if outFileName.endswith('py'):
83 <    outFile.write("import FWCore.ParameterSet.Config as cms\n")
84 <    outFile.write(cmsProcess.dumpPython())
85 <    if (debug):
86 <      print "writeCfg output:"
87 <      print "import FWCore.ParameterSet.Config as cms"
88 <      print cmsProcess.dumpPython()
89 <  else:
90 <    outFile.write(str(cfg))
91 <    if (debug):
92 <      print "writeCfg output:"
93 <      print str(cfg)
94 <  outFile.close()
80 >    # Parse remaining parameters
81 >    try:
82 >        fileName    = args[0]
83 >        outFileName = args[1]
84 >    except IndexError:
85 >        print main.__doc__
86 >        sys.exit()
87 >
88 >  # Optional Parameters
89 >
90 >    maxEvents  = int(os.environ.get('MaxEvents', '0'))
91 >    skipEvents = int(os.environ.get('SkipEvents','0'))
92 >    firstEvent = int(os.environ.get('FirstEvent','0'))
93 >    compHEPFirstEvent = int(os.environ.get('CompHEPFirstEvent','0'))
94 >    firstRun   = int(os.environ.get('FirstRun',  '0'))
95 >    nJob       = int(os.environ.get('NJob',      '0'))
96 >
97 >    inputFiles     = os.environ.get('InputFiles','')
98 >    parentFiles    = os.environ.get('ParentFiles','')
99 >    preserveSeeds  = os.environ.get('PreserveSeeds','')
100 >    incrementSeeds = os.environ.get('IncrementSeeds','')
101 >
102 >  # Read Input cfg or python cfg file, FUTURE: Get rid cfg mode
103 >
104 >    if fileName.endswith('py'):
105 >        handle = open(fileName, 'r')
106 >        try:   # Nested form for Python < 2.5
107 >            try:
108 >                print "Importing .py file"
109 >                cfo = imp.load_source("pycfg", fileName, handle)
110 >                cmsProcess = cfo.process
111 >            except Exception, ex:
112 >                msg = "Your pycfg file is not valid python: %s" % str(ex)
113 >                raise ConfigException(msg)
114 >        finally:
115 >            handle.close()
116 >    else:
117 >        try:
118 >            print "Importing .cfg file"
119 >            cfo = include(fileName)
120 >            cmsProcess = cfo
121 >        except Exception, ex:
122 >            msg =  "The cfg file is not valid, %s\n" % str(ex)
123 >            raise ConfigException(msg)
124 >
125 >    cfg = CfgInterface(cmsProcess)
126 >
127 >    # Set parameters for job
128 >    print "Setting parameters"
129 >    inModule = cfg.inputSource
130 >    if maxEvents:
131 >        cfg.maxEvents.setMaxEventsInput(maxEvents)
132 >
133 >    if skipEvents:
134 >        inModule.setSkipEvents(skipEvents)
135 >    if firstEvent:
136 >        cmsProcess.source.firstEvent = CfgTypes.untracked(CfgTypes.uint32(firstEvent))
137 >    if compHEPFirstEvent:
138 >        cmsProcess.source.CompHEPFirstEvent = CfgTypes.int32(compHEPFirstEvent)
139 >    if inputFiles:
140 >        inputFiles = inputFiles.replace('\\','')
141 >        inputFiles = inputFiles.replace('"','')
142 >        inputFileNames = inputFiles.split(',')
143 >        inModule.setFileNames(*inputFileNames)
144 >
145 >    # handle parent files if needed
146 >    if parentFiles:
147 >        parentFiles = parentFiles.replace('\\','')
148 >        parentFiles = parentFiles.replace('"','')
149 >        parentFileNames = parentFiles.split(',')
150 >        inModule.setSecondaryFileNames(*parentFileNames)
151 >
152 >    # Pythia parameters
153 >    if (firstRun):
154 >        inModule.setFirstRun(firstRun)
155 >
156 >    incrementSeedList = []
157 >    preserveSeedList  = []
158 >
159 >    if incrementSeeds:
160 >        incrementSeedList = incrementSeeds.split(',')
161 >    if preserveSeeds:
162 >        preserveSeedList  = preserveSeeds.split(',')
163 >
164 >    # FUTURE: This function tests the CMSSW version and presence of old-style seed specification.
165 >    # Reduce when we drop support for old versions
166 >    if cfg.data.services.has_key('RandomNumberGeneratorService'): # There are random #'s to deal with
167 >        print "RandomNumberGeneratorService found, will attempt to change seeds"
168 >        ranGenerator = cfg.data.services['RandomNumberGeneratorService']
169 >        ranModules   = getattr(ranGenerator, "moduleSeeds", None)
170 >        oldSource    = getattr(ranGenerator, "sourceSeed",  None)
171 >        if ranModules != None or oldSource != None:     # Old format present, no matter the CMSSW version
172 >            print "Old-style random number seeds found, will be changed."
173 >            if oldSource != None:
174 >                sourceSeed = int(ranGenerator.sourceSeed.value())
175 >                if ('sourceSeed' in preserveSeedList) or ('theSource' in preserveSeedList):
176 >                    pass
177 >                elif ('sourceSeed' in incrementSeedList) or ('theSource' in incrementSeedList):
178 >                    ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(sourceSeed+nJob))
179 >                else:
180 >                    ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(MyRandom.randint(1, _MAXINT)))
181 >
182 >            for seed in incrementSeedList:
183 >                curSeed = getattr(ranGenerator.moduleSeeds, seed, None)
184 >                if curSeed:
185 >                    curValue = int(curSeed.value())
186 >                    setattr(ranGenerator.moduleSeeds, seed, CfgTypes.untracked(CfgTypes.uint32(curValue+nJob)))
187 >                    preserveSeedList.append(seed)
188 >
189 >            if ranModules != None:
190 >                for seed in ranGenerator.moduleSeeds.parameterNames_():
191 >                    if seed not in preserveSeedList:
192 >                        curSeed = getattr(ranGenerator.moduleSeeds, seed, None)
193 >                        if curSeed:
194 >                            curValue = int(curSeed.value())
195 >                            setattr(ranGenerator.moduleSeeds, seed,
196 >                                    CfgTypes.untracked(CfgTypes.uint32(MyRandom.randint(1,_MAXINT))))
197 >        elif CMSSW_major > 2 or (CMSSW_major == 2 and CMSSW_minor >= 1): # Treatment for  seeds, CMSSW 2_1_x and later
198 >            print "New-style random number seeds found, will be changed."
199 >            from IOMC.RandomEngine.RandomServiceHelper import RandomNumberServiceHelper
200 >            randSvc = RandomNumberServiceHelper(ranGenerator)
201 >
202 >            # Increment requested seed sets
203 >            for seedName in incrementSeedList:
204 >                curSeeds = randSvc.getNamedSeed(seedName)
205 >                newSeeds = [x+nJob for x in curSeeds]
206 >                randSvc.setNamedSeed(seedName, *newSeeds)
207 >                preserveSeedList.append(seedName)
208 >
209 >            # Randomize remaining seeds
210 >            randSvc.populate(*preserveSeedList)
211 >        else:
212 >            print "Neither old nor new seed format found!"
213 >
214 >      # End version specific code
215 >
216 >    # Write out new config file in one format or the other, FUTURE: Get rid of cfg mode
217 >    outFile = open(outFileName,"w")
218 >    if outFileName.endswith('py'):
219 >        outFile.write("import FWCore.ParameterSet.Config as cms\n")
220 >        outFile.write(cmsProcess.dumpPython())
221 >        if (debug):
222 >            print "writeCfg output:"
223 >            print "import FWCore.ParameterSet.Config as cms"
224 >            print cmsProcess.dumpPython()
225 >    else:
226 >        outFile.write(cfg.data.dumpConfig())
227 >        if (debug):
228 >            print "writeCfg output:"
229 >            print str(cfg.data.dumpConfig())
230 >    outFile.close()
231  
232  
233   if __name__ == '__main__' :

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines