CMS 3D CMS Logo

EventHypothesisLooper.h
Go to the documentation of this file.
1 #ifndef DataFormats_EventHypothesis_interface_EventHypothesisLooper_h
2 #define DataFormats_EventHypothesis_interface_EventHypothesisLooper_h
3 
6 #include <algorithm>
7 
8 namespace pat { namespace eventhypothesis {
9  template<typename T>
11  public:
12  const T *get(const reco::Candidate *ptr) ;
13  void clearCache() { isPtrCached_ = false; }
14  bool typeOk(const reco::Candidate *ptr) { doPtr(ptr); return cachePtr_ != 0; }
15  private:
16  void doPtr(const reco::Candidate *ptr) ;
18  const T * cachePtr_;
19  };
20  template<typename T>
22  if (!isPtrCached_) {
23  cachePtr_ = dynamic_cast<const T *>(ptr);
24  isPtrCached_ = true;
25  }
26  }
27  template<typename T>
29  doPtr(ptr);
30  if ((ptr != nullptr) && (cachePtr_ == 0)) throw cms::Exception("Type Checking") <<
31  "You can't convert a " << typeid(*ptr).name() << " to a " << typeid(T).name() << "\n" <<
32  "note: you can use c++filt command to convert the above in human readable types.\n";
33  return cachePtr_;
34  }
35 
36  template<>
38  const reco::Candidate *get(const reco::Candidate *ptr) { return ptr; }
39  void clearCache() {}
40  bool typeOk(const reco::Candidate *ptr) { return true; }
41  };
42 
43  template<typename T>
44  class Looper {
45  public:
48  Looper(const EventHypothesis &eh, const ParticleFilter &filter) ;
49 
52  Looper(const EventHypothesis &eh, const ParticleFilter *filter) ;
55  Looper(const EventHypothesis &eh, const ParticleFilterPtr &filter) ;
56  ~Looper() {}
57 
59  const T & operator*() const { return ptr_.get(iter_->second.get()); }
61  const T * operator->() const { return ptr_.get(iter_->second.get()); }
63  const T * get() const { return ptr_.get(iter_->second.get()); }
64 
66  bool isTypeOk() const { return ptr_.typeOk(iter_->second.get()); }
67 
69  const std::string & role() const { return iter_->first; }
71  const CandRefType & ref () const { return iter_->second; }
73  const reco::Candidate & cand () const { return *iter_->second; }
74 
76  size_t globalIndex() { return iter_ - eh_.begin(); }
78  size_t index() const { return num_; }
80  size_t size() const { if (total_ < 0) realSize(); return total_; }
81 
83  Looper & operator++() ;
85  Looper & operator--() ;
87  Looper & skip(int delta) ;
90  Looper & reset(int item=0) ;
91 
94  operator bool() const ;
95 
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  template<typename T2> bool operator>(const Looper<T2> &other) const { return iter_ > other.iter_; }
103 
104  private:
105  struct null_deleter{ void operator()(void const *) const { } };
107 
108  void first() ;
109  void realSize() const ;
110  bool assertOk() const;
111 
114  const_iterator iter_;
115  int num_;
116  mutable int total_; // mutable as it is not computed unless needed
118  };
120 
121  template<typename T>
123  eh_(eh), filter_(ParticleFilterPtr(&filter, typename Looper<T>::null_deleter())), total_(-1)
124  {
125  first();
126  }
127 
128  template<typename T>
130  eh_(eh), filter_(filter), total_(-1)
131  {
132  first();
133  }
134 
135  template<typename T>
137  eh_(eh), filter_(filter), total_(-1)
138  {
139  first();
140  }
141 
142 
143  template<typename T>
144  bool Looper<T>::assertOk() const {
145  assert(iter_ <= eh_.end());
146  assert((iter_+1) >= eh_.begin());
147  assert((iter_ < eh_.begin()) || (iter_ == eh_.end()) || ((*filter_)(*iter_)));
148  return true;
149  }
150 
151  template<typename T>
153  ptr_.clearCache();
154  assert(assertOk());
155  if (iter_ == eh_.end()) return *this;
156  do {
157  ++iter_;
158  if (iter_ == eh_.end()) break;
159  if ((*filter_)(*iter_)) {
160  assert(assertOk());
161  ++num_; return *this;
162  }
163  } while (true);
164  assert(assertOk());
165  return *this;
166  }
167  template<typename T>
169  ptr_.clearCache();
170  assert(assertOk());
171  if (num_ < 0) return *this;
172  do {
173  --iter_;
174  if (iter_ < eh_.begin()) { num_ = -1; break; }
175  if ((*filter_)(*iter_)) {
176  assert(assertOk());
177  --num_; return *this;
178  }
179  } while (true);
180  assert(assertOk());
181  return *this;
182  }
183 
184  template<typename T>
186  assert(assertOk());
187  std::advance(this, delta);
188  assert(assertOk());
189  return *this;
190  }
191 
192  template<typename T>
194  assert(assertOk());
195  if (item >= 0) {
196  first();
197  std::advance(this, item);
198  } else {
199  num_ = item + 1; iter_ = eh_.end();
200  std::advance(this, item);
201  }
202  assert(assertOk());
203  return *this;
204  }
205 
206  template<typename T>
208  num_ = 0;
209  iter_ = eh_.begin();
210  ptr_.clearCache();
211  for (; iter_ != eh_.end(); ++iter_) {
212  if ((*filter_)(*iter_)) break;
213  }
214  assert(assertOk());
215  }
216 
217  template<typename T>
219  return (iter_ < eh_.end()) && (iter_ >= eh_.begin());
220  }
221 
222  template<typename T>
223  void Looper<T>::realSize() const {
225  if (it < eh_.begin()) {
226  it = eh_.begin(); total_ = 0;
227  } else {
228  total_ = num_;
229  }
230  for (; it != eh_.end(); ++it) {
231  if ((*filter_)(*it)) ++total_;
232  }
233  }
234 } } // namespaces
235 
236 #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:76
bool operator!=(const Looper< T2 > &other) const
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
Definition: HeavyIon.h:7
const reco::Candidate & cand() const
C++ reference to pointed particle.
const_iterator end() const
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
fixed size matrix
Looper & skip(int delta)
skip (might be slow)
Looper< reco::Candidate > CandLooper
const ParticleFilterPtr filter_
long double T
void reset(double vett[256])
Definition: TPedValues.cc:11
const_iterator begin() const
bool operator>(const Looper< T2 > &other) const
size_t size() const
Number of particles in the loop.