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