Creating a tree from an ASCII file

I have copied from an old mail (2003) the code for writing a tree from an ASCII file. It is reported at the end of this message. With version 4.03/02
the code have some problem: x0 values are always a nonsense, X1 contains data from the first column, and so on… data of the last column are never read. Even if I try to use only n-1 columns, some commands like (tree->Draw(“x1:x2”,“x3==0”,“box”) don’t work.

Is the macro too old? Should I change something? Data are correctly read,
since if I try to fill an histogram instead than a tree, no problem appears.

TTree* read (const TString& filename, Int_t ncol)
TString vars;

Int_t nlin=0;

for (Int_t i = 0; i < ncol; i++) {
  vars += Form("x%d", i); 
  if (i==0) vars += "/D"; 
  if (i + 1 != ncol) vars += ":";

TArrayD x(ncol);
TTree* tree = new TTree("tree", "Tree"); 
tree->Branch("data", &x.fArray, vars.Data());

ifstream in(filename.Data());
if (!in || in.bad()) return 1;

while (!in.eof()) {
  for (Int_t i = 0; i < ncol; i++) in >> x[i];


cout<<nlin<<" lines"<<endl;
return tree;


Can you send a running example showing the problem ? (small macro and small data file)

here all the files I was using
readASCII.C (665 Bytes)
create_tree.C (219 Bytes)
Analisi.txt (48 KB)

Instead of

tree->Branch("data", &x.fArray, vars.Data()); use