Problem with closing files

Hello,

I have a problem with closing the files. Probably there is some trivial error…

I have an array with 71 files. I define “infile” outside the loop. Although I use command “Close” the files are still open which was checked with gROOT->GetListOfFiles()->Last()->GetName() and gROOT->GetListOfFiles()->At(0)->GetName()

The program crashes with the following message
SysError in TFile::TFile: file C14R2_FINAL_RootFiles/GoodPosition_ana_C14R2_vert_b2_zP3000_all.root can not be opened for reading (Too many open files)
when around 947 files are open.

Here is a part of my code:

TFile * infile = new TFile();

for (Int_t i=0; i<71; i++) {

infile->Open(FileName[i]);

cout << "LAST FILE = " << gROOT->GetListOfFiles()->Last()->GetName() << endl;
cout << "FIRST FILE = " << gROOT->GetListOfFiles()->At(0)->GetName() << endl;

cout<<"Filename = "<<FileName[i]<<endl;

// … here I take data from histograms stored in the files …

infile->Close();

}

I have also tried with infile->Close(“R”) and infile->Delete() but they don’t work…

Could you please advise me how to solve it?

Best regards,
Agnieszka Priebe

[code]TFile *infile = ((TFile *)0);

for (Int_t i = 0; i < 71; i++) {

if (!(FileName && FileName[i] && (*(FileName[i])))) continue; // just a precaution

if (infile) delete infile; // just a precaution
// http://root.cern.ch/root/html/TFile.html#TFile:Open
infile = TFile::Open(FileName[i]);
if (!infile) continue; // requested ROOT file does not exist or is unreadable

cout << "LAST FILE = " << gROOT->GetListOfFiles()->Last()->GetName() << endl;
cout << "FIRST FILE = " << gROOT->GetListOfFiles()->At(0)->GetName() << endl;

cout << "Filename = " << FileName[i] << endl;

// … here I take data from histograms stored in the files …

delete infile;
infile = ((TFile *)0);

}[/code]

Indeed it says that the “first” and the “last” files are the same but it crashes just after reading the second file …

" *** Break *** segmentation violation"

Possibly the file that you try to open does not exist or you have no permission to read it … try to protect your code … I modified my example source code, in my previous post, accordingly.

Another possible reason is covered by: [url]Copying object from tfiles

I used the command you added but it still crashes in the same moment.

The files are fine and they all exist. With a code which I posted here before, they were read 14 times each. But then, since they were not closed afterwards, my initial program was crashing when around 993 files were open (by the way I don’t know how is it possible that one file could be open 14 times at the same time … )

Do you have any other idea what I could do? Thank you for your help!

If you can’t get it working when you properly open and then close your ROOT files in your “for” loop,
try this brutal fix … [code]TFile *infile;

for (Int_t i = 0; i < 71; i++) {

if (!(FileName && FileName[i] && (*(FileName[i])))) continue; // just a precaution

// at first, try to find the “FileName[i]” among already opened ROOT files
infile = ((TFile *)(gROOT->GetListOfFiles()->FindObject(FileName[i])));
// and if NOT found, try to open the “FileName[i]” ROOT file right now
// http://root.cern.ch/root/html/TFile.html#TFile:Open
if (!infile) infile = TFile::Open(FileName[i]);
// finally, make sure that we really have a valid pointer to work with
if (!infile) continue; // requested ROOT file does not exist or is unreadable
infile->cd(); // change the current directory to the “FileName[i]” ROOT file

cout << "LAST FILE = " << gROOT->GetListOfFiles()->Last()->GetName() << endl;
cout << "FIRST FILE = " << gROOT->GetListOfFiles()->At(0)->GetName() << endl;

cout << "Filename = " << FileName[i] << endl;

// … here I take data from histograms stored in the files …

// note: the “FileName[i]” ROOT file IS deliberately LEFT OPENED!
gROOT->cd(); // change the current directory to the “gROOT”

}[/code]