SysError in <TFile::TFile>: file can not be opened for reading (Too many open files)

I am executing the following code which opens some root files (many ~500) and then creates from them one histogram per one root file.

Then these histograms are stored in another root file, which later on is read for further processing.
If I run this code for a small number of initial root files (i.e. ~50) I have no problem running the code.
When I push it to process much more files I get the following error

[quote][code]SysError in TFile::TFile: file movies_205837_1.root can not be opened for reading (Too many open files)
CAUTION: tmpfile /tmp/Q9Kv4O_cint can’t open
Error: could not create file /tmp/Q9Kv4O_cint

*** Break *** segmentation violation
[/code][/quote]

Any idea on how to prevent this from happening? I think it has to do with memory manipulation, but I am not aware on how to fix it of confirm it!

Thanks in advance!

[code]#include “/afs/cern.ch/user/a/astamato/public/lib_thanos/c_header.h”
#include “/afs/cern.ch/user/a/astamato/public/lib_thanos/root_header.h”

void pulse_shape(int run, int bunch, int movie, int det){

// (0) global variable definitions
std::vector<TH1F*> hists;//Raw signals - input to stac

double x, x_start, x_end, baseline;// x-values of the signal
float y;// y-values of the signal

int entries, minimum, minimumBin;

bool reject;

double scale;

// (1) Get the signals from the TTree and store them individually in a root file as histograms
for (int bun=0; bun<=bunch; bun++){
for (int mov=0; mov<=movie; mov++){
// (1a) Read the file
TFile *fin = new TFile(TString::Format(“FIMG-%d_%d-0-s1_%d-%d.root”, det, run, bunch, mov));
if(!fin) continue;
if ( !fin->IsOpen() || fin->IsZombie() ) continue;

// (1b) Get TTree
TTree data = (TTree)fin->Get(“Data”);
if(!data) continue;
entries = data->GetEntries();
data->SetBranchStatus("*",0);
data->SetBranchStatus(“signal_x”,1); data->SetBranchAddress(“signal_x”,&x);
data->SetBranchStatus(“signal_y”,1); data->SetBranchAddress(“signal_y”,&y);

// (1c) Create the histogram to store the movie
TH1F *h = new TH1F(“h”, TString::Format(“h_%d_%d”, bun, mov), 2500, 0, 2500);
for (int i=0; i<entries; i++){
data->GetEntry(i);
h->Fill(i,y);
}

// (1g) Save the histograms in a root file
TFile fout(TString::Format(“movies_%d_%d.root”, run, det), “UPDATE”);
//cout << "Saving File " << fout.GetName() << endl;
if (h) {h->SetName(TString::Format(“h_%d_%d”, bun, mov)); h->Write(); fout.Close();}

}// end of loops over movies
}// end of loop over bunches
cout<< “1” << endl;
// (2) Process the recorded signals
for (int bun=0; bun<=bunch; bun++){
for (int mov=0; mov<=movie; mov++){

// (2a) Open the root file containg the signals
TFile *fsignals = new TFile(TString::Format(“movies_%d_%d.root”,run, det));
if(!fsignals) continue;
if ( !fsignals->IsOpen() || fsignals->IsZombie() ) continue;
//cout << “File " << fsignals->GetName() << " opened successfully” << endl;

// (2b) Get the Signal histogram
TH1F hIndv = (TH1F)fsignals->FindObjectAny(TString::Format(“h_%d_%d;1”, bun, mov));
if(!hIndv)
{
//cout << “hist " << TString::Format(“h%d;1”,mov) << " does not exist in this file.” << endl;
//fsignals->ls();
continue;
}
//if (hIndv) cout << “hIndv: " << hIndv->GetName() << " correctly opened.” << endl;

// (2c) Find the constant baseline and move the signal to 0
minimum = hIndv->GetMinimum(0);
hIndv->GetBinWithContent(minimum,minimumBin,0);
TF1 *baselineFit = new TF1(“baselineFit”,exclude,0,2500,4);
baselineFit->SetParameters(0,0);
baselineFit->FixParameter(2, minimumBin);
baselineFit->FixParameter(3, 300);
hIndv->Fit(“baselineFit”,“WNCQ”);
baseline = baselineFit->GetParameter(0);
for (int bin = 0; binGetNbinsX(); bin++){
hIndv->SetBinContent(bin+1, hIndv->GetBinContent(bin+1)-baseline);
}

// (2d) Normalize the signal to its maximum value
minimum = hIndv->GetMinimum(-1000);
hIndv->Scale(-1./minimum);

// (2e) Align the signal in time - maximum point
//for (int bin=1; bin<=2500-minimumBin; bin++){//Start shifting the pulse to the left
// hIndv->SetBinContent(bin, hIndv->GetBinContent(bin+minimumBin));
// for (bin=2500-minimumBin; bin<=2500; bin++){
// hIndv->SetBinContent(bin,0);//End shifting
// }
//}
for (int bin=1; bin<=hIndv->GetNbinsX(); bin++){
hIndv->SetBinContent(bin, hIndv->GetBinContent(minimumBin+bin-1000));
}
//hIndv->Draw(“histosame”);

  hists.push_back(hIndv);

}// end of loop over movies
}// end of loop over bunches

cout << "Hist size is : " << hists.size() << endl;

TH2F *hStack = Stack(hists);
TProfile * hProf = hStack->ProfileX("hProfname");
hStack->Draw("COLZ");
hProf->Draw("PLsame");

// Save the file
//TFile fStack(TString::Format(“PSA.root”, run, det), “RECREATE”);
// cout << "Saving File " << fStack.GetName() << endl;
// hStack->Write();
// hProf->SetLineColor(kRed); hProf->SetMarkerColor(kRed); hProf->Write();
//fStack.Close();

}[/code]

This was solved by removing the Open File outside of the for loop.

[code]// (2) Process the recorded signals

TFile *fsignals = new TFile(TString::Format(“movies_%d_%d.root”,run, det));
//if(!fsignals) continue;
if ( !fsignals->IsOpen() || fsignals->IsZombie() ) return;

for (int bun=0; bun<=bunch; bun++){
for (int mov=0; mov<=movie; mov++){

// (2a) Open the root file containg the signals

  //cout << "File " << fsignals->GetName() << " opened successfully" << endl;

// (2b) Get the Signal histogram
TH1F hIndv = (TH1F)fsignals->FindObjectAny(TString::Format(“h_%d_%d;1”, bun, mov));
if(!hIndv)continue;[/code]