Logo ROOT  
Reference Guide
TLeafS.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 TLeafS
13 \ingroup tree
14 
15 A TLeaf for a 16 bit Integer data type.
16 */
17 
18 #include "TLeafS.h"
19 #include "TBranch.h"
20 #include "TBuffer.h"
21 #include "TClonesArray.h"
22 #include <iostream>
23 
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Default constructor for LeafS.
28 
30 {
31  fValue = 0;
32  fPointer = 0;
33  fMinimum = 0;
34  fMaximum = 0;
35  fLenType = 2;
36 }
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Create a LeafS.
40 
41 TLeafS::TLeafS(TBranch *parent, const char *name, const char *type)
42  :TLeaf(parent,name,type)
43 {
44  fLenType = 2;
45  fMinimum = 0;
46  fMaximum = 0;
47  fValue = 0;
48  fPointer = 0;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Default destructor for a LeafS.
53 
55 {
56  if (ResetAddress(0,kTRUE)) delete [] fValue;
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Export element from local leaf buffer to ClonesArray.
61 
63 {
64  Int_t j = 0;
65  for (Int_t i=0;i<n;i++) {
66  memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 2*fLen);
67  j += fLen;
68  }
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Pack leaf elements in Basket output buffer.
73 
75 {
76  Int_t i;
77  Int_t len = GetLen();
78  if (fPointer) fValue = *fPointer;
79  if (IsRange()) {
80  if (fValue[0] > fMaximum) fMaximum = fValue[0];
81  }
82  if (IsUnsigned()) {
83  for (i=0;i<len;i++) b << (UShort_t)fValue[i];
84  } else {
85  b.WriteFastArray(fValue,len);
86  }
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Returns name of leaf type.
91 
92 const char *TLeafS::GetTypeName() const
93 {
94  if (fIsUnsigned) return "UShort_t";
95  return "Short_t";
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Returns current value of leaf.
100 /// - if leaf is a simple type, i must be set to 0
101 /// - if leaf is an array, i is the array element number to be returned
102 
104 {
105  if (fIsUnsigned) return (UShort_t)fValue[i];
106  return fValue[i];
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Copy/set fMinimum and fMaximum to include/be wide than those of the parameter
111 
113 {
114  if (input) {
115  if (input->GetMaximum() > this->GetMaximum())
116  this->SetMaximum( input->GetMaximum() );
117  if (input->GetMinimum() < this->GetMinimum())
118  this->SetMinimum( input->GetMinimum() );
119  return kTRUE;
120  } else {
121  return kFALSE;
122  }
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Import element from ClonesArray into local leaf buffer.
127 
129 {
130  const Short_t kShortUndefined = -9999;
131  Int_t j = 0;
132  char *clone;
133  for (Int_t i=0;i<n;i++) {
134  clone = (char*)list->UncheckedAt(i);
135  if (clone) memcpy(&fValue[j],clone + fOffset, 2*fLen);
136  else memcpy(&fValue[j],&kShortUndefined, 2*fLen);
137  j += fLen;
138  }
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Prints leaf value.
143 
145 {
146  if (fIsUnsigned) {
147  UShort_t *uvalue = (UShort_t*)GetValuePointer();
148  printf("%u",uvalue[l]);
149  } else {
150  Short_t *value = (Short_t*)GetValuePointer();
151  printf("%d",value[l]);
152  }
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Read leaf elements from Basket input buffer.
157 
159 {
160  if (!fLeafCount && fNdata == 1) {
161  b.ReadShort(fValue[0]);
162  }else {
163  if (fLeafCount) {
164  Long64_t entry = fBranch->GetReadEntry();
165  if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
166  fLeafCount->GetBranch()->GetEntry(entry);
167  }
168  Int_t len = Int_t(fLeafCount->GetValue());
169  if (len > fLeafCount->GetMaximum()) {
170  printf("ERROR leaf:%s, len=%d and max=%d\n",GetName(),len,fLeafCount->GetMaximum());
171  len = fLeafCount->GetMaximum();
172  }
173  fNdata = len*fLen;
174  b.ReadFastArray(fValue,len*fLen);
175  } else {
176  b.ReadFastArray(fValue,fLen);
177  }
178  }
179 }
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 /// Deserialize input by performing byteswap as needed.
184 {
185  if (R__unlikely(fLeafCount)) {return false;}
186  return input_buf.ByteSwapBuffer(fLen*N, kShort_t);
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Read leaf elements from Basket input buffer and export buffer to
191 /// TClonesArray objects.
192 
194 {
195  if (n*fLen == 1) {
196  b >> fValue[0];
197  } else {
198  b.ReadFastArray(fValue,n*fLen);
199  }
200 
201  Int_t j = 0;
202  for (Int_t i=0;i<n;i++) {
203  memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 2*fLen);
204  j += fLen;
205  }
206 }
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Read a integer integer from std::istream s and store it into the branch buffer.
210 
211 void TLeafS::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
212 {
213  if (fIsUnsigned) {
214  UShort_t *uvalue = (UShort_t*)GetValuePointer();
215  for (Int_t i=0;i<fLen;i++) s >> uvalue[i];
216  } else {
217  Short_t *value = (Short_t*)GetValuePointer();
218  for (Int_t i=0;i<fLen;i++) s >> value[i];
219  }
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// Set leaf buffer data address.
224 
225 void TLeafS::SetAddress(void *add)
226 {
227  if (ResetAddress(add) && (add!=fValue)) {
228  delete [] fValue;
229  }
230  if (add) {
231  if (TestBit(kIndirectAddress)) {
232  fPointer = (Short_t**) add;
233  Int_t ncountmax = fLen;
234  if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
235  if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
236  ncountmax > fNdata || *fPointer == 0) {
237  if (*fPointer) delete [] *fPointer;
238  if (ncountmax > fNdata) fNdata = ncountmax;
239  *fPointer = new Short_t[fNdata];
240  }
241  fValue = *fPointer;
242  } else {
243  fValue = (Short_t*)add;
244  }
245  } else {
246  fValue = new Short_t[fNdata];
247  fValue[0] = 0;
248  }
249 }
TLeaf::IsUnsigned
virtual Bool_t IsUnsigned() const
Definition: TLeaf.h:149
l
auto * l
Definition: textangle.C:4
n
const Int_t n
Definition: legend1.C:16
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
TLeafS::fMaximum
Short_t fMaximum
Maximum value if leaf range is specified.
Definition: TLeafS.h:36
UShort_t
unsigned short UShort_t
Definition: RtypesCore.h:40
TLeafS.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TLeafS::SetAddress
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
Definition: TLeafS.cxx:225
TBranch.h
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TBranch::GetReadEntry
Long64_t GetReadEntry() const
Definition: TBranch.h:233
TLeafS::FillBasket
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeafS.cxx:74
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
Int_t
int Int_t
Definition: RtypesCore.h:45
TLeafS::ReadBasket
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Definition: TLeafS.cxx:158
N
#define N
TLeaf::GetValue
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:182
TLeafS::fMinimum
Short_t fMinimum
Minimum value if leaf range is specified.
Definition: TLeafS.h:35
TBuffer
Definition: TBuffer.h:43
TObjArray::UncheckedAt
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:90
b
#define b(i)
Definition: RSha256.hxx:118
bool
TClonesArray.h
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
TLeafS::SetMaximum
virtual void SetMaximum(Short_t max)
Definition: TLeafS.h:61
TLeaf::fLen
Int_t fLen
Number of fixed length elements in the leaf's data.
Definition: TLeaf.h:72
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TLeafS
Definition: TLeafS.h:26
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
R__unlikely
#define R__unlikely(expr)
Definition: RConfig.hxx:604
TLeaf::kIndirectAddress
@ kIndirectAddress
Data member is a pointer to an array of basic types.
Definition: TLeaf.h:95
TLeafS::GetValuePointer
virtual void * GetValuePointer() const
Definition: TLeafS.h:51
TLeafS::PrintValue
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition: TLeafS.cxx:144
TLeaf::IsRange
virtual Bool_t IsRange() const
Definition: TLeaf.h:148
TBuffer::ByteSwapBuffer
Bool_t ByteSwapBuffer(Long64_t n, EDataType type)
Byte-swap N primitive-elements in the buffer.
Definition: TBuffer.cxx:392
Short_t
short Short_t
Definition: RtypesCore.h:39
TLeaf::fIsUnsigned
Bool_t fIsUnsigned
(=kTRUE if unsigned, kFALSE otherwise)
Definition: TLeaf.h:76
TLeafS::fPointer
Short_t ** fPointer
! Address of pointer to data buffer
Definition: TLeafS.h:38
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
TLeafS::TLeafS
TLeafS()
Default constructor for LeafS.
Definition: TLeafS.cxx:29
TLeafS::ReadBasketFast
virtual bool ReadBasketFast(TBuffer &, Long64_t)
Deserialize input by performing byteswap as needed.
Definition: TLeafS.cxx:183
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
TLeaf::GetBranch
TBranch * GetBranch() const
Definition: TLeaf.h:115
TLeaf::GetMaximum
virtual Int_t GetMaximum() const
Definition: TLeaf.h:133
Double_t
double Double_t
Definition: RtypesCore.h:59
TLeaf::GetMinimum
virtual Int_t GetMinimum() const
Definition: TLeaf.h:134
TLeafS::fValue
Short_t * fValue
! Pointer to data buffer
Definition: TLeafS.h:37
name
char name[80]
Definition: TGX11.cxx:110
TLeafS::SetMinimum
virtual void SetMinimum(Short_t min)
Definition: TLeafS.h:62
TLeafS::GetValue
Double_t GetValue(Int_t i=0) const
Returns current value of leaf.
Definition: TLeafS.cxx:103
TLeaf::fLeafCount
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:77
TLeafS::Export
virtual void Export(TClonesArray *list, Int_t n)
Export element from local leaf buffer to ClonesArray.
Definition: TLeafS.cxx:62
TLeafS::Import
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition: TLeafS.cxx:128
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
type
int type
Definition: TGX11.cxx:121
TLeafS::ReadValue
virtual void ReadValue(std::istream &s, Char_t delim=' ')
Read a integer integer from std::istream s and store it into the branch buffer.
Definition: TLeafS.cxx:211
TLeafS::~TLeafS
virtual ~TLeafS()
Default destructor for a LeafS.
Definition: TLeafS.cxx:54
kShort_t
@ kShort_t
Definition: TDataType.h:35
Char_t
char Char_t
Definition: RtypesCore.h:33
TClonesArray
Definition: TClonesArray.h:29
TLeaf::fBranch
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:78
TLeafS::IncludeRange
virtual Bool_t IncludeRange(TLeaf *)
Copy/set fMinimum and fMaximum to include/be wide than those of the parameter.
Definition: TLeafS.cxx:112
int
TLeafS::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: TLeafS.cxx:193
TLeafS::GetTypeName
const char * GetTypeName() const
Returns name of leaf type.
Definition: TLeafS.cxx:92