Belle II Software development
PyStoreArray.cc
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#include <framework/pybasf2/PyStoreArray.h>
9
10#include <framework/logging/Logger.h>
11#include <framework/datastore/DataStore.h>
12#include <framework/datastore/StoreAccessorBase.h>
13
14#include "TClonesArray.h"
15#include "TClass.h"
16
17using namespace Belle2;
18using namespace std;
19
20namespace {
21 template<class T>
22 T* replaceNullPtr(T* value, T* fallback)
23 {
24 return value ? value : fallback;
25 }
26}
27
28std::vector<std::string> PyStoreArray::list(DataStore::EDurability durability)
29{
30 return DataStore::Instance().getListOfArrays(TObject::Class(),
31 durability);
32}
33
35{
36 for (const auto& n : list(durability))
37 B2INFO(n);
38}
39
40PyStoreArray::PyStoreArray(const std::string& name,
41 DataStore::EDurability durability):
42 PyStoreArray(replaceNullPtr(DataStore::getTClassFromDefaultArrayName(name),
43 TObject::Class()),
44 /* Default to TObject for unknown class for backwards compatability */
45 name,
46 durability)
47{
48}
49
51 DataStore::EDurability durability) :
52 PyStoreArray(objClass, DataStore::defaultArrayName(objClass), durability)
53{
54}
55
57 const std::string& name,
58 DataStore::EDurability durability) :
59 m_storeAccessor(name, durability, objClass, true)
60{
61 attach();
62}
63
65{
66 return registerInDataStore(m_storeAccessor.getName(), storeFlags);
67}
68
69bool PyStoreArray::registerInDataStore(const std::string& name,
70 DataStore::EStoreFlags storeFlags)
71{
72 if (not hasValidClass()) {
73 B2ERROR("Cannot register PyStoreArray '" << name << "' with unknown TClass. Please supply one to the PyStoreArray constructor.");
74 return false;
75 }
76
77 bool success = m_storeAccessor.registerInDataStore(name, storeFlags);
78 if (success) attach();
79 return success;
80}
81
82bool PyStoreArray::isRequired(const std::string& name)
83{
84 return m_storeAccessor.isRequired(name);
85}
86
87bool PyStoreArray::isOptional(const std::string& name)
88{
89 return m_storeAccessor.isOptional(name);
90}
91
93 DataStore::EDurability durability,
94 DataStore::EStoreFlags storeFlags,
95 std::string const& namedRelation) const
96{
98 toArray.m_storeAccessor,
99 durability,
100 storeFlags,
101 namedRelation);
102}
103
105 DataStore::EDurability durability,
106 std::string const& namedRelation) const
107{
109 toArray.m_storeAccessor,
110 durability,
111 namedRelation);
112}
113
115 DataStore::EDurability durability,
116 std::string const& namedRelation) const
117{
119 toArray.m_storeAccessor,
120 durability,
121 namedRelation);
122}
123
125 DataStore::EDurability durability,
126 const std::string& namedRelation) const
127{
129 toArray.m_storeAccessor,
130 durability,
131 namedRelation);
132}
133
135 DataStore::EDurability durability,
136 const std::string& namedRelation) const
137{
139 this->m_storeAccessor,
140 durability,
141 namedRelation);
142}
143
145{
146 const TClass* objClass = m_storeAccessor.getClass();
147 return objClass and objClass != TObject::Class();
148}
149
151{
152 return m_storeEntry and m_storeEntry->ptr;
153}
154
155TObject* PyStoreArray::operator [](int i) const
156{
158 if (not isValid()) {
159 return nullptr;
160 } else {
161 return m_storeEntry->getPtrAsArray()->At(i);
162 }
163}
164
166{
168 return isValid() ? (m_storeEntry->getPtrAsArray()->GetEntriesFast()) : 0;
169}
170
172{
174 // will create empty iterator if nullptr
175 return TIter(isValid() ? m_storeEntry->getPtrAsArray() : nullptr);
176}
177
179{
181 if (isValid()) {
182 return getPtr()->ConstructedAt(getEntries());
183 } else {
184 B2ERROR("Cannot create an object in invalid PyStoreArray.");
185 return nullptr;
186 }
187}
188
189TClonesArray* PyStoreArray::getPtr()
190{
192 return isValid() ? m_storeEntry->getPtrAsArray() : nullptr;
193}
194
196{
197 create(false);
198}
199
200bool PyStoreArray::create(bool replace)
201{
203 if (not m_storeEntry) {
204 // Attaching failed
205 B2ERROR("Cannot create unregistered PyStoreArray.");
206 return false;
207 }
208
209 // Short cut when an array has been created and no replacement is requested.
210 if (isValid() and not replace) return true;
211
212 if (not isValid() and not hasValidClass()) {
213 B2ERROR("Cannot create PyStoreArray with unknown TClass.");
214 return false;
215 } else {
216 // Array has been created before or has a valid class
217 // Go ahead and (re)create it
218 return m_storeAccessor.create(replace);
219 }
220}
221
223{
224 if (not m_storeEntry) {
225 attach();
226 if (not m_storeEntry) {
227 B2ERROR("PyStoreArray " << m_storeAccessor.readableName() << " has not been registered!");
228 }
229 }
230}
231
233{
235}
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:69
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
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
StoreEntry * getEntry(const StoreAccessorBase &accessor)
Check whether an entry with the correct type is registered in the DataStore map and return it.
Definition: DataStore.cc:294
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
StoreAccessorBase m_storeAccessor
Store accessor to retrieve the object.
Definition: PyStoreArray.h:262
static void printList(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Print list of available arrays for given durability.
Definition: PyStoreArray.cc:34
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 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.
Definition: PyStoreArray.cc:82
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Definition: PyStoreArray.cc:87
PyStoreArray(const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
constructor.
Definition: PyStoreArray.cc:40
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.
Definition: PyStoreArray.cc:92
bool registerInDataStore(DataStore::EStoreFlags storeFlags)
Register the array in the data store.
Definition: PyStoreArray.cc:64
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...
Definition: PyStoreArray.h:265
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.
Definition: PyStoreArray.cc:28
const std::string & getName() const
Return name under which the object is saved in the DataStore.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
std::string readableName() const
Convert this acessor into a readable string (for messages).
TClass * getClass() const
The underlying object's type.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
bool create(bool replace=false)
Create a default object in the data store.
Abstract base class for different kinds of events.
STL namespace.
TObject * ptr
The pointer to the returned object, either equal to 'object' or null, depending on wether the object ...
Definition: StoreEntry.h:51
TClonesArray * getPtrAsArray() const
Return ptr cast to TClonesArray.
Definition: StoreEntry.cc:83