# Reading Multiple root files and plotting one histogram

**URL:** <https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599>\
**Category:** Newbie\
**Created:** [August 31, 2021, 8:10pm UTC](https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599 "2021-08-31T20:10:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rin\_Toshaka](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rin_toshaka/32/24444_2.png) [@Rin\_Toshaka](https://root-forum.cern.ch/u/Rin_Toshaka)\
**Post date:** [August 31, 2021, 8:10pm UTC](https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599/1 "2021-08-31T20:10:50Z")

</div>

HOW to read multiple root files and overlay a particular histogram from all these files into one histogram. I m confused pls help…

---

<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:** [September 1, 2021, 6:16am UTC](https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599/2 "2021-09-01T06:16:29Z")

</div>

You can get the histograms you need from the various files and group them into a THStack in order to plot them with axis’ scales automatically computed.  
Something like:

```auto
auto f1 = new TFile("file1.root");
TH1F *h1 = (TH1F*)f1->Get("hist1");

```

… etc… for the other files. Then:

```auto
auto hs = new THStack();
hs->Add(h1);
hs->Add(h2);

```

etc … . Then:

```auto
hs->Draw();

```

---

<div class="post-metadata">

**Author:** ![Rin\_Toshaka](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rin_toshaka/32/24444_2.png) [@Rin\_Toshaka](https://root-forum.cern.ch/u/Rin_Toshaka)\
**Post date:** [September 1, 2021, 10:15am UTC](https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599/3 "2021-09-01T10:15:13Z")

</div>

can i do the same for TEfficiency plots?  
and I m using c++

void overlayhist(){

```
    string fileName = "first.root";

    TFile *f1 = TFile::Open(fileName.c_str());

    string histName1 = "ABC/ABC/eTimingEfficiency";

    TEfficiency* h1 = static_cast<TEfficiency*>(f1->Get(histName1.c_str()));
    TCanvas* c1 = new TCanvas("c1","c1",1000,600);
    c1->cd(1);
    h1->SetTitle("Timing Efficiency");

    h1->Draw();
    gStyle->SetOptStat(0);
    TLegend *leg = new TLegend(0.6,0.7,0.9,0.9);
    leg->AddEntry(h1,"Number: 32");
    leg->Draw();

```

}

I m using this function to and it successfully plots one file how to add more histograms from other root files as well overlaying it

---

<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:** [September 1, 2021, 11:29am UTC](https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599/4 "2021-09-01T11:29:05Z")

</div>

THStack works with histograms. may be you can get the histogram from TEfficiency ?  
I guess @moneta can tell.

---

<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:** [September 1, 2021, 12:39pm UTC](https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599/5 "2021-09-01T12:39:46Z")

</div>

Hi,  
TEfficiency will drow TGraphAsymErrors objects. You might use for this instead of THStack TMultiGraph.

Lorenzo

---

<div class="post-metadata">

**Author:** ![Rin\_Toshaka](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rin_toshaka/32/24444_2.png) [@Rin\_Toshaka](https://root-forum.cern.ch/u/Rin_Toshaka)\
**Post date:** [September 1, 2021, 1:26pm UTC](https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599/6 "2021-09-01T13:26:55Z")

</div>

> [@moneta](#):
>
> TGraphAsymErrors

can you help me with some dummy code that I have posted above ? To bring more clarity

---

<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:** [September 1, 2021, 1:29pm UTC](https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599/7 "2021-09-01T13:29:14Z")

</div>

I guess you can retrieve the graphs using [GetPaintedGraph](https://root.cern/doc/master/classTEfficiency.html#ad6ceab62cbe2505c726c07b049ace605).

---

<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:** [September 1, 2021, 3:22pm UTC](https://root-forum.cern.ch/t/reading-multiple-root-files-and-plotting-one-histogram/46599/8 "2021-09-01T15:22:11Z")

</div>

Something like this should work

```auto
void overlayEfficiencies(){

   TMultiGraph * mg = new TMultiGraph(); 

// loop input files 
{
    string fileName = "first.root";
  
    TFile *f1 = TFile::Open(fileName.c_str());

    string histName1 = "ABC/ABC/eTimingEfficiency";

    TEfficiency* h1 = static_cast<TEfficiency*>(f1->Get(histName1.c_str()));
    TCanvas* c1 = new TCanvas("c1","c1",1000,600);
    c1->cd(1);
    h1->SetTitle("Timing Efficiency");

    h1->Draw();
    auto g1 = h1->GetPaintedGraph(); 
    mg->Add(g1);
} 

// end loop 

// draw all the graphs 
mg->Draw();  

```

}
