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