|
| 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 <memory> |
| 15 | +#include <string> |
| 16 | +#include <string_view> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +using Vector = std::vector<std::int32_t>; |
| 20 | + |
| 21 | +static std::shared_ptr<Vector> MakeVectorField(RNTupleModel &model, |
| 22 | + std::string_view name, |
| 23 | + EColumnType indexType) { |
| 24 | + auto field = std::make_unique<RField<Vector>>(name); |
| 25 | + field->SetColumnRepresentatives({{indexType}}); |
| 26 | + model.AddField(std::move(field)); |
| 27 | + return model.GetDefaultEntry().GetPtr<Vector>(name); |
| 28 | +} |
| 29 | + |
| 30 | +template <typename T> |
| 31 | +static void AddProjectedCardinalityField(RNTupleModel &model, |
| 32 | + std::string_view name, |
| 33 | + std::string_view source) { |
| 34 | + auto field = std::make_unique<RField<ROOT::RNTupleCardinality<T>>>(name); |
| 35 | + model.AddProjectedField(std::move(field), [&source](const std::string &) { |
| 36 | + return std::string{source}; |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +void write(std::string_view filename = "projections.cardinality.root") { |
| 41 | + auto model = RNTupleModel::Create(); |
| 42 | + |
| 43 | + // Non-split index encoding |
| 44 | + auto Index32 = MakeVectorField(*model, "Index32", EColumnType::kIndex32); |
| 45 | + auto Index64 = MakeVectorField(*model, "Index64", EColumnType::kIndex64); |
| 46 | + |
| 47 | + // Split index encoding |
| 48 | + auto SplitIndex32 = |
| 49 | + MakeVectorField(*model, "SplitIndex32", EColumnType::kSplitIndex32); |
| 50 | + auto SplitIndex64 = |
| 51 | + MakeVectorField(*model, "SplitIndex64", EColumnType::kSplitIndex64); |
| 52 | + |
| 53 | + // Create RNTupleCardinality projections |
| 54 | + AddProjectedCardinalityField<std::uint32_t>(*model, "Index32Cardinality", |
| 55 | + "Index32"); |
| 56 | + AddProjectedCardinalityField<std::uint64_t>(*model, "Index64Cardinality", |
| 57 | + "Index64"); |
| 58 | + AddProjectedCardinalityField<std::uint32_t>(*model, "SplitIndex32Cardinality", |
| 59 | + "SplitIndex32"); |
| 60 | + AddProjectedCardinalityField<std::uint64_t>(*model, "SplitIndex64Cardinality", |
| 61 | + "SplitIndex64"); |
| 62 | + |
| 63 | + RNTupleWriteOptions options; |
| 64 | + options.SetCompression(0); |
| 65 | + auto writer = |
| 66 | + RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options); |
| 67 | + |
| 68 | + // First entry: single-element vectors, with ascending values |
| 69 | + *Index32 = {1}; |
| 70 | + *Index64 = {2}; |
| 71 | + *SplitIndex32 = {3}; |
| 72 | + *SplitIndex64 = {4}; |
| 73 | + writer->Fill(); |
| 74 | + |
| 75 | + // Second entry: empty vectors |
| 76 | + *Index32 = {}; |
| 77 | + *Index64 = {}; |
| 78 | + *SplitIndex32 = {}; |
| 79 | + *SplitIndex64 = {}; |
| 80 | + writer->Fill(); |
| 81 | + |
| 82 | + // Third entry: increasing number of elements in the vector |
| 83 | + *Index32 = {1}; |
| 84 | + *Index64 = {2, 3}; |
| 85 | + *SplitIndex32 = {4, 5, 6}; |
| 86 | + *SplitIndex64 = {7, 8, 9, 10}; |
| 87 | + writer->Fill(); |
| 88 | +} |
0 commit comments