Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TLeafF.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 TLeafF
13 \ingroup tree
14 
15 A TLeaf for a 32 bit floating point data type.
16 */
17 
18 #include "TLeafF.h"
19 #include "TBranch.h"
20 #include "TBuffer.h"
21 #include "TClonesArray.h"
22 #include "Riostream.h"
23 
24 ClassImp(TLeafF);
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Default constructor for LeafF.
28 
29 TLeafF::TLeafF(): TLeaf()
30 {
31  fLenType = 4;
32  fMinimum = 0;
33  fMaximum = 0;
34  fValue = 0;
35  fPointer = 0;
36 }
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Create a LeafF.
40 
41 TLeafF::TLeafF(TBranch *parent, const char *name, const char *type)
42  :TLeaf(parent, name,type)
43 {
44  fLenType = 4;
45  fMinimum = 0;
46  fMaximum = 0;
47  fValue = 0;
48  fPointer = 0;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Default destructor for a LeafF.
53 
54 TLeafF::~TLeafF()
55 {
56  if (ResetAddress(0,kTRUE)) delete [] fValue;
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Export element from local leaf buffer to ClonesArray.
61 
62 void TLeafF::Export(TClonesArray *list, Int_t n)
63 {
64  Float_t *value = fValue;
65  for (Int_t i=0;i<n;i++) {
66  char *first = (char*)list->UncheckedAt(i);
67  Float_t *ff = (Float_t*)&first[fOffset];
68  for (Int_t j=0;j<fLen;j++) {
69  ff[j] = value[j];
70  }
71  value += fLen;
72  }
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Pack leaf elements in Basket output buffer.
77 
78 void TLeafF::FillBasket(TBuffer &b)
79 {
80  Int_t len = GetLen();
81  if (fPointer) fValue = *fPointer;
82  b.WriteFastArray(fValue,len);
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Import element from ClonesArray into local leaf buffer.
87 
88 void TLeafF::Import(TClonesArray *list, Int_t n)
89 {
90  const Float_t kFloatUndefined = -9999.;
91  Int_t j = 0;
92  char *clone;
93  for (Int_t i=0;i<n;i++) {
94  clone = (char*)list->UncheckedAt(i);
95  if (clone) memcpy(&fValue[j],clone + fOffset, 4*fLen);
96  else memcpy(&fValue[j],&kFloatUndefined, 4*fLen);
97  j += fLen;
98  }
99 }
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Prints leaf value.
103 
104 void TLeafF::PrintValue(Int_t l) const
105 {
106  Float_t *value = (Float_t *)GetValuePointer();
107  printf("%g",value[l]);
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Read leaf elements from Basket input buffer.
112 
113 void TLeafF::ReadBasket(TBuffer &b)
114 {
115  if (!fLeafCount && fNdata == 1) {
116  b.ReadFloat(fValue[0]);
117  }else {
118  if (fLeafCount) {
119  Long64_t entry = fBranch->GetReadEntry();
120  if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
121  fLeafCount->GetBranch()->GetEntry(entry);
122  }
123  Int_t len = Int_t(fLeafCount->GetValue());
124  if (len > fLeafCount->GetMaximum()) {
125  printf("ERROR leaf:%s, len=%d and max=%d\n",GetName(),len,fLeafCount->GetMaximum());
126  len = fLeafCount->GetMaximum();
127  }
128  fNdata = len*fLen;
129  b.ReadFastArray(fValue,len*fLen);
130  } else {
131  b.ReadFastArray(fValue,fLen);
132  }
133  }
134 }
135 
136 // Deserialize N events from an input buffer.
137 bool TLeafF::ReadBasketFast(TBuffer &input_buf, Long64_t N) {
138  if (R__unlikely(fLeafCount)) {return false;}
139  return input_buf.ByteSwapBuffer(fLen*N, kFloat_t);
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// Read leaf elements from Basket input buffer and export buffer to
144 /// TClonesArray objects.
145 
146 void TLeafF::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
147 {
148  if (n*fLen == 1) {
149  b >> fValue[0];
150  } else {
151  b.ReadFastArray(fValue,n*fLen);
152  }
153 
154  Float_t *value = fValue;
155  for (Int_t i=0;i<n;i++) {
156  char *first = (char*)list->UncheckedAt(i);
157  Float_t *ff = (Float_t*)&first[fOffset];
158  for (Int_t j=0;j<fLen;j++) {
159  ff[j] = value[j];
160  }
161  value += fLen;
162  }
163 }
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// Read a float from std::istream s and store it into the branch buffer.
167 
168 void TLeafF::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
169 {
170  Float_t *value = (Float_t*)GetValuePointer();
171  for (Int_t i=0;i<fLen;i++) s >> value[i];
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// Set leaf buffer data address.
176 
177 void TLeafF::SetAddress(void *add)
178 {
179  if (ResetAddress(add) && (add!= fValue)) {
180  delete [] fValue;
181  }
182 
183  if (add) {
184  if (TestBit(kIndirectAddress)) {
185  fPointer = (Float_t**) add;
186  Int_t ncountmax = fLen;
187  if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
188  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
189  ncountmax > fNdata || *fPointer == 0) {
190  if (*fPointer) delete [] *fPointer;
191  if (ncountmax > fNdata) fNdata = ncountmax;
192  *fPointer = new Float_t[fNdata];
193  }
194  fValue = *fPointer;
195  } else {
196  fValue = (Float_t*)add;
197  }
198  } else {
199  fValue = new Float_t[fNdata];
200  fValue[0] = 0;
201  }
202 }