Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TLeafL.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 12/01/96
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 TLeafL
13 \ingroup tree
14 
15 A TLeaf for a 64 bit Integer data type.
16 */
17 
18 #include "TLeafL.h"
19 #include "TBranch.h"
20 #include "TBuffer.h"
21 #include "TClonesArray.h"
22 #include "Riostream.h"
23 
24 ClassImp(TLeafL);
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Default constructor for LeafL.
28 
29 TLeafL::TLeafL(): TLeaf()
30 {
31  fLenType = sizeof(Long64_t);
32  fMinimum = 0;
33  fMaximum = 0;
34  fValue = 0;
35  fPointer = 0;
36 }
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Create a LeafL.
40 
41 TLeafL::TLeafL(TBranch *parent, const char *name, const char *type)
42  :TLeaf(parent, name,type)
43 {
44  fLenType = sizeof(Long64_t);
45  fMinimum = 0;
46  fMaximum = 0;
47  fValue = 0;
48  fPointer = 0;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Default destructor for a LeafL.
53 
54 TLeafL::~TLeafL()
55 {
56  if (ResetAddress(0,kTRUE)) delete [] fValue;
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Export element from local leaf buffer to ClonesArray.
61 
62 void TLeafL::Export(TClonesArray *list, Int_t n)
63 {
64  Long64_t *value = fValue;
65  for (Int_t i=0;i<n;i++) {
66  char *first = (char*)list->UncheckedAt(i);
67  Long64_t *ii = (Long64_t*)&first[fOffset];
68  for (Int_t j=0;j<fLen;j++) {
69  ii[j] = value[j];
70  }
71  value += fLen;
72  }
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Pack leaf elements in Basket output buffer.
77 
78 void TLeafL::FillBasket(TBuffer &b)
79 {
80  Int_t i;
81  Int_t len = GetLen();
82  if (fPointer) fValue = *fPointer;
83  if (IsRange()) {
84  if (fValue[0] > fMaximum) fMaximum = fValue[0];
85  }
86  if (IsUnsigned()) {
87  for (i=0;i<len;i++) b << (ULong64_t)fValue[i];
88  } else {
89  b.WriteFastArray(fValue,len);
90  }
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Returns name of leaf type.
95 
96 const char *TLeafL::GetTypeName() const
97 {
98  if (fIsUnsigned) return "ULong64_t";
99  return "Long64_t";
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 /// Returns current value of leaf.
104 /// - if leaf is a simple type, i must be set to 0
105 /// - if leaf is an array, i is the array element number to be returned
106 
107 Double_t TLeafL::GetValue(Int_t i) const
108 {
109  if (fIsUnsigned) return (Double_t)((ULong64_t)fValue[i]);
110  return fValue[i];
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Returns current value of leaf
115 /// - if leaf is a simple type, i must be set to 0
116 /// - if leaf is an array, i is the array element number to be returned
117 
118 LongDouble_t TLeafL::GetValueLongDouble(Int_t i) const
119 {
120  if (fIsUnsigned) return (LongDouble_t)((ULong64_t)fValue[i]);
121  return fValue[i];
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// Copy/set fMinimum and fMaximum to include/be wide than those of the parameter
126 
127 Bool_t TLeafL::IncludeRange(TLeaf *input)
128 {
129  if (input) {
130  if (input->GetMaximum() > this->GetMaximum())
131  this->SetMaximum( input->GetMaximum() );
132  if (input->GetMinimum() < this->GetMinimum())
133  this->SetMinimum( input->GetMinimum() );
134  return kTRUE;
135  } else {
136  return kFALSE;
137  }
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// Import element from ClonesArray into local leaf buffer.
142 
143 void TLeafL::Import(TClonesArray *list, Int_t n)
144 {
145  const Int_t kIntUndefined = -9999;
146  Int_t j = 0;
147  char *clone;
148  for (Int_t i=0;i<n;i++) {
149  clone = (char*)list->UncheckedAt(i);
150  if (clone) memcpy(&fValue[j],clone + fOffset, 8*fLen);
151  else memcpy(&fValue[j],&kIntUndefined, 8*fLen);
152  j += fLen;
153  }
154 }
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// Prints leaf value.
158 
159 void TLeafL::PrintValue(Int_t l) const
160 {
161  if (fIsUnsigned) {
162  ULong64_t *uvalue = (ULong64_t*)GetValuePointer();
163  printf("%llu",uvalue[l]);
164  } else {
165  Long64_t *value = (Long64_t*)GetValuePointer();
166  printf("%lld",value[l]);
167  }
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// Read leaf elements from Basket input buffer.
172 
173 void TLeafL::ReadBasket(TBuffer &b)
174 {
175  if (!fLeafCount && fNdata == 1) {
176  b.ReadLong64(fValue[0]);
177  } else {
178  if (fLeafCount) {
179  Long64_t entry = fBranch->GetReadEntry();
180  if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
181  fLeafCount->GetBranch()->GetEntry(entry);
182  }
183  Int_t len = Int_t(fLeafCount->GetValue());
184  if (len > fLeafCount->GetMaximum()) {
185  printf("ERROR leaf:%s, len=%d and max=%d\n",GetName(),len,fLeafCount->GetMaximum());
186  len = fLeafCount->GetMaximum();
187  }
188  fNdata = len*fLen;
189  b.ReadFastArray(fValue,len*fLen);
190  } else {
191  b.ReadFastArray(fValue,fLen);
192  }
193  }
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// Deserialize input by performing byteswap as needed.
198 bool TLeafL::ReadBasketFast(TBuffer& input_buf, Long64_t N)
199 {
200  if (R__unlikely(fLeafCount)) {return false;}
201  return input_buf.ByteSwapBuffer(fLen*N, kLong64_t);
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Read leaf elements from Basket input buffer and export buffer to
206 /// TClonesArray objects.
207 
208 void TLeafL::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
209 {
210  if (n*fLen == 1) {
211  b >> fValue[0];
212  } else {
213  b.ReadFastArray(fValue,n*fLen);
214  }
215  Long64_t *value = fValue;
216  for (Int_t i=0;i<n;i++) {
217  char *first = (char*)list->UncheckedAt(i);
218  Long64_t *ii = (Long64_t*)&first[fOffset];
219  for (Int_t j=0;j<fLen;j++) {
220  ii[j] = value[j];
221  }
222  value += fLen;
223  }
224 }
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 /// Read a long integer from std::istream s and store it into the branch buffer.
228 
229 void TLeafL::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
230 {
231 #if defined(_MSC_VER) && (_MSC_VER<1300)
232  printf("Due to a bug in VC++6, the function TLeafL::ReadValue is dummy\n");
233 #else
234  if (fIsUnsigned) {
235  ULong64_t *uvalue = (ULong64_t*)GetValuePointer();
236  for (Int_t i=0;i<fLen;i++) s >> uvalue[i];
237  } else {
238  Long64_t *value = (Long64_t*)GetValuePointer();
239  for (Int_t i=0;i<fLen;i++) s >> value[i];
240  }
241 #endif
242 }
243 
244 ////////////////////////////////////////////////////////////////////////////////
245 /// Set leaf buffer data address.
246 
247 void TLeafL::SetAddress(void *add)
248 {
249  if (ResetAddress(add) && (add!=fValue)) {
250  delete [] fValue;
251  }
252  if (add) {
253  if (TestBit(kIndirectAddress)) {
254  fPointer = (Long64_t**) add;
255  Int_t ncountmax = fLen;
256  if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
257  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
258  ncountmax > fNdata || *fPointer == 0) {
259  if (*fPointer) delete [] *fPointer;
260  if (ncountmax > fNdata) fNdata = ncountmax;
261  *fPointer = new Long64_t[fNdata];
262  }
263  fValue = *fPointer;
264  } else {
265  fValue = (Long64_t*)add;
266  }
267  } else {
268  fValue = new Long64_t[fNdata];
269  fValue[0] = 0;
270  }
271 }
272