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/EcalPedestals.h"
0014 #include "CondTools/Ecal/interface/EcalPedestalsXMLTranslator.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 EcalPedestalsXMLTranslator::readXML(const std::string& filename, EcalCondHeader& header, EcalPedestals& 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 << "EcalPedestalsXMLTranslator::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   int chan = 0;
0045   while (cellnode) {
0046     //    std::cout << " Channel " << chan << std::endl;
0047     float mean12 = 0;
0048     float mean6 = 0;
0049     float mean1 = 0;
0050     float rms12 = 0;
0051     float rms6 = 0;
0052     float rms1 = 0;
0053     DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
0054     //    std::cout << " readCell Id Channel " << chan << " tag " << mean12_tag << std::endl;
0055 
0056     DOMNode* mean12_node = getChildNode(cellnode, mean12_tag);
0057     GetNodeData(mean12_node, mean12);
0058     //    std::cout << " tag " << mean12_tag << " mean12 " << mean12 << std::endl;
0059 
0060     DOMNode* mean6_node = getChildNode(cellnode, mean6_tag);
0061     GetNodeData(mean6_node, mean6);
0062     //    std::cout << " tag " << mean6_tag << " mean6 " << mean6 << std::endl;
0063 
0064     DOMNode* mean1_node = getChildNode(cellnode, mean1_tag);
0065     GetNodeData(mean1_node, mean1);
0066     //    std::cout << " tag " << mean1_tag << " mean1 " << mean1 << std::endl;
0067 
0068     DOMNode* rms12_node = getChildNode(cellnode, rms12_tag);
0069     GetNodeData(rms12_node, rms12);
0070     //    std::cout << " tag 12 " << rms12_tag << " rms 12 " << rms12 << std::endl;
0071 
0072     DOMNode* rms6_node = getChildNode(cellnode, rms6_tag);
0073     GetNodeData(rms6_node, rms6);
0074     //    std::cout << " tag 6 " << rms6_tag << " rms 6 " << rms6 << std::endl;
0075 
0076     DOMNode* rms1_node = getChildNode(cellnode, rms1_tag);
0077     //    std::cout << " tag 1 " << rms1_tag << std::endl;
0078 
0079     GetNodeData(rms1_node, rms1);
0080     //    std::cout << " Channel " << chan << " mean12 " << mean12 << " rms 1 " << rms1 << std::endl;
0081     record[detid].mean_x12 = mean12;
0082     record[detid].mean_x6 = mean6;
0083     record[detid].mean_x1 = mean1;
0084     record[detid].rms_x12 = rms12;
0085     record[detid].rms_x6 = rms6;
0086     record[detid].rms_x1 = rms1;
0087 
0088     cellnode = cellnode->getNextSibling();
0089 
0090     while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
0091       cellnode = cellnode->getNextSibling();
0092     chan++;
0093   }
0094 
0095   delete parser;
0096   cms::concurrency::xercesTerminate();
0097   std::cout << " nb of channels found in xml file " << chan << std::endl;
0098   return 0;
0099 }
0100 
0101 int EcalPedestalsXMLTranslator::writeXML(const std::string& filename,
0102                                          const EcalCondHeader& header,
0103                                          const EcalPedestals& record) {
0104   cms::concurrency::xercesInitialize();
0105 
0106   std::fstream fs(filename.c_str(), ios::out);
0107   fs << dumpXML(header, record);
0108 
0109   cms::concurrency::xercesTerminate();
0110 
0111   return 0;
0112 }
0113 
0114 std::string EcalPedestalsXMLTranslator::dumpXML(const EcalCondHeader& header, const EcalPedestals& record) {
0115   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0116 
0117   DOMLSSerializer* writer = impl->createLSSerializer();
0118   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0119     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0120 
0121   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0122   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(Pedestals_tag.c_str()).ptr(), doctype);
0123 
0124   DOMElement* root = doc->getDocumentElement();
0125 
0126   xuti::writeHeader(root, header);
0127   if (record.barrelItems().empty())
0128     return std::string();
0129   for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
0130     uint32_t rawid = EBDetId::unhashIndex(cellid);
0131 
0132     if (record.find(rawid) == record.end())
0133       continue;
0134     if (!record[rawid].mean_x12 && !record[rawid].rms_x12)
0135       continue;
0136 
0137     DOMElement* cellnode = writeCell(root, rawid);
0138 
0139     WriteNodeWithValue(cellnode, mean12_tag, record[rawid].mean_x12);
0140     WriteNodeWithValue(cellnode, mean6_tag, record[rawid].mean_x6);
0141     WriteNodeWithValue(cellnode, mean1_tag, record[rawid].mean_x1);
0142     WriteNodeWithValue(cellnode, rms12_tag, record[rawid].rms_x12);
0143     WriteNodeWithValue(cellnode, rms6_tag, record[rawid].rms_x6);
0144     WriteNodeWithValue(cellnode, rms1_tag, record[rawid].rms_x1);
0145   }
0146 
0147   if (record.endcapItems().empty())
0148     return std::string();
0149   for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
0150     if (!EEDetId::validHashIndex(cellid))
0151       continue;
0152 
0153     uint32_t rawid = EEDetId::unhashIndex(cellid);
0154 
0155     if (record.find(rawid) == record.end())
0156       continue;
0157     if (!record[rawid].mean_x12 && !record[rawid].rms_x12)
0158       continue;
0159 
0160     DOMElement* cellnode = writeCell(root, rawid);
0161 
0162     WriteNodeWithValue(cellnode, mean12_tag, record[rawid].mean_x12);
0163     WriteNodeWithValue(cellnode, mean6_tag, record[rawid].mean_x6);
0164     WriteNodeWithValue(cellnode, mean1_tag, record[rawid].mean_x1);
0165     WriteNodeWithValue(cellnode, rms12_tag, record[rawid].rms_x12);
0166     WriteNodeWithValue(cellnode, rms6_tag, record[rawid].rms_x6);
0167     WriteNodeWithValue(cellnode, rms1_tag, record[rawid].rms_x1);
0168   }
0169 
0170   std::string dump = cms::xerces::toString(writer->writeToString(root));
0171   doc->release();
0172   doctype->release();
0173   writer->release();
0174 
0175   return dump;
0176 }