Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl010_skim.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Example creating a derived RNTuple
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date February 2024
10/// \author The ROOT Team
11
12// NOTE: The RNTuple classes are experimental at this point.
13// Functionality, interface, and data format is still subject to changes.
14// Do not use for real data!
15
16#include <ROOT/RNTupleModel.hxx>
19
20#include <TH1F.h>
21#include <TRandom.h>
22
23#include <cstdint>
24
25// Import classes from experimental namespace for the time being.
30
31// Input and output.
32constexpr char const *kNTupleInputName = "ntpl";
33constexpr char const *kNTupleInputFileName = "ntpl010_input.root";
34constexpr char const *kNTupleOutputName = "ntpl_skim";
35constexpr char const *kNTupleOutputFileName = "ntpl010_skim.root";
36constexpr int kNEvents = 25000;
37
38static void Write()
39{
40 auto model = RNTupleModel::Create();
41
42 auto fldVpx = model->MakeField<std::vector<float>>("vpx");
43 auto fldVpy = model->MakeField<std::vector<float>>("vpy");
44 auto fldVpz = model->MakeField<std::vector<float>>("vpz");
45 auto fldN = model->MakeField<float>("n");
46
47 auto writer = RNTupleWriter::Recreate(std::move(model), kNTupleInputName, kNTupleInputFileName);
48
50 for (int i = 0; i < kNEvents; i++) {
51 *fldN = static_cast<int>(gRandom->Rndm(1) * 15);
52
53 fldVpx->clear();
54 fldVpy->clear();
55 fldVpz->clear();
56
57 for (int j = 0; j < *fldN; ++j) {
58 float px, py, pz;
59 gRandom->Rannor(px, py);
60 pz = px * px + py * py;
61
62 fldVpx->emplace_back(px);
63 fldVpy->emplace_back(py);
64 fldVpz->emplace_back(pz);
65 }
66
67 writer->Fill();
68 }
69}
70
71void ntpl010_skim()
72{
73 Write();
74
75 auto reader = RNTupleReader::Open(kNTupleInputName, kNTupleInputFileName);
76
77 auto skimModel = RNTupleModel::Create();
78 // Loop through the top-level fields of the input RNTuple
79 for (const auto &value : reader->GetModel().GetDefaultEntry()) {
80 // Drop "n" field from skimmed dataset
81 if (value.GetField().GetFieldName() == "n")
82 continue;
83
84 // Add a renamed clone of the other fields to the skim model
85 const std::string newName = "skim_" + value.GetField().GetFieldName();
86 skimModel->AddField(value.GetField().Clone(newName));
87 // Connect input and output field
88 skimModel->GetDefaultEntry().BindValue<void>(newName, value.GetPtr<void>());
89 }
90
91 // Add an additional field to the skimmed dataset
92 auto ptrSkip = skimModel->MakeField<std::uint16_t>("skip", 0);
93
94 auto writer = RNTupleWriter::Recreate(std::move(skimModel), kNTupleOutputName, kNTupleOutputFileName);
95
96 auto hSkip = new TH1F("h", "distribution of skipped entries", 10, 0, 10);
97 auto ptrN = reader->GetModel().GetDefaultEntry().GetPtr<float>("n");
98 for (auto numEntry : *reader) {
99 reader->LoadEntry(numEntry);
100 if (*ptrN <= 7) {
101 (*ptrSkip)++;
102 continue;
103 }
104 writer->Fill();
105 hSkip->Fill(*ptrSkip);
106 *ptrSkip = 0;
107 }
108
109 TCanvas *c1 = new TCanvas("", "Skimming Example", 200, 10, 700, 500);
110 hSkip->DrawCopy();
111}
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
The RNTupleModel encapulates the schema of an ntuple.
An RNTuple that is used to read data from storage.
An RNTuple that gets filled with entries (data) and writes them to storage.
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:61
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:615
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
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
return c1
Definition legend1.C:41