Belle II Software  release-05-01-25
PyStoreObj.cc
1 #include <framework/pybasf2/PyStoreObj.h>
2 
3 #include <framework/datastore/DataStore.h>
4 #include <framework/datastore/StoreAccessorBase.h>
5 #include <framework/logging/Logger.h>
6 
7 #include <TObject.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 vector<string> PyStoreObj::list(DataStore::EDurability durability)
22 {
23  return DataStore::Instance().getListOfObjects(TObject::Class(), durability);
24 }
25 
27 {
28  for (const auto& n : list(durability))
29  B2INFO(n);
30 }
31 
32 
33 PyStoreObj::PyStoreObj(const std::string& name,
34  DataStore::EDurability durability):
35  PyStoreObj(replaceNullPtr(DataStore::getTClassFromDefaultObjectName(name),
36  TObject::Class()),
37  /* Default to TObject for unknown class for backwards compatability */
38  name,
39  durability)
40 {
41 }
42 
43 PyStoreObj::PyStoreObj(TClass* objClass,
44  DataStore::EDurability durability) :
45  PyStoreObj(objClass, DataStore::defaultObjectName(objClass), durability)
46 {
47 }
48 
49 PyStoreObj::PyStoreObj(TClass* objClass,
50  const std::string& name,
51  DataStore::EDurability durability) :
52  m_storeAccessor(name, durability, objClass, false)
53 {
54  // Attach if already created
55  attach();
56 }
57 
59 {
60  return registerInDataStore(m_storeAccessor.getName(), storeFlags);
61 }
62 
63 bool PyStoreObj::registerInDataStore(const std::string& name, DataStore::EStoreFlags storeFlags)
64 {
65  if (not hasValidClass()) {
66  B2ERROR("Cannot register PyStoreObj '" << name << "' with unknown TClass. Please supply one to the PyStoreObj 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 PyStoreObj::isRequired(const std::string& name)
76 {
77  return m_storeAccessor.isRequired(name);
78 }
79 
80 bool PyStoreObj::isOptional(const std::string& name)
81 {
82  return m_storeAccessor.isOptional(name);
83 }
84 
85 bool PyStoreObj::isValid() const
86 {
87  return m_storeEntry and m_storeEntry->ptr;
88 }
89 
91 {
92  const TClass* objClass = m_storeAccessor.getClass();
93  return objClass and objClass != TObject::Class();
94 }
95 
96 bool PyStoreObj::create(bool replace)
97 {
99  if (not m_storeEntry) {
100  // Attaching failed
101  B2ERROR("Cannot create unregistered PyStoreObj.");
102  return false;
103  }
104 
105  // Short cut when an object has been created and no replacement is requested.
106  if (isValid() and not replace) return true;
107 
108  if (not isValid() and not hasValidClass()) {
109  B2ERROR("Cannot create PyStoreObj with unknown TClass.");
110  return false;
111  } else {
112  // StoreObj has been created before or has a valid class
113  // Go ahead and (re)create it
114  return m_storeAccessor.create(replace);
115  }
116 }
117 
119 {
120  if (not m_storeEntry) {
121  attach();
122  }
123  if (not m_storeEntry) {
124  B2ERROR("PyStoreObj " << m_storeAccessor.readableName() << " has not been registered!");
125  }
126 }
127 
128 void PyStoreObj::attach() const
129 {
131 }
132 
133 bool PyStoreObj::assign(TObject* object, bool replace)
134 {
135  return m_storeAccessor.assign(object, replace);
136 }
Belle2::PyStoreObj::registerInDataStore
bool registerInDataStore(DataStore::EStoreFlags storeFlags)
Register the object in the DataStore.
Definition: PyStoreObj.cc:58
Belle2::PyStoreObj::isValid
bool isValid() const
Check whether the object was registered and created.
Definition: PyStoreObj.cc:85
Belle2::PyStoreObj::PyStoreObj
PyStoreObj(const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
constructor.
Definition: PyStoreObj.cc:33
Belle2::StoreAccessorBase::getClass
TClass * getClass() const
The underlying object's type.
Definition: StoreAccessorBase.h:151
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::PyStoreObj::list
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available objects for given durability.
Definition: PyStoreObj.cc:21
Belle2::StoreAccessorBase::assign
bool assign(TObject *object, bool replace=false)
Assign 'object' to this accessor.
Definition: StoreAccessorBase.cc:35
Belle2::DataStore::EStoreFlags
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:71
Belle2::StoreAccessorBase::isOptional
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Definition: StoreAccessorBase.h:95
Belle2::PyStoreObj::printList
static void printList(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Print list of available objects for given durability.
Definition: PyStoreObj.cc:26
Belle2::StoreAccessorBase::getName
const std::string & getName() const
Return name under which the object is saved in the DataStore.
Definition: StoreAccessorBase.h:130
Belle2::PyStoreObj
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:69
Belle2::StoreAccessorBase::registerInDataStore
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Definition: StoreAccessorBase.h:54
Belle2::DataStore::getListOfObjects
std::vector< std::string > getListOfObjects(const TClass *objClass, EDurability durability) const
Returns a list of names of StoreObjPtr-objects whose class is (or inherits from) objClass.
Definition: DataStore.cc:672
Belle2::StoreAccessorBase::isRequired
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Definition: StoreAccessorBase.h:80
Belle2::PyStoreObj::m_storeEntry
StoreEntry * m_storeEntry
Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataS...
Definition: PyStoreObj.h:183
Belle2::PyStoreObj::hasValidClass
bool hasValidClass() const
Check whether a TClass for the contained object could be determined.
Definition: PyStoreObj.cc:90
Belle2::StoreAccessorBase::create
bool create(bool replace=false)
Create a default object in the data store.
Definition: StoreAccessorBase.h:109
Belle2::PyStoreObj::ensureAttached
void ensureAttached() const
Ensure that contained TObject has been attached to a memory location on the DataStore.
Definition: PyStoreObj.cc:118
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::PyStoreObj::isOptional
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Definition: PyStoreObj.cc:80
Belle2::PyStoreObj::m_storeAccessor
StoreAccessorBase m_storeAccessor
Store accessor to retrieve the object.
Definition: PyStoreObj.h:180
Belle2::PyStoreObj::create
bool create(bool replace=false)
Create default constructed object in the DataStore.
Definition: PyStoreObj.cc:96
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::PyStoreObj::isRequired
bool isRequired(const std::string &name="")
Ensure this object has been registered previously.
Definition: PyStoreObj.cc:75
Belle2::PyStoreObj::attach
void attach() const
Lookup the store entry and cache a pointer to it.
Definition: PyStoreObj.cc:128
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::PyStoreObj::assign
bool assign(TObject *object, bool replace=false)
Assign 'object' to the accessor.
Definition: PyStoreObj.cc:133
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