Belle II Software development
RelationIndex< FROM, TO > Class Template Reference

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

#include <RelationIndex.h>

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.
 
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.
 
 RelationIndex (const StoreArray< FROM > &from, const StoreArray< TO > &to, const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
 Constructor with checks.
 
 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.
 
range_from getElementsFrom (const FROM &from) const
 Return a range of all elements pointing from the given object.
 
range_to getElementsTo (const TO *to) const
 Return a range of all elements pointing to the given object.
 
range_to getElementsTo (const TO &to) const
 Return a range of all elements pointing to the given object.
 
const ElementgetFirstElementFrom (const FROM &from) const
 Return a pointer to the first relation Element of the given object.
 
const ElementgetFirstElementFrom (const FROM *from) const
 Return a pointer to the first relation Element of the given object.
 
const ElementgetFirstElementTo (const TO &to) const
 Return a pointer to the first relation Element of the given object.
 
const ElementgetFirstElementTo (const TO *to) const
 Return a pointer to the first relation Element of the given object.
 
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());
}
Example Detector.
Definition: CDCSimHit.h:21
Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation.
Definition: RelationIndex.h:76
const Element * getFirstElementTo(const TO &to) const
Return a pointer to the first relation Element of the given object.

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;
//...
}
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
range_from getElementsFrom(const FROM *from) const
Return a range of all elements pointing from the given object.

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

◆ Element

typedef RelationIndexContainer<FROM,TO>::Element Element

Struct representing a single element in the index.

Definition at line 79 of file RelationIndex.h.

◆ ElementIndex

typedef RelationIndexContainer<FROM,TO>::ElementIndex ElementIndex

Class representing a the index on the relation.

Definition at line 82 of file RelationIndex.h.

◆ index_from

typedef ElementIndex::template nth_index<0>::type index_from

Typedef for easy access to the from side of the index.

Definition at line 85 of file RelationIndex.h.

◆ index_to

typedef ElementIndex::template nth_index<1>::type index_to

Typedef for easy access to the to side of the index.

Definition at line 88 of file RelationIndex.h.

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

◆ iterator_to

typedef index_to::const_iterator iterator_to

Element iterator of the to side index.

Definition at line 98 of file RelationIndex.h.

◆ range_from

typedef boost::iterator_range<iterator_from> range_from

Iterator range [first,second) of the from side.

Definition at line 101 of file RelationIndex.h.

◆ range_to

typedef boost::iterator_range<iterator_to> range_to

Iterator range [first,second) of the to side.

Definition at line 104 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.

132 :
133 m_index(RelationIndexManager::Instance().get<FROM, TO>(RelationArray(from, to, name, durability))),
134 m_from(m_index->index().template get<0>()),
135 m_to(m_index->index().template get<1>()) {}

Member Function Documentation

◆ getAccessorParams()

AccessorParams getAccessorParams ( ) const
inline

Get the AccessorParams of the underlying relation.

Definition at line 231 of file RelationIndex.h.

231{ return m_index->getAccessorParams(); }

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

160{ return m_from.equal_range(&from); }

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

149{ return m_from.equal_range(from); }

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

182{ return m_to.equal_range(&to); }

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

171{ return m_to.equal_range(to); }

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

191{ return getFirstElementFrom(&from); }
const Element * getFirstElementFrom(const FROM &from) const
Return a pointer to the first relation Element of the given object.

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

201 {
202 iterator_from it = m_from.find(from);
203 if (it == m_from.end()) return 0;
204 return &(*it);
205 }
index_from::const_iterator iterator_from
Element iterator of the from side index.
Definition: RelationIndex.h:95

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

214{ return getFirstElementTo(&to); }

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

224 {
225 iterator_to it = m_to.find(to);
226 if (it == m_to.end()) return 0;
227 return &(*it);
228 }
index_to::const_iterator iterator_to
Element iterator of the to side index.
Definition: RelationIndex.h:98

◆ getFromAccessorParams()

const AccessorParams & getFromAccessorParams ( ) const
inline

Get the AccessorParams of the StoreArray the relation points from.

Definition at line 234 of file RelationIndex.h.

234{ return m_index->getFromAccessorParams(); }

◆ getToAccessorParams()

const AccessorParams & getToAccessorParams ( ) const
inline

Get the AccessorParams of the StoreArray the relation points to.

Definition at line 237 of file RelationIndex.h.

237{ return m_index->getToAccessorParams(); }

◆ operator bool()

operator bool ( ) const
inline

check if index is based on valid relation.

Definition at line 138 of file RelationIndex.h.

138{ return *(m_index.get()); }

◆ size()

size_t size ( ) const
inline

Get the size of the index.

Definition at line 240 of file RelationIndex.h.

240{ return m_index->index().size(); }

Member Data Documentation

◆ m_from

const index_from& m_from
protected

Reference to the from index.

Definition at line 246 of file RelationIndex.h.

◆ m_index

const std::shared_ptr<RelationIndexContainer<FROM, TO> > m_index
protected

Reference to the IndexContainer.

Definition at line 243 of file RelationIndex.h.

◆ m_to

const index_to& m_to
protected

Reference to the to index.

Definition at line 249 of file RelationIndex.h.


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