CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CaloTPGTranscoderULUT.cc
Go to the documentation of this file.
7 #include <iostream>
8 #include <fstream>
9 #include <math.h>
10 
11 //#include "FWCore/Framework/interface/Frameworkfwd.h"
14 
15 using namespace std;
16 
17 
19  const std::string& decompressionFile)
20  : nominal_gain_(0.), rctlsb_factor_(0.),
21  compressionFile_(compressionFile),
22  decompressionFile_(decompressionFile)
23 {
24  for (int i = 0; i < NOUTLUTS; i++) outputLUT_[i] = 0;
25 }
26 
28  for (int i = 0; i < NOUTLUTS; i++) {
29  if (outputLUT_[i] != 0) delete [] outputLUT_[i];
30  }
31 }
32 
34  HcalTrigTowerGeometry const& theTrigTowerGeometry) {
35 // Initialize analytical compression LUT's here
36  // TODO cms::Error log
37  if (OUTPUT_LUT_SIZE != (unsigned int) 0x400) std::cout << "Error: Analytic compression expects 10-bit LUT; found LUT with " << OUTPUT_LUT_SIZE << " entries instead" << std::endl;
38 
39  std::vector<unsigned int> analyticalLUT(OUTPUT_LUT_SIZE, 0);
40  std::vector<unsigned int> identityLUT(OUTPUT_LUT_SIZE, 0);
41 
42  // Compute compression LUT
43  for (unsigned int i=0; i < OUTPUT_LUT_SIZE; i++) {
44  analyticalLUT[i] = (unsigned int)(sqrt(14.94*log(1.+i/14.94)*i) + 0.5);
45  identityLUT[i] = min(i,0xffu);
46  }
47 
48  for (int ieta=-32; ieta <= 32; ieta++){
49  for (int iphi = 1; iphi <= 72; iphi++){
50  if (!HTvalid(ieta,iphi)) continue;
51  int lutId = getOutputLUTId(ieta,iphi);
52  // TODO cms::Error log
53  if (outputLUT_[lutId] != 0){
54  std::cout << "Error: LUT with (ieta,iphi) = (" << ieta << "," << iphi << ") has been previously allocated!" << std::endl;
55  continue;
56  }
57 
58  outputLUT_[lutId] = new LUT[OUTPUT_LUT_SIZE];
59 
60  HcalTrigTowerDetId id(ieta, iphi);
61  const HcalLutMetadatum *meta = lutMetadata.getValues(id);
62  int threshold = meta->getOutputLutThreshold();
63 
64  for (int i = 0; i < threshold; ++i)
65  outputLUT_[lutId][i] = 0;
66 
67  for (unsigned int i = threshold; i < OUTPUT_LUT_SIZE; ++i)
68  outputLUT_[lutId][i] = (abs(ieta) < theTrigTowerGeometry.firstHFTower()) ? analyticalLUT[i] : identityLUT[i];
69  } //for iphi
70  } //for ieta
71 }
72 
73 void CaloTPGTranscoderULUT::loadHCALCompress(const std::string& filename, HcalLutMetadata const& lutMetadata, HcalTrigTowerGeometry const& theTrigTowerGeometry) {
74  int tool;
75  std::ifstream userfile;
76  std::vector< std::vector<LUT> > outputluts;
77 
78  std::cout << "Initializing compression LUT's from " << (char *)filename.data() << std::endl;
79  for (int i = 0; i < NOUTLUTS; i++) outputLUT_[i] = 0;
80  int maxid = 0, minid = 0x7FFFFFFF, rawid = 0;
81  for (int ieta=-32; ieta <= 32; ieta++) {
82  for (int iphi = 1; iphi <= 72; iphi++) {
83  if (HTvalid(ieta,iphi)) {
84  rawid = getOutputLUTId(ieta, iphi);
85  if (outputLUT_[rawid] != 0) std::cout << "Error: LUT with (ieta,iphi) = (" << ieta << "," << iphi << ") has been previously allocated!" << std::endl;
86  else outputLUT_[rawid] = new LUT[OUTPUT_LUT_SIZE];
87  if (rawid < minid) minid = rawid;
88  if (rawid > maxid) maxid = rawid;
89  }
90  }
91  }
92 
93  userfile.open((char *)filename.data());
94 
95  if( userfile ) {
96  int nluts = 0;
97  std::string s;
98  std::vector<int> loieta,hiieta;
99  std::vector<int> loiphi,hiiphi;
100  getline(userfile,s);
101 
102  getline(userfile,s);
103 
104  unsigned int index = 0;
105  while (index < s.length()) {
106  while (isspace(s[index])) index++;
107  if (index < s.length()) nluts++;
108  while (!isspace(s[index])) index++;
109  }
110  for (unsigned int i=0; i<=s.length(); i++) userfile.unget(); //rewind last line
111  outputluts.resize(nluts);
112  for (int i=0; i<nluts; i++) outputluts[i].resize(OUTPUT_LUT_SIZE);
113 
114 
115  for (int i=0; i<nluts; i++) {
116  userfile >> tool;
117  loieta.push_back(tool);
118  }
119 
120  for (int i=0; i<nluts; i++) {
121  userfile >> tool;
122  hiieta.push_back(tool);
123  }
124 
125  for (int i=0; i<nluts; i++) {
126  userfile >> tool;
127  loiphi.push_back(tool);
128 
129  }
130 
131  for (int i=0; i<nluts; i++) {
132  userfile >> tool;
133  hiiphi.push_back(tool);
134 
135  }
136 
137  for (unsigned int j=0; j<OUTPUT_LUT_SIZE; j++) {
138  for(int i=0; i <nluts; i++) {
139  userfile >> tool;
140  if (tool < 0) {
141  std::cout << "Error: LUT can't have negative numbers; 0 used instead: " << i << ", " << j << " : = " << tool << std::endl;
142  tool = 0;
143  } else if (tool > 0xff) {
144  std::cout << "Error: LUT can't have >8-bit numbers; 0xff used instead: " << i << ", " << j << " : = " << tool << std::endl;
145  tool = 0xff;
146  }
147  outputluts[i][j] = tool;
148  if (userfile.eof()) std::cout << "Error: LUT file is truncated or has a wrong format: " << i << "," << j << std::endl;
149  }
150  }
151  userfile.close();
152 
153  HcalDetId cell;
154  int id, ntot = 0;
155  for (int i=0; i < nluts; i++) {
156  int nini = 0;
157  for (int iphi = loiphi[i]; iphi <= hiiphi[i]; iphi++) {
158  for (int ieta=loieta[i]; ieta <= hiieta[i]; ieta++) {
159  if (HTvalid(ieta,iphi)) {
160  id = getOutputLUTId(ieta,iphi);
161  if (outputLUT_[id] == 0) throw cms::Exception("PROBLEM: outputLUT_ has not been initialized for ieta, iphi, id = ") << ieta << ", " << iphi << ", " << id << std::endl;
162  for (int j = 0; j <= 0x3ff; j++) outputLUT_[id][j] = outputluts[i][j];
163  nini++;
164  ntot++;
165  }
166  }
167  }
168 
169  }
170 
171  } else {
172 
173  loadHCALCompress(lutMetadata,theTrigTowerGeometry);
174  }
175 }
176 
178  HcalTrigTowerGeometry const& theTrigTowerGeometry) {
179  hcaluncomp_.clear();
180  for (int i = 0; i < NOUTLUTS; i++){
181  RCTdecompression decompressionTable(TPGMAX,0);
182  hcaluncomp_.push_back(decompressionTable);
183  }
184 
185  for (int ieta = -32; ieta <= 32; ++ieta){
186 
187  double eta_low = 0., eta_high = 0.;
188  theTrigTowerGeometry.towerEtaBounds(ieta,eta_low,eta_high);
189  double cosh_ieta = fabs(cosh((eta_low + eta_high)/2.));
190 
191  for (int iphi = 1; iphi <= 72; iphi++) {
192  if (!HTvalid(ieta, iphi)) continue;
193 
194  int lutId = getOutputLUTId(ieta,iphi);
195  HcalTrigTowerDetId id(ieta, iphi);
196 
197  const HcalLutMetadatum *meta = lutMetadata.getValues(id);
198  double factor = 0.;
199 
200  // HF
201  if (abs(ieta) >= theTrigTowerGeometry.firstHFTower())
202  factor = rctlsb_factor_;
203  // HBHE
204  else
205  factor = nominal_gain_ / cosh_ieta * meta->getLutGranularity();
206 
207  // tpg - compressed value
208  unsigned int tpg = outputLUT_[lutId][0];
209  int low = 0;
210  for (unsigned int i = 0; i < OUTPUT_LUT_SIZE; ++i){
211  if (outputLUT_[lutId][i] != tpg){
212  unsigned int mid = (low + i)/2;
213  hcaluncomp_[lutId][tpg] = (tpg == 0 ? low : factor * mid);
214  low = i;
215  tpg = outputLUT_[lutId][i];
216  }
217  }
218  hcaluncomp_[lutId][tpg] = factor * low;
219  } // for phi
220  } // for ieta
221 }
222 
223 void CaloTPGTranscoderULUT::loadHCALUncompress(const std::string& filename, HcalLutMetadata const& lutMetadata, HcalTrigTowerGeometry const& theTrigTowerGeometry) {
224  std::ifstream userfile;
225  userfile.open(filename.c_str());
226 
227  hcaluncomp_.resize(NOUTLUTS);
228  for (int i = 0; i < NOUTLUTS; i++) hcaluncomp_[i].resize(TPGMAX);
229 
230  static const int etabound = 32;
231  if( userfile ) {
232  double et;
233  for (int i=0; i<TPGMAX; i++) {
234  for(int j = 1; j <=etabound; j++) {
235  userfile >> et;
236  for (int iphi = 1; iphi <= 72; iphi++) {
237  int itower = getOutputLUTId(j,iphi);
238  if (itower >= 0) hcaluncomp_[itower][i] = et;
239  itower = getOutputLUTId(-j,iphi);
240  if (itower >= 0) hcaluncomp_[itower][i] = et;
241  }
242  }
243  }
244  userfile.close();
245  }
246  else {
247 
248  loadHCALUncompress(lutMetadata,theTrigTowerGeometry);
249  }
250 }
251 
253  int ieta = id.ieta();
254  int iphi = id.iphi();
255 // if (abs(ieta) > 28) iphi = iphi/4 + 1; // Changing iphi index from 1, 5, ..., 69 to 1, 2, ..., 18
256  int itower = getOutputLUTId(ieta,iphi);
257 
258  if (itower < 0) cms::Exception("Invalid Data") << "No trigger tower found for ieta, iphi = " << ieta << ", " << iphi;
259  if (sample >= OUTPUT_LUT_SIZE) {
260 
261  throw cms::Exception("Out of Range") << "LUT has 1024 entries for " << itower << " but " << sample << " was requested.";
262  sample=OUTPUT_LUT_SIZE - 1;
263  }
264 
265  return HcalTriggerPrimitiveSample(outputLUT_[itower][sample],fineGrain,0,0);
266 }
267 
268 double CaloTPGTranscoderULUT::hcaletValue(const int& ieta, const int& iphi, const int& compET) const {
269  double etvalue = 0.;
270  int itower = getOutputLUTId(ieta,iphi);
271  if (itower < 0) std::cout << "hcaletValue error: no decompression LUT found for ieta, iphi = " << ieta << ", " << iphi << std::endl;
272  else if (compET < 0 || compET > 0xff) std::cout << "hcaletValue error: compressed value out of range: eta, phi, cET = " << ieta << ", " << iphi << ", " << compET << std::endl;
273  else etvalue = hcaluncomp_[itower][compET];
274  return(etvalue);
275 }
276 
277 double CaloTPGTranscoderULUT::hcaletValue(const int& ieta, const int& compET) const {
278 // This is now an obsolete method; we return the AVERAGE over all the allowed iphi channels if it's invoked
279 // The user is encouraged to use hcaletValue(const int& ieta, const int& iphi, const int& compET) instead
280 
281  double etvalue = 0.;
282  if (compET < 0 || compET > 0xff) std::cout << "hcaletValue error: compressed value out of range: eta, cET = " << ieta << ", " << compET << std::endl;
283  else {
284  int nphi = 0;
285  for (int iphi=1; iphi <= 72; iphi++) {
286  if (HTvalid(ieta,iphi)) {
287  nphi++;
288  int itower = getOutputLUTId(ieta,iphi);
289  etvalue += hcaluncomp_[itower][compET];
290  }
291  }
292  if (nphi > 0) etvalue /= nphi;
293  else std::cout << "hcaletValue error: no decompression LUTs found for any iphi for ieta = " << ieta << std::endl;
294  }
295  return(etvalue);
296 }
297 
299 
300  int ieta = hid.ieta(); // No need to check the validity,
301  int iphi = hid.iphi(); // as the values are guaranteed
302  int compET = hc.compressedEt(); // to be within the range by the class
303  int itower = getOutputLUTId(ieta,iphi);
304  double etvalue = hcaluncomp_[itower][compET];
305  return(etvalue);
306 }
307 
309  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::ecalCompress";
310 }
311 
313  const EcalTrigTowerDetId& eid, const EcalTriggerPrimitiveSample& ec,
314  unsigned int& et, bool& egVecto, bool& activity) const {
315  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctEGammaUncompress";
316 }
318  const EcalTrigTowerDetId& eid, const EcalTriggerPrimitiveSample& ec,
319  unsigned int& et) const {
320  throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctJetUncompress";
321 }
322 
323 bool CaloTPGTranscoderULUT::HTvalid(const int ieta, const int iphiin) const {
324  int iphi = iphiin;
325  if (iphi <= 0 || iphi > 72 || ieta == 0 || abs(ieta) > 32) return false;
326  if (abs(ieta) > 28) {
327  if (newHFphi) {
328  if ((iphi/4)*4 + 1 != iphi) return false;
329  iphi = iphi/4 + 1;
330  }
331  if (iphi > 18) return false;
332  }
333  return true;
334 }
335 
336 int CaloTPGTranscoderULUT::getOutputLUTId(const int ieta, const int iphiin) const {
337  int iphi = iphiin;
338  if (HTvalid(ieta, iphi)) {
339  int offset = 0, ietaabs;
340  ietaabs = abs(ieta);
341  if (ieta < 0) offset = NOUTLUTS/2;
342  if (ietaabs < 29) return 72*(ietaabs - 1) + (iphi - 1) + offset;
343  else {
344  if (newHFphi) iphi = iphi/4 + 1;
345  return 18*(ietaabs - 29) + iphi + 2015 + offset;
346  }
347  } else return -1;
348 }
349 
350 std::vector<unsigned char> CaloTPGTranscoderULUT::getCompressionLUT(HcalTrigTowerDetId id) const {
351  std::vector<unsigned char> lut;
352  int itower = getOutputLUTId(id.ieta(),id.iphi());
353  if (itower >= 0) {
354  lut.resize(OUTPUT_LUT_SIZE);
355  for (unsigned int i = 0; i < OUTPUT_LUT_SIZE; i++) lut[i]=outputLUT_[itower][i];
356  }
357  return lut;
358 }
359 
360 void CaloTPGTranscoderULUT::setup(HcalLutMetadata const& lutMetadata, HcalTrigTowerGeometry const& theTrigTowerGeometry)
361 {
362  nominal_gain_ = lutMetadata.getNominalGain();
363  float rctlsb =lutMetadata.getRctLsb();
364  if (rctlsb != 0.25 && rctlsb != 0.5)
365  throw cms::Exception("RCTLSB") << " value=" << rctlsb << " (should be 0.25 or 0.5)" << std::endl;
366  rctlsb_factor_ = rctlsb;
367 
368  if (compressionFile_.empty() && decompressionFile_.empty()) {
369  loadHCALCompress(lutMetadata,theTrigTowerGeometry);
370  }
371  else {
372  // TODO Message to discourage using txt.
373  std::cout << "From Text File:" << std::endl;
374  loadHCALCompress(compressionFile_, lutMetadata,theTrigTowerGeometry);
375  }
376  loadHCALUncompress(decompressionFile_, lutMetadata,theTrigTowerGeometry);
377 }
378 
379 //int CaloTPGTranscoderULUT::getLutGranularity(const DetId& id) const{
380 // int ieta = id.ietaAbs();
381 // if (ieta < 18) return 1;
382 // else if (ieta < 27) return 2;
383 // else if (ieta < 29) return 5;
384 // return 0;
385 //}
386 //
387 //int CaloTPGTranscoderULUT::getLutThreshold(const DetId& id) const{
388 // int ieta = id.ietaAbs();
389 // if (ieta < 18) return 4;
390 // else if (ieta < 27) return 2;
391 // else if (ieta < 29) return 1;
392 // return 0;
393 //}
394 //
int i
Definition: DBlmapReader.cc:9
std::vector< RCTdecompression > hcaluncomp_
uint8_t getOutputLutThreshold() const
CaloTPGTranscoderULUT(const std::string &compressionFile="", const std::string &decompressionFile="")
const Item * getValues(DetId fId, bool throwOnFail=true) const
virtual std::vector< unsigned char > getCompressionLUT(HcalTrigTowerDetId id) const
uint8_t getLutGranularity() const
void loadHCALUncompress(HcalLutMetadata const &, HcalTrigTowerGeometry const &)
int ieta() const
get the tower ieta
virtual void rctJetUncompress(const HcalTrigTowerDetId &hid, const HcalTriggerPrimitiveSample &hc, const EcalTrigTowerDetId &eid, const EcalTriggerPrimitiveSample &ec, unsigned int &et) const
Uncompression for the JET path in the RCT.
T sqrt(T t)
Definition: SSEVec.h:48
float getNominalGain() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int j
Definition: DBlmapReader.cc:9
static const unsigned int OUTPUT_LUT_SIZE
virtual void rctEGammaUncompress(const HcalTrigTowerDetId &hid, const HcalTriggerPrimitiveSample &hc, const EcalTrigTowerDetId &eid, const EcalTriggerPrimitiveSample &ec, unsigned int &et, bool &egVecto, bool &activity) const
Uncompression for the Electron/Photon path in the RCT.
tuple lut
Definition: lumiPlot.py:244
T min(T a, T b)
Definition: MathUtil.h:58
unsigned int offset(bool)
virtual bool HTvalid(const int ieta, const int iphi) const
void loadHCALCompress(HcalLutMetadata const &, HcalTrigTowerGeometry const &)
void towerEtaBounds(int ieta, double &eta1, double &eta2) const
where this tower begins and ends in eta
std::vector< double > RCTdecompression
float getRctLsb() const
virtual void setup(HcalLutMetadata const &, HcalTrigTowerGeometry const &)
virtual double hcaletValue(const int &ieta, const int &compressedValue) const
tuple filename
Definition: lut2db_cfg.py:20
susybsm::HSCParticleCollection hc
Definition: classes.h:25
virtual int getOutputLUTId(const int ieta, const int iphi) const
int compressedEt() const
get the encoded/compressed Et
tuple cout
Definition: gather_cfg.py:121
virtual EcalTriggerPrimitiveSample ecalCompress(const EcalTrigTowerDetId &id, unsigned int sample, bool fineGrain) const
Compression from linear samples+fine grain in the ECAL.
static const bool newHFphi
virtual HcalTriggerPrimitiveSample hcalCompress(const HcalTrigTowerDetId &id, unsigned int sample, bool fineGrain) const
Compression from linear samples+fine grain in the HTR.
int iphi() const
get the tower iphi