CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CondTools/Ecal/src/EcalGainRatiosXMLTranslator.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <sstream>
00003 #include <fstream>
00004 #include <xercesc/dom/DOMNode.hpp>
00005 #include <xercesc/dom/DOM.hpp>
00006 #include <xercesc/parsers/XercesDOMParser.hpp>
00007 #include <xercesc/util/PlatformUtils.hpp>
00008 #include <xercesc/util/XMLString.hpp>
00009 #include <xercesc/sax/SAXException.hpp>
00010 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00011 
00012 
00013 #include "CondFormats/EcalObjects/interface/EcalGainRatios.h"
00014 #include "CondTools/Ecal/interface/EcalGainRatiosXMLTranslator.h"
00015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00016 #include "CondTools/Ecal/interface/XMLTags.h"
00017 
00018 
00019 using namespace XERCES_CPP_NAMESPACE;
00020 using namespace xuti;
00021 using namespace std;
00022 
00023  
00024 
00025 
00026 
00027 
00028 int  EcalGainRatiosXMLTranslator::readXML(const std::string& filename, 
00029                                           EcalCondHeader& header,
00030                                           EcalGainRatios& record){
00031 
00032  
00033 
00034   XMLPlatformUtils::Initialize();
00035 
00036   XercesDOMParser* parser = new XercesDOMParser;
00037   parser->setValidationScheme( XercesDOMParser::Val_Never );
00038   parser->setDoNamespaces( false );
00039   parser->setDoSchema( false );
00040   
00041   parser->parse(filename.c_str());
00042 
00043   DOMDocument* xmlDoc = parser->getDocument();
00044   if (!xmlDoc) {
00045     std::cout << "EcalGainRatiosXMLTranslator::Error parsing document" << std::endl;
00046     return -1;
00047   }
00048 
00049   DOMElement* elementRoot = xmlDoc->getDocumentElement();
00050 
00051   xuti::readHeader(elementRoot,header);
00052 
00053   DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
00054 
00055   while(cellnode)
00056     {
00057       float g12_6 = 0;
00058       float g6_1 = 0;
00059       DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
00060 
00061       DOMNode* g12_6_node = getChildNode(cellnode,Gain12Over6_tag);
00062       GetNodeData(g12_6_node,g12_6);
00063 
00064       DOMNode* g6_1_node = getChildNode(cellnode,Gain6Over1_tag);
00065       GetNodeData(g6_1_node,g6_1);
00066 
00067       record[detid].setGain12Over6(g12_6);
00068       record[detid].setGain6Over1(g6_1);
00069 
00070       cellnode = cellnode->getNextSibling();
00071 
00072       while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
00073         cellnode = cellnode->getNextSibling();
00074 
00075     } 
00076 
00077   delete parser;
00078   XMLPlatformUtils::Terminate();
00079   return 0;
00080   
00081   
00082 }
00083   
00084 
00085 
00086 
00087 
00088 int EcalGainRatiosXMLTranslator::writeXML(const std::string& filename, 
00089                                           const EcalCondHeader& header,
00090                                           const EcalGainRatios& record){
00091   std::fstream fs(filename.c_str(),ios::out);
00092   fs<< dumpXML(header,record);
00093   return 0;  
00094 }
00095 
00096 
00097 std::string EcalGainRatiosXMLTranslator::dumpXML(const EcalCondHeader& header,const EcalGainRatios& record){
00098 
00099     XMLPlatformUtils::Initialize();
00100 
00101     DOMImplementation*  impl =
00102       DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00103 
00104     DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00105     writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00106 
00107     DOMDocumentType* doctype = impl->createDocumentType(fromNative("XML").c_str(), 0, 0 );
00108     DOMDocument *    doc = 
00109          impl->createDocument( 0, fromNative(GainRatios_tag).c_str(), doctype );
00110 
00111 
00112     doc->setEncoding(fromNative("UTF-8").c_str() );
00113     doc->setStandalone(true);
00114     doc->setVersion(fromNative("1.0").c_str() );
00115 
00116     
00117     DOMElement* root = doc->getDocumentElement();
00118  
00119 
00120 
00121     xuti::writeHeader(root,header);
00122     if (!record.barrelItems().size()) return std::string();
00123     for(int cellid = EBDetId::MIN_HASH;
00124         cellid < EBDetId::kSizeForDenseIndexing;
00125         ++cellid)
00126       {
00127          
00128         uint32_t rawid = EBDetId::unhashIndex(cellid);
00129 
00130         if (record.find(rawid) == record.end()) continue;
00131         if(!record[rawid].gain12Over6() && !record[rawid].gain6Over1()) continue;
00132           
00133         DOMElement* cellnode=writeCell(root,rawid);
00134 
00135         WriteNodeWithValue(cellnode,Gain12Over6_tag,record[rawid].gain12Over6());
00136         WriteNodeWithValue(cellnode,Gain6Over1_tag,record[rawid].gain6Over1());
00137       }
00138 
00139     if (!record.endcapItems().size()) return std::string();
00140     for(int cellid = 0;
00141         cellid < EEDetId::kSizeForDenseIndexing;
00142         ++cellid)
00143       {
00144         if(!EEDetId::validHashIndex(cellid)) continue;
00145 
00146         uint32_t rawid = EEDetId::unhashIndex(cellid); 
00147 
00148         if (record.find(rawid) == record.end()) continue;
00149         if(!record[rawid].gain12Over6() && !record[rawid].gain6Over1()) continue;
00150 
00151         DOMElement* cellnode=writeCell(root,rawid);
00152 
00153         WriteNodeWithValue(cellnode,Gain12Over6_tag,record[rawid].gain12Over6());
00154         WriteNodeWithValue(cellnode,Gain6Over1_tag,record[rawid].gain6Over1());
00155           
00156           
00157       }
00158 
00159  
00160     std::string dump= toNative(writer->writeToString(*root)); 
00161     doc->release(); 
00162     return dump;
00163 }