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