Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:41

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWModelFilter
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Fri Feb 29 13:39:56 PST 2008
0011 //
0012 
0013 // system include files
0014 #include <sstream>
0015 
0016 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0017 
0018 // user include files
0019 #include "Fireworks/Core/interface/FWModelFilter.h"
0020 #include "Fireworks/Core/interface/FWExpressionException.h"
0021 
0022 #include "CommonTools/Utils/interface/parser/Grammar.h"
0023 #include "CommonTools/Utils/interface/parser/Exception.h"
0024 
0025 #include "Fireworks/Core/src/expressionFormatHelpers.h"
0026 
0027 //
0028 // constants, enums and typedefs
0029 //
0030 
0031 //
0032 // static data member definitions
0033 //
0034 
0035 //
0036 // constructors and destructor
0037 //
0038 FWModelFilter::FWModelFilter(const std::string& iExpression, const std::string& iClassName)
0039     : m_className(iClassName), m_type(edm::TypeWithDict::byName(iClassName)) {
0040   setExpression(iExpression);
0041 }
0042 
0043 // FWModelFilter::FWModelFilter(const FWModelFilter& rhs)
0044 // {
0045 //    // do actual copying here;
0046 // }
0047 
0048 FWModelFilter::~FWModelFilter() {}
0049 
0050 //
0051 // assignment operators
0052 //
0053 // const FWModelFilter& FWModelFilter::operator=(const FWModelFilter& rhs)
0054 // {
0055 //   //An exception safe implementation is
0056 //   FWModelFilter temp(rhs);
0057 //   swap(rhs);
0058 //
0059 //   return *this;
0060 // }
0061 
0062 //
0063 // member functions
0064 //
0065 void FWModelFilter::setExpression(const std::string& iExpression) {
0066   if (m_type != edm::TypeWithDict() && !iExpression.empty()) {
0067     using namespace fireworks::expression;
0068 
0069     //Backwards compatibility with old format
0070     std::string temp = oldToNewFormat(iExpression);
0071 
0072     //now setup the parser
0073     using namespace boost::spirit::classic;
0074     reco::parser::SelectorPtr tmpPtr;
0075     reco::parser::Grammar grammar(tmpPtr, m_type);
0076     try {
0077       if (parse(temp.c_str(), grammar.use_parser<0>() >> end_p, space_p).full) {
0078         m_selector = tmpPtr;
0079         m_expression = iExpression;
0080       } else {
0081         throw FWExpressionException("syntax error", -1);
0082         //std::cout <<"failed to parse "<<iExpression<<" because of syntax error"<<std::endl;
0083       }
0084     } catch (const reco::parser::BaseException& e) {
0085       //NOTE: need to calculate actual position before doing the regex
0086       throw FWExpressionException(reco::parser::baseExceptionWhat(e),
0087                                   indexFromNewFormatToOldFormat(temp, e.where - temp.c_str(), iExpression));
0088       //std::cout <<"failed to parse "<<iExpression<<" because "<<reco::parser::baseExceptionWhat(e)<<std::endl;
0089     }
0090   } else {
0091     m_expression = iExpression;
0092   }
0093 }
0094 
0095 void FWModelFilter::setClassName(const std::string& iClassName) {
0096   //NOTE: How do we handle the case where the filter was created before
0097   // the library for the class was loaded and therefore we don't have
0098   // a dictionary for it?
0099 
0100   m_className = iClassName;
0101   m_type = edm::TypeWithDict::byName(iClassName);
0102   setExpression(m_expression);
0103 }
0104 
0105 //
0106 // const member functions
0107 //
0108 const std::string& FWModelFilter::expression() const { return m_expression; }
0109 
0110 bool FWModelFilter::passesFilter(const void* iObject) const {
0111   if (m_expression.empty() || !m_selector.get()) {
0112     return true;
0113   }
0114 
0115   edm::ObjectWithDict o(m_type, const_cast<void*>(iObject));
0116   return (*m_selector)(o);
0117 }
0118 
0119 bool FWModelFilter::trivialFilter() const { return m_expression.empty(); }
0120 
0121 //
0122 // static member functions
0123 //