Logo ROOT  
Reference Guide
TLeaf.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 12/01/96
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TLeaf
13 \ingroup tree
14 
15 A TLeaf describes individual elements of a TBranch
16 See TBranch structure in TTree.
17 */
18 
19 #include "TLeaf.h"
20 #include "TBranch.h"
21 #include "TBuffer.h"
22 #include "TTree.h"
23 #include "TVirtualPad.h"
24 #include "TBrowser.h"
25 
26 #include <cctype>
27 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 
33  : TNamed()
34  , fNdata(0)
35  , fLen(0)
36  , fLenType(0)
37  , fOffset(0)
38  , fIsRange(kFALSE)
39  , fIsUnsigned(kFALSE)
40  , fLeafCount(0)
41  , fBranch(0)
42  , fLeafCountValues(0)
43 {
44 }
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Create a Leaf.
48 ///
49 /// See the TTree and TBranch constructors for explanation of parameters.
50 
51 TLeaf::TLeaf(TBranch *parent, const char* name, const char *)
52  : TNamed(name, name)
53  , fNdata(0)
54  , fLen(0)
55  , fLenType(4)
56  , fOffset(0)
57  , fIsRange(kFALSE)
58  , fIsUnsigned(kFALSE)
59  , fLeafCount(0)
60  , fBranch(parent)
61  , fLeafCountValues(0)
62 {
64 
65  if (fLen == -1) {
66  MakeZombie();
67  return;
68  }
69 
70  const char *bracket = strchr(name, '[');
71  if (bracket) fName.ReplaceAll(bracket,"");
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// Copy constructor.
76 
77 TLeaf::TLeaf(const TLeaf& lf) :
78  TNamed(lf),
79  fNdata(lf.fNdata),
80  fLen(lf.fLen),
81  fLenType(lf.fLenType),
82  fOffset(lf.fOffset),
83  fIsRange(lf.fIsRange),
84  fIsUnsigned(lf.fIsUnsigned),
85  fLeafCount(lf.fLeafCount),
86  fBranch(lf.fBranch),
87  fLeafCountValues(nullptr)
88 {
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 /// Assignment operator.
93 
95 {
96  if(this!=&lf) {
98  fNdata=lf.fNdata;
99  fLen=lf.fLen;
100  fLenType=lf.fLenType;
101  fOffset=lf.fOffset;
102  fIsRange=lf.fIsRange;
105  fBranch=lf.fBranch;
106  if (fLeafCountValues) {
108  fLeafCountValues->fValues.resize(0);
109  }
110  }
111  return *this;
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Destructor.
116 
118 {
119  if (fBranch) {
120  TTree* tree = fBranch->GetTree();
121  fBranch = 0;
122  if (tree) {
123  TObjArray *lst = tree->GetListOfLeaves();
124  if (lst->GetLast()!=-1) lst->Remove(this);
125  }
126  }
127  fLeafCount = 0;
128  delete fLeafCountValues;
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Browse the content of this leaf.
133 
135 {
136  if (strchr(GetName(), '.')) {
137  fBranch->GetTree()->Draw(GetName(), "", b ? b->GetDrawOption() : "");
138  } else {
139  if ((fBranch->GetListOfLeaves()->GetEntries() > 1) ||
140  (strcmp(fBranch->GetName(), GetName()) != 0)) {
142  if (!name.EndsWith(".")) name += ".";
143  name += GetName();
144  fBranch->GetTree()->Draw(name, "", b ? b->GetDrawOption() : "");
145  } else {
146  fBranch->GetTree()->Draw(GetName(), "", b ? b->GetDrawOption() : "");
147  }
148  }
149  if (gPad) {
150  gPad->Update();
151  }
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Pack leaf elements in Basket output buffer.
156 
158 {
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// If the class supports it, generate an offset array base.
163 ///
164 /// This class only returns `nullptr` on error.
166 {
167  // In order to avoid a virtual call, we assume ROOT developers will override
168  // the default GenerateOffsetArray for cases where this function does not apply.
169 
170  Int_t *retval = new Int_t[events];
171  if (R__unlikely(!retval || !fLeafCount)) {
172  delete [] retval;
173  return nullptr;
174  }
175 
176  Long64_t orig_entry = std::max(fBranch->GetReadEntry(), 0LL); // -1 indicates to start at the beginning
177  const std::vector<Int_t> *countValues = fLeafCount->GetLeafCountValues(orig_entry, events);
178 
179  if (!countValues || ((Int_t)countValues->size()) < events) {
180  Error("GenerateOffsetArrayBase", "The leaf %s could not retrieve enough entries from its branch count (%s), ask for %d and got %ld",
181  GetName(), fLeafCount->GetName(), events, (long)(countValues ? countValues->size() : -1));
182  delete [] retval;
183  return nullptr;
184  }
185 
186  Int_t header = GetOffsetHeaderSize();
187  Int_t len = 0;
188  for (Int_t idx = 0, offset = base; idx < events; idx++) {
189  retval[idx] = offset;
190  len = (*countValues)[idx];
191  offset += fLenType * len + header;
192  }
193 
194  return retval;
195 }
196 
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Return the full name (including the parent's branch names) of the leaf.
200 
202 {
203  TString branchname = GetBranch()->GetFullName();
204  if (branchname.Length() && (branchname[branchname.Length()-1] == '.'))
205  return branchname + GetName();
206  else
207  return branchname + "." + GetName();
208 }
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// Return a pointer to the counter of this leaf (if any) or store the number of elements that the leaf contains in
212 /// countval.
213 ///
214 /// - If leaf name has the form var[nelem], where nelem is alphanumeric, then
215 /// if nelem is a leaf name, return countval = 1 and the pointer to
216 /// the leaf named nelem, otherwise return 0.
217 /// - If leaf name has the form var[nelem], where nelem is a non-negative integer, then
218 /// return countval = nelem and a null pointer.
219 /// - If leaf name has the form of a multi-dimensional array (e.g. var[nelem][nelem2]
220 /// where nelem and nelem2 are non-negative integers) then
221 /// return countval = product of all dimension sizes and a null pointer.
222 /// - If leaf name has the form var[... (and does not match the previous 2
223 /// cases) return countval = -1 and null pointer;
224 /// - Otherwise return countval = 1 and a null pointer.
225 
227 {
228  countval = 1;
229  const char* name = GetTitle();
230  char* bleft = (char*) strchr(name, '[');
231  if (!bleft) {
232  return 0;
233  }
234  bleft++;
235  Int_t nch = strlen(bleft);
236  char* countname = new char[nch+1];
237  strcpy(countname, bleft);
238  char* bright = (char*) strchr(countname, ']');
239  if (!bright) {
240  delete[] countname;
241  countname = 0;
242  countval = -1;
243  return 0;
244  }
245  char *bleft2 = (char*) strchr(countname, '[');
246  *bright = 0;
247  nch = strlen(countname);
248 
249  // Now search a branch name with a leaf name = countname
250  if (fBranch == 0) {
251  Error("GetLeafCounter","TLeaf %s is not setup properly, fBranch is null.",GetName());
252  delete[] countname;
253  return 0;
254  }
255  if (fBranch->GetTree() == 0) {
256  Error("GetLeafCounter","For Leaf %s, the TBranch %s is not setup properly, fTree is null.",GetName(),fBranch->GetName());
257  delete[] countname;
258  return 0;
259  }
260  TTree* pTree = fBranch->GetTree();
261 
262  TLeaf* leaf = (TLeaf*) GetBranch()->GetListOfLeaves()->FindObject(countname);
263  if (leaf == 0) {
264  // Try outside the branch:
265  leaf = (TLeaf*) pTree->GetListOfLeaves()->FindObject(countname);
266  }
267  //if not found, make one more trial in case the leaf name has a "."
268  if (!leaf && strchr(GetName(), '.')) {
269  char* withdot = new char[strlen(GetName())+strlen(countname)+1];
270  strcpy(withdot, GetName());
271  char* lastdot = strrchr(withdot, '.');
272  strcpy(lastdot, countname);
273  leaf = (TLeaf*) pTree->GetListOfLeaves()->FindObject(countname);
274  delete[] withdot;
275  withdot = 0;
276  }
277  if (!leaf && strchr(countname,'.')) {
278  // Not yet found and the countname has a dot in it, let's try
279  // to find the leaf using its full name
280  leaf = pTree->FindLeaf(countname);
281  }
282  Int_t i = 0;
283  if (leaf) {
284  countval = 1;
285  leaf->SetRange();
286  if (bleft2) {
287  sscanf(bleft2, "[%d]", &i);
288  countval *= i;
289  }
290  bleft = bleft2;
291  while (bleft) {
292  bleft2++;
293  bleft = (char*) strchr(bleft2, '[');
294  if (!bleft) {
295  break;
296  }
297  sscanf(bleft, "[%d]", &i);
298  countval *= i;
299  bleft2 = bleft;
300  }
301  delete[] countname;
302  countname = 0;
303  return leaf;
304  }
305  // not found in a branch/leaf. Is it a numerical value?
306  for (i = 0; i < nch; i++) {
307  if (!isdigit(countname[i])) {
308  delete[] countname;
309  countname = 0;
310  countval = -1;
311  return 0;
312  }
313  }
314  sscanf(countname, "%d", &countval);
315  if (bleft2) {
316  sscanf(bleft2, "[%d]", &i);
317  countval *= i;
318  }
319  bleft = bleft2;
320  while (bleft) {
321  bleft2++;
322  bleft = (char*) strchr(bleft2, '[');
323  if (!bleft) {
324  break;
325  }
326  sscanf(bleft, "[%d]", &i);
327  countval *= i;
328  bleft2 = bleft;
329  }
330 
331  delete[] countname;
332  countname = 0;
333  return 0;
334 }
335 
336 ////////////////////////////////////////////////////////////////////////////////
337 /// If this branch is a branch count, return the set of collection size for
338 /// the entry range requested
339 /// start: first entry to read and return information about
340 /// len: number of entries to read.
342 {
343  if (len <= 0 || !IsRange())
344  return nullptr;
345 
346  if (fLeafCountValues) {
347  if (fLeafCountValues->fStartEntry == start && len < (Long64_t)fLeafCountValues->fValues.size())
348  {
349  return &fLeafCountValues->fValues;
350  }
351  if (start >= fLeafCountValues->fStartEntry &&
352  (start+len) <= (Long64_t)(fLeafCountValues->fStartEntry + fLeafCountValues->fValues.size()))
353  {
354  auto &values(fLeafCountValues->fValues);
355  values.erase(values.begin(), values.begin() + start-fLeafCountValues->fStartEntry);
356  return &values;
357  }
358  } else {
360  }
361 
362 
363  fLeafCountValues->fValues.clear();
364  fLeafCountValues->fValues.reserve(len);
365  fLeafCountValues->fStartEntry = start;
366 
367  auto branch = GetBranch();
368  Long64_t orig_leaf_entry = branch->GetReadEntry();
369  for (Long64_t idx = 0; idx < len; ++idx) {
370  branch->GetEntry(start + idx);
371  auto size = static_cast<Int_t>(GetValue());
372  fLeafCountValues->fValues.push_back( size );
373  }
374  branch->GetEntry(orig_leaf_entry);
375  return &(fLeafCountValues->fValues);
376 }
377 
378 ////////////////////////////////////////////////////////////////////////////////
379 /// Return the number of effective elements of this leaf, for the current entry.
380 
382 {
383  if (fLeafCount) {
384  // -- We are a varying length array.
385  Int_t len = Int_t(fLeafCount->GetValue());
386  if (len > fLeafCount->GetMaximum()) {
387  Error("GetLen", "Leaf counter is greater than maximum! leaf: '%s' len: %d max: %d", GetName(), len, fLeafCount->GetMaximum());
388  len = fLeafCount->GetMaximum();
389  }
390  return len * fLen;
391  } else {
392  // -- We are a fixed size thing.
393  return fLen;
394  }
395 }
396 
397 ////////////////////////////////////////////////////////////////////////////////
398 /// Helper routine for TLeafX::SetAddress.
399 ///
400 /// The return value is non-zero if we owned the old
401 /// value buffer and must delete it now. The size
402 /// of the value buffer is recalculated and stored,
403 /// and a decision is made whether or not we own the
404 /// new value buffer.
405 
406 Int_t TLeaf::ResetAddress(void* addr, Bool_t calledFromDestructor)
407 {
408  // The kNewValue bit records whether or not we own
409  // the current value buffer or not. If we own it,
410  // then we are responsible for deleting it.
411  Bool_t deleteValue = kFALSE;
412  if (TestBit(kNewValue)) {
413  deleteValue = kTRUE;
414  }
415  // If we are not being called from a destructor,
416  // recalculate the value buffer size and decide
417  // whether or not we own the new value buffer.
418  if (!calledFromDestructor) {
419  // -- Recalculate value buffer size and decide ownership of value.
420  if (fLeafCount) {
421  // -- Varying length array data member.
422  fNdata = (fLeafCount->GetMaximum() + 1) * fLen;
423  } else {
424  // -- Fixed size data member.
425  fNdata = fLen;
426  }
427  // If we were provided an address, then we do not own
428  // the value, otherwise we do and must delete it later,
429  // keep track of this with bit kNewValue.
430  if (addr) {
432  } else {
433  SetBit(kNewValue);
434  }
435  }
436  return deleteValue;
437 }
438 
439 ////////////////////////////////////////////////////////////////////////////////
440 /// Set the leaf count of this leaf.
441 
443 {
444  if (IsZombie() && (fLen == -1) && leaf) {
445  // The constructor noted that it could not find the
446  // leafcount. Now that we did find it, let's remove
447  // the side-effects.
448  ResetBit(kZombie);
449  fLen = 1;
450  }
451  fLeafCount = leaf;
452 }
453 
454 ////////////////////////////////////////////////////////////////////////////////
455 /// Stream a class object.
456 
457 void TLeaf::Streamer(TBuffer &b)
458 {
459  if (b.IsReading()) {
460  UInt_t R__s, R__c;
461  Version_t R__v = b.ReadVersion(&R__s, &R__c);
462  if (R__v > 1) {
463  b.ReadClassBuffer(TLeaf::Class(), this, R__v, R__s, R__c);
464  } else {
465  // -- Process old versions before automatic schema evolution.
466  TNamed::Streamer(b);
467  b >> fLen;
468  b >> fLenType;
469  b >> fOffset;
470  b >> fIsRange;
471  b >> fIsUnsigned;
472  b >> fLeafCount;
473  b.CheckByteCount(R__s, R__c, TLeaf::IsA());
474  }
475  if (!fLen) {
476  fLen = 1;
477  }
478  // We do not own the value buffer right now.
480  SetAddress();
481  } else {
482  b.WriteClassBuffer(TLeaf::Class(), this);
483  }
484 }
485 
TBrowser
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TObject::TestBit
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
TLeaf::GetLeafCounter
virtual TLeaf * GetLeafCounter(Int_t &countval) const
Return a pointer to the counter of this leaf (if any) or store the number of elements that the leaf c...
Definition: TLeaf.cxx:226
Version_t
short Version_t
Definition: RtypesCore.h:65
TObjArray
An array of TObjects.
Definition: TObjArray.h:37
TNamed::operator=
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:51
TLeaf::SetRange
virtual void SetRange(Bool_t range=kTRUE)
Definition: TLeaf.h:164
tree
Definition: tree.py:1
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TLeaf::GetLeafCountValues
virtual const Counts_t * GetLeafCountValues(Long64_t start, Long64_t len)
If this branch is a branch count, return the set of collection size for the entry range requested sta...
Definition: TLeaf.cxx:341
TObjArray::Remove
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TObjArray.cxx:719
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
TLeaf::Counts_t
std::vector< Int_t > Counts_t
Definition: TLeaf.h:65
TBranch.h
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TBranch::GetReadEntry
Long64_t GetReadEntry() const
Definition: TBranch.h:233
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
Int_t
int Int_t
Definition: RtypesCore.h:45
TBrowser.h
TLeaf::LeafCountValues
Definition: TLeaf.h:66
TNamed::fName
TString fName
Definition: TNamed.h:32
TLeaf::GetOffsetHeaderSize
virtual Int_t GetOffsetHeaderSize() const
Definition: TLeaf.h:61
TObjArray::GetEntries
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:523
TString::Length
Ssiz_t Length() const
Definition: TString.h:410
TLeaf::SetAddress
virtual void SetAddress(void *add=0)
Definition: TLeaf.h:184
TLeaf::GetValue
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:182
TBuffer
Buffer base class used for serializing objects.
Definition: TBuffer.h:43
TLeaf::GetFullName
virtual TString GetFullName() const
Return the full name (including the parent's branch names) of the leaf.
Definition: TLeaf.cxx:201
TTree.h
TString
Basic string class.
Definition: TString.h:136
TLeaf::GenerateOffsetArrayBase
Int_t * GenerateOffsetArrayBase(Int_t base, Int_t events) const
If the class supports it, generate an offset array base.
Definition: TLeaf.cxx:165
b
#define b(i)
Definition: RSha256.hxx:100
bool
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TLeaf::LeafCountValues::fStartEntry
Long64_t fStartEntry
! entry number of corresponding to element 0 of the vector.
Definition: TLeaf.h:68
TLeaf::fLenType
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:73
TBranch
A TTree is a list of TBranches.
Definition: TBranch.h:89
TLeaf::LeafCountValues::fValues
Counts_t fValues
Definition: TLeaf.h:67
TObjArray::GetLast
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:577
TBuffer.h
TLeaf.h
TObject::ResetBit
void ResetBit(UInt_t f)
Definition: TObject.h:171
TLeaf::FillBasket
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeaf.cxx:157
TLeaf::kNewValue
@ kNewValue
Set if we own the value buffer and so must delete it ourselves.
Definition: TLeaf.h:96
TLeaf
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:57
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TLeaf::fLen
Int_t fLen
Number of fixed length elements in the leaf's data.
Definition: TLeaf.h:72
TLeaf::fLeafCountValues
LeafCountValues * fLeafCountValues
! Cache of collection/array sizes
Definition: TLeaf.h:79
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
R__unlikely
#define R__unlikely(expr)
Definition: RConfig.hxx:604
TTree::Draw
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:427
TVirtualPad.h
TLeaf::SetLeafCount
virtual void SetLeafCount(TLeaf *leaf)
Set the leaf count of this leaf.
Definition: TLeaf.cxx:442
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TLeaf::IsRange
virtual Bool_t IsRange() const
Definition: TLeaf.h:148
TObject::MakeZombie
void MakeZombie()
Definition: TObject.h:49
TLeaf::fIsRange
Bool_t fIsRange
(=kTRUE if leaf has a range, kFALSE otherwise). This is equivalent to being a 'leafcount'....
Definition: TLeaf.h:75
TLeaf::fIsUnsigned
Bool_t fIsUnsigned
(=kTRUE if unsigned, kFALSE otherwise)
Definition: TLeaf.h:76
TLeaf::ResetAddress
Int_t ResetAddress(void *add, Bool_t destructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:406
TTree::FindLeaf
virtual TLeaf * FindLeaf(const char *name)
Find leaf..
Definition: TTree.cxx:4846
TLeaf::fOffset
Int_t fOffset
Offset in ClonesArray object (if one)
Definition: TLeaf.h:74
TObject::IsZombie
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:134
TLeaf::fNdata
Int_t fNdata
! Number of elements in fAddress data buffer.
Definition: TLeaf.h:71
TLeaf::GetLen
virtual Int_t GetLen() const
Return the number of effective elements of this leaf, for the current entry.
Definition: TLeaf.cxx:381
TLeaf::GetBranch
TBranch * GetBranch() const
Definition: TLeaf.h:115
TLeaf::GetMaximum
virtual Int_t GetMaximum() const
Definition: TLeaf.h:133
TObjArray::FindObject
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
Definition: TObjArray.cxx:415
TLeaf::TLeaf
TLeaf()
Definition: TLeaf.cxx:32
TObject::kZombie
@ kZombie
object ctor failed
Definition: TObject.h:79
name
char name[80]
Definition: TGX11.cxx:110
gPad
#define gPad
Definition: TVirtualPad.h:287
TLeaf::fLeafCount
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:77
TLeaf::operator=
TLeaf & operator=(const TLeaf &)
Assignment operator.
Definition: TLeaf.cxx:94
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TBranch::GetFullName
virtual TString GetFullName() const
Return the 'full' name of the branch.
Definition: TBranch.cxx:1947
Class
void Class()
Definition: Class.C:29
TBranch::GetListOfLeaves
TObjArray * GetListOfLeaves()
Definition: TBranch.h:243
TBranch::GetTree
TTree * GetTree() const
Definition: TBranch.h:248
TLeaf::Browse
virtual void Browse(TBrowser *b)
Browse the content of this leaf.
Definition: TLeaf.cxx:134
TTree::GetListOfLeaves
virtual TObjArray * GetListOfLeaves()
Definition: TTree.h:484
TLeaf::fBranch
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:78
int
TLeaf::~TLeaf
virtual ~TLeaf()
Destructor.
Definition: TLeaf.cxx:117