AliPhysics  608b256 (608b256)
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  bool operator==(const iterator &ret) const;
127 
128  iterator &operator++();
129  iterator operator++(int);
130  iterator &operator--();
131  iterator operator--(int);
132 
133  typename STAR::star_return_type operator*() { return STAR::star(&(this->fCurrentElement)); }
134  typename STAR::arrow_return_type operator->() { return STAR::arrow(&(this->fCurrentElement)); }
135 
136  int current_index() const { return fkData->GetInternalIndex(fCurrent); }
137  const AliTLorentzVector& get_momentum() const { return this->fCurrentElement.first; }
138 
139  private:
140  iterator();
141 
143  int fCurrent;
144  bool fForward;
145  momentum_object_pair fCurrentElement;
146 
147  inline void update_current_element() {
148  if (fCurrent < 0 || fCurrent >= fkData->GetContainer()->GetNEntries()) {
149  this->fCurrentElement.first.SetPxPyPzE(0,0,0,0);
150  this->fCurrentElement.second = 0;
151  }
152  else {
153  this->fCurrentElement.second = (*fkData)[fCurrent];
154  fkData->GetContainer()->GetMomentum(this->fCurrentElement.first, fkData->GetInternalIndex(fCurrent));
155  }
156  }
157  };
158 
160  AliEmcalIterableContainerT(const AliEmcalContainer *cont, bool useAccept);
163 
168 
169  T *operator[](int index) const;
170 
175  operator int() const { return GetEntries(); }
176 
181  const AliEmcalContainer *GetContainer() const { return fkContainer; }
182 
183  int GetEntries() const;
184 
190  iterator begin() const { return iterator(this, 0, true); }
191 
197  iterator end() const { return iterator(this, GetEntries(), true); }
198 
204  iterator rbegin() const { return iterator(this, GetEntries()-1, false); }
205 
211  iterator rend() const { return iterator(this, -1, false); }
212 
213 protected:
214  void BuildAcceptIndices();
215 
216 private:
220 
221  inline int GetInternalIndex(int index) const {
222  if (fUseAccepted) {
223  return index < 0 || index >= fAcceptIndices.GetSize() ? -1 : fAcceptIndices[index];
224  }
225  else {
226  return index;
227  }
228  }
229 };
230 }
231 
232 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
233 #pragma GCC diagnostic pop
234 #endif
235 
236 #include "AliEmcalContainer.h"
237 
238 namespace EMCALIterableContainer {
242 template <typename T, typename STAR>
244  fkContainer(NULL),
245  fAcceptIndices(),
246  fUseAccepted(kFALSE)
247 {
248 
249 }
250 
257 template <typename T, typename STAR>
259  fkContainer(cont),
260  fAcceptIndices(),
261  fUseAccepted(useAccept)
262 {
264 }
265 
271 template <typename T, typename STAR>
276 {
277 
278 }
279 
286 template <typename T, typename STAR>
288  if(this != &ref){
289  fkContainer = ref.fkContainer;
292  }
293  return *this;
294 }
295 
301 template <typename T, typename STAR>
303  return fUseAccepted ? fAcceptIndices.GetSize() : fkContainer->GetNEntries();
304 }
305 
321 template <typename T, typename STAR>
323  const AliEmcalContainer &contref = *fkContainer;
324  int _index = GetInternalIndex(index);
325  if(_index < 0 || _index >= contref.GetNEntries()) return NULL;
326  return static_cast<T*>(contref[_index]);
327 }
328 
334 template <typename T, typename STAR>
337  int acceptCounter = 0;
338  for(int index = 0; index < fkContainer->GetNEntries(); index++){
339  UInt_t rejectionReason = 0;
340  if(fkContainer->AcceptObject(index, rejectionReason)) fAcceptIndices[acceptCounter++] = index;
341  }
342 }
343 
347 
360 template <typename T, typename STAR>
362  fkData(cont),
363  fCurrent(currentpos),
364  fForward(forward),
365  fCurrentElement()
366 {
368 }
369 
375 template <typename T, typename STAR>
377  fkData(ref.fkData),
378  fCurrent(ref.fCurrent),
379  fForward(ref.fForward),
381 {
383 }
384 
391 template <typename T, typename STAR>
393  if(this != &ref){
394  fkData = ref.fkData;
395  fCurrent = ref.fCurrent;
396  fForward = ref.fForward;
398  }
399  return *this;
400 }
401 
407 template <typename T, typename STAR>
409  return fCurrent != ref.fCurrent;
410 }
411 
417 template <typename T, typename STAR>
419  return fCurrent == ref.fCurrent;
420 }
421 
427 template <typename T, typename STAR>
429  if(fForward) fCurrent++;
430  else fCurrent--;
431 
433 
434  return *this;
435 }
436 
442 template <typename T, typename STAR>
444  if(fForward) fCurrent--;
445  else fCurrent++;
446 
448 
449  return *this;
450 }
451 
459 template <typename T, typename STAR>
462  operator++();
463  return tmp;
464 }
465 
473 template <typename T, typename STAR>
476  operator--();
477  return tmp;
478 }
479 
480 }
481 
482 #endif
483 #endif /* ALIEMCALITERABLECONTAINER_H */
AliEmcalIterableContainerT & operator=(const AliEmcalIterableContainerT< T, STAR > &ref)
Int_t GetNAcceptEntries() const
Count accepted entries in the container.
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
virtual Bool_t AcceptObject(Int_t i, UInt_t &rejectionReason) const =0
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)
Base class for container structures within the EMCAL framework.
virtual Bool_t GetMomentum(TLorentzVector &mom, Int_t i) const =0
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 operator==(const AliAnalysisTaskDmesonJets::AliHFJetDefinition &lhs, const AliAnalysisTaskDmesonJets::AliHFJetDefinition &rhs)
Int_t GetNEntries() const
bool Bool_t
Definition: External.C:53