-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathread.C
More file actions
161 lines (144 loc) · 4.03 KB
/
read.C
File metadata and controls
161 lines (144 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>
using ROOT::Experimental::REntry;
using ROOT::Experimental::RNTupleReader;
#include <cstdint>
#include <fstream>
#include <ostream>
#include <string>
#include <string_view>
#include <tuple>
#include <variant>
#include <vector>
using Tuple_Int32_String = std::tuple<std::int32_t, std::string>;
using VectorInt32 = std::vector<std::int32_t>;
template <typename T> static void PrintValue(const T &value, std::ostream &os);
template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
os << value;
}
template <> void PrintValue(const std::string &value, std::ostream &os) {
os << "\"" << value << "\"";
}
template <> void PrintValue(const VectorInt32 &value, std::ostream &os) {
os << "[";
bool first = true;
for (auto element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n " << element;
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
}
template <> void PrintValue(const Tuple_Int32_String &value, std::ostream &os) {
os << "[\n";
os << " " << std::get<0>(value) << ",\n";
os << " \"" << std::get<1>(value) << "\"\n";
os << " ]";
}
static void PrintTupleValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value =
*entry.GetPtr<std::tuple<std::int32_t, std::string, VectorInt32>>(name);
os << " \"" << name << "\": [\n";
os << " ";
PrintValue(std::get<0>(value), os);
os << ",\n";
os << " ";
PrintValue(std::get<1>(value), os);
os << ",\n";
os << " ";
PrintValue(std::get<2>(value), os);
os << "\n";
os << " ]";
if (!last) {
os << ",";
}
os << "\n";
}
static void PrintVariantValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value =
*entry.GetPtr<std::tuple<std::variant<std::int32_t, std::string>>>(name);
os << " \"" << name << "\": [\n";
os << " ";
auto &variant = std::get<0>(value);
if (variant.index() == 0) {
PrintValue(std::get<std::int32_t>(variant), os);
} else if (variant.index() == 1) {
PrintValue(std::get<std::string>(variant), os);
}
os << "\n";
os << " ]";
if (!last) {
os << ",";
}
os << "\n";
}
static void PrintNestedValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value = *entry.GetPtr<std::tuple<Tuple_Int32_String>>(name);
os << " \"" << name << "\": [\n";
os << " ";
PrintValue(std::get<0>(value), os);
os << "\n";
os << " ]";
if (!last) {
os << ",";
}
os << "\n";
}
static void PrintVectorTupleValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value = *entry.GetPtr<std::vector<Tuple_Int32_String>>(name);
os << " \"" << name << "\": [";
bool first = true;
for (auto &&element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n ";
PrintValue(element, os);
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
if (!last) {
os << ",";
}
os << "\n";
}
void read(std::string_view input = "types.tuple.root",
std::string_view output = "types.tuple.json") {
std::ofstream os(std::string{output});
os << "[\n";
auto reader = RNTupleReader::Open("ntpl", input);
auto &entry = reader->GetModel().GetDefaultEntry();
bool first = true;
for (auto index : *reader) {
reader->LoadEntry(index);
if (first) {
first = false;
} else {
os << ",\n";
}
os << " {\n";
PrintTupleValue(entry, "Int32_String_Vector", os);
PrintVariantValue(entry, "Variant", os);
PrintNestedValue(entry, "Tuple", os);
PrintVectorTupleValue(entry, "VectorTuple", os, /*last=*/true);
os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}