Logo ROOT  
Reference Guide
TLeafC.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 17/03/97
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 TLeafC
13 \ingroup tree
14 
15 A TLeaf for a variable length string.
16 */
17 
18 #include "TLeafC.h"
19 #include "TBuffer.h"
20 #include "TBranch.h"
21 #include "TBasket.h"
22 #include "TClonesArray.h"
23 #include "strlcpy.h"
24 
25 #include <string>
26 #include <iostream>
27 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Default constructor for LeafC.
32 
34 {
35  fLenType = 1;
36  fMinimum = 0;
37  fMaximum = 0;
38  fValue = 0;
39  fPointer = 0;
40 }
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// Create a LeafC.
44 
45 TLeafC::TLeafC(TBranch *parent, const char *name, const char *type)
46  :TLeaf(parent, name,type)
47 {
48  fLenType = 1;
49  fMinimum = 0;
50  fMaximum = 0;
51  fValue = 0;
52  fPointer = 0;
53 }
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Default destructor for a LeafC.
57 
59 {
60  if (ResetAddress(0,kTRUE)) delete [] fValue;
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Export element from local leaf buffer to ClonesArray.
65 
67 {
68  Int_t j = 0;
69  for (Int_t i=0;i<n;i++) {
70  memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
71  j += fLen;
72  }
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Pack leaf elements in Basket output buffer.
77 
79 {
80  if (fPointer) fValue = *fPointer;
81  Int_t len = strlen(fValue);
82  if (len >= fMaximum) fMaximum = len+1;
83  if (len >= fLen) fLen = len+1;
84  b.WriteFastArrayString(fValue,len);
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Returns name of leaf type.
89 
90 const char *TLeafC::GetTypeName() const
91 {
92  if (fIsUnsigned) return "UChar_t";
93  return "Char_t";
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Copy/set fMinimum and fMaximum to include/be wide than those of the parameter
98 
100 {
101  if (input) {
102  if (input->GetMaximum() > this->GetMaximum())
103  this->SetMaximum( input->GetMaximum() );
104  if (input->GetMinimum() < this->GetMinimum())
105  this->SetMinimum( input->GetMinimum() );
106  return kTRUE;
107  } else {
108  return kFALSE;
109  }
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Import element from ClonesArray into local leaf buffer.
114 
116 {
117  Int_t j = 0;
118  for (Int_t i=0;i<n;i++) {
119  memcpy(&fValue[j],(char*)list->UncheckedAt(i) + fOffset, 1);
120  j += fLen;
121  }
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// Prints leaf value.
126 
128 {
129  char *value = (char*)GetValuePointer();
130  printf("%s",value);
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Read leaf elements from Basket input buffer.
135 
137 {
138  // Try to deal with the file written during the time where len was not
139  // written to disk when len was == 0.
140  Int_t readbasket = GetBranch()->GetReadBasket();
141  TBasket *basket = GetBranch()->GetBasket(readbasket);
142  if (!basket) {
143  fValue[0] = '\0';
144  return;
145  }
146  Int_t* entryOffset = basket->GetEntryOffset();
147  if (entryOffset) {
148  Long64_t first = GetBranch()->GetBasketEntry()[readbasket];
149  Long64_t entry = GetBranch()->GetReadEntry();
150  if ( (readbasket == GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetEntries()) /* Very last entry */
151  ||
152  (readbasket < GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetBasketEntry()[readbasket+1] ) /* Last entry of the basket */
153  )
154  {
155  if ( entryOffset[entry-first] == basket->GetLast() ) /* The 'read' point is at the end of the basket */
156  {
157  // Empty string
158  fValue[0] = '\0';
159  return;
160  }
161  }
162  else if ( entryOffset[entry-first] == entryOffset[entry-first+1] ) /* This string did not use up any space in the buffer */
163  {
164  // Empty string
165  fValue[0] = '\0';
166  return;
167  }
168  }
169  b.ReadFastArrayString(fValue,fLen);
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Read leaf elements from Basket input buffer and export buffer to
174 /// TClonesArray objects.
175 
177 {
178  UChar_t len;
179  b >> len;
180  if (len) {
181  if (len >= fLen) len = fLen-1;
182  b.ReadFastArray(fValue,len);
183  fValue[len] = 0;
184  } else {
185  fValue[0] = 0;
186  }
187 
188  Int_t j = 0;
189  for (Int_t i=0;i<n;i++) {
190  memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
191  j += fLen;
192  }
193 }
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// Read a string from std::istream s up to delimiter and store it into the branch
197 /// buffer.
198 
199 void TLeafC::ReadValue(std::istream &s, Char_t delim /*= ' '*/)
200 {
201  std::string temp;
202  std::getline(s, temp, delim);
203  if (TestBit(kNewValue) &&
204  (temp.length()+1 > ((UInt_t)fNdata))) {
205  // Grow buffer if needed and we created the buffer.
206  fNdata = ((UInt_t)temp.size()) + 1;
208  delete [] *fPointer;
209  *fPointer = new char[fNdata];
210  } else {
211  fValue = new char[fNdata];
212  }
213  }
214  strlcpy(fValue,temp.c_str(),fNdata);
215 }
216 
217 ////////////////////////////////////////////////////////////////////////////////
218 /// Set leaf buffer data address.
219 
220 void TLeafC::SetAddress(void *add)
221 {
222  if (ResetAddress(add)) {
223  delete [] fValue;
224  }
225  if (add) {
226  if (TestBit(kIndirectAddress)) {
227  fPointer = (char**)add;
228  Int_t ncountmax = fLen;
229  if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
230  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
231  ncountmax > fNdata || *fPointer == 0) {
232  if (*fPointer) delete [] *fPointer;
233  if (ncountmax > fNdata) fNdata = ncountmax;
234  *fPointer = new char[fNdata];
235  }
236  fValue = *fPointer;
237  } else {
238  fValue = (char*)add;
239  }
240  }
241  else {
242  fValue = new char[fNdata];
243  fValue[0] = 0;
244  }
245 }
TLeafC
Definition: TLeafC.h:26
TBranch::GetReadBasket
Int_t GetReadBasket() const
Definition: TBranch.h:232
n
const Int_t n
Definition: legend1.C:16
first
Definition: first.py:1
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
TLeafC::fValue
Char_t * fValue
! Pointer to data buffer
Definition: TLeafC.h:37
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TBranch.h
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TLeafC::fMinimum
Int_t fMinimum
Minimum value if leaf range is specified.
Definition: TLeafC.h:35
TBranch::GetReadEntry
Long64_t GetReadEntry() const
Definition: TBranch.h:233
TLeafC::GetValuePointer
virtual void * GetValuePointer() const
Definition: TLeafC.h:51
TLeafC::IncludeRange
virtual Bool_t IncludeRange(TLeaf *)
Copy/set fMinimum and fMaximum to include/be wide than those of the parameter.
Definition: TLeafC.cxx:99
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
Int_t
int Int_t
Definition: RtypesCore.h:45
TLeafC::Import
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition: TLeafC.cxx:115
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
TBranch::GetBasketEntry
Long64_t * GetBasketEntry() const
Definition: TBranch.h:211
TLeafC::Export
virtual void Export(TClonesArray *list, Int_t n)
Export element from local leaf buffer to ClonesArray.
Definition: TLeafC.cxx:66
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
TLeafC::FillBasket
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeafC.cxx:78
TBuffer.h
TLeafC::ReadBasket
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Definition: TLeafC.cxx:136
TLeaf::kNewValue
@ kNewValue
Set if we own the value buffer and so must delete it ourselves.
Definition: TLeaf.h:96
TLeafC::fPointer
Char_t ** fPointer
! Address of pointer to data buffer
Definition: TLeafC.h:38
TLeafC::~TLeafC
virtual ~TLeafC()
Default destructor for a LeafC.
Definition: TLeafC.cxx:58
TLeaf
Definition: TLeaf.h:57
TLeaf::fLen
Int_t fLen
Number of fixed length elements in the leaf's data.
Definition: TLeaf.h:72
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TLeafC.h
TLeaf::kIndirectAddress
@ kIndirectAddress
Data member is a pointer to an array of basic types.
Definition: TLeaf.h:95
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TBasket::GetEntryOffset
Int_t * GetEntryOffset()
Definition: TBasket.h:122
TLeaf::fIsUnsigned
Bool_t fIsUnsigned
(=kTRUE if unsigned, kFALSE otherwise)
Definition: TLeaf.h:76
TLeaf::ResetAddress
Int_t ResetAddress(void *add, Bool_t destructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:406
TLeaf::fOffset
Int_t fOffset
Offset in ClonesArray object (if one)
Definition: TLeaf.h:74
TLeafC::GetTypeName
const char * GetTypeName() const
Returns name of leaf type.
Definition: TLeafC.cxx:90
unsigned int
TLeaf::fNdata
Int_t fNdata
! Number of elements in fAddress data buffer.
Definition: TLeaf.h:71
TBasket::GetLast
Int_t GetLast() const
Definition: TBasket.h:129
TLeaf::GetBranch
TBranch * GetBranch() const
Definition: TLeaf.h:115
TBasket
Definition: TBasket.h:34
TLeafC::ReadValue
virtual void ReadValue(std::istream &s, Char_t delim=' ')
Read a string from std::istream s up to delimiter and store it into the branch buffer.
Definition: TLeafC.cxx:199
TLeaf::GetMaximum
virtual Int_t GetMaximum() const
Definition: TLeaf.h:133
TLeafC::SetMaximum
virtual void SetMaximum(Int_t max)
Definition: TLeafC.h:60
TLeaf::GetMinimum
virtual Int_t GetMinimum() const
Definition: TLeaf.h:134
UChar_t
unsigned char UChar_t
Definition: RtypesCore.h:38
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
TLeafC::SetMinimum
virtual void SetMinimum(Int_t min)
Definition: TLeafC.h:61
TLeafC::ReadBasketExport
virtual void ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
Read leaf elements from Basket input buffer and export buffer to TClonesArray objects.
Definition: TLeafC.cxx:176
type
int type
Definition: TGX11.cxx:121
TBranch::GetBasket
TBasket * GetBasket(Int_t basket)
Definition: TBranch.h:209
TLeafC::SetAddress
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
Definition: TLeafC.cxx:220
Char_t
char Char_t
Definition: RtypesCore.h:33
TLeafC::fMaximum
Int_t fMaximum
Maximum value if leaf range is specified.
Definition: TLeafC.h:36
TClonesArray
Definition: TClonesArray.h:29
TLeafC::PrintValue
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition: TLeafC.cxx:127
TBasket.h
TLeafC::TLeafC
TLeafC()
Default constructor for LeafC.
Definition: TLeafC.cxx:33
int