# Looping through root files, plot histograms and record values to an output file

**URL:** <https://root-forum.cern.ch/t/looping-through-root-files-plot-histograms-and-record-values-to-an-output-file/44810>\
**Category:** Newbie\
**Created:** [May 11, 2021, 10:09am UTC](https://root-forum.cern.ch/t/looping-through-root-files-plot-histograms-and-record-values-to-an-output-file/44810 "2021-05-11T10:09:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rootNewbie](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rootnewbie/32/25428_2.png) [@rootNewbie](https://root-forum.cern.ch/u/rootNewbie)\
**Post date:** [May 11, 2021, 10:09am UTC](https://root-forum.cern.ch/t/looping-through-root-files-plot-histograms-and-record-values-to-an-output-file/44810/1 "2021-05-11T10:09:52Z")

</div>

Hello all,

I am trying to loop through a folder containing about 150 ntuple root files with a TTree histogram called EdepNTuple;1 stored in each of them. My goal is to plot the histogram with log(y) axis, save the canvas of each plot and perform some analysis detailed in my code below. The output csv files should contain values of x, y, z for each of those files.

I’ve spent quite some time on my own trying to solve this issue and will really appreciate some help. The files are too many to perform this manually

```auto
void efficiencyandplots(const char *dirname="$HOME/B1-b/", const char *ext=".root"){
 
 char* dir = gSystem->ExpandPathName(dirname);
 void* dirp = gSystem->OpenDirectory(dir);
  
 const char* entry;
 Int_t n = 0;
 TString str;

 while((entry = (char*)gSystem->GetDirEntry(dirp))) {
  str = entry;
  if(str.EndsWith(ext))
  	TFile *f = new TFile(str,"READ");
 	TTree *E = (TTree*)f->Get("EdepNtuple;1");
 	E->Draw("Edep>>hist");
 	double a = hist->Integral(1,1)/hist->Integral()
 	double b = hist->Integral(1,20)/hist->Integral()
 	double c = hist->Integral(1,30)/hist->Integral()
        
        double x = 1 - a
        double y = 1 - b
        double z = 1 - c
    
  }

}

```

---

<div class="post-metadata">

**Author:** ![rootNewbie](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rootnewbie/32/25428_2.png) [@rootNewbie](https://root-forum.cern.ch/u/rootNewbie)\
**Post date:** [May 11, 2021, 10:15am UTC](https://root-forum.cern.ch/t/looping-through-root-files-plot-histograms-and-record-values-to-an-output-file/44810/2 "2021-05-11T10:15:09Z")

</div>

@Wile_E_Coyote and @couet I will be really grateful if you can please take a look at this. Any guide you give will be very helpful.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 11, 2021, 10:35am UTC](https://root-forum.cern.ch/t/looping-through-root-files-plot-histograms-and-record-values-to-an-output-file/44810/3 "2021-05-11T10:35:28Z")

</div>

What does not work ?

---

<div class="post-metadata">

**Author:** ![rootNewbie](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rootnewbie/32/25428_2.png) [@rootNewbie](https://root-forum.cern.ch/u/rootNewbie)\
**Post date:** [May 11, 2021, 10:45am UTC](https://root-forum.cern.ch/t/looping-through-root-files-plot-histograms-and-record-values-to-an-output-file/44810/4 "2021-05-11T10:45:02Z")

</div>

Hello  
the loop does not work and I haven’t found a way to store neither the canvas from the plot for each root file or the outputs. I have tried doing it manually for each file and it works but the files are too many for this.

I have attached samples of the root files if it is of any help.

Thank you so much!

[EdepNTuples.zip](https://root-forum.cern.ch/uploads/short-url/AtqBM01o1Knq3uNC7eHylXLHLMY.zip) (303.5 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 11, 2021, 11:04am UTC](https://root-forum.cern.ch/t/looping-through-root-files-plot-histograms-and-record-values-to-an-output-file/44810/5 "2021-05-11T11:04:24Z")

</div>

You should create the canvas before the loop and then print it in the loop using TCnavas::Print()

```auto
auto c = new TCanvas();
....

loop {
 .....
 c->Print("image.png");
}

```
