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.5 by gutsche, Sun Aug 27 21:10:04 2006 UTC vs.
Revision 1.35 by ewv, Mon Apr 5 16:47:36 2010 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2 <                                                                                                                                                            
3 < import os, sys, commands
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  
11 < # Crabpydir=commands.getoutput('which crab')
12 < # Topdir=string.replace(Crabpydir,'/python/crab','')
13 < # sys.path.append(Topdir+'/PsetCode')
14 <
15 < from cmsconfig import cmsconfig
16 < from CfgInterface import CfgInterface
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):
24 <        """
25 <        Convert Pset in Python format  
19 <        and initialize  
24 >        """
25 >        Read in Pset object and initialize
26          """
27  
28          self.pset = pset
29 <        #convert Pset
30 <        self.pyPset = os.path.basename(pset)  
31 <        cmd = 'EdmConfigToPython > '+common.work_space.shareDir()+self.pyPset+'py < '+ self.pset
32 <        #cmd = 'EdmConfigToPython > '+common.work_space.shareDir()+self.pset+'py < '+ self.pset
33 <        cmd_out = runCommand(cmd)  
34 <        if cmd_out != '':
35 <            msg = 'Could not convert Pset.cfg into python Dictionary \n'
36 <            msg1= '      Did you do eval `scramv1 runtime ...` from your CMSSW working area ?'
37 <            raise CrabException(msg+msg1)
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 = 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 >
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
33        
34        self.par = file(common.work_space.shareDir()+self.pyPset+'py').read()
35       # par = file(common.work_space.shareDir()+self.pset+'py').read()
36
37        # get PSet
38        self.cfg = CfgInterface(self.par,True)
39
40    def inputModule(self, source):
41        """ Clean  String FileName if there
42            and add  vString Filenames key
43        """
44        # set input module
45        inModule = self.cfg.inputSource
46        inModule.cleanStringFileNames() ## Add Daniele
47        inModule.setFileNames(source)
48        return
49  
50    def pythiaSeed(self,seed):
51        """
52        Set pythia seed key
53        """
54        # set seed
55        inModule = self.cfg.inputSource
56        inModule.setPythiaSeed(self.cfg,seed)
57        return
58
59    def pythiaSeedVtx(self,vtxSeed):
60        """
61        Set vtx seed key
62        """
63        # set seed
64        inModule = self.cfg.inputSource
65        inModule.setPythiaVtxSeed(self.cfg,vtxSeed)
66        return
47  
48      def maxEvent(self, maxEv):
49 <        """ """
50 <        # set input module
51 <        inModule = self.cfg.inputSource
52 <        inModule.cleanMaxEvent()  
73 <        inModule.setMaxEvents(maxEv)   ## Add Daniele
49 >        """
50 >        Set max event in the standalone untracked module
51 >        """
52 >        self.cfg.maxEvents.setMaxEventsInput(maxEv)
53          return
54  
55      def skipEvent(self, skipEv):
56 <        """ """
57 <        # set input module
58 <        inModule = self.cfg.inputSource
59 <        inModule.cleanSkipEvent()
81 <        inModule.setSkipEvents(skipEv)   ## Add Daniele
82 <        return
83 <
84 <    def outputModule(self, output):
85 <
86 <        #set output module
87 <        outModule = self.cfg.outputModules['out']
88 <        outModule.setFileName('file:'+str(output))
89 <
56 >        """
57 >        Set max event in the standalone untracked module
58 >        """
59 >        self.cfg.inputSource.setSkipEvents(skipEv)
60          return
61  
62      def psetWriter(self, name):
63 +        """
64 +        Write out modified CMSSW.py
65 +        """
66  
67 <        configObject = cmsconfig(str(self.cfg))
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  
96        file1 = open(common.work_space.jobDir()+name,"w")
97        file1.write(str(configObject.asConfigurationString()))
98        file1.close()
79  
80          return
81 +
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 +    def getList(self):
120 +        return self._poolList

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines