Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:55

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