43constexpr char const *kNTupleFileName =
"ntpl009_parallelWriter.root";
46constexpr int kNWriterThreads = 4;
49constexpr int kNEventsPerThread = 25000;
54 static std::atomic<std::uint32_t> gThreadId;
55 const auto threadId = ++gThreadId;
57 auto prng = std::make_unique<TRandom3>();
60 auto fillContext =
writer->CreateFillContext();
61 auto entry = fillContext->CreateEntry();
63 auto id = entry->GetPtr<std::uint32_t>(
"id");
65 auto vpx = entry->GetPtr<std::vector<float>>(
"vpx");
66 auto vpy = entry->GetPtr<std::vector<float>>(
"vpy");
67 auto vpz = entry->GetPtr<std::vector<float>>(
"vpz");
69 for (
int i = 0; i < kNEventsPerThread; i++) {
74 int npx =
static_cast<int>(prng->Rndm(1) * 15);
76 for (
int j = 0; j < npx; ++j) {
79 pz = px * px + py * py;
81 vpx->emplace_back(px);
82 vpy->emplace_back(py);
83 vpz->emplace_back(pz);
86 fillContext->Fill(*entry);
94 auto model = RNTupleModel::CreateBare();
95 model->MakeField<std::uint32_t>(
"id");
96 model->MakeField<std::vector<float>>(
"vpx");
97 model->MakeField<std::vector<float>>(
"vpy");
98 model->MakeField<std::vector<float>>(
"vpz");
102 RNTupleWriteOptions options;
103 options.SetApproxZippedClusterSize(1024 * 1024);
106 auto writer = RNTupleParallelWriter::Recreate(std::move(model),
"NTuple", kNTupleFileName, options);
108 std::vector<std::thread> threads;
109 for (
int i = 0; i < kNWriterThreads; ++i)
110 threads.emplace_back(FillData,
writer.get());
111 for (
int i = 0; i < kNWriterThreads; ++i)
121 auto reader = RNTupleReader::Open(
"NTuple", kNTupleFileName);
122 auto viewVpx = reader->GetView<
float>(
"vpx._0");
126 TCanvas *
c1 =
new TCanvas(
"c2",
"Multi-Threaded Filling Example", 200, 10, 1500, 500);
130 TH1F h(
"h",
"This is the px distribution", 100, -4, 4);
133 for (
auto i : viewVpx.GetFieldRange())
139 auto nEvents = reader->GetNEntries();
140 auto viewId = reader->GetView<std::uint32_t>(
"id");
141 TH2F hFillSequence(
"",
"Entry Id vs Thread Id;Entry Sequence Number;Filling Thread", 100, 0, nEvents, 100, 0,
142 kNWriterThreads + 1);
143 for (
auto i : reader->GetEntryRange())
144 hFillSequence.Fill(i, viewId(i));
145 hFillSequence.DrawCopy();
148void ntpl009_parallelWriter()
R__EXTERN TStyle * gStyle
The RNTupleModel encapulates the schema of an ntuple.
A writer to fill an RNTuple from multiple contexts.
An RNTuple that is used to read data from storage.
Common user-tunable settings for storing ntuples.
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...
void FillData(BinData &dv, const TH1 *hist, TF1 *func=nullptr)
fill the data vector from a TH1.