CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  } else {
87  outputLUT_[index][i] = analyticalLUT[i];
88  }
89  } else {
90  for (unsigned int i = threshold; i < lutsize; ++i)
91  outputLUT_[index][i] = version == 1 ? linearNctLUT[i] : linearRctLUT[i];
92  }
93 
94  double eta_low = 0., eta_high = 0.;
95  theTrigTowerGeometry.towerEtaBounds(ieta, version, eta_low, eta_high);
96  double cosh_ieta = fabs(cosh((eta_low + eta_high) / 2.));
97  double granularity = meta->getLutGranularity();
98 
99  if (isHBHE) {
100  if (allLinear_) {
101  LUT tpg = outputLUT_[index][0];
102  hcaluncomp_[index][tpg] = 0;
103  for (unsigned int i = 0; i < lutsize; ++i) {
104  if (outputLUT_[index][i] != tpg) {
105  tpg = outputLUT_[index][i];
107  }
108  }
109  } else {
110  double factor = nominal_gain_ / cosh_ieta * granularity;
111  LUT tpg = outputLUT_[index][0];
112  int low = 0;
113  for (unsigned int i = 0; i < lutsize; ++i) {
114  if (outputLUT_[index][i] != tpg) {
115  unsigned int mid = (low + i) / 2;
116  hcaluncomp_[index][tpg] = (tpg == 0 ? low : factor * mid);
117  low = i;
118  tpg = outputLUT_[index][i];
119  }
120  }
121  hcaluncomp_[index][tpg] = factor * low;
122  }
123  } else {
124  LUT tpg = outputLUT_[index][0];
125  hcaluncomp_[index][tpg] = 0;
126  for (unsigned int i = 0; i < lutsize; ++i) {
127  if (outputLUT_[index][i] != tpg) {
128  tpg = outputLUT_[index][i];
129  hcaluncomp_[index][tpg] = lsb_factor_ * i / (version == 1 ? nct_factor_ : rct_factor_);
130  }
131  }
132  }
133  }
134 }
135 
137  unsigned int sample,
138  int fineGrain) const {
139  unsigned int itower = getOutputLUTId(id);
140 
141  if (sample >= getOutputLUTSize(id))
142  throw cms::Exception("Out of Range") << "LUT has " << getOutputLUTSize(id) << " entries for " << id << " but "
143  << sample << " was requested.";
144 
145  if (itower >= outputLUT_.size())
146  throw cms::Exception("Out of Range") << "No decompression LUT found for " << id;
147 
148  return HcalTriggerPrimitiveSample(outputLUT_[itower][sample], fineGrain);
149 }
150 
151 double CaloTPGTranscoderULUT::hcaletValue(const int& ieta,
152  const int& iphi,
153  const int& version,
154  const int& compET) const {
155  double etvalue = 0.;
156  int itower = getOutputLUTId(ieta, iphi, version);
157  if (itower < 0) {
158  edm::LogError("CaloTPGTranscoderULUT") << "No decompression LUT found for ieta, iphi = " << ieta << ", " << iphi;
159  } else if (compET < 0 || compET >= (int)TPGMAX) {
160  edm::LogError("CaloTPGTranscoderULUT")
161  << "Compressed value out of range: eta, phi, cET = " << ieta << ", " << iphi << ", " << compET;
162  } else {
163  etvalue = hcaluncomp_[itower][compET];
164  }
165  return (etvalue);
166 }
167 
169  int compET = hc.compressedEt(); // to be within the range by the class
170  int itower = getOutputLUTId(hid);
171  double etvalue = hcaluncomp_[itower][compET];
172  return etvalue;
173 }
174 
176  unsigned int sample,
177  bool fineGrain) const {
178  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::ecalCompress";
179 }
180 
182  const HcalTriggerPrimitiveSample& hc,
183  const EcalTrigTowerDetId& eid,
184  const EcalTriggerPrimitiveSample& ec,
185  unsigned int& et,
186  bool& egVecto,
187  bool& activity) const {
188  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctEGammaUncompress";
189 }
191  const HcalTriggerPrimitiveSample& hc,
192  const EcalTrigTowerDetId& eid,
193  const EcalTriggerPrimitiveSample& ec,
194  unsigned int& et) const {
195  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctJetUncompress";
196 }
197 
198 bool CaloTPGTranscoderULUT::HTvalid(const int ieta, const int iphiin, const int version) const {
199  HcalTrigTowerDetId id(ieta, iphiin);
200  id.setVersion(version);
201  if (!theTopology) {
202  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
203  }
204  return theTopology->validHT(id);
205 }
206 
208  if (!theTopology) {
209  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
210  }
211  return theTopology->detId2denseIdHT(id);
212 }
213 
214 int CaloTPGTranscoderULUT::getOutputLUTId(const int ieta, const int iphiin, const int version) const {
215  if (!theTopology) {
216  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
217  }
218  HcalTrigTowerDetId id(ieta, iphiin);
219  id.setVersion(version);
220  return theTopology->detId2denseIdHT(id);
221 }
222 
224  if (!theTopology)
225  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
226 
227  switch (theTopology->triggerMode()) {
230  return QIE8_OUTPUT_LUT_SIZE;
232  if (id.ietaAbs() <= theTopology->lastHERing())
233  return QIE8_OUTPUT_LUT_SIZE;
234  else
235  return QIE10_OUTPUT_LUT_SIZE;
237  if (plan1_towers_.find(id) != plan1_towers_.end())
238  return QIE11_OUTPUT_LUT_SIZE;
239  else if (id.ietaAbs() <= theTopology->lastHERing())
240  return QIE8_OUTPUT_LUT_SIZE;
241  else
242  return QIE10_OUTPUT_LUT_SIZE;
245  if (id.ietaAbs() <= theTopology->lastHBRing())
246  return QIE8_OUTPUT_LUT_SIZE;
247  else if (id.ietaAbs() <= theTopology->lastHERing())
248  return QIE11_OUTPUT_LUT_SIZE;
249  else
250  return QIE10_OUTPUT_LUT_SIZE;
252  if (id.ietaAbs() <= theTopology->lastHBHERing())
253  return QIE11_OUTPUT_LUT_SIZE;
254  else
255  return QIE10_OUTPUT_LUT_SIZE;
256  default:
257  throw cms::Exception("CaloTPGTranscoderULUT") << "Unknown trigger mode used by the topology!";
258  }
259 }
260 
262  if (!theTopology)
263  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
264 
265  switch (theTopology->triggerMode()) {
267  if (plan1_towers_.find(id) != plan1_towers_.end() and id.ietaAbs() != theTopology->lastHBRing())
268  return true;
269  return false;
272  if (id.ietaAbs() <= theTopology->lastHBRing())
273  return false;
274  return true;
276  return true;
277  default:
278  throw cms::Exception("CaloTPGTranscoderULUT") << "Unknown trigger mode used by the topology!";
279  }
280 }
281 
282 const std::vector<unsigned int> CaloTPGTranscoderULUT::getCompressionLUT(const HcalTrigTowerDetId& id) const {
283  int itower = getOutputLUTId(id);
284  auto lut = outputLUT_[itower];
285  std::vector<unsigned int> result(lut.begin(), lut.end());
286  return result;
287 }
288 
290  HcalTrigTowerGeometry const& theTrigTowerGeometry,
291  int nctScaleShift,
292  int rctScaleShift,
293  double lsbQIE8,
294  double lsbQIE11,
295  bool allLinear) {
296  theTopology = lutMetadata.topo();
297  nominal_gain_ = lutMetadata.getNominalGain();
298  lsb_factor_ = lutMetadata.getRctLsb();
299 
300  allLinear_ = allLinear;
301 
302  rct_factor_ = lsb_factor_ / (HcaluLUTTPGCoder::lsb_ * (1 << rctScaleShift));
303  nct_factor_ = lsb_factor_ / (HcaluLUTTPGCoder::lsb_ * (1 << nctScaleShift));
304  lin8_factor_ = lsb_factor_ / lsbQIE8;
305  lin11_factor_ = lsb_factor_ / lsbQIE11;
306 
307  outputLUT_.resize(theTopology->getHTSize());
308  hcaluncomp_.resize(theTopology->getHTSize());
309 
310  plan1_towers_.clear();
311  for (const auto& id : lutMetadata.getAllChannels()) {
312  if (not(id.det() == DetId::Hcal and theTopology->valid(id)))
313  continue;
314  HcalDetId cell(id);
315  if (not theTopology->dddConstants()->isPlan1(cell))
316  continue;
317  for (const auto& tower : theTrigTowerGeometry.towerIds(cell))
318  plan1_towers_.emplace(tower);
319  }
320 
321  if (compressionFile_.empty() && decompressionFile_.empty()) {
322  loadHCALCompress(lutMetadata, theTrigTowerGeometry);
323  } else {
324  throw cms::Exception("Not Implemented") << "setup of CaloTPGTranscoderULUT from text files";
325  }
326 }
const HcalDDDRecConstants * dddConstants() const
Definition: HcalTopology.h:164
std::vector< RCTdecompression > hcaluncomp_
static std::vector< std::string > checklist log
bool isHcalZDCDetId() const
std::vector< HcalTrigTowerDetId > towerIds(const HcalDetId &cellId) const
the mapping to and from DetIds
std::vector< std::vector< LUT > > outputLUT_
uint16_t *__restrict__ id
std::set< HcalDetId > plan1_towers_
static const unsigned int QIE8_OUTPUT_LUT_SIZE
uint8_t getOutputLutThreshold() const
virtual bool HTvalid(const int ieta, const int iphi, const int version) const
bool valid(const DetId &id) const override
unsigned int detId2denseIdHT(const DetId &id) const
return a linear packed id from HT
virtual void setup(HcalLutMetadata const &, HcalTrigTowerGeometry const &, int nctScaleShift, int rctScaleShift, double lsbQIE8, double lsbQIE11, bool allLinear)
unsigned int getOutputLUTSize(const HcalTrigTowerDetId &id) const
virtual const std::vector< unsigned int > getCompressionLUT(const HcalTrigTowerDetId &id) const
int lastHBRing() const
Definition: HcalTopology.h:92
static const float lsb_
void towerEtaBounds(int ieta, int version, double &eta1, double &eta2) const
where this tower begins and ends in eta
CaloTPGTranscoderULUT(const std::string &compressionFile="", const std::string &decompressionFile="")
const Item * getValues(DetId fId, bool throwOnFail=true) const
Log< level::Error, false > LogError
uint8_t getLutGranularity() const
HcalTopologyMode::TriggerMode triggerMode() const
Definition: HcalTopology.h:35
const std::string decompressionFile_
bool isOnlyQIE11(const HcalTrigTowerDetId &id) const
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.
tuple result
Definition: mps_fire.py:311
HcalTriggerPrimitiveSample hcalCompress(const HcalTrigTowerDetId &id, unsigned int sample, int fineGrain) const override
Compression from linear samples+fine grain in the HTR.
bool isHcalTrigTowerDetId() const
const HcalTopology * theTopology
std::vector< DetId > getAllChannels() const
T sqrt(T t)
Definition: SSEVec.h:19
float getNominalGain() const
static const unsigned int QIE10_OUTPUT_LUT_SIZE
virtual int getOutputLUTId(const HcalTrigTowerDetId &id) const
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.
unsigned int getHTSize() const
Definition: HcalTopology.h:136
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static const unsigned int OUTPUT_LUT_SIZE
T min(T a, T b)
Definition: MathUtil.h:58
bool isHcalDetId() const
int lastHBHERing() const
Definition: HcalTopology.h:95
void loadHCALCompress(HcalLutMetadata const &, HcalTrigTowerGeometry 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
float getRctLsb() const
const std::string compressionFile_
static const unsigned int TPGMAX
int compressedEt() const
get the encoded/compressed Et
bool isPlan1(const HcalDetId &id) const
bool isHcalCastorDetId() const
Log< level::Warning, false > LogWarning
double hcaletValue(const int &ieta, const int &iphi, const int &version, const int &compressedValue) const override
int lastHERing() const
Definition: HcalTopology.h:94
const HcalTopology * topo() const
bool validHT(const HcalTrigTowerDetId &id) const
int firstHFTower(int version) const