Belle II Software  release-08-01-10
DBImportArray.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/DBImportBase.h>
11 #include <framework/utilities/ArrayIterator.h>
12 #include <TClonesArray.h>
13 #include <stdexcept>
14 
15 namespace Belle2 {
25  template<class T> class DBImportArray: public DBImportBase {
26  public:
27 
32 
37  explicit DBImportArray(const std::string& name = ""):
38  DBImportBase(DBStore::arrayName<T>(name))
39  {
40  m_object = new TClonesArray(T::Class());
41  }
42 
47  {
48  delete m_object;
49  }
50 
54  inline int getEntries() const
55  {
56  return static_cast<TClonesArray*>(m_object)->GetEntriesFast();
57  }
58 
67  inline T* appendNew() { return new (nextFreeAdress()) T(); }
68 
80  template<class ...Args> T* appendNew(Args&& ... params)
81  {
82  return new (nextFreeAdress()) T(std::forward<Args>(params)...);
83  }
84 
91  inline T* operator [](int i) const
92  {
93  TObject* obj = static_cast<TClonesArray*>(m_object)->At(i);
94  if (obj == nullptr)
95  throw std::out_of_range("Out-of-range access in DBImportArray::operator[], for "
96  + getName() + ", index "
97  + std::to_string(i));
98  return static_cast<T*>(obj);
99  }
100 
104  iterator begin() { return iterator(*static_cast<TClonesArray*>(m_object), 0); }
105 
109  iterator end() { return iterator(*static_cast<TClonesArray*>(m_object), getEntries()); }
110 
114  const_iterator begin() const { return const_iterator(*static_cast<TClonesArray*>(m_object), 0); }
115 
119  const_iterator end() const { return const_iterator(*static_cast<TClonesArray*>(m_object), getEntries()); }
120 
125  void addEventDependency(unsigned int eventNumber) override
126  {
127  addIntraRunDependency(eventNumber, c_Event);
128  m_object = new TClonesArray(T::Class());
129  }
130 
135  void addTimeStampDependency(unsigned long long int timeStamp) override
136  {
137  addIntraRunDependency(timeStamp, c_TimeStamp);
138  m_object = new TClonesArray(T::Class());
139  }
140 
145  void addSubrunDependency(int subrun) override
146  {
147  addIntraRunDependency(subrun, c_Subrun);
148  m_object = new TClonesArray(T::Class());
149  }
150 
154  void clear() override
155  {
157  m_object = new TClonesArray(T::Class());
158  }
159 
160 
161  private:
162 
167  inline T* nextFreeAdress()
168  {
169  return static_cast<T*>(static_cast<TClonesArray*>(m_object)->AddrAt(getEntries()));
170  }
171 
172  };
174 }
Class for importing array of objects to the database.
Definition: DBImportArray.h:25
const_iterator begin() const
Returns const_iterator to first entry.
T * appendNew(Args &&... params)
Construct a new T object at the end of the array.
Definition: DBImportArray.h:80
DBImportArray(const std::string &name="")
Constructor.
Definition: DBImportArray.h:37
T * appendNew()
Construct a new T object at the end of the array.
Definition: DBImportArray.h:67
T * operator[](int i) const
Access to the stored objects.
Definition: DBImportArray.h:91
void addTimeStampDependency(unsigned long long int timeStamp) override
add time stamp dependency
void addEventDependency(unsigned int eventNumber) override
add event dependency
ObjArrayIterator< TClonesArray, T > iterator
STL-like iterator over the T objects (not T* ).
Definition: DBImportArray.h:29
int getEntries() const
Returns number of objects in the array.
Definition: DBImportArray.h:54
void addSubrunDependency(int subrun) override
add subrun dependency
iterator end()
Returns iterator to last entry +1.
~DBImportArray()
Destructor.
Definition: DBImportArray.h:46
const_iterator end() const
Returns const_iterator to last entry +1.
iterator begin()
Returns iterator to first entry.
void clear() override
Clear the content, e.g.
T * nextFreeAdress()
Returns address of the next free position of the array.
ObjArrayIterator< const TClonesArray, const T > const_iterator
STL-like const_iterator over the T objects (not T* ).
Definition: DBImportArray.h:31
Base class for importing objects to the database.
Definition: DBImportBase.h:23
void addIntraRunDependency(unsigned long long int tag, EIntraRunDependency type)
add intra run dependency
Definition: DBImportBase.cc:17
const std::string & getName() const
Returns the name under which the object will be stored in the database.
Definition: DBImportBase.h:41
virtual void clear()
Clear the content, e.g.
Definition: DBImportBase.cc:58
TObject * m_object
pointer to allocated object or array
Definition: DBImportBase.h:100
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
Abstract base class for different kinds of events.