Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions projections/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Projections

* [`cardinality`](cardinality): `ROOT::RNTupleCardinality`
* [`collection`](collection): of collection fields
* [`leaf`](leaf): of leaf fields
* [`nested`](nested): projection of nested fields
11 changes: 11 additions & 0 deletions projections/collection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Projections of Collection Fields

## Fields

* `Vector` of type `std::vector<std::int32_t>`
* A projected field `Projected` of types `ROOT::RVec<std::int32_t>`

## Entries

1. Ascending values
2. Empty collection
67 changes: 67 additions & 0 deletions projections/collection/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>
#include <ROOT/RVec.hxx>

using ROOT::Experimental::REntry;
using ROOT::Experimental::RNTupleReader;

#include <cstdint>
#include <fstream>
#include <ostream>
#include <string>
#include <string_view>
#include <vector>

template <typename C>
static void PrintCollectionValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
C &value = *entry.GetPtr<C>(name);
os << " \"" << name << "\": [";
bool first = true;
for (auto element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n " << element;
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "projections.collection.root",
std::string_view output = "projections.collection.json") {
std::ofstream os(std::string{output});
os << "[\n";

auto reader = RNTupleReader::Open("ntpl", input);
auto &entry = reader->GetModel().GetDefaultEntry();
bool first = true;
for (auto index : *reader) {
reader->LoadEntry(index);

if (first) {
first = false;
} else {
os << ",\n";
}
os << " {\n";

PrintCollectionValue<std::vector<std::int32_t>>(entry, "Vector", os);
PrintCollectionValue<ROOT::RVec<std::int32_t>>(entry, "Projected", os,
/*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}
45 changes: 45 additions & 0 deletions projections/collection/write.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <ROOT/RField.hxx>
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleWriteOptions.hxx>
#include <ROOT/RNTupleWriter.hxx>
#include <ROOT/RVec.hxx>

using ROOT::Experimental::RField;
using ROOT::Experimental::RNTupleModel;
using ROOT::Experimental::RNTupleWriteOptions;
using ROOT::Experimental::RNTupleWriter;

#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

void write(std::string_view filename = "projections.collection.root") {
auto model = RNTupleModel::Create();

auto Vector = model->MakeField<std::vector<std::int32_t>>("Vector");
auto Projected =
std::make_unique<RField<ROOT::RVec<std::int32_t>>>("Projected");
model->AddProjectedField(std::move(Projected),
[](const std::string &fieldName) {
if (fieldName == "Projected") {
return "Vector";
} else {
return "Vector._0";
}
});

RNTupleWriteOptions options;
options.SetCompression(0);
auto writer =
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);

// First entry: ascending values
*Vector = {1, 2};
writer->Fill();

// Second entry: empty collection
Vector->clear();
writer->Fill();
}
11 changes: 11 additions & 0 deletions projections/nested/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Projections of Nested Fields

## Fields

* `VectorPair` of type `std::vector<std::pair<std::int32_t, float>>`
* `VectorInt` and `VectorFloat` projected fields of types `std::vector<std::int32_t>` and `std::vector<float>`

## Entries

1. Ascending values
2. Empty collection
93 changes: 93 additions & 0 deletions projections/nested/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>
#include <ROOT/RVec.hxx>

using ROOT::Experimental::REntry;
using ROOT::Experimental::RNTupleReader;

#include <cstdint>
#include <fstream>
#include <ostream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

template <typename T> static void PrintValue(const T &value, std::ostream &os);

template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
os << value;
}

template <> void PrintValue(const float &value, std::ostream &os) {
os << "\"" << value << "\"";
}

template <>
void PrintValue(const std::pair<std::int32_t, float> &value, std::ostream &os) {
os << "[\n";
os << " ";
PrintValue(value.first, os);
os << ",\n";
os << " ";
PrintValue(value.second, os);
os << "\n";
os << " ]";
}

template <typename T>
static void PrintVectorValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value = *entry.GetPtr<std::vector<T>>(name);
os << " \"" << name << "\": [";
bool first = true;
for (auto element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n ";
PrintValue(element, os);
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "projections.nested.root",
std::string_view output = "projections.nested.json") {
std::ofstream os(std::string{output});
// Print floating-point numbers as hexadecimal literals.
os << std::hexfloat;
os << "[\n";

auto reader = RNTupleReader::Open("ntpl", input);
auto &entry = reader->GetModel().GetDefaultEntry();
bool first = true;
for (auto index : *reader) {
reader->LoadEntry(index);

if (first) {
first = false;
} else {
os << ",\n";
}
os << " {\n";

PrintVectorValue<std::pair<std::int32_t, float>>(entry, "VectorPair", os);
PrintVectorValue<std::int32_t>(entry, "VectorInt", os);
PrintVectorValue<float>(entry, "VectorFloat", os, /*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}
55 changes: 55 additions & 0 deletions projections/nested/write.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <ROOT/RField.hxx>
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleWriteOptions.hxx>
#include <ROOT/RNTupleWriter.hxx>
#include <ROOT/RVec.hxx>

using ROOT::Experimental::RField;
using ROOT::Experimental::RNTupleModel;
using ROOT::Experimental::RNTupleWriteOptions;
using ROOT::Experimental::RNTupleWriter;

#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

template <typename T>
static void AddProjectedField(RNTupleModel &model, std::string_view name,
std::string_view source) {
auto field = std::make_unique<RField<std::vector<T>>>(name);
model.AddProjectedField(std::move(field),
[&name, &source](const std::string &fieldName) {
if (fieldName == name) {
return std::string("VectorPair");
} else {
return "VectorPair._0." + std::string(source);
}
});
}

void write(std::string_view filename = "projections.nested.root") {
auto model = RNTupleModel::Create();

auto VectorPair =
model->MakeField<std::vector<std::pair<std::int32_t, float>>>(
"VectorPair");
AddProjectedField<std::int32_t>(*model, "VectorInt", "_0");
AddProjectedField<float>(*model, "VectorFloat", "_1");

RNTupleWriteOptions options;
options.SetCompression(0);
auto writer =
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);

// First entry: ascending values
VectorPair->emplace_back(1, 2.0);
VectorPair->emplace_back(3, 4.0);
writer->Fill();

// Second entry: empty collection
VectorPair->clear();
writer->Fill();
}