|
| 1 | +#include <ROOT/REntry.hxx> |
| 2 | +#include <ROOT/RNTupleReader.hxx> |
| 3 | + |
| 4 | +using ROOT::Experimental::REntry; |
| 5 | +using ROOT::Experimental::RNTupleReader; |
| 6 | + |
| 7 | +#include <atomic> |
| 8 | +#include <cstddef> // for std::byte |
| 9 | +#include <cstdint> |
| 10 | +#include <fstream> |
| 11 | +#include <ostream> |
| 12 | +#include <string> |
| 13 | +#include <string_view> |
| 14 | + |
| 15 | +template <typename T> |
| 16 | +static void PrintIntegerValue(const REntry &entry, std::string_view name, |
| 17 | + std::ostream &os, bool last = false) { |
| 18 | + T value = *entry.GetPtr<std::atomic<T>>(name); |
| 19 | + os << " \"" << name << "\": "; |
| 20 | + // We want to print the integer value even if it is a character; use the unary |
| 21 | + // + operator (https://stackoverflow.com/a/28414758). |
| 22 | + os << +value; |
| 23 | + if (!last) { |
| 24 | + os << ","; |
| 25 | + } |
| 26 | + os << "\n"; |
| 27 | +} |
| 28 | + |
| 29 | +template <typename T> |
| 30 | +static void PrintRealValue(const REntry &entry, std::string_view name, |
| 31 | + std::ostream &os, bool last = false) { |
| 32 | + T value = *entry.GetPtr<std::atomic<T>>(name); |
| 33 | + os << " \"" << name << "\": \"" << value << "\""; |
| 34 | + if (!last) { |
| 35 | + os << ","; |
| 36 | + } |
| 37 | + os << "\n"; |
| 38 | +} |
| 39 | + |
| 40 | +void read(std::string_view input = "types.atomic.root", |
| 41 | + std::string_view output = "types.atomic.json") { |
| 42 | + std::ofstream os(std::string{output}); |
| 43 | + // Print floating-point numbers as hexadecimal literals. |
| 44 | + os << std::hexfloat; |
| 45 | + os << "[\n"; |
| 46 | + |
| 47 | + auto reader = RNTupleReader::Open("ntpl", input); |
| 48 | + auto &entry = reader->GetModel().GetDefaultEntry(); |
| 49 | + bool first = true; |
| 50 | + for (auto index : *reader) { |
| 51 | + reader->LoadEntry(index); |
| 52 | + |
| 53 | + if (first) { |
| 54 | + first = false; |
| 55 | + } else { |
| 56 | + os << ",\n"; |
| 57 | + } |
| 58 | + os << " {\n"; |
| 59 | + |
| 60 | + PrintIntegerValue<bool>(entry, "Bit", os); |
| 61 | + os << " \"Byte\": "; |
| 62 | + os << std::to_integer<int>(*entry.GetPtr<std::atomic<std::byte>>("Byte")); |
| 63 | + os << ",\n"; |
| 64 | + PrintIntegerValue<char>(entry, "Char", os); |
| 65 | + PrintIntegerValue<std::int8_t>(entry, "Int8", os); |
| 66 | + PrintIntegerValue<std::uint8_t>(entry, "UInt8", os); |
| 67 | + PrintIntegerValue<std::int16_t>(entry, "Int16", os); |
| 68 | + PrintIntegerValue<std::uint16_t>(entry, "UInt16", os); |
| 69 | + PrintIntegerValue<std::int32_t>(entry, "Int32", os); |
| 70 | + PrintIntegerValue<std::uint32_t>(entry, "UInt32", os); |
| 71 | + PrintIntegerValue<std::int64_t>(entry, "Int64", os); |
| 72 | + PrintIntegerValue<std::uint64_t>(entry, "UInt64", os); |
| 73 | + PrintRealValue<float>(entry, "Real32", os); |
| 74 | + PrintRealValue<double>(entry, "Real64", os, /*last=*/true); |
| 75 | + |
| 76 | + os << " }"; |
| 77 | + // Newline is intentionally missing, may need to print a comma before the |
| 78 | + // next entry. |
| 79 | + } |
| 80 | + os << "\n"; |
| 81 | + os << "]\n"; |
| 82 | +} |
0 commit comments