Logo ROOT   6.10/09
Reference Guide
TLeafB.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 TLeafB
13 \ingroup tree
14 
15 A TLeaf for an 8 bit Integer data type.
16 */
17 
18 #include "TLeafB.h"
19 #include "TBranch.h"
20 #include "TBuffer.h"
21 #include "TClonesArray.h"
22 #include "Riostream.h"
23 
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Default constructor.
28 
30 : TLeaf()
31 , fMinimum(0)
32 , fMaximum(0)
33 , fValue(0)
34 , fPointer(0)
35 {
36  fLenType = 1;
37 }
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Create a LeafB.
41 
42 TLeafB::TLeafB(TBranch *parent, const char* name, const char* type)
43  : TLeaf(parent, name, type)
44  , fMinimum(0)
45  , fMaximum(0)
46  , fValue(0)
47  , fPointer(0)
48 {
49  fLenType = 1;
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Destructor.
54 
56 {
57  if (ResetAddress(0, kTRUE)) {
58  delete[] fValue;
59  fValue = 0;
60  }
61  // Note: We do not own this, the user's object does.
62  fPointer = 0;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Export element from local leaf buffer to a ClonesArray.
67 
69 {
70  for (Int_t i = 0, j = 0; i < n; i++, j += fLen) {
71  memcpy(((char*) list->UncheckedAt(i)) + fOffset, &fValue[j], fLen);
72  }
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Pack leaf elements into Basket output buffer.
77 
79 {
80  Int_t len = GetLen();
81  if (fPointer) {
82  fValue = *fPointer;
83  }
84  if (IsRange()) {
85  if (fValue[0] > fMaximum) {
86  fMaximum = fValue[0];
87  }
88  }
89  if (IsUnsigned()) {
90  for (Int_t i = 0; i < len; i++) {
91  b << (UChar_t) fValue[i];
92  }
93  } else {
94  b.WriteFastArray(fValue, len);
95  }
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Returns name of leaf type.
100 
101 const char *TLeafB::GetTypeName() const
102 {
103  if (fIsUnsigned) {
104  return "UChar_t";
105  }
106  return "Char_t";
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Import element from ClonesArray into local leaf buffer.
111 
113 {
114  for (Int_t i = 0, j = 0; i < n; i++, j+= fLen) {
115  memcpy(&fValue[j], ((char*) list->UncheckedAt(i)) + fOffset, fLen);
116  }
117 }
118 
119 ////////////////////////////////////////////////////////////////////////////////
120 /// Prints leaf value.
121 
123 {
124  if (fIsUnsigned) {
125  UChar_t *uvalue = (UChar_t*) GetValuePointer();
126  printf("%u", uvalue[l]);
127  } else {
128  Char_t *value = (Char_t*) GetValuePointer();
129  printf("%d", value[l]);
130  }
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Read leaf elements from Basket input buffer.
135 
137 {
138  if (!fLeafCount && (fNdata == 1)) {
139  b.ReadChar(fValue[0]);
140  } else {
141  if (fLeafCount) {
142  Long64_t entry = fBranch->GetReadEntry();
143  if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
144  fLeafCount->GetBranch()->GetEntry(entry);
145  }
146  Int_t len = Int_t(fLeafCount->GetValue());
147  if (len > fLeafCount->GetMaximum()) {
148  Error("ReadBasket", "leaf: '%s' len: %d max: %d", GetName(), len, fLeafCount->GetMaximum());
149  len = fLeafCount->GetMaximum();
150  }
151  fNdata = len * fLen;
152  b.ReadFastArray(fValue, len*fLen);
153  } else {
155  }
156  }
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Read leaf elements from Basket input buffer and export buffer to
161 /// TClonesArray objects.
162 
164 {
165  b.ReadFastArray(fValue, n*fLen);
166 
167  for (Int_t i = 0, j = 0; i < n; i++, j += fLen) {
168  memcpy(((char*) list->UncheckedAt(i)) + fOffset, &fValue[j], fLen);
169  }
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Read a 8 bit integer from std::istream s and store it into the branch buffer.
174 
175 void TLeafB::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
176 {
177  if (fIsUnsigned) {
178  UChar_t *uvalue = (UChar_t*)GetValuePointer();
179  for (Int_t i=0;i<fLen;i++) {
180  UShort_t tmp;
181  s >> tmp;
182  uvalue[i] = tmp;
183  }
184  } else {
185  Char_t *value = (Char_t*)GetValuePointer();
186  for (Int_t i=0;i<fLen;i++) {
187  Short_t tmp;
188  s >> tmp;
189  value[i] = tmp;
190  }
191  }
192 }
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Set value buffer address.
196 
197 void TLeafB::SetAddress(void *addr)
198 {
199  // Check ownership of the value buffer and
200  // calculate a new size for it.
201  if (ResetAddress(addr)) {
202  // -- We owned the old value buffer, delete it.
203  delete[] fValue;
204  fValue = 0;
205  }
206  if (addr) {
207  // -- We have been provided a new value buffer.
208  if (TestBit(kIndirectAddress)) {
209  // -- The data member is a pointer to an array.
210  fPointer = (Char_t**) addr;
211  // Calculate the maximum size we have ever needed
212  // for the value buffer.
213  Int_t ncountmax = fLen;
214  if (fLeafCount) {
215  ncountmax = (fLeafCount->GetMaximum() + 1) * fLen;
216  }
217  // Reallocate the value buffer if needed.
218  if ((fLeafCount && (Int_t(fLeafCount->GetValue()) < ncountmax)) ||
219  (fNdata < ncountmax) ||
220  (*fPointer == 0)) {
221  // -- Reallocate.
222  // Note:
223  // 1) For a varying length array we do this based on
224  // an indirect estimator of the size of the value
225  // buffer since we have no record of how large it
226  // actually is. If the current length of the
227  // varying length array is less than it has been
228  // in the past, then reallocate the value buffer
229  // to the larger of either the calculated new size
230  // or the maximum size it has ever been.
231  //
232  // 2) The second condition checks if the new value
233  // buffer size calculated by ResetAddress() is
234  // smaller than the most we have ever used, and
235  // if it is, then we increase the new size and
236  // reallocate.
237  //
238  // 3) The third condition is checking to see if we
239  // have been given a value buffer, if not then
240  // we must allocate one.
241  //
242  if (fNdata < ncountmax) {
243  fNdata = ncountmax;
244  }
245  delete[] *fPointer;
246  *fPointer = 0;
247  *fPointer = new Char_t[fNdata];
248  }
249  fValue = *fPointer;
250  } else {
251  // -- The data member is fixed size.
252  // FIXME: What about fPointer???
253  fValue = (char*) addr;
254  }
255  } else {
256  // -- We must create the value buffer ourselves.
257  // Note: We are using the size calculated by ResetAddress().
258  fValue = new char[fNdata];
259  // FIXME: Why initialize at all?
260  fValue[0] = 0;
261  }
262 }
263 
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:32
A TLeaf for an 8 bit Integer data type.
Definition: TLeafB.h:26
virtual void Export(TClonesArray *list, Int_t n)
Export element from local leaf buffer to a ClonesArray.
Definition: TLeafB.cxx:68
long long Long64_t
Definition: RtypesCore.h:69
unsigned short UShort_t
Definition: RtypesCore.h:36
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:159
virtual Bool_t IsUnsigned() const
Definition: TLeaf.h:86
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
Data member is a pointer to an array of basic types.
Definition: TLeaf.h:54
int Int_t
Definition: RtypesCore.h:41
const char * GetTypeName() const
Returns name of leaf type.
Definition: TLeafB.cxx:101
Char_t fMaximum
Maximum value if leaf range is specified.
Definition: TLeafB.h:30
virtual void ReadChar(Char_t &c)=0
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:38
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition: TLeafB.cxx:112
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:117
virtual void ReadBasketExport(TBuffer &, TClonesArray *list, Int_t n)
Read leaf elements from Basket input buffer and export buffer to TClonesArray objects.
Definition: TLeafB.cxx:163
virtual Bool_t IsRange() const
Definition: TLeaf.h:85
Int_t fLen
Number of fixed length elements.
Definition: TLeaf.h:37
virtual Int_t GetLen() const
Return the number of effective elements of this leaf.
Definition: TLeaf.cxx:279
TLeafB()
Default constructor.
Definition: TLeafB.cxx:29
Int_t fNdata
! Number of elements in fAddress data buffer
Definition: TLeaf.h:36
virtual void SetAddress(void *addr=0)
Set value buffer address.
Definition: TLeafB.cxx:197
PyObject * fValue
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:873
short Short_t
Definition: RtypesCore.h:35
TLine * l
Definition: textangle.C:4
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition: TBranch.cxx:1291
virtual void ReadFastArray(Bool_t *b, Int_t n)=0
virtual void WriteFastArray(const Bool_t *b, Int_t n)=0
virtual void ReadBasket(TBuffer &)
Read leaf elements from Basket input buffer.
Definition: TLeafB.cxx:136
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:89
virtual Int_t GetMaximum() const
Definition: TLeaf.h:71
#define ClassImp(name)
Definition: Rtypes.h:336
virtual void ReadValue(std::istream &s, Char_t delim=' ')
Read a 8 bit integer from std::istream s and store it into the branch buffer.
Definition: TLeafB.cxx:175
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition: TLeafB.cxx:122
int type
Definition: TGX11.cxx:120
Bool_t fIsUnsigned
(=kTRUE if unsigned, kFALSE otherwise)
Definition: TLeaf.h:41
virtual void FillBasket(TBuffer &b)
Pack leaf elements into Basket output buffer.
Definition: TLeafB.cxx:78
Char_t ** fPointer
! Address of a pointer to data buffer!
Definition: TLeafB.h:32
Int_t fOffset
Offset in ClonesArray object (if one)
Definition: TLeaf.h:39
char Char_t
Definition: RtypesCore.h:29
virtual void * GetValuePointer() const
Definition: TLeafB.h:45
An array of clone (identical) objects.
Definition: TClonesArray.h:32
Char_t * fValue
! Pointer to data buffer
Definition: TLeafB.h:31
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:43
Int_t ResetAddress(void *add, Bool_t destructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:304
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:42
Long64_t GetReadEntry() const
Definition: TBranch.h:174
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
unsigned char UChar_t
Definition: RtypesCore.h:34
virtual ~TLeafB()
Destructor.
Definition: TLeafB.cxx:55
TBranch * GetBranch() const
Definition: TLeaf.h:65
A TTree is a list of TBranches.
Definition: TBranch.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:91
const Int_t n
Definition: legend1.C:16