TTreeRow.cxx

Go to the documentation of this file.
00001 // @(#)root/tree:$Id: TTreeRow.cxx 31603 2009-12-07 18:59:15Z pcanal $
00002 // Author: Fons Rademakers   30/11/99
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TTreeRow                                                             //
00015 //                                                                      //
00016 // Class defining interface to a row of a TTree query result.           //
00017 // Objects of this class are created by TTreeResult methods.            //
00018 //                                                                      //
00019 // Related classes are TTreeResult.                                     //
00020 //                                                                      //
00021 //////////////////////////////////////////////////////////////////////////
00022 
00023 #include "TTreeRow.h"
00024 #include "TObjArray.h"
00025 
00026 ClassImp(TTreeRow)
00027 
00028 //______________________________________________________________________________
00029 TTreeRow::TTreeRow()
00030 {
00031    // Single row of a query result.
00032 
00033    fColumnCount = 0;
00034    fFields      = 0;
00035    fOriginal    = 0;
00036    fRow         = 0;
00037    
00038 }
00039 
00040 //______________________________________________________________________________
00041 TTreeRow::TTreeRow(Int_t nfields)
00042 {
00043    // Single row of a query result.
00044 
00045    fColumnCount = nfields;
00046    fFields      = 0;
00047    fOriginal    = 0;
00048    fRow         = 0;
00049    
00050 }
00051 
00052 //______________________________________________________________________________
00053 TTreeRow::TTreeRow(Int_t nfields, const Int_t *fields, const char *row)
00054 {
00055    // Single row of a query result.
00056 
00057    fColumnCount = nfields;
00058    fFields      = 0;
00059    fOriginal    = 0;
00060    fRow         = 0;
00061    SetRow(fields,row);
00062 }
00063 
00064 //______________________________________________________________________________
00065 TTreeRow::TTreeRow(TSQLRow *original)
00066 {
00067    // This is a shallow copy of a real row, i.e. it only contains
00068    // a pointer to the original.
00069 
00070    fFields      = 0;
00071    fOriginal    = 0;
00072    fColumnCount = 0;
00073    fRow         = 0;
00074    
00075    if (!original) {
00076       Error("TTreeRow", "original may not be 0");
00077       return;
00078    }
00079    if (original->IsA() != TTreeRow::Class()) {
00080       Error("TTreeRow", "original must be a TTreeRow");
00081       return;
00082    }
00083 
00084    fOriginal = (TTreeRow*) original;
00085    fColumnCount = fOriginal->fColumnCount;
00086 }
00087 
00088 //______________________________________________________________________________
00089 TTreeRow::~TTreeRow()
00090 {
00091    // Destroy row object.
00092 
00093    if (fFields)
00094       Close();
00095 }
00096 
00097 //______________________________________________________________________________
00098 void TTreeRow::Close(Option_t *)
00099 {
00100    // Close row.
00101 
00102    if (fRow)    delete [] fRow;
00103    if (fFields) delete [] fFields;
00104    fColumnCount = 0;
00105    fOriginal = 0;
00106 }
00107 
00108 //______________________________________________________________________________
00109 Bool_t TTreeRow::IsValid(Int_t field)
00110 {
00111    // Check if row is open and field index within range.
00112 
00113    if (!fFields && !fOriginal) {
00114       Error("IsValid", "row closed");
00115       return kFALSE;
00116    }
00117    if (field < 0 || field >= fColumnCount) {
00118       Error("IsValid", "field index out of bounds");
00119       return kFALSE;
00120    }
00121    return kTRUE;
00122 }
00123 
00124 //______________________________________________________________________________
00125 ULong_t TTreeRow::GetFieldLength(Int_t field)
00126 {
00127    // Get length in bytes of specified field.
00128 
00129    if (!IsValid(field))
00130       return 0;
00131 
00132    if (fOriginal)
00133       return fOriginal->GetFieldLength(field);
00134 
00135    if (field > 0) return fFields[field] - fFields[field-1] -1;
00136    else           return fFields[0] -1;
00137 }
00138 
00139 //______________________________________________________________________________
00140 const char *TTreeRow::GetField(Int_t field) 
00141 {
00142    // Get specified field from row (0 <= field < GetFieldCount()).
00143 
00144    if (!IsValid(field))
00145       return 0;
00146 
00147    if (fOriginal)
00148       return fOriginal->GetField(field);
00149 
00150    if (field > 0) return fRow +fFields[field-1];
00151    else           return fRow;
00152 }
00153 
00154 //______________________________________________________________________________
00155 void TTreeRow::SetRow(const Int_t *fields, const char *row)
00156 {
00157    // The field and row information.
00158 
00159    if (!fColumnCount) return;
00160    if (fFields) delete [] fFields;
00161    Int_t nch    = fields[fColumnCount-1];
00162    fFields      = new Int_t[fColumnCount];
00163    fOriginal    = 0;
00164    fRow         = new char[nch];
00165    for (Int_t i=0;i<fColumnCount;i++) fFields[i] = fields[i];
00166    memcpy(fRow,row,nch);
00167 }
00168 
00169 //______________________________________________________________________________
00170 void TTreeRow::Streamer(TBuffer &R__b)
00171 {
00172    // Stream an object of class TTreeRow.
00173    UInt_t R__s, R__c;
00174    if (R__b.IsReading()) {
00175       R__b.ReadVersion(&R__s, &R__c);
00176       TSQLRow::Streamer(R__b);
00177       R__b >> fColumnCount;
00178       fFields = new Int_t[fColumnCount];
00179       R__b.ReadFastArray(fFields,fColumnCount);
00180       Int_t nch;
00181       R__b >> nch;
00182       fRow = new char[nch];
00183       R__b.ReadFastArray(fRow,nch);
00184       R__b.CheckByteCount(R__s, R__c, TTreeRow::IsA());
00185    } else {
00186       R__c = R__b.WriteVersion(TTreeRow::Class(),kTRUE);
00187       TSQLRow::Streamer(R__b);
00188       R__b << fColumnCount;
00189       R__b.WriteFastArray(fFields,fColumnCount);
00190       Int_t nch = fFields[fColumnCount-1];
00191       R__b << nch;
00192       R__b.WriteFastArray(fRow,nch);
00193       R__b.SetByteCount(R__c,kTRUE);
00194    }
00195 }

Generated on Tue Jul 5 15:34:15 2011 for ROOT_528-00b_version by  doxygen 1.5.1