Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl012_processor_chain.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Demonstrate the RNTupleProcessor for vertical compositions (chains) of RNTuples
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date April 2024
10/// \author The ROOT Team
11
12// NOTE: The RNTupleProcessor and related classes are experimental at this point.
13// Functionality and interface are still subject to changes.
14
15#include <ROOT/RNTupleModel.hxx>
18
19#include <TCanvas.h>
20#include <TH1F.h>
21#include <TRandom.h>
22
23// Number of events to generate for each ntuple.
24constexpr int kNEvents = 10000;
25
26void Write(std::string_view ntupleName, std::string_view fileName)
27{
29
30 auto fldVpx = model->MakeField<std::vector<float>>("vpx");
31 auto fldVpy = model->MakeField<std::vector<float>>("vpy");
32 auto fldVpz = model->MakeField<std::vector<float>>("vpz");
33 auto fldN = model->MakeField<std::uint64_t>("vn");
34
35 auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), ntupleName, fileName);
36
37 for (int i = 0; i < kNEvents; ++i) {
38 fldVpx->clear();
39 fldVpy->clear();
40 fldVpz->clear();
41
42 *fldN = gRandom->Integer(15);
43 for (int j = 0; j < *fldN; ++j) {
44 float px, py, pz;
45 gRandom->Rannor(px, py);
46 pz = px * px + py * py;
47
48 fldVpx->emplace_back(px);
49 fldVpy->emplace_back(py);
50 fldVpz->emplace_back(pz);
51 }
52
53 writer->Fill();
54 }
55}
56
57void Read(const std::vector<ROOT::RNTupleInputSpec> &ntuples)
58{
59 auto c = new TCanvas("c", "RNTupleProcessor Example", 200, 10, 700, 500);
60 TH1F hPx("h", "This is the px distribution", 100, -4, 4);
61 hPx.SetFillColor(48);
62
63 // The chain-based processor can be created by passing a list of RNTupleInputSpecs, describing the name and location
64 // of each ntuple in the chain.
65 auto processor = ROOT::RNTupleProcessor::CreateChain(ntuples);
66
67 // Access to the processor's fields is done by first requesting them through RNTupleProcessor::RequestField(). The
68 // returned value can be used to read the current entry's value for that particular field.
69 auto px = processor->RequestField<std::vector<float>>("vpx");
70
71 // The iterator value gives access to the the current entry to process.
72 for (const auto &idx : *processor) {
73 // Load the entry.
74 processor->LoadEntry(idx);
75 // We use the value returned from requesting the field to read its data for the current entry.
76 for (auto x : *px) {
77 hPx.Fill(x);
78 }
79 }
80
81 std::cout << "Processed a total of " << processor->GetNEntriesProcessed() << " entries" << std::endl;
82
83 hPx.DrawCopy();
84}
85
86void ntpl012_processor_chain()
87{
88 Write("ntuple1", "ntuple1.root");
89 Write("ntuple2", "ntuple2.root");
90 Write("ntuple3", "ntuple3.root");
91
92 // The ntuples to generate and subsequently process. The model of the first ntuple will be used to construct the
93 // entry used by the processor.
94 std::vector<ROOT::RNTupleInputSpec> ntuples = {
95 {"ntuple1", "ntuple1.root"}, {"ntuple2", "ntuple2.root"}, {"ntuple3", "ntuple3.root"}};
96
97 Read(ntuples);
98}
#define c(i)
Definition RSha256.hxx:101
R__EXTERN TRandom * gRandom
Definition TRandom.h:73
static std::unique_ptr< RNTupleModel > Create()
static std::unique_ptr< RNTupleProcessor > CreateChain(std::vector< RNTupleInputSpec > ntuples, const RNTupleProcessorOptions &opts=RNTupleProcessorOptions())
Create an RNTupleProcessor for a chain (i.e., a vertical combination) of RNTuples.
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.
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition TRandom.cxx:506
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition TRandom.cxx:360
Double_t x[n]
Definition legend1.C:17