CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ConfigurationDatabaseImplXMLFile.cc
Go to the documentation of this file.
1 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseImplXMLFile.hh"
2 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationItemNotFoundException.hh"
3 #include <zlib.h>
4 
5 #ifdef HAVE_XDAQ
6 #include <toolbox/string.h>
7 #else
8 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h" // Replaces toolbox::toString
9 #endif
10 
11 DECLARE_PLUGGABLE(hcal::ConfigurationDatabaseImpl, ConfigurationDatabaseImplXMLFile)
12 
13 ConfigurationDatabaseImplXMLFile::ConfigurationDatabaseImplXMLFile() {}
14 ConfigurationDatabaseImplXMLFile::~ConfigurationDatabaseImplXMLFile() {}
15 bool ConfigurationDatabaseImplXMLFile::canHandleMethod(const std::string& method) const { return method == "xmlfile"; }
16 
17 void ConfigurationDatabaseImplXMLFile::connect(const std::string& accessor) noexcept(false) {
18  // open file and copy into a string
19  std::string theFile = accessor;
20  std::string::size_type i = theFile.find("://");
21  if (i != std::string::npos)
22  theFile.erase(0, i + 2); // remove up to the ://
23  gzFile f = gzopen(theFile.c_str(), "rb");
24 
25  if (f == nullptr) {
26  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException, "Unable to open file " + theFile);
27  }
28  int c;
29  while ((c = gzgetc(f)) != EOF)
30  m_buffer += (unsigned char)c;
31  gzclose(f);
32 
33  // iterate through the string and extract the CFGBrick boundaries
35  while ((i = m_buffer.find("<CFGBrick>", j)) != std::string::npos) {
36  j = m_buffer.find("</CFGBrick>", i) + strlen("</CFGBrick>");
37  if (j == std::string::npos)
38  break;
39  // extract all parameters
40  std::map<std::string, std::string> params = extractParams(i, j);
41  std::string key = createKey(params);
42  // printf(" --> %s\n",key.c_str());
43  std::pair<int, int> ptrs(i, j);
44  m_lookup.insert(std::pair<std::string, std::pair<int, int> >(key, ptrs));
45  }
46 }
47 
48 std::string ConfigurationDatabaseImplXMLFile::createKey(const std::map<std::string, std::string>& params) {
49  std::string retval;
50  if (params.find("PATTERN_SPEC_NAME") != params.end()) { // HTR pattern
51  retval = params.find("TAG")->second + ":" + params.find("CRATE")->second + ":" + params.find("SLOT")->second + ":" +
52  params.find("TOPBOTTOM")->second + ":" + params.find("FIBER")->second;
53  } else if (params.find("LUT_TYPE") != params.end()) { // HTR LUT
54  retval = params.find("TAG")->second + ":" + params.find("CRATE")->second + ":" + params.find("SLOT")->second + ":" +
55  params.find("TOPBOTTOM")->second + ":" + params.find("LUT_TYPE")->second;
56  if (params.find("FIBER") != params.end())
57  retval += ":" + params.find("FIBER")->second + ":" + params.find("FIBERCHAN")->second;
58  if (params.find("SLB") != params.end())
59  retval += ":" + params.find("SLB")->second + ":" + params.find("SLBCHAN")->second;
60  } else if (params.find("BOARD") != params.end()) { // firmware!
61  int ver = strtol(params.find("VERSION")->second.c_str(), nullptr, 0);
62  retval = params.find("BOARD")->second + ":" + ::toolbox::toString("%x", ver);
63  } else if (params.find("ZS_TYPE") != params.end()) { // ZS thresholds
64  retval = params.find("TAG")->second + ":" + params.find("CRATE")->second + ":" + params.find("SLOT")->second + ":" +
65  params.find("TOPBOTTOM")->second;
66  } else
67  retval = "WHAT";
68  return retval;
69 }
70 
71 std::map<std::string, std::string> ConfigurationDatabaseImplXMLFile::extractParams(int beg, int end) {
72  std::map<std::string, std::string> pval;
73  std::string::size_type l = beg, i, j;
75 
76  while ((i = m_buffer.find("<Parameter", l)) != std::string::npos && i < (unsigned int)end) {
77  j = m_buffer.find("name=", i);
78  char separator = m_buffer[j + 5];
79  i = m_buffer.find(separator, j + 6);
80  name = m_buffer.substr(j + 6, i - (j + 6));
81  if (name == "CREATIONTAG")
82  name = "TAG"; // RENAME!
83  j = m_buffer.find('>', j);
84  i = m_buffer.find("</", j);
85  val = m_buffer.substr(j + 1, i - j - 1);
86  pval.insert(std::pair<std::string, std::string>(name, val));
87  l = j;
88  }
89 
90  return pval;
91 }
92 
93 void ConfigurationDatabaseImplXMLFile::disconnect() {
94  m_lookup.clear();
95  m_buffer.clear();
96 }
97 
98 std::map<std::string, std::string> ConfigurationDatabaseImplXMLFile::parseWhere(const std::string& where) {
99  std::string::size_type i, j = 0, k, k2;
100  std::map<std::string, std::string> itis;
101 
102  while ((i = where.find('=', j)) != std::string::npos) {
103  k = where.rfind(' ', i);
104  k2 = where.rfind('(', i);
105  if (k2 != std::string::npos && k2 > k)
106  k = k2;
107  if (k == std::string::npos)
108  k = 0;
109  else
110  k++;
111  std::string key = where.substr(k, i - k), value;
112  if (where[i + 1] == '\'' || where[i + 1] == '\"') {
113  j = where.find(where[i + 1], i + 2);
114  value = where.substr(i + 2, j - i - 2);
115  } else {
116  j = where.find(' ', i);
117  k = where.find(')', i);
118  if (k != std::string::npos && k < j)
119  j = k;
120  value = where.substr(i + 1, j - i - 1);
121  }
122  itis.insert(std::pair<std::string, std::string>(key, value));
123  }
124  return itis;
125 }
126 
127 /*
128 hcal::ConfigurationDatabaseIterator* ConfigurationDatabaseImplXMLFile::query(const std::string& sector, const std::string& draftSelect, const std::string& draftWhere) noexcept(false) {
129 
130  std::map<std::string,std::string> whereMap=parseWhere(draftWhere);
131  if (sector=="PATTERN") whereMap["PATTERN_SPEC_NAME"]=whereMap["TAG"];
132  std::string lookup=createKey(whereMap);
133  // printf("'%s'\n",lookup.c_str());
134  std::map<std::string, std::pair<int,int> >::iterator j=m_lookup.find(lookup);
135  if (j==m_lookup.end()) return new ConfigurationDatabaseImplXMLFileIterator("");
136  std::string data="<?xml version='1.0'?>\n";
137  data+=m_buffer.substr(j->second.first,j->second.second-j->second.first);
138  return new ConfigurationDatabaseImplXMLFileIterator(data);
139 }
140 */
141 
142 unsigned int ConfigurationDatabaseImplXMLFile::getFirmwareChecksum(const std::string& board,
143  unsigned int version) noexcept(false) {
144  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException, "Unsupported");
145 }
146 
147 void ConfigurationDatabaseImplXMLFile::getFirmwareMCS(const std::string& board,
148  unsigned int version,
149  std::vector<std::string>& mcsLines) noexcept(false) {
150  std::string key = ::toolbox::toString("%s:%x", board.c_str(), version);
151 
152  std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
153  if (j == m_lookup.end()) {
154  XCEPT_RAISE(hcal::exception::ConfigurationItemNotFoundException, "");
155  }
156  std::string data = "<?xml version='1.0'?>\n";
157  data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
158 
159  std::map<std::string, std::string> params;
160  std::string encoding;
161  m_parser.parse(data, params, mcsLines, encoding);
162 }
163 
164 void ConfigurationDatabaseImplXMLFile::getLUTChecksums(
165  const std::string& tag,
166  std::map<hcal::ConfigurationDatabase::LUTId, hcal::ConfigurationDatabase::MD5Fingerprint>&
167  checksums) noexcept(false) {
168  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException, "Unsupported");
169 }
170 
171 void ConfigurationDatabaseImplXMLFile::getLUTs(
172  const std::string& tag,
173  int crate,
174  int slot,
175  std::map<hcal::ConfigurationDatabase::LUTId, hcal::ConfigurationDatabase::LUT>& LUTs) noexcept(false) {
176  LUTs.clear();
177 
178  for (int tb = 0; tb <= 1; tb++)
179  for (int fiber = 1; fiber <= 8; fiber++)
180  for (int fiberChan = 0; fiberChan <= 2; fiberChan++) {
181  int lut_type = 1;
182 
183  std::string key =
184  toolbox::toString("%s:%d:%d:%d:%d:%d:%d", tag.c_str(), crate, slot, tb, lut_type, fiber, fiberChan);
185  std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
186  if (j == m_lookup.end())
187  continue;
188  std::string data = "<?xml version='1.0'?>\n";
189  data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
190 
191  std::map<std::string, std::string> params;
192  std::vector<std::string> values;
193  std::string encoding;
194  m_parser.parse(data, params, values, encoding);
195 
196  hcal::ConfigurationDatabase::LUTId id(crate,
197  slot,
198  (hcal::ConfigurationDatabase::FPGASelection)tb,
199  fiber,
200  fiberChan,
201  (hcal::ConfigurationDatabase::LUTType)lut_type);
203  lut.reserve(values.size());
204 
205  int strtol_base = 0;
206  if (encoding == "hex")
207  strtol_base = 16;
208  else if (encoding == "dec")
209  strtol_base = 10;
210 
211  // convert the data
212  for (unsigned int j = 0; j < values.size(); j++)
213  lut.push_back(strtol(values[j].c_str(), nullptr, strtol_base));
214  }
215  for (int tb = 0; tb <= 1; tb++)
216  for (int slb = 1; slb <= 6; slb++)
217  for (int slbChan = 0; slbChan <= 3; slbChan++) {
218  int lut_type = 2;
219 
220  std::string key =
221  toolbox::toString("%s:%d:%d:%d:%d:%d:%d", tag.c_str(), crate, slot, tb, lut_type, slb, slbChan);
222 
223  std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
224  if (j == m_lookup.end())
225  continue;
226  std::string data = "<?xml version='1.0'?>\n";
227  data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
228 
229  std::map<std::string, std::string> params;
230  std::vector<std::string> values;
231  std::string encoding;
232  m_parser.parse(data, params, values, encoding);
233 
234  hcal::ConfigurationDatabase::LUTId id(crate,
235  slot,
236  (hcal::ConfigurationDatabase::FPGASelection)tb,
237  slb,
238  slbChan,
239  (hcal::ConfigurationDatabase::LUTType)lut_type);
241  lut.reserve(values.size());
242 
243  int strtol_base = 0;
244  if (encoding == "hex")
245  strtol_base = 16;
246  else if (encoding == "dec")
247  strtol_base = 10;
248 
249  // convert the data
250  for (unsigned int j = 0; j < values.size(); j++)
251  lut.push_back(strtol(values[j].c_str(), nullptr, strtol_base));
252  }
253 }
254 
255 void ConfigurationDatabaseImplXMLFile::getZSThresholds(
256  const std::string& tag,
257  int crate,
258  int slot,
259  std::map<hcal::ConfigurationDatabase::ZSChannelId, int>& thresholds) noexcept(false) {
260  thresholds.clear();
261  for (int tb = 0; tb <= 1; tb++) {
262  std::string key = toolbox::toString("%s:%d:%d:%d", tag.c_str(), crate, slot, tb);
263  std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
264  if (j == m_lookup.end())
265  continue;
266  std::string data = "<?xml version='1.0'?>\n";
267  data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
268 
269  std::map<std::string, std::string> params;
270  std::vector<std::string> values;
271  std::string encoding;
272  m_parser.parse(data, params, values, encoding);
273 
274  if (values.size() != 24) {
275  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
276  ::toolbox::toString("Must have 24 items in ZS list. Saw %d for %s", values.size(), key.c_str()));
277  }
278  for (int fiber = 1; fiber <= 8; fiber++)
279  for (int fc = 0; fc < 3; fc++) {
280  hcal::ConfigurationDatabase::ZSChannelId id(
281  crate, slot, (hcal::ConfigurationDatabase::FPGASelection)tb, fiber, fc);
282 
283  int strtol_base = 0;
284  if (encoding == "hex")
285  strtol_base = 16;
286  else if (encoding == "dec")
287  strtol_base = 10;
288 
289  thresholds[id] = strtol(values[(fiber - 1) * 3 + fc].c_str(), nullptr, strtol_base);
290  }
291  }
292 }
293 
294 void ConfigurationDatabaseImplXMLFile::getPatterns(
295  const std::string& tag,
296  int crate,
297  int slot,
298  std::map<hcal::ConfigurationDatabase::PatternId, hcal::ConfigurationDatabase::HTRPattern>& patterns) noexcept(false) {
299  patterns.clear();
300  for (int tb = 0; tb <= 1; tb++)
301  for (int fiber = 1; fiber <= 8; fiber++) {
302  std::string key = toolbox::toString("%s:%d:%d:%d:%d", tag.c_str(), crate, slot, tb, fiber);
303  std::map<std::string, std::pair<int, int> >::iterator j = m_lookup.find(key);
304  if (j == m_lookup.end())
305  continue;
306  std::string data = "<?xml version='1.0'?>\n";
307  data += m_buffer.substr(j->second.first, j->second.second - j->second.first);
308 
309  std::map<std::string, std::string> params;
310  std::vector<std::string> values;
311  std::string encoding;
312  m_parser.parse(data, params, values, encoding);
313 
314  hcal::ConfigurationDatabase::PatternId id(crate, slot, (hcal::ConfigurationDatabase::FPGASelection)tb, fiber);
315  hcal::ConfigurationDatabase::HTRPattern& lut = patterns[id];
316  lut.reserve(values.size());
317 
318  int strtol_base = 0;
319  if (encoding == "hex")
320  strtol_base = 16;
321  else if (encoding == "dec")
322  strtol_base = 10;
323 
324  // convert the data
325  for (unsigned int j = 0; j < values.size(); j++)
326  lut.push_back(strtol(values[j].c_str(), nullptr, strtol_base));
327  }
328 }
329 
330 /*
331 // added by Gena Kukartsev
332 oracle::occi::Connection * ConfigurationDatabaseImplXMLFile::getConnection( void ){
333  return NULL;
334 }
335 
336 oracle::occi::Environment * ConfigurationDatabaseImplXMLFile::getEnvironment( void ){
337  return NULL;
338 }
339 */
const edm::EventSetup & c
uint16_t *__restrict__ id
static int slb(const HcalTriggerPrimitiveSample &theSample)
static int slbChan(const HcalTriggerPrimitiveSample &theSample)
uint16_t size_type
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
tuple key
prepare the HTCondor submission files and eventually submit them
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
string end
Definition: dataset.py:937
static std::string const separator(":")
std::vector< unsigned short int > LUT
Definition: DTTracoLUTs.h:31