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