Logo ROOT   6.21/01
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 "TTree.h"
22 #include "TVirtualPad.h"
23 #include "TBrowser.h"
24 #include "TClass.h"
25 
26 #include <ctype.h>
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 /// Return a pointer to the counter of this leaf (if any) or store the number of elements that the leaf contains in
199 /// countval.
200 ///
201 /// - If leaf name has the form var[nelem], where nelem is alphanumeric, then
202 /// if nelem is a leaf name, return countval = 1 and the pointer to
203 /// the leaf named nelem, otherwise return 0.
204 /// - If leaf name has the form var[nelem], where nelem is a non-negative integer, then
205 /// return countval = nelem and a null pointer.
206 /// - If leaf name has the form of a multi-dimensional array (e.g. var[nelem][nelem2]
207 /// where nelem and nelem2 are non-negative integers) then
208 /// return countval = product of all dimension sizes and a null pointer.
209 /// - If leaf name has the form var[... (and does not match the previous 2
210 /// cases) return countval = -1 and null pointer;
211 /// - Otherwise return countval = 1 and a null pointer.
212 
214 {
215  countval = 1;
216  const char* name = GetTitle();
217  char* bleft = (char*) strchr(name, '[');
218  if (!bleft) {
219  return 0;
220  }
221  bleft++;
222  Int_t nch = strlen(bleft);
223  char* countname = new char[nch+1];
224  strcpy(countname, bleft);
225  char* bright = (char*) strchr(countname, ']');
226  if (!bright) {
227  delete[] countname;
228  countname = 0;
229  countval = -1;
230  return 0;
231  }
232  char *bleft2 = (char*) strchr(countname, '[');
233  *bright = 0;
234  nch = strlen(countname);
235 
236  // Now search a branch name with a leaf name = countname
237  if (fBranch == 0) {
238  Error("GetLeafCounter","TLeaf %s is not setup properly, fBranch is null.",GetName());
239  delete[] countname;
240  return 0;
241  }
242  if (fBranch->GetTree() == 0) {
243  Error("GetLeafCounter","For Leaf %s, the TBranch %s is not setup properly, fTree is null.",GetName(),fBranch->GetName());
244  delete[] countname;
245  return 0;
246  }
247  TTree* pTree = fBranch->GetTree();
248 
249  TLeaf* leaf = (TLeaf*) GetBranch()->GetListOfLeaves()->FindObject(countname);
250  if (leaf == 0) {
251  // Try outside the branch:
252  leaf = (TLeaf*) pTree->GetListOfLeaves()->FindObject(countname);
253  }
254  //if not found, make one more trial in case the leaf name has a "."
255  if (!leaf && strchr(GetName(), '.')) {
256  char* withdot = new char[strlen(GetName())+strlen(countname)+1];
257  strcpy(withdot, GetName());
258  char* lastdot = strrchr(withdot, '.');
259  strcpy(lastdot, countname);
260  leaf = (TLeaf*) pTree->GetListOfLeaves()->FindObject(countname);
261  delete[] withdot;
262  withdot = 0;
263  }
264  if (!leaf && strchr(countname,'.')) {
265  // Not yet found and the countname has a dot in it, let's try
266  // to find the leaf using its full name
267  leaf = pTree->FindLeaf(countname);
268  }
269  Int_t i = 0;
270  if (leaf) {
271  countval = 1;
272  leaf->SetRange();
273  if (bleft2) {
274  sscanf(bleft2, "[%d]", &i);
275  countval *= i;
276  }
277  bleft = bleft2;
278  while (bleft) {
279  bleft2++;
280  bleft = (char*) strchr(bleft2, '[');
281  if (!bleft) {
282  break;
283  }
284  sscanf(bleft, "[%d]", &i);
285  countval *= i;
286  bleft2 = bleft;
287  }
288  delete[] countname;
289  countname = 0;
290  return leaf;
291  }
292  // not found in a branch/leaf. Is it a numerical value?
293  for (i = 0; i < nch; i++) {
294  if (!isdigit(countname[i])) {
295  delete[] countname;
296  countname = 0;
297  countval = -1;
298  return 0;
299  }
300  }
301  sscanf(countname, "%d", &countval);
302  if (bleft2) {
303  sscanf(bleft2, "[%d]", &i);
304  countval *= i;
305  }
306  bleft = bleft2;
307  while (bleft) {
308  bleft2++;
309  bleft = (char*) strchr(bleft2, '[');
310  if (!bleft) {
311  break;
312  }
313  sscanf(bleft, "[%d]", &i);
314  countval *= i;
315  bleft2 = bleft;
316  }
317 
318  delete[] countname;
319  countname = 0;
320  return 0;
321 }
322 
323 ////////////////////////////////////////////////////////////////////////////////
324 /// If this branch is a branch count, return the set of collection size for
325 /// the entry range requested
326 /// start: first entry to read and return information about
327 /// len: number of entries to read.
329 {
330  if (len <= 0 || !IsRange())
331  return nullptr;
332 
333  if (fLeafCountValues) {
334  if (fLeafCountValues->fStartEntry == start && len < (Long64_t)fLeafCountValues->fValues.size())
335  {
336  return &fLeafCountValues->fValues;
337  }
338  if (start >= fLeafCountValues->fStartEntry &&
339  (start+len) <= (Long64_t)(fLeafCountValues->fStartEntry + fLeafCountValues->fValues.size()))
340  {
341  auto &values(fLeafCountValues->fValues);
342  values.erase(values.begin(), values.begin() + start-fLeafCountValues->fStartEntry);
343  return &values;
344  }
345  } else {
347  }
348 
349 
350  fLeafCountValues->fValues.clear();
351  fLeafCountValues->fValues.reserve(len);
352  fLeafCountValues->fStartEntry = start;
353 
354  auto branch = GetBranch();
355  Long64_t orig_leaf_entry = branch->GetReadEntry();
356  for (Long64_t idx = 0; idx < len; ++idx) {
357  branch->GetEntry(start + idx);
358  auto size = static_cast<Int_t>(GetValue());
359  fLeafCountValues->fValues.push_back( size );
360  }
361  branch->GetEntry(orig_leaf_entry);
362  return &(fLeafCountValues->fValues);
363 }
364 
365 ////////////////////////////////////////////////////////////////////////////////
366 /// Return the number of effective elements of this leaf, for the current entry.
367 
369 {
370  if (fLeafCount) {
371  // -- We are a varying length array.
372  Int_t len = Int_t(fLeafCount->GetValue());
373  if (len > fLeafCount->GetMaximum()) {
374  Error("GetLen", "Leaf counter is greater than maximum! leaf: '%s' len: %d max: %d", GetName(), len, fLeafCount->GetMaximum());
375  len = fLeafCount->GetMaximum();
376  }
377  return len * fLen;
378  } else {
379  // -- We are a fixed size thing.
380  return fLen;
381  }
382 }
383 
384 ////////////////////////////////////////////////////////////////////////////////
385 /// Helper routine for TLeafX::SetAddress.
386 ///
387 /// The return value is non-zero if we owned the old
388 /// value buffer and must delete it now. The size
389 /// of the value buffer is recalculated and stored,
390 /// and a decision is made whether or not we own the
391 /// new value buffer.
392 
393 Int_t TLeaf::ResetAddress(void* addr, Bool_t calledFromDestructor)
394 {
395  // The kNewValue bit records whether or not we own
396  // the current value buffer or not. If we own it,
397  // then we are responsible for deleting it.
398  Bool_t deleteValue = kFALSE;
399  if (TestBit(kNewValue)) {
400  deleteValue = kTRUE;
401  }
402  // If we are not being called from a destructor,
403  // recalculate the value buffer size and decide
404  // whether or not we own the new value buffer.
405  if (!calledFromDestructor) {
406  // -- Recalculate value buffer size and decide ownership of value.
407  if (fLeafCount) {
408  // -- Varying length array data member.
409  fNdata = (fLeafCount->GetMaximum() + 1) * fLen;
410  } else {
411  // -- Fixed size data member.
412  fNdata = fLen;
413  }
414  // If we were provided an address, then we do not own
415  // the value, otherwise we do and must delete it later,
416  // keep track of this with bit kNewValue.
417  if (addr) {
419  } else {
420  SetBit(kNewValue);
421  }
422  }
423  return deleteValue;
424 }
425 
426 ////////////////////////////////////////////////////////////////////////////////
427 /// Set the leaf count of this leaf.
428 
430 {
431  if (IsZombie() && (fLen == -1) && leaf) {
432  // The constructor noted that it could not find the
433  // leafcount. Now that we did find it, let's remove
434  // the side-effects.
435  ResetBit(kZombie);
436  fLen = 1;
437  }
438  fLeafCount = leaf;
439 }
440 
441 ////////////////////////////////////////////////////////////////////////////////
442 /// Stream a class object.
443 
444 void TLeaf::Streamer(TBuffer &b)
445 {
446  if (b.IsReading()) {
447  UInt_t R__s, R__c;
448  Version_t R__v = b.ReadVersion(&R__s, &R__c);
449  if (R__v > 1) {
450  b.ReadClassBuffer(TLeaf::Class(), this, R__v, R__s, R__c);
451  } else {
452  // -- Process old versions before automatic schema evolution.
453  TNamed::Streamer(b);
454  b >> fLen;
455  b >> fLenType;
456  b >> fOffset;
457  b >> fIsRange;
458  b >> fIsUnsigned;
459  b >> fLeafCount;
460  b.CheckByteCount(R__s, R__c, TLeaf::IsA());
461  }
462  if (!fLen) {
463  fLen = 1;
464  }
465  // We do not own the value buffer right now.
467  SetAddress();
468  } else {
469  b.WriteClassBuffer(TLeaf::Class(), this);
470  }
471 }
472 
virtual ~TLeaf()
Destructor.
Definition: TLeaf.cxx:117
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:49
An array of TObjects.
Definition: TObjArray.h:37
long long Long64_t
Definition: RtypesCore.h:69
short Version_t
Definition: RtypesCore.h:61
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:328
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
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:213
#define R__unlikely(expr)
Definition: RConfig.hxx:611
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
Int_t * GenerateOffsetArrayBase(Int_t base, Int_t events) const
If the class supports it, generate an offset array base.
Definition: TLeaf.cxx:165
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TObjArray.cxx:718
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
Long64_t fStartEntry
! entry number of corresponding to element 0 of the vector.
Definition: TLeaf.h:60
Basic string class.
Definition: TString.h:131
virtual Int_t GetOffsetHeaderSize() const
Definition: TLeaf.h:53
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:65
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
virtual void SetRange(Bool_t range=kTRUE)
Definition: TLeaf.h:155
std::vector< Int_t > Counts_t
Definition: TLeaf.h:57
void Class()
Definition: Class.C:29
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:173
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
Definition: TObjArray.cxx:414
Set if we own the value buffer and so must delete it ourselves.
Definition: TLeaf.h:88
virtual Bool_t IsRange() const
Definition: TLeaf.h:139
Counts_t fValues
Definition: TLeaf.h:59
Int_t fLen
Number of fixed length elements in the leaf&#39;s data.
Definition: TLeaf.h:64
virtual void SetAddress(void *add=0)
Definition: TLeaf.h:175
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeaf.cxx:157
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:134
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:576
virtual Int_t GetLen() const
Return the number of effective elements of this leaf, for the current entry.
Definition: TLeaf.cxx:368
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:51
Int_t fNdata
! Number of elements in fAddress data buffer.
Definition: TLeaf.h:63
LeafCountValues * fLeafCountValues
! Cache of collection/array sizes
Definition: TLeaf.h:71
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
virtual TLeaf * FindLeaf(const char *name)
Find leaf..
Definition: TTree.cxx:4748
virtual void SetLeafCount(TLeaf *leaf)
Set the leaf count of this leaf.
Definition: TLeaf.cxx:429
TString fName
Definition: TNamed.h:32
const Bool_t kFALSE
Definition: RtypesCore.h:88
virtual Int_t GetMaximum() const
Definition: TLeaf.h:124
#define ClassImp(name)
Definition: Rtypes.h:365
virtual void Browse(TBrowser *b)
Browse the content of this leaf.
Definition: TLeaf.cxx:134
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:411
Bool_t fIsUnsigned
(=kTRUE if unsigned, kFALSE otherwise)
Definition: TLeaf.h:68
TObjArray * GetListOfLeaves()
Definition: TBranch.h:240
Int_t fOffset
Offset in ClonesArray object (if one)
Definition: TLeaf.h:66
Bool_t fIsRange
(=kTRUE if leaf has a range, kFALSE otherwise). This is equivalent to being a &#39;leafcount&#39;. For a TLeafElement the range information is actually store in the TBranchElement.
Definition: TLeaf.h:67
object ctor failed
Definition: TObject.h:79
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:70
Int_t ResetAddress(void *add, Bool_t destructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:393
void MakeZombie()
Definition: TObject.h:49
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:69
Long64_t GetReadEntry() const
Definition: TBranch.h:230
TLeaf()
Definition: TLeaf.cxx:32
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
TTree * GetTree() const
Definition: TBranch.h:245
#define gPad
Definition: TVirtualPad.h:286
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:522
Definition: tree.py:1
A TTree represents a columnar dataset.
Definition: TTree.h:72
void ResetBit(UInt_t f)
Definition: TObject.h:171
TLeaf & operator=(const TLeaf &)
Assignment operator.
Definition: TLeaf.cxx:94
TBranch * GetBranch() const
Definition: TLeaf.h:107
A TTree is a list of TBranches.
Definition: TBranch.h:90
const Bool_t kTRUE
Definition: RtypesCore.h:87
char name[80]
Definition: TGX11.cxx:109
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual TObjArray * GetListOfLeaves()
Definition: TTree.h:468