Drawing histogram from ROOT File

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

Here’s my code:

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:

Please help me fix it. Thank you.

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

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

Below code is all that’s needed.

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()");
  }


1 Like