Reading large text files with ROOT

Hello

My root program is not able to read a text file of 1 GB (about 145000 lines). Is there any way of reading such large files? My code is given below:

#include "TAxis.h"
#include "TH2.h"
#include "TStyle.h"
#include "TCanvas.h"
#include "TNtupleD.h"
#include <stdio.h>
#include <stdlib.h>
#include <vector.h>

analyse(){


  TFile *temp=new TFile("analyse.root", "RECREATE");

   
   ntuple = new TNtuple("ntuple","NTUPLE","p:px:py:pz:x:y:z");
    
  
      ifstream fromfile("pions05.txt");   
   
       double p,xp,yp,zp,x0,y0,z0;
       char rubish[20],usefull[20];;

       //     for (Int_t i=0; i<=100000000; i++){ 
       while (!fromfile.eof()) {
       fromfile>>rubish>>rubish>>p>>rubish>>rubish>>xp>>rubish>>rubish>>yp>>rubish>>rubish>>zp>>rubish>>rubish>>x0>>rubish>>rubish>>y0>>rubish>>rubish>>z0;
     
       ntuple->Fill(p,xp,yp,zp,x0,y0,z0); 
              }
     

		
   TCanvas *c1_2  = new TCanvas("c1_2","momentum background",200,200,500,500);
   ntuple->Draw("p");
  
   temp->Write(); 


}

Thank you

Adriana :frowning:

Hi Adriana,

Replace your program by the few following lines

void analyse(){ TFile *temp=new TFile("analyse.root", "RECREATE"); TNtuple *ntuple = new TNtuple("ntuple","NTUPLE","p:px:py:pz:x:y:z"); ntuple->ReadFile("pions05.txt"); ntuple->Write(); }

Hello

Thank you, the root file is produced using the lines in your replay. But the ntuple is not filled, as ntuple->Scan() shows no entry and ntuple->Draw(“p”) produces no hystogram.

The large file I am trying to read does not contain only numbers but also characters. A sample of the first two lines in this text file is given next:

P(MeV) is: 131.903 Px is: 70.2237 Py is: 104.252 Pz is: 40.0863 x0(mm) is: 0.00299218 y0(mm) is: 0.00359255 z0(mm) is: -2.21823
P(MeV) is: 100.646 Px is: 3.04723 Py is: -79.18 Pz is: 62.1233 x0(mm) is: 0.0195584 y0(mm) is: 0.00218446 z0(mm) is: 2.02693

Best regards
Adriana :unamused:

It is difficult to test without your data file. I suggest the following

Rene

[code]void analyze(){

TFile *temp=new TFile(“adriana.root”, “RECREATE”);
ntuple = new TNtuple(“ntuple”,“NTUPLE”,“p:px:py:pz:x:y:z”);
FILE *fp = fopen(“pions05.txt”,“r”);
float p,xp,yp,zp,x0,y0,z0;
char line[127];
while (fgets(&line,127,fp)) {
sscanf(&line[0],“P(MeV) is: %f Px is: %f Py is: %f Pz is: %f x0(mm) is: %f y0(mm) is: %f z0(mm) is: %f”,&p,&xp,&yp,&zp,&x0,&y0,&z0);
printf(“p=%f, xp=%f, yp=%f, zp=%f,x0=%f, y0=%f, z0=%f\n”,p,xp,yp,zp,x0,y0,z0);
ntuple->Fill(p,xp,yp,zp,x0,y0,z0);
}
TCanvas *c1_2 = new TCanvas(“c1_2”,“momentum background”,200,200,500,500);
ntuple->Draw(“p”);

temp->Write();
}[/code]

It worked! Thank you!

Regards
Adriana :stuck_out_tongue: