# How do I get an object from a file?

**URL:** <https://root-forum.cern.ch/t/how-do-i-get-an-object-from-a-file/33260>\
**Category:** Newbie\
**Created:** [March 21, 2019, 1:17pm UTC](https://root-forum.cern.ch/t/how-do-i-get-an-object-from-a-file/33260 "2019-03-21T13:17:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marta/32/18269_2.png) [@Marta](https://root-forum.cern.ch/u/Marta)\
**Post date:** [March 21, 2019, 1:17pm UTC](https://root-forum.cern.ch/t/how-do-i-get-an-object-from-a-file/33260/1 "2019-03-21T13:17:07Z")

</div>

Hello,  
I am totally new to ROOT, pretty much new to coding altogether and I need help. I need to get a histogram out of a TFile, here is what I have: (I know this program itself does not do much, but it fits into a bigger program.)

```auto
void minihstack(){
  TFile file("histograms.root"); // Let's open the TFile
  TH1F * h_muon0_pt; // Get the Histogram out 
  file.GetObject("h_muon0_pt",h_muon0_pt);
  h_muon0_pt->Draw();
}

```

I always get “warning: null passed to a callee that requires a non-null argument”. Obviously the line with GetObject is wrong, but I cannot find a way to correct it. I am positive the histogram h\_muon0\_pt is in the histograms.root file.  
Thank you very much for any ideas on how to fix this!

---

<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:** [March 21, 2019, 1:30pm UTC](https://root-forum.cern.ch/t/how-do-i-get-an-object-from-a-file/33260/2 "2019-03-21T13:30:24Z")

</div>

```auto
file.ls();
```

---

<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 21, 2019, 1:32pm UTC](https://root-forum.cern.ch/t/how-do-i-get-an-object-from-a-file/33260/3 "2019-03-21T13:32:22Z")

</div>

For info your code is working with an file I have:

```auto
{
  TFile file("hsimple.root"); // Let's open the TFile
  TH1F * hpx; // Get the Histogram out
  file.GetObject("hpx",hpx);
  hpx->Draw();
}

```

as Wile suggest it would be good to see the content of your file.

---

<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 21, 2019, 1:34pm UTC](https://root-forum.cern.ch/t/how-do-i-get-an-object-from-a-file/33260/4 "2019-03-21T13:34:58Z")

</div>

Also you better do:

```auto
{
  auto file = new TFile("histograms.root"); // Let's open the TFile
  TH1F * h_muon0_pt; // Get the Histogram out
  file->GetObject("h_muon0_pt",h_muon0_pt);
  h_muon0_pt->Draw();
}

```

Otherwise you will get an empty plot.

---

<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:** [March 21, 2019, 1:36pm UTC](https://root-forum.cern.ch/t/how-do-i-get-an-object-from-a-file/33260/5 "2019-03-21T13:36:32Z")

</div>

Actually, in the original macro, you simply need `h_muon0_pt->DrawCopy();` 😉

---

<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 21, 2019, 1:43pm UTC](https://root-forum.cern.ch/t/how-do-i-get-an-object-from-a-file/33260/6 "2019-03-21T13:43:26Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> Actually, in the original macro, you simply need `h_muon0_pt->DrawCopy();` 😉

Yes that’s an other way …

---

<div class="post-metadata">

**Author:** ![Marta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marta/32/18269_2.png) [@Marta](https://root-forum.cern.ch/u/Marta)\
**Post date:** [March 21, 2019, 1:59pm UTC](https://root-forum.cern.ch/t/how-do-i-get-an-object-from-a-file/33260/7 "2019-03-21T13:59:47Z")

</div>

Thank you so much, it works! There was a semicolon behind the name of the histogram for some odd reason and I missed that.  
And now the plot is not empty, either 😃
