Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TLeafD.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 12/01/96
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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 TLeafD
13 \ingroup tree
14 
15 A TLeaf for a 64 bit floating point data type.
16 */
17 
18 #include "TLeafD.h"
19 #include "TBranch.h"
20 #include "TBuffer.h"
21 #include "TClonesArray.h"
22 #include "Riostream.h"
23 
24 ClassImp(TLeafD);
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Default constructor for LeafD.
28 
29 TLeafD::TLeafD(): TLeaf()
30 {
31  fLenType = 8;
32  fMinimum = 0;
33  fMaximum = 0;
34  fValue = 0;
35  fPointer = 0;
36 }
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Create a LeafD.
40 
41 TLeafD::TLeafD(TBranch *parent, const char *name, const char *type)
42  :TLeaf(parent, name,type)
43 {
44  fLenType = 8;
45  fMinimum = 0;
46  fMaximum = 0;
47  fValue = 0;
48  fPointer = 0;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Default destructor for a LeafD.
53 
54 TLeafD::~TLeafD()
55 {
56  if (ResetAddress(0,kTRUE)) delete [] fValue;
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Export element from local leaf buffer to ClonesArray.
61 
62 void TLeafD::Export(TClonesArray *list, Int_t n)
63 {
64  Int_t j = 0;
65  for (Int_t i=0;i<n;i++) {
66  memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 8*fLen);
67  j += fLen;
68  }
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Pack leaf elements in Basket output buffer.
73 
74 void TLeafD::FillBasket(TBuffer &b)
75 {
76  Int_t len = GetLen();
77  if (fPointer) fValue = *fPointer;
78  b.WriteFastArray(fValue,len);
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Import element from ClonesArray into local leaf buffer.
83 
84 void TLeafD::Import(TClonesArray *list, Int_t n)
85 {
86  const Double_t kDoubleUndefined = -9999.;
87  Int_t j = 0;
88  char *clone;
89  for (Int_t i=0;i<n;i++) {
90  clone = (char*)list->UncheckedAt(i);
91  if (clone) memcpy(&fValue[j],clone + fOffset, 8*fLen);
92  else memcpy(&fValue[j],&kDoubleUndefined, 8*fLen);
93  j += fLen;
94  }
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Prints leaf value.
99 
100 void TLeafD::PrintValue(Int_t l) const
101 {
102  Double_t *value = (Double_t *)GetValuePointer();
103  printf("%g",value[l]);
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Read leaf elements from Basket input buffer.
108 
109 void TLeafD::ReadBasket(TBuffer &b)
110 {
111  if (!fLeafCount && fNdata == 1) {
112  b.ReadDouble(fValue[0]);
113  }else {
114  if (fLeafCount) {
115  Long64_t entry = fBranch->GetReadEntry();
116  if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
117  fLeafCount->GetBranch()->GetEntry(entry);
118  }
119  Int_t len = Int_t(fLeafCount->GetValue());
120  if (len > fLeafCount->GetMaximum()) {
121  printf("ERROR leaf:%s, len=%d and max=%d\n",GetName(),len,fLeafCount->GetMaximum());
122  len = fLeafCount->GetMaximum();
123  }
124  fNdata = len*fLen;
125  b.ReadFastArray(fValue,len*fLen);
126  } else {
127  b.ReadFastArray(fValue,fLen);
128  }
129  }
130 }
131 
132 // Deserialize N events from an input buffer.
133 bool TLeafD::ReadBasketFast(TBuffer &input_buf, Long64_t N) {
134  if (R__unlikely(fLeafCount)) { return false; }
135  return input_buf.ByteSwapBuffer(fLen*N, kDouble_t);
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Read leaf elements from Basket input buffer and export buffer to
140 /// TClonesArray objects.
141 
142 void TLeafD::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
143 {
144  b.ReadFastArray(fValue,n*fLen);
145 
146  Int_t j = 0;
147  for (Int_t i=0;i<n;i++) {
148  memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 8*fLen);
149  j += fLen;
150  }
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Read a double from std::istream s and store it into the branch buffer.
155 
156 void TLeafD::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
157 {
158  Double_t *value = (Double_t*)GetValuePointer();
159  for (Int_t i=0;i<fLen;i++) s >> value[i];
160 }
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// Set leaf buffer data address.
164 
165 void TLeafD::SetAddress(void *add)
166 {
167  if (ResetAddress(add) && (add!= fValue)) {
168  delete [] fValue;
169  }
170  if (add) {
171  if (TestBit(kIndirectAddress)) {
172  fPointer = (Double_t**) add;
173  Int_t ncountmax = fLen;
174  if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
175  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
176  ncountmax > fNdata || *fPointer == 0) {
177  if (*fPointer) delete [] *fPointer;
178  if (ncountmax > fNdata) fNdata = ncountmax;
179  *fPointer = new Double_t[fNdata];
180  }
181  fValue = *fPointer;
182  } else {
183  fValue = (Double_t*)add;
184  }
185  } else {
186  fValue = new Double_t[fNdata];
187  fValue[0] = 0;
188  }
189 }