# 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:** 1\
**Showing post:** 2

<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”)));

---

_[View the full topic](https://root-forum.cern.ch/t/opening-a-histogram-from-a-file/18101)._
