Logo ROOT  
Reference Guide
TLeafD32.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 TLeafD32
13 \ingroup tree
14 
15 A TLeaf for a 24 bit truncated floating point data type.
16 */
17 
18 #include "TLeafD32.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 LeafD32.
29 
31 {
32  fLenType = 8;
33  fMinimum = 0;
34  fMaximum = 0;
35  fValue = nullptr;
36  fPointer = nullptr;
37  fElement = nullptr;
38 }
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Create a LeafD32.
42 
43 TLeafD32::TLeafD32(TBranch *parent, const char *name, const char *type) : TLeaf(parent, name, type)
44 {
45  fLenType = 8;
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, "Double32_t");
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Default destructor for a LeafD32.
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  Int_t j = 0;
75  for (Int_t i = 0; i < n; i++) {
76  memcpy((char *)list->UncheckedAt(i) + fOffset, &fValue[j], 8 * fLen);
77  j += fLen;
78  }
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Pack leaf elements in Basket output buffer.
83 
85 {
86  Int_t len = GetLen();
87  if (fPointer)
88  fValue = *fPointer;
89  b.WriteFastArrayDouble32(fValue, len, fElement);
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Import element from ClonesArray into local leaf buffer.
94 
96 {
97  const Double32_t kDoubleUndefined = -9999.;
98  Int_t j = 0;
99  for (Int_t i = 0; i < n; i++) {
100  auto clone = (char *)list->UncheckedAt(i);
101  if (clone)
102  memcpy(&fValue[j], clone + fOffset, 8 * fLen);
103  else
104  memcpy(&fValue[j], &kDoubleUndefined, 8 * fLen);
105  j += fLen;
106  }
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Prints leaf value.
111 
113 {
114  auto value = (Double32_t *)GetValuePointer();
115  printf("%g", value[l]);
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Read leaf elements from Basket input buffer.
120 
122 {
123  if (!fLeafCount && fNdata == 1) {
124  b.ReadDouble32(fValue, fElement);
125  } else {
126  if (fLeafCount) {
127  Long64_t entry = fBranch->GetReadEntry();
128  if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
129  fLeafCount->GetBranch()->GetEntry(entry);
130  }
131  auto len = Int_t(fLeafCount->GetValue());
132  if (len > fLeafCount->GetMaximum()) {
133  printf("ERROR leaf:%s, len=%d and max=%d\n", GetName(), len, fLeafCount->GetMaximum());
134  len = fLeafCount->GetMaximum();
135  }
136  fNdata = len * fLen;
137  b.ReadFastArrayDouble32(fValue, len * fLen, fElement);
138  } else {
139  b.ReadFastArrayDouble32(fValue, fLen, fElement);
140  }
141  }
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// Read leaf elements from Basket input buffer and export buffer to
146 /// TClonesArray objects.
147 
149 {
150  b.ReadFastArrayDouble32(fValue, n * fLen, fElement);
151 
152  Int_t j = 0;
153  for (Int_t i = 0; i < n; i++) {
154  memcpy((char *)list->UncheckedAt(i) + fOffset, &fValue[j], 8 * fLen);
155  j += fLen;
156  }
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Read a double from std::istream s and store it into the branch buffer.
161 
162 void TLeafD32::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
163 {
164  auto value = (Double32_t *)GetValuePointer();
165  for (Int_t i = 0; i < fLen; i++)
166  s >> value[i];
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Set leaf buffer data address.
171 
172 void TLeafD32::SetAddress(void *add)
173 {
174  if (ResetAddress(add) && (add != fValue)) {
175  delete[] fValue;
176  }
177  if (add) {
178  if (TestBit(kIndirectAddress)) {
179  fPointer = (Double32_t **)add;
180  Int_t ncountmax = fLen;
181  if (fLeafCount)
182  ncountmax = fLen * (fLeafCount->GetMaximum() + 1);
183  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) || ncountmax > fNdata || *fPointer == nullptr) {
184  if (*fPointer)
185  delete[] * fPointer;
186  if (ncountmax > fNdata)
187  fNdata = ncountmax;
188  *fPointer = new Double32_t[fNdata];
189  }
190  fValue = *fPointer;
191  } else {
192  fValue = (Double32_t *)add;
193  }
194  } else {
195  fValue = new Double32_t[fNdata];
196  fValue[0] = 0;
197  }
198 }
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// Stream an object of class TLeafD32.
202 
203 void TLeafD32::Streamer(TBuffer &R__b)
204 {
205  if (R__b.IsReading()) {
206  R__b.ReadClassBuffer(TLeafD32::Class(), this);
207 
208  if (fTitle.Contains("["))
209  fElement = new TStreamerElement(Form("%s_Element", fName.Data()), fTitle.Data(), 0, 0, "Double32_t");
210  } else {
211  R__b.WriteClassBuffer(TLeafD32::Class(), this);
212  }
213 }
l
auto * l
Definition: textangle.C:4
n
const Int_t n
Definition: legend1.C:16
TLeafD32::Export
virtual void Export(TClonesArray *list, Int_t n)
Export element from local leaf buffer to ClonesArray.
Definition: TLeafD32.cxx:72
TLeafD32.h
TLeafD32::PrintValue
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition: TLeafD32.cxx:112
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
TLeafD32::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: TLeafD32.cxx:148
TLeafD32::Import
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition: TLeafD32.cxx:95
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
TLeafD32::fMaximum
Double32_t fMaximum
Maximum value if leaf range is specified.
Definition: TLeafD32.h:32
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TBranch::GetReadEntry
Long64_t GetReadEntry() const
Definition: TBranch.h:233
TLeafD32::ReadBasket
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Definition: TLeafD32.cxx:121
TLeafD32::TLeafD32
TLeafD32()
Default constructor for LeafD32.
Definition: TLeafD32.cxx:30
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
TLeafD32::fPointer
Double32_t ** fPointer
! Address of pointer to data buffer
Definition: TLeafD32.h:34
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
TLeafD32::fElement
TStreamerElement * fElement
! StreamerElement used for TBuffer read / write
Definition: TLeafD32.h:35
b
#define b(i)
Definition: RSha256.hxx:118
TClonesArray.h
TNamed::fTitle
TString fTitle
Definition: TNamed.h:39
TLeafD32::FillBasket
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeafD32.cxx:84
TLeaf::fLenType
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:73
TBranch
Definition: TBranch.h:89
TBuffer.h
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
Double32_t
double Double32_t
Definition: RtypesCore.h:60
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
TLeafD32::~TLeafD32
virtual ~TLeafD32()
Default destructor for a LeafD32.
Definition: TLeafD32.cxx:60
TLeafD32::ReadValue
virtual void ReadValue(std::istream &s, Char_t delim=' ')
Read a double from std::istream s and store it into the branch buffer.
Definition: TLeafD32.cxx:162
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
TLeaf::fNdata
Int_t fNdata
! Number of elements in fAddress data buffer.
Definition: TLeaf.h:71
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
TLeafD32
Definition: TLeafD32.h:28
TLeaf::GetMaximum
virtual Int_t GetMaximum() const
Definition: TLeaf.h:133
TLeafD32::GetValuePointer
virtual void * GetValuePointer() const
Definition: TLeafD32.h:46
TLeafD32::fMinimum
Double32_t fMinimum
Minimum value if leaf range is specified.
Definition: TLeafD32.h:31
name
char name[80]
Definition: TGX11.cxx:110
TLeaf::fLeafCount
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:77
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
TLeafD32::SetAddress
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
Definition: TLeafD32.cxx:172
Char_t
char Char_t
Definition: RtypesCore.h:33
TClonesArray
Definition: TClonesArray.h:29
TStreamerElement
Definition: TStreamerElement.h:33
TLeaf::fBranch
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:78
int
TLeafD32::fValue
Double32_t * fValue
! Pointer to data buffer
Definition: TLeafD32.h:33