24 #include "L1TCaloLayer1FetchLUTs.hh"
25 #include "UCTLogging.hh"
27 using namespace l1tcalo;
34 std::vector<unsigned int> &ePhiMap,
35 std::vector<unsigned int> &hPhiMap,
36 std::vector<unsigned int> &hfPhiMap,
48 <<
"Using Stage2-Layer1 but HCAL Geometry has use1x1 = 0! HF will be suppressed. Check Global Tag, etc.";
55 if (paramsHandle.
product() ==
nullptr) {
56 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"Missing CaloParams object! Check Global Tag, etc.";
65 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloLSB (caloParams.towerLsbSum()) != 0.5, actually = " << caloLSB;
73 auto ecalScaleETBins =
caloParams.layer1ECalScaleETBins();
74 auto ecalScalePhiBins =
caloParams.layer1ECalScalePhiBins();
75 if (ecalScalePhiBins.empty()) {
77 ecalScalePhiBins.resize(36, 0);
78 }
else if (ecalScalePhiBins.size() % 36 != 0) {
79 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1ECalScaleETBins().size() is not multiple of 36 !!";
82 size_t numEcalPhiBins = (*std::max_element(ecalScalePhiBins.begin(), ecalScalePhiBins.end())) + 1;
83 auto ecalSF =
caloParams.layer1ECalScaleFactors();
84 if (ecalSF.size() != ecalScaleETBins.size() * numEcalPhiBins * 28) {
85 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1ECalScaleFactors().size() != "
86 "caloParams.layer1ECalScaleETBins().size()*numEcalPhiBins*28 !!";
89 auto hcalScaleETBins =
caloParams.layer1HCalScaleETBins();
90 auto hcalScalePhiBins =
caloParams.layer1HCalScalePhiBins();
91 if (hcalScalePhiBins.empty()) {
92 hcalScalePhiBins.resize(36, 0);
93 }
else if (hcalScalePhiBins.size() % 36 != 0) {
94 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1HCalScaleETBins().size() is not multiple of 36 !!";
97 size_t numHcalPhiBins = (*std::max_element(hcalScalePhiBins.begin(), hcalScalePhiBins.end())) + 1;
98 auto hcalSF =
caloParams.layer1HCalScaleFactors();
99 if (hcalSF.size() != hcalScaleETBins.size() * numHcalPhiBins * 28) {
100 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1HCalScaleFactors().size() != "
101 "caloParams.layer1HCalScaleETBins().size()*numHcalPhiBins*28 !!";
110 auto hfScaleETBins =
caloParams.layer1HFScaleETBins();
111 auto hfScalePhiBins =
caloParams.layer1HFScalePhiBins();
112 if (hfScalePhiBins.empty()) {
113 hfScalePhiBins.resize(36, 0);
114 }
else if (hfScalePhiBins.size() % 36 != 0) {
115 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1HFScaleETBins().size() is not multiple of 36 !!";
118 size_t numHFPhiBins = (*std::max_element(hfScalePhiBins.begin(), hfScalePhiBins.end())) + 1;
120 if (
hfSF.size() != hfScaleETBins.size() * numHFPhiBins * 12) {
122 <<
"caloParams.layer1HFScaleFactors().size() != caloParams.layer1HFScaleETBins().size()*numHFPhiBins*12 !!";
127 if (
useCalib && (ecalSF.empty() || hcalSF.empty() ||
hfSF.empty())) {
128 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"Layer 1 calibrations requested (useCalib = True) but there are missing "
129 "scale factors in CaloParams! Please check conditions setup.";
133 const double ecalLSB = 0.5;
138 if (decoder.
product() ==
nullptr) {
139 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"Missing CaloTPGTranscoder object! Check Global Tag, etc.";
145 auto decodeHcalEt = [&decoder](
int iEta, uint32_t compressedEt, uint32_t iPhi = 3) ->
double {
156 std::array<std::array<std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins> phiLUT;
157 eLUT.push_back(phiLUT);
159 for (uint32_t fb = 0; fb < nCalSideBins; fb++) {
160 for (uint32_t ecalInput = 0; ecalInput <= 0xFF; ecalInput++) {
161 uint32_t
value = ecalInput;
163 double linearizedECalInput = ecalInput * ecalLSB;
166 for (;
etBin < ecalScaleETBins.size();
etBin++) {
167 if (linearizedECalInput < ecalScaleETBins[
etBin])
170 if (
etBin >= ecalScaleETBins.size())
171 etBin = ecalScaleETBins.size() - 1;
173 double calibratedECalInput = linearizedECalInput;
175 calibratedECalInput *= ecalSF.at(
phiBin * ecalScaleETBins.size() * 28 +
etBin * 28 +
etaBin);
177 calibratedECalInput /= caloLSB;
179 value = calibratedECalInput;
183 if (
value > 0xFF || ecalInput == 0xFF) {
195 uint32_t et_log2 = ((uint32_t)log2(
value)) & 0x7;
196 value |= (et_log2 << 12);
207 std::array<std::array<std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins> phiLUT;
208 hLUT.push_back(phiLUT);
213 if (
pos != hcalScalePhiBins.end()) {
218 iPhi =
index * 4 + 1;
220 iPhi = (
index - 18) * 4 + 1;
223 for (uint32_t fb = 0; fb < nCalSideBins; fb++) {
224 for (uint32_t hcalInput = 0; hcalInput <= 0xFF; hcalInput++) {
225 uint32_t
value = hcalInput;
228 double linearizedHcalInput = decodeHcalEt(caloEta, hcalInput, iPhi);
231 for (;
etBin < hcalScaleETBins.size();
etBin++) {
232 if (linearizedHcalInput < hcalScaleETBins[
etBin])
235 if (
etBin >= hcalScaleETBins.size())
236 etBin = hcalScaleETBins.size() - 1;
238 double calibratedHcalInput = linearizedHcalInput;
240 calibratedHcalInput *= hcalSF.at(
phiBin * hcalScaleETBins.size() * 28 +
etBin * 28 +
etaBin);
242 calibratedHcalInput /= caloLSB;
244 value = calibratedHcalInput;
247 if (
value > 0xFF || hcalInput == 0xFF) {
259 uint32_t et_log2 = ((uint32_t)log2(
value)) & 0x7;
260 value |= (et_log2 << 12);
271 std::array<std::array<uint32_t, nEtBins>, nHfEtaBins> phiLUT;
272 hfLUT.push_back(phiLUT);
274 int caloEta =
etaBin + 30;
277 if (
pos != hfScalePhiBins.end()) {
281 iPhi =
index * 4 - 1;
283 iPhi = (
index - 18) * 4 - 1;
288 for (uint32_t etCode = 0; etCode < nEtBins; etCode++) {
289 uint32_t
value = etCode;
291 double linearizedHFInput = 0;
293 linearizedHFInput = decodeHcalEt(caloEta,
value, iPhi);
297 for (;
etBin < hfScaleETBins.size();
etBin++) {
298 if (linearizedHFInput < hfScaleETBins[
etBin])
301 if (
etBin >= hfScaleETBins.size())
302 etBin = hfScaleETBins.size() - 1;
304 double calibratedHFInput = linearizedHFInput;
308 calibratedHFInput /= caloLSB;
311 uint32_t absCaloEta =
std::abs(caloEta);
312 if (absCaloEta > 29 && absCaloEta < 40) {
314 calibratedHFInput *= 0.5;
315 }
else if (absCaloEta == 40 || absCaloEta == 41) {
317 calibratedHFInput *= 0.25;
319 value = calibratedHFInput;
321 if (
value >= 0xFF || etCode == 0xFF) {
325 value = calibratedHFInput;
337 for (uint32_t isPos = 0; isPos < 2; isPos++) {
338 for (uint32_t iPhi = 1; iPhi <= 72; iPhi++) {
339 uint32_t card = floor((iPhi + 1) / 4);
342 ePhiMap[isPos * 72 + iPhi - 1] = ecalScalePhiBins[isPos * 18 + card];
343 hPhiMap[isPos * 72 + iPhi - 1] = hcalScalePhiBins[isPos * 18 + card];
344 hfPhiMap[isPos * 72 + iPhi - 1] = hfScalePhiBins[isPos * 18 + card];