Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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/EcalLinearCorrections.h"
0014 #include "CondTools/Ecal/interface/EcalLinearCorrectionsXMLTranslator.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 //
0024 // TODO: write and read time map
0025 //
0026 //
0027 
0028 int EcalLinearCorrectionsXMLTranslator::readXML(const std::string& filename,
0029                                                 EcalCondHeader& header,
0030                                                 EcalLinearCorrections& record) {
0031   cms::concurrency::xercesInitialize();
0032 
0033   XercesDOMParser* parser = new XercesDOMParser;
0034   parser->setValidationScheme(XercesDOMParser::Val_Never);
0035   parser->setDoNamespaces(false);
0036   parser->setDoSchema(false);
0037 
0038   parser->parse(filename.c_str());
0039 
0040   DOMDocument* xmlDoc = parser->getDocument();
0041   if (!xmlDoc) {
0042     std::cout << "EcalLinearCorrectionsXMLTranslator::Error parsing document" << std::endl;
0043     return -1;
0044   }
0045 
0046   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0047 
0048   xuti::readHeader(elementRoot, header);
0049 
0050   DOMNode* cellnode = getChildNode(elementRoot, Cell_tag);
0051 
0052   while (cellnode) {
0053     float p1 = 0;
0054     float p2 = 0;
0055     float p3 = 0;
0056     //       edm::TimeStamp t1=0;
0057     //       edm::TimeStamp t2=0;
0058     //       edm::TimeStamp t3=0;
0059 
0060     DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
0061 
0062     DOMNode* p1_node = getChildNode(cellnode, Laser_p1_tag);
0063     GetNodeData(p1_node, p1);
0064 
0065     DOMNode* p2_node = getChildNode(cellnode, Laser_p2_tag);
0066     GetNodeData(p2_node, p2);
0067 
0068     DOMNode* p3_node = getChildNode(cellnode, Laser_p3_tag);
0069     GetNodeData(p3_node, p3);
0070 
0071     //       DOMNode* t1_node = getChildNode(cellnode,Laser_t1_tag);
0072     //       GetNodeData(t1_node,t1);
0073 
0074     //       DOMNode* t2_node = getChildNode(cellnode,Laser_t2_tag);
0075     //       GetNodeData(t3_node,t2);
0076 
0077     //       DOMNode* p1_node = getChildNode(cellnode,Laser_t3_tag);
0078     //       GetNodeData(t3_node,t3);
0079 
0080     EcalLinearCorrections::Values pair;
0081     pair.p1 = p1;
0082     pair.p2 = p2;
0083     pair.p3 = p3;
0084 
0085     record.setValue(detid, pair);
0086 
0087     cellnode = cellnode->getNextSibling();
0088 
0089     while (cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
0090       cellnode = cellnode->getNextSibling();
0091   }
0092 
0093   delete parser;
0094   cms::concurrency::xercesTerminate();
0095   return 0;
0096 }
0097 
0098 int EcalLinearCorrectionsXMLTranslator::writeXML(const std::string& filename,
0099                                                  const EcalCondHeader& header,
0100                                                  const EcalLinearCorrections& record) {
0101   cms::concurrency::xercesInitialize();
0102 
0103   std::fstream fs(filename.c_str(), ios::out);
0104   fs << dumpXML(header, record);
0105 
0106   cms::concurrency::xercesTerminate();
0107 
0108   return 0;
0109 }
0110 
0111 std::string EcalLinearCorrectionsXMLTranslator::dumpXML(const EcalCondHeader& header,
0112                                                         const EcalLinearCorrections& record) {
0113   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0114 
0115   DOMLSSerializer* writer = impl->createLSSerializer();
0116   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0117     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0118 
0119   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0120   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(Laser_tag.c_str()).ptr(), doctype);
0121 
0122   DOMElement* root = doc->getDocumentElement();
0123 
0124   xuti::writeHeader(root, header);
0125 
0126   string Lasertag = "Laser", LMtag = "LM";
0127   for (int cellid = 0; cellid < (int)record.getTimeMap().size(); cellid++) {
0128     DOMElement* cellnode = doc->createElement(cms::xerces::uStr(Lasertag.c_str()).ptr());
0129     root->appendChild(cellnode);
0130     stringstream value_s;
0131     value_s << cellid;
0132     cellnode->setAttribute(cms::xerces::uStr(LMtag.c_str()).ptr(), cms::xerces::uStr(value_s.str().c_str()).ptr());
0133 
0134     long int t123[3];
0135     t123[0] = (record.getTimeMap())[cellid].t1.value();
0136     t123[1] = (record.getTimeMap())[cellid].t2.value();
0137     t123[2] = (record.getTimeMap())[cellid].t3.value();
0138     string Laser_t_tag[3] = {"t1", "t2", "t3"};
0139     for (int i = 0; i < 3; i++) {
0140       time_t t = t123[i] >> 32;
0141       char buf[256];
0142       struct tm lt;
0143       localtime_r(&t, &lt);
0144       strftime(buf, sizeof(buf), "%F %R:%S", &lt);
0145       buf[sizeof(buf) - 1] = 0;
0146       DOMDocument* subdoc = cellnode->getOwnerDocument();
0147       DOMElement* new_node = subdoc->createElement(cms::xerces::uStr(Laser_t_tag[i].c_str()).ptr());
0148       cellnode->appendChild(new_node);
0149       std::stringstream value_ss;
0150       value_ss << t123[i];
0151       string newstr = value_ss.str() + " [" + string(buf) + "]";
0152       DOMText* tvalue = subdoc->createTextNode(cms::xerces::uStr(newstr.c_str()).ptr());
0153       new_node->appendChild(tvalue);
0154     }
0155   }
0156 
0157   for (int cellid = EBDetId::MIN_HASH; cellid < EBDetId::kSizeForDenseIndexing; ++cellid) {
0158     uint32_t rawid = EBDetId::unhashIndex(cellid);
0159 
0160     DOMElement* cellnode = writeCell(root, rawid);
0161 
0162     float p1 = (record.getValueMap())[rawid].p1;
0163     float p2 = (record.getValueMap())[rawid].p2;
0164     float p3 = (record.getValueMap())[rawid].p3;
0165 
0166     WriteNodeWithValue(cellnode, Laser_p1_tag, p1);
0167     WriteNodeWithValue(cellnode, Laser_p2_tag, p2);
0168     WriteNodeWithValue(cellnode, Laser_p3_tag, p3);
0169   }
0170 
0171   for (int cellid = 0; cellid < EEDetId::kSizeForDenseIndexing; ++cellid) {
0172     if (!EEDetId::validHashIndex(cellid))
0173       continue;
0174 
0175     uint32_t rawid = EEDetId::unhashIndex(cellid);
0176 
0177     DOMElement* cellnode = writeCell(root, rawid);
0178 
0179     float p1 = (record.getValueMap())[rawid].p1;
0180     float p2 = (record.getValueMap())[rawid].p2;
0181     float p3 = (record.getValueMap())[rawid].p3;
0182 
0183     WriteNodeWithValue(cellnode, Laser_p1_tag, p1);
0184     WriteNodeWithValue(cellnode, Laser_p2_tag, p2);
0185     WriteNodeWithValue(cellnode, Laser_p3_tag, p3);
0186   }
0187 
0188   std::string dump = cms::xerces::toString(writer->writeToString(root));
0189   doc->release();
0190   doctype->release();
0191   writer->release();
0192 
0193   return dump;
0194 }