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// Import classes from the `Experimental` namespace for the time being.
26
27// Number of events to generate for each ntuple.
28constexpr int kNEvents = 10000;
29
30void Write(std::string_view ntupleName, std::string_view fileName)
31{
32 auto model = ROOT::RNTupleModel::Create();
33
34 auto fldVpx = model->MakeField<std::vector<float>>("vpx");
35 auto fldVpy = model->MakeField<std::vector<float>>("vpy");
36 auto fldVpz = model->MakeField<std::vector<float>>("vpz");
37 auto fldN = model->MakeField<std::uint64_t>("vn");
38
39 auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), ntupleName, fileName);
40
41 for (int i = 0; i < kNEvents; ++i) {
42 fldVpx->clear();
43 fldVpy->clear();
44 fldVpz->clear();
45
46 *fldN = gRandom->Integer(15);
47 for (int j = 0; j < *fldN; ++j) {
48 float px, py, pz;
49 gRandom->Rannor(px, py);
50 pz = px * px + py * py;
51
52 fldVpx->emplace_back(px);
53 fldVpy->emplace_back(py);
54 fldVpz->emplace_back(pz);
55 }
56
57 writer->Fill();
58 }
59}
60
61void Read(const std::vector<RNTupleOpenSpec> &ntuples)
62{
63 auto c = new TCanvas("c", "RNTupleProcessor Example", 200, 10, 700, 500);
64 TH1F hPx("h", "This is the px distribution", 100, -4, 4);
65 hPx.SetFillColor(48);
66
67 auto model = ROOT::RNTupleModel::Create();
68 auto ptrPx = model->MakeField<std::vector<float>>("vpx");
69
70 // By passing a model to the processor, we can use the pointers to field values created upon model creation during
71 // processing. When no model is provided, a default model is created based on the first ntuple specified.
72 // Access to the entry values in this case can be achieved through RNTupleProcessor::GetEntry() or through its
73 // iterator.
74 auto processor = RNTupleProcessor::CreateChain(ntuples, std::move(model));
75 int prevProcessorNumber{-1};
76
77 for (const auto &entry : *processor) {
78 // The RNTupleProcessor provides some additional bookkeeping information. The local entry number is reset each
79 // a new ntuple in the chain is opened for processing.
80 if (static_cast<int>(processor->GetCurrentProcessorNumber()) > prevProcessorNumber) {
81 prevProcessorNumber = processor->GetCurrentProcessorNumber();
82 std::cout << "Processing `ntuple" << prevProcessorNumber + 1 << "` (" << processor->GetNEntriesProcessed()
83 << " total entries processed so far)" << std::endl;
84 }
85
86 // We can use the pointer to the field obtained while creating our model to read the field's data for the current
87 // entry.
88 for (auto x : *ptrPx) {
89 hPx.Fill(x);
90 }
91 }
92
93 std::cout << "Processed a total of " << processor->GetNEntriesProcessed() << " entries" << std::endl;
94
95 hPx.DrawCopy();
96}
97
99{
100 Write("ntuple1", "ntuple1.root");
101 Write("ntuple2", "ntuple2.root");
102 Write("ntuple3", "ntuple3.root");
103
104 // The ntuples to generate and subsequently process. The model of the first ntuple will be used to construct the
105 // entry used by the processor.
106 std::vector<RNTupleOpenSpec> ntuples = {
107 {"ntuple1", "ntuple1.root"}, {"ntuple2", "ntuple2.root"}, {"ntuple3", "ntuple3.root"}};
108
109 Read(ntuples);
110}
#define c(i)
Definition RSha256.hxx:101
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
Specification of the name and location of an RNTuple, used for creating a new RNTupleProcessor.
Interface for iterating over entries of RNTuples and vertically concatenated RNTuples (chains).
static std::unique_ptr< RNTupleModel > Create()
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())
Throws an exception if the model is null.
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:877
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:507
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition TRandom.cxx:361
Double_t x[n]
Definition legend1.C:17