CMS 3D CMS Logo

CaloTPGTranscoderULUT.cc
Go to the documentation of this file.
8 #include <iostream>
9 #include <fstream>
10 #include <math.h>
11 
12 //#include "FWCore/Framework/interface/Frameworkfwd.h"
16 
17 using namespace std;
18 
20  const std::string& decompressionFile)
21  : theTopology(0),
22  nominal_gain_(0.), lsb_factor_(0.), rct_factor_(1.), nct_factor_(1.),
23  compressionFile_(compressionFile),
24  decompressionFile_(decompressionFile)
25 {
26 }
27 
29 }
30 
32  HcalTrigTowerGeometry const& theTrigTowerGeometry) {
33  if (!theTopology) {
34  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
35  }
36 
37  std::array<unsigned int, OUTPUT_LUT_SIZE> analyticalLUT;
38  std::array<unsigned int, OUTPUT_LUT_SIZE> linearRctLUT;
39  std::array<unsigned int, OUTPUT_LUT_SIZE> linearNctLUT;
40 
41  // Compute compression LUT
42  for (unsigned int i=0; i < OUTPUT_LUT_SIZE; i++) {
43  analyticalLUT[i] = min((unsigned int)(sqrt(14.94*log(1.+i/14.94)*i) + 0.5), TPGMAX - 1);
44  linearRctLUT[i] = min((unsigned int)(i/rct_factor_), TPGMAX - 1);
45  linearNctLUT[i] = min((unsigned int)(i/nct_factor_), TPGMAX - 1);
46  }
47 
48  std::vector<DetId> allChannels = lutMetadata.getAllChannels();
49 
50  for(std::vector<DetId>::iterator i=allChannels.begin(); i!=allChannels.end(); ++i){
51 
52  if (not HcalGenericDetId(*i).isHcalTrigTowerDetId()) {
53  if ((not HcalGenericDetId(*i).isHcalDetId()) and
54  (not HcalGenericDetId(*i).isHcalZDCDetId()) and
56  edm::LogWarning("CaloTPGTranscoderULUT") << "Encountered invalid HcalDetId " << HcalGenericDetId(*i);
57  continue;
58  }
59 
61  if(!theTopology->validHT(id)) continue;
62 
63  unsigned int index = getOutputLUTId(id);
64 
65  const HcalLutMetadatum *meta = lutMetadata.getValues(id);
66  unsigned int threshold = meta->getOutputLutThreshold();
67 
68  int ieta=id.ieta();
69  int version=id.version();
70  bool isHBHE = (abs(ieta) < theTrigTowerGeometry.firstHFTower(version));
71 
72  unsigned int lutsize = getOutputLUTSize(id);
73 
74  for (unsigned int i = 0; i < threshold; ++i)
75  outputLUT_[index][i] = 0;
76 
77  if (isHBHE) {
78  for (unsigned int i = threshold; i < lutsize; ++i)
79  outputLUT_[index][i] = analyticalLUT[i];
80  } else {
81  for (unsigned int i = threshold; i < lutsize; ++i)
82  outputLUT_[index][i] = version == 0 ? linearRctLUT[i] : linearNctLUT[i];
83  }
84 
85  double eta_low = 0., eta_high = 0.;
86  theTrigTowerGeometry.towerEtaBounds(ieta,version,eta_low,eta_high);
87  double cosh_ieta = fabs(cosh((eta_low + eta_high)/2.));
88  double granularity = meta->getLutGranularity();
89 
90  if(isHBHE){
91  double factor = nominal_gain_ / cosh_ieta * granularity;
92  LUT tpg = outputLUT_[index][0];
93  int low = 0;
94  for (unsigned int i = 0; i < getOutputLUTSize(id); ++i){
95  if (outputLUT_[index][i] != tpg){
96  unsigned int mid = (low + i)/2;
97  hcaluncomp_[index][tpg] = (tpg == 0 ? low : factor * mid);
98  low = i;
99  tpg = outputLUT_[index][i];
100  }
101  }
102  hcaluncomp_[index][tpg] = factor * low;
103  }
104  else{
105  LUT tpg = outputLUT_[index][0];
106  hcaluncomp_[index][tpg]=0;
107  for (unsigned int i = 0; i < getOutputLUTSize(id); ++i){
108  if (outputLUT_[index][i] != tpg){
109  tpg = outputLUT_[index][i];
110  hcaluncomp_[index][tpg] = lsb_factor_ * i / (version==0?rct_factor_:nct_factor_);
111  }
112  }
113  }
114  }
115 }
116 
118  unsigned int itower = getOutputLUTId(id);
119 
120  if (sample >= getOutputLUTSize(id))
121  throw cms::Exception("Out of Range")
122  << "LUT has " << getOutputLUTSize(id) << " entries for " << id << " but " << sample << " was requested.";
123 
124  if(itower >= outputLUT_.size())
125  throw cms::Exception("Out of Range") << "No decompression LUT found for " << id;
126 
127  return HcalTriggerPrimitiveSample(outputLUT_[itower][sample], fineGrain);
128 }
129 
130 double CaloTPGTranscoderULUT::hcaletValue(const int& ieta, const int& iphi, const int& version, const int& compET) const {
131  double etvalue = 0.;
132  int itower = getOutputLUTId(ieta,iphi, version);
133  if (itower < 0) {
134  edm::LogError("CaloTPGTranscoderULUT") << "No decompression LUT found for ieta, iphi = " << ieta << ", " << iphi;
135  } else if (compET < 0 || compET >= (int) TPGMAX) {
136  edm::LogError("CaloTPGTranscoderULUT") << "Compressed value out of range: eta, phi, cET = " << ieta << ", " << iphi << ", " << compET;
137  } else {
138  etvalue = hcaluncomp_[itower][compET];
139  }
140  return(etvalue);
141 }
142 
144  int compET = hc.compressedEt(); // to be within the range by the class
145  int itower = getOutputLUTId(hid);
146  double etvalue = hcaluncomp_[itower][compET];
147  return etvalue;
148 }
149 
151  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::ecalCompress";
152 }
153 
156  unsigned int& et, bool& egVecto, bool& activity) const {
157  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctEGammaUncompress";
158 }
161  unsigned int& et) const {
162  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctJetUncompress";
163 }
164 
165 bool CaloTPGTranscoderULUT::HTvalid(const int ieta, const int iphiin, const int version) const {
166  HcalTrigTowerDetId id(ieta, iphiin);
167  id.setVersion(version);
168  if (!theTopology) {
169  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
170  }
171  return theTopology->validHT(id);
172 }
173 
175  if (!theTopology) {
176  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
177  }
178  return theTopology->detId2denseIdHT(id);
179 }
180 
181 int CaloTPGTranscoderULUT::getOutputLUTId(const int ieta, const int iphiin, const int version) const {
182  if (!theTopology) {
183  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
184  }
185  HcalTrigTowerDetId id(ieta, iphiin);
186  id.setVersion(version);
187  return theTopology->detId2denseIdHT(id);
188 }
189 
190 unsigned int
192 {
193  if (!theTopology)
194  throw cms::Exception("CaloTPGTranscoderULUT")
195  << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
196 
197  switch (theTopology->triggerMode()) {
200  return QIE8_OUTPUT_LUT_SIZE;
202  if (id.ietaAbs() <= theTopology->lastHERing())
203  return QIE8_OUTPUT_LUT_SIZE;
204  else
205  return QIE10_OUTPUT_LUT_SIZE;
207  if (plan1_towers_.find(id) != plan1_towers_.end())
208  return QIE11_OUTPUT_LUT_SIZE;
209  else if (id.ietaAbs() <= theTopology->lastHERing())
210  return QIE8_OUTPUT_LUT_SIZE;
211  else
212  return QIE10_OUTPUT_LUT_SIZE;
215  if (id.ietaAbs() <= theTopology->lastHBRing())
216  return QIE8_OUTPUT_LUT_SIZE;
217  else if (id.ietaAbs() <= theTopology->lastHERing())
218  return QIE11_OUTPUT_LUT_SIZE;
219  else
220  return QIE10_OUTPUT_LUT_SIZE;
222  if (id.ietaAbs() <= theTopology->lastHERing())
223  return QIE11_OUTPUT_LUT_SIZE;
224  else
225  return QIE10_OUTPUT_LUT_SIZE;
226  default:
227  throw cms::Exception("CaloTPGTranscoderULUT")
228  << "Unknown trigger mode used by the topology!";
229  }
230 }
231 
232 const std::vector<unsigned int> CaloTPGTranscoderULUT::getCompressionLUT(const HcalTrigTowerDetId& id) const {
233  int itower = getOutputLUTId(id);
234  auto lut = outputLUT_[itower];
235  std::vector<unsigned int> result(lut.begin(), lut.end());
236  return result;
237 }
238 
239 void CaloTPGTranscoderULUT::setup(HcalLutMetadata const& lutMetadata, HcalTrigTowerGeometry const& theTrigTowerGeometry, int nctScaleShift, int rctScaleShift)
240 {
241  theTopology = lutMetadata.topo();
242  nominal_gain_ = lutMetadata.getNominalGain();
243  lsb_factor_ = lutMetadata.getRctLsb();
244 
245  rct_factor_ = lsb_factor_/(HcaluLUTTPGCoder::lsb_*(1<<rctScaleShift));
246  nct_factor_ = lsb_factor_/(HcaluLUTTPGCoder::lsb_*(1<<nctScaleShift));
247 
248  outputLUT_.resize(theTopology->getHTSize());
249  hcaluncomp_.resize(theTopology->getHTSize());
250 
251  plan1_towers_.clear();
252  for (const auto& id: lutMetadata.getAllChannels()) {
253  if (not (id.det() == DetId::Hcal and theTopology->valid(id)))
254  continue;
255  HcalDetId cell(id);
256  if (not theTopology->dddConstants()->isPlan1(cell))
257  continue;
258  for (const auto& tower: theTrigTowerGeometry.towerIds(cell))
259  plan1_towers_.emplace(tower);
260  }
261 
262  if (compressionFile_.empty() && decompressionFile_.empty()) {
263  loadHCALCompress(lutMetadata,theTrigTowerGeometry);
264  }
265  else {
266  throw cms::Exception("Not Implemented") << "setup of CaloTPGTranscoderULUT from text files";
267  }
268 }
const HcalDDDRecConstants * dddConstants() const
Definition: HcalTopology.h:161
std::vector< RCTdecompression > hcaluncomp_
std::vector< std::array< LUT, OUTPUT_LUT_SIZE > > outputLUT_
bool isHcalZDCDetId() const
std::vector< HcalTrigTowerDetId > towerIds(const HcalDetId &cellId) const
the mapping to and from DetIds
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
unsigned int detId2denseIdHT(const DetId &id) const
return a linear packed id from HT
unsigned int getOutputLUTSize(const HcalTrigTowerDetId &id) const
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
virtual HcalTriggerPrimitiveSample hcalCompress(const HcalTrigTowerDetId &id, unsigned int sample, int fineGrain) const override
Compression from linear samples+fine grain in the HTR.
virtual 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: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
void loadHCALCompress(HcalLutMetadata const &, HcalTrigTowerGeometry const &)
virtual void setup(HcalLutMetadata const &, HcalTrigTowerGeometry const &, int, int)
virtual 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
virtual bool valid(const DetId &id) const
et
define resolution functions of each parameter
virtual double hcaletValue(const int &ieta, const int &iphi, const int &version, const int &compressedValue) const override
static const unsigned int TPGMAX
virtual 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