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/variant/recursive_variant.hpp>
9 
10 #include <iostream>
11 #include <cstdlib>
12 #include <typeinfo>
13 #include <functional>
14 #include <unordered_map>
15 
16 struct BinaryOP;
17 struct UnaryOP;
18 struct ExpressionAST;
19 struct Nil {};
20 
21 namespace detail {
22  ExpressionAST opLesser(ExpressionAST const& lhs, ExpressionAST const& rhs);
23  ExpressionAST opLesserEq(ExpressionAST const& lhs, ExpressionAST const& rhs);
24  ExpressionAST opEq(ExpressionAST const& lhs, ExpressionAST const& rhs);
25  ExpressionAST opNotEq(ExpressionAST const& lhs, ExpressionAST const& rhs);
26  ExpressionAST opGreater(ExpressionAST const& lhs, ExpressionAST const& rhs);
27  ExpressionAST opGreaterEq(ExpressionAST const& lhs, ExpressionAST const& rhs);
28  ExpressionAST opAnd(ExpressionAST const& lhs, ExpressionAST const& rhs);
29  ExpressionAST opOr(ExpressionAST const& lhs, ExpressionAST const& rhs);
30 } // namespace detail
31 
32 struct ExpressionAST {
33  typedef boost::variant<Nil,
34  int,
36  boost::recursive_wrapper<ExpressionAST>,
37  boost::recursive_wrapper<BinaryOP>,
38  boost::recursive_wrapper<UnaryOP> >
40 
42 
43  template <typename Expr>
44  ExpressionAST(Expr const& expr) : expr(expr) {}
45 
46  int evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const;
47 
49 
51 
52  friend ExpressionAST operator>(ExpressionAST const& lhs, ExpressionAST const& rhs) {
53  return detail::opGreater(lhs, rhs);
54  }
55  friend ExpressionAST operator>=(ExpressionAST const& lhs, ExpressionAST const& rhs) {
56  return detail::opGreaterEq(lhs, rhs);
57  }
58  friend ExpressionAST operator==(ExpressionAST const& lhs, ExpressionAST const& rhs) { return detail::opEq(lhs, rhs); }
59  friend ExpressionAST operator!=(ExpressionAST const& lhs, ExpressionAST const& rhs) {
60  return detail::opNotEq(lhs, rhs);
61  }
62  friend ExpressionAST operator<(ExpressionAST const& lhs, ExpressionAST const& rhs) {
63  return detail::opLesser(lhs, rhs);
64  }
65  friend ExpressionAST operator<=(ExpressionAST const& lhs, ExpressionAST const& rhs) {
66  return detail::opLesserEq(lhs, rhs);
67  }
68  friend ExpressionAST operator&&(ExpressionAST const& lhs, ExpressionAST const& rhs) {
69  return detail::opAnd(lhs, rhs);
70  }
71  friend ExpressionAST operator||(ExpressionAST const& lhs, ExpressionAST const& rhs) { return detail::opOr(lhs, rhs); }
72 };
73 
74 struct BinaryOP {
78 
80 
81  int evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const {
82  switch (op) {
83  case OP::GREATER:
84  return left.evaluate(detId, trackerTopology) > right.evaluate(detId, trackerTopology);
85  case OP::GREATER_EQUAL:
86  return left.evaluate(detId, trackerTopology) >= right.evaluate(detId, trackerTopology);
87  case OP::EQUAL:
88  return left.evaluate(detId, trackerTopology) == right.evaluate(detId, trackerTopology);
89  case OP::LESS_EQUAL:
90  return left.evaluate(detId, trackerTopology) <= right.evaluate(detId, trackerTopology);
91  case OP::LESS:
92  return left.evaluate(detId, trackerTopology) < right.evaluate(detId, trackerTopology);
93  case OP::NOT_EQUAL:
94  return left.evaluate(detId, trackerTopology) != right.evaluate(detId, trackerTopology);
95  case OP::AND:
96  return left.evaluate(detId, trackerTopology) && right.evaluate(detId, trackerTopology);
97  case OP::OR:
98  return left.evaluate(detId, trackerTopology) || right.evaluate(detId, trackerTopology);
99  }
100  return 0;
101  }
102 };
103 
104 struct UnaryOP {
105  enum class OP { NEG } op;
108 
109  int evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const {
110  switch (op) {
111  case OP::NEG:
112  return !subject.evaluate(detId, trackerTopology);
113  }
114  return 0;
115  }
116 };
117 
119 private:
120  const DetId& _detId;
122 
123 public:
124  typedef std::function<int(const TrackerTopology& trackerTopology, const DetId&)> DetIdFunction;
125  typedef std::unordered_map<std::string, DetIdFunction> StringFunctionMap;
127 
128  TrackerDetIdSelector(const DetId& detId, const TrackerTopology& trackerTopology)
129  : _detId(detId), _trackerTopology(trackerTopology) {}
130 
131  bool passSelection(const std::string& selectionStr) const;
132 };
133 
134 class Accessor : public boost::static_visitor<int> {
135 private:
136  const DetId& _detId;
138 
139 public:
140  Accessor(const DetId& detId, const TrackerTopology& trackerTopology)
141  : _detId(detId), _trackerTopology(trackerTopology) {}
142 
143  int operator()(Nil i) const {
144  throw cms::Exception("FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
145  "while evaluating a DetId selection a symbol was not set");
146  }
147  int operator()(const int& i) const { return i; }
148  int operator()(const std::string& s) const {
149  TrackerDetIdSelector::StringFunctionMap::const_iterator it = TrackerDetIdSelector::functionTable.find(s);
150  int value = 0;
151  if (it != TrackerDetIdSelector::functionTable.cend()) {
152  value = (it->second)(_trackerTopology, _detId);
153  //std::cout<<"attr="<<s<<", value="<<value<<std::endl;
154  } else {
155  //std::cout<<"attr="<<s<<" unknown"<<std::endl;
156  std::string msg =
157  "error while parsing DetId selection: named identifier '" + s + "' not known. Possible values are: ";
159  msg += pair.first + ",";
160  }
161  throw cms::Exception("FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector", msg);
162  }
163  return value;
164  }
165  int operator()(const ExpressionAST& ast) const { return ast.evaluate(_detId, _trackerTopology); }
166  int operator()(const BinaryOP& binaryOP) const { return binaryOP.evaluate(_detId, _trackerTopology); }
167  int operator()(const UnaryOP& unaryOP) const { return unaryOP.evaluate(_detId, _trackerTopology); }
168 };
169 
170 struct WalkAST {
172 
173  WalkAST(const DetId& detId, const TrackerTopology& trackerTopology) : _acc(detId, trackerTopology) {}
174 
175  typedef void result_type;
176 
177  void operator()() const {}
178  void operator()(int n) const {
179  std::cout << n;
180  std::cout << " [" << _acc(n) << "] ";
181  }
182  void operator()(std::string str) const {
183  std::cout << str;
184  std::cout << " [" << _acc(str) << "] ";
185  }
186  void operator()(ExpressionAST const& ast) const {
187  boost::apply_visitor(*this, ast.expr);
188  std::cout << " [=" << _acc(ast) << "] ";
189  }
190 
191  void operator()(BinaryOP const& expr) const {
192  std::cout << "(";
193  boost::apply_visitor(*this, expr.left.expr);
194  switch (expr.op) {
196  std::cout << " > ";
197  break;
199  std::cout << " >= ";
200  break;
201  case BinaryOP::OP::EQUAL:
202  std::cout << " == ";
203  break;
205  std::cout << " <= ";
206  break;
207  case BinaryOP::OP::LESS:
208  std::cout << " < ";
209  break;
211  std::cout << " != ";
212  break;
213  case BinaryOP::OP::AND:
214  std::cout << " && ";
215  break;
216  case BinaryOP::OP::OR:
217  std::cout << " || ";
218  break;
219  }
220  boost::apply_visitor(*this, expr.right.expr);
221  std::cout << ')';
222  }
223 
224  void operator()(UnaryOP const& expr) const {
225  switch (expr.op) {
226  case UnaryOP::OP::NEG:
227  std::cout << " !(";
228  break;
229  }
230  boost::apply_visitor(*this, expr.subject.expr);
231  std::cout << ')';
232  }
233 };
234 
235 inline int ExpressionAST::evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const {
236  return boost::apply_visitor(Accessor(detId, trackerTopology), this->expr);
237 }
238 
239 #endif
ExpressionAST opGreaterEq(ExpressionAST const &lhs, ExpressionAST const &rhs)
ExpressionAST left
friend ExpressionAST operator!=(ExpressionAST const &lhs, ExpressionAST const &rhs)
WalkAST(const DetId &detId, const TrackerTopology &trackerTopology)
void operator()(std::string str) const
bool passSelection(const std::string &selectionStr) const
ExpressionAST right
friend ExpressionAST operator<=(ExpressionAST const &lhs, ExpressionAST const &rhs)
void operator()(UnaryOP const &expr) const
int operator()(const UnaryOP &unaryOP) const
enum UnaryOP::OP op
ExpressionAST opLesserEq(ExpressionAST const &lhs, ExpressionAST const &rhs)
Accessor(const DetId &detId, const TrackerTopology &trackerTopology)
ExpressionAST & operator!()
friend ExpressionAST operator||(ExpressionAST const &lhs, ExpressionAST const &rhs)
int operator()(const BinaryOP &binaryOP) const
friend ExpressionAST operator>(ExpressionAST const &lhs, ExpressionAST const &rhs)
void operator()(int n) const
static const StringFunctionMap functionTable
const DetId & _detId
int operator()(const int &i) const
const TrackerTopology & _trackerTopology
friend ExpressionAST operator<(ExpressionAST const &lhs, ExpressionAST const &rhs)
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
ExpressionAST opOr(ExpressionAST const &lhs, ExpressionAST const &rhs)
void operator()(ExpressionAST const &ast) const
void operator()(BinaryOP const &expr) const
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
std::function< int(const TrackerTopology &trackerTopology, const DetId &)> DetIdFunction
friend ExpressionAST operator==(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: value.py:1
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
ExpressionAST opAnd(ExpressionAST const &lhs, ExpressionAST const &rhs)
ExpressionAST opLesser(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: DetId.h:17
ExpressionAST subject
int operator()(Nil i) const
tuple msg
Definition: mps_check.py:286
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
ExpressionAST(Expr const &expr)
friend ExpressionAST operator>=(ExpressionAST const &lhs, ExpressionAST const &rhs)
const TrackerTopology & _trackerTopology
TrackerDetIdSelector(const DetId &detId, const TrackerTopology &trackerTopology)
BinaryOP(OP op, ExpressionAST const &left, ExpressionAST const &right)
#define str(s)
ExpressionAST opNotEq(ExpressionAST const &lhs, ExpressionAST const &rhs)
void operator()() const
friend ExpressionAST operator &&(ExpressionAST const &lhs, ExpressionAST const &rhs)
int operator()(const std::string &s) const
int operator()(const ExpressionAST &ast) const
ExpressionAST opEq(ExpressionAST const &lhs, ExpressionAST const &rhs)
enum BinaryOP::OP op
UnaryOP(OP op, ExpressionAST const &subject)
ExpressionAST opGreater(ExpressionAST const &lhs, ExpressionAST const &rhs)