CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/CondTools/Ecal/src/EcalTPGStripStatusXMLTranslator.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 #include "DataFormats/DetId/interface/DetId.h"
00013 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00014 
00015 #include "CondTools/Ecal/interface/EcalTPGStripStatusXMLTranslator.h"
00016 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00017 #include "CondTools/Ecal/interface/XMLTags.h"
00018 
00019 #include "CondFormats/DataRecord/interface/EcalTPGStripStatusRcd.h"
00020 
00021 using namespace XERCES_CPP_NAMESPACE;
00022 using namespace xuti;
00023 using namespace std;
00024 
00025 int  EcalTPGStripStatusXMLTranslator::readXML(const std::string& filename, 
00026                                           EcalCondHeader& header,
00027                                           EcalTPGStripStatus& record){
00028 
00029   std::cout << " TPGStripStatus should not be filled out from an xml file ..." << std::endl;
00030   XMLPlatformUtils::Initialize();
00031 
00032   XercesDOMParser* parser = new XercesDOMParser;
00033   parser->setValidationScheme( XercesDOMParser::Val_Never );
00034   parser->setDoNamespaces( false );
00035   parser->setDoSchema( false );
00036   
00037   parser->parse(filename.c_str());
00038 
00039   DOMDocument* xmlDoc = parser->getDocument();
00040   if (!xmlDoc) {
00041     std::cout << "EcalTPGStripStatusXMLTranslator::Error parsing document" << std::endl;
00042     return -1;
00043   }
00044 
00045   DOMElement* elementRoot = xmlDoc->getDocumentElement();
00046 
00047   xuti::readHeader(elementRoot,header);
00048 
00049   delete parser;
00050   XMLPlatformUtils::Terminate();
00051   return 0;
00052  }
00053 
00054 int EcalTPGStripStatusXMLTranslator::writeXML(const std::string& filename, 
00055                                           const EcalCondHeader& header,
00056                                           const EcalTPGStripStatus& record){
00057   std::fstream fs(filename.c_str(),ios::out);
00058   fs<< dumpXML(header,record);
00059   return 0;  
00060 }
00061 
00062 
00063 std::string EcalTPGStripStatusXMLTranslator::dumpXML(const EcalCondHeader& header,const EcalTPGStripStatus& record){
00064 
00065   XMLPlatformUtils::Initialize();
00066   DOMImplementation*  impl =
00067     DOMImplementationRegistry::getDOMImplementation(fromNative("LS").c_str());
00068 
00069   DOMWriter* writer =static_cast<DOMImplementationLS*>(impl)->createDOMWriter( );
00070   writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00071 
00072   DOMDocumentType* doctype = impl->createDocumentType(fromNative("XML").c_str(), 0, 0 );
00073   DOMDocument *    doc = 
00074     impl->createDocument( 0, fromNative(TPGStripStatus_tag).c_str(), doctype );
00075 
00076   doc->setEncoding(fromNative("UTF-8").c_str() );
00077   doc->setStandalone(true);
00078   doc->setVersion(fromNative("1.0").c_str() );
00079 
00080   DOMElement* root = doc->getDocumentElement();
00081 
00082   xuti::writeHeader(root,header);
00083   std::string TCC_tag("TCC");
00084   std::string TT_tag("TT");
00085   std::string ST_tag("ST");
00086   const EcalTPGStripStatusMap &stripMap = record.getMap();
00087   std::cout << "EcalTPGStripStatusXMLTranslator::dumpXML strip map size " << stripMap.size() << std::endl;
00088   EcalTPGStripStatusMapIterator itSt;
00089   for(itSt = stripMap.begin(); itSt != stripMap.end(); ++itSt) {
00090     if(itSt->second > 0) {
00091       int tccid = itSt->first/8192 & 0x7F;
00092       int tt  = itSt->first/64 & 0x7F;
00093       int pseudostrip  = itSt->first/8 & 0x7;
00094       //      std::cout << "Bad strip ID = " << itSt->first 
00095       //                << " TCC " << tccid << " TT " << tt << " ST " << pseudostrip
00096       //                << ", status = " << itSt->second << std::endl;
00097       DOMElement* cell_node = 
00098         root->getOwnerDocument()->createElement( fromNative(Cell_tag).c_str());
00099       stringstream value_s;
00100       value_s << tccid ;
00101       cell_node->setAttribute(fromNative(TCC_tag).c_str(),
00102                               fromNative(value_s.str()).c_str());
00103       value_s.str("");
00104       value_s << tt ;
00105       cell_node->setAttribute(fromNative(TT_tag).c_str(),
00106                               fromNative(value_s.str()).c_str());
00107       value_s.str("");
00108       value_s << pseudostrip;
00109       cell_node->setAttribute(fromNative(ST_tag).c_str(),
00110                               fromNative(value_s.str()).c_str());
00111       root->appendChild(cell_node);
00112 
00113       WriteNodeWithValue(cell_node, TPGStripStatus_tag, 1);
00114     }
00115   }
00116 
00117   std::string dump = toNative(writer->writeToString(*root)); 
00118   doc->release(); 
00119   return dump;
00120 }