1 |
|
#!/usr/bin/env python |
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 * |
10 |
|
|
11 |
< |
from cmsconfig import cmsconfig |
12 |
< |
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 |
15 |
< |
and initialize |
24 |
> |
""" |
25 |
> |
Read in Pset object and initialize |
26 |
|
""" |
27 |
|
|
28 |
|
self.pset = pset |
19 |
– |
#convert Pset |
20 |
– |
self.pyPset = os.path.basename(pset) |
21 |
– |
cmd = 'EdmConfigToPython > '+common.work_space.shareDir()+self.pyPset+'py < '+ self.pset |
22 |
– |
cmd_out = runCommand(cmd) |
23 |
– |
if cmd_out != '': |
24 |
– |
msg = 'Could not convert Pset.cfg into python Dictionary \n' |
25 |
– |
msg1= ' Did you do eval `scramv1 runtime ...` from your CMSSW working area ?' |
26 |
– |
raise CrabException(msg+msg1) |
27 |
– |
pass |
28 |
– |
|
29 |
– |
self.par = file(common.work_space.shareDir()+self.pyPset+'py').read() |
29 |
|
|
30 |
< |
# get PSet |
31 |
< |
self.cfg = CfgInterface(self.par,True) |
32 |
< |
|
33 |
< |
def inputModule(self, source): |
34 |
< |
""" Clean String FileName if there |
35 |
< |
and add vString Filenames key |
36 |
< |
""" |
37 |
< |
# set input module |
38 |
< |
inModule = self.cfg.inputSource |
39 |
< |
inModule.cleanStringFileNames() ## Add Daniele |
40 |
< |
inModule.setFileNames(source) |
41 |
< |
return |
42 |
< |
|
43 |
< |
def pythiaSeed(self,seed): |
44 |
< |
""" |
45 |
< |
Set pythia seed key |
46 |
< |
""" |
48 |
< |
# set seed |
49 |
< |
inModule = self.cfg.inputSource |
50 |
< |
inModule.setPythiaSeed(self.cfg,seed) |
51 |
< |
return |
52 |
< |
|
53 |
< |
def pythiaSeedVtx(self,vtxSeed): |
54 |
< |
""" |
55 |
< |
Set vtx seed key |
56 |
< |
""" |
57 |
< |
# set seed |
58 |
< |
inModule = self.cfg.inputSource |
59 |
< |
inModule.setPythiaVtxSeed(self.cfg,vtxSeed) |
60 |
< |
return |
61 |
< |
|
62 |
< |
def pythiaFirstRun(self, firstrun): |
63 |
< |
""" """ |
64 |
< |
# set input module |
65 |
< |
inModule = self.cfg.inputSource |
66 |
< |
inModule.setFirstRun(firstrun) ## Add Daniele |
67 |
< |
return |
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 |
47 |
|
|
48 |
|
def maxEvent(self, maxEv): |
49 |
< |
""" """ |
50 |
< |
# set input module |
51 |
< |
inModule = self.cfg.inputSource |
52 |
< |
inModule.cleanMaxEvent() |
74 |
< |
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() |
82 |
< |
inModule.setSkipEvents(skipEv) ## Add Daniele |
83 |
< |
return |
84 |
< |
|
85 |
< |
def outputModule(self, output): |
86 |
< |
|
87 |
< |
#set output module |
88 |
< |
outModule = self.cfg.outputModules['out'] |
89 |
< |
outModule.setFileName('file:'+str(output)) |
90 |
< |
|
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): |
94 |
– |
|
95 |
– |
configObject = cmsconfig(str(self.cfg)) |
96 |
– |
|
97 |
– |
file1 = open(common.work_space.jobDir()+name,"w") |
98 |
– |
file1.write(str(configObject.asConfigurationString())) |
99 |
– |
file1.close() |
100 |
– |
|
101 |
– |
return |
102 |
– |
|
103 |
– |
def addCrabFJR(self,name): |
63 |
|
""" |
64 |
< |
|
106 |
< |
_addCrabFJR_ |
107 |
< |
|
108 |
< |
add CRAB specific FrameworkJobReport (FJR) |
109 |
< |
|
110 |
< |
if already a FJR exist in input CMSSW parameter-set, add a second one |
111 |
< |
|
64 |
> |
Write out modified CMSSW.py |
65 |
|
""" |
66 |
|
|
67 |
< |
# check if MessageLogger service already exist in configuration, if not, add it |
68 |
< |
if not "MessageLogger" in self.cfg.cmsConfig.serviceNames() : |
69 |
< |
self.cfg.cmsConfig.psdata['services']['MessageLogger'] = { |
70 |
< |
'@classname': ('string', 'tracked', 'MessageLogger'), |
71 |
< |
} |
72 |
< |
|
73 |
< |
# get MessageLogger service |
74 |
< |
loggerSvc = self.cfg.cmsConfig.service("MessageLogger") |
67 |
> |
pklFileName=common.work_space.jobDir()+name+".pkl" |
68 |
> |
pklFile = open(pklFileName,"w") |
69 |
> |
myPickle = pickle.Pickler(pklFile) |
70 |
> |
myPickle.dump(self.cmsProcess) |
71 |
> |
pklFile.close() |
72 |
> |
pklFile = open(pklFileName,"rb") |
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("pickledCfg=\"\"\"") |
77 |
> |
outFile.write(pklFile.read()) |
78 |
> |
outFile.write("\"\"\"\n") |
79 |
> |
outFile.write("process = pickle.loads(pickledCfg)\n") |
80 |
> |
pklFile.close() |
81 |
> |
outFile.close() |
82 |
|
|
83 |
< |
# check if FJR is in MessageLogger service configuration, if not, add it |
124 |
< |
if not loggerSvc.has_key("fwkJobReports"): |
125 |
< |
loggerSvc['fwkJobReports'] = ("vstring", "untracked", []) |
83 |
> |
return |
84 |
|
|
85 |
< |
# check if crab FJR configuration is in MessageLogger configuration, if not, add it |
86 |
< |
if not '\"'+name+'\"' in loggerSvc['fwkJobReports'][2] : |
87 |
< |
loggerSvc['fwkJobReports'][2].append('\"'+name+'\"') |
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 |
> |
outputFinder = PoolOutputFinder() |
98 |
> |
for p in self.cfg.data.endpaths.itervalues(): |
99 |
> |
p.visit(outputFinder) |
100 |
> |
return outputFinder.getList() |
101 |
> |
|
102 |
> |
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 |
110 |
> |
|
111 |
> |
class PoolOutputFinder(object): |
112 |
> |
|
113 |
> |
def __init__(self): |
114 |
> |
self._poolList = [] |
115 |
> |
def enter(self,visitee): |
116 |
> |
if isinstance(visitee,OutputModule) and visitee.type_() == "PoolOutputModule": |
117 |
> |
self._poolList.append(visitee.fileName.value()) |
118 |
> |
def leave(self,visitee): |
119 |
> |
pass |
120 |
|
|
121 |
< |
# check that default is taken for CRAB FJR configuration and any user specific is removed |
122 |
< |
if loggerSvc.has_key(name): |
133 |
< |
del loggerSvc[name] |
121 |
> |
def getList(self): |
122 |
> |
return self._poolList |