00001 // @(#)root/table:$Id: TGenericTable.cxx 34233 2010-06-30 12:28:20Z brun $ 00002 // Author: Valery Fine(fine@bnl.gov) 30/06/2001 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 #include "TGenericTable.h" 00014 00015 ////////////////////////////////////////////////////////////////////////// 00016 // // 00017 // TGenericTable // 00018 // // 00019 // This is the class to represent the array of C-struct // 00020 // defined at run-time // 00021 // // 00022 // Example: see $ROOTSYS/tutorials/tree/staff.C // 00023 // ------- // 00024 // !{ 00025 // !// example of macro to read data from an ascii file and 00026 // !// create a root file with an histogram and an ntuple. 00027 // !// A'la the famous ROOT/PAW staff data example 00028 // !// ( see PAW - Long write up, CERN, page33. ) 00029 // ! 00030 // ! gROOT->Reset(); 00031 // ! gSystem->Load("libRootKernel"); 00032 // ! 00033 // ! struct staff_t { 00034 // ! Int_t cat; 00035 // ! Int_t division; 00036 // ! Int_t flag; 00037 // ! Int_t age; 00038 // ! Int_t service; 00039 // ! Int_t children; 00040 // ! Int_t grade; 00041 // ! Int_t step; 00042 // ! Int_t nation; 00043 // ! Int_t hrweek; 00044 // ! Int_t cost; 00045 // ! }; 00046 // ! 00047 // ! staff_t staff; 00048 // ! 00049 // ! // open ASCII data file 00050 // ! FILE *fp = fopen("staff.dat","r"); 00051 // ! 00052 // ! char line[81]; 00053 // ! 00054 // ! // Create the generic table for 1000 rows (it may grow then) 00055 // ! TGenericTable *allStaff = new TGenericTable("staff_t","Staff-data",1000); 00056 // ! 00057 // ! // Fill the memory resident table 00058 // ! while (fgets(&line,80,fp)) { 00059 // ! sscanf(&line[0] ,"%d%d%d%d", &staff.cat,&staff.division,&staff.flag,&staff.age); 00060 // ! sscanf(&line[13],"%d%d%d%d", &staff.service,&staff.children,&staff.grade,&staff.step); 00061 // ! sscanf(&line[24],"%d%d%d", &staff.nation,&staff.hrweek,&staff.cost); 00062 // ! allStaff->AddAt(&staff); 00063 // ! } 00064 // ! fclose(fp); 00065 // ! // Delete unused space; 00066 // ! allStaff->Purge(); 00067 // ! 00068 // ! allStaff->Print(0,10); 00069 // ! 00070 // !// Create ROOT file 00071 // ! TFile *f = new TFile("aptuple.root","RECREATE"); 00072 // ! allStaff->Write(); 00073 // ! f->Write(); 00074 // ! 00075 // ! // We should close TFile otherwise all histograms we create below 00076 // ! // may be written to the file too occasionaly 00077 // ! f->Close(); 00078 // ! 00079 // !// Create ROOT Browser 00080 // ! new TBrowser("staff",allStaff); 00081 // ! 00082 // !// Create couple of the histograms 00083 // ! TCanvas *canva = new TCanvas("Staff","CERN Population",600,600); 00084 // ! canva->Divide(1,2); 00085 // ! 00086 // ! 00087 // !// one can use 2 meta variable: 00088 // !// n$ - the total number of the rows in the table 00089 // !// i$ - stands for the current row index i = [0 -> (n$-1)] 00090 // ! 00091 // ! gStyle->SetHistFillColor(10); 00092 // ! gStyle->SetHistFillStyle(3013); 00093 // ! canva->cd(1); 00094 // ! allStaff->Draw("age"); 00095 // ! canva->Update(); 00096 // ! canva->cd(2); 00097 // ! allStaff->Draw("cost"); 00098 // ! canva->Update(); 00099 // !} 00100 // 00101 ////////////////////////////////////////////////////////////////////////// 00102 00103 ClassImp(TGenericTable) 00104 TableClassStreamerImp(TGenericTable) 00105 00106 // Create TGenericTable by TTableDescriptor pointer 00107 //______________________________________________________________________________ 00108 TGenericTable::TGenericTable(const TTableDescriptor &dsc, const char *name) : TTable(name,dsc.Sizeof()),fColDescriptors(0) 00109 { 00110 /////////////////////////////////////////////////////////// 00111 // 00112 // Create TGenericTable by TTableDescriptor pointer: 00113 // 00114 // dsc - Pointer to the table descriptor 00115 // name - The name of this object 00116 // 00117 /////////////////////////////////////////////////////////// 00118 00119 // Create a private copy of the descriptor provided; 00120 SetDescriptorPointer(new TTableDescriptor(dsc)); 00121 SetGenericType(); 00122 } 00123 //______________________________________________________________________________ 00124 TGenericTable::TGenericTable(const TTableDescriptor &dsc, Int_t n) : TTable("TGenericTable",n,dsc.Sizeof()),fColDescriptors(0) 00125 { 00126 /////////////////////////////////////////////////////////// 00127 // 00128 // Create TGenericTable by TTableDescriptor pointer: 00129 // 00130 // dsc - Pointer to the table descriptor 00131 // name - "TGenericTable" 00132 // n - The initial number of allocated rows 00133 // 00134 /////////////////////////////////////////////////////////// 00135 00136 // Create a provate copy of the descriptor provided; 00137 SetDescriptorPointer(new TTableDescriptor(dsc)); 00138 SetGenericType(); 00139 } 00140 00141 //______________________________________________________________________________ 00142 TGenericTable::TGenericTable(const TTableDescriptor &dsc,const char *name,Int_t n) : TTable(name,n,dsc.Sizeof()),fColDescriptors(0) 00143 { 00144 /////////////////////////////////////////////////////////// 00145 // 00146 // Create TGenericTable by TTableDescriptor pointer: 00147 // 00148 // dsc - Pointer to the table descriptor 00149 // name - The name of this object 00150 // n - The initial number of allocated rows 00151 // 00152 /////////////////////////////////////////////////////////// 00153 00154 // Create a provate copy of the descriptor provided; 00155 SetDescriptorPointer(new TTableDescriptor(dsc)); 00156 SetGenericType(); 00157 } 00158 00159 // Create TGenericTable by C structure name provided 00160 //______________________________________________________________________________ 00161 TGenericTable::TGenericTable(const char *structName, const char *name) : TTable(name,-1),fColDescriptors(0) 00162 { 00163 /////////////////////////////////////////////////////////// 00164 // 00165 // Create TGenericTable by C structure name provided: 00166 // 00167 // dsc - Pointer to the table descriptor 00168 // name - The name of this object 00169 // n - The initial number of allocated rows 00170 // 00171 /////////////////////////////////////////////////////////// 00172 TTableDescriptor *dsc = TTableDescriptor::MakeDescriptor(structName); 00173 if (dsc) { 00174 SetDescriptorPointer(dsc); 00175 fSize = dsc->Sizeof(); 00176 } 00177 if ( !dsc || !fSize) Warning("TGenericTable","Wrong table format"); 00178 SetGenericType(); 00179 } 00180 //______________________________________________________________________________ 00181 TGenericTable::TGenericTable(const char *structName, Int_t n) : TTable("TGenericTable",-1),fColDescriptors(0) 00182 { 00183 /////////////////////////////////////////////////////////// 00184 // 00185 // Create TGenericTable by C structure name provided: 00186 // 00187 // dsc - Pointer to the table descriptor 00188 // name - The name of this object 00189 // n - The initial number of allocated rows 00190 // 00191 /////////////////////////////////////////////////////////// 00192 00193 TTableDescriptor *dsc = TTableDescriptor::MakeDescriptor(structName); 00194 if (dsc) { 00195 SetDescriptorPointer(dsc); 00196 fSize = dsc->Sizeof(); 00197 } 00198 if ( !dsc || !fSize) Warning("TGenericTable","Wrong table format"); 00199 if (n > 0) Set(n); 00200 SetGenericType(); 00201 } 00202 //______________________________________________________________________________ 00203 TGenericTable::TGenericTable(const char *structName, const char *name,Int_t n) : TTable(name,-1),fColDescriptors(0) 00204 { 00205 /////////////////////////////////////////////////////////// 00206 // 00207 // Create TGenericTable by C structure name provided: 00208 // 00209 // dsc - Pointer to the table descriptor 00210 // name - The name of this object 00211 // n - The initial number of allocated rows 00212 // 00213 /////////////////////////////////////////////////////////// 00214 00215 TTableDescriptor *dsc = TTableDescriptor::MakeDescriptor(structName); 00216 if (dsc) { 00217 SetDescriptorPointer(dsc); 00218 fSize = dsc->Sizeof(); 00219 } 00220 if ( !dsc || !fSize) Warning("TGenericTable","Wrong table format dsc=0x%lx, size=%ld",(Long_t)dsc,fSize); 00221 if (n > 0) Set(n); 00222 SetGenericType(); 00223 } 00224 00225 //______________________________________________________________________________ 00226 TGenericTable::~TGenericTable() 00227 { 00228 //destructor 00229 delete fColDescriptors; 00230 }