|
| 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 <TSystem.h> |
| 14 | + |
| 15 | +#include <cstdint> |
| 16 | +#include <filesystem> |
| 17 | +#include <memory> |
| 18 | +#include <string_view> |
| 19 | +#include <unordered_map> |
| 20 | + |
| 21 | +using UnorderedMultimap = std::unordered_multimap<std::string, |
| 22 | + std::unordered_multimap<std::string, std::int32_t>>; |
| 23 | + |
| 24 | +static std::shared_ptr<UnorderedMultimap> MakeUnorderedMultimapField(RNTupleModel &model, |
| 25 | + std::string_view name, |
| 26 | + EColumnType indexType) { |
| 27 | + auto field = std::make_unique<RField<UnorderedMultimap>>(name); |
| 28 | + field->SetColumnRepresentatives({{indexType}}); |
| 29 | + field->GetSubFields()[0]->GetSubFields()[1]->SetColumnRepresentatives( |
| 30 | + {{indexType}}); |
| 31 | + model.AddField(std::move(field)); |
| 32 | + return model.GetDefaultEntry().GetPtr<UnorderedMultimap>(name); |
| 33 | +} |
| 34 | + |
| 35 | +void write(std::string_view filename = "types.unordered_multimap.nested.root") { |
| 36 | + if (gSystem->Load("libNestedUnorderedMultimap") == -1) |
| 37 | + throw std::runtime_error("could not find the required ROOT dictionaries, " |
| 38 | + "please make sure to run `make` first"); |
| 39 | + |
| 40 | + auto model = RNTupleModel::Create(); |
| 41 | + |
| 42 | + // Non-split index encoding |
| 43 | + auto Index32 = MakeUnorderedMultimapField(*model, "Index32", EColumnType::kIndex32); |
| 44 | + auto Index64 = MakeUnorderedMultimapField(*model, "Index64", EColumnType::kIndex64); |
| 45 | + |
| 46 | + // Split index encoding |
| 47 | + auto SplitIndex32 = |
| 48 | + MakeUnorderedMultimapField(*model, "SplitIndex32", EColumnType::kSplitIndex32); |
| 49 | + auto SplitIndex64 = |
| 50 | + MakeUnorderedMultimapField(*model, "SplitIndex64", EColumnType::kSplitIndex64); |
| 51 | + |
| 52 | + RNTupleWriteOptions options; |
| 53 | + options.SetCompression(0); |
| 54 | + auto writer = |
| 55 | + RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options); |
| 56 | + |
| 57 | + // First entry: single-element maps, with ascending values |
| 58 | + *Index32 = {{"a", {{"aa", 1}}}}; |
| 59 | + *Index64 = {{"b", {{"bb", 2}}}}; |
| 60 | + *SplitIndex32 = {{"c", {{"cc", 3}}}}; |
| 61 | + *SplitIndex64 = {{"d", {{"dd", 4}}}}; |
| 62 | + writer->Fill(); |
| 63 | + |
| 64 | + // Second entry: empty maps |
| 65 | + *Index32 = {}; |
| 66 | + *Index64 = {}; |
| 67 | + *SplitIndex32 = {}; |
| 68 | + *SplitIndex64 = {}; |
| 69 | + writer->Fill(); |
| 70 | + |
| 71 | + // Third entry: increasing number of elements in the outer map |
| 72 | + *Index32 = {{"a", {{"aa", 1}}}}; |
| 73 | + *Index64 = {{"b", {{"ba", 2}}}, {"c", {{"ca", 3}}}}; |
| 74 | + *SplitIndex32 = { |
| 75 | + {"d", {{"da", 4}}}, {"e", {{"ea", 5}, {"eb", 6}}}, {"f", {}}}; |
| 76 | + *SplitIndex64 = {{"g", {{"ga", 7}, {"gb", 8}}}, |
| 77 | + {"h", {}}, |
| 78 | + {"i", {{"ia", 9}}}, |
| 79 | + {"j", {{"ja", 10}, {"jb", 11}, {"jc", 12}}}}; |
| 80 | + writer->Fill(); |
| 81 | +} |
0 commit comments