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