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 |
+ |
from crab_logger import Logger |
11 |
+ |
|
12 |
+ |
from ProdCommon.CMSConfigTools.ConfigAPI.CfgInterface import CfgInterface |
13 |
+ |
from FWCore.ParameterSet.DictTypes import SortedKeysDict |
14 |
+ |
from FWCore.ParameterSet.Modules import Service |
15 |
+ |
from FWCore.ParameterSet.Types import * |
16 |
|
|
17 |
< |
from cmsconfig import cmsconfig |
18 |
< |
from CfgInterface import CfgInterface |
17 |
> |
import FWCore.ParameterSet.Types as CfgTypes |
18 |
> |
import FWCore.ParameterSet.Modules as CfgModules |
19 |
|
|
20 |
|
class PsetManipulator: |
21 |
|
def __init__(self, pset): |
22 |
< |
""" |
23 |
< |
Convert Pset in Python format |
15 |
< |
and initialize |
22 |
> |
""" |
23 |
> |
Read in Pset object and initialize |
24 |
|
""" |
25 |
|
|
26 |
|
self.pset = pset |
27 |
|
#convert Pset |
28 |
< |
self.pyPset = os.path.basename(pset) |
29 |
< |
cmd = 'EdmConfigToPython > '+common.work_space.shareDir()+self.pyPset+'py < '+ self.pset |
30 |
< |
exit_code = os.system(cmd) |
31 |
< |
if exit_code != 0 : |
32 |
< |
msg = 'Could not convert '+self.pset+' into a python Dictionary \n' |
33 |
< |
msg1= ' Did you do eval `scramv1 runtime ...` from your CMSSW working area ?' |
34 |
< |
raise CrabException(msg+msg1) |
35 |
< |
pass |
36 |
< |
|
37 |
< |
self.par = file(common.work_space.shareDir()+self.pyPset+'py').read() |
38 |
< |
|
39 |
< |
# get PSet |
40 |
< |
self.cfg = CfgInterface(self.par,True) |
41 |
< |
|
42 |
< |
def inputModule(self, source): |
43 |
< |
""" Clean String FileName if there |
44 |
< |
and add vString Filenames key |
45 |
< |
""" |
46 |
< |
# set input module |
47 |
< |
inModule = self.cfg.inputSource |
48 |
< |
inModule.cleanStringFileNames() ## Add Daniele |
49 |
< |
inModule.setFileNames(source) |
50 |
< |
return |
51 |
< |
|
44 |
< |
def pythiaSeed(self,seed): |
45 |
< |
""" |
46 |
< |
Set pythia seed key |
47 |
< |
""" |
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 |
28 |
> |
from FWCore.ParameterSet.Config import include |
29 |
> |
common.logger.debug(3,"PsetManipulator::__init__: PSet file = "+self.pset) |
30 |
> |
# FUTURE: Can drop cfg mode for CMSSW < 2_1_x |
31 |
> |
if self.pset.endswith('py'): |
32 |
> |
handle = open(self.pset, 'r') |
33 |
> |
try: # Nested form for Python < 2.5 |
34 |
> |
try: |
35 |
> |
self.cfo = imp.load_source("pycfg", self.pset, handle) |
36 |
> |
self.cmsProcess = self.cfo.process |
37 |
> |
except Exception, ex: |
38 |
> |
msg = "Your config file is not valid python: %s" % str(ex) |
39 |
> |
raise CrabException(msg) |
40 |
> |
finally: |
41 |
> |
handle.close() |
42 |
> |
else: |
43 |
> |
try: |
44 |
> |
self.cfo = include(self.pset) |
45 |
> |
self.cmsProcess = self.cfo |
46 |
> |
except Exception, ex: |
47 |
> |
msg = "Your cfg file is not valid, %s\n" % str(ex) |
48 |
> |
msg += " https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabFaq#Problem_with_ParameterSet_parsin\n" |
49 |
> |
msg += " may help you understand the problem." |
50 |
> |
raise CrabException(msg) |
51 |
> |
self.cfg = CfgInterface(self.cmsProcess) |
52 |
|
|
53 |
|
def maxEvent(self, maxEv): |
54 |
< |
""" """ |
55 |
< |
# set input module |
56 |
< |
inModule = self.cfg.inputSource |
57 |
< |
inModule.cleanMaxEvent() |
74 |
< |
inModule.setMaxEvents(maxEv) ## Add Daniele |
54 |
> |
""" |
55 |
> |
Set max event in the standalone untracked module |
56 |
> |
""" |
57 |
> |
self.cfg.maxEvents.setMaxEventsInput(maxEv) |
58 |
|
return |
59 |
|
|
60 |
|
def skipEvent(self, skipEv): |
61 |
< |
""" """ |
62 |
< |
# set input module |
63 |
< |
inModule = self.cfg.inputSource |
64 |
< |
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 |
< |
|
61 |
> |
""" |
62 |
> |
Set max event in the standalone untracked module |
63 |
> |
""" |
64 |
> |
self.cfg.inputSource.setSkipEvents(skipEv) |
65 |
|
return |
66 |
|
|
67 |
|
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): |
68 |
|
""" |
69 |
< |
|
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 |
< |
|
69 |
> |
Write out modified CMSSW.cfg |
70 |
|
""" |
71 |
|
|
72 |
< |
# check if MessageLogger service already exist in configuration, if not, add it |
73 |
< |
if not "MessageLogger" in self.cfg.cmsConfig.serviceNames() : |
74 |
< |
self.cfg.cmsConfig.psdata['services']['MessageLogger'] = { |
75 |
< |
'@classname': ('string', 'tracked', 'MessageLogger'), |
76 |
< |
} |
77 |
< |
|
78 |
< |
# get MessageLogger service |
79 |
< |
loggerSvc = self.cfg.cmsConfig.service("MessageLogger") |
72 |
> |
# FUTURE: Can drop cfg mode for CMSSW < 2_1_x |
73 |
> |
outFile = open(common.work_space.jobDir()+name,"w") |
74 |
> |
if name.endswith('py'): |
75 |
> |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
76 |
> |
outFile.write("import pickle\n") |
77 |
> |
outFile.write("pickledCfg=\"\"\"%s\"\"\"\n" % pickle.dumps(self.cmsProcess)) |
78 |
> |
outFile.write("process = pickle.loads(pickledCfg)\n") |
79 |
> |
else: |
80 |
> |
outFile.write(self.cfg.data.dumpConfig()) |
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 |
> |
if not self.cfg.data.outputModules: |
98 |
> |
return None |
99 |
> |
poolOutputModule = self.cfg.data.outputModules |
100 |
> |
for out in poolOutputModule: |
101 |
> |
return poolOutputModule[out].fileName.value() |
102 |
|
|
131 |
– |
# check that default is taken for CRAB FJR configuration and any user specific is removed |
132 |
– |
if loggerSvc.has_key(name): |
133 |
– |
del loggerSvc[name] |