1 #ifndef FastSimulation_TrackingRecHitProducer_TrackerDetIdSelector_H 2 #define FastSimulation_TrackingRecHitProducer_TrackerDetIdSelector_H 9 #include <boost/config/warning_disable.hpp> 10 #include <boost/spirit/include/qi.hpp> 11 #include <boost/spirit/include/qi_rule.hpp> 12 #include <boost/lambda/lambda.hpp> 13 #include <boost/spirit/include/phoenix.hpp> 14 #include <boost/phoenix/bind/bind_member_function.hpp> 15 #include <boost/spirit/include/qi_grammar.hpp> 21 #include <unordered_map> 35 boost::recursive_wrapper<ExpressionAST>,
36 boost::recursive_wrapper<BinaryOP>,
37 boost::recursive_wrapper<UnaryOP>
46 template <
typename Expr>
72 GREATER, GREATER_EQUAL, EQUAL, LESS_EQUAL, LESS, NOT_EQUAL,
AND, OR
89 return left.
evaluate(detId,trackerTopology) > right.
evaluate(detId,trackerTopology);
90 case OP::GREATER_EQUAL:
91 return left.
evaluate(detId,trackerTopology) >= right.
evaluate(detId,trackerTopology);
93 return left.
evaluate(detId,trackerTopology) == right.
evaluate(detId,trackerTopology);
95 return left.
evaluate(detId,trackerTopology) <= right.
evaluate(detId,trackerTopology);
97 return left.
evaluate(detId,trackerTopology) < right.
evaluate(detId,trackerTopology);
99 return left.
evaluate(detId,trackerTopology) != right.
evaluate(detId,trackerTopology);
101 return left.
evaluate(detId,trackerTopology) && right.
evaluate(detId,trackerTopology);
103 return left.
evaluate(detId,trackerTopology) || right.
evaluate(detId,trackerTopology);
127 return !subject.
evaluate(detId,trackerTopology);
141 typedef std::function<int(const TrackerTopology& trackerTopology, const DetId&)>
DetIdFunction;
147 _trackerTopology(trackerTopology)
151 bool passSelection(
std::string selectionStr)
const;
155 public boost::static_visitor<int>
163 _trackerTopology(trackerTopology)
169 throw cms::Exception(
"FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
"while evaluating a DetId selection a symbol was not set");
181 value = (it->second)(_trackerTopology,_detId);
187 std::string msg =
"error while parsing DetId selection: named identifier '"+s+
"' not known. Possible values are: ";
192 throw cms::Exception(
"FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",msg);
198 return ast.
evaluate(_detId,_trackerTopology);
202 return binaryOP.
evaluate(_detId,_trackerTopology);
206 return unaryOP.
evaluate(_detId,_trackerTopology);
219 _acc(detId,trackerTopology)
230 boost::apply_visitor(*
this, ast.
expr);
237 boost::apply_visitor(*
this, expr.
left.
expr);
265 boost::apply_visitor(*
this, expr.
right.
expr);
282 template <
typename ITERATOR>
284 boost::spirit::qi::grammar<
287 boost::spirit::ascii::space_type,
288 boost::spirit::qi::locals<ExpressionAST>
291 boost::spirit::qi::rule<
294 boost::spirit::ascii::space_type
297 boost::spirit::qi::rule<
300 boost::spirit::ascii::space_type
303 boost::spirit::qi::rule<
306 boost::spirit::ascii::space_type,
307 boost::spirit::qi::locals<ExpressionAST>
314 namespace qi = boost::spirit::qi;
315 namespace ascii = boost::spirit::ascii;
316 namespace phoenix = boost::phoenix;
319 qi::lexeme[+
qi::alpha[qi::_val+=qi::_1]];
322 (qi::true_[qi::_val=1] | qi::false_[qi::_val=0]) |
323 (qi::int_[qi::_val=qi::_1]) |
324 identifierFctRule[qi::_val=qi::_1];
327 (expressionRule[qi::_a=qi::_1] >>
329 (qi::lit(
"&&") >> expressionRule[qi::_a=qi::_a && qi::_1]) |
330 (qi::lit(
"||") >> expressionRule[qi::_a=qi::_a || qi::_1])
334 qi::lit(
"(") >> comboRule[qi::_val=qi::_1] >> qi::lit(
")") |
335 (identifierRule >> qi::lit(
">") >> identifierRule)[qi::_val=qi::_1>qi::_2] |
336 (identifierRule >> qi::lit(
">=") >> identifierRule)[qi::_val=qi::_1>=qi::_2] |
337 (identifierRule >> qi::lit(
"<") >> identifierRule)[qi::_val=qi::_1<qi::_2] |
338 (identifierRule >> qi::lit(
"<=") >> identifierRule)[qi::_val=qi::_1<=qi::_2] |
339 (identifierRule >> qi::lit(
"==") >> identifierRule)[qi::_val=qi::_1==qi::_2] |
340 (identifierRule >> qi::lit(
"!=") >> identifierRule)[qi::_val=qi::_1!=qi::_2] |
341 identifierRule[qi::_val=qi::_1];
349 std::string::const_iterator
begin = selectionStr.cbegin();
350 std::string::const_iterator
end = selectionStr.cend();
356 bool success = boost::spirit::qi::phrase_parse(begin,end, grammar, boost::spirit::ascii::space, exprAST);
359 throw cms::Exception(
"FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
364 throw cms::Exception(
"FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
"parsing selection '"+selectionStr+
"' failed.");
371 return exprAST.
evaluate(_detId,_trackerTopology);
379 return boost::apply_visitor(
Accessor(detId,trackerTopology),this->
expr);
ExpressionAST operator!=(ExpressionAST const &lhs, ExpressionAST const &rhs)
WalkAST(const DetId &detId, const TrackerTopology &trackerTopology)
void operator()(UnaryOP const &expr) const
ExpressionAST operator<(ExpressionAST const &lhs, ExpressionAST const &rhs)
int operator()(const UnaryOP &unaryOP) const
ExpressionAST operator>=(ExpressionAST const &lhs, ExpressionAST const &rhs)
void operator()(std::string str) const
void operator()(BinaryOP const &expr) const
Accessor(const DetId &detId, const TrackerTopology &trackerTopology)
int operator()(const int &i) const
static const StringFunctionMap functionTable
void operator()(ExpressionAST const &ast) const
ExpressionAST operator||(ExpressionAST const &lhs, ExpressionAST const &rhs)
boost::spirit::qi::rule< ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type, boost::spirit::qi::locals< ExpressionAST > > comboRule
int operator()(const BinaryOP &binaryOP) const
ExpressionAST operator<=(ExpressionAST const &lhs, ExpressionAST const &rhs)
const TrackerTopology & _trackerTopology
bool passSelection(std::string selectionStr) const
boost::spirit::qi::rule< ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type > identifierRule
int operator()(const ExpressionAST &ast) const
Container::value_type value_type
std::unordered_map< std::string, DetIdFunction > StringFunctionMap
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
ExpressionAST operator&&(ExpressionAST const &lhs, ExpressionAST const &rhs)
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
std::function< int(const TrackerTopology &trackerTopology, const DetId &)> DetIdFunction
int operator()(Nil i) const
boost::spirit::qi::rule< ITERATOR, std::string(), boost::spirit::ascii::space_type > identifierFctRule
ExpressionAST operator==(ExpressionAST const &lhs, ExpressionAST const &rhs)
ExpressionAST operator>(ExpressionAST const &lhs, ExpressionAST const &rhs)
TrackerDetIdSelectorGrammar()
ExpressionAST(Expr const &expr)
bool AND(const TrackBaseRef &track, const RecoTauQualityCuts::TrackQCutFuncCollection &cuts)
boost::variant< Nil, int, std::string, boost::recursive_wrapper< ExpressionAST >, boost::recursive_wrapper< BinaryOP >, boost::recursive_wrapper< UnaryOP > > Type
void operator()(int n) const
const TrackerTopology & _trackerTopology
TrackerDetIdSelector(const DetId &detId, const TrackerTopology &trackerTopology)
BinaryOP(OP op, ExpressionAST const &left, ExpressionAST const &right)
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
int operator()(const std::string &s) const
UnaryOP(OP op, ExpressionAST const &subject)