Belle II Software  release-05-02-19
PyStoreArray.cc
1 #include <framework/pybasf2/PyStoreArray.h>
2 
3 #include <framework/logging/Logger.h>
4 #include <framework/datastore/DataStore.h>
5 #include <framework/datastore/StoreAccessorBase.h>
6 
7 #include "TClonesArray.h"
8 #include "TClass.h"
9 
10 using namespace Belle2;
11 using namespace std;
12 
13 namespace {
14  template<class T>
15  T* replaceNullPtr(T* value, T* fallback)
16  {
17  return value ? value : fallback;
18  }
19 }
20 
21 std::vector<std::string> PyStoreArray::list(DataStore::EDurability durability)
22 {
23  return DataStore::Instance().getListOfArrays(TObject::Class(),
24  durability);
25 }
26 
28 {
29  for (const auto& n : list(durability))
30  B2INFO(n);
31 }
32 
33 PyStoreArray::PyStoreArray(const std::string& name,
34  DataStore::EDurability durability):
35  PyStoreArray(replaceNullPtr(DataStore::getTClassFromDefaultArrayName(name),
36  TObject::Class()),
37  /* Default to TObject for unknown class for backwards compatability */
38  name,
39  durability)
40 {
41 }
42 
43 PyStoreArray::PyStoreArray(TClass* objClass,
44  DataStore::EDurability durability) :
45  PyStoreArray(objClass, DataStore::defaultArrayName(objClass), durability)
46 {
47 }
48 
49 PyStoreArray::PyStoreArray(TClass* objClass,
50  const std::string& name,
51  DataStore::EDurability durability) :
52  m_storeAccessor(name, durability, objClass, true)
53 {
54  attach();
55 }
56 
58 {
59  return registerInDataStore(m_storeAccessor.getName(), storeFlags);
60 }
61 
62 bool PyStoreArray::registerInDataStore(const std::string& name,
63  DataStore::EStoreFlags storeFlags)
64 {
65  if (not hasValidClass()) {
66  B2ERROR("Cannot register PyStoreArray '" << name << "' with unknown TClass. Please supply one to the PyStoreArray constructor.");
67  return false;
68  }
69 
70  bool success = m_storeAccessor.registerInDataStore(name, storeFlags);
71  if (success) attach();
72  return success;
73 }
74 
75 bool PyStoreArray::isRequired(const std::string& name)
76 {
77  return m_storeAccessor.isRequired(name);
78 }
79 
80 bool PyStoreArray::isOptional(const std::string& name)
81 {
82  return m_storeAccessor.isOptional(name);
83 }
84 
86  DataStore::EDurability durability,
87  DataStore::EStoreFlags storeFlags,
88  std::string const& namedRelation) const
89 {
91  toArray.m_storeAccessor,
92  durability,
93  storeFlags,
94  namedRelation);
95 }
96 
98  DataStore::EDurability durability,
99  std::string const& namedRelation) const
100 {
102  toArray.m_storeAccessor,
103  durability,
104  namedRelation);
105 }
106 
108  DataStore::EDurability durability,
109  std::string const& namedRelation) const
110 {
112  toArray.m_storeAccessor,
113  durability,
114  namedRelation);
115 }
116 
118  DataStore::EDurability durability,
119  const std::string& namedRelation) const
120 {
122  toArray.m_storeAccessor,
123  durability,
124  namedRelation);
125 }
126 
128  DataStore::EDurability durability,
129  const std::string& namedRelation) const
130 {
132  this->m_storeAccessor,
133  durability,
134  namedRelation);
135 }
136 
138 {
139  const TClass* objClass = m_storeAccessor.getClass();
140  return objClass and objClass != TObject::Class();
141 }
142 
144 {
145  return m_storeEntry and m_storeEntry->ptr;
146 }
147 
148 TObject* PyStoreArray::operator [](int i) const
149 {
150  ensureAttached();
151  if (not isValid()) {
152  return nullptr;
153  } else {
154  return m_storeEntry->getPtrAsArray()->At(i);
155  }
156 }
157 
159 {
160  ensureAttached();
161  return isValid() ? (m_storeEntry->getPtrAsArray()->GetEntriesFast()) : 0;
162 }
163 
165 {
166  ensureAttached();
167  // will create empty iterator if nullptr
168  return TIter(isValid() ? m_storeEntry->getPtrAsArray() : nullptr);
169 }
170 
172 {
173  ensureCreated();
174  if (isValid()) {
175  return getPtr()->ConstructedAt(getEntries());
176  } else {
177  B2ERROR("Cannot create an object in invalid PyStoreArray.");
178  return nullptr;
179  }
180 }
181 
182 TClonesArray* PyStoreArray::getPtr()
183 {
184  ensureCreated();
185  return isValid() ? m_storeEntry->getPtrAsArray() : nullptr;
186 }
187 
189 {
190  create(false);
191 }
192 
193 bool PyStoreArray::create(bool replace)
194 {
195  ensureAttached();
196  if (not m_storeEntry) {
197  // Attaching failed
198  B2ERROR("Cannot create unregistered PyStoreArray.");
199  return false;
200  }
201 
202  // Short cut when an array has been created and no replacement is requested.
203  if (isValid() and not replace) return true;
204 
205  if (not isValid() and not hasValidClass()) {
206  B2ERROR("Cannot create PyStoreArray with unknown TClass.");
207  return false;
208  } else {
209  // Array has been created before or has a valid class
210  // Go ahead and (re)create it
211  return m_storeAccessor.create(replace);
212  }
213 }
214 
216 {
217  if (not m_storeEntry) {
218  attach();
219  }
220  if (not m_storeEntry) {
221  B2ERROR("PyStoreArray " << m_storeAccessor.readableName() << " has not been registered!");
222  }
223 }
224 
226 {
228 }
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::PyStoreArray::ensureCreated
void ensureCreated()
Ensure that contained TClonesArray has been created on the DataStore.
Definition: PyStoreArray.cc:188
Belle2::PyStoreArray::registerRelationTo
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:85
Belle2::PyStoreArray::getPtr
TClonesArray * getPtr()
Raw access to the underlying TClonesArray.
Definition: PyStoreArray.cc:182
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::StoreAccessorBase::getClass
TClass * getClass() const
The underlying object's type.
Definition: StoreAccessorBase.h:151
Belle2::PyStoreArray::requireRelationTo
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.
Definition: PyStoreArray.cc:97
Belle2::PyStoreArray::ensureAttached
void ensureAttached() const
Ensure that contained TClonesArray has been attached to a memory location on the DataStore.
Definition: PyStoreArray.cc:215
Belle2::DataStore::Instance
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Belle2::StoreAccessorBase::readableName
std::string readableName() const
Convert this acessor into a readable string (for messages).
Definition: StoreAccessorBase.cc:20
Belle2::PyStoreArray::m_storeEntry
StoreEntry * m_storeEntry
Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataS...
Definition: PyStoreArray.h:249
Belle2::DataStore::EStoreFlags
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:71
Belle2::PyStoreArray::hasValidClass
bool hasValidClass() const
Check whether a TClass of the objects in this PyStoreArray could be determined.
Definition: PyStoreArray.cc:137
Belle2::StoreAccessorBase::isOptional
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Definition: StoreAccessorBase.h:95
Belle2::StoreAccessorBase::getName
const std::string & getName() const
Return name under which the object is saved in the DataStore.
Definition: StoreAccessorBase.h:130
Belle2::PyStoreArray::list
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available arrays for given durability.
Definition: PyStoreArray.cc:21
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::StoreAccessorBase::registerInDataStore
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Definition: StoreAccessorBase.h:54
Belle2::PyStoreArray::isValid
bool isValid() const
Check whether the array was registered and created.
Definition: PyStoreArray.cc:143
Belle2::PyStoreArray::hasRelationFrom
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)
Definition: PyStoreArray.cc:127
Belle2::PyStoreArray::PyStoreArray
PyStoreArray(const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
constructor.
Definition: PyStoreArray.cc:33
Belle2::PyStoreArray::getEntries
int getEntries() const
returns number of entries for current event.
Definition: PyStoreArray.cc:158
Belle2::StoreAccessorBase::isRequired
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Definition: StoreAccessorBase.h:80
Belle2::PyStoreArray::hasRelationTo
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)
Definition: PyStoreArray.cc:117
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::PyStoreArray::appendNew
TObject * appendNew()
Construct a new object of the array's type at the end of the array.
Definition: PyStoreArray.cc:171
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::PyStoreArray::optionalRelationTo
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.
Definition: PyStoreArray.cc:107
Belle2::PyStoreArray::__iter__
TIter __iter__() const
Allow iteration using for in Python.
Definition: PyStoreArray.cc:164
Belle2::PyStoreArray::attach
void attach() const
Lookup the store entry and cache a pointer to it.
Definition: PyStoreArray.cc:225
Belle2::PyStoreArray::operator[]
TObject * operator[](int i) const
returns object at index i, or null pointer if out of range (+error)
Definition: PyStoreArray.cc:148
Belle2::StoreEntry::getPtrAsArray
TClonesArray * getPtrAsArray() const
Return ptr cast to TClonesArray.
Definition: StoreEntry.cc:76
Belle2::PyStoreArray
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:58
Belle2::StoreEntry::ptr
TObject * ptr
The pointer to the returned object, either equal to 'object' or null, depending on wether the object ...
Definition: StoreEntry.h:44
Belle2::PyStoreArray::printList
static void printList(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Print list of available arrays for given durability.
Definition: PyStoreArray.cc:27
Belle2::PyStoreArray::m_storeAccessor
StoreAccessorBase m_storeAccessor
Store accessor to retrieve the object.
Definition: PyStoreArray.h:246
Belle2::PyStoreArray::registerInDataStore
bool registerInDataStore(DataStore::EStoreFlags storeFlags)
Register the array in the data store.
Definition: PyStoreArray.cc:57
Belle2::PyStoreArray::isOptional
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Definition: PyStoreArray.cc:80
Belle2::PyStoreArray::create
bool create(bool replace=false)
Create constructed TClonesArray in the DataStore.
Definition: PyStoreArray.cc:193
Belle2::PyStoreArray::isRequired
bool isRequired(const std::string &name="")
Ensure this array has been registered previously.
Definition: PyStoreArray.cc:75
Belle2::DataStore::getEntry
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
Belle2::DataStore
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:53
Belle2::DataStore::EDurability
EDurability
Durability types.
Definition: DataStore.h:60