Logo ROOT  
Reference Guide
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 "TStreamerElement.h"
23 #include <iostream>
24 
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Default constructor for LeafF16.
29 
31 {
32  fLenType = 4;
33  fMinimum = 0;
34  fMaximum = 0;
35  fValue = nullptr;
36  fPointer = nullptr;
37  fElement = nullptr;
38 }
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Create a LeafF16.
42 
43 TLeafF16::TLeafF16(TBranch *parent, const char *name, const char *type) : TLeaf(parent, name, type)
44 {
45  fLenType = 4;
46  fMinimum = 0;
47  fMaximum = 0;
48  fValue = nullptr;
49  fPointer = nullptr;
50  fElement = nullptr;
51  fTitle = type;
52 
53  if (strchr(type, '['))
54  fElement = new TStreamerElement(Form("%s_Element", name), type, 0, 0, "Float16_t");
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Default destructor for a LeafF16.
59 
61 {
62  if (ResetAddress(nullptr, kTRUE))
63  delete[] fValue;
64 
65  if (fElement)
66  delete fElement;
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Export element from local leaf buffer to ClonesArray.
71 
73 {
74  Float16_t *value = fValue;
75  for (Int_t i = 0; i < n; i++) {
76  auto first = (char *)list->UncheckedAt(i);
77  auto ff = (Float16_t *)&first[fOffset];
78  for (Int_t j = 0; j < fLen; j++) {
79  ff[j] = value[j];
80  }
81  value += fLen;
82  }
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Pack leaf elements in Basket output buffer.
87 
89 {
90  Int_t len = GetLen();
91  if (fPointer)
92  fValue = *fPointer;
93  b.WriteFastArrayFloat16(fValue, len, fElement);
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Import element from ClonesArray into local leaf buffer.
98 
100 {
101  const Float16_t kFloatUndefined = -9999.;
102  Int_t j = 0;
103  for (Int_t i = 0; i < n; i++) {
104  auto clone = (char *)list->UncheckedAt(i);
105  if (clone)
106  memcpy(&fValue[j], clone + fOffset, 4 * fLen);
107  else
108  memcpy(&fValue[j], &kFloatUndefined, 4 * fLen);
109  j += fLen;
110  }
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Prints leaf value.
115 
117 {
118  auto value = (Float16_t *)GetValuePointer();
119  printf("%g", value[l]);
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Read leaf elements from Basket input buffer.
124 
126 {
127  if (!fLeafCount && fNdata == 1) {
128  b.ReadFloat16(fValue, fElement);
129  } else {
130  if (fLeafCount) {
131  Long64_t entry = fBranch->GetReadEntry();
132  if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
133  fLeafCount->GetBranch()->GetEntry(entry);
134  }
135  auto len = Int_t(fLeafCount->GetValue());
136  if (len > fLeafCount->GetMaximum()) {
137  printf("ERROR leaf:%s, len=%d and max=%d\n", GetName(), len, fLeafCount->GetMaximum());
138  len = fLeafCount->GetMaximum();
139  }
140  fNdata = len * fLen;
141  b.ReadFastArrayFloat16(fValue, len * fLen, fElement);
142  } else {
143  b.ReadFastArrayFloat16(fValue, fLen, fElement);
144  }
145  }
146 }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Read leaf elements from Basket input buffer and export buffer to
150 /// TClonesArray objects.
151 
153 {
154  if (n * fLen == 1) {
155  b.ReadFloat16(fValue, fElement);
156  } else {
157  b.ReadFastArrayFloat16(fValue, n * fLen, fElement);
158  }
159 
160  Float16_t *value = fValue;
161  for (Int_t i = 0; i < n; i++) {
162  auto first = (char *)list->UncheckedAt(i);
163  auto ff = (Float16_t *)&first[fOffset];
164  for (Int_t j = 0; j < fLen; j++) {
165  ff[j] = value[j];
166  }
167  value += fLen;
168  }
169 }
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 /// Read a float from std::istream s and store it into the branch buffer.
173 
174 void TLeafF16::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
175 {
176  auto value = (Float16_t *)GetValuePointer();
177  for (Int_t i = 0; i < fLen; i++)
178  s >> value[i];
179 }
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 /// Set leaf buffer data address.
183 
184 void TLeafF16::SetAddress(void *add)
185 {
186  if (ResetAddress(add) && (add != fValue)) {
187  delete[] fValue;
188  }
189 
190  if (add) {
191  if (TestBit(kIndirectAddress)) {
192  fPointer = (Float16_t **)add;
193  Int_t ncountmax = fLen;
194  if (fLeafCount)
195  ncountmax = fLen * (fLeafCount->GetMaximum() + 1);
196  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) || ncountmax > fNdata || *fPointer == nullptr) {
197  if (*fPointer)
198  delete[] * fPointer;
199  if (ncountmax > fNdata)
200  fNdata = ncountmax;
201  *fPointer = new Float16_t[fNdata];
202  }
203  fValue = *fPointer;
204  } else {
205  fValue = (Float16_t *)add;
206  }
207  } else {
208  fValue = new Float16_t[fNdata];
209  fValue[0] = 0;
210  }
211 }
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 /// Stream an object of class TLeafF16.
215 
216 void TLeafF16::Streamer(TBuffer &R__b)
217 {
218  if (R__b.IsReading()) {
219  R__b.ReadClassBuffer(TLeafF16::Class(), this);
220 
221  if (fTitle.Contains("["))
222  fElement = new TStreamerElement(Form("%s_Element", fName.Data()), fTitle.Data(), 0, 0, "Float16_t");
223  } else {
224  R__b.WriteClassBuffer(TLeafF16::Class(), this);
225  }
226 }
TLeafF16::Export
virtual void Export(TClonesArray *list, Int_t n)
Export element from local leaf buffer to ClonesArray.
Definition: TLeafF16.cxx:72
l
auto * l
Definition: textangle.C:4
n
const Int_t n
Definition: legend1.C:16
first
Definition: first.py:1
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TObject::TestBit
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
TLeafF16::ReadBasket
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Definition: TLeafF16.cxx:125
TLeafF16::fPointer
Float16_t ** fPointer
! Address of pointer to data buffer!
Definition: TLeafF16.h:33
TString::Data
const char * Data() const
Definition: TString.h:369
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
Form
char * Form(const char *fmt,...)
TStreamerElement.h
TBuffer::ReadClassBuffer
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
TBranch.h
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TBranch::GetReadEntry
Long64_t GetReadEntry() const
Definition: TBranch.h:233
TLeafF16
Definition: TLeafF16.h:27
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
Int_t
int Int_t
Definition: RtypesCore.h:45
TNamed::fName
TString fName
Definition: TNamed.h:38
TString::Contains
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
TLeaf::GetValue
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:182
TBuffer
Definition: TBuffer.h:43
TObjArray::UncheckedAt
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:90
TLeafF16::fMinimum
Float16_t fMinimum
Minimum value if leaf range is specified.
Definition: TLeafF16.h:30
b
#define b(i)
Definition: RSha256.hxx:118
TClonesArray.h
TNamed::fTitle
TString fTitle
Definition: TNamed.h:39
TLeaf::fLenType
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:73
TBranch
Definition: TBranch.h:89
Float16_t
float Float16_t
Definition: RtypesCore.h:58
TLeafF16::Import
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition: TLeafF16.cxx:99
TLeafF16::FillBasket
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeafF16.cxx:88
TBuffer.h
TLeafF16::PrintValue
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition: TLeafF16.cxx:116
TLeaf
Definition: TLeaf.h:57
TLeaf::fLen
Int_t fLen
Number of fixed length elements in the leaf's data.
Definition: TLeaf.h:72
TBuffer::WriteClassBuffer
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
TBranch::GetEntry
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition: TBranch.cxx:1582
TLeaf::kIndirectAddress
@ kIndirectAddress
Data member is a pointer to an array of basic types.
Definition: TLeaf.h:95
TLeafF16::GetValuePointer
virtual void * GetValuePointer() const
Definition: TLeafF16.h:45
TLeafF16::fElement
TStreamerElement * fElement
! StreamerElement used for TBuffer read / write
Definition: TLeafF16.h:34
TLeaf::ResetAddress
Int_t ResetAddress(void *add, Bool_t destructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:406
TLeaf::fOffset
Int_t fOffset
Offset in ClonesArray object (if one)
Definition: TLeaf.h:74
TLeafF16::~TLeafF16
virtual ~TLeafF16()
Default destructor for a LeafF16.
Definition: TLeafF16.cxx:60
TLeaf::fNdata
Int_t fNdata
! Number of elements in fAddress data buffer.
Definition: TLeaf.h:71
TLeafF16::SetAddress
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
Definition: TLeafF16.cxx:184
TLeafF16::ReadBasketExport
virtual void ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
Read leaf elements from Basket input buffer and export buffer to TClonesArray objects.
Definition: TLeafF16.cxx:152
TLeaf::GetLen
virtual Int_t GetLen() const
Return the number of effective elements of this leaf, for the current entry.
Definition: TLeaf.cxx:381
TBuffer::IsReading
Bool_t IsReading() const
Definition: TBuffer.h:86
TLeaf::GetBranch
TBranch * GetBranch() const
Definition: TLeaf.h:115
TLeafF16::ReadValue
virtual void ReadValue(std::istream &s, Char_t delim=' ')
Read a float from std::istream s and store it into the branch buffer.
Definition: TLeafF16.cxx:174
TLeaf::GetMaximum
virtual Int_t GetMaximum() const
Definition: TLeaf.h:133
name
char name[80]
Definition: TGX11.cxx:110
TLeafF16::fMaximum
Float16_t fMaximum
Maximum value if leaf range is specified.
Definition: TLeafF16.h:31
TLeaf::fLeafCount
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:77
TLeafF16.h
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
type
int type
Definition: TGX11.cxx:121
Class
void Class()
Definition: Class.C:29
Char_t
char Char_t
Definition: RtypesCore.h:33
TClonesArray
Definition: TClonesArray.h:29
TLeafF16::TLeafF16
TLeafF16()
Default constructor for LeafF16.
Definition: TLeafF16.cxx:30
TStreamerElement
Definition: TStreamerElement.h:33
TLeaf::fBranch
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:78
int
TLeafF16::fValue
Float16_t * fValue
! Pointer to data buffer
Definition: TLeafF16.h:32