Belle II Software  release-05-02-19
StoreArray.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Heck, Thomas Kuhr *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/datastore/StoreAccessorBase.h>
14 #include <framework/datastore/DataStore.h>
15 
16 #include <framework/utilities/ArrayIterator.h>
17 
18 #include <TClonesArray.h>
19 #include <stdexcept>
20 
21 namespace Belle2 {
27  namespace _StoreArrayImpl {
29  void clearRelations(const StoreAccessorBase& array);
30  };
114  template <class T>
115  class StoreArray : public StoreAccessorBase {
116  public:
118  typedef ObjArrayIterator<TClonesArray, T> iterator;
120  typedef ObjArrayIterator<const TClonesArray, const T> const_iterator;
121 
128  explicit StoreArray(const std::string& name = "", DataStore::EDurability durability = DataStore::c_Event):
129  StoreAccessorBase(DataStore::arrayName<T>(name), durability, T::Class(), true), m_storeArray(0) {}
130 
131 
142  template <class TO> bool registerRelationTo(const StoreArray<TO>& toArray, DataStore::EDurability durability = DataStore::c_Event,
143  DataStore::EStoreFlags storeFlags = DataStore::c_WriteOut, const std::string& namedRelation = "") const
144  {
145  return DataStore::Instance().registerRelation(*this, toArray, durability, storeFlags, namedRelation);
146  }
147 
157  template <class TO> bool requireRelationTo(const StoreArray<TO>& toArray,
159  const std::string& namedRelation = "") const
160  {
161  return DataStore::Instance().requireRelation(*this, toArray, durability, namedRelation);
162  }
163 
174  template <class TO> bool optionalRelationTo(const StoreArray<TO>& toArray,
176  const std::string& namedRelation = "") const
177  {
178  return DataStore::Instance().optionalRelation(*this, toArray, durability, namedRelation);
179  }
180 
187  template <class TO> bool hasRelationTo(const StoreArray<TO>& toArray, DataStore::EDurability durability = DataStore::c_Event,
188  const std::string& namedRelation = "") const
189  {
190  return DataStore::Instance().hasRelation(*this, toArray, durability, namedRelation);
191  }
192 
199  template <class FROM> bool hasRelationFrom(const StoreArray<FROM>& fromArray,
200  DataStore::EDurability durability = DataStore::c_Event, const std::string& namedRelation = "") const
201  {
202  return DataStore::Instance().hasRelation(fromArray, *this, durability, namedRelation);
203  }
204 
209  void clear() override
210  {
211  if (getEntries() != 0) {
212  (*m_storeArray)->Delete();
214  }
215  }
216 
218  inline int getEntries() const { return isCreated() ? ((*m_storeArray)->GetEntriesFast()) : 0;}
219 
230  inline T* operator [](int i) const
231  {
232  ensureCreated();
233  //At() checks for out-of-range and returns NULL in that case
234  TObject* obj = (*m_storeArray)->At(i);
235  if (obj == nullptr)
236  throw std::out_of_range("Out-of-range access in StoreArray::operator[], for " + readableName() + ", index " + std::to_string(i));
237  return static_cast<T*>(obj); //type was checked by DataStore, so the cast is safe.
238  }
239 
248  inline T* appendNew() { return new(nextFreeAdress()) T(); }
249 
270  template<class ...Args> T* appendNew(Args&& ... params)
271  {
272  return new(nextFreeAdress()) T(std::forward<Args>(params)...);
273  }
274 
275 
277  static std::vector<std::string> getArrayList(DataStore::EDurability durability = DataStore::c_Event)
278  {
279  return DataStore::Instance().getListOfArrays(T::Class(), durability);
280  }
281 
282 
290  inline bool isValid() const
291  {
292  ensureAttached();
293  return m_storeArray;
294  }
295 
303  inline operator bool() const { return isValid(); }
304 
313  TClonesArray* getPtr() const
314  {
315  ensureCreated();
316  return (m_storeArray ? *m_storeArray : nullptr);
317  }
318 
322  iterator end() { ensureAttached(); return iterator(m_storeArray, true); }
323 
327  const_iterator end() const { ensureAttached(); return const_iterator(m_storeArray, true); }
328 
329  private:
331  bool create(bool replace = false) { return StoreAccessorBase::create(replace); }
332 
337  inline T* nextFreeAdress()
338  {
340  return static_cast<T*>((*m_storeArray)->AddrAt(getEntries()));
341  }
342 
344  inline void ensureAttached() const
345  {
346  if (!m_storeArray) {
347  const_cast<StoreArray*>(this)->m_storeArray = reinterpret_cast<TClonesArray**>(DataStore::Instance().getObject(*this));
348  }
349  }
354  inline void ensureCreated() const
355  {
356  if (!isCreated()) {
357  if (!const_cast<StoreArray*>(this)->create())
358  throw std::runtime_error("Write access to " + readableName() + " failed, did you remember to call registerInDataStore()?");
359  }
360  }
361 
363  inline bool isCreated() const
364  {
365  ensureAttached();
366  return m_storeArray && *m_storeArray;
367  }
368 
370  TClonesArray** m_storeArray;
371 
372  };
374 } // end namespace Belle2
Belle2::StoreArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:256
Belle2::StoreArray::ensureAttached
void ensureAttached() const
Ensure that this object is attached.
Definition: StoreArray.h:352
Belle2::DataStore::registerRelation
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
Belle2::StoreArray::optionalRelationTo
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:182
Belle2::StoreArray::StoreArray
StoreArray(const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
Constructor to access an array in the DataStore.
Definition: StoreArray.h:136
Belle2::StoreArray::registerRelationTo
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:150
Belle2::DataStore::getListOfArrays
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:667
Belle2::DataStore::Instance
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Belle2::StoreArray::m_storeArray
TClonesArray ** m_storeArray
Pointer that actually holds the TClonesArray.
Definition: StoreArray.h:378
Belle2::DataStore::EStoreFlags
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:71
Belle2::StoreArray::getArrayList
static std::vector< std::string > getArrayList(DataStore::EDurability durability=DataStore::c_Event)
Return list of array names with matching type.
Definition: StoreArray.h:285
Belle2::StoreAccessorBase
Base class for StoreObjPtr and StoreArray for easier common treatment.
Definition: StoreAccessorBase.h:29
Belle2::DataStore::getObject
TObject ** getObject(const StoreAccessorBase &accessor)
Get a pointer to a pointer of an object in the DataStore.
Definition: DataStore.cc:306
Belle2::DataStore::hasRelation
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
Belle2::_StoreArrayImpl::clearRelations
void clearRelations(const StoreAccessorBase &array)
clear all relations touching the given array.
Definition: StoreArray.cc:18
Belle2::DataStore::c_WriteOut
@ c_WriteOut
Object/array should be saved by output modules.
Definition: DataStore.h:72
Belle2::StoreArray::ensureCreated
void ensureCreated() const
Ensure that the array has been created.
Definition: StoreArray.h:362
Belle2::StoreAccessorBase::create
bool create(bool replace=false)
Create a default object in the data store.
Definition: StoreAccessorBase.h:109
Belle2::DataStore::requireRelation
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:723
Belle2::StoreArray::clear
void clear() override
Delete all entries in this array.
Definition: StoreArray.h:217
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreArray::iterator
ObjArrayIterator< TClonesArray, T > iterator
STL-like iterator over the T objects (not T* ).
Definition: StoreArray.h:126
Belle2::StoreArray::hasRelationFrom
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:207
Belle2::StoreArray::operator[]
T * operator[](int i) const
Access to the stored objects.
Definition: StoreArray.h:238
Belle2::StoreArray::begin
iterator begin()
Return iterator to first entry.
Definition: StoreArray.h:328
Belle2::StoreArray::isValid
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:298
Belle2::StoreArray::end
iterator end()
Return iterator to last entry +1.
Definition: StoreArray.h:330
Belle2::StoreArray::getPtr
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition: StoreArray.h:321
Belle2::StoreArray::const_iterator
ObjArrayIterator< const TClonesArray, const T > const_iterator
STL-like const_iterator over the T objects (not T* ).
Definition: StoreArray.h:128
Belle2::StoreArray::nextFreeAdress
T * nextFreeAdress()
Returns address of the next free position of the array.
Definition: StoreArray.h:345
Belle2::StoreArray::requireRelationTo
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:165
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::DataStore::optionalRelation
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:740
Belle2::DataStore::c_Event
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:61
Belle2::DataStore
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:53
Belle2::StoreArray::create
bool create(bool replace=false)
Creating StoreArrays is unnecessary, only used internally.
Definition: StoreArray.h:339
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::StoreArray::hasRelationTo
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:195
Belle2::StoreArray::isCreated
bool isCreated() const
Check wether the array object was created.
Definition: StoreArray.h:371
Belle2::DataStore::EDurability
EDurability
Durability types.
Definition: DataStore.h:60