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

Low-level class to create/modify relations between StoreArrays. More...

#include <RelationArray.h>

Inheritance diagram for RelationArray:
Collaboration diagram for RelationArray:

Classes

struct  Identity
 Struct for identity transformation on indices. More...
 
class  ReplaceMap
 Struct to replace indices based on a map-like container. More...
 
class  ReplaceVec
 Struct to replace indices based on a sequential container. More...
 

Public Types

enum  EConsolidationAction {
  c_doNothing ,
  c_negativeWeight ,
  c_zeroWeight ,
  c_deleteElement
}
 Modification actions for the consolidate member. More...
 
typedef ObjArrayIterator< const TClonesArray, const RelationElementconst_iterator
 STL-like const_iterator over the T objects (not T* ).
 
typedef RelationElement::index_type index_type
 Typedef to simplify use of correct index_type.
 
typedef RelationElement::weight_type weight_type
 Typedef to simplify use of correct weight_type.
 
typedef std::pair< index_type, bool > consolidation_type
 Typedef declaring the return value of any consolidation mapping. More...
 

Public Member Functions

bool create (bool replace=false)
 Create an empty relation array in the data store. More...
 
template<class FROM , class TO >
 RelationArray (const StoreArray< FROM > &from, const StoreArray< TO > &to, const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
 Constructor which takes both store arrays and performs some sanity checks on the relation. More...
 
 RelationArray (const AccessorParams &fromAccessor, const AccessorParams &toAccessor, DataStore::EDurability durability=DataStore::c_Event)
 Constructor with AccessorParams for from- and to-side. More...
 
 RelationArray (const std::string &name, DataStore::EDurability durability=DataStore::c_Event)
 Constructor which only accepts name and durability of the relation. More...
 
 ~RelationArray ()
 Empty destructor.
 
bool isValid () const
 Check whether the object was created. More...
 
 operator bool () const
 Check whether the object was created.
 
const RelationElementoperator[] (int i) const
 Imitate array functionality.
 
int getEntries () const
 Get the number of elements.
 
const AccessorParamsgetFromAccessorParams () const
 Return the AccessorParams the attached relation points from.
 
const AccessorParamsgetToAccessorParams () const
 Return the AccessorParams the attached relation points to.
 
bool getModified () const
 Get modified flag of underlying container.
 
void setModified (bool modified)
 Set modified flag of underlying container.
 
void clear () override
 Clear all elements from the relation.
 
void add (index_type from, index_type to, weight_type weight=1.0)
 Add a new element to the relation. More...
 
void add (index_type from, const std::vector< index_type > &to, weight_type weight=1.0)
 Add a new element to the relation. More...
 
void add (index_type from, const std::vector< index_type > &to, const std::vector< weight_type > &weights)
 Add a new element to the relation. More...
 
template<class InputIterator >
void add (index_type from, const InputIterator &begin, const InputIterator &end)
 Add a new element to the relation. More...
 
void consolidate ()
 Consolidate Relation Elements. More...
 
template<class FunctionFrom , class FunctionTo >
void consolidate (const FunctionFrom &replaceFrom=FunctionFrom(), const FunctionTo &replaceTo=FunctionTo(), EConsolidationAction action=c_doNothing)
 Consolidate RelationElements. More...
 
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...
 

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

 RelationArray (const AccessorParams &params)
 Constructor which accepts the AccessorParams of the relation. More...
 
RelationElementnext ()
 Return address where the next RelationElement should be created.
 
void checkRelation (const std::string &direction, const AccessorParams &array, const AccessorParams &rel) const
 Check that the AccessorParams stored in the relation and the one given to the constructor are the same.
 
void ensureAttached () const
 Attach to relation, if necessary.
 
void assertValid () const
 check that pointer exits, otherwise bail out.
 
void assertCreated ()
 Create relation, if necessary. More...
 

Private Attributes

AccessorParams m_accessorFrom
 Accessor params for from array.
 
AccessorParams m_accessorTo
 Accessor params for to array.
 
RelationContainer ** m_relations
 Pointer that actually holds the relations.
 

Friends

template<class FROM , class TO >
class RelationIndex
 
template<class FROM , class TO >
class RelationIndexContainer
 

Detailed Description

Low-level class to create/modify relations between StoreArrays.

Relations connect objects stored in two StoreArrays with each other, with the possibility of n:n connections and individual weights.

Note
In almost all cases, you'll want to create/find relations using RelationsObject instead.

Creating new relations

Assuming you have two StoreArrays called 'particles' and 'cdcsimhits', you can use RelationArray to create relations between entries:

RelationArray particlesToCdchits(particles, cdcsimhits);
for(int iPart = 0; iPart < particles.getEntries(); iPart++) {
//... create new hit 'myhit'
cdcsimhits.appendNew(myhit);
int cdcsimhitIdx = cdcsimhits.getEntries()-1; //index of last object stored
//connect objects at indices iPart and cdcsimhitIdx
particlesToCdchits.add(iPart, cdcsimhitIdx);
}
RelationArray(const StoreArray< FROM > &from, const StoreArray< TO > &to, const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
Constructor which takes both store arrays and performs some sanity checks on the relation.

This example loops over the 'particles' array and might for example simulate the particles' interaction with the detector. New hits are added and also connected with the particles that created them.

As with other data store objects, you should register relations you want to store in your implementation of Module::initialize(), see StoreArray::registerRelationTo()

See also
RelationsObject for the main user interface to relations.
RelationIndex provides an low-levvel interface to quickly find objects related to a given FROM/TO side object.
The on-disk data structure is provided by RelationElement objects in a RelationContainer.

Definition at line 62 of file RelationArray.h.

Member Typedef Documentation

◆ consolidation_type

typedef std::pair<index_type, bool> consolidation_type

Typedef declaring the return value of any consolidation mapping.

It contains the new index of the element and a bool which is true if the old element has been re-attributed and false if there was just an reordering.

Definition at line 88 of file RelationArray.h.

Member Enumeration Documentation

◆ EConsolidationAction

Modification actions for the consolidate member.

Determines what is to be done with elements where the original element was re-attributed and not just reordered

Enumerator
c_doNothing 

Do nothing, just treat it as reordering.

c_negativeWeight 

Flip the sign of the weight to become negative if the original element got re-attributed.

c_zeroWeight 

Set the weight of the relation to 0 if the original element got re-attributed.

c_deleteElement 

Delete the whole relation element if the original element got re-attributed.

Definition at line 77 of file RelationArray.h.

Constructor & Destructor Documentation

◆ RelationArray() [1/4]

RelationArray ( const StoreArray< FROM > &  from,
const StoreArray< TO > &  to,
const std::string &  name = "",
DataStore::EDurability  durability = DataStore::c_Event 
)
inline

Constructor which takes both store arrays and performs some sanity checks on the relation.

If the relation already exists it will be checked that the relation really relates between the given StoreArrays

Parameters
fromStoreArray the relation points from
toStoreArray the relation points to
nameName of the relation. Default is to use the default name based on the StoreArrays
durabilityDurability of the relation. If the durability is larger than the durability of the related StoreArrays, an error is raised.

Definition at line 178 of file RelationArray.h.

◆ RelationArray() [2/4]

RelationArray ( const AccessorParams fromAccessor,
const AccessorParams toAccessor,
DataStore::EDurability  durability = DataStore::c_Event 
)
inline

Constructor with AccessorParams for from- and to-side.

Parameters
fromAccessor(name, durability) of from side array
toAccessor(name, durability) of to side array
durabilityDurability of the relation. If the durability is larger than the durability of the related StoreArrays, an error is raised.

Definition at line 198 of file RelationArray.h.

◆ RelationArray() [3/4]

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

Constructor which only accepts name and durability of the relation.

This constructor will only assign existing relations and will not create new ones since it lacks the information on the StoreArrays to relate

Parameters
nameName of the (existing) Relation
durabilityDurability of the (existing) Relation

Definition at line 218 of file RelationArray.h.

◆ RelationArray() [4/4]

RelationArray ( const AccessorParams params)
inlineexplicitprivate

Constructor which accepts the AccessorParams of the relation.

This constructor will only assign existing relations and will not create new ones since it lacks the information on the StoreArrays to relate

Parameters
paramsAccessorParams for the (existing) Relation

Definition at line 381 of file RelationArray.h.

Member Function Documentation

◆ add() [1/4]

void add ( index_type  from,
const InputIterator &  begin,
const InputIterator &  end 
)
inline

Add a new element to the relation.

Parameters
fromindex to point from
beginiterator pointing to the begin of a sequence of std::pair<index_type,weight_type> or compatible
enditerator pointing to the end of a sequence of std::pair<index_type,weight_type> or compatible

Definition at line 320 of file RelationArray.h.

◆ add() [2/4]

void add ( index_type  from,
const std::vector< index_type > &  to,
const std::vector< weight_type > &  weights 
)
inline

Add a new element to the relation.

Parameters
fromindex to point from
toindices to point to
weightsweights of the relations

Definition at line 306 of file RelationArray.h.

◆ add() [3/4]

void add ( index_type  from,
const std::vector< index_type > &  to,
weight_type  weight = 1.0 
)
inline

Add a new element to the relation.

Parameters
fromindex to point from
toindices to point to
weightweight for all relations

Definition at line 293 of file RelationArray.h.

◆ add() [4/4]

void add ( index_type  from,
index_type  to,
weight_type  weight = 1.0 
)
inline

Add a new element to the relation.

Parameters
fromindex to point from
toindex to point to
weightweight of the relation

Definition at line 281 of file RelationArray.h.

◆ assertCreated()

void assertCreated ( )
inlineprivate

Create relation, if necessary.

After this function returns, the relation is guaranteed to be writeable.

Definition at line 442 of file RelationArray.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.

◆ consolidate()

void consolidate ( )
inline

Consolidate Relation Elements.

This function will loop over the relation and "compress" it by merging all elements with the same fromIndex.

Definition at line 331 of file RelationArray.h.

◆ create()

bool create ( bool  replace = false)
inline

Create an empty relation array in the data store.

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

Definition at line 153 of file RelationArray.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.

◆ 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 whether the object was created.

Returns
True if the object exists.

Definition at line 234 of file RelationArray.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.

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


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