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