ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PsetManipulator.py
Revision: 1.29
Committed: Tue May 26 10:23:01 2009 UTC (15 years, 11 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_6_1_pre1, CRAB_2_6_0, CRAB_2_6_0_pre14, CRAB_2_6_0_pre13, CRAB_2_6_0_pre12, CRAB_2_6_0_pre11, CRAB_2_6_0_pre10, CRAB_2_6_0_pre9, CRAB_2_6_0_pre8, CRAB_2_6_0_pre7, CRAB_2_6_0_pre6, CRAB_2_6_0_pre5, CRAB_2_6_0_pre4, CRAB_2_6_0_pre3
Changes since 1.28: +1 -2 lines
Log Message:
adapting code to logging usage. (crab_logger removed)

File Contents

# User Rev Content
1 gutsche 1.1 #!/usr/bin/env python
2 ewv 1.12
3 slacapra 1.8 import os
4 gutsche 1.1 import common
5 ewv 1.16 import imp
6 ewv 1.27 import pickle
7 ewv 1.16
8 gutsche 1.1 from crab_util import *
9     from crab_exceptions import *
10 ewv 1.12
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 gutsche 1.1
16 ewv 1.12 import FWCore.ParameterSet.Types as CfgTypes
17     import FWCore.ParameterSet.Modules as CfgModules
18 gutsche 1.1
19     class PsetManipulator:
20     def __init__(self, pset):
21 ewv 1.12 """
22     Read in Pset object and initialize
23 gutsche 1.1 """
24    
25     self.pset = pset
26     #convert Pset
27 ewv 1.12 from FWCore.ParameterSet.Config import include
28 spiga 1.29 common.logger.debug("PsetManipulator::__init__: PSet file = "+self.pset)
29 ewv 1.26 # FUTURE: Can drop cfg mode for CMSSW < 2_1_x
30 ewv 1.16 if self.pset.endswith('py'):
31 slacapra 1.15 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 ewv 1.17 self.cmsProcess = self.cfo.process
36 slacapra 1.15 except Exception, ex:
37 ewv 1.16 msg = "Your config file is not valid python: %s" % str(ex)
38 slacapra 1.15 raise CrabException(msg)
39     finally:
40     handle.close()
41     else:
42 ewv 1.14 try:
43 slacapra 1.15 self.cfo = include(self.pset)
44 ewv 1.17 self.cmsProcess = self.cfo
45 ewv 1.14 except Exception, ex:
46 slacapra 1.15 msg = "Your cfg file is not valid, %s\n" % str(ex)
47 slacapra 1.21 msg += " https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabFaq#Problem_with_ParameterSet_parsin\n"
48 slacapra 1.15 msg += " may help you understand the problem."
49     raise CrabException(msg)
50 ewv 1.17 self.cfg = CfgInterface(self.cmsProcess)
51 spiga 1.7
52 gutsche 1.1 def maxEvent(self, maxEv):
53 ewv 1.12 """
54     Set max event in the standalone untracked module
55     """
56     self.cfg.maxEvents.setMaxEventsInput(maxEv)
57 gutsche 1.1 return
58    
59 ewv 1.24 def skipEvent(self, skipEv):
60     """
61     Set max event in the standalone untracked module
62     """
63     self.cfg.inputSource.setSkipEvents(skipEv)
64     return
65    
66 gutsche 1.1 def psetWriter(self, name):
67 ewv 1.12 """
68     Write out modified CMSSW.cfg
69     """
70 gutsche 1.1
71 ewv 1.16 # 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 slacapra 1.18 outFile.write("import FWCore.ParameterSet.Config as cms\n")
75 ewv 1.27 outFile.write("import pickle\n")
76     outFile.write("pickledCfg=\"\"\"%s\"\"\"\n" % pickle.dumps(self.cmsProcess))
77     outFile.write("process = pickle.loads(pickledCfg)\n")
78 ewv 1.16 else:
79 ewv 1.22 outFile.write(self.cfg.data.dumpConfig())
80 ewv 1.16 outFile.close()
81 gutsche 1.1
82     return
83 gutsche 1.6
84 slacapra 1.18 def getTFileService(self):
85     """ Get Output filename from TFileService and return it. If not existing, return None """
86     if not self.cfg.data.services.has_key('TFileService'):
87     return None
88     tFileService = self.cfg.data.services['TFileService']
89     if "fileName" in tFileService.parameterNames_():
90     fileName = getattr(tFileService,'fileName',None).value()
91     return fileName
92     return None
93 ewv 1.22
94 slacapra 1.19 def getPoolOutputModule(self):
95     """ Get Output filename from PoolOutputModule and return it. If not existing, return None """
96 slacapra 1.20 if not self.cfg.data.outputModules:
97     return None
98     poolOutputModule = self.cfg.data.outputModules
99     for out in poolOutputModule:
100     return poolOutputModule[out].fileName.value()
101 slacapra 1.18
102 ewv 1.28 def getBadFilesSetting(self):
103     setting = False
104     try:
105     if self.cfg.data.source.skipBadFiles.value():
106     setting = True
107     except AttributeError:
108     pass # Either no source or no setting of skipBadFiles
109     return setting