1 #ifndef FastSimulation_TrackingRecHitProducer_TrackerDetIdSelector_H 2 #define FastSimulation_TrackingRecHitProducer_TrackerDetIdSelector_H 8 #include <boost/config/warning_disable.hpp> 9 #include <boost/spirit/include/qi.hpp> 10 #include <boost/spirit/include/qi_rule.hpp> 11 #include <boost/lambda/lambda.hpp> 12 #include <boost/spirit/include/phoenix.hpp> 13 #include <boost/phoenix/bind/bind_member_function.hpp> 14 #include <boost/spirit/include/qi_grammar.hpp> 20 #include <unordered_map> 27 typedef boost::variant<
Nil,
30 boost::recursive_wrapper<ExpressionAST>,
31 boost::recursive_wrapper<BinaryOP>,
32 boost::recursive_wrapper<UnaryOP> >
37 template <
typename Expr>
57 enum class OP { GREATER, GREATER_EQUAL, EQUAL, LESS_EQUAL, LESS, NOT_EQUAL,
AND, OR } op;
66 return left.
evaluate(detId, trackerTopology) > right.
evaluate(detId, trackerTopology);
67 case OP::GREATER_EQUAL:
68 return left.
evaluate(detId, trackerTopology) >= right.
evaluate(detId, trackerTopology);
70 return left.
evaluate(detId, trackerTopology) == right.
evaluate(detId, trackerTopology);
72 return left.
evaluate(detId, trackerTopology) <= right.
evaluate(detId, trackerTopology);
74 return left.
evaluate(detId, trackerTopology) < right.
evaluate(detId, trackerTopology);
76 return left.
evaluate(detId, trackerTopology) != right.
evaluate(detId, trackerTopology);
78 return left.
evaluate(detId, trackerTopology) && right.
evaluate(detId, trackerTopology);
80 return left.
evaluate(detId, trackerTopology) || right.
evaluate(detId, trackerTopology);
87 enum class OP { NEG } op;
94 return !subject.
evaluate(detId, trackerTopology);
106 typedef std::function<int(const TrackerTopology& trackerTopology, const DetId&)>
DetIdFunction;
111 : _detId(detId), _trackerTopology(trackerTopology) {}
116 class Accessor :
public boost::static_visitor<int> {
123 : _detId(detId), _trackerTopology(trackerTopology) {}
126 throw cms::Exception(
"FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
127 "while evaluating a DetId selection a symbol was not set");
134 value = (it->second)(_trackerTopology, _detId);
139 "error while parsing DetId selection: named identifier '" + s +
"' not known. Possible values are: ";
141 msg += pair.first +
",";
143 throw cms::Exception(
"FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector", msg);
169 boost::apply_visitor(*
this, ast.
expr);
175 boost::apply_visitor(*
this, expr.
left.
expr);
202 boost::apply_visitor(*
this, expr.
right.
expr);
217 template <
typename ITERATOR>
220 boost::spirit::ascii::space_type,
221 boost::spirit::qi::locals<ExpressionAST> > {
222 boost::spirit::qi::rule<ITERATOR, std::string(), boost::spirit::ascii::space_type>
identifierFctRule;
224 boost::spirit::qi::rule<ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type>
identifierRule, expressionRule;
227 rule<ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type, boost::spirit::qi::locals<ExpressionAST> >
231 namespace qi = boost::spirit::qi;
232 namespace ascii = boost::spirit::ascii;
233 namespace phoenix = boost::phoenix;
235 identifierFctRule = qi::lexeme[+
qi::alpha[qi::_val += qi::_1]];
237 identifierRule = (qi::true_[qi::_val = 1] | qi::false_[qi::_val = 0]) | (qi::int_[qi::_val = qi::_1]) |
238 identifierFctRule[qi::_val = qi::_1];
240 comboRule = (expressionRule[qi::_a = qi::_1] >>
241 *((qi::lit(
"&&") >> expressionRule[qi::_a = qi::_a && qi::_1]) |
242 (qi::lit(
"||") >> expressionRule[qi::_a = qi::_a || qi::_1])))[qi::_val = qi::_a];
244 expressionRule = qi::lit(
"(") >> comboRule[qi::_val = qi::_1] >> qi::lit(
")") |
245 (identifierRule >> qi::lit(
">") >> identifierRule)[qi::_val = qi::_1 > qi::_2] |
246 (identifierRule >> qi::lit(
">=") >> identifierRule)[qi::_val = qi::_1 >= qi::_2] |
247 (identifierRule >> qi::lit(
"<") >> identifierRule)[qi::_val = qi::_1 < qi::_2] |
248 (identifierRule >> qi::lit(
"<=") >> identifierRule)[qi::_val = qi::_1 <= qi::_2] |
249 (identifierRule >> qi::lit(
"==") >> identifierRule)[qi::_val = qi::_1 == qi::_2] |
250 (identifierRule >> qi::lit(
"!=") >> identifierRule)[qi::_val = qi::_1 != qi::_2] |
251 identifierRule[qi::_val = qi::_1];
256 std::string::const_iterator
begin = selectionStr.cbegin();
257 std::string::const_iterator
end = selectionStr.cend();
262 bool success = boost::spirit::qi::phrase_parse(begin, end, grammar, boost::spirit::ascii::space, exprAST);
264 throw cms::Exception(
"FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
265 "parsing selection '" + selectionStr +
"' failed at " +
269 throw cms::Exception(
"FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
270 "parsing selection '" + selectionStr +
"' failed.");
277 return exprAST.
evaluate(_detId, _trackerTopology);
281 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)
int operator()(const BinaryOP &binaryOP) const
boost::spirit::qi::rule< ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type, boost::spirit::qi::locals< ExpressionAST > > comboRule
ExpressionAST operator<=(ExpressionAST const &lhs, ExpressionAST const &rhs)
const TrackerTopology & _trackerTopology
bool passSelection(std::string selectionStr) const
int operator()(const ExpressionAST &ast) const
Container::value_type value_type
std::unordered_map< std::string, DetIdFunction > StringFunctionMap
boost::variant< Nil, int, std::string, boost::recursive_wrapper< ExpressionAST >, boost::recursive_wrapper< BinaryOP >, boost::recursive_wrapper< UnaryOP > > Type
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
ExpressionAST operator==(ExpressionAST const &lhs, ExpressionAST const &rhs)
boost::spirit::qi::rule< ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type > identifierRule
ExpressionAST operator>(ExpressionAST const &lhs, ExpressionAST const &rhs)
TrackerDetIdSelectorGrammar()
ExpressionAST(Expr const &expr)
bool AND(const TrackBaseRef &track, const RecoTauQualityCuts::TrackQCutFuncCollection &cuts)
void operator()(int n) const
alpha
zGenParticlesMatch = cms.InputTag(""),
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
boost::spirit::qi::rule< ITERATOR, std::string(), boost::spirit::ascii::space_type > identifierFctRule
int operator()(const std::string &s) const
UnaryOP(OP op, ExpressionAST const &subject)