Belle II Software light-2406-ragdoll
StoreArray.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
9#pragma once
10
11#include <framework/datastore/StoreAccessorBase.h>
12#include <framework/datastore/DataStore.h>
13
14#include <framework/utilities/ArrayIterator.h>
15
16#include <TClonesArray.h>
17#include <stdexcept>
18
19namespace Belle2 {
25 namespace _StoreArrayImpl {
27 void clearRelations(const StoreAccessorBase& array);
28 };
112 template <class T>
114 public:
119
126 explicit StoreArray(const std::string& name = "", DataStore::EDurability durability = DataStore::c_Event):
127 StoreAccessorBase(DataStore::arrayName<T>(name), durability, T::Class(), true), m_storeArray(0) {}
128
129
140 template <class TO> bool registerRelationTo(const StoreArray<TO>& toArray, DataStore::EDurability durability = DataStore::c_Event,
141 DataStore::EStoreFlags storeFlags = DataStore::c_WriteOut, const std::string& namedRelation = "") const
142 {
143 return DataStore::Instance().registerRelation(*this, toArray, durability, storeFlags, namedRelation);
144 }
145
155 template <class TO> bool requireRelationTo(const StoreArray<TO>& toArray,
157 const std::string& namedRelation = "") const
158 {
159 return DataStore::Instance().requireRelation(*this, toArray, durability, namedRelation);
160 }
161
172 template <class TO> bool optionalRelationTo(const StoreArray<TO>& toArray,
174 const std::string& namedRelation = "") const
175 {
176 return DataStore::Instance().optionalRelation(*this, toArray, durability, namedRelation);
177 }
178
185 template <class TO> bool hasRelationTo(const StoreArray<TO>& toArray, DataStore::EDurability durability = DataStore::c_Event,
186 const std::string& namedRelation = "") const
187 {
188 return DataStore::Instance().hasRelation(*this, toArray, durability, namedRelation);
189 }
190
197 template <class FROM> bool hasRelationFrom(const StoreArray<FROM>& fromArray,
198 DataStore::EDurability durability = DataStore::c_Event, const std::string& namedRelation = "") const
199 {
200 return DataStore::Instance().hasRelation(fromArray, *this, durability, namedRelation);
201 }
202
207 void clear() override
208 {
209 if (getEntries() != 0) {
210 (*m_storeArray)->Delete();
212 }
213 }
214
216 inline int getEntries() const { return isCreated() ? ((*m_storeArray)->GetEntriesFast()) : 0;}
217
228 inline T* operator [](int i) const
229 {
231 //At() checks for out-of-range and returns NULL in that case
232 TObject* obj = (*m_storeArray)->At(i);
233 if (obj == nullptr)
234 throw std::out_of_range("Out-of-range access in StoreArray::operator[], for " + readableName() + ", index " + std::to_string(i));
235 return static_cast<T*>(obj); //type was checked by DataStore, so the cast is safe.
236 }
237
246 inline T* appendNew() { return new (nextFreeAdress()) T(); }
247
268 template<class ...Args> T* appendNew(Args&& ... params)
269 {
270 return new (nextFreeAdress()) T(std::forward<Args>(params)...);
271 }
272
273
275 static std::vector<std::string> getArrayList(DataStore::EDurability durability = DataStore::c_Event)
276 {
277 return DataStore::Instance().getListOfArrays(T::Class(), durability);
278 }
279
280
288 inline bool isValid() const
289 {
291 return m_storeArray;
292 }
293
301 inline operator bool() const { return isValid(); }
302
311 TClonesArray* getPtr() const
312 {
314 return (m_storeArray ? *m_storeArray : nullptr);
315 }
316
321
326
327 private:
329 bool create(bool replace = false) { return StoreAccessorBase::create(replace); }
330
335 inline T* nextFreeAdress()
336 {
338 return static_cast<T*>((*m_storeArray)->AddrAt(getEntries()));
339 }
340
342 inline void ensureAttached() const
343 {
344 if (!m_storeArray) {
345 const_cast<StoreArray*>(this)->m_storeArray = reinterpret_cast<TClonesArray**>(DataStore::Instance().getObject(*this));
346 }
347 }
352 inline void ensureCreated() const
353 {
354 if (!isCreated()) {
355 if (!const_cast<StoreArray*>(this)->create())
356 throw std::runtime_error("Write access to " + readableName() + " failed, did you remember to call registerInDataStore()?");
357 }
358 }
359
361 inline bool isCreated() const
362 {
364 return m_storeArray && *m_storeArray;
365 }
366
368 TClonesArray** m_storeArray;
369
370 };
372} // end namespace Belle2
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
bool optionalRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, std::string const &namedRelation)
Register the given relation as an optional input.
Definition: DataStore.cc:766
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:69
@ c_WriteOut
Object/array should be saved by output modules.
Definition: DataStore.h:70
bool registerRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, EStoreFlags storeFlags, const std::string &namedRelation)
Register a relation in the DataStore map.
Definition: DataStore.cc:244
std::vector< std::string > getListOfArrays(const TClass *arrayClass, EDurability durability) const
Returns a list of names of arrays which are of type (or inherit from) arrayClass.
Definition: DataStore.cc:666
EDurability
Durability types.
Definition: DataStore.h:58
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:59
bool requireRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, std::string const &namedRelation)
Produce ERROR message if no relation of given durability exists between fromArray and toArray (in tha...
Definition: DataStore.cc:749
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
bool hasRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, const std::string &namedRelation)
Check for the existence of a relation in the DataStore map.
Definition: DataStore.cc:271
TObject ** getObject(const StoreAccessorBase &accessor)
Get a pointer to a pointer of an object in the DataStore.
Definition: DataStore.cc:306
Optimizes class to iterate over TObjArray and classes inheriting from it.
Definition: ArrayIterator.h:23
Base class for StoreObjPtr and StoreArray for easier common treatment.
std::string readableName() const
Convert this acessor into a readable string (for messages).
bool create(bool replace=false)
Create a default object in the data store.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
T * nextFreeAdress()
Returns address of the next free position of the array.
Definition: StoreArray.h:335
static std::vector< std::string > getArrayList(DataStore::EDurability durability=DataStore::c_Event)
Return list of array names with matching type.
Definition: StoreArray.h:275
bool requireRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Produce error if no relation from this array to 'toArray' has been registered.
Definition: StoreArray.h:155
const_iterator begin() const
Return const_iterator to first entry.
Definition: StoreArray.h:323
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:246
T * operator[](int i) const
Access to the stored objects.
Definition: StoreArray.h:228
void ensureAttached() const
Ensure that this object is attached.
Definition: StoreArray.h:342
bool hasRelationFrom(const StoreArray< FROM > &fromArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Check for the existence of a relation from the given StoreArray.
Definition: StoreArray.h:197
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:288
TClonesArray ** m_storeArray
Pointer that actually holds the TClonesArray.
Definition: StoreArray.h:368
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition: StoreArray.h:311
ObjArrayIterator< TClonesArray, T > iterator
STL-like iterator over the T objects (not T* ).
Definition: StoreArray.h:116
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
T * appendNew(Args &&... params)
Construct a new T object directly at the end of the array.
Definition: StoreArray.h:268
iterator end()
Return iterator to last entry +1.
Definition: StoreArray.h:320
const_iterator end() const
Return const_iterator to last entry +1.
Definition: StoreArray.h:325
bool hasRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Check for the existence of a relation to the given StoreArray.
Definition: StoreArray.h:185
bool create(bool replace=false)
Creating StoreArrays is unnecessary, only used internally.
Definition: StoreArray.h:329
StoreArray(const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
Constructor to access an array in the DataStore.
Definition: StoreArray.h:126
iterator begin()
Return iterator to first entry.
Definition: StoreArray.h:318
void ensureCreated() const
Ensure that the array has been created.
Definition: StoreArray.h:352
void clear() override
Delete all entries in this array.
Definition: StoreArray.h:207
ObjArrayIterator< const TClonesArray, const T > const_iterator
STL-like const_iterator over the T objects (not T* ).
Definition: StoreArray.h:118
bool optionalRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Tell the data store about a relation that we could make use of.
Definition: StoreArray.h:172
bool isCreated() const
Check wether the array object was created.
Definition: StoreArray.h:361
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Definition: StoreArray.h:140
void clearRelations(const StoreAccessorBase &array)
clear all relations touching the given array.
Definition: StoreArray.cc:16
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24