|
| 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 <variant> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +using VariantTy = std::variant<std::int32_t, std::string>; |
| 16 | +using VectorTy = 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 VariantTy &value, std::ostream &os) { |
| 29 | + if (value.index() == 0) { |
| 30 | + PrintValue(std::get<std::int32_t>(value), os); |
| 31 | + } else if (value.index() == 1) { |
| 32 | + PrintValue(std::get<std::string>(value), os); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +template <> void PrintValue(const VectorTy &value, std::ostream &os) { |
| 37 | + os << "["; |
| 38 | + bool first = true; |
| 39 | + for (auto element : value) { |
| 40 | + if (first) { |
| 41 | + first = false; |
| 42 | + } else { |
| 43 | + os << ","; |
| 44 | + } |
| 45 | + os << "\n " << element; |
| 46 | + } |
| 47 | + if (!value.empty()) { |
| 48 | + os << "\n "; |
| 49 | + } |
| 50 | + os << "]"; |
| 51 | +} |
| 52 | + |
| 53 | +template <typename T> |
| 54 | +static void PrintUniquePtrValue(const REntry &entry, std::string_view name, |
| 55 | + std::ostream &os, bool last = false) { |
| 56 | + auto &value = *entry.GetPtr<std::unique_ptr<T>>(name); |
| 57 | + os << " \"" << name << "\": "; |
| 58 | + if (!value) { |
| 59 | + os << "null"; |
| 60 | + } else { |
| 61 | + PrintValue(*value, os); |
| 62 | + } |
| 63 | + if (!last) { |
| 64 | + os << ","; |
| 65 | + } |
| 66 | + os << "\n"; |
| 67 | +} |
| 68 | + |
| 69 | +void read(std::string_view input = "types.unique_ptr.root", |
| 70 | + std::string_view output = "types.unique_ptr.json") { |
| 71 | + std::ofstream os(std::string{output}); |
| 72 | + os << "[\n"; |
| 73 | + |
| 74 | + auto reader = RNTupleReader::Open("ntpl", input); |
| 75 | + auto &entry = reader->GetModel().GetDefaultEntry(); |
| 76 | + bool first = true; |
| 77 | + for (auto index : *reader) { |
| 78 | + reader->LoadEntry(index); |
| 79 | + |
| 80 | + if (first) { |
| 81 | + first = false; |
| 82 | + } else { |
| 83 | + os << ",\n"; |
| 84 | + } |
| 85 | + os << " {\n"; |
| 86 | + |
| 87 | + PrintUniquePtrValue<std::int32_t>(entry, "Int32", os); |
| 88 | + PrintUniquePtrValue<std::string>(entry, "String", os); |
| 89 | + PrintUniquePtrValue<VariantTy>(entry, "Variant", os); |
| 90 | + PrintUniquePtrValue<VectorTy>(entry, "Vector", os, /*last=*/true); |
| 91 | + |
| 92 | + os << " }"; |
| 93 | + // Newline is intentionally missing, may need to print a comma before the |
| 94 | + // next entry. |
| 95 | + } |
| 96 | + os << "\n"; |
| 97 | + os << "]\n"; |
| 98 | +} |
0 commit comments