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 <TCanvas.h>
21#include <TH1F.h>
22#include <TRandom.h>
23
24#include <cstdint>
25
26// Import classes from experimental namespace for the time being.
31
32// Input and output.
33constexpr char const *kNTupleInputName = "ntpl";
34constexpr char const *kNTupleInputFileName = "ntpl010_input.root";
35constexpr char const *kNTupleOutputName = "ntpl_skim";
36constexpr char const *kNTupleOutputFileName = "ntpl010_skim.root";
37constexpr int kNEvents = 25000;
38
39static void Write()
40{
41 auto model = RNTupleModel::Create();
42
43 auto fldVpx = model->MakeField<std::vector<float>>("vpx");
44 auto fldVpy = model->MakeField<std::vector<float>>("vpy");
45 auto fldVpz = model->MakeField<std::vector<float>>("vpz");
46 auto fldN = model->MakeField<float>("n");
47
48 auto writer = RNTupleWriter::Recreate(std::move(model), kNTupleInputName, kNTupleInputFileName);
49
51 for (int i = 0; i < kNEvents; i++) {
52 *fldN = static_cast<int>(gRandom->Rndm(1) * 15);
53
54 fldVpx->clear();
55 fldVpy->clear();
56 fldVpz->clear();
57
58 for (int j = 0; j < *fldN; ++j) {
59 float px, py, pz;
60 gRandom->Rannor(px, py);
61 pz = px * px + py * py;
62
63 fldVpx->emplace_back(px);
64 fldVpy->emplace_back(py);
65 fldVpz->emplace_back(pz);
66 }
67
68 writer->Fill();
69 }
70}
71
72void ntpl010_skim()
73{
74 Write();
75
76 auto reader = RNTupleReader::Open(kNTupleInputName, kNTupleInputFileName);
77
78 auto skimModel = RNTupleModel::Create();
79 // Loop through the top-level fields of the input RNTuple
80 for (const auto &value : reader->GetModel().GetDefaultEntry()) {
81 // Drop "n" field from skimmed dataset
82 if (value.GetField().GetFieldName() == "n")
83 continue;
84
85 // Add a renamed clone of the other fields to the skim model
86 const std::string newName = "skim_" + value.GetField().GetFieldName();
87 skimModel->AddField(value.GetField().Clone(newName));
88 // Connect input and output field
89 skimModel->GetDefaultEntry().BindValue<void>(newName, value.GetPtr<void>());
90 }
91
92 // Add an additional field to the skimmed dataset
93 auto ptrSkip = skimModel->MakeField<std::uint16_t>("skip", 0);
94
95 auto writer = RNTupleWriter::Recreate(std::move(skimModel), kNTupleOutputName, kNTupleOutputFileName);
96
97 auto hSkip = new TH1F("h", "distribution of skipped entries", 10, 0, 10);
98 auto ptrN = reader->GetModel().GetDefaultEntry().GetPtr<float>("n");
99 for (auto numEntry : *reader) {
100 reader->LoadEntry(numEntry);
101 if (*ptrN <= 7) {
102 (*ptrSkip)++;
103 continue;
104 }
105 writer->Fill();
106 hSkip->Fill(*ptrSkip);
107 *ptrSkip = 0;
108 }
109
110 TCanvas *c1 = new TCanvas("", "Skimming Example", 200, 10, 700, 500);
111 hSkip->DrawCopy();
112}
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:622
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