1#include "datamodel/ExampleMCCollection.h"
2#include "datamodel/MutableExampleReferencingType.h"
3#include "datamodel/MutableExampleWithVectorMember.h"
15 std::string(__FILE__) + std::string(":") + std::to_string(__LINE__) + std::string(" in ") + \
16 std::string(__PRETTY_FUNCTION__)
18#define ASSERT_CONDITION(condition, message) \
21 throw std::runtime_error(LOCATION() + std::string(": ") + message); \
25#define ASSERT_EQUAL(left, right, message) \
27 std::stringstream msg; \
28 msg << message << " | expected: " << right << " - actual: " << left; \
29 ASSERT_CONDITION(left == right, msg.str()) \
33 for (
int i = 0; i < 10; ++i) {
36 collection.push_back(mcp);
40 auto mcp = collection[0];
41 mcp.adddaughters(collection[1]);
42 mcp.adddaughters(collection[5]);
43 mcp.adddaughters(collection[4]);
47 mcp.adddaughters(collection[1]);
48 mcp.adddaughters(collection[9]);
49 mcp.addparents(collection[8]);
50 mcp.addparents(collection[6]);
54 mcp.adddaughters(collection[2]);
55 mcp.addparents(collection[3]);
56 collection[3].adddaughters(collection[6]);
62 ASSERT_CONDITION(collection[7].daughters().empty() && collection[7].parents().empty(),
63 "RelationRange of empty collection is not empty");
65 ASSERT_CONDITION(collection[7].daughters().empty() && collection[7].parents().empty(),
66 "RelationRange of empty collection is not empty");
69 for (
const auto& p [[maybe_unused]] : collection[7].daughters()) {
70 throw std::runtime_error(
"Range based for loop entered on a supposedly empty range");
73 ASSERT_EQUAL(collection[0].daughters().size(), 3,
"Range has wrong size");
75 std::vector<int> expectedPDG = {1, 5, 4};
77 for (
const auto& p : collection[0].daughters()) {
78 ASSERT_EQUAL(p.PDG(), expectedPDG[index],
"ExampleMC daughters range points to wrong particle (by PDG)");
83 const auto daughters = collection[0].daughters();
84 for (
size_t i = 0; i < expectedPDG.size(); ++i) {
85 const auto daughter = daughters[i];
86 ASSERT_EQUAL(daughter.PDG(), expectedPDG[i],
"ExampleMC daughter points to the wrong particle (by PDG)");
90 ASSERT_EQUAL(collection[2].daughters().size(), 2,
"Range has wrong size");
91 ASSERT_EQUAL(collection[2].parents().size(), 2,
"Range has wrong size");
95 for (
const auto& p : collection[2].daughters()) {
96 ASSERT_EQUAL(p.PDG(), expectedPDG[index],
"ExampleMC daughters range points to wrong particle (by PDG)");
101 for (
const auto& p : collection[2].parents()) {
102 ASSERT_EQUAL(p.PDG(), expectedPDG[index],
"ExampleMC parents range points to wrong particle (by PDG)");
107 const auto parents = collection[2].parents();
108 for (
size_t i = 0; i < expectedPDG.size(); ++i) {
109 const auto parent = parents.at(i);
110 ASSERT_EQUAL(parent.PDG(), expectedPDG[i],
"ExampleMC parents points to the wrong particle (by PDG)");
114 const auto parent [[maybe_unused]] = parents.at(3);
115 throw std::runtime_error(
"Trying to access out of bounds in a RelationRange::at should throw");
116 }
catch (
const std::out_of_range& err) {
117 ASSERT_EQUAL(err.what(), std::string(
"index out of bounds for RelationRange"),
118 "Access out of bounds throws wrong exception");
122 auto mcp6 = collection[6];
123 ASSERT_EQUAL(mcp6.daughters().size(), 1,
"Wrong number of daughters");
124 auto parent = mcp6.parents(0);
125 ASSERT_EQUAL(parent.daughters().size(), 1,
"Wrong number of daughters");
127 for (
const auto& p : mcp6.parents()) {
129 ASSERT_EQUAL(p, parent,
"parent-daughter relation is not as expected");
131 for (
const auto& p : parent.daughters()) {
133 ASSERT_EQUAL(p, mcp6,
"daughter-parent relation is not as expected");
138 ExampleMCCollection mcps;
144 MutableExampleWithVectorMember ex;
149 ASSERT_EQUAL(ex.count().size(), 3,
"vector member range has wrong size");
151 std::vector<int> expected = {1, 2, 10};
153 for (
const int c : ex.count()) {
154 ASSERT_EQUAL(c, expected[index],
"wrong content in range-based for loop");
160 MutableExampleReferencingType ex;
161 ExampleReferencingType ex1;
162 ExampleReferencingType ex2;
167 ASSERT_EQUAL(ex.Refs().size(), 2,
"Wrong number of references");
170 for (
const auto& e : ex.Refs()) {
172 ASSERT_EQUAL(e, ex1,
"First element of range is not as expected");
174 ASSERT_EQUAL(e, ex2,
"Second element of range is not as expected");
180 ASSERT_CONDITION(!ex.Refs().empty(),
"Relation range of element with relations should not be empty");
181 ASSERT_CONDITION(ex1.Refs().empty(),
"Relation range of element with no relations should be empty");
188 auto& collection = store.
create<ExampleMCCollection>(
"mcparticles");
191 for (
int i = 0; i < 10; ++i) {
201 reader.
openFile(
"relation_range_io_test.root");
204 for (
int i = 0; i < 10; ++i) {
205 auto& readColl = readStore.
get<ExampleMCCollection>(
"mcparticles");
206 ASSERT_CONDITION(readColl.isValid(),
"Collection 'mcparticles' should be present");
207 ASSERT_EQUAL(readColl.size(), 10,
"'mcparticles' should have 10 entries");
221 }
catch (std::runtime_error
const& ex) {
222 std::cerr << ex.what() << std::endl;
bool get(const std::string &name, const T *&collection)
access a collection by name. returns true if successful
void clear()
clears itself; deletes collections (use at end of event processing)
void clearCollections()
empties collections.
T & create(const std::string &name)
create a new collection
void setReader(IReader *reader)
set the reader
void openFile(const std::string &filename) override
void endOfEvent() override
Preparing to read next event.
bool registerForWrite(const std::string &name)
void testExampleReferencingType()
#define ASSERT_CONDITION(condition, message)
void doTestExampleMC(ExampleMCCollection const &collection)
void testExampleWithVectorMember()
#define ASSERT_EQUAL(left, right, message)
void fillExampleMCCollection(ExampleMCCollection &collection)