PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
write_test.h File Reference
#include "datamodel/EventInfoCollection.h"
#include "datamodel/ExampleClusterCollection.h"
#include "datamodel/ExampleHitCollection.h"
#include "datamodel/ExampleMCCollection.h"
#include "datamodel/ExampleReferencingTypeCollection.h"
#include "datamodel/ExampleWithARelationCollection.h"
#include "datamodel/ExampleWithArrayCollection.h"
#include "datamodel/ExampleWithComponentCollection.h"
#include "datamodel/ExampleWithFixedWidthIntegersCollection.h"
#include "datamodel/ExampleWithNamespaceCollection.h"
#include "datamodel/ExampleWithOneRelationCollection.h"
#include "datamodel/ExampleWithVectorMemberCollection.h"
#include "podio/EventStore.h"
#include "podio/UserDataCollection.h"
#include <iostream>
#include <limits>
#include <sstream>
#include <vector>

Go to the source code of this file.

Macros

#define PODIO_TESTS_WRITE_TEST_H
 

Functions

template<class WriterT >
void write (podio::EventStore &store, WriterT &writer)
 

Macro Definition Documentation

◆ PODIO_TESTS_WRITE_TEST_H

#define PODIO_TESTS_WRITE_TEST_H

Definition at line 2 of file write_test.h.

Function Documentation

◆ write()

template<class WriterT >
void write ( podio::EventStore store,
WriterT &  writer 
)

Definition at line 28 of file write_test.h.

28 {
29 std::cout << "start processing" << std::endl;
30
31 auto& info = store.create<EventInfoCollection>("info");
32 auto& mcps = store.create<ExampleMCCollection>("mcparticles");
33 auto& moreMCs = store.create<ExampleMCCollection>("moreMCs");
34 auto& mcpsRefs = store.create<ExampleMCCollection>("mcParticleRefs");
35 mcpsRefs.setSubsetCollection();
36 auto& hits = store.create<ExampleHitCollection>("hits");
37 auto& hitRefs = store.create<ExampleHitCollection>("hitRefs");
38 hitRefs.setSubsetCollection();
39 auto& clusters = store.create<ExampleClusterCollection>("clusters");
40 auto& refs = store.create<ExampleReferencingTypeCollection>("refs");
41 auto& refs2 = store.create<ExampleReferencingTypeCollection>("refs2");
42 auto& comps = store.create<ExampleWithComponentCollection>("Component");
43 auto& oneRels = store.create<ExampleWithOneRelationCollection>("OneRelation");
44 auto& vecs = store.create<ExampleWithVectorMemberCollection>("WithVectorMember");
45 auto& namesps = store.create<ex42::ExampleWithNamespaceCollection>("WithNamespaceMember");
46 auto& namesprels = store.create<ex42::ExampleWithARelationCollection>("WithNamespaceRelation");
47 auto& cpytest = store.create<ex42::ExampleWithARelationCollection>("WithNamespaceRelationCopy");
48 auto& arrays = store.create<ExampleWithArrayCollection>("arrays");
49 auto& fixedWidthInts = store.create<ExampleWithFixedWidthIntegersCollection>("fixedWidthInts");
50 auto& usrInts = store.create<podio::UserDataCollection<uint64_t>>("userInts");
51 auto& usrDoubles = store.create<podio::UserDataCollection<double>>("userDoubles");
52
53 writer.registerForWrite("info");
54 writer.registerForWrite("mcparticles");
55 writer.registerForWrite("moreMCs");
56 writer.registerForWrite("mcParticleRefs");
57 writer.registerForWrite("hits");
58 writer.registerForWrite("hitRefs");
59 writer.registerForWrite("clusters");
60 writer.registerForWrite("refs");
61 writer.registerForWrite("refs2");
62 writer.registerForWrite("Component");
63 writer.registerForWrite("OneRelation");
64 writer.registerForWrite("WithVectorMember");
65 writer.registerForWrite("WithNamespaceMember");
66 writer.registerForWrite("WithNamespaceRelation");
67 writer.registerForWrite("WithNamespaceRelationCopy");
68 writer.registerForWrite("arrays");
69 writer.registerForWrite("fixedWidthInts");
70 writer.registerForWrite("userInts");
71 writer.registerForWrite("userDoubles");
72
73 unsigned nevents = 2000;
74
75 for (unsigned i = 0; i < nevents; ++i) {
76 if (i % 1000 == 0) {
77 std::cout << "processing event " << i << std::endl;
78 }
79
80 auto item1 = MutableEventInfo();
81 item1.Number(i);
82 info.push_back(item1);
83
84 auto& evtMD = store.getEventMetaData();
85 evtMD.setValue("UserEventWeight", (float)100. * i);
86 std::stringstream ss;
87 ss << " event_number_" << i;
88 evtMD.setValue("UserEventName", ss.str());
89 evtMD.setValue("SomeVectorData", {1, 2, 3, 4});
90 evtMD.setValue("SomeVectorData", {i * 1.1, i * 2.2});
91
92 auto& colMD = store.getCollectionMetaData(hits.getID());
93 colMD.setValue("CellIDEncodingString", "system:8,barrel:3,layer:6,slice:5,x:-16,y:-16");
94
95 auto hit1 = ExampleHit(0xbad, 0., 0., 0., 23. + i);
96 auto hit2 = ExampleHit(0xcaffee, 1., 0., 0., 12. + i);
97
98 hits.push_back(hit1);
99 hits.push_back(hit2);
100
101 hitRefs.push_back(hit2);
102 hitRefs.push_back(hit1);
103
104 // ---- add some MC particles ----
105 auto mcp0 = ExampleMC();
106 auto mcp1 = ExampleMC();
107 auto mcp2 = ExampleMC();
108 auto mcp3 = ExampleMC();
109 auto mcp4 = ExampleMC();
110 auto mcp5 = ExampleMC();
111 auto mcp6 = ExampleMC();
112 auto mcp7 = ExampleMC();
113 auto mcp8 = ExampleMC();
114 auto mcp9 = ExampleMC();
115
116 mcps.push_back(mcp0);
117 mcps.push_back(mcp1);
118 mcps.push_back(mcp2);
119 mcps.push_back(mcp3);
120 mcps.push_back(mcp4);
121 mcps.push_back(mcp5);
122 mcps.push_back(mcp6);
123 mcps.push_back(mcp7);
124 mcps.push_back(mcp8);
125 mcps.push_back(mcp9);
126
127 auto mcp = mcps[0];
128 mcp.adddaughters(mcps[2]);
129 mcp.adddaughters(mcps[3]);
130 mcp.adddaughters(mcps[4]);
131 mcp.adddaughters(mcps[5]);
132 mcp = mcps[1];
133 mcp.adddaughters(mcps[2]);
134 mcp.adddaughters(mcps[3]);
135 mcp.adddaughters(mcps[4]);
136 mcp.adddaughters(mcps[5]);
137 mcp = mcps[2];
138 mcp.adddaughters(mcps[6]);
139 mcp.adddaughters(mcps[7]);
140 mcp.adddaughters(mcps[8]);
141 mcp.adddaughters(mcps[9]);
142 mcp = mcps[3];
143 mcp.adddaughters(mcps[6]);
144 mcp.adddaughters(mcps[7]);
145 mcp.adddaughters(mcps[8]);
146 mcp.adddaughters(mcps[9]);
147
148 //--- now fix the parent relations
149 // use a range-based for loop here to see if we get mutable objects from the
150 // begin/end iterators
151 for (auto mc : mcps) {
152 for (auto p : mc.daughters()) {
153 int dIndex = p.getObjectID().index;
154 auto d = mcps[dIndex];
155 d.addparents(p);
156 }
157 }
158 //-------- print relations for debugging:
159 for (auto p : mcps) {
160 std::cout << " particle " << p.getObjectID().index << " has daughters: ";
161 for (auto it = p.daughters_begin(), end = p.daughters_end(); it != end; ++it) {
162 std::cout << " " << it->getObjectID().index;
163 }
164 std::cout << " and parents: ";
165 for (auto it = p.parents_begin(), end = p.parents_end(); it != end; ++it) {
166 std::cout << " " << it->getObjectID().index;
167 }
168 std::cout << std::endl;
169
170 // make sure that this does not crash when we do it on an immutable object
171 ExampleMC constP{p};
172 std::cout << "The const particle still has the same relations: daughters: ";
173 for (auto it = constP.daughters_begin(); it != constP.daughters_end(); ++it) {
174 std::cout << " " << it->getObjectID().index;
175 }
176 std::cout << " and parents: ";
177 for (auto it = constP.parents_begin(); it != constP.parents_end(); ++it) {
178 std::cout << " " << it->getObjectID().index;
179 }
180 }
181 //-------------------------------
182
183 // ----------------- create a second MC collection -----------------
184 // Can use it to test subset collections that store elements from multiple
185 // collections
186 for (const auto&& mc : mcps) {
187 moreMCs.push_back(mc.clone());
188 }
189
190 // ----------------- add all "odd" mc particles into a subset collection
191 for (auto p : mcps) {
192 if (p.id() % 2) {
193 mcpsRefs.push_back(p);
194 }
195 }
196 // ----------------- add the "even" counterparts from a different collection
197 for (auto p : moreMCs) {
198 if (p.id() % 2 == 0) {
199 mcpsRefs.push_back(p);
200 }
201 }
202
203 if (mcpsRefs.size() != mcps.size()) {
204 throw std::runtime_error(
205 "The mcParticleRefs collection should now contain as many elements as the mcparticles collection");
206 }
207 //-------------------------------
208
209 auto cluster = MutableExampleCluster();
210 auto clu0 = MutableExampleCluster();
211 auto clu1 = MutableExampleCluster();
212
213 clu0.addHits(hit1);
214 clu0.energy(hit1.energy());
215 clu1.addHits(hit2);
216 clu1.energy(hit2.energy());
217 cluster.addHits(hit1);
218 cluster.addHits(hit2);
219 cluster.energy(hit1.energy() + hit2.energy());
220 cluster.addClusters(clu0);
221 cluster.addClusters(clu1);
222
223 clusters.push_back(clu0);
224 clusters.push_back(clu1);
225 clusters.push_back(cluster);
226
227 auto ref = MutableExampleReferencingType();
228 refs.push_back(ref);
229
230 auto ref2 = ExampleReferencingType();
231 refs2.push_back(ref2);
232
233 ref.addClusters(cluster);
234 ref.addRefs(ref2);
235
236 auto comp = MutableExampleWithComponent();
237 comp.component().data.x = 0;
238 comp.component().data.y = 1;
239 comp.component().data.z = i;
240 comps.push_back(comp);
241
242 auto cyclic = MutableExampleReferencingType();
243 cyclic.addRefs(cyclic);
244 refs.push_back(cyclic);
245
246 auto oneRel = MutableExampleWithOneRelation();
247 oneRel.cluster(cluster);
248 oneRels.push_back(oneRel);
249
250 // write non-filled relation
251 auto oneRelEmpty = ExampleWithOneRelation();
252 oneRels.push_back(oneRelEmpty);
253
254 auto vec = MutableExampleWithVectorMember();
255 vec.addcount(i);
256 vec.addcount(i + 10);
257 vecs.push_back(vec);
258 auto vec1 = MutableExampleWithVectorMember();
259 vec1.addcount(i + 1);
260 vec1.addcount(i + 11);
261 vecs.push_back(vec1);
262
263 for (int j = 0; j < 5; j++) {
264 auto rel = ex42::MutableExampleWithARelation();
265 rel.number(0.5 * j);
266 auto exWithNamesp = ex42::MutableExampleWithNamespace();
267 exWithNamesp.component().x = i;
268 exWithNamesp.component().y = 1000 * i;
269 namesps.push_back(exWithNamesp);
270 if (j != 3) { // also check for empty relations
271 rel.ref(exWithNamesp);
272 for (int k = 0; k < 5; k++) {
273 auto namesp = ex42::MutableExampleWithNamespace();
274 namesp.x(3 * k);
275 namesp.component().y = k;
276 namesps.push_back(namesp);
277 rel.addrefs(namesp);
278 }
279 }
280 namesprels.push_back(rel);
281 }
282 for (auto&& namesprel : namesprels) {
283 cpytest.push_back(namesprel.clone());
284 }
285
286 std::array<int, 4> arrayTest = {0, 0, 2, 3};
287 std::array<int, 4> arrayTest2 = {4, 4, 2 * static_cast<int>(i)};
288 NotSoSimpleStruct a;
289 a.data.p = arrayTest2;
290 ex2::NamespaceStruct nstruct;
291 nstruct.x = static_cast<int>(i);
292 std::array<ex2::NamespaceStruct, 4> structArrayTest = {nstruct, nstruct, nstruct, nstruct};
293 auto array = MutableExampleWithArray(a, arrayTest, arrayTest, arrayTest, arrayTest, structArrayTest);
294 array.myArray(1, i);
295 array.arrayStruct(a);
296 arrays.push_back(array);
297
298 auto maxValues = fixedWidthInts.create();
299 maxValues.fixedI16(std::numeric_limits<std::int16_t>::max()); // 2^(16 - 1) - 1 == 32767
300 maxValues.fixedU32(std::numeric_limits<std::uint32_t>::max()); // 2^32 - 1 == 4294967295
301 maxValues.fixedU64(std::numeric_limits<std::uint64_t>::max()); // 2^64 - 1 == 18446744073709551615
302 auto& maxComp = maxValues.fixedWidthStruct();
303 maxComp.fixedUnsigned16 = std::numeric_limits<std::uint16_t>::max(); // 2^16 - 1 == 65535
304 maxComp.fixedInteger64 = std::numeric_limits<std::int64_t>::max(); // 2^(64 -1) - 1 == 9223372036854775807
305 maxComp.fixedInteger32 = std::numeric_limits<std::int32_t>::max(); // 2^(32 - 1) - 1 == 2147483647
306
307 auto minValues = fixedWidthInts.create();
308 minValues.fixedI16(std::numeric_limits<std::int16_t>::min()); // -2^(16 - 1) == -32768
309 minValues.fixedU32(std::numeric_limits<std::uint32_t>::min()); // 0
310 minValues.fixedU64(std::numeric_limits<std::uint64_t>::min()); // 0
311 auto& minComp = minValues.fixedWidthStruct();
312 minComp.fixedUnsigned16 = std::numeric_limits<std::uint16_t>::min(); // 0
313 minComp.fixedInteger64 = std::numeric_limits<std::int64_t>::min(); // -2^(64 - 1) == -9223372036854775808
314 minComp.fixedInteger32 = std::numeric_limits<std::int32_t>::min(); // -2^(32 - 1) == -2147483648
315
316 auto arbValues = fixedWidthInts.create();
317 arbValues.fixedI16(-12345);
318 arbValues.fixedU32(1234567890);
319 arbValues.fixedU64(1234567890123456789);
320 auto& arbComp = arbValues.fixedWidthStruct();
321 arbComp.fixedUnsigned16 = 12345;
322 arbComp.fixedInteger32 = -1234567890;
323 arbComp.fixedInteger64 = -1234567890123456789ll;
324
325 // add some plain ints as user data
326 auto& uivec = usrInts;
327 uivec.resize(i + 1);
328 int myInt = 0;
329 for (auto& iu : uivec) {
330 iu = myInt++;
331 }
332 // and some user double values
333 unsigned nd = 100;
334 usrDoubles.resize(nd);
335 for (unsigned id = 0; id < nd; ++id) {
336 usrDoubles[id] = 42.;
337 }
338
339 writer.writeEvent();
340 store.clearCollections();
341 }
342
343 writer.finish();
344}
GenericParameters & getEventMetaData() override
return the event meta data for the current event
Definition: EventStore.cc:89
void clearCollections()
empties collections.
Definition: EventStore.cc:119
T & create(const std::string &name)
create a new collection
Definition: EventStore.h:133
GenericParameters & getCollectionMetaData(int colID) override
return the collection meta data for the given colID
Definition: EventStore.cc:109
void setValue(const std::string &key, T value)
Store (a copy of) the passed value under the given key.

Referenced by main().