Belle II Software  release-05-02-19
DBArray.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015-2018 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Kuhr, Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/database/DBAccessorBase.h>
13 
14 #include <framework/utilities/ArrayIterator.h>
15 
16 #include <TClonesArray.h>
17 #include <stdexcept>
18 
19 namespace Belle2 {
28  template<class T> class DBArray: public DBAccessorBase {
29  public:
31  typedef ObjArrayIterator<const TClonesArray, const T> const_iterator;
32 
40  explicit DBArray(const std::string& name = "", bool required = true):
41  DBAccessorBase(DBStore::arrayName<T>(name), T::Class(), true, required) {}
42 
44  inline int getEntries() const { return isValid() ? getObject<TClonesArray>()->GetEntriesFast() : 0;}
45 
53  inline const T* operator [](int i) const
54  {
55  if (!isValid()) {
56  throw std::out_of_range("Out-of-range access in DBArray::operator[], for " + getName() + ": Object not valid");
57  }
58  //At() checks for out-of-range and returns NULL in that case
59  TObject* obj = getObject<TClonesArray>()->At(i);
60  if (obj == nullptr)
61  throw std::out_of_range("Out-of-range access in DBArray::operator[], for " + getName() + ", index " + std::to_string(i));
62  return static_cast<T*>(obj); //type was checked by DataStore, so the cast is safe.
63  }
64 
71  template<class KEY> const T* getByKey(KEY(T::*method)(void) const, KEY key) const
72  {
73  const TClonesArray& array = *getObject<TClonesArray>();
74  for (int i = 0; i < getEntries(); i++) {
75  T* obj = static_cast<T*>(array.At(i));
76  if ((*obj.*method)() == key) {
77  return obj;
78  }
79  }
80  return nullptr;
81  }
82 
84  const_iterator begin() const { return const_iterator(getObject<TClonesArray>(), false); }
86  const_iterator end() const { return const_iterator(getObject<TClonesArray>(), true); }
87  };
88 
99  template<class T> class OptionalDBArray: public DBArray<T> {
100  public:
103  explicit OptionalDBArray(const std::string& name = ""): DBArray<T>(name, false) {}
104  };
106 }
Belle2::DBArray::end
const_iterator end() const
Return const_iterator to last entry +1.
Definition: DBArray.h:94
Belle2::DBArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: DBArray.h:52
Belle2::OptionalDBArray::OptionalDBArray
OptionalDBArray(const std::string &name="")
Construct a new Array with a given name or with the default name which is identical to the class name...
Definition: DBArray.h:111
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::DBArray::DBArray
DBArray(const std::string &name="", bool required=true)
Constructor to access an array of objects in the DBStore.
Definition: DBArray.h:48
Belle2::DBArray::operator[]
const T * operator[](int i) const
Access to the stored objects.
Definition: DBArray.h:61
Belle2::DBArray::const_iterator
ObjArrayIterator< const TClonesArray, const T > const_iterator
STL-like const_iterator over the T objects (not T* ).
Definition: DBArray.h:39
Belle2::DBArray::begin
const_iterator begin() const
Return const_iterator to first entry.
Definition: DBArray.h:92
Belle2::DBArray::getByKey
const T * getByKey(KEY(T::*method)(void) const, KEY key) const
Access object by key instead of by index.
Definition: DBArray.h:79
Belle2::DBAccessorBase::isValid
bool isValid() const
Check whether a valid object was obtained from the database.
Definition: DBAccessorBase.h:75
Belle2::DBAccessorBase::getName
const std::string & getName() const
Return name under which the object is saved in the DBStore.
Definition: DBAccessorBase.h:69
Belle2::DBAccessorBase::DBAccessorBase
DBAccessorBase(const std::string &name, const TClass *objClass, bool isArray, bool isRequired)
Constructor to access an object in the DBStore.
Definition: DBAccessorBase.h:38