Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:30:37

0001 /*******************************************************************************
0002 *                                                                              *
0003 *  Karol Bunkowski                                                             *
0004 *  Warsaw University 2005                                                      *
0005 *                                                                              *
0006 *******************************************************************************/
0007 #ifndef _STAND_ALONE
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #endif  // _STAND_ALONE
0010 #include "L1Trigger/RPCTrigger/interface/RPCPatternsParser.h"
0011 #include <xercesc/sax2/SAX2XMLReader.hpp>
0012 #include <xercesc/sax2/XMLReaderFactory.hpp>
0013 #include "Utilities/Xerces/interface/Xerces.h"
0014 #include <sstream>
0015 #include <fstream>
0016 #include <iostream>
0017 #include "L1Trigger/RPCTrigger/interface/RPCException.h"
0018 //#ifndef __BORLANDC__
0019 //#include "xoap.h"
0020 //#endif
0021 
0022 using namespace xercesc;
0023 using namespace std;
0024 
0025 string xMLCh2String(const XMLCh* ch) {
0026 #ifdef __BORLANDC__
0027   if (ch == 0)
0028     return "";
0029   WideString wstr(ch);
0030   AnsiString astr(wstr);
0031   return astr.c_str();
0032 #else
0033   if (ch == nullptr)
0034     return "";
0035 
0036   //unique_ptr<char> v(XMLString::transcode(ch));
0037   //return string(v.get());
0038   char* buf = XMLString::transcode(ch);
0039   string str(buf);
0040   XMLString::release(&buf);
0041   return str;
0042 #endif
0043 }
0044 
0045 const RPCPattern::RPCPatVec& RPCPatternsParser::getPatternsVec(const RPCConst::l1RpcConeCrdnts& coneCrds) const {
0046   TPatternsVecsMap::const_iterator patVecIt = m_PatternsVecsMap.find(coneCrds);
0047   if (patVecIt == m_PatternsVecsMap.end()) {
0048     std::stringstream ss;
0049     ss << coneCrds.m_Tower << " " << coneCrds.m_LogSector << " " << coneCrds.m_LogSegment;
0050     throw RPCException(std::string("no such a cone in m_PatternsVecsMap: ") + ss.str());
0051     //edm::LogError("RPCTrigger")<< "no such a cone in m_PatternsVecsMap";
0052   }
0053   return patVecIt->second;  // XXX - TMF - was in if{}, changed to avoid warning
0054 }
0055 
0056 const RPCPattern::RPCPatVec& RPCPatternsParser::getPatternsVec(const int tower, const int sc, const int sg) const {
0057   RPCConst::l1RpcConeCrdnts cords(tower, sc, sg);
0058 
0059   return getPatternsVec(cords);
0060 }
0061 
0062 // ---------------------------------------------------------------------------
0063 //  This is a simple class that lets us do easy(though not terribly efficient)
0064 //  trancoding of char* data to XMLCh data.
0065 // ---------------------------------------------------------------------------
0066 class XStr {
0067 public:
0068   // -----------------------------------------------------------------------
0069   //  Constructors and Destructor
0070   // -----------------------------------------------------------------------
0071   XStr(const char* const toTranscode) {
0072     // Call the private transcoding method
0073     m_fUnicodeForm = XMLString::transcode(toTranscode);
0074   }
0075 
0076   ~XStr() { XMLString::release(&m_fUnicodeForm); }
0077 
0078   // -----------------------------------------------------------------------
0079   //  Getter methods
0080   // -----------------------------------------------------------------------
0081   const XMLCh* unicodeForm() const { return m_fUnicodeForm; }
0082 
0083 private:
0084   // -----------------------------------------------------------------------
0085   //  Private data members
0086   //
0087   //  m_fUnicodeForm
0088   //      This is the Unicode XMLCh format of the string.
0089   // -----------------------------------------------------------------------
0090   XMLCh* m_fUnicodeForm;
0091 };
0092 
0093 #define Char2XMLCh(str) XStr(str).unicodeForm()
0094 
0095 int RPCPatternsParser::m_InstanceCount = 0;
0096 
0097 RPCPatternsParser::RPCPatternsParser() {
0098   if (m_InstanceCount == 0) {
0099     try {
0100       cms::concurrency::xercesInitialize();
0101       //XPathEvaluator::initialize();
0102       m_InstanceCount++;
0103     } catch (const XMLException& toCatch) {
0104       throw RPCException("Error during Xerces-c Initialization: " + xMLCh2String(toCatch.getMessage()));
0105       //edm::LogError("RPCTrigger")<< "Error during Xerces-c Initialization: "
0106       //           + xMLCh2String(toCatch.getMessage());
0107     }
0108   }
0109 }
0110 
0111 RPCPatternsParser::~RPCPatternsParser() { cms::concurrency::xercesTerminate(); }
0112 
0113 void RPCPatternsParser::parse(std::string fileName) {
0114   ifstream fin;
0115   fin.open(fileName.c_str());
0116   if (fin.fail()) {
0117     throw RPCException("Cannot open the file" + fileName);
0118     //edm::LogError("RPCTrigger") << "Cannot open the file" + fileName;
0119   }
0120   fin.close();
0121 
0122   SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
0123   parser->setContentHandler(this);
0124 
0125   m_QualityVec.clear();
0126   parser->parse(fileName.c_str());
0127   delete parser;
0128 }
0129 
0130 void RPCPatternsParser::startElement(const XMLCh* const uri,
0131                                      const XMLCh* const localname,
0132                                      const XMLCh* const qname,
0133                                      const Attributes& attrs) {
0134   RPCConst rpcconst;
0135 
0136   m_CurrElement = xMLCh2String(localname);
0137   if (m_CurrElement == "quality") {
0138     //<quality id = "0" planes = "011110" val = 1/>
0139     RPCPattern::TQuality quality;
0140 
0141     quality.m_QualityTabNumber = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("id"))));
0142     std::bitset<8> firedPl(xMLCh2String(attrs.getValue(Char2XMLCh("planes"))));
0143     unsigned long fpUL = firedPl.to_ulong();
0144     quality.m_FiredPlanes = (unsigned char)(fpUL & 0xFF);
0145     //quality.m_FiredPlanes = xMLCh2String(attrs.getValue(Char2XMLCh("planes")));
0146     quality.m_QualityValue = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("val"))));
0147 
0148     m_QualityVec.push_back(quality);
0149   } else if (m_CurrElement == "pac") {
0150     //<pac id ="0" m_tower = "0" logSector = "0" logSegment = "0" descr = "">
0151     RPCConst::l1RpcConeCrdnts cone;
0152     cone.m_Tower = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("tower"))));
0153     cone.m_LogSector = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("logSector"))));
0154     cone.m_LogSegment = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("logSegment"))));
0155     pair<TPatternsVecsMap::iterator, bool> res =
0156         m_PatternsVecsMap.insert(TPatternsVecsMap::value_type(cone, RPCPattern::RPCPatVec()));
0157     if (res.second == true)
0158       m_CurPacIt = res.first;
0159     else
0160       throw RPCException(std::string("m_PatternsVecsMap insertion failed - cone already exixsts?"));
0161     //edm::LogError("RPCTrigger") << "m_PatternsVecsMap insertion failed - cone already exixsts?";
0162   } else if (m_CurrElement == "pat") {
0163     //<pat type="E" grp="0" qual="0" sign="0" code="31" num="0">
0164     string pt = xMLCh2String(attrs.getValue(Char2XMLCh("type")));
0165     if (pt == "E")
0166       m_CurPattern.setPatternType(RPCPattern::PAT_TYPE_E);
0167     else if (pt == "T")
0168       m_CurPattern.setPatternType(RPCPattern::PAT_TYPE_T);
0169     else
0170       throw RPCException("unknown pattern type: " + pt);
0171     //edm::LogError("RPCTrigger") << "unknown pattern type: " + pt;
0172 
0173     m_CurPattern.setRefGroup(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("grp")))));
0174     m_CurPattern.setQualityTabNumber(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("qual")))));
0175 
0176     m_CurPattern.setSign(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("sign")))));
0177     m_CurPattern.setCode(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("code")))));
0178     m_CurPattern.setNumber(rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("num")))));
0179   } else if (m_CurrElement == "str") {
0180     //<logstrip plane="m_LOGPLANE1" from="32" to="32"/>
0181     int logPlane = rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("Pl"))));
0182     m_CurPattern.setStripFrom(logPlane, rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("f")))));
0183     m_CurPattern.setStripTo(logPlane, rpcconst.stringToInt(xMLCh2String(attrs.getValue(Char2XMLCh("t")))) + 1);
0184   }
0185 }
0186 
0187 void RPCPatternsParser::endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname) {
0188   string element = xMLCh2String(localname);
0189   if (element == "pat") {
0190     m_CurPacIt->second.push_back(m_CurPattern);
0191   }
0192 }