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