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 Nil {};
19 
20 struct ExpressionAST {
21  typedef boost::variant<Nil,
22  int,
24  boost::recursive_wrapper<ExpressionAST>,
25  boost::recursive_wrapper<BinaryOP>,
26  boost::recursive_wrapper<UnaryOP> >
28 
30 
31  template <typename Expr>
32  ExpressionAST(Expr const& expr) : expr(expr) {}
33 
34  int evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const;
35 
37 
39 };
40 
41 ExpressionAST operator>(ExpressionAST const& lhs, ExpressionAST const& rhs);
42 ExpressionAST operator>=(ExpressionAST const& lhs, ExpressionAST const& rhs);
43 ExpressionAST operator==(ExpressionAST const& lhs, ExpressionAST const& rhs);
44 ExpressionAST operator<=(ExpressionAST const& lhs, ExpressionAST const& rhs);
45 ExpressionAST operator<(ExpressionAST const& lhs, ExpressionAST const& rhs);
46 ExpressionAST operator!=(ExpressionAST const& lhs, ExpressionAST const& rhs);
47 ExpressionAST operator&&(ExpressionAST const& lhs, ExpressionAST const& rhs);
48 ExpressionAST operator||(ExpressionAST const& lhs, ExpressionAST const& rhs);
49 
50 struct BinaryOP {
54 
56 
57  int evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const {
58  switch (op) {
59  case OP::GREATER:
60  return left.evaluate(detId, trackerTopology) > right.evaluate(detId, trackerTopology);
61  case OP::GREATER_EQUAL:
62  return left.evaluate(detId, trackerTopology) >= right.evaluate(detId, trackerTopology);
63  case OP::EQUAL:
64  return left.evaluate(detId, trackerTopology) == right.evaluate(detId, trackerTopology);
65  case OP::LESS_EQUAL:
66  return left.evaluate(detId, trackerTopology) <= right.evaluate(detId, trackerTopology);
67  case OP::LESS:
68  return left.evaluate(detId, trackerTopology) < right.evaluate(detId, trackerTopology);
69  case OP::NOT_EQUAL:
70  return left.evaluate(detId, trackerTopology) != right.evaluate(detId, trackerTopology);
71  case OP::AND:
72  return left.evaluate(detId, trackerTopology) && right.evaluate(detId, trackerTopology);
73  case OP::OR:
74  return left.evaluate(detId, trackerTopology) || right.evaluate(detId, trackerTopology);
75  }
76  return 0;
77  }
78 };
79 
80 struct UnaryOP {
81  enum class OP { NEG } op;
84 
85  int evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const {
86  switch (op) {
87  case OP::NEG:
88  return !subject.evaluate(detId, trackerTopology);
89  }
90  return 0;
91  }
92 };
93 
95 private:
96  const DetId& _detId;
98 
99 public:
100  typedef std::function<int(const TrackerTopology& trackerTopology, const DetId&)> DetIdFunction;
101  typedef std::unordered_map<std::string, DetIdFunction> StringFunctionMap;
103 
104  TrackerDetIdSelector(const DetId& detId, const TrackerTopology& trackerTopology)
105  : _detId(detId), _trackerTopology(trackerTopology) {}
106 
107  bool passSelection(const std::string& selectionStr) const;
108 };
109 
110 class Accessor : public boost::static_visitor<int> {
111 private:
112  const DetId& _detId;
114 
115 public:
116  Accessor(const DetId& detId, const TrackerTopology& trackerTopology)
117  : _detId(detId), _trackerTopology(trackerTopology) {}
118 
119  int operator()(Nil i) const {
120  throw cms::Exception("FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector",
121  "while evaluating a DetId selection a symbol was not set");
122  }
123  int operator()(const int& i) const { return i; }
124  int operator()(const std::string& s) const {
125  TrackerDetIdSelector::StringFunctionMap::const_iterator it = TrackerDetIdSelector::functionTable.find(s);
126  int value = 0;
127  if (it != TrackerDetIdSelector::functionTable.cend()) {
128  value = (it->second)(_trackerTopology, _detId);
129  //std::cout<<"attr="<<s<<", value="<<value<<std::endl;
130  } else {
131  //std::cout<<"attr="<<s<<" unknown"<<std::endl;
132  std::string msg =
133  "error while parsing DetId selection: named identifier '" + s + "' not known. Possible values are: ";
135  msg += pair.first + ",";
136  }
137  throw cms::Exception("FastSimulation/TrackingRecHitProducer/TrackerDetIdSelector", msg);
138  }
139  return value;
140  }
141  int operator()(const ExpressionAST& ast) const { return ast.evaluate(_detId, _trackerTopology); }
142  int operator()(const BinaryOP& binaryOP) const { return binaryOP.evaluate(_detId, _trackerTopology); }
143  int operator()(const UnaryOP& unaryOP) const { return unaryOP.evaluate(_detId, _trackerTopology); }
144 };
145 
146 struct WalkAST {
148 
149  WalkAST(const DetId& detId, const TrackerTopology& trackerTopology) : _acc(detId, trackerTopology) {}
150 
151  typedef void result_type;
152 
153  void operator()() const {}
154  void operator()(int n) const {
155  std::cout << n;
156  std::cout << " [" << _acc(n) << "] ";
157  }
158  void operator()(std::string str) const {
159  std::cout << str;
160  std::cout << " [" << _acc(str) << "] ";
161  }
162  void operator()(ExpressionAST const& ast) const {
163  boost::apply_visitor(*this, ast.expr);
164  std::cout << " [=" << _acc(ast) << "] ";
165  }
166 
167  void operator()(BinaryOP const& expr) const {
168  std::cout << "(";
169  boost::apply_visitor(*this, expr.left.expr);
170  switch (expr.op) {
172  std::cout << " > ";
173  break;
175  std::cout << " >= ";
176  break;
177  case BinaryOP::OP::EQUAL:
178  std::cout << " == ";
179  break;
181  std::cout << " <= ";
182  break;
183  case BinaryOP::OP::LESS:
184  std::cout << " < ";
185  break;
187  std::cout << " != ";
188  break;
189  case BinaryOP::OP::AND:
190  std::cout << " && ";
191  break;
192  case BinaryOP::OP::OR:
193  std::cout << " || ";
194  break;
195  }
196  boost::apply_visitor(*this, expr.right.expr);
197  std::cout << ')';
198  }
199 
200  void operator()(UnaryOP const& expr) const {
201  switch (expr.op) {
202  case UnaryOP::OP::NEG:
203  std::cout << " !(";
204  break;
205  }
206  boost::apply_visitor(*this, expr.subject.expr);
207  std::cout << ')';
208  }
209 };
210 
211 inline int ExpressionAST::evaluate(const DetId& detId, const TrackerTopology& trackerTopology) const {
212  return boost::apply_visitor(Accessor(detId, trackerTopology), this->expr);
213 }
214 
215 #endif
WalkAST::operator()
void operator()(std::string str) const
Definition: TrackerDetIdSelector.h:158
UnaryOP::subject
ExpressionAST subject
Definition: TrackerDetIdSelector.h:82
BinaryOP::OP::LESS
Accessor::operator()
int operator()(const ExpressionAST &ast) const
Definition: TrackerDetIdSelector.h:141
operator>
ExpressionAST operator>(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:83
Accessor::operator()
int operator()(const BinaryOP &binaryOP) const
Definition: TrackerDetIdSelector.h:142
Nil
Definition: TrackerDetIdSelector.h:18
operator<
ExpressionAST operator<(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:103
mps_fire.i
i
Definition: mps_fire.py:428
ExpressionAST::ExpressionAST
ExpressionAST(Expr const &expr)
Definition: TrackerDetIdSelector.h:32
operator<=
ExpressionAST operator<=(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:98
ExpressionAST::Type
boost::variant< Nil, int, std::string, boost::recursive_wrapper< ExpressionAST >, boost::recursive_wrapper< BinaryOP >, boost::recursive_wrapper< UnaryOP > > Type
Definition: TrackerDetIdSelector.h:27
ExpressionAST
Definition: TrackerDetIdSelector.h:20
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
UnaryOP::op
enum UnaryOP::OP op
Accessor::_detId
const DetId & _detId
Definition: TrackerDetIdSelector.h:112
WalkAST
Definition: TrackerDetIdSelector.h:146
TrackerTopology
Definition: TrackerTopology.h:16
gather_cfg.cout
cout
Definition: gather_cfg.py:144
BinaryOP::OP::LESS_EQUAL
WalkAST::operator()
void operator()(ExpressionAST const &ast) const
Definition: TrackerDetIdSelector.h:162
TrackerDetIdSelector::TrackerDetIdSelector
TrackerDetIdSelector(const DetId &detId, const TrackerTopology &trackerTopology)
Definition: TrackerDetIdSelector.h:104
operator!=
ExpressionAST operator!=(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:108
mps_check.msg
tuple msg
Definition: mps_check.py:285
Accessor::operator()
int operator()(Nil i) const
Definition: TrackerDetIdSelector.h:119
BinaryOP::evaluate
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
Definition: TrackerDetIdSelector.h:57
ExpressionAST::evaluate
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
Definition: TrackerDetIdSelector.h:211
operator>=
ExpressionAST operator>=(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:88
TrackerDetIdSelector::_trackerTopology
const TrackerTopology & _trackerTopology
Definition: TrackerDetIdSelector.h:97
UnaryOP::UnaryOP
UnaryOP(OP op, ExpressionAST const &subject)
Definition: TrackerDetIdSelector.h:83
trigObjTnPSource_cfi.selectionStr
selectionStr
Definition: trigObjTnPSource_cfi.py:69
Accessor::operator()
int operator()(const std::string &s) const
Definition: TrackerDetIdSelector.h:124
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:81
WalkAST::result_type
void result_type
Definition: TrackerDetIdSelector.h:151
UnaryOP::evaluate
int evaluate(const DetId &detId, const TrackerTopology &trackerTopology) const
Definition: TrackerDetIdSelector.h:85
TrackerDetIdSelector
Definition: TrackerDetIdSelector.h:94
str
#define str(s)
Definition: TestProcessor.cc:53
operator||
ExpressionAST operator||(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:118
UnaryOP
Definition: TrackerDetIdSelector.h:80
Accessor::operator()
int operator()(const UnaryOP &unaryOP) const
Definition: TrackerDetIdSelector.h:143
BinaryOP::op
enum BinaryOP::OP op
TrackerDetIdSelector::DetIdFunction
std::function< int(const TrackerTopology &trackerTopology, const DetId &)> DetIdFunction
Definition: TrackerDetIdSelector.h:100
BinaryOP::OP::GREATER_EQUAL
Accessor::operator()
int operator()(const int &i) const
Definition: TrackerDetIdSelector.h:123
UnaryOP::OP::NEG
operator==
ExpressionAST operator==(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:93
BinaryOP::right
ExpressionAST right
Definition: TrackerDetIdSelector.h:53
createfilelist.int
int
Definition: createfilelist.py:10
value
Definition: value.py:1
operator&&
ExpressionAST operator&&(ExpressionAST const &lhs, ExpressionAST const &rhs)
Definition: TrackerDetIdSelector.cc:113
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
Accessor
Definition: TrackerDetIdSelector.h:110
WalkAST::operator()
void operator()(BinaryOP const &expr) const
Definition: TrackerDetIdSelector.h:167
BinaryOP::OP::GREATER
TrackerDetIdSelector::passSelection
bool passSelection(const std::string &selectionStr) const
Definition: TrackerDetIdSelector.cc:166
jets_cff.expr
expr
Definition: jets_cff.py:497
DetId.h
ExpressionAST::operator!
ExpressionAST & operator!()
Definition: TrackerDetIdSelector.cc:123
WalkAST::_acc
Accessor _acc
Definition: TrackerDetIdSelector.h:147
relativeConstraints.value
value
Definition: relativeConstraints.py:53
Exception
Definition: hltDiff.cc:245
WalkAST::operator()
void operator()(int n) const
Definition: TrackerDetIdSelector.h:154
BinaryOP
Definition: TrackerDetIdSelector.h:50
HiBiasedCentrality_cfi.function
function
Definition: HiBiasedCentrality_cfi.py:4
BinaryOP::BinaryOP
BinaryOP(OP op, ExpressionAST const &left, ExpressionAST const &right)
Definition: TrackerDetIdSelector.h:55
Accessor::_trackerTopology
const TrackerTopology & _trackerTopology
Definition: TrackerDetIdSelector.h:113
Exception.h
BinaryOP::left
ExpressionAST left
Definition: TrackerDetIdSelector.h:52
TrackerDetIdSelector::functionTable
const static StringFunctionMap functionTable
Definition: TrackerDetIdSelector.h:102
WalkAST::operator()
void operator()(UnaryOP const &expr) const
Definition: TrackerDetIdSelector.h:200
ExpressionAST::expr
Type expr
Definition: TrackerDetIdSelector.h:38
ExpressionAST::ExpressionAST
ExpressionAST()
Definition: TrackerDetIdSelector.h:29
TrackerDetIdSelector::_detId
const DetId & _detId
Definition: TrackerDetIdSelector.h:96
WalkAST::operator()
void operator()() const
Definition: TrackerDetIdSelector.h:153
BinaryOP::OP::NOT_EQUAL
WalkAST::WalkAST
WalkAST(const DetId &detId, const TrackerTopology &trackerTopology)
Definition: TrackerDetIdSelector.h:149
TrackerDetIdSelector::StringFunctionMap
std::unordered_map< std::string, DetIdFunction > StringFunctionMap
Definition: TrackerDetIdSelector.h:101
Accessor::Accessor
Accessor(const DetId &detId, const TrackerTopology &trackerTopology)
Definition: TrackerDetIdSelector.h:116
BinaryOP::OP::EQUAL
BinaryOP::OP
OP
Definition: TrackerDetIdSelector.h:51