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()
{
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");
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) {
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;
}
}
hpx.DrawCopy();
}
void Read()
{
auto fldVpx = model->MakeField<std::vector<float>>("vpx");
reader->PrintInfo();
std::cout << "Entry number 42 in JSON format:" << std::endl;
reader->Show(41);
TH1F h(
"h",
"This is the px distribution", 100, -4, 4);
for (auto entryId : *reader) {
reader->LoadEntry(entryId);
for (auto px : *fldVpx) {
}
if (entryId && (entryId % kUpdateGuiFreq) == 0) {
if (entryId == kUpdateGuiFreq)
h.Draw();
break;
}
}
}
void ntpl002_vector()
{
Write();
Read();
}
static std::unique_ptr< RNTupleModel > Create()
static std::unique_ptr< RNTupleReader > Open(std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Open an RNTuple for reading.
static std::unique_ptr< RNTupleWriter > Recreate(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Creates an RNTupleWriter backed by storage, overwriting it if one with the same URI exists.
1-D histogram with a float per channel (see TH1 documentation)