Belle II Software light-2406-ragdoll
PyStoreObj.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/PyStoreObj.h>
9
10#include <framework/datastore/DataStore.h>
11#include <framework/datastore/StoreAccessorBase.h>
12#include <framework/logging/Logger.h>
13
14#include <TObject.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
28vector<string> PyStoreObj::list(DataStore::EDurability durability)
29{
30 return DataStore::Instance().getListOfObjects(TObject::Class(), durability);
31}
32
34{
35 for (const auto& n : list(durability))
36 B2INFO(n);
37}
38
39
40PyStoreObj::PyStoreObj(const std::string& name,
41 DataStore::EDurability durability):
42 PyStoreObj(replaceNullPtr(DataStore::getTClassFromDefaultObjectName(name),
43 TObject::Class()),
44 /* Default to TObject for unknown class for backwards compatability */
45 name,
46 durability)
47{
48}
49
50PyStoreObj::PyStoreObj(TClass* objClass,
51 DataStore::EDurability durability) :
52 PyStoreObj(objClass, DataStore::defaultObjectName(objClass), durability)
53{
54}
55
56PyStoreObj::PyStoreObj(TClass* objClass,
57 const std::string& name,
58 DataStore::EDurability durability) :
59 m_storeAccessor(name, durability, objClass, false)
60{
61 // Attach if already created
62 attach();
63}
64
66{
67 return registerInDataStore(m_storeAccessor.getName(), storeFlags);
68}
69
70bool PyStoreObj::registerInDataStore(const std::string& name, DataStore::EStoreFlags storeFlags)
71{
72 if (not hasValidClass()) {
73 B2ERROR("Cannot register PyStoreObj '" << name << "' with unknown TClass. Please supply one to the PyStoreObj constructor.");
74 return false;
75 }
76
77 bool success = m_storeAccessor.registerInDataStore(name, storeFlags);
78 if (success) attach();
79 return success;
80}
81
82bool PyStoreObj::isRequired(const std::string& name)
83{
84 return m_storeAccessor.isRequired(name);
85}
86
87bool PyStoreObj::isOptional(const std::string& name)
88{
89 return m_storeAccessor.isOptional(name);
90}
91
93{
94 return m_storeEntry and m_storeEntry->ptr;
95}
96
98{
99 const TClass* objClass = m_storeAccessor.getClass();
100 return objClass and objClass != TObject::Class();
101}
102
103bool PyStoreObj::create(bool replace)
104{
106 if (not m_storeEntry) {
107 // Attaching failed
108 B2ERROR("Cannot create unregistered PyStoreObj.");
109 return false;
110 }
111
112 // Short cut when an object has been created and no replacement is requested.
113 if (isValid() and not replace) return true;
114
115 if (not isValid() and not hasValidClass()) {
116 B2ERROR("Cannot create PyStoreObj with unknown TClass.");
117 return false;
118 } else {
119 // StoreObj has been created before or has a valid class
120 // Go ahead and (re)create it
121 return m_storeAccessor.create(replace);
122 }
123}
124
126{
127 if (not m_storeEntry) {
128 attach();
129 if (not m_storeEntry) {
130 B2ERROR("PyStoreObj " << m_storeAccessor.readableName() << " has not been registered!");
131 }
132 }
133}
134
136{
138}
139
140bool PyStoreObj::assign(TObject* object, bool replace)
141{
142 return m_storeAccessor.assign(object, replace);
143}
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
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:671
EDurability
Durability types.
Definition: DataStore.h:58
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
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 StoreObjPtr.
Definition: PyStoreObj.h:67
StoreAccessorBase m_storeAccessor
Store accessor to retrieve the object.
Definition: PyStoreObj.h:178
static void printList(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Print list of available objects for given durability.
Definition: PyStoreObj.cc:33
PyStoreObj(const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
constructor.
Definition: PyStoreObj.cc:40
void ensureAttached() const
Ensure that contained TObject has been attached to a memory location on the DataStore.
Definition: PyStoreObj.cc:125
bool hasValidClass() const
Check whether a TClass for the contained object could be determined.
Definition: PyStoreObj.cc:97
bool isValid() const
Check whether the object was registered and created.
Definition: PyStoreObj.cc:92
bool isRequired(const std::string &name="")
Ensure this object has been registered previously.
Definition: PyStoreObj.cc:82
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Definition: PyStoreObj.cc:87
bool registerInDataStore(DataStore::EStoreFlags storeFlags)
Register the object in the DataStore.
Definition: PyStoreObj.cc:65
bool assign(TObject *object, bool replace=false)
Assign 'object' to the accessor.
Definition: PyStoreObj.cc:140
StoreEntry * m_storeEntry
Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataS...
Definition: PyStoreObj.h:181
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available objects for given durability.
Definition: PyStoreObj.cc:28
void attach() const
Lookup the store entry and cache a pointer to it.
Definition: PyStoreObj.cc:135
bool create(bool replace=false)
Create default constructed object in the DataStore.
Definition: PyStoreObj.cc:103
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).
bool assign(TObject *object, bool replace=false)
Assign 'object' to this accessor.
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.
Definition: ClusterUtils.h:24
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