# Too many open files

**URL:** <https://root-forum.cern.ch/t/too-many-open-files/9237>\
**Category:** ROOT\
**Created:** [November 16, 2009, 11:23am UTC](https://root-forum.cern.ch/t/too-many-open-files/9237 "2009-11-16T11:23:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [November 16, 2009, 11:23am UTC](https://root-forum.cern.ch/t/too-many-open-files/9237/1 "2009-11-16T11:23:30Z")

</div>

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 close the file and delete the myFile object (at the end of the loop)

myFile-\>Close();  
delete myFile;

However, after some 938 files ROOT crashes telling me that there are too many open files. How can this be? I only open one file at the time and I also close it afterwards? I did some manual testing with cint and also there experienced that even if a TFile is close and the pointer deleted the command “.ls” will still print what is inside the file.

Help would be most appreciated, my Ph.D depends on this merge 😉

Cheers,  
Karin

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 16, 2009, 1:53pm UTC](https://root-forum.cern.ch/t/too-many-open-files/9237/2 "2009-11-16T13:53:20Z")

</div>

Hi,

you should simply call TFile::Open(…), not new TFile(). And even better yet would be to use hadd from $ROOTSYS/bin. You call it as “hadd out.root in1.root in2.root …”

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [November 16, 2009, 2:47pm UTC](https://root-forum.cern.ch/t/too-many-open-files/9237/3 "2009-11-16T14:47:01Z")

</div>

Hello!

I have also tried this, but unfortunately it doesn’t work either. I have slimmed my macro down to a minimum (see below). This crashed after 994 files ( I want 1000). I do not wish to use hadd since I am manipulating the histos.

/Karin

PS  
I use ROOT 5.18 on Ubuntu 9.10

```auto
void runSpectrumBAM(){
  int iM,iR;
  int nR = 500;
  char material[30];
  char folder[1000];
  char suffix[400];
  char fluence[1000];

  TH1F *h_Ptmp;
  TH1F *h_Stmp;

  TFile fluenceFile;
  
  gROOT->Reset();
  
  sprintf(folder,"/media/FREECOM\ HDD/DATA/Emax8MeV/WithoutPSF");
  
  //Loop over material
  for(iM=0; iM<2; iM++){
    //Loop over runs
    for(iR=0 ; iR<nR ; iR++){
      if(iM==0){
	sprintf(material,"Water");
	sprintf(suffix,"%u%03d",1,iR);
      }
      else{
	sprintf(material,"EFD_10cm");
	sprintf(suffix,"%u%03d",2,iR);
      }
      //print file-name
      sprintf(fluence,"%s/plots/spectra/histos/histos_Spectrum_%s_xCoord_0cm_yCoord_0cm_depth_10.05cm_%s.root",
	      folder,material,suffix);

      fluenceFile.Open(fluence,"read");
      cout<<"* Loading fluence file: "<<fluence<<endl;

      //Copy histograms of interest depending on input parameter (either electron or photon)
      h_Ptmp = new TH1F(*h_primaryElectronFluenceSpectrum);
      h_Stmp = new TH1F(*h_scatterElectronFluenceSpectrum);
      
      fluenceFile.Close();
      
      
    }//iR	        
  }//iM
}//end
```

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 16, 2009, 3:33pm UTC](https://root-forum.cern.ch/t/too-many-open-files/9237/4 "2009-11-16T15:33:38Z")

</div>

Hi Karin,

never ever call gROOT-\>Reset() inside a named macro!

TFile::Open() is a static function; you should not call it on an object (…-\>Open()) but simply call TFile::Open() and use the returned TFile\*, which you delete in the end, e.g.

`TFile* myFile = TFile::Open("myFile.root");
...
delete myFile;`  
No need to call TFile::Close() etc. So I’d use:

```auto
void runSpectrumBAM(){
  int iM,iR;
  int nR = 500;
  char material[30];
  char folder[1000];
  char suffix[400];
  char fluence[1000];

  TH1F *h_Ptmp;
  TH1F *h_Stmp;

  
  // NO! gROOT->Reset();
  
  sprintf(folder,"/media/FREECOM\ HDD/DATA/Emax8MeV/WithoutPSF");
  
  //Loop over material
  for(iM=0; iM<2; iM++){
    //Loop over runs
    for(iR=0 ; iR<nR ; iR++){
      if(iM==0){
	sprintf(material,"Water");
	sprintf(suffix,"%u%03d",1,iR);
      }
      else{
	sprintf(material,"EFD_10cm");
	sprintf(suffix,"%u%03d",2,iR);
      }
      //print file-name
      sprintf(fluence,"%s/plots/spectra/histos/histos_Spectrum_%s_xCoord_0cm_yCoord_0cm_depth_10.05cm_%s.root",
	      folder,material,suffix);

        TFile* fluenceFile = TFile::Open(fluence,"read");
      cout<<"* Loading fluence file: "<<fluence<<endl;

      //Copy histograms of interest depending on input parameter (either electron or photon)
      h_Ptmp = new TH1F(*h_primaryElectronFluenceSpectrum);
      h_Stmp = new TH1F(*h_scatterElectronFluenceSpectrum);
      
      delete fluenceFile;
      
      
    }//iR	        
  }//iM
}//end
```

Let me know how that works for you!

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [November 16, 2009, 4:04pm UTC](https://root-forum.cern.ch/t/too-many-open-files/9237/5 "2009-11-16T16:04:09Z")

</div>

Hello Axel!I

Thanks for your help, but unfortunately it still crashes. I used the code you gave me and it crashed at the second file with the message cited below. Removing the delete-statement makes the program run exactly the same way it did before, crashing after 994 files.

Other hints are most welcomed! 🙂

cheers,  
Karin

> [@](#):
>
> karin@ubuntu:~/MyAnalysis$ root -l runSpectrumBAM.C  
> root [0]  
> Processing runSpectrumBAM.C…
> 
> - Loading fluence file: /media/FREECOM HDD/DATA/Emax8MeV/WithoutPSF/plots/spectra/histos/histos\_Spectrum\_Water\_xCoord\_0cm\_yCoord\_0cm\_depth\_10.05cm\_1000.root
> - Loading fluence file: /media/FREECOM HDD/DATA/Emax8MeV/WithoutPSF/plots/spectra/histos/histos\_Spectrum\_Water\_xCoord\_0cm\_yCoord\_0cm\_depth\_10.05cm\_1001.root
> 
> \*\*\* Break \*\*\* segmentation violation  
> Attaching to program: /proc/5305/exe, process 5305  
> [Thread debugging using libthread\_db enabled]  
> 0x00435422 in \_\_kernel\_vsyscall ()  
> error detected on stdin  
> A debugging session is active.
> 
> ```
> Inferior 1 [process 5305] will be detached.
> 
> ```
> 
> Quit anyway? (y or n) [answered Y; input not from terminal]  
> Detaching from program: /proc/5305/exe, process 5305  
> Root \> Function runSpectrumBAM() busy flag cleared

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 16, 2009, 9:06pm UTC](https://root-forum.cern.ch/t/too-many-open-files/9237/6 "2009-11-16T21:06:17Z")

</div>

Hi,

Which version of ROOT are you using?  
Did you try to run with valgrind and/or gdb to pinpoint the origin of the segmentation?

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [November 16, 2009, 10:06pm UTC](https://root-forum.cern.ch/t/too-many-open-files/9237/7 "2009-11-16T22:06:44Z")

</div>

I do not understand what you are trying to do in your loops.  
In the code below I have added a few statements that may help you.

Rene

[code]void runSpectrumBAM(){  
int iM,iR;  
int nR = 500;  
char material[30];  
char folder[1000];  
char suffix[400];  
char fluence[1000];  
TH1::AddDirectory(kFALSE); //\<============

TH1F \*h\_Ptmp;  
TH1F \*h\_Stmp;

// NO! gROOT-\>Reset();

sprintf(folder,"/media/FREECOM\ HDD/DATA/Emax8MeV/WithoutPSF");

//Loop over material  
for(iM=0; iM\<2; iM++){  
//Loop over runs  
for(iR=0 ; iR\<nR ; iR++){  
if(iM==0){  
sprintf(material,“Water”);  
sprintf(suffix,"%u%03d",1,iR);  
}  
else{  
sprintf(material,“EFD\_10cm”);  
sprintf(suffix,"%u%03d",2,iR);  
}  
//print file-name  
sprintf(fluence,"%s/plots/spectra/histos/histos\_Spectrum\_%s\_xCoord\_0cm\_yCoord\_0cm\_depth\_10.05cm\_%s.root",  
folder,material,suffix);

```
    TFile* fluenceFile = TFile::Open(fluence,"read"); 
  cout<<"* Loading fluence file: "<<fluence<<endl; 

  //Copy histograms of interest depending on input parameter (either electron or photon) 
  // h_Ptmp = new TH1F(*h_primaryElectronFluenceSpectrum); 
  //h_Stmp = new TH1F(*h_scatterElectronFluenceSpectrum); 
   h_Ptmp = (TH1F*)fluenceFile->Get("h_primaryElectronFluenceSpectrum"); 
  h_Stmp = (TH1F*)fluenceFile->Get("h_scatterElectronFluenceSpectrum"); 
  
  delete fluenceFile; 
  
  
}//iR           

```

}//iM  
}//end [/code]

---

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [November 17, 2009, 10:01am UTC](https://root-forum.cern.ch/t/too-many-open-files/9237/8 "2009-11-17T10:01:05Z")

</div>

Thank you Axel and Rene!

The TFile::Open together withTH1::AddDirectory(kFALSE) fixed it and the whole loop now runs without problem.

/Karin
