Logo ROOT   6.07/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 \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 /// Import element from ClonesArray into local leaf buffer.
95 
97 {
98  Int_t j = 0;
99  for (Int_t i=0;i<n;i++) {
100  memcpy(&fValue[j],(char*)list->UncheckedAt(i) + fOffset, 1);
101  j += fLen;
102  }
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Prints leaf value.
107 
109 {
110  char *value = (char*)GetValuePointer();
111  printf("%s",value);
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Read leaf elements from Basket input buffer.
116 
118 {
119  // Try to deal with the file written during the time where len was not
120  // written to disk when len was == 0.
121  Int_t readbasket = GetBranch()->GetReadBasket();
122  TBasket *basket = GetBranch()->GetBasket(readbasket);
123  if (!basket) {
124  fValue[0] = '\0';
125  return;
126  }
127  Int_t* entryOffset = basket->GetEntryOffset();
128  if (entryOffset) {
129  Long64_t first = GetBranch()->GetBasketEntry()[readbasket];
130  Long64_t entry = GetBranch()->GetReadEntry();
131  if ( (readbasket == GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetEntries()) /* Very last entry */
132  ||
133  (readbasket < GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetBasketEntry()[readbasket+1] ) /* Last entry of the basket */
134  )
135  {
136  if ( entryOffset[entry-first] == basket->GetLast() ) /* The 'read' point is at the end of the basket */
137  {
138  // Empty string
139  fValue[0] = '\0';
140  return;
141  }
142  }
143  else if ( entryOffset[entry-first] == entryOffset[entry-first+1] ) /* This string did not use up any space in the buffer */
144  {
145  // Empty string
146  fValue[0] = '\0';
147  return;
148  }
149  }
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Read leaf elements from Basket input buffer and export buffer to
155 /// TClonesArray objects.
156 
158 {
159  UChar_t len;
160  b >> len;
161  if (len) {
162  if (len >= fLen) len = fLen-1;
163  b.ReadFastArray(fValue,len);
164  fValue[len] = 0;
165  } else {
166  fValue[0] = 0;
167  }
168 
169  Int_t j = 0;
170  for (Int_t i=0;i<n;i++) {
171  memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
172  j += fLen;
173  }
174 }
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// Read a string from std::istream s up to delimiter and store it into the branch
178 /// buffer.
179 
180 void TLeafC::ReadValue(std::istream &s, Char_t delim /*= ' '*/)
181 {
182  std::string temp;
183  std::getline(s, temp, delim);
184  if (TestBit(kNewValue) &&
185  (temp.length()+1 > ((UInt_t)fNdata))) {
186  // Grow buffer if needed and we created the buffer.
187  fNdata = ((UInt_t)temp.size()) + 1;
189  delete [] *fPointer;
190  *fPointer = new char[fNdata];
191  } else {
192  fValue = new char[fNdata];
193  }
194  }
195  strlcpy(fValue,temp.c_str(),fNdata);
196 }
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Set leaf buffer data address.
200 
201 void TLeafC::SetAddress(void *add)
202 {
203  if (ResetAddress(add)) {
204  delete [] fValue;
205  }
206  if (add) {
207  if (TestBit(kIndirectAddress)) {
208  fPointer = (char**)add;
209  Int_t ncountmax = fLen;
210  if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
211  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
212  ncountmax > fNdata || *fPointer == 0) {
213  if (*fPointer) delete [] *fPointer;
214  if (ncountmax > fNdata) fNdata = ncountmax;
215  *fPointer = new char[fNdata];
216  }
217  fValue = *fPointer;
218  } else {
219  fValue = (char*)add;
220  }
221  }
222  else {
223  fValue = new char[fNdata];
224  fValue[0] = 0;
225  }
226 }
Long64_t GetReadEntry() const
Definition: TBranch.h:170
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:37
Set if we own the value buffer and so must delete it ourselves.
Definition: TLeaf.h:60
Int_t * GetEntryOffset() const
Definition: TBasket.h:77
long long Long64_t
Definition: RtypesCore.h:69
Int_t fMaximum
Maximum value if leaf range is specified.
Definition: TLeafC.h:32
TLeafC()
Default constructor for LeafC.
Definition: TLeafC.cxx:30
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
virtual void * GetValuePointer() const
Definition: TLeafC.h:47
int Int_t
Definition: RtypesCore.h:41
Int_t fMinimum
Minimum value if leaf range is specified.
Definition: TLeafC.h:31
TBranch * GetBranch() const
Definition: TLeaf.h:70
Char_t * fValue
! Pointer to data buffer
Definition: TLeafC.h:33
A TLeaf for a variable length string.
Definition: TLeafC.h:28
Long64_t * GetBasketEntry() const
Definition: TBranch.h:150
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:43
const char * GetTypeName() const
Returns name of leaf type.
Definition: TLeafC.cxx:87
virtual void WriteFastArrayString(const Char_t *c, Int_t n)=0
Data member is a pointer to an array of basic types.
Definition: TLeaf.h:59
TBasket * GetBasket(Int_t basket)
Return pointer to basket basketnumber in this Branch.
Definition: TBranch.cxx:1095
Int_t fLen
Number of fixed length elements.
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:180
Int_t fNdata
! Number of elements in fAddress data buffer
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:159
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:157
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
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Definition: TLeafC.cxx:117
#define ClassImp(name)
Definition: Rtypes.h:279
int type
Definition: TGX11.cxx:120
Bool_t fIsUnsigned
(=kTRUE if unsigned, kFALSE otherwise)
Definition: TLeaf.h:46
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition: TLeafC.cxx:96
Char_t ** fPointer
! Address of pointer to data buffer
Definition: TLeafC.h:34
Int_t fOffset
Offset in ClonesArray object (if one)
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:108
Int_t ResetAddress(void *add, Bool_t destructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:303
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:47
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
A TTree is a list of TBranches.
Definition: TBranch.h:58
const Bool_t kTRUE
Definition: Rtypes.h:91
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109
Int_t GetReadBasket() const
Definition: TBranch.h:169
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
Definition: TLeafC.cxx:201