StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
staff.C
1 {
2 // example of macro to read data from an ascii file and
3 // create a root file with an histogram and an ntuple.
4 // A'la the famous ROOT/PAW staff data example
5 // ( see PAW - Long write up, CERN, page33. )
6 
7  gROOT->Reset();
8  gSystem->Load("libTable");
9 
10  struct staff_t {
11  Int_t cat; // catetory
12  Int_t division; // CERN division
13  Int_t flag;
14  Int_t age; // age of the person
15  Int_t service;
16  Int_t children; // The number of the children
17  Int_t grade; // the grade
18  Int_t step;
19  Int_t nation; // citizenship
20  Int_t hrweek; // number of working hours per week
21  Int_t cost; // salary
22  };
23 
24  staff_t staff;
25 
26  // open ASCII data file
27  TString pathName = "$ROOTSYS/tutorials/cernstaff.dat";
28  gSystem->ExpandPathName(pathName);
29  if (gSystem->AccessPathName(pathName.Data()))
30  {
31  printf(" Can not find file %s\n",pathName.Data());
32  return;
33  }
34  FILE *fp = fopen(pathName.Data(),"r");
35 
36  char line[81];
37 
38  // Create the generic table for 1000 rows (it may grow then)
39  TGenericTable *allStaff = new TGenericTable("staff_t","Staff-data",1000);
40 
41  // Fill the memory resident table
42  while (fgets(&line,80,fp)) {
43  sscanf(&line[0] ,"%d%d%d%d", &staff.cat,&staff.division,&staff.flag,&staff.age);
44  sscanf(&line[13],"%d%d%d%d", &staff.service,&staff.children,&staff.grade,&staff.step);
45  sscanf(&line[24],"%d%d%d", &staff.nation,&staff.hrweek,&staff.cost);
46  allStaff->AddAt(&staff);
47  }
48  fclose(fp);
49  // Delete unused space;
50  allStaff->Purge();
51 
52  allStaff->Print(0,10);
53 
54 // Create ROOT file
55  TFile *f = new TFile("aptuple.root","RECREATE");
56  allStaff->Write();
57  f->Write();
58 
59  // We should close TFile otherwise all histograms we create below
60  // may be written to the file too occasionaly
61  f->Close();
62 
63 // Create ROOT Browser
64  new TBrowser("staff",allStaff);
65 
66 // Create couple of the histograms
67  TCanvas *canva = new TCanvas("Staff","CERN Population",600,600);
68  canva->Divide(1,2);
69 
70 
71 // one can use 2 meta variable:
72 // n$ - the total number of the rows in the table
73 // i$ - stands for the current row index i = [0 -> (n$-1)]
74 
75  gStyle->SetHistFillColor(10);
76  gStyle->SetHistFillStyle(3013);
77  canva->cd(1);
78  allStaff->Draw("age");
79  canva->Update();
80  canva->cd(2);
81  allStaff->Draw("cost");
82  canva->Update();
83 }
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Definition: TDataSet.cxx:893
virtual Int_t AddAt(const void *c)
Definition: TTable.cxx:1122
Definition: staff.C:10
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 Int_t Purge(Option_t *opt="")
Shrink the table to free the unused but still allocated rows.
Definition: TTable.cxx:1801