Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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///
6/// \macro_code
7///
8/// \author Rene Brun
9
10TFile *cernbuild(Int_t getFile=0, Int_t print=1) {
11
12 Int_t Category;
13 UInt_t Flag;
14 Int_t Age;
15 Int_t Service;
16 Int_t Children;
17 Int_t Grade;
18 Int_t Step;
19 Int_t Hrweek;
20 Int_t Cost;
21 Char_t Division[4];
22 Char_t Nation[3];
23
24 //The input file cern.dat is a copy of the CERN staff data base
25 //from 1988
26 TString filename = "cernstaff.root";
27 TString dir = gROOT->GetTutorialDir();
28 dir.Append("/tree/");
29 dir.ReplaceAll("/./","/");
30 FILE *fp = fopen(Form("%scernstaff.dat",dir.Data()),"r");
31
32 TFile *hfile = 0;
33 if (getFile) {
34 // if the argument getFile =1 return the file "cernstaff.root"
35 // if the file does not exist, it is created
36 if (!gSystem->AccessPathName(dir+"cernstaff.root",kFileExists)) {
37 hfile = TFile::Open(dir+"cernstaff.root"); //in $ROOTSYS/tutorials/tree
38 if (hfile) return hfile;
39 }
40 //otherwise try $PWD/cernstaff.root
41 if (!gSystem->AccessPathName("cernstaff.root",kFileExists)) {
42 hfile = TFile::Open("cernstaff.root"); //in current dir
43 if (hfile) return hfile;
44 }
45 }
46 //no cernstaff.root file found. Must generate it !
47 //generate cernstaff.root in $ROOTSYS/tutorials/tree if we have write access
49 printf("you must run the script in a directory with write access\n");
50 return 0;
51 }
52 hfile = TFile::Open(filename,"RECREATE");
53 TTree *tree = new TTree("T","CERN 1988 staff data");
54 tree->Branch("Category",&Category,"Category/I");
55 tree->Branch("Flag",&Flag,"Flag/i");
56 tree->Branch("Age",&Age,"Age/I");
57 tree->Branch("Service",&Service,"Service/I");
58 tree->Branch("Children",&Children,"Children/I");
59 tree->Branch("Grade",&Grade,"Grade/I");
60 tree->Branch("Step",&Step,"Step/I");
61 tree->Branch("Hrweek",&Hrweek,"Hrweek/I");
62 tree->Branch("Cost",&Cost,"Cost/I");
63 tree->Branch("Division",Division,"Division/C");
64 tree->Branch("Nation",Nation,"Nation/C");
65 char line[80];
66 while (fgets(line,80,fp)) {
67 sscanf(&line[0],"%d %d %d %d %d %d %d %d %d %s %s",
68 &Category,&Flag,&Age,&Service,&Children,&Grade,&Step,&Hrweek,&Cost,Division,Nation);
69 tree->Fill();
70 }
71 if (print) tree->Print();
72 tree->Write();
73
74 fclose(fp);
75 delete hfile;
76 if (getFile) {
77 //we come here when the script is executed outside $ROOTSYS/tutorials/tree
78 hfile = TFile::Open(filename);
79 return hfile;
80 }
81 return 0;
82}
int Int_t
Definition RtypesCore.h:45
char Char_t
Definition RtypesCore.h:37
unsigned int UInt_t
Definition RtypesCore.h:46
#define gROOT
Definition TROOT.h:404
char * Form(const char *fmt,...)
@ kFileExists
Definition TSystem.h:44
@ kWritePermission
Definition TSystem.h:46
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
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:4025
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:692
TString & Append(const char *cs)
Definition TString.h:564
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:1296
A TTree represents a columnar dataset.
Definition TTree.h:79
TLine * line
void Step(const gsl_rng *r, void *xp, double step_size)
Definition tree.py:1