CMS 3D CMS Logo

LutXml.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CaloOnlineTools/HcalOnlineDb
4 // Class : LutXml
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Gena Kukartsev, kukarzev@fnal.gov
10 // Created: Tue Mar 18 14:30:20 CDT 2008
11 //
12 
13 #include <iostream>
14 #include <string>
15 #include <vector>
16 #include <sstream>
17 #include <iconv.h>
18 #include <sys/time.h>
19 
22 #include "md5.h"
27 
28 using namespace std;
30 
32  infotype = "LUT";
33  ieta = -1000;
34  iphi = -1000;
35  depth = -1;
36  crate = -1;
37  slot = -1;
38  topbottom = -1;
39  fiber = -1;
40  fiberchan = -1;
41  lut_type = -1;
42  creationtag = "default_tag";
43 
44  char timebuf[50];
45  time_t _time = time(nullptr);
46  strftime(timebuf, 50, "%Y-%m-%d %H:%M:%S", gmtime(&_time));
47  creationstamp = timebuf;
48 
49  formatrevision = "default_revision";
50  targetfirmware = "default_revision";
51  generalizedindex = -1;
52  weight = -1;
53 }
54 
55 LutXml::LutXml() : XMLDOMBlock("CFGBrickSet", 1) { init(); }
56 
57 LutXml::LutXml(InputSource &_source) : XMLDOMBlock(_source) { init(); }
58 
60 
64 }
65 
66 void LutXml::init(void) {
67  root = XMLString::transcode("CFGBrickSet");
68  brick = XMLString::transcode("CFGBrick");
69  brickElem = nullptr;
70 }
71 
72 std::vector<unsigned int> *LutXml::getLutFast(uint32_t det_id) {
73  if (lut_map.find(det_id) != lut_map.end())
74  return &(lut_map)[det_id];
75  edm::LogError("LutXml") << "LUT not found, null pointer is returned";
76  return nullptr;
77 }
78 
79 // checksums_xml is 0 by default
80 void LutXml::addLut(LutXml::Config &_config, XMLDOMBlock *checksums_xml) {
81  DOMElement *rootElem = document->getDocumentElement();
82 
83  brickElem = document->createElement(XMLProcessor::_toXMLCh("CFGBrick"));
84  rootElem->appendChild(brickElem);
85 
86  addParameter("INFOTYPE", "string", _config.infotype);
87  addParameter("CREATIONTAG", "string", _config.creationtag);
88  addParameter("CREATIONSTAMP", "string", _config.creationstamp);
89  addParameter("FORMATREVISION", "string", _config.formatrevision);
90  addParameter("TARGETFIRMWARE", "string", _config.targetfirmware);
91  addParameter("GENERALIZEDINDEX", "int", _config.generalizedindex);
92  addParameter("CRATE", "int", _config.crate);
93  addParameter("SLOT", "int", _config.slot);
94 
95  if (checksums_xml) {
96  addParameter("CHECKSUM", "string", get_checksum(_config.lut));
97  }
98 
99  if (_config.lut_type == 1) { // linearizer LUT
100  addParameter("IETA", "int", _config.ieta);
101  addParameter("IPHI", "int", _config.iphi);
102  addParameter("TOPBOTTOM", "int", _config.topbottom);
103  addParameter("LUT_TYPE", "int", _config.lut_type);
104  addParameter("FIBER", "int", _config.fiber);
105  addParameter("FIBERCHAN", "int", _config.fiberchan);
106  addParameter("DEPTH", "int", _config.depth);
107  addData(to_string(_config.lut.size()), "hex", _config.lut);
108  } else if (_config.lut_type == 2 || _config.lut_type == 4) { // compression LUT or HE feature bit LUT
109  addParameter("IETA", "int", _config.ieta);
110  addParameter("IPHI", "int", _config.iphi);
111  addParameter("TOPBOTTOM", "int", _config.topbottom);
112  addParameter("LUT_TYPE", "int", _config.lut_type);
113  addParameter("SLB", "int", _config.fiber);
114  addParameter("SLBCHAN", "int", _config.fiberchan);
115  addParameter("WEIGHT", "int", _config.weight);
116  addData(to_string(_config.lut.size()), "hex", _config.lut);
117  } else if (_config.lut_type == 5) { // channel masks
118  addParameter("MASK_TYPE", "string", "TRIGGERCHANMASK");
119  addData(to_string(_config.mask.size()), "hex", _config.mask);
120  } else if (_config.lut_type == 6) { // adc threshold for tdc mask
121  addParameter("THRESH_TYPE", "string", "TRIGINTIME");
122  addData(to_string(_config.mask.size()), "hex", _config.mask);
123  } else if (_config.lut_type == 7) { // tdc mask
124  addParameter("TDCMAP_TYPE", "string", "TRIGINTIME");
125  addData(to_string(_config.mask.size()), "hex", _config.mask);
126  } else {
127  edm::LogError("LutXml") << "Unknown LUT type...produced XML will be incorrect";
128  }
129 
130  if (checksums_xml) {
131  add_checksum(checksums_xml->getDocument(), _config);
132  }
133 }
134 
135 template <typename T>
136 DOMElement *LutXml::addData(std::string _elements, std::string _encoding, const T &_lut) {
137  DOMElement *child = document->createElement(XMLProcessor::_toXMLCh("Data"));
138  child->setAttribute(XMLProcessor::_toXMLCh("elements"), XMLProcessor::_toXMLCh(_elements));
139  child->setAttribute(XMLProcessor::_toXMLCh("encoding"), XMLProcessor::_toXMLCh(_encoding));
140 
141  std::stringstream buf;
142 
143  for (const auto &iter : _lut) {
144  char buf2[16];
145  sprintf(buf2, "%lx", uint64_t(iter));
146  buf << buf2 << " ";
147  }
148 
149  std::string _value = buf.str();
150 
151  DOMText *data_value = document->createTextNode(XMLProcessor::_toXMLCh(_value));
152  child->appendChild(data_value);
153 
154  brickElem->appendChild(child);
155 
156  return child;
157 }
158 
159 DOMElement *LutXml::add_checksum(DOMDocument *parent, Config &config) {
160  DOMElement *child = parent->createElement(XMLProcessor::_toXMLCh("Data"));
161  child->setAttribute(XMLProcessor::_toXMLCh("crate"), XMLProcessor::_toXMLCh(config.crate));
162  child->setAttribute(XMLProcessor::_toXMLCh("slot"), XMLProcessor::_toXMLCh(config.slot));
163  child->setAttribute(XMLProcessor::_toXMLCh("fpga"), XMLProcessor::_toXMLCh(config.topbottom));
164  child->setAttribute(XMLProcessor::_toXMLCh("fiber"), XMLProcessor::_toXMLCh(config.fiber));
165  child->setAttribute(XMLProcessor::_toXMLCh("fiberchan"), XMLProcessor::_toXMLCh(config.fiberchan));
166  child->setAttribute(XMLProcessor::_toXMLCh("luttype"), XMLProcessor::_toXMLCh(config.lut_type));
167  child->setAttribute(XMLProcessor::_toXMLCh("elements"), XMLProcessor::_toXMLCh("1"));
168  child->setAttribute(XMLProcessor::_toXMLCh("encoding"), XMLProcessor::_toXMLCh("hex"));
169  DOMText *checksum_value = parent->createTextNode(XMLProcessor::_toXMLCh(get_checksum(config.lut)));
170  child->appendChild(checksum_value);
171 
172  parent->getDocumentElement()->appendChild(child);
173 
174  return child;
175 }
176 
177 DOMElement *LutXml::addParameter(std::string _name, std::string _type, std::string _value) {
178  DOMElement *child = document->createElement(XMLProcessor::_toXMLCh("Parameter"));
179  child->setAttribute(XMLProcessor::_toXMLCh("name"), XMLProcessor::_toXMLCh(_name));
180  child->setAttribute(XMLProcessor::_toXMLCh("type"), XMLProcessor::_toXMLCh(_type));
181  DOMText *parameter_value = document->createTextNode(XMLProcessor::_toXMLCh(_value));
182  child->appendChild(parameter_value);
183 
184  brickElem->appendChild(child);
185 
186  return child;
187 }
188 
189 DOMElement *LutXml::addParameter(std::string _name, std::string _type, int _value) {
190  char buf[128];
191  sprintf(buf, "%d", _value);
192  std::string str_value = buf;
193  return addParameter(_name, _type, str_value);
194 }
195 
197 
198 // do MD5 checksum
199 std::string LutXml::get_checksum(std::vector<unsigned int> &lut) {
200  std::stringstream result;
201  md5_state_t md5er;
202  md5_byte_t digest[16];
203  md5_init(&md5er);
204  // linearizer LUT:
205  if (lut.size() == 128) {
206  unsigned char tool[2];
207  for (int i = 0; i < 128; i++) {
208  tool[0] = lut[i] & 0xFF;
209  tool[1] = (lut[i] >> 8) & 0xFF;
210  md5_append(&md5er, tool, 2);
211  }
212  } else if (lut.size() == 256) {
213  unsigned char tool[2];
214  for (int i = 0; i < 256; i++) {
215  tool[0] = lut[i] & 0xFF;
216  tool[1] = (lut[i] >> 8) & 0xFF;
217  md5_append(&md5er, tool, 2);
218  }
219  }
220  // compression LUT:
221  else if (lut.size() == 1024) {
222  unsigned char tool;
223  for (int i = 0; i < 1024; i++) {
224  tool = lut[i] & 0xFF;
225  md5_append(&md5er, &tool, 1);
226  }
227  } else if (lut.size() == 2048) {
228  unsigned char tool;
229  for (int i = 0; i < 2048; i++) {
230  tool = lut[i] & 0xFF;
231  md5_append(&md5er, &tool, 1);
232  }
233  }
234  // HE fine grain LUT
235  else if (lut.size() == 4096) {
236  unsigned char tool;
237  for (int i = 0; i < 4096; i++) {
238  tool = lut[i] & 0xFF;
239  md5_append(&md5er, &tool, 1);
240  }
241  } else {
242  edm::LogError("LutXml") << "Irregular LUT size, " << lut.size()
243  << " , do not know how to compute checksum, exiting...";
244  exit(-1);
245  }
246  md5_finish(&md5er, digest);
247  for (int i = 0; i < 16; i++)
248  result << std::hex << (((int)(digest[i])) & 0xFF);
249 
250  return result.str();
251 }
252 
254  edm::LogInfo("LutXml") << "Created map size: " << lut_map.size();
255 
256  struct timeval _t;
257  gettimeofday(&_t, nullptr);
258  double _time = (double)(_t.tv_sec) + (double)(_t.tv_usec) / 1000000.0;
259 
260  HcalEmap _emap("./backup/official_emap_v6.04_080905.txt");
261  std::vector<HcalEmap::HcalEmapRow> &_map = _emap.get_map();
262  edm::LogInfo("LutXml") << "HcalEmap contains " << _map.size() << " entries";
263 
264  int _counter = 0;
265  for (std::vector<HcalEmap::HcalEmapRow>::const_iterator row = _map.begin(); row != _map.end(); ++row) {
266  if (row->subdet == "HB") {
267  HcalDetId det_id(HcalBarrel, row->ieta, row->iphi, row->idepth);
268  uint32_t raw_id = det_id.rawId();
269  std::vector<unsigned int> *l = getLutFast(raw_id);
270  if (l)
271  _counter++;
272  }
273  if (row->subdet == "HE") {
274  HcalDetId det_id(HcalEndcap, row->ieta, row->iphi, row->idepth);
275  uint32_t raw_id = det_id.rawId();
276  std::vector<unsigned int> *l = getLutFast(raw_id);
277  if (l)
278  _counter++;
279  }
280  if (row->subdet == "HF") {
281  HcalDetId det_id(HcalForward, row->ieta, row->iphi, row->idepth);
282  uint32_t raw_id = det_id.rawId();
283  std::vector<unsigned int> *l = getLutFast(raw_id);
284  if (l)
285  _counter++;
286  }
287  if (row->subdet == "HO") {
288  HcalDetId det_id(HcalOuter, row->ieta, row->iphi, row->idepth);
289  uint32_t raw_id = det_id.rawId();
290  std::vector<unsigned int> *l = getLutFast(raw_id);
291  if (l)
292  _counter++;
293  }
294  }
295  gettimeofday(&_t, nullptr);
296  edm::LogInfo("LutXml") << "access to " << _counter
297  << " HCAL channels took: " << (double)(_t.tv_sec) + (double)(_t.tv_usec) / 1000000.0 - _time
298  << "sec";
299 
300  return 0;
301 }
302 
303 HcalSubdetector LutXml::subdet_from_crate(int crate_, int slot, int fiber) {
304  // HBHE: 0,1,4,5,10,11,14,15,17 (+20)
305  // HF: 2,9,12 (+20)
306  // HO: 3,6,7,13,18 (+20)
307  int crate = crate_ < 20 ? crate_ : crate_ - 20;
308  if (crate == 2 || crate == 9 || crate == 12)
309  return HcalForward;
310  else if (crate == 3 || crate == 6 || crate == 7 || crate == 13 || crate == 18)
311  return HcalOuter;
312  else if (crate == 0 || crate == 1 || crate == 4 || crate == 5 || crate == 10 || crate == 11 || crate == 14 ||
313  crate == 15 || crate == 17) {
314  if (slot % 3 == 1)
315  return HcalBarrel;
316  else if (slot % 3 == 0)
317  return HcalEndcap;
318  else if (fiber < 12)
319  return HcalBarrel;
320  else
321  return HcalEndcap;
322  }
323  edm::LogWarning("LutXml::subdet_from_crate") << "crate " << crate_ << " is not accounted for";
324  return HcalEmpty;
325 }
326 
327 int LutXml::a_to_i(char *inbuf) {
328  int result;
329  sscanf(inbuf, "%d", &result);
330  return result;
331 }
332 
333 // organize all LUTs in XML into a map for fast access
334 //
335 // FIXME: uses hardcoded CRATE-to-subdetector mapping
336 // FIXME: it would be better to use some official map
337 //
339  //delete lut_map;
340  lut_map.clear();
341  //lut_map = new std::map<uint32_t,std::vector<unsigned int> >();
342 
343  if (document) {
344  //DOMElement * rootElem =
345  DOMNodeList *brick_list = document->getDocumentElement()->getElementsByTagName(brick);
346  int n_of_bricks = brick_list->getLength();
347  for (int i = 0; i != n_of_bricks; i++) {
348  DOMElement *aBrick = (DOMElement *)(brick_list->item(i));
349  DOMNodeList *par_list = aBrick->getElementsByTagName(XMLString::transcode("Parameter"));
350  int n_of_par = par_list->getLength();
351  int ieta = -99;
352  int iphi = -99;
353  int depth = -99;
354  int crate = -99;
355  int slot = -99;
356  int fiber = -99;
357  int lut_type = -99;
358  int slb = -99;
359  HcalSubdetector subdet;
360  for (int j = 0; j != n_of_par; j++) {
361  DOMElement *aPar = (DOMElement *)(par_list->item(j));
362  char *aName = XMLString::transcode(aPar->getAttribute(XMLProcessor::_toXMLCh("name")));
363  if (strcmp(aName, "IETA") == 0)
364  ieta = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
365  if (strcmp(aName, "IPHI") == 0)
366  iphi = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
367  if (strcmp(aName, "DEPTH") == 0)
368  depth = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
369  if (strcmp(aName, "CRATE") == 0)
370  crate = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
371  if (strcmp(aName, "SLOT") == 0)
372  slot = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
373  if (strcmp(aName, "FIBER") == 0)
374  fiber = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
375  if (strcmp(aName, "LUT_TYPE") == 0)
376  lut_type = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
377  if (strcmp(aName, "SLB") == 0)
378  slb = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
379  }
380  subdet = subdet_from_crate(crate, slot, fiber);
381  DOMElement *_data = (DOMElement *)(aBrick->getElementsByTagName(XMLString::transcode("Data"))->item(0));
382  char *_str = XMLString::transcode(_data->getFirstChild()->getNodeValue());
383 
384  // get the LUT vector
385  int _string_length = strlen(_str);
386  std::vector<unsigned int> _lut;
387  unsigned int _base = 16;
388  unsigned int _item = 0;
389  for (int i = 0; i != _string_length; i++) {
390  bool _range = false;
391  char ch_cur = _str[i];
392  if (_base == 16)
393  _range = (ch_cur >= '0' and ch_cur <= '9') || (ch_cur >= 'a' and ch_cur <= 'f') ||
394  (ch_cur >= 'A' and ch_cur <= 'F');
395  else if (_base == 10)
396  _range = (ch_cur >= '0' and ch_cur <= '9');
397  if (_range) {
398  if (ch_cur >= 'a' and ch_cur <= 'f')
399  ch_cur += 10 - 'a';
400  else if (ch_cur >= 'A' and ch_cur <= 'F')
401  ch_cur += 10 - 'A';
402  else if (ch_cur >= '0' and ch_cur <= '9')
403  ch_cur += -'0';
404  _item = _item * _base;
405  _item += ch_cur;
406  bool last_digit = false;
407  if ((i + 1) == _string_length)
408  last_digit = true;
409  else {
410  char ch_next = _str[i + 1];
411  bool _range_next = false;
412  if (_base == 16)
413  _range_next = (ch_next >= '0' and ch_next <= '9') || (ch_next >= 'a' and ch_next <= 'f') ||
414  (ch_next >= 'A' and ch_next <= 'F');
415  else if (_base == 10)
416  _range_next = (ch_next >= '0' and ch_next <= '9');
417  if (!_range_next)
418  last_digit = true;
419  }
420  if (last_digit) {
421  _lut.push_back(_item);
422  _item = 0;
423  }
424  }
425  }
426  // filling the map
427  uint32_t _key = 0;
428  if (lut_type == 1) {
429  HcalDetId _id(subdet, ieta, iphi, depth);
430  _key = _id.rawId();
431  } else if (lut_type == 2) {
432  int version = (abs(ieta) > 29 && slb != 12 && crate > 20) ? 1 : 0;
433  HcalTrigTowerDetId _id(ieta, iphi, 10 * version);
434  _key = _id.rawId();
435  } else
436  continue;
437  lut_map.insert(std::pair<uint32_t, std::vector<unsigned int> >(_key, _lut));
438  }
439  } else {
440  edm::LogError("LutXml") << "XML file with LUTs is not loaded, cannot create map!";
441  }
442 
443  return 0;
444 }
445 
446 LutXml::const_iterator LutXml::begin() const { return lut_map.begin(); }
447 
448 LutXml::const_iterator LutXml::end() const { return lut_map.end(); }
449 
450 LutXml::const_iterator LutXml::find(uint32_t id) const { return lut_map.find(id); }
XERCES_CPP_NAMESPACE::DOMElement * addParameter(std::string _name, std::string _type, std::string _value)
Definition: LutXml.cc:177
XERCES_CPP_NAMESPACE::DOMDocument * document
Definition: XMLDOMBlock.h:117
std::string targetfirmware
Definition: LutXml.h:36
int generalizedindex
Definition: LutXml.h:37
void addLut(Config &_config, XMLDOMBlock *checksums_xml=nullptr)
Definition: LutXml.cc:80
static int slb(const HcalTriggerPrimitiveSample &theSample)
XERCES_CPP_NAMESPACE::DOMElement * add_checksum(XERCES_CPP_NAMESPACE::DOMDocument *parent, Config &config)
Definition: LutXml.cc:159
XERCES_CPP_NAMESPACE::DOMElement * addData(std::string _elements, std::string _encoding, const T &_lut)
int create_lut_map(void)
Definition: LutXml.cc:338
std::string to_string(const V &value)
Definition: OMSAccess.h:71
Definition: weight.py:1
Definition: config.py:1
Log< level::Error, false > LogError
int a_to_i(char *inbuf)
Definition: LutXml.cc:327
std::string & getCurrentBrick(void)
Definition: LutXml.cc:196
~LutXml() override
Definition: LutXml.cc:61
int topbottom
Definition: LutXml.h:32
std::string & getString(void)
Definition: XMLDOMBlock.cc:419
const_iterator find(uint32_t) const
Definition: LutXml.cc:450
std::vector< unsigned int > lut
Definition: LutXml.h:39
std::string creationtag
Definition: LutXml.h:33
XERCES_CPP_NAMESPACE::DOMDocument * getDocument(void)
Definition: XMLDOMBlock.cc:268
static std::string get_checksum(std::vector< unsigned int > &lut)
Definition: LutXml.cc:199
HcalSubdetector
Definition: HcalAssistant.h:31
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void init(void)
Definition: LutXml.cc:66
std::string formatrevision
Definition: LutXml.h:35
Log< level::Info, false > LogInfo
HcalSubdetector subdet_from_crate(int crate, int slot, int fiber)
Definition: LutXml.cc:303
std::map< uint32_t, std::vector< unsigned int > > lut_map
Definition: LutXml.h:92
unsigned long long uint64_t
Definition: Time.h:13
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
const_iterator end() const
Definition: LutXml.cc:448
std::string infotype
Definition: LutXml.h:31
std::map< uint32_t, std::vector< unsigned int > >::const_iterator const_iterator
Definition: LutXml.h:73
std::vector< HcalEmap::HcalEmapRow > & get_map(void)
Definition: HcalEmap.cc:71
static XMLCh * _toXMLCh(std::string temp)
Definition: XMLProcessor.h:172
std::vector< unsigned int > * getLutFast(uint32_t det_id)
Definition: LutXml.cc:72
XMLCh * brick
Definition: LutXml.h:80
int test_access(std::string filename)
Definition: LutXml.cc:253
Definition: Config.py:1
Log< level::Warning, false > LogWarning
long double T
XERCES_CPP_NAMESPACE::DOMElement * brickElem
Definition: LutXml.h:89
const_iterator begin() const
Definition: LutXml.cc:446
int lut_type
Definition: LutXml.h:32
std::string creationstamp
Definition: LutXml.h:34
int fiberchan
Definition: LutXml.h:32
LutXml()
Definition: LutXml.cc:55
Helper class to handle FWLite file input sources.
def exit(msg="")
std::vector< uint64_t > mask
Definition: LutXml.h:40