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.15 by slacapra, Fri Jan 4 17:30:56 2008 UTC vs.
Revision 1.35 by ewv, Mon Apr 5 16:47:36 2010 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 *
7 from crab_logger import Logger
10  
11   from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface
12 + from FWCore.ParameterSet.Config    import include
13   from FWCore.ParameterSet.DictTypes import SortedKeysDict
14 + from FWCore.ParameterSet.Modules   import OutputModule
15   from FWCore.ParameterSet.Modules   import Service
16   from FWCore.ParameterSet.Types     import *
17  
18   import FWCore.ParameterSet.Types   as CfgTypes
19   import FWCore.ParameterSet.Modules as CfgModules
20 + import FWCore.ParameterSet.Config  as cms
21  
22   class PsetManipulator:
23      def __init__(self, pset):
# Line 21 | Line 26 | class PsetManipulator:
26          """
27  
28          self.pset = pset
29 <        #convert Pset
30 <        from FWCore.ParameterSet.Config import include
31 <        common.logger.debug(3,"PsetManipulator::__init__: PSet file = "+self.pset)
32 <        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:
29 >
30 >        common.logger.debug("PsetManipulator::__init__: PSet file = "+self.pset)
31 >        handle = open(self.pset, 'r')
32 >        try:   # Nested form for Python < 2.5
33              try:
34 <                self.cfo = include(self.pset)
35 <                self.cfg = CfgInterface(self.cfo)
34 >                self.cfo = imp.load_source("pycfg", self.pset, handle)
35 >                self.cmsProcess = self.cfo.process
36              except Exception, ex:
37 <                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."
37 >                msg = "Your config file is not valid python: %s" % str(ex)
38                  raise CrabException(msg)
39 <        pass
39 >        finally:
40 >            handle.close()
41  
42 <    def inputModule(self, source):
43 <        """
44 <        Set  vString Filenames key
45 <        """
46 <        # set input module
54 <        inModule = self.cfg.inputSource
55 <        inModule.setFileNames(source)
56 <        return
57 <
58 <    def pythiaSeed(self,seed):
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.setFirstRun(firstrun)   ## Add Daniele
101 <        return
42 >        self.cfg = CfgInterface(self.cmsProcess)
43 >        try: # Quiet the output
44 >            self.cfg.data.MessageLogger.cerr.FwkReport.reportEvery = cms.untracked.int32(100)
45 >        except AttributeError:
46 >            pass
47  
48      def maxEvent(self, maxEv):
49          """
# Line 109 | Line 54 | class PsetManipulator:
54  
55      def skipEvent(self, skipEv):
56          """
57 <        Set skipEvents
57 >        Set max event in the standalone untracked module
58          """
59 <        inModule = self.cfg.inputSource
115 <        inModule.setSkipEvents(skipEv)   ## Add Daniele
116 <        return
117 <
118 <    def outputModule(self, output):
119 <
120 <        #set output module
121 <        outModule = self.cfg.outputModules['out']
122 <        outModule.setFileName('file:'+str(output))
123 <
59 >        self.cfg.inputSource.setSkipEvents(skipEv)
60          return
61  
62      def psetWriter(self, name):
63          """
64 <        Write out modified CMSSW.cfg
64 >        Write out modified CMSSW.py
65          """
66  
67 <        file1 = open(common.work_space.jobDir()+name,"w")
68 <        file1.write(str(self.cfg))
69 <        file1.close()
67 >        pklFileName = common.work_space.jobDir() + name + ".pkl"
68 >        pklFile = open(pklFileName, "wb")
69 >        myPickle = pickle.Pickler(pklFile)
70 >        myPickle.dump(self.cmsProcess)
71 >        pklFile.close()
72 >
73 >        outFile = open(common.work_space.jobDir()+name, "w")
74 >        outFile.write("import FWCore.ParameterSet.Config as cms\n")
75 >        outFile.write("import pickle\n")
76 >        outFile.write("process = pickle.load(open('%s', 'rb'))\n" % (name + ".pkl"))
77 >        outFile.close()
78  
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"))
79  
80 <        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.
80 >        return
81  
82 <        if name not in messageLogger.fwkJobReports:
83 <            messageLogger.fwkJobReports.append(name)
82 >    def getTFileService(self):
83 >        """ Get Output filename from TFileService and return it. If not existing, return None """
84 >        if not self.cfg.data.services.has_key('TFileService'):
85 >            return None
86 >        tFileService = self.cfg.data.services['TFileService']
87 >        if "fileName" in tFileService.parameterNames_():
88 >            fileName = getattr(tFileService,'fileName',None).value()
89 >            return fileName
90 >        return None
91 >
92 >    def getPoolOutputModule(self):
93 >        """ Get Output filename from PoolOutputModule and return it. If not existing, return None """
94 >        outputFinder = PoolOutputFinder()
95 >        for p  in self.cfg.data.endpaths.itervalues():
96 >            p.visit(outputFinder)
97 >        return outputFinder.getList()
98 >
99 >    def getBadFilesSetting(self):
100 >        setting = False
101 >        try:
102 >            if self.cfg.data.source.skipBadFiles.value():
103 >                setting = True
104 >        except AttributeError:
105 >            pass # Either no source or no setting of skipBadFiles
106 >        return setting
107 >
108 > class PoolOutputFinder(object):
109 >
110 >    def __init__(self):
111 >        self._poolList = []
112 >    def enter(self,visitee):
113 >        if isinstance(visitee,OutputModule) and visitee.type_() == "PoolOutputModule":
114 >            filename=visitee.fileName.value().split(":")[-1]
115 >            self._poolList.append(filename)
116 >    def leave(self,visitee):
117 >        pass
118  
119 <        return
119 >    def getList(self):
120 >        return self._poolList

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines