Warning, /FWCore/ParameterSet/scripts/edmPythonConfigToCppValidation is written in an unsupported language. File is not indexed.
0001 #! /usr/bin/env python3
0002
0003 from builtins import str
0004 from FWCore.ParameterSet.Modules import _TypedParameterizable
0005 from FWCore.ParameterSet.Mixins import _ValidatingParameterListBase
0006 import FWCore.ParameterSet.Config as cms
0007
0008 def simpleItemToString(param):
0009 return str(param)
0010 def boolItemToString(param):
0011 if param:
0012 return "true"
0013 return "false"
0014 def stringItemToString(param):
0015 return '"'+param+'"'
0016
0017 def simpleValueToString(param):
0018 return simpleItemToString(param.value())
0019 def boolValueToString(param):
0020 return boolItemToString(param.value())
0021 def stringValueToString(param):
0022 return stringItemToString(param.value())
0023 def pythonValueToString(param):
0024 t,c = simpleParamHandlers[type(param).__name__]
0025 return t+"("+param.pythonValue()+")"
0026 def stringOrInputTagValueToString(param):
0027 if isinstance(param, str):
0028 param = cms.InputTag(param)
0029 return pythonValueToString(param)
0030
0031 simpleParamHandlers = {"int32":("int",simpleValueToString),
0032 "uint32":("unsigned int",simpleValueToString),
0033 "double":("double",simpleValueToString),
0034 "int64":("long int",simpleValueToString),
0035 "uint64":("unsigned long int",simpleValueToString),
0036 "bool":("bool",boolValueToString),
0037 "string":("std::string",stringValueToString),
0038 "ESInputTag":("edm::ESInputTag",pythonValueToString),
0039 "InputTag":("edm::InputTag",pythonValueToString),
0040 "EventID":("edm::EventID",pythonValueToString),
0041 "ESInputTag":("edm::ESInputTag",pythonValueToString),
0042 "EventRange":("edm::EventRange",pythonValueToString),
0043 "FileInPath":("edm::FileInPath",pythonValueToString),
0044 "LuminosityBlockID":("edm::LuminosityBlockID",pythonValueToString),
0045 "LuminosityBlockRange":("edm::LuminosityBlockRange",pythonValueToString)
0046 }
0047
0048
0049 itemParamHandlers = {"vint32":("int",simpleItemToString),
0050 "vuint32":("unsigned int",simpleItemToString),
0051 "vdouble":("double",simpleItemToString),
0052 "vint64":("long int",simpleItemToString),
0053 "vuint64":("unsigned long int",simpleItemToString),
0054 "vbool":("bool",boolItemToString),
0055 "vstring":("std::string",stringItemToString),
0056 "VESInputTag":("edm::ESInputTag",pythonValueToString),
0057 "VInputTag":("edm::InputTag",stringOrInputTagValueToString),
0058 "VEventID":("edm::EventID",stringItemToString),
0059 "VESInputTag":("edm::ESInputTag",pythonValueToString),
0060 "VEventRange":("edm::EventRange",stringItemToString),
0061 "VFileInPath":("edm::FileInPath",pythonValueToString),
0062 "VLuminosityBlockID":("edm::LuminosityBlockID",stringItemToString),
0063 "VLuminosityBlockRange":("edm::LuminosityBlockRange",stringItemToString)
0064 }
0065
0066 def printVPSetDescription(spacing,descName,pList,label,depth):
0067 print(spacing+"{")
0068 newSpacing = spacing+" "
0069 newDescName = "vpsd"+str(depth)
0070 print(newSpacing+"edm::ParameterSetDescription "+newDescName+";")
0071 if len(pList) > 0:
0072 printParameterSetDescription(newSpacing,newDescName,pList[0],0)
0073 tempName = "temp"+str(depth)
0074 print(newSpacing+"std::vector<edm::ParameterSet> "+tempName+";")
0075 print(newSpacing+tempName+".reserve("+str(len(pList))+");")
0076 for i in pList:
0077 print(newSpacing+"{")
0078 newerSpacing = newSpacing+" "
0079 otherTempName = "temp"+str(depth+1)
0080 print(newerSpacing+"edm::ParameterSet "+otherTempName+";")
0081 printParameterSet(newerSpacing,otherTempName,i,depth+1)
0082 print(newerSpacing+tempName+".push_back("+otherTempName+");")
0083 print(newSpacing+"}")
0084
0085 trackiness=""
0086 if not pList.isTracked():
0087 trackiness="Untracked"
0088 print(newSpacing+descName+".addVPSet"+trackiness+'("'+label+'", '+newDescName+", "+tempName+");")
0089 print(spacing+"}")
0090
0091 def printListTypeParameterDescription(spacing,descName,pList,label,depth):
0092 if isinstance(pList,cms.VPSet):
0093 printVPSetDescription(spacing,descName,pList,label,depth)
0094 return
0095 itemType,converter = itemParamHandlers[type(pList).__name__]
0096 trackiness=""
0097 if not pList.isTracked():
0098 trackiness="Untracked"
0099 if len(pList) == 0:
0100 print(spacing+descName+".add"+trackiness+"<std::vector<"+itemType+'>>("'+label+'", {});')
0101 else:
0102 print(spacing+descName+".add"+trackiness+"<std::vector<"+itemType+'>>("'+label+'", {')
0103 for i in pList:
0104 print(spacing+' '+converter(i)+',')
0105 print(spacing+'});')
0106
0107
0108 def expandRefToPSet(pset):
0109 # check whether we should expand a refToPSet_ parameter to full PSet
0110 if not 'refToPSet_' in pset.parameters_():
0111 return pset
0112
0113 if len(pset.parameters_()) > 1:
0114 raise RuntimeError('A "refToPSet_" should be the only parameter in a PSet')
0115 refToPSet = pset.parameters_()['refToPSet_']
0116 if not isinstance(refToPSet, cms.string):
0117 raise RuntimeError('A "refToPSet_" parameter is not a "cms.string"')
0118 refToPSetName = refToPSet.value()
0119 global config
0120 if not refToPSetName in config:
0121 raise RuntimeError('top-level PSet "%s", referenced by a refToPSet_, not found' % refToPSetName)
0122 refToPSetValue = config[refToPSetName]
0123 if not isinstance(refToPSetValue, cms.PSet):
0124 raise RuntimeError('top-level parameter "%s", referenced by a refToPSet_, is not a cms.PSet' % refToPSetName)
0125
0126 return refToPSetValue
0127
0128
0129 def printParameterSetDescription(spacing,descName, pset, depth):
0130 pset = expandRefToPSet(pset)
0131 for l,p in pset.parameters_().items():
0132 if isinstance(p,cms.PSet):
0133 print(spacing+"{")
0134 newSpacing = spacing+" "
0135 newDescName = "psd"+str(depth)
0136 print(newSpacing+"edm::ParameterSetDescription "+newDescName+";")
0137 printParameterSetDescription(newSpacing,newDescName,p,depth+1)
0138 trackiness=""
0139 if not p.isTracked():
0140 trackiness="Untracked"
0141 print(newSpacing+descName+".add"+trackiness+'<edm::ParameterSetDescription>("'+l+'", '+newDescName+");")
0142 print(spacing+"}")
0143 else:
0144 if isinstance(p,_ValidatingParameterListBase):
0145 printListTypeParameterDescription(spacing,descName,p,l,depth+1)
0146 #print p
0147 #raise RuntimeError()
0148 else:
0149 trackiness=""
0150 if not p.isTracked():
0151 trackiness="Untracked"
0152 (t,c) = simpleParamHandlers[type(p).__name__]
0153 print(spacing+descName+".add"+trackiness+"<"+t+'>("'+l+'", '+c(p)+");")
0154
0155 def printVPSet(spacing,psetName,pList,label,depth):
0156 print(spacing+"{")
0157 newSpacing = spacing+" "
0158 tempName = "temp"+str(depth)
0159 print(newSpacing+"std::vector<edm::ParameterSet> "+tempName+";")
0160 print(newSpacing+tempName+".reserve("+str(len(pList))+");")
0161 for i in pList:
0162 print(newSpacing+"{")
0163 newerSpacing = newSpacing+" "
0164 newPSetName = "vps"
0165 print(newerSpacing+"edm::ParameterSet "+newPSetName+";")
0166 printParameterSet(newerSpacing,newPSetName,i,depth+1)
0167 print(newerSpacing+tempName+".push_back("+newPSetName+");")
0168 print(newSpacing+"}")
0169
0170 trackiness=""
0171 if not pList.isTracked():
0172 trackiness="Untracked"
0173 print(newSpacing+psetName+".add"+trackiness+'Parameter<std::vector<edm::ParameterSet>>("'+label+'", '+tempName+");")
0174 print(spacing+"}")
0175
0176 def printListTypeParameter(spacing,psetName,pList,label,depth):
0177 if isinstance(pList,cms.VPSet):
0178 printVPSet(spacing,psetName,pList,label,depth)
0179 return
0180
0181 itemType,converter = itemParamHandlers[type(pList).__name__]
0182 trackiness=""
0183 if not pList.isTracked():
0184 trackiness="Untracked"
0185 if len(pList) == 0:
0186 print(spacing+psetName+".add"+trackiness+'Parameter<std::vector<'+itemType+'>>("'+label+'", {});')
0187 else:
0188 print(spacing+psetName+".add"+trackiness+'Parameter<std::vector<'+itemType+'>>("'+label+'", {')
0189 for i in pList:
0190 print(spacing+' '+converter(i)+',')
0191 print(spacing+'});')
0192
0193
0194 def printParameterSet(spacing, psetName, pset, depth):
0195 pset = expandRefToPSet(pset)
0196 for l,p in pset.parameters_().items():
0197 if isinstance(p,cms.PSet):
0198 print(spacing+"{")
0199 newSpacing = spacing+" "
0200 newPSetName = "ps"+str(depth)
0201 print(newSpacing+"edm::ParameterSet "+newPSetName+";")
0202 printParameterSet(newSpacing,newPSetName,p,depth+1)
0203 trackiness=""
0204 if not p.isTracked():
0205 trackiness="Untracked"
0206 print(newSpacing+psetName+".add"+trackiness+'Parameter<edm::ParameterSet>("'+l+'", '+newPSetName+");")
0207 print(spacing+"}")
0208 else:
0209 if isinstance(p,_ValidatingParameterListBase):
0210 printListTypeParameter(spacing,psetName,p,l,depth)
0211 #print p
0212 #raise RuntimeError()
0213 else:
0214 trackiness=""
0215 if not p.isTracked():
0216 trackiness="Untracked"
0217 (t,c) = simpleParamHandlers[type(p).__name__]
0218 print(spacing+psetName+".add"+trackiness+"Parameter<"+t+'>("'+l+'", '+c(p)+");")
0219
0220
0221 from argparse import ArgumentParser
0222 parser = ArgumentParser()
0223 parser.add_argument("config_file",type=str)
0224 options = parser.parse_args()
0225
0226 filename = options.config_file
0227
0228 f = open(filename,'r').read()
0229
0230 #this dictionary will contain the python objects from the file
0231 config = dict()
0232
0233 exec(f, config)
0234
0235 #print(config)
0236
0237 modules = {}
0238 for item in config:
0239 #print item
0240 if item.startswith('_'):
0241 continue
0242
0243 if isinstance(config[item], _TypedParameterizable):
0244 modules[item] = config[item]
0245
0246 if not modules:
0247 raise RuntimeError("No module found in file '"+filename+"'")
0248
0249 modulesTypes = set(module.type_() for module in modules.values())
0250 if len(modulesTypes) > 1:
0251 raise RuntimeError("The file '"+filename+"' contains modules of different C++ types");
0252 moduleType = modulesTypes.pop()
0253
0254 spacing = ' ';
0255 print('#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"')
0256 print('#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"')
0257 print()
0258 print('void')
0259 print(moduleType + '::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {')
0260 if len(modules) > 1:
0261 newSpacing = spacing + ' ';
0262 else:
0263 newSpacing = spacing
0264 for label, module in modules.items():
0265 if len(modules) > 1:
0266 print(spacing+'{')
0267 print(newSpacing + '// ' + label)
0268 print(newSpacing + 'edm::ParameterSetDescription desc;')
0269 printParameterSetDescription(newSpacing, 'desc', module, 0)
0270 print(newSpacing + 'descriptions.add("' + label + '", desc);')
0271 print(newSpacing + '// or use the following to generate the label from the module\'s C++ type')
0272 print(newSpacing + '//descriptions.addWithDefaultLabel(desc);')
0273 if len(modules) > 1:
0274 print(spacing+'}')
0275 print('}')