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