Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02: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/EcalPulseShapes.h"
0014 #include "CondTools/Ecal/interface/EcalPulseShapesXMLTranslator.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 EcalPulseShapesXMLTranslator::readXML(const std::string& filename,
0023                                           EcalCondHeader& header,
0024                                           EcalPulseShapes& record) {
0025   cms::concurrency::xercesInitialize();
0026 
0027   XercesDOMParser* parser = new XercesDOMParser;
0028   parser->setValidationScheme(XercesDOMParser::Val_Never);
0029   parser->setDoNamespaces(false);
0030   parser->setDoSchema(false);
0031 
0032   parser->parse(filename.c_str());
0033 
0034   DOMDocument* xmlDoc = parser->getDocument();
0035   if (!xmlDoc) {
0036     std::cout << "EcalPulseShapesXMLTranslator::Error parsing document" << std::endl;
0037     return -1;
0038   }
0039 
0040   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0041 
0042   xuti::readHeader(elementRoot, header);
0043 
0044   DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
0045 
0046   int chan = 0;
0047   while (cellnode) {
0048     //    std::cout << " Channel " << chan << std::endl;
0049     float samples[EcalPulseShape::TEMPLATESAMPLES];
0050 
0051     DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
0052     //    std::cout << " readCell Id Channel " << chan << " tag " << mean12_tag << std::endl;
0053 
0054     DOMNode* sample0_node = getChildNode(cellnode, sample0_tag);
0055     GetNodeData(sample0_node, samples[0]);
0056     //    std::cout << " tag " << sample0_tag << " sample0 " << sample0 << std::endl;
0057 
0058     DOMNode* sample1_node = getChildNode(cellnode, sample1_tag);
0059     GetNodeData(sample1_node, samples[1]);
0060     //    std::cout << " tag " << sample1_tag << " sample1 " << sample1 << std::endl;
0061 
0062     DOMNode* sample2_node = getChildNode(cellnode, sample2_tag);
0063     GetNodeData(sample2_node, samples[2]);
0064     //    std::cout << " tag " << sample2_tag << " sample2 " << sample2 << std::endl;
0065 
0066     DOMNode* sample3_node = getChildNode(cellnode, sample3_tag);
0067     GetNodeData(sample3_node, samples[3]);
0068     //    std::cout << " tag " << sample3_tag << " sample3 " << sample3 << std::endl;
0069 
0070     DOMNode* sample4_node = getChildNode(cellnode, sample4_tag);
0071     GetNodeData(sample4_node, samples[4]);
0072     //    std::cout << " tag " << sample4_tag << " sample4 " << sample4 << std::endl;
0073 
0074     DOMNode* sample5_node = getChildNode(cellnode, sample5_tag);
0075     GetNodeData(sample5_node, samples[5]);
0076     //    std::cout << " tag " << sample5_tag << " sample5 " << sample5 << std::endl;
0077 
0078     DOMNode* sample6_node = getChildNode(cellnode, sample6_tag);
0079     GetNodeData(sample6_node, samples[6]);
0080     //    std::cout << " tag " << sample6_tag << " sample6 " << sample6 << std::endl;
0081 
0082     DOMNode* sample7_node = getChildNode(cellnode, sample7_tag);
0083     GetNodeData(sample7_node, samples[7]);
0084     //    std::cout << " tag " << sample7_tag << " sample7 " << sample7 << std::endl;
0085 
0086     DOMNode* sample8_node = getChildNode(cellnode, sample8_tag);
0087     GetNodeData(sample8_node, samples[8]);
0088     //    std::cout << " tag " << sample8_tag << " sample8 " << sample8 << std::endl;
0089 
0090     DOMNode* sample9_node = getChildNode(cellnode, sample9_tag);
0091     GetNodeData(sample9_node, samples[9]);
0092     //    std::cout << " tag " << sample9_tag << " sample9 " << sample9 << std::endl;
0093 
0094     DOMNode* sample10_node = getChildNode(cellnode, sample10_tag);
0095     GetNodeData(sample10_node, samples[10]);
0096     //    std::cout << " tag " << sample10_tag << " sample10 " << sample10 << std::endl;
0097 
0098     DOMNode* sample11_node = getChildNode(cellnode, sample11_tag);
0099     GetNodeData(sample11_node, samples[11]);
0100     //    std::cout << " tag " << sample11_tag << " sample11 " << sample11 << std::endl;
0101 
0102     for (int s = 0; s < EcalPulseShape::TEMPLATESAMPLES; ++s)
0103       record[detid].pdfval[s] = samples[s];
0104 
0105     cellnode = cellnode->getNextSibling();
0106 
0107     while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
0108       cellnode = cellnode->getNextSibling();
0109     chan++;
0110   }
0111 
0112   delete parser;
0113   cms::concurrency::xercesTerminate();
0114   std::cout << " nb of channels found in xml file " << chan << std::endl;
0115   return 0;
0116 }
0117 
0118 int EcalPulseShapesXMLTranslator::writeXML(const std::string& filename,
0119                                            const EcalCondHeader& header,
0120                                            const EcalPulseShapes& record) {
0121   cms::concurrency::xercesInitialize();
0122 
0123   std::fstream fs(filename.c_str(), ios::out);
0124   fs << dumpXML(header, record);
0125 
0126   cms::concurrency::xercesTerminate();
0127 
0128   return 0;
0129 }
0130 
0131 std::string EcalPulseShapesXMLTranslator::dumpXML(const EcalCondHeader& header, const EcalPulseShapes& record) {
0132   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0133 
0134   DOMLSSerializer* writer = impl->createLSSerializer();
0135   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0136     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0137 
0138   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0139   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(PulseShapes_tag.c_str()).ptr(), doctype);
0140 
0141   DOMElement* root = doc->getDocumentElement();
0142 
0143   xuti::writeHeader(root, header);
0144   if (record.barrelItems().empty())
0145     return std::string();
0146   for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
0147     uint32_t rawid = EBDetId::unhashIndex(cellid);
0148 
0149     if (record.find(rawid) == record.end())
0150       continue;
0151     if (!record[rawid].pdfval[5])
0152       continue;
0153 
0154     DOMElement* cellnode = writeCell(root, rawid);
0155 
0156     WriteNodeWithValue(cellnode, sample0_tag, record[rawid].pdfval[0]);
0157     WriteNodeWithValue(cellnode, sample1_tag, record[rawid].pdfval[1]);
0158     WriteNodeWithValue(cellnode, sample2_tag, record[rawid].pdfval[2]);
0159     WriteNodeWithValue(cellnode, sample3_tag, record[rawid].pdfval[3]);
0160     WriteNodeWithValue(cellnode, sample4_tag, record[rawid].pdfval[4]);
0161     WriteNodeWithValue(cellnode, sample5_tag, record[rawid].pdfval[5]);
0162     WriteNodeWithValue(cellnode, sample6_tag, record[rawid].pdfval[6]);
0163     WriteNodeWithValue(cellnode, sample7_tag, record[rawid].pdfval[7]);
0164     WriteNodeWithValue(cellnode, sample8_tag, record[rawid].pdfval[8]);
0165     WriteNodeWithValue(cellnode, sample9_tag, record[rawid].pdfval[9]);
0166     WriteNodeWithValue(cellnode, sample10_tag, record[rawid].pdfval[10]);
0167     WriteNodeWithValue(cellnode, sample11_tag, record[rawid].pdfval[11]);
0168   }
0169 
0170   if (record.endcapItems().empty())
0171     return std::string();
0172   for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
0173     if (!EEDetId::validHashIndex(cellid))
0174       continue;
0175 
0176     uint32_t rawid = EEDetId::unhashIndex(cellid);
0177 
0178     if (record.find(rawid) == record.end())
0179       continue;
0180     if (!record[rawid].pdfval[5])
0181       continue;
0182 
0183     DOMElement* cellnode = writeCell(root, rawid);
0184 
0185     WriteNodeWithValue(cellnode, sample0_tag, record[rawid].pdfval[0]);
0186     WriteNodeWithValue(cellnode, sample1_tag, record[rawid].pdfval[1]);
0187     WriteNodeWithValue(cellnode, sample2_tag, record[rawid].pdfval[2]);
0188     WriteNodeWithValue(cellnode, sample3_tag, record[rawid].pdfval[3]);
0189     WriteNodeWithValue(cellnode, sample4_tag, record[rawid].pdfval[4]);
0190     WriteNodeWithValue(cellnode, sample5_tag, record[rawid].pdfval[5]);
0191     WriteNodeWithValue(cellnode, sample6_tag, record[rawid].pdfval[6]);
0192     WriteNodeWithValue(cellnode, sample7_tag, record[rawid].pdfval[7]);
0193     WriteNodeWithValue(cellnode, sample8_tag, record[rawid].pdfval[8]);
0194     WriteNodeWithValue(cellnode, sample9_tag, record[rawid].pdfval[9]);
0195     WriteNodeWithValue(cellnode, sample10_tag, record[rawid].pdfval[10]);
0196     WriteNodeWithValue(cellnode, sample11_tag, record[rawid].pdfval[11]);
0197   }
0198 
0199   std::string dump = cms::xerces::toString(writer->writeToString(root));
0200   doc->release();
0201   doctype->release();
0202   writer->release();
0203 
0204   return dump;
0205 }