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.2 by slacapra, Thu Jun 22 16:05:18 2006 UTC vs.
Revision 1.15 by slacapra, Fri Jan 4 17:30:56 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines