Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl008_import.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Example of converting data stored in a TTree into an RNTuple
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date December 2022
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// Until C++ runtime modules are universally used, we explicitly load the ntuple library. Otherwise
17// triggering autoloading from the use of templated types would require an exhaustive enumeration
18// of "all" template instances in the LinkDef file.
19R__LOAD_LIBRARY(ROOTNTupleUtil)
20
21#include <ROOT/RNTuple.hxx>
22#include <ROOT/RNTupleDS.hxx>
24
25#include <TFile.h>
26#include <TROOT.h>
27
28// Import classes from experimental namespace for the time being.
29using RNTuple = ROOT::Experimental::RNTuple;
30using RNTupleImporter = ROOT::Experimental::RNTupleImporter;
31using RNTupleReader = ROOT::Experimental::RNTupleReader;
32
33// Input and output.
34constexpr char const *kTreeFileName = "http://root.cern.ch/files/HiggsTauTauReduced/GluGluToHToTauTau.root";
35constexpr char const *kTreeName = "Events";
36constexpr char const *kNTupleFileName = "ntpl008_import.root";
37
38void ntpl008_import()
39{
40 // RNTupleImporter appends keys to the output file; make sure a second run of the tutorial does not fail
41 // with `Key 'Events' already exists in file ntpl008_import.root` by removing the output file.
42 gSystem->Unlink(kNTupleFileName);
43
44 // Use multiple threads to compress RNTuple data.
46
47 // Create a new RNTupleImporter object.
48 auto importer = RNTupleImporter::Create(kTreeFileName, kTreeName, kNTupleFileName);
49
50 // Begin importing.
51 importer->Import();
52
53 // Inspect the schema of the written RNTuple.
54 auto file = std::unique_ptr<TFile>(TFile::Open(kNTupleFileName));
55 if (!file || file->IsZombie()) {
56 std::cerr << "cannot open " << kNTupleFileName << std::endl;
57 return;
58 }
59 auto ntpl = file->Get<RNTuple>("Events");
60 auto reader = RNTupleReader::Open(ntpl);
61 reader->PrintInfo();
62
63 auto df = ROOT::RDF::Experimental::FromRNTuple("Events", kNTupleFileName);
64 df.Histo1D({"Jet_pt", "Jet_pt", 100, 0, 0}, "Jet_pt")->DrawCopy();
65}
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:491
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
Converts a TTree into an RNTuple.
An RNTuple that is used to read data from storage.
Definition RNTuple.hxx:101
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:512
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4075
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1368
RDataFrame FromRNTuple(std::string_view ntupleName, std::string_view fileName)
void EnableImplicitMT(UInt_t numthreads=0)
Enable ROOT's implicit multi-threading for all objects and methods that provide an internal paralleli...
Definition TROOT.cxx:539
Definition file.py:1