LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
Selector.h
Go to the documentation of this file.
1 #ifndef art_Framework_Principal_Selector_h
2 #define art_Framework_Principal_Selector_h
3 
4 /*----------------------------------------------------------------------
5 
6 Classes for all "selector" objects, used to select
7 EDProducts based on information in the associated Provenance.
8 
9 Developers who make their own Selector class should inherit
10 from SelectorBase.
11 
12 Users can use the classes defined below
13 
14  ModuleLabelSelector
15  ProcessNameSelector
16  ProductInstanceNameSelector
17 
18 Users can also use the class Selector, which can be constructed given a
19 logical expression formed from any other selectors, combined with &&
20 (the AND operator), || (the OR operator) or ! (the NOT operator).
21 
22 For example, to select only products produced by a module with label
23 "mymodule" and made in the process "PROD", one can use:
24 
25  Selector s( ModuleLabelSelector("mymodule") &&
26  ProcessNameSelector("PROD") );
27 
28 If a module (EDProducter, EDFilter, EDAnalyzer, or OutputModule) is
29 to use such a selector, it is best to initialize it directly upon
30 construction of the module, rather than creating a new Selector instance
31 for every event.
32 
33 ----------------------------------------------------------------------*/
34 
38 #include "cetlib/value_ptr.h"
39 
40 #include <string>
41 #include <type_traits>
42 
43 namespace art {
44 
45  template <class A, class B>
49  operator&&(A const& a, B const& b);
50 
51  template <class A, class B>
53  std::is_base_of<art::SelectorBase, B>::value,
55  operator||(A const& a, B const& b);
56 
57  template <class A>
60  operator!(A const& a);
61 }
62 
63 //------------------------------------------------------------------
64 //
71 //------------------------------------------------------------------
72 
74 public:
75  ProcessNameSelector(const std::string& pn)
76  : pn_(pn.empty() ? std::string("*") : pn)
77  {}
78 
79  virtual ProcessNameSelector*
80  clone() const override
81  {
82  return new ProcessNameSelector(*this);
83  }
84 
85  std::string const&
86  name() const
87  {
88  return pn_;
89  }
90 
91 private:
92  virtual bool
93  doMatch(BranchDescription const& p) const override
94  {
95  return (pn_ == "*") || (p.processName() == pn_);
96  }
97 
98  std::string pn_;
99 };
100 
101 //------------------------------------------------------------------
102 //
105 //
106 //------------------------------------------------------------------
107 
109 public:
110  ProductInstanceNameSelector(const std::string& pin) : pin_(pin) {}
111 
113  clone() const override
114  {
115  return new ProductInstanceNameSelector(*this);
116  }
117 
118 private:
119  virtual bool
120  doMatch(BranchDescription const& p) const override
121  {
122  return p.productInstanceName() == pin_;
123  }
124 
125  std::string pin_;
126 };
127 
128 //------------------------------------------------------------------
129 //
132 //
133 //------------------------------------------------------------------
134 
136 public:
137  ModuleLabelSelector(const std::string& label) : label_(label) {}
138 
139  virtual ModuleLabelSelector*
140  clone() const override
141  {
142  return new ModuleLabelSelector(*this);
143  }
144 
145 private:
146  virtual bool
147  doMatch(BranchDescription const& p) const override
148  {
149  return p.moduleLabel() == label_;
150  }
151 
152  std::string label_;
153 };
154 
155 //------------------------------------------------------------------
156 //
159 //
160 //------------------------------------------------------------------
161 
163 public:
165 
166  virtual MatchAllSelector*
167  clone() const override
168  {
169  return new MatchAllSelector;
170  }
171 
172 private:
173  virtual bool
174  doMatch(BranchDescription const&) const override
175  {
176  return true;
177  }
178 };
179 
180 //----------------------------------------------------------
181 //
182 // AndHelper template.
183 // Used to form expressions involving && between other selectors.
184 //
185 //----------------------------------------------------------
186 
187 template <class A, class B>
188 class art::AndHelper : public SelectorBase {
189 public:
190  AndHelper(A const& a, B const& b) : a_(a), b_(b) {}
191  virtual AndHelper*
192  clone() const override
193  {
194  return new AndHelper(*this);
195  }
196 
197 private:
198  virtual bool
199  doMatch(BranchDescription const& p) const override
200  {
201  return a_.match(p) && b_.match(p);
202  }
203 
204  A a_;
205  B b_;
206 };
207 
208 template <class A, class B>
212 art::operator&&(A const& a, B const& b)
213 {
214  return art::AndHelper<A, B>(a, b);
215 }
216 
217 //----------------------------------------------------------
218 //
219 // OrHelper template.
220 // Used to form expressions involving || between other selectors.
221 //
222 //----------------------------------------------------------
223 
224 template <class A, class B>
225 class art::OrHelper : public art::SelectorBase {
226 public:
227  OrHelper(A const& a, B const& b) : a_(a), b_(b) {}
228  virtual OrHelper*
229  clone() const override
230  {
231  return new OrHelper(*this);
232  }
233 
234 private:
235  virtual bool
236  doMatch(BranchDescription const& p) const override
237  {
238  return a_.match(p) || b_.match(p);
239  }
240 
241  A a_;
242  B b_;
243 };
244 
245 template <class A, class B>
249 art::operator||(A const& a, B const& b)
250 {
251  return art::OrHelper<A, B>(a, b);
252 }
253 
254 //----------------------------------------------------------
255 //
256 // NotHelper template.
257 // Used to form expressions involving ! acting on a selector.
258 //
259 //----------------------------------------------------------
260 
261 template <class A>
262 class art::NotHelper : public art::SelectorBase {
263 public:
264  explicit NotHelper(A const& a) : a_(a) {}
265  NotHelper*
266  clone() const override
267  {
268  return new NotHelper(*this);
269  }
270 
271 private:
272  virtual bool
273  doMatch(BranchDescription const& p) const override
274  {
275  return !a_.match(p);
276  }
277 
278  A a_;
279 };
280 
281 template <class A>
284  art::operator!(A const& a)
285 {
286  return art::NotHelper<A>(a);
287 }
288 
289 //----------------------------------------------------------
290 //
291 // ComposedSelectorWrapper template
292 // Used to hold an expression formed from the various helpers.
293 //
294 //----------------------------------------------------------
295 
296 template <class T>
298 public:
299  typedef T wrapped_type;
300  explicit ComposedSelectorWrapper(T const& t) : expression_(t) {}
303  clone() const override
304  {
305  return new ComposedSelectorWrapper<T>(*this);
306  }
307 
308 private:
309  virtual bool
310  doMatch(BranchDescription const& p) const override
311  {
312  return expression_.match(p);
313  }
314 
315  wrapped_type expression_;
316 };
317 
318 //----------------------------------------------------------
319 //
320 // Selector
321 //
322 //----------------------------------------------------------
323 
325 public:
326  template <class T>
327  Selector(T const& expression);
328  Selector(Selector const& other);
329  Selector& operator=(Selector const& other) &;
330  void swap(Selector& other);
331  virtual ~Selector();
332  virtual Selector* clone() const override;
333 
334 private:
335  virtual bool doMatch(BranchDescription const& p) const override;
336 
337  cet::value_ptr<SelectorBase> sel_;
338 };
339 
340 template <class T>
341 art::Selector::Selector(T const& expression)
342  : sel_(new ComposedSelectorWrapper<T>(expression))
343 {}
344 
345 #endif /* art_Framework_Principal_Selector_h */
346 
347 // Local Variables:
348 // mode: c++
349 // End:
virtual ModuleLabelSelector * clone() const override
Definition: Selector.h:140
ModuleLabelSelector(const std::string &label)
Definition: Selector.h:137
AndHelper(A const &a, B const &b)
Definition: Selector.h:190
Selector(T const &expression)
Definition: Selector.h:341
virtual MatchAllSelector * clone() const override
Definition: Selector.h:167
virtual bool doMatch(BranchDescription const &p) const override
Definition: Selector.h:93
Int_t B
Definition: plot.C:25
virtual bool doMatch(BranchDescription const &p) const override
Definition: Selector.h:147
virtual OrHelper * clone() const override
Definition: Selector.h:229
STL namespace.
virtual bool doMatch(BranchDescription const &p) const override
Definition: Selector.h:310
virtual ProductInstanceNameSelector * clone() const override
Definition: Selector.h:113
ComposedSelectorWrapper< T > * clone() const override
Definition: Selector.h:303
ProcessNameSelector(const std::string &pn)
Definition: Selector.h:75
void swap(Handle< T > &a, Handle< T > &b)
virtual bool doMatch(BranchDescription const &p) const override
Definition: Selector.h:273
virtual bool doMatch(BranchDescription const &) const override
Definition: Selector.h:174
virtual ProcessNameSelector * clone() const override
Definition: Selector.h:80
std::string const & moduleLabel() const
virtual bool doMatch(BranchDescription const &p) const override
Definition: Selector.h:120
virtual AndHelper * clone() const override
Definition: Selector.h:192
cet::value_ptr< SelectorBase > sel_
Definition: Selector.h:337
std::enable_if_t< std::is_base_of< art::SelectorBase, A >::value &&std::is_base_of< art::SelectorBase, B >::value, art::AndHelper< A, B > > operator&&(A const &a, B const &b)
Definition: Selector.h:212
std::enable_if_t< std::is_base_of< art::SelectorBase, A >::value &&std::is_base_of< art::SelectorBase, B >::value, art::OrHelper< A, B > > operator||(A const &a, B const &b)
Definition: Selector.h:249
virtual bool doMatch(BranchDescription const &p) const override
Definition: Selector.h:199
std::string value(boost::any const &)
OrHelper(A const &a, B const &b)
Definition: Selector.h:227
ComposedSelectorWrapper(T const &t)
Definition: Selector.h:300
NotHelper * clone() const override
Definition: Selector.h:266
ProductInstanceNameSelector(const std::string &pin)
Definition: Selector.h:110
std::string const & productInstanceName() const
HLT enums.
NotHelper(A const &a)
Definition: Selector.h:264
std::string const & processName() const
std::enable_if_t< std::is_base_of< art::SelectorBase, A >::value, art::NotHelper< A > > operator!(A const &a)
Definition: Selector.h:284
std::string const & name() const
Definition: Selector.h:86
virtual bool doMatch(BranchDescription const &p) const override
Definition: Selector.h:236