A problem with Tree read ascii

Hi,

By referring to tutorials:tree:basic.C
https://root.cern/doc/master/basic_8C.html

I would like to read data from an ascii file and create a root file with tree.
But the result is the entries is 0.
I don’t know what wrong with my code.

Here is my code:

 
#include "Riostream.h"
void basic() {

   TString dir = gROOT->GetTutorialDir();
   dir.Append("/tree/");
   dir.ReplaceAll("/./","/");
   ifstream in;
   in.open(Form("%su_1e5.txt",dir.Data()));
 
   Float_t x;
   Int_t nlines = 0;
   auto f = TFile::Open("basic.root","RECREATE");
   TH1F h1("h1","x distribution",100,0,4);
   TNtuple ntuple("ntuple","data from ascii file","x");
 
   while (1) {
      in >> x ;
      if (!in.good()) break;
      if (nlines < 5) printf("x=%8f",x);
      h1.Fill(x);
      ntuple.Fill(x);
      nlines++;
   }
   printf(" found %d points\n",nlines);
 
   in.close();
 
   f->Write();
}

ROOT Version: 6.20/02
Platform: Not Provided
Compiler: Not Provided


u_1e5.txt (1.7 MB)

Hi,
It is easier if you do:

TNtuple ntuple("ntuple","data from ascii file","x");
ntuple.ReadFile("u_1e5.txt","x")

Lorenzo

It works well.
Thank you for your reply.

Best wishes,
…CHEN

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.