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

A (simplified) python wrapper for StoreArray. More...

#include <PyStoreArray.h>

Collaboration diagram for PyStoreArray:

Public Member Functions

 PyStoreArray (const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor. More...
 
 PyStoreArray (TClass *objClass, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor. More...
 
 PyStoreArray (TClass *objClass, const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor. More...
 
bool registerInDataStore (DataStore::EStoreFlags storeFlags)
 Register the array in the data store. More...
 
bool registerInDataStore (const std::string &name="", DataStore::EStoreFlags storeFlags=DataStore::EStoreFlags::c_WriteOut)
 Register the array in the data store. More...
 
bool isRequired (const std::string &name="")
 Ensure this array has been registered previously. More...
 
bool isOptional (const std::string &name="")
 Tell the DataStore about an optional input. More...
 
bool registerRelationTo (const PyStoreArray &toArray, DataStore::EDurability durability=DataStore::EDurability::c_Event, DataStore::EStoreFlags storeFlags=DataStore::EStoreFlags::c_WriteOut, std::string const &namedRelation="") const
 Register a relation to the given PyStoreArray. More...
 
bool requireRelationTo (const PyStoreArray &toArray, DataStore::EDurability durability=DataStore::c_Event, std::string const &namedRelation="") const
 Produce error if no relation from this array to 'toArray' has been registered. More...
 
bool optionalRelationTo (const PyStoreArray &toArray, DataStore::EDurability durability=DataStore::c_Event, std::string const &namedRelation="") const
 Tell the data store about a relation that we could make use of. More...
 
bool hasRelationTo (const PyStoreArray &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
 Check for the existence of a relation to the provided toArray (from this Pystorearray) More...
 
bool hasRelationFrom (const PyStoreArray &fromArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
 Check for the existence of a relation from the provided toArray (to this Pystorearray) More...
 
std::string getName () const
 Return name under which the object is saved in the DataStore.
 
bool hasValidClass () const
 Check whether a TClass of the objects in this PyStoreArray could be determined.
 
bool isValid () const
 Check whether the array was registered and created.
 
 operator bool () const
 Does this PyStoreArray contain a valid datastore array?

 
TObject * operator[] (int i) const
 returns object at index i, or null pointer if out of range (+error)
 
int getEntries () const
 returns number of entries for current event.
 
int __len__ () const
 Support for len().
 
TIter __iter__ () const
 Allow iteration using for in Python.
 
TObject * appendNew ()
 Construct a new object of the array's type at the end of the array. More...
 
TClonesArray * getPtr ()
 Raw access to the underlying TClonesArray. More...
 

Static Public Member Functions

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

Private Member Functions

void ensureCreated ()
 Ensure that contained TClonesArray has been created on the DataStore.
 
bool create (bool replace=false)
 Create constructed TClonesArray in the DataStore. More...
 
void ensureAttached () const
 Ensure that contained TClonesArray 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 StoreArray.

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

Relations can also be accessed using functions like RelationsObject::getRelationsTo() as long as the data objects are derived from RelationsObject. A full example for accessing simhits and some related objects can be found in framework/examples/cdcplotmodule.py Additional registration and filling of PyStoreArrays is demonstrated in framework/examples/register_pystorearray.py

Example:

from ROOT import Belle2
simhits = Belle2.PyStoreArray('PXDSimHits')
# Alternative: simhits = Belle2.PyStoreArray(Belle2.PXDSimHits.Class())
for hit in simhits:
part = hit.getRelatedFrom('MCParticles')
print("Edep: ", str(hit.getEnergyDep()))
print("Particle: ", str(part.getPDG()))
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72

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

In case you want to access a named relation (for example, from "Tracks" to "KLMClusters" with a named relation "Secondary"), you can follow the following example:

from ROOT import Belle2
tracks = Belle2.PyStoreArray('Tracks')
for tracks in tracks:
clusters = track.getRelationsTo['KLMCluster']('KLMClusters', 'Secondary')
for cluster in clusters:
# Do something

The synthax is: ‘getRelationsTo['ClassName’]('StoreArrayName', 'RelationName') whereStoreArrayName` can be omitted in case of default store arrays.

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

Definition at line 72 of file PyStoreArray.h.

Constructor & Destructor Documentation

◆ PyStoreArray() [1/3]

PyStoreArray ( 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 PyStoreArray.cc.

41  :
43  TObject::Class()),
44  /* Default to TObject for unknown class for backwards compatability */
45  name,
46  durability)
47 {
48 }
static TClass * getTClassFromDefaultArrayName(const std::string &arrayName)
Tries to deduce the TClass from a default array name, which is generally the name of the C++ class wi...
Definition: DataStore.cc:116
PyStoreArray(const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
constructor.
Definition: PyStoreArray.cc:40

◆ PyStoreArray() [2/3]

PyStoreArray ( 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 PyStoreArray.cc.

◆ PyStoreArray() [3/3]

PyStoreArray ( 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 PyStoreArray.cc.

Member Function Documentation

◆ appendNew()

TObject * appendNew ( )

Construct a new object of the array's type at the end of the array.

Returns
the created object, to be modified by the user

Definition at line 178 of file PyStoreArray.cc.

◆ create()

bool create ( bool  replace = false)
private

Create constructed TClonesArray in the DataStore.

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

Definition at line 200 of file PyStoreArray.cc.

◆ getPtr()

TClonesArray * getPtr ( )

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 functions like operator[], getEntries() or appendNew() instead.

Definition at line 189 of file PyStoreArray.cc.

◆ hasRelationFrom()

bool hasRelationFrom ( const PyStoreArray fromArray,
DataStore::EDurability  durability = DataStore::c_Event,
const std::string &  namedRelation = "" 
) const

Check for the existence of a relation from the provided toArray (to this Pystorearray)

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 134 of file PyStoreArray.cc.

◆ hasRelationTo()

bool hasRelationTo ( const PyStoreArray toArray,
DataStore::EDurability  durability = DataStore::c_Event,
const std::string &  namedRelation = "" 
) const

Check for the existence of a relation to the provided toArray (from this Pystorearray)

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 124 of file PyStoreArray.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 array.
Returns
True if the array exists.

Definition at line 87 of file PyStoreArray.cc.

◆ isRequired()

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

Ensure this array 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 array.
Returns
True if the array exists.

Definition at line 82 of file PyStoreArray.cc.

◆ optionalRelationTo()

bool optionalRelationTo ( const PyStoreArray toArray,
DataStore::EDurability  durability = DataStore::c_Event,
std::string const &  namedRelation = "" 
) const

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. This must be called in the initialization phase.

Parameters
toArrayArray the relation should point to (from this PyStoreArray)
durabilityDurability of the relation.
namedRelationName of the relation in case it's not the default name
Returns
True if the relations exists.

Definition at line 114 of file PyStoreArray.cc.

◆ registerInDataStore() [1/2]

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

Register the array in the data store.

This must be called in the initialization phase.

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 69 of file PyStoreArray.cc.

◆ registerInDataStore() [2/2]

bool registerInDataStore ( DataStore::EStoreFlags  storeFlags)

Register the array in the data store.

This must be called in the initialization phase.

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

Definition at line 64 of file PyStoreArray.cc.

◆ registerRelationTo()

bool registerRelationTo ( const PyStoreArray toArray,
DataStore::EDurability  durability = DataStore::EDurability::c_Event,
DataStore::EStoreFlags  storeFlags = DataStore::EStoreFlags::c_WriteOut,
std::string const &  namedRelation = "" 
) const

Register a relation to the given PyStoreArray.

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 PyStoreArray)
durabilityDurability of the relation.
storeFlagsORed combination of DataStore::EStoreFlags
namedRelationAdditional name for the relation, or "" for the default naming

Definition at line 92 of file PyStoreArray.cc.

◆ requireRelationTo()

bool requireRelationTo ( const PyStoreArray toArray,
DataStore::EDurability  durability = DataStore::c_Event,
std::string const &  namedRelation = "" 
) const

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 PyStoreArray)
durabilityDurability of the relation.
namedRelationName of the relation in case it's not the default name
Returns
True if the relations exists.

Definition at line 104 of file PyStoreArray.cc.


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