Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:52:05

0001 #include <iostream>
0002 #include <sstream>
0003 #include <fstream>
0004 #include <xercesc/dom/DOMNode.hpp>
0005 #include <xercesc/dom/DOM.hpp>
0006 #include <xercesc/parsers/XercesDOMParser.hpp>
0007 #include "Utilities/Xerces/interface/Xerces.h"
0008 #include "Utilities/Xerces/interface/XercesStrUtils.h"
0009 #include <xercesc/util/XMLString.hpp>
0010 #include <xercesc/sax/SAXException.hpp>
0011 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0012 
0013 #include "CondTools/Ecal/interface/EcalWeightSetXMLTranslator.h"
0014 #include "CondTools/Ecal/interface/XMLTags.h"
0015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0016 
0017 using namespace XERCES_CPP_NAMESPACE;
0018 using namespace xuti;
0019 using namespace std;
0020 
0021 int EcalWeightSetXMLTranslator::readXML(const std::string& filename, EcalCondHeader& header, EcalWeightSet& record) {
0022   cms::concurrency::xercesInitialize();
0023 
0024   XercesDOMParser* parser = new XercesDOMParser;
0025   parser->setValidationScheme(XercesDOMParser::Val_Never);
0026   parser->setDoNamespaces(false);
0027   parser->setDoSchema(false);
0028 
0029   parser->parse(filename.c_str());
0030 
0031   DOMDocument* xmlDoc = parser->getDocument();
0032   if (!xmlDoc) {
0033     std::cout << "EcalWeightSetXMLTranslator::Error parsing document" << std::endl;
0034     return -1;
0035   }
0036 
0037   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0038 
0039   xuti::readHeader(elementRoot, header);
0040   // get the first cell node
0041   DOMNode* wgtBSnode = getChildNode(elementRoot, wgtBeforeSwitch_tag);
0042   DOMNode* wgtASnode = getChildNode(elementRoot, wgtAfterSwitch_tag);
0043   DOMNode* wgtChi2BSnode = getChildNode(elementRoot, wgtChi2BeforeSwitch_tag);
0044   DOMNode* wgtChi2ASnode = getChildNode(elementRoot, wgtChi2AfterSwitch_tag);
0045 
0046   DOMNode* rownode = getChildNode(wgtBSnode, row_tag);
0047 
0048   DOMElement* rowelement;
0049 
0050   // loop on row nodes
0051   while (rownode) {
0052     rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
0053 
0054     std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
0055 
0056     std::stringstream rowid_ss(rowid_s);
0057     int rowid = 0;
0058     rowid_ss >> rowid;
0059 
0060     std::string weightrow = cms::xerces::toString(rownode->getTextContent());
0061 
0062     std::stringstream weightrow_s(weightrow);
0063     double weight = 0;
0064     int i = 0;
0065     while (weightrow_s >> weight) {
0066       record.getWeightsBeforeGainSwitch()(i, rowid) = weight;
0067       i++;
0068     }
0069 
0070     // get next cell
0071     rownode = rownode->getNextSibling();
0072 
0073     while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
0074       rownode = rownode->getNextSibling();
0075   }
0076 
0077   rownode = getChildNode(wgtASnode, row_tag);
0078 
0079   // loop on row nodes
0080   while (rownode) {
0081     rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
0082 
0083     std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
0084 
0085     std::stringstream rowid_ss(rowid_s);
0086     int rowid = 0;
0087     rowid_ss >> rowid;
0088 
0089     std::string weightrow = cms::xerces::toString(rownode->getTextContent());
0090 
0091     std::stringstream weightrow_s(weightrow);
0092     double weight = 0;
0093     int i = 0;
0094     while (weightrow_s >> weight) {
0095       record.getWeightsAfterGainSwitch()(i, rowid) = weight;
0096       i++;
0097     }
0098 
0099     // get next cell
0100     rownode = rownode->getNextSibling();
0101 
0102     while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
0103       rownode = rownode->getNextSibling();
0104   }
0105 
0106   rownode = getChildNode(wgtChi2BSnode, row_tag);
0107 
0108   // loop on row nodes
0109   while (rownode) {
0110     rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
0111     std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
0112 
0113     std::stringstream rowid_ss(rowid_s);
0114     int rowid = 0;
0115     rowid_ss >> rowid;
0116 
0117     std::string weightrow = cms::xerces::toString(rownode->getTextContent());
0118 
0119     std::stringstream weightrow_s(weightrow);
0120     double weight = 0;
0121     int i = 0;
0122     while (weightrow_s >> weight) {
0123       record.getChi2WeightsBeforeGainSwitch()(i, rowid) = weight;
0124       i++;
0125     }
0126 
0127     // get next cell
0128     rownode = rownode->getNextSibling();
0129 
0130     while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
0131       rownode = rownode->getNextSibling();
0132   }
0133 
0134   rownode = getChildNode(wgtChi2ASnode, row_tag);
0135 
0136   // loop on row nodes
0137   while (rownode) {
0138     rowelement = dynamic_cast<xercesc::DOMElement*>(rownode);
0139     std::string rowid_s = cms::xerces::toString(rowelement->getAttribute(cms::xerces::uStr(id_tag.c_str()).ptr()));
0140 
0141     std::stringstream rowid_ss(rowid_s);
0142     int rowid = 0;
0143     rowid_ss >> rowid;
0144 
0145     std::string weightrow = cms::xerces::toString(rownode->getTextContent());
0146 
0147     std::stringstream weightrow_s(weightrow);
0148     double weight = 0;
0149     int i = 0;
0150     while (weightrow_s >> weight) {
0151       record.getChi2WeightsAfterGainSwitch()(i, rowid) = weight;
0152       i++;
0153     }
0154 
0155     // get next cell
0156     rownode = rownode->getNextSibling();
0157 
0158     while (rownode && rownode->getNodeType() != DOMNode::ELEMENT_NODE)
0159       rownode = rownode->getNextSibling();
0160   }
0161 
0162   delete parser;
0163   cms::concurrency::xercesTerminate();
0164   return 0;
0165 }
0166 
0167 int EcalWeightSetXMLTranslator::writeXML(const std::string& filename,
0168                                          const EcalCondHeader& header,
0169                                          const EcalWeightSet& record) {
0170   cms::concurrency::xercesInitialize();
0171 
0172   std::fstream fs(filename.c_str(), ios::out);
0173   fs << dumpXML(header, record);
0174 
0175   cms::concurrency::xercesTerminate();
0176 
0177   return 0;
0178 }
0179 
0180 void EcalWeightSetXMLTranslator::write10x10(xercesc::DOMElement* node, const EcalWeightSet& record) {
0181   DOMElement* row[10];
0182   DOMAttr* rowid[10];
0183   DOMText* rowvalue[10];
0184   EcalWeightSet::EcalChi2WeightMatrix echi2wmatrix;
0185 
0186   if (cms::xerces::toString(node->getNodeName()) == wgtChi2BeforeSwitch_tag) {
0187     echi2wmatrix = record.getChi2WeightsBeforeGainSwitch();
0188   }
0189   if (cms::xerces::toString(node->getNodeName()) == wgtChi2AfterSwitch_tag) {
0190     echi2wmatrix = record.getChi2WeightsAfterGainSwitch();
0191   }
0192 
0193   for (int i = 0; i < 10; ++i) {
0194     row[i] = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
0195     node->appendChild(row[i]);
0196 
0197     stringstream value_s;
0198     value_s << i;
0199 
0200     rowid[i] = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
0201     rowid[i]->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
0202     row[i]->setAttributeNode(rowid[i]);
0203 
0204     stringstream row_s;
0205 
0206     for (int k = 0; k < 10; ++k) {
0207       row_s << " ";
0208       row_s << echi2wmatrix(k, i);
0209       row_s << " ";
0210     }  //for loop on element
0211 
0212     rowvalue[i] = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
0213     row[i]->appendChild(rowvalue[i]);
0214   }  //for loop on row
0215 }
0216 
0217 void EcalWeightSetXMLTranslator::write3x10(xercesc::DOMElement* node, const EcalWeightSet& record) {
0218   DOMElement* row[10];
0219   DOMAttr* rowid[10];
0220   DOMText* rowvalue[10];
0221   EcalWeightSet::EcalWeightMatrix ewmatrix;
0222 
0223   if (cms::xerces::toString(node->getNodeName()) == wgtBeforeSwitch_tag)
0224     ewmatrix = record.getWeightsBeforeGainSwitch();
0225 
0226   if (cms::xerces::toString(node->getNodeName()) == wgtAfterSwitch_tag)
0227     ewmatrix = record.getWeightsAfterGainSwitch();
0228 
0229   for (int i = 0; i < 10; ++i) {
0230     row[i] = node->getOwnerDocument()->createElement(cms::xerces::uStr(row_tag.c_str()).ptr());
0231     node->appendChild(row[i]);
0232 
0233     stringstream value_s;
0234     value_s << i;
0235 
0236     rowid[i] = node->getOwnerDocument()->createAttribute(cms::xerces::uStr(id_tag.c_str()).ptr());
0237 
0238     rowid[i]->setValue(cms::xerces::uStr(value_s.str().c_str()).ptr());
0239 
0240     row[i]->setAttributeNode(rowid[i]);
0241 
0242     stringstream row_s;
0243 
0244     for (int k = 0; k < 3; ++k) {
0245       row_s << " ";
0246       row_s << ewmatrix(k, i);
0247       row_s << " ";
0248     }  //for loop on element
0249 
0250     rowvalue[i] = node->getOwnerDocument()->createTextNode(cms::xerces::uStr(row_s.str().c_str()).ptr());
0251     row[i]->appendChild(rowvalue[i]);
0252   }  //for loop on row
0253 }
0254 
0255 std::string EcalWeightSetXMLTranslator::dumpXML(const EcalCondHeader& header, const EcalWeightSet& record) {
0256   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0257 
0258   DOMLSSerializer* writer = impl->createLSSerializer();
0259   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0260     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0261 
0262   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0263   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(EcalWeightSet_tag.c_str()).ptr(), doctype);
0264 
0265   DOMElement* root = doc->getDocumentElement();
0266 
0267   xuti::writeHeader(root, header);
0268 
0269   DOMElement* wgtBS = doc->createElement(cms::xerces::uStr(wgtBeforeSwitch_tag.c_str()).ptr());
0270   root->appendChild(wgtBS);
0271 
0272   DOMElement* wgtAS = doc->createElement(cms::xerces::uStr(wgtAfterSwitch_tag.c_str()).ptr());
0273   root->appendChild(wgtAS);
0274 
0275   DOMElement* wgtChi2BS = doc->createElement(cms::xerces::uStr(wgtChi2BeforeSwitch_tag.c_str()).ptr());
0276   root->appendChild(wgtChi2BS);
0277 
0278   DOMElement* wgtChi2AS = doc->createElement(cms::xerces::uStr(wgtChi2AfterSwitch_tag.c_str()).ptr());
0279   root->appendChild(wgtChi2AS);
0280 
0281   write3x10(wgtBS, record);
0282   write3x10(wgtAS, record);
0283 
0284   write10x10(wgtChi2BS, record);
0285   write10x10(wgtChi2AS, record);
0286 
0287   std::string dump = cms::xerces::toString(writer->writeToString(root));
0288   doc->release();
0289   doctype->release();
0290   writer->release();
0291 
0292   return dump;
0293 }