-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwrite.C
More file actions
54 lines (43 loc) · 1.32 KB
/
write.C
File metadata and controls
54 lines (43 loc) · 1.32 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
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleWriteOptions.hxx>
#include <ROOT/RNTupleWriter.hxx>
using ROOT::Experimental::RNTupleModel;
using ROOT::Experimental::RNTupleWriteOptions;
using ROOT::Experimental::RNTupleWriter;
#include <cstdint>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
using VectorInt32 = std::vector<std::int32_t>;
using Variant = std::variant<std::int32_t, std::string, VectorInt32>;
using Vector = std::vector<std::variant<std::int32_t, std::string>>;
void write(std::string_view filename = "types.variant.root") {
auto model = RNTupleModel::Create();
auto value = model->MakeField<Variant>("f");
auto vector = model->MakeField<Vector>("Vector");
RNTupleWriteOptions options;
options.SetCompression(0);
auto writer =
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
// First entry: std::int32_t
*value = 1;
*vector = {1};
writer->Fill();
// Second entry: std::string
*value = "abc";
*vector = {"abc"};
writer->Fill();
// Third entry: std::vector<std::int32_t>
*value = VectorInt32{1, 2, 3};
*vector = {1, "2", 3};
writer->Fill();
// Fourth entry: empty std::string
*value = "";
*vector = {""};
writer->Fill();
// Fifth entry: empty std::vector
*value = VectorInt32{};
*vector = {};
writer->Fill();
}