24 #include "L1TCaloLayer1FetchLUTs.hh" 25 #include "UCTLogging.hh" 30 const L1TCaloLayer1FetchLUTsTokens &iTokens,
35 std::vector<unsigned long long int> &hcalFBLUT,
36 std::vector<unsigned int> &ePhiMap,
37 std::vector<unsigned int> &hPhiMap,
38 std::vector<unsigned int> &hfPhiMap,
50 <<
"Using Stage2-Layer1 but HCAL Geometry has use1x1 = 0! HF will be suppressed. Check Global Tag, etc.";
56 if (not paramsHandle.
isValid()) {
57 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"Missing CaloParams object! Check Global Tag, etc.";
66 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloLSB (caloParams.towerLsbSum()) != 0.5, actually = " << caloLSB;
74 auto ecalScaleETBins =
caloParams.layer1ECalScaleETBins();
75 auto ecalScalePhiBins =
caloParams.layer1ECalScalePhiBins();
76 if (ecalScalePhiBins.empty()) {
78 ecalScalePhiBins.resize(36, 0);
79 }
else if (ecalScalePhiBins.size() % 36 != 0) {
80 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1ECalScaleETBins().size() is not multiple of 36 !!";
83 size_t numEcalPhiBins = (*std::max_element(ecalScalePhiBins.begin(), ecalScalePhiBins.end())) + 1;
84 auto ecalSF =
caloParams.layer1ECalScaleFactors();
85 if (ecalSF.size() != ecalScaleETBins.size() * numEcalPhiBins * 28) {
86 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1ECalScaleFactors().size() != " 87 "caloParams.layer1ECalScaleETBins().size()*numEcalPhiBins*28 !!";
90 auto hcalScaleETBins =
caloParams.layer1HCalScaleETBins();
91 auto hcalScalePhiBins =
caloParams.layer1HCalScalePhiBins();
92 if (hcalScalePhiBins.empty()) {
93 hcalScalePhiBins.resize(36, 0);
94 }
else if (hcalScalePhiBins.size() % 36 != 0) {
95 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1HCalScaleETBins().size() is not multiple of 36 !!";
98 size_t numHcalPhiBins = (*std::max_element(hcalScalePhiBins.begin(), hcalScalePhiBins.end())) + 1;
99 auto hcalSF =
caloParams.layer1HCalScaleFactors();
100 if (hcalSF.size() != hcalScaleETBins.size() * numHcalPhiBins * 28) {
101 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1HCalScaleFactors().size() != " 102 "caloParams.layer1HCalScaleETBins().size()*numHcalPhiBins*28 !!";
111 auto hfScaleETBins =
caloParams.layer1HFScaleETBins();
112 auto hfScalePhiBins =
caloParams.layer1HFScalePhiBins();
113 if (hfScalePhiBins.empty()) {
114 hfScalePhiBins.resize(36, 0);
115 }
else if (hfScalePhiBins.size() % 36 != 0) {
116 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"caloParams.layer1HFScaleETBins().size() is not multiple of 36 !!";
119 size_t numHFPhiBins = (*std::max_element(hfScalePhiBins.begin(), hfScalePhiBins.end())) + 1;
121 if (
hfSF.size() != hfScaleETBins.size() * numHFPhiBins * 12) {
123 <<
"caloParams.layer1HFScaleFactors().size() != caloParams.layer1HFScaleETBins().size()*numHFPhiBins*12 !!";
128 if (
useCalib && (ecalSF.empty() || hcalSF.empty() ||
hfSF.empty())) {
129 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"Layer 1 calibrations requested (useCalib = True) but there are missing " 130 "scale factors in CaloParams! Please check conditions setup.";
137 auto fbLUTUpper =
caloParams.layer1HCalFBLUTUpper();
138 auto fbLUTLower =
caloParams.layer1HCalFBLUTLower();
141 if (fbLUTUpper.size() != nCalEtaBins) {
143 <<
"caloParams.layer1HCalFBLUTUpper().size() " << fbLUTUpper.size() <<
" != " << nCalEtaBins <<
" !!";
146 if (fbLUTLower.size() != nCalEtaBins) {
148 <<
"caloParams.layer1HCalFBLUTLower().size() " << fbLUTLower.size() <<
" != " << nCalEtaBins <<
" !!";
154 const double ecalLSB = 0.5;
159 edm::LogError(
"L1TCaloLayer1FetchLUTs") <<
"Missing CaloTPGTranscoder object! Check Global Tag, etc.";
165 auto decodeHcalEt = [&decoder](
int iEta, uint32_t compressedEt, uint32_t iPhi = 3) ->
double {
171 return decoder->hcaletValue(
id,
sample);
176 std::array<std::array<std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins> phiLUT;
177 eLUT.push_back(phiLUT);
179 for (uint32_t fb = 0; fb < nCalSideBins; fb++) {
180 for (uint32_t ecalInput = 0; ecalInput <= 0xFF; ecalInput++) {
181 uint32_t
value = ecalInput;
183 double linearizedECalInput = ecalInput * ecalLSB;
186 for (;
etBin < ecalScaleETBins.size();
etBin++) {
187 if (linearizedECalInput < ecalScaleETBins[
etBin])
190 if (
etBin >= ecalScaleETBins.size())
191 etBin = ecalScaleETBins.size() - 1;
193 double calibratedECalInput = linearizedECalInput;
195 calibratedECalInput *= ecalSF.at(
phiBin * ecalScaleETBins.size() * 28 +
etBin * 28 +
etaBin);
197 calibratedECalInput /= caloLSB;
199 value = calibratedECalInput;
203 if (
value > 0xFF || ecalInput == 0xFF) {
215 uint32_t et_log2 = ((uint32_t)log2(
value)) & 0x7;
216 value |= (et_log2 << 12);
227 std::array<std::array<std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins> phiLUT;
228 hLUT.push_back(phiLUT);
233 if (
pos != hcalScalePhiBins.end()) {
238 iPhi =
index * 4 + 1;
240 iPhi = (
index - 18) * 4 + 1;
243 for (uint32_t fb = 0; fb < nCalSideBins; fb++) {
244 for (uint32_t hcalInput = 0; hcalInput <= 0xFF; hcalInput++) {
245 uint32_t
value = hcalInput;
248 double linearizedHcalInput = decodeHcalEt(caloEta, hcalInput, iPhi);
251 for (;
etBin < hcalScaleETBins.size();
etBin++) {
252 if (linearizedHcalInput < hcalScaleETBins[
etBin])
255 if (
etBin >= hcalScaleETBins.size())
256 etBin = hcalScaleETBins.size() - 1;
258 double calibratedHcalInput = linearizedHcalInput;
260 calibratedHcalInput *= hcalSF.at(
phiBin * hcalScaleETBins.size() * 28 +
etBin * 28 +
etaBin);
262 calibratedHcalInput /= caloLSB;
264 value = calibratedHcalInput;
267 if (
value > 0xFF || hcalInput == 0xFF) {
279 uint32_t et_log2 = ((uint32_t)log2(
value)) & 0x7;
280 value |= (et_log2 << 12);
291 std::array<std::array<uint32_t, nEtBins>, nHfEtaBins> phiLUT;
292 hfLUT.push_back(phiLUT);
294 int caloEta =
etaBin + 30;
297 if (
pos != hfScalePhiBins.end()) {
301 iPhi =
index * 4 - 1;
303 iPhi = (
index - 18) * 4 - 1;
308 for (uint32_t etCode = 0; etCode < nEtBins; etCode++) {
309 uint32_t
value = etCode;
311 double linearizedHFInput = 0;
313 linearizedHFInput = decodeHcalEt(caloEta,
value, iPhi);
317 for (;
etBin < hfScaleETBins.size();
etBin++) {
318 if (linearizedHFInput < hfScaleETBins[
etBin])
321 if (
etBin >= hfScaleETBins.size())
322 etBin = hfScaleETBins.size() - 1;
324 double calibratedHFInput = linearizedHFInput;
328 calibratedHFInput /= caloLSB;
331 uint32_t absCaloEta =
std::abs(caloEta);
332 if (absCaloEta > 29 && absCaloEta < 40) {
334 calibratedHFInput *= 0.5;
335 }
else if (absCaloEta == 40 || absCaloEta == 41) {
337 calibratedHFInput *= 0.25;
339 value = calibratedHFInput;
341 if (
value >= 0xFF || etCode == 0xFF) {
345 value = calibratedHFInput;
362 hcalFBLUT.push_back(
value);
366 for (uint32_t isPos = 0; isPos < 2; isPos++) {
367 for (uint32_t iPhi = 1; iPhi <= 72; iPhi++) {
368 uint32_t card = floor((iPhi + 1) / 4);
371 ePhiMap[isPos * 72 + iPhi - 1] = ecalScalePhiBins[isPos * 18 + card];
372 hPhiMap[isPos * 72 + iPhi - 1] = hcalScalePhiBins[isPos * 18 + card];
373 hfPhiMap[isPos * 72 + iPhi - 1] = hfScalePhiBins[isPos * 18 + card];
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Log< level::Error, false > LogError
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
T const * product() const
Abs< T >::type abs(const T &t)
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
unsigned long long uint64_t
bool L1TCaloLayer1FetchLUTs(const L1TCaloLayer1FetchLUTsTokens &iTokens, const edm::EventSetup &iSetup, std::vector< std::array< std::array< std::array< uint32_t, nEtBins >, nCalSideBins >, nCalEtaBins > > &eLUT, std::vector< std::array< std::array< std::array< uint32_t, nEtBins >, nCalSideBins >, nCalEtaBins > > &hLUT, std::vector< std::array< std::array< uint32_t, nEtBins >, nHfEtaBins > > &hfLUT, std::vector< unsigned long long int > &hcalFBLUT, std::vector< unsigned int > &ePhiMap, std::vector< unsigned int > &hPhiMap, std::vector< unsigned int > &hfPhiMap, bool useLSB, bool useCalib, bool useECALLUT, bool useHCALLUT, bool useHFLUT, bool useHCALFBLUT, int fwVersion)