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.28 by ewv, Wed May 20 16:37:01 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 *
10   from crab_logger import Logger
# Line 25 | 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 <        if (self.pset.endswith('py') or self.pset.endswith('pycfg') ):
31 <          handle = open(self.pset, 'r')
32 <          try:   # Nested form for Python < 2.5
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 = imp.load_source("pycfg", self.pset, handle)
44 >                self.cfo = include(self.pset)
45 >                self.cmsProcess = self.cfo
46              except Exception, ex:
47 <              msg = "Your pycfg file is not valid python: %s" % str(ex)
48 <              raise CrabException(msg)
49 <          finally:
50 <              handle.close()
51 <          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
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 108 | 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
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 <
64 >        self.cfg.inputSource.setSkipEvents(skipEv)
65          return
66  
67      def psetWriter(self, name):
# Line 127 | 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