Belle II Software  release-06-02-00
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 class TClonesArray;
17 class TClass;
18 class TObject;
19 
20 #include <string>
21 
22 namespace Belle2 {
56  class PyStoreArray {
57  public:
59  static std::vector<std::string> list(DataStore::EDurability durability = DataStore::EDurability::c_Event);
60 
62  static void printList(DataStore::EDurability durability = DataStore::EDurability::c_Event);
63 
68  explicit PyStoreArray(const std::string& name,
69  DataStore::EDurability durability = DataStore::EDurability::c_Event);
70 
75  explicit PyStoreArray(TClass* objClass,
76  DataStore::EDurability durability = DataStore::EDurability::c_Event);
77 
83  explicit PyStoreArray(TClass* objClass,
84  const std::string& name,
85  DataStore::EDurability durability = DataStore::EDurability::c_Event);
86 
94 
102  bool registerInDataStore(const std::string& name = "",
103  DataStore::EStoreFlags storeFlags = DataStore::EStoreFlags::c_WriteOut);
104 
112  bool isRequired(const std::string& name = "");
113 
122  bool isOptional(const std::string& name = "");
123 
134  bool registerRelationTo(const PyStoreArray& toArray,
135  DataStore::EDurability durability = DataStore::EDurability::c_Event,
136  DataStore::EStoreFlags storeFlags = DataStore::EStoreFlags::c_WriteOut,
137  std::string const& namedRelation = "") const;
138 
148  bool requireRelationTo(const PyStoreArray& toArray,
150  std::string const& namedRelation = "") const;
151 
162  bool optionalRelationTo(const PyStoreArray& toArray,
164  std::string const& namedRelation = "") const;
165 
172  bool hasRelationTo(const PyStoreArray& toArray,
174  const std::string& namedRelation = "") const;
175 
182  bool hasRelationFrom(const PyStoreArray& fromArray,
184  const std::string& namedRelation = "") const;
185 
187  std::string getName() const { return m_storeAccessor.getName(); }
188 
190  bool hasValidClass() const;
191 
193  bool isValid() const;
194 
196  operator bool() const { return isValid(); }
197 
198  //------------------------------ Accessing content of the array ----------------------------------
200  TObject* operator [](int i) const;
201 
203  int getEntries() const;
204 
206  int __len__() const { return getEntries(); }
207 
210  TIter __iter__() const;
211 
216  TObject* appendNew();
217 
225  TClonesArray* getPtr();
226 
227  private:
229  void ensureCreated();
230 
236  bool create(bool replace = false);
237 
239  void ensureAttached() const;
240 
242  void attach() const;
243 
244  private:
247 
249  mutable StoreEntry* m_storeEntry = nullptr;
250  };
252 }
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:69
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
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:56
StoreAccessorBase m_storeAccessor
Store accessor to retrieve the object.
Definition: PyStoreArray.h:246
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
std::string getName() const
Return name under which the object is saved in the DataStore.
Definition: PyStoreArray.h:187
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
int __len__() const
Support for len().
Definition: PyStoreArray.h:206
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:249
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
Base class for StoreObjPtr and StoreArray for easier common treatment.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
Abstract base class for different kinds of events.
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:22