Belle II Software  release-08-01-10
DBArray.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <framework/database/DBAccessorBase.h>
11 
12 #include <framework/utilities/ArrayIterator.h>
13 
14 #include <TClonesArray.h>
15 #include <stdexcept>
16 
17 namespace Belle2 {
26  template<class T> class DBArray: public DBAccessorBase {
27  public:
30 
38  explicit DBArray(const std::string& name = "", bool required = true):
39  DBAccessorBase(DBStore::arrayName<T>(name), T::Class(), true, required) {}
40 
42  inline int getEntries() const { return isValid() ? getObject<TClonesArray>()->GetEntriesFast() : 0;}
43 
51  inline const T* operator [](int i) const
52  {
53  if (!isValid()) {
54  throw std::out_of_range("Out-of-range access in DBArray::operator[], for " + getName() + ": Object not valid");
55  }
56  //At() checks for out-of-range and returns NULL in that case
57  TObject* obj = getObject<TClonesArray>()->At(i);
58  if (obj == nullptr)
59  throw std::out_of_range("Out-of-range access in DBArray::operator[], for " + getName() + ", index " + std::to_string(i));
60  return static_cast<T*>(obj); //type was checked by DataStore, so the cast is safe.
61  }
62 
69  template<class KEY> const T* getByKey(KEY(T::*method)(void) const, KEY key) const
70  {
71  const TClonesArray& array = *getObject<TClonesArray>();
72  for (int i = 0; i < getEntries(); i++) {
73  T* obj = static_cast<T*>(array.At(i));
74  if ((*obj.*method)() == key) {
75  return obj;
76  }
77  }
78  return nullptr;
79  }
80 
82  const_iterator begin() const { return const_iterator(getObject<TClonesArray>(), false); }
84  const_iterator end() const { return const_iterator(getObject<TClonesArray>(), true); }
85  };
86 
97  template<class T> class OptionalDBArray: public DBArray<T> {
98  public:
101  explicit OptionalDBArray(const std::string& name = ""): DBArray<T>(name, false) {}
102  };
104 }
Base class for DBObjPtr and DBArray for easier common treatment.
bool isValid() const
Check whether a valid object was obtained from the database.
const std::string & getName() const
Return name under which the object is saved in the DBStore.
Class for accessing arrays of objects in the database.
Definition: DBArray.h:26
const_iterator begin() const
Return const_iterator to first entry.
Definition: DBArray.h:82
const T * getByKey(KEY(T::*method)(void) const, KEY key) const
Access object by key instead of by index.
Definition: DBArray.h:69
DBArray(const std::string &name="", bool required=true)
Constructor to access an array of objects in the DBStore.
Definition: DBArray.h:38
const T * operator[](int i) const
Access to the stored objects.
Definition: DBArray.h:51
int getEntries() const
Get the number of objects in the array.
Definition: DBArray.h:42
const_iterator end() const
Return const_iterator to last entry +1.
Definition: DBArray.h:84
ObjArrayIterator< const TClonesArray, const T > const_iterator
STL-like const_iterator over the T objects (not T* ).
Definition: DBArray.h:29
Singleton class to cache database objects.
Definition: DBStore.h:31
Optimizes class to iterate over TObjArray and classes inheriting from it.
Definition: ArrayIterator.h:23
Optional DBArray: This class behaves the same as the DBArray except that it will not raise errors whe...
Definition: DBArray.h:97
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:101
Abstract base class for different kinds of events.