# Accessing TH1D and manipulating them?

**URL:** <https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938>\
**Category:** Newbie\
**Created:** [July 1, 2019, 2:34pm UTC](https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938 "2019-07-01T14:34:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [July 1, 2019, 2:34pm UTC](https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938/1 "2019-07-01T14:34:25Z")

</div>

Hi!

I have a number of TH1D files in a ROOT file and I would like to read them out one by one and plot them.

I currently have written the following:

`void histo_redraw() { TString filename = "file.root"; TFile* file = TFile::Open(filename); TCanvas* c = (TCanvas*) file->Get("c2"); c->ls(); }`

This shows me the following:  
 ![Screenshot%20from%202019-07-01%2016-31-53](https://root-forum.cern.ch/uploads/default/original/3X/0/d/0dc017406a42cd016b6fefcf87d1cb75b5c0397c.png)

I would like to draw the TH1Ds all into a new canvas and save them. I’ve tried `c->GetObject("muon", histoMuon);` but it says that there is “no member named GetObject in TCanvas”.

Could anyone give me an idea on how to proceed?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 2, 2019, 7:51am UTC](https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938/2 "2019-07-02T07:51:46Z")

</div>

Try something like this:

```auto
  TString filename = "file.root";
  TFile* file = TFile::Open(filename);
  TCanvas* c = (TCanvas*) file->Get("c2");
  c->ls();
  c->Draw();
  TH1D *measurement = (TH1D*)c->GetPrimitive("measurement");
  TH1D *Muons = (TH1D*)c->GetPrimitive("Muons");
    (...etc...)
    (...change to another canvas)
  measurement->Draw();
    (...etc...)

```

Note that if you want to draw the histos in the same “original” canvas (“c”), you should get them all before drawing them, since drawing will clear the canvas (unless you use “same”).

---

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [July 2, 2019, 9:01am UTC](https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938/3 "2019-07-02T09:01:08Z")

</div>

Hi @dastudillo:

I somehow get the error that `null passed to a callee that requires a non-null argument [-Wnonnull]`, especially when I’m plotting the `Muons` and `innerPbShielding_Pb210` TH1Ds. Any idea why?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 2, 2019, 9:14am UTC](https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938/4 "2019-07-02T09:14:21Z")

</div>

Can you post your code and output?

---

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [July 2, 2019, 9:23am UTC](https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938/5 "2019-07-02T09:23:16Z")

</div>

![screenshot](https://root-forum.cern.ch/uploads/default/original/3X/8/9/893141569a30052cc4e93edeb704e47f7f91f1b8.png)

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 2, 2019, 10:27am UTC](https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938/6 "2019-07-02T10:27:36Z")

</div>

Ok, those histograms are inside a THStack. To get them from there you can try:

```auto
   THStack *hs = (THStack*)c->GetPrimitive("stack_all");
   TH1D *Muons = (TH1D*)hs->GetHists()->FindObject("Muons");
   ...etc

```

---

<div class="post-metadata">

**Author:** ![tk231](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tk231](https://root-forum.cern.ch/u/tk231)\
**Post date:** [July 2, 2019, 11:14am UTC](https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938/7 "2019-07-02T11:14:31Z")

</div>

It worked! Thanks a lot!

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 2, 2019, 11:16am UTC](https://root-forum.cern.ch/t/accessing-th1d-and-manipulating-them/34938/8 "2019-07-02T11:16:21Z")

</div>

Great, cheers!

- Daniel
