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

Comparing COMP/CRAB/python/PsetManipulator.py (file contents):
Revision 1.11 by slacapra, Thu May 10 13:47:06 2007 UTC vs.
Revision 1.14 by ewv, Fri Dec 7 21:41:20 2007 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2 <                                                                                                                                                            
2 >
3   import os
4   import common
5 + import imp
6   from crab_util import *
7   from crab_exceptions import *
8 + from crab_logger import Logger
9 +
10 + from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface
11 + from FWCore.ParameterSet.DictTypes import SortedKeysDict
12 + from FWCore.ParameterSet.Modules   import Service
13 + from FWCore.ParameterSet.Types     import *
14  
15 < from cmsconfig import cmsconfig
16 < from CfgInterface import CfgInterface
15 > import FWCore.ParameterSet.Types   as CfgTypes
16 > import FWCore.ParameterSet.Modules as CfgModules
17  
18   class PsetManipulator:
19      def __init__(self, pset):
20 <        """
21 <        Convert Pset in Python format  
15 <        and initialize  
20 >        """
21 >        Read in Pset object and initialize
22          """
23  
24          self.pset = pset
25          #convert Pset
26 <        self.pyPset = os.path.basename(pset)  
27 <        cmd = 'EdmConfigToPython > '+common.work_space.shareDir()+self.pyPset+'py < '+ self.pset
28 <        exit_code = os.system(cmd)
29 <        if exit_code != 0 :
30 <            msg = 'Could not convert '+self.pset+' into a python Dictionary \n'
31 <            msg += 'Failed to execute \n'+cmd+'\n'
32 <            msg += 'Exit code : '+str(exit_code)
33 <
26 >        from FWCore.ParameterSet.Config import include
27 >        common.logger.debug(3,"PsetManipulator::__init__: PSet file = "+self.pset)
28 >        if (self.pset.endswith('py') or self.pset.endswith('pycfg') ):
29 >          handle = open(self.pset, 'r')
30 >          try:   # Nested form for Python < 2.5
31 >            try:
32 >              self.cfo = imp.load_source("pycfg", self.pset, handle)
33 >            except Exception, ex:
34 >              msg = "Your pycfg file is not valid python: %s" % str(ex)
35 >              raise CrabException(msg)
36 >          finally:
37 >              handle.close()
38 >          self.cfg = CfgInterface(self.cfo.process)
39 >        else:
40 >          try:
41 >            self.cfo = include(self.pset)
42 >            self.cfg = CfgInterface(self.cfo)
43 >          except Exception, ex:
44 >            msg =  "Your cfg file is not valid, %s\n" % str(ex)
45 >            msg += "  https://twiki.cern.ch/twiki/bin/view/CMS/CrabFaq#Problem_with_ParameterSet_parsin\n"
46 >            msg += "  may help you understand the problem."
47              raise CrabException(msg)
29            pass
30        
31        self.par = file(common.work_space.shareDir()+self.pyPset+'py').read()
32
33        # get PSet
34        self.cfg = CfgInterface(self.par,True)
35
48      def inputModule(self, source):
49 <        """ Clean  String FileName if there
50 <            and add  vString Filenames key
49 >        """
50 >        Set  vString Filenames key
51          """
52          # set input module
53          inModule = self.cfg.inputSource
42        inModule.cleanStringFileNames() ## Add Daniele
54          inModule.setFileNames(source)
55          return
56 <  
56 >
57      def pythiaSeed(self,seed):
58 <        """
58 >        """
59          Set pythia seed key
60          """
61 <        # set seed
62 <        inModule = self.cfg.inputSource
63 <        inModule.setPythiaSeed(self.cfg,seed)
53 <        return
61 >        ranGenerator = self.cfg.data.services['RandomNumberGeneratorService']
62 >        ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(seed))
63 >        return
64  
65      def vtxSeed(self,vtxSeed):
66 <        """
66 >        """
67          Set vtx seed key
68          """
69 +        ranGenerator = self.cfg.data.services['RandomNumberGeneratorService']
70 +        ranModules   = ranGenerator.moduleSeeds
71          # set seed
72 <        inModule = self.cfg.inputSource
73 <        inModule.setVtxSeed(self.cfg,vtxSeed)
62 <        return
72 >        ranModules.VtxSmeared = CfgTypes.untracked(CfgTypes.uint32(vtxSeed))
73 >        return
74  
75      def g4Seed(self,g4Seed):
76 <        """
76 >        """
77          Set g4 seed key
78          """
79 +        ranGenerator = self.cfg.data.services['RandomNumberGeneratorService']
80 +        ranModules   = ranGenerator.moduleSeeds
81          # set seed
82 <        inModule = self.cfg.inputSource
83 <        inModule.setG4Seed(self.cfg, g4Seed)
71 <        return
82 >        ranModules.g4SimHits = CfgTypes.untracked(CfgTypes.uint32(g4Seed))
83 >        return
84  
85      def mixSeed(self,mixSeed):
86 <        """
86 >        """
87          Set mix seed key
88          """
89 <        # set seed
90 <        inModule = self.cfg.inputSource
91 <        inModule.setMixSeed(self.cfg, mixSeed)
92 <        return
89 >        ranGenerator = self.cfg.data.services['RandomNumberGeneratorService']
90 >        ranModules   = ranGenerator.moduleSeeds
91 >        ranModules.mix = CfgTypes.untracked(CfgTypes.uint32(mixSeed))
92 >        return
93  
94      def pythiaFirstRun(self, firstrun):
95 <        """ """
96 <        # set input module
95 >        """
96 >        Set firstRun
97 >        """
98          inModule = self.cfg.inputSource
99 <        inModule.setFirstRun(firstrun)   ## Add Daniele
99 >        inModule.setFirstRun(firstrun)   ## Add Daniele
100          return
101  
102      def maxEvent(self, maxEv):
103 <        """ """
104 <        # set input module
105 <        inModule = self.cfg.inputSource
106 <        inModule.cleanMaxEvent()  
94 <        inModule.setMaxEvents(maxEv)   ## Add Daniele
103 >        """
104 >        Set max event in the standalone untracked module
105 >        """
106 >        self.cfg.maxEvents.setMaxEventsInput(maxEv)
107          return
108  
109      def skipEvent(self, skipEv):
110 <        """ """
111 <        # set input module
110 >        """
111 >        Set skipEvents
112 >        """
113          inModule = self.cfg.inputSource
114 <        inModule.cleanSkipEvent()
102 <        inModule.setSkipEvents(skipEv)   ## Add Daniele
114 >        inModule.setSkipEvents(skipEv)   ## Add Daniele
115          return
116  
117      def outputModule(self, output):
# Line 111 | Line 123 | class PsetManipulator:
123          return
124  
125      def psetWriter(self, name):
126 <
127 <        configObject = cmsconfig(str(self.cfg))
126 >        """
127 >        Write out modified CMSSW.cfg
128 >        """
129  
130          file1 = open(common.work_space.jobDir()+name,"w")
131 <        file1.write(str(configObject.asConfigurationString()))
131 >        file1.write(str(self.cfg))
132          file1.close()
133  
134          return
135  
136      def addCrabFJR(self,name):
137          """
125
138          _addCrabFJR_
127
139          add CRAB specific FrameworkJobReport (FJR)
140 +        if a FJR already exists in input CMSSW parameter-set, add a second one.
141 +        This code is not needed for CMSSW >= 1.5.x and is non-functional in CMSSW >= 1.7.x.
142 +        It should be removed at some point in the future.
143 +        """
144  
145 <        if already a FJR exist in input CMSSW parameter-set, add a second one
145 >        # Check if MessageLogger service already exists in configuration. If not, add it
146 >        svcs = self.cfg.data.services
147 >        if not svcs.has_key('MessageLogger'):
148 >            self.cfg.data.add_(CfgModules.Service("MessageLogger"))
149  
150 <        """
150 >        messageLogger = self.cfg.data.services['MessageLogger']
151 >
152 >        # Add fwkJobReports to Message logger if it doesn't exist
153 >        if "fwkJobReports" not in messageLogger.parameterNames_():
154 >            messageLogger.fwkJobReports = CfgTypes.untracked(CfgTypes.vstring())
155 >
156 >        # should figure out how to remove "name" if it is there.
157 >
158 >        if name not in messageLogger.fwkJobReports:
159 >            messageLogger.fwkJobReports.append(name)
160  
161 <        # check if MessageLogger service already exist in configuration, if not, add it
135 <        if not "MessageLogger" in self.cfg.cmsConfig.serviceNames() :
136 <            self.cfg.cmsConfig.psdata['services']['MessageLogger'] = {
137 <                '@classname': ('string', 'tracked', 'MessageLogger'),
138 <                }
139 <            
140 <        # get MessageLogger service
141 <        loggerSvc = self.cfg.cmsConfig.service("MessageLogger")
142 <
143 <        # check if FJR is in MessageLogger service configuration, if not, add it
144 <        if not loggerSvc.has_key("fwkJobReports"):
145 <            loggerSvc['fwkJobReports'] = ("vstring", "untracked", [])
146 <
147 <        # check if crab FJR configuration is in MessageLogger configuration, if not, add it
148 <        if not '\"'+name+'\"' in loggerSvc['fwkJobReports'][2] :
149 <            loggerSvc['fwkJobReports'][2].append('\"'+name+'\"')
150 <
151 <        # check that default is taken for CRAB FJR configuration and any user specific is removed
152 <        if loggerSvc.has_key(name):
153 <            del loggerSvc[name]
161 >        return

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines