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

Class to access a DBObjPtr from Python. More...

#include <PyDBObj.h>

Inheritance diagram for PyDBObj:
Collaboration diagram for PyDBObj:

Public Member Functions

 PyDBObj (const std::string &name, bool required=true)
 Construct the object from the name of the payload.
 
 PyDBObj (const TClass *objClass, bool required=true)
 Construct the object from the type of the payload, payload name will be the class name.
 
 PyDBObj (const std::string &name, const TClass *objClass, bool required=true)
 Construct the object from the name of the payload and make sure the class if compatible with objClass.
 
const TObject * _obj () const
 Return the object. More...
 
PyObject * getIntraRunBoundariesList () const
 Get the intra-run boundaries, if any, as a python list.
 
bool isValid () const
 Check whether a valid object was obtained from the database. More...
 
bool hasChanged ()
 Check whether the object has changed since the last call to hasChanged of the accessor).
 

Private Member Functions

const std::string & getName () const
 Return name under which the object is saved in the DBStore.
 
bool isValid () const
 Check whether a valid object was obtained from the database. More...
 
 operator bool () const
 Imitate pointer functionality.
 
virtual bool operator== (const DBAccessorBase &other) const
 Check if two store accessors point to the same object/array.
 
virtual bool operator!= (const DBAccessorBase &other) const
 Check if two store accessors point to a different object/array.
 
void addCallback (std::function< void(const std::string &)> callback, bool onDestruction=false)
 Add a callback method. More...
 
void addCallback (std::function< void()> callback, bool onDestruction=false)
 Add a callback method. More...
 
template<class T >
void addCallback (T *object, void(T::*callback)())
 Add a callback method of an object. More...
 
const std::string & getGlobaltag () const
 Return the globaltag name (or testing payloads path) this object is loaded from.
 
unsigned int getRevision () const
 Return current revision of the object.
 
IntervalOfValidity getIoV () const
 Return current IoV of the object.
 
const std::string & getChecksum () const
 Get current checksum.
 
const std::string & getFilename () const
 Get the filename this object is loaded from.
 
bool isIntraRunDependent () const
 Check wether this conditions object has some intra-run dependencies.
 
const std::vector< unsigned int > getIntraRunBoundaries () const
 Get the intra-run boundaries, if any. More...
 
bool isRequired () const
 Check whether this conditions object is required (at least one user declared it as required)
 
template<class T = TObject>
const T * getObject () const
 Return a pointer to the Object already cast to the correct type.
 
bool ensureAttached () const
 Make sure we are attached to the the DBStore. More...
 
void storeEntryChanged (bool destructed)
 Callback function which gets called by the DBStoreEntry object if it changes.
 

Private Attributes

const DBStoreEntry::EPayloadType m_type
 Type of the payload.
 
const std::string m_name
 Name of the payload in the database.
 
const TClass * m_objClass
 Class of the payload if type is c_Object.
 
const bool m_isArray
 True if the payload is an array of objects.
 
const bool m_isRequired
 True if the payload is required, otherwise no errors will be raised if it cannot be found.
 
DBStoreEntrym_entry
 Pointer to the entry in the DBStore.
 
bool m_changed {false}
 Internal flag whether the object has changed since we last checked.
 
std::vector< std::pair< std::function< void(const std::string &)>, bool > > m_callbacks
 List of all registered callback functions.
 

Detailed Description

Class to access a DBObjPtr from Python.

In contrast to the C++ DBObjPtr we don't have templates but python will handle the typing dynamically.

from ROOT import Belle2
beamparams = Belle2.PyDBObj('BeamParameters')
# alternative: beamparams = Belle2.PyDBObj(Belle2.BeamParameters.Class())
# Or with name and type: beamparams = Belle2.PyDBObj("BeamParameters", Belle2.BeamParameters.Class())
print(beamparams.getMass(), beamparams.getEnergy())
# alternative: beamparams.obj().getMass()
Class to access a DBObjPtr from Python.
Definition: PyDBObj.h:50

Most of the time you can just use the PyDBObj instance like an constant 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 PyDBObj (for example isValid() or hasChanged()). In this case you need to use the obj() member to obtain a reference to the real object first as shown in the example.

Warning
Be aware that PyDBObj objects are only usable during event flow, that is in the initialize() or event() calls of a module as there is no Conditions data available otherwise.
See also
the array version PyDBArray as well as the interface classes to the DataStore, PyStoreArray and PyStoreObj

Definition at line 50 of file PyDBObj.h.

Member Function Documentation

◆ _obj()

const TObject* _obj ( ) const
inline

Return the object.

Sadly the const in the return value is lost in python so this is a protected method and we add a pure python method to wrap the object in something to guarantee constness in framework/scripts/basf2.py

Definition at line 70 of file PyDBObj.h.

70 { return isValid() ? getObject() : nullptr; }
const T * getObject() const
Return a pointer to the Object already cast to the correct type.
bool isValid() const
Check whether a valid object was obtained from the database.

◆ addCallback() [1/3]

void addCallback ( std::function< void()>  callback,
bool  onDestruction = false 
)
inlineinherited

Add a callback method.

The given method will be called whenever there is a new database entry for this DBAccessor.

Parameters
callbackfunction pointer to a callback function
onDestructionif true the callback will not be called for each change but only when the Database entry is deleted and can be used to remove dangling pointers to the entry.

Definition at line 118 of file DBAccessorBase.h.

◆ addCallback() [2/3]

void addCallback ( std::function< void(const std::string &)>  callback,
bool  onDestruction = false 
)
inlineinherited

Add a callback method.

The given method will be called whenever there is a new database entry for this DBAccessor. The one parameter is the name of the DB entry which changed (in case the same callback method is to be used for multiple payloads.

Parameters
callbackfunction pointer to a callback function
onDestructionif true the callback will not be called for each change but only when the Database entry is deleted and can be used to remove dangling pointers to the entry.

Definition at line 105 of file DBAccessorBase.h.

◆ addCallback() [3/3]

void addCallback ( T *  object,
void(T::*)()  callback 
)
inlineinherited

Add a callback method of an object.

The given method will be called whenever there is a new database entry for this DBAccessor.

Parameters
objectThe object with the callback method.
callbackThe callback method.

Definition at line 130 of file DBAccessorBase.h.

◆ ensureAttached()

bool ensureAttached ( ) const
inlineprotectedinherited

Make sure we are attached to the the DBStore.

If not try to reconnect

Definition at line 171 of file DBAccessorBase.h.

◆ getIntraRunBoundaries()

const std::vector<unsigned int> getIntraRunBoundaries ( ) const
inlineinherited

Get the intra-run boundaries, if any.

If the intra-run dependency is by event number, the event numbers used as boundaries are returned.

Definition at line 157 of file DBAccessorBase.h.

◆ isValid() [1/2]

bool isValid ( ) const
inlineinherited

Check whether a valid object was obtained from the database.

Returns
True if the object exists.

Definition at line 72 of file DBAccessorBase.h.

◆ isValid() [2/2]

bool isValid
inline

Check whether a valid object was obtained from the database.

Returns
True if the object exists.

Definition at line 72 of file DBAccessorBase.h.


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