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