# Opening a histogram from a file

**URL:** <https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101>\
**Category:** ROOT\
**Created:** [August 23, 2014, 5:08pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101 "2014-08-23T17:08:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serelesis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Serelesis](https://root-forum.cern.ch/u/Serelesis)\
**Post date:** [August 23, 2014, 5:08pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/1 "2014-08-23T17:08:36Z")

</div>

…a cryptically difficult task, apparently.

So I’ve managed to find quite a few threads that more or less cover the issue I’m having… but nothing I have tried seems to be working at all.

The short of it is, I have two files, each with one histogram inside. Not a tree, just a histogram. I’d like to open them one at a time, and make a copy of each of these histograms in a way that I can perform simple functions such as GetXmin() and GetBinContent(bin). Later of course, there are more complicated things to be done with the content, but it’s the first part that doesn’t seem to be working for me.

Let’s start with something a bit more simple, and I’ll work my way towards something I actually want to do, though:

```auto
TFile *file1 = new TFile("first_file.root"); // open the file, easy.
c1->Draw(); // the histogram I want is definitely there.
TH1D *hist1 = (TH1D*)file1->Get("c1"); // get the histogram, no complaints here.
hist1->Draw(); // I can draw the new histogram, great! Let's start doing useful things with it.
hist1->GetXaxis()->GetXmax(); // ...Oh. It doesn't work. I get the message: Error: Symbol hist1 is not defined in current scope. Productivity grinds to a screeching halt.
// in fact, at this point I can't even draw the histogram again. I can draw it once, and then it's gone; no longer defined in the current scope.
```

I’m lost. I can draw it once, but I can’t do anything else with it. Every thread I can find points towards this approach. If I were drawing data from a tree I’d be golden, but the syntax here seems to have taken a leap into the unknown: This is baffling.

---

<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 23, 2014, 5:49pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/2 "2014-08-23T17:49:43Z")

</div>

Execute the following and post the output here:  
TFile \*f = TFile::Open(“first\_file.root”);  
f-\>ls();  
c1-\>Draw();  
c1-\>GetListOfPrimitives()-\>Print();

See also, for example:

> [@Error fitting Histo from a Canvas](https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097/4):
>
> TFile miofile(“test.root”); TCanvas can = (TCanvas)miofile.Get(“c1”); TH1D his = ((TH1D)(can-\>FindObject(“MCA”))); Double\_t xmin = his-\>GetBinCenter(1); Double\_t xmax = his-\>GetBinCenter(his-\>GetNbinsX()); his-\>Fit(“gaus”, “”, “”, xmin, xmax); can-\>Draw(); TFile miofile(“test.root”); TCanvas can = (TCanvas)miofile.Get(“c1”); ((TPad \*)can-\>FindObject(“pad1”))-\>GetListOfPrimitives()-\>ls(); ((TPad \*)can-\>FindObject(“pad1”))-\>GetListOfPrimitives()-\>Print(); TH1D his = ((TH1D)(((TPad \*)can…

> [@Problem with accessing histogram from canvas](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/14):
>
> If your histogram resides in any sub-pad, it will NOT be found by “canvas-\>GetPrimitive(…)”. For some solutions see here (the “MCA” resides in the “pad1”): [url][Error fitting Histo from a Canvas](https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097/4)

> [@How to adjust range in canvas?](https://root-forum.cern.ch/t/how-to-adjust-range-in-canvas/18048/3):
>
> I’m afraid each canvas / pad will have a different name for its “frame” histogram and so you need to play a bit with the TList returned by the “TList \*l = c-\>GetListOfPrimitives();” in order to find it (for example, find the first key which name begins with “frame\_” or maybe it’s sufficient to find the first key which is a TH1D or maybe it’s simply always the second key in the list). Your attached “file.root” has a “frame\_2a3b440”. [code]#include “TFile.h” #include “TCanvas.h” #include “TH1.h…

> [@Getting TGraph from TChain](https://root-forum.cern.ch/t/getting-tgraph-from-tchain/17904/3):
>
> TGraph \*g = ((TGraph \*)((c.GetPrimitive(“Graph”)).Clone(“MyNewGraph”))); TGraph \*g = ((TGraph \*)((c.FindObject(“Graph”)).Clone(“MyNewGraph”)));

---

<div class="post-metadata">

**Author:** ![Serelesis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Serelesis](https://root-forum.cern.ch/u/Serelesis)\
**Post date:** [August 23, 2014, 5:56pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/3 "2014-08-23T17:56:52Z")

</div>

This seems to work just fine:

`root [0] TFile *f = TFile::Open("first_file.root");
root [1] f->ls();
TFile** first_file.root	
 TFile* first_file.root	
  KEY: TCanvas	c1;1	c1
root [2] c1->Draw();
root [3] c1->GetListOfPrimitives()->Print();
Collection name='TList', class='TList', size=3
 TFrame X1=100.000000 Y1=0.000000 X2=2000.000000 Y2=3102.750000 FillStyle=1001
 TH1.Print Name = hMGTDC, Entries= 1089924, Total sum= 1.05843e+06
 TPaveText X1=725.962639 Y1=3234.551182 X2=1374.037361 Y2=3471.201610
 Collection name='TList', class='TList', size=1
  Text X=0.000000 Y=0.000000 Text=MGTDC Energy Font=0 Size=0.000000 Color=0 Align=0
`

I will take a look at the linked threads for solutions.

---

<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 23, 2014, 6:00pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/4 "2014-08-23T18:00:08Z")

</div>

after “c1-\>Draw()” type “c1-\>cd()” and then “hMGTDC” in the ROOT prompt (it should return something like “(TH[123][DFISC]\*)0x12345678”) … one needs to learn what kind of histogram it is.

---

<div class="post-metadata">

**Author:** ![Serelesis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Serelesis](https://root-forum.cern.ch/u/Serelesis)\
**Post date:** [August 23, 2014, 6:08pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/5 "2014-08-23T18:08:55Z")

</div>

> [@Wile E. Coyote](#):
>
> after “c1-\>Draw()” type “c1-\>cd()” and then “hMGTDC” in the ROOT prompt (it should return something like “(TH1[FD]\*)0x12345678”) … one needs to learn what kind of histogram it is.

Quoth the terminal: Error: Symbol hMGTDC is not defined in current scope (tmpfile):1:

---

<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 23, 2014, 6:13pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/6 "2014-08-23T18:13:26Z")

</div>

Try it with a mouse … after “c1-\>Draw()” move the mouse cursor to one of the drawn points and click it with the right mouse button -\> a new menu should appear and it’s title will tell you what kind of a histogram it is (should be something like “TH[123][DFISC]::hMGTDC”).

---

<div class="post-metadata">

**Author:** ![Serelesis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Serelesis](https://root-forum.cern.ch/u/Serelesis)\
**Post date:** [August 23, 2014, 6:18pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/7 "2014-08-23T18:18:24Z")

</div>

> [@Wile E. Coyote](#):
>
> Then try it with a mouse … after “c1-\>Draw()” move the mouse cursor to one of the drawn points and click with the right mouse button → a new menu should appear and it’s title will tell you what kind of a histogram it is (something like “TH[123][DFISC]::hMGTDC”).

It’s TH1I. Ammending previous commands doesn’t seem to change anything.

---

<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 23, 2014, 6:24pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/8 "2014-08-23T18:24:09Z")

</div>

So now you know everything you need to know about it.  
Just follow examples in the links from my first post here.

---

<div class="post-metadata">

**Author:** ![Serelesis](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Serelesis](https://root-forum.cern.ch/u/Serelesis)\
**Post date:** [August 23, 2014, 6:31pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/9 "2014-08-23T18:31:57Z")

</div>

[quote=“Wile E. Coyote”]So now you know everything you need to know about it.  
Just follow examples in the links from my first post here.[/quote]

…this doesn’t help me though. I still cannot create a copy of the histogram in that file, in such a way that allows me to perform tasks with it. As I said before: Ammending previous commands doesn’t seem to change anything.

To be more concise: Knowing that it’s a TH1I still leaves me without the correct syntax to make a copy of some sort, as what I’m doing is clearly wrong.

If I type:

`TFile *file1 = new TFile("first_file.root")
TH1I *hist1 = (TH1I*)file1->Get("c1")`

I have exactly the same issue has I started with. I also cannot use c1 in the way I’d like to be able to use hist1.

Did I miss some explanation here?

---

<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 23, 2014, 6:41pm UTC](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101/10 "2014-08-23T18:41:54Z")

</div>

`TFile *file1 = TFile::Open("first_file.root");
TCanvas *c1; file1->GetObject("c1", c1);
if (!c1) {std::cout << "Error: c1 NOT found!" << std::endl;}
TH1I *hMGTDC = 0;
if (c1) hMGTDC = ((TH1I *)(c1->GetPrimitive("hMGTDC"));
if (!hMGTDC) {std::cout << "Error: hMGTDC NOT found!" << std::endl;}`  
BTW. I’ve got another idea how one can quickly get the kind of the histogram. After “c1-\>Draw()”, in the ROOT prompt, type one of:  
c1-\>GetListOfPrimitives()-\>ls()  
c1-\>GetPrimitive(“hMGTDC”)-\>ClassName()  
c1-\>GetPrimitive(“hMGTDC”)-\>IsA()-\>GetName()
