CMS 3D CMS Logo

CaloTPGTranscoderULUT.cc
Go to the documentation of this file.
8 #include <iostream>
9 #include <fstream>
10 #include <cmath>
11 
12 //#include "FWCore/Framework/interface/Frameworkfwd.h"
16 
17 using namespace std;
18 
19 CaloTPGTranscoderULUT::CaloTPGTranscoderULUT(const std::string& compressionFile, const std::string& decompressionFile)
20  : theTopology(nullptr),
21  nominal_gain_(0.),
22  lsb_factor_(0.),
23  rct_factor_(1.),
24  nct_factor_(1.),
25  lin8_factor_(1.),
26  lin11_factor_(1.),
27  compressionFile_(compressionFile),
28  decompressionFile_(decompressionFile) {}
29 
31 
33  HcalTrigTowerGeometry const& theTrigTowerGeometry) {
34  if (!theTopology) {
35  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
36  }
37 
38  std::array<unsigned int, OUTPUT_LUT_SIZE> analyticalLUT;
39  std::array<unsigned int, OUTPUT_LUT_SIZE> linearQIE8LUT;
40  std::array<unsigned int, OUTPUT_LUT_SIZE> linearQIE11LUT;
41  std::array<unsigned int, OUTPUT_LUT_SIZE> linearRctLUT;
42  std::array<unsigned int, OUTPUT_LUT_SIZE> linearNctLUT;
43 
44  // Compute compression LUT
45  for (unsigned int i = 0; i < OUTPUT_LUT_SIZE; i++) {
46  analyticalLUT[i] = min(static_cast<unsigned int>(sqrt(14.94 * log(1. + i / 14.94) * i) + 0.5), TPGMAX - 1);
47  linearQIE8LUT[i] = min(static_cast<unsigned int>((i + .5) / lin8_factor_), TPGMAX - 1);
48  linearQIE11LUT[i] = min(static_cast<unsigned int>((i + .5) / lin11_factor_), TPGMAX - 1);
49  linearRctLUT[i] = min(static_cast<unsigned int>((i + .5) / rct_factor_), TPGMAX - 1);
50  linearNctLUT[i] = min(static_cast<unsigned int>((i + .5) / nct_factor_), TPGMAX - 1);
51  }
52 
53  std::vector<DetId> allChannels = lutMetadata.getAllChannels();
54 
55  for (std::vector<DetId>::iterator i = allChannels.begin(); i != allChannels.end(); ++i) {
56  if (not HcalGenericDetId(*i).isHcalTrigTowerDetId()) {
57  if ((not HcalGenericDetId(*i).isHcalDetId()) and (not HcalGenericDetId(*i).isHcalZDCDetId()) and
59  edm::LogWarning("CaloTPGTranscoderULUT") << "Encountered invalid HcalDetId " << HcalGenericDetId(*i);
60  continue;
61  }
62 
64  if (!theTopology->validHT(id))
65  continue;
66 
67  unsigned int index = getOutputLUTId(id);
68 
69  const HcalLutMetadatum* meta = lutMetadata.getValues(id);
70  unsigned int threshold = meta->getOutputLutThreshold();
71 
72  int ieta = id.ieta();
73  int version = id.version();
74  bool isHBHE = (abs(ieta) < theTrigTowerGeometry.firstHFTower(version));
75 
76  unsigned int lutsize = getOutputLUTSize(id);
77  outputLUT_[index].resize(lutsize);
78 
79  for (unsigned int i = 0; i < threshold; ++i)
80  outputLUT_[index][i] = 0;
81 
82  if (isHBHE) {
83  for (unsigned int i = threshold; i < lutsize; ++i)
84  if (allLinear_) {
85  outputLUT_[index][i] = isOnlyQIE11(id) ? linearQIE11LUT[i] : linearQIE8LUT[i];
86  //Modifying the saturation (127 -> 255) for the 'split cells'.
87  if (abs(ieta) > 20 && isOnlyQIE11(id) && linearQIE11LUT[i] >= (TPGMAX - 2) / 2.) {
88  outputLUT_[index][i] = TPGMAX - 1;
89  }
90  } else {
91  outputLUT_[index][i] = analyticalLUT[i];
92  }
93  } else {
94  for (unsigned int i = threshold; i < lutsize; ++i)
95  outputLUT_[index][i] = version == 1 ? linearNctLUT[i] : linearRctLUT[i];
96  }
97 
98  double eta_low = 0., eta_high = 0.;
99  theTrigTowerGeometry.towerEtaBounds(ieta, version, eta_low, eta_high);
100  double cosh_ieta = fabs(cosh((eta_low + eta_high) / 2.));
101  double granularity = meta->getLutGranularity();
102 
103  if (isHBHE) {
104  if (allLinear_) {
105  LUT tpg = outputLUT_[index][0];
106  hcaluncomp_[index][tpg] = 0;
107  for (unsigned int i = 0; i < lutsize; ++i) {
108  if (outputLUT_[index][i] != tpg) {
109  tpg = outputLUT_[index][i];
111  //Modifying the saturation for the 'split cells'
112  if (abs(ieta) > 20 && isOnlyQIE11(id) && linearQIE11LUT[i] >= (TPGMAX - 2) / 2.) {
113  hcaluncomp_[index][tpg] = (TPGMAX - 1) / 2.;
114  }
115  }
116  }
117  } else {
118  double factor = nominal_gain_ / cosh_ieta * granularity;
119  LUT tpg = outputLUT_[index][0];
120  int low = 0;
121  for (unsigned int i = 0; i < lutsize; ++i) {
122  if (outputLUT_[index][i] != tpg) {
123  unsigned int mid = (low + i) / 2;
124  hcaluncomp_[index][tpg] = (tpg == 0 ? low : factor * mid);
125  low = i;
126  tpg = outputLUT_[index][i];
127  }
128  }
129  hcaluncomp_[index][tpg] = factor * low;
130  }
131  } else {
132  LUT tpg = outputLUT_[index][0];
133  hcaluncomp_[index][tpg] = 0;
134  for (unsigned int i = 0; i < lutsize; ++i) {
135  if (outputLUT_[index][i] != tpg) {
136  tpg = outputLUT_[index][i];
138  }
139  }
140  }
141  }
142 }
143 
145  unsigned int sample,
146  int fineGrain) const {
147  unsigned int itower = getOutputLUTId(id);
148 
149  if (sample >= getOutputLUTSize(id))
150  throw cms::Exception("Out of Range") << "LUT has " << getOutputLUTSize(id) << " entries for " << id << " but "
151  << sample << " was requested.";
152 
153  if (itower >= outputLUT_.size())
154  throw cms::Exception("Out of Range") << "No decompression LUT found for " << id;
155 
156  return HcalTriggerPrimitiveSample(outputLUT_[itower][sample], fineGrain);
157 }
158 
160  const int& iphi,
161  const int& version,
162  const int& compET) const {
163  double etvalue = 0.;
164  int itower = getOutputLUTId(ieta, iphi, version);
165  if (itower < 0) {
166  edm::LogError("CaloTPGTranscoderULUT") << "No decompression LUT found for ieta, iphi = " << ieta << ", " << iphi;
167  } else if (compET < 0 || compET >= (int)TPGMAX) {
168  edm::LogError("CaloTPGTranscoderULUT")
169  << "Compressed value out of range: eta, phi, cET = " << ieta << ", " << iphi << ", " << compET;
170  } else {
171  etvalue = hcaluncomp_[itower][compET];
172  }
173  return (etvalue);
174 }
175 
177  int compET = hc.compressedEt(); // to be within the range by the class
178  int itower = getOutputLUTId(hid);
179  double etvalue = hcaluncomp_[itower][compET];
180  return etvalue;
181 }
182 
184  unsigned int sample,
185  bool fineGrain) const {
186  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::ecalCompress";
187 }
188 
190  const HcalTriggerPrimitiveSample& hc,
191  const EcalTrigTowerDetId& eid,
192  const EcalTriggerPrimitiveSample& ec,
193  unsigned int& et,
194  bool& egVecto,
195  bool& activity) const {
196  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctEGammaUncompress";
197 }
199  const HcalTriggerPrimitiveSample& hc,
200  const EcalTrigTowerDetId& eid,
201  const EcalTriggerPrimitiveSample& ec,
202  unsigned int& et) const {
203  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctJetUncompress";
204 }
205 
206 bool CaloTPGTranscoderULUT::HTvalid(const int ieta, const int iphiin, const int version) const {
207  HcalTrigTowerDetId id(ieta, iphiin);
208  id.setVersion(version);
209  if (!theTopology) {
210  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
211  }
212  return theTopology->validHT(id);
213 }
214 
216  if (!theTopology) {
217  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
218  }
219  return theTopology->detId2denseIdHT(id);
220 }
221 
222 int CaloTPGTranscoderULUT::getOutputLUTId(const int ieta, const int iphiin, const int version) const {
223  if (!theTopology) {
224  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
225  }
226  HcalTrigTowerDetId id(ieta, iphiin);
227  id.setVersion(version);
228  return theTopology->detId2denseIdHT(id);
229 }
230 
232  if (!theTopology)
233  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
234 
235  switch (theTopology->triggerMode()) {
238  return QIE8_OUTPUT_LUT_SIZE;
240  if (id.ietaAbs() <= theTopology->lastHERing())
241  return QIE8_OUTPUT_LUT_SIZE;
242  else
243  return QIE10_OUTPUT_LUT_SIZE;
245  if (plan1_towers_.find(id) != plan1_towers_.end())
246  return QIE11_OUTPUT_LUT_SIZE;
247  else if (id.ietaAbs() <= theTopology->lastHERing())
248  return QIE8_OUTPUT_LUT_SIZE;
249  else
250  return QIE10_OUTPUT_LUT_SIZE;
253  if (id.ietaAbs() <= theTopology->lastHBRing())
254  return QIE8_OUTPUT_LUT_SIZE;
255  else if (id.ietaAbs() <= theTopology->lastHERing())
256  return QIE11_OUTPUT_LUT_SIZE;
257  else
258  return QIE10_OUTPUT_LUT_SIZE;
260  if (id.ietaAbs() <= theTopology->lastHBHERing())
261  return QIE11_OUTPUT_LUT_SIZE;
262  else
263  return QIE10_OUTPUT_LUT_SIZE;
264  default:
265  throw cms::Exception("CaloTPGTranscoderULUT") << "Unknown trigger mode used by the topology!";
266  }
267 }
268 
270  if (!theTopology)
271  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
272 
273  switch (theTopology->triggerMode()) {
275  if (plan1_towers_.find(id) != plan1_towers_.end() and id.ietaAbs() != theTopology->lastHBRing())
276  return true;
277  return false;
280  if (id.ietaAbs() <= theTopology->lastHBRing())
281  return false;
282  return true;
284  return true;
285  default:
286  throw cms::Exception("CaloTPGTranscoderULUT") << "Unknown trigger mode used by the topology!";
287  }
288 }
289 
290 const std::vector<unsigned int> CaloTPGTranscoderULUT::getCompressionLUT(const HcalTrigTowerDetId& id) const {
291  int itower = getOutputLUTId(id);
292  auto lut = outputLUT_[itower];
293  std::vector<unsigned int> result(lut.begin(), lut.end());
294  return result;
295 }
296 
298  HcalTrigTowerGeometry const& theTrigTowerGeometry,
299  int nctScaleShift,
300  int rctScaleShift,
301  double lsbQIE8,
302  double lsbQIE11,
303  bool allLinear) {
304  theTopology = lutMetadata.topo();
305  nominal_gain_ = lutMetadata.getNominalGain();
306  lsb_factor_ = lutMetadata.getRctLsb();
307 
308  allLinear_ = allLinear;
309 
310  rct_factor_ = lsb_factor_ / (HcaluLUTTPGCoder::lsb_ * (1 << rctScaleShift));
311  nct_factor_ = lsb_factor_ / (HcaluLUTTPGCoder::lsb_ * (1 << nctScaleShift));
312  lin8_factor_ = lsb_factor_ / lsbQIE8;
313  lin11_factor_ = lsb_factor_ / lsbQIE11;
314 
315  outputLUT_.resize(theTopology->getHTSize());
316  hcaluncomp_.resize(theTopology->getHTSize());
317 
318  plan1_towers_.clear();
319  for (const auto& id : lutMetadata.getAllChannels()) {
320  if (not(id.det() == DetId::Hcal and theTopology->valid(id)))
321  continue;
322  HcalDetId cell(id);
323  if (not theTopology->dddConstants()->isPlan1(cell))
324  continue;
325  for (const auto& tower : theTrigTowerGeometry.towerIds(cell))
326  plan1_towers_.emplace(tower);
327  }
328 
329  if (compressionFile_.empty() && decompressionFile_.empty()) {
330  loadHCALCompress(lutMetadata, theTrigTowerGeometry);
331  } else {
332  throw cms::Exception("Not Implemented") << "setup of CaloTPGTranscoderULUT from text files";
333  }
334 }
bool validHT(const HcalTrigTowerDetId &id) const
std::vector< RCTdecompression > hcaluncomp_
std::vector< std::vector< LUT > > outputLUT_
std::set< HcalDetId > plan1_towers_
static const unsigned int QIE8_OUTPUT_LUT_SIZE
virtual bool HTvalid(const int ieta, const int iphi, const int version) const
int compressedEt() const
get the encoded/compressed Et
uint8_t getLutGranularity() const
bool valid(const DetId &id) const override
virtual void setup(HcalLutMetadata const &, HcalTrigTowerGeometry const &, int nctScaleShift, int rctScaleShift, double lsbQIE8, double lsbQIE11, bool allLinear)
static const float lsb_
uint8_t getOutputLutThreshold() const
CaloTPGTranscoderULUT(const std::string &compressionFile="", const std::string &decompressionFile="")
Log< level::Error, false > LogError
const std::string decompressionFile_
void rctJetUncompress(const HcalTrigTowerDetId &hid, const HcalTriggerPrimitiveSample &hc, const EcalTrigTowerDetId &eid, const EcalTriggerPrimitiveSample &ec, unsigned int &et) const override
Uncompression for the JET path in the RCT.
const Item * getValues(DetId fId, bool throwOnFail=true) const
int firstHFTower(int version) const
ALPAKA_FN_ACC ALPAKA_FN_INLINE uint32_t ietaAbs(uint32_t id)
const HcalTopology * topo() const
virtual const std::vector< unsigned int > getCompressionLUT(const HcalTrigTowerDetId &id) const
bool isHcalDetId() const
HcalTriggerPrimitiveSample hcalCompress(const HcalTrigTowerDetId &id, unsigned int sample, int fineGrain) const override
Compression from linear samples+fine grain in the HTR.
const HcalTopology * theTopology
HcalTopologyMode::TriggerMode triggerMode() const
Definition: HcalTopology.h:31
T sqrt(T t)
Definition: SSEVec.h:23
int lastHBRing() const
Definition: HcalTopology.h:89
static const unsigned int QIE10_OUTPUT_LUT_SIZE
void rctEGammaUncompress(const HcalTrigTowerDetId &hid, const HcalTriggerPrimitiveSample &hc, const EcalTrigTowerDetId &eid, const EcalTriggerPrimitiveSample &ec, unsigned int &et, bool &egVecto, bool &activity) const override
Uncompression for the Electron/Photon path in the RCT.
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static const unsigned int OUTPUT_LUT_SIZE
std::vector< DetId > getAllChannels() const
bool isHcalCastorDetId() const
unsigned int getHTSize() const
Definition: HcalTopology.h:133
void loadHCALCompress(HcalLutMetadata const &, HcalTrigTowerGeometry const &)
float getRctLsb() const
EcalTriggerPrimitiveSample ecalCompress(const EcalTrigTowerDetId &id, unsigned int sample, bool fineGrain) const override
Compression from linear samples+fine grain in the ECAL.
static const unsigned int QIE11_OUTPUT_LUT_SIZE
bool isHcalZDCDetId() const
const std::string compressionFile_
static const unsigned int TPGMAX
void towerEtaBounds(int ieta, int version, double &eta1, double &eta2) const
where this tower begins and ends in eta
int lastHERing() const
Definition: HcalTopology.h:91
const HcalDDDRecConstants * dddConstants() const
Definition: HcalTopology.h:161
unsigned int getOutputLUTSize(const HcalTrigTowerDetId &id) const
Log< level::Warning, false > LogWarning
bool isPlan1(const HcalDetId &id) const
double hcaletValue(const int &ieta, const int &iphi, const int &version, const int &compressedValue) const override
std::vector< HcalTrigTowerDetId > towerIds(const HcalDetId &cellId) const
the mapping to and from DetIds
float getNominalGain() const
unsigned int detId2denseIdHT(const DetId &id) const
return a linear packed id from HT
bool isOnlyQIE11(const HcalTrigTowerDetId &id) const
bool isHcalTrigTowerDetId() const
virtual int getOutputLUTId(const HcalTrigTowerDetId &id) const
int lastHBHERing() const
Definition: HcalTopology.h:92