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