CMS 3D CMS Logo

Functions
L1TCaloLayer1FetchLUTs.cc File Reference
#include <vector>
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "L1Trigger/L1TCalorimeter/interface/CaloParamsHelper.h"
#include "CondFormats/L1TObjects/interface/CaloParams.h"
#include "CondFormats/DataRecord/interface/L1TCaloStage2ParamsRcd.h"
#include "CalibFormats/CaloTPG/interface/CaloTPGTranscoder.h"
#include "CalibFormats/CaloTPG/interface/CaloTPGRecord.h"
#include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
#include "DataFormats/HcalDigi/interface/HcalTriggerPrimitiveSample.h"
#include "DataFormats/HcalDigi/interface/HcalTriggerPrimitiveDigi.h"
#include "Geometry/HcalTowerAlgo/interface/HcalTrigTowerGeometry.h"
#include "Geometry/Records/interface/CaloGeometryRecord.h"
#include "L1TCaloLayer1FetchLUTs.hh"
#include "UCTLogging.hh"

Go to the source code of this file.

Functions

bool L1TCaloLayer1FetchLUTs (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 int > &ePhiMap, std::vector< unsigned int > &hPhiMap, std::vector< unsigned int > &hfPhiMap, bool useLSB, bool useCalib, bool useECALLUT, bool useHCALLUT, bool useHFLUT)
 

Function Documentation

bool L1TCaloLayer1FetchLUTs ( 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 int > &  ePhiMap,
std::vector< unsigned int > &  hPhiMap,
std::vector< unsigned int > &  hfPhiMap,
bool  useLSB,
bool  useCalib,
bool  useECALLUT,
bool  useHCALLUT,
bool  useHFLUT 
)

Definition at line 29 of file L1TCaloLayer1FetchLUTs.cc.

References funct::abs(), SoftLeptonByDistance_cfi::distance, conversionPostprocessing_cfi::etaBin, conversionPostprocessing_cfi::etBin, benchmark_cfg::fb, spr::find(), edm::EventSetup::get(), CaloTPGTranscoder::hcaletValue(), hcalTTPDigis_cfi::id, diffTreeTool::index, nullptr, conversionPostprocessing_cfi::phiBin, edm::ESHandle< T >::product(), simplePhotonAnalyzer_cfi::sample, l1t::CaloParamsHelper::towerLsbSum(), HcalTrigTowerGeometry::use1x1(), and relativeConstraints::value.

Referenced by L1TCaloLayer1::beginRun().

40  {
41 
42  int hfValid = 1;
44  iSetup.get<CaloGeometryRecord>().get(pG);
45  if (! pG->use1x1()){
46  edm::LogError("L1TCaloLayer1FetchLUTs") << "Using Stage2-Layer1 but HCAL Geometry has use1x1 = 0! HF will be suppressed. Check Global Tag, etc.";
47  hfValid = 0;
48  }
49 
50  // CaloParams contains all persisted parameters for Layer 1
51  edm::ESHandle<l1t::CaloParams> paramsHandle;
52  iSetup.get<L1TCaloStage2ParamsRcd>().get(paramsHandle);
53  if ( paramsHandle.product() == nullptr ) {
54  edm::LogError("L1TCaloLayer1FetchLUTs") << "Missing CaloParams object! Check Global Tag, etc.";
55  return false;
56  }
57  l1t::CaloParamsHelper caloParams(*paramsHandle.product());
58 
59  // Calo Trigger Layer1 output LSB Real ET value
60  double caloLSB = caloParams.towerLsbSum();
61  if ( caloLSB != 0.5 ) {
62  // Lots of things expect this, better give fair warning if not
63  edm::LogError("L1TCaloLayer1FetchLUTs") << "caloLSB (caloParams.towerLsbSum()) != 0.5, actually = " << caloLSB;
64  }
65 
66  // ECal/HCal scale factors will be a x*y*28 array:
67  // ieta = 28 eta scale factors (1 .. 28)
68  // etBin = size of Real ET Bins vector
69  // phiBin = max(Real Phi Bins vector)
70  // So, index = phiBin*etBin*28+etBin*28+ieta
71  auto ecalScaleETBins = caloParams.layer1ECalScaleETBins();
72  auto ecalScalePhiBins = caloParams.layer1ECalScalePhiBins();
73  auto epos = std::max_element(ecalScalePhiBins.begin(), ecalScalePhiBins.end());
74  auto numEcalPhiBins = ecalScalePhiBins[std::distance(ecalScalePhiBins.begin(), epos)]+1;
75  auto ecalSF = caloParams.layer1ECalScaleFactors();
76  if ( ecalSF.size() != ecalScaleETBins.size()*numEcalPhiBins*28 ) {
77  edm::LogError("L1TCaloLayer1FetchLUTs") << "caloParams.layer1ECalScaleFactors().size() != caloParams.layer1ECalScaleETBins().size()*numEcalPhiBins*28 !!";
78  return false;
79  }
80  auto hcalScaleETBins = caloParams.layer1HCalScaleETBins();
81  auto hcalScalePhiBins = caloParams.layer1HCalScalePhiBins();
82  auto hpos = std::max_element(hcalScalePhiBins.begin(), hcalScalePhiBins.end());
83  auto numHcalPhiBins = hcalScalePhiBins[std::distance(hcalScalePhiBins.begin(), hpos)]+1;
84  auto hcalSF = caloParams.layer1HCalScaleFactors();
85  if ( hcalSF.size() != hcalScaleETBins.size()*numHcalPhiBins*28 ) {
86  edm::LogError("L1TCaloLayer1FetchLUTs") << "caloParams.layer1HCalScaleFactors().size() != caloParams.layer1HCalScaleETBins().size()*numHcalPhiBins*28 !!";
87  return false;
88  }
89 
90  // HF 1x1 scale factors will be a x*y*12 array:
91  // ieta = 12 eta scale factors (30 .. 41)
92  // etBin = size of Real ET Bins vector
93  // phiBin = max(Real Phi Bins vector)
94  // So, index = phiBin*etBin*12+etBin*12+ieta
95  auto hfScaleETBins = caloParams.layer1HFScaleETBins();
96  auto hfScalePhiBins = caloParams.layer1HFScalePhiBins();
97  auto hfpos = std::max_element(hfScalePhiBins.begin(), hfScalePhiBins.end());
98  auto numHFPhiBins = hfScalePhiBins[std::distance(hfScalePhiBins.begin(), hfpos)]+1;
99  auto hfSF = caloParams.layer1HFScaleFactors();
100  if ( hfSF.size() != hfScaleETBins.size()*numHFPhiBins*12 ) {
101  edm::LogError("L1TCaloLayer1FetchLUTs") << "caloParams.layer1HFScaleFactors().size() != caloParams.layer1HFScaleETBins().size()*numHFPhiBins*12 !!";
102  return false;
103  }
104 
105  // Sanity check scale factors exist
106  if ( useCalib && (ecalSF.size()==0 || hcalSF.size()==0 || hfSF.size()==0) ) {
107  edm::LogError("L1TCaloLayer1FetchLUTs") << "Layer 1 calibrations requested (useCalib = True) but there are missing scale factors in CaloParams! Please check conditions setup.";
108  return false;
109  }
110  // get energy scale to convert input from ECAL - this should be linear with LSB = 0.5 GeV
111  const double ecalLSB = 0.5;
112 
113  // get energy scale to convert input from HCAL
115  iSetup.get<CaloTPGRecord>().get(decoder);
116  if ( decoder.product() == nullptr ) {
117  edm::LogError("L1TCaloLayer1FetchLUTs") << "Missing CaloTPGTranscoder object! Check Global Tag, etc.";
118  return false;
119  }
120 
121  // TP compression scale is always phi symmetric
122  // We default to 3 since HF has no ieta=41 iphi=1,2
123  auto decodeHcalEt = [&decoder](int iEta, uint32_t compressedEt, uint32_t iPhi=3) -> double {
124  HcalTriggerPrimitiveSample sample(compressedEt);
125  HcalTrigTowerDetId id(iEta, iPhi);
126  if ( std::abs(iEta) >= 30 ) {
127  id.setVersion(1);
128  }
129  return decoder->hcaletValue(id, sample);
130  };
131 
132 
133  // Make ECal LUT
134  for(uint32_t phiBin=0; phiBin<numEcalPhiBins; phiBin++) {
135  std::array< std::array< std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins> phiLUT;
136  eLUT.push_back(phiLUT);
137  for(uint32_t etaBin = 0; etaBin < nCalEtaBins; etaBin++) {
138  for(uint32_t fb = 0; fb < nCalSideBins; fb++) {
139  for(uint32_t ecalInput = 0; ecalInput <= 0xFF; ecalInput++) {
140  uint32_t value = ecalInput;
141  if(useECALLUT) {
142  double linearizedECalInput = ecalInput*ecalLSB; // in GeV
143 
144  uint32_t etBin = 0;
145  for(; etBin < ecalScaleETBins.size(); etBin++) {
146  if(linearizedECalInput < ecalScaleETBins[etBin]) break;
147  }
148  if ( etBin >= ecalScaleETBins.size() ) etBin = ecalScaleETBins.size()-1;
149 
150  double calibratedECalInput = linearizedECalInput;
151  if (useCalib) calibratedECalInput *= ecalSF.at(phiBin*ecalScaleETBins.size()*28 + etBin*28 + etaBin);
152  if (useLSB) calibratedECalInput /= caloLSB;
153 
154  value = calibratedECalInput;
155  if(value > 0xFF) {
156  value = 0xFF;
157  }
158  }
159  if(value == 0) {
160  value = (1 << 11);
161  }
162  else {
163  uint32_t et_log2 = ((uint32_t) log2(value)) & 0x7;
164  value |= (et_log2 << 12);
165  }
166  value |= (fb << 10);
167  eLUT[phiBin][etaBin][fb][ecalInput] = value;
168  }
169  }
170  }
171  }
172 
173  // Make HCal LUT
174  for(uint32_t phiBin=0; phiBin<numHcalPhiBins; phiBin++) {
175  std::array< std::array< std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins> phiLUT;
176  hLUT.push_back(phiLUT);
177  for(uint32_t etaBin = 0; etaBin < nCalEtaBins; etaBin++) {
178  int caloEta = etaBin+1;
179  int iPhi = 3;
180  auto pos = std::find(hcalScalePhiBins.begin(), hcalScalePhiBins.end(), phiBin);
181  if (pos!=hcalScalePhiBins.end()) {
182  // grab an iPhi bin
183  auto index = std::distance(hcalScalePhiBins.begin(),pos);
184  if (index<18) {
185  caloEta*=-1;
186  iPhi = index*4+1;
187  }
188  else {
189  iPhi = (index-18)*4+1;
190  }
191  }
192  for(uint32_t fb = 0; fb < nCalSideBins; fb++) {
193  for(uint32_t hcalInput = 0; hcalInput <= 0xFF; hcalInput++) {
194  uint32_t value = hcalInput;
195  if(useHCALLUT) {
196  // hcaletValue defined in L137 of CalibCalorimetry/CaloTPG/src/CaloTPGTranscoderULUT.cc
197  double linearizedHcalInput = decodeHcalEt(caloEta, hcalInput, iPhi); // in GeV
198 
199  uint32_t etBin = 0;
200  for(; etBin < hcalScaleETBins.size(); etBin++) {
201  if(linearizedHcalInput < hcalScaleETBins[etBin]) break;
202  }
203  if ( etBin >= hcalScaleETBins.size() ) etBin = hcalScaleETBins.size()-1;
204 
205  double calibratedHcalInput = linearizedHcalInput;
206  if(useCalib) calibratedHcalInput *= hcalSF.at(phiBin*hcalScaleETBins.size()*28 + etBin*28 + etaBin);
207  if(useLSB) calibratedHcalInput /= caloLSB;
208 
209  value = calibratedHcalInput;
210  if(value > 0xFF) {
211  value = 0xFF;
212  }
213  }
214  if(value == 0) {
215  value = (1 << 11);
216  }
217  else {
218  uint32_t et_log2 = ((uint32_t) log2(value)) & 0x7;
219  value |= (et_log2 << 12);
220  }
221  value |= (fb << 10);
222  hLUT[phiBin][etaBin][fb][hcalInput] = value;
223  }
224  }
225  }
226  }
227 
228  // Make HF LUT
229  for(uint32_t phiBin=0; phiBin<numHFPhiBins; phiBin++) {
230  std::array< std::array<uint32_t, nEtBins>, nHfEtaBins> phiLUT;
231  hfLUT.push_back(phiLUT);
232  for(uint32_t etaBin = 0; etaBin < nHfEtaBins; etaBin++) {
233  int caloEta = etaBin+30;
234  int iPhi = 3;
235  auto pos = std::find(hfScalePhiBins.begin(), hfScalePhiBins.end(), phiBin);
236  if (pos!=hfScalePhiBins.end()) {
237  auto index = std::distance(hfScalePhiBins.begin(),pos);
238  if (index<18) {
239  caloEta*=-1;
240  iPhi = index*4+1;
241  }
242  else {
243  iPhi = (index-18)*4+1;
244  }
245  if (std::abs(caloEta) == 41 && iPhi<3) iPhi = 3;
246  }
247  for(uint32_t etCode = 0; etCode < nEtBins; etCode++) {
248  uint32_t value = etCode;
249  if(useHFLUT) {
250 
251  double linearizedHFInput = 0;
252  if (hfValid){
253  linearizedHFInput = decodeHcalEt(caloEta, value, iPhi); // in GeV
254  }
255 
256  uint32_t etBin = 0;
257  for(; etBin < hfScaleETBins.size(); etBin++) {
258  if(linearizedHFInput < hfScaleETBins[etBin]) break;
259  }
260  if ( etBin >= hfScaleETBins.size() ) etBin = hfScaleETBins.size()-1;
261 
262  double calibratedHFInput = linearizedHFInput;
263  if(useCalib) calibratedHFInput *= hfSF.at(phiBin*hfScalePhiBins.size()*12+etBin*12+etaBin);
264  if(useLSB) calibratedHFInput /= caloLSB;
265 
266  value = calibratedHFInput;
267  if(value > 0xFF) {
268  value = 0xFF;
269  }
270  }
271  hfLUT[phiBin][etaBin][etCode] = value;
272  }
273  }
274  }
275 
276  // plus/minus, 18 CTP7, 4 iPhi each
277  for(uint32_t isPos=0; isPos<2; isPos++) {
278  for(uint32_t iPhi=1; iPhi<=72; iPhi++) {
279  uint32_t card = floor((iPhi+1)/4);
280  if (card>17) card-=18;
281  ePhiMap[isPos*72+iPhi-1] = ecalScalePhiBins[isPos*18+card];
282  hPhiMap[isPos*72+iPhi-1] = hcalScalePhiBins[isPos*18+card];
283  hfPhiMap[isPos*72+iPhi-1] = hfScalePhiBins[isPos*18+card];
284  }
285  }
286 
287  return true;
288 }
#define nullptr
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Definition: value.py:1
const T & get() const
Definition: EventSetup.h:56
double towerLsbSum() const
virtual double hcaletValue(const int &ieta, const int &iphi, const int &version, const int &compressedValue) const =0
T const * product() const
Definition: ESHandle.h:86