|
| 1 | +#include <ROOT/REntry.hxx> |
| 2 | +#include <ROOT/RNTupleReader.hxx> |
| 3 | + |
| 4 | +using ROOT::Experimental::REntry; |
| 5 | +using ROOT::Experimental::RNTupleReader; |
| 6 | + |
| 7 | +#include <bitset> |
| 8 | +#include <cstddef> |
| 9 | +#include <fstream> |
| 10 | +#include <ostream> |
| 11 | +#include <string> |
| 12 | +#include <string_view> |
| 13 | + |
| 14 | +template <std::size_t N> |
| 15 | +static void PrintBitsetValue(const REntry &entry, std::string_view name, |
| 16 | + std::ostream &os, bool last = false) { |
| 17 | + auto &value = *entry.GetPtr<std::bitset<N>>(name); |
| 18 | + os << " \"" << name << "\": \"" << value.to_string() << "\""; |
| 19 | + if (!last) { |
| 20 | + os << ","; |
| 21 | + } |
| 22 | + os << "\n"; |
| 23 | +} |
| 24 | + |
| 25 | +void read(std::string_view input = "types.bitset.root", |
| 26 | + std::string_view output = "types.bitset.json") { |
| 27 | + std::ofstream os(std::string{output}); |
| 28 | + os << "[\n"; |
| 29 | + |
| 30 | + auto reader = RNTupleReader::Open("ntpl", input); |
| 31 | + auto &entry = reader->GetModel().GetDefaultEntry(); |
| 32 | + bool first = true; |
| 33 | + for (auto index : *reader) { |
| 34 | + reader->LoadEntry(index); |
| 35 | + |
| 36 | + if (first) { |
| 37 | + first = false; |
| 38 | + } else { |
| 39 | + os << ",\n"; |
| 40 | + } |
| 41 | + os << " {\n"; |
| 42 | + |
| 43 | + PrintBitsetValue<1>(entry, "Bitset1", os); |
| 44 | + PrintBitsetValue<64>(entry, "Bitset64", os); |
| 45 | + PrintBitsetValue<65>(entry, "Bitset65", os, /*last=*/true); |
| 46 | + |
| 47 | + os << " }"; |
| 48 | + // Newline is intentionally missing, may need to print a comma before the |
| 49 | + // next entry. |
| 50 | + } |
| 51 | + os << "\n"; |
| 52 | + os << "]\n"; |
| 53 | +} |
0 commit comments