CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/L1Trigger/RPCTrigger/src/RPCPatternsParser.cc

Go to the documentation of this file.
00001 /*******************************************************************************
00002 *                                                                              *
00003 *  Karol Bunkowski                                                             *
00004 *  Warsaw University 2005                                                      *
00005 *                                                                              *
00006 *******************************************************************************/
00007 #ifndef _STAND_ALONE
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 #endif // _STAND_ALONE
00010 #include "L1Trigger/RPCTrigger/interface/RPCPatternsParser.h"
00011 #include <xercesc/sax2/SAX2XMLReader.hpp>
00012 #include <xercesc/sax2/XMLReaderFactory.hpp>
00013 #include <xercesc/util/PlatformUtils.hpp>
00014 #include <sstream>
00015 #include <fstream> 
00016 #include <iostream>
00017 #include "L1Trigger/RPCTrigger/interface/RPCException.h"
00018 //#ifndef __BORLANDC__
00019 //#include "xoap.h"
00020 //#endif
00021 
00022 using namespace xercesc;
00023 using namespace std;
00024 
00025 string xMLCh2String(const XMLCh* ch) {
00026 #ifdef __BORLANDC__
00027   if(ch == 0) return "";
00028   WideString wstr(ch);
00029   AnsiString astr(wstr);
00030   return astr.c_str();
00031 #else
00032         if(ch == 0) return "";
00033 
00034         //auto_ptr<char> v(XMLString::transcode(ch));
00035   //return string(v.get());
00036   char* buf = XMLString::transcode(ch);
00037   string str(buf);
00038   XMLString::release(&buf);
00039   return str;
00040 #endif
00041 }
00042 
00043 const RPCPattern::RPCPatVec& RPCPatternsParser::getPatternsVec(const RPCConst::l1RpcConeCrdnts& coneCrds) const {
00044   TPatternsVecsMap::const_iterator patVecIt  = m_PatternsVecsMap.find(coneCrds);
00045   if(patVecIt == m_PatternsVecsMap.end()){
00046 
00047     std::stringstream ss;
00048     ss << coneCrds.m_Tower << " " << coneCrds.m_LogSector << " " << coneCrds.m_LogSegment;
00049     throw RPCException( std::string("no such a cone in m_PatternsVecsMap: ")+ ss.str() );
00050     //edm::LogError("RPCTrigger")<< "no such a cone in m_PatternsVecsMap";
00051   }
00052   return patVecIt->second; // XXX - TMF - was in if{}, changed to avoid warning
00053 }
00054 
00055 const RPCPattern::RPCPatVec& RPCPatternsParser::getPatternsVec(const int tower, const int sc, const int sg) const {
00056 
00057     RPCConst::l1RpcConeCrdnts cords(tower,sc,sg);
00058 
00059     return getPatternsVec(cords);
00060 
00061 }
00062 
00063 
00064 // ---------------------------------------------------------------------------
00065 //  This is a simple class that lets us do easy(though not terribly efficient)
00066 //  trancoding of char* data to XMLCh data.
00067 // ---------------------------------------------------------------------------
00068 class XStr
00069 {
00070 public :
00071     // -----------------------------------------------------------------------
00072     //  Constructors and Destructor
00073     // -----------------------------------------------------------------------
00074     XStr(const char* const toTranscode)
00075     {
00076         // Call the private transcoding method
00077         m_fUnicodeForm = XMLString::transcode(toTranscode);
00078     }
00079 
00080     ~XStr()
00081     {
00082         XMLString::release(&m_fUnicodeForm);
00083     }
00084 
00085 
00086     // -----------------------------------------------------------------------
00087     //  Getter methods
00088     // -----------------------------------------------------------------------
00089     const XMLCh* unicodeForm() const
00090     {
00091         return m_fUnicodeForm;
00092     }
00093 
00094 private :
00095     // -----------------------------------------------------------------------
00096     //  Private data members
00097     //
00098     //  m_fUnicodeForm
00099     //      This is the Unicode XMLCh format of the string.
00100     // -----------------------------------------------------------------------
00101     XMLCh*   m_fUnicodeForm;
00102 };
00103 
00104 #define Char2XMLCh(str) XStr(str).unicodeForm()
00105 
00106 int RPCPatternsParser::m_InstanceCount = 0;
00107 
00108 RPCPatternsParser::RPCPatternsParser()
00109 {
00110    if(m_InstanceCount == 0) { 
00111     try {
00112         XMLPlatformUtils::Initialize();
00113         //XPathEvaluator::initialize();
00114         m_InstanceCount++;
00115     }
00116     catch(const XMLException &toCatch)  {
00117       throw RPCException("Error during Xerces-c Initialization: "
00118            + xMLCh2String(toCatch.getMessage()));
00119       //edm::LogError("RPCTrigger")<< "Error during Xerces-c Initialization: " 
00120       //           + xMLCh2String(toCatch.getMessage());
00121     }
00122   }  
00123 }
00124 
00125 
00126 
00127 RPCPatternsParser::~RPCPatternsParser() {
00128 }
00129 
00130 void RPCPatternsParser::parse(std::string fileName)
00131 {
00132   ifstream fin;
00133   fin.open(fileName.c_str());
00134   if(fin.fail()) {
00135     throw RPCException("Cannot open the file" + fileName);
00136     //edm::LogError("RPCTrigger") << "Cannot open the file" + fileName;
00137   }
00138   fin.close();
00139 
00140   SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
00141   parser->setContentHandler(this);
00142   
00143   m_QualityVec.clear();
00144   parser->parse(fileName.c_str());
00145   delete parser;
00146 }
00147 
00148 void RPCPatternsParser::startElement(const XMLCh* const uri,
00149                                        const XMLCh* const localname,
00150                                        const XMLCh* const qname,
00151                                        const Attributes& attrs) {
00152   RPCConst rpcconst;
00153   
00154   m_CurrElement = xMLCh2String(localname);
00155   if(m_CurrElement == "quality") {
00156     //<quality id = "0" planes = "011110" val = 1/>
00157     RPCPattern::TQuality quality;
00158     
00159     
00160     quality.m_QualityTabNumber = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("id"))));
00161     std::bitset<8> firedPl( xMLCh2String(attrs.getValue(Char2XMLCh("planes")) )) ;
00162     unsigned long fpUL = firedPl.to_ulong();
00163     quality.m_FiredPlanes = (unsigned char) (fpUL & 0xFF );
00164     //quality.m_FiredPlanes = xMLCh2String(attrs.getValue(Char2XMLCh("planes")));
00165     quality.m_QualityValue = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("val"))));
00166 
00167     m_QualityVec.push_back(quality);
00168   }
00169   else if(m_CurrElement == "pac") {
00170     //<pac id ="0" m_tower = "0" logSector = "0" logSegment = "0" descr = "">       
00171     RPCConst::l1RpcConeCrdnts cone;
00172     cone.m_Tower = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("tower"))));
00173     cone.m_LogSector = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("logSector"))));
00174     cone.m_LogSegment = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("logSegment"))));
00175     pair <TPatternsVecsMap::iterator, bool> res = m_PatternsVecsMap.insert(TPatternsVecsMap::value_type(cone,
00176                                                                                                 RPCPattern::RPCPatVec()));
00177     if(res.second == true)
00178       m_CurPacIt = res.first;
00179     else
00180       throw RPCException( std::string("m_PatternsVecsMap insertion failed - cone already exixsts?"));
00181       //edm::LogError("RPCTrigger") << "m_PatternsVecsMap insertion failed - cone already exixsts?";
00182   }
00183   else if(m_CurrElement == "pat") {
00184     //<pat type="E" grp="0" qual="0" sign="0" code="31" num="0">
00185     string pt = xMLCh2String(attrs.getValue(Char2XMLCh("type")));
00186     if(pt == "E")
00187       m_CurPattern.setPatternType(RPCPattern::PAT_TYPE_E);
00188     else if(pt == "T")
00189       m_CurPattern.setPatternType(RPCPattern::PAT_TYPE_T);
00190     else
00191       throw RPCException("unknown pattern type: " + pt);
00192       //edm::LogError("RPCTrigger") << "unknown pattern type: " + pt;
00193 
00194     m_CurPattern.setRefGroup(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("grp")))));
00195     m_CurPattern.setQualityTabNumber(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("qual")))));
00196     
00197     m_CurPattern.setSign(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("sign")))));
00198     m_CurPattern.setCode(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("code")))));
00199     m_CurPattern.setNumber(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("num")))));
00200   }
00201   else if(m_CurrElement == "str") {
00202     //<logstrip plane="m_LOGPLANE1" from="32" to="32"/>
00203     int logPlane = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("Pl"))));
00204     m_CurPattern.setStripFrom(logPlane, rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("f")))));
00205     m_CurPattern.setStripTo(logPlane, rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("t")))) + 1);
00206   }
00207 }
00208 
00209 void RPCPatternsParser::endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname) {
00210   string element = xMLCh2String(localname);
00211   if(element == "pat") {
00212     m_CurPacIt->second.push_back(m_CurPattern);
00213   }
00214 }