Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree500_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/// \macro_output
8///
9/// \author Rene Brun
10
12{
15 Int_t Age;
19 Int_t Step;
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 from 1988
26 TString filename = "cernstaff.root";
27 TString dir = gROOT->GetTutorialDir();
28 dir.Append("/io/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/io/tree
38 if (hfile)
39 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)
45 return hfile;
46 }
47 }
48 //no cernstaff.root file found. Must generate it !
49 //generate cernstaff.root in $ROOTSYS/tutorials/io/tree if we have write access
51 printf("you must run the script in a directory with write access\n");
52 return 0;
53 }
54 hfile = TFile::Open(filename, "RECREATE");
55 auto TTree("T", "CERN 1988 staff data");
56 Category, "Category/I");
57 Flag, "Flag/i");
58 Age, "Age/I");
59 Service, "Service/I");
60 Children, "Children/I");
61 Grade, "Grade/I");
62 tree->Branch("Step", &Step, "Step/I");
63 Hrweek, "Hrweek/I");
64 Cost, "Cost/I");
65 tree->Branch("Division", Division, "Division/C");
66 Nation, "Nation/C");
67 char line[80];
68 while (fgets(line, 80, fp)) {
69 sscanf(&line[0],"%d %d %d %d %d %d %d %d %d %s %s",
70 &Category, &Flag, &Age, &Service, &Children, &Grade, &Step, &Hrweek, &Cost, Division, Nation);
71 tree->Fill();
72 }
73 if (print)
74 tree->Print();
75 tree->Write();
76
77 fclose(fp);
78 delete hfile;
79 if (getFile) {
80 //we come here when the script is executed outside $ROOTSYS/tutorials/io/tree
82 return hfile;
83 }
84 return 0;
85}
int Int_t
Definition RtypesCore.h:45
char Char_t
Definition RtypesCore.h:37
unsigned int UInt_t
Definition RtypesCore.h:46
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
#define gROOT
Definition TROOT.h:414
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
@ kFileExists
Definition TSystem.h:52
@ kWritePermission
Definition TSystem.h:54
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
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:4131
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
TString & Append(const char *cs)
Definition TString.h:572
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:1308
A TTree represents a columnar dataset.
Definition TTree.h:84
TLine * line
void Step(const gsl_rng *r, void *xp, double step_size)