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.14 by ewv, Fri Dec 7 21:41:20 2007 UTC vs.
Revision 1.30 by ewv, Mon Jul 20 19:33:02 2009 UTC

# Line 3 | Line 3
3   import os
4   import common
5   import imp
6 + import pickle
7 +
8   from crab_util import *
9   from crab_exceptions import *
8 from crab_logger import Logger
10  
11   from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface
12   from FWCore.ParameterSet.DictTypes import SortedKeysDict
# Line 24 | Line 25 | class PsetManipulator:
25          self.pset = pset
26          #convert Pset
27          from FWCore.ParameterSet.Config import include
28 <        common.logger.debug(3,"PsetManipulator::__init__: PSet file = "+self.pset)
29 <        if (self.pset.endswith('py') or self.pset.endswith('pycfg') ):
30 <          handle = open(self.pset, 'r')
31 <          try:   # Nested form for Python < 2.5
28 >        common.logger.debug("PsetManipulator::__init__: PSet file = "+self.pset)
29 >        # FUTURE: Can drop cfg mode for CMSSW < 2_1_x
30 >        if self.pset.endswith('py'):
31 >            handle = open(self.pset, 'r')
32 >            try:   # Nested form for Python < 2.5
33 >                try:
34 >                    self.cfo = imp.load_source("pycfg", self.pset, handle)
35 >                    self.cmsProcess = self.cfo.process
36 >                except Exception, ex:
37 >                    msg = "Your config file is not valid python: %s" % str(ex)
38 >                    raise CrabException(msg)
39 >            finally:
40 >                handle.close()
41 >        else:
42              try:
43 <              self.cfo = imp.load_source("pycfg", self.pset, handle)
43 >                self.cfo = include(self.pset)
44 >                self.cmsProcess = self.cfo
45              except Exception, ex:
46 <              msg = "Your pycfg file is not valid python: %s" % str(ex)
47 <              raise CrabException(msg)
48 <          finally:
49 <              handle.close()
50 <          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)
48 <    def inputModule(self, source):
49 <        """
50 <        Set  vString Filenames key
51 <        """
52 <        # set input module
53 <        inModule = self.cfg.inputSource
54 <        inModule.setFileNames(source)
55 <        return
56 <
57 <    def pythiaSeed(self,seed):
58 <        """
59 <        Set pythia seed key
60 <        """
61 <        ranGenerator = self.cfg.data.services['RandomNumberGeneratorService']
62 <        ranGenerator.sourceSeed = CfgTypes.untracked(CfgTypes.uint32(seed))
63 <        return
64 <
65 <    def vtxSeed(self,vtxSeed):
66 <        """
67 <        Set vtx seed key
68 <        """
69 <        ranGenerator = self.cfg.data.services['RandomNumberGeneratorService']
70 <        ranModules   = ranGenerator.moduleSeeds
71 <        # set seed
72 <        ranModules.VtxSmeared = CfgTypes.untracked(CfgTypes.uint32(vtxSeed))
73 <        return
74 <
75 <    def g4Seed(self,g4Seed):
76 <        """
77 <        Set g4 seed key
78 <        """
79 <        ranGenerator = self.cfg.data.services['RandomNumberGeneratorService']
80 <        ranModules   = ranGenerator.moduleSeeds
81 <        # set seed
82 <        ranModules.g4SimHits = CfgTypes.untracked(CfgTypes.uint32(g4Seed))
83 <        return
84 <
85 <    def mixSeed(self,mixSeed):
86 <        """
87 <        Set mix seed key
88 <        """
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 firstRun
97 <        """
98 <        inModule = self.cfg.inputSource
99 <        inModule.setFirstRun(firstrun)   ## Add Daniele
100 <        return
46 >                msg =  "Your cfg file is not valid, %s\n" % str(ex)
47 >                msg += "  https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabFaq#Problem_with_ParameterSet_parsin\n"
48 >                msg += "  may help you understand the problem."
49 >                raise CrabException(msg)
50 >        self.cfg = CfgInterface(self.cmsProcess)
51  
52      def maxEvent(self, maxEv):
53          """
# Line 108 | Line 58 | class PsetManipulator:
58  
59      def skipEvent(self, skipEv):
60          """
61 <        Set skipEvents
61 >        Set max event in the standalone untracked module
62          """
63 <        inModule = self.cfg.inputSource
114 <        inModule.setSkipEvents(skipEv)   ## Add Daniele
115 <        return
116 <
117 <    def outputModule(self, output):
118 <
119 <        #set output module
120 <        outModule = self.cfg.outputModules['out']
121 <        outModule.setFileName('file:'+str(output))
122 <
63 >        self.cfg.inputSource.setSkipEvents(skipEv)
64          return
65  
66      def psetWriter(self, name):
# Line 127 | Line 68 | class PsetManipulator:
68          Write out modified CMSSW.cfg
69          """
70  
71 <        file1 = open(common.work_space.jobDir()+name,"w")
72 <        file1.write(str(self.cfg))
73 <        file1.close()
71 >        # FUTURE: Can drop cfg mode for CMSSW < 2_1_x
72 >        outFile = open(common.work_space.jobDir()+name,"w")
73 >        if name.endswith('py'):
74 >            outFile.write("import FWCore.ParameterSet.Config as cms\n")
75 >            outFile.write("import pickle\n")
76 >            outFile.write("pickledCfg=\"\"\"%s\"\"\"\n" % pickle.dumps(self.cmsProcess))
77 >            outFile.write("process = pickle.loads(pickledCfg)\n")
78 >        else:
79 >            outFile.write(self.cfg.data.dumpConfig())
80 >        outFile.close()
81  
82          return
83  
84 <    def addCrabFJR(self,name):
85 <        """
86 <        _addCrabFJR_
87 <        add CRAB specific FrameworkJobReport (FJR)
88 <        if a FJR already exists in input CMSSW parameter-set, add a second one.
89 <        This code is not needed for CMSSW >= 1.5.x and is non-functional in CMSSW >= 1.7.x.
90 <        It should be removed at some point in the future.
91 <        """
92 <
93 <        # Check if MessageLogger service already exists in configuration. If not, add it
94 <        svcs = self.cfg.data.services
95 <        if not svcs.has_key('MessageLogger'):
96 <            self.cfg.data.add_(CfgModules.Service("MessageLogger"))
97 <
98 <        messageLogger = self.cfg.data.services['MessageLogger']
99 <
100 <        # Add fwkJobReports to Message logger if it doesn't exist
101 <        if "fwkJobReports" not in messageLogger.parameterNames_():
102 <            messageLogger.fwkJobReports = CfgTypes.untracked(CfgTypes.vstring())
103 <
104 <        # should figure out how to remove "name" if it is there.
105 <
106 <        if name not in messageLogger.fwkJobReports:
107 <            messageLogger.fwkJobReports.append(name)
108 <
109 <        return
84 >    def getTFileService(self):
85 >        """ Get Output filename from TFileService and return it. If not existing, return None """
86 >        if not self.cfg.data.services.has_key('TFileService'):
87 >            return None
88 >        tFileService = self.cfg.data.services['TFileService']
89 >        if "fileName" in tFileService.parameterNames_():
90 >            fileName = getattr(tFileService,'fileName',None).value()
91 >            return fileName
92 >        return None
93 >
94 >    def getPoolOutputModule(self):
95 >        """ Get Output filename from PoolOutputModule and return it. If not existing, return None """
96 >        if not self.cfg.data.outputModules:
97 >            return None
98 >        poolOutputModule = self.cfg.data.outputModules
99 >        # FIXME: Still a "bug" here in that only one name is returned and the POM can be in any order
100 >        for out in poolOutputModule:
101 >            if poolOutputModule[out].type_() != "PoolOutputModule":
102 >                continue
103 >            return poolOutputModule[out].fileName.value()
104 >
105 >    def getBadFilesSetting(self):
106 >        setting = False
107 >        try:
108 >            if self.cfg.data.source.skipBadFiles.value():
109 >                setting = True
110 >        except AttributeError:
111 >            pass # Either no source or no setting of skipBadFiles
112 >        return setting

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines