Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <iostream>
0002 #include <sstream>
0003 #include <fstream>
0004 #include <vector>
0005 #include <xercesc/dom/DOMNode.hpp>
0006 #include <xercesc/dom/DOM.hpp>
0007 #include <xercesc/parsers/XercesDOMParser.hpp>
0008 #include "Utilities/Xerces/interface/Xerces.h"
0009 #include "Utilities/Xerces/interface/XercesStrUtils.h"
0010 #include <xercesc/util/XMLString.hpp>
0011 #include <xercesc/sax/SAXException.hpp>
0012 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0013 #include <TString.h>
0014 
0015 #include "CondFormats/EcalObjects/interface/EcalPulseSymmCovariances.h"
0016 #include "CondTools/Ecal/interface/EcalPulseSymmCovariancesXMLTranslator.h"
0017 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0018 #include "CondTools/Ecal/interface/XMLTags.h"
0019 
0020 using namespace XERCES_CPP_NAMESPACE;
0021 using namespace xuti;
0022 using namespace std;
0023 
0024 int EcalPulseSymmCovariancesXMLTranslator::readXML(const std::string& filename,
0025                                                    EcalCondHeader& header,
0026                                                    EcalPulseSymmCovariances& record) {
0027   cms::concurrency::xercesInitialize();
0028 
0029   XercesDOMParser* parser = new XercesDOMParser;
0030   parser->setValidationScheme(XercesDOMParser::Val_Never);
0031   parser->setDoNamespaces(false);
0032   parser->setDoSchema(false);
0033 
0034   parser->parse(filename.c_str());
0035 
0036   DOMDocument* xmlDoc = parser->getDocument();
0037   if (!xmlDoc) {
0038     std::cout << "EcalPulseSymmCovariancesXMLTranslator::Error parsing document" << std::endl;
0039     return -1;
0040   }
0041 
0042   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0043 
0044   xuti::readHeader(elementRoot, header);
0045 
0046   DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
0047 
0048   int chan = 0;
0049   while (cellnode) {
0050     //    std::cout << " Channel " << chan << std::endl;
0051     float covs[EcalPulseShape::TEMPLATESAMPLES * (EcalPulseShape::TEMPLATESAMPLES + 1) / 2];
0052 
0053     DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
0054     //    std::cout << " readCell Id Channel " << chan << " tag " << mean12_tag << std::endl;
0055 
0056     std::vector<std::string> pulsecov_tag(
0057         static_cast<size_t>(EcalPulseShape::TEMPLATESAMPLES * (EcalPulseShape::TEMPLATESAMPLES + 1) / 2));
0058     for (int i = 0; i < EcalPulseShape::TEMPLATESAMPLES; ++i) {
0059       for (int j = 0; j < EcalPulseShape::TEMPLATESAMPLES; ++j) {
0060         int k = -1;
0061         if (j >= i)
0062           k = j + (EcalPulseShape::TEMPLATESAMPLES - 1) * i;
0063         else
0064           k = i + (EcalPulseShape::TEMPLATESAMPLES - 1) * j;
0065         pulsecov_tag[k] = Form("samplecov_%d_%d", i, j);
0066       }
0067     }
0068 
0069     DOMNode** covs_node =
0070         new DOMNode*[int(EcalPulseShape::TEMPLATESAMPLES * (EcalPulseShape::TEMPLATESAMPLES + 1) / 2)];
0071     for (int k = 0; k < EcalPulseShape::TEMPLATESAMPLES * (EcalPulseShape::TEMPLATESAMPLES + 1) / 2; ++k) {
0072       covs_node[k] = getChildNode(cellnode, pulsecov_tag[k]);
0073       GetNodeData(covs_node[k], covs[k]);
0074     }
0075 
0076     for (int k = 0; k < EcalPulseShape::TEMPLATESAMPLES * (EcalPulseShape::TEMPLATESAMPLES + 1) / 2; ++k)
0077       record[detid].covval[k] = covs[k];
0078 
0079     cellnode = cellnode->getNextSibling();
0080 
0081     while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
0082       cellnode = cellnode->getNextSibling();
0083     chan++;
0084   }
0085 
0086   delete parser;
0087   cms::concurrency::xercesTerminate();
0088   std::cout << " nb of channels found in xml file " << chan << std::endl;
0089   return 0;
0090 }
0091 
0092 int EcalPulseSymmCovariancesXMLTranslator::writeXML(const std::string& filename,
0093                                                     const EcalCondHeader& header,
0094                                                     const EcalPulseSymmCovariances& record) {
0095   cms::concurrency::xercesInitialize();
0096 
0097   std::fstream fs(filename.c_str(), ios::out);
0098   fs << dumpXML(header, record);
0099 
0100   cms::concurrency::xercesTerminate();
0101 
0102   return 0;
0103 }
0104 
0105 std::string EcalPulseSymmCovariancesXMLTranslator::dumpXML(const EcalCondHeader& header,
0106                                                            const EcalPulseSymmCovariances& record) {
0107   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0108 
0109   DOMLSSerializer* writer = impl->createLSSerializer();
0110   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0111     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0112 
0113   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0114   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(PulseSymmCovariances_tag.c_str()).ptr(), doctype);
0115 
0116   DOMElement* root = doc->getDocumentElement();
0117 
0118   std::vector<std::string> pulsecov_tag(static_cast<size_t>(std::pow(EcalPulseShape::TEMPLATESAMPLES, 2)));
0119   for (int k = 0; k < std::pow(EcalPulseShape::TEMPLATESAMPLES, 2); ++k) {
0120     int i = k / EcalPulseShape::TEMPLATESAMPLES;
0121     int j = k % EcalPulseShape::TEMPLATESAMPLES;
0122     int linK = -1;
0123     if (j >= i)
0124       linK = j + (EcalPulseShape::TEMPLATESAMPLES - 1) * i;
0125     else
0126       linK = i + (EcalPulseShape::TEMPLATESAMPLES - 1) * j;
0127     pulsecov_tag[linK] = Form("samplecov_%d_%d", i, j);
0128   }
0129 
0130   xuti::writeHeader(root, header);
0131   if (record.barrelItems().empty())
0132     return std::string();
0133   for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
0134     uint32_t rawid = EBDetId::unhashIndex(cellid);
0135 
0136     if (record.find(rawid) == record.end())
0137       continue;
0138 
0139     DOMElement* cellnode = writeCell(root, rawid);
0140 
0141     for (int k = 0; k < std::pow(EcalPulseShape::TEMPLATESAMPLES, 2); ++k) {
0142       WriteNodeWithValue(cellnode, pulsecov_tag[k], record[rawid].covval[k]);
0143     }
0144   }
0145   if (record.endcapItems().empty())
0146     return std::string();
0147   for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
0148     if (!EEDetId::validHashIndex(cellid))
0149       continue;
0150 
0151     uint32_t rawid = EEDetId::unhashIndex(cellid);
0152 
0153     if (record.find(rawid) == record.end())
0154       continue;
0155 
0156     DOMElement* cellnode = writeCell(root, rawid);
0157 
0158     for (int k = 0; k < std::pow(EcalPulseShape::TEMPLATESAMPLES, 2); ++k) {
0159       WriteNodeWithValue(cellnode, pulsecov_tag[k], record[rawid].covval[k]);
0160     }
0161   }
0162 
0163   std::string dump = cms::xerces::toString(writer->writeToString(root));
0164   doc->release();
0165   doctype->release();
0166   writer->release();
0167 
0168   return dump;
0169 }