Belle II Software  release-08-01-10
RelationIndex< FROM, TO > Class Template Reference

Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation. More...

#include <RelationIndex.h>

Collaboration diagram for RelationIndex< FROM, TO >:

Public Types

typedef RelationIndexContainer< FROM, TO >::Element Element
 Struct representing a single element in the index.
 
typedef RelationIndexContainer< FROM, TO >::ElementIndex ElementIndex
 Class representing a the index on the relation.
 
typedef ElementIndex::template nth_index< 0 >::type index_from
 Typedef for easy access to the from side of the index.
 
typedef ElementIndex::template nth_index< 1 >::type index_to
 Typedef for easy access to the to side of the index.
 
typedef index_from::const_iterator iterator_from
 Element iterator of the from side index. More...
 
typedef index_to::const_iterator iterator_to
 Element iterator of the to side index.
 
typedef boost::iterator_range< iterator_fromrange_from
 Iterator range [first,second) of the from side.
 
typedef boost::iterator_range< iterator_torange_to
 Iterator range [first,second) of the to side.
 

Public Member Functions

 RelationIndex (const std::string &name=(DataStore::defaultRelationName< FROM, TO >()), DataStore::EDurability durability=DataStore::c_Event)
 Constructor. More...
 
 RelationIndex (const StoreArray< FROM > &from, const StoreArray< TO > &to, const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
 Constructor with checks. More...
 
 operator bool () const
 check if index is based on valid relation.
 
range_from getElementsFrom (const FROM *from) const
 Return a range of all elements pointing from the given object. More...
 
range_from getElementsFrom (const FROM &from) const
 Return a range of all elements pointing from the given object. More...
 
range_to getElementsTo (const TO *to) const
 Return a range of all elements pointing to the given object. More...
 
range_to getElementsTo (const TO &to) const
 Return a range of all elements pointing to the given object. More...
 
const ElementgetFirstElementFrom (const FROM &from) const
 Return a pointer to the first relation Element of the given object. More...
 
const ElementgetFirstElementFrom (const FROM *from) const
 Return a pointer to the first relation Element of the given object. More...
 
const ElementgetFirstElementTo (const TO &to) const
 Return a pointer to the first relation Element of the given object. More...
 
const ElementgetFirstElementTo (const TO *to) const
 Return a pointer to the first relation Element of the given object. More...
 
AccessorParams getAccessorParams () const
 Get the AccessorParams of the underlying relation.
 
const AccessorParamsgetFromAccessorParams () const
 Get the AccessorParams of the StoreArray the relation points from.
 
const AccessorParamsgetToAccessorParams () const
 Get the AccessorParams of the StoreArray the relation points to.
 
size_t size () const
 Get the size of the index.
 

Protected Attributes

const std::shared_ptr< RelationIndexContainer< FROM, TO > > m_index
 Reference to the IndexContainer.
 
const index_fromm_from
 Reference to the from index.
 
const index_tom_to
 Reference to the to index.
 

Detailed Description

template<class FROM, class TO>
class Belle2::RelationIndex< FROM, TO >

Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation.

Note
This class is used internally by RelationsObject/RelationsInterface. In most cases, you should avoid using RelationIndex directly and use these more friendly interfaces.

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

This class provides bidirectional access to a given relation to ease use of Relations for the normal user. There is no support for changing or adding Elements of the relation, this should be done directly using RelationArray.

Finding related objects

Given an object of type FROM or TO, RelationIndex can easily find objects on the other side connected to it. If there can be at most one relation from/to the given objects, this is especially simple:

RelationIndex<MCParticle, CDCSimHit> mcparticlesToCdcsimhits;
if(!mcparticlesToCdcsimhits)
B2FATAL("No MCParticle -> CDCSimHit relation found!");
for(int iCDC = 0; iCDC < cdcsimhits.getEntries(); iCDC++) {
const CDCSimHit* hit = cdcsimhits[iCDC];
//assuming 'hit' was only created by a single particle
const relElement_t* rel = mcparticlesToCdcsimhits.getFirstElementTo(hit);
if(!rel) {
B2WARNING("no MCParticle found for CDCSimHit " << iCDC);
continue;
}
B2INFO("this CDCHit came from a particle with PDG code " << rel->from->getPDG());
}
RelationIndexContainer< FROM, TO >::Element Element
Struct representing a single element in the index.
Definition: RelationIndex.h:79

If more than one associated object exists, one can loop over them using range-based for. For example, when one instead wants to find the CDCSimHits belonging to a particle, one can use:

const MCParticle* particle = ...;
for(const relElement_t & rel: mcparticlesToCdcsimhits.getElementsFrom(particle)) {
const CDCSimHit* hit = rel.to;
//...
}

The documentation of the relation element type used by getFirstElementTo()/ getFirstElementFrom() or during the for loop can be found in RelationIndexContainer<FROM, TO>::Element.

See also
See RelationArray for examples on how to create new relations between objects.

Definition at line 76 of file RelationIndex.h.

Member Typedef Documentation

◆ iterator_from

typedef index_from::const_iterator iterator_from

Element iterator of the from side index.

Note
Both iterator_from and iterator_to point to objects of type Element, but reflect the structure of the underlying index.

Definition at line 95 of file RelationIndex.h.

Constructor & Destructor Documentation

◆ RelationIndex() [1/2]

RelationIndex ( const std::string &  name = (DataStore::defaultRelationName<FROM, TO>()),
DataStore::EDurability  durability = DataStore::c_Event 
)
inlineexplicit

Constructor.

Parameters
nameName of the relation. Empty string will be replaced with the default relation name for the given types
durabilityDurabiliy of the relation

Definition at line 113 of file RelationIndex.h.

114  :
115  m_index(RelationIndexManager::Instance().get<FROM, TO>(RelationArray(name, durability))),
116  m_from(m_index->index().template get<0>()),
117  m_to(m_index->index().template get<1>()) {}
static RelationIndexManager & Instance()
Returns the singleton instance.
const std::shared_ptr< RelationIndexContainer< FROM, TO > > m_index
Reference to the IndexContainer.
const index_to & m_to
Reference to the to index.
const index_from & m_from
Reference to the from index.

◆ RelationIndex() [2/2]

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

Constructor with checks.

This constructor takes the StoreArrays as arguments to perform additional sanity checks if the relation is correct.

Parameters
fromStoreArray the relation is supposed to point from
toStoreArray the relation is supposed to point to
nameName of the relation. Empty string will be replaced with the default relation name for the given types
durabilityDurabiliy of the relation

Definition at line 131 of file RelationIndex.h.

Member Function Documentation

◆ getElementsFrom() [1/2]

range_from getElementsFrom ( const FROM &  from) const
inline

Return a range of all elements pointing from the given object.

Can be used with range-based for, see RelationIndex class documentation for an example.

Parameters
fromReference for which to get the relation
Returns
Iterator range [first,second) of elements which point from this object

Definition at line 160 of file RelationIndex.h.

◆ getElementsFrom() [2/2]

range_from getElementsFrom ( const FROM *  from) const
inline

Return a range of all elements pointing from the given object.

Can be used with range-based for, see RelationIndex class documentation for an example.

Parameters
fromPointer for which to get the relation.
Returns
Iterator range [first,second) of elements which point from this object.

Definition at line 149 of file RelationIndex.h.

◆ getElementsTo() [1/2]

range_to getElementsTo ( const TO &  to) const
inline

Return a range of all elements pointing to the given object.

Can be used with range-based for, see RelationIndex class documentation for an example.

Parameters
toReference for which to get the relation
Returns
Iterator range [first,second) of elements which point to this object

Definition at line 182 of file RelationIndex.h.

◆ getElementsTo() [2/2]

range_to getElementsTo ( const TO *  to) const
inline

Return a range of all elements pointing to the given object.

Can be used with range-based for, see RelationIndex class documentation for an example.

Parameters
toPointer for which to get the relation
Returns
Iterator range [first,second) of elements which point to this object

Definition at line 171 of file RelationIndex.h.

◆ getFirstElementFrom() [1/2]

const Element* getFirstElementFrom ( const FROM &  from) const
inline

Return a pointer to the first relation Element of the given object.

Useful if there is at most one relation

Parameters
fromReference for which to get the Relation
Returns
Pointer to the RelationIndex<FROM,TO>::Element, can be NULL if no relation exists

Definition at line 191 of file RelationIndex.h.

◆ getFirstElementFrom() [2/2]

const Element* getFirstElementFrom ( const FROM *  from) const
inline

Return a pointer to the first relation Element of the given object.

Useful if there is at most one relation

Parameters
fromPointer for which to get the Relation
Returns
Pointer to the RelationIndex<FROM,TO>::Element, can be NULL if no relation exists

Definition at line 200 of file RelationIndex.h.

◆ getFirstElementTo() [1/2]

const Element* getFirstElementTo ( const TO &  to) const
inline

Return a pointer to the first relation Element of the given object.

Useful if there is at most one relation

Parameters
toReference for which to get the Relation
Returns
Pointer to the RelationIndex<FROM,TO>::Element, can be NULL if no relation exists

Definition at line 214 of file RelationIndex.h.

◆ getFirstElementTo() [2/2]

const Element* getFirstElementTo ( const TO *  to) const
inline

Return a pointer to the first relation Element of the given object.

Useful if there is at most one relation

Parameters
toPointer for which to get the Relation
Returns
Pointer to the RelationIndex<FROM,TO>::Element, can be NULL if no relation exists

Definition at line 223 of file RelationIndex.h.


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