Basic


Read data from an ascii file and create a root file with an histogram and an ntuple. See a variant of this macro in basic2.C.

Author: Rene Brun
This notebook tutorial was automatically generated with ROOTBOOK-izer (Beta) from the macro found in the ROOT repository on Tuesday, January 17, 2017 at 02:42 PM.


In [1]:
%%cpp -d
#include "Riostream.h"



Read file $rootsys/tutorials/tree/basic.dat this file has 3 columns of float data


In [2]:
TString dir = gROOT->GetTutorialsDir();
dir.Append("/tree/");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sbasic.dat",dir.Data()));

Float_t x,y,z;
Int_t nlines = 0;
auto f = TFile::Open("basic.root","RECREATE");
TH1F h1("h1","x distribution",100,-4,4);
TNtuple ntuple("ntuple","data from ascii file","x:y:z");

while (1) {
   in >> x >> y >> z;
   if (!in.good()) break;
   if (nlines < 5) printf("x=%8f, y=%8f, z=%8f\n",x,y,z);
   h1.Fill(x);
   ntuple.Fill(x,y,z);
   nlines++;
}
printf(" found %d points\n",nlines);

in.close();

f->Write();


x=-1.102279, y=-1.799389, z=4.452822
x=1.867178, y=-0.596622, z=3.842313
x=-0.524181, y=1.868521, z=3.766139
x=-0.380611, y=0.969128, z=1.084074
x=0.552454, y=-0.212309, z=0.350281
 found 1000 points