Problems with TTree.Project("my_histo", "my_var")

Hello,

I am using pyROOT, and I find a very strange issue regarding TTree.Project:

my_tree.Project(“my_histo”,“my_var”)

is giving to my an empty histogram (I checked drawing it or printing its content).

Also the making the following command:

my_tree.Draw(“my_var>>my_hist”) is giving to me an empty histogram.

Firstly, I ensure that the variable inside my tree exists, and also that I am using the name of the histogram (my_histo=TH1D(“my_histo”,“my_histo”,40,1,8))

I also tried to implement gROOT.cd(), before the command regarding the TTree (as I saw here: )

I really don’t know why is making like that


Please read tips for efficient and successful posting and posting code

Please fill also the fields below. Note that root -b -q will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug from the ROOT prompt to pre-populate a topic.

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Can you provide a small reproducer ?

yes sure.

Following:

gROOT.cd()

my_histo=ROOT.TH1D("my_histo","mmm",40,1,8)

my_path = "my_path/"
data=ROOT.TFile.Open(my_path+"my_file_data.root")

print("Name of histo is: ",my_histo.GetName())
my_tree=data.Get("tree")
#my_tree.Scan("log10(mc_E)")                                                                                                          

gROOT.cd()

my_tree.Project("my_histo","log10(mc_E)")

With this test.py:

import ROOT as r

r.gROOT.cd()

h = r.TH1D("h","h",40,-4,4)

data = r.TFile.Open("hsimple.root")

t = data.Get("ntuple")

r.gROOT.cd()

t.Project("h","log10(random)")
h.Draw()

doing python3 -i test.py I get


So it’s not an issue in these lines of code (tested with ROOT v.6.30/06) (maybe you are doing more, that you didn’t show here?) Check that you are really reading the file and getting the histogram with those exact lines. Try

my_tree.Draw("log10(mc_E)")

instead of Project, or also try my_tree.Draw("mc_E"), and see if you get something.

I did two different checks:

using my_tree.Scan(“log10(mc_E)”), I get the following:

************************
*    Row   * log10(mc *
************************
*        0 * 2.8216822 *
*        1 *         0 *
*        2 *         0 *
*        3 * 4.3740312 *
*        4 * 3.1815388 *
*        5 *         0 *
*        6 * 2.6226526 *
*        7 *         0 *
*        8 *         0 *
*        9 *         0 *
*       10 * 3.0238666 *
*       11 *  3.305447 *
*       12 *         0 *
*       13 * 3.5245655 *
*       14 * 2.1336656 *
*       15 *         0 *
*       16 * 1.3761637 *
*       17 * 2.8447526 *
*       18 * 3.2496485 *
*       19 * 3.5313797 *
*       20 *         0 *
*       21 * 3.6976848 *
*       22 * 3.5177172 *
*       23 * 0.2069718 *
*       24 *         0 *

And I of course used my_tree.Draw(“log10(mc_E)”):

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