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