CMS 3D CMS Logo

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