Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:00:07

0001 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseImplXMLFile.hh"
0002 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationItemNotFoundException.hh"
0003 #include <zlib.h>
0004 
0005 #ifdef HAVE_XDAQ
0006 #include <toolbox/string.h>
0007 #else
0008 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h"  // Replaces toolbox::toString
0009 #endif
0010 
0011 DECLARE_PLUGGABLE(hcal::ConfigurationDatabaseImpl, ConfigurationDatabaseImplXMLFile)
0012 
0013 ConfigurationDatabaseImplXMLFile::ConfigurationDatabaseImplXMLFile() {}
0014 ConfigurationDatabaseImplXMLFile::~ConfigurationDatabaseImplXMLFile() {}
0015 bool ConfigurationDatabaseImplXMLFile::canHandleMethod(const std::string& method) const { return method == "xmlfile"; }
0016 
0017 void ConfigurationDatabaseImplXMLFile::connect(const std::string& accessor) noexcept(false) {
0018   // open file and copy into a string
0019   std::string theFile = accessor;
0020   std::string::size_type i = theFile.find("://");
0021   if (i != std::string::npos)
0022     theFile.erase(0, i + 2);  // remove up to the ://
0023   gzFile f = gzopen(theFile.c_str(), "rb");
0024 
0025   if (f == nullptr) {
0026     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException, "Unable to open file " + theFile);
0027   }
0028   int c;
0029   while ((c = gzgetc(f)) != EOF)
0030     m_buffer += (unsigned char)c;
0031   gzclose(f);
0032 
0033   // iterate through the string and extract the CFGBrick boundaries
0034   std::string::size_type j = 0;
0035   while ((i = m_buffer.find("<CFGBrick>", j)) != std::string::npos) {
0036     j = m_buffer.find("</CFGBrick>", i) + strlen("</CFGBrick>");
0037     if (j == std::string::npos)
0038       break;
0039     // extract all parameters
0040     std::map<std::string, std::string> params = extractParams(i, j);
0041     std::string key = createKey(params);
0042     //  printf(" --> %s\n",key.c_str());
0043     std::pair<int, int> ptrs(i, j);
0044     m_lookup.insert(std::pair<std::string, std::pair<int, int> >(key, ptrs));
0045   }
0046 }
0047 
0048 std::string ConfigurationDatabaseImplXMLFile::createKey(const std::map<std::string, std::string>& params) {
0049   std::string retval;
0050   if (params.find("PATTERN_SPEC_NAME") != params.end()) {  // HTR pattern
0051     retval = params.find("TAG")->second + ":" + params.find("CRATE")->second + ":" + params.find("SLOT")->second + ":" +
0052              params.find("TOPBOTTOM")->second + ":" + params.find("FIBER")->second;
0053   } else if (params.find("LUT_TYPE") != params.end()) {  // HTR LUT
0054     retval = params.find("TAG")->second + ":" + params.find("CRATE")->second + ":" + params.find("SLOT")->second + ":" +
0055              params.find("TOPBOTTOM")->second + ":" + params.find("LUT_TYPE")->second;
0056     if (params.find("FIBER") != params.end())
0057       retval += ":" + params.find("FIBER")->second + ":" + params.find("FIBERCHAN")->second;
0058     if (params.find("SLB") != params.end())
0059       retval += ":" + params.find("SLB")->second + ":" + params.find("SLBCHAN")->second;
0060   } else if (params.find("BOARD") != params.end()) {  // firmware!
0061     int ver = strtol(params.find("VERSION")->second.c_str(), nullptr, 0);
0062     retval = params.find("BOARD")->second + ":" + ::toolbox::toString("%x", ver);
0063   } else if (params.find("ZS_TYPE") != params.end()) {  // ZS thresholds
0064     retval = params.find("TAG")->second + ":" + params.find("CRATE")->second + ":" + params.find("SLOT")->second + ":" +
0065              params.find("TOPBOTTOM")->second;
0066   } else
0067     retval = "WHAT";
0068   return retval;
0069 }
0070 
0071 std::map<std::string, std::string> ConfigurationDatabaseImplXMLFile::extractParams(int beg, int end) {
0072   std::map<std::string, std::string> pval;
0073   std::string::size_type l = beg, i, j;
0074   std::string name, val;
0075 
0076   while ((i = m_buffer.find("<Parameter", l)) != std::string::npos && i < (unsigned int)end) {
0077     j = m_buffer.find("name=", i);
0078     char separator = m_buffer[j + 5];
0079     i = m_buffer.find(separator, j + 6);
0080     name = m_buffer.substr(j + 6, i - (j + 6));
0081     if (name == "CREATIONTAG")
0082       name = "TAG";  // RENAME!
0083     j = m_buffer.find('>', j);
0084     i = m_buffer.find("</", j);
0085     val = m_buffer.substr(j + 1, i - j - 1);
0086     pval.insert(std::pair<std::string, std::string>(name, val));
0087     l = j;
0088   }
0089 
0090   return pval;
0091 }
0092 
0093 void ConfigurationDatabaseImplXMLFile::disconnect() {
0094   m_lookup.clear();
0095   m_buffer.clear();
0096 }
0097 
0098 std::map<std::string, std::string> ConfigurationDatabaseImplXMLFile::parseWhere(const std::string& where) {
0099   std::string::size_type i, j = 0, k, k2;
0100   std::map<std::string, std::string> itis;
0101 
0102   while ((i = where.find('=', j)) != std::string::npos) {
0103     k = where.rfind(' ', i);
0104     k2 = where.rfind('(', i);
0105     if (k2 != std::string::npos && k2 > k)
0106       k = k2;
0107     if (k == std::string::npos)
0108       k = 0;
0109     else
0110       k++;
0111     std::string key = where.substr(k, i - k), value;
0112     if (where[i + 1] == '\'' || where[i + 1] == '\"') {
0113       j = where.find(where[i + 1], i + 2);
0114       value = where.substr(i + 2, j - i - 2);
0115     } else {
0116       j = where.find(' ', i);
0117       k = where.find(')', i);
0118       if (k != std::string::npos && k < j)
0119         j = k;
0120       value = where.substr(i + 1, j - i - 1);
0121     }
0122     itis.insert(std::pair<std::string, std::string>(key, value));
0123   }
0124   return itis;
0125 }
0126 
0127 /*
0128 hcal::ConfigurationDatabaseIterator* ConfigurationDatabaseImplXMLFile::query(const std::string& sector, const std::string& draftSelect, const std::string& draftWhere) noexcept(false) { 
0129 
0130   std::map<std::string,std::string> whereMap=parseWhere(draftWhere);
0131   if (sector=="PATTERN") whereMap["PATTERN_SPEC_NAME"]=whereMap["TAG"];
0132   std::string lookup=createKey(whereMap);
0133   //  printf("'%s'\n",lookup.c_str());
0134   std::map<std::string, std::pair<int,int> >::iterator j=m_lookup.find(lookup);
0135   if (j==m_lookup.end()) return new ConfigurationDatabaseImplXMLFileIterator("");
0136   std::string data="<?xml version='1.0'?>\n";
0137   data+=m_buffer.substr(j->second.first,j->second.second-j->second.first);
0138   return new ConfigurationDatabaseImplXMLFileIterator(data);
0139 }
0140 */
0141 
0142 unsigned int ConfigurationDatabaseImplXMLFile::getFirmwareChecksum(const std::string& board,
0143                                                                    unsigned int version) noexcept(false) {
0144   XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException, "Unsupported");
0145 }
0146 
0147 void ConfigurationDatabaseImplXMLFile::getFirmwareMCS(const std::string& board,
0148                                                       unsigned int version,
0149                                                       std::vector<std::string>& mcsLines) noexcept(false) {
0150   std::string key = ::toolbox::toString("%s:%x", board.c_str(), version);
0151 
0152   std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
0153   if (j == m_lookup.end()) {
0154     XCEPT_RAISE(hcal::exception::ConfigurationItemNotFoundException, "");
0155   }
0156   std::string data = "<?xml version='1.0'?>\n";
0157   data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
0158 
0159   std::map<std::string, std::string> params;
0160   std::string encoding;
0161   m_parser.parse(data, params, mcsLines, encoding);
0162 }
0163 
0164 void ConfigurationDatabaseImplXMLFile::getLUTChecksums(
0165     const std::string& tag,
0166     std::map<hcal::ConfigurationDatabase::LUTId, hcal::ConfigurationDatabase::MD5Fingerprint>&
0167         checksums) noexcept(false) {
0168   XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException, "Unsupported");
0169 }
0170 
0171 void ConfigurationDatabaseImplXMLFile::getLUTs(
0172     const std::string& tag,
0173     int crate,
0174     int slot,
0175     std::map<hcal::ConfigurationDatabase::LUTId, hcal::ConfigurationDatabase::LUT>& LUTs) noexcept(false) {
0176   LUTs.clear();
0177 
0178   for (int tb = 0; tb <= 1; tb++)
0179     for (int fiber = 1; fiber <= 8; fiber++)
0180       for (int fiberChan = 0; fiberChan <= 2; fiberChan++) {
0181         int lut_type = 1;
0182 
0183         std::string key =
0184             toolbox::toString("%s:%d:%d:%d:%d:%d:%d", tag.c_str(), crate, slot, tb, lut_type, fiber, fiberChan);
0185         std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
0186         if (j == m_lookup.end())
0187           continue;
0188         std::string data = "<?xml version='1.0'?>\n";
0189         data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
0190 
0191         std::map<std::string, std::string> params;
0192         std::vector<std::string> values;
0193         std::string encoding;
0194         m_parser.parse(data, params, values, encoding);
0195 
0196         hcal::ConfigurationDatabase::LUTId id(crate,
0197                                               slot,
0198                                               (hcal::ConfigurationDatabase::FPGASelection)tb,
0199                                               fiber,
0200                                               fiberChan,
0201                                               (hcal::ConfigurationDatabase::LUTType)lut_type);
0202         hcal::ConfigurationDatabase::LUT& lut = LUTs[id];
0203         lut.reserve(values.size());
0204 
0205         int strtol_base = 0;
0206         if (encoding == "hex")
0207           strtol_base = 16;
0208         else if (encoding == "dec")
0209           strtol_base = 10;
0210 
0211         // convert the data
0212         for (unsigned int j = 0; j < values.size(); j++)
0213           lut.push_back(strtol(values[j].c_str(), nullptr, strtol_base));
0214       }
0215   for (int tb = 0; tb <= 1; tb++)
0216     for (int slb = 1; slb <= 6; slb++)
0217       for (int slbChan = 0; slbChan <= 3; slbChan++) {
0218         int lut_type = 2;
0219 
0220         std::string key =
0221             toolbox::toString("%s:%d:%d:%d:%d:%d:%d", tag.c_str(), crate, slot, tb, lut_type, slb, slbChan);
0222 
0223         std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
0224         if (j == m_lookup.end())
0225           continue;
0226         std::string data = "<?xml version='1.0'?>\n";
0227         data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
0228 
0229         std::map<std::string, std::string> params;
0230         std::vector<std::string> values;
0231         std::string encoding;
0232         m_parser.parse(data, params, values, encoding);
0233 
0234         hcal::ConfigurationDatabase::LUTId id(crate,
0235                                               slot,
0236                                               (hcal::ConfigurationDatabase::FPGASelection)tb,
0237                                               slb,
0238                                               slbChan,
0239                                               (hcal::ConfigurationDatabase::LUTType)lut_type);
0240         hcal::ConfigurationDatabase::LUT& lut = LUTs[id];
0241         lut.reserve(values.size());
0242 
0243         int strtol_base = 0;
0244         if (encoding == "hex")
0245           strtol_base = 16;
0246         else if (encoding == "dec")
0247           strtol_base = 10;
0248 
0249         // convert the data
0250         for (unsigned int j = 0; j < values.size(); j++)
0251           lut.push_back(strtol(values[j].c_str(), nullptr, strtol_base));
0252       }
0253 }
0254 
0255 void ConfigurationDatabaseImplXMLFile::getZSThresholds(
0256     const std::string& tag,
0257     int crate,
0258     int slot,
0259     std::map<hcal::ConfigurationDatabase::ZSChannelId, int>& thresholds) noexcept(false) {
0260   thresholds.clear();
0261   for (int tb = 0; tb <= 1; tb++) {
0262     std::string key = toolbox::toString("%s:%d:%d:%d", tag.c_str(), crate, slot, tb);
0263     std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
0264     if (j == m_lookup.end())
0265       continue;
0266     std::string data = "<?xml version='1.0'?>\n";
0267     data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
0268 
0269     std::map<std::string, std::string> params;
0270     std::vector<std::string> values;
0271     std::string encoding;
0272     m_parser.parse(data, params, values, encoding);
0273 
0274     if (values.size() != 24) {
0275       XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
0276                   ::toolbox::toString("Must have 24 items in ZS list.  Saw %d for %s", values.size(), key.c_str()));
0277     }
0278     for (int fiber = 1; fiber <= 8; fiber++)
0279       for (int fc = 0; fc < 3; fc++) {
0280         hcal::ConfigurationDatabase::ZSChannelId id(
0281             crate, slot, (hcal::ConfigurationDatabase::FPGASelection)tb, fiber, fc);
0282 
0283         int strtol_base = 0;
0284         if (encoding == "hex")
0285           strtol_base = 16;
0286         else if (encoding == "dec")
0287           strtol_base = 10;
0288 
0289         thresholds[id] = strtol(values[(fiber - 1) * 3 + fc].c_str(), nullptr, strtol_base);
0290       }
0291   }
0292 }
0293 
0294 void ConfigurationDatabaseImplXMLFile::getPatterns(
0295     const std::string& tag,
0296     int crate,
0297     int slot,
0298     std::map<hcal::ConfigurationDatabase::PatternId, hcal::ConfigurationDatabase::HTRPattern>& patterns) noexcept(false) {
0299   patterns.clear();
0300   for (int tb = 0; tb <= 1; tb++)
0301     for (int fiber = 1; fiber <= 8; fiber++) {
0302       std::string key = toolbox::toString("%s:%d:%d:%d:%d", tag.c_str(), crate, slot, tb, fiber);
0303       std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
0304       if (j == m_lookup.end())
0305         continue;
0306       std::string data = "<?xml version='1.0'?>\n";
0307       data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
0308 
0309       std::map<std::string, std::string> params;
0310       std::vector<std::string> values;
0311       std::string encoding;
0312       m_parser.parse(data, params, values, encoding);
0313 
0314       hcal::ConfigurationDatabase::PatternId id(crate, slot, (hcal::ConfigurationDatabase::FPGASelection)tb, fiber);
0315       hcal::ConfigurationDatabase::HTRPattern& lut = patterns[id];
0316       lut.reserve(values.size());
0317 
0318       int strtol_base = 0;
0319       if (encoding == "hex")
0320         strtol_base = 16;
0321       else if (encoding == "dec")
0322         strtol_base = 10;
0323 
0324       // convert the data
0325       for (unsigned int j = 0; j < values.size(); j++)
0326         lut.push_back(strtol(values[j].c_str(), nullptr, strtol_base));
0327     }
0328 }
0329 
0330 /*
0331 // added by Gena Kukartsev
0332 oracle::occi::Connection * ConfigurationDatabaseImplXMLFile::getConnection( void ){
0333   return NULL;
0334 }
0335 
0336 oracle::occi::Environment * ConfigurationDatabaseImplXMLFile::getEnvironment( void ){
0337   return NULL;
0338 }
0339 */