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