|
| 1 | +#include <ROOT/REntry.hxx> |
| 2 | +#include <ROOT/RNTupleReader.hxx> |
| 3 | + |
| 4 | +using ROOT::Experimental::REntry; |
| 5 | +using ROOT::Experimental::RNTupleReader; |
| 6 | + |
| 7 | +#include <TSystem.h> |
| 8 | + |
| 9 | +#include <cstdint> |
| 10 | +#include <fstream> |
| 11 | +#include <ostream> |
| 12 | +#include <string> |
| 13 | +#include <string_view> |
| 14 | +#include <variant> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include "UserClass.hxx" |
| 18 | + |
| 19 | +static void PrintUserMembers(const User &value, std::ostream &os, |
| 20 | + std::string_view indent) { |
| 21 | + os << indent << "\":_0\": {\n"; |
| 22 | + os << indent << " \"fInt\": " << value.fInt << "\n"; |
| 23 | + os << indent << "},\n"; |
| 24 | + os << indent << "\"fString\": \"" << value.fString << "\",\n"; |
| 25 | + |
| 26 | + os << indent << "\"fVariant\": "; |
| 27 | + if (value.fVariant.index() == 0) { |
| 28 | + os << std::get<std::int32_t>(value.fVariant); |
| 29 | + } else if (value.fVariant.index() == 1) { |
| 30 | + os << "\"" << std::get<std::string>(value.fVariant) << "\""; |
| 31 | + } |
| 32 | + os << ",\n"; |
| 33 | + |
| 34 | + os << indent << "\"fVector\": ["; |
| 35 | + bool first = true; |
| 36 | + for (auto element : value.fVector) { |
| 37 | + if (first) { |
| 38 | + first = false; |
| 39 | + } else { |
| 40 | + os << ","; |
| 41 | + } |
| 42 | + os << "\n" << indent << " " << element; |
| 43 | + } |
| 44 | + if (!value.fVector.empty()) { |
| 45 | + os << "\n" << indent; |
| 46 | + } |
| 47 | + os << "],\n"; |
| 48 | + |
| 49 | + os << indent << "\"fNested\": {\n"; |
| 50 | + os << indent << " \"fInt\": " << value.fNested.fInt << "\n"; |
| 51 | + os << indent << "}\n"; |
| 52 | +} |
| 53 | + |
| 54 | +static void PrintUserValue(const REntry &entry, std::string_view name, |
| 55 | + std::ostream &os, bool last = false) { |
| 56 | + auto &value = *entry.GetPtr<User>(name); |
| 57 | + os << " \"" << name << "\": {\n"; |
| 58 | + PrintUserMembers(value, os, " "); |
| 59 | + os << " }"; |
| 60 | + if (!last) { |
| 61 | + os << ","; |
| 62 | + } |
| 63 | + os << "\n"; |
| 64 | +} |
| 65 | + |
| 66 | +static void PrintUserVector(const REntry &entry, std::string_view name, |
| 67 | + std::ostream &os, bool last = false) { |
| 68 | + auto &vector = *entry.GetPtr<std::vector<User>>(name); |
| 69 | + os << " \"" << name << "\": ["; |
| 70 | + bool first = true; |
| 71 | + for (const auto &element : vector) { |
| 72 | + if (first) { |
| 73 | + first = false; |
| 74 | + } else { |
| 75 | + os << ","; |
| 76 | + } |
| 77 | + os << "\n {\n"; |
| 78 | + PrintUserMembers(element, os, " "); |
| 79 | + os << " }"; |
| 80 | + } |
| 81 | + if (!vector.empty()) { |
| 82 | + os << "\n "; |
| 83 | + } |
| 84 | + os << "]"; |
| 85 | + if (!last) { |
| 86 | + os << ","; |
| 87 | + } |
| 88 | + os << "\n"; |
| 89 | +} |
| 90 | + |
| 91 | +void read(std::string_view input = "types.user.class.root", |
| 92 | + std::string_view output = "types.user.class.json") { |
| 93 | + if (gSystem->Load("libUserClass") == -1) |
| 94 | + throw std::runtime_error("could not find the required ROOT dictionaries, " |
| 95 | + "please make sure to run `make` first"); |
| 96 | + |
| 97 | + std::ofstream os(std::string{output}); |
| 98 | + os << "[\n"; |
| 99 | + |
| 100 | + auto reader = RNTupleReader::Open("ntpl", input); |
| 101 | + auto &entry = reader->GetModel().GetDefaultEntry(); |
| 102 | + bool first = true; |
| 103 | + for (auto index : *reader) { |
| 104 | + reader->LoadEntry(index); |
| 105 | + |
| 106 | + if (first) { |
| 107 | + first = false; |
| 108 | + } else { |
| 109 | + os << ",\n"; |
| 110 | + } |
| 111 | + os << " {\n"; |
| 112 | + |
| 113 | + PrintUserValue(entry, "f", os); |
| 114 | + PrintUserVector(entry, "Vector", os, /*last=*/true); |
| 115 | + |
| 116 | + os << " }"; |
| 117 | + // Newline is intentionally missing, may need to print a comma before the |
| 118 | + // next entry. |
| 119 | + } |
| 120 | + os << "\n"; |
| 121 | + os << "]\n"; |
| 122 | +} |
0 commit comments