# Looping over root file to create histograms on same plot

**URL:** <https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396>\
**Category:** Newbie\
**Created:** [March 13, 2020, 12:38pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396 "2020-03-13T12:38:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 13, 2020, 12:38pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/1 "2020-03-13T12:38:13Z")

</div>

So I have a root file: output.root, which has a subfolder ‘FirstCut’, containing 2 histograms, differing in a parameter which is 30 for the first histogram and 50 for the second. Now I want to loop over this parameter, and draw both histograms on the same plot. The path of these histograms is FirstCut\_30 and FirstCut\_50. This is my attempt:

ROOT Version: 6.18/04  
Linux

```auto
void Function(){
Tfile *f = new TFile ("output.root");
std::vector<double> FirstCut = {30, 50};
for (unsigned i = 0; i < FirstCut.size(); ++i){
  TH1D *h[i] = (TH1D*)f->Get("FirstCut_[i]");
  h[i]->Draw(same); 
  }
}

```

I get 2 errors:

- variable sized object may not be initialised (where it points at the h in h[i])
- use of undeclared identifier ‘same’ (in Draw(same))

---

<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:** [March 13, 2020, 12:51pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/2 "2020-03-13T12:51:30Z")

</div>

Many mistakes in your code…  
I do not have your ROOT file, so here is my guess:

```auto
void Function(){
   auto f = new TFile ("output.root");
   f->cd("FirstCut"); // you said your histograms are in the subfolder "FisrtCut"
   std::vector<int> FirstCut = {30, 50};
   int n = FirstCut.size();
   TH1D *h[n];
   for (unsigned i = 0; i < n; i++) {
     h[i] = (TH1D*)f->Get(Form("FirstCut_%d",FirstCut[i]));
     if (i==0) h[i]->Draw(); 
     else h[i]->Draw("same")
  }
}

```

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 13, 2020, 1:16pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/3 "2020-03-13T13:16:25Z")

</div>

Thanks! It worked! Do you know how I can give the different histograms different colours?

---

<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:** [March 13, 2020, 2:08pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/4 "2020-03-13T14:08:12Z")

</div>

Yes you can:

```auto
h->SetLineColor(...);

```

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 13, 2020, 3:42pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/5 "2020-03-13T15:42:10Z")

</div>

I am now trying to divide a histogram (let’s call it k1) by these histograms and plot these ‘ratios’ on the same graph. I tried this:

```auto
void Function(){
  auto f = new TFile ("output.root");
  TH1D *k1 = (TH1D*)f->Get("SecondCut");
  std::vector<int> FirstCut = {30, 50};
  int n = FirstCut.size();
  TH1D *h[n];
  TH1D *ratio[n]; 
  for (unsigned i = 0; i < n; i++) {
    h[i] = (TH1D*)f->Get(Form ("FirstCut_%d", FirstCut[i]));
    TH1D *ratio[i] = new TH1D(*k1);
    ratio[i]->Divide(h[i]);
    ratio[i]->SetNameTitle("hratio", "k1/h_%d"); 
    if (i==0) ratio[i]->Draw();
    else ratio[i]->Draw("same")
  }
}

```

But I get the error that variable-sized object may not be initialised: TH1D \*ratio[i] = new TH1D(\*k1);

Do you perhaps know what I am doing wrong?

---

<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:** [March 13, 2020, 3:46pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/6 "2020-03-13T15:46:58Z")

</div>

> [@evdv](#):
>
> TH1D \*ratio[i] = new TH1D(\*k1);

should be:

```auto
ratio[i] = new TH1D(*k1);

```

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 13, 2020, 3:49pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/7 "2020-03-13T15:49:34Z")

</div>

It works again! Thanks a lot!

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 16, 2020, 4:07pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/8 "2020-03-16T16:07:32Z")

</div>

I am back again… I was just wondering, I am dividing these histograms to obtain an efficiency and so I want the y values to be between 0 and 1. However, the errors exceed these limitations, and I think this might be the case because my k1 histogram is a subset of my h histograms. I read something about using TEfficiency class rather than the Divide function, but I am not sure on how to use that in this case.

---

<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:** [March 16, 2020, 4:19pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/9 "2020-03-16T16:19:25Z")

</div>

I think @moneta can help you with this.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 17, 2020, 10:06am UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/10 "2020-03-17T10:06:07Z")

</div>

See the TEfficiency documentation  
[https://root.cern.ch/doc/master/classTEfficiency.html](https://root.cern.ch/doc/master/classTEfficiency.html)

It is very detailed and there you have examples on how to use it .  
If you have specific questions, please let us know.

Lorenzo

---

<div class="post-metadata">

**Author:** ![Bikash](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/ee59a6/32.png) [@Bikash](https://root-forum.cern.ch/u/Bikash)\
**Post date:** [January 17, 2023, 6:11pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/11 "2023-01-17T18:11:03Z")

</div>

Hello. I have a root file within which there is a folder which contains different histogram. I want to loop those and plot different histogram for all of them. The folder name is Energy spectra and within that there are histograms named Energy ch 1;1 , Energy ch 2;1, Energy ch 3;1, and so on.

I wrote the following code and it did not work

#include   
#include \<TFile.h\>  
#include \<TH1D.h\>  
#include \<TCanvas.h\>  
#include

using namespace std;

void tut17() {  
//Open an existing ROOT File using the READ command. READ is used so as to not modify the data saved in the ROOT file.  
TFile\* fileOut = TFile::Open(“C:/root\_v5.34.36/macros/run00037\_000.root”, “READ”);  
//TFile \*fileOut = TFile::Open(“ROOT DIRECTORY + /DAQRoot.root”,“READ”);

```
if (!fileOut) {
	cout << "File not found." << endl;
	return 0;
}

TDirectory* folder = (TDirectory*)fileOut->Get("Energy spectra"); // get the folder from the file

for (int i = 1; i < 33; i++)
{
	//std::cout << file_name << std::endl;
	TH1F* histo[i] = (TH1F*)folder->Get(Form("Energy ch %d",i)); // get the histogram from the folder
	//double counts = histo->Integral(200, 1000);

	TCanvas* canvas = new TCanvas("canvas", "Histogram", 800, 600); // create a canvas for the plot

	histo[i]->Draw("same");// draw the histogram
	//cout << counts << endl;
	//cout << histo[i]->Integral(200, 1000) << endl;

	//canvas->SaveAs("histo.pdf"); // save the plot as a pdf
}

```

}

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [January 18, 2023, 12:25pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/12 "2023-01-18T12:25:58Z")

</div>

Hi @Bikash,

Could you please open a new topic regarding your issue – you posted (3 years later!) on a topic that was marked as resolved. Also, when you do so, could you be more specific about the errors that you are getting?

It seems that there are syntactic errors in the excerpt you posted, e.g. some of the `#include` lines or the declaration quoted below

> [@Bikash](#):
>
> ```auto
> TH1F* histo[i] = (TH1F*)folder->Get(Form("Energy ch %d",i)); // get the histogram from the folder
> 
> ```

Cheers,  
J.

---

<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:** [January 18, 2023, 2:27pm UTC](https://root-forum.cern.ch/t/looping-over-root-file-to-create-histograms-on-same-plot/38396/13 "2023-01-18T14:27:39Z")

</div>

The new post is here: [Plotting histogram by looping](https://root-forum.cern.ch/t/plotting-histogram-by-looping/53145)
