AliPhysics  66e96a0 (66e96a0)
 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 <TArrayI.h>
10 #include "AliTLorentzVector.h"
11 
12 class AliEmcalContainer;
13 
54 namespace EMCALIterableContainer {
55 template <typename _T, bool _mom>
56 class operator_star {};
57 
58 template <typename T>
59 class operator_star<T, true> {
60 public:
61  typedef typename std::pair<AliTLorentzVector, T*> momentum_object_pair;
62 
63  operator_star() : fCurrentElement() {}
64  operator_star(const operator_star &ref) : fCurrentElement(ref.fCurrentElement) {}
65  operator_star &operator=(const operator_star &ref) { fCurrentElement = ref.fCurrentElement; return *this; }
66 
67  const momentum_object_pair& operator*() const { return fCurrentElement; }
68  const momentum_object_pair* operator->() const { return &(fCurrentElement); }
69 
70 protected:
72 };
73 
74 template <typename T>
75 class operator_star<T, false> {
76 public:
77  typedef typename std::pair<AliTLorentzVector, T*> momentum_object_pair;
78 
79  operator_star() : fCurrentElement() {}
80  operator_star(const operator_star &ref) : fCurrentElement(ref.fCurrentElement) {}
81  operator_star &operator=(const operator_star &ref) { fCurrentElement = ref.fCurrentElement; return *this; }
82 
83  T* operator*() const { return fCurrentElement.second; }
84  T** operator->() const { return &(fCurrentElement.second); }
85 
86 protected:
88 };
89 }
90 
91 template <typename T, bool mom = false>
93 public:
94  typedef typename std::pair<AliTLorentzVector, T*> momentum_object_pair;
95  typedef typename std::conditional<mom, momentum_object_pair, T*>::type value_type;
96 
114  class iterator : public std::iterator<std::bidirectional_iterator_tag, value_type, std::ptrdiff_t>,
116  public:
117  iterator(const AliEmcalIterableContainerT<T, mom> *cont, int currentpos, bool forward = true);
118  iterator(const iterator &ref);
119  iterator &operator=(const iterator &ref);
120  virtual ~iterator(){}
121 
122  bool operator!=(const iterator &ref) const;
123 
124  iterator &operator++();
125  iterator operator++(int);
126  iterator &operator--();
127  iterator operator--(int);
128 
129  int current_index() const { return fkData->GetInternalIndex(fCurrent); }
130  const AliTLorentzVector& get_momentum() const { return this->fCurrentElement.first; }
131 
132  private:
133  iterator();
134 
136  int fCurrent;
137  bool fForward;
138 
139  inline void update_current_element() {
140  this->fCurrentElement.second = (*fkData)[fCurrent];
141  fkData->GetContainer()->GetMomentum(this->fCurrentElement.first, fkData->GetInternalIndex(fCurrent));
142  }
143  };
144 
146  AliEmcalIterableContainerT(const AliEmcalContainer *cont, bool useAccept);
149 
154 
155  T *operator[](int index) const;
156 
161  operator int() const { return GetEntries(); }
162 
167  const AliEmcalContainer *GetContainer() const { return fkContainer; }
168 
169  int GetEntries() const;
170 
176  iterator begin() const { return iterator(this, 0, true); }
177 
183  iterator end() const { return iterator(this, GetEntries(), true); }
184 
190  iterator rbegin() const { return iterator(this, GetEntries()-1, false); }
191 
197  iterator rend() const { return iterator(this, -1, false); }
198 
199 protected:
200  void BuildAcceptIndices();
201 
202 private:
203  const AliEmcalContainer *fkContainer;
204  TArrayI fAcceptIndices;
205  Bool_t fUseAccepted;
206 
207  inline int GetInternalIndex(int index) const {
208  if (fUseAccepted) {
209  return index < 0 || index >= fAcceptIndices.GetSize() ? -1 : fAcceptIndices[index];
210  }
211  else {
212  return index;
213  }
214  }
215 };
216 
217 #include "AliEmcalContainer.h"
218 
222 template <typename T, bool mom>
224  fkContainer(NULL),
225  fAcceptIndices(),
226  fUseAccepted(kFALSE)
227 {
228 
229 }
230 
237 template <typename T, bool mom>
238 AliEmcalIterableContainerT<T, mom>::AliEmcalIterableContainerT(const AliEmcalContainer *cont, bool useAccept):
239  fkContainer(cont),
240  fAcceptIndices(),
241  fUseAccepted(useAccept)
242 {
244 }
245 
251 template <typename T, bool mom>
253  fkContainer(ref.fkContainer),
254  fAcceptIndices(ref.fAcceptIndices),
255  fUseAccepted(ref.fUseAccepted)
256 {
257 
258 }
259 
266 template <typename T, bool mom>
268  if(this != &ref){
269  fkContainer = ref.fkContainer;
270  fAcceptIndices = ref.fAcceptIndices;
271  fUseAccepted = ref.fUseAccepted;
272  }
273  return *this;
274 }
275 
281 template <typename T, bool mom>
283  return fUseAccepted ? fAcceptIndices.GetSize() : fkContainer->GetNEntries();
284 }
285 
301 template <typename T, bool mom>
303  const AliEmcalContainer &contref = *fkContainer;
304  int _index = GetInternalIndex(index);
305  if(_index < 0 || _index >= contref.GetNEntries()) return NULL;
306  return static_cast<T*>(contref[_index]);
307 }
308 
314 template <typename T, bool mom>
316  fAcceptIndices.Set(fkContainer->GetNAcceptEntries());
317  int acceptCounter = 0;
318  for(int index = 0; index < fkContainer->GetNEntries(); index++){
319  UInt_t rejectionReason = 0;
320  if(fkContainer->AcceptObject(index, rejectionReason)) fAcceptIndices[acceptCounter++] = index;
321  }
322 }
323 
327 
340 template <typename T, bool mom>
342  EMCALIterableContainer::operator_star<T, mom>(),
343  fkData(cont),
344  fCurrent(currentpos),
345  fForward(forward)
346 {
347  update_current_element();
348 }
349 
355 template <typename T, bool mom>
357  EMCALIterableContainer::operator_star<T, mom>(ref),
358  fkData(ref.fkData),
359  fCurrent(ref.fCurrent),
360  fForward(ref.fForward)
361 {
362  update_current_element();
363 }
364 
371 template <typename T, bool mom>
374  if(this != &ref){
375  fkData = ref.fkData;
376  fCurrent = ref.fCurrent;
377  fForward = ref.fForward;
378  }
379  return *this;
380 }
381 
387 template <typename T, bool mom>
389  return fCurrent != ref.fCurrent;
390 }
391 
397 template <typename T, bool mom>
399  if(fForward) fCurrent++;
400  else fCurrent--;
401 
402  update_current_element();
403 
404  return *this;
405 }
406 
412 template <typename T, bool mom>
414  if(fForward) fCurrent--;
415  else fCurrent++;
416 
417  update_current_element();
418 
419  return *this;
420 }
421 
429 template <typename T, bool mom>
432  operator++();
433  return tmp;
434 }
435 
443 template <typename T, bool mom>
446  operator--();
447  return tmp;
448 }
449 
450 #endif
451 #endif /* ALIEMCALITERABLECONTAINER_H */
std::conditional< mom, momentum_object_pair, T * >::type value_type
Bool_t fUseAccepted
Switch between accepted and all objects.
momentum_object_pair fCurrentElement
current element pair (momentum, pointer object)
const AliEmcalContainer * fkContainer
Container to be iterated over.
operator_star & operator=(const operator_star &ref)
momentum_object_pair fCurrentElement
current element pair (momentum, pointer object)
AliEmcalIterableContainerT & operator=(const AliEmcalIterableContainerT< T, mom > &ref)
std::pair< AliTLorentzVector, T * > momentum_object_pair
operator_star & operator=(const operator_star &ref)
bidirectional stl iterator over the EMCAL iterable container
std::pair< AliTLorentzVector, T * > momentum_object_pair
int fCurrent
current index in the container
const momentum_object_pair * operator->() const
bool fForward
use forward or backward direction
const AliEmcalContainer * GetContainer() const
TArrayI fAcceptIndices
Array of accepted indices.
int GetInternalIndex(int index) const
iterator & operator=(const iterator &ref)
std::pair< AliTLorentzVector, T * > momentum_object_pair
const AliTLorentzVector & get_momentum() const
const AliEmcalIterableContainerT< T, mom > * fkData
container with data
bool operator!=(const iterator &ref) const