Belle II Software  release-08-01-10
StoreArray< T > Class Template Reference

Accessor to arrays stored in the data store. More...

#include <StoreArray.h>

Inheritance diagram for StoreArray< T >:
Collaboration diagram for StoreArray< T >:

Public Types

typedef ObjArrayIterator< TClonesArray, T > iterator
 STL-like iterator over the T objects (not T* ).
 
typedef ObjArrayIterator< const TClonesArray, const T > const_iterator
 STL-like const_iterator over the T objects (not T* ).
 

Public Member Functions

 StoreArray (const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
 Constructor to access an array in the DataStore. More...
 
template<class TO >
bool registerRelationTo (const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
 Register a relation to the given StoreArray. More...
 
template<class TO >
bool requireRelationTo (const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
 Produce error if no relation from this array to 'toArray' has been registered. More...
 
template<class TO >
bool optionalRelationTo (const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
 Tell the data store about a relation that we could make use of. More...
 
template<class TO >
bool hasRelationTo (const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
 Check for the existence of a relation to the given StoreArray. More...
 
template<class FROM >
bool hasRelationFrom (const StoreArray< FROM > &fromArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
 Check for the existence of a relation from the given StoreArray. More...
 
void clear () override
 Delete all entries in this array. More...
 
int getEntries () const
 Get the number of objects in the array.
 
T * operator[] (int i) const
 Access to the stored objects. More...
 
T * appendNew ()
 Construct a new T object at the end of the array. More...
 
template<class ... Args>
T * appendNew (Args &&... params)
 Construct a new T object directly at the end of the array. More...
 
bool isValid () const
 Check wether the array was registered. More...
 
 operator bool () const
 Check wether the array was registered. More...
 
TClonesArray * getPtr () const
 Raw access to the underlying TClonesArray. More...
 
iterator begin ()
 Return iterator to first entry.
 
iterator end ()
 Return iterator to last entry +1.
 
const_iterator begin () const
 Return const_iterator to first entry.
 
const_iterator end () const
 Return const_iterator to last entry +1.
 
bool registerInDataStore (DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
 Register the object/array in the DataStore. More...
 
bool registerInDataStore (const std::string &name, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
 Register the object/array in the DataStore. More...
 
bool isRequired (const std::string &name="")
 Ensure this array/object has been registered previously. More...
 
bool isOptional (const std::string &name="")
 Tell the DataStore about an optional input. More...
 
bool assign (TObject *object, bool replace=false)
 Assign 'object' to this accessor. More...
 
const std::string & getName () const
 Return name under which the object is saved in the DataStore.
 
DataStore::EDurability getDurability () const
 Return durability with which the object is saved in the DataStore.
 
AccessorParams getAccessorParams () const
 Return pair of name and durability under which stored object is saved. More...
 
virtual bool operator== (const StoreAccessorBase &other) const
 Check if two store accessors point to the same object/array.
 
virtual bool operator!= (const StoreAccessorBase &other) const
 Check if two store accessors point to a different object/array.
 
TClass * getClass () const
 The underlying object's type.
 
bool isArray () const
 Is this an accessor for an array?
 
bool notWrittenOut () const
 Returns true if this object/array should not be saved by output modules. More...
 
std::string readableName () const
 Convert this acessor into a readable string (for messages). More...
 

Static Public Member Functions

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

Protected Attributes

std::string m_name
 Store name under which this object/array is saved.
 
DataStore::EDurability m_durability
 Store durability under which the object/array is saved.
 
TClass * m_class
 The underlying object's type.
 
bool m_isArray
 Is this an accessor for an array?
 

Private Member Functions

bool create (bool replace=false)
 Creating StoreArrays is unnecessary, only used internally.
 
T * nextFreeAdress ()
 Returns address of the next free position of the array. More...
 
void ensureAttached () const
 Ensure that this object is attached.
 
void ensureCreated () const
 Ensure that the array has been created. More...
 
bool isCreated () const
 Check wether the array object was created. More...
 

Private Attributes

TClonesArray ** m_storeArray
 Pointer that actually holds the TClonesArray.
 

Detailed Description

template<class T>
class Belle2::StoreArray< T >

Accessor to arrays stored in the data store.

StoreArrays (like StoreObjPtrs) are uniquely identified by their name and durability. In most cases, arrays are created with durability DataStore::c_Event and the default name corresponding to their type. (i.e. typename + 's') Thus, calling the constructor StoreArray<MyType>() will connect this StoreArray with the array called 'MyTypes' in the data store, with a lifetime of one event.

Accessing elements of an existing array

Stored objects can be accessed directly using their array index and operator[]. For example, the following code snippet loops over all entries in an array of CDCSimHits:

StoreArray<CDCSimHit> cdcsimhits;
//loop over all CDC simhits
for(int iCDC = 0; iCDC < cdcsimhits.getEntries(); iCDC++) {
const CDCSimHit* hit = cdcsimhits[iCDC]; //get iCDC'th entry in StoreArray
// Use hit's data here...
}
//or using range-based for:
for(const CDCSimHit& hit : cdcsimhits) {
// Use hit's data here...
}
Note
Remember to use references or pointers when iterating, otherwise accessing, e.g., relations will not work.

Objects linked together using relations can also be obtained by querying the objects themselves, since they should derive from RelationsObject (See class documentation for usage examples.)

Adding elements

Elements can be added to the array in a few ways. The easiest is to use something like:

StoreArray<CDCSimHit> cdcsimhits;
//...
CDCSimHit* newhit = cdcsimhits.appendNew();
//fill newhit with data here...

appendNew() can also use non-default constructors, e.g. if there is a constructor that takes the arguments (int, float), you can use appendNew(int, float) instead.

Registration of arrays

Note that you have to register an array in the initialize method of a module first (using registerInDataStore()) before you can use it. This procedure is the same as for objects handled by StoreObjPtr.

Using StoreArray as a module member variable

To avoid some overhead involved in re-creating the StoreArray e.g. in each event() function call, you can also make StoreArray 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_fooHits("", 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 StoreArray to the array with the given name.

See also
Objects in different arrays can be linked using relations, see RelationsInterface, RelationsObject
See StoreObjPtr for a way store single objects
Data can also be accessed from Python modules using PyStoreArray
While individual elements cannot be removed directly from StoreArray, SelectSubset can be used to filter it.

Definition at line 113 of file StoreArray.h.

Constructor & Destructor Documentation

◆ StoreArray()

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

Constructor to access an array in the DataStore.

Parameters
nameName under which the array 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 array.

Definition at line 126 of file StoreArray.h.

126  :
127  StoreAccessorBase(DataStore::arrayName<T>(name), durability, T::Class(), true), m_storeArray(0) {}
StoreAccessorBase(const std::string &name, DataStore::EDurability durability, TClass *objClass, bool isArray)
Constructor to access an object or array in the DataStore.
TClonesArray ** m_storeArray
Pointer that actually holds the TClonesArray.
Definition: StoreArray.h:368

Member Function Documentation

◆ appendNew() [1/2]

T* appendNew ( )
inline

Construct a new T object at the end of the array.

Appends a new object to the array, and returns a pointer so it can be filled with data. The default constructor is used for the object's creation.

Returns
pointer to the created object

Definition at line 246 of file StoreArray.h.

◆ appendNew() [2/2]

T* appendNew ( Args &&...  params)
inline

Construct a new T object directly at the end of the array.

This is done by forwarding all arguments to the constructor of the type T. If there is a constructor which takes the given combination of arguments then this call will succeed, otherwise it fails on compilation.

This method imposes no overhead as no temporary has to be constructed and should be the preferred solution for creating new objects.

It handles just like the (C++11) standard library's emplace..() functions:

myStoreArray.appendNew(some ctor arguments);
Note
since all arguments are passed to the constructor of class T, doing something like appendNew(T(ctor arguments)) is unnecessary. This would construct a temporary object of class T, then copy-construct another T object in the array and destroy the temporary object again.
Returns
pointer to the created object

Definition at line 268 of file StoreArray.h.

◆ assign()

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

Assign 'object' to this 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 33 of file StoreAccessorBase.cc.

◆ clear()

void clear ( void  )
inlineoverridevirtual

Delete all entries in this array.

Any relations to objects in this array are also removed.

Reimplemented from StoreAccessorBase.

Definition at line 207 of file StoreArray.h.

◆ ensureCreated()

void ensureCreated ( ) const
inlineprivate

Ensure that the array has been created.

Called automatically by write operations.

Definition at line 352 of file StoreArray.h.

◆ getAccessorParams()

AccessorParams getAccessorParams ( ) const
inlineinherited

Return pair of name and durability under which stored object is saved.


Definition at line 134 of file StoreAccessorBase.h.

◆ getArrayList()

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

Return list of array names with matching type.


Definition at line 275 of file StoreArray.h.

◆ getPtr()

TClonesArray* getPtr ( ) const
inline

Raw access to the underlying TClonesArray.

Warning
TClonesArray is dangerously easy to misuse. Whatever you do will probably be slow, leak memory, and murder your pets. In most cases, you'll want to use StoreArray functions like operator[], getEntries() or appendNew() instead.
Returns
pointer to TClonesArray, or NULL if this array was not registered in the data store

Definition at line 311 of file StoreArray.h.

◆ hasRelationFrom()

bool hasRelationFrom ( const StoreArray< FROM > &  fromArray,
DataStore::EDurability  durability = DataStore::c_Event,
const std::string &  namedRelation = "" 
) const
inline

Check for the existence of a relation from the given StoreArray.

Parameters
fromArrayArray the relation should point to (from this StoreArray)
durabilityDurability of the relation.
namedRelationAdditional name for the relation, or "" for the default naming

Definition at line 197 of file StoreArray.h.

◆ hasRelationTo()

bool hasRelationTo ( const StoreArray< TO > &  toArray,
DataStore::EDurability  durability = DataStore::c_Event,
const std::string &  namedRelation = "" 
) const
inline

Check for the existence of a relation to the given StoreArray.

Parameters
toArrayArray the relation should point to (from this StoreArray)
durabilityDurability of the relation.
namedRelationAdditional name for the relation, or "" for the default naming

Definition at line 185 of file StoreArray.h.

◆ isCreated()

bool isCreated ( ) const
inlineprivate

Check wether the array object was created.


Definition at line 361 of file StoreArray.h.

◆ isOptional()

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

Tell the DataStore about an optional input.

Mainly useful for creating diagrams of module inputs and outputs.

Parameters
nameIf not empty, set non-default name for this object/array. This is permanent, so that e.g. after using registerInDataStore("myName") in initialize(), this object will continue refer to 'myName' in event().
Returns
True if the object/array exists.

Definition at line 93 of file StoreAccessorBase.h.

◆ isRequired()

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

Ensure this array/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, set non-default name for this object/array. This is permanent, so that e.g. after using registerInDataStore("myName") in initialize(), this object will continue refer to 'myName' in event().
Returns
True if the object/array exists.

Definition at line 78 of file StoreAccessorBase.h.

◆ isValid()

bool isValid ( ) const
inline

Check wether the array was registered.

Note
Iterating over the array or calling getEntries() is safe even if this returns false. (getEntries() will return 0)
Returns
True if the array was registered.

Definition at line 288 of file StoreArray.h.

◆ nextFreeAdress()

T* nextFreeAdress ( )
inlineprivate

Returns address of the next free position of the array.

Returns
pointer to address just past the last array element

Definition at line 335 of file StoreArray.h.

◆ notWrittenOut()

bool notWrittenOut ( ) const
inherited

Returns true if this object/array should not be saved by output modules.

See DataStore::c_DontWriteOut. Can be changed by re-registering it with/without the flag.

Definition at line 53 of file StoreAccessorBase.cc.

◆ operator bool()

operator bool ( ) const
inline

Check wether the array was registered.

Note
Iterating over the array or calling getEntries() is safe even if this returns false. (getEntries() will return 0)
Returns
True if the array was registered.

Definition at line 301 of file StoreArray.h.

◆ operator[]()

T* operator[] ( int  i) const
inline

Access to the stored objects.

Out-of-bounds accesses throw an std::out_of_range exception

Note that using iterators (or range-based for) avoids the range-check internally, and thus might be slightly faster.

Parameters
iArray index, should be in 0..getEntries()-1
Returns
pointer to the object

Definition at line 228 of file StoreArray.h.

◆ optionalRelationTo()

bool optionalRelationTo ( const StoreArray< TO > &  toArray,
DataStore::EDurability  durability = DataStore::c_Event,
const std::string &  namedRelation = "" 
) const
inline

Tell the data store about a relation that we could make use of.

(aka. optional input)

Mainly useful for creating diagrams of module inputs and outputs.

Parameters
toArrayArray the relation should point to (from this StoreArray)
durabilityDurability of the relation.
namedRelationAdditional name for the relation, or "" for the default naming
Returns
True if the relations exists.

Definition at line 172 of file StoreArray.h.

◆ readableName()

std::string readableName ( ) const
inherited

Convert this acessor into a readable string (for messages).

e.g. "object EventMetaData (durability: event)"

Definition at line 18 of file StoreAccessorBase.cc.

◆ registerInDataStore() [1/2]

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

Register the object/array in the DataStore.

This must be called in the initialization phase.

Parameters
nameIf not empty, set non-default name for this object/array. This is permanent, so that e.g. after using registerInDataStore("myName") in initialize(), this object will continue refer to 'myName' in event().
storeFlagsORed combination of DataStore::EStoreFlags.
Returns
True if the registration succeeded.

Definition at line 64 of file StoreAccessorBase.h.

◆ registerInDataStore() [2/2]

bool registerInDataStore ( DataStore::EStoreFlags  storeFlags = DataStore::c_WriteOut)
inlineinherited

Register the object/array in the DataStore.

This must be called in the initialization phase.

Parameters
storeFlagsORed combination of DataStore::EStoreFlags.
Returns
True if the registration succeeded.

Definition at line 52 of file StoreAccessorBase.h.

◆ registerRelationTo()

bool registerRelationTo ( const StoreArray< TO > &  toArray,
DataStore::EDurability  durability = DataStore::c_Event,
DataStore::EStoreFlags  storeFlags = DataStore::c_WriteOut,
const std::string &  namedRelation = "" 
) const
inline

Register a relation to the given StoreArray.

Use this if you want to create relate objects in this array to objects in 'toArray'. Must be called in the initialization phase.

Parameters
toArrayArray the relation should point to (from this StoreArray)
durabilityDurability of the relation.
storeFlagsORed combination of DataStore::EStoreFlags
namedRelationAdditional name for the relation, or "" for the default naming

Definition at line 140 of file StoreArray.h.

◆ requireRelationTo()

bool requireRelationTo ( const StoreArray< TO > &  toArray,
DataStore::EDurability  durability = DataStore::c_Event,
const std::string &  namedRelation = "" 
) const
inline

Produce error if no relation from this array to 'toArray' has been registered.

Must be called in initialization phase, aborts job if it fails. (allowing you to catch problems early)

Parameters
toArrayArray the relation should point to (from this StoreArray)
durabilityDurability of the relation.
namedRelationAdditional name for the relation, or "" for the default naming
Returns
True if the relations exists.

Definition at line 155 of file StoreArray.h.


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