3#include "catch2/catch_test_macros.hpp"
5#include "datamodel/ExampleClusterCollection.h"
6#include "datamodel/ExampleHitCollection.h"
14 auto clusters = ExampleClusterCollection();
15 clusters.create(3.14f);
16 clusters.create(42.0f);
18 event.put(std::move(clusters),
"clusters");
20 auto& coll =
event.get<ExampleClusterCollection>(
"clusters");
21 REQUIRE(coll[0].energy() == 3.14f);
22 REQUIRE(coll[1].energy() == 42.0f);
28 event.putParameter(
"aString",
"from a string literal");
29 REQUIRE(event.getParameter<std::string>(
"aString") ==
"from a string literal");
31 event.putParameter(
"someInts", {42, 123});
32 const auto& ints =
event.getParameter<std::vector<int>>(
"someInts");
33 REQUIRE(ints.size() == 2);
34 REQUIRE(ints[0] == 42);
35 REQUIRE(ints[1] == 123);
37 event.putParameter(
"someStrings", {
"one",
"two",
"three"});
38 const auto& strings =
event.getParameter<std::vector<std::string>>(
"someStrings");
39 REQUIRE(strings.size() == 3);
40 REQUIRE(strings[0] ==
"one");
41 REQUIRE(strings[1] ==
"two");
42 REQUIRE(strings[2] ==
"three");
44 const auto stringKeys =
event.getParameterKeys<std::string>();
45 REQUIRE(stringKeys.size() == 2);
47 REQUIRE(std::find(stringKeys.begin(), stringKeys.end(),
"aString") != stringKeys.end());
48 REQUIRE(std::find(stringKeys.begin(), stringKeys.end(),
"someStrings") != stringKeys.end());
56TEST_CASE(
"Frame collections multithreaded insert",
"[frame][basics][multithread]") {
57 constexpr int nThreads = 10;
58 std::vector<std::thread> threads;
64 for (
int i = 0; i < nThreads; ++i) {
65 threads.emplace_back([&frame, i]() {
66 auto clusters = ExampleClusterCollection();
67 clusters.create(i * 3.14);
68 clusters.create(i * 3.14);
69 frame.put(std::move(clusters),
"clusters_" + std::to_string(i));
71 auto hits = ExampleHitCollection();
72 hits.create(i * 100ULL);
73 hits.create(i * 100ULL);
74 hits.create(i * 100ULL);
75 frame.put(std::move(hits),
"hits_" + std::to_string(i));
79 for (
auto& t : threads) {
84 for (
int i = 0; i < nThreads; ++i) {
85 auto& hits = frame.get<ExampleHitCollection>(
"hits_" + std::to_string(i));
86 REQUIRE(hits.size() == 3);
87 for (
const auto h : hits) {
88 REQUIRE(h.cellID() == i * 100ULL);
91 auto& clusters = frame.get<ExampleClusterCollection>(
"clusters_" + std::to_string(i));
92 REQUIRE(clusters.size() == 2);
93 for (
const auto c : clusters) {
94 REQUIRE(c.energy() == i * 3.14);
103 frame.put(ExampleClusterCollection(),
"emptyClusters");
106 auto& hits = frame.put(
108 auto coll = ExampleHitCollection();
109 auto hit = coll.create(0x42ULL, 0., 0., 0., 0.);
110 auto hit2 = coll.create(0x123ULL, 1., 1., 1., 1.);
115 auto clusters = ExampleClusterCollection();
116 auto cluster = clusters.create(3.14f);
117 cluster.addHits(hits[0]);
118 auto cluster2 = clusters.create(42.0f);
119 cluster2.addHits(hits[1]);
120 cluster2.addClusters(cluster);
123 frame.put(std::move(clusters),
"clusters");
125 frame.putParameter(
"anInt", 42);
126 frame.putParameter(
"someFloats", {1.23f, 2.34f, 3.45f});
132std::string
makeName(
const std::string& prefix,
int index) {
133 return prefix +
"_" + std::to_string(index);
146TEST_CASE(
"Frame collections multithreaded insert and read",
"[frame][basics][multithread]") {
147 constexpr int nThreads = 10;
148 std::vector<std::thread> threads;
158 std::array<int, nThreads> successes{};
161 for (
int i = 0; i < nThreads; ++i) {
162 threads.emplace_back([&frame, i, &successes]() {
163 auto clusters = ExampleClusterCollection();
164 clusters.create(i * 3.14);
165 clusters.create(i * 3.14);
166 frame.put(std::move(clusters),
makeName(
"clusters", i));
169 auto& existingClu = frame.get<ExampleClusterCollection>(
"clusters");
171 auto& existingHits = frame.get<ExampleHitCollection>(
"hits");
174 auto hits = ExampleHitCollection();
175 hits.create(i * 100ULL);
176 hits.create(i * 100ULL);
177 hits.create(i * 100ULL);
178 frame.put(std::move(hits),
makeName(
"hits", i));
182 constexpr int nColls = 100;
183 for (
int k = 0; k < nColls; ++k) {
184 frame.put(ExampleHitCollection(),
"h_" + std::to_string(i) +
"_" + std::to_string(k));
189 for (
auto& t : threads) {
194 for (
int i = 0; i < nThreads; ++i) {
196 REQUIRE(successes[i] == 2);
198 auto& hits = frame.get<ExampleHitCollection>(
makeName(
"hits", i));
199 REQUIRE(hits.size() == 3);
200 for (
const auto h : hits) {
201 REQUIRE(h.cellID() == i * 100ULL);
204 auto& clusters = frame.get<ExampleClusterCollection>(
makeName(
"clusters", i));
205 REQUIRE(clusters.size() == 2);
206 for (
const auto c : clusters) {
207 REQUIRE(c.energy() == i * 3.14);
216 auto& hits = frame.
get<ExampleHitCollection>(
"hits");
217 REQUIRE(hits.size() == 2);
218 REQUIRE(hits[0].energy() == 0);
219 REQUIRE(hits[0].cellID() == 0x42ULL);
220 REQUIRE(hits[1].energy() == 1);
221 REQUIRE(hits[1].cellID() == 0x123ULL);
223 REQUIRE(frame.
get<ExampleClusterCollection>(
"emptyClusters").size() == 0);
225 auto& clusters = frame.
get<ExampleClusterCollection>(
"clusters");
226 REQUIRE(clusters.size() == 2);
227 REQUIRE(clusters[0].energy() == 3.14f);
228 REQUIRE(clusters[0].Hits().size() == 1);
229 REQUIRE(clusters[0].Hits()[0] == hits[0]);
230 REQUIRE(clusters[0].Clusters().empty());
232 REQUIRE(clusters[1].energy() == 42.f);
233 REQUIRE(clusters[1].Hits().size() == 1);
234 REQUIRE(clusters[1].Hits()[0] == hits[1]);
235 REQUIRE(clusters[1].Clusters()[0] == clusters[0]);
238 auto& floats = frame.
getParameter<std::vector<float>>(
"someFloats");
239 REQUIRE(floats.size() == 3);
240 REQUIRE(floats[0] == 1.23f);
241 REQUIRE(floats[1] == 2.34f);
242 REQUIRE(floats[2] == 3.45f);
245TEST_CASE(
"Frame movability",
"[frame][move-semantics]") {
249 SECTION(
"Move constructor") {
250 auto otherFrame = std::move(frame);
254 SECTION(
"Move assignment operator") {
256 otherFrame = std::move(frame);
260 SECTION(
"Use after move construction") {
261 auto otherFrame = std::move(frame);
263 otherFrame.putParameter(
"aString",
"Can add strings after move-constructing");
264 REQUIRE(otherFrame.getParameter<std::string>(
"aString") ==
"Can add strings after move-constructing");
268 auto coll = ExampleHitCollection();
276 auto& hits = otherFrame.get<ExampleHitCollection>(
"moreHits");
277 REQUIRE(hits.size() == 3);
282TEST_CASE(
"Frame parameters multithread insert",
"[frame][basics][multithread]") {
284 constexpr int nThreads = 10;
285 std::vector<std::thread> threads;
286 threads.reserve(nThreads);
290 for (
int i = 0; i < nThreads; ++i) {
291 threads.emplace_back([&frame, i]() {
292 frame.putParameter(
makeName(
"int_par", i), i);
294 frame.putParameter(
makeName(
"float_par", i), (
float)i);
296 frame.putParameter(
makeName(
"string_par", i), std::to_string(i));
300 for (
auto& t : threads) {
304 for (
int i = 0; i < nThreads; ++i) {
305 REQUIRE(frame.getParameter<
int>(
makeName(
"int_par", i)) == i);
306 REQUIRE(frame.getParameter<
float>(
makeName(
"float_par", i)) == (
float)i);
307 REQUIRE(frame.getParameter<std::string>(
makeName(
"string_par", i)) == std::to_string(i));
311TEST_CASE(
"Frame parameters multithread insert and read",
"[frame][basics][multithread]") {
312 constexpr int nThreads = 10;
313 std::vector<std::thread> threads;
314 threads.reserve(nThreads);
317 frame.putParameter(
"int_par", 42);
318 frame.putParameter(
"string_par",
"some string");
319 frame.putParameter(
"float_pars", {1.23f, 4.56f, 7.89f});
325 std::array<int, nThreads> successes{};
327 for (
int i = 0; i < nThreads; ++i) {
328 threads.emplace_back([&frame, i, &successes]() {
329 frame.putParameter(
makeName(
"int", i), i);
330 frame.putParameter(
makeName(
"float", i), (
float)i);
332 CHECK_INCREASE(frame.getParameter<
int>(
"int_par") == 42, successes[i]);
335 frame.putParameter(
makeName(
"string", i), std::to_string(i));
336 CHECK_INCREASE(frame.getParameter<std::string>(
"string_par") ==
"some string", successes[i]);
338 const auto& floatPars = frame.getParameter<std::vector<float>>(
"float_pars");
346 constexpr int nParams = 100;
347 for (
int k = 0; k < nParams; ++k) {
348 frame.putParameter(
makeName(
"intPar", i) + std::to_string(k), i * k);
349 frame.putParameter(
makeName(
"floatPar", i) + std::to_string(k), (
float)i * k);
350 frame.putParameter(
makeName(
"stringPar", i) + std::to_string(k), std::to_string(i * k));
355 for (
auto& t : threads) {
359 for (
int i = 0; i < nThreads; ++i) {
361 REQUIRE(successes[i] == 7);
363 REQUIRE(frame.getParameter<
int>(
makeName(
"int", i)) == i);
364 REQUIRE(frame.getParameter<
float>(
makeName(
"float", i)) == (
float)i);
365 REQUIRE(frame.getParameter<std::string>(
makeName(
"string", i)) == std::to_string(i));
const CollT & get(const std::string &name) const
podio::GenericDataReturnType< T > getParameter(const std::string &key) const
TEST_CASE("Frame collections", "[frame][basics]")
void checkFrame(const podio::Frame &frame)
void CHECK_INCREASE(const bool condition, int &counter)
std::string makeName(const std::string &prefix, int index)