|
| 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 <memory> |
| 14 | +#include <string> |
| 15 | +#include <string_view> |
| 16 | + |
| 17 | +static std::shared_ptr<std::string> MakeStringField(RNTupleModel &model, |
| 18 | + std::string_view name, |
| 19 | + EColumnType indexType) { |
| 20 | + auto field = std::make_unique<RField<std::string>>(name); |
| 21 | + field->SetColumnRepresentatives({{indexType, EColumnType::kChar}}); |
| 22 | + model.AddField(std::move(field)); |
| 23 | + return model.GetDefaultEntry().GetPtr<std::string>(name); |
| 24 | +} |
| 25 | + |
| 26 | +void write(std::string_view filename = "types.string.root") { |
| 27 | + auto model = RNTupleModel::Create(); |
| 28 | + |
| 29 | + // Non-split index encoding |
| 30 | + auto Index32 = MakeStringField(*model, "Index32", EColumnType::kIndex32); |
| 31 | + auto Index64 = MakeStringField(*model, "Index64", EColumnType::kIndex64); |
| 32 | + |
| 33 | + // Split index encoding |
| 34 | + auto SplitIndex32 = |
| 35 | + MakeStringField(*model, "SplitIndex32", EColumnType::kSplitIndex32); |
| 36 | + auto SplitIndex64 = |
| 37 | + MakeStringField(*model, "SplitIndex64", EColumnType::kSplitIndex64); |
| 38 | + |
| 39 | + RNTupleWriteOptions options; |
| 40 | + options.SetCompression(0); |
| 41 | + auto writer = |
| 42 | + RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options); |
| 43 | + |
| 44 | + // First entry: one character strings, with ascending values |
| 45 | + *Index32 = "a"; |
| 46 | + *Index64 = "b"; |
| 47 | + *SplitIndex32 = "c"; |
| 48 | + *SplitIndex64 = "d"; |
| 49 | + writer->Fill(); |
| 50 | + |
| 51 | + // Second entry: empty strings |
| 52 | + *Index32 = ""; |
| 53 | + *Index64 = ""; |
| 54 | + *SplitIndex32 = ""; |
| 55 | + *SplitIndex64 = ""; |
| 56 | + writer->Fill(); |
| 57 | + |
| 58 | + // Third entry: increasing length of strings |
| 59 | + *Index32 = "a"; |
| 60 | + *Index64 = "bc"; |
| 61 | + *SplitIndex32 = "def"; |
| 62 | + *SplitIndex64 = "ghij"; |
| 63 | + writer->Fill(); |
| 64 | +} |
0 commit comments