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