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