Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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/EcalTimeBiasCorrections.h"
0014 #include "CondTools/Ecal/interface/EcalTimeBiasCorrectionsXMLTranslator.h"
0015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0016 
0017 using namespace XERCES_CPP_NAMESPACE;
0018 using namespace xuti;
0019 using namespace std;
0020 
0021 int EcalTimeBiasCorrectionsXMLTranslator::readXML(const std::string& filename,
0022                                                   EcalCondHeader& header,
0023                                                   EcalTimeBiasCorrections& record) {
0024   cms::concurrency::xercesInitialize();
0025 
0026   XercesDOMParser* parser = new XercesDOMParser;
0027   parser->setValidationScheme(XercesDOMParser::Val_Never);
0028   parser->setDoNamespaces(false);
0029   parser->setDoSchema(false);
0030 
0031   parser->parse(filename.c_str());
0032 
0033   DOMDocument* xmlDoc = parser->getDocument();
0034   if (!xmlDoc) {
0035     std::cout << "EcalTimeBiasCorrectionsXMLTranslator::Error parsing document" << std::endl;
0036     return -1;
0037   }
0038 
0039   // Get the top-level element
0040   DOMElement* elementRoot = xmlDoc->getDocumentElement();
0041 
0042   xuti::readHeader(elementRoot, header);
0043 
0044   delete parser;
0045   cms::concurrency::xercesTerminate();
0046   return 0;
0047 }
0048 
0049 int EcalTimeBiasCorrectionsXMLTranslator::writeXML(const std::string& filename,
0050                                                    const EcalCondHeader& header,
0051                                                    const EcalTimeBiasCorrections& record) {
0052   cms::concurrency::xercesInitialize();
0053 
0054   std::fstream fs(filename.c_str(), ios::out);
0055   fs << dumpXML(header, record);
0056 
0057   cms::concurrency::xercesTerminate();
0058 
0059   return 0;
0060 }
0061 
0062 std::string EcalTimeBiasCorrectionsXMLTranslator::dumpXML(const EcalCondHeader& header,
0063                                                           const EcalTimeBiasCorrections& record) {
0064   unique_ptr<DOMImplementation> impl(DOMImplementationRegistry::getDOMImplementation(cms::xerces::uStr("LS").ptr()));
0065 
0066   DOMLSSerializer* writer = impl->createLSSerializer();
0067   if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
0068     writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
0069 
0070   DOMDocumentType* doctype = impl->createDocumentType(cms::xerces::uStr("XML").ptr(), nullptr, nullptr);
0071   DOMDocument* doc = impl->createDocument(nullptr, cms::xerces::uStr(IntercalibConstants_tag.c_str()).ptr(), doctype);
0072   DOMElement* root = doc->getDocumentElement();
0073 
0074   xuti::writeHeader(root, header);
0075 
0076   std::vector<float> vect = record.EBTimeCorrAmplitudeBins;
0077   std::vector<float>::iterator it;
0078 
0079   std::string ETCAB_tag = "EBTimeCorrAmplitudeBins";
0080   //  std::cout << ETCAB_tag << vect.size()<< "\n";
0081   DOMElement* ETCAB = root->getOwnerDocument()->createElement(cms::xerces::uStr(ETCAB_tag.c_str()).ptr());
0082   root->appendChild(ETCAB);
0083   for (it = vect.begin(); it != vect.end(); it++) {
0084     //    std::cout << *it << " ";
0085     WriteNodeWithValue(ETCAB, Value_tag, *it);
0086   }
0087   //  std::cout << "\n";
0088   vect = record.EBTimeCorrShiftBins;
0089   ETCAB_tag = "EBTimeCorrShiftBins";
0090   //  std::cout << ETCAB_tag << vect.size()<< "\n";
0091   ETCAB = root->getOwnerDocument()->createElement(cms::xerces::uStr(ETCAB_tag.c_str()).ptr());
0092   root->appendChild(ETCAB);
0093   for (it = vect.begin(); it != vect.end(); it++) {
0094     //    std::cout << *it << " ";
0095     WriteNodeWithValue(ETCAB, Value_tag, *it);
0096   }
0097   //  std::cout << "\n";
0098   vect = record.EETimeCorrAmplitudeBins;
0099   ETCAB_tag = "EETimeCorrAmplitudeBins";
0100   //  std::cout << ETCAB_tag << vect.size()<< "\n";
0101   ETCAB = root->getOwnerDocument()->createElement(cms::xerces::uStr(ETCAB_tag.c_str()).ptr());
0102   root->appendChild(ETCAB);
0103   for (it = vect.begin(); it != vect.end(); it++) {
0104     //    std::cout << *it << " ";
0105     WriteNodeWithValue(ETCAB, Value_tag, *it);
0106   }
0107   //  std::cout << "\n";
0108   vect = record.EETimeCorrShiftBins;
0109   ETCAB_tag = "EETimeCorrShiftBins";
0110   //  std::cout << ETCAB_tag << vect.size()<< "\n";
0111   ETCAB = root->getOwnerDocument()->createElement(cms::xerces::uStr(ETCAB_tag.c_str()).ptr());
0112   root->appendChild(ETCAB);
0113   for (it = vect.begin(); it != vect.end(); it++) {
0114     //    std::cout << *it << " ";
0115     WriteNodeWithValue(ETCAB, Value_tag, *it);
0116   }
0117   //  std::cout << "\n";
0118 
0119   std::string dump = cms::xerces::toString(writer->writeToString(root));
0120   doc->release();
0121   doctype->release();
0122   writer->release();
0123 
0124   return dump;
0125 }