CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcaluLUTTPGCoder.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <cmath>
4 #include <string>
28 
29 const float HcaluLUTTPGCoder::lsb_=1./16;
30 
34 
35 
36 HcaluLUTTPGCoder::HcaluLUTTPGCoder(const HcalTopology* top) : topo_(top), LUTGenerationMode_(true), bitToMask_(0) {
52  size_t nluts= (size_t)(sizeHB_+sizeHE_+sizeHF_+1);
53  inputLUT_ = std::vector<HcaluLUTTPGCoder::Lut>(nluts,HcaluLUTTPGCoder::Lut(INPUT_LUT_SIZE, 0));
54  upgradeQIE10LUT_ = std::vector<HcaluLUTTPGCoder::Lut>(nluts,HcaluLUTTPGCoder::Lut(UPGRADE_LUT_SIZE, 0));
55  upgradeQIE11LUT_ = std::vector<HcaluLUTTPGCoder::Lut>(nluts,HcaluLUTTPGCoder::Lut(UPGRADE_LUT_SIZE, 0));
56  gain_ = std::vector<float>(nluts, 0.);
57  ped_ = std::vector<float>(nluts, 0.);
58 }
59 
60 void HcaluLUTTPGCoder::compress(const IntegerCaloSamples& ics, const std::vector<bool>& featureBits, HcalTriggerPrimitiveDigi& tp) const {
61  throw cms::Exception("PROBLEM: This method should never be invoked!");
62 }
63 
65 }
66 
67 int HcaluLUTTPGCoder::getLUTId(HcalSubdetector id, int ieta, int iphi, int depth) const {
68  int retval(0);
69  if (id == HcalBarrel) {
70  retval = (depth-1)+maxDepthHB_*(iphi-1);
71  if (ieta>0) retval+=maxDepthHB_*nFi_*(ieta-firstHBEta_);
72  else retval+=maxDepthHB_*nFi_*(ieta+lastHBEta_+nHBEta_);
73  } else if (id == HcalEndcap) {
74  retval = sizeHB_;
75  retval+= (depth-1)+maxDepthHE_*(iphi-1);
76  if (ieta>0) retval+=maxDepthHE_*nFi_*(ieta-firstHEEta_);
77  else retval+=maxDepthHE_*nFi_*(ieta+lastHEEta_+nHEEta_);
78  } else if (id == HcalForward) {
79  retval = sizeHB_+sizeHE_;
80  retval+= (depth-1)+maxDepthHF_*(iphi-1);
81  if (ieta>0) retval+=maxDepthHF_*nFi_*(ieta-firstHFEta_);
82  else retval+=maxDepthHF_*nFi_*(ieta+lastHFEta_+nHFEta_);
83  }
84  return retval;
85 }
86 
87 int HcaluLUTTPGCoder::getLUTId(uint32_t rawid) const {
88  HcalDetId detid(rawid);
89  return getLUTId(detid.subdet(), detid.ieta(), detid.iphi(), detid.depth());
90 }
91 
92 int HcaluLUTTPGCoder::getLUTId(const HcalDetId& detid) const {
93  return getLUTId(detid.subdet(), detid.ieta(), detid.iphi(), detid.depth());
94 }
95 
96 void HcaluLUTTPGCoder::update(const char* filename, bool appendMSB) {
97 
98  std::ifstream file(filename, std::ios::in);
99  assert(file.is_open());
100 
101  std::vector<HcalSubdetector> subdet;
102  std::string buffer;
103 
104  // Drop first (comment) line
105  std::getline(file, buffer);
106  std::getline(file, buffer);
107 
108  unsigned int index = buffer.find("H", 0);
109  while (index < buffer.length()){
110  std::string subdetStr = buffer.substr(index, 2);
111  if (subdetStr == "HB") subdet.push_back(HcalBarrel);
112  else if (subdetStr == "HE") subdet.push_back(HcalEndcap);
113  else if (subdetStr == "HF") subdet.push_back(HcalForward);
114  //TODO Check subdet
115  //else exception
116  index += 2;
117  index = buffer.find("H", index);
118  }
119 
120  // Get upper/lower ranges for ieta/iphi/depth
121  size_t nCol = subdet.size();
122  assert(nCol > 0);
123 
124  std::vector<int> ietaU;
125  std::vector<int> ietaL;
126  std::vector<int> iphiU;
127  std::vector<int> iphiL;
128  std::vector<int> depU;
129  std::vector<int> depL;
130  std::vector< Lut > lutFromFile(nCol);
131  LutElement lutValue;
132 
133  for (size_t i=0; i<nCol; ++i) {
134  int ieta;
135  file >> ieta;
136  ietaL.push_back(ieta);
137  }
138 
139  for (size_t i=0; i<nCol; ++i) {
140  int ieta;
141  file >> ieta;
142  ietaU.push_back(ieta);
143  }
144 
145  for (size_t i=0; i<nCol; ++i) {
146  int iphi;
147  file >> iphi;
148  iphiL.push_back(iphi);
149  }
150 
151  for (size_t i=0; i<nCol; ++i) {
152  int iphi;
153  file >> iphi;
154  iphiU.push_back(iphi);
155  }
156 
157  for (size_t i=0; i<nCol; ++i) {
158  int dep;
159  file >> dep;
160  depL.push_back(dep);
161  }
162 
163  for (size_t i=0; i<nCol; ++i) {
164  int dep;
165  file >> dep;
166  depU.push_back(dep);
167  }
168 
169  // Read Lut Entry
170  for (size_t i=0; file >> lutValue; i = (i+1) % nCol){
171  lutFromFile[i].push_back(lutValue);
172  }
173 
174  // Check lut size
175  for (size_t i=0; i<nCol; ++i) assert(lutFromFile[i].size() == INPUT_LUT_SIZE);
176 
177  for (size_t i=0; i<nCol; ++i){
178  for (int ieta = ietaL[i]; ieta <= ietaU[i]; ++ieta){
179  for (int iphi = iphiL[i]; iphi <= iphiU[i]; ++iphi){
180  for (int depth = depL[i]; depth <= depU[i]; ++depth){
181 
182  HcalDetId id(subdet[i], ieta, iphi, depth);
183  if (!topo_->valid(id)) continue;
184 
185  int lutId = getLUTId(id);
186  for (size_t adc = 0; adc < INPUT_LUT_SIZE; ++adc){
187  if (appendMSB){
188  // Append FG bit LUT to MSB
189  // MSB = Most Significant Bit = bit 10
190  // Overwrite bit 10
191  LutElement msb = (lutFromFile[i][adc] != 0 ? QIE8_LUT_MSB : 0);
192  inputLUT_[lutId][adc] = (msb | (inputLUT_[lutId][adc] & QIE8_LUT_BITMASK));
193  }
194  else inputLUT_[lutId][adc] = lutFromFile[i][adc];
195  }// for adc
196  }// for depth
197  }// for iphi
198  }// for ieta
199  }// for nCol
200 }
201 
203  LutXml * _xml = new LutXml(filename);
204  _xml->create_lut_map();
206  for (int ieta = -HcalDetId::kHcalEtaMask2;
207  ieta <= HcalDetId::kHcalEtaMask2; ++ieta) {
208  for (int iphi = 0; iphi <= HcalDetId::kHcalPhiMask2; ++iphi) {
209  for (int depth = 1; depth < HcalDetId::kHcalDepthMask2; ++depth) {
210  for (int isub=0; isub<3; ++isub) {
211  HcalDetId detid(subdet[isub], ieta, iphi, depth);
212  if (!topo_->valid(detid)) continue;
213  int id = getLUTId(subdet[isub], ieta, iphi, depth);
214  std::vector<unsigned int>* lut = _xml->getLutFast(detid);
215  if (lut==0) throw cms::Exception("PROBLEM: No inputLUT_ in xml file for ") << detid << std::endl;
216  if (lut->size()!=INPUT_LUT_SIZE) throw cms::Exception ("PROBLEM: Wrong inputLUT_ size in xml file for ") << detid << std::endl;
217  for (unsigned int i=0; i<INPUT_LUT_SIZE; ++i) inputLUT_[id][i] = (LutElement)lut->at(i);
218  }
219  }
220  }
221  }
222  delete _xml;
224 }
225 
226 void HcaluLUTTPGCoder::update(const HcalDbService& conditions) {
227 
228  HcalCalibrations calibrations;
229  const HcalLutMetadata *metadata = conditions.getHcalLutMetadata();
230  assert(metadata !=0);
231  float nominalgain_ = metadata->getNominalGain();
232 
233  std::map<int, float> cosh_ieta;
234  for (int i = firstHFEta_; i <= lastHFEta_; ++i){
235  std::pair<double,double> etas = topo_->etaRange(HcalForward,i);
236  double eta1 = etas.first;
237  double eta2 = etas.second;
238  cosh_ieta[i] = cosh((eta1 + eta2)/2.);
239  }
240 
242  for (int isub = 0; isub < 3; ++isub){
243  HcalSubdetector subdet = subdets[isub];
244  for (int ieta = -HcalDetId::kHcalEtaMask2; ieta <= HcalDetId::kHcalEtaMask2; ++ieta) {
245  for (int iphi = 0; iphi <= HcalDetId::kHcalPhiMask2; ++iphi) {
246  for (int depth = 1; depth < HcalDetId::kHcalDepthMask2; ++depth) {
247  HcalDetId cell(subdet, ieta, iphi, depth);
248  if (!topo_->valid(cell)) continue;
249 
250  const HcalQIECoder* channelCoder = conditions.getHcalCoder (cell);
251  const HcalQIEShape* shape = conditions.getHcalShape(cell);
252  HcalCoderDb coder (*channelCoder, *shape);
253  const HcalLutMetadatum *meta = metadata->getValues(cell);
254 
255  unsigned int mipMax = 0;
256  unsigned int mipMin = 0;
257 
259  const HcalTPChannelParameter *channelParameters = conditions.getHcalTPChannelParameter(cell);
260  mipMax = channelParameters->getFGBitInfo() >> 16;
261  mipMin = channelParameters->getFGBitInfo() & 0xFFFF;
262  }
263 
264  int lutId = getLUTId(subdet, ieta, iphi, depth);
265  float ped = 0;
266  float gain = 0;
267  uint32_t status = 0;
268 
269  if (LUTGenerationMode_){
270  const HcalCalibrations& calibrations = conditions.getHcalCalibrations(cell);
271  for (int capId = 0; capId < 4; ++capId){
272  ped += calibrations.pedestal(capId);
273  gain += calibrations.LUTrespcorrgain(capId);
274  }
275  ped /= 4.0;
276  gain /= 4.0;
277 
278  //Get Channel Quality
279  const HcalChannelStatus* channelStatus = conditions.getHcalChannelStatus(cell);
280  status = channelStatus->getValue();
281  } else {
282  const HcalL1TriggerObject* myL1TObj = conditions.getHcalL1TriggerObject(cell);
283  ped = myL1TObj->getPedestal();
284  gain = myL1TObj->getRespGain();
285  status = myL1TObj->getFlag();
286  } // LUTGenerationMode_
287 
288  ped_[lutId] = ped;
289  gain_[lutId] = gain;
290  bool isMasked = ( (status & bitToMask_) > 0 );
291  float rcalib = meta->getRCalib();
292 
293  // Input LUT for HB/HE/HF
294  if (subdet == HcalBarrel || subdet == HcalEndcap){
295  HBHEDataFrame frame(cell);
296  frame.setSize(1);
297  CaloSamples samples(cell, 1);
298 
299  int granularity = meta->getLutGranularity();
300 
301  for (unsigned int adc = 0; adc < INPUT_LUT_SIZE; ++adc) {
302  frame.setSample(0,HcalQIESample(adc));
303  coder.adc2fC(frame,samples);
304  float adc2fC = samples[0];
305 
306  if (isMasked) inputLUT_[lutId][adc] = 0;
307  else inputLUT_[lutId][adc] = (LutElement) std::min(std::max(0, int((adc2fC -ped) * gain * rcalib / nominalgain_ / granularity)), QIE8_LUT_BITMASK);
308  }
309 
310  unsigned short data[] = {0, 0, 0};
311  QIE11DataFrame upgradeFrame(edm::DataFrame(0, data, 3));
312  CaloSamples upgradeSamples(cell, 1);
313  for (unsigned int adc = 0; adc < UPGRADE_LUT_SIZE; ++adc) {
314  upgradeFrame.setSample(0, adc, 0, true);
315  coder.adc2fC(upgradeFrame, upgradeSamples);
316  float adc2fC = upgradeSamples[0];
317 
318  if (isMasked) {
319  upgradeQIE11LUT_[lutId][adc] = 0;
320  } else {
321  upgradeQIE11LUT_[lutId][adc] = (LutElement) std::min(std::max(0, int((adc2fC -ped) * gain * rcalib / nominalgain_ / granularity)), QIE11_LUT_BITMASK);
322  if (adc >= mipMin and adc < mipMax)
324  else if (adc >= mipMax)
326  }
327  }
328  } // endif HBHE
329  else if (subdet == HcalForward){
330  HFDataFrame frame(cell);
331  frame.setSize(1);
332  CaloSamples samples(cell, 1);
333 
334  for (unsigned int adc = 0; adc < INPUT_LUT_SIZE; ++adc) {
335  frame.setSample(0,HcalQIESample(adc));
336  coder.adc2fC(frame,samples);
337  float adc2fC = samples[0];
338  if (isMasked) inputLUT_[lutId][adc] = 0;
339  else inputLUT_[lutId][adc] = std::min(std::max(0,int((adc2fC - ped) * gain * rcalib / lsb_ / cosh_ieta[abs(ieta)] )), QIE8_LUT_BITMASK);
340  }
341 
342  unsigned short data[] = {0, 0, 0, 0};
343  QIE10DataFrame upgradeFrame(edm::DataFrame(0, data, 4));
344  CaloSamples upgradeSamples(cell, 1);
345  for (unsigned int adc = 0; adc < UPGRADE_LUT_SIZE; ++adc) {
346  upgradeFrame.setSample(0, adc, 0, 0, 0, true);
347  coder.adc2fC(upgradeFrame, upgradeSamples);
348  float adc2fC = upgradeSamples[0];
349 
350  if (isMasked)
351  upgradeQIE10LUT_[lutId][adc] = 0;
352  else
353  upgradeQIE10LUT_[lutId][adc] = std::min(std::max(0,int((adc2fC - ped) * gain * rcalib / lsb_ / cosh_ieta[abs(ieta)] )), QIE10_LUT_BITMASK);
354  }
355  } // endif HF
356 
357  } // for depth
358  } // for iphi
359  } // for iphi
360  }// for subdet
361 }
362 
364  int lutId = getLUTId(df.id());
365  const Lut& lut = inputLUT_.at(lutId);
366  for (int i=0; i<df.size(); i++){
367  ics[i] = (lut.at(df[i].adc()) & QIE8_LUT_BITMASK);
368  }
369 }
370 
372  int lutId = getLUTId(df.id());
373  const Lut& lut = inputLUT_.at(lutId);
374  for (int i=0; i<df.size(); i++){
375  ics[i] = (lut.at(df[i].adc()) & QIE8_LUT_BITMASK);
376  }
377 }
378 
380  int lutId = getLUTId(HcalDetId(df.id()));
381  const Lut& lut = upgradeQIE10LUT_.at(lutId);
382  for (int i=0; i<df.samples(); i++){
383  ics[i] = (lut.at(df[i].adc()) & QIE10_LUT_BITMASK);
384  }
385 }
386 
388  int lutId = getLUTId(HcalDetId(df.id()));
389  const Lut& lut = upgradeQIE11LUT_.at(lutId);
390  for (int i=0; i<df.samples(); i++){
391  ics[i] = (lut.at(df[i].adc()) & QIE11_LUT_BITMASK);
392  }
393 }
394 
396  int lutId = getLUTId(id);
397  return ((inputLUT_.at(lutId)).at(sample.adc()) & QIE8_LUT_BITMASK);
398 }
399 
401  int lutId = getLUTId(id);
402  return ped_.at(lutId);
403 }
404 
406  int lutId = getLUTId(id);
407  return gain_.at(lutId);
408 }
409 
410 std::vector<unsigned short> HcaluLUTTPGCoder::getLinearizationLUTWithMSB(const HcalDetId& id) const{
411  int lutId = getLUTId(id);
412  return inputLUT_.at(lutId);
413 }
414 
415 void HcaluLUTTPGCoder::lookupMSB(const HBHEDataFrame& df, std::vector<bool>& msb) const{
416  msb.resize(df.size());
417  for (int i=0; i<df.size(); ++i)
418  msb[i] = getMSB(df.id(), df.sample(i).adc());
419 }
420 
421 bool HcaluLUTTPGCoder::getMSB(const HcalDetId& id, int adc) const{
422  int lutId = getLUTId(id);
423  const Lut& lut = inputLUT_.at(lutId);
424  return (lut.at(adc) & QIE8_LUT_MSB);
425 }
426 
427 void
428 HcaluLUTTPGCoder::lookupMSB(const QIE11DataFrame& df, std::vector<std::bitset<2>>& msb) const
429 {
430  int lutId = getLUTId(HcalDetId(df.id()));
431  const Lut& lut = upgradeQIE11LUT_.at(lutId);
432  for (int i = 0; i < df.samples(); ++i) {
433  msb[i][0] = lut.at(df[i].adc()) & QIE11_LUT_MSB0;
434  msb[i][1] = lut.at(df[i].adc()) & QIE11_LUT_MSB1;
435  }
436 }
int adc(sample_type sample)
get the ADC sample (12 bits)
int samples() const
total number of samples in the digi
uint32_t getFlag() const
void setSample(edm::DataFrame::size_type isample, int adc, int tdc, bool soi=false)
set the sample contents
int firstHFRing() const
Definition: HcalTopology.h:87
float getPedestal() const
int i
Definition: DBlmapReader.cc:9
static const int nFi_
Definition: LutXml.h:27
HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:49
const HcalTPChannelParameter * getHcalTPChannelParameter(const HcalGenericDetId &fId) const
static const int QIE11_LUT_MSB1
static const int QIE11_LUT_BITMASK
virtual ~HcaluLUTTPGCoder()
const HcalChannelStatus * getHcalChannelStatus(const HcalGenericDetId &fId) const
int adc() const
get the ADC sample
Definition: HcalQIESample.h:22
int size() const
total number of samples in the digi
Definition: HBHEDataFrame.h:26
int create_lut_map(void)
Definition: LutXml.cc:497
assert(m_qm.get())
const HcalTopology * topo_
int firstHBRing() const
Definition: HcalTopology.h:83
edm::DataFrame::id_type id() const
int lastHBRing() const
Definition: HcalTopology.h:84
static const int QIE11_LUT_MSB0
static const float lsb_
double pedestal(int fCapId) const
get pedestal for capid=0..3
const Item * getValues(DetId fId, bool throwOnFail=true) const
static const int QIE8_LUT_BITMASK
uint8_t getLutGranularity() const
HcalTopologyMode::TriggerMode triggerMode() const
Definition: HcalTopology.h:32
void setSize(int size)
unsigned short LutElement
void update(const HcalDbService &conditions)
static const int kHcalDepthMask2
Definition: HcalDetId.h:26
void updateXML(const char *filename)
static const size_t UPGRADE_LUT_SIZE
virtual float getLUTGain(HcalDetId id) const override
int depth() const
get the tower depth
Definition: HcalDetId.cc:108
void lookupMSB(const HBHEDataFrame &df, std::vector< bool > &msb) const
int getLUTId(HcalSubdetector id, int ieta, int iphi, int depth) const
float getRespGain() const
void setSample(int i, const HcalQIESample &sam)
Definition: HBHEDataFrame.h:50
int terminate(void)
virtual void adc2fC(const HBHEDataFrame &df, CaloSamples &lf) const
Definition: HcalCoderDb.cc:68
int maxDepth(HcalSubdetector subdet) const
float getNominalGain() const
float getRCalib() const
const HcalL1TriggerObject * getHcalL1TriggerObject(const HcalGenericDetId &fId) const
int ieta() const
get the cell ieta
Definition: HcalDetId.h:56
int lastHFRing() const
Definition: HcalTopology.h:88
edm::DataFrame::id_type id() const
HcalSubdetector
Definition: HcalAssistant.h:31
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< unsigned short > getLinearizationLUTWithMSB(const HcalDetId &id) const
const HcalLutMetadata * getHcalLutMetadata() const
std::vector< LutElement > Lut
tuple lut
Definition: lumiPlot.py:244
T min(T a, T b)
Definition: MathUtil.h:58
virtual float getLUTPedestal(HcalDetId id) const override
void setSample(int i, const HcalQIESample &sam)
Definition: HFDataFrame.h:50
static const int QIE8_LUT_MSB
std::vector< Lut > upgradeQIE11LUT_
static const int QIE10_LUT_BITMASK
double const adc2fC[256]
Definition: Constants.h:221
int iphi() const
get the cell iphi
Definition: HcalDetId.cc:103
static const int kHcalPhiMask2
Definition: HcalDetId.h:16
double LUTrespcorrgain(int fCapId) const
get LUT corrected and response corrected gain for capid=0..3
std::vector< float > ped_
std::vector< float > gain_
int size() const
total number of samples in the digi
Definition: HFDataFrame.h:26
const HcalQIESample & sample(int i) const
access a sample
Definition: HBHEDataFrame.h:39
virtual void adc2Linear(const HBHEDataFrame &df, IntegerCaloSamples &ics) const override
int firstHERing() const
Definition: HcalTopology.h:85
const HcalQIECoder * getHcalCoder(const HcalGenericDetId &fId) const
virtual bool valid(const DetId &id) const
const HcalQIEShape * getHcalShape(const HcalGenericDetId &fId) const
void setSize(int size)
Definition: HFDataFrame.cc:17
std::vector< unsigned int > * getLutFast(uint32_t det_id)
Definition: LutXml.cc:127
static const size_t INPUT_LUT_SIZE
std::pair< double, double > etaRange(HcalSubdetector subdet, int ieta) const
HcaluLUTTPGCoder(const HcalTopology *topo)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
bool getMSB(const HcalDetId &id, int adc) const
tuple filename
Definition: lut2db_cfg.py:20
static const int kHcalEtaMask2
Definition: HcalDetId.h:20
uint32_t getFGBitInfo() const
get FG bit information
std::vector< Lut > upgradeQIE10LUT_
const HcalDetId & id() const
Definition: HBHEDataFrame.h:22
virtual void compress(const IntegerCaloSamples &ics, const std::vector< bool > &featureBits, HcalTriggerPrimitiveDigi &tp) const override
const HcalDetId & id() const
Definition: HFDataFrame.h:22
uint32_t getValue() const
const HcalCalibrations & getHcalCalibrations(const HcalGenericDetId &fId) const
void setSample(edm::DataFrame::size_type isample, int adc, int le_tdc, int te_tdc, int capid, bool soi=false, bool ok=true)
set the sample contents
int samples() const
total number of samples in the digi
static const std::string subdets[7]
Definition: TrackUtils.cc:72
tuple size
Write out results.
static XMLProcessor * getInstance()
Definition: XMLProcessor.h:145
int lastHERing() const
Definition: HcalTopology.h:86
Definition: Lut.h:32
tuple status
Definition: mps_update.py:57
std::vector< Lut > inputLUT_