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// NOTE: The RNTuple classes are experimental at this point.
15// Functionality, interface, and data format is still subject to changes.
16// Do not use for real data!
17
18#include <ROOT/RNTupleModel.hxx>
21
22#include <TCanvas.h>
23#include <TH1I.h>
24#include <TROOT.h>
25#include <TString.h>
26
27#include <cassert>
28#include <cstdio>
29#include <fstream>
30#include <iostream>
31#include <memory>
32#include <string>
33#include <sstream>
34#include <utility>
35
36// Import classes from experimental namespace for the time being
37using RNTupleModel = ROOT::Experimental::RNTupleModel;
38using RNTupleReader = ROOT::Experimental::RNTupleReader;
39using RNTupleWriter = ROOT::Experimental::RNTupleWriter;
40
41constexpr char const* kNTupleFileName = "ntpl001_staff.root";
42
43void Ingest() {
44 // The input file cernstaff.dat is a copy of the CERN staff data base from 1988
45 ifstream fin(gROOT->GetTutorialDir() + "/tree/cernstaff.dat");
46 assert(fin.is_open());
47
48 // We create a unique pointer to an empty data model
49 auto model = RNTupleModel::Create();
50
51 // To define the data model, we create fields with a given C++ type and name. Fields are roughly TTree branches.
52 // MakeField returns a shared pointer to a memory location that we can populate to fill the ntuple with data
53 auto fldCategory = model->MakeField<int>("Category");
54 auto fldFlag = model->MakeField<unsigned int>("Flag");
55 auto fldAge = model->MakeField<int>("Age");
56 auto fldService = model->MakeField<int>("Service");
57 auto fldChildren = model->MakeField<int>("Children");
58 auto fldGrade = model->MakeField<int>("Grade");
59 auto fldStep = model->MakeField<int>("Step");
60 auto fldHrweek = model->MakeField<int>("Hrweek");
61 auto fldCost = model->MakeField<int>("Cost");
62 auto fldDivision = model->MakeField<std::string>("Division");
63 auto fldNation = model->MakeField<std::string>("Nation");
64
65 // We hand-over the data model to a newly created ntuple of name "Staff", stored in kNTupleFileName
66 // In return, we get a unique pointer to an ntuple that we can fill
67 auto ntuple = RNTupleWriter::Recreate(std::move(model), "Staff", kNTupleFileName);
68
69 std::string record;
70 while (std::getline(fin, record)) {
71 std::istringstream iss(record);
72 iss >> *fldCategory >> *fldFlag >> *fldAge >> *fldService >> *fldChildren >> *fldGrade >> *fldStep >> *fldHrweek
73 >> *fldCost >> *fldDivision >> *fldNation;
74 ntuple->Fill();
75 }
76
77 // The ntuple unique pointer goes out of scope here. On destruction, the ntuple flushes unwritten data to disk
78 // and closes the attached ROOT file.
79}
80
81void Analyze() {
82 // Get a unique pointer to an empty RNTuple model
83 auto model = RNTupleModel::Create();
84
85 // We only define the fields that are needed for reading
86 std::shared_ptr<int> fldAge = model->MakeField<int>("Age");
87
88 // Create an ntuple and attach the read model to it
89 auto ntuple = RNTupleReader::Open(std::move(model), "Staff", kNTupleFileName);
90
91 // Quick overview of the ntuple and list of fields.
92 ntuple->PrintInfo();
93
94 std::cout << "The first entry in JSON format:" << std::endl;
95 ntuple->Show(0);
96 // In a future version of RNTuple, there will be support for ntuple->Scan()
97
98 auto c = new TCanvas("c", "", 200, 10, 700, 500);
99 TH1I h("h", "Age Distribution CERN, 1988", 100, 0, 100);
100 h.SetFillColor(48);
101
102 for (auto entryId : *ntuple) {
103 // Populate fldAge
104 ntuple->LoadEntry(entryId);
105 h.Fill(*fldAge);
106 }
107
108 h.DrawCopy();
109}
110
111void ntpl001_staff() {
112 Ingest();
113 Analyze();
114}
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
#define gROOT
Definition TROOT.h:407
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.
The Canvas class.
Definition TCanvas.h:23
1-D histogram with an int per channel (see TH1 documentation)
Definition TH1.h:540