PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
read_frame.h File Reference
#include "read_test.h"
#include "extension_model/ContainedTypeCollection.h"
#include "extension_model/ExternalComponentTypeCollection.h"
#include "extension_model/ExternalRelationTypeCollection.h"
#include "podio/Frame.h"
#include <iostream>

Go to the source code of this file.

Macros

#define PODIO_TESTS_READ_FRAME_H
 
#define ASSERT(condition, msg)
 

Functions

void processExtensions (const podio::Frame &event, int iEvent, podio::version::Version)
 
template<typename ReaderT >
int read_frames (const std::string &filename)
 

Macro Definition Documentation

◆ ASSERT

#define ASSERT (   condition,
  msg 
)
Value:
if (!(condition)) { \
throw std::runtime_error(msg); \
}

Definition at line 14 of file read_frame.h.

◆ PODIO_TESTS_READ_FRAME_H

#define PODIO_TESTS_READ_FRAME_H

Definition at line 2 of file read_frame.h.

Function Documentation

◆ processExtensions()

void processExtensions ( const podio::Frame event,
int  iEvent,
podio::version::Version   
)

Definition at line 19 of file read_frame.h.

19 {
20 const auto& extColl = event.get<extension::ContainedTypeCollection>("extension_Contained");
21 ASSERT(extColl.isValid(), "extension_Contained collection should be present");
22 ASSERT(extColl.size() == 1, "extension_Contained collection should have one element");
23 auto extElem = extColl[0];
24 const auto& polVec = extElem.getAVector();
25 ASSERT(polVec.r == iEvent * 123.f, "polVec.r value not as expected");
26 ASSERT(polVec.phi == 0.15f, "polVec.phi value not as epxected");
27 ASSERT(polVec.rho == 3.14f, "polVec.phi value not as epxected");
28
29 const auto& extCompColl = event.get<extension::ExternalComponentTypeCollection>("extension_ExternalComponent");
30 ASSERT(extCompColl.isValid(), "extension_ExternalComponent collection should be present");
31 ASSERT(extCompColl.size() == 1, "extension_ExternalComponent should have one element");
32 auto extCompElem = extCompColl[0];
33 ASSERT((extCompElem.getAStruct().p == std::array{iEvent, iEvent - 2, iEvent + 4, iEvent * 8}),
34 "aStruct.p value not as expected");
35 ASSERT(extCompElem.getAComponent().aStruct.data.x == 42 * iEvent, "aComponent.aStruct.x value not as expected");
36 ASSERT(extCompElem.getAComponent().nspStruct.x == iEvent, "aComponent.nspStruct.x value not as expected");
37 ASSERT(extCompElem.getAComponent().nspStruct.y == iEvent * 2, "aComponent.nspStruct.y value not as expected");
38
39 const auto& extRelColl = event.get<extension::ExternalRelationTypeCollection>("extension_ExternalRelation");
40 ASSERT(extRelColl.isValid(), "extension_ExternalRelation collection should be present");
41 ASSERT(extRelColl.size() == 3, "extension_ExternalRelation collection should contain 3 elements");
42
43 const auto& hits = event.get<ExampleHitCollection>("hits");
44 auto elem0 = extRelColl[0];
45 ASSERT(elem0.getWeight() == iEvent * 100.f, "weight of first element not as expected");
46 ASSERT(elem0.getSingleHit() == hits[0], "single hit relation not as expected");
47
48 const auto& clusters = event.get<ExampleClusterCollection>("clusters");
49 auto elem1 = extRelColl[1];
50 const auto relClusters = elem1.getClusters();
51 ASSERT(relClusters.size() == 2, "element should have two related clusters");
52 ASSERT(relClusters[0] == clusters[1], "first related cluster not as expected");
53 ASSERT(relClusters[1] == clusters[0], "first related cluster not as expected");
54
55 auto elem2 = extRelColl[2];
56 const auto& structs = elem2.getSomeStructs();
57 ASSERT(structs.size() == 3, "element should have 3 struct vector members");
58 ASSERT(structs[0].y == 0, "struct value not as expected");
59 ASSERT(structs[1].y == iEvent, "struct value not as expected");
60 ASSERT(structs[2].y == 2 * iEvent, "struct value not as expected");
61}
#define ASSERT(condition, msg)
Definition: read_frame.h:14

Referenced by read_frames().

◆ read_frames()

template<typename ReaderT >
int read_frames ( const std::string &  filename)

Definition at line 64 of file read_frame.h.

64 {
65 auto reader = ReaderT();
66 try {
67 reader.openFile(filename);
68 } catch (const std::runtime_error& e) {
69 std::cout << "File could not be opened, aborting." << std::endl;
70 return 1;
71 }
72
73 if (reader.currentFileVersion() != podio::version::build_version) {
74 std::cerr << "The podio build version could not be read back correctly. "
75 << "(expected:" << podio::version::build_version << ", actual: " << reader.currentFileVersion() << ")"
76 << std::endl;
77 return 1;
78 }
79
80 if (reader.getEntries("events") != 10) {
81 std::cerr << "Could not read back the number of events correctly. "
82 << "(expected:" << 10 << ", actual: " << reader.getEntries("events") << ")" << std::endl;
83 return 1;
84 }
85
86 if (reader.getEntries("events") != reader.getEntries("other_events")) {
87 std::cerr << "Could not read back the number of events correctly. "
88 << "(expected:" << 10 << ", actual: " << reader.getEntries("other_events") << ")" << std::endl;
89 return 1;
90 }
91
92 // Read the frames in a different order than when writing them here to make
93 // sure that the writing/reading order does not impose any usage requirements
94 for (size_t i = 0; i < reader.getEntries("events"); ++i) {
95 auto frame = podio::Frame(reader.readNextEntry("events"));
96 if (frame.get("emptySubsetColl") == nullptr) {
97 std::cerr << "Could not retrieve an empty subset collection" << std::endl;
98 return 1;
99 }
100 if (frame.get("emptyCollection") == nullptr) {
101 std::cerr << "Could not retrieve an empty collection" << std::endl;
102 return 1;
103 }
104
105 processEvent(frame, i, reader.currentFileVersion());
106
107 auto otherFrame = podio::Frame(reader.readNextEntry("other_events"));
108 processEvent(otherFrame, i + 100, reader.currentFileVersion());
109 // The other_events category also holds external collections
110 processExtensions(otherFrame, i + 100, reader.currentFileVersion());
111 }
112
113 if (reader.readNextEntry("events")) {
114 std::cerr << "Trying to read more frame data than is present should return a nullptr" << std::endl;
115 return 1;
116 }
117
118 std::cout << "========================================================\n" << std::endl;
119 if (reader.readNextEntry("not_present")) {
120 std::cerr << "Trying to read non-existant frame data should return a nullptr" << std::endl;
121 return 1;
122 }
123
124 // Reading specific (jumping to) entry
125 {
126 auto frame = podio::Frame(reader.readEntry("events", 4));
127 processEvent(frame, 4, reader.currentFileVersion());
128 // Reading the next entry after jump, continues from after the jump
129 auto nextFrame = podio::Frame(reader.readNextEntry("events"));
130 processEvent(nextFrame, 5, reader.currentFileVersion());
131
132 auto otherFrame = podio::Frame(reader.readEntry("other_events", 4));
133 processEvent(otherFrame, 4 + 100, reader.currentFileVersion());
134 processExtensions(otherFrame, 4 + 100, reader.currentFileVersion());
135
136 // Jumping back also works
137 auto previousFrame = podio::Frame(reader.readEntry("other_events", 2));
138 processEvent(previousFrame, 2 + 100, reader.currentFileVersion());
139 processExtensions(previousFrame, 2 + 100, reader.currentFileVersion());
140
141 // Trying to read a Frame that is not present returns a nullptr
142 if (reader.readEntry("events", 10)) {
143 std::cerr << "Trying to read a specific entry that does not exist should return a nullptr" << std::endl;
144 return 1;
145 }
146 }
147
148 return 0;
149}
void processExtensions(const podio::Frame &event, int iEvent, podio::version::Version)
Definition: read_frame.h:19
void processEvent(StoreT &store, int eventNum, podio::version::Version fileVersion)
Definition: read_test.h:46