Belle II Software  release-05-02-19
RelationVector.cc
1 #include <framework/datastore/RelationVector.h>
2 #include <framework/datastore/RelationArray.h>
3 
4 using namespace Belle2;
5 
6 void RelationVectorBase::apply(int index,
7  const std::function<void(std::vector<RelationElement::index_type>&, std::vector<RelationElement::weight_type>&, size_t)>& f)
8 {
9  const TObject* obj = m_relations.at(index).object;
10  float objweight = m_relations.at(index).weight;
11 
12  for (const auto& name : m_relationNames) {
14 
15  //fill fromIndex, toIndex the rght way around
16  unsigned int fromIndex, toIndex;
17  StoreEntry* entry = nullptr;
18  int otherIndex = -1;
19  if (!DataStore::Instance().findStoreEntry(obj, entry, otherIndex))
20  B2FATAL("RelationVectorBase points to object not in DataStore?");
21 
22  if (m_name == rel.getFromAccessorParams().first and entry->name == rel.getToAccessorParams().first) {
23  fromIndex = m_index;
24  toIndex = otherIndex;
25  } else if (entry->name == rel.getFromAccessorParams().first and m_name == rel.getToAccessorParams().first) {
26  toIndex = m_index;
27  fromIndex = otherIndex;
28  } else {
29  continue;
30  }
31 
32  //go through rel, see if we can find fromIndex and toIndex
33  for (int i = 0; i < rel.getEntries(); i++) {
34  if (rel[i].getFromIndex() == fromIndex) {
35  auto& toIndices = const_cast<std::vector<RelationElement::index_type>&>(rel[i].getToIndices());
36  auto& toWeights = const_cast<std::vector<RelationElement::weight_type>&>(rel[i].getWeights());
37  for (size_t j = 0; j < toIndices.size(); j++) {
38  //we also compare the weight here to handle duplicate relations properly
39  if (toIndices[j] == toIndex and toWeights[j] == objweight) {
40  f(toIndices, toWeights, j);
41  rel.setModified(true);
42  return;
43  }
44  }
45  }
46  }
47  }
48 
49  B2FATAL("RelationVectorBase and DataStore differ!??");
50 }
51 
53 {
54  if ((other.m_name != m_name) or (other.m_index != m_index))
55  B2FATAL("Trying to add RelationVectorBase for " << m_name << m_index << " and " << other.m_name << other.m_index << "!");
56 
57  m_relations.insert(m_relations.end(), other.m_relations.begin(), other.m_relations.end());
58  if (!other.m_relations.empty())
59  m_relationNames.insert(m_relationNames.end(), other.m_relationNames.begin(), other.m_relationNames.end());
60 }
Belle2::RelationArray
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:72
Belle2::RelationArray::setModified
void setModified(bool modified)
Set modified flag of underlying container.
Definition: RelationArray.h:276
Belle2::DataStore::Instance
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Belle2::RelationVectorBase::add
void add(const RelationVectorBase &other)
add another list of relations.
Definition: RelationVector.cc:52
Belle2::RelationVectorBase::m_index
int m_index
index of object these relations belong to.
Definition: RelationVector.h:51
Belle2::StoreEntry
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:15
Belle2::RelationArray::getFromAccessorParams
const AccessorParams & getFromAccessorParams() const
Return the AccessorParams the attached relation points from.
Definition: RelationArray.h:255
Belle2::RelationVectorBase::apply
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.
Definition: RelationVector.cc:6
Belle2::RelationVectorBase::m_relationNames
std::vector< std::string > m_relationNames
Names of associated relations.
Definition: RelationVector.h:53
Belle2::RelationVectorBase
base class for RelationVector<T>
Definition: RelationVector.h:34
Belle2::StoreEntry::name
std::string name
Name of the entry.
Definition: StoreEntry.h:46
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RelationArray::getToAccessorParams
const AccessorParams & getToAccessorParams() const
Return the AccessorParams the attached relation points to.
Definition: RelationArray.h:264
Belle2::RelationVectorBase::m_name
std::string m_name
entry name of array containing object these relations belong to.
Definition: RelationVector.h:50
Belle2::RelationArray::getEntries
int getEntries() const
Get the number of elements.
Definition: RelationArray.h:252
Belle2::DataStore::c_Event
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:61
Belle2::RelationVectorBase::m_relations
std::vector< RelationEntry > m_relations
The vector of relation entries.
Definition: RelationVector.h:52