Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "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
43TLeafF16::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)
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
174void 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
184void TLeafF16::SetAddress(void *add)
185{
186 if (ResetAddress(add) && (add != fValue)) {
187 delete[] fValue;
188 }
189
190 if (add) {
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
216void 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}
#define b(i)
Definition RSha256.hxx:100
float Float16_t
Definition RtypesCore.h:58
int Int_t
Definition RtypesCore.h:45
char Char_t
Definition RtypesCore.h:37
long long Long64_t
Definition RtypesCore.h:73
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
int type
Definition TGX11.cxx:121
char * Form(const char *fmt,...)
A TTree is a list of TBranches.
Definition TBranch.h:89
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:1652
Long64_t GetReadEntry() const
Definition TBranch.h:233
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
An array of clone (identical) objects.
A TLeaf for a 24 bit truncated floating point data type.
Definition TLeafF16.h:27
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
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition TLeafF16.cxx:88
virtual void Export(TClonesArray *list, Int_t n)
Export element from local leaf buffer to ClonesArray.
Definition TLeafF16.cxx:72
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Definition TLeafF16.cxx:125
virtual ~TLeafF16()
Default destructor for a LeafF16.
Definition TLeafF16.cxx:60
Float16_t ** fPointer
! Address of pointer to data buffer!
Definition TLeafF16.h:33
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
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition TLeafF16.cxx:116
TStreamerElement * fElement
! StreamerElement used for TBuffer read / write
Definition TLeafF16.h:34
Float16_t fMaximum
Maximum value if leaf range is specified.
Definition TLeafF16.h:31
Float16_t * fValue
! Pointer to data buffer
Definition TLeafF16.h:32
TLeafF16()
Default constructor for LeafF16.
Definition TLeafF16.cxx:30
virtual void * GetValuePointer() const
Definition TLeafF16.h:46
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition TLeafF16.cxx:99
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
Definition TLeafF16.cxx:184
Float16_t fMinimum
Minimum value if leaf range is specified.
Definition TLeafF16.h:30
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
virtual Double_t GetValue(Int_t i=0) const
Definition TLeaf.h:183
Int_t fLenType
Number of bytes for this data type.
Definition TLeaf.h:73
virtual Int_t GetMaximum() const
Definition TLeaf.h:134
Int_t fLen
Number of fixed length elements in the leaf's data.
Definition TLeaf.h:72
Int_t fNdata
! Number of elements in fAddress data buffer.
Definition TLeaf.h:71
virtual Int_t GetLen() const
Return the number of effective elements of this leaf, for the current entry.
Definition TLeaf.cxx:404
TBranch * GetBranch() const
Definition TLeaf.h:116
Int_t fOffset
Offset in ClonesArray object (if one)
Definition TLeaf.h:74
Int_t ResetAddress(void *add, Bool_t calledFromDestructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition TLeaf.cxx:429
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition TLeaf.h:78
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition TLeaf.h:77
@ kIndirectAddress
Data member is a pointer to an array of basic types.
Definition TLeaf.h:95
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:369
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
const Int_t n
Definition legend1.C:16
Definition first.py:1
auto * l
Definition textangle.C:4