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/EcalWeightGroupXMLTranslator.h"
0014 #include "CondFormats/EcalObjects/interface/EcalXtalGroupId.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 EcalWeightGroupXMLTranslator::readXML(const std::string& filename,
0022                                           EcalCondHeader& header,
0023                                           EcalWeightXtalGroups& record) {
0024   cms::concurrency::xercesInitialize();
0025 
0026   XercesDOMParser* parser = new XercesDOMParser;
0027   parser->setValidationScheme(XercesDOMParser::Val_Never);
0028   parser->setDoNamespaces(false);
0029   parser->setDoSchema(false);
0030 
0031   parser->parse(filename.c_str());
0032 
0033   DOMDocument* xmlDoc = parser->getDocument();
0034   if (!xmlDoc) {
0035     std::cout << "EcalWeightGroupXMLTranslator::Error parsing document" << std::endl;
0036     return -1;
0037   }
0038 
0039   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0040 
0041   xuti::readHeader(elementRoot, header);
0042   // get the first cell node
0043   DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
0044 
0045   // loop on cell nodes
0046   while (cellnode) {
0047     unsigned int group = 0;
0048 
0049     // read id
0050     DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
0051 
0052     // read constant
0053     DOMNode* c_node = getChildNode(cellnode, WeightGroup_tag);
0054     GetNodeData(c_node, group);
0055 
0056     // fill record
0057     record[detid] = EcalXtalGroupId(group);
0058 
0059     // get next cell
0060     cellnode = cellnode->getNextSibling();
0061 
0062     while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
0063       cellnode = cellnode->getNextSibling();
0064   }
0065 
0066   delete parser;
0067   cms::concurrency::xercesTerminate();
0068   return 0;
0069 }
0070 
0071 int EcalWeightGroupXMLTranslator::writeXML(const std::string& filename,
0072                                            const EcalCondHeader& header,
0073                                            const EcalWeightXtalGroups& record) {
0074   cms::concurrency::xercesInitialize();
0075 
0076   std::fstream fs(filename.c_str(), ios::out);
0077   fs << dumpXML(header, record);
0078 
0079   cms::concurrency::xercesTerminate();
0080 
0081   return 0;
0082 }
0083 
0084 std::string EcalWeightGroupXMLTranslator::dumpXML(const EcalCondHeader& header, const EcalWeightXtalGroups& record) {
0085   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0086 
0087   DOMLSSerializer* writer = impl->createLSSerializer();
0088   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0089     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0090 
0091   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0092   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(WeightGroups_tag.c_str()).ptr(), doctype);
0093   DOMElement* root = doc->getDocumentElement();
0094 
0095   xuti::writeHeader(root, header);
0096 
0097   for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {  // loop on EB cells
0098 
0099     uint32_t rawid = EBDetId::unhashIndex(cellid);
0100 
0101     //if (!record[rawid]) continue; // cell absent from original record
0102 
0103     DOMElement* cellnode = writeCell(root, rawid);
0104 
0105     WriteNodeWithValue(cellnode, WeightGroup_tag, record[rawid].id());
0106 
0107   }  // loop on EB cells
0108 
0109   for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {  // loop on EE cells
0110 
0111     if (!EEDetId::validHashIndex(cellid))
0112       continue;
0113 
0114     uint32_t rawid = EEDetId::unhashIndex(cellid);
0115     //xif (!record[rawid]) continue; // cell absent from original record
0116 
0117     DOMElement* cellnode = writeCell(root, rawid);
0118     WriteNodeWithValue(cellnode, WeightGroup_tag, record[rawid].id());
0119 
0120   }  // loop on EE cells
0121 
0122   std::string dump = cms::xerces::toString(writer->writeToString(root));
0123   doc->release();
0124   doctype->release();
0125   writer->release();
0126 
0127   return dump;
0128 }