PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
read_and_write_associated.cpp File Reference
#include "podio/EventStore.h"
#include "podio/ROOTReader.h"
#include "podio/ROOTWriter.h"
#include "datamodel/EventInfoCollection.h"
#include "datamodel/ExampleClusterCollection.h"
#include "datamodel/ExampleHitCollection.h"
#include <iostream>
#include <string>

Go to the source code of this file.

Functions

void writeCollection ()
 
void readCollection ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 130 of file read_and_write_associated.cpp.

130 {
131
134
135 return 0;
136}
void readCollection()
void writeCollection()

◆ readCollection()

void readCollection ( )

Definition at line 77 of file read_and_write_associated.cpp.

77 {
78
79 // Start reading the input
80 auto reader = podio::ROOTReader();
81 reader.openFile("associations.root");
82
83 auto store = podio::EventStore();
84 store.setReader(&reader);
85
86 const auto nEvents = reader.getEntries();
87
88 for (unsigned i = 0; i < nEvents; ++i) {
89
90 auto& clusters = store.get<ExampleClusterCollection>("clusters");
91 if (clusters.isValid()) {
92 for (const auto& cluster : clusters) {
93 if (cluster.isAvailable()) {
94 for (const auto& hit : cluster.Hits()) {
95 if (hit.isAvailable()) {
96 throw std::runtime_error("Hit is available, although it has not been written");
97 }
98 }
99 }
100 }
101 } else {
102 throw std::runtime_error("Collection 'clusters' should be present");
103 }
104
105 // Test for subset collections
106 auto& hits_subset = store.get<ExampleHitCollection>("hits_subset");
107 if (hits_subset.isValid()) {
108 if (!hits_subset.isSubsetCollection()) {
109 throw std::runtime_error("hits_subset should be a subset collection");
110 }
111
112 if (hits_subset.size() != 2) {
113 throw std::runtime_error("subset collection should have original size");
114 }
115
116 for (const auto& hit : hits_subset) {
117 if (hit.isAvailable()) {
118 throw std::runtime_error("Hit is available, although it has not been written");
119 }
120 }
121 } else {
122 throw std::runtime_error("Collection 'hits_subset' should be present");
123 }
124
125 store.clear();
126 reader.endOfEvent();
127 }
128}

Referenced by main().

◆ writeCollection()

void writeCollection ( )

Definition at line 12 of file read_and_write_associated.cpp.

12 {
13
14 auto store = podio::EventStore();
15 podio::ROOTWriter writer("associations.root", &store);
16
17 std::cout << "start writting collections...\n";
18 auto& info = store.create<EventInfoCollection>("info");
19 auto& hits = store.create<ExampleHitCollection>("hits");
20 auto& clusters = store.create<ExampleClusterCollection>("clusters");
21 auto& hits_subset = store.create<ExampleHitCollection>("hits_subset");
22 hits_subset.setSubsetCollection(true);
23
24 writer.registerForWrite("clusters");
25 // writer.registerForWrite("hits");
26 writer.registerForWrite("hits_subset");
27
28 unsigned nevents = 2;
29
30 for (unsigned i = 0; i < nevents; ++i) {
31
32 auto item1 = MutableEventInfo();
33 item1.Number(i);
34 info.push_back(item1);
35
36 auto& evtMD = store.getEventMetaData();
37 evtMD.setValue("UserEventWeight", (float)100. * i);
38 std::cout << " event number: " << i << std::endl;
39 evtMD.setValue("UserEventName", std::to_string(i));
40
41 auto hit1 = ExampleHit(0xbad, 0., 0., 0., 23. + i);
42 auto hit2 = ExampleHit(0xcaffee, 1., 0., 0., 12. + i);
43
44 hits.push_back(hit1);
45 hits.push_back(hit2);
46
47 // Clusters
48 auto cluster = MutableExampleCluster();
49 auto clu0 = MutableExampleCluster();
50 auto clu1 = MutableExampleCluster();
51
52 clu0.addHits(hit1);
53 clu0.energy(hit1.energy());
54 clu1.addHits(hit2);
55 clu1.energy(hit2.energy());
56 cluster.addHits(hit1);
57 cluster.addHits(hit2);
58 cluster.energy(hit1.energy() + hit2.energy());
59 cluster.addClusters(clu0);
60 cluster.addClusters(clu1);
61
62 // Add tracked hits to subset hits collection
63 hits_subset.push_back(hit1);
64 hits_subset.push_back(hit2);
65
66 // Push all clusters
67 clusters.push_back(clu0);
68 clusters.push_back(clu1);
69 clusters.push_back(cluster);
70
71 writer.writeEvent();
72 store.clearCollections();
73 }
74 writer.finish();
75}

Referenced by main().