CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CondTools/Ecal/src/EcalChannelStatusXMLTranslator.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/EcalChannelStatus.h"
00014 #include "CondTools/Ecal/interface/EcalChannelStatusXMLTranslator.h"
00015 
00016 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00017 #include "CondTools/Ecal/interface/XMLTags.h"
00018 
00019 using namespace XERCES_CPP_NAMESPACE;
00020 using namespace xuti;
00021 using namespace std;
00022 
00023 
00024 
00025 
00026 
00027 int  EcalChannelStatusXMLTranslator::readXML(const std::string& filename, 
00028                                              EcalCondHeader& header,
00029                                              EcalChannelStatus& record){
00030 
00031 
00032 
00033   XMLPlatformUtils::Initialize();
00034 
00035   XercesDOMParser* parser = new XercesDOMParser;
00036   parser->setValidationScheme( XercesDOMParser::Val_Never );
00037   parser->setDoNamespaces( false );
00038   parser->setDoSchema( false );
00039   
00040   parser->parse(filename.c_str());
00041 
00042   DOMDocument* xmlDoc = parser->getDocument();
00043   if (!xmlDoc) {
00044     std::cout << "EcalChannelStatusXMLTranslator::Error parsing document" << std::endl;
00045     return -1;
00046   }
00047 
00048   DOMElement* elementRoot = xmlDoc->getDocumentElement();
00049 
00050   xuti::readHeader(elementRoot,header);
00051 
00052   DOMNode * cellnode = getChildNode(elementRoot,Cell_tag);
00053 
00054   while(cellnode)
00055     {
00056       uint16_t csc = 0;
00057 
00058       DetId detid = readCellId(dynamic_cast<DOMElement*>(cellnode));
00059 
00060       DOMNode* c_node = getChildNode(cellnode,ChannelStatusCode_tag);
00061       GetNodeData(c_node,csc);
00062 
00063       EcalChannelStatusCode ecalCSC = EcalChannelStatusCode(csc);
00064       record[detid] = ecalCSC;
00065 
00066       cellnode = cellnode->getNextSibling();
00067 
00068       while(cellnode && cellnode->getNodeType() != DOMNode::ELEMENT_NODE)
00069         cellnode = cellnode->getNextSibling();
00070 
00071  
00072     }  
00073 
00074   delete parser;
00075   XMLPlatformUtils::Terminate();
00076   return 0;
00077   
00078   
00079 }
00080   
00081 
00082 
00083 
00084 
00085   int EcalChannelStatusXMLTranslator::writeXML(const std::string& filename, 
00086                                                const EcalCondHeader& header,
00087                                                const EcalChannelStatus& record){
00088     std::fstream fs(filename.c_str(),ios::out);
00089     fs<< dumpXML(header,record);
00090     return 0;
00091   }
00092 
00093 
00094 std::string EcalChannelStatusXMLTranslator::dumpXML(
00095                                              const EcalCondHeader& header,
00096                                              const EcalChannelStatus& record){
00097  
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(ChannelStatus_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     xuti::writeHeader(root,header);
00120 
00121     for(int cellid = EBDetId::MIN_HASH;
00122         cellid < EBDetId::kSizeForDenseIndexing;
00123         ++cellid)
00124       {
00125 
00126         uint32_t rawid = EBDetId::unhashIndex(cellid);
00127 
00128         if(!record[rawid].getStatusCode()) continue;
00129       
00130 
00131         DOMElement* cellnode = writeCell(root,rawid);     
00132 
00133         WriteNodeWithValue(cellnode,ChannelStatusCode_tag,record[rawid].getStatusCode());
00134 
00135                           
00136       }
00137 
00138 
00139 
00140    
00141     for(int cellid = 0;
00142         cellid < EEDetId::kSizeForDenseIndexing;
00143         ++cellid)
00144       {
00145           
00146         if(!EEDetId::validHashIndex(cellid)) continue;
00147           
00148         uint32_t rawid = EEDetId::unhashIndex(cellid); 
00149 
00150         if(!record[rawid].getStatusCode()) continue;
00151 
00152         DOMElement* cellnode = writeCell(root,rawid);
00153         WriteNodeWithValue(cellnode,ChannelStatusCode_tag,record[rawid].getStatusCode());
00154           
00155       }
00156     
00157 
00158     std::string dump= toNative(writer->writeToString(*root)); 
00159      doc->release();
00160     //   XMLPlatformUtils::Terminate();
00161 
00162     return dump;
00163 
00164 }