CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/CondTools/Ecal/src/EcalWeightGroupXMLTranslator.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 
00014 #include "CondTools/Ecal/interface/EcalWeightGroupXMLTranslator.h"
00015 #include "CondFormats/EcalObjects/interface/EcalXtalGroupId.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 
00024 int  EcalWeightGroupXMLTranslator::readXML(const std::string& filename, 
00025                                            EcalCondHeader& header,
00026                                            EcalWeightXtalGroups& 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   if (!xmlDoc) {
00039     std::cout << "EcalWeightGroupXMLTranslator::Error parsing document" << std::endl;
00040     return -1;
00041   }
00042 
00043   DOMElement* elementRoot = xmlDoc->getDocumentElement();
00044 
00045   xuti::readHeader(elementRoot,header);
00046   // get the first cell node
00047   DOMNode * cellnode=getChildNode(elementRoot,Cell_tag);
00048   
00049   // loop on cell nodes
00050   while  (cellnode){
00051 
00052     unsigned int  group=0;
00053 
00054     // read id
00055     DetId detid= readCellId(dynamic_cast<DOMElement*> (cellnode));
00056        
00057     // read constant
00058     DOMNode * c_node = getChildNode(cellnode,WeightGroup_tag);
00059     GetNodeData(c_node,group);
00060 
00061 
00062     // fill record
00063     record[detid]=EcalXtalGroupId(group);
00064    
00065 
00066     // get next cell
00067     cellnode= cellnode->getNextSibling();
00068     
00069     while (cellnode&& cellnode->getNodeType( ) != DOMNode::ELEMENT_NODE)      
00070       cellnode= cellnode->getNextSibling();
00071     
00072     
00073   }
00074 
00075 
00076   delete parser;
00077   XMLPlatformUtils::Terminate();
00078   return 0;
00079     
00080 }
00081 
00082 
00083 
00084 
00085 
00086 int EcalWeightGroupXMLTranslator::writeXML(const std::string& filename, 
00087                                            const EcalCondHeader& header,
00088                                            const EcalWeightXtalGroups& record){
00089   std::fstream fs(filename.c_str(),ios::out);
00090   fs<< dumpXML(header,record);
00091   return 0; 
00092   
00093 }
00094 
00095 
00096 
00097 std::string 
00098 EcalWeightGroupXMLTranslator::dumpXML(const EcalCondHeader& header,
00099                                       const EcalWeightXtalGroups& record){
00100 
00101   XMLPlatformUtils::Initialize();
00102   
00103   DOMImplementation*  impl =
00104     DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00105   
00106   DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00107   writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00108   
00109   DOMDocumentType* doctype = impl->createDocumentType( fromNative("XML").c_str(), 0, 0 );
00110   DOMDocument *    doc = 
00111     impl->createDocument( 0, fromNative(WeightGroups_tag).c_str(), doctype );
00112   
00113   
00114   doc->setEncoding(fromNative("UTF-8").c_str() );
00115   doc->setStandalone(true);
00116   doc->setVersion(fromNative("1.0").c_str() );
00117   
00118   
00119   DOMElement* root = doc->getDocumentElement();
00120  
00121   xuti::writeHeader(root,header);
00122 
00123   for (int cellid = EBDetId::MIN_HASH; 
00124        cellid < EBDetId::kSizeForDenseIndexing; 
00125        ++cellid){// loop on EB cells
00126     
00127     uint32_t rawid= EBDetId::unhashIndex(cellid);
00128 
00129     //if (!record[rawid]) continue; // cell absent from original record
00130     
00131     DOMElement* cellnode=writeCell(root,rawid);
00132 
00133     WriteNodeWithValue(cellnode,WeightGroup_tag,record[rawid].id());
00134   
00135 
00136   } // loop on EB cells
00137   
00138   
00139   
00140   for (int cellid = 0; 
00141        cellid < EEDetId::kSizeForDenseIndexing; 
00142        ++cellid){// loop on EE cells
00143     
00144     if (!EEDetId::validHashIndex(cellid)) continue;
00145 
00146     uint32_t rawid= EEDetId::unhashIndex(cellid);
00147     //xif (!record[rawid]) continue; // cell absent from original record
00148 
00149         
00150     DOMElement* cellnode=writeCell(root,rawid);
00151     WriteNodeWithValue(cellnode,WeightGroup_tag,record[rawid].id());
00152     
00153   } // loop on EE cells
00154   
00155   
00156   std::string dump= toNative(writer->writeToString(*root));
00157   doc->release();
00158   //   XMLPlatformUtils::Terminate();
00159   
00160   return dump;
00161 
00162 }