Looping over files in directory

HELP!!!

I’m trying to loop over a bunch of files in a directory and plots them in histograms but for some reason ROOT stops after one iteration. I’m using the following code:

const char* inDir = "/data/Data\ Anaylsis/"
const char* ext = “.csv”

char* dir = gSystem->ExpandPathName(inDir);
void* dirp = gSystem->OpenDirectory(dir);

const char* entry;
const char* filename;
TString str;

while(entry->gSystem->GetDirEntry(dirp)){
str = entry;
for(n=0; n<49; n++){
if(str.EndsWith(ext)){
filename[n] = gSystem->ConcatFileName(dir,entry);
FILE *inp[n] = fopen(filename[n],“r”);

}
}
}

Why doesn’t it work?? Actually this version just causes a segmentation break while the task is executed only once without the for-loop…

Regards,
Kati Nikolics
kati.nikolics@oeaw.ac.at

Hi Kati,
you forgot declare filename and inp as array.const char *filename[49]; FILE *inp[49];
Jan

thanks for your help! at first declaring the filename and the FILE as arrays seemed to work but then nothing happened - i mean ROOT seemed to be computing but there were no data produced so after a few hours i decided to kill the program. then when i tried to run it again there was a segmentation violation and the whole terminal crashed!

what’s wrong??? any ideas?

Kati

#include "TSystem.h"

void loop(const char* ext = ".C")
{
  const char* inDir = "$ROOTSYS/tutorials";

  char* dir = gSystem->ExpandPathName(inDir);
  void* dirp = gSystem->OpenDirectory(dir);

  const char* entry;
  const char* filename[100];
  Int_t n = 0;
  TString str;

  while((entry = (char*)gSystem->GetDirEntry(dirp))) {
    str = entry;
    if(str.EndsWith(ext))
      filename[n++] = gSystem->ConcatFileName(dir, entry);
  }

  for (Int_t i = 0; i < n; i++)
    Printf("file -> %s", filename[i]);
}

[ul]
root [0] .L loop.C
root [1] loop()
file -> /cern/root/tutorials/rootenv.C
file -> /cern/root/tutorials/rootalias.C
file -> /cern/root/tutorials/tasks.C
file -> /cern/root/tutorials/demos.C
file -> /cern/root/tutorials/regexp.C
file -> /cern/root/tutorials/htmlex.C
file -> /cern/root/tutorials/benchmarks.C
file -> /cern/root/tutorials/rootlogon.C
file -> /cern/root/tutorials/hsimple.C
file -> /cern/root/tutorials/rootlogoff.C
file -> /cern/root/tutorials/geant3tasks.C
file -> /cern/root/tutorials/demoshelp.C
file -> /cern/root/tutorials/rootmarks.C
root [2] loop(".cxx")
file -> /cern/root/tutorials/MyTasks.cxx
root [3]
[/ul]
all working OK …
CVS version of ROOT

Jan

do not forget gSystem->FreeDirectory(dirp); :wink:

Ok, I kind of got it work… at least looping seems to be ok.

However I’m having another problem now: I wanna create a bunch of histograms but that’s apparently too much for ROOT, it crashes every time (some kind of memory issue?).
I’m trying to do the following:

TH1F *adc = new TH1F(“adc”,nbins,xmin,xmax);
FILE *inp = fopen("/data/Data\ Analysis/9023156.csv",“r”);

for(int k = 0; k> nbins; k++) {
fscanf(inp, “%f; %f”, &x,&y )
adc->Fill(x,y);
}

fclose(inp);

char imgname[50];
sprintf(imgname,"/data/Data\ Analysis/%s.png",entry);
adc->SaveAs(imgname);

However it doesn’t work. I mean there is a png-file created but I can’t open it and the loop stops again after one iteration!

Thanks for your help…

Kati

Hum! you seem to have several problems with your code!
-Are you sure of your statement

for(int k = 0; k> nbins; k++) {
-What do you expect from the statement:

adc->SaveAs(imgname);
Rene

Well I’d like to save the histograms as pictures without the windows popping up in between though…

I assume that you want to see the result of drawing the histogram, so
you should create a canvas and draw the histogram in the canvas, tehn print the canvas as a png file, eg

TCancas c1; adc->Draw(); //may be with some options c1.Print("c1.png");
Rene