StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TGenericTable.cxx
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 
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 //
102 
103  ClassImp(TGenericTable);
104  TableClassStreamerImp(TGenericTable)
105 
106 // Create TGenericTable by TTableDescriptor pointer
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 }
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 
151 
152 TGenericTable::TGenericTable(const TTableDescriptor &dsc,const char *name,Int_t n) : TTable(name,n,dsc.Sizeof()),fColDescriptors(0)
153 {
154  // Create a provate copy of the descriptor provided;
156  SetGenericType();
157 }
158 
159 // Create TGenericTable by C structure name provided
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 }
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 }
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 
228 
230 {
231  delete fColDescriptors;
232 }
virtual void SetDescriptorPointer(TTableDescriptor *list)
to be documented
Definition: TGenericTable.h:22
Int_t Sizeof() const
to be documented
virtual void Set(Int_t n)
Set array size of TTable object to n longs. If n<0 leave array unchanged.
Definition: TTable.cxx:1952
Definition: TTable.h:48
virtual ~TGenericTable()
destructor
static TTableDescriptor * MakeDescriptor(const char *structName)