Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:58

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/EcalGainRatios.h"
0014 #include "CondTools/Ecal/interface/EcalGainRatiosXMLTranslator.h"
0015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0016 #include "CondTools/Ecal/interface/XMLTags.h"
0017 
0018 using namespace XERCES_CPP_NAMESPACE;
0019 using namespace xuti;
0020 using namespace std;
0021 
0022 int EcalGainRatiosXMLTranslator::readXML(const std::string& filename, EcalCondHeader& header, EcalGainRatios& record) {
0023   cms::concurrency::xercesInitialize();
0024 
0025   XercesDOMParser* parser = new XercesDOMParser;
0026   parser->setValidationScheme(XercesDOMParser::Val_Never);
0027   parser->setDoNamespaces(false);
0028   parser->setDoSchema(false);
0029 
0030   parser->parse(filename.c_str());
0031 
0032   DOMDocument* xmlDoc = parser->getDocument();
0033   if (!xmlDoc) {
0034     std::cout << "EcalGainRatiosXMLTranslator::Error parsing document" << std::endl;
0035     return -1;
0036   }
0037 
0038   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0039 
0040   xuti::readHeader(elementRoot, header);
0041 
0042   DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
0043 
0044   while (cellnode) {
0045     float g12_6 = 0;
0046     float g6_1 = 0;
0047     DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
0048 
0049     DOMNode* g12_6_node = getChildNode(cellnode, Gain12Over6_tag);
0050     GetNodeData(g12_6_node, g12_6);
0051 
0052     DOMNode* g6_1_node = getChildNode(cellnode, Gain6Over1_tag);
0053     GetNodeData(g6_1_node, g6_1);
0054 
0055     record[detid].setGain12Over6(g12_6);
0056     record[detid].setGain6Over1(g6_1);
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 EcalGainRatiosXMLTranslator::writeXML(const std::string& filename,
0070                                           const EcalCondHeader& header,
0071                                           const EcalGainRatios& 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 EcalGainRatiosXMLTranslator::dumpXML(const EcalCondHeader& header, const EcalGainRatios& 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(GainRatios_tag.c_str()).ptr(), doctype);
0091   DOMElement* root = doc->getDocumentElement();
0092 
0093   xuti::writeHeader(root, header);
0094   if (record.barrelItems().empty())
0095     return std::string();
0096   for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
0097     uint32_t rawid = EBDetId::unhashIndex(cellid);
0098 
0099     if (record.find(rawid) == record.end())
0100       continue;
0101     if (!record[rawid].gain12Over6() && !record[rawid].gain6Over1())
0102       continue;
0103 
0104     DOMElement* cellnode = writeCell(root, rawid);
0105 
0106     WriteNodeWithValue(cellnode, Gain12Over6_tag, record[rawid].gain12Over6());
0107     WriteNodeWithValue(cellnode, Gain6Over1_tag, record[rawid].gain6Over1());
0108   }
0109 
0110   if (record.endcapItems().empty())
0111     return std::string();
0112   for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
0113     if (!EEDetId::validHashIndex(cellid))
0114       continue;
0115 
0116     uint32_t rawid = EEDetId::unhashIndex(cellid);
0117 
0118     if (record.find(rawid) == record.end())
0119       continue;
0120     if (!record[rawid].gain12Over6() && !record[rawid].gain6Over1())
0121       continue;
0122 
0123     DOMElement* cellnode = writeCell(root, rawid);
0124 
0125     WriteNodeWithValue(cellnode, Gain12Over6_tag, record[rawid].gain12Over6());
0126     WriteNodeWithValue(cellnode, Gain6Over1_tag, record[rawid].gain6Over1());
0127   }
0128 
0129   std::string dump = cms::xerces::toString(writer->writeToString(root));
0130   doc->release();
0131   doctype->release();
0132   writer->release();
0133 
0134   return dump;
0135 }