CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/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 
00108 std::string 
00109 EcalFloatCondObjectContainerXMLTranslator::dumpXML(       
00110                                   const EcalCondHeader&   header,
00111                                   const std::vector<float>& eb,
00112                                   const std::vector<float>& ee){
00113 
00114 
00115   if (eb.size() != EBDetId::kSizeForDenseIndexing){
00116     std::cerr<<"Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Barrel array size: "
00117              <<eb.size() << " should be "<<  EBDetId::kSizeForDenseIndexing<< std::endl;
00118     return std::string("");
00119   }
00120 
00121   if (ee.size() != EEDetId::kSizeForDenseIndexing){
00122     std::cerr<<"Error in EcalFloatCondObjectContainerXMLTranslator::dumpXML, invalid Endcap array size: "
00123              <<ee.size() << " should be "<<  EEDetId::kSizeForDenseIndexing<< std::endl;
00124     return std::string("");
00125   }
00126 
00127   EcalFloatCondObjectContainer record;
00128   
00129   for (int cellid = 0; 
00130        cellid < EBDetId::kSizeForDenseIndexing; 
00131        ++cellid){// loop on EB cells
00132         
00133     uint32_t rawid = EBDetId::unhashIndex(cellid);
00134     record[rawid]   = eb[cellid];
00135   } 
00136   
00137   for (int cellid = 0; 
00138        cellid < EEDetId::kSizeForDenseIndexing; 
00139        ++cellid){// loop on EE cells
00140     
00141     
00142     
00143     if (EEDetId::validHashIndex(cellid)){  
00144       uint32_t rawid = EEDetId::unhashIndex(cellid);
00145    
00146       record[rawid]=ee[cellid];
00147     } // if
00148   }
00149   
00150   return dumpXML(header,record);
00151 
00152 
00153 }
00154 
00155 std::string 
00156 EcalFloatCondObjectContainerXMLTranslator::dumpXML(       
00157                                   const EcalCondHeader&   header,
00158                                   const EcalFloatCondObjectContainer& record){
00159     
00160 
00161 
00162   
00163   XMLPlatformUtils::Initialize();
00164   
00165   DOMImplementation*  impl =
00166     DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00167   
00168   DOMWriter* writer =
00169     static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00170   writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00171   
00172   DOMDocumentType* doctype = 
00173     impl->createDocumentType( fromNative("XML").c_str(), 0, 0 );
00174   DOMDocument *    doc = 
00175     impl->createDocument( 0, fromNative(EcalFloatCondObjectContainer_tag).c_str(), doctype );
00176   
00177   
00178   doc->setEncoding(fromNative("UTF-8").c_str() );
00179   doc->setStandalone(true);
00180   doc->setVersion(fromNative("1.0").c_str() );
00181   
00182   
00183   DOMElement* root = doc->getDocumentElement();
00184   xuti::writeHeader(root, header);
00185 
00186   for (int cellid = EBDetId::MIN_HASH; 
00187        cellid < EBDetId::kSizeForDenseIndexing; 
00188        ++cellid){// loop on EB cells
00189     
00190     uint32_t rawid= EBDetId::unhashIndex(cellid);
00191     EcalFloatCondObjectContainer::const_iterator value_ptr=
00192       record.find(rawid);
00193     if (value_ptr==record.end()) continue; // cell absent from original record
00194     
00195     DOMElement* cellnode=writeCell(root,rawid);
00196 
00197     WriteNodeWithValue(cellnode,Value_tag,*value_ptr);
00198  
00199 
00200   } // loop on EB cells
00201   
00202   
00203   
00204   for (int cellid = 0; 
00205        cellid < EEDetId::kSizeForDenseIndexing; 
00206        ++cellid){// loop on EE cells
00207     
00208     if (!EEDetId::validHashIndex(cellid)) continue;
00209 
00210     uint32_t rawid= EEDetId::unhashIndex(cellid);
00211     EcalFloatCondObjectContainer::const_iterator value_ptr=
00212       record.find(rawid);
00213     if (value_ptr==record.end()) continue; // cell absent from original record
00214     
00215 
00216     DOMElement* cellnode= writeCell(root,rawid);
00217     WriteNodeWithValue(cellnode,Value_tag,*value_ptr);
00218 
00219     
00220   } // loop on EE cells
00221   
00222   
00223   std::string dump= toNative(writer->writeToString(*root));
00224   doc->release();
00225   return dump;
00226 }
00227 
00228 
00229 
00230 int 
00231 EcalFloatCondObjectContainerXMLTranslator::writeXML(const std::string& filename,         
00232                                                const EcalCondHeader&   header,
00233                                                const EcalFloatCondObjectContainer& record){
00234 
00235   std::fstream fs(filename.c_str(),ios::out);
00236   fs<< dumpXML(header,record);
00237   return 0;  
00238 }