Too many open files

Dear all,

Im writing a code that reads data from a file and stores it in a histogram.
I would like to repeat this for a large number of files and use different selection criteria.

Im closing and deleting the files with
filename->Close();
delete filename;

However after compiling I get the error; Too many open files.
I only get this error when the loop over opening the files is large.

I found a possible answer in;

However using
TFile::Open
TH1::AddDirectory(kFALSE)

didn’t work out or I don’t understand how to implement this.

The structure of the files is a TTree with the name ‘Physics’ with variables from which I can draw the data with a certain selection criteria (and weight).
The structure of the code is as follows;

Any help or comments would be very much appreciated! Thanks… Michael.

====> code is in the second message.

Hi,

could you post the code that you are actually executing?
The attached macro is incomplete and has portions which are not well designed. For example, the same file is always opened, the argument of selection is ignored, at least a curly brace is missing.

Danilo

Hi Danile tnx for your reply.
The code is around 8000 lines I will try to upload it as an attachment.

I removed some things so only the structure remained. A loop over opening and closing a file.
Still, while every file is closed and deleted, it gives the error of ‘too many open files’.

Ps. I added the missing }

double read(TString,TString);
double read(TString var,TString sel){

TFile file000 = TFile::Open("./filename000.root");
//open file
TH1F
hisst000 = new TH1F(“hisst000”,"bla,50,1,100);
//new TH1F
TString string1 = “>>hisst000”;
TString string2 = var+string1;
physics->Draw(string2,sel);

//the last sentence boils down to;
//physics->Draw(variable_x>>hist000,1*variable_x>1)
//so take from the file data of variable_x with selection criteria that variable_x>1 and put in histogram //hist000

    hist000->Draw()

//Draw the histogram

file000->Close();
delete file000;
//close and delete the file
}

//loop the function above for different selection criteria
void select(double);
void select(double ii){
for(int i=1;i<100;i++){
TString stringa = Form("%d", i);
TString stringb = “variable_x>”;
TString stringc = stringb+stringa;

TString selection = TString::Format(“Weight * ( %s)”, stringc.Data() );
TString variable = “variable_x”;

reads(variable,selection);
}}

void program(){
TH1::AddDirectory(kFALSE);
double i=1;
select(i);
gSystem->ProcessEvents();
}

Hi,

this is not a reproducer, sorry.

Cheers,
Danilo

ok let me phrase it more shortly?;

how do I loop over the following lines without the error of too many open files? #-o #-o

double loop(TString,TString);
double loop(TString var,TString sel){
TFile file000 = TFile::Open("./filename.root");
TH1F
hisst000 = new TH1F(“hisst000”,“name”,nbins,min,max);
physics->Draw(var>>hist000,sel);
hist000->Draw()
file000->Close();
delete file000;}

Hi,

I again took the freedom to make the code compilable.
If I run this code

#include <string>
#include <limits>
#include <iostream>
#include "TFile.h"
#include "TH1F.h"

//-----------------------------------------------------

void loop(unsigned int i)
{
   TFile *file000 = TFile::Open("./filename.root");
   std::string name="hist"+std::to_string(i);
   TH1F* hist000 = new TH1F(name.c_str(),name.c_str(),100,0,10);
   hist000->Draw();
   file000->Close();
   delete file000;
}

//-----------------------------------------------------

int main(){
   for (unsigned int i=0;i<std::numeric_limits<unsigned int>::max();++i){
      if (i%10000 == 0) std::cout << "i = " << i << std::endl;
      loop(i);
   }
}

I cannot unfortunately encounter the issue you describe.