{ "cells": [ { "cell_type": "markdown", "id": "32cc2ef4", "metadata": {}, "source": [ "# tree500_cernbuild\n", "Read data (CERN staff) from an ascii file and create a root file with a Tree.\n", "\n", "\n", "\n", "\n", "**Author:** Rene Brun \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:19 PM." ] }, { "cell_type": "markdown", "id": "4505ef8f", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 1, "id": "38315b90", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:19:56.787032Z", "iopub.status.busy": "2026-05-19T20:19:56.786901Z", "iopub.status.idle": "2026-05-19T20:19:57.112282Z", "shell.execute_reply": "2026-05-19T20:19:57.111638Z" } }, "outputs": [], "source": [ "Int_t getFile = 0;\n", "Int_t print = 1;" ] }, { "cell_type": "code", "execution_count": 2, "id": "77aec303", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:19:57.113656Z", "iopub.status.busy": "2026-05-19T20:19:57.113518Z", "iopub.status.idle": "2026-05-19T20:19:57.319067Z", "shell.execute_reply": "2026-05-19T20:19:57.318409Z" } }, "outputs": [], "source": [ "Int_t Category;\n", "UInt_t Flag;\n", "Int_t Age;\n", "Int_t Service;\n", "Int_t Children;\n", "Int_t Grade;\n", "Int_t Step;\n", "Int_t Hrweek;\n", "Int_t Cost;\n", "Char_t Division[4];\n", "Char_t Nation[3];" ] }, { "cell_type": "markdown", "id": "9f2033b5", "metadata": {}, "source": [ "The input file cern.dat is a copy of the CERN staff data base from 1988" ] }, { "cell_type": "code", "execution_count": 3, "id": "d4db3457", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:19:57.320530Z", "iopub.status.busy": "2026-05-19T20:19:57.320414Z", "iopub.status.idle": "2026-05-19T20:19:57.522590Z", "shell.execute_reply": "2026-05-19T20:19:57.522022Z" } }, "outputs": [], "source": [ "TString filename = \"cernstaff.root\";\n", "TString dir = gROOT->GetTutorialDir();\n", "dir.Append(\"/io/tree/\");\n", "dir.ReplaceAll(\"/./\",\"/\");\n", "FILE *fp = fopen(Form(\"%scernstaff.dat\", dir.Data()), \"r\");\n", "\n", "TFile *hfile = 0;\n", "if (getFile) {\n", " // if the argument getFile =1 return the file \"cernstaff.root\"\n", " // if the file does not exist, it is created\n", " if (!gSystem->AccessPathName(dir + \"cernstaff.root\", kFileExists)) {\n", " hfile = TFile::Open(dir + \"cernstaff.root\"); //in $ROOTSYS/tutorials/io/tree\n", " if (hfile)\n", " return hfile;\n", " }\n", " // otherwise try $PWD/cernstaff.root\n", " if (!gSystem->AccessPathName(\"cernstaff.root\", kFileExists)) {\n", " hfile = TFile::Open(\"cernstaff.root\"); //in current dir\n", " if (hfile)\n", " return hfile;\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "c290e3eb", "metadata": {}, "source": [ "no cernstaff.root file found. Must generate it !\n", "generate cernstaff.root in $ROOTSYS/tutorials/io/tree if we have write access" ] }, { "cell_type": "code", "execution_count": 4, "id": "4ca9c785", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:19:57.524132Z", "iopub.status.busy": "2026-05-19T20:19:57.524014Z", "iopub.status.idle": "2026-05-19T20:19:57.732106Z", "shell.execute_reply": "2026-05-19T20:19:57.731540Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "******************************************************************************\n", "*Tree :T : CERN 1988 staff data *\n", "*Entries : 3354 : Total = 176339 bytes File Size = 19066 *\n", "* : : Tree compression factor = 2.16 *\n", "******************************************************************************\n", "*Br 0 :Category : Category/I *\n", "*Entries : 3354 : Total Size= 14073 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 1 :Flag : Flag/i *\n", "*Entries : 3354 : Total Size= 14049 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 2 :Age : Age/I *\n", "*Entries : 3354 : Total Size= 14043 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 3 :Service : Service/I *\n", "*Entries : 3354 : Total Size= 14067 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 4 :Children : Children/I *\n", "*Entries : 3354 : Total Size= 14073 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 5 :Grade : Grade/I *\n", "*Entries : 3354 : Total Size= 14055 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 6 :Step : Step/I *\n", "*Entries : 3354 : Total Size= 14049 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 7 :Hrweek : Hrweek/I *\n", "*Entries : 3354 : Total Size= 14061 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 8 :Cost : Cost/I *\n", "*Entries : 3354 : Total Size= 14049 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 9 :Division : Division/C *\n", "*Entries : 3354 : Total Size= 25326 bytes File Size = 9748 *\n", "*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.13 *\n", "*............................................................................*\n", "*Br 10 :Nation : Nation/C *\n", "*Entries : 3354 : Total Size= 24209 bytes File Size = 9318 *\n", "*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.19 *\n", "*............................................................................*\n" ] } ], "source": [ "if (gSystem->AccessPathName(\".\", kWritePermission)) {\n", " printf(\"you must run the script in a directory with write access\\n\");\n", " return 0;\n", "}\n", "hfile = TFile::Open(filename, \"RECREATE\");\n", "auto tree = new TTree(\"T\", \"CERN 1988 staff data\");\n", "tree->Branch(\"Category\", &Category, \"Category/I\");\n", "tree->Branch(\"Flag\", &Flag, \"Flag/i\");\n", "tree->Branch(\"Age\", &Age, \"Age/I\");\n", "tree->Branch(\"Service\", &Service, \"Service/I\");\n", "tree->Branch(\"Children\", &Children, \"Children/I\");\n", "tree->Branch(\"Grade\", &Grade, \"Grade/I\");\n", "tree->Branch(\"Step\", &Step, \"Step/I\");\n", "tree->Branch(\"Hrweek\", &Hrweek, \"Hrweek/I\");\n", "tree->Branch(\"Cost\", &Cost, \"Cost/I\");\n", "tree->Branch(\"Division\", Division, \"Division/C\");\n", "tree->Branch(\"Nation\", Nation, \"Nation/C\");\n", "char line[80];\n", "while (fgets(line, 80, fp)) {\n", " sscanf(&line[0],\"%d %d %d %d %d %d %d %d %d %s %s\",\n", " &Category, &Flag, &Age, &Service, &Children, &Grade, &Step, &Hrweek, &Cost, Division, Nation);\n", " tree->Fill();\n", "}\n", "if (print)\n", " tree->Print();\n", "tree->Write();\n", "\n", "fclose(fp);\n", "delete hfile;\n", "if (getFile) {\n", " //we come here when the script is executed outside $ROOTSYS/tutorials/io/tree\n", " hfile = TFile::Open(filename);\n", " return hfile;\n", "}\n", "return 0;" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }