Logo ROOT  
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
11TFile *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}
int Int_t
Definition: RtypesCore.h:43
char Char_t
Definition: RtypesCore.h:31
unsigned int UInt_t
Definition: RtypesCore.h:44
#define gROOT
Definition: TROOT.h:406
char * Form(const char *fmt,...)
@ kFileExists
Definition: TSystem.h:43
@ kWritePermission
Definition: TSystem.h:45
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3942
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
TString & Append(const char *cs)
Definition: TString.h:559
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:1291
A TTree represents a columnar dataset.
Definition: TTree.h:78
TLine * line
void Step(const gsl_rng *r, void *xp, double step_size)
Definition: tree.py:1