00001 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseImplXMLFile.hh"
00002 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationItemNotFoundException.hh"
00003 #include <zlib.h>
00004 #include "toolbox/string.h"
00005
00006 DECLARE_PLUGGABLE(hcal::ConfigurationDatabaseImpl,ConfigurationDatabaseImplXMLFile)
00007
00008 ConfigurationDatabaseImplXMLFile::ConfigurationDatabaseImplXMLFile() {
00009 }
00010 ConfigurationDatabaseImplXMLFile::~ConfigurationDatabaseImplXMLFile() {
00011 }
00012 bool ConfigurationDatabaseImplXMLFile::canHandleMethod(const std::string& method) const { return method=="xmlfile"; }
00013
00014 void ConfigurationDatabaseImplXMLFile::connect(const std::string& accessor) throw (hcal::exception::ConfigurationDatabaseException) {
00015
00016 std::string theFile=accessor;
00017 std::string::size_type i=theFile.find("://");
00018 if (i!=std::string::npos) theFile.erase(0,i+2);
00019 gzFile f=gzopen(theFile.c_str(),"rb");
00020
00021 if (f==0) {
00022 XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,"Unable to open file "+theFile);
00023 }
00024 int c;
00025 while ((c=gzgetc(f))!=EOF) m_buffer+=(unsigned char)c;
00026 gzclose(f);
00027
00028
00029 std::string::size_type j=0;
00030 while ((i=m_buffer.find("<CFGBrick>",j))!=std::string::npos) {
00031 j=m_buffer.find("</CFGBrick>",i)+strlen("</CFGBrick>");
00032 if (j==std::string::npos) break;
00033
00034 std::map<std::string,std::string> params=extractParams(i,j);
00035 std::string key=createKey(params);
00036
00037 std::pair<int,int> ptrs(i,j);
00038 m_lookup.insert(std::pair<std::string, std::pair<int,int> >(key,ptrs));
00039 }
00040 }
00041
00042 std::string ConfigurationDatabaseImplXMLFile::createKey(const std::map<std::string,std::string>& params) {
00043 std::string retval;
00044 if (params.find("PATTERN_SPEC_NAME")!=params.end()) {
00045 retval=params.find("TAG")->second+":"+
00046 params.find("CRATE")->second+":"+
00047 params.find("SLOT")->second+":"+
00048 params.find("TOPBOTTOM")->second+":"+
00049 params.find("FIBER")->second;
00050 } else if (params.find("LUT_TYPE")!=params.end()) {
00051 retval=params.find("TAG")->second+":"+
00052 params.find("CRATE")->second+":"+
00053 params.find("SLOT")->second+":"+
00054 params.find("TOPBOTTOM")->second+":"+
00055 params.find("LUT_TYPE")->second;
00056 if (params.find("FIBER")!=params.end())
00057 retval+=":"+params.find("FIBER")->second+":" +
00058 params.find("FIBERCHAN")->second;
00059 if (params.find("SLB")!=params.end())
00060 retval+=":"+params.find("SLB")->second+":" +
00061 params.find("SLBCHAN")->second;
00062 } else if (params.find("BOARD")!=params.end()) {
00063 int ver=strtol(params.find("VERSION")->second.c_str(),0,0);
00064 retval=params.find("BOARD")->second+":"+
00065 ::toolbox::toString("%x",ver);
00066 } else if (params.find("ZS_TYPE")!=params.end()) {
00067 retval=params.find("TAG")->second+":"+
00068 params.find("CRATE")->second+":"+
00069 params.find("SLOT")->second+":"+
00070 params.find("TOPBOTTOM")->second;
00071 } else retval="WHAT";
00072 return retval;
00073 }
00074
00075
00076 std::map<std::string, std::string> ConfigurationDatabaseImplXMLFile::extractParams(int beg, int end) {
00077 std::map<std::string,std::string> pval;
00078 std::string::size_type l=beg,i,j;
00079 std::string name,val;
00080
00081 while ((i=m_buffer.find("<Parameter",l))!=std::string::npos && i<(unsigned int)end) {
00082 j=m_buffer.find("name=",i);
00083 char separator=m_buffer[j+5];
00084 i=m_buffer.find(separator,j+6);
00085 name=m_buffer.substr(j+6,i-(j+6));
00086 if (name=="CREATIONTAG") name="TAG";
00087 j=m_buffer.find('>',j);
00088 i=m_buffer.find("</",j);
00089 val=m_buffer.substr(j+1,i-j-1);
00090 pval.insert(std::pair<std::string,std::string>(name,val));
00091 l=j;
00092 }
00093
00094 return pval;
00095 }
00096
00097 void ConfigurationDatabaseImplXMLFile::disconnect() {
00098 m_lookup.clear();
00099 m_buffer.clear();
00100 }
00101
00102 std::map<std::string, std::string> ConfigurationDatabaseImplXMLFile::parseWhere(const std::string& where) {
00103 std::string::size_type i,j=0,k,k2;
00104 std::map<std::string,std::string> itis;
00105
00106 while ((i=where.find('=',j))!=std::string::npos) {
00107 k=where.rfind(' ',i);
00108 k2=where.rfind('(',i);
00109 if (k2!=std::string::npos && k2>k) k=k2;
00110 if (k==std::string::npos) k=0;
00111 else k++;
00112 std::string key = where.substr(k,i-k),value;
00113 if (where[i+1]=='\'' || where[i+1]=='\"') {
00114 j=where.find(where[i+1],i+2);
00115 value=where.substr(i+2,j-i-2);
00116 } else {
00117 j=where.find(' ',i);
00118 k=where.find(')',i);
00119 if (k!=std::string::npos && k<j) j=k;
00120 value=where.substr(i+1,j-i-1);
00121 }
00122 itis.insert(std::pair<std::string,std::string>(key,value));
00123 }
00124 return itis;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 unsigned int ConfigurationDatabaseImplXMLFile::getFirmwareChecksum(const std::string& board, unsigned int version) throw (hcal::exception::ConfigurationDatabaseException) {
00143 XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,"Unsupported");
00144 }
00145
00146 void ConfigurationDatabaseImplXMLFile::getFirmwareMCS(const std::string& board, unsigned int version, std::vector<std::string>& mcsLines) throw (hcal::exception::ConfigurationDatabaseException) {
00147 std::string key=::toolbox::toString("%s:%x",board.c_str(),version);
00148
00149 std::map<std::string, std::pair<int,int> >::iterator j=m_lookup.find(key);
00150 if (j==m_lookup.end()) {
00151 XCEPT_RAISE(hcal::exception::ConfigurationItemNotFoundException,"");
00152 }
00153 std::string data="<?xml version='1.0'?>\n";
00154 data+=m_buffer.substr(j->second.first,j->second.second-j->second.first);
00155
00156 std::map<std::string, std::string> params;
00157 std::string encoding;
00158 m_parser.parse(data.c_str(),params,mcsLines,encoding);
00159
00160 }
00161
00162 void ConfigurationDatabaseImplXMLFile::getLUTChecksums(const std::string& tag, std::map<hcal::ConfigurationDatabase::LUTId, hcal::ConfigurationDatabase::MD5Fingerprint>& checksums) throw (hcal::exception::ConfigurationDatabaseException) {
00163 XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,"Unsupported");
00164 }
00165
00166 void ConfigurationDatabaseImplXMLFile::getLUTs(const std::string& tag, int crate, int slot, std::map<hcal::ConfigurationDatabase::LUTId, hcal::ConfigurationDatabase::LUT >& LUTs) throw (hcal::exception::ConfigurationDatabaseException) {
00167 LUTs.clear();
00168
00169 for (int tb=0; tb<=1; tb++)
00170 for (int fiber=1; fiber<=8; fiber++)
00171 for (int fiberChan=0; fiberChan<=2; fiberChan++) {
00172 int lut_type=1;
00173
00174 std::string key=toolbox::toString("%s:%d:%d:%d:%d:%d:%d",
00175 tag.c_str(), crate, slot, tb, lut_type, fiber, fiberChan);
00176 std::map<std::string, std::pair<int,int> >::iterator j=m_lookup.find(key);
00177 if (j==m_lookup.end()) continue;
00178 std::string data="<?xml version='1.0'?>\n";
00179 data+=m_buffer.substr(j->second.first,j->second.second-j->second.first);
00180
00181 std::map<std::string, std::string> params;
00182 std::vector<std::string> values;
00183 std::string encoding;
00184 m_parser.parse(data.c_str(),params,values,encoding);
00185
00186 hcal::ConfigurationDatabase::LUTId id(crate,slot,(hcal::ConfigurationDatabase::FPGASelection)tb,fiber,fiberChan,(hcal::ConfigurationDatabase::LUTType)lut_type);
00187 hcal::ConfigurationDatabase::LUT& lut=LUTs[id];
00188 lut.reserve(values.size());
00189
00190 int strtol_base=0;
00191 if (encoding=="hex") strtol_base=16;
00192 else if (encoding=="dec") strtol_base=10;
00193
00194
00195 for (unsigned int j=0; j<values.size(); j++)
00196 lut.push_back(strtol(values[j].c_str(),0,strtol_base));
00197 }
00198 for (int tb=0; tb<=1; tb++)
00199 for (int slb=1; slb<=6; slb++)
00200 for (int slbChan=0; slbChan<=3; slbChan++) {
00201 int lut_type=2;
00202
00203 std::string key=toolbox::toString("%s:%d:%d:%d:%d:%d:%d",
00204 tag.c_str(), crate, slot, tb, lut_type, slb, slbChan);
00205
00206 std::map<std::string, std::pair<int,int> >::iterator j=m_lookup.find(key);
00207 if (j==m_lookup.end()) continue;
00208 std::string data="<?xml version='1.0'?>\n";
00209 data+=m_buffer.substr(j->second.first,j->second.second-j->second.first);
00210
00211 std::map<std::string, std::string> params;
00212 std::vector<std::string> values;
00213 std::string encoding;
00214 m_parser.parse(data.c_str(),params,values,encoding);
00215
00216 hcal::ConfigurationDatabase::LUTId id(crate,slot,(hcal::ConfigurationDatabase::FPGASelection)tb,slb,slbChan,(hcal::ConfigurationDatabase::LUTType)lut_type);
00217 hcal::ConfigurationDatabase::LUT& lut=LUTs[id];
00218 lut.reserve(values.size());
00219
00220 int strtol_base=0;
00221 if (encoding=="hex") strtol_base=16;
00222 else if (encoding=="dec") strtol_base=10;
00223
00224
00225 for (unsigned int j=0; j<values.size(); j++)
00226 lut.push_back(strtol(values[j].c_str(),0,strtol_base));
00227 }
00228 }
00229
00230 void ConfigurationDatabaseImplXMLFile::getZSThresholds(const std::string& tag, int crate, int slot, std::map<hcal::ConfigurationDatabase::ZSChannelId, int>& thresholds) throw (hcal::exception::ConfigurationDatabaseException) {
00231 thresholds.clear();
00232 for (int tb=0; tb<=1; tb++) {
00233 std::string key=toolbox::toString("%s:%d:%d:%d",
00234 tag.c_str(), crate, slot, tb);
00235 std::map<std::string, std::pair<int,int> >::iterator j=m_lookup.find(key);
00236 if (j==m_lookup.end()) continue;
00237 std::string data="<?xml version='1.0'?>\n";
00238 data+=m_buffer.substr(j->second.first,j->second.second-j->second.first);
00239
00240 std::map<std::string, std::string> params;
00241 std::vector<std::string> values;
00242 std::string encoding;
00243 m_parser.parse(data.c_str(),params,values,encoding);
00244
00245 if (values.size()!=24) {
00246 XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Must have 24 items in ZS list. Saw %d for %s",values.size(),key.c_str()));
00247 }
00248 for (int fiber=1; fiber<=8; fiber++)
00249 for (int fc=0; fc<3; fc++) {
00250 hcal::ConfigurationDatabase::ZSChannelId id(crate,slot,(hcal::ConfigurationDatabase::FPGASelection)tb,fiber,fc);
00251
00252 int strtol_base=0;
00253 if (encoding=="hex") strtol_base=16;
00254 else if (encoding=="dec") strtol_base=10;
00255
00256 thresholds[id]=strtol(values[(fiber-1)*3+fc].c_str(),0,strtol_base);
00257 }
00258 }
00259 }
00260
00261 void ConfigurationDatabaseImplXMLFile::getPatterns(const std::string& tag, int crate, int slot, std::map<hcal::ConfigurationDatabase::PatternId, hcal::ConfigurationDatabase::HTRPattern >& patterns) throw (hcal::exception::ConfigurationDatabaseException) {
00262 patterns.clear();
00263 for (int tb=0; tb<=1; tb++)
00264 for (int fiber=1; fiber<=8; fiber++) {
00265 std::string key=toolbox::toString("%s:%d:%d:%d:%d",
00266 tag.c_str(), crate, slot, tb, fiber);
00267 std::map<std::string, std::pair<int,int> >::iterator j=m_lookup.find(key);
00268 if (j==m_lookup.end()) continue;
00269 std::string data="<?xml version='1.0'?>\n";
00270 data+=m_buffer.substr(j->second.first,j->second.second-j->second.first);
00271
00272 std::map<std::string, std::string> params;
00273 std::vector<std::string> values;
00274 std::string encoding;
00275 m_parser.parse(data.c_str(),params,values,encoding);
00276
00277 hcal::ConfigurationDatabase::PatternId id(crate,slot,(hcal::ConfigurationDatabase::FPGASelection)tb,fiber);
00278 hcal::ConfigurationDatabase::HTRPattern& lut=patterns[id];
00279 lut.reserve(values.size());
00280
00281 int strtol_base=0;
00282 if (encoding=="hex") strtol_base=16;
00283 else if (encoding=="dec") strtol_base=10;
00284
00285
00286 for (unsigned int j=0; j<values.size(); j++)
00287 lut.push_back(strtol(values[j].c_str(),0,strtol_base));
00288 }
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300