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