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.11 by slacapra, Thu May 10 13:47:06 2007 UTC vs.
Revision 1.26 by ewv, Tue Feb 24 16:19:25 2009 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2 <                                                                                                                                                            
2 >
3   import os
4   import common
5 + import imp
6 +
7   from crab_util import *
8   from crab_exceptions import *
9 + from crab_logger import Logger
10 +
11 + from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface
12 + from FWCore.ParameterSet.DictTypes import SortedKeysDict
13 + from FWCore.ParameterSet.Modules   import Service
14 + from FWCore.ParameterSet.Types     import *
15  
16 < from cmsconfig import cmsconfig
17 < from CfgInterface import CfgInterface
16 > import FWCore.ParameterSet.Types   as CfgTypes
17 > import FWCore.ParameterSet.Modules as CfgModules
18  
19   class PsetManipulator:
20      def __init__(self, pset):
21 <        """
22 <        Convert Pset in Python format  
15 <        and initialize  
21 >        """
22 >        Read in Pset object and initialize
23          """
24  
25          self.pset = pset
26          #convert Pset
27 <        self.pyPset = os.path.basename(pset)  
28 <        cmd = 'EdmConfigToPython > '+common.work_space.shareDir()+self.pyPset+'py < '+ self.pset
29 <        exit_code = os.system(cmd)
30 <        if exit_code != 0 :
31 <            msg = 'Could not convert '+self.pset+' into a python Dictionary \n'
32 <            msg += 'Failed to execute \n'+cmd+'\n'
33 <            msg += 'Exit code : '+str(exit_code)
34 <
35 <            raise CrabException(msg)
36 <            pass
37 <        
38 <        self.par = file(common.work_space.shareDir()+self.pyPset+'py').read()
39 <
40 <        # get PSet
41 <        self.cfg = CfgInterface(self.par,True)
42 <
43 <    def inputModule(self, source):
44 <        """ Clean  String FileName if there
45 <            and add  vString Filenames key
46 <        """
47 <        # set input module
48 <        inModule = self.cfg.inputSource
49 <        inModule.cleanStringFileNames() ## Add Daniele
50 <        inModule.setFileNames(source)
44 <        return
45 <  
46 <    def pythiaSeed(self,seed):
47 <        """
48 <        Set pythia seed key
49 <        """
50 <        # set seed
51 <        inModule = self.cfg.inputSource
52 <        inModule.setPythiaSeed(self.cfg,seed)
53 <        return
54 <
55 <    def vtxSeed(self,vtxSeed):
56 <        """
57 <        Set vtx seed key
58 <        """
59 <        # set seed
60 <        inModule = self.cfg.inputSource
61 <        inModule.setVtxSeed(self.cfg,vtxSeed)
62 <        return
63 <
64 <    def g4Seed(self,g4Seed):
65 <        """
66 <        Set g4 seed key
67 <        """
68 <        # set seed
69 <        inModule = self.cfg.inputSource
70 <        inModule.setG4Seed(self.cfg, g4Seed)
71 <        return
72 <
73 <    def mixSeed(self,mixSeed):
74 <        """
75 <        Set mix seed key
76 <        """
77 <        # set seed
78 <        inModule = self.cfg.inputSource
79 <        inModule.setMixSeed(self.cfg, mixSeed)
80 <        return
81 <
82 <    def pythiaFirstRun(self, firstrun):
83 <        """ """
84 <        # set input module
85 <        inModule = self.cfg.inputSource
86 <        inModule.setFirstRun(firstrun)   ## Add Daniele
87 <        return
27 >        from FWCore.ParameterSet.Config import include
28 >        common.logger.debug(3,"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 = include(self.pset)
44 >                self.cmsProcess = self.cfo
45 >            except Exception, ex:
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 <        """ """
54 <        # set input module
55 <        inModule = self.cfg.inputSource
56 <        inModule.cleanMaxEvent()  
94 <        inModule.setMaxEvents(maxEv)   ## Add Daniele
53 >        """
54 >        Set max event in the standalone untracked module
55 >        """
56 >        self.cfg.maxEvents.setMaxEventsInput(maxEv)
57          return
58  
59      def skipEvent(self, skipEv):
60 <        """ """
61 <        # set input module
62 <        inModule = self.cfg.inputSource
63 <        inModule.cleanSkipEvent()
102 <        inModule.setSkipEvents(skipEv)   ## Add Daniele
103 <        return
104 <
105 <    def outputModule(self, output):
106 <
107 <        #set output module
108 <        outModule = self.cfg.outputModules['out']
109 <        outModule.setFileName('file:'+str(output))
110 <
60 >        """
61 >        Set max event in the standalone untracked module
62 >        """
63 >        self.cfg.inputSource.setSkipEvents(skipEv)
64          return
65  
66      def psetWriter(self, name):
114
115        configObject = cmsconfig(str(self.cfg))
116
117        file1 = open(common.work_space.jobDir()+name,"w")
118        file1.write(str(configObject.asConfigurationString()))
119        file1.close()
120
121        return
122
123    def addCrabFJR(self,name):
67          """
68 <
126 <        _addCrabFJR_
127 <
128 <        add CRAB specific FrameworkJobReport (FJR)
129 <
130 <        if already a FJR exist in input CMSSW parameter-set, add a second one
131 <
68 >        Write out modified CMSSW.cfg
69          """
70  
71 <        # check if MessageLogger service already exist in configuration, if not, add it
72 <        if not "MessageLogger" in self.cfg.cmsConfig.serviceNames() :
73 <            self.cfg.cmsConfig.psdata['services']['MessageLogger'] = {
74 <                '@classname': ('string', 'tracked', 'MessageLogger'),
75 <                }
76 <            
77 <        # get MessageLogger service
78 <        loggerSvc = self.cfg.cmsConfig.service("MessageLogger")
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 >            try:
76 >                outFile.write(self.cmsProcess.dumpPython())
77 >            except Exception, ex:
78 >                msg =  "Your cfg file is not valid, %s\n" % str(ex)
79 >                msg += "  https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabFaq#Problem_with_ParameterSet_parsin\n"
80 >                msg += "  may help you understand the problem."
81 >                raise CrabException(msg)
82 >
83 >        else:
84 >            outFile.write(self.cfg.data.dumpConfig())
85 >        outFile.close()
86  
87 <        # check if FJR is in MessageLogger service configuration, if not, add it
144 <        if not loggerSvc.has_key("fwkJobReports"):
145 <            loggerSvc['fwkJobReports'] = ("vstring", "untracked", [])
87 >        return
88  
89 <        # check if crab FJR configuration is in MessageLogger configuration, if not, add it
90 <        if not '\"'+name+'\"' in loggerSvc['fwkJobReports'][2] :
91 <            loggerSvc['fwkJobReports'][2].append('\"'+name+'\"')
89 >    def getTFileService(self):
90 >        """ Get Output filename from TFileService and return it. If not existing, return None """
91 >        if not self.cfg.data.services.has_key('TFileService'):
92 >            return None
93 >        tFileService = self.cfg.data.services['TFileService']
94 >        if "fileName" in tFileService.parameterNames_():
95 >            fileName = getattr(tFileService,'fileName',None).value()
96 >            return fileName
97 >        return None
98 >
99 >    def getPoolOutputModule(self):
100 >        """ Get Output filename from PoolOutputModule and return it. If not existing, return None """
101 >        if not self.cfg.data.outputModules:
102 >            return None
103 >        poolOutputModule = self.cfg.data.outputModules
104 >        for out in poolOutputModule:
105 >            return poolOutputModule[out].fileName.value()
106  
151        # check that default is taken for CRAB FJR configuration and any user specific is removed
152        if loggerSvc.has_key(name):
153            del loggerSvc[name]

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines