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  outputLUT_[index].resize(lutsize);
74 
75  for (unsigned int i = 0; i < threshold; ++i)
76  outputLUT_[index][i] = 0;
77 
78  if (isHBHE) {
79  for (unsigned int i = threshold; i < lutsize; ++i)
80  outputLUT_[index][i] = analyticalLUT[i];
81  } else {
82  for (unsigned int i = threshold; i < lutsize; ++i)
83  outputLUT_[index][i] = version == 0 ? linearRctLUT[i] : linearNctLUT[i];
84  }
85 
86  double eta_low = 0., eta_high = 0.;
87  theTrigTowerGeometry.towerEtaBounds(ieta,version,eta_low,eta_high);
88  double cosh_ieta = fabs(cosh((eta_low + eta_high)/2.));
89  double granularity = meta->getLutGranularity();
90 
91  if(isHBHE){
92  double factor = nominal_gain_ / cosh_ieta * granularity;
93  LUT tpg = outputLUT_[index][0];
94  int low = 0;
95  for (unsigned int i = 0; i < getOutputLUTSize(id); ++i){
96  if (outputLUT_[index][i] != tpg){
97  unsigned int mid = (low + i)/2;
98  hcaluncomp_[index][tpg] = (tpg == 0 ? low : factor * mid);
99  low = i;
100  tpg = outputLUT_[index][i];
101  }
102  }
103  hcaluncomp_[index][tpg] = factor * low;
104  }
105  else{
106  LUT tpg = outputLUT_[index][0];
107  hcaluncomp_[index][tpg]=0;
108  for (unsigned int i = 0; i < getOutputLUTSize(id); ++i){
109  if (outputLUT_[index][i] != tpg){
110  tpg = outputLUT_[index][i];
111  hcaluncomp_[index][tpg] = lsb_factor_ * i / (version==0?rct_factor_:nct_factor_);
112  }
113  }
114  }
115  }
116 
117 }
118 
120  unsigned int itower = getOutputLUTId(id);
121 
122  if (sample >= getOutputLUTSize(id))
123  throw cms::Exception("Out of Range")
124  << "LUT has " << getOutputLUTSize(id) << " entries for " << id << " but " << sample << " was requested.";
125 
126  if(itower >= outputLUT_.size())
127  throw cms::Exception("Out of Range") << "No decompression LUT found for " << id;
128 
129  return HcalTriggerPrimitiveSample(outputLUT_[itower][sample], fineGrain);
130 }
131 
132 double CaloTPGTranscoderULUT::hcaletValue(const int& ieta, const int& iphi, const int& version, const int& compET) const {
133  double etvalue = 0.;
134  int itower = getOutputLUTId(ieta,iphi, version);
135  if (itower < 0) {
136  edm::LogError("CaloTPGTranscoderULUT") << "No decompression LUT found for ieta, iphi = " << ieta << ", " << iphi;
137  } else if (compET < 0 || compET >= (int) TPGMAX) {
138  edm::LogError("CaloTPGTranscoderULUT") << "Compressed value out of range: eta, phi, cET = " << ieta << ", " << iphi << ", " << compET;
139  } else {
140  etvalue = hcaluncomp_[itower][compET];
141  }
142  return(etvalue);
143 }
144 
146  int compET = hc.compressedEt(); // to be within the range by the class
147  int itower = getOutputLUTId(hid);
148  double etvalue = hcaluncomp_[itower][compET];
149  return etvalue;
150 }
151 
153  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::ecalCompress";
154 }
155 
158  unsigned int& et, bool& egVecto, bool& activity) const {
159  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctEGammaUncompress";
160 }
163  unsigned int& et) const {
164  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctJetUncompress";
165 }
166 
167 bool CaloTPGTranscoderULUT::HTvalid(const int ieta, const int iphiin, const int version) const {
168  HcalTrigTowerDetId id(ieta, iphiin);
169  id.setVersion(version);
170  if (!theTopology) {
171  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
172  }
173  return theTopology->validHT(id);
174 }
175 
177  if (!theTopology) {
178  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
179  }
180  return theTopology->detId2denseIdHT(id);
181 }
182 
183 int CaloTPGTranscoderULUT::getOutputLUTId(const int ieta, const int iphiin, const int version) const {
184  if (!theTopology) {
185  throw cms::Exception("CaloTPGTranscoderULUT") << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
186  }
187  HcalTrigTowerDetId id(ieta, iphiin);
188  id.setVersion(version);
189  return theTopology->detId2denseIdHT(id);
190 }
191 
192 unsigned int
194 {
195  if (!theTopology)
196  throw cms::Exception("CaloTPGTranscoderULUT")
197  << "Topology not set! Use CaloTPGTranscoderULUT::setup(...) first!";
198 
199  switch (theTopology->triggerMode()) {
202  return QIE8_OUTPUT_LUT_SIZE;
204  if (id.ietaAbs() <= theTopology->lastHERing())
205  return QIE8_OUTPUT_LUT_SIZE;
206  else
207  return QIE10_OUTPUT_LUT_SIZE;
209  if (plan1_towers_.find(id) != plan1_towers_.end())
210  return QIE11_OUTPUT_LUT_SIZE;
211  else if (id.ietaAbs() <= theTopology->lastHERing())
212  return QIE8_OUTPUT_LUT_SIZE;
213  else
214  return QIE10_OUTPUT_LUT_SIZE;
217  if (id.ietaAbs() <= theTopology->lastHBRing())
218  return QIE8_OUTPUT_LUT_SIZE;
219  else if (id.ietaAbs() <= theTopology->lastHERing())
220  return QIE11_OUTPUT_LUT_SIZE;
221  else
222  return QIE10_OUTPUT_LUT_SIZE;
224  if (id.ietaAbs() <= theTopology->lastHERing())
225  return QIE11_OUTPUT_LUT_SIZE;
226  else
227  return QIE10_OUTPUT_LUT_SIZE;
228  default:
229  throw cms::Exception("CaloTPGTranscoderULUT")
230  << "Unknown trigger mode used by the topology!";
231  }
232 }
233 
234 const std::vector<unsigned int> CaloTPGTranscoderULUT::getCompressionLUT(const HcalTrigTowerDetId& id) const {
235  int itower = getOutputLUTId(id);
236  auto lut = outputLUT_[itower];
237  std::vector<unsigned int> result(lut.begin(), lut.end());
238  return result;
239 }
240 
241 void CaloTPGTranscoderULUT::setup(HcalLutMetadata const& lutMetadata, HcalTrigTowerGeometry const& theTrigTowerGeometry, int nctScaleShift, int rctScaleShift)
242 {
243  theTopology = lutMetadata.topo();
244  nominal_gain_ = lutMetadata.getNominalGain();
245  lsb_factor_ = lutMetadata.getRctLsb();
246 
247  rct_factor_ = lsb_factor_/(HcaluLUTTPGCoder::lsb_*(1<<rctScaleShift));
248  nct_factor_ = lsb_factor_/(HcaluLUTTPGCoder::lsb_*(1<<nctScaleShift));
249 
250  outputLUT_.resize(theTopology->getHTSize());
251  hcaluncomp_.resize(theTopology->getHTSize());
252 
253  plan1_towers_.clear();
254  for (const auto& id: lutMetadata.getAllChannels()) {
255  if (not (id.det() == DetId::Hcal and theTopology->valid(id)))
256  continue;
257  HcalDetId cell(id);
258  if (not theTopology->dddConstants()->isPlan1(cell))
259  continue;
260  for (const auto& tower: theTrigTowerGeometry.towerIds(cell))
261  plan1_towers_.emplace(tower);
262  }
263 
264  if (compressionFile_.empty() && decompressionFile_.empty()) {
265  loadHCALCompress(lutMetadata,theTrigTowerGeometry);
266  }
267  else {
268  throw cms::Exception("Not Implemented") << "setup of CaloTPGTranscoderULUT from text files";
269  }
270 }
const HcalDDDRecConstants * dddConstants() const
Definition: HcalTopology.h:161
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
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
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