Skip to content

Commit 2b32351

Browse files
committed
Add tests for projections of leaf fields
Closes #26
1 parent 98d43b4 commit 2b32351

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

projections/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Projections
22

33
* [`cardinality`](cardinality): `ROOT::RNTupleCardinality`
4+
* [`leaf`](leaf): of leaf fields

projections/leaf/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Projections of Leaf Fields
2+
3+
## Fields
4+
5+
* `Pair` of type `std::pair<std::int32_t, float>`
6+
* `Int32` and `Float` projected fields of types `std::int32_t` and `float`
7+
8+
## Entries
9+
10+
1. Ascending values

projections/leaf/read.C

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <ROOT/REntry.hxx>
2+
#include <ROOT/RNTupleReader.hxx>
3+
#include <ROOT/RNTupleUtil.hxx>
4+
5+
using ROOT::Experimental::REntry;
6+
using ROOT::Experimental::RNTupleReader;
7+
8+
#include <cstdint>
9+
#include <fstream>
10+
#include <ostream>
11+
#include <string>
12+
#include <string_view>
13+
#include <utility>
14+
15+
template <typename T> static void PrintValue(const T &value, std::ostream &os);
16+
17+
template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
18+
os << value;
19+
}
20+
21+
template <> void PrintValue(const float &value, std::ostream &os) {
22+
os << "\"" << value << "\"";
23+
}
24+
25+
template <typename T, typename U>
26+
static void PrintPairValue(const REntry &entry, std::string_view name,
27+
std::ostream &os, bool last = false) {
28+
auto &value = *entry.GetPtr<std::pair<T, U>>(name);
29+
os << " \"" << name << "\": [\n";
30+
os << " ";
31+
PrintValue(value.first, os);
32+
os << ",\n";
33+
os << " ";
34+
PrintValue(value.second, os);
35+
os << "\n";
36+
os << " ]";
37+
if (!last) {
38+
os << ",";
39+
}
40+
os << "\n";
41+
}
42+
43+
template <typename T>
44+
static void PrintValue(const REntry &entry, std::string_view name,
45+
std::ostream &os, bool last = false) {
46+
T value = *entry.GetPtr<T>(name);
47+
os << " \"" << name << "\": ";
48+
PrintValue(value, os);
49+
if (!last) {
50+
os << ",";
51+
}
52+
os << "\n";
53+
}
54+
55+
void read(std::string_view input = "projections.leaf.root",
56+
std::string_view output = "projections.leaf.json") {
57+
std::ofstream os(std::string{output});
58+
// Print floating-point numbers as hexadecimal literals.
59+
os << std::hexfloat;
60+
os << "[\n";
61+
62+
auto reader = RNTupleReader::Open("ntpl", input);
63+
auto &entry = reader->GetModel().GetDefaultEntry();
64+
bool first = true;
65+
for (auto index : *reader) {
66+
reader->LoadEntry(index);
67+
68+
if (first) {
69+
first = false;
70+
} else {
71+
os << ",\n";
72+
}
73+
os << " {\n";
74+
75+
PrintPairValue<std::int32_t, float>(entry, "Pair", os);
76+
PrintValue<std::int32_t>(entry, "Int32", os);
77+
PrintValue<float>(entry, "Float", os, /*last=*/true);
78+
79+
os << " }";
80+
// Newline is intentionally missing, may need to print a comma before the
81+
// next entry.
82+
}
83+
os << "\n";
84+
os << "]\n";
85+
}

projections/leaf/write.C

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <ROOT/RField.hxx>
2+
#include <ROOT/RNTupleModel.hxx>
3+
#include <ROOT/RNTupleWriteOptions.hxx>
4+
#include <ROOT/RNTupleWriter.hxx>
5+
6+
using ROOT::Experimental::RField;
7+
using ROOT::Experimental::RNTupleModel;
8+
using ROOT::Experimental::RNTupleWriteOptions;
9+
using ROOT::Experimental::RNTupleWriter;
10+
11+
#include <cstdint>
12+
#include <memory>
13+
#include <string>
14+
#include <string_view>
15+
#include <utility>
16+
17+
template <typename T>
18+
static void AddProjectedField(RNTupleModel &model, std::string_view name,
19+
std::string_view source) {
20+
auto field = std::make_unique<RField<T>>(name);
21+
model.AddProjectedField(std::move(field), [&source](const std::string &) {
22+
return std::string{source};
23+
});
24+
}
25+
26+
void write(std::string_view filename = "projections.leaf.root") {
27+
auto model = RNTupleModel::Create();
28+
29+
auto Pair = model->MakeField<std::pair<std::int32_t, float>>("Pair");
30+
AddProjectedField<std::int32_t>(*model, "Int32", "Pair._0");
31+
AddProjectedField<float>(*model, "Float", "Pair._1");
32+
33+
RNTupleWriteOptions options;
34+
options.SetCompression(0);
35+
auto writer =
36+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
37+
38+
// First entry: ascending values
39+
*Pair = {1, 2.0};
40+
writer->Fill();
41+
}

0 commit comments

Comments
 (0)