Belle II Software development
PyStoreObj Class Reference

a (simplified) python wrapper for StoreObjPtr. More...

#include <PyStoreObj.h>

Public Member Functions

 PyStoreObj (const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor.
 
 PyStoreObj (TClass *objClass, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor.
 
 PyStoreObj (TClass *objClass, const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor.
 
bool registerInDataStore (DataStore::EStoreFlags storeFlags)
 Register the object in the DataStore.
 
bool registerInDataStore (const std::string &name="", DataStore::EStoreFlags storeFlags=DataStore::EStoreFlags::c_WriteOut)
 Register the object in the DataStore.
 
bool isRequired (const std::string &name="")
 Ensure this object has been registered previously.
 
bool isOptional (const std::string &name="")
 Tell the DataStore about an optional input.
 
bool hasValidClass () const
 Check whether a TClass for the contained object could be determined.
 
bool isValid () const
 Check whether the object was registered and created.
 
 operator bool () const
 Does this PyStoreObj contain a valid datastore object?
 
TObject * obj () const
 Returns the attached DataStore object, or nullptr if no valid object exists.
 
TObject * operator-> () const
 Returns the attached DataStore object, or nullptr if no valid object exists.
 
bool assign (TObject *object, bool replace=false)
 Assign 'object' to the accessor.
 
bool create (bool replace=false)
 Create default constructed object in the DataStore.
 

Static Public Member Functions

static std::vector< std::string > list (DataStore::EDurability durability=DataStore::EDurability::c_Event)
 Return list of available objects for given durability.
 
static void printList (DataStore::EDurability durability=DataStore::EDurability::c_Event)
 Print list of available objects for given durability.
 

Private Member Functions

void ensureAttached () const
 Ensure that contained TObject has been attached to a memory location on the DataStore.
 
void attach () const
 Lookup the store entry and cache a pointer to it.
 

Private Attributes

StoreAccessorBase m_storeAccessor
 Store accessor to retrieve the object.
 
StoreEntrym_storeEntry = nullptr
 Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataStore.
 

Detailed Description

a (simplified) python wrapper for StoreObjPtr.

Compared to StoreObjPtr, PyStoreObj returns only TObjects (since it doesn't use templates). Thanks to Python, you can still access all public functions and data members of the actual type.

You can check the runtime type information of the returned objects by using Python's built-in type() function.

Usage example

Inside a Python module's event() function, you can access DataStore objects like this:

from ROOT import Belle2
evtmetadata = Belle2.PyStoreObj('EventMetaData')
# Alternatively: evtmetadata = Belle2.PyStoreObj(Belle2.EventMetaData.Class())
if not evtmetadata:
B2ERROR("No EventMetaData found");
else:
event = evtmetadata.getEvent()
# alternatively: evtmetadata.obj().getEvent()
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67

Most of the time you can just use the PyStoreObj instance like an instance of the class it represents, i.e. call all the members. The only exceptions are if the class has members which are also present in PyStoreObj (for example isValid() or isRequired()). In this case you need to use the obj() member to obtain a reference to the real object first as shown in the example.

Creating objects

You can also create new objects in your Python basf2 module, using registerInDataStore() and create(). Since you cannot specify the type directly, as with template arguments to StoreObjPtr, the class name is assumed to be identical to the 'name' argument given to the constructor, and to reside in the Belle2 namespace. Consequently, you can only create objects with their default names.

See display/examples/displaydata.py for a concrete example.

See also
PyStoreArray and the Conditions Data interface classes PyDBObj and PyDBArray

Definition at line 67 of file PyStoreObj.h.

Constructor & Destructor Documentation

◆ PyStoreObj() [1/3]

PyStoreObj ( const std::string &  name,
DataStore::EDurability  durability = DataStore::EDurability::c_Event 
)
explicit

constructor.

Parameters
nameName of the entry to be accessed
durability0: event, 1: persistent

Definition at line 40 of file PyStoreObj.cc.

41 :
43 TObject::Class()),
44 /* Default to TObject for unknown class for backwards compatability */
45 name,
46 durability)
47{
48}
static TClass * getTClassFromDefaultObjectName(const std::string &objectName)
Tries to deduce the TClass from a default object name, which is generally the name of the C++ class.
Definition: DataStore.cc:105

◆ PyStoreObj() [2/3]

PyStoreObj ( TClass *  objClass,
DataStore::EDurability  durability = DataStore::EDurability::c_Event 
)
explicit

constructor.

Parameters
objClassClass of the object to be accessed
durability0: event, 1: persistent

Definition at line 50 of file PyStoreObj.cc.

51 :
52 PyStoreObj(objClass, DataStore::defaultObjectName(objClass), durability)
53{
54}
static std::string defaultObjectName()
Return the default storage name for an object of the given type.
Definition: DataStore.h:127

◆ PyStoreObj() [3/3]

PyStoreObj ( TClass *  objClass,
const std::string &  name,
DataStore::EDurability  durability = DataStore::EDurability::c_Event 
)
explicit

constructor.

Parameters
objClassClass of the object to be accessed
nameName of the entry to be accessed
durability0: event, 1: persistent

Definition at line 56 of file PyStoreObj.cc.

58 :
59 m_storeAccessor(name, durability, objClass, false)
60{
61 // Attach if already created
62 attach();
63}
StoreAccessorBase m_storeAccessor
Store accessor to retrieve the object.
Definition: PyStoreObj.h:178
void attach() const
Lookup the store entry and cache a pointer to it.
Definition: PyStoreObj.cc:135

Member Function Documentation

◆ assign()

bool assign ( TObject *  object,
bool  replace = false 
)

Assign 'object' to the accessor.

(takes ownership).

Parameters
objectThe object that should be put in the DataStore, should be of same type as the one used by this accessor.
replaceShould an existing object be replaced? (if existing and supplied object are equal, this has no effect)
Returns
True if the assignment succeeded. If false, assign() will delete 'object', do not use it afterwards.

Definition at line 140 of file PyStoreObj.cc.

141{
142 return m_storeAccessor.assign(object, replace);
143}
bool assign(TObject *object, bool replace=false)
Assign 'object' to this accessor.

◆ attach()

void attach ( ) const
private

Lookup the store entry and cache a pointer to it.

Definition at line 135 of file PyStoreObj.cc.

136{
138}
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
StoreEntry * m_storeEntry
Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataS...
Definition: PyStoreObj.h:181

◆ create()

bool create ( bool  replace = false)

Create default constructed object in the DataStore.

Parameters
replaceShould an existing object be replaced?
Returns
True if the creation succeeded.

Definition at line 103 of file PyStoreObj.cc.

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}
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 create(bool replace=false)
Create a default object in the data store.

◆ ensureAttached()

void ensureAttached ( ) const
private

Ensure that contained TObject has been attached to a memory location on the DataStore.

Definition at line 125 of file PyStoreObj.cc.

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}
std::string readableName() const
Convert this acessor into a readable string (for messages).

◆ hasValidClass()

bool hasValidClass ( ) const

Check whether a TClass for the contained object could be determined.

Definition at line 97 of file PyStoreObj.cc.

98{
99 const TClass* objClass = m_storeAccessor.getClass();
100 return objClass and objClass != TObject::Class();
101}
TClass * getClass() const
The underlying object's type.

◆ isOptional()

bool isOptional ( const std::string &  name = "")

Tell the DataStore about an optional input.

Mainly useful for creating diagrams of module inputs and outputs. This must be called in the initialization phase.

Parameters
nameIf not empty, use non-default name for this object.
Returns
True if the object exists.

Definition at line 87 of file PyStoreObj.cc.

88{
89 return m_storeAccessor.isOptional(name);
90}
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.

◆ isRequired()

bool isRequired ( const std::string &  name = "")

Ensure this object has been registered previously.

Will cause an ERROR if it does not exist. This must be called in the initialization phase.

Parameters
nameIf not empty, use non-default name for this object.
Returns
True if the object exists.

Definition at line 82 of file PyStoreObj.cc.

83{
84 return m_storeAccessor.isRequired(name);
85}
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.

◆ isValid()

bool isValid ( ) const

Check whether the object was registered and created.

Definition at line 92 of file PyStoreObj.cc.

93{
94 return m_storeEntry and m_storeEntry->ptr;
95}
TObject * ptr
The pointer to the returned object, either equal to 'object' or null, depending on wether the object ...
Definition: StoreEntry.h:51

◆ list()

vector< string > list ( DataStore::EDurability  durability = DataStore::EDurability::c_Event)
static

Return list of available objects for given durability.

Definition at line 28 of file PyStoreObj.cc.

29{
30 return DataStore::Instance().getListOfObjects(TObject::Class(), durability);
31}
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

◆ obj()

TObject * obj ( ) const
inline

Returns the attached DataStore object, or nullptr if no valid object exists.

Definition at line 148 of file PyStoreObj.h.

148{ ensureAttached(); return isValid() ? m_storeEntry->ptr : nullptr; }

◆ operator bool()

operator bool ( ) const
inline

Does this PyStoreObj contain a valid datastore object?

Accessing the object's data is UNSAFE if this returns false.

Definition at line 145 of file PyStoreObj.h.

145{ return isValid(); }

◆ operator->()

TObject * operator-> ( ) const
inline

Returns the attached DataStore object, or nullptr if no valid object exists.

Definition at line 151 of file PyStoreObj.h.

151{ return obj(); }
TObject * obj() const
Returns the attached DataStore object, or nullptr if no valid object exists.
Definition: PyStoreObj.h:148

◆ printList()

void printList ( DataStore::EDurability  durability = DataStore::EDurability::c_Event)
static

Print list of available objects for given durability.

Definition at line 33 of file PyStoreObj.cc.

34{
35 for (const auto& n : list(durability))
36 B2INFO(n);
37}
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

◆ registerInDataStore() [1/2]

bool registerInDataStore ( const std::string &  name = "",
DataStore::EStoreFlags  storeFlags = DataStore::EStoreFlags::c_WriteOut 
)

Register the object in the DataStore.

This must be called in the initialization phase, and a TClass or name=class name must have been supplied during construction.

Parameters
nameName of the entry to be registered. Empty for default name.
storeFlagsORed combination of DataStore::EStoreFlags. (default: c_WriteOut)
Returns
True if the registration succeeded.

Definition at line 70 of file PyStoreObj.cc.

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}
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.

◆ registerInDataStore() [2/2]

bool registerInDataStore ( DataStore::EStoreFlags  storeFlags)

Register the object in the DataStore.

This must be called in the initialization phase.

Parameters
storeFlagsORed combination of DataStore::EStoreFlags. (default: c_WriteOut)
Returns
True if the registration succeeded.

Definition at line 65 of file PyStoreObj.cc.

66{
67 return registerInDataStore(m_storeAccessor.getName(), storeFlags);
68}
bool registerInDataStore(DataStore::EStoreFlags storeFlags)
Register the object in the DataStore.
Definition: PyStoreObj.cc:65
const std::string & getName() const
Return name under which the object is saved in the DataStore.

Member Data Documentation

◆ m_storeAccessor

StoreAccessorBase m_storeAccessor
private

Store accessor to retrieve the object.

Definition at line 178 of file PyStoreObj.h.

◆ m_storeEntry

StoreEntry* m_storeEntry = nullptr
mutableprivate

Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataStore.

Definition at line 181 of file PyStoreObj.h.


The documentation for this class was generated from the following files: