AliPhysics  0fdb362 (0fdb362)
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 
30 
31 template <typename T>
33 public:
34  typedef typename std::pair<AliTLorentzVector, T*> momentum_object_pair;
35  typedef const momentum_object_pair& star_return_type;
36  typedef const momentum_object_pair* arrow_return_type;
37  typedef momentum_object_pair iterator_type;
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;
144  momentum_object_pair fCurrentElement;
145 
146  inline void update_current_element() {
147  if (fCurrent < 0 || fCurrent >= fkData->GetContainer()->GetNEntries()) {
148  this->fCurrentElement.first.SetPxPyPzE(0,0,0,0);
149  this->fCurrentElement.second = 0;
150  }
151  else {
152  this->fCurrentElement.second = (*fkData)[fCurrent];
153  fkData->GetContainer()->GetMomentum(this->fCurrentElement.first, fkData->GetInternalIndex(fCurrent));
154  }
155  }
156  };
157 
159  AliEmcalIterableContainerT(const AliEmcalContainer *cont, bool useAccept);
162 
167 
168  T *operator[](int index) const;
169 
174  operator int() const { return GetEntries(); }
175 
180  const AliEmcalContainer *GetContainer() const { return fkContainer; }
181 
182  int GetEntries() const;
183 
189  iterator begin() const { return iterator(this, 0, true); }
190 
196  iterator end() const { return iterator(this, GetEntries(), true); }
197 
203  iterator rbegin() const { return iterator(this, GetEntries()-1, false); }
204 
210  iterator rend() const { return iterator(this, -1, false); }
211 
212 protected:
213  void BuildAcceptIndices();
214 
215 private:
216  const AliEmcalContainer *fkContainer;
219 
220  inline int GetInternalIndex(int index) const {
221  if (fUseAccepted) {
222  return index < 0 || index >= fAcceptIndices.GetSize() ? -1 : fAcceptIndices[index];
223  }
224  else {
225  return index;
226  }
227  }
228 };
229 }
230 
231 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
232 #pragma GCC diagnostic pop
233 #endif
234 
235 #include "AliEmcalContainer.h"
236 
237 namespace EMCALIterableContainer {
241 template <typename T, typename STAR>
243  fkContainer(NULL),
244  fAcceptIndices(),
245  fUseAccepted(kFALSE)
246 {
247 
248 }
249 
256 template <typename T, typename STAR>
257 AliEmcalIterableContainerT<T, STAR>::AliEmcalIterableContainerT(const AliEmcalContainer *cont, bool useAccept):
258  fkContainer(cont),
259  fAcceptIndices(),
260  fUseAccepted(useAccept)
261 {
263 }
264 
270 template <typename T, typename STAR>
275 {
276 
277 }
278 
285 template <typename T, typename STAR>
287  if(this != &ref){
288  fkContainer = ref.fkContainer;
291  }
292  return *this;
293 }
294 
300 template <typename T, typename STAR>
302  return fUseAccepted ? fAcceptIndices.GetSize() : fkContainer->GetNEntries();
303 }
304 
320 template <typename T, typename STAR>
322  const AliEmcalContainer &contref = *fkContainer;
323  int _index = GetInternalIndex(index);
324  if(_index < 0 || _index >= contref.GetNEntries()) return NULL;
325  return static_cast<T*>(contref[_index]);
326 }
327 
333 template <typename T, typename STAR>
335  fAcceptIndices.Set(fkContainer->GetNAcceptEntries());
336  int acceptCounter = 0;
337  for(int index = 0; index < fkContainer->GetNEntries(); index++){
338  UInt_t rejectionReason = 0;
339  if(fkContainer->AcceptObject(index, rejectionReason)) fAcceptIndices[acceptCounter++] = index;
340  }
341 }
342 
346 
359 template <typename T, typename STAR>
361  fkData(cont),
362  fCurrent(currentpos),
363  fForward(forward),
364  fCurrentElement()
365 {
367 }
368 
374 template <typename T, typename STAR>
376  fkData(ref.fkData),
377  fCurrent(ref.fCurrent),
378  fForward(ref.fForward),
380 {
382 }
383 
390 template <typename T, typename STAR>
392  if(this != &ref){
393  fkData = ref.fkData;
394  fCurrent = ref.fCurrent;
395  fForward = ref.fForward;
397  }
398  return *this;
399 }
400 
406 template <typename T, typename STAR>
408  return fCurrent != ref.fCurrent;
409 }
410 
416 template <typename T, typename STAR>
418  if(fForward) fCurrent++;
419  else fCurrent--;
420 
422 
423  return *this;
424 }
425 
431 template <typename T, typename STAR>
433  if(fForward) fCurrent--;
434  else fCurrent++;
435 
437 
438  return *this;
439 }
440 
448 template <typename T, typename STAR>
451  operator++();
452  return tmp;
453 }
454 
462 template <typename T, typename STAR>
465  operator--();
466  return tmp;
467 }
468 
469 }
470 
471 #endif
472 #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