|
| 1 | +#include <ROOT/REntry.hxx> |
| 2 | +#include <ROOT/RNTupleReader.hxx> |
| 3 | +#include <ROOT/RNTupleUtil.hxx> |
| 4 | + |
| 5 | +using ROOT::Experimental::REntry; |
| 6 | +using ROOT::Experimental::RNTupleReader; |
| 7 | + |
| 8 | +#include <cstdint> |
| 9 | +#include <fstream> |
| 10 | +#include <ostream> |
| 11 | +#include <string> |
| 12 | +#include <string_view> |
| 13 | +#include <utility> |
| 14 | + |
| 15 | +template <typename T> static void PrintValue(const T &value, std::ostream &os); |
| 16 | + |
| 17 | +template <> void PrintValue(const std::int32_t &value, std::ostream &os) { |
| 18 | + os << value; |
| 19 | +} |
| 20 | + |
| 21 | +template <> void PrintValue(const float &value, std::ostream &os) { |
| 22 | + os << "\"" << value << "\""; |
| 23 | +} |
| 24 | + |
| 25 | +template <typename T, typename U> |
| 26 | +static void PrintPairValue(const REntry &entry, std::string_view name, |
| 27 | + std::ostream &os, bool last = false) { |
| 28 | + auto &value = *entry.GetPtr<std::pair<T, U>>(name); |
| 29 | + os << " \"" << name << "\": [\n"; |
| 30 | + os << " "; |
| 31 | + PrintValue(value.first, os); |
| 32 | + os << ",\n"; |
| 33 | + os << " "; |
| 34 | + PrintValue(value.second, os); |
| 35 | + os << "\n"; |
| 36 | + os << " ]"; |
| 37 | + if (!last) { |
| 38 | + os << ","; |
| 39 | + } |
| 40 | + os << "\n"; |
| 41 | +} |
| 42 | + |
| 43 | +template <typename T> |
| 44 | +static void PrintValue(const REntry &entry, std::string_view name, |
| 45 | + std::ostream &os, bool last = false) { |
| 46 | + T value = *entry.GetPtr<T>(name); |
| 47 | + os << " \"" << name << "\": "; |
| 48 | + PrintValue(value, os); |
| 49 | + if (!last) { |
| 50 | + os << ","; |
| 51 | + } |
| 52 | + os << "\n"; |
| 53 | +} |
| 54 | + |
| 55 | +void read(std::string_view input = "projections.leaf.root", |
| 56 | + std::string_view output = "projections.leaf.json") { |
| 57 | + std::ofstream os(std::string{output}); |
| 58 | + // Print floating-point numbers as hexadecimal literals. |
| 59 | + os << std::hexfloat; |
| 60 | + os << "[\n"; |
| 61 | + |
| 62 | + auto reader = RNTupleReader::Open("ntpl", input); |
| 63 | + auto &entry = reader->GetModel().GetDefaultEntry(); |
| 64 | + bool first = true; |
| 65 | + for (auto index : *reader) { |
| 66 | + reader->LoadEntry(index); |
| 67 | + |
| 68 | + if (first) { |
| 69 | + first = false; |
| 70 | + } else { |
| 71 | + os << ",\n"; |
| 72 | + } |
| 73 | + os << " {\n"; |
| 74 | + |
| 75 | + PrintPairValue<std::int32_t, float>(entry, "Pair", os); |
| 76 | + PrintValue<std::int32_t>(entry, "Int32", os); |
| 77 | + PrintValue<float>(entry, "Float", os, /*last=*/true); |
| 78 | + |
| 79 | + os << " }"; |
| 80 | + // Newline is intentionally missing, may need to print a comma before the |
| 81 | + // next entry. |
| 82 | + } |
| 83 | + os << "\n"; |
| 84 | + os << "]\n"; |
| 85 | +} |
0 commit comments