# Drawing histogram from ROOT File

**URL:** <https://root-forum.cern.ch/t/drawing-histogram-from-root-file/55304>\
**Category:** Newbie\
**Created:** [June 14, 2023, 7:21pm UTC](https://root-forum.cern.ch/t/drawing-histogram-from-root-file/55304 "2023-06-14T19:21:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [June 14, 2023, 7:21pm UTC](https://root-forum.cern.ch/t/drawing-histogram-from-root-file/55304/1 "2023-06-14T19:21:24Z")

</div>

I am trying to draw a histogram from a ROOT file and my ROOT file structure looks like this.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/0/4/048d789d7b68bdf0e6f74b5f3292614ce8380cef.png)

Here’s my code:

```auto
1 //This code extracts event-cluster histogram from the CAF file and makes changes to them
  2 
  3 #include <iostream>
  4 #include <TFile.h>
  5 #include <TBranch.h>
  6 #include <TLeaf.h>
  7 #include <TH1F.h>
  8 #include <TCanvas.h>
  9 
 10 void evtclust() {
 11 
 12 //Histogram Details
 13 TH1F* hist = new TH1F("hist", "Event vs Cluster; Cluster; Event", 2000, 0, 90);
 14 
 15 //Opening ROOT File and Getting TTree
 16 TFile* file = TFile::Open("emphdata_v03.caf.root");
 17 TTree* tree = (TTree*)file->Get("recTree");
 18         
 19 //Getting Branches 
 20 TBranch* branch1 = tree->GetBranch("rec>>cluster>>cluster.clust");
 21 // TBranch* branch2 = branch1->GetBranch("cluster");
 22 // TBranch* branch3 = branch2->GetBranch("cluster.clust");
 23  
 24 //Getting Leaf
 25 TLeaf* leaf = branch1->GetLeaf("@size");
 26 leaf->Draw("hist");
 27         
 28 file->Close();
 29 }

```

The error I am getting is:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/d/d/dd80cfa9aaf3f74b482e212bd0721731e912afdf.png)

Please help me fix it. Thank you.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 15, 2023, 6:42am UTC](https://root-forum.cern.ch/t/drawing-histogram-from-root-file/55304/2 "2023-06-15T06:42:43Z")

</div>

> [@TheScientist](#):
>
> `TBranch* branch1 = tree->GetBranch("rec>>cluster>>cluster.clust");`

The syntax here is wrong. And to avoid the error message you got do something like this:

```auto
TBranch* branch1 = tree->GetBranch("branch_name");
//Getting Leaf
if (branch1) {
   TLeaf* leaf = branch1->GetLeaf("size");
   if (leaf)
      leaf->Draw("hist");
}

```

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [June 15, 2023, 7:20pm UTC](https://root-forum.cern.ch/t/drawing-histogram-from-root-file/55304/3 "2023-06-15T19:20:27Z")

</div>

Below code is all that’s needed.

```auto
void Code()
   {
         TFile fInput("emphdata_v03.caf.root","READ"); //Opening TFile in read mode
         TTree* tree = (TTree*)fInput.Get("recTree"); //Getting Tree from a ROOT file
         tree->Draw("rec.cluster.@clust.size()");
  }

```
