Belle II Software  release-05-01-25
StoreObjPtr< T > Class Template Reference

Type-safe access to single objects in the data store. More...

#include <StoreObjPtr.h>

Public Member Functions

 StoreObjPtr (const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
 Constructor to access an object in the DataStore. More...
 
bool isValid () const
 Check whether the object was created. More...
 
template<class ... Args>
bool construct (Args &&... params)
 Construct an object of type T in this StoreObjPtr, using the provided constructor arguments. More...
 
template<class ... Args>
bool constructAndReplace (Args &&... params)
 Construct an object of type T in this StoreObjPtr, using the provided constructor arguments. More...
 

Static Public Member Functions

static std::vector< std::string > getObjectList (DataStore::EDurability durability=DataStore::c_Event)
 Return list of object names with matching type. More...
 

Private Member Functions

void ensureAttached () const
 Ensure that this object is attached.
 
void ensureValid () const
 if accesses to this object would crash, throw an std::runtime_error
 

Private Attributes

TObject ** m_storeObjPtr
 Store of actual pointer. More...
 

Detailed Description

template<class T>
class Belle2::StoreObjPtr< T >

Type-safe access to single objects in the data store.

This class provides access to single (i.e. non-array) objects in the data store, identified by their name and durability.

Accessing existing objects

This example creates a new StoreObjPtr for the EventMetaData object, using the default name (EventMetaData) and default durability (event). If no object 'EventMetaData' is found in the data store, the store object pointer is invalid (accesses to it will cause an exception).

StoreObjPtr<EventMetaData> eventmetadata;
if(!eventmetadata) {
B2INFO("an object called '" << eventmetadata.getName() << "' does not exist in the data store.");
} else {
//object exists, you can now access its data
B2INFO("we're currently in event " << eventmetadata->getEvent() << "!");
}

Storing objects

First, objects have to be registered in the data store during the initialization phase, meaning in the initialize method of a module:

void MyModule::initialize() {
//register a single cdchit
StoreObjPtr<CDCHit> cdchit;
cdchit.registerInDataStore();
//register a single cdchit under the name "AnotherHit" and do not write
//it to the output file by default
StoreObjPtr<CDCHit> cdchit2;
cdchit2.registerInDataStore("AnotherHit", DataStore::c_DontWriteOut);
}

Before objects can be accessed they have to be created (in each event if the durability is c_Event):

//store a single cdchit
StoreObjPtr<CDCHit> cdchit;
cdchit.create(); // or construct() if you want to specify constructor arguments
cdchit->setCharge(5.0);

To put an existing object in the data store, use the assign method:

//store a single cdchit
CDCHit* cdchit = new CDCHit;
StoreObjPtr<CDCHit> cdchitPtr;
cdchitPtr.assign(cdchit);
cdchitPtr->setCharge(5.0);

Note that the datastore takes ownership of the object!

Using StoreOjbPtr as a module member variable

To avoid some overhead involved in re-creating the StoreObjPtr e.g. in each event() function call, you can also make StoreObjPtr a member variable of your class. If it is of non-event durability, you'll need to add the appropriate constructor call to the initializer list, e.g. (here with default name):

MyModule::MyModule():
m_fooPtr("", DataStore::c_Persistent)
{}

In initialize(), you should also use registerInDataStore() or isOptional()/isRequired() to specify wether it is an input or output. For non-default names (which you might not know in the constructor, e.g. in the case of module parameters), set the 'name' argument of any of these three functions to permanently bind the StoreObjPtr to the array with the given name.

See also
If you want to store more than a single object of one type, use the StoreArray class.
Data can also be created/accessed from Python modules using PyStoreObj

Definition at line 33 of file ParticleList.h.

Constructor & Destructor Documentation

◆ StoreObjPtr()

StoreObjPtr ( const std::string &  name = "",
DataStore::EDurability  durability = DataStore::c_Event 
)
inlineexplicit

Constructor to access an object in the DataStore.

Parameters
nameName under which the object is stored in the DataStore. If an empty string is supplied, the type name will be used.
durabilityDecides durability map used for getting the accessed object.

Definition at line 113 of file StoreObjPtr.h.

121  {

Member Function Documentation

◆ construct()

bool construct ( Args &&...  params)
inline

Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.

If this StoreObjPtr already contains an object, this function will fail.

Returns
True if the creation succeeded.

Definition at line 128 of file StoreObjPtr.h.

◆ constructAndReplace()

bool constructAndReplace ( Args &&...  params)
inline

Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.

If this StoreObjPtr already contains an object, it will be replaced.

Returns
True if the creation succeeded.

Definition at line 140 of file StoreObjPtr.h.

◆ getObjectList()

static std::vector<std::string> getObjectList ( DataStore::EDurability  durability = DataStore::c_Event)
inlinestatic

Return list of object names with matching type.


Definition at line 153 of file StoreObjPtr.h.

◆ isValid()

bool isValid ( ) const
inline

Check whether the object was created.

Returns
True if the object exists.

Definition at line 120 of file StoreObjPtr.h.

Member Data Documentation

◆ m_storeObjPtr

TObject** m_storeObjPtr
private

Store of actual pointer.

Don't make this a T** as this might cause problems with multiple inheritance objects

Definition at line 175 of file StoreObjPtr.h.


The documentation for this class was generated from the following files:
Belle2::DataStore::c_DontWriteOut
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition: DataStore.h:73