Belle II Software light-2406-ragdoll
RelationVector< T > Class Template Reference

Class for type safe access to objects that are referred to in relations. More...

#include <RelationVector.h>

Inheritance diagram for RelationVector< T >:
Collaboration diagram for RelationVector< T >:

Public Types

typedef ArrayIterator< RelationVector< T >, T > iterator
 STL-like iterator over the T objects (not T* ).
 
typedef ArrayIterator< RelationVector< T >, const T > const_iterator
 STL-like const_iterator over the T objects (not T* ).
 

Public Member Functions

 RelationVector (const RelationVectorBase &b)
 Constructor.
 
const std::vector< Belle2::RelationEntry > & relations () const
 Accessor for the relations vector.
 
size_t size () const
 Get number of relations.
 
T * object (int index) const
 Get object with index.
 
T * operator[] (int index) const
 Get object with index.
 
float weight (int index) const
 Get weight with index.
 
void remove (int index)
 Remove relation at given index.
 
void setWeight (int index, float weight)
 Set a new weight for the given relation.
 
iterator begin ()
 Return iterator to first entry.
 
iterator end ()
 Return iterator to last entry +1.
 
const_iterator begin () const
 Return const_iterator to first entry.
 
const_iterator end () const
 Return const_iterator to last entry +1.
 

Protected Member Functions

void add (const RelationVectorBase &other)
 add another list of relations.
 
void apply (int index, const std::function< void(std::vector< unsigned int > &, std::vector< float > &, size_t)> &f)
 apply function to the relation associated with the RelationEntry at given index.
 

Protected Attributes

std::string m_name
 entry name of array containing object these relations belong to.
 
int m_index { -1}
 index of object these relations belong to.
 
std::vector< RelationEntrym_relations
 The vector of relation entries.
 
std::vector< std::string > m_relationNames
 Names of associated relations.
 

Detailed Description

template<class T>
class Belle2::RelationVector< T >

Class for type safe access to objects that are referred to in relations.

Objects of this class are returned by RelationsObject functinons to provide type safe access to the objects in a vector of relations returned by the data store.

Besides accessing objects/weights directly using operator[](int) and weight(int), you can also iterate over the objects directly:

const MCParticle* particle = particles[i];
for (const CDCSimHit& simhit : particle->getRelationsTo<CDCSimHit>()) {
//do things with simhit
}
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.

If you want to modify the related objects in the loop body, you can use a non-const reference instead.

Use setWeight() to modify the weight of a single relation. Use remove() to delete a relation.

Definition at line 67 of file RelationVector.h.

Member Typedef Documentation

◆ const_iterator

STL-like const_iterator over the T objects (not T* ).

Definition at line 72 of file RelationVector.h.

◆ iterator

STL-like iterator over the T objects (not T* ).

Definition at line 70 of file RelationVector.h.

Constructor & Destructor Documentation

◆ RelationVector()

RelationVector ( const RelationVectorBase b)
inlineexplicit

Constructor.

Parameters
bNot type-safe base class containing the data

Definition at line 79 of file RelationVector.h.

RelationVectorBase()
Construct empty set.

Member Function Documentation

◆ add()

void add ( const RelationVectorBase other)
protectedinherited

add another list of relations.

(internal use)

Definition at line 59 of file RelationVector.cc.

60{
61 if ((other.m_name != m_name) or (other.m_index != m_index))
62 B2FATAL("Trying to add RelationVectorBase for " << m_name << m_index << " and " << other.m_name << other.m_index << "!");
63
64 m_relations.insert(m_relations.end(), other.m_relations.begin(), other.m_relations.end());
65 if (!other.m_relations.empty())
66 m_relationNames.insert(m_relationNames.end(), other.m_relationNames.begin(), other.m_relationNames.end());
67}
std::vector< RelationEntry > m_relations
The vector of relation entries.
int m_index
index of object these relations belong to.
std::string m_name
entry name of array containing object these relations belong to.
std::vector< std::string > m_relationNames
Names of associated relations.

◆ apply()

void apply ( int  index,
const std::function< void(std::vector< unsigned int > &, std::vector< float > &, size_t)> &  f 
)
protectedinherited

apply function to the relation associated with the RelationEntry at given index.

Definition at line 13 of file RelationVector.cc.

15{
16 const TObject* obj = m_relations.at(index).object;
17 float objweight = m_relations.at(index).weight;
18
19 for (const auto& name : m_relationNames) {
21
22 //fill fromIndex, toIndex the rght way around
23 unsigned int fromIndex, toIndex;
24 StoreEntry* entry = nullptr;
25 int otherIndex = -1;
26 if (!DataStore::Instance().findStoreEntry(obj, entry, otherIndex))
27 B2FATAL("RelationVectorBase points to object not in DataStore?");
28
29 if (m_name == rel.getFromAccessorParams().first and entry->name == rel.getToAccessorParams().first) {
30 fromIndex = m_index;
31 toIndex = otherIndex;
32 } else if (entry->name == rel.getFromAccessorParams().first and m_name == rel.getToAccessorParams().first) {
33 toIndex = m_index;
34 fromIndex = otherIndex;
35 } else {
36 continue;
37 }
38
39 //go through rel, see if we can find fromIndex and toIndex
40 for (int i = 0; i < rel.getEntries(); i++) {
41 if (rel[i].getFromIndex() == fromIndex) {
42 auto& toIndices = const_cast<std::vector<RelationElement::index_type>&>(rel[i].getToIndices());
43 auto& toWeights = const_cast<std::vector<RelationElement::weight_type>&>(rel[i].getWeights());
44 for (size_t j = 0; j < toIndices.size(); j++) {
45 //we also compare the weight here to handle duplicate relations properly
46 if (toIndices[j] == toIndex and toWeights[j] == objweight) {
47 f(toIndices, toWeights, j);
48 rel.setModified(true);
49 return;
50 }
51 }
52 }
53 }
54 }
55
56 B2FATAL("RelationVectorBase and DataStore differ!??");
57}
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:59
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:22
std::string name
Name of the entry.
Definition: StoreEntry.h:53

◆ begin() [1/2]

iterator begin ( )
inline

Return iterator to first entry.

Definition at line 141 of file RelationVector.h.

141{ return iterator(this, 0); }
ArrayIterator< RelationVector< T >, T > iterator
STL-like iterator over the T objects (not T* ).

◆ begin() [2/2]

const_iterator begin ( ) const
inline

Return const_iterator to first entry.

Definition at line 146 of file RelationVector.h.

146{ return const_iterator(this, 0); }
ArrayIterator< RelationVector< T >, const T > const_iterator
STL-like const_iterator over the T objects (not T* ).

◆ end() [1/2]

iterator end ( )
inline

Return iterator to last entry +1.

Definition at line 143 of file RelationVector.h.

143{ return iterator(this, size()); }
size_t size() const
Get number of relations.

◆ end() [2/2]

const_iterator end ( ) const
inline

Return const_iterator to last entry +1.

Definition at line 148 of file RelationVector.h.

148{ return const_iterator(this, size()); }

◆ object()

T * object ( int  index) const
inline

Get object with index.

Parameters
indexIndex of relation.
Returns
Object that the relation points to.

Definition at line 96 of file RelationVector.h.

96{return static_cast<T*>(m_relations[index].object);}

◆ operator[]()

T * operator[] ( int  index) const
inline

Get object with index.

Parameters
indexIndex of relation.
Returns
Object that the relation points to.

Definition at line 103 of file RelationVector.h.

103{return object(index);}
T * object(int index) const
Get object with index.

◆ relations()

const std::vector< Belle2::RelationEntry > & relations ( ) const
inline

Accessor for the relations vector.

Returns
Vector of RelationEntry objects.

Definition at line 85 of file RelationVector.h.

85{return m_relations;}

◆ remove()

void remove ( int  index)
inline

Remove relation at given index.

This will decrease size() by one. Iterators pointing beyond given index will be invalidated when calling this function.

Note
May be slow if done frequently.

Definition at line 119 of file RelationVector.h.

120 {
121 apply(index, [](std::vector<unsigned int>& indices, std::vector<float>& weights,
122 size_t elidx) {
123 indices.erase(indices.begin() + elidx);
124 weights.erase(weights.begin() + elidx);
125 });
126
127 m_relations.erase(m_relations.begin() + index);
128 }
void apply(int index, const std::function< void(std::vector< unsigned int > &, std::vector< float > &, size_t)> &f)
apply function to the relation associated with the RelationEntry at given index.

◆ setWeight()

void setWeight ( int  index,
float  weight 
)
inline

Set a new weight for the given relation.

Definition at line 131 of file RelationVector.h.

132 {
133 apply(index, [weight](std::vector<unsigned int>&, std::vector<float>& weights, size_t elidx) {
134 weights[elidx] = weight;
135 });
136
137 m_relations[index].weight = weight;
138 }
float weight(int index) const
Get weight with index.

◆ size()

size_t size ( ) const
inline

Get number of relations.


Definition at line 88 of file RelationVector.h.

88{ return m_relations.size();}

◆ weight()

float weight ( int  index) const
inline

Get weight with index.

Parameters
indexIndex of relation.
Returns
Weight that the relation has.

Definition at line 110 of file RelationVector.h.

110{return m_relations[index].weight;}

Member Data Documentation

◆ m_index

int m_index { -1}
protectedinherited

index of object these relations belong to.

Definition at line 41 of file RelationVector.h.

◆ m_name

std::string m_name
protectedinherited

entry name of array containing object these relations belong to.

Definition at line 40 of file RelationVector.h.

◆ m_relationNames

std::vector<std::string> m_relationNames
protectedinherited

Names of associated relations.

Definition at line 43 of file RelationVector.h.

◆ m_relations

std::vector<RelationEntry> m_relations
protectedinherited

The vector of relation entries.

Definition at line 42 of file RelationVector.h.


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