Adapted from the hvector tree tutorial.
#include <cstdio>
#include <iostream>
#include <memory>
#include <vector>
#include <utility>
constexpr char const* kNTupleFileName = "ntpl002_vector.root";
constexpr int kUpdateGuiFreq = 1000;
constexpr int kNEvents = 25000;
void Write()
{
auto model = RNTupleModel::Create();
std::shared_ptr<std::vector<float>> fldVpx = model->MakeField<std::vector<float>>("vpx");
auto fldVpy = model->MakeField<std::vector<float>>("vpy");
auto fldVpz = model->MakeField<std::vector<float>>("vpz");
auto fldVrand = model->MakeField<std::vector<float>>("vrand");
auto ntuple = RNTupleWriter::Recreate(std::move(model), "F", kNTupleFileName);
TH1F hpx(
"hpx",
"This is the px distribution", 100, -4, 4);
hpx.SetFillColor(48);
auto c1 =
new TCanvas(
"c1",
"Dynamic Filling Example", 200, 10, 700, 500);
for (
int i = 0;
i < kNEvents;
i++) {
int npx =
static_cast<int>(
gRandom->Rndm(1) * 15);
fldVpx->clear();
fldVpy->clear();
fldVpz->clear();
fldVrand->clear();
for (int j = 0; j < npx; ++j) {
hpx.Fill(px);
fldVpx->emplace_back(px);
fldVpy->emplace_back(
py);
fldVpz->emplace_back(pz);
fldVrand->emplace_back(random);
}
if (
i && (
i % kUpdateGuiFreq) == 0) {
if (
i == kUpdateGuiFreq) hpx.Draw();
break;
}
ntuple->Fill();
}
hpx.DrawCopy();
}
void Read()
{
auto model = RNTupleModel::Create();
auto fldVpx = model->MakeField<std::vector<float>>("vpx");
auto ntuple = RNTupleReader::Open(std::move(model), "F", kNTupleFileName);
ntuple->PrintInfo();
std::cout << "Entry number 42 in JSON format:" << std::endl;
ntuple->Show(41);
TH1F h(
"h",
"This is the px distribution", 100, -4, 4);
for (auto entryId : *ntuple) {
ntuple->LoadEntry(entryId);
for (auto px : *fldVpx) {
}
if (entryId && (entryId % kUpdateGuiFreq) == 0) {
if (entryId == kUpdateGuiFreq)
h.Draw();
break;
}
}
}
void ntpl002_vector()
{
Write();
Read();
}
R__EXTERN TRandom * gRandom
R__EXTERN TSystem * gSystem
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 a float per channel (see TH1 documentation)