Skip to content

Commit 4291447

Browse files
authored
Refactor the read tests partially (#724)
* Refactor read tests to make them more unittest like * Refactor more checks into unittike tests * Also make links checks reusable
1 parent ad3fc80 commit 4291447

3 files changed

Lines changed: 139 additions & 107 deletions

File tree

tests/read_frame.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818

1919
#include <iostream>
2020

21-
#define ASSERT(condition, msg) \
22-
if (!(condition)) { \
23-
throw std::runtime_error(msg); \
24-
}
25-
2621
void processExtensions(const podio::Frame& event, int iEvent, podio::version::Version) {
2722
const auto& extColl = event.get<extension::ContainedTypeCollection>("extension_Contained");
2823
ASSERT(extColl.isValid(), "extension_Contained collection should be present")

tests/read_test.h

Lines changed: 138 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,136 @@
3030
#include <stdexcept>
3131
#include <vector>
3232

33+
#define ASSERT(condition, msg) \
34+
if (!(condition)) { \
35+
throw std::runtime_error(msg); \
36+
}
37+
38+
void checkIntUserDataCollection(const podio::Frame& event, int eventNum) {
39+
auto& usrInts = event.get<podio::UserDataCollection<uint64_t>>("userInts");
40+
ASSERT(usrInts.size() == static_cast<unsigned>(eventNum + 1), "userInts collection does not have the expected size")
41+
42+
auto& uivec = usrInts.vec();
43+
int myInt = 0;
44+
for (int iu : uivec) {
45+
ASSERT(iu == myInt++, "userInts contents not as expected");
46+
}
47+
48+
myInt = 0;
49+
for (int iu : usrInts) {
50+
ASSERT(iu == myInt++, "userInts contents not as expected");
51+
}
52+
}
53+
54+
void checkHitCollection(const podio::Frame& event, int eventNum) {
55+
const auto& hits = event.get<ExampleHitCollection>("hits");
56+
57+
ASSERT(hits.size() == 2, "size of hits collection not as expected");
58+
59+
const auto expectedHit1 = ExampleHit(0xbadULL, 0., 0., 0., 23. + eventNum);
60+
const auto expectedHit2 = ExampleHit(0xcaffeeULL, 1., 0., 0., 12. + eventNum);
61+
62+
const auto compareHits = [](const ExampleHit& hitA, const ExampleHit& hitB) {
63+
return hitA.cellID() == hitB.cellID() && hitA.energy() == hitB.energy() && hitA.x() == hitB.x() &&
64+
hitA.y() == hitB.y() && hitA.z() == hitB.z();
65+
};
66+
67+
auto hit1 = hits[0];
68+
ASSERT(compareHits(hit1, expectedHit1), "first hit in hits not as expected");
69+
auto hit2 = hits[1];
70+
ASSERT(compareHits(hit2, expectedHit2), "second hit in hits not as expected");
71+
}
72+
73+
void checkClusterCollection(const podio::Frame& event, const ExampleHitCollection& hits) {
74+
const auto& clusters = event.get<ExampleClusterCollection>("clusters");
75+
ASSERT(clusters.size() == 3, "size of clusters collection not as expected");
76+
77+
auto clu0 = clusters[0];
78+
auto clu1 = clusters[1];
79+
auto cluster = clusters[2];
80+
ASSERT(clu0.Hits().size() == 1, "first cluster should only have one hit");
81+
ASSERT(clu1.Hits().size() == 1, "second cluster should only have one hit");
82+
ASSERT(cluster.Hits().size() == 2, "third cluster should have two hits");
83+
ASSERT(cluster.Clusters().size() == 2, "third cluster should have two clusters");
84+
ASSERT(cluster.Clusters(0) == clu0, "first cluster of third cluster not as expected");
85+
ASSERT(cluster.Clusters(1) == clu1, "second cluster of third cluster not as expected");
86+
87+
auto hit1 = hits[0];
88+
auto hit2 = hits[1];
89+
ASSERT(clu0.Hits(0) == hit1, "hit related to first cluster not as expected");
90+
ASSERT(clu0.energy() == hit1.energy(), "energy of first cluster not as expected");
91+
ASSERT(clu1.Hits(0) == hit2, "hit related to second cluster not as expected");
92+
ASSERT(clu1.energy() == hit2.energy(), "energy of second cluster not as expected");
93+
ASSERT(cluster.Hits(0) == hit1, "first hit related to third cluster not as expected");
94+
ASSERT(cluster.Hits(1) == hit2, "second hit related to third cluster not as expected");
95+
ASSERT(cluster.energy() == hit1.energy() + hit2.energy(), "energy of third cluster not as expected");
96+
}
97+
98+
void checkMCParticleCollection(const podio::Frame& event, const podio::version::Version fileVersion) {
99+
const auto& mcps = event.get<ExampleMCCollection>("mcparticles");
100+
ASSERT(mcps.size() == 10, "mcparticles collection does not have the correct size");
101+
102+
auto mcp = mcps[0];
103+
ASSERT(mcp.daughters().size() == 4, "first mc particle does not have the expected number of daughters");
104+
ASSERT(mcp.daughters(0) == mcps[2], "daughter relation 0 for mcparticle 0 not as expected");
105+
ASSERT(mcp.daughters(1) == mcps[3], "daughter relation 1 for mcparticle 0 not as expected");
106+
ASSERT(mcp.daughters(2) == mcps[4], "daughter relation 2 for mcparticle 0 not as expected");
107+
ASSERT(mcp.daughters(3) == mcps[5], "daughter relation 3 for mcparticle 0 not as expected");
108+
109+
mcp = mcps[1];
110+
ASSERT(mcp.daughters().size() == 4, "second mc particle does not have the expected number of daughters");
111+
ASSERT(mcp.daughters(0) == mcps[2], "daughter relation 0 for mcparticle 1 not as expected");
112+
ASSERT(mcp.daughters(1) == mcps[3], "daughter relation 1 for mcparticle 1 not as expected");
113+
ASSERT(mcp.daughters(2) == mcps[4], "daughter relation 2 for mcparticle 1 not as expected");
114+
ASSERT(mcp.daughters(3) == mcps[5], "daughter relation 3 for mcparticle 1 not as expected");
115+
116+
mcp = mcps[2];
117+
ASSERT(mcp.daughters().size() == 4, "third mc particle does not have the expected number of daughters");
118+
ASSERT(mcp.daughters(0) == mcps[6], "daughter relation 0 for mcparticle 2 not as expected");
119+
ASSERT(mcp.daughters(1) == mcps[7], "daughter relation 1 for mcparticle 2 not as expected");
120+
ASSERT(mcp.daughters(2) == mcps[8], "daughter relation 2 for mcparticle 2 not as expected");
121+
ASSERT(mcp.daughters(3) == mcps[9], "daughter relation 3 for mcparticle 2 not as expected");
122+
123+
mcp = mcps[3];
124+
ASSERT(mcp.daughters().size() == 4, "fourth mc particle does not have the expected number of daughters");
125+
ASSERT(mcp.daughters(0) == mcps[6], "daughter relation 0 for mcparticle 3 not as expected");
126+
ASSERT(mcp.daughters(1) == mcps[7], "daughter relation 1 for mcparticle 3 not as expected");
127+
ASSERT(mcp.daughters(2) == mcps[8], "daughter relation 2 for mcparticle 3 not as expected");
128+
ASSERT(mcp.daughters(3) == mcps[9], "daughter relation 3 for mcparticle 3 not as expected");
129+
130+
// spot check some parent relations as well
131+
mcp = mcps[4];
132+
ASSERT(mcp.parents().size() == 2, "fivth mc particle does not have the expected number of parents");
133+
// Bugged writing before this version
134+
if (fileVersion >= podio::version::Version(1, 2, 0)) {
135+
ASSERT(mcp.parents(0) == mcps[0], "parent relation 0 for mcparticle 4 is not as expected");
136+
ASSERT(mcp.parents(1) == mcps[1], "parent relation 0 for mcparticle 4 is not as expected");
137+
}
138+
139+
mcp = mcps[7];
140+
ASSERT(mcp.parents().size() == 2, "eigth mc particle does not have the expected number of parents");
141+
// Bugged writing before this version
142+
if (fileVersion >= podio::version::Version(1, 2, 1)) {
143+
ASSERT(mcp.parents(0) == mcps[2], "parent relation 0 for mcparticle 8 is not as expected");
144+
ASSERT(mcp.parents(1) == mcps[3], "parent relation 0 for mcparticle 8 is not as expected");
145+
}
146+
}
147+
148+
void checkLinkCollection(const podio::Frame& event, const ExampleHitCollection& hits,
149+
const ExampleClusterCollection& clusters) {
150+
const auto& links = event.get<TestLinkCollection>("links");
151+
const auto nLinks = std::min(clusters.size(), hits.size());
152+
ASSERT(links.size() == nLinks, "LinksColelction does not have the expected size");
153+
154+
int linkIndex = 0;
155+
for (auto link : links) {
156+
ASSERT((link.getWeight() == 0.5 * linkIndex) && (link.getFrom() == hits[linkIndex]) &&
157+
(link.getTo() == clusters[nLinks - 1 - linkIndex]),
158+
"Link does not have expected content");
159+
linkIndex++;
160+
}
161+
}
162+
33163
template <typename FixedWidthT>
34164
bool check_fixed_width_value(FixedWidthT actual, FixedWidthT expected, const std::string& type) {
35165
if (actual != expected) {
@@ -75,7 +205,8 @@ void processEvent(const podio::Frame& event, int eventNum, podio::version::Versi
75205
}
76206
}
77207

78-
// read collection meta data
208+
checkHitCollection(event, eventNum);
209+
79210
auto& hits = event.get<ExampleHitCollection>("hits");
80211

81212
if (fileVersion > podio::version::Version{0, 14, 0}) {
@@ -88,15 +219,9 @@ void processEvent(const podio::Frame& event, int eventNum, podio::version::Versi
88219
}
89220
}
90221

222+
checkClusterCollection(event, hits);
223+
91224
auto& clusters = event.get<ExampleClusterCollection>("clusters");
92-
if (clusters.isValid()) {
93-
auto cluster = clusters[0];
94-
for (auto i = cluster.Hits_begin(), end = cluster.Hits_end(); i != end; ++i) {
95-
std::cout << " Referenced hit has an energy of " << i->energy() << std::endl;
96-
}
97-
} else {
98-
throw std::runtime_error("Collection 'clusters' should be present");
99-
}
100225

101226
if (fileVersion >= podio::version::Version{0, 13, 2}) {
102227
// Read the mcParticleRefs before reading any of the other collections that
@@ -124,68 +249,13 @@ void processEvent(const podio::Frame& event, int eventNum, podio::version::Versi
124249
}
125250
}
126251

252+
checkMCParticleCollection(event, fileVersion);
253+
127254
auto& mcps = event.get<ExampleMCCollection>("mcparticles");
128255
if (!mcps.isValid()) {
129256
throw std::runtime_error("Collection 'mcparticles' should be present");
130257
}
131258

132-
// check that we can retrieve the correct parent daughter relation
133-
// set in write_test.h :
134-
//-------- print relations for debugging:
135-
for (auto p : mcps) {
136-
std::cout << " particle " << p.getObjectID().index << " has daughters: ";
137-
for (auto it = p.daughters_begin(), end = p.daughters_end(); it != end; ++it) {
138-
std::cout << " " << it->getObjectID().index;
139-
}
140-
std::cout << " and parents: ";
141-
for (auto it = p.parents_begin(), end = p.parents_end(); it != end; ++it) {
142-
std::cout << " " << it->getObjectID().index;
143-
}
144-
std::cout << std::endl;
145-
}
146-
147-
// particle 0 has particles 2,3,4 and 5 as daughters:
148-
auto p = mcps[0];
149-
150-
auto d0 = p.daughters(0);
151-
auto d1 = p.daughters(1);
152-
auto d2 = p.daughters(2);
153-
auto d3 = p.daughters(3);
154-
155-
if (d0 != mcps[2]) {
156-
throw std::runtime_error(" error: 1. daughter of particle 0 is not particle 2 ");
157-
}
158-
if (d1 != mcps[3]) {
159-
throw std::runtime_error(" error: 2. daughter of particle 0 is not particle 3 ");
160-
}
161-
if (d2 != mcps[4]) {
162-
throw std::runtime_error(" error: 3. daughter of particle 0 is not particle 4 ");
163-
}
164-
if (d3 != mcps[5]) {
165-
throw std::runtime_error(" error: 4. daughter of particle 0 is not particle 5 ");
166-
}
167-
168-
// particle 3 has particles 6,7,8 and 9 as daughters:
169-
p = mcps[3];
170-
171-
d0 = p.daughters(0);
172-
d1 = p.daughters(1);
173-
d2 = p.daughters(2);
174-
d3 = p.daughters(3);
175-
176-
if (d0 != mcps[6]) {
177-
throw std::runtime_error(" error: 1. daughter of particle 3 is not particle 6 ");
178-
}
179-
if (d1 != mcps[7]) {
180-
throw std::runtime_error(" error: 2. daughter of particle 3 is not particle 7 ");
181-
}
182-
if (d2 != mcps[8]) {
183-
throw std::runtime_error(" error: 3. daughter of particle 3 is not particle 8 ");
184-
}
185-
if (d3 != mcps[9]) {
186-
throw std::runtime_error(" error: 4. daughter of particle 3 is not particle 9 ");
187-
}
188-
189259
// Check the MCParticle subset collection only if it is technically possible
190260
// to be in the file
191261
if (fileVersion >= podio::version::Version{0, 13, 2}) {
@@ -380,27 +450,7 @@ void processEvent(const podio::Frame& event, int eventNum, podio::version::Versi
380450
}
381451

382452
if (fileVersion >= podio::version::Version{0, 13, 2}) {
383-
auto& usrInts = event.get<podio::UserDataCollection<uint64_t>>("userInts");
384-
385-
if (usrInts.size() != static_cast<unsigned>(eventNum + 1)) {
386-
throw std::runtime_error("Could not read all userInts properly (expected: " + std::to_string(eventNum + 1) +
387-
", actual: " + std::to_string(usrInts.size()) + ")");
388-
}
389-
390-
auto& uivec = usrInts.vec();
391-
int myInt = 0;
392-
for (int iu : uivec) {
393-
if (iu != myInt++) {
394-
throw std::runtime_error("Couldn't read userInts properly");
395-
}
396-
}
397-
398-
myInt = 0;
399-
for (int iu : usrInts) {
400-
if (iu != myInt++) {
401-
throw std::runtime_error("Couldn't read userInts properly");
402-
}
403-
}
453+
checkIntUserDataCollection(event, eventNum);
404454

405455
auto& usrDbl = event.get<podio::UserDataCollection<double>>("userDoubles");
406456
if (usrDbl.size() != 100) {
@@ -417,20 +467,7 @@ void processEvent(const podio::Frame& event, int eventNum, podio::version::Versi
417467

418468
// ======================= Links ==========================
419469
if (fileVersion >= podio::version::Version{1, 1, 99}) {
420-
auto& links = event.get<TestLinkCollection>("links");
421-
const auto nLinks = std::min(clusters.size(), hits.size());
422-
if (links.size() != nLinks) {
423-
throw std::runtime_error("LinksCollection does not have the expected size");
424-
}
425-
int linkIndex = 0;
426-
for (auto link : links) {
427-
if (!((link.getWeight() == 0.5 * linkIndex) && (link.getFrom() == hits[linkIndex]) &&
428-
(link.getTo() == clusters[nLinks - 1 - linkIndex]))) {
429-
throw std::runtime_error("Link does not have expected content");
430-
}
431-
linkIndex++;
432-
}
433-
470+
checkLinkCollection(event, hits, clusters);
434471
auto& interfaceLinks = event.get<TestInterfaceLinkCollection>("links_with_interfaces");
435472
if (interfaceLinks.size() != 3) {
436473
throw std::runtime_error("Links with interfaces collection does not have the expected size (expected 3, actual " +

tests/write_frame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ auto createMCCollection() {
7878
for (auto p : mc.daughters()) {
7979
int dIndex = p.getObjectID().index;
8080
auto d = mcps[dIndex];
81-
d.addparents(p);
81+
d.addparents(mc);
8282
}
8383
}
8484

0 commit comments

Comments
 (0)