Logo ROOT   6.14/05
Reference Guide
cernbuild.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook -nodraw
4 /// Read data (CERN staff) from an ascii file and create a root file with a Tree.
5 /// See also a variant in staff.C
6 ///
7 /// \macro_code
8 ///
9 /// \author Rene Brun
10 
11 TFile *cernbuild(Int_t getFile=0, Int_t print=1) {
12 
13  Int_t Category;
14  UInt_t Flag;
15  Int_t Age;
16  Int_t Service;
17  Int_t Children;
18  Int_t Grade;
19  Int_t Step;
20  Int_t Hrweek;
21  Int_t Cost;
22  Char_t Division[4];
23  Char_t Nation[3];
24 
25  //The input file cern.dat is a copy of the CERN staff data base
26  //from 1988
27  TString filename = "cernstaff.root";
28  TString dir = gROOT->GetTutorialDir();
29  dir.Append("/tree/");
30  dir.ReplaceAll("/./","/");
31  FILE *fp = fopen(Form("%scernstaff.dat",dir.Data()),"r");
32 
33  TFile *hfile = 0;
34  if (getFile) {
35  // if the argument getFile =1 return the file "cernstaff.root"
36  // if the file does not exist, it is created
37  if (!gSystem->AccessPathName(dir+"cernstaff.root",kFileExists)) {
38  hfile = TFile::Open(dir+"cernstaff.root"); //in $ROOTSYS/tutorials/tree
39  if (hfile) return hfile;
40  }
41  //otherwise try $PWD/cernstaff.root
42  if (!gSystem->AccessPathName("cernstaff.root",kFileExists)) {
43  hfile = TFile::Open("cernstaff.root"); //in current dir
44  if (hfile) return hfile;
45  }
46  }
47  //no cernstaff.root file found. Must generate it !
48  //generate cernstaff.root in $ROOTSYS/tutorials/tree if we have write access
50  printf("you must run the script in a directory with write access\n");
51  return 0;
52  }
53  hfile = TFile::Open(filename,"RECREATE");
54  TTree *tree = new TTree("T","CERN 1988 staff data");
55  tree->Branch("Category",&Category,"Category/I");
56  tree->Branch("Flag",&Flag,"Flag/i");
57  tree->Branch("Age",&Age,"Age/I");
58  tree->Branch("Service",&Service,"Service/I");
59  tree->Branch("Children",&Children,"Children/I");
60  tree->Branch("Grade",&Grade,"Grade/I");
61  tree->Branch("Step",&Step,"Step/I");
62  tree->Branch("Hrweek",&Hrweek,"Hrweek/I");
63  tree->Branch("Cost",&Cost,"Cost/I");
64  tree->Branch("Division",Division,"Division/C");
65  tree->Branch("Nation",Nation,"Nation/C");
66  char line[80];
67  while (fgets(line,80,fp)) {
68  sscanf(&line[0],"%d %d %d %d %d %d %d %d %d %s %s",
69  &Category,&Flag,&Age,&Service,&Children,&Grade,&Step,&Hrweek,&Cost,Division,Nation);
70  tree->Fill();
71  }
72  if (print) tree->Print();
73  tree->Write();
74 
75  fclose(fp);
76  delete hfile;
77  if (getFile) {
78  //we come here when the script is executed outside $ROOTSYS/tutorials/tree
79  hfile = TFile::Open(filename);
80  return hfile;
81  }
82  return 0;
83 }
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1276
TLine * line
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6926
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
void Step(const gsl_rng *r, void *xp, double step_size)
#define gROOT
Definition: TROOT.h:410
Basic string class.
Definition: TString.h:131
int Int_t
Definition: RtypesCore.h:41
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3976
TString & Append(const char *cs)
Definition: TString.h:559
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:9298
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1711
char Char_t
Definition: RtypesCore.h:29
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:70
const char * Data() const
Definition: TString.h:364