Adapted from the cernbuild and cernstaff tree tutorials. Illustrates the type-safe ntuple model interface, which is used to define a data model that is in a second step taken by an ntuple reader or writer.
#include <ROOT/RNTuple.hxx>
#include <cassert>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <sstream>
#include <utility>
constexpr char const* kNTupleFileName = "ntpl001_staff.root";
void Ingest() {
ifstream fin(
gROOT->GetTutorialDir() +
"/tree/cernstaff.dat");
assert(fin.is_open());
auto model = RNTupleModel::Create();
auto fldCategory = model->MakeField<int>("Category");
auto fldFlag = model->MakeField<unsigned int>("Flag");
auto fldAge = model->MakeField<int>("Age");
auto fldService = model->MakeField<int>("Service");
auto fldChildren = model->MakeField<int>("Children");
auto fldGrade = model->MakeField<int>("Grade");
auto fldStep = model->MakeField<int>("Step");
auto fldHrweek = model->MakeField<int>("Hrweek");
auto fldCost = model->MakeField<int>("Cost");
auto fldDivision = model->MakeField<std::string>("Division");
auto fldNation = model->MakeField<std::string>("Nation");
auto ntuple = RNTupleWriter::Recreate(std::move(model), "Staff", kNTupleFileName);
std::string record;
while (std::getline(fin, record)) {
std::istringstream iss(record);
iss >> *fldCategory >> *fldFlag >> *fldAge >> *fldService >> *fldChildren >> *fldGrade >> *fldStep >> *fldHrweek
>> *fldCost >> *fldDivision >> *fldNation;
ntuple->Fill();
}
}
void Analyze() {
auto model = RNTupleModel::Create();
std::shared_ptr<int> fldAge = model->MakeField<int>("Age");
auto ntuple = RNTupleReader::Open(std::move(model), "Staff", kNTupleFileName);
ntuple->PrintInfo();
std::cout << "The first entry in JSON format:" << std::endl;
ntuple->Show(0);
auto c =
new TCanvas(
"c",
"", 200, 10, 700, 500);
TH1I h(
"h",
"Age Distribution CERN, 1988", 100, 0, 100);
for (auto entryId : *ntuple) {
ntuple->LoadEntry(entryId);
}
}
void ntpl001_staff() {
Ingest();
Analyze();
}
#define R__LOAD_LIBRARY(LIBRARY)
The RNTupleModel encapulates the schema of an ntuple.
An RNTuple that is used to read data from storage.
An RNTuple that gets filled with entries (data) and writes them to storage.
1-D histogram with an int per channel (see TH1 documentation)}