Belle II Software development
relations_consolidate.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/dataobjects/EventMetaData.h>
9#include <framework/dataobjects/ProfileInfo.h>
10#include <framework/datastore/RelationArray.h>
11#include <framework/datastore/StoreArray.h>
12
13#include <gtest/gtest.h>
14
15using namespace std;
16using namespace Belle2;
17
18namespace {
20 class RelationConsolidateTest : public ::testing::Test {
21 protected:
23 void SetUp() override
24 {
25 evtData = new StoreArray<EventMetaData>;
26 profileData = new StoreArray<ProfileInfo>;
28 evtData->registerInDataStore();
29 profileData->registerInDataStore();
30 evtData->registerRelationTo(*profileData);
32
33 for (int i = 0; i < 10; ++i) {
34 evtData->appendNew();
35 profileData->appendNew();
36 }
37
38 relArray = new RelationArray(*evtData, *profileData);
39 relArray->add(0, 0, 1.0);
40 relArray->add(0, 1, 2.0);
41 relArray->add(0, 1, 3.0);
42 relArray->add(1, 0, 1.0);
43 relArray->add(1, 1, 2.0);
44 relArray->add(1, 2, 3.0);
45 }
46
48 void TearDown() override
49 {
51 delete evtData;
52 delete profileData;
53 delete relArray;
54 }
55
57 StoreArray<ProfileInfo>* profileData;
58 RelationArray* relArray;
59 };
60
62 TEST_F(RelationConsolidateTest, RelationConsolidateDefault)
63 {
64 RelationArray& relation = *relArray;
65 relation.consolidate();
66 ASSERT_EQ(relation.getEntries(), 2);
67 EXPECT_EQ(relation[0].getSize(), 2u);
68 EXPECT_EQ(relation[0].getWeight(0), 1.0);
69 EXPECT_EQ(relation[0].getWeight(1), 5.0);
70 EXPECT_EQ(relation[1].getSize(), 3u);
71 EXPECT_EQ(relation[1].getWeight(0), 1.0);
72 EXPECT_EQ(relation[1].getWeight(1), 2.0);
73 EXPECT_EQ(relation[1].getWeight(2), 3.0);
74
75 std::map<unsigned int, std::pair<unsigned int, bool> > replace;
76 replace[0] = std::make_pair(1, true);
77 replace[2] = std::make_pair(3, false);
78 RelationArray::ReplaceMap<> replaceMap(replace);
79 relation.consolidate(replaceMap, replaceMap);
80 ASSERT_EQ(relation.getEntries(), 1);
81 EXPECT_EQ(relation[0].getSize(), 2u);
82 EXPECT_EQ(relation[0].getWeight(0), 9.0);
83 EXPECT_EQ(relation[0].getWeight(1), 3.0);
84 EXPECT_EQ(relation[0].getFromIndex(), 1u);
85 EXPECT_EQ(relation[0].getToIndex(0), 1u);
86 EXPECT_EQ(relation[0].getToIndex(1), 3u);
87 }
88
90 TEST_F(RelationConsolidateTest, RelationConsolidateZero)
91 {
92 RelationArray& relation = *relArray;
93 std::map<unsigned int, std::pair<unsigned int, bool> > replace;
94 replace[0] = std::make_pair(1, true);
95 replace[2] = std::make_pair(3, false);
96 RelationArray::ReplaceMap<> replaceMap(replace);
97 relation.consolidate(replaceMap, replaceMap, RelationArray::c_zeroWeight);
98 ASSERT_EQ(relation.getEntries(), 1);
99 EXPECT_EQ(relation[0].getSize(), 2u);
100 EXPECT_EQ(relation[0].getWeight(0), 2.0);
101 EXPECT_EQ(relation[0].getWeight(1), 3.0);
102 EXPECT_EQ(relation[0].getFromIndex(), 1u);
103 EXPECT_EQ(relation[0].getToIndex(0), 1u);
104 EXPECT_EQ(relation[0].getToIndex(1), 3u);
105
106 //second run, check that weight of the second element changes to zero and index 3->2
107 std::map<unsigned int, std::pair<unsigned int, bool> > replace2;
108 replace2[3] = std::make_pair(2, true);
109 RelationArray::ReplaceMap<> replaceMap2(replace2);
110 relation.consolidate(replaceMap2, replaceMap2, RelationArray::c_zeroWeight);
111 ASSERT_EQ(relation.getEntries(), 1);
112 EXPECT_EQ(relation[0].getSize(), 2u);
113 EXPECT_EQ(relation[0].getWeight(0), 2.0);
114 EXPECT_EQ(relation[0].getWeight(1), 0.0);
115 EXPECT_EQ(relation[0].getFromIndex(), 1u);
116 EXPECT_EQ(relation[0].getToIndex(0), 1u);
117 EXPECT_EQ(relation[0].getToIndex(1), 2u);
118 }
119
121 TEST_F(RelationConsolidateTest, RelationConsolidateNegative)
122 {
123 RelationArray& relation = *relArray;
124 std::map<unsigned int, std::pair<unsigned int, bool> > replace;
125 replace[0] = std::make_pair(1, true);
126 replace[2] = std::make_pair(3, false);
127 RelationArray::ReplaceMap<> replaceMap(replace);
128 relation.consolidate(replaceMap, replaceMap, RelationArray::c_negativeWeight);
129 ASSERT_EQ(relation.getEntries(), 1);
130 EXPECT_EQ(relation[0].getSize(), 2u);
131 // resulting weight should be -1 + -2 + -3 + -1 + 2 = -5
132 EXPECT_EQ(relation[0].getWeight(0), -5.0);
133 EXPECT_EQ(relation[0].getWeight(1), 3.0);
134 EXPECT_EQ(relation[0].getFromIndex(), 1u);
135 EXPECT_EQ(relation[0].getToIndex(0), 1u);
136 EXPECT_EQ(relation[0].getToIndex(1), 3u);
137
138 //Second run, check that the weight for the second element goes to -3.0 but -5 should stay -5
139 std::map<unsigned int, std::pair<unsigned int, bool> > replace2;
140 replace2[1] = std::make_pair(1, true);
141 replace2[3] = std::make_pair(2, true);
142 RelationArray::ReplaceMap<> replaceMap2(replace2);
143 relation.consolidate(replaceMap2, replaceMap2, RelationArray::c_negativeWeight);
144 ASSERT_EQ(relation.getEntries(), 1);
145 EXPECT_EQ(relation[0].getSize(), 2u);
146 EXPECT_EQ(relation[0].getWeight(0), -5.0);
147 EXPECT_EQ(relation[0].getWeight(1), -3.0);
148 EXPECT_EQ(relation[0].getFromIndex(), 1u);
149 EXPECT_EQ(relation[0].getToIndex(0), 1u);
150 EXPECT_EQ(relation[0].getToIndex(1), 2u);
151 }
152
154 TEST_F(RelationConsolidateTest, RelationConsolidateDelete)
155 {
156 RelationArray& relation = *relArray;
157 std::map<unsigned int, std::pair<unsigned int, bool> > replace;
158 replace[0] = std::make_pair(1, true);
159 replace[2] = std::make_pair(3, false);
160 RelationArray::ReplaceMap<> replaceMap(replace);
161 relation.consolidate(replaceMap, replaceMap, RelationArray::c_deleteElement);
162 ASSERT_EQ(relation.getEntries(), 1);
163 EXPECT_EQ(relation[0].getSize(), 2u);
164 EXPECT_EQ(relation[0].getWeight(0), 2.0);
165 EXPECT_EQ(relation[0].getWeight(1), 3.0);
166 EXPECT_EQ(relation[0].getFromIndex(), 1u);
167 EXPECT_EQ(relation[0].getToIndex(0), 1u);
168 EXPECT_EQ(relation[0].getToIndex(1), 3u);
169
170 //No let's delete the second element
171 std::map<unsigned int, std::pair<unsigned int, bool> > replace2;
172 replace2[3] = std::make_pair(3, true);
173 RelationArray::ReplaceMap<> replaceMap2(replace2);
174 relation.consolidate(replaceMap2, replaceMap2, RelationArray::c_deleteElement);
175 ASSERT_EQ(relation.getEntries(), 1);
176 EXPECT_EQ(relation[0].getSize(), 1u);
177 EXPECT_EQ(relation[0].getWeight(0), 2.0);
178 EXPECT_EQ(relation[0].getFromIndex(), 1u);
179 EXPECT_EQ(relation[0].getToIndex(0), 1u);
180
181 //Check that we get an empty relation if we delete the last one
182 std::map<unsigned int, std::pair<unsigned int, bool> > replace3;
183 replace3[1] = std::make_pair(2, true);
184 RelationArray::ReplaceMap<> replaceMap3(replace3);
185 relation.consolidate(replaceMap3, replaceMap3, RelationArray::c_deleteElement);
186 ASSERT_EQ(relation.getEntries(), 0);
187 }
188
189} // namespace
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
void setInitializeActive(bool active)
Setter for m_initializeActive.
Definition: DataStore.cc:94
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
Definition: DataStore.cc:86
Struct to replace indices based on a map-like container.
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
void consolidate()
Consolidate Relation Elements.
@ c_negativeWeight
Flip the sign of the weight to become negative if the original element got re-attributed.
Definition: RelationArray.h:79
@ c_zeroWeight
Set the weight of the relation to 0 if the original element got re-attributed.
Definition: RelationArray.h:80
@ c_deleteElement
Delete the whole relation element if the original element got re-attributed.
Definition: RelationArray.h:81
int getEntries() const
Get the number of elements.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Abstract base class for different kinds of events.
STL namespace.