Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TLeafF16.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Simon Spies 23/02/19
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2019, 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 TLeafF16
13 \ingroup tree
14 
15 A TLeaf for a 24 bit truncated floating point data type.
16 */
17 
18 #include "TLeafF16.h"
19 #include "TBranch.h"
20 #include "TBuffer.h"
21 #include "TClonesArray.h"
22 #include "Riostream.h"
23 
24 ClassImp(TLeafF16);
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Default constructor for LeafF16.
28 
29 TLeafF16::TLeafF16() : TLeaf()
30 {
31  fLenType = 4;
32  fMinimum = 0;
33  fMaximum = 0;
34  fValue = nullptr;
35  fPointer = nullptr;
36  fElement = nullptr;
37 }
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Create a LeafF16.
41 
42 TLeafF16::TLeafF16(TBranch *parent, const char *name, const char *type) : TLeaf(parent, name, type)
43 {
44  fLenType = 4;
45  fMinimum = 0;
46  fMaximum = 0;
47  fValue = nullptr;
48  fPointer = nullptr;
49  fElement = nullptr;
50  fTitle = type;
51 
52  if (strchr(type, '['))
53  fElement = new TStreamerElement(Form("%s_Element", name), type, 0, 0, "Float16_t");
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Default destructor for a LeafF16.
58 
59 TLeafF16::~TLeafF16()
60 {
61  if (ResetAddress(nullptr, kTRUE))
62  delete[] fValue;
63 
64  if (fElement)
65  delete fElement;
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// Export element from local leaf buffer to ClonesArray.
70 
71 void TLeafF16::Export(TClonesArray *list, Int_t n)
72 {
73  Float16_t *value = fValue;
74  for (Int_t i = 0; i < n; i++) {
75  auto first = (char *)list->UncheckedAt(i);
76  auto ff = (Float16_t *)&first[fOffset];
77  for (Int_t j = 0; j < fLen; j++) {
78  ff[j] = value[j];
79  }
80  value += fLen;
81  }
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Pack leaf elements in Basket output buffer.
86 
87 void TLeafF16::FillBasket(TBuffer &b)
88 {
89  Int_t len = GetLen();
90  if (fPointer)
91  fValue = *fPointer;
92  b.WriteFastArrayFloat16(fValue, len, fElement);
93 }
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Import element from ClonesArray into local leaf buffer.
97 
98 void TLeafF16::Import(TClonesArray *list, Int_t n)
99 {
100  const Float16_t kFloatUndefined = -9999.;
101  Int_t j = 0;
102  for (Int_t i = 0; i < n; i++) {
103  auto clone = (char *)list->UncheckedAt(i);
104  if (clone)
105  memcpy(&fValue[j], clone + fOffset, 4 * fLen);
106  else
107  memcpy(&fValue[j], &kFloatUndefined, 4 * fLen);
108  j += fLen;
109  }
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Prints leaf value.
114 
115 void TLeafF16::PrintValue(Int_t l) const
116 {
117  auto value = (Float16_t *)GetValuePointer();
118  printf("%g", value[l]);
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Read leaf elements from Basket input buffer.
123 
124 void TLeafF16::ReadBasket(TBuffer &b)
125 {
126  if (!fLeafCount && fNdata == 1) {
127  b.ReadFloat16(fValue, fElement);
128  } else {
129  if (fLeafCount) {
130  Long64_t entry = fBranch->GetReadEntry();
131  if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
132  fLeafCount->GetBranch()->GetEntry(entry);
133  }
134  auto len = Int_t(fLeafCount->GetValue());
135  if (len > fLeafCount->GetMaximum()) {
136  printf("ERROR leaf:%s, len=%d and max=%d\n", GetName(), len, fLeafCount->GetMaximum());
137  len = fLeafCount->GetMaximum();
138  }
139  fNdata = len * fLen;
140  b.ReadFastArrayFloat16(fValue, len * fLen, fElement);
141  } else {
142  b.ReadFastArrayFloat16(fValue, fLen, fElement);
143  }
144  }
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// Read leaf elements from Basket input buffer and export buffer to
149 /// TClonesArray objects.
150 
151 void TLeafF16::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
152 {
153  if (n * fLen == 1) {
154  b.ReadFloat16(fValue, fElement);
155  } else {
156  b.ReadFastArrayFloat16(fValue, n * fLen, fElement);
157  }
158 
159  Float16_t *value = fValue;
160  for (Int_t i = 0; i < n; i++) {
161  auto first = (char *)list->UncheckedAt(i);
162  auto ff = (Float16_t *)&first[fOffset];
163  for (Int_t j = 0; j < fLen; j++) {
164  ff[j] = value[j];
165  }
166  value += fLen;
167  }
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// Read a float from std::istream s and store it into the branch buffer.
172 
173 void TLeafF16::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
174 {
175  auto value = (Float16_t *)GetValuePointer();
176  for (Int_t i = 0; i < fLen; i++)
177  s >> value[i];
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// Set leaf buffer data address.
182 
183 void TLeafF16::SetAddress(void *add)
184 {
185  if (ResetAddress(add) && (add != fValue)) {
186  delete[] fValue;
187  }
188 
189  if (add) {
190  if (TestBit(kIndirectAddress)) {
191  fPointer = (Float16_t **)add;
192  Int_t ncountmax = fLen;
193  if (fLeafCount)
194  ncountmax = fLen * (fLeafCount->GetMaximum() + 1);
195  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) || ncountmax > fNdata || *fPointer == nullptr) {
196  if (*fPointer)
197  delete[] * fPointer;
198  if (ncountmax > fNdata)
199  fNdata = ncountmax;
200  *fPointer = new Float16_t[fNdata];
201  }
202  fValue = *fPointer;
203  } else {
204  fValue = (Float16_t *)add;
205  }
206  } else {
207  fValue = new Float16_t[fNdata];
208  fValue[0] = 0;
209  }
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// Stream an object of class TLeafF16.
214 
215 void TLeafF16::Streamer(TBuffer &R__b)
216 {
217  if (R__b.IsReading()) {
218  R__b.ReadClassBuffer(TLeafF16::Class(), this);
219 
220  if (fTitle.Contains("["))
221  fElement = new TStreamerElement(Form("%s_Element", fName.Data()), fTitle.Data(), 0, 0, "Float16_t");
222  } else {
223  R__b.WriteClassBuffer(TLeafF16::Class(), this);
224  }
225 }