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
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