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
15A 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
25
26////////////////////////////////////////////////////////////////////////////////
27/// Default constructor for LeafF16.
28
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
42TLeafF16::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
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
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
88{
89 Int_t len = GetLen();
90 if (fPointer)
92 b.WriteFastArrayFloat16(fValue, len, fElement);
93}
94
95////////////////////////////////////////////////////////////////////////////////
96/// Import element from ClonesArray into local leaf buffer.
97
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
116{
117 auto value = (Float16_t *)GetValuePointer();
118 printf("%g", value[l]);
119}
120
121////////////////////////////////////////////////////////////////////////////////
122/// Read leaf elements from Basket input buffer.
123
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
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
173void 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
183void TLeafF16::SetAddress(void *add)
184{
185 if (ResetAddress(add) && (add != fValue)) {
186 delete[] fValue;
187 }
188
189 if (add) {
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
215void 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}
void Class()
Definition: Class.C:29
#define b(i)
Definition: RSha256.hxx:100
float Float16_t
Definition: RtypesCore.h:56
int Int_t
Definition: RtypesCore.h:43
char Char_t
Definition: RtypesCore.h:31
long long Long64_t
Definition: RtypesCore.h:71
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
char * Form(const char *fmt,...)
A TTree is a list of TBranches.
Definition: TBranch.h:91
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:1591
Long64_t GetReadEntry() const
Definition: TBranch.h:235
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
Bool_t IsReading() const
Definition: TBuffer.h:85
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
An array of clone (identical) objects.
Definition: TClonesArray.h:32
A TLeaf for a 24 bit truncated floating point data type.
Definition: TLeafF16.h:26
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:151
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeafF16.cxx:87
virtual void Export(TClonesArray *list, Int_t n)
Export element from local leaf buffer to ClonesArray.
Definition: TLeafF16.cxx:71
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Definition: TLeafF16.cxx:124
virtual ~TLeafF16()
Default destructor for a LeafF16.
Definition: TLeafF16.cxx:59
Float16_t ** fPointer
! Address of pointer to data buffer!
Definition: TLeafF16.h:32
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:173
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition: TLeafF16.cxx:115
TStreamerElement * fElement
! StreamerElement used for TBuffer read / write
Definition: TLeafF16.h:33
Float16_t fMaximum
Maximum value if leaf range is specified.
Definition: TLeafF16.h:30
Float16_t * fValue
! Pointer to data buffer
Definition: TLeafF16.h:31
TLeafF16()
Default constructor for LeafF16.
Definition: TLeafF16.cxx:29
virtual void * GetValuePointer() const
Definition: TLeafF16.h:44
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition: TLeafF16.cxx:98
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
Definition: TLeafF16.cxx:183
Float16_t fMinimum
Minimum value if leaf range is specified.
Definition: TLeafF16.h:29
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:49
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:174
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:65
virtual Int_t GetMaximum() const
Definition: TLeaf.h:125
Int_t fLen
Number of fixed length elements in the leaf's data.
Definition: TLeaf.h:64
Int_t fNdata
! Number of elements in fAddress data buffer.
Definition: TLeaf.h:63
virtual Int_t GetLen() const
Return the number of effective elements of this leaf, for the current entry.
Definition: TLeaf.cxx:382
TBranch * GetBranch() const
Definition: TLeaf.h:107
Int_t fOffset
Offset in ClonesArray object (if one)
Definition: TLeaf.h:66
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:70
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:69
@ kIndirectAddress
Data member is a pointer to an array of basic types.
Definition: TLeaf.h:87
Int_t ResetAddress(void *add, Bool_t destructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:407
TString fTitle
Definition: TNamed.h:33
TString fName
Definition: TNamed.h:32
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:90
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:187
const char * Data() const
Definition: TString.h:364
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
const Int_t n
Definition: legend1.C:16
static constexpr double s
Definition: first.py:1
auto * l
Definition: textangle.C:4