CMS 3D CMS Logo

convertParamsToOnlineFormat.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 import argparse
4 import FWCore.ParameterSet.Config as cms
5 from importlib import import_module
6 import os
7 import sys
8 import xml.etree.ElementTree as ET
9 
10 import six
11 
12 # Pairwise generator: returns pairs of adjacent elements in a list / other iterable
13 def pairwiseGen(aList):
14  for i in xrange(len(aList)-1):
15  yield (aList[i], aList[i+1])
16 
17 def parseOfflineLUTfile(aRelPath):
18  # Find file by looking under directories listed in 'CMSSW_SEARCH_PATH' as outlined in https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideEdmFileInPath
19  searchPaths = os.getenv('CMSSW_SEARCH_PATH').split(':')
20  resolvedPath = None
21  for baseDir in searchPaths:
22  print "Looking for '" + aRelPath + "' under '" + baseDir + "'"
23  if os.path.isfile(os.path.join(baseDir, aRelPath)):
24  print " success!"
25  resolvedPath = os.path.join(baseDir, aRelPath)
26  break
27  if resolvedPath is None:
28  raise RuntimeError("Could not find LUT file '" + aRelPath + "' under directories in 'CMSSW_SEARCH_PATH'")
29 
30  with open(resolvedPath) as f:
31  entries = []
32  line_nr = 0
33  for line in f:
34  line_nr += 1
35  # Ignore comment lines
36  if line.startswith('#') or line == '\n':
37  continue
38 
39  # Remove trailing comments from data lines
40  stripped_line = line[:line.find('#')]
41 
42  # Split line into list of whitespace-separated items
43  items = stripped_line.split()
44  if len(items) != 2:
45  print "ERROR parsing file", resolvedPath, "on line", line_nr, "'" + line + "' : Splitting on whitespace produced", len(items), "items"
46  sys.exit(1)
47 
48  entries.append( (int(items[0]), int(items[1])) )
49 
50  # Sort the LUT
51  entries.sort(key= lambda x : x[0])
52  # Check that the LUT is not empty
53  if len(entries) == 0:
54  print "ERROR parsing file", resolvedPath, ": No LUT entries defined in the file"
55  sys.exit(1)
56 
57  # Check that no items from the LUT are missing
58  if entries[0][0] != 0:
59  print "ERROR parsing file", resolvedPath, ": LUT entries before index", entries[0][0], "are not defined"
60  sys.exit(1)
61 
62  for x1, x2 in pairwiseGen(entries):
63  if x1[0] != (x2[0]-1):
64  print "ERROR parsing file", resolvedPath, ": ", x2[0] - x1[0] - 1,"LUT entries between indices", x1[0], "and", x2[0], "are not defined"
65  sys.exit(1)
66 
67  return [x[1] for x in entries]
68 
69 
71 
72  def divideByEgLsb(aParam):
73  return int(aParam.value() / aModule.egLsb.value())
74 
75  def divideByTauLsb(aParam):
76  return int(aParam.value() / aModule.tauLsb.value())
77 
78  def divideByJetLsb(aParam):
79  return int(aParam.value() / aModule.jetLsb.value())
80 
81 
82  result = [
83  (('mp_common', 'sdfile'), None, ''),
84  (('mp_common', 'algoRev'), None, ''),
85  (('mp_common', 'leptonSeedThreshold'), '2_ClusterSeedThreshold.mif', divideByEgLsb(aModule.egSeedThreshold)),
86  (('mp_common', 'leptonTowerThreshold'), '3_ClusterThreshold.mif', divideByEgLsb(aModule.egNeighbourThreshold)),
87  (('mp_common', 'pileUpTowerThreshold'), '4_PileUpThreshold.mif', 0x0)
88  ]
89 
90  result += [
91  (('mp_egamma', 'egammaRelaxationThreshold'), '10_EgRelaxThr.mif', divideByEgLsb(aModule.egMaxPtHOverE)),
92  (('mp_egamma', 'egammaMaxEta'), '5_EgammaTauEtaMax.mif', aModule.egEtaCut.value()),
93  (('mp_egamma', 'egammaBypassCuts'), 'BypassEgVeto.mif', bool(aModule.egBypassEGVetos.value())),
94  (('mp_egamma', 'egammaHOverECut_iEtaLT15'), '_RatioCutLt15.mif', aModule.egHOverEcutBarrel.value()),
95  (('mp_egamma', 'egammaHOverECut_iEtaGTEq15'), '_RatioCutGe15.mif', aModule.egHOverEcutEndcap.value()),
96  (('mp_egamma', 'egammaBypassExtendedHOverE'), '_BypassExtHE.mif', bool(aModule.egBypassExtHOverE)),
97  (('mp_egamma', 'egammaEnergyCalibLUT'), 'C_EgammaCalibration_12to18.mif', parseOfflineLUTfile(aModule.egCalibrationLUTFile.value())),
98  (('mp_egamma', 'egammaIsoLUT1'), 'D_EgammaIsolation1_13to9.mif', parseOfflineLUTfile(aModule.egIsoLUTFile.value())),
99  (('mp_egamma', 'egammaIsoLUT2'), 'D_EgammaIsolation2_13to9.mif', parseOfflineLUTfile(aModule.egIsoLUTFile2.value()))
100  ]
101 
102  result += [
103  (('mp_tau', 'tauMaxEta'), '5_EgammaTauEtaMax.mif', aModule.isoTauEtaMax.value()),
104  (('mp_tau', 'tauEnergyCalibLUT'), 'I_TauCalibration_11to18.mif', parseOfflineLUTfile(aModule.tauCalibrationLUTFile.value())),
105  (('mp_tau', 'tauIsoLUT'), 'H_TauIsolation_12to9.mif', parseOfflineLUTfile(aModule.tauIsoLUTFile.value()))
106  ]
107 
108  result += [
109  (('mp_jet', 'jetSeedThreshold'), '1_JetSeedThreshold.mif', divideByJetLsb(aModule.jetSeedThreshold)),
110  (('mp_jet', 'jetMaxEta'), '6_JetEtaMax.mif', 0x00028),
111  (('mp_jet', 'HTMHT_maxJetEta'), '7_RingEtaMax.mif', aModule.etSumEtaMax[1]), # assert == etSumEtaMax[3] ?
112  (('mp_jet', 'HT_jetThreshold'), '8_HtThreshold.mif', int(aModule.etSumEtThreshold[1] / aModule.etSumLsb.value())),
113  (('mp_jet', 'MHT_jetThreshold'), '9_MHtThreshold.mif', int(aModule.etSumEtThreshold[3] / aModule.etSumLsb.value())),
114  (('mp_jet', 'jetBypassPileUpSub'), 'BypassJetPUS.mif', bool(aModule.jetBypassPUS.value())),
115  (('mp_jet', 'jetEnergyCalibLUT'), 'L_JetCalibration_11to18.mif', aModule.jetCalibrationLUTFile)
116  ]
117 
118  result += [
119  (('mp_sums', 'towerCountThreshold'), 'HeavyIonThr.mif', int(aModule.etSumEtThreshold[4] / aModule.etSumLsb.value()) ),
120  (('mp_sums', 'towerCountMaxEta'), 'HeavyIonEta.mif', aModule.etSumEtaMax[4]),
121  (('mp_sums', 'ETMET_maxTowerEta'), '7_RingEtaMax.mif', aModule.etSumEtaMax[0]), # assert == etSumEtaMax[2] ?
122  (('mp_sums', 'ecalET_towerThresholdLUT'), 'X_EcalTHR_11to9.mif', aModule.etSumEcalSumPUSLUTFile),
123  (('mp_sums', 'ET_towerThresholdLUT'), 'X_ETTHR_11to9.mif', aModule.etSumMetPUSLUTFile),
124  (('mp_sums', 'MET_towerThresholdLUT'), 'X_METTHR_11to9.mif', aModule.etSumMetPUSLUTFile)
125  ]
126 
127  result += [
128  (('demux', 'sdfile'), None, ''),
129  (('demux', 'algoRev'), None, 0xcafe)
130  ]
131 
132  result = [(a, b, parseOfflineLUTfile(c.value()) if isinstance(c, cms.FileInPath) else c) for a, b, c in result]
133 
134  return result
135 
136 
137 def getXmlParameterMap(aModule):
138  result = {}
139  for xmlDetails, mifName, value in getFullListOfParameters(aModule):
140  if xmlDetails is not None:
141  if xmlDetails[0] in result:
142  result[xmlDetails[0]] += [(xmlDetails[1], value)]
143  else:
144  result[xmlDetails[0]] = [(xmlDetails[1], value)]
145 
146  return result
147 
148 
149 def getMifParameterMap(aModule):
150 
151  fullList = getFullListOfParameters(aModule)
152 
153  return {mifFileName : value for (_, mifFileName, value) in fullList if mifFileName is not None}
154 
155 
156 # Stolen from https://stackoverflow.com/questions/3095434/inserting-newlines-in-xml-file-generated-via-xml-etree-elementtree-in-python
157 def indent(elem, level=0):
158  i = "\n" + level*" "
159  if len(elem):
160  if not elem.text or not elem.text.strip():
161  elem.text = i + " "
162  if not elem.tail or not elem.tail.strip():
163  elem.tail = i
164  for elem in elem:
165  indent(elem, level+1)
166  if not elem.tail or not elem.tail.strip():
167  elem.tail = i
168  else:
169  if level and (not elem.tail or not elem.tail.strip()):
170  elem.tail = i
171 
172 def createMIF(aFilePath, aValue):
173  print "Writing MIF file:", aFilePath
174  with open(aFilePath, 'w') as f:
175  if isinstance(aValue, bool):
176  aValue = (1 if aValue else 0)
177 
178  if isinstance(aValue, int):
179  f.write( hex(aValue) )
180  elif isinstance(aValue, list):
181  f.write("\n".join([hex(x) for x in aValue]))
182  else:
183  raise RuntimeError("Do not know how to deal with parameter of type " + str(type(aValue)))
184 
185 
186 def createXML(parameters, contextId, outputFilePath):
187  topNode = ET.Element('algo', id='calol2')
188  contextNode = ET.SubElement(topNode, 'context', id=contextId)
189  for paramId, value in parameters:
190  if isinstance(value, bool):
191  ET.SubElement(contextNode, 'param', id=paramId, type='bool').text = str(value).lower()
192  elif isinstance(value, int):
193  ET.SubElement(contextNode, 'param', id=paramId, type='uint').text = "0x{0:05X}".format(value)
194  elif isinstance(value, str):
195  ET.SubElement(contextNode, 'param', id=paramId, type='string').text = value
196  elif isinstance(value, list):
197  ET.SubElement(contextNode, 'param', id=paramId, type='vector:uint').text = "\n " + ",\n ".join(["0x{0:05X}".format(x) for x in value]) + "\n "
198  else:
199  raise RuntimeError("Do not know how to deal with parameter '" + paramId + "' of type " + str(type(value)))
200  indent(topNode)
201 
202  print "Writing XML file:", outputFilePath
203  with open(outputFilePath, 'w') as f:
204  f.write(ET.tostring(topNode))
205 
206 
207 
208 if __name__ == '__main__':
209 
210  parser = argparse.ArgumentParser()
211 
212  parser.add_argument('params_cfi', help='Name of CMSSW cfi python file specifying the values for the calo parameters')
213  parser.add_argument('output_dir', help='Directory for MIF/XML output files')
214 
215  outputFormatGroup = parser.add_mutually_exclusive_group(required=True)
216  outputFormatGroup.add_argument('--mif', action='store_true')
217  outputFormatGroup.add_argument('--xml', action='store_true')
218 
219  args = parser.parse_args()
220 
221  moduleName = 'L1Trigger.L1TCalorimeter.' + args.params_cfi
222  print "Importing calo params from module:", moduleName
223  caloParams = import_module(moduleName).caloStage2Params
224 
225  print caloParams.egCalibrationLUTFile.value()
226  print caloParams.egIsoLUTFile.value()
227  print caloParams.egIsoLUTFile2.value()
228  os.mkdir(args.output_dir)
229 
230  if args.mif:
231  for fileName, value in getMifParameterMap(six.iteritems(caloParams)):
232  createMIF(args.output_dir + '/' + fileName, value)
233  else:
234  for fileTag, paramList in getXmlParameterMap(six.iteritems(caloParams)):
235  createXML(paramList, 'MainProcessor' if fileTag.startswith('mp') else 'Demux', args.output_dir + '/algo_' + fileTag + '.xml')
def createXML(parameters, contextId, outputFilePath)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
#define str(s)
double split
Definition: MVATrainer.cc:139