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