Logo ROOT  
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 <iostream>
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 /// Copy/set fMinimum and fMaximum to include/be wide than those of the parameter
111 
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 
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 
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 
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 
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 
TLeaf::IsUnsigned
virtual Bool_t IsUnsigned() const
Definition: TLeaf.h:149
l
auto * l
Definition: textangle.C:4
n
const Int_t n
Definition: legend1.C:16
TLeafB::GetValuePointer
virtual void * GetValuePointer() const
Definition: TLeafB.h:52
TLeafB::fPointer
Char_t ** fPointer
! Address of a pointer to data buffer!
Definition: TLeafB.h:38
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TObject::TestBit
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
TLeafB::Export
virtual void Export(TClonesArray *list, Int_t n)
Export element from local leaf buffer to a ClonesArray.
Definition: TLeafB.cxx:68
UShort_t
unsigned short UShort_t
Definition: RtypesCore.h:40
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TBranch.h
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TBranch::GetReadEntry
Long64_t GetReadEntry() const
Definition: TBranch.h:233
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
Int_t
int Int_t
Definition: RtypesCore.h:45
TLeaf::GetValue
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:182
TBuffer
Definition: TBuffer.h:43
TObjArray::UncheckedAt
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:90
TLeafB::GetTypeName
const char * GetTypeName() const
Returns name of leaf type.
Definition: TLeafB.cxx:101
TLeafB::ReadValue
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:191
TLeafB::FillBasket
virtual void FillBasket(TBuffer &b)
Pack leaf elements into Basket output buffer.
Definition: TLeafB.cxx:78
b
#define b(i)
Definition: RSha256.hxx:118
bool
TClonesArray.h
TLeaf::fLenType
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:73
TBranch
Definition: TBranch.h:89
TBuffer.h
TLeaf
Definition: TLeaf.h:57
TLeaf::fLen
Int_t fLen
Number of fixed length elements in the leaf's data.
Definition: TLeaf.h:72
TLeafB::TLeafB
TLeafB()
Default constructor.
Definition: TLeafB.cxx:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TBranch::GetEntry
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:1582
TLeaf::kIndirectAddress
@ kIndirectAddress
Data member is a pointer to an array of basic types.
Definition: TLeaf.h:95
TLeafB::ReadBasketExport
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:179
TLeaf::IsRange
virtual Bool_t IsRange() const
Definition: TLeaf.h:148
Short_t
short Short_t
Definition: RtypesCore.h:39
TLeaf::fIsUnsigned
Bool_t fIsUnsigned
(=kTRUE if unsigned, kFALSE otherwise)
Definition: TLeaf.h:76
TLeafB.h
TLeaf::ResetAddress
Int_t ResetAddress(void *add, Bool_t destructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:406
TLeafB::fMaximum
Char_t fMaximum
Maximum value if leaf range is specified.
Definition: TLeafB.h:36
TLeaf::fOffset
Int_t fOffset
Offset in ClonesArray object (if one)
Definition: TLeaf.h:74
TLeafB::Import
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition: TLeafB.cxx:128
TLeaf::fNdata
Int_t fNdata
! Number of elements in fAddress data buffer.
Definition: TLeaf.h:71
TLeaf::GetLen
virtual Int_t GetLen() const
Return the number of effective elements of this leaf, for the current entry.
Definition: TLeaf.cxx:381
TLeaf::GetBranch
TBranch * GetBranch() const
Definition: TLeaf.h:115
TLeaf::GetMaximum
virtual Int_t GetMaximum() const
Definition: TLeaf.h:133
TLeafB::PrintValue
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition: TLeafB.cxx:138
TLeaf::GetMinimum
virtual Int_t GetMinimum() const
Definition: TLeaf.h:134
TLeafB::SetMaximum
virtual void SetMaximum(Char_t max)
Definition: TLeafB.h:60
UChar_t
unsigned char UChar_t
Definition: RtypesCore.h:38
TLeafB::IncludeRange
virtual Bool_t IncludeRange(TLeaf *)
Copy/set fMinimum and fMaximum to include/be wide than those of the parameter.
Definition: TLeafB.cxx:112
TLeafB
Definition: TLeafB.h:26
name
char name[80]
Definition: TGX11.cxx:110
TLeaf::fLeafCount
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:77
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TLeafB::ReadBasket
virtual void ReadBasket(TBuffer &)
Read leaf elements from Basket input buffer.
Definition: TLeafB.cxx:152
type
int type
Definition: TGX11.cxx:121
TLeafB::~TLeafB
virtual ~TLeafB()
Destructor.
Definition: TLeafB.cxx:55
Char_t
char Char_t
Definition: RtypesCore.h:33
TClonesArray
Definition: TClonesArray.h:29
TLeafB::SetMinimum
virtual void SetMinimum(Char_t min)
Definition: TLeafB.h:61
TLeaf::fBranch
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:78
TLeafB::fValue
Char_t * fValue
! Pointer to data buffer
Definition: TLeafB.h:37
TLeafB::SetAddress
virtual void SetAddress(void *addr=0)
Set value buffer address.
Definition: TLeafB.cxx:213
int