Go to the documentation of this file.00001 #include <iostream>
00002 #include <sstream>
00003 #include <fstream>
00004 #include <xercesc/dom/DOMNode.hpp>
00005 #include <xercesc/dom/DOM.hpp>
00006 #include <xercesc/parsers/XercesDOMParser.hpp>
00007 #include <xercesc/util/PlatformUtils.hpp>
00008 #include <xercesc/util/XMLString.hpp>
00009 #include <xercesc/sax/SAXException.hpp>
00010 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00011
00012
00013
00014 #include "CondTools/Ecal/interface/EcalWeightSetXMLTranslator.h"
00015 #include "CondTools/Ecal/interface/XMLTags.h"
00016 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00017
00018
00019 using namespace XERCES_CPP_NAMESPACE;
00020 using namespace xuti;
00021 using namespace std;
00022
00023
00024
00025 int EcalWeightSetXMLTranslator::readXML(const std::string& filename,
00026 EcalCondHeader& header,
00027 EcalWeightSet& record){
00028
00029 XMLPlatformUtils::Initialize();
00030
00031 XercesDOMParser* parser = new XercesDOMParser;
00032 parser->setValidationScheme( XercesDOMParser::Val_Never );
00033 parser->setDoNamespaces( false );
00034 parser->setDoSchema( false );
00035
00036 parser->parse(filename.c_str());
00037
00038 DOMDocument* xmlDoc = parser->getDocument();
00039 if (!xmlDoc) {
00040 std::cout << "EcalWeightSetXMLTranslator::Error parsing document" << std::endl;
00041 return -1;
00042 }
00043
00044 DOMElement* elementRoot = xmlDoc->getDocumentElement();
00045
00046 xuti::readHeader(elementRoot, header);
00047
00048 DOMNode * wgtBSnode=getChildNode(elementRoot,wgtBeforeSwitch_tag);
00049 DOMNode * wgtASnode=getChildNode(elementRoot,wgtAfterSwitch_tag);
00050 DOMNode * wgtChi2BSnode=getChildNode(elementRoot,wgtChi2BeforeSwitch_tag);
00051 DOMNode * wgtChi2ASnode=getChildNode(elementRoot,wgtChi2AfterSwitch_tag);
00052
00053
00054 DOMNode* rownode = getChildNode(wgtBSnode,row_tag);
00055
00056 DOMElement* rowelement;
00057
00058
00059 while (rownode){
00060
00061 rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
00062
00063 std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
00064
00065 std::stringstream rowid_ss(rowid_s);
00066 int rowid = 0;
00067 rowid_ss >> rowid;
00068
00069 std::string weightrow = toNative(rownode->getTextContent());
00070
00071 std::stringstream weightrow_s(weightrow);
00072 double weight = 0;
00073 int i = 0;
00074 while(weightrow_s >> weight)
00075 {
00076 record.getWeightsBeforeGainSwitch()(i,rowid)= weight;
00077 i++;
00078 }
00079
00080
00081
00082 rownode = rownode->getNextSibling();
00083
00084 while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
00085 rownode = rownode->getNextSibling();
00086
00087
00088 }
00089
00090 rownode = getChildNode(wgtASnode,row_tag);
00091
00092
00093 while (rownode){
00094
00095 rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
00096
00097 std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
00098
00099 std::stringstream rowid_ss(rowid_s);
00100 int rowid = 0;
00101 rowid_ss >> rowid;
00102
00103 std::string weightrow = toNative(rownode->getTextContent());
00104
00105 std::stringstream weightrow_s(weightrow);
00106 double weight = 0;
00107 int i = 0;
00108 while(weightrow_s >> weight)
00109 {
00110 record.getWeightsAfterGainSwitch()(i,rowid)= weight;
00111 i++;
00112 }
00113
00114
00115
00116 rownode = rownode->getNextSibling();
00117
00118 while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
00119 rownode = rownode->getNextSibling();
00120
00121
00122 }
00123
00124 rownode = getChildNode(wgtChi2BSnode,row_tag);
00125
00126
00127
00128 while (rownode){
00129
00130 rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
00131 std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
00132
00133 std::stringstream rowid_ss(rowid_s);
00134 int rowid = 0;
00135 rowid_ss >> rowid;
00136
00137 std::string weightrow = toNative(rownode->getTextContent());
00138
00139 std::stringstream weightrow_s(weightrow);
00140 double weight = 0;
00141 int i = 0;
00142 while(weightrow_s >> weight)
00143 {
00144 record.getChi2WeightsBeforeGainSwitch()(i,rowid)= weight;
00145 i++;
00146 }
00147
00148
00149
00150 rownode = rownode->getNextSibling();
00151
00152 while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
00153 rownode = rownode->getNextSibling();
00154
00155
00156 }
00157
00158
00159 rownode = getChildNode(wgtChi2ASnode,row_tag);
00160
00161
00162
00163 while (rownode){
00164
00165 rowelement = dynamic_cast< xercesc::DOMElement* >(rownode);
00166 std::string rowid_s = toNative(rowelement->getAttribute(fromNative(id_tag).c_str()));
00167
00168 std::stringstream rowid_ss(rowid_s);
00169 int rowid = 0;
00170 rowid_ss >> rowid;
00171
00172 std::string weightrow = toNative(rownode->getTextContent());
00173
00174 std::stringstream weightrow_s(weightrow);
00175 double weight = 0;
00176 int i = 0;
00177 while(weightrow_s >> weight)
00178 {
00179 record.getChi2WeightsAfterGainSwitch()(i,rowid)= weight;
00180 i++;
00181 }
00182
00183
00184
00185 rownode = rownode->getNextSibling();
00186
00187 while (rownode && rownode->getNodeType( ) != DOMNode::ELEMENT_NODE)
00188 rownode = rownode->getNextSibling();
00189
00190
00191 }
00192
00193 delete parser;
00194 XMLPlatformUtils::Terminate();
00195 return 0;
00196
00197 }
00198
00199
00200
00201
00202
00203 int EcalWeightSetXMLTranslator::writeXML(const std::string& filename,
00204 const EcalCondHeader& header,
00205 const EcalWeightSet& record){
00206
00207 std::fstream fs(filename.c_str(),ios::out);
00208 fs<< dumpXML(header,record);
00209 return 0;
00210
00211 }
00212
00213
00214
00215
00216 void EcalWeightSetXMLTranslator::write10x10(xercesc::DOMElement* node,
00217 const EcalWeightSet& record){
00218
00219 DOMElement* row[10];
00220 DOMAttr* rowid[10];
00221 DOMText* rowvalue[10];
00222 EcalWeightSet::EcalChi2WeightMatrix echi2wmatrix;
00223
00224
00225 if(toNative(node->getNodeName()) == wgtChi2BeforeSwitch_tag)
00226 {
00227 echi2wmatrix = record.getChi2WeightsBeforeGainSwitch();
00228
00229 }
00230 if(toNative(node->getNodeName()) == wgtChi2AfterSwitch_tag)
00231 {
00232 echi2wmatrix = record.getChi2WeightsAfterGainSwitch();
00233
00234 }
00235
00236 for(int i=0;i<10;++i)
00237 {
00238
00239
00240 row[i] = node->getOwnerDocument()->createElement(fromNative(row_tag).c_str());
00241 node->appendChild(row[i]);
00242
00243 stringstream value_s;
00244 value_s << i;
00245
00246 rowid[i] = node->getOwnerDocument()->createAttribute(fromNative(id_tag).c_str());
00247 rowid[i]->setValue(fromNative(value_s.str()).c_str());
00248 row[i]->setAttributeNode(rowid[i]);
00249
00250 stringstream row_s;
00251
00252 for(int k=0;k<10;++k)
00253 {
00254 row_s << " ";
00255 row_s << echi2wmatrix(k,i);
00256 row_s << " ";
00257 }
00258
00259
00260 rowvalue[i] = node->getOwnerDocument()->createTextNode(fromNative(row_s.str()).c_str());
00261 row[i]->appendChild(rowvalue[i]);
00262 }
00263 }
00264
00265
00266
00267 void EcalWeightSetXMLTranslator::write3x10(xercesc::DOMElement* node,
00268 const EcalWeightSet& record){
00269
00270 DOMElement* row[10];
00271 DOMAttr* rowid[10];
00272 DOMText* rowvalue[10];
00273 EcalWeightSet::EcalWeightMatrix ewmatrix;
00274
00275
00276 if(toNative(node->getNodeName()) == wgtBeforeSwitch_tag)
00277 ewmatrix = record.getWeightsBeforeGainSwitch();
00278
00279 if(toNative(node->getNodeName()) == wgtAfterSwitch_tag)
00280 ewmatrix = record.getWeightsAfterGainSwitch();
00281
00282
00283 for(int i=0;i<10;++i)
00284 {
00285
00286 row[i] = node->getOwnerDocument()->createElement(fromNative(row_tag).c_str());
00287 node->appendChild(row[i]);
00288
00289 stringstream value_s;
00290 value_s << i;
00291
00292 rowid[i] = node->getOwnerDocument()->createAttribute(fromNative(id_tag).c_str());
00293
00294 rowid[i]->setValue(fromNative(value_s.str()).c_str());
00295
00296 row[i]->setAttributeNode(rowid[i]);
00297
00298
00299 stringstream row_s;
00300
00301 for(int k=0;k<3;++k)
00302 {
00303 row_s << " ";
00304 row_s << ewmatrix(k,i);
00305 row_s << " ";
00306 }
00307
00308
00309 rowvalue[i] = node->getOwnerDocument()->createTextNode(fromNative(row_s.str()).c_str());
00310 row[i]->appendChild(rowvalue[i]);
00311 }
00312 }
00313
00314
00315 std::string EcalWeightSetXMLTranslator::dumpXML(const EcalCondHeader& header,
00316 const EcalWeightSet& record){
00317
00318
00319 XMLPlatformUtils::Initialize();
00320
00321 DOMImplementation* impl =
00322 DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00323
00324 DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00325 writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00326
00327 DOMDocumentType* doctype = impl->createDocumentType( fromNative("XML").c_str(), 0, 0 );
00328 DOMDocument * doc =
00329 impl->createDocument( 0,fromNative(EcalWeightSet_tag).c_str(), doctype );
00330
00331
00332 doc->setEncoding(fromNative("UTF-8").c_str() );
00333 doc->setStandalone(true);
00334 doc->setVersion(fromNative("1.0").c_str() );
00335
00336 DOMElement* root = doc->getDocumentElement();
00337
00338 xuti::writeHeader(root, header);
00339
00340 DOMElement* wgtBS = doc->createElement(fromNative(wgtBeforeSwitch_tag).c_str());
00341 root->appendChild(wgtBS);
00342
00343 DOMElement* wgtAS = doc->createElement(fromNative(wgtAfterSwitch_tag).c_str());
00344 root->appendChild(wgtAS);
00345
00346 DOMElement* wgtChi2BS = doc->createElement(fromNative(wgtChi2BeforeSwitch_tag).c_str());
00347 root->appendChild(wgtChi2BS);
00348
00349 DOMElement* wgtChi2AS = doc->createElement(fromNative(wgtChi2AfterSwitch_tag).c_str());
00350 root->appendChild(wgtChi2AS);
00351
00352 write3x10(wgtBS,record);
00353 write3x10(wgtAS,record);
00354
00355 write10x10(wgtChi2BS,record);
00356 write10x10(wgtChi2AS,record);
00357
00358 std::string dump= toNative(writer->writeToString(*root));
00359 doc->release();
00360
00361 return dump;
00362
00363
00364 }