StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TTable.h
1 // @(#)root/table:$Id$
2 // Author: Valery Fine(fine@mail.cern.ch) 03/07/98
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 #ifndef ROOT_TTable
13 #define ROOT_TTable
14 
16 // //
17 // TTable //
18 // //
19 // It is a base class to create a "wrapper" class //
20 // holding the plain C-structure array //
21 // (1 element of the structure per element) //
22 // //
24 
25 #ifdef __CINT__
26 #pragma Ccomment on
27 #endif
28 
29 #include "Ttypes.h"
30 #include "TDataSet.h"
31 #include "tableDescriptor.h"
32 #ifndef ROOT_TCut
33 # include "TCut.h"
34 #endif
35 
36 #ifndef __CINT__
37 # include <string.h>
38 # include <assert.h>
39 #endif
40 
41 #include <vector>
42 
43 class TTableDescriptor;
44 class TH1;
45 class TTableMap;
46 typedef TTableMap* Ptr_t;
47 
48 class TTable : public TDataSet {
49  friend class TDataSet;
50  friend class St_XDFFile;
51 protected:
52  Long_t fSize; // Size of the one element (row) of the table
53 
54 protected:
55 
56  Int_t fN; //Number of array elements
57  Char_t *fTable; // Array of (fN*fSize) longs
58  Long_t fMaxIndex; // The used capacity of this array
59 
60  Bool_t BoundsOk(const char *where, Int_t at) const;
61  Bool_t OutOfBoundsError(const char *where, Int_t i) const;
62 
63  void CopyStruct(Char_t *dest, const Char_t *src);
64  Char_t *Create();
65  virtual void Clear(Option_t *opt="");
66  virtual void Delete(Option_t *opt="");
67  virtual Bool_t EntryLoop(const Char_t *exprFileName,Int_t &action, TObject *obj, Int_t nentries=1000000000, Int_t firstentry=0, Option_t *option="");
68  Int_t SetfN(Long_t len);
69  void SetTablePointer(void *table);
70  void SetUsedRows(Int_t n);
71  virtual void SetType(const char *const type);
72  void StreamerHeader(TBuffer &b,Version_t version=3);
73  void StreamerTable(TBuffer &b,Version_t version=3);
74  virtual TTableDescriptor *GetDescriptorPointer() const;
75  virtual void SetDescriptorPointer(TTableDescriptor *list);
76 
77  void ReAlloc(Int_t newsize);
78  static const char *TableDictionary(const char *className,const char *structName,TTableDescriptor *&ColDescriptors);
79 
80 public:
81 
82  enum EColumnType {kNAN, kFloat, kInt, kLong, kShort, kDouble, kUInt
83  ,kULong, kUShort, kUChar, kChar, kPtr, kBool
84  ,kEndColumnType };
85  enum ETableBits {
86  kIsNotOwn = BIT(23) // if the TTable wrapper doesn't own the STAF table
87  // As result of the Update() method for example
88  };
89  static const char *fgTypeName[kEndColumnType];
90  TTable(const char *name=0, Int_t size=0);
91  TTable(const char *name, Int_t n,Int_t size);
92  TTable(const char *name, Int_t n, Char_t *array,Int_t size);
93  TTable(const char *name, const char *type, Int_t n, Char_t *array, Int_t size);
94  TTable(const TTable &table);
95  TTable &operator=(const TTable &rhs);
96  virtual ~TTable();
97 
98  virtual void Adopt(Int_t n, void *array);
99  virtual Int_t AddAt(const void *c);
100  virtual void AddAt(const void *c, Int_t i);
101  virtual void AddAt(TDataSet *dataset,Int_t idx=0);
102  virtual Long_t AppendRows(const void *row, UInt_t nRows);
103  virtual void AsString(void *buf, EColumnType type, Int_t width, std::ostream &out) const;
104  const void *At(Int_t i) const;
105  virtual void Browse(TBrowser *b);
106  virtual void CopySet(TTable &array);
107  Int_t CopyRows(const TTable *srcTable,Long_t srcRow=0, Long_t dstRow=0, Long_t nRows=0, Bool_t expand=kFALSE);
108  virtual void DeleteRows(Long_t indx,UInt_t nRows=1);
109  virtual void Draw(Option_t *opt);
110  virtual TH1 *Draw(TCut varexp, TCut selection, Option_t *option=""
111  ,Int_t nentries=1000000000, Int_t firstentry=0);
112  virtual TH1 *Draw(const char *varexp, const char *selection, Option_t *option=""
113  ,Int_t nentries=1000000000, Int_t firstentry=0); // *MENU*
114  void *GetArray() const ;
115  virtual TClass *GetRowClass() const ;
116  Int_t GetSize() const { return fN; }
117  virtual Long_t GetNRows() const;
118  virtual Long_t GetRowSize() const;
119  virtual Long_t GetTableSize() const;
120  virtual TTableDescriptor *GetTableDescriptors() const;
121  virtual TTableDescriptor *GetRowDescriptors() const;
122  virtual const Char_t *GetType() const;
123  virtual void Fit(const char *formula ,const char *varexp, const char *selection="",Option_t *option="" ,Option_t *goption=""
124  ,Int_t nentries=1000000000, Int_t firstentry=0); // *MENU*
125 
126  virtual Long_t HasData() const { return 1; }
127  virtual Long_t InsertRows(const void *rows, Long_t indx, UInt_t nRows=1);
128  virtual Bool_t IsFolder() const;
129  Int_t NaN();
130  static TTable *New(const Char_t *name, const Char_t *type, void *array, UInt_t size);
131  virtual Char_t *MakeExpression(const Char_t *expressions[],Int_t nExpressions);
132  virtual Char_t *Print(Char_t *buf,Int_t n) const ;
133  virtual void Print(Option_t *opt="") const;
134  virtual const Char_t *Print(Int_t row, Int_t rownumber=10,
135  const Char_t *colfirst="", const Char_t *collast="") const; // *MENU*
136  virtual void PrintContents(Option_t *opt="") const;
137  virtual const Char_t *PrintHeader() const; // *MENU*
138  virtual void Project(const char *hname, const char *varexp, const char *selection="", Option_t *option=""
139  ,Int_t nentries=1000000000, Int_t firstentry=0);
140 
141  virtual Int_t Purge(Option_t *opt="");
142 
143  void *ReAllocate(Int_t newsize);
144  void *ReAllocate();
145  virtual void SavePrimitive(std::ostream &out, Option_t *option = "");
146  virtual void Set(Int_t n);
147  virtual void Set(Int_t n, Char_t *array);
148  virtual void SetNRows(Int_t n);
149  virtual void Reset(Int_t c=0);
150  virtual void ResetMap(Bool_t wipe=kTRUE);
151  virtual void Update();
152  virtual void Update(TDataSet *set,UInt_t opt=0);
153  void *operator[](Int_t i);
154  const void *operator[](Int_t i) const;
155 
156 
157  // ---- Table descriptor service ------
158 
159  virtual Int_t GetColumnIndex(const Char_t *columnName) const;
160  virtual const Char_t *GetColumnName(Int_t columnIndex) const;
161  virtual const UInt_t *GetIndexArray(Int_t columnIndex) const;
162  virtual UInt_t GetNumberOfColumns() const;
163  virtual UInt_t GetOffset(Int_t columnIndex) const;
164  virtual Int_t GetOffset(const Char_t *columnName=0) const;
165  virtual UInt_t GetColumnSize(Int_t columnIndex) const;
166  virtual Int_t GetColumnSize(const Char_t *columnName=0) const;
167  virtual UInt_t GetTypeSize(Int_t columnIndex) const;
168  virtual Int_t GetTypeSize(const Char_t *columnName=0) const ;
169  virtual UInt_t GetDimensions(Int_t columnIndex) const;
170  virtual Int_t GetDimensions(const Char_t *columnName=0) const ;
171  virtual EColumnType GetColumnType(Int_t columnIndex) const;
172  virtual EColumnType GetColumnType(const Char_t *columnName=0) const;
173  virtual const Char_t *GetColumnComment(Int_t columnIndex) const;
174 
175  static const char *GetTypeName(EColumnType type);
176  static EColumnType GetTypeId(const char *typeName);
177 
178  // Table index iterator:
179  class iterator {
180  public:
181  typedef std::vector<Long_t>::iterator vec_iterator;
182  typedef std::vector<Long_t>::const_iterator vec_const_iterator;
183  private:
184  Long_t fRowSize;
185  const TTable *fThisTable;
186  vec_iterator fCurrentRow;
187  public:
188  iterator(const TTable &table, vec_iterator &arowPtr) :
189  fRowSize(table.GetRowSize()), fThisTable(&table), fCurrentRow(arowPtr) {;}
190  iterator(const TTable &table, vec_const_iterator &arowPtr) :
191  fRowSize(table.GetRowSize()), fThisTable(&table),
192  fCurrentRow(*(std::vector<Long_t>::iterator *)(void *)&arowPtr) {;}
193  //fCurrentRow(* const_cast<std::vector<Long_t>::iterator *>(&arowPtr) ) {;}
194  iterator(const iterator& iter) : fRowSize (iter.fRowSize), fThisTable(iter.fThisTable),fCurrentRow(iter.fCurrentRow){}
195  iterator &operator=(const iterator& iter) { fRowSize = iter.fRowSize; fThisTable = iter.fThisTable; fCurrentRow=iter.fCurrentRow; return *this; }
196  void operator++();
197  void operator++(int);
198  void operator--();
199  void operator--(int);
200  iterator operator+(Int_t idx);
201  iterator operator-(Int_t idx);
202  void operator+=(Int_t idx);
203  void operator-=(Int_t idx);
204  void *rowPtr() const;
205  operator void *() const;
206  Int_t operator-(const iterator &it) const;
207  Long_t operator *() const;
208  Bool_t operator==(const iterator &t) const;
209  Bool_t operator!=(const iterator &t) const;
210 
211  const TTable &Table() const { return *fThisTable;}
212  const Long_t &RowSize() const { return fRowSize;}
213 #ifndef __CINT__
214  const std::vector<Long_t>::iterator &Row() const { return fCurrentRow;}
215 #endif
216  };
217 
218 #ifndef __CINT__
219  // pointer iterator
220  // This create an iterator to iterate over all table column of the
221  // type provided.
222  // For example" piterator(table,kPtr) is to iterate over
223  // all cells of (TTableMap *) type
224 
225  class piterator {
226  private:
227  std::vector<ULong_t> fPtrs;
228  UInt_t fCurrentRowIndex;
229  UInt_t fCurrentColIndex;
230  UInt_t fRowSize;
231  const Char_t *fCurrentRowPtr;
232  void **fCurrentColPtr;
233 
234  protected:
235  void **column() {return fCurrentColPtr = (void **)(fCurrentRowPtr + fPtrs[fCurrentColIndex]);}
236 
237  public:
238  piterator(const TTable *t=0,EColumnType type=kPtr);
239  piterator(const piterator& iter);
240  void operator=(const piterator& iter);
241 
242  void operator++();
243  void operator++(int);
244  void operator--();
245  void operator--(int);
246 
247 // operator const char *() const;
248  void **operator *();
249 
250  Bool_t operator==(const piterator &t) const;
251  Bool_t operator!=(const piterator &t) const;
252 
253  UInt_t Row() const;
254  UInt_t Column() const;
255 
256  void MakeEnd(UInt_t lastRowIndex);
257  }; // class iterator over pointers
258 
259  piterator pbegin(){ return piterator(this); }
260  piterator pend(){ piterator pLast(this); pLast.MakeEnd(GetNRows()); return pLast; }
261 #endif
262  static const char *TableDictionary() { return 0; };
263  ClassDef(TTable,4) // vector of the C structures
264 };
265 
266 //________________________________________________________________________
267 inline void TTable::AddAt(TDataSet *dataset,Int_t idx)
268 { TDataSet::AddAt(dataset,idx); }
269 
270 //________________________________________________________________________
271 inline Bool_t TTable::BoundsOk(const char *where, Int_t at) const
272 {
273  return (at < 0 || at >= fN)
274  ? OutOfBoundsError(where, at)
275  : kTRUE;
276 }
277 
278 //________________________________________________________________________
279 inline void *TTable::GetArray() const { return (void *)fTable;}
280 
281 //________________________________________________________________________
282 inline void TTable::Print(Option_t *) const { Print((Char_t *)0,Int_t(0)); }
283 
284 //________________________________________________________________________
285 inline void TTable::SetUsedRows(Int_t n) { fMaxIndex = n;}
286 //________________________________________________________________________
287 inline void TTable::SetNRows(Int_t n) {SetUsedRows(n);}
288 // ULong_t &operator(){ return GetTable();}
289 
290 //________________________________________________________________________
291 inline void *TTable::operator[](Int_t i)
292 {
293  if (!BoundsOk("TTable::operator[]", i))
294  i = 0;
295  return (void *)(fTable+i*fSize);
296 }
297 
298 //________________________________________________________________________
299 inline const void *TTable::operator[](Int_t i) const
300 {
301  if (!BoundsOk("TTable::operator[]", i))
302  i = 0;
303  return (const void *)(fTable+i*fSize);
304 }
305 
306 //________________________________________________________________________
307 inline void TTable::Draw(Option_t *opt)
308 { Draw(opt, "", "", 1000000000, 0); }
309 
310 #ifndef __CINT__
311  //________________________________________________________________________________________________________________
312  inline TTable::piterator::piterator(const piterator& iter) :
313  fPtrs (iter.fPtrs),
314  fCurrentRowIndex(iter.fCurrentRowIndex),
315  fCurrentColIndex(iter.fCurrentColIndex),
316  fRowSize(iter.fRowSize),
317  fCurrentRowPtr(iter.fCurrentRowPtr),
318  fCurrentColPtr(iter.fCurrentColPtr)
319  {}
320  //________________________________________________________________________________________________________________
321  inline void TTable::piterator::operator=(const piterator& iter){
322  fPtrs = iter.fPtrs;
323  fCurrentRowIndex = iter.fCurrentRowIndex;
324  fCurrentColIndex = iter.fCurrentColIndex;
325  fRowSize = iter.fRowSize;
326  fCurrentRowPtr = iter.fCurrentRowPtr;
327  fCurrentColPtr = iter.fCurrentColPtr;
328  }
329  //________________________________________________________________________________________________________________
330  inline void TTable::piterator::operator++()
331  {
332  ++fCurrentColIndex;
333  if (fCurrentColIndex >= fPtrs.size()) {
334  fCurrentColIndex = 0;
335  ++fCurrentRowIndex;
336  fCurrentRowPtr += fRowSize;
337  }
338  column();
339  }
340  //________________________________________________________________________________________________________________
341  inline void TTable::piterator::operator++(int) { operator++(); }
342  //________________________________________________________________________________________________________________
343  inline void TTable::piterator::operator--()
344  {
345  if (fCurrentColIndex > 0) {
346  fCurrentColIndex--;
347  fCurrentColIndex = fPtrs.size()-1;
348  --fCurrentRowIndex;
349  fCurrentRowPtr -= fRowSize;
350  } else {
351  fCurrentColIndex--;
352  }
353  column();
354  }
355  //________________________________________________________________________________________________________________
356  inline void TTable::piterator::operator--(int) { operator--(); }
357  //________________________________________________________________________________________________________________
358  // inline TTable::piterator::operator const char *() const { return fCurrentColPtr; }
359  //________________________________________________________________________________________________________________
360  inline void **TTable::piterator::operator *() { return fCurrentColPtr; }
361  //________________________________________________________________________________________________________________
362  inline Bool_t TTable::piterator::operator==(const piterator &t) const {
363  return (
364  (fCurrentRowIndex== t.fCurrentRowIndex)
365  && (fCurrentColIndex == t.fCurrentColIndex)
366 // && (fCurrentRowPtr == t.fCurrentRowPtr )
367 // && (fCurrentColPtr == t.fCurrentColPtr )
368  );
369  }
370  //________________________________________________________________________________________________________________
371  inline Bool_t TTable::piterator::operator!=(const piterator &t) const { return !operator==(t); }
372  //________________________________________________________________________________________________________________
373  inline void TTable::piterator::MakeEnd(UInt_t lastRowIndex){fCurrentColIndex = 0; fCurrentRowIndex = lastRowIndex;}
374  //________________________________________________________________________________________________________________
375  inline UInt_t TTable::piterator::Row() const { return fCurrentRowIndex;}
376  //________________________________________________________________________________________________________________
377  inline UInt_t TTable::piterator::Column() const { return fCurrentColIndex;}
378 #endif
379 
380 #endif
virtual Long_t InsertRows(const void *rows, Long_t indx, UInt_t nRows=1)
Definition: TTable.cxx:1203
Int_t SetfN(Long_t len)
to be documented
Definition: TTable.cxx:2174
virtual TClass * GetRowClass() const
to be documented
Definition: TTable.cxx:1375
void * ReAllocate()
Definition: TTable.cxx:1222
virtual Long_t GetRowSize() const
Returns the size (in bytes) of one table row.
Definition: TTable.cxx:1395
virtual const Char_t * GetType() const
Returns the type of the wrapped C-structure kept as the TNamed title.
Definition: TTable.cxx:1444
void StreamerHeader(TBuffer &b, Version_t version=3)
Read &quot;table parameters first&quot;.
Definition: TTable.cxx:2135
TTable & operator=(const TTable &rhs)
Definition: TTable.cxx:1083
virtual void Browse(TBrowser *b)
Wrap each table coulumn with TColumnView object to browse.
Definition: TTable.cxx:1294
virtual const Char_t * PrintHeader() const
Print general table inforamtion.
Definition: TTable.cxx:1620
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a primitive as a C++ statement(s) on output stream &quot;out&quot;.
Definition: TTable.cxx:1810
TTable(const char *name=0, Int_t size=0)
Default TTable ctor.
Definition: TTable.cxx:1031
virtual void PrintContents(Option_t *opt="") const
to be documented
Definition: TTable.cxx:1770
static const char * TableDictionary(const char *className, const char *structName, TTableDescriptor *&ColDescriptors)
Definition: TTable.cxx:2410
virtual void Set(Int_t n)
Set array size of TTable object to n longs. If n&lt;0 leave array unchanged.
Definition: TTable.cxx:1952
Int_t CopyRows(const TTable *srcTable, Long_t srcRow=0, Long_t dstRow=0, Long_t nRows=0, Bool_t expand=kFALSE)
Definition: TTable.cxx:332
virtual Bool_t EntryLoop(const Char_t *exprFileName, Int_t &action, TObject *obj, Int_t nentries=1000000000, Int_t firstentry=0, Option_t *option="")
Definition: TTable.cxx:785
virtual TTableDescriptor * GetRowDescriptors() const
to be documented
Definition: TTable.cxx:2222
virtual void SetType(const char *const type)
to be documented
Definition: TTable.cxx:1973
virtual Long_t GetNRows() const
Returns the number of the used rows for the wrapped table.
Definition: TTable.cxx:1388
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
Definition: TTable.cxx:1540
virtual Int_t AddAt(const void *c)
Definition: TTable.cxx:1122
virtual Long_t AppendRows(const void *row, UInt_t nRows)
Definition: TTable.cxx:1178
virtual void Fit(const char *formula, const char *varexp, const char *selection="", Option_t *option="", Option_t *goption="", Int_t nentries=1000000000, Int_t firstentry=0)
Definition: TTable.cxx:1422
virtual void Update()
to be documented
Definition: TTable.cxx:2377
Int_t NaN()
Definition: TTable.cxx:1474
virtual void ResetMap(Bool_t wipe=kTRUE)
Definition: TTable.cxx:2087
virtual void DeleteRows(Long_t indx, UInt_t nRows=1)
Definition: TTable.cxx:364
virtual ~TTable()
Delete TTable object.
Definition: TTable.cxx:1098
virtual TTableDescriptor * GetTableDescriptors() const
protected: create a new TTableDescriptor descriptor for this table
Definition: TTable.cxx:214
void StreamerTable(TBuffer &b, Version_t version=3)
Stream an object of class TTable.
Definition: TTable.cxx:2117
virtual void AddAt(TDataSet *dataset, Int_t idx=0)
Definition: TDataSet.cxx:235
void SetTablePointer(void *table)
to be documented
Definition: TTable.cxx:1964
void CopyStruct(Char_t *dest, const Char_t *src)
Definition: TTable.cxx:1151
static EColumnType GetTypeId(const char *typeName)
Definition: TTable.cxx:292
static TTable * New(const Char_t *name, const Char_t *type, void *array, UInt_t size)
This static method creates a new TTable object if provided.
Definition: TTable.cxx:1515
virtual Long_t GetTableSize() const
Returns the number of the allocated rows.
Definition: TTable.cxx:1402
Definition: TTable.h:48
virtual Char_t * MakeExpression(const Char_t *expressions[], Int_t nExpressions)
Definition: TTable.cxx:2003
static const char * GetTypeName(EColumnType type)
return table type name
Definition: TTable.cxx:282
virtual void Project(const char *hname, const char *varexp, const char *selection="", Option_t *option="", Int_t nentries=1000000000, Int_t firstentry=0)
Definition: TTable.cxx:1787
Char_t * Create()
Definition: TTable.cxx:1268
const void * At(Int_t i) const
Returns a pointer to the i-th row of the table.
Definition: TTable.cxx:303
virtual TTableDescriptor * GetDescriptorPointer() const
to be documented
Definition: TTable.cxx:2236
virtual void SetDescriptorPointer(TTableDescriptor *list)
to be documented
Definition: TTable.cxx:2245
piterator(const TTable *t=0, EColumnType type=kPtr)
to be documented
Definition: TTable.cxx:2444
virtual void Delete(Option_t *opt="")
Definition: TTable.cxx:1366
virtual Bool_t IsFolder() const
Definition: TTable.cxx:1456
virtual void Clear(Option_t *opt="")
Definition: TTable.cxx:1342
virtual Char_t * Print(Char_t *buf, Int_t n) const
Create IDL table defintion (to be used for XDF I/O)
Definition: TTable.cxx:1548
virtual void Adopt(Int_t n, void *array)
Definition: TTable.cxx:1107
virtual void CopySet(TTable &array)
to be documented
Definition: TTable.cxx:1158
void ReAlloc(Int_t newsize)
The table is reallocated if it is an owner of the internal array.
Definition: TTable.cxx:1243
virtual const Char_t * GetColumnComment(Int_t columnIndex) const
Get a comment from the table descriptor.
Definition: TTable.cxx:1166
virtual void AsString(void *buf, EColumnType type, Int_t width, std::ostream &out) const
Definition: TTable.cxx:229
virtual void Reset(Int_t c=0)
Fill the entire table with byte &quot;c&quot; ; / c=0 &quot;be default&quot;
Definition: TTable.cxx:2072
virtual Int_t Purge(Option_t *opt="")
Shrink the table to free the unused but still allocated rows.
Definition: TTable.cxx:1801