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