Belle II Software  release-05-02-19
PyStoreArray.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2012 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/datastore/DataStore.h>
14 #include <framework/datastore/StoreAccessorBase.h>
15 
16 #include <TCollection.h> //for TIter
17 
18 class TClonesArray;
19 class TClass;
20 class TObject;
21 
22 #include <string>
23 
24 namespace Belle2 {
58  class PyStoreArray {
59  public:
61  static std::vector<std::string> list(DataStore::EDurability durability = DataStore::EDurability::c_Event);
62 
64  static void printList(DataStore::EDurability durability = DataStore::EDurability::c_Event);
65 
70  explicit PyStoreArray(const std::string& name,
71  DataStore::EDurability durability = DataStore::EDurability::c_Event);
72 
77  explicit PyStoreArray(TClass* objClass,
78  DataStore::EDurability durability = DataStore::EDurability::c_Event);
79 
85  explicit PyStoreArray(TClass* objClass,
86  const std::string& name,
87  DataStore::EDurability durability = DataStore::EDurability::c_Event);
88 
96 
104  bool registerInDataStore(const std::string& name = "",
105  DataStore::EStoreFlags storeFlags = DataStore::EStoreFlags::c_WriteOut);
106 
114  bool isRequired(const std::string& name = "");
115 
124  bool isOptional(const std::string& name = "");
125 
136  bool registerRelationTo(const PyStoreArray& toArray,
137  DataStore::EDurability durability = DataStore::EDurability::c_Event,
138  DataStore::EStoreFlags storeFlags = DataStore::EStoreFlags::c_WriteOut,
139  std::string const& namedRelation = "") const;
140 
149  bool requireRelationTo(const PyStoreArray& toArray,
151  std::string const& namedRelation = "") const;
152 
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 }
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::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::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::StoreEntry
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:15
Belle2::StoreAccessorBase::getName
const std::string & getName() const
Return name under which the object is saved in the DataStore.
Definition: StoreAccessorBase.h:130
Belle2::StoreAccessorBase
Base class for StoreObjPtr and StoreArray for easier common treatment.
Definition: StoreAccessorBase.h:29
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::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::PyStoreArray::__len__
int __len__() const
Support for len().
Definition: PyStoreArray.h:206
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::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::getName
std::string getName() const
Return name under which the object is saved in the DataStore.
Definition: PyStoreArray.h:187
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::PyStoreArray
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:58
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::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::EDurability
EDurability
Durability types.
Definition: DataStore.h:60