|
| 1 | +#include <ROOT/RField.hxx> |
| 2 | +#include <ROOT/RNTupleModel.hxx> |
| 3 | +#include <ROOT/RNTupleUtil.hxx> |
| 4 | +#include <ROOT/RNTupleWriteOptions.hxx> |
| 5 | +#include <ROOT/RNTupleWriter.hxx> |
| 6 | + |
| 7 | +using ROOT::Experimental::EColumnType; |
| 8 | +using ROOT::Experimental::RField; |
| 9 | +using ROOT::Experimental::RNTupleModel; |
| 10 | +using ROOT::Experimental::RNTupleWriteOptions; |
| 11 | +using ROOT::Experimental::RNTupleWriter; |
| 12 | + |
| 13 | +#include <cstdint> |
| 14 | +#include <limits> |
| 15 | +#include <memory> |
| 16 | +#include <string_view> |
| 17 | + |
| 18 | +template <typename T> |
| 19 | +static std::shared_ptr<T> MakeFundamentalField(RNTupleModel &model, |
| 20 | + std::string_view name, |
| 21 | + EColumnType type) { |
| 22 | + auto field = std::make_unique<RField<T>>(name); |
| 23 | + field->SetColumnRepresentatives({{type}}); |
| 24 | + model.AddField(std::move(field)); |
| 25 | + return model.GetDefaultEntry().GetPtr<T>(name); |
| 26 | +} |
| 27 | + |
| 28 | +void write(std::string_view filename = "types.fundamental.integer.root") { |
| 29 | + auto model = RNTupleModel::Create(); |
| 30 | + |
| 31 | + auto Int8 = |
| 32 | + MakeFundamentalField<std::int8_t>(*model, "Int8", EColumnType::kInt8); |
| 33 | + auto UInt8 = |
| 34 | + MakeFundamentalField<std::uint8_t>(*model, "UInt8", EColumnType::kUInt8); |
| 35 | + |
| 36 | + // Non-split integer encoding |
| 37 | + auto Int16 = |
| 38 | + MakeFundamentalField<std::int16_t>(*model, "Int16", EColumnType::kInt16); |
| 39 | + auto UInt16 = MakeFundamentalField<std::uint16_t>(*model, "UInt16", |
| 40 | + EColumnType::kUInt16); |
| 41 | + auto Int32 = |
| 42 | + MakeFundamentalField<std::int32_t>(*model, "Int32", EColumnType::kInt32); |
| 43 | + auto UInt32 = MakeFundamentalField<std::uint32_t>(*model, "UInt32", |
| 44 | + EColumnType::kUInt32); |
| 45 | + auto Int64 = |
| 46 | + MakeFundamentalField<std::int64_t>(*model, "Int64", EColumnType::kInt64); |
| 47 | + auto UInt64 = MakeFundamentalField<std::uint64_t>(*model, "UInt64", |
| 48 | + EColumnType::kUInt64); |
| 49 | + |
| 50 | + // Split integer encoding |
| 51 | + auto SplitInt16 = MakeFundamentalField<std::int16_t>( |
| 52 | + *model, "SplitInt16", EColumnType::kSplitInt16); |
| 53 | + auto SplitUInt16 = MakeFundamentalField<std::uint16_t>( |
| 54 | + *model, "SplitUInt16", EColumnType::kSplitUInt16); |
| 55 | + auto SplitInt32 = MakeFundamentalField<std::int32_t>( |
| 56 | + *model, "SplitInt32", EColumnType::kSplitInt32); |
| 57 | + auto SplitUInt32 = MakeFundamentalField<std::uint32_t>( |
| 58 | + *model, "SplitUInt32", EColumnType::kSplitUInt32); |
| 59 | + auto SplitInt64 = MakeFundamentalField<std::int64_t>( |
| 60 | + *model, "SplitInt64", EColumnType::kSplitInt64); |
| 61 | + auto SplitUInt64 = MakeFundamentalField<std::uint64_t>( |
| 62 | + *model, "SplitUInt64", EColumnType::kSplitUInt64); |
| 63 | + |
| 64 | + RNTupleWriteOptions options; |
| 65 | + options.SetCompression(0); |
| 66 | + auto writer = |
| 67 | + RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options); |
| 68 | + |
| 69 | + // First entry: ascending values |
| 70 | + *Int8 = 1; |
| 71 | + *UInt8 = 2; |
| 72 | + *Int16 = 3; |
| 73 | + *UInt16 = 4; |
| 74 | + *Int32 = 5; |
| 75 | + *UInt32 = 6; |
| 76 | + *Int64 = 7; |
| 77 | + *UInt64 = 8; |
| 78 | + *SplitInt16 = 9; |
| 79 | + *SplitUInt16 = 10; |
| 80 | + *SplitInt32 = 11; |
| 81 | + *SplitUInt32 = 12; |
| 82 | + *SplitInt64 = 13; |
| 83 | + *SplitUInt64 = 14; |
| 84 | + writer->Fill(); |
| 85 | + |
| 86 | + // Second entry: values in each byte (to validate split encoding) |
| 87 | + static constexpr std::uint8_t Value8 = 0x12; // = 18 |
| 88 | + static constexpr std::uint16_t Value16 = 0x1234; // = 4660 |
| 89 | + static constexpr std::uint32_t Value32 = 0x12345678; // = 305419896 |
| 90 | + static constexpr std::uint64_t Value64 = |
| 91 | + 0x123456780112358c; // = 1311768464885691788 |
| 92 | + *Int8 = Value8; |
| 93 | + *UInt8 = Value8; |
| 94 | + *Int16 = Value16; |
| 95 | + *UInt16 = Value16; |
| 96 | + *Int32 = Value32; |
| 97 | + *UInt32 = Value32; |
| 98 | + *Int64 = Value64; |
| 99 | + *UInt64 = Value64; |
| 100 | + *SplitInt16 = Value16; |
| 101 | + *SplitUInt16 = Value16; |
| 102 | + *SplitInt32 = Value32; |
| 103 | + *SplitUInt32 = Value32; |
| 104 | + *SplitInt64 = Value64; |
| 105 | + *SplitUInt64 = Value64; |
| 106 | + writer->Fill(); |
| 107 | + |
| 108 | + // Third entry: negative values for signed integer types (to validate zigzag |
| 109 | + // encoding) |
| 110 | + static constexpr std::int8_t NegativeValue8 = 0x92; // = -110 |
| 111 | + static constexpr std::int16_t NegativeValue16 = 0x9234; // = -28108 |
| 112 | + static constexpr std::int32_t NegativeValue32 = 0x92345678; // = -1842063752 |
| 113 | + static constexpr std::int64_t NegativeValue64 = |
| 114 | + 0x923456780112358c; // = -7911603571969084020 |
| 115 | + *Int8 = NegativeValue8; |
| 116 | + *UInt8 = Value8; |
| 117 | + *Int16 = NegativeValue16; |
| 118 | + *UInt16 = Value16; |
| 119 | + *Int32 = NegativeValue32; |
| 120 | + *UInt32 = Value32; |
| 121 | + *Int64 = NegativeValue64; |
| 122 | + *UInt64 = Value64; |
| 123 | + *SplitInt16 = NegativeValue16; |
| 124 | + *SplitUInt16 = Value16; |
| 125 | + *SplitInt32 = NegativeValue32; |
| 126 | + *SplitUInt32 = Value32; |
| 127 | + *SplitInt64 = NegativeValue64; |
| 128 | + *SplitUInt64 = Value64; |
| 129 | + writer->Fill(); |
| 130 | + |
| 131 | + // Fourth entry: minimum (lowest) values |
| 132 | + *Int8 = std::numeric_limits<std::int8_t>::lowest(); |
| 133 | + *UInt8 = std::numeric_limits<std::uint8_t>::lowest(); |
| 134 | + *Int16 = std::numeric_limits<std::int16_t>::lowest(); |
| 135 | + *UInt16 = std::numeric_limits<std::uint16_t>::lowest(); |
| 136 | + *Int32 = std::numeric_limits<std::int32_t>::lowest(); |
| 137 | + *UInt32 = std::numeric_limits<std::uint32_t>::lowest(); |
| 138 | + *Int64 = std::numeric_limits<std::int64_t>::lowest(); |
| 139 | + *UInt64 = std::numeric_limits<std::uint64_t>::lowest(); |
| 140 | + *SplitInt16 = std::numeric_limits<std::int16_t>::lowest(); |
| 141 | + *SplitUInt16 = std::numeric_limits<std::uint16_t>::lowest(); |
| 142 | + *SplitInt32 = std::numeric_limits<std::int32_t>::lowest(); |
| 143 | + *SplitUInt32 = std::numeric_limits<std::uint32_t>::lowest(); |
| 144 | + *SplitInt64 = std::numeric_limits<std::int64_t>::lowest(); |
| 145 | + *SplitUInt64 = std::numeric_limits<std::uint64_t>::lowest(); |
| 146 | + writer->Fill(); |
| 147 | + |
| 148 | + // Fifth entry: maximum values |
| 149 | + *Int8 = std::numeric_limits<std::int8_t>::max(); |
| 150 | + *UInt8 = std::numeric_limits<std::uint8_t>::max(); |
| 151 | + *Int16 = std::numeric_limits<std::int16_t>::max(); |
| 152 | + *UInt16 = std::numeric_limits<std::uint16_t>::max(); |
| 153 | + *Int32 = std::numeric_limits<std::int32_t>::max(); |
| 154 | + *UInt32 = std::numeric_limits<std::uint32_t>::max(); |
| 155 | + *Int64 = std::numeric_limits<std::int64_t>::max(); |
| 156 | + *UInt64 = std::numeric_limits<std::uint64_t>::max(); |
| 157 | + *SplitInt16 = std::numeric_limits<std::int16_t>::max(); |
| 158 | + *SplitUInt16 = std::numeric_limits<std::uint16_t>::max(); |
| 159 | + *SplitInt32 = std::numeric_limits<std::int32_t>::max(); |
| 160 | + *SplitUInt32 = std::numeric_limits<std::uint32_t>::max(); |
| 161 | + *SplitInt64 = std::numeric_limits<std::int64_t>::max(); |
| 162 | + *SplitUInt64 = std::numeric_limits<std::uint64_t>::max(); |
| 163 | + writer->Fill(); |
| 164 | +} |
0 commit comments