Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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 \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 
24 ClassImp(TLeafB);
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Default constructor.
28 
29 TLeafB::TLeafB()
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 
55 TLeafB::~TLeafB()
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 
68 void TLeafB::Export(TClonesArray* list, Int_t n)
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 
78 void TLeafB::FillBasket(TBuffer& b)
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 /// Copy/set fMinimum and fMaximum to include/be wide than those of the parameter
111 
112 Bool_t TLeafB::IncludeRange(TLeaf *input)
113 {
114  if (input) {
115  if (input->GetMaximum() > this->GetMaximum())
116  this->SetMaximum( input->GetMaximum() );
117  if (input->GetMinimum() < this->GetMinimum())
118  this->SetMinimum( input->GetMinimum() );
119  return kTRUE;
120  } else {
121  return kFALSE;
122  }
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Import element from ClonesArray into local leaf buffer.
127 
128 void TLeafB::Import(TClonesArray *list, Int_t n)
129 {
130  for (Int_t i = 0, j = 0; i < n; i++, j+= fLen) {
131  memcpy(&fValue[j], ((char*) list->UncheckedAt(i)) + fOffset, fLen);
132  }
133 }
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// Prints leaf value.
137 
138 void TLeafB::PrintValue(Int_t l) const
139 {
140  if (fIsUnsigned) {
141  UChar_t *uvalue = (UChar_t*) GetValuePointer();
142  printf("%u", uvalue[l]);
143  } else {
144  Char_t *value = (Char_t*) GetValuePointer();
145  printf("%d", value[l]);
146  }
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// Read leaf elements from Basket input buffer.
151 
152 void TLeafB::ReadBasket(TBuffer &b)
153 {
154  if (!fLeafCount && (fNdata == 1)) {
155  b.ReadChar(fValue[0]);
156  } else {
157  if (fLeafCount) {
158  Long64_t entry = fBranch->GetReadEntry();
159  if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
160  fLeafCount->GetBranch()->GetEntry(entry);
161  }
162  Int_t len = Int_t(fLeafCount->GetValue());
163  if (len > fLeafCount->GetMaximum()) {
164  Error("ReadBasket", "leaf: '%s' len: %d max: %d", GetName(), len, fLeafCount->GetMaximum());
165  len = fLeafCount->GetMaximum();
166  }
167  fNdata = len * fLen;
168  b.ReadFastArray(fValue, len*fLen);
169  } else {
170  b.ReadFastArray(fValue, fLen);
171  }
172  }
173 }
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 /// Read leaf elements from Basket input buffer and export buffer to
177 /// TClonesArray objects.
178 
179 void TLeafB::ReadBasketExport(TBuffer& b, TClonesArray* list, Int_t n)
180 {
181  b.ReadFastArray(fValue, n*fLen);
182 
183  for (Int_t i = 0, j = 0; i < n; i++, j += fLen) {
184  memcpy(((char*) list->UncheckedAt(i)) + fOffset, &fValue[j], fLen);
185  }
186 }
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 /// Read a 8 bit integer from std::istream s and store it into the branch buffer.
190 
191 void TLeafB::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
192 {
193  if (fIsUnsigned) {
194  UChar_t *uvalue = (UChar_t*)GetValuePointer();
195  for (Int_t i=0;i<fLen;i++) {
196  UShort_t tmp;
197  s >> tmp;
198  uvalue[i] = tmp;
199  }
200  } else {
201  Char_t *value = (Char_t*)GetValuePointer();
202  for (Int_t i=0;i<fLen;i++) {
203  Short_t tmp;
204  s >> tmp;
205  value[i] = tmp;
206  }
207  }
208 }
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// Set value buffer address.
212 
213 void TLeafB::SetAddress(void *addr)
214 {
215  // Check ownership of the value buffer and
216  // calculate a new size for it.
217  if (ResetAddress(addr)) {
218  // -- We owned the old value buffer, delete it.
219  delete[] fValue;
220  fValue = 0;
221  }
222  if (addr) {
223  // -- We have been provided a new value buffer.
224  if (TestBit(kIndirectAddress)) {
225  // -- The data member is a pointer to an array.
226  fPointer = (Char_t**) addr;
227  // Calculate the maximum size we have ever needed
228  // for the value buffer.
229  Int_t ncountmax = fLen;
230  if (fLeafCount) {
231  ncountmax = (fLeafCount->GetMaximum() + 1) * fLen;
232  }
233  // Reallocate the value buffer if needed.
234  if ((fLeafCount && (Int_t(fLeafCount->GetValue()) < ncountmax)) ||
235  (fNdata < ncountmax) ||
236  (*fPointer == 0)) {
237  // -- Reallocate.
238  // Note:
239  // 1) For a varying length array we do this based on
240  // an indirect estimator of the size of the value
241  // buffer since we have no record of how large it
242  // actually is. If the current length of the
243  // varying length array is less than it has been
244  // in the past, then reallocate the value buffer
245  // to the larger of either the calculated new size
246  // or the maximum size it has ever been.
247  //
248  // 2) The second condition checks if the new value
249  // buffer size calculated by ResetAddress() is
250  // smaller than the most we have ever used, and
251  // if it is, then we increase the new size and
252  // reallocate.
253  //
254  // 3) The third condition is checking to see if we
255  // have been given a value buffer, if not then
256  // we must allocate one.
257  //
258  if (fNdata < ncountmax) {
259  fNdata = ncountmax;
260  }
261  delete[] *fPointer;
262  *fPointer = 0;
263  *fPointer = new Char_t[fNdata];
264  }
265  fValue = *fPointer;
266  } else {
267  // -- The data member is fixed size.
268  // FIXME: What about fPointer???
269  fValue = (char*) addr;
270  }
271  } else {
272  // -- We must create the value buffer ourselves.
273  // Note: We are using the size calculated by ResetAddress().
274  fValue = new char[fNdata];
275  // FIXME: Why initialize at all?
276  fValue[0] = 0;
277  }
278 }
279