Logo ROOT   6.08/07
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 {
43 }
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// Create a Leaf.
47 ///
48 /// See the TTree and TBranch constructors for explanation of parameters.
49 
50 TLeaf::TLeaf(TBranch *parent, const char* name, const char *)
51  : TNamed(name, name)
52  , fNdata(0)
53  , fLen(0)
54  , fLenType(4)
55  , fOffset(0)
56  , fIsRange(kFALSE)
57  , fIsUnsigned(kFALSE)
58  , fLeafCount(0)
59  , fBranch(parent)
60 {
62 
63  if (fLen == -1) {
64  MakeZombie();
65  return;
66  }
67 
68  const char *bracket = strchr(name, '[');
69  if (bracket) fName.ReplaceAll(bracket,"");
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Copy constructor.
74 
75 TLeaf::TLeaf(const TLeaf& lf) :
76  TNamed(lf),
77  fNdata(lf.fNdata),
78  fLen(lf.fLen),
79  fLenType(lf.fLenType),
80  fOffset(lf.fOffset),
81  fIsRange(lf.fIsRange),
84  fBranch(lf.fBranch)
85 {
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// Assignment operator.
90 
92 {
93  if(this!=&lf) {
95  fNdata=lf.fNdata;
96  fLen=lf.fLen;
97  fLenType=lf.fLenType;
98  fOffset=lf.fOffset;
99  fIsRange=lf.fIsRange;
102  fBranch=lf.fBranch;
103  }
104  return *this;
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Destructor.
109 
111 {
112  if (fBranch) {
113  TTree* tree = fBranch->GetTree();
114  fBranch = 0;
115  if (tree) {
116  TObjArray *lst = tree->GetListOfLeaves();
117  if (lst->GetLast()!=-1) lst->Remove(this);
118  }
119  }
120  fLeafCount = 0;
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Browse the content of this leaf.
125 
127 {
128  if (strchr(GetName(), '.')) {
129  fBranch->GetTree()->Draw(GetName(), "", b ? b->GetDrawOption() : "");
130  } else {
131  if ((fBranch->GetListOfLeaves()->GetEntries() > 1) ||
132  (strcmp(fBranch->GetName(), GetName()) != 0)) {
134  if (!name.EndsWith(".")) name += ".";
135  name += GetName();
136  fBranch->GetTree()->Draw(name, "", b ? b->GetDrawOption() : "");
137  } else {
138  fBranch->GetTree()->Draw(GetName(), "", b ? b->GetDrawOption() : "");
139  }
140  }
141  if (gPad) {
142  gPad->Update();
143  }
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Pack leaf elements in Basket output buffer.
148 
150 {
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Return a pointer to the counter of this leaf.
155 ///
156 /// - If leaf name has the form var[nelem], where nelem is alphanumeric, then
157 /// if nelem is a leaf name, return countval = 1 and the pointer to
158 /// the leaf named nelem, otherwise return 0.
159 /// - If leaf name has the form var[nelem], where nelem is a non-negative integer, then
160 /// return countval = nelem and a null pointer.
161 /// - If leaf name has the form of a multi-dimensional array (e.g. var[nelem][nelem2]
162 /// where nelem and nelem2 are non-negative integers) then
163 /// return countval = product of all dimension sizes and a null pointer.
164 /// - If leaf name has the form var[... (and does not match the previous 2
165 /// cases) return countval = -1 and null pointer;
166 /// - Otherwise return countval = 1 and a null pointer.
167 
169 {
170  countval = 1;
171  const char* name = GetTitle();
172  char* bleft = (char*) strchr(name, '[');
173  if (!bleft) {
174  return 0;
175  }
176  bleft++;
177  Int_t nch = strlen(bleft);
178  char* countname = new char[nch+1];
179  strcpy(countname, bleft);
180  char* bright = (char*) strchr(countname, ']');
181  if (!bright) {
182  delete[] countname;
183  countname = 0;
184  countval = -1;
185  return 0;
186  }
187  char *bleft2 = (char*) strchr(countname, '[');
188  *bright = 0;
189  nch = strlen(countname);
190 
191  // Now search a branch name with a leaf name = countname
192  if (fBranch == 0) {
193  Error("GetLeafCounter","TLeaf %s is not setup properly, fBranch is null.",GetName());
194  return 0;
195  }
196  if (fBranch->GetTree() == 0) {
197  Error("GetLeafCounter","For Leaf %s, the TBranch %s is not setup properly, fTree is null.",GetName(),fBranch->GetName());
198  return 0;
199  }
200  TTree* pTree = fBranch->GetTree();
201 
202  TLeaf* leaf = (TLeaf*) GetBranch()->GetListOfLeaves()->FindObject(countname);
203  if (leaf == 0) {
204  // Try outside the branch:
205  leaf = (TLeaf*) pTree->GetListOfLeaves()->FindObject(countname);
206  }
207  //if not found, make one more trial in case the leaf name has a "."
208  if (!leaf && strchr(GetName(), '.')) {
209  char* withdot = new char[strlen(GetName())+strlen(countname)+1];
210  strcpy(withdot, GetName());
211  char* lastdot = strrchr(withdot, '.');
212  strcpy(lastdot, countname);
213  leaf = (TLeaf*) pTree->GetListOfLeaves()->FindObject(countname);
214  delete[] withdot;
215  withdot = 0;
216  }
217  if (!leaf && strchr(countname,'.')) {
218  // Not yet found and the countname has a dot in it, let's try
219  // to find the leaf using its full name
220  leaf = pTree->FindLeaf(countname);
221  }
222  Int_t i = 0;
223  if (leaf) {
224  countval = 1;
225  leaf->SetRange();
226  if (bleft2) {
227  sscanf(bleft2, "[%d]", &i);
228  countval *= i;
229  }
230  bleft = bleft2;
231  while (bleft) {
232  bleft2++;
233  bleft = (char*) strchr(bleft2, '[');
234  if (!bleft) {
235  break;
236  }
237  sscanf(bleft, "[%d]", &i);
238  countval *= i;
239  bleft2 = bleft;
240  }
241  delete[] countname;
242  countname = 0;
243  return leaf;
244  }
245  // not found in a branch/leaf. Is it a numerical value?
246  for (i = 0; i < nch; i++) {
247  if (!isdigit(countname[i])) {
248  delete[] countname;
249  countname = 0;
250  countval = -1;
251  return 0;
252  }
253  }
254  sscanf(countname, "%d", &countval);
255  if (bleft2) {
256  sscanf(bleft2, "[%d]", &i);
257  countval *= i;
258  }
259  bleft = bleft2;
260  while (bleft) {
261  bleft2++;
262  bleft = (char*) strchr(bleft2, '[');
263  if (!bleft) {
264  break;
265  }
266  sscanf(bleft, "[%d]", &i);
267  countval *= i;
268  bleft2 = bleft;
269  }
270 
271  delete[] countname;
272  countname = 0;
273  return 0;
274 }
275 
276 ////////////////////////////////////////////////////////////////////////////////
277 /// Return the number of effective elements of this leaf.
278 
280 {
281  if (fLeafCount) {
282  // -- We are a varying length array.
283  Int_t len = Int_t(fLeafCount->GetValue());
284  if (len > fLeafCount->GetMaximum()) {
285  Error("GetLen", "Leaf counter is greater than maximum! leaf: '%s' len: %d max: %d", GetName(), len, fLeafCount->GetMaximum());
286  len = fLeafCount->GetMaximum();
287  }
288  return len * fLen;
289  } else {
290  // -- We are a fixed size thing.
291  return fLen;
292  }
293 }
294 
295 ////////////////////////////////////////////////////////////////////////////////
296 /// Helper routine for TLeafX::SetAddress.
297 ///
298 /// The return value is non-zero if we owned the old
299 /// value buffer and must delete it now. The size
300 /// of the value buffer is recalculated and stored,
301 /// and a decision is made whether or not we own the
302 /// new value buffer.
303 
304 Int_t TLeaf::ResetAddress(void* addr, Bool_t calledFromDestructor)
305 {
306  // The kNewValue bit records whether or not we own
307  // the current value buffer or not. If we own it,
308  // then we are responsible for deleting it.
309  Bool_t deleteValue = kFALSE;
310  if (TestBit(kNewValue)) {
311  deleteValue = kTRUE;
312  }
313  // If we are not being called from a destructor,
314  // recalculate the value buffer size and decide
315  // whether or not we own the new value buffer.
316  if (!calledFromDestructor) {
317  // -- Recalculate value buffer size and decide ownership of value.
318  if (fLeafCount) {
319  // -- Varying length array data member.
320  fNdata = (fLeafCount->GetMaximum() + 1) * fLen;
321  } else {
322  // -- Fixed size data member.
323  fNdata = fLen;
324  }
325  // If we were provided an address, then we do not own
326  // the value, otherwise we do and must delete it later,
327  // keep track of this with bit kNewValue.
328  if (addr) {
330  } else {
331  SetBit(kNewValue);
332  }
333  }
334  return deleteValue;
335 }
336 
337 ////////////////////////////////////////////////////////////////////////////////
338 /// Set the leaf count of this leaf.
339 
341 {
342  if (IsZombie() && (fLen == -1) && leaf) {
343  // The constructor noted that it could not find the
344  // leafcount. Now that we did find it, let's remove
345  // the side-effects.
346  ResetBit(kZombie);
347  fLen = 1;
348  }
349  fLeafCount = leaf;
350 }
351 
352 ////////////////////////////////////////////////////////////////////////////////
353 /// Stream a class object.
354 
355 void TLeaf::Streamer(TBuffer &b)
356 {
357  if (b.IsReading()) {
358  UInt_t R__s, R__c;
359  Version_t R__v = b.ReadVersion(&R__s, &R__c);
360  if (R__v > 1) {
361  b.ReadClassBuffer(TLeaf::Class(), this, R__v, R__s, R__c);
362  } else {
363  // -- Process old versions before automatic schema evolution.
364  TNamed::Streamer(b);
365  b >> fLen;
366  b >> fLenType;
367  b >> fOffset;
368  b >> fIsRange;
369  b >> fIsUnsigned;
370  b >> fLeafCount;
371  b.CheckByteCount(R__s, R__c, TLeaf::IsA());
372  }
373  if (!fLen) {
374  fLen = 1;
375  }
376  // We do not own the value buffer right now.
378  SetAddress();
379  } else {
380  b.WriteClassBuffer(TLeaf::Class(), this);
381  }
382 }
383 
virtual ~TLeaf()
Destructor.
Definition: TLeaf.cxx:110
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:37
Bool_t IsReading() const
Definition: TBuffer.h:83
An array of TObjects.
Definition: TObjArray.h:39
object ctor failed
Definition: TObject.h:70
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
short Version_t
Definition: RtypesCore.h:61
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:157
virtual TLeaf * GetLeafCounter(Int_t &countval) const
Return a pointer to the counter of this leaf.
Definition: TLeaf.cxx:168
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TObjArray.cxx:653
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
const char * Class
Definition: TXMLSetup.cxx:64
Set if we own the value buffer and so must delete it ourselves.
Definition: TLeaf.h:60
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:43
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:739
virtual void SetRange(Bool_t range=kTRUE)
Definition: TLeaf.h:104
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:122
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
Definition: TObjArray.cxx:396
Int_t fLen
Number of fixed length elements.
Definition: TLeaf.h:42
virtual void SetAddress(void *add=0)
Definition: TLeaf.h:124
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeaf.cxx:149
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:528
virtual Int_t GetLen() const
Return the number of effective elements of this leaf.
Definition: TLeaf.cxx:279
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:42
Int_t fNdata
! Number of elements in fAddress data buffer
Definition: TLeaf.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:925
virtual TLeaf * FindLeaf(const char *name)
Find leaf..
Definition: TTree.cxx:4630
virtual void SetLeafCount(TLeaf *leaf)
Set the leaf count of this leaf.
Definition: TLeaf.cxx:340
TString fName
Definition: TNamed.h:36
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
virtual Int_t GetMaximum() const
Definition: TLeaf.h:76
#define ClassImp(name)
Definition: Rtypes.h:279
Bool_t IsZombie() const
Definition: TObject.h:120
virtual void Browse(TBrowser *b)
Browse the content of this leaf.
Definition: TLeaf.cxx:126
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:367
Bool_t fIsUnsigned
(=kTRUE if unsigned, kFALSE otherwise)
Definition: TLeaf.h:46
TObjArray * GetListOfLeaves()
Definition: TBranch.h:179
Int_t fOffset
Offset in ClonesArray object (if one)
Definition: TLeaf.h:44
Bool_t fIsRange
(=kTRUE if leaf has a range, kFALSE otherwise)
Definition: TLeaf.h:45
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:48
Int_t ResetAddress(void *add, Bool_t destructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:304
void MakeZombie()
Definition: TObject.h:47
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:47
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:184
#define gPad
Definition: TVirtualPad.h:289
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:98
void ResetBit(UInt_t f)
Definition: TObject.h:156
Option_t * GetDrawOption() const
Get option used by the graphics system to draw this object.
Definition: TBrowser.h:108
TLeaf & operator=(const TLeaf &)
Assignment operator.
Definition: TLeaf.cxx:91
TBranch * GetBranch() const
Definition: TLeaf.h:70
A TTree is a list of TBranches.
Definition: TBranch.h:58
const Bool_t kTRUE
Definition: Rtypes.h:91
char name[80]
Definition: TGX11.cxx:109
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
virtual TObjArray * GetListOfLeaves()
Definition: TTree.h:418