Belle II Software  release-08-01-10
PyStoreObj Class Reference

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

#include <PyStoreObj.h>

Collaboration diagram for PyStoreObj:

Public Member Functions

 PyStoreObj (const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor. More...
 
 PyStoreObj (TClass *objClass, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor. More...
 
 PyStoreObj (TClass *objClass, const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor. More...
 
bool registerInDataStore (DataStore::EStoreFlags storeFlags)
 Register the object in the DataStore. More...
 
bool registerInDataStore (const std::string &name="", DataStore::EStoreFlags storeFlags=DataStore::EStoreFlags::c_WriteOut)
 Register the object in the DataStore. More...
 
bool isRequired (const std::string &name="")
 Ensure this object has been registered previously. More...
 
bool isOptional (const std::string &name="")
 Tell the DataStore about an optional input. More...
 
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? More...
 
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. More...
 
bool create (bool replace=false)
 Create default constructed object in the DataStore. More...
 

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(const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
constructor.
Definition: PyStoreObj.cc:40

◆ 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.

◆ 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.

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.

◆ 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.

◆ 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.

◆ 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.

◆ 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(); }
bool isValid() const
Check whether the object was registered and created.
Definition: PyStoreObj.cc:92

◆ 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.

◆ 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.


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