Histogram from leaf of tree

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!

See “Retrieving the result of Draw” in: TTree::Draw

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

No I am getting some error.

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

You have the wrong quotes:
try:

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

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)

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

The root file that I am using is large. But here is the link.
https://drive.google.com/file/d/1KEwynNDrXrOGenniBhYhZFwGuFHTtAAu/view?usp=sharing
A1.C (4.6 KB)

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

Any solution for that?

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

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.

Try with:
TH1F *myhist = new TH1F("myhist", "myhist", 100, 0., 0.); // automatic binning

1 Like


image


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).

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