# Too many open files

**URL:** <https://root-forum.cern.ch/t/too-many-open-files/18001>\
**Category:** ROOT\
**Created:** [July 31, 2014, 9:50pm UTC](https://root-forum.cern.ch/t/too-many-open-files/18001 "2014-07-31T21:50:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelm](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@michaelm](https://root-forum.cern.ch/u/michaelm)\
**Post date:** [July 31, 2014, 9:50pm UTC](https://root-forum.cern.ch/t/too-many-open-files/18001/1 "2014-07-31T21:50:46Z")

</div>

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;

> [@Too many open files](https://root-forum.cern.ch/t/too-many-open-files/9237/1):
>
> Dear Rooters, I am writing a macro where I wish to read histograms from 1000 files and add them together. I open each file at the time in a loop using: TFile \*myFile = new TFile(); myFile-\>Open(fileName,“read”); From the file I copy two histograms (I don’t think this is relevant but) using the ROOT feature that whenever a file is opened you automatically enter this file: h\_Ptmp = new TH1F(\*h\_primaryElectronFluenceSpectrum); h\_Stmp = new TH1F(\*h\_scatterElectronFluenceSpectrum); Finally I c…

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.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [August 1, 2014, 8:39am UTC](https://root-forum.cern.ch/t/too-many-open-files/18001/2 "2014-08-01T08:39:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelm](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@michaelm](https://root-forum.cern.ch/u/michaelm)\
**Post date:** [August 2, 2014, 2:25pm UTC](https://root-forum.cern.ch/t/too-many-open-files/18001/3 "2014-08-02T14:25:34Z")

</div>

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 }

---

<div class="post-metadata">

**Author:** ![michaelm](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@michaelm](https://root-forum.cern.ch/u/michaelm)\
**Post date:** [August 2, 2014, 2:29pm UTC](https://root-forum.cern.ch/t/too-many-open-files/18001/4 "2014-08-02T14:29:20Z")

</div>

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();  
}

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [August 2, 2014, 3:26pm UTC](https://root-forum.cern.ch/t/too-many-open-files/18001/5 "2014-08-02T15:26:43Z")

</div>

Hi,

this is not a reproducer, sorry.

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![michaelm](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@michaelm](https://root-forum.cern.ch/u/michaelm)\
**Post date:** [August 3, 2014, 10:00am UTC](https://root-forum.cern.ch/t/too-many-open-files/18001/6 "2014-08-03T10:00:17Z")

</div>

ok let me phrase it more shortly?;

how do I loop over the following lines without the error of too many open files? ![#-o](https://root-forum.cern.ch/uploads/default/original/1X/378b417f7f25e635458e14f511257ba15acc7a37.gif "d'oh!") ![#-o](https://root-forum.cern.ch/uploads/default/original/1X/378b417f7f25e635458e14f511257ba15acc7a37.gif "d'oh!")

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;}

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [August 5, 2014, 7:57am UTC](https://root-forum.cern.ch/t/too-many-open-files/18001/7 "2014-08-05T07:57:12Z")

</div>

Hi,

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

```auto
#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.
