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 auto FixTitle = [](TNamed *obj) {
48 TString title = obj->GetTitle();
49 title.ReplaceAll("\\/", "/");
50 title.ReplaceAll("#","\\#");
51 obj->SetTitle(title.Data());
52 };
53
54 FixTitle(htemp);
55 FixTitle(htemp->GetXaxis());
56 FixTitle(htemp->GetYaxis());
57 FixTitle(htemp->GetZaxis());
58
59 htemp->BufferEmpty();
60
61 return htemp;
62 }
63
65 {
66 expr.ReplaceAll("/", "\\/");
67
68 auto pos = name.First('[');
69 if (pos != kNPOS) {
70 name.Remove(pos);
71 pos = expr.First('[');
72 if (pos != kNPOS) {
73 expr.Remove(pos);
74 expr.Append("[]");
75 }
76 }
77
78 if (name.First('@') != 0)
79 return;
80
81 name.Remove(0, 1);
82
83 pos = expr.Index(".@");
84
85 if ((pos != kNPOS) && (expr.Index("()", pos) != expr.Length() - 2))
86 expr.Append("()");
87
88 if ((pos != kNPOS) && (pos > 1)) {
89 expr.Remove(pos+1, 1);
90 pos --;
91 while ((pos > 0) && (expr[pos] != '.')) pos--;
92 if (pos > 0)
93 expr.Insert(pos+1, "@");
94 else
95 expr.Prepend("@");
96 }
97
98 expr.ReplaceAll("->@","@->");
99 }
100
101 bool GetDrawExpr(const TBranch *tbranch, TString &expr, TString &name)
102 {
103 if (!tbranch)
104 return false;
105
106 // there are many leaves, plain TTree::Draw does not work
107 if (tbranch->GetNleaves() > 1)
108 return false;
109
110 // there are sub-branches, plain TTree::Draw does not work
111 if (const_cast<TBranch *>(tbranch)->GetListOfBranches()->GetEntriesFast() > 0)
112 return false;
113
114 name = tbranch->GetName();
115
116 expr = tbranch->GetFullName();
117
118 AdjustExpr(expr, name);
119
120 return true;
121 }
122
123 bool GetDrawExpr(const TLeaf *tleaf, TString &expr, TString &name)
124 {
125 if (!tleaf)
126 return false;
127
128 auto tbranch = tleaf->GetBranch();
129 if (tbranch && (tbranch->GetNleaves() == 1))
130 return GetDrawExpr(tbranch, expr, name);
131
132 name = tleaf->GetName();
133
134 expr = tleaf->GetFullName();
135
136 AdjustExpr(expr, name);
137
138 return true;
139 }
140
141
142 TH1 *DrawBranch(const TBranch *tbranch)
143 {
144 TString expr, name;
145 if (!GetDrawExpr(tbranch, expr, name))
146 return nullptr;
147
148 return DrawTree(tbranch->GetTree(), expr.Data(), name.Data());
149 }
150
151 TH1 *DrawBranch(std::unique_ptr<RHolder> &obj)
152 {
153 return DrawBranch(obj->get_object<TBranch>());
154 }
155
156 TH1 *DrawLeaf(std::unique_ptr<RHolder> &obj)
157 {
158 auto tleaf = obj->get_object<TLeaf>();
159
160 TString expr, name;
161
162 if (!GetDrawExpr(tleaf,expr, name))
163 return nullptr;
164
165 return DrawTree(tleaf->GetBranch()->GetTree(), expr.Data(), name.Data());
166 }
167
168 bool GetDrawExpr(const TBranchElement *tbranch, TString &expr, TString &name)
169 {
170 if (!tbranch)
171 return false;
172
173 // there are sub-branches, plain TTree::Draw does not work
174 if (const_cast<TBranchElement *>(tbranch)->GetListOfBranches()->GetEntriesFast() > 0)
175 return false;
176
177 // just copy and paste code from TBranchElement::Browse
178 expr = name = tbranch->GetName();
179
180 Int_t pos = expr.First('[');
181 if (pos != kNPOS)
182 expr.Remove(pos);
183 if (tbranch->GetMother()) {
184 TString mothername = tbranch->GetMother()->GetName();
185 pos = mothername.First('[');
186 if (pos != kNPOS) {
187 mothername.Remove(pos);
188 }
189 Int_t len = mothername.Length();
190 if (len) {
191 if (mothername(len-1) != '.') {
192 // We do not know for sure whether the mother's name is
193 // already preprended. So we need to check:
194 // a) it is prepended
195 // b) it is NOT the name of a daugher (i.e. mothername.mothername exist)
196 TString doublename = mothername;
197 doublename.Append(".");
198 Int_t isthere = (expr.Index(doublename) == 0);
199 if (!isthere) {
200 expr.Prepend(doublename);
201 } else {
202 if (tbranch->GetMother()->FindBranch(mothername)) {
203 doublename.Append(mothername);
204 isthere = (expr.Index(doublename) == 0);
205 if (!isthere) {
206 mothername.Append(".");
207 expr.Prepend(mothername);
208 }
209 } else {
210 // Nothing to do because the mother's name is
211 // already in the name.
212 }
213 }
214 } else {
215 // If the mother's name end with a dot then
216 // the daughter probably already contains the mother's name
217 if (expr.Index(mothername) == kNPOS) {
218 expr.Prepend(mothername);
219 }
220 }
221 }
222 }
223
224 AdjustExpr(expr, name);
225
226 return true;
227 }
228
229 TH1 *DrawBranchElement(std::unique_ptr<RHolder> &obj)
230 {
231 auto tbranch = obj->get_object<TBranchElement>();
232 TString expr, name;
233 if (!GetDrawExpr(tbranch, expr, name))
234 return nullptr;
235
236 return DrawTree(tbranch->GetTree(), expr.Data(), name.Data());
237 }
238
240 {
241 if (!browsable)
242 return false;
243
244 auto cl = browsable->GetClassType();
245
246 bool can_draw = (!cl || (cl->GetCollectionProxy() && cl->GetCollectionProxy()->GetType() > 0));
247 if (!can_draw)
248 return false;
249
250 auto br = browsable->GetBranch();
251 if (!br)
252 return false;
253
254 browsable->GetScope(expr);
255
256 name = browsable->GetName();
257
258 // If this is meant to be run on the collection
259 // we need to "move" the "@" from branch.@member
260 // to branch@.member
261 // fullname.ReplaceAll(".@","@.");
262
263 AdjustExpr(expr, name);
264
265 return true;
266 }
267
268
269 TH1 *DrawBranchBrowsable(std::unique_ptr<RHolder> &obj)
270 {
271 auto browsable = obj->get_object<TVirtualBranchBrowsable>();
272
273 TString expr, name;
274
275 if (!GetDrawExpr(browsable, expr, name))
276 return nullptr;
277
278 return DrawTree(browsable->GetBranch()->GetTree(), expr.Data(), name.Data());
279 }
280
281};
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:124
#define gDirectory
Definition TDirectory.h:386
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
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
TTree * GetTree() const
Definition TBranch.h:248
virtual TString GetFullName() const
Return the 'full' name of the branch.
Definition TBranch.cxx:1977
Int_t GetNleaves() const
Definition TBranch.h:245
virtual TBranch * FindBranch(const char *name)
Find the immediate sub-branch with passed name.
Definition TBranch.cxx:1035
TBranch * GetMother() const
Get our top-level parent branch in the tree.
Definition TBranch.cxx:2073
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:8803
Provider for drawing of branches / leafs in the TTree.
bool GetDrawExpr(const TBranch *tbranch, TString &expr, TString &name)
bool GetDrawExpr(const TVirtualBranchBrowsable *browsable, TString &expr, TString &name)
TH1 * DrawBranch(const TBranch *tbranch)
TH1 * DrawBranch(std::unique_ptr< RHolder > &obj)
void AdjustExpr(TString &expr, TString &name)
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)
bool GetDrawExpr(const TBranchElement *tbranch, TString &expr, TString &name)
bool GetDrawExpr(const TLeaf *tleaf, TString &expr, TString &name)
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
TBranch * GetBranch() const
Definition TLeaf.h:116
virtual TString GetFullName() const
Return the full name (including the parent's branch names) of the leaf.
Definition TLeaf.cxx:224
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:421
TString & Insert(Ssiz_t pos, const char *s)
Definition TString.h:661
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition TString.cxx:531
const char * Data() const
Definition TString.h:380
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
TString & Prepend(const char *cs)
Definition TString.h:673
TString & Remove(Ssiz_t pos)
Definition TString.h:685
TString & Append(const char *cs)
Definition TString.h:576
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
A TTree represents a columnar dataset.
Definition TTree.h:79
void Draw(Option_t *opt) override
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...
const TBranch * GetBranch() const
return the parent branch (might be many levels up)
void GetScope(TString &scope) const
Returns the full name for TTree::Draw to draw *this.
TClass * GetClassType() const
return the type of this browsable object