42constexpr char const *kNTupleFileName =
"ntpl007_mtFill.root";
45constexpr int kNWriterThreads = 4;
48constexpr int kNEventsPerThread = 25000;
51void FillData(std::unique_ptr<REntry> entry, RNTupleWriter *
writer) {
53 static std::mutex gLock;
55 static std::atomic<std::uint32_t> gThreadId;
56 const auto threadId = ++gThreadId;
58 auto prng = std::make_unique<TRandom3>();
61 auto id = entry->GetPtr<std::uint32_t>(
"id");
63 auto vpx = entry->GetPtr<std::vector<float>>(
"vpx");
64 auto vpy = entry->GetPtr<std::vector<float>>(
"vpy");
65 auto vpz = entry->GetPtr<std::vector<float>>(
"vpz");
67 for (
int i = 0; i < kNEventsPerThread; i++) {
72 int npx =
static_cast<int>(prng->Rndm(1) * 15);
74 for (
int j = 0; j < npx; ++j) {
79 vpx->emplace_back(px);
80 vpy->emplace_back(py);
81 vpz->emplace_back(pz);
84 std::lock_guard<std::mutex> guard(gLock);
93 auto model = RNTupleModel::Create();
94 model->MakeField<std::uint32_t>(
"id");
95 model->MakeField<std::vector<float>>(
"vpx");
96 model->MakeField<std::vector<float>>(
"vpy");
97 model->MakeField<std::vector<float>>(
"vpz");
100 auto writer = RNTupleWriter::Recreate(std::move(model),
"NTuple", kNTupleFileName);
102 std::vector<std::unique_ptr<REntry>> entries;
103 std::vector<std::thread> threads;
104 for (
int i = 0; i < kNWriterThreads; ++i)
105 entries.emplace_back(
writer->CreateEntry());
106 for (
int i = 0; i < kNWriterThreads; ++i)
107 threads.emplace_back(FillData, std::move(entries[i]),
writer.get());
108 for (
int i = 0; i < kNWriterThreads; ++i)
119 auto reader = RNTupleReader::Open(
"NTuple", kNTupleFileName);
120 auto viewVpx = reader->GetView<
float>(
"vpx._0");
124 TCanvas *
c1 =
new TCanvas(
"c2",
"Multi-Threaded Filling Example", 200, 10, 1500, 500);
128 TH1F h(
"h",
"This is the px distribution", 100, -4, 4);
137 auto nEvents = reader->GetNEntries();
138 auto viewId = reader->GetView<std::uint32_t>(
"id");
139 TH2F hFillSequence(
"",
"Entry Id vs Thread Id;Entry Sequence Number;Filling Thread", 100, 0, nEvents, 100, 0,
140 kNWriterThreads + 1);
141 for (
auto i : reader->GetEntryRange())
142 hFillSequence.Fill(i, viewId(i));
143 hFillSequence.DrawCopy();
R__EXTERN TStyle * gStyle
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
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)
2-D histogram with a float per channel (see TH1 documentation)
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
RNTupleGlobalRange GetFieldRange(const RFieldBase &field, const RPageSource &pageSource)
Helper to get the iteration space of the given field that needs to be connected to the given page sou...
void FillData(BinData &dv, const TH1 *hist, TF1 *func=nullptr)
fill the data vector from a TH1.