Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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