Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TLeafProvider.hxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
3 * All rights reserved. *
4 * *
5 * For the licensing terms see $ROOTSYS/LICENSE. *
6 * For the list of contributors see $ROOTSYS/README/CREDITS. *
7 *************************************************************************/
8
10
11#include "TString.h"
12#include "TLeaf.h"
13#include "TBranch.h"
14#include "TBranchElement.h"
15#include "TBranchBrowsable.h"
16#include "TTree.h"
17#include "TH1.h"
18#include "TDirectory.h"
19
20using namespace ROOT::Experimental::Browsable;
21
22/** Provider for drawing of branches / leafs in the TTree */
23
24class TLeafProvider : public RProvider {
25public:
26
27 TH1 *DrawTree(TTree *ttree, const std::string &expr, const std::string &hname)
28 {
29 if (!ttree)
30 return nullptr;
31
32 std::string expr2 = expr + ">>htemp_tree_draw";
33
34 ttree->Draw(expr2.c_str(),"","goff");
35
36 if (!gDirectory)
37 return nullptr;
38
39 auto htemp = dynamic_cast<TH1*>(gDirectory->FindObject("htemp_tree_draw"));
40
41 if (!htemp)
42 return nullptr;
43
44 htemp->SetDirectory(nullptr);
45 htemp->SetName(hname.c_str());
46
47 htemp->BufferEmpty();
48
49 return htemp;
50 }
51
52 TH1 *DrawLeaf(std::unique_ptr<RHolder> &obj)
53 {
54 auto tleaf = obj->get_object<TLeaf>();
55 if (!tleaf)
56 return nullptr;
57
58 return DrawTree(tleaf->GetBranch()->GetTree(), tleaf->GetName(), tleaf->GetName());
59 }
60
61 TH1 *DrawBranch(std::unique_ptr<RHolder> &obj)
62 {
63 auto tbranch = obj->get_object<TBranch>();
64 if (!tbranch)
65 return nullptr;
66
67 // there are many leaves, plain TTree::Draw does not work
68 if (tbranch->GetNleaves() > 1)
69 return nullptr;
70
71 TString name = tbranch->GetName();
72 Int_t pos = name.First('[');
73 if (pos!=kNPOS) name.Remove(pos);
74
75 return DrawTree(tbranch->GetTree(), name.Data(), name.Data());
76 }
77
78 TH1 *DrawBranchElement(std::unique_ptr<RHolder> &obj)
79 {
80 auto tbranch = obj->get_object<TBranchElement>();
81 if (!tbranch)
82 return nullptr;
83
84 // there are sub-branches, plain TTree::Draw does not work
85 if (tbranch->GetListOfBranches()->GetEntriesFast() > 0)
86 return nullptr;
87
88 // just copy and paste code from TBranchElement::Browse
89 TString slash("/");
90 TString escapedSlash("\\/");
91 TString name = tbranch->GetName();
92 Int_t pos = name.First('[');
93 if (pos != kNPOS)
94 name.Remove(pos);
95 if (tbranch->GetMother()) {
96 TString mothername = tbranch->GetMother()->GetName();
97 pos = mothername.First('[');
98 if (pos != kNPOS) {
99 mothername.Remove(pos);
100 }
101 Int_t len = mothername.Length();
102 if (len) {
103 if (mothername(len-1) != '.') {
104 // We do not know for sure whether the mother's name is
105 // already preprended. So we need to check:
106 // a) it is prepended
107 // b) it is NOT the name of a daugher (i.e. mothername.mothername exist)
108 TString doublename = mothername;
109 doublename.Append(".");
110 Int_t isthere = (name.Index(doublename) == 0);
111 if (!isthere) {
112 name.Prepend(doublename);
113 } else {
114 if (tbranch->GetMother()->FindBranch(mothername)) {
115 doublename.Append(mothername);
116 isthere = (name.Index(doublename) == 0);
117 if (!isthere) {
118 mothername.Append(".");
119 name.Prepend(mothername);
120 }
121 } else {
122 // Nothing to do because the mother's name is
123 // already in the name.
124 }
125 }
126 } else {
127 // If the mother's name end with a dot then
128 // the daughter probably already contains the mother's name
129 if (name.Index(mothername) == kNPOS) {
130 name.Prepend(mothername);
131 }
132 }
133 }
134 }
135 name.ReplaceAll(slash, escapedSlash);
136
137 return DrawTree(tbranch->GetTree(), name.Data(), tbranch->GetName());
138 }
139
140 TH1 *DrawBranchBrowsable(std::unique_ptr<RHolder> &obj)
141 {
142 auto browsable = obj->get_object<TVirtualBranchBrowsable>();
143 if (!browsable)
144 return nullptr;
145
146 auto cl = browsable->GetClassType();
147
148 bool can_draw = (!cl || (cl->GetCollectionProxy() && cl->GetCollectionProxy()->GetType() > 0));
149 if (!can_draw)
150 return nullptr;
151
152 auto br = browsable->GetBranch();
153 if (!br)
154 return nullptr;
155
157 browsable->GetScope(name);
158
159 // If this is meant to be run on the collection
160 // we need to "move" the "@" from branch.@member
161 // to branch@.member
162 name.ReplaceAll(".@","@.");
163 name.ReplaceAll("->@","@->");
164
165 return DrawTree(br->GetTree(), name.Data(), browsable->GetName());
166 }
167
168};
const Ssiz_t kNPOS
Definition RtypesCore.h:124
#define gDirectory
Definition TDirectory.h:385
char name[80]
Definition TGX11.cxx:110
Provider of different browsing methods for supported classes.
Definition RProvider.hxx:36
A Branch for the case of an object.
A TTree is a list of TBranches.
Definition TBranch.h:89
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:8767
Provider for drawing of branches / leafs in the TTree.
TH1 * DrawBranch(std::unique_ptr< RHolder > &obj)
TH1 * DrawBranchBrowsable(std::unique_ptr< RHolder > &obj)
TH1 * DrawBranchElement(std::unique_ptr< RHolder > &obj)
TH1 * DrawTree(TTree *ttree, const std::string &expr, const std::string &hname)
TH1 * DrawLeaf(std::unique_ptr< RHolder > &obj)
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition TString.cxx:523
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:692
TString & Prepend(const char *cs)
Definition TString.h:661
TString & Remove(Ssiz_t pos)
Definition TString.h:673
TString & Append(const char *cs)
Definition TString.h:564
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition TTree.h:428
TVirtualBranchBrowsable is a base class (not really abstract, but useless by itself) for helper objec...
TClass * GetClassType() const
return the type of this browsable object
TCanvas * slash()
Definition slash.C:1