ROOT logo

From $ROOTSYS/tutorials/tree/basic.C

#include "Riostream.h"
void 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
      

// read file $ROOTSYS/tutorials/tree/basic.dat
// this file has 3 columns of float data
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("basic.C","");
   dir.ReplaceAll("/./","/");
   ifstream in;
   in.open(Form("%sbasic.dat",dir.Data()));

   Float_t x,y,z;
   Int_t nlines = 0;
   TFile *f = new TFile("basic.root","RECREATE");
   TH1F *h1 = new TH1F("h1","x distribution",100,-4,4);
   TNtuple *ntuple = new TNtuple("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();
}
 basic.C:1
 basic.C:2
 basic.C:3
 basic.C:4
 basic.C:5
 basic.C:6
 basic.C:7
 basic.C:8
 basic.C:9
 basic.C:10
 basic.C:11
 basic.C:12
 basic.C:13
 basic.C:14
 basic.C:15
 basic.C:16
 basic.C:17
 basic.C:18
 basic.C:19
 basic.C:20
 basic.C:21
 basic.C:22
 basic.C:23
 basic.C:24
 basic.C:25
 basic.C:26
 basic.C:27
 basic.C:28
 basic.C:29
 basic.C:30
 basic.C:31
 basic.C:32
 basic.C:33
 basic.C:34
 basic.C:35
 basic.C:36