PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
write_frame.h
Go to the documentation of this file.
1#ifndef PODIO_TESTS_WRITE_FRAME_H // NOLINT(llvm-header-guard): folder structure not suitable
2#define PODIO_TESTS_WRITE_FRAME_H // NOLINT(llvm-header-guard): folder structure not suitable
3
4#include "datamodel/EventInfoCollection.h"
5#include "datamodel/ExampleClusterCollection.h"
6#include "datamodel/ExampleHitCollection.h"
7#include "datamodel/ExampleMCCollection.h"
8#include "datamodel/ExampleReferencingTypeCollection.h"
9#include "datamodel/ExampleWithARelationCollection.h"
10#include "datamodel/ExampleWithArrayCollection.h"
11#include "datamodel/ExampleWithFixedWidthIntegersCollection.h"
12#include "datamodel/ExampleWithNamespaceCollection.h"
13#include "datamodel/ExampleWithOneRelationCollection.h"
14#include "datamodel/ExampleWithVectorMemberCollection.h"
15
16#include "extension_model/ContainedTypeCollection.h"
17#include "extension_model/ExternalComponentTypeCollection.h"
18#include "extension_model/ExternalRelationTypeCollection.h"
19
20#include "podio/Frame.h"
22
23#include <string>
24#include <tuple>
25#include <vector>
26
27static const std::vector<std::string> collsToWrite = {"mcparticles",
28 "moreMCs",
29 "arrays",
30 "mcParticleRefs",
31 "hits",
32 "hitRefs",
33 "refs",
34 "refs2",
35 "clusters",
36 "OneRelation",
37 "info",
38 "WithVectorMember",
39 "fixedWidthInts",
40 "userInts",
41 "userDoubles",
42 "WithNamespaceMember",
43 "WithNamespaceRelation",
44 "WithNamespaceRelationCopy",
45 "emptyCollection",
46 "emptySubsetColl"};
47
49 auto mcps = ExampleMCCollection();
50
51 // ---- add some MC particles ----
52 auto mcp0 = mcps.create();
53 auto mcp1 = mcps.create();
54 auto mcp2 = mcps.create();
55 auto mcp3 = mcps.create();
56 auto mcp4 = mcps.create();
57 auto mcp5 = mcps.create();
58 auto mcp6 = mcps.create();
59 auto mcp7 = mcps.create();
60 auto mcp8 = mcps.create();
61 auto mcp9 = mcps.create();
62
63 auto mcp = mcps[0];
64 mcp.adddaughters(mcps[2]);
65 mcp.adddaughters(mcps[3]);
66 mcp.adddaughters(mcps[4]);
67 mcp.adddaughters(mcps[5]);
68 mcp = mcps[1];
69 mcp.adddaughters(mcps[2]);
70 mcp.adddaughters(mcps[3]);
71 mcp.adddaughters(mcps[4]);
72 mcp.adddaughters(mcps[5]);
73 mcp = mcps[2];
74 mcp.adddaughters(mcps[6]);
75 mcp.adddaughters(mcps[7]);
76 mcp.adddaughters(mcps[8]);
77 mcp.adddaughters(mcps[9]);
78 mcp = mcps[3];
79 mcp.adddaughters(mcps[6]);
80 mcp.adddaughters(mcps[7]);
81 mcp.adddaughters(mcps[8]);
82 mcp.adddaughters(mcps[9]);
83
84 //--- now fix the parent relations
85 // use a range-based for loop here to see if we get mutable objects from the
86 // begin/end iterators
87 for (auto mc : mcps) {
88 for (auto p : mc.daughters()) {
89 int dIndex = p.getObjectID().index;
90 auto d = mcps[dIndex];
91 d.addparents(p);
92 }
93 }
94
95 return mcps;
96}
97
99 auto arrays = ExampleWithArrayCollection();
100
101 std::array<int, 4> arrayTest = {0, 0, 2, 3};
102 std::array<int, 4> arrayTest2 = {4, 4, 2 * static_cast<int>(i)};
103 NotSoSimpleStruct a;
104 a.data.p = arrayTest2;
105 ex2::NamespaceStruct nstruct;
106 nstruct.x = static_cast<int>(i);
107 std::array<ex2::NamespaceStruct, 4> structArrayTest = {nstruct, nstruct, nstruct, nstruct};
108 auto array = MutableExampleWithArray(a, arrayTest, arrayTest, arrayTest, arrayTest, structArrayTest);
109 array.myArray(1, i);
110 array.arrayStruct(a);
111 arrays.push_back(array);
112
113 return arrays;
114}
115
116auto createMCRefCollection(const ExampleMCCollection& mcps, const ExampleMCCollection& moreMCs) {
117 auto mcpsRefs = ExampleMCCollection();
118 mcpsRefs.setSubsetCollection();
119 // ----------------- add all "odd" mc particles into a subset collection
120 for (auto p : mcps) {
121 if (p.id() % 2) {
122 mcpsRefs.push_back(p);
123 }
124 }
125 // ----------------- add the "even" counterparts from a different collection
126 for (auto p : moreMCs) {
127 if (p.id() % 2 == 0) {
128 mcpsRefs.push_back(p);
129 }
130 }
131
132 if (mcpsRefs.size() != mcps.size()) {
133 throw std::runtime_error(
134 "The mcParticleRefs collection should now contain as many elements as the mcparticles collection");
135 }
136
137 return mcpsRefs;
138}
139
141 ExampleHitCollection hits;
142
143 auto hit1 = ExampleHit(0xbad, 0., 0., 0., 23. + i);
144 auto hit2 = ExampleHit(0xcaffee, 1., 0., 0., 12. + i);
145
146 hits.push_back(hit1);
147 hits.push_back(hit2);
148
149 return hits;
150}
151
152auto createHitRefCollection(const ExampleHitCollection& hits) {
153 ExampleHitCollection hitRefs;
154 hitRefs.setSubsetCollection();
155
156 hitRefs.push_back(hits[1]);
157 hitRefs.push_back(hits[0]);
158
159 return hitRefs;
160}
161
162auto createClusterCollection(const ExampleHitCollection& hits) {
163 ExampleClusterCollection clusters;
164
165 auto cluster = MutableExampleCluster();
166 auto clu0 = MutableExampleCluster();
167 auto clu1 = MutableExampleCluster();
168
169 auto hit1 = hits[0];
170 auto hit2 = hits[1];
171
172 clu0.addHits(hit1);
173 clu0.energy(hit1.energy());
174 clu1.addHits(hit2);
175 clu1.energy(hit2.energy());
176 cluster.addHits(hit1);
177 cluster.addHits(hit2);
178 cluster.energy(hit1.energy() + hit2.energy());
179 cluster.addClusters(clu0);
180 cluster.addClusters(clu1);
181
182 clusters.push_back(clu0);
183 clusters.push_back(clu1);
184 clusters.push_back(cluster);
185
186 return clusters;
187}
188
189auto createReferencingCollections(const ExampleClusterCollection& clusters) {
190 auto retType = std::tuple<ExampleReferencingTypeCollection, ExampleReferencingTypeCollection>();
191 auto& [refs, refs2] = retType;
192
193 auto ref = MutableExampleReferencingType();
194 refs.push_back(ref);
195
196 auto ref2 = ExampleReferencingType();
197 refs2.push_back(ref2);
198
199 ref.addClusters(clusters[2]);
200 ref.addRefs(ref2);
201
202 auto cyclic = MutableExampleReferencingType();
203 cyclic.addRefs(cyclic);
204 refs.push_back(cyclic);
205
206 return retType;
207}
208
209auto createOneRelCollection(const ExampleClusterCollection& clusters) {
210 ExampleWithOneRelationCollection oneRels;
211
212 auto oneRel = MutableExampleWithOneRelation();
213 oneRel.cluster(clusters[2]);
214 oneRels.push_back(oneRel);
215
216 // write non-filled relation
217 auto oneRelEmpty = ExampleWithOneRelation();
218 oneRels.push_back(oneRelEmpty);
219
220 return oneRels;
221}
222
224 ExampleWithVectorMemberCollection vecs;
225
226 auto vec = MutableExampleWithVectorMember();
227 vec.addcount(i);
228 vec.addcount(i + 10);
229 vecs.push_back(vec);
230 auto vec1 = MutableExampleWithVectorMember();
231 vec1.addcount(i + 1);
232 vec1.addcount(i + 11);
233 vecs.push_back(vec1);
234
235 return vecs;
236}
237
239 EventInfoCollection info;
240
241 auto item1 = MutableEventInfo();
242 item1.Number(i);
243 info.push_back(item1);
244
245 return info;
246}
247
249 auto fixedWidthInts = ExampleWithFixedWidthIntegersCollection();
250
251 auto maxValues = fixedWidthInts.create();
252 maxValues.fixedI16(std::numeric_limits<std::int16_t>::max()); // 2^(16 - 1) - 1 == 32767
253 maxValues.fixedU32(std::numeric_limits<std::uint32_t>::max()); // 2^32 - 1 == 4294967295
254 maxValues.fixedU64(std::numeric_limits<std::uint64_t>::max()); // 2^64 - 1 == 18446744073709551615
255 auto& maxComp = maxValues.fixedWidthStruct();
256 maxComp.fixedUnsigned16 = std::numeric_limits<std::uint16_t>::max(); // 2^16 - 1 == 65535
257 maxComp.fixedInteger64 = std::numeric_limits<std::int64_t>::max(); // 2^(64 -1) - 1 == 9223372036854775807
258 maxComp.fixedInteger32 = std::numeric_limits<std::int32_t>::max(); // 2^(32 - 1) - 1 == 2147483647
259
260 auto minValues = fixedWidthInts.create();
261 minValues.fixedI16(std::numeric_limits<std::int16_t>::min()); // -2^(16 - 1) == -32768
262 minValues.fixedU32(std::numeric_limits<std::uint32_t>::min()); // 0
263 minValues.fixedU64(std::numeric_limits<std::uint64_t>::min()); // 0
264 auto& minComp = minValues.fixedWidthStruct();
265 minComp.fixedUnsigned16 = std::numeric_limits<std::uint16_t>::min(); // 0
266 minComp.fixedInteger64 = std::numeric_limits<std::int64_t>::min(); // -2^(64 - 1) == -9223372036854775808
267 minComp.fixedInteger32 = std::numeric_limits<std::int32_t>::min(); // -2^(32 - 1) == -2147483648
268
269 auto arbValues = fixedWidthInts.create();
270 arbValues.fixedI16(-12345);
271 arbValues.fixedU32(1234567890);
272 arbValues.fixedU64(1234567890123456789);
273 auto& arbComp = arbValues.fixedWidthStruct();
274 arbComp.fixedUnsigned16 = 12345;
275 arbComp.fixedInteger32 = -1234567890;
276 arbComp.fixedInteger64 = -1234567890123456789ll;
277
278 return fixedWidthInts;
279}
280
282 auto retType = std::tuple<podio::UserDataCollection<uint64_t>, podio::UserDataCollection<double>>();
283 auto& [usrInts, usrDoubles] = retType;
284
285 // add some plain ints as user data
286 usrInts.resize(i + 1);
287 int myInt = 0;
288 for (auto& iu : usrInts) {
289 iu = myInt++;
290 }
291
292 // and some user double values
293 unsigned nd = 100;
294 usrDoubles.resize(nd);
295 for (unsigned id = 0; id < nd; ++id) {
296 usrDoubles[id] = 42.;
297 }
298
299 return retType;
300}
301
303 auto retVal = std::tuple<ex42::ExampleWithNamespaceCollection, ex42::ExampleWithARelationCollection,
304 ex42::ExampleWithARelationCollection>{};
305 auto& [namesps, namesprels, cpytest] = retVal;
306
307 for (int j = 0; j < 5; j++) {
308 auto rel = ex42::MutableExampleWithARelation();
309 rel.number(0.5 * j);
310 auto exWithNamesp = ex42::MutableExampleWithNamespace();
311 exWithNamesp.component().x = i;
312 exWithNamesp.component().y = 1000 * i;
313 namesps.push_back(exWithNamesp);
314 if (j != 3) { // also check for empty relations
315 rel.ref(exWithNamesp);
316 for (int k = 0; k < 5; k++) {
317 auto namesp = ex42::MutableExampleWithNamespace();
318 namesp.x(3 * k);
319 namesp.component().y = k;
320 namesps.push_back(namesp);
321 rel.addrefs(namesp);
322 }
323 }
324 namesprels.push_back(rel);
325 }
326 for (auto&& namesprel : namesprels) {
327 cpytest.push_back(namesprel.clone());
328 }
329
330 return retVal;
331}
332
334 auto coll = extension::ContainedTypeCollection();
335 // The ContainedType only has a polar vector
336 auto contElem = coll.create();
337 contElem.setAVector({i * 123.f, 0.15f, 3.14f});
338
339 return coll;
340}
341
343 auto coll = extension::ExternalComponentTypeCollection();
344
345 // Set the upstream components only here
346 auto extCompElem = coll.create();
347 extension::ExtComponent extComp{};
348 extComp.aStruct.data.x = 42 * i;
349 extComp.nspStruct = ex2::NamespaceStruct{i, 2 * i};
350 extCompElem.setAComponent(extComp);
351 SimpleStruct simpleS{};
352 simpleS.p = {i, i - 2, i + 4, i * 8};
353 extCompElem.setAStruct(simpleS);
354
355 return coll;
356}
357
358auto createExtensionExternalRelationCollection(int i, const ExampleHitCollection& hits,
359 const ExampleClusterCollection& clusters) {
360 auto coll = extension::ExternalRelationTypeCollection();
361
362 auto elem0 = coll.create();
363 elem0.setWeight(i * 100.f);
364 elem0.setSingleHit(hits[0]);
365
366 auto elem1 = coll.create();
367 elem1.addToClusters(clusters[1]);
368 elem1.addToClusters(clusters[0]);
369
370 auto elem2 = coll.create();
371 for (int j = 0; j < 3; j++) {
372 auto s = SimpleStruct();
373 s.y = j * i;
374 elem2.addToSomeStructs(s);
375 }
376
377 return coll;
378}
379
381 podio::Frame frame{};
382
383 frame.put(createArrayCollection(iFrame), "arrays");
384 frame.put(createVectorMemberCollection(iFrame), "WithVectorMember");
385 frame.put(createInfoCollection(iFrame), "info");
386 frame.put(createFixedWidthCollection(), "fixedWidthInts");
387
388 auto& mcps = frame.put(createMCCollection(), "mcparticles");
389
390 ExampleMCCollection moreMCs{};
391 for (const auto&& mc : mcps) {
392 moreMCs.push_back(mc.clone());
393 }
394 auto& otherMCs = frame.put(std::move(moreMCs), "moreMCs");
395 frame.put(createMCRefCollection(mcps, otherMCs), "mcParticleRefs");
396
397 const auto& hits = frame.put(createHitCollection(iFrame), "hits");
398 frame.put(createHitRefCollection(hits), "hitRefs");
399
400 const auto& clusters = frame.put(createClusterCollection(hits), "clusters");
401
402 auto [refs, refs2] = createReferencingCollections(clusters);
403 frame.put(std::move(refs), "refs");
404 frame.put(std::move(refs2), "refs2");
405
406 frame.put(createOneRelCollection(clusters), "OneRelation");
407
408 auto [usrInts, usrDoubles] = createUserDataCollections(iFrame);
409 frame.put(std::move(usrInts), "userInts");
410 frame.put(std::move(usrDoubles), "userDoubles");
411
412 auto [namesps, namespsrels, cpytest] = createNamespaceRelationCollection(iFrame);
413 frame.put(std::move(namesps), "WithNamespaceMember");
414 frame.put(std::move(namespsrels), "WithNamespaceRelation");
415 frame.put(std::move(cpytest), "WithNamespaceRelationCopy");
416
417 // Parameters
418 frame.putParameter("anInt", 42 + iFrame);
419 frame.putParameter("UserEventWeight", 100.f * iFrame);
420 frame.putParameter("UserEventName", " event_number_" + std::to_string(iFrame));
421 frame.putParameter("SomeVectorData", {1, 2, 3, 4});
422 frame.putParameter("SomeVectorData", {"just", "some", "strings"});
423 frame.putParameter("SomeVectorData", {iFrame * 1.1, iFrame * 2.2});
424 frame.putParameter("SomeValue", "string value");
425 frame.putParameter("SomeValue", 42);
426
427 // An empty collection
428 frame.put(ExampleClusterCollection(), "emptyCollection");
429 // An empty subset collection
430 auto emptySubsetColl = ExampleHitCollection();
431 emptySubsetColl.setSubsetCollection();
432 frame.put(std::move(emptySubsetColl), "emptySubsetColl");
433
434 frame.put(createExtensionContainedCollection(iFrame), "extension_Contained");
435 frame.put(createExtensionExternalComponentCollection(iFrame), "extension_ExternalComponent");
436 frame.put(createExtensionExternalRelationCollection(iFrame, hits, clusters), "extension_ExternalRelation");
437
438 return frame;
439}
440
441template <typename WriterT>
442void write_frames(const std::string& filename) {
443 WriterT writer(filename);
444
445 for (int i = 0; i < 10; ++i) {
446 auto frame = makeFrame(i);
447 writer.writeFrame(frame, "events", collsToWrite);
448 }
449
450 for (int i = 100; i < 110; ++i) {
451 auto frame = makeFrame(i);
452 writer.writeFrame(frame, "other_events");
453 }
454
455 writer.finish();
456}
457
458#endif // PODIO_TESTS_WRITE_FRAME_H
const CollT & put(CollT &&coll, const std::string &name)
Definition: Frame.h:319
auto createExtensionExternalRelationCollection(int i, const ExampleHitCollection &hits, const ExampleClusterCollection &clusters)
Definition: write_frame.h:358
auto createUserDataCollections(int i)
Definition: write_frame.h:281
auto createHitRefCollection(const ExampleHitCollection &hits)
Definition: write_frame.h:152
auto createArrayCollection(int i)
Definition: write_frame.h:98
auto createInfoCollection(int i)
Definition: write_frame.h:238
auto createVectorMemberCollection(int i)
Definition: write_frame.h:223
podio::Frame makeFrame(int iFrame)
Definition: write_frame.h:380
auto createFixedWidthCollection()
Definition: write_frame.h:248
auto createClusterCollection(const ExampleHitCollection &hits)
Definition: write_frame.h:162
auto createMCRefCollection(const ExampleMCCollection &mcps, const ExampleMCCollection &moreMCs)
Definition: write_frame.h:116
auto createHitCollection(int i)
Definition: write_frame.h:140
auto createMCCollection()
Definition: write_frame.h:48
auto createOneRelCollection(const ExampleClusterCollection &clusters)
Definition: write_frame.h:209
auto createExtensionExternalComponentCollection(int i)
Definition: write_frame.h:342
auto createReferencingCollections(const ExampleClusterCollection &clusters)
Definition: write_frame.h:189
void write_frames(const std::string &filename)
Definition: write_frame.h:442
auto createNamespaceRelationCollection(int i)
Definition: write_frame.h:302
auto createExtensionContainedCollection(int i)
Definition: write_frame.h:333