test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EventHypothesisLooper.h
Go to the documentation of this file.
1 #ifndef DataFormats_EventHypothesis_interface_EventHypothesisLooper_h
2 #define DataFormats_EventHypothesis_interface_EventHypothesisLooper_h
3 
5 #include <algorithm>
6 
7 namespace pat { namespace eventhypothesis {
8  template<typename T>
9  class DynCastCandPtr {
10  public:
11  const T *get(const reco::Candidate *ptr) ;
12  void clearCache() { isPtrCached_ = false; }
13  bool typeOk(const reco::Candidate *ptr) { doPtr(ptr); return cachePtr_ != 0; }
14  private:
15  void doPtr(const reco::Candidate *ptr) ;
17  const T * cachePtr_;
18  };
19  template<typename T>
21  if (!isPtrCached_) {
22  cachePtr_ = dynamic_cast<const T *>(ptr);
23  isPtrCached_ = true;
24  }
25  }
26  template<typename T>
28  doPtr(ptr);
29  if ((ptr != 0) && (cachePtr_ == 0)) throw cms::Exception("Type Checking") <<
30  "You can't convert a " << typeid(*ptr).name() << " to a " << typeid(T).name() << "\n" <<
31  "note: you can use c++filt command to convert the above in human readable types.\n";
32  return cachePtr_;
33  }
34 
35  template<>
37  const reco::Candidate *get(const reco::Candidate *ptr) { return ptr; }
38  void clearCache() {}
39  bool typeOk(const reco::Candidate *ptr) { return true; }
40  };
41 
42  template<typename T>
43  class Looper {
44  public:
47  Looper(const EventHypothesis &eh, const ParticleFilter &filter) ;
48 
51  Looper(const EventHypothesis &eh, const ParticleFilter *filter) ;
54  Looper(const EventHypothesis &eh, const ParticleFilterPtr &filter) ;
55  ~Looper() {}
56 
58  const T & operator*() const { return ptr_.get(iter_->second.get()); }
60  const T * operator->() const { return ptr_.get(iter_->second.get()); }
62  const T * get() const { return ptr_.get(iter_->second.get()); }
63 
65  bool isTypeOk() const { return ptr_.typeOk(iter_->second.get()); }
66 
68  const std::string & role() const { return iter_->first; }
70  const CandRefType & ref () const { return iter_->second; }
72  const reco::Candidate & cand () const { return *iter_->second; }
73 
75  size_t globalIndex() { return iter_ - eh_.begin(); }
77  size_t index() const { return num_; }
79  size_t size() const { if (total_ < 0) realSize(); return total_; }
80 
82  Looper & operator++() ;
84  Looper & operator--() ;
86  Looper & skip(int delta) ;
89  Looper & reset(int item=0) ;
90 
93  operator bool() const ;
94 
96  template<typename T2> bool operator==(const Looper<T2> &other) const { return iter_ == other.iter_; }
97  template<typename T2> bool operator!=(const Looper<T2> &other) const { return iter_ != other.iter_; }
98  template<typename T2> bool operator<=(const Looper<T2> &other) const { return iter_ <= other.iter_; }
99  template<typename T2> bool operator>=(const Looper<T2> &other) const { return iter_ >= other.iter_; }
100  template<typename T2> bool operator<(const Looper<T2> &other) const { return iter_ < other.iter_; }
101  template<typename T2> bool operator>(const Looper<T2> &other) const { return iter_ > other.iter_; }
102 
103  private:
104  struct null_deleter{ void operator()(void const *) const { } };
106 
107  void first() ;
108  void realSize() const ;
109  bool assertOk() const;
110 
114  int num_;
115  mutable int total_; // mutable as it is not computed unless needed
117  };
119 
120  template<typename T>
122  eh_(eh), filter_(ParticleFilterPtr(&filter, typename Looper<T>::null_deleter())), total_(-1)
123  {
124  first();
125  }
126 
127  template<typename T>
129  eh_(eh), filter_(filter), total_(-1)
130  {
131  first();
132  }
133 
134  template<typename T>
136  eh_(eh), filter_(filter), total_(-1)
137  {
138  first();
139  }
140 
141 
142  template<typename T>
143  bool Looper<T>::assertOk() const {
144  assert(iter_ <= eh_.end());
145  assert((iter_+1) >= eh_.begin());
146  assert((iter_ < eh_.begin()) || (iter_ == eh_.end()) || ((*filter_)(*iter_)));
147  return true;
148  }
149 
150  template<typename T>
152  ptr_.clearCache();
153  assert(assertOk());
154  if (iter_ == eh_.end()) return *this;
155  do {
156  ++iter_;
157  if (iter_ == eh_.end()) break;
158  if ((*filter_)(*iter_)) {
159  assert(assertOk());
160  ++num_; return *this;
161  }
162  } while (true);
163  assert(assertOk());
164  return *this;
165  }
166  template<typename T>
168  ptr_.clearCache();
169  assert(assertOk());
170  if (num_ < 0) return *this;
171  do {
172  --iter_;
173  if (iter_ < eh_.begin()) { num_ = -1; break; }
174  if ((*filter_)(*iter_)) {
175  assert(assertOk());
176  --num_; return *this;
177  }
178  } while (true);
179  assert(assertOk());
180  return *this;
181  }
182 
183  template<typename T>
185  assert(assertOk());
186  std::advance(this, delta);
187  assert(assertOk());
188  return *this;
189  }
190 
191  template<typename T>
193  assert(assertOk());
194  if (item >= 0) {
195  first();
196  std::advance(this, item);
197  } else {
198  num_ = item + 1; iter_ = eh_.end();
199  std::advance(this, item);
200  }
201  assert(assertOk());
202  return *this;
203  }
204 
205  template<typename T>
207  num_ = 0;
208  iter_ = eh_.begin();
209  ptr_.clearCache();
210  for (; iter_ != eh_.end(); ++iter_) {
211  if ((*filter_)(*iter_)) break;
212  }
213  assert(assertOk());
214  }
215 
216  template<typename T>
217  Looper<T>::operator bool() const {
218  return (iter_ < eh_.end()) && (iter_ >= eh_.begin());
219  }
220 
221  template<typename T>
222  void Looper<T>::realSize() const {
224  if (it < eh_.begin()) {
225  it = eh_.begin(); total_ = 0;
226  } else {
227  total_ = num_;
228  }
229  for (; it != eh_.end(); ++it) {
230  if ((*filter_)(*it)) ++total_;
231  }
232  }
233 } } // namespaces
234 
235 #endif
dbl * delta
Definition: mlp_gen.cc:36
const T * get(const reco::Candidate *ptr)
bool isTypeOk() const
test if the type is correct
const T * operator->() const
Accessor as if it was a const_iterator on a list of T.
bool typeOk(const reco::Candidate *ptr)
const CandRefType & ref() const
EDM Ref to pointed particle.
const char * ptr_
Definition: DataKey.cc:90
bool operator!=(const Looper< T2 > &other) const
assert(m_qm.get())
Looper(const EventHypothesis &eh, const ParticleFilter &filter)
bool operator==(const Looper< T2 > &other) const
returns true if loopers point to the same record
size_t globalIndex()
Index of this item in the full EventHypothesis.
boost::shared_ptr< const ParticleFilter > ParticleFilterPtr
const reco::Candidate & cand() const
C++ reference to pointed particle.
const std::string & role() const
Role of pointed item.
size_t index() const
Index of this item among those in the loop.
const T & operator*() const
Accessor as if it was a const_iterator on a list of T.
void doPtr(const reco::Candidate *ptr)
EventHypothesis::const_iterator const_iterator
vector_type::const_iterator const_iterator
bool operator>=(const Looper< T2 > &other) const
Looper & skip(int delta)
skip (might be slow)
Looper< reco::Candidate > CandLooper
const ParticleFilterPtr filter_
long double T
const_iterator begin() const
bool operator>(const Looper< T2 > &other) const
size_t size() const
Number of particles in the loop.