CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EventHypothesis.h
Go to the documentation of this file.
1 #ifndef DataFormats_EventHypothesis_interface_EventHypothesis_h
2 #define DataFormats_EventHypothesis_interface_EventHypothesis_h
3 
6 #include <boost/regex.hpp>
7 #include <boost/shared_ptr.hpp>
8 
9 namespace pat {
10  // forward declaration
11  namespace eventhypothesis { template<typename T> struct Looper; }
12  // real declarations
13  namespace eventhypothesis {
14  // typedef for the Ref
15  typedef reco::CandidatePtr CandRefType; // Ptr one day in the future
16  // filter
17  struct ParticleFilter {
18  virtual ~ParticleFilter() {}
19  bool operator()(const std::pair<std::string, CandRefType> &p) const { return operator()(p.second, p.first); }
20  virtual bool operator()(const CandRefType &cand, const std::string &role) const = 0;
21  };
22  // smart pointer to the filter
23  typedef boost::shared_ptr<const ParticleFilter> ParticleFilterPtr;
24  }
25  // the class
27  public:
29  typedef std::pair<std::string, CandRefType> value_type;
30  typedef std::vector<value_type> vector_type;
31  typedef vector_type::const_iterator const_iterator;
32  typedef vector_type::const_reverse_iterator const_reverse_iterator;
34 
35  void add(const CandRefType &ref, const std::string &role) ;
36 
37  const_iterator begin() const { return particles_.begin(); }
38  const_iterator end() const { return particles_.end(); }
39  const_reverse_iterator rbegin() const { return particles_.rbegin(); }
40  const_reverse_iterator rend() const { return particles_.rend(); }
41 
42  class ByRole {
43  public:
44  ByRole(const std::string &role) : role_(role) {}
45  bool operator()(const value_type &p) const { return p.first == role_; }
46  private:
47  const std::string &role_;
48  };
49 
52 
53  const CandRefType & get(const std::string &role, int index=0) const ;
54  const CandRefType & get(const ParticleFilter &filter, int index=0) const ;
55  template<typename T> const T * getAs(const std::string &role, int index=0) const ;
56  template<typename T> const T * getAs(const ParticleFilter &filter, int index=0) const ;
57  const CandRefType & operator[](const std::string &role) const { return get(role,0); }
58  const CandRefType & operator[](const ParticleFilter &filter) const { return get(filter,0); }
59 
61  std::vector<CandRefType> all(const std::string &roleRegexp) const;
63  std::vector<CandRefType> all(const ParticleFilter &filter) const;
64 
65  size_t count() const { return particles_.size(); }
67  size_t count(const std::string &roleRegexp) const;
69  size_t count(const ParticleFilter &role) const;
70 
72  CandLooper loop() const ;
74  CandLooper loop(const std::string &roleRegexp) const;
77  CandLooper loop(const ParticleFilter &filter) const;
81  CandLooper loop(const ParticleFilter *filter) const;
84 
85 
86 
88  template<typename T> eventhypothesis::Looper<T> loopAs(const std::string &roleRegexp) const ;
91  template<typename T> eventhypothesis::Looper<T> loopAs(const ParticleFilter &filter) const;
95  template<typename T> eventhypothesis::Looper<T> loopAs(const ParticleFilter *filter) const;
97  template<typename T> eventhypothesis::Looper<T> loopAs(const ParticleFilterPtr &filter) const;
98  private:
99  template<typename Iterator, typename Predicate>
100  Iterator realGet(const Iterator &realBegin, const Iterator &realEnd, const Predicate &p, size_t idx) const ;
101 
102  std::vector<value_type> particles_;
103 
104  } ;
105 
106  namespace eventhypothesis {
107  struct AcceptAllFilter : public ParticleFilter {
108  static const AcceptAllFilter & get() { static AcceptAllFilter dummyFilter; return dummyFilter; }
109  virtual bool operator()(const CandRefType &cand, const std::string &role) const { return true; }
110  };
112  public:
113  explicit RoleRegexpFilter(const std::string &roleRegexp) : re_(roleRegexp) {}
114  virtual bool operator()(const CandRefType &cand, const std::string &role) const {
115  return boost::regex_match(role, re_);
116  }
117  private:
118  boost::regex re_;
119  };
120  }
121 
122  template<typename Iterator, typename Predicate>
123  Iterator EventHypothesis::realGet(const Iterator &realBegin, const Iterator &realEnd, const Predicate &pred, size_t idx) const
124  {
125  Iterator it = realBegin;
126  while (it != realEnd) {
127  if (pred(*it)) {
128  if (idx == 0) return it;
129  idx--;
130  }
131  ++it;
132  }
133  return it;
134  }
135 
136  template<typename T>
137  const T *
138  EventHypothesis::getAs(const std::string &role, int index) const
139  {
140  CandRefType ref = get(role, index);
141  const T* ret = dynamic_cast<const T*>(ref.get());
142  if ((ret == 0) && (ref.get() != 0)) throw cms::Exception("Type Checking") <<
143  "You can't convert a " << typeid(*ref).name() << " to a " << typeid(T).name() << "\n" <<
144  "note: you can use c++filt command to convert the above in human readable types.\n";
145  return ret;
146  }
147  template<typename T>
148  const T *
150  {
151  CandRefType ref = get(filter, index);
152  const T* ret = dynamic_cast<const T*>(ref.get());
153  if ((ret == 0) && (ref.get() != 0)) throw cms::Exception("Type Checking") <<
154  "You can't convert a " << typeid(*ref).name() << " to a " << typeid(T).name() << "\n" <<
155  "note: you can use c++filt command to convert the above in human readable types.\n";
156  return ret;
157  }
158  template<typename T>
160  EventHypothesis::loopAs(const std::string &roleRegexp) const
161  {
162  return loopAs<T>(new pat::eventhypothesis::RoleRegexpFilter(roleRegexp));
163  }
164 
165  template<typename T>
168  {
169  return pat::eventhypothesis::Looper<T>(*this, role);
170  }
171 
172  template<typename T>
175  {
176  return pat::eventhypothesis::Looper<T>(*this, role);
177  }
178 
179  template<typename T>
182  {
183  return pat::eventhypothesis::Looper<T>(*this, role);
184  }
185 }
186 
187 #endif
const std::string & role_
CandLooper loop() const
Loops over all particles.
eventhypothesis::ParticleFilter ParticleFilter
std::vector< value_type > particles_
eventhypothesis::Looper< T > loopAs(const std::string &roleRegexp) const
Loops over particles which have certaint roles.
std::pair< std::string, CandRefType > value_type
Iterator realGet(const Iterator &realBegin, const Iterator &realEnd, const Predicate &p, size_t idx) const
RoleRegexpFilter(const std::string &roleRegexp)
size_t count() const
void add(const CandRefType &ref, const std::string &role)
std::vector< CandRefType > all(const std::string &roleRegexp) const
Return EDM references to all particles which have certaint roles.
const T * getAs(const std::string &role, int index=0) const
boost::shared_ptr< const ParticleFilter > ParticleFilterPtr
const CandRefType & operator[](const std::string &role) const
reco::CandidatePtr CandRefType
const_iterator end() const
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:143
const CandRefType & operator[](const ParticleFilter &filter) const
eventhypothesis::CandRefType CandRefType
bool operator()(const value_type &p) const
eventhypothesis::Looper< reco::Candidate > CandLooper
std::vector< value_type > vector_type
eventhypothesis::ParticleFilterPtr ParticleFilterPtr
vector_type::const_iterator const_iterator
const_reverse_iterator rend() const
ByRole(const std::string &role)
virtual bool operator()(const CandRefType &cand, const std::string &role) const
virtual bool operator()(const CandRefType &cand, const std::string &role) const
long double T
const_iterator begin() const
vector_type::const_reverse_iterator const_reverse_iterator
bool operator()(const std::pair< std::string, CandRefType > &p) const
const_reverse_iterator rbegin() const