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 cmsconfig import cmsconfig |
12 |
< |
from CfgInterface import CfgInterface |
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 |
> |
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 |
< |
msg1= ' Did you do eval `scramv1 runtime ...` from your CMSSW working area ?' |
33 |
< |
raise CrabException(msg+msg1) |
34 |
< |
pass |
35 |
< |
|
36 |
< |
self.par = file(common.work_space.shareDir()+self.pyPset+'py').read() |
37 |
< |
|
38 |
< |
# get PSet |
39 |
< |
self.cfg = CfgInterface(self.par,True) |
40 |
< |
|
41 |
< |
def inputModule(self, source): |
42 |
< |
""" Clean String FileName if there |
43 |
< |
and add vString Filenames key |
44 |
< |
""" |
45 |
< |
# set input module |
46 |
< |
inModule = self.cfg.inputSource |
47 |
< |
inModule.cleanStringFileNames() ## Add Daniele |
48 |
< |
inModule.setFileNames(source) |
49 |
< |
return |
43 |
< |
|
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 |
27 |
> |
from FWCore.ParameterSet.Config import include |
28 |
> |
common.logger.debug(3,"PsetManipulator::__init__: PSet file = "+self.pset) |
29 |
> |
if self.pset.endswith('py'): |
30 |
> |
handle = open(self.pset, 'r') |
31 |
> |
try: # Nested form for Python < 2.5 |
32 |
> |
try: |
33 |
> |
self.cfo = imp.load_source("pycfg", self.pset, handle) |
34 |
> |
self.cmsProcess = self.cfo.process |
35 |
> |
except Exception, ex: |
36 |
> |
msg = "Your config file is not valid python: %s" % str(ex) |
37 |
> |
raise CrabException(msg) |
38 |
> |
finally: |
39 |
> |
handle.close() |
40 |
> |
else: |
41 |
> |
try: |
42 |
> |
self.cfo = include(self.pset) |
43 |
> |
self.cmsProcess = self.cfo |
44 |
> |
except Exception, ex: |
45 |
> |
msg = "Your cfg file is not valid, %s\n" % str(ex) |
46 |
> |
msg += " https://twiki.cern.ch/twiki/bin/view/CMS/CrabFaq#Problem_with_ParameterSet_parsin\n" |
47 |
> |
msg += " may help you understand the problem." |
48 |
> |
raise CrabException(msg) |
49 |
> |
self.cfg = CfgInterface(self.cmsProcess) |
50 |
|
|
51 |
|
def maxEvent(self, maxEv): |
52 |
< |
""" """ |
53 |
< |
# set input module |
54 |
< |
inModule = self.cfg.inputSource |
55 |
< |
inModule.cleanMaxEvent() |
74 |
< |
inModule.setMaxEvents(maxEv) ## Add Daniele |
75 |
< |
return |
76 |
< |
|
77 |
< |
def skipEvent(self, skipEv): |
78 |
< |
""" """ |
79 |
< |
# set input module |
80 |
< |
inModule = self.cfg.inputSource |
81 |
< |
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 |
< |
|
52 |
> |
""" |
53 |
> |
Set max event in the standalone untracked module |
54 |
> |
""" |
55 |
> |
self.cfg.maxEvents.setMaxEventsInput(maxEv) |
56 |
|
return |
57 |
|
|
58 |
|
def psetWriter(self, name): |
59 |
+ |
""" |
60 |
+ |
Write out modified CMSSW.cfg |
61 |
+ |
""" |
62 |
|
|
63 |
< |
configObject = cmsconfig(str(self.cfg)) |
64 |
< |
|
65 |
< |
file1 = open(common.work_space.jobDir()+name,"w") |
66 |
< |
file1.write(str(configObject.asConfigurationString())) |
67 |
< |
file1.close() |
63 |
> |
# FUTURE: Can drop cfg mode for CMSSW < 2_1_x |
64 |
> |
outFile = open(common.work_space.jobDir()+name,"w") |
65 |
> |
if name.endswith('py'): |
66 |
> |
outFile.write("import FWCore.ParameterSet.Config as cms\n") |
67 |
> |
outFile.write(self.cmsProcess.dumpPython()) |
68 |
> |
else: |
69 |
> |
outFile.write(str(self.cfg)) |
70 |
> |
outFile.close() |
71 |
|
|
72 |
|
return |
73 |
|
|
74 |
|
def addCrabFJR(self,name): |
75 |
|
""" |
105 |
– |
|
76 |
|
_addCrabFJR_ |
107 |
– |
|
77 |
|
add CRAB specific FrameworkJobReport (FJR) |
78 |
+ |
if a FJR already exists in input CMSSW parameter-set, add a second one. |
79 |
+ |
This code is not needed for CMSSW >= 1.5.x and is non-functional in CMSSW >= 1.7.x. |
80 |
+ |
It should be removed at some point in the future. |
81 |
+ |
""" |
82 |
|
|
83 |
< |
if already a FJR exist in input CMSSW parameter-set, add a second one |
83 |
> |
# Check if MessageLogger service already exists in configuration. If not, add it |
84 |
> |
svcs = self.cfg.data.services |
85 |
> |
if not svcs.has_key('MessageLogger'): |
86 |
> |
self.cfg.data.add_(CfgModules.Service("MessageLogger")) |
87 |
|
|
88 |
< |
""" |
88 |
> |
messageLogger = self.cfg.data.services['MessageLogger'] |
89 |
> |
|
90 |
> |
# Add fwkJobReports to Message logger if it doesn't exist |
91 |
> |
if "fwkJobReports" not in messageLogger.parameterNames_(): |
92 |
> |
messageLogger.fwkJobReports = CfgTypes.untracked(CfgTypes.vstring()) |
93 |
> |
|
94 |
> |
# should figure out how to remove "name" if it is there. |
95 |
> |
|
96 |
> |
if name not in messageLogger.fwkJobReports: |
97 |
> |
messageLogger.fwkJobReports.append(name) |
98 |
> |
|
99 |
> |
return |
100 |
> |
|
101 |
> |
def getTFileService(self): |
102 |
> |
""" Get Output filename from TFileService and return it. If not existing, return None """ |
103 |
> |
if not self.cfg.data.services.has_key('TFileService'): |
104 |
> |
return None |
105 |
> |
tFileService = self.cfg.data.services['TFileService'] |
106 |
> |
if "fileName" in tFileService.parameterNames_(): |
107 |
> |
fileName = getattr(tFileService,'fileName',None).value() |
108 |
> |
return fileName |
109 |
> |
return None |
110 |
> |
|
111 |
> |
def getPoolOutputModule(self): |
112 |
> |
""" Get Output filename from PoolOutputModule and return it. If not existing, return None """ |
113 |
> |
return None |
114 |
> |
# if not self.cfg.data.outputModules: |
115 |
> |
# return None |
116 |
> |
# poolOutputModule = self.cfg.data.outputModules |
117 |
> |
# for out in poolOutputModule: |
118 |
> |
# print out |
119 |
> |
# fileName = poolOutputModule.getattr(poolOutputModule.values()[0],'fileName', None) |
120 |
> |
# return fileName |
121 |
|
|
114 |
– |
# check if MessageLogger service already exist in configuration, if not, add it |
115 |
– |
if not "MessageLogger" in self.cfg.cmsConfig.serviceNames() : |
116 |
– |
self.cfg.cmsConfig.psdata['services']['MessageLogger'] = { |
117 |
– |
'@classname': ('string', 'tracked', 'MessageLogger'), |
118 |
– |
} |
119 |
– |
|
120 |
– |
# get MessageLogger service |
121 |
– |
loggerSvc = self.cfg.cmsConfig.service("MessageLogger") |
122 |
– |
|
123 |
– |
# check if FJR is in MessageLogger service configuration, if not, add it |
124 |
– |
if not loggerSvc.has_key("fwkJobReports"): |
125 |
– |
loggerSvc['fwkJobReports'] = ("vstring", "untracked", []) |
126 |
– |
|
127 |
– |
# check if crab FJR configuration is in MessageLogger configuration, if not, add it |
128 |
– |
if not '\"'+name+'\"' in loggerSvc['fwkJobReports'][2] : |
129 |
– |
loggerSvc['fwkJobReports'][2].append('\"'+name+'\"') |
130 |
– |
|
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] |