Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-03-30 22:45:10

0001 // -*- C++ -*-
0002 //
0003 // Package:     CaloOnlineTools/HcalOnlineDb
0004 // Class  :     LutXml
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Gena Kukartsev, kukarzev@fnal.gov
0010 //         Created:  Tue Mar 18 14:30:20 CDT 2008
0011 //
0012 
0013 #include <iostream>
0014 #include <string>
0015 #include <vector>
0016 #include <sstream>
0017 #include <iconv.h>
0018 #include <sys/time.h>
0019 
0020 #include "CalibCalorimetry/HcalTPGAlgos/interface/LutXml.h"
0021 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
0022 #include "md5.h"
0023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0024 #include "CalibCalorimetry/HcalTPGAlgos/interface/HcalEmap.h"
0025 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0026 #include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
0027 
0028 using namespace std;
0029 XERCES_CPP_NAMESPACE_USE
0030 
0031 LutXml::Config::_Config() {
0032   infotype = "LUT";
0033   ieta = -1000;
0034   iphi = -1000;
0035   depth = -1;
0036   crate = -1;
0037   slot = -1;
0038   topbottom = -1;
0039   fiber = -1;
0040   fiberchan = -1;
0041   lut_type = -1;
0042   creationtag = "default_tag";
0043 
0044   char timebuf[50];
0045   time_t _time = time(nullptr);
0046   strftime(timebuf, 50, "%Y-%m-%d %H:%M:%S", gmtime(&_time));
0047   creationstamp = timebuf;
0048 
0049   formatrevision = "default_revision";
0050   targetfirmware = "default_revision";
0051   generalizedindex = -1;
0052   weight = -1;
0053 }
0054 
0055 LutXml::LutXml() : XMLDOMBlock("CFGBrickSet", 1) { init(); }
0056 
0057 LutXml::LutXml(InputSource &_source) : XMLDOMBlock(_source) { init(); }
0058 
0059 LutXml::LutXml(std::string filename) : XMLDOMBlock(filename) { init(); }
0060 
0061 LutXml::~LutXml() {
0062   XMLString::release(&root);
0063   XMLString::release(&brick);
0064 }
0065 
0066 void LutXml::init(void) {
0067   root = XMLString::transcode("CFGBrickSet");
0068   brick = XMLString::transcode("CFGBrick");
0069   brickElem = nullptr;
0070 }
0071 
0072 std::vector<unsigned int> *LutXml::getLutFast(uint32_t det_id) {
0073   if (lut_map.find(det_id) != lut_map.end())
0074     return &(lut_map)[det_id];
0075   edm::LogError("LutXml") << "LUT not found, null pointer is returned";
0076   return nullptr;
0077 }
0078 
0079 // checksums_xml is 0 by default
0080 void LutXml::addLut(LutXml::Config &_config, XMLDOMBlock *checksums_xml) {
0081   DOMElement *rootElem = document->getDocumentElement();
0082 
0083   brickElem = document->createElement(XMLProcessor::_toXMLCh("CFGBrick"));
0084   rootElem->appendChild(brickElem);
0085 
0086   addParameter("INFOTYPE", "string", _config.infotype);
0087   addParameter("CREATIONTAG", "string", _config.creationtag);
0088   addParameter("CREATIONSTAMP", "string", _config.creationstamp);
0089   addParameter("FORMATREVISION", "string", _config.formatrevision);
0090   addParameter("TARGETFIRMWARE", "string", _config.targetfirmware);
0091   addParameter("GENERALIZEDINDEX", "int", _config.generalizedindex);
0092   addParameter("CRATE", "int", _config.crate);
0093   addParameter("SLOT", "int", _config.slot);
0094 
0095   if (checksums_xml) {
0096     addParameter("CHECKSUM", "string", get_checksum(_config.lut));
0097   }
0098 
0099   if (_config.lut_type == 1) {  // linearizer LUT
0100     addParameter("IETA", "int", _config.ieta);
0101     addParameter("IPHI", "int", _config.iphi);
0102     addParameter("TOPBOTTOM", "int", _config.topbottom);
0103     addParameter("LUT_TYPE", "int", _config.lut_type);
0104     addParameter("FIBER", "int", _config.fiber);
0105     addParameter("FIBERCHAN", "int", _config.fiberchan);
0106     addParameter("DEPTH", "int", _config.depth);
0107     addData(to_string(_config.lut.size()), "hex", _config.lut);
0108   } else if (_config.lut_type == 2 || _config.lut_type == 4) {  // compression LUT or HE feature bit LUT
0109     addParameter("IETA", "int", _config.ieta);
0110     addParameter("IPHI", "int", _config.iphi);
0111     addParameter("TOPBOTTOM", "int", _config.topbottom);
0112     addParameter("LUT_TYPE", "int", _config.lut_type);
0113     addParameter("SLB", "int", _config.fiber);
0114     addParameter("SLBCHAN", "int", _config.fiberchan);
0115     addParameter("WEIGHT", "int", _config.weight);
0116     addData(to_string(_config.lut.size()), "hex", _config.lut);
0117   } else if (_config.lut_type == 5) {  // channel masks
0118     addParameter("MASK_TYPE", "string", "TRIGGERCHANMASK");
0119     addData(to_string(_config.mask.size()), "hex", _config.mask);
0120   } else if (_config.lut_type == 6) {  // adc threshold for tdc mask
0121     addParameter("THRESH_TYPE", "string", "TRIGINTIME");
0122     addData(to_string(_config.mask.size()), "hex", _config.mask);
0123   } else if (_config.lut_type == 7) {  // tdc mask
0124     addParameter("TDCMAP_TYPE", "string", "TRIGINTIME");
0125     addData(to_string(_config.mask.size()), "hex", _config.mask);
0126   } else {
0127     edm::LogError("LutXml") << "Unknown LUT type...produced XML will be incorrect";
0128   }
0129 
0130   if (checksums_xml) {
0131     add_checksum(checksums_xml->getDocument(), _config);
0132   }
0133 }
0134 
0135 template <typename T>
0136 DOMElement *LutXml::addData(std::string _elements, std::string _encoding, const T &_lut) {
0137   DOMElement *child = document->createElement(XMLProcessor::_toXMLCh("Data"));
0138   child->setAttribute(XMLProcessor::_toXMLCh("elements"), XMLProcessor::_toXMLCh(_elements));
0139   child->setAttribute(XMLProcessor::_toXMLCh("encoding"), XMLProcessor::_toXMLCh(_encoding));
0140 
0141   std::stringstream buf;
0142 
0143   for (const auto &iter : _lut) {
0144     char buf2[16];
0145     sprintf(buf2, "%lx", uint64_t(iter));
0146     buf << buf2 << " ";
0147   }
0148 
0149   std::string _value = buf.str();
0150 
0151   DOMText *data_value = document->createTextNode(XMLProcessor::_toXMLCh(_value));
0152   child->appendChild(data_value);
0153 
0154   brickElem->appendChild(child);
0155 
0156   return child;
0157 }
0158 
0159 DOMElement *LutXml::add_checksum(DOMDocument *parent, Config &config) {
0160   DOMElement *child = parent->createElement(XMLProcessor::_toXMLCh("Data"));
0161   child->setAttribute(XMLProcessor::_toXMLCh("crate"), XMLProcessor::_toXMLCh(config.crate));
0162   child->setAttribute(XMLProcessor::_toXMLCh("slot"), XMLProcessor::_toXMLCh(config.slot));
0163   child->setAttribute(XMLProcessor::_toXMLCh("fpga"), XMLProcessor::_toXMLCh(config.topbottom));
0164   child->setAttribute(XMLProcessor::_toXMLCh("fiber"), XMLProcessor::_toXMLCh(config.fiber));
0165   child->setAttribute(XMLProcessor::_toXMLCh("fiberchan"), XMLProcessor::_toXMLCh(config.fiberchan));
0166   child->setAttribute(XMLProcessor::_toXMLCh("luttype"), XMLProcessor::_toXMLCh(config.lut_type));
0167   child->setAttribute(XMLProcessor::_toXMLCh("elements"), XMLProcessor::_toXMLCh("1"));
0168   child->setAttribute(XMLProcessor::_toXMLCh("encoding"), XMLProcessor::_toXMLCh("hex"));
0169   DOMText *checksum_value = parent->createTextNode(XMLProcessor::_toXMLCh(get_checksum(config.lut)));
0170   child->appendChild(checksum_value);
0171 
0172   parent->getDocumentElement()->appendChild(child);
0173 
0174   return child;
0175 }
0176 
0177 DOMElement *LutXml::addParameter(std::string _name, std::string _type, std::string _value) {
0178   DOMElement *child = document->createElement(XMLProcessor::_toXMLCh("Parameter"));
0179   child->setAttribute(XMLProcessor::_toXMLCh("name"), XMLProcessor::_toXMLCh(_name));
0180   child->setAttribute(XMLProcessor::_toXMLCh("type"), XMLProcessor::_toXMLCh(_type));
0181   DOMText *parameter_value = document->createTextNode(XMLProcessor::_toXMLCh(_value));
0182   child->appendChild(parameter_value);
0183 
0184   brickElem->appendChild(child);
0185 
0186   return child;
0187 }
0188 
0189 DOMElement *LutXml::addParameter(std::string _name, std::string _type, int _value) {
0190   char buf[128];
0191   sprintf(buf, "%d", _value);
0192   std::string str_value = buf;
0193   return addParameter(_name, _type, str_value);
0194 }
0195 
0196 std::string &LutXml::getCurrentBrick(void) { return getString(brickElem); }
0197 
0198 // do MD5 checksum
0199 std::string LutXml::get_checksum(std::vector<unsigned int> &lut) {
0200   std::stringstream result;
0201   md5_state_t md5er;
0202   md5_byte_t digest[16];
0203   md5_init(&md5er);
0204   // linearizer LUT:
0205   if (lut.size() == 128) {
0206     unsigned char tool[2];
0207     for (int i = 0; i < 128; i++) {
0208       tool[0] = lut[i] & 0xFF;
0209       tool[1] = (lut[i] >> 8) & 0xFF;
0210       md5_append(&md5er, tool, 2);
0211     }
0212   } else if (lut.size() == 256) {
0213     unsigned char tool[2];
0214     for (int i = 0; i < 256; i++) {
0215       tool[0] = lut[i] & 0xFF;
0216       tool[1] = (lut[i] >> 8) & 0xFF;
0217       md5_append(&md5er, tool, 2);
0218     }
0219   }
0220   // compression LUT:
0221   else if (lut.size() == 1024) {
0222     unsigned char tool;
0223     for (int i = 0; i < 1024; i++) {
0224       tool = lut[i] & 0xFF;
0225       md5_append(&md5er, &tool, 1);
0226     }
0227   } else if (lut.size() == 2048) {
0228     unsigned char tool;
0229     for (int i = 0; i < 2048; i++) {
0230       tool = lut[i] & 0xFF;
0231       md5_append(&md5er, &tool, 1);
0232     }
0233   }
0234   // HE fine grain LUT
0235   else if (lut.size() == 4096) {
0236     unsigned char tool;
0237     for (int i = 0; i < 4096; i++) {
0238       tool = lut[i] & 0xFF;
0239       md5_append(&md5er, &tool, 1);
0240     }
0241   } else {
0242     edm::LogError("LutXml") << "Irregular LUT size, " << lut.size()
0243                             << " , do not know how to compute checksum, exiting...";
0244     exit(-1);
0245   }
0246   md5_finish(&md5er, digest);
0247   for (int i = 0; i < 16; i++)
0248     result << std::hex << (((int)(digest[i])) & 0xFF);
0249 
0250   return result.str();
0251 }
0252 
0253 int LutXml::test_access(std::string filename) {
0254   edm::LogInfo("LutXml") << "Created map size: " << lut_map.size();
0255 
0256   struct timeval _t;
0257   gettimeofday(&_t, nullptr);
0258   double _time = (double)(_t.tv_sec) + (double)(_t.tv_usec) / 1000000.0;
0259 
0260   HcalEmap _emap("./backup/official_emap_v6.04_080905.txt");
0261   std::vector<HcalEmap::HcalEmapRow> &_map = _emap.get_map();
0262   edm::LogInfo("LutXml") << "HcalEmap contains " << _map.size() << " entries";
0263 
0264   int _counter = 0;
0265   for (std::vector<HcalEmap::HcalEmapRow>::const_iterator row = _map.begin(); row != _map.end(); ++row) {
0266     if (row->subdet == "HB") {
0267       HcalDetId det_id(HcalBarrel, row->ieta, row->iphi, row->idepth);
0268       uint32_t raw_id = det_id.rawId();
0269       std::vector<unsigned int> *l = getLutFast(raw_id);
0270       if (l)
0271         _counter++;
0272     }
0273     if (row->subdet == "HE") {
0274       HcalDetId det_id(HcalEndcap, row->ieta, row->iphi, row->idepth);
0275       uint32_t raw_id = det_id.rawId();
0276       std::vector<unsigned int> *l = getLutFast(raw_id);
0277       if (l)
0278         _counter++;
0279     }
0280     if (row->subdet == "HF") {
0281       HcalDetId det_id(HcalForward, row->ieta, row->iphi, row->idepth);
0282       uint32_t raw_id = det_id.rawId();
0283       std::vector<unsigned int> *l = getLutFast(raw_id);
0284       if (l)
0285         _counter++;
0286     }
0287     if (row->subdet == "HO") {
0288       HcalDetId det_id(HcalOuter, row->ieta, row->iphi, row->idepth);
0289       uint32_t raw_id = det_id.rawId();
0290       std::vector<unsigned int> *l = getLutFast(raw_id);
0291       if (l)
0292         _counter++;
0293     }
0294   }
0295   gettimeofday(&_t, nullptr);
0296   edm::LogInfo("LutXml") << "access to " << _counter
0297                          << " HCAL channels took: " << (double)(_t.tv_sec) + (double)(_t.tv_usec) / 1000000.0 - _time
0298                          << "sec";
0299 
0300   return 0;
0301 }
0302 
0303 HcalSubdetector LutXml::subdet_from_crate(int crate_, int slot, int fiber) {
0304   // HBHE: 0,1,4,5,10,11,14,15,17 (+20)
0305   // HF: 2,9,12  (+20)
0306   // HO: 3,6,7,13,18  (+20)
0307   int crate = crate_ < 20 ? crate_ : crate_ - 20;
0308   if (crate == 2 || crate == 9 || crate == 12)
0309     return HcalForward;
0310   else if (crate == 3 || crate == 6 || crate == 7 || crate == 13 || crate == 18)
0311     return HcalOuter;
0312   else if (crate == 0 || crate == 1 || crate == 4 || crate == 5 || crate == 10 || crate == 11 || crate == 14 ||
0313            crate == 15 || crate == 17) {
0314     if (slot % 3 == 1)
0315       return HcalBarrel;
0316     else if (slot % 3 == 0)
0317       return HcalEndcap;
0318     else if (fiber < 12)
0319       return HcalBarrel;
0320     else
0321       return HcalEndcap;
0322   }
0323   edm::LogWarning("LutXml::subdet_from_crate") << "crate " << crate_ << " is not accounted for";
0324   return HcalEmpty;
0325 }
0326 
0327 int LutXml::a_to_i(char *inbuf) {
0328   int result;
0329   sscanf(inbuf, "%d", &result);
0330   return result;
0331 }
0332 
0333 // organize all LUTs in XML into a map for fast access
0334 //
0335 // FIXME: uses hardcoded CRATE-to-subdetector mapping
0336 // FIXME: it would be better to use some official map
0337 //
0338 int LutXml::create_lut_map(void) {
0339   //delete lut_map;
0340   lut_map.clear();
0341   //lut_map = new std::map<uint32_t,std::vector<unsigned int> >();
0342 
0343   if (document) {
0344     //DOMElement * rootElem =
0345     DOMNodeList *brick_list = document->getDocumentElement()->getElementsByTagName(brick);
0346     int n_of_bricks = brick_list->getLength();
0347     for (int i = 0; i != n_of_bricks; i++) {
0348       DOMElement *aBrick = (DOMElement *)(brick_list->item(i));
0349       DOMNodeList *par_list = aBrick->getElementsByTagName(XMLString::transcode("Parameter"));
0350       int n_of_par = par_list->getLength();
0351       int ieta = -99;
0352       int iphi = -99;
0353       int depth = -99;
0354       int crate = -99;
0355       int slot = -99;
0356       int fiber = -99;
0357       int lut_type = -99;
0358       int slb = -99;
0359       HcalSubdetector subdet;
0360       for (int j = 0; j != n_of_par; j++) {
0361         DOMElement *aPar = (DOMElement *)(par_list->item(j));
0362         char *aName = XMLString::transcode(aPar->getAttribute(XMLProcessor::_toXMLCh("name")));
0363         if (strcmp(aName, "IETA") == 0)
0364           ieta = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
0365         if (strcmp(aName, "IPHI") == 0)
0366           iphi = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
0367         if (strcmp(aName, "DEPTH") == 0)
0368           depth = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
0369         if (strcmp(aName, "CRATE") == 0)
0370           crate = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
0371         if (strcmp(aName, "SLOT") == 0)
0372           slot = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
0373         if (strcmp(aName, "FIBER") == 0)
0374           fiber = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
0375         if (strcmp(aName, "LUT_TYPE") == 0)
0376           lut_type = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
0377         if (strcmp(aName, "SLB") == 0)
0378           slb = a_to_i(XMLString::transcode(aPar->getFirstChild()->getNodeValue()));
0379       }
0380       subdet = subdet_from_crate(crate, slot, fiber);
0381       DOMElement *_data = (DOMElement *)(aBrick->getElementsByTagName(XMLString::transcode("Data"))->item(0));
0382       char *_str = XMLString::transcode(_data->getFirstChild()->getNodeValue());
0383 
0384       // get the LUT vector
0385       int _string_length = strlen(_str);
0386       std::vector<unsigned int> _lut;
0387       unsigned int _base = 16;
0388       unsigned int _item = 0;
0389       for (int i = 0; i != _string_length; i++) {
0390         bool _range = false;
0391         char ch_cur = _str[i];
0392         if (_base == 16)
0393           _range = (ch_cur >= '0' and ch_cur <= '9') || (ch_cur >= 'a' and ch_cur <= 'f') ||
0394                    (ch_cur >= 'A' and ch_cur <= 'F');
0395         else if (_base == 10)
0396           _range = (ch_cur >= '0' and ch_cur <= '9');
0397         if (_range) {
0398           if (ch_cur >= 'a' and ch_cur <= 'f')
0399             ch_cur += 10 - 'a';
0400           else if (ch_cur >= 'A' and ch_cur <= 'F')
0401             ch_cur += 10 - 'A';
0402           else if (ch_cur >= '0' and ch_cur <= '9')
0403             ch_cur += -'0';
0404           _item = _item * _base;
0405           _item += ch_cur;
0406           bool last_digit = false;
0407           if ((i + 1) == _string_length)
0408             last_digit = true;
0409           else {
0410             char ch_next = _str[i + 1];
0411             bool _range_next = false;
0412             if (_base == 16)
0413               _range_next = (ch_next >= '0' and ch_next <= '9') || (ch_next >= 'a' and ch_next <= 'f') ||
0414                             (ch_next >= 'A' and ch_next <= 'F');
0415             else if (_base == 10)
0416               _range_next = (ch_next >= '0' and ch_next <= '9');
0417             if (!_range_next)
0418               last_digit = true;
0419           }
0420           if (last_digit) {
0421             _lut.push_back(_item);
0422             _item = 0;
0423           }
0424         }
0425       }
0426       // filling the map
0427       uint32_t _key = 0;
0428       if (lut_type == 1) {
0429         HcalDetId _id(subdet, ieta, iphi, depth);
0430         _key = _id.rawId();
0431       } else if (lut_type == 2) {
0432         int version = (abs(ieta) > 29 && slb != 12 && crate > 20) ? 1 : 0;
0433         HcalTrigTowerDetId _id(ieta, iphi, 10 * version);
0434         _key = _id.rawId();
0435       } else
0436         continue;
0437       lut_map.insert(std::pair<uint32_t, std::vector<unsigned int> >(_key, _lut));
0438     }
0439   } else {
0440     edm::LogError("LutXml") << "XML file with LUTs is not loaded, cannot create map!";
0441   }
0442 
0443   return 0;
0444 }
0445 
0446 LutXml::const_iterator LutXml::begin() const { return lut_map.begin(); }
0447 
0448 LutXml::const_iterator LutXml::end() const { return lut_map.end(); }
0449 
0450 LutXml::const_iterator LutXml::find(uint32_t id) const { return lut_map.find(id); }