# Histogram from leaf of tree

**URL:** <https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864>\
**Category:** ROOT\
**Tags:** tree, hist, root\
**Created:** [October 6, 2022, 2:20pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864 "2022-10-06T14:20:31Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![tanmaya\_shree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tanmaya\_shree](https://root-forum.cern.ch/u/tanmaya_shree)\
**Post date:** [October 6, 2022, 2:20pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/1 "2022-10-06T14:20:32Z")

</div>

I have a root file with a tree. I need to write a macro to access the histogram from the tree.  
Opening the root file in the terminal and using the command  
tree.Draw(“leafname”)  
gives me the histogram.

But I need to write a macro and access the histogram.

Thanks!

---

<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:** [October 6, 2022, 2:24pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/2 "2022-10-06T14:24:37Z")

</div>

See “`Retrieving the result of Draw`” in: [TTree::Draw](https://root.cern/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45)

---

<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:** [October 6, 2022, 2:41pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/3 "2022-10-06T14:41:02Z")

</div>

```auto
tree.Draw("leafname");
auto h1 = (TH1F*)gPad->GetPrimitive("htemp"); 

```

---

<div class="post-metadata">

**Author:** ![tanmaya\_shree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tanmaya\_shree](https://root-forum.cern.ch/u/tanmaya_shree)\
**Post date:** [October 6, 2022, 2:44pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/4 "2022-10-06T14:44:43Z")

</div>

No I am getting some error.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/9/7/97963e033a73aff0d3261c69de8771ef7d7ce888.png)

I am trying something like this:

TLeaf \*leaf = tree-\>GetLeaf(“Mult”);  
for (Int\_t i=0; i\<entries; i++){  
leaf-\>GetBranch()-\>GetEntry(i);   
myhist-\>Fill(leaf-\>GetValue());  
}  
myhist-\>Draw();

---

<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:** [October 6, 2022, 2:46pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/5 "2022-10-06T14:46:18Z")

</div>

You have the wrong quotes:  
try:

```auto
TLeaf *leaf = tree->GetLeaf("Mult");

```

---

<div class="post-metadata">

**Author:** ![tanmaya\_shree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tanmaya\_shree](https://root-forum.cern.ch/u/tanmaya_shree)\
**Post date:** [October 6, 2022, 2:50pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/6 "2022-10-06T14:50:02Z")

</div>

TH1F \*myhist = new TH1F();  
TLeaf \*leaf = tree-\>GetLeaf(“Mult”);  
for (Int\_t i=0; i\<entries; i++){  
leaf-\>GetBranch()-\>GetEntry(i);   
myhist-\>Fill(leaf-\>GetValue());  
}  
myhist-\>Draw();

I tried but I am getting empty histogram. I copied that line of code. (quotes line)

---

<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:** [October 6, 2022, 3:01pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/7 "2022-10-06T15:01:01Z")

</div>

Can you post something complete we can run ? for instance, in your example we do not know what `entries` is…

---

<div class="post-metadata">

**Author:** ![tanmaya\_shree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tanmaya\_shree](https://root-forum.cern.ch/u/tanmaya_shree)\
**Post date:** [October 6, 2022, 3:09pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/8 "2022-10-06T15:09:11Z")

</div>

The root file that I am using is large. But here is the link.  
[https://drive.google.com/file/d/1KEwynNDrXrOGenniBhYhZFwGuFHTtAAu/view?usp=sharing](https://drive.google.com/file/d/1KEwynNDrXrOGenniBhYhZFwGuFHTtAAu/view?usp=sharing)  
[A1.C](https://root-forum.cern.ch/uploads/short-url/535eSUoYcnsnqxeaOtWt2eHouSI.C) (4.6 KB)

---

<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:** [October 6, 2022, 3:16pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/9 "2022-10-06T15:16:22Z")

</div>

All your data are in Overflow. You did not specify the histogram limits.

 ![Screenshot 2022-10-06 at 17.15.08](https://root-forum.cern.ch/uploads/default/original/3X/9/7/9769d0a173fc53c0ef92513ddad2e2f2a9a195c6.png)

---

<div class="post-metadata">

**Author:** ![tanmaya\_shree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tanmaya\_shree](https://root-forum.cern.ch/u/tanmaya_shree)\
**Post date:** [October 6, 2022, 3:21pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/10 "2022-10-06T15:21:31Z")

</div>

Any solution for that?

---

<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:** [October 6, 2022, 3:24pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/11 "2022-10-06T15:24:17Z")

</div>

```auto
   double xmin = ???? ;// should be defined
   double xmax = ???? ; // should be defined
   int nbins = ???; // should be defined
   TH1F *myhist = new TH1F("myhist", "myhist", nbins, xmin, xmax);

```

---

<div class="post-metadata">

**Author:** ![tanmaya\_shree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tanmaya\_shree](https://root-forum.cern.ch/u/tanmaya_shree)\
**Post date:** [October 6, 2022, 4:17pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/12 "2022-10-06T16:17:54Z")

</div>

So can I obtain those values from the command line. Like when I use the command line and open the root file and draw the histogram using tree.Draw(“leafname”) command.

---

<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:** [October 6, 2022, 4:31pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/13 "2022-10-06T16:31:55Z")

</div>

[Try with](https://root.cern/doc/master/classTH1.html):  
`TH1F *myhist = new TH1F("myhist", "myhist", 100, 0., 0.); // automatic binning`

---

<div class="post-metadata">

**Author:** ![tanmaya\_shree](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tanmaya\_shree](https://root-forum.cern.ch/u/tanmaya_shree)\
**Post date:** [October 6, 2022, 4:57pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/14 "2022-10-06T16:57:45Z")

</div>

![image](https://root-forum.cern.ch/uploads/default/original/3X/e/d/ed89e5e64945ec79cfa93a4d09fe262b84727279.png)  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/5/0/5002cae851a5aebac12b3bf60de4fe91feaf5f32.png)  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/9/4/948319c39225344f3fcc892a646a1ffd903f1a96.png)  
 ![Screenshot from 2022-10-06 22-23-45](https://root-forum.cern.ch/uploads/default/original/3X/3/3/33636fea7013ca1164d67610df329806c378b088.png)  
 ![c1](https://root-forum.cern.ch/uploads/default/original/3X/a/b/ab0d0d7ce0f64765bef77006671208eddfc24755.png)

Okay so I have some doubts. In the first picture when I do .ls, there are two trees. current cycle and backup cycle. I don’t understand why.  
And I find two different histograms with different number of entries by viewing it in TBrowser l.  
But using the macro and finding the histogram it gives me 10000 entries (c1.png).

---

<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:** [October 6, 2022, 5:00pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/15 "2022-10-06T17:00:10Z")

</div>

> [@Aux;1 aux;2 aux3;](https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345):
>
> Hello! I’m filling a tree and after I, sometimes, get this funny behavior AUX;1 AUX;2 with the same histograms inside but with different events in each ;1 and ;2… Can somebody please tell me why this is happening? Many thanks, Stefanos

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [October 20, 2022, 5:00pm UTC](https://root-forum.cern.ch/t/histogram-from-leaf-of-tree/51864/16 "2022-10-20T17:00:59Z")

</div>

Closed on October 20, 2022, 5:00pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
