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, const HcalTopology& theTopo, 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  assert(nCol > 0);
85 
86  std::vector<int> ietaU;
87  std::vector<int> ietaL;
88  std::vector<int> iphiU;
89  std::vector<int> iphiL;
90  std::vector<int> depU;
91  std::vector<int> depL;
92  std::vector< Lut > lutFromFile(nCol);
93  LutElement lutValue;
94 
95  for (size_t i=0; i<nCol; ++i) {
96  int ieta;
97  file >> ieta;
98  ietaL.push_back(ieta);
99  }
100 
101  for (size_t i=0; i<nCol; ++i) {
102  int ieta;
103  file >> ieta;
104  ietaU.push_back(ieta);
105  }
106 
107  for (size_t i=0; i<nCol; ++i) {
108  int iphi;
109  file >> iphi;
110  iphiL.push_back(iphi);
111  }
112 
113  for (size_t i=0; i<nCol; ++i) {
114  int iphi;
115  file >> iphi;
116  iphiU.push_back(iphi);
117  }
118 
119  for (size_t i=0; i<nCol; ++i) {
120  int dep;
121  file >> dep;
122  depL.push_back(dep);
123  }
124 
125  for (size_t i=0; i<nCol; ++i) {
126  int dep;
127  file >> dep;
128  depU.push_back(dep);
129  }
130 
131  // Read Lut Entry
132  for (size_t i=0; file >> lutValue; i = (i+1) % nCol){
133  lutFromFile[i].push_back(lutValue);
134  }
135 
136  // Check lut size
137  for (size_t i=0; i<nCol; ++i) assert(lutFromFile[i].size() == INPUT_LUT_SIZE);
138 
139  for (size_t i=0; i<nCol; ++i){
140  for (int ieta = ietaL[i]; ieta <= ietaU[i]; ++ieta){
141  for (int iphi = iphiL[i]; iphi <= iphiU[i]; ++iphi){
142  for (int depth = depL[i]; depth <= depU[i]; ++depth){
143 
144  HcalDetId id(subdet[i], ieta, iphi, depth);
145  if (!theTopo.valid(id)) continue;
146 
147  int lutId = getLUTId(id);
148  for (size_t adc = 0; adc < INPUT_LUT_SIZE; ++adc){
149  if (appendMSB){
150  // Append FG bit LUT to MSB
151  // MSB = Most Significant Bit = bit 10
152  // Overwrite bit 10
153  LutElement msb = (lutFromFile[i][adc] != 0 ? 0x400 : 0);
154  inputLUT_[lutId][adc] = (msb | (inputLUT_[lutId][adc] & 0x3FF));
155  }
156  else inputLUT_[lutId][adc] = lutFromFile[i][adc];
157  }// for adc
158  }// for depth
159  }// for iphi
160  }// for ieta
161  }// for nCol
162 }
163 
164 void HcaluLUTTPGCoder::updateXML(const char* filename, const HcalTopology& theTopo) {
165  LutXml * _xml = new LutXml(filename);
166  _xml->create_lut_map();
168  for (int ieta=-41; ieta<=41; ++ieta){
169  for (int iphi=1; iphi<=72; ++iphi){
170  for (int depth=1; depth<=3; ++depth){
171  for (int isub=0; isub<3; ++isub){
172  HcalDetId detid(subdet[isub], ieta, iphi, depth);
173  if (!theTopo.valid(detid)) continue;
174  int id = getLUTId(subdet[isub], ieta, iphi, depth);
175  std::vector<unsigned int>* lut = _xml->getLutFast(detid);
176  if (lut==0) throw cms::Exception("PROBLEM: No inputLUT_ in xml file for ") << detid << std::endl;
177  if (lut->size()!=128) throw cms::Exception ("PROBLEM: Wrong inputLUT_ size in xml file for ") << detid << std::endl;
178  for (int i=0; i<128; ++i) inputLUT_[id][i] = (LutElement)lut->at(i);
179  }
180  }
181  }
182  }
183  delete _xml;
185 }
186 
187 void HcaluLUTTPGCoder::update(const HcalDbService& conditions) {
188 
189  HcalCalibrations calibrations;
190  const HcalLutMetadata *metadata = conditions.getHcalLutMetadata();
191  assert(metadata !=0);
192  float nominalgain_ = metadata->getNominalGain();
193 
194  std::map<int, float> cosh_ieta;
195  for (int i = 0; i < 13; ++i)
196  cosh_ieta[i+29] = cosh((theHFEtaBounds[i+1] + theHFEtaBounds[i])/2.);
197 
199  for (int isub = 0; isub < 3; ++isub){
200  HcalSubdetector subdet = subdets[isub];
201  for (int ieta = -41; ieta <= 41; ++ieta){
202  for (int iphi = 1; iphi <= 72; ++iphi){
203  for (int depth = 1; depth <= 3; ++depth){
204  HcalDetId cell(subdet, ieta, iphi, depth);
205  if (!metadata->topo()->valid(cell)) continue;
206 
207  const HcalQIECoder* channelCoder = conditions.getHcalCoder (cell);
208  const HcalQIEShape* shape = conditions.getHcalShape(cell);
209  HcalCoderDb coder (*channelCoder, *shape);
210  const HcalLutMetadatum *meta = metadata->getValues(cell);
211 
212 
213  int lutId = getLUTId(subdet, ieta, iphi, depth);
214  float ped = 0;
215  float gain = 0;
216  uint32_t status = 0;
217 
218  if (LUTGenerationMode_){
219  const HcalCalibrations& calibrations = conditions.getHcalCalibrations(cell);
220  for (int capId = 0; capId < 4; ++capId){
221  ped += calibrations.pedestal(capId);
222  gain += calibrations.LUTrespcorrgain(capId);
223  }
224  ped /= 4.0;
225  gain /= 4.0;
226 
227  //Get Channel Quality
228  const HcalChannelStatus* channelStatus = conditions.getHcalChannelStatus(cell);
229  status = channelStatus->getValue();
230  }
231  else {
232  const HcalL1TriggerObject* myL1TObj = conditions.getHcalL1TriggerObject(cell);
233  ped = myL1TObj->getPedestal();
234  gain = myL1TObj->getRespGain();
235  status = myL1TObj->getFlag();
236  } // LUTGenerationMode_
237 
238  ped_[lutId] = ped;
239  gain_[lutId] = gain;
240  bool isMasked = ( (status & bitToMask_) > 0 );
241  float rcalib = meta->getRCalib();
242 
243  // Input LUT for HB/HE/HF
244  if (subdet == HcalBarrel || subdet == HcalEndcap){
245  HBHEDataFrame frame(cell);
246  frame.setSize(1);
247  CaloSamples samples(cell, 1);
248 
249  int granularity = meta->getLutGranularity();
250 
251  for (int adc = 0; adc <= 0x7F; ++adc) {
252  frame.setSample(0,HcalQIESample(adc));
253  coder.adc2fC(frame,samples);
254  float adc2fC = samples[0];
255 
256  if (isMasked) inputLUT_[lutId][adc] = 0;
257  else inputLUT_[lutId][adc] = (LutElement) std::min(std::max(0, int((adc2fC -ped) * gain * rcalib / nominalgain_ / granularity)), 0x3FF);
258  }
259  } // endif HBHE
260  else if (subdet == HcalForward){
261  HFDataFrame frame(cell);
262  frame.setSize(1);
263  CaloSamples samples(cell, 1);
264 
265  float one_adc2fC = 0.0;
266  for (int capId = 0; capId < 4; ++capId)
267  one_adc2fC += channelCoder->charge(*shape, 1, capId) - channelCoder->charge(*shape, 0, capId);
268  one_adc2fC /= 4.0;
269  // Lumi offset of 1 adc (in fC) for the four rings used to measure lumi
270  float offset = (abs(ieta) >= 33 && abs(ieta) <= 36) ? one_adc2fC : 0;
271 
272  for (int adc = 0; adc <= 0x7F; ++adc) {
273  frame.setSample(0,HcalQIESample(adc));
274  coder.adc2fC(frame,samples);
275  float adc2fC = samples[0];
276  if (isMasked) inputLUT_[lutId][adc] = 0;
277  else inputLUT_[lutId][adc] = std::min(std::max(0,int((adc2fC - ped + offset) * gain * rcalib / lsb_ / cosh_ieta[abs(ieta)] )), 0x3FF);
278  }
279  } // endif HF
280  } // for depth
281  } // for iphi
282  } // for iphi
283  }// for subdet
284 }
285 
287  int lutId = getLUTId(df.id());
288  const Lut& lut = inputLUT_.at(lutId);
289  for (int i=0; i<df.size(); i++){
290  ics[i] = (lut.at(df[i].adc()) & 0x3FF);
291  }
292 }
293 
295  int lutId = getLUTId(df.id());
296  const Lut& lut = inputLUT_.at(lutId);
297  for (int i=0; i<df.size(); i++){
298  ics[i] = (lut.at(df[i].adc()) & 0x3FF);
299  }
300 }
301 
303  int lutId = getLUTId(id);
304  return ((inputLUT_.at(lutId)).at(sample.adc()) & 0x3FF);
305 }
306 
308  int lutId = getLUTId(id);
309  return ped_.at(lutId);
310 }
311 
313  int lutId = getLUTId(id);
314  return gain_.at(lutId);
315 }
316 
317 std::vector<unsigned short> HcaluLUTTPGCoder::getLinearizationLUTWithMSB(const HcalDetId& id) const{
318  int lutId = getLUTId(id);
319  return inputLUT_.at(lutId);
320 }
321 
322 void HcaluLUTTPGCoder::lookupMSB(const HBHEDataFrame& df, std::vector<bool>& msb) const{
323  msb.resize(df.size());
324  for (int i=0; i<df.size(); ++i)
325  msb[i] = getMSB(df.id(), df.sample(i).adc());
326 }
327 
328 bool HcaluLUTTPGCoder::getMSB(const HcalDetId& id, int adc) const{
329  int lutId = getLUTId(id);
330  const Lut& lut = inputLUT_.at(lutId);
331  return (lut.at(adc) & 0x400);
332 }
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:27
HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:30
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
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:40
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:36
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
unsigned int offset(bool)
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:38
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
virtual bool valid(const DetId &id) const
Definition: HcalTopology.cc:58
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
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:146
Definition: Lut.h:32
const HcalTopology * topo() const
float adc2fC[128]
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