Project
Loading...
Searching...
No Matches
test_IndexBuilder.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
13#include "../src/IndexJSONHelpers.h"
14#include <catch_amalgamated.hpp>
15
16using namespace o2::framework;
17using namespace arrow;
18using namespace o2::soa;
19using namespace o2::aod;
20
21namespace o2::aod
22{
23O2ORIGIN("TST");
24namespace coords
25{
26DECLARE_SOA_COLUMN_FULL(X, x, float, "x");
27DECLARE_SOA_COLUMN_FULL(Y, y, float, "y");
28DECLARE_SOA_COLUMN_FULL(Z, z, float, "z");
29} // namespace coords
30DECLARE_SOA_TABLE(Points, "TST", "POINTS", Index<>, coords::X, coords::Y, coords::Z);
31
32namespace extra_1
33{
35DECLARE_SOA_COLUMN_FULL(D, d, float, "d");
36} // namespace extra_1
37DECLARE_SOA_TABLE(Distances, "TST", "DISTANCES", Index<>, extra_1::PointId, extra_1::D);
38
39namespace extra_2
40{
42DECLARE_SOA_COLUMN_FULL(IsTrue, istrue, bool, "istrue");
43} // namespace extra_2
44DECLARE_SOA_TABLE(Flags, "TST", "Flags", Index<>, extra_2::PointId, extra_2::IsTrue);
45
46namespace extra_3
47{
49DECLARE_SOA_COLUMN_FULL(Category, category, int32_t, "category");
50} // namespace extra_3
51DECLARE_SOA_TABLE(Categorys, "TST", "Categories", Index<>, extra_3::PointId, extra_3::Category);
52
53namespace test_indices
54{
58DECLARE_SOA_INDEX_COLUMN(Category, category);
59} // namespace test_indices
60
61DECLARE_SOA_INDEX_TABLE(IDXs, Points, "Index1", test_indices::PointId, test_indices::DistanceId, test_indices::FlagId, test_indices::CategoryId);
62DECLARE_SOA_INDEX_TABLE(IDX2s, Points, "Index2", test_indices::DistanceId, test_indices::PointId, test_indices::FlagId, test_indices::CategoryId);
63} // namespace o2::aod
64
65TEST_CASE("TestIndexBuilder")
66{
67 TableBuilder b1;
68 auto w1 = b1.cursor<Points>();
69 TableBuilder b2;
70 auto w2 = b2.cursor<Distances>();
71 TableBuilder b3;
72 auto w3 = b3.cursor<Flags>();
73 TableBuilder b4;
74 auto w4 = b4.cursor<Categorys>();
75
76 for (auto i = 0; i < 10; ++i) {
77 w1(0, i * 2., i * 3., i * 4.);
78 }
79
80 std::array<int, 7> d{0, 1, 2, 4, 7, 8, 9};
81 std::array<int, 5> f{0, 1, 2, 5, 8};
82 std::array<int, 7> c{0, 1, 2, 3, 5, 7, 8};
83
84 for (auto i : d) {
85 w2(0, i, i * 10.);
86 }
87
88 for (auto i : f) {
89 w3(0, i, static_cast<bool>(i % 2));
90 }
91
92 for (auto i : c) {
93 w4(0, i, i + 2);
94 }
95
96 auto t1 = b1.finalize();
97 Points st1{t1};
98 auto t2 = b2.finalize();
99 Distances st2{t2};
100 auto t3 = b3.finalize();
101 Flags st3{t3};
102 auto t4 = b4.finalize();
103 Categorys st4{t4};
104
105 auto map = getIndexMapping<o2::aod::MetadataTrait<o2::aod::Hash<"Index1/0"_h>>::metadata>();
106 auto schema1 = o2::aod::MetadataTrait<o2::aod::Hash<"Index1/0"_h>>::metadata::getSchema();
107 std::vector<o2::framework::IndexColumnBuilder> builders1;
108 auto t5 = IndexBuilder::materialize(builders1, {t1, t2, t3, t4}, map, schema1, true);
109 // auto t5 = IndexBuilder::materialize({t1, t2, t3, t4}, map, schema1, true);
110 REQUIRE(t5->num_rows() == 4);
111 IDXs idxt{t5};
112 idxt.bindExternalIndices(&st1, &st2, &st3, &st4);
113 for (auto& row : idxt) {
114 REQUIRE(row.distance().pointId() == row.pointId());
115 REQUIRE(row.flag().pointId() == row.pointId());
116 REQUIRE(row.category().pointId() == row.pointId());
117 }
118
119 map = getIndexMapping<o2::aod::MetadataTrait<o2::aod::Hash<"Index2/0"_h>>::metadata>();
120 auto schema2 = o2::aod::MetadataTrait<o2::aod::Hash<"Index2/0"_h>>::metadata::getSchema();
121 std::vector<o2::framework::IndexColumnBuilder> builders2;
122 auto t6 = IndexBuilder::materialize(builders2, {t2, t1, t3, t4}, map, schema2, false);
123 REQUIRE(t6->num_rows() == st2.size());
124 IDX2s idxs{t6};
125 std::array<int, 7> fs{0, 1, 2, -1, -1, 4, -1};
126 std::array<int, 7> cs{0, 1, 2, -1, 5, 6, -1};
127 idxs.bindExternalIndices(&st1, &st2, &st3, &st4);
128 auto i = 0;
129 for (auto const& row : idxs) {
130 REQUIRE(row.has_distance());
131 REQUIRE(row.has_point());
132 if (row.has_flag()) {
133 REQUIRE(row.flag().pointId() == row.pointId());
134 }
135 if (row.has_category()) {
136 REQUIRE(row.category().pointId() == row.pointId());
137 }
138 REQUIRE(row.flagId() == fs[i]);
139 REQUIRE(row.categoryId() == cs[i]);
140 ++i;
141 }
142}
143
144namespace o2::aod
145{
146namespace extra_4
147{
148DECLARE_SOA_COLUMN_FULL(Bin, bin, int, "bin");
149DECLARE_SOA_COLUMN_FULL(Color, color, int, "color");
150} // namespace extra_4
151
152DECLARE_SOA_TABLE(BinnedPoints, "TST", "BinnedPoints", Index<>, extra_4::Bin, test_indices::PointId);
153DECLARE_SOA_TABLE(ColoredPoints, "TST", "ColoredPoints", Index<>, extra_4::Color, test_indices::PointId);
154
155namespace test_indices
156{
157DECLARE_SOA_SLICE_INDEX_COLUMN(BinnedPoint, binsSlice);
158DECLARE_SOA_ARRAY_INDEX_COLUMN(ColoredPoint, colorsList);
159} // namespace test_indices
160
161DECLARE_SOA_INDEX_TABLE(IDX3s, Points, "Index3", test_indices::PointId, test_indices::BinnedPointIdSlice, test_indices::ColoredPointIds);
162} // namespace o2::aod
163
164TEST_CASE("AdvancedIndexTables")
165{
166 TableBuilder b1;
167 auto w1 = b1.cursor<Points>();
168 for (auto i = 0; i < 10; ++i) {
169 w1(0, i * 2., i * 3., i * 4.);
170 }
171 auto t1 = b1.finalize();
172 Points st1{t1};
173
174 TableBuilder b2;
175 auto w2 = b2.cursor<BinnedPoints>();
176 std::array<int, 3> skipPoints = {2, 6, 9};
177 std::array<int, 10> sizes = {5, 3, 0, 12, 4, 1, 0, 8, 2, 0};
178 auto count = 0;
179 for (auto i = 0; i < 10; ++i) {
180 if (i == skipPoints[count]) {
181 ++count;
182 continue;
183 }
184 for (auto j = 0; j < sizes[i]; ++j) {
185 w2(0, j + 1, i);
186 }
187 }
188 auto t2 = b2.finalize();
189 BinnedPoints st2{t2};
190
191 TableBuilder b3;
192 auto w3 = b3.cursor<ColoredPoints>();
193 std::array<int, 20> pointIds1 = {19, 2, 10, 5, 7, 17, 1, 3, 9, 12, 17, 6, 4, 13, 8, 5, 16, 15, 18, 0};
194 std::array<int, 20> pointIds2 = {3, 19, 2, 6, 4, 13, 11, 5, 7, 11, 1, 9, 12, 17, 8, 14, 16, 2, 18, 0};
195 std::array<int, 20> pointIds3 = {19, 2, 9, 15, 1, 3, 9, 12, 17, 18, 0, 10, 5, 7, 11, 6, 4, 13, 9, 14};
196 for (int i = 0; i < 20; ++i) {
197 w3(0, i, pointIds1[i]);
198 }
199 for (int i = 0; i < 20; ++i) {
200 w3(0, i + 20, pointIds2[i]);
201 }
202 for (int i = 0; i < 20; ++i) {
203 w3(0, i + 40, pointIds3[i]);
204 }
205 auto tc = b3.finalize();
206 ColoredPoints st3{tc};
207
208 std::array<int, 10> colorsizes = {3, 3, 4, 3, 3, 4, 3, 3, 2, 5};
209 std::array<std::vector<int>, 10> colorvalues = {{{19, 39, 50},
210 {6, 30, 44},
211 {1, 22, 37, 41},
212 {7, 20, 45},
213 {12, 24, 56},
214 {3, 15, 27, 52},
215 {11, 23, 55},
216 {4, 28, 53},
217 {14, 34},
218 {8, 31, 42, 46, 58}}};
219
220 auto map = getIndexMapping<o2::aod::MetadataTrait<o2::aod::Hash<"Index3/0"_h>>::metadata>();
221 auto schema3 = o2::aod::MetadataTrait<o2::aod::Hash<"Index3/0"_h>>::metadata::getSchema();
222 std::vector<o2::framework::IndexColumnBuilder> builders3;
223 auto t3 = IndexBuilder::materialize(builders3, {t1, t2, tc}, map, schema3, false);
224 REQUIRE(t3->num_rows() == st1.size());
225 IDX3s idxs{t3};
226 idxs.bindExternalIndices(&st1, &st2, &st3);
227 count = 0;
228 for (auto const& row : idxs) {
229 REQUIRE(row.has_point());
230 if (row.has_binsSlice()) {
231 auto slice = row.binsSlice();
232 REQUIRE(slice.size() == sizes[count]);
233 for (auto const& bin : slice) {
234 REQUIRE(bin.pointId() == row.pointId());
235 }
236 }
237 auto colors = row.colorsList();
238 REQUIRE(colors.size() == (size_t)colorsizes[count]);
239 for (auto j = 0U; j < colors.size(); ++j) {
240 REQUIRE(colors[j].color() == colorvalues[count][j]);
241 }
242 ++count;
243 }
244}
245
246TEST_CASE("IndexRecordsSerialization")
247{
248 auto map = getIndexMapping<o2::aod::MetadataTrait<o2::aod::Hash<"Index1/0"_h>>::metadata>();
249
250 std::stringstream osm;
251 IndexJSONHelpers::write(osm, map);
252
253 std::stringstream ism;
254 ism.str(osm.str());
255 auto rmap = IndexJSONHelpers::read(ism);
256 REQUIRE(map == rmap);
257
258 map = getIndexMapping<o2::aod::MetadataTrait<o2::aod::Hash<"Index2/0"_h>>::metadata>();
259
260 osm.clear();
261 osm.str("");
262 IndexJSONHelpers::write(osm, map);
263
264 ism.clear();
265 ism.str(osm.str());
266 rmap = IndexJSONHelpers::read(ism);
267 REQUIRE(map == rmap);
268
269 map = getIndexMapping<o2::aod::MetadataTrait<o2::aod::Hash<"Index3/0"_h>>::metadata>();
270
271 osm.clear();
272 osm.str("");
273 IndexJSONHelpers::write(osm, map);
274
275 ism.clear();
276 ism.str(osm.str());
277 rmap = IndexJSONHelpers::read(ism);
278 REQUIRE(map == rmap);
279}
#define DECLARE_SOA_ARRAY_INDEX_COLUMN(_Name_, _Getter_)
Definition ASoA.h:2798
#define O2ORIGIN(_Str_)
Pre-declare Hash specialization for an origin string.
Definition ASoA.h:301
#define DECLARE_SOA_SLICE_INDEX_COLUMN(_Name_, _Getter_)
Definition ASoA.h:2642
#define DECLARE_SOA_TABLE(_Name_, _Origin_, _Desc_,...)
Definition ASoA.h:3198
#define DECLARE_SOA_COLUMN_FULL(_Name_, _Getter_, _Type_, _Label_)
Definition ASoA.h:2352
#define DECLARE_SOA_INDEX_COLUMN(_Name_, _Getter_)
Definition ASoA.h:2880
#define DECLARE_SOA_INDEX_TABLE(_Name_, _Key_, _Description_,...)
Definition ASoA.h:3336
std::pair< double, double > Point
int32_t i
uint32_t j
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
std::shared_ptr< arrow::Table > finalize()
GLint GLenum GLint x
Definition glcorearb.h:403
GLint GLsizei count
Definition glcorearb.h:399
GLuint color
Definition glcorearb.h:1272
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
Definition glcorearb.h:2595
GLsizei const GLubyte GLsizei GLenum const void * coords
Definition glcorearb.h:5468
GLdouble f
Definition glcorearb.h:310
GLsizei GLsizei GLfloat distance
Definition glcorearb.h:5506
GLdouble GLdouble GLdouble z
Definition glcorearb.h:843
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition glcorearb.h:5034
Defining PrimaryVertex explicitly as messageable.
TEST_CASE("test_prepareArguments")
static constexpr char const *const str
Definition ASoA.h:276
static void write(std::ostream &o, std::vector< o2::soa::IndexRecord > &irs)
static std::vector< o2::soa::IndexRecord > read(std::istream &s)
static std::shared_ptr< arrow::Table > materialize(std::vector< framework::IndexColumnBuilder > &builders, std::vector< std::shared_ptr< arrow::Table > > &&tables, std::vector< soa::IndexRecord > const &records, std::shared_ptr< arrow::Schema > const &schema, bool exclusive)
std::vector< int > row