Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree500_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.

{
Int_t Step;
Char_t Division[4];
// 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("/io/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/io/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/io/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");
auto 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/io/tree
return hfile;
}
return 0;
}
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:406
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:4130
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:1296
A TTree represents a columnar dataset.
Definition TTree.h:79
TLine * line
******************************************************************************
*Tree :T : CERN 1988 staff data *
*Entries : 3354 : Total = 176339 bytes File Size = 15005 *
* : : Tree compression factor = 2.74 *
******************************************************************************
*Br 0 :Category : Category/I *
*Entries : 3354 : Total Size= 14073 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 1 :Flag : Flag/i *
*Entries : 3354 : Total Size= 14049 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 2 :Age : Age/I *
*Entries : 3354 : Total Size= 14043 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 3 :Service : Service/I *
*Entries : 3354 : Total Size= 14067 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 4 :Children : Children/I *
*Entries : 3354 : Total Size= 14073 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 5 :Grade : Grade/I *
*Entries : 3354 : Total Size= 14055 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 6 :Step : Step/I *
*Entries : 3354 : Total Size= 14049 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 7 :Hrweek : Hrweek/I *
*Entries : 3354 : Total Size= 14061 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 8 :Cost : Cost/I *
*Entries : 3354 : Total Size= 14049 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 9 :Division : Division/C *
*Entries : 3354 : Total Size= 25326 bytes File Size = 8325 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.49 *
*............................................................................*
*Br 10 :Nation : Nation/C *
*Entries : 3354 : Total Size= 24209 bytes File Size = 6680 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 3.05 *
*............................................................................*
(TFile *) nullptr
Author
Rene Brun

Definition in file tree500_cernbuild.C.