Long winded load from ASCII file

I am reading rows of floats and ints from a text file, and filling histograms as I go. For some reason this takes ages … I can almost read the lines as fast as ROOT is reading them :laughing:

What am I doing wrong? Here is the code. The text file is about 126,000 lines long.

{

   gROOT->Reset();

  TBrowser b;

#include "Riostream.h"

   char str[512];
   ifstream in;

   in.open("Lander_multiple.data");

   Double_t x,y,z;
   Double_t xfree,yfree,zfree;
   Int_t type;
   Double_t radius,dummy;
   Int_t nlines = 0;
   Int_t i;


   // create array of histos
   TObjArray hlist(0);


   TH2F *h1 = new TH2F("h1","Distribution of particles on Mars surface (20km x 20km)",100,-10000,10000,100,-10000,10000);
   TH1F *h2 = new TH1F("h2","Generated particle sizes (microns)",100,0,100);
   TH1F *h3 = new TH1F("h3","Particle sizes remaining on Lander (microns)",100,0,100);
   TH1F *h4 = new TH1F("h4","Particle sizes on Mars surface (microns)",100,0,100);
   TH1F *h5 = new TH1F("h5","Detachment fraction (surface/generated)",100,0,100);
   TH2F *h6 = new TH2F("h6","Particle size (microns) vs distance from Lander (m)",100,0,20000,100,0,100);

   hlist.Add(h1);
   hlist.Add(h2);
   hlist.Add(h3);
   hlist.Add(h4);
   hlist.Add(h5);
   hlist.Add(h6);
   //hlist.Add(h1);


   TNtuple *ntuple = new TNtuple("ntuple","Simulation data","type:radius:x:y:z");
   Int_t nparticles = 0;
   Int_t n = 0;

   Double_t xmax=0;
   Double_t ymax=0;

   while (1) {
	   if(nlines < 6) {
		   in.getline(str,512);
		   printf("%s\n",str);
	   } else if(nlines == 6) {
		   in >> nparticles;
		   printf("%d particles\n",nparticles);
	   } else {
	      in >> type >> x >> y >> z >> radius >> xfree >> yfree >> zfree;
 		  if (!in.good()) break;
		  n++;
		  
		  x *= 0.01;
		  y *= 0.01;
		  radius *= 1.e6;
		  if(abs(x)>xmax) xmax = abs(x);
		  if(abs(y)>ymax) ymax = abs(y);
          h1->Fill(x,y);
		  h2->Fill(radius);
		  if(type == 1) h3->Fill(radius);
		  if(type == 3) h4->Fill(radius);
		  if(type == 3) h5->Fill(radius);
		  h6->Fill(sqrt(x*x + y*y),radius);
          ntuple->Fill(type,radius,x,y,z);
		  if(n >= nparticles) break;
		   }
      nlines++;
   }
	h5->Divide((TF1*)h2);

   in.close();
   printf(" found %d \n",nlines);
   printf(" xmax, ymax %8f %8f\n",xmax,ymax);

   TFile *fout = new TFile("analysis.root","recreate");

   ntuple->Write();
     hlist.Write();
     fout.Close();



}

(This code is largely based on something I found in the How To)

Here’s an example line from the input file:

1	86.90132252314862	458.73803387157665	72.34328508550698	1.4975942412068489E-6	0.0	0.0	0.0

Thanks!
Julian

Julian,

I suggest to make your code more efficient (as shown below).
On my machine using a data file of 126000 lines it executes in
6.6s with CINT (.x bunn.C)
3.7s with ACLIC (.x bunn.C+)

With CINT, the interpreter goes automatically in non optimized mode as soon as you have 3 nested ifs or more.

Rene

[code]#include “TNtuple.h”
#include “TFile.h”
#include “TH2.h”
#include "Riostream.h"
void bunn() {

//gROOT->Reset();

//TBrowser b;

char str[512];
ifstream in;

in.open(“Lander_multiple.data”);

Double_t x,y,z;
Double_t xfree,yfree,zfree;
Int_t type;
Double_t radius; //,dummy;
Int_t nlines = 0;
Int_t i;

// create array of histos
TObjArray hlist(6);

TH2F *h1 = new TH2F(“h1”,“Distribution of particles on Mars surface (20km x 20km)”,100,-10000,10000,100,-10000,10000);
TH1F *h2 = new TH1F(“h2”,“Generated particle sizes (microns)”,100,0,100);
TH1F *h3 = new TH1F(“h3”,“Particle sizes remaining on Lander (microns)”,100,0,100);
TH1F *h4 = new TH1F(“h4”,“Particle sizes on Mars surface (microns)”,100,0,100);
TH1F *h5 = new TH1F(“h5”,“Detachment fraction (surface/generated)”,100,0,100);
TH2F *h6 = new TH2F(“h6”,“Particle size (microns) vs distance from Lander (m)”,100,0,20000,100,0,100);

hlist.Add(h1);
hlist.Add(h2);
hlist.Add(h3);
hlist.Add(h4);
hlist.Add(h5);
hlist.Add(h6);

TFile *fout = new TFile(“analysis.root”,“recreate”);
TNtuple *ntuple = new TNtuple(“ntuple”,“Simulation data”,“type:radius:x:y:z”);
Int_t nparticles = 0;
Int_t n = 0;

Double_t xmax=0;
Double_t ymax=0;

for (i=0;i<6;i++) {
in.getline(str,512);
printf("%s\n",str);
}
in >> nparticles;
printf("%d particles\n",nparticles);
while (1) {
in >> type >> x >> y >> z >> radius >> xfree >> yfree >> zfree;
if (!in.good()) break;
n++;

  x *= 0.01;
  y *= 0.01;
  radius *= 1.e6;
  if(abs(x)>xmax) xmax = abs(x);
  if(abs(y)>ymax) ymax = abs(y);
  h1->Fill(x,y);
  h2->Fill(radius);
  if(type == 1) h3->Fill(radius);
  if(type == 3) h4->Fill(radius);
  if(type == 3) h5->Fill(radius);
  h6->Fill(sqrt(x*x + y*y),radius);
  ntuple->Fill(type,radius,x,y,z);
  if(n >= nparticles) break;
  nlines++;

}
//h5->Divide((TF1*)h2);

in.close();
printf(" found %d \n",nlines);
printf(" xmax, ymax %8f %8f\n",xmax,ymax);

ntuple->Write();
hlist.Write();
fout->Close();
}
[/code]

Rene: many thanks indeed … I will try your modified version and let you know how it improves the speed.

Best wishes from sunny Pasadena :wink:

Julian