# Read files from one Directory

**URL:** <https://root-forum.cern.ch/t/read-files-from-one-directory/35589>\
**Category:** Newbie\
**Created:** [August 10, 2019, 4:21am UTC](https://root-forum.cern.ch/t/read-files-from-one-directory/35589 "2019-08-10T04:21:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BAJA](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@BAJA](https://root-forum.cern.ch/u/BAJA)\
**Post date:** [August 10, 2019, 4:21am UTC](https://root-forum.cern.ch/t/read-files-from-one-directory/35589/1 "2019-08-10T04:21:54Z")

</div>

Rooters,

I am still beginners in root,

I posted my problem in the root forum before, and i get some help but i still dont get the target.

this code should be show some results in the terminal, I have to Run it for more than 1000 data files in the same directory. the files in the order as follows:  
file-000, file-001, file-003 … file-010, file-011, … file-100, file-101 … and so one.  
when in run the code for only one file i just write this line in the terminal

```auto
root [0].x Test.C(1)

```

but now can you show me how can i Run this code, and it have mistakes or not?

```auto
void Code(const char *dirname = "/home/Documents/");
{
double lifetime;
TString name;
void* dirp = gSystem->OpenDirectory(dirname);
      if (dirp) {
      while ((name = gSystem->GetDirEntry(dirp)) != "" ){
      if (name.EndsWith(".root")) {
            auto Spectra = new TH1F("Area","Area",8192,0,8192);
            Code(Spectra, name, lifetime);
            Spectra->Draw ();
         }
      }
   }
}
// ****************************************************************************
void Code(TH1F* h, string& filename, double lifetime)
{
double d, Channel, Count, Ch1, Count1=0;
string line;
int linenumber=0, i=0;

ifstream inputfile;
ofstream outputfile;

inputfile.open(filename);
outputfile.open("Results1.txt");

while (!inputfile.eof()){
      getline(inputfile,line);
      linenumber++;

       if (linenumber>=12){
           inputfile >>d;
           h->SetBinContent(linenumber-12, d);
           outputfile<<linenumber-12<<" "<<d<<endl;}

       if(linenumber>=8192+12)
         {break;}}

inputfile.close();
outputfile.close();

inputfile.open("Results1.txt");
while (inputfile >> Channel >> Count){
       if ((Channel>=400) && (Channel<=600)){
            if (Count1<Count){
            Count1=Count;
            Ch1=Channel;}
            else{}}}

cout<<"Possition 1 "<<Ch1<<endl;
cout<< "With "<<Count1<<endl;
inputfile.close();

remove("Results1.txt");
remove("Results2.txt");
}

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 10, 2019, 7:36am UTC](https://root-forum.cern.ch/t/read-files-from-one-directory/35589/2 "2019-08-10T07:36:10Z")

</div>

Try with:

```auto
#include "TSystem.h"
#include "TString.h"
#include "THStack.h"
#include "TH1.h"
#include <iostream>
#include <string>

void Code(TH1F *h, std::string &filename, double lifetime); // declare it here

void Code(const char *dirname = "/home/Documents/") {
  if (!(dirname && dirname[0])) dirname = "./"; // just a precaution
  void *dirp = gSystem->OpenDirectory(dirname);
  if (dirp) {
    THStack *hs = new THStack("hs", "All Area histograms");
    double lifetime = 0.;
    TString name;
    while ((name = gSystem->GetDirEntry(dirp)) != "" ) {
      if (name.BeginsWith("file-") && !name.EndsWith(".root")) {
	std::string n(name.Data()); // from TString to std::string
	std::cout << "Processing: " << n << std::endl;
	TH1F *h = new TH1F("Area-" + name, "Area from " + name, 8192, 0., 8192.);
	Code(h, n, lifetime);
	hs->Add(h);
      }
    }
    // hs->ls();
    hs->Draw("NOSTACK PLC PMC");
  }
}

```
