1 |
|
#!/usr/bin/env python |
2 |
< |
|
3 |
< |
import os, sys, commands |
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 |
< |
# Crabpydir=commands.getoutput('which crab') |
12 |
< |
# Topdir=string.replace(Crabpydir,'/python/crab','') |
13 |
< |
# sys.path.append(Topdir+'/PsetCode') |
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 |
19 |
< |
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 |
< |
#cmd = 'EdmConfigToPython > '+common.work_space.shareDir()+self.pset+'py < '+ self.pset |
30 |
< |
cmd_out = runCommand(cmd) |
31 |
< |
if cmd_out != '': |
32 |
< |
msg = 'Could not convert Pset.cfg into 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 |
< |
# par = file(common.work_space.shareDir()+self.pset+'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 |
47 |
< |
inModule.setFileNames(source) |
48 |
< |
return |
49 |
< |
|
50 |
< |
def pythiaSeed(self,seed,vtxSeed): |
51 |
< |
""" |
52 |
< |
Set pythia seed key |
53 |
< |
""" |
54 |
< |
# set seed |
55 |
< |
inModule = self.cfg.inputSource |
56 |
< |
inModule.setPythiaSeed(self.cfg,seed) |
57 |
< |
inModule.setPythiaVtxSeed(self.cfg,vtxSeed) |
58 |
< |
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/SWGuideCrabFaq#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() |
65 |
< |
inModule.setMaxEvents(maxEv) ## Add Daniele |
52 |
> |
""" |
53 |
> |
Set max event in the standalone untracked module |
54 |
> |
""" |
55 |
> |
self.cfg.maxEvents.setMaxEventsInput(maxEv) |
56 |
|
return |
57 |
|
|
58 |
< |
def outputModule(self, output): |
58 |
> |
def psetWriter(self, name): |
59 |
> |
""" |
60 |
> |
Write out modified CMSSW.cfg |
61 |
> |
""" |
62 |
|
|
63 |
< |
#set output module |
64 |
< |
outModule = self.cfg.outputModules['out'] |
65 |
< |
outModule.setFileName('file:'+str(output)) |
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 |
> |
try: |
68 |
> |
outFile.write(self.cmsProcess.dumpPython()) |
69 |
> |
except Exception, ex: |
70 |
> |
msg = "Your cfg file is not valid, %s\n" % str(ex) |
71 |
> |
msg += " https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabFaq#Problem_with_ParameterSet_parsin\n" |
72 |
> |
msg += " may help you understand the problem." |
73 |
> |
raise CrabException(msg) |
74 |
> |
|
75 |
> |
else: |
76 |
> |
outFile.write(self.cfg.data.dumpConfig()) |
77 |
> |
outFile.close() |
78 |
|
|
79 |
|
return |
80 |
|
|
81 |
< |
def psetWriter(self, name): |
81 |
> |
def addCrabFJR(self,name): |
82 |
> |
""" |
83 |
> |
_addCrabFJR_ |
84 |
> |
add CRAB specific FrameworkJobReport (FJR) |
85 |
> |
if a FJR already exists in input CMSSW parameter-set, add a second one. |
86 |
> |
This code is not needed for CMSSW >= 1.5.x and is non-functional in CMSSW >= 1.7.x. |
87 |
> |
It should be removed at some point in the future. |
88 |
> |
""" |
89 |
> |
|
90 |
> |
# Check if MessageLogger service already exists in configuration. If not, add it |
91 |
> |
svcs = self.cfg.data.services |
92 |
> |
if not svcs.has_key('MessageLogger'): |
93 |
> |
self.cfg.data.add_(CfgModules.Service("MessageLogger")) |
94 |
|
|
95 |
< |
configObject = cmsconfig(str(self.cfg)) |
95 |
> |
messageLogger = self.cfg.data.services['MessageLogger'] |
96 |
|
|
97 |
< |
file1 = open(common.work_space.jobDir()+name,"w") |
98 |
< |
file1.write(str(configObject.asConfigurationString())) |
99 |
< |
file1.close() |
97 |
> |
# Add fwkJobReports to Message logger if it doesn't exist |
98 |
> |
if "fwkJobReports" not in messageLogger.parameterNames_(): |
99 |
> |
messageLogger.fwkJobReports = CfgTypes.untracked(CfgTypes.vstring()) |
100 |
> |
|
101 |
> |
# should figure out how to remove "name" if it is there. |
102 |
> |
|
103 |
> |
if name not in messageLogger.fwkJobReports: |
104 |
> |
messageLogger.fwkJobReports.append(name) |
105 |
|
|
106 |
|
return |
107 |
+ |
|
108 |
+ |
def getTFileService(self): |
109 |
+ |
""" Get Output filename from TFileService and return it. If not existing, return None """ |
110 |
+ |
if not self.cfg.data.services.has_key('TFileService'): |
111 |
+ |
return None |
112 |
+ |
tFileService = self.cfg.data.services['TFileService'] |
113 |
+ |
if "fileName" in tFileService.parameterNames_(): |
114 |
+ |
fileName = getattr(tFileService,'fileName',None).value() |
115 |
+ |
return fileName |
116 |
+ |
return None |
117 |
+ |
|
118 |
+ |
def getPoolOutputModule(self): |
119 |
+ |
""" Get Output filename from PoolOutputModule and return it. If not existing, return None """ |
120 |
+ |
if not self.cfg.data.outputModules: |
121 |
+ |
return None |
122 |
+ |
poolOutputModule = self.cfg.data.outputModules |
123 |
+ |
for out in poolOutputModule: |
124 |
+ |
return poolOutputModule[out].fileName.value() |
125 |
+ |
|