Skip to content

Commit 389a1cc

Browse files
committed
[ntuple] Update tutorials
1 parent 443f1e2 commit 389a1cc

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

tutorials/io/ntuple/ntpl012_processor_chain.C

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,20 @@ void Read(const std::vector<RNTupleOpenSpec> &ntuples)
6464
TH1F hPx("h", "This is the px distribution", 100, -4, 4);
6565
hPx.SetFillColor(48);
6666

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.
67+
auto model = ROOT::RNTupleModel::CreateBare();
68+
model->MakeField<std::vector<float>>("vpx");
69+
70+
// By passing a model to the processor, we can instruct the RNTupleProcessor to only read a particular subset of
71+
// fields. Because the RNTupleProcessor adds some additional bookkeeping to the standard REntry, only bare models
72+
// (i.e., models without a default entry) can be passed. When no model is provided, a default model is created based
73+
// on the first ntuple specified.
74+
// Access to the entry values in both cases is provided through RNTupleProcessor::GetValuePtr() (see below).
7475
auto processor = RNTupleProcessor::CreateChain(ntuples, std::move(model));
7576
int prevProcessorNumber{-1};
7677

77-
for (const auto &entry : *processor) {
78+
auto ptrPx = processor->GetValuePtr<std::vector<float>>("vpx");
79+
80+
for (const auto &_ : *processor) {
7881
// The RNTupleProcessor provides some additional bookkeeping information. The local entry number is reset each
7982
// a new ntuple in the chain is opened for processing.
8083
if (static_cast<int>(processor->GetCurrentProcessorNumber()) > prevProcessorNumber) {

tutorials/io/ntuple/ntpl015_processor_join.C

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
using ROOT::Experimental::RNTupleOpenSpec;
2525
using ROOT::Experimental::RNTupleProcessor;
2626

27-
const std::string kMainNTupleName = "mainNTuple";
27+
const std::string kPrimaryNTupleName = "mainNTuple";
2828
const std::string kMainNTuplePath = "main_ntuple.root";
2929
const std::string kAuxNTupleName = "auxNTuple";
3030
const std::string kAuxNTuplePath = "aux_ntuple.root";
3131

32-
// Number of events to generate for the auxiliary ntuple. The main ntuple will have a fifth of this number.
32+
// Number of events to generate for the auxiliary ntuple. The primary ntuple will have a fifth of this number.
3333
constexpr int kNEvents = 10000;
3434

35-
void WriteMain(std::string_view ntupleName, std::string_view ntupleFileName)
35+
void WritePrimary(std::string_view ntupleName, std::string_view ntupleFileName)
3636
{
3737
auto model = ROOT::RNTupleModel::Create();
3838

@@ -41,15 +41,15 @@ void WriteMain(std::string_view ntupleName, std::string_view ntupleFileName)
4141

4242
auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), ntupleName, ntupleFileName);
4343

44-
// The main ntuple only contains a subset of the entries present in the auxiliary ntuple.
44+
// The primary ntuple only contains a subset of the entries present in the auxiliary ntuple.
4545
for (int i = 0; i < kNEvents; i += 5) {
4646
*fldI = i;
4747
*fldVpx = gRandom->Gaus();
4848

4949
writer->Fill();
5050
}
5151

52-
std::cout << "Wrote " << writer->GetNEntries() << " to the main RNTuple" << std::endl;
52+
std::cout << "Wrote " << writer->GetNEntries() << " to the primary RNTuple" << std::endl;
5353
}
5454

5555
void WriteAux(std::string_view ntupleName, std::string_view ntupleFileName)
@@ -77,23 +77,23 @@ void Read()
7777
TH1F hPy("h", "This is the px + py distribution", 100, -4, 4);
7878
hPy.SetFillColor(48);
7979

80-
// The first specified ntuple is the main 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 main ntuple.
82-
// We specify field "i" as the join field. This field, which should be present in all ntuples specified is used to
83-
// identify which entries belong together. Multiple join fields can be specified, in which case the combination of
84-
// field values is used. It is possible to specify up to 4 join fields. Providing an empty list of join fields
85-
// signals to the processor that all entries are aligned.
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.
8686
auto processor =
87-
RNTupleProcessor::CreateJoin({kMainNTupleName, kMainNTuplePath}, {kAuxNTupleName, kAuxNTuplePath}, {"i"});
87+
RNTupleProcessor::CreateJoin({kPrimaryNTupleName, kMainNTuplePath}, {kAuxNTupleName, kAuxNTuplePath}, {"i"});
8888

89-
float px, py;
90-
for (const auto &entry : *processor) {
91-
// Fields from the main ntuple are accessed by their original name.
92-
px = *entry.GetPtr<float>("vpx");
93-
// Fields from auxiliary ntuples are accessed by prepending the name of the auxiliary ntuple.
94-
py = *entry.GetPtr<float>(kAuxNTupleName + ".vpy");
89+
// Access to the processor's entry values is provided through RNTupleProcessor::GetValuePtr().
90+
// Fields from the primary ntuple are accessed by their original name.
91+
auto px = processor->GetValuePtr<float>("vpx");
92+
// Fields from auxiliary ntuples are accessed by prepending the name of the auxiliary ntuple.
93+
auto py = processor->GetValuePtr<float>(kAuxNTupleName + ".vpy");
9594

96-
hPy.Fill(px + py);
95+
for (const auto &_ : *processor) {
96+
hPy.Fill(*px + *py);
9797
}
9898

9999
std::cout << "Processed a total of " << processor->GetNEntriesProcessed() << " entries" << std::endl;
@@ -103,7 +103,7 @@ void Read()
103103

104104
void ntpl015_processor_join()
105105
{
106-
WriteMain(kMainNTupleName, kMainNTuplePath);
106+
WritePrimary(kPrimaryNTupleName, kMainNTuplePath);
107107
WriteAux(kAuxNTupleName, kAuxNTuplePath);
108108

109109
Read();

0 commit comments

Comments
 (0)