# Example to show how to display histograms?

**URL:** <https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818>\
**Category:** Newbie\
**Created:** [February 2, 2020, 4:39pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818 "2020-02-02T16:39:38Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 2, 2020, 4:39pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/1 "2020-02-02T16:39:38Z")

</div>

Hi all,

Is there an example that will show me how to display the histograms in the attached picture?

Thanks in advance

Tim

![root_screenshot](https://root-forum.cern.ch/uploads/default/original/3X/7/6/764f197d2a35e631da2ac947aa45864900a5d898.jpeg)

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [February 3, 2020, 8:17am UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/2 "2020-02-03T08:17:24Z")

</div>

Hi Tim,

You can double-click on the histogram and it will appear on the right side of the browser.

Cheers,  
Jakob

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 4, 2020, 8:22pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/3 "2020-02-04T20:22:57Z")

</div>

Hi Jakob, thanks for your reply.

I suppose I wasn’t sufficiently clear with my request. I want to draw the histograms by code and not through the Root browser so I can do some funky fitting stuff in that code. But I’ve found an example that may help (tree1.C). I’ll play with that first.

Regards

Tim

---

<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:** [February 5, 2020, 9:20am UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/4 "2020-02-05T09:20:10Z")

</div>

> [@TimFulcher](#):
>
> I want to draw the histograms by code and not through the Root browser

```auto
your_histogram->Draw();

```

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 12, 2020, 1:59pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/5 "2020-02-12T13:59:00Z")

</div>

Apologies for late reply. I’m a part-timer and don’t get as much time to dedicate to this as I’d like.

Having thought about this for a while I think my confusion stems from the difference between a tree, branch and leaf.

If we look at the picture I posted, there are examples that describe how to read and display histograms on the level immediately below the root file but my histograms are 2 levels below that. May I consider those histograms (in this case) as leaves of the branch PerEntryHistograms which is a branch off Event which is, in turn a branch off the root file?

If I’m correct in my above assumption I suppose I would address the branch Event and, off that, address the branch PerEntryHistograms and, finally, open and draw my histograms?

Regards

Tim

---

<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:** [February 12, 2020, 2:18pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/6 "2020-02-12T14:18:18Z")

</div>

```auto
your_file->cd("subdir");
your_histogram->Draw();

```

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 12, 2020, 3:03pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/7 "2020-02-12T15:03:58Z")

</div>

Hi Olivier,

sorry not at my machine with root installed currently. But do the “branches” appear like directories to root?

Regards

Tim

---

<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:** [February 12, 2020, 3:21pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/8 "2020-02-12T15:21:27Z")

</div>

What your picture shows is a `TFile` with several levels of directories. At the bottom level you have histograms. You just need to `cd` to that level and do a `Draw` of your histogram.

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 12, 2020, 7:01pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/9 "2020-02-12T19:01:28Z")

</div>

Thanks Olivier. I found this great tut which explains it well: [SLAC link](https://www.slac.stanford.edu/BFROOT/www/doc/workbook/root2/root2.html)

So I’m on my way…I hope 😊

Regards

Tim

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 14, 2020, 11:34am UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/10 "2020-02-14T11:34:59Z")

</div>

Hi all

Oliviers suggestion worked fine but only from the Root command line. If I try do the same in a macro it raises an error when I try to Draw the histogram. Not on my Linux machine currently but will paste the error when I get home.

Regards

Tim

---

<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:** [February 14, 2020, 12:16pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/11 "2020-02-14T12:16:20Z")

</div>

In a macro do:

```auto
your_file->cd("subdir");
TH1F *h = (TH1F*)your_file->Get("your_histogram");
h->Draw();

```

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 14, 2020, 5:24pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/12 "2020-02-14T17:24:35Z")

</div>

Sorry for being an idiot but I’m getting a warning when I use your suggestion:

Error in : Trying to dereference null pointer or trying to call routine taking non-null arguments.  
Execution of your code was aborted.  
In file included from input\_line\_28:1:  
/home/tim/Data/Thesis/bdsim/WP/read\_plot\_from\_file.C:20:2: warning: null passed to a callee that requires a non-null argument [-Wnonnull]  
h-\>Draw();

I tried passing a non-null argument in Draw (“SAME”).

Regards

Tim

`
read_plot_from_file.C (504 Bytes)`

---

<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:** [February 16, 2020, 6:30pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/13 "2020-02-16T18:30:10Z")

</div>

Try:

```cpp
gDirectory->ls();
TH1F *h1; gDirectory->GetObject("Primary_Energy", h1);

```

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 16, 2020, 6:55pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/14 "2020-02-16T18:55:14Z")

</div>

Nah, sorry. Same error.

---

<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:** [February 16, 2020, 6:57pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/15 "2020-02-16T18:57:39Z")

</div>

What do you get from `gDirectory->ls();`

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 16, 2020, 6:59pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/16 "2020-02-16T18:59:28Z")

</div>

Truncated, terser, version of previous f-\>ls();

TDirectoryFile\* PerEntryHistograms PerEntryHistograms  
KEY: TH1D Primary\_Energy;1 Primary\_Energy  
KEY: TH1D EnergyLoss;1 EnergyLoss

---

<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:** [February 16, 2020, 7:01pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/17 "2020-02-16T19:01:22Z")

</div>

So, if you have a “TH1D” in your file, why are you surprised that it cannot retrieve a “TH1F”?

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 16, 2020, 7:05pm UTC](https://root-forum.cern.ch/t/example-to-show-how-to-display-histograms/37818/18 "2020-02-16T19:05:31Z")

</div>

'cause I’m as thick as 😊 (in my defence I’m a noob).

Cheers mate. That’s got it.

Regards

Tim
