Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl015_processor_join.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Demonstrate the RNTupleProcessor for horizontal compositions (joins) of RNTuples
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date November 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
27const std::string kPrimaryNTupleName = "mainNTuple";
28const std::string kMainNTuplePath = "main_ntuple.root";
29const std::string kAuxNTupleName = "auxNTuple";
30const std::string kAuxNTuplePath = "aux_ntuple.root";
31
32// Number of events to generate for the auxiliary ntuple. The primary ntuple will have a fifth of this number.
33constexpr int kNEvents = 10000;
34
35void WritePrimary(std::string_view ntupleName, std::string_view ntupleFileName)
36{
37 auto model = ROOT::RNTupleModel::Create();
38
39 auto fldI = model->MakeField<std::uint32_t>("i");
40 auto fldVpx = model->MakeField<float>("vpx");
41
43
44 // The primary ntuple only contains a subset of the entries present in the auxiliary ntuple.
45 for (int i = 0; i < kNEvents; i += 5) {
46 *fldI = i;
47 *fldVpx = gRandom->Gaus();
48
49 writer->Fill();
50 }
51
52 std::cout << "Wrote " << writer->GetNEntries() << " to the primary RNTuple" << std::endl;
53}
54
55void WriteAux(std::string_view ntupleName, std::string_view ntupleFileName)
56{
57 auto model = ROOT::RNTupleModel::Create();
58
59 auto fldI = model->MakeField<std::uint32_t>("i");
60 auto fldVpy = model->MakeField<float>("vpy");
61
63
64 for (int i = 0; i < kNEvents; ++i) {
65 *fldI = i;
66 *fldVpy = gRandom->Gaus();
67
68 writer->Fill();
69 }
70
71 std::cout << "Wrote " << writer->GetNEntries() << " to the auxiliary RNTuple" << std::endl;
72}
73
74void Read()
75{
76 auto c = new TCanvas("c", "RNTupleJoinProcessor Example", 200, 10, 700, 500);
77 TH1F hPy("h", "This is the px + py distribution", 100, -4, 4);
78 hPy.SetFillColor(48);
79
80 // The first specified ntuple is the primary ntuple and will be used to drive the processor loop. The subsequent
81 // list of ntuples (in this case, only one) are auxiliary and will be joined with the entries from the primary
82 // ntuple. We specify field "i" as the join field. This field, which should be present in all ntuples specified is
83 // used to identify which entries belong together. Multiple join fields can be specified, in which case the
84 // combination of field values is used. It is possible to specify up to 4 join fields. Providing an empty list of
85 // join fields signals to the processor that all entries are aligned.
86 auto processor =
87 RNTupleProcessor::CreateJoin({kPrimaryNTupleName, kMainNTuplePath}, {kAuxNTupleName, kAuxNTuplePath}, {"i"});
88
89 // Access to the processor's fields is done by first requesting them through RNTupleProcessor::RequestField(). The
90 // returned value can be used to read the current entry's value for that particular field. Fields from the primary
91 // ntuple are requested by their original name.
92 auto px = processor->RequestField<float>("vpx");
93 // Fields from auxiliary ntuples are requested by prepending the name of the auxiliary ntuple.
94 auto py = processor->RequestField<float>(kAuxNTupleName + ".vpy");
95
96 // The iterator value is the index of the current entry being processed. In this example, we don't use it.
97 for (auto _ : *processor) {
98 hPy.Fill(*px + *py);
99 }
100
101 std::cout << "Processed a total of " << processor->GetNEntriesProcessed() << " entries" << std::endl;
102
103 hPy.DrawCopy();
104}
105
107{
110
111 Read();
112}
#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
#define _(A, B)
Definition cfortran.h:108
Specification of the name and location of an RNTuple, used for creating a new RNTupleProcessor.
Interface for iterating over entries of vertically ("chained") and/or horizontally ("joined") combine...
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())
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 Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:274