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