Skip to content
Merged
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
1 change: 1 addition & 0 deletions projections/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Projections

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

## Fields

* `Pair` of type `std::pair<std::int32_t, float>`
* `Int32` and `Float` projected fields of types `std::int32_t` and `float`

## Entries

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

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

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

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 <typename T, typename U>
static void PrintPairValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value = *entry.GetPtr<std::pair<T, U>>(name);
os << " \"" << name << "\": [\n";
os << " ";
PrintValue(value.first, os);
os << ",\n";
os << " ";
PrintValue(value.second, os);
os << "\n";
os << " ]";
if (!last) {
os << ",";
}
os << "\n";
}

template <typename T>
static void PrintValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
T value = *entry.GetPtr<T>(name);
os << " \"" << name << "\": ";
PrintValue(value, os);
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "projections.leaf.root",
std::string_view output = "projections.leaf.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";

PrintPairValue<std::int32_t, float>(entry, "Pair", os);
PrintValue<std::int32_t>(entry, "Int32", os);
PrintValue<float>(entry, "Float", os, /*last=*/true);

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

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

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

auto Pair = model->MakeField<std::pair<std::int32_t, float>>("Pair");
AddProjectedField<std::int32_t>(*model, "Int32", "Pair._0");
AddProjectedField<float>(*model, "Float", "Pair._1");

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

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