Belle II Software prerelease-10-00-00a
PyStoreArray.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/DataStore.h>
12#include <framework/datastore/StoreAccessorBase.h>
13
14#include <TCollection.h> //for TIter
15
16#include <TClonesArray.h>
17
18class TClass;
19class TObject;
20
21#include <string>
22
23namespace Belle2 {
74 public:
76 static std::vector<std::string> list(DataStore::EDurability durability = DataStore::EDurability::c_Event);
77
80
85 explicit PyStoreArray(const std::string& name,
87
92 explicit PyStoreArray(TClass* objClass,
94
100 explicit PyStoreArray(TClass* objClass,
101 const std::string& name,
103
111
119 bool registerInDataStore(const std::string& name = "",
121
129 bool isRequired(const std::string& name = "");
130
139 bool isOptional(const std::string& name = "");
140
151 bool registerRelationTo(const PyStoreArray& toArray,
154 std::string const& namedRelation = "") const;
155
165 bool requireRelationTo(const PyStoreArray& toArray,
167 std::string const& namedRelation = "") const;
168
179 bool optionalRelationTo(const PyStoreArray& toArray,
181 std::string const& namedRelation = "") const;
182
189 bool hasRelationTo(const PyStoreArray& toArray,
191 const std::string& namedRelation = "") const;
192
199 bool hasRelationFrom(const PyStoreArray& fromArray,
201 const std::string& namedRelation = "") const;
202
204 std::string getName() const { return m_storeAccessor.getName(); }
205
207 TClass* getClass() const {return m_storeAccessor.getClass(); }
208
210 bool hasValidClass() const;
211
213 bool isValid() const;
214
216 operator bool() const { return isValid(); }
217
218 //------------------------------ Accessing content of the array ----------------------------------
220 TObject* operator [](int i) const;
221
223 int getEntries() const;
224
226 int __len__() const { return getEntries(); }
227
230 TIter __iter__() const;
231
236 TObject* appendNew();
237
245 TClonesArray* getPtr();
246
256 template <class T, typename... Args> void fillArray(size_t len, Args... args)
257 {
258 TClonesArray* array = getPtr();
259 for (size_t i = 0; i < len; i++) {
260 new ((*array)[i]) T(args[i]...);
261 }
262 }
263
270 template <class T, typename... Args> void readArray(Args... args)
271 {
272 TClonesArray* array = getPtr();
273 for (size_t i = 0; i < array->GetEntriesFast(); i++) {
274 ((T*)(*array)[i])->fillValues(&args[i]...);
275 }
276 }
277
278
279
280 private:
282 void ensureCreated();
283
289 bool create(bool replace = false);
290
292 void ensureAttached() const;
293
295 void attach() const;
296
297 private:
300
302 mutable StoreEntry* m_storeEntry = nullptr;
303 };
304
305}
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
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
StoreAccessorBase m_storeAccessor
Store accessor to retrieve the object.
static void printList(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Print list of available arrays for given durability.
bool hasRelationFrom(const PyStoreArray &fromArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Check for the existence of a relation from the provided toArray (to this Pystorearray)
void readArray(Args... args)
Templated function to read from the PyStoreArray objects of a certain class (PXDDigits,...
void ensureAttached() const
Ensure that contained TClonesArray has been attached to a memory location on the DataStore.
bool hasValidClass() const
Check whether a TClass of the objects in this PyStoreArray could be determined.
bool isValid() const
Check whether the array was registered and created.
bool isRequired(const std::string &name="")
Ensure this array has been registered previously.
void fillArray(size_t len, Args... args)
Templated function to fill the PyStoreArray with objects of a certain class (PXDDigits,...
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
PyStoreArray(const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
constructor.
std::string getName() const
Return name under which the object is saved in the DataStore.
bool optionalRelationTo(const PyStoreArray &toArray, DataStore::EDurability durability=DataStore::c_Event, std::string const &namedRelation="") const
Tell the data store about a relation that we could make use of.
bool hasRelationTo(const PyStoreArray &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Check for the existence of a relation to the provided toArray (from this Pystorearray)
TObject * operator[](int i) const
returns object at index i, or null pointer if out of range (+error)
bool registerRelationTo(const PyStoreArray &toArray, DataStore::EDurability durability=DataStore::EDurability::c_Event, DataStore::EStoreFlags storeFlags=DataStore::EStoreFlags::c_WriteOut, std::string const &namedRelation="") const
Register a relation to the given PyStoreArray.
int __len__() const
Support for len().
bool registerInDataStore(DataStore::EStoreFlags storeFlags)
Register the array in the data store.
TObject * appendNew()
Construct a new object of the array's type at the end of the array.
int getEntries() const
returns number of entries for current event.
void ensureCreated()
Ensure that contained TClonesArray has been created on the DataStore.
StoreEntry * m_storeEntry
Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataS...
TClass * getClass() const
Return class of the object that is saved in the DataStore.
TClonesArray * getPtr()
Raw access to the underlying TClonesArray.
bool requireRelationTo(const PyStoreArray &toArray, DataStore::EDurability durability=DataStore::c_Event, std::string const &namedRelation="") const
Produce error if no relation from this array to 'toArray' has been registered.
void attach() const
Lookup the store entry and cache a pointer to it.
bool create(bool replace=false)
Create constructed TClonesArray in the DataStore.
TIter __iter__() const
Allow iteration using for in Python.
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available arrays for given durability.
Base class for StoreObjPtr and StoreArray for easier common treatment.
STL class.
Abstract base class for different kinds of events.
Wraps a stored array/object, stored under unique (name, durability) key.
Definition StoreEntry.h:22