Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl001_staff.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Write and read tabular data with RNTuple. Adapted from the cernbuild and cernstaff tree tutorials.
5/// Illustrates the type-safe ntuple model interface, which is used to define a data model that is in a second step
6/// taken by an ntuple reader or writer.
7///
8/// \macro_image
9/// \macro_code
10///
11/// \date April 2019
12/// \author The ROOT Team
13
14#include <ROOT/RNTupleModel.hxx>
17
18#include <TCanvas.h>
19#include <TH1I.h>
20#include <TROOT.h>
21#include <TString.h>
22
23#include <cassert>
24#include <cstdio>
25#include <fstream>
26#include <iostream>
27#include <memory>
28#include <string>
29#include <sstream>
30#include <utility>
31
32constexpr char const* kNTupleFileName = "ntpl001_staff.root";
33
34void Ingest() {
35 // The input file cernstaff.dat is a copy of the CERN staff data base from 1988
36 ifstream fin(gROOT->GetTutorialDir() + "/io/tree/cernstaff.dat");
37 assert(fin.is_open());
38
39 // We create a unique pointer to an empty data model
40 auto model = ROOT::RNTupleModel::Create();
41
42 // To define the data model, we create fields with a given C++ type and name. Fields are roughly TTree branches.
43 // MakeField returns a shared pointer to a memory location that we can populate to fill the ntuple with data
44 auto fldCategory = model->MakeField<int>("Category");
45 auto fldFlag = model->MakeField<unsigned int>("Flag");
46 auto fldAge = model->MakeField<int>("Age");
47 auto fldService = model->MakeField<int>("Service");
48 auto fldChildren = model->MakeField<int>("Children");
49 auto fldGrade = model->MakeField<int>("Grade");
50 auto fldStep = model->MakeField<int>("Step");
51 auto fldHrweek = model->MakeField<int>("Hrweek");
52 auto fldCost = model->MakeField<int>("Cost");
53 auto fldDivision = model->MakeField<std::string>("Division");
54 auto fldNation = model->MakeField<std::string>("Nation");
55
56 // We hand-over the data model to a newly created ntuple of name "Staff", stored in kNTupleFileName
57 // In return, we get a unique pointer to an ntuple that we can fill
58 auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "Staff", kNTupleFileName);
59
60 std::string record;
61 while (std::getline(fin, record)) {
62 std::istringstream iss(record);
64 >> *fldCost >> *fldDivision >> *fldNation;
65 writer->Fill();
66 }
67
68 // The ntuple unique pointer goes out of scope here. On destruction, the ntuple flushes unwritten data to disk
69 // and closes the attached ROOT file.
70}
71
72void Analyze() {
73 // Get a unique pointer to an empty RNTuple model
74 auto model = ROOT::RNTupleModel::Create();
75
76 // We only define the fields that are needed for reading
77 std::shared_ptr<int> fldAge = model->MakeField<int>("Age");
78
79 // Create an ntuple and attach the read model to it
80 auto reader = ROOT::RNTupleReader::Open(std::move(model), "Staff", kNTupleFileName);
81
82 // Quick overview of the ntuple and list of fields.
83 reader->PrintInfo();
84
85 std::cout << "The first entry in JSON format:" << std::endl;
86 reader->Show(0);
87
88 auto c = new TCanvas("c", "", 200, 10, 700, 500);
89 TH1I h("h", "Age Distribution CERN, 1988", 100, 0, 100);
90 h.SetFillColor(48);
91
92 for (auto entryId : *reader) {
93 // Populate fldAge
94 reader->LoadEntry(entryId);
95 h.Fill(*fldAge);
96 }
97
98 h.DrawCopy();
99}
100
101void ntpl001_staff() {
102 Ingest();
103 Analyze();
104}
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gROOT
Definition TROOT.h:414
static std::unique_ptr< RNTupleModel > Create()
static std::unique_ptr< RNTupleReader > Open(std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Open an RNTuple for reading.
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 an int per channel (see TH1 documentation)
Definition TH1.h:795