Logo ROOT   6.12/07
Reference Guide
TGenericTable.cxx
Go to the documentation of this file.
1 // @(#)root/table:$Id$
2 // Author: Valery Fine(fine@bnl.gov) 30/06/2001
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 
13 #include "TGenericTable.h"
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // TGenericTable //
18 // //
19 // This is the class to represent the array of C-struct //
20 // defined at run-time //
21 // //
22 // Example: see $ROOTSYS/tutorials/tree/staff.C //
23 // ------- //
24 // !{
25 // !// example of macro to read data from an ascii file and
26 // !// create a root file with an histogram and an ntuple.
27 // !// A'la the famous ROOT/PAW staff data example
28 // !// ( see PAW - Long write up, CERN, page33. )
29 // !
30 // ! gROOT->Reset();
31 // ! gSystem->Load("libRootKernel");
32 // !
33 // ! struct staff_t {
34 // ! Int_t cat;
35 // ! Int_t division;
36 // ! Int_t flag;
37 // ! Int_t age;
38 // ! Int_t service;
39 // ! Int_t children;
40 // ! Int_t grade;
41 // ! Int_t step;
42 // ! Int_t nation;
43 // ! Int_t hrweek;
44 // ! Int_t cost;
45 // ! };
46 // !
47 // ! staff_t staff;
48 // !
49 // ! // open ASCII data file
50 // ! FILE *fp = fopen("staff.dat","r");
51 // !
52 // ! char line[81];
53 // !
54 // ! // Create the generic table for 1000 rows (it may grow then)
55 // ! TGenericTable *allStaff = new TGenericTable("staff_t","Staff-data",1000);
56 // !
57 // ! // Fill the memory resident table
58 // ! while (fgets(&line,80,fp)) {
59 // ! sscanf(&line[0] ,"%d%d%d%d", &staff.cat,&staff.division,&staff.flag,&staff.age);
60 // ! sscanf(&line[13],"%d%d%d%d", &staff.service,&staff.children,&staff.grade,&staff.step);
61 // ! sscanf(&line[24],"%d%d%d", &staff.nation,&staff.hrweek,&staff.cost);
62 // ! allStaff->AddAt(&staff);
63 // ! }
64 // ! fclose(fp);
65 // ! // Delete unused space;
66 // ! allStaff->Purge();
67 // !
68 // ! allStaff->Print(0,10);
69 // !
70 // !// Create ROOT file
71 // ! TFile *f = new TFile("aptuple.root","RECREATE");
72 // ! allStaff->Write();
73 // ! f->Write();
74 // !
75 // ! // We should close TFile otherwise all histograms we create below
76 // ! // may be written to the file too occasionaly
77 // ! f->Close();
78 // !
79 // !// Create ROOT Browser
80 // ! new TBrowser("staff",allStaff);
81 // !
82 // !// Create couple of the histograms
83 // ! TCanvas *canva = new TCanvas("Staff","CERN Population",600,600);
84 // ! canva->Divide(1,2);
85 // !
86 // !
87 // !// one can use 2 meta variable:
88 // !// n$ - the total number of the rows in the table
89 // !// i$ - stands for the current row index i = [0 -> (n$-1)]
90 // !
91 // ! gStyle->SetHistFillColor(10);
92 // ! gStyle->SetHistFillStyle(3013);
93 // ! canva->cd(1);
94 // ! allStaff->Draw("age");
95 // ! canva->Update();
96 // ! canva->cd(2);
97 // ! allStaff->Draw("cost");
98 // ! canva->Update();
99 // !}
100 //
101 //////////////////////////////////////////////////////////////////////////
102 
105 
106 // Create TGenericTable by TTableDescriptor pointer
107 ////////////////////////////////////////////////////////////////////////////////
108 ////////////////////////////////////////////////////////////
109 ///
110 /// Create TGenericTable by TTableDescriptor pointer:
111 ///
112 /// dsc - Pointer to the table descriptor
113 /// name - The name of this object
114 ///
115 ////////////////////////////////////////////////////////////
116 
117 TGenericTable::TGenericTable(const TTableDescriptor &dsc, const char *name) : TTable(name,dsc.Sizeof()),fColDescriptors(0)
118 {
119  // Create a private copy of the descriptor provided;
120  SetDescriptorPointer(new TTableDescriptor(dsc));
121  SetGenericType();
122 }
123 ////////////////////////////////////////////////////////////////////////////////
124 ////////////////////////////////////////////////////////////
125 ///
126 /// Create TGenericTable by TTableDescriptor pointer:
127 ///
128 /// dsc - Pointer to the table descriptor
129 /// name - "TGenericTable"
130 /// n - The initial number of allocated rows
131 ///
132 ////////////////////////////////////////////////////////////
133 
134 TGenericTable::TGenericTable(const TTableDescriptor &dsc, Int_t n) : TTable("TGenericTable",n,dsc.Sizeof()),fColDescriptors(0)
135 {
136  // Create a provate copy of the descriptor provided;
138  SetGenericType();
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 ////////////////////////////////////////////////////////////
143 ///
144 /// Create TGenericTable by TTableDescriptor pointer:
145 ///
146 /// dsc - Pointer to the table descriptor
147 /// name - The name of this object
148 /// n - The initial number of allocated rows
149 ///
150 ////////////////////////////////////////////////////////////
151 
153 {
154  // Create a provate copy of the descriptor provided;
156  SetGenericType();
157 }
158 
159 // Create TGenericTable by C structure name provided
160 ////////////////////////////////////////////////////////////////////////////////
161 ////////////////////////////////////////////////////////////
162 ///
163 /// Create TGenericTable by C structure name provided:
164 ///
165 /// dsc - Pointer to the table descriptor
166 /// name - The name of this object
167 /// n - The initial number of allocated rows
168 ///
169 ////////////////////////////////////////////////////////////
170 
171 TGenericTable::TGenericTable(const char *structName, const char *name) : TTable(name,-1),fColDescriptors(0)
172 {
174  if (dsc) {
176  fSize = dsc->Sizeof();
177  }
178  if ( !dsc || !fSize) Warning("TGenericTable","Wrong table format");
179  SetGenericType();
180 }
181 ////////////////////////////////////////////////////////////////////////////////
182 ////////////////////////////////////////////////////////////
183 ///
184 /// Create TGenericTable by C structure name provided:
185 ///
186 /// dsc - Pointer to the table descriptor
187 /// name - The name of this object
188 /// n - The initial number of allocated rows
189 ///
190 ////////////////////////////////////////////////////////////
191 
192 TGenericTable::TGenericTable(const char *structName, Int_t n) : TTable("TGenericTable",-1),fColDescriptors(0)
193 {
195  if (dsc) {
197  fSize = dsc->Sizeof();
198  }
199  if ( !dsc || !fSize) Warning("TGenericTable","Wrong table format");
200  if (n > 0) Set(n);
201  SetGenericType();
202 }
203 ////////////////////////////////////////////////////////////////////////////////
204 ////////////////////////////////////////////////////////////
205 ///
206 /// Create TGenericTable by C structure name provided:
207 ///
208 /// dsc - Pointer to the table descriptor
209 /// name - The name of this object
210 /// n - The initial number of allocated rows
211 ///
212 ////////////////////////////////////////////////////////////
213 
214 TGenericTable::TGenericTable(const char *structName, const char *name,Int_t n) : TTable(name,-1),fColDescriptors(0)
215 {
217  if (dsc) {
219  fSize = dsc->Sizeof();
220  }
221  if ( !dsc || !fSize) Warning("TGenericTable","Wrong table format dsc=0x%lx, size=%ld",(Long_t)dsc,fSize);
222  if (n > 0) Set(n);
223  SetGenericType();
224 }
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 ///destructor
228 
230 {
231  delete fColDescriptors;
232 }
Long_t fSize
Definition: TTable.h:52
virtual void SetDescriptorPointer(TTableDescriptor *list)
to be documented
Definition: TGenericTable.h:22
virtual Int_t Sizeof() const
Return size of the TNamed part of the TObject.
Definition: TNamed.cxx:173
int Int_t
Definition: RtypesCore.h:41
TTableDescriptor * fColDescriptors
Definition: TGenericTable.h:20
virtual void Set(Int_t n)
Set array size of TTable object to n longs. If n<0 leave array unchanged.
Definition: TTable.cxx:1951
void SetGenericType()
Definition: TGenericTable.h:23
long Long_t
Definition: RtypesCore.h:50
#define ClassImp(name)
Definition: Rtypes.h:359
Definition: TTable.h:48
virtual ~TGenericTable()
destructor
static TTableDescriptor * MakeDescriptor(const char *structName)
MakeDescriptor(const char *structName) - static method structName - the name of the C structure to cr...
TableClassStreamerImp(TGenericTable) TGenericTable
Create TGenericTable by TTableDescriptor pointer:
Int_t Sizeof() const
to be documented
const Int_t n
Definition: legend1.C:16
char name[80]
Definition: TGX11.cxx:109
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:866