Adapted from the hvector tree tutorial.
#include <ROOT/RNTuple.hxx>
#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++) {
fldVpx->clear();
fldVpy->clear();
fldVpz->clear();
fldVrand->clear();
for (int j = 0; j < npx; ++j) {
float px, py, pz;
pz = px*px + py*py;
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();
}
#define R__LOAD_LIBRARY(LIBRARY)
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)}
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
virtual Double_t Rndm()
Machine independent random number generator.
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).