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 }
117 
119  unsigned int itower = getOutputLUTId(id);
120 
121  if (sample >= getOutputLUTSize(id))
122  throw cms::Exception("Out of Range")
123  << "LUT has " << getOutputLUTSize(id) << " entries for " << id << " but " << sample << " was requested.";
124 
125  if(itower >= outputLUT_.size())
126  throw cms::Exception("Out of Range") << "No decompression LUT found for " << id;
127 
128  return HcalTriggerPrimitiveSample(outputLUT_[itower][sample], fineGrain);
129 }
130 
131 double CaloTPGTranscoderULUT::hcaletValue(const int& ieta, const int& iphi, const int& version, const int& compET) const {
132  double etvalue = 0.;
133  int itower = getOutputLUTId(ieta,iphi, version);
134  if (itower < 0) {
135  edm::LogError("CaloTPGTranscoderULUT") << "No decompression LUT found for ieta, iphi = " << ieta << ", " << iphi;
136  } else if (compET < 0 || compET >= (int) TPGMAX) {
137  edm::LogError("CaloTPGTranscoderULUT") << "Compressed value out of range: eta, phi, cET = " << ieta << ", " << iphi << ", " << compET;
138  } else {
139  etvalue = hcaluncomp_[itower][compET];
140  }
141  return(etvalue);
142 }
143 
145  int compET = hc.compressedEt(); // to be within the range by the class
146  int itower = getOutputLUTId(hid);
147  double etvalue = hcaluncomp_[itower][compET];
148  return etvalue;
149 }
150 
152  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::ecalCompress";
153 }
154 
157  unsigned int& et, bool& egVecto, bool& activity) const {
158  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctEGammaUncompress";
159 }
162  unsigned int& et) const {
163  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctJetUncompress";
164 }
165 
166 bool CaloTPGTranscoderULUT::HTvalid(const int ieta, const int iphiin, const int version) const {
167  HcalTrigTowerDetId id(ieta, iphiin);
168  id.setVersion(version);
169  if (!theTopology) {
170  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
171  }
172  return theTopology->validHT(id);
173 }
174 
176  if (!theTopology) {
177  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
178  }
179  return theTopology->detId2denseIdHT(id);
180 }
181 
182 int CaloTPGTranscoderULUT::getOutputLUTId(const int ieta, const int iphiin, const int version) const {
183  if (!theTopology) {
184  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
185  }
186  HcalTrigTowerDetId id(ieta, iphiin);
187  id.setVersion(version);
188  return theTopology->detId2denseIdHT(id);
189 }
190 
191 unsigned int
193 {
194  if (!theTopology)
195  throw cms::Exception("CaloTPGTranscoderULUT")
196  << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
197 
198  switch (theTopology->triggerMode()) {
201  return QIE8_OUTPUT_LUT_SIZE;
203  if (id.ietaAbs() <= theTopology->lastHERing())
204  return QIE8_OUTPUT_LUT_SIZE;
205  else
206  return QIE10_OUTPUT_LUT_SIZE;
208  if (plan1_towers_.find(id) != plan1_towers_.end())
209  return QIE11_OUTPUT_LUT_SIZE;
210  else if (id.ietaAbs() <= theTopology->lastHERing())
211  return QIE8_OUTPUT_LUT_SIZE;
212  else
213  return QIE10_OUTPUT_LUT_SIZE;
216  if (id.ietaAbs() <= theTopology->lastHBRing())
217  return QIE8_OUTPUT_LUT_SIZE;
218  else if (id.ietaAbs() <= theTopology->lastHERing())
219  return QIE11_OUTPUT_LUT_SIZE;
220  else
221  return QIE10_OUTPUT_LUT_SIZE;
223  if (id.ietaAbs() <= theTopology->lastHERing())
224  return QIE11_OUTPUT_LUT_SIZE;
225  else
226  return QIE10_OUTPUT_LUT_SIZE;
227  default:
228  throw cms::Exception("CaloTPGTranscoderULUT")
229  << "Unknown trigger mode used by the topology!";
230  }
231 }
232 
233 const std::vector<unsigned int> CaloTPGTranscoderULUT::getCompressionLUT(const HcalTrigTowerDetId& id) const {
234  int itower = getOutputLUTId(id);
235  auto lut = outputLUT_[itower];
236  std::vector<unsigned int> result(lut.begin(), lut.end());
237  return result;
238 }
239 
240 void CaloTPGTranscoderULUT::setup(HcalLutMetadata const& lutMetadata, HcalTrigTowerGeometry const& theTrigTowerGeometry, int nctScaleShift, int rctScaleShift)
241 {
242  theTopology = lutMetadata.topo();
243  nominal_gain_ = lutMetadata.getNominalGain();
244  lsb_factor_ = lutMetadata.getRctLsb();
245 
246  rct_factor_ = lsb_factor_/(HcaluLUTTPGCoder::lsb_*(1<<rctScaleShift));
247  nct_factor_ = lsb_factor_/(HcaluLUTTPGCoder::lsb_*(1<<nctScaleShift));
248 
249  outputLUT_.resize(theTopology->getHTSize());
250  hcaluncomp_.resize(theTopology->getHTSize());
251 
252  plan1_towers_.clear();
253  for (const auto& id: lutMetadata.getAllChannels()) {
254  if (not (id.det() == DetId::Hcal and theTopology->valid(id)))
255  continue;
256  HcalDetId cell(id);
257  if (not theTopology->dddConstants()->isPlan1(cell))
258  continue;
259  for (const auto& tower: theTrigTowerGeometry.towerIds(cell))
260  plan1_towers_.emplace(tower);
261  }
262 
263  if (compressionFile_.empty() && decompressionFile_.empty()) {
264  loadHCALCompress(lutMetadata,theTrigTowerGeometry);
265  }
266  else {
267  throw cms::Exception("Not Implemented") << "setup of CaloTPGTranscoderULUT from text files";
268  }
269 }
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