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 
20  const std::string& decompressionFile)
21  : theTopology(nullptr),
22  nominal_gain_(0.), lsb_factor_(0.), rct_factor_(1.), nct_factor_(1.),
23  lin8_factor_(1.), lin11_factor_(1.),
24  compressionFile_(compressionFile),
25  decompressionFile_(decompressionFile)
26 {
27 }
28 
30 }
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 
57  if (not HcalGenericDetId(*i).isHcalTrigTowerDetId()) {
58  if ((not HcalGenericDetId(*i).isHcalDetId()) and
59  (not HcalGenericDetId(*i).isHcalZDCDetId()) and
61  edm::LogWarning("CaloTPGTranscoderULUT") << "Encountered invalid HcalDetId " << HcalGenericDetId(*i);
62  continue;
63  }
64 
66  if(!theTopology->validHT(id)) continue;
67 
68  unsigned int index = getOutputLUTId(id);
69 
70  const HcalLutMetadatum *meta = lutMetadata.getValues(id);
71  unsigned int threshold = meta->getOutputLutThreshold();
72 
73  int ieta=id.ieta();
74  int version=id.version();
75  bool isHBHE = (abs(ieta) < theTrigTowerGeometry.firstHFTower(version));
76 
77  unsigned int lutsize = getOutputLUTSize(id);
78  outputLUT_[index].resize(lutsize);
79 
80  for (unsigned int i = 0; i < threshold; ++i)
81  outputLUT_[index][i] = 0;
82 
83  if (isHBHE) {
84  for (unsigned int i = threshold; i < lutsize; ++i)
85  if (allLinear_) {
86  outputLUT_[index][i] = isOnlyQIE11(id) ? linearQIE11LUT[i] : linearQIE8LUT[i];
87  } else {
88  outputLUT_[index][i] = analyticalLUT[i];
89  }
90  } else {
91  for (unsigned int i = threshold; i < lutsize; ++i)
92  outputLUT_[index][i] = version == 0 ? linearRctLUT[i] : linearNctLUT[i];
93  }
94 
95  double eta_low = 0., eta_high = 0.;
96  theTrigTowerGeometry.towerEtaBounds(ieta,version,eta_low,eta_high);
97  double cosh_ieta = fabs(cosh((eta_low + eta_high)/2.));
98  double granularity = meta->getLutGranularity();
99 
100  if(isHBHE){
101  if (allLinear_) {
102  LUT tpg = outputLUT_[index][0];
103  hcaluncomp_[index][tpg] = 0;
104  for (unsigned int i = 0; i < lutsize; ++i){
105  if (outputLUT_[index][i] != tpg){
106  tpg = outputLUT_[index][i];
108  }
109  }
110  } else {
111  double factor = nominal_gain_ / cosh_ieta * granularity;
112  LUT tpg = outputLUT_[index][0];
113  int low = 0;
114  for (unsigned int i = 0; i < lutsize; ++i){
115  if (outputLUT_[index][i] != tpg){
116  unsigned int mid = (low + i)/2;
117  hcaluncomp_[index][tpg] = (tpg == 0 ? low : factor * mid);
118  low = i;
119  tpg = outputLUT_[index][i];
120  }
121  }
122  hcaluncomp_[index][tpg] = factor * low;
123  }
124  }
125  else{
126  LUT tpg = outputLUT_[index][0];
127  hcaluncomp_[index][tpg]=0;
128  for (unsigned int i = 0; i < lutsize; ++i){
129  if (outputLUT_[index][i] != tpg){
130  tpg = outputLUT_[index][i];
131  hcaluncomp_[index][tpg] = lsb_factor_ * i / (version==0?rct_factor_:nct_factor_);
132  }
133  }
134  }
135  }
136 
137 }
138 
140  unsigned int itower = getOutputLUTId(id);
141 
142  if (sample >= getOutputLUTSize(id))
143  throw cms::Exception("Out of Range")
144  << "LUT has " << getOutputLUTSize(id) << " entries for " << id << " but " << sample << " was requested.";
145 
146  if(itower >= outputLUT_.size())
147  throw cms::Exception("Out of Range") << "No decompression LUT found for " << id;
148 
149  return HcalTriggerPrimitiveSample(outputLUT_[itower][sample], fineGrain);
150 }
151 
152 double CaloTPGTranscoderULUT::hcaletValue(const int& ieta, const int& iphi, const int& version, const int& compET) const {
153  double etvalue = 0.;
154  int itower = getOutputLUTId(ieta,iphi, version);
155  if (itower < 0) {
156  edm::LogError("CaloTPGTranscoderULUT") << "No decompression LUT found for ieta, iphi = " << ieta << ", " << iphi;
157  } else if (compET < 0 || compET >= (int) TPGMAX) {
158  edm::LogError("CaloTPGTranscoderULUT") << "Compressed value out of range: eta, phi, cET = " << ieta << ", " << iphi << ", " << compET;
159  } else {
160  etvalue = hcaluncomp_[itower][compET];
161  }
162  return(etvalue);
163 }
164 
166  int compET = hc.compressedEt(); // to be within the range by the class
167  int itower = getOutputLUTId(hid);
168  double etvalue = hcaluncomp_[itower][compET];
169  return etvalue;
170 }
171 
173  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::ecalCompress";
174 }
175 
178  unsigned int& et, bool& egVecto, bool& activity) const {
179  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctEGammaUncompress";
180 }
183  unsigned int& et) const {
184  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctJetUncompress";
185 }
186 
187 bool CaloTPGTranscoderULUT::HTvalid(const int ieta, const int iphiin, const int version) const {
188  HcalTrigTowerDetId id(ieta, iphiin);
189  id.setVersion(version);
190  if (!theTopology) {
191  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
192  }
193  return theTopology->validHT(id);
194 }
195 
197  if (!theTopology) {
198  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
199  }
200  return theTopology->detId2denseIdHT(id);
201 }
202 
203 int CaloTPGTranscoderULUT::getOutputLUTId(const int ieta, const int iphiin, const int version) const {
204  if (!theTopology) {
205  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
206  }
207  HcalTrigTowerDetId id(ieta, iphiin);
208  id.setVersion(version);
209  return theTopology->detId2denseIdHT(id);
210 }
211 
212 unsigned int
214 {
215  if (!theTopology)
216  throw cms::Exception("CaloTPGTranscoderULUT")
217  << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
218 
219  switch (theTopology->triggerMode()) {
222  return QIE8_OUTPUT_LUT_SIZE;
224  if (id.ietaAbs() <= theTopology->lastHERing())
225  return QIE8_OUTPUT_LUT_SIZE;
226  else
227  return QIE10_OUTPUT_LUT_SIZE;
229  if (plan1_towers_.find(id) != plan1_towers_.end())
230  return QIE11_OUTPUT_LUT_SIZE;
231  else if (id.ietaAbs() <= theTopology->lastHERing())
232  return QIE8_OUTPUT_LUT_SIZE;
233  else
234  return QIE10_OUTPUT_LUT_SIZE;
237  if (id.ietaAbs() <= theTopology->lastHBRing())
238  return QIE8_OUTPUT_LUT_SIZE;
239  else if (id.ietaAbs() <= theTopology->lastHERing())
240  return QIE11_OUTPUT_LUT_SIZE;
241  else
242  return QIE10_OUTPUT_LUT_SIZE;
244  if (id.ietaAbs() <= theTopology->lastHERing())
245  return QIE11_OUTPUT_LUT_SIZE;
246  else
247  return QIE10_OUTPUT_LUT_SIZE;
248  default:
249  throw cms::Exception("CaloTPGTranscoderULUT")
250  << "Unknown trigger mode used by the topology!";
251  }
252 }
253 
254 bool
256 {
257  if (!theTopology)
258  throw cms::Exception("CaloTPGTranscoderULUT")
259  << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
260 
261  switch (theTopology->triggerMode()) {
263  if (plan1_towers_.find(id) != plan1_towers_.end() and id.ietaAbs() != theTopology->lastHBRing())
264  return true;
265  return false;
268  if (id.ietaAbs() <= theTopology->lastHBRing())
269  return false;
270  return true;
272  return true;
273  default:
274  throw cms::Exception("CaloTPGTranscoderULUT")
275  << "Unknown trigger mode used by the topology!";
276  }
277 }
278 
279 
280 const std::vector<unsigned int> CaloTPGTranscoderULUT::getCompressionLUT(const HcalTrigTowerDetId& id) const {
281  int itower = getOutputLUTId(id);
282  auto lut = outputLUT_[itower];
283  std::vector<unsigned int> result(lut.begin(), lut.end());
284  return result;
285 }
286 
287 void CaloTPGTranscoderULUT::setup(HcalLutMetadata const& lutMetadata, HcalTrigTowerGeometry const& theTrigTowerGeometry, int nctScaleShift, int rctScaleShift, double lsbQIE8, double lsbQIE11, bool allLinear)
288 {
289  theTopology = lutMetadata.topo();
290  nominal_gain_ = lutMetadata.getNominalGain();
291  lsb_factor_ = lutMetadata.getRctLsb();
292 
293  allLinear_ = allLinear;
294 
295  rct_factor_ = lsb_factor_/(HcaluLUTTPGCoder::lsb_*(1<<rctScaleShift));
296  nct_factor_ = lsb_factor_/(HcaluLUTTPGCoder::lsb_*(1<<nctScaleShift));
297  lin8_factor_ = lsb_factor_/lsbQIE8;
298  lin11_factor_ = lsb_factor_/lsbQIE11;
299 
300  outputLUT_.resize(theTopology->getHTSize());
301  hcaluncomp_.resize(theTopology->getHTSize());
302 
303  plan1_towers_.clear();
304  for (const auto& id: lutMetadata.getAllChannels()) {
305  if (not (id.det() == DetId::Hcal and theTopology->valid(id)))
306  continue;
307  HcalDetId cell(id);
308  if (not theTopology->dddConstants()->isPlan1(cell))
309  continue;
310  for (const auto& tower: theTrigTowerGeometry.towerIds(cell))
311  plan1_towers_.emplace(tower);
312  }
313 
314  if (compressionFile_.empty() && decompressionFile_.empty()) {
315  loadHCALCompress(lutMetadata,theTrigTowerGeometry);
316  }
317  else {
318  throw cms::Exception("Not Implemented") << "setup of CaloTPGTranscoderULUT from text files";
319  }
320 }
const HcalDDDRecConstants * dddConstants() const
Definition: HcalTopology.h:168
std::vector< RCTdecompression > hcaluncomp_
bool isHcalZDCDetId() const
std::vector< HcalTrigTowerDetId > towerIds(const HcalDetId &cellId) const
the mapping to and from DetIds
std::vector< std::vector< LUT > > outputLUT_
std::set< HcalDetId > plan1_towers_
static const unsigned int QIE8_OUTPUT_LUT_SIZE
bool valid(const DetId &id) const override
uint8_t getOutputLutThreshold() const
virtual bool HTvalid(const int ieta, const int iphi, const int version) const
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
#define nullptr
virtual const std::vector< unsigned int > getCompressionLUT(const HcalTrigTowerDetId &id) const
int lastHBRing() const
Definition: HcalTopology.h:88
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
uint8_t getLutGranularity() const
HcalTopologyMode::TriggerMode triggerMode() const
Definition: HcalTopology.h:32
const std::string decompressionFile_
bool isOnlyQIE11(const HcalTrigTowerDetId &id) const
HcalTriggerPrimitiveSample hcalCompress(const HcalTrigTowerDetId &id, unsigned int sample, int fineGrain) const override
Compression from linear samples+fine grain in the HTR.
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.
bool isHcalTrigTowerDetId() const
const HcalTopology * theTopology
std::vector< DetId > getAllChannels() const
T sqrt(T t)
Definition: SSEVec.h:18
float getNominalGain() const
static const unsigned int QIE10_OUTPUT_LUT_SIZE
virtual int getOutputLUTId(const HcalTrigTowerDetId &id) const
unsigned int getHTSize() const
Definition: HcalTopology.h:142
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
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
et
define resolution functions of each parameter
double hcaletValue(const int &ieta, const int &iphi, const int &version, const int &compressedValue) const override
const std::string compressionFile_
static const unsigned int TPGMAX
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.
susybsm::HSCParticleCollection hc
Definition: classes.h:25
int compressedEt() const
get the encoded/compressed Et
bool isPlan1(const HcalDetId &id) const
bool isHcalCastorDetId() const
int lastHERing() const
Definition: HcalTopology.h:90
const HcalTopology * topo() const
bool validHT(const HcalTrigTowerDetId &id) const
int firstHFTower(int version) const