Logo ROOT  
Reference Guide
TTreeRow.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Fons Rademakers 30/11/99
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 TTreeRow
13 \ingroup tree
14 
15 Class defining interface to a row of a TTree query result.
16 Objects of this class are created by TTreeResult methods.
17 
18 Related classes are TTreeResult.
19 */
20 
21 #include "TTreeRow.h"
22 #include "TBuffer.h"
23 
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Single row of a query result.
28 
30 {
31  fColumnCount = 0;
32  fFields = 0;
33  fOriginal = 0;
34  fRow = 0;
35 
36 }
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Single row of a query result.
40 
42 {
43  fColumnCount = nfields;
44  fFields = 0;
45  fOriginal = 0;
46  fRow = 0;
47 
48 }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Single row of a query result.
52 
53 TTreeRow::TTreeRow(Int_t nfields, const Int_t *fields, const char *row)
54 {
55  fColumnCount = nfields;
56  fFields = 0;
57  fOriginal = 0;
58  fRow = 0;
59  SetRow(fields,row);
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// This is a shallow copy of a real row, i.e. it only contains
64 /// a pointer to the original.
65 
67 {
68  fFields = 0;
69  fOriginal = 0;
70  fColumnCount = 0;
71  fRow = 0;
72 
73  if (!original) {
74  Error("TTreeRow", "original may not be 0");
75  return;
76  }
77  if (original->IsA() != TTreeRow::Class()) {
78  Error("TTreeRow", "original must be a TTreeRow");
79  return;
80  }
81 
82  fOriginal = (TTreeRow*) original;
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Destroy row object.
88 
90 {
91  if (fFields)
92  Close();
93 }
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Close row.
97 
99 {
100  if (fRow) delete [] fRow;
101  if (fFields) delete [] fFields;
102  fColumnCount = 0;
103  fOriginal = 0;
104  fRow = nullptr;
105  fFields = nullptr;
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Check if row is open and field index within range.
110 
112 {
113  if (!fFields && !fOriginal) {
114  Error("IsValid", "row closed");
115  return kFALSE;
116  }
117  if (field < 0 || field >= fColumnCount) {
118  Error("IsValid", "field index out of bounds");
119  return kFALSE;
120  }
121  return kTRUE;
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// Get length in bytes of specified field.
126 
128 {
129  if (!IsValid(field))
130  return 0;
131 
132  if (fOriginal)
133  return fOriginal->GetFieldLength(field);
134 
135  if (field > 0) return fFields[field] - fFields[field-1] -1;
136  else return fFields[0] -1;
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Get specified field from row (0 <= field < GetFieldCount()).
141 
142 const char *TTreeRow::GetField(Int_t field)
143 {
144  if (!IsValid(field))
145  return 0;
146 
147  if (fOriginal)
148  return fOriginal->GetField(field);
149 
150  if (field > 0) return fRow +fFields[field-1];
151  else return fRow;
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// The field and row information.
156 
157 void TTreeRow::SetRow(const Int_t *fields, const char *row)
158 {
159  if (!fColumnCount) return;
160  if (fFields) delete [] fFields;
161  Int_t nch = fields[fColumnCount-1];
162  fFields = new Int_t[fColumnCount];
163  fOriginal = 0;
164  if (fRow) delete [] fRow;
165  fRow = new char[nch];
166  for (Int_t i=0;i<fColumnCount;i++) fFields[i] = fields[i];
167  memcpy(fRow,row,nch);
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// Stream an object of class TTreeRow.
172 
173 void TTreeRow::Streamer(TBuffer &R__b)
174 {
175  UInt_t R__s, R__c;
176  if (R__b.IsReading()) {
177  R__b.ReadVersion(&R__s, &R__c);
178  TSQLRow::Streamer(R__b);
179  R__b >> fColumnCount;
180  fFields = new Int_t[fColumnCount];
182  Int_t nch;
183  R__b >> nch;
184  fRow = new char[nch];
185  R__b.ReadFastArray(fRow,nch);
186  R__b.CheckByteCount(R__s, R__c, TTreeRow::IsA());
187  } else {
188  R__c = R__b.WriteVersion(TTreeRow::Class(),kTRUE);
189  TSQLRow::Streamer(R__b);
190  R__b << fColumnCount;
192  Int_t nch = fFields ? fFields[fColumnCount-1] : 0;
193  R__b << nch;
194  R__b.WriteFastArray(fRow,nch);
195  R__b.SetByteCount(R__c,kTRUE);
196  }
197 }
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TBuffer::WriteVersion
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
TBuffer::SetByteCount
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
TBuffer::ReadFastArray
virtual void ReadFastArray(Bool_t *b, Int_t n)=0
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
Int_t
int Int_t
Definition: RtypesCore.h:45
TBuffer
Definition: TBuffer.h:43
TBuffer::CheckByteCount
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
bool
TTreeRow::SetRow
void SetRow(const Int_t *fields, const char *row)
The field and row information.
Definition: TTreeRow.cxx:157
TTreeRow::GetFieldLength
ULong_t GetFieldLength(Int_t field)
Get length in bytes of specified field.
Definition: TTreeRow.cxx:127
TTreeRow::GetField
const char * GetField(Int_t field)
Get specified field from row (0 <= field < GetFieldCount()).
Definition: TTreeRow.cxx:142
TTreeRow::~TTreeRow
virtual ~TTreeRow()
Destroy row object.
Definition: TTreeRow.cxx:89
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TBuffer.h
TTreeRow::TTreeRow
TTreeRow()
Single row of a query result.
Definition: TTreeRow.cxx:29
TBuffer::WriteFastArray
virtual void WriteFastArray(const Bool_t *b, Int_t n)=0
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TSQLRow
Definition: TSQLRow.h:30
TTreeRow::fFields
Int_t * fFields
[fColumnCount] index in fRow of the end of each field
Definition: TTreeRow.h:42
TTreeRow::IsValid
Bool_t IsValid(Int_t field)
Check if row is open and field index within range.
Definition: TTreeRow.cxx:111
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
ULong_t
unsigned long ULong_t
Definition: RtypesCore.h:55
TBuffer::ReadVersion
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
TBuffer::IsReading
Bool_t IsReading() const
Definition: TBuffer.h:86
TTreeRow::Close
void Close(Option_t *option="")
Close row.
Definition: TTreeRow.cxx:98
TTreeRow::fOriginal
TTreeRow * fOriginal
! pointer to original row
Definition: TTreeRow.h:44
TTreeRow
Definition: TTreeRow.h:29
TTreeRow::fColumnCount
Int_t fColumnCount
number of columns in row
Definition: TTreeRow.h:41
TTreeRow.h
Class
void Class()
Definition: Class.C:29
TTreeRow::fRow
char * fRow
string with all the fColumnCount fields
Definition: TTreeRow.h:43
int