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