Too many open files

Hi,

I am trying to copy a tree with a selection. I have searched the RooTalk for this subject but I didn’t see exactly my question…

I do open a file and search my event, if I find it I write the tree in another output file. Here is the piece …

string selection = “RunNumber==”+run_number+"&&EventNumber=="+event_number;
std::string theFile="";
for (int iFile = 0; iFile < (int)fileList.size(); ++iFile)
{
TFile fn = new TFile(fileList[iFile].c_str(), “READ”);
TTree tree = (TTree)(fn->Get(“physics”));
Long64_t result = tree->GetEntries(selection.c_str());
if ( result > 0 )
{
theFile=fileList[iFile];
//tree->SetBranchStatus("
",1); // I don’t think I need this.
stringstream skiminput;
skiminput << “/scratch/tcuhadar/skimD3PD-” <<nfile<<".root";
TFile *skimfile = new TFile(skiminput.str().c_str(),“RECREATE”);
TTree *newtree = tree->CopyTree(selection.c_str());
newtree->SetName(“physics”);
newtree->Write();
skimfile->Close();
fn->Close();
delete skimfile;
delete fn;
break; // because I can see the event in more than one file as soon as I find it I quit from the loop…
}
}

This piece of code in a loop of run_number which has 38 runs. But after 32nd run_number I get error

SysError in TFile::TFile: file /atlas2/paganis/DATA2012/user.Tulay.analysisD200D00204976.physics_Egamma.120621222717/user.Tulay.003487._00009.filteredFile.root can not be opened for reading (Too many open files)

I open one file at a time and close it … What am I missing in this logic ?

Cheers,
Tulay

Try to change the last two lines:
}
}
into:
}
fn->Close();
delete fn;
}

Replacing the close & delete lines solves the problem.
Thanks,
Tulay