CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/CondTools/Ecal/src/EcalFloatCondObjectContainerXMLTranslator.cc

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