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