3 from __future__
import print_function
5 import FWCore.ParameterSet.Config
as cms
6 from importlib
import import_module
9 import xml.etree.ElementTree
as ET
14 for i
in xrange(len(aList)-1):
15 yield (aList[i], aList[i+1])
19 searchPaths = os.getenv(
'CMSSW_SEARCH_PATH').
split(
':')
21 for baseDir
in searchPaths:
22 print(
"Looking for '" + aRelPath +
"' under '" + baseDir +
"'")
23 if os.path.isfile(os.path.join(baseDir, aRelPath)):
25 resolvedPath = os.path.join(baseDir, aRelPath)
27 if resolvedPath
is None:
28 raise RuntimeError(
"Could not find LUT file '" + aRelPath +
"' under directories in 'CMSSW_SEARCH_PATH'")
30 with open(resolvedPath)
as f:
36 if line.startswith(
'#')
or line ==
'\n':
40 stripped_line = line[:line.find(
'#')]
43 items = stripped_line.split()
45 print(
"ERROR parsing file", resolvedPath,
"on line", line_nr,
"'" + line +
"' : Splitting on whitespace produced", len(items),
"items")
48 entries.append( (
int(items[0]),
int(items[1])) )
51 entries.sort(key=
lambda x : x[0])
54 print(
"ERROR parsing file", resolvedPath,
": No LUT entries defined in the file")
58 if entries[0][0] != 0:
59 print(
"ERROR parsing file", resolvedPath,
": LUT entries before index", entries[0][0],
"are not defined")
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")
67 result = [x[1]
for x
in entries]
69 if (len(result) < aExpectedSize)
and not (aPaddingValue
is None):
70 print (
"WARNING : Padding",
str(len(result))+
"-entry LUT with value", aPaddingValue,
"to have", aExpectedSize,
"entries")
71 result += ([aPaddingValue] * (aExpectedSize - len(result)))
72 elif (len(result) > aExpectedSize)
and aTruncate:
73 print (
"WARNING : Truncating",
str(len(result))+
"-entry LUT to have", aExpectedSize,
"entries")
74 result = result[0:aExpectedSize]
75 elif len(result) != aExpectedSize:
76 print (
"ERROR parsing file", resolvedPath,
": Expected LUT of size", aExpectedSize,
", but", len(result),
"entries were specified (and no padding/truncation requested)")
84 def divideByEgLsb(aParam):
85 return int(aParam.value() / aModule.egLsb.value())
87 def divideByTauLsb(aParam):
88 return int(aParam.value() / aModule.tauLsb.value())
90 def divideByJetLsb(aParam):
91 return int(aParam.value() / aModule.jetLsb.value())
95 ((
'mp_common',
'sdfile'),
None,
''),
96 ((
'mp_common',
'algoRev'),
None,
''),
97 ((
'mp_common',
'leptonSeedThreshold'),
'2_ClusterSeedThreshold.mif', divideByEgLsb(aModule.egSeedThreshold)),
98 ((
'mp_common',
'leptonTowerThreshold'),
'3_ClusterThreshold.mif', divideByEgLsb(aModule.egNeighbourThreshold)),
99 ((
'mp_common',
'pileUpTowerThreshold'),
'4_PileUpThreshold.mif', 0x0)
103 ((
'mp_egamma',
'egammaRelaxationThreshold'),
'10_EgRelaxThr.mif', divideByEgLsb(aModule.egMaxPtHOverE)),
104 ((
'mp_egamma',
'egammaMaxEta'),
'egammaMaxEta.mif', aModule.egEtaCut.value()),
105 ((
'mp_egamma',
'egammaBypassCuts'),
'BypassEgVeto.mif',
bool(aModule.egBypassEGVetos.value())),
106 ((
'mp_egamma',
'egammaBypassShape'),
'BypassEgShape.mif',
bool(aModule.egBypassShape.value())),
107 ((
'mp_egamma',
'egammaBypassEcalFG'),
'BypassEcalFG.mif',
bool(aModule.egBypassECALFG.value())),
108 ((
'mp_egamma',
'egammaBypassExtendedHOverE'),
'_BypassExtHE.mif',
bool(aModule.egBypassExtHOverE)),
109 ((
'mp_egamma',
'egammaHOverECut_iEtaLT15'),
'_RatioCutLt15.mif', aModule.egHOverEcutBarrel.value()),
110 ((
'mp_egamma',
'egammaHOverECut_iEtaGTEq15'),
'_RatioCutGe15.mif', aModule.egHOverEcutEndcap.value()),
111 ((
'mp_egamma',
'egammaEnergyCalibLUT'),
'C_EgammaCalibration_12to18.mif',
parseOfflineLUTfile(aModule.egCalibrationLUTFile.value(), 4096)),
112 ((
'mp_egamma',
'egammaIsoLUT1'),
'D_EgammaIsolation1_13to9.mif',
parseOfflineLUTfile(aModule.egIsoLUTFile.value(), 8192)),
113 ((
'mp_egamma',
'egammaIsoLUT2'),
'D_EgammaIsolation2_13to9.mif',
parseOfflineLUTfile(aModule.egIsoLUTFile2.value(), 8192))
117 ((
'mp_tau',
'tauMaxEta'),
'tauMaxEta.mif', aModule.isoTauEtaMax.value()),
118 ((
'mp_tau',
'tauEnergyCalibLUT'),
'I_TauCalibration_11to18.mif',
parseOfflineLUTfile(aModule.tauCalibrationLUTFile.value(), 2048, 0x0)),
119 ((
'mp_tau',
'tauIsoLUT'),
'H_TauIsolation_12to9.mif',
parseOfflineLUTfile(aModule.tauIsoLUTFile.value(), 4096)),
120 ((
'mp_tau',
'tauTrimmingLUT'),
'P_TauTrimming_13to8.mif',
parseOfflineLUTfile(aModule.tauTrimmingShapeVetoLUTFile.value(), 8192))
124 ((
'mp_jet',
'jetSeedThreshold'),
'1_JetSeedThreshold.mif', divideByJetLsb(aModule.jetSeedThreshold)),
125 ((
'mp_jet',
'jetMaxEta'),
'6_JetEtaMax.mif', 0x00028),
126 ((
'mp_jet',
'jetBypassPileUpSub'),
'BypassJetPUS.mif',
bool(aModule.jetBypassPUS.value())),
127 ((
'mp_jet',
'jetPUSUsePhiRing'),
'PhiRingPUS.mif',
bool(aModule.jetPUSUsePhiRing.value())),
128 ((
'mp_jet',
'jetEnergyCalibLUT'),
'L_JetCalibration_11to18.mif',
parseOfflineLUTfile(aModule.jetCalibrationLUTFile.value(), 2048)),
129 ((
'mp_jet',
'HTMHT_maxJetEta'),
'HTMHT_maxJetEta.mif', aModule.etSumEtaMax[1]),
130 ((
'mp_jet',
'HT_jetThreshold'),
'8_HtThreshold.mif',
int(aModule.etSumEtThreshold[1] / aModule.etSumLsb.value())),
131 ((
'mp_jet',
'MHT_jetThreshold'),
'9_MHtThreshold.mif',
int(aModule.etSumEtThreshold[3] / aModule.etSumLsb.value())),
135 ((
'mp_sums',
'towerCountThreshold'),
'HeavyIonThr.mif',
int(aModule.etSumEtThreshold[4] / aModule.etSumLsb.value()) ),
136 ((
'mp_sums',
'towerCountMaxEta'),
'HeavyIonEta.mif', aModule.etSumEtaMax[4]),
137 ((
'mp_sums',
'ETMET_maxTowerEta'),
'ETMET_maxTowerEta.mif', aModule.etSumEtaMax[0]),
138 ((
'mp_sums',
'ecalET_towerThresholdLUT'),
'X_EcalTHR_11to9.mif',
parseOfflineLUTfile(aModule.etSumEcalSumPUSLUTFile.value(), 2048, aTruncate =
True)),
139 ((
'mp_sums',
'ET_towerThresholdLUT'),
'X_ETTHR_11to9.mif',
parseOfflineLUTfile(aModule.etSumEttPUSLUTFile.value(), 2048, aTruncate =
True)),
140 ((
'mp_sums',
'MET_towerThresholdLUT'),
'X_METTHR_11to9.mif',
parseOfflineLUTfile(aModule.etSumMetPUSLUTFile.value(), 2048))
144 ((
'demux',
'sdfile'),
None,
''),
145 ((
'demux',
'algoRev'),
None, 0xcafe),
146 ((
'demux',
'ET_centralityLowerThresholds'),
'CentralityLowerThrs.mif', [
int(round(loBound / aModule.etSumLsb.value()))
for loBound
in aModule.etSumCentralityLower.value()]),
147 ((
'demux',
'ET_centralityUpperThresholds'),
'CentralityUpperThrs.mif', [
int(round(upBound / aModule.etSumLsb.value()))
for upBound
in aModule.etSumCentralityUpper.value()]),
148 ((
'demux',
'MET_energyCalibLUT'),
'M_METnoHFenergyCalibration_12to18.mif',
parseOfflineLUTfile(aModule.metCalibrationLUTFile.value(), 4096, aTruncate =
True)),
149 ((
'demux',
'METHF_energyCalibLUT'),
'M_METwithHFenergyCalibration_12to18.mif',
parseOfflineLUTfile(aModule.metHFCalibrationLUTFile.value(), 4096, aTruncate =
True)),
150 ((
'demux',
'ET_energyCalibLUT'),
'S_ETcalibration_12to18.mif',
parseOfflineLUTfile(aModule.etSumEttCalibrationLUTFile.value(), 4096, aTruncate =
True)),
151 ((
'demux',
'ecalET_energyCalibLUT'),
'R_EcalCalibration_12to18.mif',
parseOfflineLUTfile(aModule.etSumEcalSumCalibrationLUTFile.value(), 4096, aTruncate =
True)),
152 ((
'demux',
'MET_phiCalibLUT'),
'Q_METnoHFphiCalibration_12to18.mif',
parseOfflineLUTfile(aModule.metPhiCalibrationLUTFile.value(), 4096, aTruncate =
True)),
153 ((
'demux',
'METHF_phiCalibLUT'),
'Q_METwithHFphiCalibration_12to18.mif',
parseOfflineLUTfile(aModule.metHFPhiCalibrationLUTFile.value(), 4096, aTruncate =
True)),
156 result = [(a, b,
parseOfflineLUTfile(c.value())
if isinstance(c, cms.FileInPath)
else c)
for a, b, c
in result]
164 if xmlDetails
is not None:
165 if xmlDetails[0]
in result:
166 result[xmlDetails[0]] += [(xmlDetails[1], value)]
168 result[xmlDetails[0]] = [(xmlDetails[1], value)]
177 return {mifFileName : value
for (_, mifFileName, value)
in fullList
if mifFileName
is not None}
184 if not elem.text
or not elem.text.strip():
186 if not elem.tail
or not elem.tail.strip():
190 if not elem.tail
or not elem.tail.strip():
193 if level
and (
not elem.tail
or not elem.tail.strip()):
197 print(
"Writing MIF file:", aFilePath)
198 with open(aFilePath,
'w')
as f:
199 if isinstance(aValue, bool):
200 aValue = (1
if aValue
else 0)
202 if isinstance(aValue, int):
203 f.write( hex(aValue) )
204 elif isinstance(aValue, list):
205 f.write(
"\n".
join([hex(x)
for x
in aValue]))
207 raise RuntimeError(
"Do not know how to deal with parameter of type " +
str(type(aValue)))
211 topNode = ET.Element(
'algo', id=
'calol2')
212 contextNode = ET.SubElement(topNode,
'context', id=contextId)
213 for paramId, value
in parameters:
214 if isinstance(value, bool):
215 ET.SubElement(contextNode,
'param', id=paramId, type=
'bool').text =
str(value).lower()
216 elif isinstance(value, int):
217 ET.SubElement(contextNode,
'param', id=paramId, type=
'uint').text =
"0x{0:05X}".
format(value)
218 elif isinstance(value, str):
219 ET.SubElement(contextNode,
'param', id=paramId, type=
'string').text = value
220 elif isinstance(value, list):
221 ET.SubElement(contextNode,
'param', id=paramId, type=
'vector:uint').text =
"\n " +
",\n ".
join([
"0x{0:05X}".
format(x)
for x
in value]) +
"\n " 223 raise RuntimeError(
"Do not know how to deal with parameter '" + paramId +
"' of type " +
str(type(value)))
226 print(
"Writing XML file:", outputFilePath)
227 with open(outputFilePath,
'w')
as f:
228 f.write(ET.tostring(topNode))
232 if __name__ ==
'__main__':
234 parser = argparse.ArgumentParser()
236 parser.add_argument(
'params_cfi', help=
'Name of CMSSW cfi python file specifying the values for the calo parameters')
237 parser.add_argument(
'output_dir', help=
'Directory for MIF/XML output files')
239 outputFormatGroup = parser.add_mutually_exclusive_group(required=
True)
240 outputFormatGroup.add_argument(
'--mif', action=
'store_true')
241 outputFormatGroup.add_argument(
'--xml', action=
'store_true')
243 args = parser.parse_args()
245 moduleName =
'L1Trigger.L1TCalorimeter.' + args.params_cfi
246 print(
"Importing calo params from module:", moduleName)
247 caloParams = import_module(moduleName).caloStage2Params
249 print(caloParams.egCalibrationLUTFile.value())
250 print(caloParams.egIsoLUTFile.value())
251 print(caloParams.egIsoLUTFile2.value())
252 os.mkdir(args.output_dir)
256 createMIF(args.output_dir +
'/' + fileName, value)
259 createXML(paramList,
'MainProcessor' if fileTag.startswith(
'mp')
else 'Demux', args.output_dir +
'/algo_' + fileTag +
'.xml')
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
def split(sequence, size)
static std::string join(char **cmd)