Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RNTupleClassicBrowse.cxx
Go to the documentation of this file.
1/// \file RNTupleClassicBrowse.cxx
2/// \author Jakob Blomer <jblomer@cern.ch>
3/// \date 2025-06-30
4
5/*************************************************************************
6 * Copyright (C) 1995-2025, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#include <ROOT/RNTuple.hxx>
20
21#include <TBrowser.h>
22#include <TObject.h>
23#include <TPad.h>
24#include <TText.h>
25
26namespace {
27
28class RFieldBrowsable final : public TObject {
29private:
30 std::shared_ptr<ROOT::RNTupleReader> fReader;
33 bool fIsLeaf = false;
34 std::unique_ptr<TH1> fHistogram;
35 std::string fFieldName;
36 std::string fTypeName;
37
38public:
39 RFieldBrowsable(std::shared_ptr<ROOT::RNTupleReader> reader, ROOT::DescriptorId_t fieldId)
40 : fReader(reader), fFieldId(fieldId)
41 {
42 const auto &desc = fReader->GetDescriptor();
43 fBrowsableFieldId = ROOT::Internal::GetNextBrowsableField(fFieldId, desc);
44 fIsLeaf = desc.GetFieldDescriptor(fBrowsableFieldId).GetLinkIds().empty();
45 fFieldName = desc.GetFieldDescriptor(fFieldId).GetFieldName();
46 fTypeName = desc.GetFieldDescriptor(fFieldId).GetTypeName();
47 }
48
49 void Browse(TBrowser *b) final
50 {
51 if (!b)
52 return;
53
54 const auto &desc = fReader->GetDescriptor();
55
56 if (fIsLeaf) {
57 if (!gPad)
58 return;
59
60 auto view = fReader->GetView<void>(desc.GetQualifiedFieldName(fBrowsableFieldId));
61
62 ROOT::Internal::RNTupleDrawVisitor drawVisitor(fReader, desc.GetFieldDescriptor(fFieldId).GetFieldName());
63 view.GetField().AcceptVisitor(drawVisitor);
64 fHistogram = std::unique_ptr<TH1>(drawVisitor.MoveHist());
65 if (fHistogram->GetEntries() == 0) {
66 gPad->DrawFrame(-1., -1., 1., 1.);
67 TText *textEmpty = new TText(0., 0., "Empty");
68 textEmpty->SetTextAlign(22);
69 textEmpty->SetTextFont(42);
70 textEmpty->SetTextSize(0.1);
71 textEmpty->SetTextColor(1);
72 textEmpty->Draw();
73 } else {
74 fHistogram->Draw();
75 }
76 gPad->Update();
77 } else {
78 for (const auto &f : desc.GetFieldIterable(fBrowsableFieldId)) {
79 b->Add(new RFieldBrowsable(fReader, f.GetId()), f.GetFieldName().c_str());
80 }
81 }
82 }
83
84 bool IsFolder() const final { return !fIsLeaf; }
85 const char *GetIconName() const final { return IsFolder() ? "RNTuple-folder" : "RNTuple-leaf"; }
86
87 const char *GetName() const final { return fFieldName.c_str(); }
88 const char *GetTitle() const final { return fTypeName.c_str(); }
89};
90
91class RVisualizationBrowsable : public TObject {
92private:
93 std::unique_ptr<ROOT::Experimental::RNTupleInspector> fInspector;
94 std::unique_ptr<ROOT::Experimental::RTreeMapPainter> fTreeMap;
95
96public:
97 RVisualizationBrowsable(const ROOT::RNTuple &ntuple)
98 : fInspector(ROOT::Experimental::RNTupleInspector::Create(ntuple))
99 {
100 }
101 void Browse(TBrowser *b) final
102 {
103 if (!b || !gPad)
104 return;
105 gPad->GetListOfPrimitives()->Clear();
106 fTreeMap = ROOT::Experimental::CreateTreeMapFromRNTuple(*fInspector);
107 fTreeMap->Paint("");
108 gPad->Update();
109 }
110
111 const char *GetIconName() const final { return "RNTuple-visualization"; }
112 bool IsFolder() const final { return false; }
113 const char *GetName() const final { return "Visualization"; }
114 const char *GetTitle() const final { return "TreeMap visualization of RNTuple structure and disk usage"; }
115};
116
117} // anonymous namespace
118
119void ROOT::Internal::BrowseRNTuple(const void *ntuple, TBrowser *b)
120{
121 if (!b)
122 return;
123
124 std::shared_ptr<ROOT::RNTupleReader> reader = RNTupleReader::Open(*static_cast<const ROOT::RNTuple *>(ntuple));
125 const auto &desc = reader->GetDescriptor();
126 b->Add(new RVisualizationBrowsable(*static_cast<const ROOT::RNTuple *>(ntuple)), "Visualization");
127 for (const auto &f : desc.GetTopLevelFields()) {
128 b->Add(new RFieldBrowsable(reader, f.GetId()), f.GetFieldName().c_str());
129 }
130}
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define gPad
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.
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:67
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:48
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:50
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:52
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:53
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
Mother of all ROOT objects.
Definition TObject.h:42
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
std::unique_ptr< RTreeMapPainter > CreateTreeMapFromRNTuple(const RNTupleInspector &insp)
Logic for converting an RNTuple to RTreeMapPainter given RNTupleInspector.
void BrowseRNTuple(const void *ntuple, TBrowser *b)
DescriptorId_t GetNextBrowsableField(DescriptorId_t fieldId, const RNTupleDescriptor &desc)
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
constexpr DescriptorId_t kInvalidDescriptorId