CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/DQM/SiStripCommissioningClients/src/SummaryPlotXmlParser.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripCommissioningClients/interface/SummaryPlotXmlParser.h"
00002 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 #include <stdexcept>
00005 
00006 using namespace xercesc;
00007 using namespace sistrip;
00008 
00009 // -----------------------------------------------------------------------------
00010 //
00011 const std::string SummaryPlotXmlParser::rootTag_ = "root";
00012 const std::string SummaryPlotXmlParser::runTypeTag_ = "RunType";
00013 const std::string SummaryPlotXmlParser::runTypeAttr_ = "name";
00014 const std::string SummaryPlotXmlParser::summaryPlotTag_ = "SummaryPlot";
00015 const std::string SummaryPlotXmlParser::monitorableAttr_ = "monitorable";
00016 const std::string SummaryPlotXmlParser::presentationAttr_ = "presentation";
00017 const std::string SummaryPlotXmlParser::viewAttr_ = "view";
00018 const std::string SummaryPlotXmlParser::levelAttr_ = "level";
00019 const std::string SummaryPlotXmlParser::granularityAttr_ = "granularity";
00020 
00021 // -----------------------------------------------------------------------------
00022 //
00023 SummaryPlotXmlParser::SummaryPlotXmlParser() {
00024   plots_.clear();
00025   try { XMLPlatformUtils::Initialize(); }
00026   catch ( const XMLException &f ) {
00027     throw( std::runtime_error("Standard pool exception : Fatal Error on pool::TrivialFileCatalog") );
00028   }
00029 }
00030 
00031 // -----------------------------------------------------------------------------
00032 //
00033 std::vector<SummaryPlot> SummaryPlotXmlParser::summaryPlots( const sistrip::RunType& run_type ) {
00034   if( plots_.empty() ) {
00035     edm::LogWarning(mlDqmClient_)
00036       << "[SummaryPlotXmlParser" << __func__ << "]"
00037       << " You have not called the parseXML function,"
00038       << " or your XML file is erronious" << std::endl;
00039   }
00040   if( plots_.find( run_type ) != plots_.end() ) {
00041     return plots_[run_type];
00042   } else { return std::vector<SummaryPlot>(); }
00043   
00044 }
00045 
00046 // -----------------------------------------------------------------------------
00047 //
00048 void SummaryPlotXmlParser::parseXML( const std::string& f ) {
00049   
00050   plots_.clear();
00051   
00052   try {
00053 
00054     // Create parser and open XML document
00055     getDocument(f);
00056     
00057     // Retrieve root element
00058     DOMElement* root = this->doc()->getDocumentElement();
00059     if( !root ) { 
00060       std::stringstream ss;
00061       ss << "[SummaryPlotXmlParser::" << __func__ << "]"
00062          << " Unable to find any elements!"
00063          << " Empty xml document?...";
00064       throw( std::runtime_error( ss.str() ) ); 
00065     }
00066 
00067     // Check on "root" tag
00068     if( !XMLString::equals( root->getTagName(), XMLString::transcode(rootTag_.c_str()) ) ) {
00069       std::stringstream ss;
00070       ss << "[SummaryPlotXmlParser::" << __func__ << "]"
00071          << " Did not find \"" << rootTag_ << "\" tag! " 
00072          << " Tag name is "
00073          << XMLString::transcode(root->getNodeName());
00074       edm::LogWarning(mlDqmClient_) << ss.str();
00075       return;
00076     }
00077         
00078     // Retrieve nodes in xml document
00079     DOMNodeList* nodes = root->getChildNodes();
00080     if ( nodes->getLength() == 0 ) { 
00081       std::stringstream ss;
00082       ss << "[SummaryPlotXmlParser::" << __func__ << "]"
00083          << " Unable to find any children nodes!"
00084          << " Empty xml document?...";
00085       throw( std::runtime_error( ss.str() ) ); 
00086       return;
00087     }
00088 
00089 //     LogTrace(mlDqmClient_) 
00090 //       << "[SummaryPlotXmlParser::" << __func__ << "]"
00091 //       << " Found \"" << rootTag_ << "\" tag!";
00092     
00093 //     LogTrace(mlDqmClient_) 
00094 //       << "[SummaryPlotXmlParser::" << __func__ << "]"
00095 //       << " Found " << nodes->getLength()
00096 //       << " children nodes!";
00097     
00098     // Iterate through nodes
00099     for( XMLSize_t inode = 0; inode < nodes->getLength(); ++inode ) {
00100 
00101       // Check on whether node is element
00102       DOMNode* node = nodes->item(inode);
00103       if( node->getNodeType() &&
00104           node->getNodeType() == DOMNode::ELEMENT_NODE ) {
00105         
00106         DOMElement* element = dynamic_cast<DOMElement*>( node );
00107         if ( !element ) { continue; }
00108 
00109         if( XMLString::equals( element->getTagName(), 
00110                                XMLString::transcode(runTypeTag_.c_str()) ) ) {
00111           
00112           const XMLCh* attr = element->getAttribute( XMLString::transcode(runTypeAttr_.c_str()) );
00113           sistrip::RunType run_type = SiStripEnumsAndStrings::runType( XMLString::transcode(attr) );
00114           
00115 //        std::stringstream ss;
00116 //        ss << "[SummaryPlotXmlParser::" << __func__ << "]"
00117 //           << " Found \"" << runTypeTag_ << "\" tag!" << std::endl
00118 //           << "  with tag name \"" << XMLString::transcode(element->getNodeName()) << "\"" << std::endl
00119 //           << "  and attr \"" << runTypeAttr_ << "\" with value \"" << XMLString::transcode(attr) << "\"";
00120 //        LogTrace(mlDqmClient_) << ss.str();
00121           
00122           // Retrieve nodes in xml document
00123           DOMNodeList* children = node->getChildNodes();
00124           if ( nodes->getLength() == 0 ) { 
00125             std::stringstream ss;
00126             ss << "[SummaryPlotXmlParser::" << __func__ << "]"
00127                << " Unable to find any children nodes!"
00128                << " Empty xml document?...";
00129             throw( std::runtime_error( ss.str() ) ); 
00130             return;
00131           }
00132 
00133           // Iterate through nodes
00134           for( XMLSize_t jnode = 0; jnode < children->getLength(); ++jnode ) {
00135 
00136             // Check on whether node is element
00137             DOMNode* child = children->item(jnode);
00138             if( child->getNodeType() &&
00139                 child->getNodeType() == DOMNode::ELEMENT_NODE ) {
00140         
00141               DOMElement* elem = dynamic_cast<DOMElement*>( child );
00142               if ( !elem ) { continue; }
00143 
00144               if( XMLString::equals( elem->getTagName(), 
00145                                      XMLString::transcode(summaryPlotTag_.c_str()) ) ) {
00146                 
00147                 const XMLCh* mon = elem->getAttribute( XMLString::transcode(monitorableAttr_.c_str()) );
00148                 const XMLCh* pres = elem->getAttribute( XMLString::transcode(presentationAttr_.c_str()) );
00149                 const XMLCh* level = elem->getAttribute( XMLString::transcode(levelAttr_.c_str()) );
00150                 const XMLCh* gran = elem->getAttribute( XMLString::transcode(granularityAttr_.c_str()) );
00151   
00152                 SummaryPlot plot( XMLString::transcode(mon),
00153                                   XMLString::transcode(pres),
00154                                   XMLString::transcode(gran),
00155                                   XMLString::transcode(level) );
00156                 plots_[run_type].push_back( plot );
00157 
00158 //              std::stringstream ss;
00159 //              ss << "[SummaryPlotXmlParser::" << __func__ << "]"
00160 //                 << " Found \"" << summaryPlotTag_ << "\" tag!" << std::endl
00161 //                 << "  with tag name \"" << XMLString::transcode(elem->getNodeName()) << "\"" << std::endl
00162 //                 << "  and attr \"" << monitorableAttr_ << "\" with value \"" << XMLString::transcode(mon) << "\"" << std::endl
00163 //                 << "  and attr \"" << presentationAttr_ << "\" with value \"" << XMLString::transcode(pres) << "\"" << std::endl
00164 //                //<< "  and attr \"" << viewAttr_ << "\" with value \"" << XMLString::transcode(view) << "\"" << std::endl
00165 //                 << "  and attr \"" << levelAttr_ << "\" with value \"" << XMLString::transcode(level) << "\"" << std::endl
00166 //                 << "  and attr \"" << granularityAttr_ << "\" with value \"" << XMLString::transcode(gran) << "\"";
00167 //              LogTrace(mlDqmClient_) << ss.str();
00168 
00169                 // Update SummaryPlot object and push back into map
00170                 
00171               }
00172             }
00173           }
00174           
00175         }
00176       }
00177     }
00178 
00179   }
00180   catch( XMLException& e ) {
00181     char* message = XMLString::transcode(e.getMessage());
00182     std::ostringstream ss;
00183     ss << "[SummaryPlotXmlParser::" << __func__ << "]"
00184        << " Error parsing file: " << message << std::flush;
00185     XMLString::release( &message );
00186   }
00187 
00188 }
00189 
00190 // -----------------------------------------------------------------------------
00191 //
00192 std::ostream& operator<< ( std::ostream& os, const SummaryPlotXmlParser& parser ) {
00193   std::stringstream ss;
00194   parser.print(ss);
00195   os << ss.str();
00196   return os;
00197 }
00198 
00199 // -----------------------------------------------------------------------------
00200 // 
00201 void SummaryPlotXmlParser::print( std::stringstream& ss ) const {
00202   ss << "[SummaryPlotXmlParser::SummaryPlot::" << __func__ << "]" 
00203      << " Dumping contents of parsed XML file: " << std::endl;
00204   using namespace sistrip;
00205   typedef std::vector<SummaryPlot> Plots;
00206   std::map<RunType,Plots>::const_iterator irun = plots_.begin();
00207   for ( ; irun != plots_.end(); irun++ ) {
00208     ss << " RunType=\"" 
00209        << SiStripEnumsAndStrings::runType( irun->first )
00210        << "\"" << std::endl;
00211     if ( irun->second.empty() ) {
00212       ss << " No summary plots for this RunType!";
00213     } else {
00214       Plots::const_iterator iplot = irun->second.begin();
00215       for ( ; iplot != irun->second.end(); iplot++ ) {
00216         ss << *iplot << std::endl;
00217       }
00218     }
00219   }
00220 }