AliPhysics  f05a842 (f05a842)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliEmcalIterableContainer.h
Go to the documentation of this file.
1 #ifndef ALIEMCALITERABLECONTAINER_H
2 #define ALIEMCALITERABLECONTAINER_H
3 #if !(defined(__CINT__) || defined(__MAKECINT__))
4 
5 /* Copyright(c) 1998-2016, ALICE Experiment at CERN, All rights reserved. *
6  * See cxx source for full Copyright notice */
7 
8 #include <iterator>
9 #include <vector>
10 #include <type_traits>
11 #include <TArrayI.h>
12 #include "AliTLorentzVector.h"
13 
14 
15 #if (__GNUC__ >= 3) && !defined(__INTEL_COMPILER)
16 // gcc warns in level Weffc++ about non-virtual destructor
17 // in std::iterator. It is a false positive, therefore Weffc++
18 // needs to be disabled for AliEmcalIterableContainerT
19 #pragma GCC system_header
20 #endif
21 
22 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Weffc++"
25 #endif
26 
27 class AliEmcalContainer;
28 
29 namespace EMCALIterableContainer {
30 
31 template <typename T>
33 public:
34  typedef typename std::pair<AliTLorentzVector, T*> momentum_object_pair;
38 
39  static star_return_type star(momentum_object_pair* ptr) { return *ptr; }
40  static arrow_return_type arrow(momentum_object_pair* ptr) { return ptr; }
41 };
42 
43 template <typename T>
45 public:
46  typedef typename std::pair<AliTLorentzVector, T*> momentum_object_pair;
47  typedef T* star_return_type;
48  typedef T** arrow_return_type;
49  typedef T* iterator_type;
50 
51  static star_return_type star(momentum_object_pair* ptr) { return ptr->second; }
52  static arrow_return_type arrow(momentum_object_pair* ptr) { return &(ptr->second); }
53 };
54 
94 template <typename T, typename STAR=operator_star_object<T> >
96 public:
97  typedef typename std::pair<AliTLorentzVector, T*> momentum_object_pair;
98 
116  class iterator;
117  friend class iterator;
118  class iterator final : public std::iterator<std::bidirectional_iterator_tag, typename STAR::iterator_type, std::ptrdiff_t> {
119  public:
120  iterator(const AliEmcalIterableContainerT<T, STAR> *cont, int currentpos, bool forward = true);
121  iterator(const iterator &ref);
122  iterator &operator=(const iterator &ref);
124 
125  bool operator!=(const iterator &ref) const;
126 
127  iterator &operator++();
128  iterator operator++(int);
129  iterator &operator--();
130  iterator operator--(int);
131 
132  typename STAR::star_return_type operator*() { return STAR::star(&(this->fCurrentElement)); }
133  typename STAR::arrow_return_type operator->() { return STAR::arrow(&(this->fCurrentElement)); }
134 
135  int current_index() const { return fkData->GetInternalIndex(fCurrent); }
136  const AliTLorentzVector& get_momentum() const { return this->fCurrentElement.first; }
137 
138  private:
139  iterator();
140 
142  int fCurrent;
143  bool fForward;
145 
146  inline void update_current_element() {
147  this->fCurrentElement.second = (*fkData)[fCurrent];
148  fkData->GetContainer()->GetMomentum(this->fCurrentElement.first, fkData->GetInternalIndex(fCurrent));
149  }
150  };
151 
153  AliEmcalIterableContainerT(const AliEmcalContainer *cont, bool useAccept);
156 
161 
162  T *operator[](int index) const;
163 
168  operator int() const { return GetEntries(); }
169 
174  const AliEmcalContainer *GetContainer() const { return fkContainer; }
175 
176  int GetEntries() const;
177 
183  iterator begin() const { return iterator(this, 0, true); }
184 
190  iterator end() const { return iterator(this, GetEntries(), true); }
191 
197  iterator rbegin() const { return iterator(this, GetEntries()-1, false); }
198 
204  iterator rend() const { return iterator(this, -1, false); }
205 
206 protected:
207  void BuildAcceptIndices();
208 
209 private:
210  const AliEmcalContainer *fkContainer;
213 
214  inline int GetInternalIndex(int index) const {
215  if (fUseAccepted) {
216  return index < 0 || index >= fAcceptIndices.GetSize() ? -1 : fAcceptIndices[index];
217  }
218  else {
219  return index;
220  }
221  }
222 };
223 }
224 
225 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
226 #pragma GCC diagnostic pop
227 #endif
228 
229 #include "AliEmcalContainer.h"
230 
231 namespace EMCALIterableContainer {
235 template <typename T, typename STAR>
237  fkContainer(NULL),
238  fAcceptIndices(),
239  fUseAccepted(kFALSE)
240 {
241 
242 }
243 
250 template <typename T, typename STAR>
251 AliEmcalIterableContainerT<T, STAR>::AliEmcalIterableContainerT(const AliEmcalContainer *cont, bool useAccept):
252  fkContainer(cont),
253  fAcceptIndices(),
254  fUseAccepted(useAccept)
255 {
257 }
258 
264 template <typename T, typename STAR>
266  fkContainer(ref.fkContainer),
267  fAcceptIndices(ref.fAcceptIndices),
268  fUseAccepted(ref.fUseAccepted)
269 {
270 
271 }
272 
279 template <typename T, typename STAR>
281  if(this != &ref){
282  fkContainer = ref.fkContainer;
283  fAcceptIndices = ref.fAcceptIndices;
284  fUseAccepted = ref.fUseAccepted;
285  }
286  return *this;
287 }
288 
294 template <typename T, typename STAR>
296  return fUseAccepted ? fAcceptIndices.GetSize() : fkContainer->GetNEntries();
297 }
298 
314 template <typename T, typename STAR>
316  const AliEmcalContainer &contref = *fkContainer;
317  int _index = GetInternalIndex(index);
318  if(_index < 0 || _index >= contref.GetNEntries()) return NULL;
319  return static_cast<T*>(contref[_index]);
320 }
321 
327 template <typename T, typename STAR>
329  fAcceptIndices.Set(fkContainer->GetNAcceptEntries());
330  int acceptCounter = 0;
331  for(int index = 0; index < fkContainer->GetNEntries(); index++){
332  UInt_t rejectionReason = 0;
333  if(fkContainer->AcceptObject(index, rejectionReason)) fAcceptIndices[acceptCounter++] = index;
334  }
335 }
336 
340 
353 template <typename T, typename STAR>
355  fkData(cont),
356  fCurrent(currentpos),
357  fForward(forward),
358  fCurrentElement()
359 {
360  update_current_element();
361 }
362 
368 template <typename T, typename STAR>
369 AliEmcalIterableContainerT<T, STAR>::iterator::iterator(const AliEmcalIterableContainerT<T, STAR>::iterator &ref):
370  fkData(ref.fkData),
371  fCurrent(ref.fCurrent),
372  fForward(ref.fForward),
373  fCurrentElement()
374 {
375  update_current_element();
376 }
377 
384 template <typename T, typename STAR>
386  if(this != &ref){
387  fkData = ref.fkData;
388  fCurrent = ref.fCurrent;
389  fForward = ref.fForward;
391  }
392  return *this;
393 }
394 
400 template <typename T, typename STAR>
402  return fCurrent != ref.fCurrent;
403 }
404 
410 template <typename T, typename STAR>
412  if(fForward) fCurrent++;
413  else fCurrent--;
414 
415  update_current_element();
416 
417  return *this;
418 }
419 
425 template <typename T, typename STAR>
427  if(fForward) fCurrent--;
428  else fCurrent++;
429 
430  update_current_element();
431 
432  return *this;
433 }
434 
442 template <typename T, typename STAR>
445  operator++();
446  return tmp;
447 }
448 
456 template <typename T, typename STAR>
459  operator--();
460  return tmp;
461 }
462 
463 }
464 
465 #endif
466 #endif /* ALIEMCALITERABLECONTAINER_H */
AliEmcalIterableContainerT & operator=(const AliEmcalIterableContainerT< T, STAR > &ref)
bidirectional stl iterator over the EMCAL iterable container
Declaration of class AliTLorentzVector.
static star_return_type star(momentum_object_pair *ptr)
static arrow_return_type arrow(momentum_object_pair *ptr)
UShort_t T(UShort_t m, UShort_t t)
Definition: RingBits.C:60
std::pair< AliTLorentzVector, T * > momentum_object_pair
unsigned int UInt_t
Definition: External.C:33
static arrow_return_type arrow(momentum_object_pair *ptr)
Bool_t fUseAccepted
Switch between accepted and all objects.
static star_return_type star(momentum_object_pair *ptr)
momentum_object_pair fCurrentElement
current element pair (momentum, pointer object)
const AliEmcalContainer * fkContainer
Container to be iterated over.
std::pair< AliTLorentzVector, T * > momentum_object_pair
std::pair< AliTLorentzVector, T * > momentum_object_pair
const AliEmcalIterableContainerT< T, STAR > * fkData
container with data
bool Bool_t
Definition: External.C:53