Belle II Software light-2406-ragdoll
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.
 
 PyStoreArray (TClass *objClass, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor.
 
 PyStoreArray (TClass *objClass, const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
 constructor.
 
bool registerInDataStore (DataStore::EStoreFlags storeFlags)
 Register the array in the data store.
 
bool registerInDataStore (const std::string &name="", DataStore::EStoreFlags storeFlags=DataStore::EStoreFlags::c_WriteOut)
 Register the array in the data store.
 
bool isRequired (const std::string &name="")
 Ensure this array has been registered previously.
 
bool isOptional (const std::string &name="")
 Tell the DataStore about an optional input.
 
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.
 
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.
 
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.
 
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)
 
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)
 
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.
 
TClonesArray * getPtr ()
 Raw access to the underlying TClonesArray.
 

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.
 
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() [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.

51 :
52 PyStoreArray(objClass, DataStore::defaultArrayName(objClass), durability)
53{
54}
static std::string defaultArrayName()
Return the default storage name for an array of the given type.
Definition: DataStore.h:157

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

58 :
59 m_storeAccessor(name, durability, objClass, true)
60{
61 attach();
62}
StoreAccessorBase m_storeAccessor
Store accessor to retrieve the object.
Definition: PyStoreArray.h:262
void attach() const
Lookup the store entry and cache a pointer to it.

Member Function Documentation

◆ __iter__()

TIter __iter__ ( ) const

Allow iteration using for in Python.

Definition at line 171 of file PyStoreArray.cc.

172{
174 // will create empty iterator if nullptr
175 return TIter(isValid() ? m_storeEntry->getPtrAsArray() : nullptr);
176}
void ensureAttached() const
Ensure that contained TClonesArray has been attached to a memory location on the DataStore.
bool isValid() const
Check whether the array was registered and created.
StoreEntry * m_storeEntry
Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataS...
Definition: PyStoreArray.h:265
TClonesArray * getPtrAsArray() const
Return ptr cast to TClonesArray.
Definition: StoreEntry.cc:83

◆ __len__()

int __len__ ( ) const
inline

Support for len().

Definition at line 222 of file PyStoreArray.h.

222{ return getEntries(); }
int getEntries() const
returns number of entries for current event.

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

179{
181 if (isValid()) {
182 return getPtr()->ConstructedAt(getEntries());
183 } else {
184 B2ERROR("Cannot create an object in invalid PyStoreArray.");
185 return nullptr;
186 }
187}
void ensureCreated()
Ensure that contained TClonesArray has been created on the DataStore.
TClonesArray * getPtr()
Raw access to the underlying TClonesArray.

◆ attach()

void attach ( ) const
private

Lookup the store entry and cache a pointer to it.

Definition at line 232 of file PyStoreArray.cc.

233{
235}
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
StoreEntry * getEntry(const StoreAccessorBase &accessor)
Check whether an entry with the correct type is registered in the DataStore map and return it.
Definition: DataStore.cc:294

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

201{
203 if (not m_storeEntry) {
204 // Attaching failed
205 B2ERROR("Cannot create unregistered PyStoreArray.");
206 return false;
207 }
208
209 // Short cut when an array has been created and no replacement is requested.
210 if (isValid() and not replace) return true;
211
212 if (not isValid() and not hasValidClass()) {
213 B2ERROR("Cannot create PyStoreArray with unknown TClass.");
214 return false;
215 } else {
216 // Array has been created before or has a valid class
217 // Go ahead and (re)create it
218 return m_storeAccessor.create(replace);
219 }
220}
bool hasValidClass() const
Check whether a TClass of the objects in this PyStoreArray could be determined.
bool create(bool replace=false)
Create a default object in the data store.

◆ ensureAttached()

void ensureAttached ( ) const
private

Ensure that contained TClonesArray has been attached to a memory location on the DataStore.

Definition at line 222 of file PyStoreArray.cc.

223{
224 if (not m_storeEntry) {
225 attach();
226 if (not m_storeEntry) {
227 B2ERROR("PyStoreArray " << m_storeAccessor.readableName() << " has not been registered!");
228 }
229 }
230}
std::string readableName() const
Convert this acessor into a readable string (for messages).

◆ ensureCreated()

void ensureCreated ( )
private

Ensure that contained TClonesArray has been created on the DataStore.

Definition at line 195 of file PyStoreArray.cc.

196{
197 create(false);
198}
bool create(bool replace=false)
Create constructed TClonesArray in the DataStore.

◆ getEntries()

int getEntries ( ) const

returns number of entries for current event.

Definition at line 165 of file PyStoreArray.cc.

166{
168 return isValid() ? (m_storeEntry->getPtrAsArray()->GetEntriesFast()) : 0;
169}

◆ getName()

std::string getName ( ) const
inline

Return name under which the object is saved in the DataStore.

Definition at line 203 of file PyStoreArray.h.

203{ return m_storeAccessor.getName(); }
const std::string & getName() const
Return name under which the object is saved in the DataStore.

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

190{
192 return isValid() ? m_storeEntry->getPtrAsArray() : nullptr;
193}

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

137{
139 this->m_storeAccessor,
140 durability,
141 namedRelation);
142}
bool hasRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, const std::string &namedRelation)
Check for the existence of a relation in the DataStore map.
Definition: DataStore.cc:271

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

127{
129 toArray.m_storeAccessor,
130 durability,
131 namedRelation);
132}

◆ hasValidClass()

bool hasValidClass ( ) const

Check whether a TClass of the objects in this PyStoreArray could be determined.

Definition at line 144 of file PyStoreArray.cc.

145{
146 const TClass* objClass = m_storeAccessor.getClass();
147 return objClass and objClass != TObject::Class();
148}
TClass * getClass() const
The underlying object's type.

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

88{
89 return m_storeAccessor.isOptional(name);
90}
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.

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

83{
84 return m_storeAccessor.isRequired(name);
85}
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.

◆ isValid()

bool isValid ( ) const

Check whether the array was registered and created.

Definition at line 150 of file PyStoreArray.cc.

151{
152 return m_storeEntry and m_storeEntry->ptr;
153}
TObject * ptr
The pointer to the returned object, either equal to 'object' or null, depending on wether the object ...
Definition: StoreEntry.h:51

◆ list()

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

Return list of available arrays for given durability.

Definition at line 28 of file PyStoreArray.cc.

29{
30 return DataStore::Instance().getListOfArrays(TObject::Class(),
31 durability);
32}
std::vector< std::string > getListOfArrays(const TClass *arrayClass, EDurability durability) const
Returns a list of names of arrays which are of type (or inherit from) arrayClass.
Definition: DataStore.cc:666

◆ operator bool()

operator bool ( ) const
inline

Does this PyStoreArray contain a valid datastore array?

Definition at line 212 of file PyStoreArray.h.

212{ return isValid(); }

◆ operator[]()

TObject * operator[] ( int  i) const

returns object at index i, or null pointer if out of range (+error)

Definition at line 155 of file PyStoreArray.cc.

156{
158 if (not isValid()) {
159 return nullptr;
160 } else {
161 return m_storeEntry->getPtrAsArray()->At(i);
162 }
163}

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

117{
119 toArray.m_storeAccessor,
120 durability,
121 namedRelation);
122}
bool requireRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, std::string const &namedRelation)
Produce ERROR message if no relation of given durability exists between fromArray and toArray (in tha...
Definition: DataStore.cc:749

◆ printList()

void printList ( DataStore::EDurability  durability = DataStore::EDurability::c_Event)
static

Print list of available arrays for given durability.

Definition at line 34 of file PyStoreArray.cc.

35{
36 for (const auto& n : list(durability))
37 B2INFO(n);
38}
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available arrays for given durability.
Definition: PyStoreArray.cc:28

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

71{
72 if (not hasValidClass()) {
73 B2ERROR("Cannot register PyStoreArray '" << name << "' with unknown TClass. Please supply one to the PyStoreArray constructor.");
74 return false;
75 }
76
77 bool success = m_storeAccessor.registerInDataStore(name, storeFlags);
78 if (success) attach();
79 return success;
80}
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.

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

65{
66 return registerInDataStore(m_storeAccessor.getName(), storeFlags);
67}
bool registerInDataStore(DataStore::EStoreFlags storeFlags)
Register the array in the data store.
Definition: PyStoreArray.cc:64

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

96{
98 toArray.m_storeAccessor,
99 durability,
100 storeFlags,
101 namedRelation);
102}
bool registerRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, EStoreFlags storeFlags, const std::string &namedRelation)
Register a relation in the DataStore map.
Definition: DataStore.cc:244

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

107{
109 toArray.m_storeAccessor,
110 durability,
111 namedRelation);
112}

Member Data Documentation

◆ m_storeAccessor

StoreAccessorBase m_storeAccessor
private

Store accessor to retrieve the object.

Definition at line 262 of file PyStoreArray.h.

◆ m_storeEntry

StoreEntry* m_storeEntry = nullptr
mutableprivate

Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataStore.

Definition at line 265 of file PyStoreArray.h.


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