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#include "TSystem.h"
20#include "TVirtualMonitoring.h"
21
22using namespace ROOT::Browsable;
23
25
26private:
27
30
32 TTree *fTree{nullptr};
33 void *fHandle2{nullptr};
34 long long fLastProgressSendTm{0};
35
36public:
37 TTreeDrawMonitoring(Int_t period, TTree *tree, void *handle2)
38 : TVirtualMonitoringWriter(), fPeriod(period), fTree(tree), fHandle2(handle2)
39 {
40 }
41
42 // TFile related info. In general they are gathered and sent only sometimes as summaries
43 Bool_t SendFileCloseEvent(TFile * /*file*/) override { return kFALSE; }
44 Bool_t SendFileReadProgress(TFile * /*file*/) override { return kFALSE; }
45 Bool_t SendFileWriteProgress(TFile * /*file*/) override { return kFALSE; }
46
47 Bool_t SendParameters(TList * /*valuelist*/, const char * /*identifier*/ = nullptr) override { return kFALSE; }
48 Bool_t SendInfoTime() override { return kFALSE; }
49 Bool_t SendInfoUser(const char * /*user*/ = nullptr) override { return kFALSE; }
50 Bool_t SendInfoDescription(const char * /*jobtag*/) override { return kFALSE; }
51 Bool_t SendInfoStatus(const char * /*status*/) override { return kFALSE; }
52
53 Bool_t SendFileOpenProgress(TFile * /*file*/, TList * /*openphases*/, const char * /*openphasename*/,
54 Bool_t /*forcesend*/ = kFALSE) override
55 {
56 return kFALSE;
57 }
58
59 Bool_t SendProcessingStatus(const char * /*status*/, Bool_t /*restarttimer*/ = kFALSE) override { return kFALSE; }
60 Bool_t SendProcessingProgress(Double_t nevent, Double_t /*nbytes*/, Bool_t /*force*/ = kFALSE) override
61 {
62 long long millisec = gSystem->Now();
63
65 return kTRUE;
66
67 fLastProgressSendTm = millisec;
68
70
72
73 if (total > 0)
74 RProvider::ReportProgress(fHandle2, nevent <= total ? nevent / total : 1.);
75
76 return kTRUE;
77 }
78 void SetLogLevel(const char * /*loglevel*/ = "WARNING") override {}
79 void Verbose(Bool_t /*onoff*/) override {}
80};
81
82/** Provider for drawing of branches / leafs in the TTree */
83
84class TLeafProvider : public RProvider {
85
86 void *fHandle2{nullptr}; ///<! used only for reporting progress
87
88public:
89
90 TH1 *DrawTree(TTree *ttree, const std::string &expr, const std::string &hname)
91 {
92 if (!ttree)
93 return nullptr;
94
95 std::string expr2 = expr + ">>htemp_tree_draw";
96
97 auto old = gMonitoringWriter;
98 std::unique_ptr<TTreeDrawMonitoring> monitoring;
99
101 monitoring = std::make_unique<TTreeDrawMonitoring>(50, ttree, fHandle2);
102 gMonitoringWriter = monitoring.get();
103 }
104
105 ttree->Draw(expr2.c_str(),"","goff");
106
107 gMonitoringWriter = old;
108
109 if (!gDirectory)
110 return nullptr;
111
112 auto htemp = dynamic_cast<TH1*>(gDirectory->FindObject("htemp_tree_draw"));
113
114 if (!htemp)
115 return nullptr;
116
117 htemp->SetDirectory(nullptr);
118 htemp->SetName(hname.c_str());
119
120 auto FixTitle = [](TNamed *obj) {
121 TString title = obj->GetTitle();
122 title.ReplaceAll("\\/", "/");
123 title.ReplaceAll("#","\\#");
124 obj->SetTitle(title.Data());
125 };
126
127 FixTitle(htemp);
128 FixTitle(htemp->GetXaxis());
129 FixTitle(htemp->GetYaxis());
130 FixTitle(htemp->GetZaxis());
131
132 htemp->BufferEmpty();
133
134 return htemp;
135 }
136
138 {
139 expr.ReplaceAll("/", "\\/");
140
141 auto pos = name.First('[');
142 if (pos != kNPOS) {
143 name.Remove(pos);
144 pos = expr.First('[');
145 if (pos != kNPOS) {
146 expr.Remove(pos);
147 expr.Append("[]");
148 }
149 }
150
151 if (name.First('@') != 0)
152 return;
153
154 name.Remove(0, 1);
155
156 pos = expr.Index(".@");
157
158 if ((pos != kNPOS) && (expr.Index("()", pos) != expr.Length() - 2))
159 expr.Append("()");
160
161 if ((pos != kNPOS) && (pos > 1)) {
162 expr.Remove(pos+1, 1);
163 pos --;
164 while ((pos > 0) && (expr[pos] != '.')) pos--;
165 if (pos > 0)
166 expr.Insert(pos+1, "@");
167 else
168 expr.Prepend("@");
169 }
170
171 expr.ReplaceAll("->@","@->");
172 }
173
174 bool GetDrawExpr(const TBranch *tbranch, TString &expr, TString &name)
175 {
176 if (!tbranch)
177 return false;
178
179 // there are many leaves, plain TTree::Draw does not work
180 if (tbranch->GetNleaves() > 1)
181 return false;
182
183 // there are sub-branches, plain TTree::Draw does not work
184 if (const_cast<TBranch *>(tbranch)->GetListOfBranches()->GetEntriesFast() > 0)
185 return false;
186
187 name = tbranch->GetName();
188
189 expr = tbranch->GetFullName();
190
191 AdjustExpr(expr, name);
192
193 return true;
194 }
195
196 bool GetDrawExpr(const TLeaf *tleaf, TString &expr, TString &name)
197 {
198 if (!tleaf)
199 return false;
200
201 auto tbranch = tleaf->GetBranch();
202 if (tbranch && (tbranch->GetNleaves() == 1))
203 return GetDrawExpr(tbranch, expr, name);
204
205 name = tleaf->GetName();
206
207 expr = tleaf->GetFullName();
208
209 AdjustExpr(expr, name);
210
211 return true;
212 }
213
214 TH1 *DrawBranch(const TBranch *tbranch)
215 {
216 TString expr, name;
217 if (!GetDrawExpr(tbranch, expr, name))
218 return nullptr;
219
220 return DrawTree(tbranch->GetTree(), expr.Data(), name.Data());
221 }
222
223 TH1 *DrawBranch(std::unique_ptr<RHolder> &obj)
224 {
225 fHandle2 = obj.get();
226 return DrawBranch(obj->get_object<TBranch>());
227 }
228
229 TH1 *DrawLeaf(std::unique_ptr<RHolder> &obj)
230 {
231 fHandle2 = obj.get();
232
233 auto tleaf = obj->get_object<TLeaf>();
234
235 TString expr, name;
236
237 if (!GetDrawExpr(tleaf,expr, name))
238 return nullptr;
239
240 return DrawTree(tleaf->GetBranch()->GetTree(), expr.Data(), name.Data());
241 }
242
243 bool GetDrawExpr(const TBranchElement *tbranch, TString &expr, TString &name)
244 {
245 if (!tbranch)
246 return false;
247
248 // there are sub-branches, plain TTree::Draw does not work
249 if (const_cast<TBranchElement *>(tbranch)->GetListOfBranches()->GetEntriesFast() > 0)
250 return false;
251
252 // just copy and paste code from TBranchElement::Browse
253 expr = name = tbranch->GetName();
254
255 Int_t pos = expr.First('[');
256 if (pos != kNPOS)
257 expr.Remove(pos);
258 if (tbranch->GetMother()) {
259 TString mothername = tbranch->GetMother()->GetName();
260 pos = mothername.First('[');
261 if (pos != kNPOS) {
262 mothername.Remove(pos);
263 }
264 Int_t len = mothername.Length();
265 if (len) {
266 if (mothername(len-1) != '.') {
267 // We do not know for sure whether the mother's name is
268 // already preprended. So we need to check:
269 // a) it is prepended
270 // b) it is NOT the name of a daugher (i.e. mothername.mothername exist)
271 TString doublename = mothername;
272 doublename.Append(".");
273 Int_t isthere = (expr.Index(doublename) == 0);
274 if (!isthere) {
275 expr.Prepend(doublename);
276 } else {
277 if (tbranch->GetMother()->FindBranch(mothername)) {
278 doublename.Append(mothername);
279 isthere = (expr.Index(doublename) == 0);
280 if (!isthere) {
281 mothername.Append(".");
282 expr.Prepend(mothername);
283 }
284 } else {
285 // Nothing to do because the mother's name is
286 // already in the name.
287 }
288 }
289 } else {
290 // If the mother's name end with a dot then
291 // the daughter probably already contains the mother's name
292 if (expr.Index(mothername) == kNPOS) {
293 expr.Prepend(mothername);
294 }
295 }
296 }
297 }
298
299 AdjustExpr(expr, name);
300
301 return true;
302 }
303
304 TH1 *DrawBranchElement(std::unique_ptr<RHolder> &obj)
305 {
306 fHandle2 = obj.get();
307
308 auto tbranch = obj->get_object<TBranchElement>();
309 TString expr, name;
310 if (!GetDrawExpr(tbranch, expr, name))
311 return nullptr;
312
313 return DrawTree(tbranch->GetTree(), expr.Data(), name.Data());
314 }
315
317 {
318 if (!browsable)
319 return false;
320
321 auto cl = browsable->GetClassType();
322
323 bool can_draw = (!cl || (cl->GetCollectionProxy() && cl->GetCollectionProxy()->GetType() > 0));
324 if (!can_draw)
325 return false;
326
327 auto br = browsable->GetBranch();
328 if (!br)
329 return false;
330
331 browsable->GetScope(expr);
332
333 name = browsable->GetName();
334
335 // If this is meant to be run on the collection
336 // we need to "move" the "@" from branch.@member
337 // to branch@.member
338 // fullname.ReplaceAll(".@","@.");
339
340 AdjustExpr(expr, name);
341
342 return true;
343 }
344
345
346 TH1 *DrawBranchBrowsable(std::unique_ptr<RHolder> &obj)
347 {
348 auto browsable = obj->get_object<TVirtualBranchBrowsable>();
349
350 TString expr, name;
351
352 if (!GetDrawExpr(browsable, expr, name))
353 return nullptr;
354
355 return DrawTree(browsable->GetBranch()->GetTree(), expr.Data(), name.Data());
356 }
357
358};
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:124
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define gDirectory
Definition TDirectory.h:384
static unsigned int total
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
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
R__EXTERN TVirtualMonitoringWriter * gMonitoringWriter
Provider of different browsing methods for supported classes.
Definition RProvider.hxx:37
static bool ReportProgress(void *handle, float progress)
Report running progress Returns true if handling function was invoked Method can be used to detect if...
A Branch for the case of an object.
A TTree is a list of TBranches.
Definition TBranch.h:93
TTree * GetTree() const
Definition TBranch.h:252
virtual TString GetFullName() const
Return the 'full' name of the branch.
Definition TBranch.cxx:2031
Int_t GetNleaves() const
Definition TBranch.h:249
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:2127
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
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:8905
Provider for drawing of branches / leafs in the TTree.
void * fHandle2
! used only for reporting progress
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
A doubly linked list.
Definition TList.h:38
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:417
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:538
const char * Data() const
Definition TString.h:376
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:572
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
virtual TTime Now()
Get current time in milliseconds since 0:00 Jan 1 1995.
Definition TSystem.cxx:463
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:416
TTreeDrawMonitoring & operator=(const TTreeDrawMonitoring &)=delete
Bool_t SendFileReadProgress(TFile *) override
TTreeDrawMonitoring(const TTreeDrawMonitoring &)=delete
Bool_t SendFileWriteProgress(TFile *) override
Bool_t SendInfoStatus(const char *) override
TTreeDrawMonitoring(Int_t period, TTree *tree, void *handle2)
Bool_t SendProcessingStatus(const char *, Bool_t=kFALSE) override
Bool_t SendParameters(TList *, const char *=nullptr) override
Bool_t SendFileOpenProgress(TFile *, TList *, const char *, Bool_t=kFALSE) override
Bool_t SendProcessingProgress(Double_t nevent, Double_t, Bool_t=kFALSE) override
Bool_t SendFileCloseEvent(TFile *) override
void SetLogLevel(const char *="WARNING") override
long long fLastProgressSendTm
Bool_t SendInfoTime() override
Bool_t SendInfoDescription(const char *) override
void Verbose(Bool_t) override
Bool_t SendInfoUser(const char *=nullptr) override
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:431
virtual Long64_t GetEntries() const
Definition TTree.h:463
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