CMS 3D CMS Logo

TrackerDetIdSelector.h
Go to the documentation of this file.
1 #ifndef FastSimulation_TrackingRecHitProducer_TrackerDetIdSelector_H
2 #define FastSimulation_TrackingRecHitProducer_TrackerDetIdSelector_H
3 
7 
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>
15 
16 #include <iostream>
17 #include <cstdlib>
18 #include <typeinfo>
19 #include <functional>
20 #include <unordered_map>
21 
22 struct BinaryOP;
23 struct UnaryOP;
24 struct Nil {};
25 
26 struct ExpressionAST {
27  typedef boost::variant<Nil,
28  int,
30  boost::recursive_wrapper<ExpressionAST>,
31  boost::recursive_wrapper<BinaryOP>,
32  boost::recursive_wrapper<UnaryOP> >
34 
36 
37  template <typename Expr>
38  ExpressionAST(Expr const& expr) : expr(expr) {}
39 
40  int evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const;
41 
43 
45 };
46 
47 ExpressionAST operator>(ExpressionAST const& lhs, ExpressionAST const& rhs);
48 ExpressionAST operator>=(ExpressionAST const& lhs, ExpressionAST const& rhs);
49 ExpressionAST operator==(ExpressionAST const& lhs, ExpressionAST const& rhs);
50 ExpressionAST operator<=(ExpressionAST const& lhs, ExpressionAST const& rhs);
51 ExpressionAST operator<(ExpressionAST const& lhs, ExpressionAST const& rhs);
52 ExpressionAST operator!=(ExpressionAST const& lhs, ExpressionAST const& rhs);
53 ExpressionAST operator&&(ExpressionAST const& lhs, ExpressionAST const& rhs);
54 ExpressionAST operator||(ExpressionAST const& lhs, ExpressionAST const& rhs);
55 
56 struct BinaryOP {
60 
62 
63  int evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const {
64  switch (op) {
65  case OP::GREATER:
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);
69  case OP::EQUAL:
70  return left.evaluate(detId, trackerTopology) == right.evaluate(detId, trackerTopology);
71  case OP::LESS_EQUAL:
72  return left.evaluate(detId, trackerTopology) <= right.evaluate(detId, trackerTopology);
73  case OP::LESS:
74  return left.evaluate(detId, trackerTopology) < right.evaluate(detId, trackerTopology);
75  case OP::NOT_EQUAL:
76  return left.evaluate(detId, trackerTopology) != right.evaluate(detId, trackerTopology);
77  case OP::AND:
78  return left.evaluate(detId, trackerTopology) && right.evaluate(detId, trackerTopology);
79  case OP::OR:
80  return left.evaluate(detId, trackerTopology) || right.evaluate(detId, trackerTopology);
81  }
82  return 0;
83  }
84 };
85 
86 struct UnaryOP {
87  enum class OP { NEG } op;
90 
91  int evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const {
92  switch (op) {
93  case OP::NEG:
94  return !subject.evaluate(detId, trackerTopology);
95  }
96  return 0;
97  }
98 };
99 
101 private:
102  const DetId& _detId;
104 
105 public:
107  typedef std::unordered_map<std::string, DetIdFunction> StringFunctionMap;
109 
112 
114 };
115 
116 class Accessor : public boost::static_visitor<int> {
117 private:
118  const DetId& _detId;
120 
121 public:
124 
125  int operator()(Nil i) const {
126  throw cms::Exception("FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
127  "while evaluating a DetId selection a symbol was not set");
128  }
129  int operator()(const int& i) const { return i; }
130  int operator()(const std::string& s) const {
131  TrackerDetIdSelector::StringFunctionMap::const_iterator it = TrackerDetIdSelector::functionTable.find(s);
132  int value = 0;
133  if (it != TrackerDetIdSelector::functionTable.cend()) {
134  value = (it->second)(_trackerTopology, _detId);
135  //std::cout<<"attr="<<s<<", value="<<value<<std::endl;
136  } else {
137  //std::cout<<"attr="<<s<<" unknown"<<std::endl;
138  std::string msg =
139  "error while parsing DetId selection: named identifier '" + s + "' not known. Possible values are: ";
141  msg += pair.first + ",";
142  }
143  throw cms::Exception("FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector", msg);
144  }
145  return value;
146  }
147  int operator()(const ExpressionAST& ast) const { return ast.evaluate(_detId, _trackerTopology); }
148  int operator()(const BinaryOP& binaryOP) const { return binaryOP.evaluate(_detId, _trackerTopology); }
149  int operator()(const UnaryOP& unaryOP) const { return unaryOP.evaluate(_detId, _trackerTopology); }
150 };
151 
152 struct WalkAST {
154 
156 
157  typedef void result_type;
158 
159  void operator()() const {}
160  void operator()(int n) const {
161  std::cout << n;
162  std::cout << " [" << _acc(n) << "] ";
163  }
164  void operator()(std::string str) const {
165  std::cout << str;
166  std::cout << " [" << _acc(str) << "] ";
167  }
168  void operator()(ExpressionAST const& ast) const {
169  boost::apply_visitor(*this, ast.expr);
170  std::cout << " [=" << _acc(ast) << "] ";
171  }
172 
173  void operator()(BinaryOP const& expr) const {
174  std::cout << "(";
175  boost::apply_visitor(*this, expr.left.expr);
176  switch (expr.op) {
178  std::cout << " > ";
179  break;
181  std::cout << " >= ";
182  break;
183  case BinaryOP::OP::EQUAL:
184  std::cout << " == ";
185  break;
187  std::cout << " <= ";
188  break;
189  case BinaryOP::OP::LESS:
190  std::cout << " < ";
191  break;
193  std::cout << " != ";
194  break;
195  case BinaryOP::OP::AND:
196  std::cout << " && ";
197  break;
198  case BinaryOP::OP::OR:
199  std::cout << " || ";
200  break;
201  }
202  boost::apply_visitor(*this, expr.right.expr);
203  std::cout << ')';
204  }
205 
206  void operator()(UnaryOP const& expr) const {
207  switch (expr.op) {
208  case UnaryOP::OP::NEG:
209  std::cout << " !(";
210  break;
211  }
212  boost::apply_visitor(*this, expr.subject.expr);
213  std::cout << ')';
214  }
215 };
216 
217 template <typename ITERATOR>
218 struct TrackerDetIdSelectorGrammar : boost::spirit::qi::grammar<ITERATOR,
219  ExpressionAST(),
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;
223 
224  boost::spirit::qi::rule<ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type> identifierRule, expressionRule;
225 
226  boost::spirit::qi::
227  rule<ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type, boost::spirit::qi::locals<ExpressionAST> >
229 
231  namespace qi = boost::spirit::qi;
232  namespace ascii = boost::spirit::ascii;
233  namespace phoenix = boost::phoenix;
234 
235  identifierFctRule = qi::lexeme[+qi::alpha[qi::_val += qi::_1]];
236 
237  identifierRule = (qi::true_[qi::_val = 1] | qi::false_[qi::_val = 0]) | (qi::int_[qi::_val = qi::_1]) |
238  identifierFctRule[qi::_val = qi::_1];
239 
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];
243 
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];
252  }
253 };
254 
256  std::string::const_iterator begin = selectionStr.cbegin();
257  std::string::const_iterator end = selectionStr.cend();
258 
260  ExpressionAST exprAST;
261 
262  bool success = boost::spirit::qi::phrase_parse(begin, end, grammar, boost::spirit::ascii::space, exprAST);
263  if (begin != end) {
264  throw cms::Exception("FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
265  "parsing selection '" + selectionStr + "' failed at " +
266  std::string(selectionStr.cbegin(), begin) + "^^^" + std::string(begin, end));
267  }
268  if (!success) {
269  throw cms::Exception("FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
270  "parsing selection '" + selectionStr + "' failed.");
271  }
272  /* Comment out for debugging
273  WalkAST walker(_detId,_trackerTopology);
274  walker(exprAST);
275  std::cout<<std::endl;
276  */
277  return exprAST.evaluate(_detId, _trackerTopology);
278 }
279 
281  return boost::apply_visitor(Accessor(detId, trackerTopology), this->expr);
282 }
283 
284 #endif
WalkAST::operator()
void operator()(std::string str) const
Definition: TrackerDetIdSelector.h:164
UnaryOP::subject
ExpressionAST subject
Definition: TrackerDetIdSelector.h:88
BinaryOP::OP::LESS
Accessor::operator()
int operator()(const ExpressionAST &ast) const
Definition: TrackerDetIdSelector.h:147
operator>
ExpressionAST operator>(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:77
Accessor::operator()
int operator()(const BinaryOP &binaryOP) const
Definition: TrackerDetIdSelector.h:148
Nil
Definition: TrackerDetIdSelector.h:24
operator<
ExpressionAST operator<(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:97
mps_fire.i
i
Definition: mps_fire.py:355
ExpressionAST::ExpressionAST
ExpressionAST(Expr const &expr)
Definition: TrackerDetIdSelector.h:38
operator<=
ExpressionAST operator<=(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:92
ExpressionAST::Type
boost::variant< Nil, int, std::string, boost::recursive_wrapper< ExpressionAST >, boost::recursive_wrapper< BinaryOP >, boost::recursive_wrapper< UnaryOP > > Type
Definition: TrackerDetIdSelector.h:33
ExpressionAST
Definition: TrackerDetIdSelector.h:26
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
UnaryOP::op
enum UnaryOP::OP op
Accessor::_detId
const DetId & _detId
Definition: TrackerDetIdSelector.h:118
WalkAST
Definition: TrackerDetIdSelector.h:152
zMuMuMuonUserData.alpha
alpha
zGenParticlesMatch = cms.InputTag(""),
Definition: zMuMuMuonUserData.py:9
TrackerTopology
Definition: TrackerTopology.h:16
TrackerDetIdSelectorGrammar::identifierRule
boost::spirit::qi::rule< ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type > identifierRule
Definition: TrackerDetIdSelector.h:224
gather_cfg.cout
cout
Definition: gather_cfg.py:144
BinaryOP::OP::LESS_EQUAL
WalkAST::operator()
void operator()(ExpressionAST const &ast) const
Definition: TrackerDetIdSelector.h:168
TrackerDetIdSelector::TrackerDetIdSelector
TrackerDetIdSelector(const DetId &detId, const TrackerTopology &trackerTopology)
Definition: TrackerDetIdSelector.h:110
TrackerDetIdSelector::passSelection
bool passSelection(std::string selectionStr) const
Definition: TrackerDetIdSelector.h:255
operator!=
ExpressionAST operator!=(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:102
mps_check.msg
tuple msg
Definition: mps_check.py:285
Accessor::operator()
int operator()(Nil i) const
Definition: TrackerDetIdSelector.h:125
TrackerDetIdSelectorGrammar::TrackerDetIdSelectorGrammar
TrackerDetIdSelectorGrammar()
Definition: TrackerDetIdSelector.h:230
BinaryOP::evaluate
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
Definition: TrackerDetIdSelector.h:63
ExpressionAST::evaluate
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
Definition: TrackerDetIdSelector.h:280
operator>=
ExpressionAST operator>=(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:82
TrackerDetIdSelector::_trackerTopology
const TrackerTopology & _trackerTopology
Definition: TrackerDetIdSelector.h:103
UnaryOP::UnaryOP
UnaryOP(OP op, ExpressionAST const &subject)
Definition: TrackerDetIdSelector.h:89
trigObjTnPSource_cfi.selectionStr
selectionStr
Definition: trigObjTnPSource_cfi.py:69
end
#define end
Definition: vmac.h:39
Accessor::operator()
int operator()(const std::string &s) const
Definition: TrackerDetIdSelector.h:130
TrackerDetIdSelectorGrammar::identifierFctRule
boost::spirit::qi::rule< ITERATOR, std::string(), boost::spirit::ascii::space_type > identifierFctRule
Definition: TrackerDetIdSelector.h:222
DetId
Definition: DetId.h:17
TrackerTopology.h
alignCSCRings.s
s
Definition: alignCSCRings.py:92
BinaryOP::OP::OR
BinaryOP::OP::AND
UnaryOP::OP
OP
Definition: TrackerDetIdSelector.h:87
WalkAST::result_type
void result_type
Definition: TrackerDetIdSelector.h:157
UnaryOP::evaluate
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
Definition: TrackerDetIdSelector.h:91
TrackerDetIdSelector
Definition: TrackerDetIdSelector.h:100
str
#define str(s)
Definition: TestProcessor.cc:48
summarizeEdmComparisonLogfiles.success
success
Definition: summarizeEdmComparisonLogfiles.py:115
operator||
ExpressionAST operator||(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:112
UnaryOP
Definition: TrackerDetIdSelector.h:86
Accessor::operator()
int operator()(const UnaryOP &unaryOP) const
Definition: TrackerDetIdSelector.h:149
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
BinaryOP::op
enum BinaryOP::OP op
TrackerDetIdSelector::DetIdFunction
std::function< int(const TrackerTopology &trackerTopology, const DetId &)> DetIdFunction
Definition: TrackerDetIdSelector.h:106
TrackerDetIdSelectorGrammar::expressionRule
boost::spirit::qi::rule< ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type > expressionRule
Definition: TrackerDetIdSelector.h:224
BinaryOP::OP::GREATER_EQUAL
Accessor::operator()
int operator()(const int &i) const
Definition: TrackerDetIdSelector.h:129
UnaryOP::OP::NEG
operator==
ExpressionAST operator==(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:87
BinaryOP::right
ExpressionAST right
Definition: TrackerDetIdSelector.h:59
createfilelist.int
int
Definition: createfilelist.py:10
value
Definition: value.py:1
operator&&
ExpressionAST operator&&(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:107
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
Accessor
Definition: TrackerDetIdSelector.h:116
WalkAST::operator()
void operator()(BinaryOP const &expr) const
Definition: TrackerDetIdSelector.h:173
BinaryOP::OP::GREATER
trackerTopology_cfi.trackerTopology
trackerTopology
Definition: trackerTopology_cfi.py:3
jets_cff.expr
expr
Definition: jets_cff.py:449
DetId.h
ExpressionAST::operator!
ExpressionAST & operator!()
Definition: TrackerDetIdSelector.cc:117
WalkAST::_acc
Accessor _acc
Definition: TrackerDetIdSelector.h:153
relativeConstraints.value
value
Definition: relativeConstraints.py:53
Exception
Definition: hltDiff.cc:246
WalkAST::operator()
void operator()(int n) const
Definition: TrackerDetIdSelector.h:160
BinaryOP
Definition: TrackerDetIdSelector.h:56
HiBiasedCentrality_cfi.function
function
Definition: HiBiasedCentrality_cfi.py:4
BinaryOP::BinaryOP
BinaryOP(OP op, ExpressionAST const &left, ExpressionAST const &right)
Definition: TrackerDetIdSelector.h:61
Accessor::_trackerTopology
const TrackerTopology & _trackerTopology
Definition: TrackerDetIdSelector.h:119
Exception.h
BinaryOP::left
ExpressionAST left
Definition: TrackerDetIdSelector.h:58
TrackerDetIdSelector::functionTable
const static StringFunctionMap functionTable
Definition: TrackerDetIdSelector.h:108
WalkAST::operator()
void operator()(UnaryOP const &expr) const
Definition: TrackerDetIdSelector.h:206
TrackerDetIdSelectorGrammar::comboRule
boost::spirit::qi::rule< ITERATOR, ExpressionAST(), boost::spirit::ascii::space_type, boost::spirit::qi::locals< ExpressionAST > > comboRule
Definition: TrackerDetIdSelector.h:228
ExpressionAST::expr
Type expr
Definition: TrackerDetIdSelector.h:44
ExpressionAST::ExpressionAST
ExpressionAST()
Definition: TrackerDetIdSelector.h:35
TrackerDetIdSelectorGrammar
Definition: TrackerDetIdSelector.h:218
TrackerDetIdSelector::_detId
const DetId & _detId
Definition: TrackerDetIdSelector.h:102
WalkAST::operator()
void operator()() const
Definition: TrackerDetIdSelector.h:159
BinaryOP::OP::NOT_EQUAL
WalkAST::WalkAST
WalkAST(const DetId &detId, const TrackerTopology &trackerTopology)
Definition: TrackerDetIdSelector.h:155
begin
#define begin
Definition: vmac.h:32
TrackerDetIdSelector::StringFunctionMap
std::unordered_map< std::string, DetIdFunction > StringFunctionMap
Definition: TrackerDetIdSelector.h:107
Accessor::Accessor
Accessor(const DetId &detId, const TrackerTopology &trackerTopology)
Definition: TrackerDetIdSelector.h:122
BinaryOP::OP::EQUAL
BinaryOP::OP
OP
Definition: TrackerDetIdSelector.h:57