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.13 by ewv, Tue Nov 20 17:06:50 2007 UTC vs.
Revision 1.28 by ewv, Wed May 20 16:37:01 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines