Skip to content

Commit d65fffd

Browse files
committed
Add test for projections of collection fields
Closes #27
1 parent a80f97a commit d65fffd

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

projections/README.md

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

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

projections/collection/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Projections of Collection Fields
2+
3+
## Fields
4+
5+
* `Vector` of type `std::vector<std::int32_t>`
6+
* A projected field `Projected` of types `ROOT::RVec<std::int32_t>`
7+
8+
## Entries
9+
10+
1. Ascending values
11+
2. Empty collection

projections/collection/read.C

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <ROOT/REntry.hxx>
2+
#include <ROOT/RNTupleReader.hxx>
3+
#include <ROOT/RVec.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 <vector>
14+
15+
template <typename C>
16+
static void PrintCollectionValue(const REntry &entry, std::string_view name,
17+
std::ostream &os, bool last = false) {
18+
C &value = *entry.GetPtr<C>(name);
19+
os << " \"" << name << "\": [";
20+
bool first = true;
21+
for (auto element : value) {
22+
if (first) {
23+
first = false;
24+
} else {
25+
os << ",";
26+
}
27+
os << "\n " << element;
28+
}
29+
if (!value.empty()) {
30+
os << "\n ";
31+
}
32+
os << "]";
33+
if (!last) {
34+
os << ",";
35+
}
36+
os << "\n";
37+
}
38+
39+
void read(std::string_view input = "projections.collection.root",
40+
std::string_view output = "projections.collection.json") {
41+
std::ofstream os(std::string{output});
42+
os << "[\n";
43+
44+
auto reader = RNTupleReader::Open("ntpl", input);
45+
auto &entry = reader->GetModel().GetDefaultEntry();
46+
bool first = true;
47+
for (auto index : *reader) {
48+
reader->LoadEntry(index);
49+
50+
if (first) {
51+
first = false;
52+
} else {
53+
os << ",\n";
54+
}
55+
os << " {\n";
56+
57+
PrintCollectionValue<std::vector<std::int32_t>>(entry, "Vector", os);
58+
PrintCollectionValue<ROOT::RVec<std::int32_t>>(entry, "Projected", os,
59+
/*last=*/true);
60+
61+
os << " }";
62+
// Newline is intentionally missing, may need to print a comma before the
63+
// next entry.
64+
}
65+
os << "\n";
66+
os << "]\n";
67+
}

projections/collection/write.C

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <ROOT/RField.hxx>
2+
#include <ROOT/RNTupleModel.hxx>
3+
#include <ROOT/RNTupleWriteOptions.hxx>
4+
#include <ROOT/RNTupleWriter.hxx>
5+
#include <ROOT/RVec.hxx>
6+
7+
using ROOT::Experimental::RField;
8+
using ROOT::Experimental::RNTupleModel;
9+
using ROOT::Experimental::RNTupleWriteOptions;
10+
using ROOT::Experimental::RNTupleWriter;
11+
12+
#include <cstdint>
13+
#include <memory>
14+
#include <string>
15+
#include <string_view>
16+
#include <vector>
17+
18+
void write(std::string_view filename = "projections.collection.root") {
19+
auto model = RNTupleModel::Create();
20+
21+
auto Vector = model->MakeField<std::vector<std::int32_t>>("Vector");
22+
auto Projected =
23+
std::make_unique<RField<ROOT::RVec<std::int32_t>>>("Projected");
24+
model->AddProjectedField(std::move(Projected),
25+
[](const std::string &fieldName) {
26+
if (fieldName == "Projected") {
27+
return "Vector";
28+
} else {
29+
return "Vector._0";
30+
}
31+
});
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+
*Vector = {1, 2};
40+
writer->Fill();
41+
42+
// Second entry: empty collection
43+
Vector->clear();
44+
writer->Fill();
45+
}

0 commit comments

Comments
 (0)