Logo ROOT  
Reference Guide
cernbuild.C File Reference

Detailed Description

View in nbviewer Open in SWAN Read data (CERN staff) from an ascii file and create a root file with a Tree.

TFile *cernbuild(Int_t getFile=0, Int_t print=1) {
Int_t Category;
UInt_t Flag;
Int_t Age;
Int_t Service;
Int_t Children;
Int_t Grade;
Int_t Hrweek;
Int_t Cost;
Char_t Division[4];
Char_t Nation[3];
//The input file cern.dat is a copy of the CERN staff data base
//from 1988
TString filename = "cernstaff.root";
TString dir = gROOT->GetTutorialDir();
dir.Append("/tree/");
dir.ReplaceAll("/./","/");
FILE *fp = fopen(Form("%scernstaff.dat",dir.Data()),"r");
TFile *hfile = 0;
if (getFile) {
// if the argument getFile =1 return the file "cernstaff.root"
// if the file does not exist, it is created
if (!gSystem->AccessPathName(dir+"cernstaff.root",kFileExists)) {
hfile = TFile::Open(dir+"cernstaff.root"); //in $ROOTSYS/tutorials/tree
if (hfile) return hfile;
}
//otherwise try $PWD/cernstaff.root
if (!gSystem->AccessPathName("cernstaff.root",kFileExists)) {
hfile = TFile::Open("cernstaff.root"); //in current dir
if (hfile) return hfile;
}
}
//no cernstaff.root file found. Must generate it !
//generate cernstaff.root in $ROOTSYS/tutorials/tree if we have write access
printf("you must run the script in a directory with write access\n");
return 0;
}
hfile = TFile::Open(filename,"RECREATE");
TTree *tree = new TTree("T","CERN 1988 staff data");
tree->Branch("Category",&Category,"Category/I");
tree->Branch("Flag",&Flag,"Flag/i");
tree->Branch("Age",&Age,"Age/I");
tree->Branch("Service",&Service,"Service/I");
tree->Branch("Children",&Children,"Children/I");
tree->Branch("Grade",&Grade,"Grade/I");
tree->Branch("Step",&Step,"Step/I");
tree->Branch("Hrweek",&Hrweek,"Hrweek/I");
tree->Branch("Cost",&Cost,"Cost/I");
tree->Branch("Division",Division,"Division/C");
tree->Branch("Nation",Nation,"Nation/C");
char line[80];
while (fgets(line,80,fp)) {
sscanf(&line[0],"%d %d %d %d %d %d %d %d %d %s %s",
&Category,&Flag,&Age,&Service,&Children,&Grade,&Step,&Hrweek,&Cost,Division,Nation);
tree->Fill();
}
if (print) tree->Print();
tree->Write();
fclose(fp);
delete hfile;
if (getFile) {
//we come here when the script is executed outside $ROOTSYS/tutorials/tree
hfile = TFile::Open(filename);
return hfile;
}
return 0;
}
Author
Rene Brun

Definition in file cernbuild.C.

TString::Data
const char * Data() const
Definition: TString.h:369
tree
Definition: tree.py:1
Form
char * Form(const char *fmt,...)
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
TFile::Open
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:3995
ROOT::Math::GSLSimAn::Step
void Step(const gsl_rng *r, void *xp, double step_size)
Definition: GSLSimAnnealing.cxx:117
Int_t
int Int_t
Definition: RtypesCore.h:45
TString
Basic string class.
Definition: TString.h:136
TSystem::AccessPathName
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:1294
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
kWritePermission
@ kWritePermission
Definition: TSystem.h:46
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
line
TLine * line
Definition: entrylistblock_figure1.C:235
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
kFileExists
@ kFileExists
Definition: TSystem.h:44
Char_t
char Char_t
Definition: RtypesCore.h:33
gROOT
#define gROOT
Definition: TROOT.h:406