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>
26 
27 const float HcaluLUTTPGCoder::lsb_=1./16;
28 
29 HcaluLUTTPGCoder::HcaluLUTTPGCoder(const HcalTopology* top) : topo_(top), LUTGenerationMode_(true), bitToMask_(0) {
45  size_t nluts= (size_t)(sizeHB_+sizeHE_+sizeHF_+1);
46  inputLUT_ = std::vector<HcaluLUTTPGCoder::Lut>(nluts,HcaluLUTTPGCoder::Lut(INPUT_LUT_SIZE, 0));
47  gain_ = std::vector<float>(nluts, 0.);
48  ped_ = std::vector<float>(nluts, 0.);
49 }
50 
51 void HcaluLUTTPGCoder::compress(const IntegerCaloSamples& ics, const std::vector<bool>& featureBits, HcalTriggerPrimitiveDigi& tp) const {
52  throw cms::Exception("PROBLEM: This method should never be invoked!");
53 }
54 
56 }
57 
58 int HcaluLUTTPGCoder::getLUTId(HcalSubdetector id, int ieta, int iphi, int depth) const {
59  int retval(0);
60  if (id == HcalBarrel) {
61  retval = (depth-1)+maxDepthHB_*(iphi-1);
62  if (ieta>0) retval+=maxDepthHB_*nFi_*(ieta-firstHBEta_);
63  else retval+=maxDepthHB_*nFi_*(ieta+lastHBEta_+nHBEta_);
64  } else if (id == HcalEndcap) {
65  retval = sizeHB_;
66  retval+= (depth-1)+maxDepthHE_*(iphi-1);
67  if (ieta>0) retval+=maxDepthHE_*nFi_*(ieta-firstHEEta_);
68  else retval+=maxDepthHE_*nFi_*(ieta+lastHEEta_+nHEEta_);
69  } else if (id == HcalForward) {
70  retval = sizeHB_+sizeHE_;
71  retval+= (depth-1)+maxDepthHF_*(iphi-1);
72  if (ieta>0) retval+=maxDepthHF_*nFi_*(ieta-firstHFEta_);
73  else retval+=maxDepthHF_*nFi_*(ieta+lastHFEta_+nHFEta_);
74  }
75  return retval;
76 }
77 
78 int HcaluLUTTPGCoder::getLUTId(uint32_t rawid) const {
79  HcalDetId detid(rawid);
80  return getLUTId(detid.subdet(), detid.ieta(), detid.iphi(), detid.depth());
81 }
82 
83 int HcaluLUTTPGCoder::getLUTId(const HcalDetId& detid) const {
84  return getLUTId(detid.subdet(), detid.ieta(), detid.iphi(), detid.depth());
85 }
86 
87 void HcaluLUTTPGCoder::update(const char* filename, bool appendMSB) {
88 
89  std::ifstream file(filename, std::ios::in);
90  assert(file.is_open());
91 
92  std::vector<HcalSubdetector> subdet;
93  std::string buffer;
94 
95  // Drop first (comment) line
96  std::getline(file, buffer);
97  std::getline(file, buffer);
98 
99  unsigned int index = buffer.find("H", 0);
100  while (index < buffer.length()){
101  std::string subdetStr = buffer.substr(index, 2);
102  if (subdetStr == "HB") subdet.push_back(HcalBarrel);
103  else if (subdetStr == "HE") subdet.push_back(HcalEndcap);
104  else if (subdetStr == "HF") subdet.push_back(HcalForward);
105  //TODO Check subdet
106  //else exception
107  index += 2;
108  index = buffer.find("H", index);
109  }
110 
111  // Get upper/lower ranges for ieta/iphi/depth
112  size_t nCol = subdet.size();
113  assert(nCol > 0);
114 
115  std::vector<int> ietaU;
116  std::vector<int> ietaL;
117  std::vector<int> iphiU;
118  std::vector<int> iphiL;
119  std::vector<int> depU;
120  std::vector<int> depL;
121  std::vector< Lut > lutFromFile(nCol);
122  LutElement lutValue;
123 
124  for (size_t i=0; i<nCol; ++i) {
125  int ieta;
126  file >> ieta;
127  ietaL.push_back(ieta);
128  }
129 
130  for (size_t i=0; i<nCol; ++i) {
131  int ieta;
132  file >> ieta;
133  ietaU.push_back(ieta);
134  }
135 
136  for (size_t i=0; i<nCol; ++i) {
137  int iphi;
138  file >> iphi;
139  iphiL.push_back(iphi);
140  }
141 
142  for (size_t i=0; i<nCol; ++i) {
143  int iphi;
144  file >> iphi;
145  iphiU.push_back(iphi);
146  }
147 
148  for (size_t i=0; i<nCol; ++i) {
149  int dep;
150  file >> dep;
151  depL.push_back(dep);
152  }
153 
154  for (size_t i=0; i<nCol; ++i) {
155  int dep;
156  file >> dep;
157  depU.push_back(dep);
158  }
159 
160  // Read Lut Entry
161  for (size_t i=0; file >> lutValue; i = (i+1) % nCol){
162  lutFromFile[i].push_back(lutValue);
163  }
164 
165  // Check lut size
166  for (size_t i=0; i<nCol; ++i) assert(lutFromFile[i].size() == INPUT_LUT_SIZE);
167 
168  for (size_t i=0; i<nCol; ++i){
169  for (int ieta = ietaL[i]; ieta <= ietaU[i]; ++ieta){
170  for (int iphi = iphiL[i]; iphi <= iphiU[i]; ++iphi){
171  for (int depth = depL[i]; depth <= depU[i]; ++depth){
172 
173  HcalDetId id(subdet[i], ieta, iphi, depth);
174  if (!topo_->valid(id)) continue;
175 
176  int lutId = getLUTId(id);
177  for (size_t adc = 0; adc < INPUT_LUT_SIZE; ++adc){
178  if (appendMSB){
179  // Append FG bit LUT to MSB
180  // MSB = Most Significant Bit = bit 10
181  // Overwrite bit 10
182  LutElement msb = (lutFromFile[i][adc] != 0 ? 0x400 : 0);
183  inputLUT_[lutId][adc] = (msb | (inputLUT_[lutId][adc] & 0x3FF));
184  }
185  else inputLUT_[lutId][adc] = lutFromFile[i][adc];
186  }// for adc
187  }// for depth
188  }// for iphi
189  }// for ieta
190  }// for nCol
191 }
192 
194  LutXml * _xml = new LutXml(filename);
195  _xml->create_lut_map();
197  for (int ieta = -HcalDetId::kHcalEtaMask2;
198  ieta <= HcalDetId::kHcalEtaMask2; ++ieta) {
199  for (int iphi = 0; iphi <= HcalDetId::kHcalPhiMask2; ++iphi) {
200  for (int depth = 1; depth < HcalDetId::kHcalDepthMask2; ++depth) {
201  for (int isub=0; isub<3; ++isub) {
202  HcalDetId detid(subdet[isub], ieta, iphi, depth);
203  if (!topo_->valid(detid)) continue;
204  int id = getLUTId(subdet[isub], ieta, iphi, depth);
205  std::vector<unsigned int>* lut = _xml->getLutFast(detid);
206  if (lut==0) throw cms::Exception("PROBLEM: No inputLUT_ in xml file for ") << detid << std::endl;
207  if (lut->size()!=128) throw cms::Exception ("PROBLEM: Wrong inputLUT_ size in xml file for ") << detid << std::endl;
208  for (int i=0; i<128; ++i) inputLUT_[id][i] = (LutElement)lut->at(i);
209  }
210  }
211  }
212  }
213  delete _xml;
215 }
216 
217 void HcaluLUTTPGCoder::update(const HcalDbService& conditions) {
218 
219  HcalCalibrations calibrations;
220  const HcalLutMetadata *metadata = conditions.getHcalLutMetadata();
221  assert(metadata !=0);
222  float nominalgain_ = metadata->getNominalGain();
223 
224  std::map<int, float> cosh_ieta;
225  for (int i = firstHFEta_; i <= lastHFEta_; ++i){
226  std::pair<double,double> etas = topo_->etaRange(HcalForward,i);
227  double eta1 = etas.first;
228  double eta2 = etas.second;
229  cosh_ieta[i] = cosh((eta1 + eta2)/2.);
230  }
231 
233  for (int isub = 0; isub < 3; ++isub){
234  HcalSubdetector subdet = subdets[isub];
235  for (int ieta = -HcalDetId::kHcalEtaMask2;
236  ieta <= HcalDetId::kHcalEtaMask2; ++ieta) {
237  for (int iphi = 0; iphi <= HcalDetId::kHcalPhiMask2; ++iphi) {
238  for (int depth = 1; depth < HcalDetId::kHcalDepthMask2; ++depth) {
239  HcalDetId cell(subdet, ieta, iphi, depth);
240  if (!topo_->valid(cell)) continue;
241 
242  const HcalQIECoder* channelCoder = conditions.getHcalCoder (cell);
243  const HcalQIEShape* shape = conditions.getHcalShape(cell);
244  HcalCoderDb coder (*channelCoder, *shape);
245  const HcalLutMetadatum *meta = metadata->getValues(cell);
246 
247  int lutId = getLUTId(subdet, ieta, iphi, depth);
248  float ped = 0;
249  float gain = 0;
250  uint32_t status = 0;
251 
252  if (LUTGenerationMode_){
253  const HcalCalibrations& calibrations = conditions.getHcalCalibrations(cell);
254  for (int capId = 0; capId < 4; ++capId){
255  ped += calibrations.pedestal(capId);
256  gain += calibrations.LUTrespcorrgain(capId);
257  }
258  ped /= 4.0;
259  gain /= 4.0;
260 
261  //Get Channel Quality
262  const HcalChannelStatus* channelStatus = conditions.getHcalChannelStatus(cell);
263  status = channelStatus->getValue();
264  }
265  else {
266  const HcalL1TriggerObject* myL1TObj = conditions.getHcalL1TriggerObject(cell);
267  ped = myL1TObj->getPedestal();
268  gain = myL1TObj->getRespGain();
269  status = myL1TObj->getFlag();
270  } // LUTGenerationMode_
271 
272  ped_[lutId] = ped;
273  gain_[lutId] = gain;
274  bool isMasked = ( (status & bitToMask_) > 0 );
275  float rcalib = meta->getRCalib();
276 
277  // Input LUT for HB/HE/HF
278  if (subdet == HcalBarrel || subdet == HcalEndcap){
279  HBHEDataFrame frame(cell);
280  frame.setSize(1);
281  CaloSamples samples(cell, 1);
282 
283  int granularity = meta->getLutGranularity();
284 
285  for (int adc = 0; adc <= 0x7F; ++adc) {
286  frame.setSample(0,HcalQIESample(adc));
287  coder.adc2fC(frame,samples);
288  float adc2fC = samples[0];
289 
290  if (isMasked) inputLUT_[lutId][adc] = 0;
291  else inputLUT_[lutId][adc] = (LutElement) std::min(std::max(0, int((adc2fC -ped) * gain * rcalib / nominalgain_ / granularity)), 0x3FF);
292  }
293  } // endif HBHE
294  else if (subdet == HcalForward){
295  HFDataFrame frame(cell);
296  frame.setSize(1);
297  CaloSamples samples(cell, 1);
298 
299  for (int adc = 0; adc <= 0x7F; ++adc) {
300  frame.setSample(0,HcalQIESample(adc));
301  coder.adc2fC(frame,samples);
302  float adc2fC = samples[0];
303  if (isMasked) inputLUT_[lutId][adc] = 0;
304  else inputLUT_[lutId][adc] = std::min(std::max(0,int((adc2fC - ped) * gain * rcalib / lsb_ / cosh_ieta[abs(ieta)] )), 0x3FF);
305  }
306  } // endif HF
307 
308  } // for depth
309  } // for iphi
310  } // for iphi
311  }// for subdet
312 }
313 
315  int lutId = getLUTId(df.id());
316  const Lut& lut = inputLUT_.at(lutId);
317  for (int i=0; i<df.size(); i++){
318  ics[i] = (lut.at(df[i].adc()) & 0x3FF);
319  }
320 }
321 
323  int lutId = getLUTId(df.id());
324  const Lut& lut = inputLUT_.at(lutId);
325  for (int i=0; i<df.size(); i++){
326  ics[i] = (lut.at(df[i].adc()) & 0x3FF);
327  }
328 }
329 
331  int lutId = getLUTId(id);
332  return ((inputLUT_.at(lutId)).at(sample.adc()) & 0x3FF);
333 }
334 
336  int lutId = getLUTId(id);
337  return ped_.at(lutId);
338 }
339 
341  int lutId = getLUTId(id);
342  return gain_.at(lutId);
343 }
344 
345 std::vector<unsigned short> HcaluLUTTPGCoder::getLinearizationLUTWithMSB(const HcalDetId& id) const{
346  int lutId = getLUTId(id);
347  return inputLUT_.at(lutId);
348 }
349 
350 void HcaluLUTTPGCoder::lookupMSB(const HBHEDataFrame& df, std::vector<bool>& msb) const{
351  msb.resize(df.size());
352  for (int i=0; i<df.size(); ++i)
353  msb[i] = getMSB(df.id(), df.sample(i).adc());
354 }
355 
356 bool HcaluLUTTPGCoder::getMSB(const HcalDetId& id, int adc) const{
357  int lutId = getLUTId(id);
358  const Lut& lut = inputLUT_.at(lutId);
359  return (lut.at(adc) & 0x400);
360 }
int adc(sample_type sample)
get the ADC sample (12 bits)
uint32_t getFlag() const
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
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
int lastHBRing() const
Definition: HcalTopology.h:84
static const float lsb_
double pedestal(int fCapId) const
get pedestal for capid=0..3
const Item * getValues(DetId fId, bool throwOnFail=true) const
uint8_t getLutGranularity() const
void setSize(int size)
unsigned short LutElement
void update(const HcalDbService &conditions)
virtual float getLUTPedestal(HcalDetId id) const
static const int kHcalDepthMask2
Definition: HcalDetId.h:26
void updateXML(const char *filename)
int depth() const
get the tower depth
Definition: HcalDetId.cc:106
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:60
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
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
char const * subdets[11]
void setSample(int i, const HcalQIESample &sam)
Definition: HFDataFrame.h:50
virtual float getLUTGain(HcalDetId id) const
int iphi() const
get the cell iphi
Definition: HcalDetId.cc:101
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
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)
bool getMSB(const HcalDetId &id, int adc) const
tuple filename
Definition: lut2db_cfg.py:20
static const int kHcalEtaMask2
Definition: HcalDetId.h:20
const HcalDetId & id() const
Definition: HBHEDataFrame.h:22
virtual void adc2Linear(const HBHEDataFrame &df, IntegerCaloSamples &ics) const
const HcalDetId & id() const
Definition: HFDataFrame.h:22
uint32_t getValue() const
const HcalCalibrations & getHcalCalibrations(const HcalGenericDetId &fId) const
virtual void compress(const IntegerCaloSamples &ics, const std::vector< bool > &featureBits, HcalTriggerPrimitiveDigi &tp) const
tuple size
Write out results.
static XMLProcessor * getInstance()
Definition: XMLProcessor.h:145
int lastHERing() const
Definition: HcalTopology.h:86
Definition: Lut.h:32
float adc2fC[128]
tuple status
Definition: mps_update.py:57
std::vector< Lut > inputLUT_