Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondFormats/EcalObjects/interface/EcalADCToGeVConstant.h"
0002 #include "CondTools/Ecal/interface/EcalADCToGeVXMLTranslator.h"
0003 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0004 #include "Utilities/Xerces/interface/Xerces.h"
0005 #include "Utilities/Xerces/interface/XercesStrUtils.h"
0006 #include <xercesc/dom/DOMNode.hpp>
0007 #include <xercesc/dom/DOM.hpp>
0008 #include <xercesc/parsers/XercesDOMParser.hpp>
0009 #include <xercesc/util/XMLString.hpp>
0010 #include <xercesc/sax/SAXException.hpp>
0011 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0012 #include <iostream>
0013 #include <sstream>
0014 #include <fstream>
0015 
0016 using namespace XERCES_CPP_NAMESPACE;
0017 using namespace xuti;
0018 using namespace std;
0019 
0020 int EcalADCToGeVXMLTranslator::readXML(const std::string& filename,
0021                                        EcalCondHeader& header,
0022                                        EcalADCToGeVConstant& 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 << "EcalADCToGeVXMLTranslator::Error parsing document" << std::endl;
0035     return -1;
0036   }
0037 
0038   // Get the top-level element
0039   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0040 
0041   xuti::readHeader(elementRoot, header);
0042 
0043   DOMNode* barrelnode = xuti::getChildNode(elementRoot, Barrel_tag);
0044   DOMNode* endcapnode = xuti::getChildNode(elementRoot, Endcap_tag);
0045 
0046   double barrelvalue = 0;
0047   double endcapvalue = 0;
0048 
0049   xuti::GetNodeData(barrelnode, barrelvalue);
0050   xuti::GetNodeData(endcapnode, endcapvalue);
0051 
0052   record.setEBValue(barrelvalue);
0053   record.setEEValue(endcapvalue);
0054 
0055   delete parser;
0056   cms::concurrency::xercesTerminate();
0057   return 0;
0058 }
0059 
0060 int EcalADCToGeVXMLTranslator::writeXML(const std::string& filename,
0061                                         const EcalCondHeader& header,
0062                                         const EcalADCToGeVConstant& record) {
0063   cms::concurrency::xercesInitialize();
0064 
0065   std::fstream fs(filename.c_str(), ios::out);
0066   fs << dumpXML(header, record);
0067 
0068   cms::concurrency::xercesTerminate();
0069 
0070   return 0;
0071 }
0072 
0073 std::string EcalADCToGeVXMLTranslator::dumpXML(const EcalCondHeader& header, const EcalADCToGeVConstant& record) {
0074   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0075 
0076   DOMLSSerializer* writer = impl->createLSSerializer();
0077   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0078     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0079 
0080   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0081   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(ADCToGeVConstant_tag.c_str()).ptr(), doctype);
0082 
0083   DOMElement* root = doc->getDocumentElement();
0084 
0085   xuti::writeHeader(root, header);
0086 
0087   xuti::WriteNodeWithValue(root, Barrel_tag, record.getEBValue());
0088   xuti::WriteNodeWithValue(root, Endcap_tag, record.getEEValue());
0089 
0090   std::string dump = cms::xerces::toString(writer->writeToString(root));
0091   doc->release();
0092   doctype->release();
0093   writer->release();
0094 
0095   return dump;
0096 }