python return None

Question:

I am trying to return objects from a function, in the format of dictionary. It seems it is working fine inside of function, but return “None”.

===================================================
The printed result is ::

{'tree1': [<ROOT.TH1D object ("root2_tree_tree1_px_hist") at 0x7fa40eec05b0>, <ROOT.TH1D object ("root2_tree_tree1_py_hist") at 0x7fa40eec0cd0>, <ROOT.TH1D object ("root2_tree_tree1_pz_hist") at 0x7fa40eec13f0>], 'tree2': [<ROOT.TH1D object ("root2_tree_tree2_px_hist") at 0x7fa40eec2620>, <ROOT.TH1D object ("root2_tree_tree2_py_hist") at 0x7fa40eec2f20>, <ROOT.TH1D object ("root2_tree_tree2_pz_hist") at 0x7fa40eeb4850>]}

{'tree1': [None, None, None], 'tree2': [None, None, None]}

So, now I am confused. It is printing right value at function, but not returning correctly.
The codes is like below.

def set_histograms():
    .
    .
    .
    print(DichistList)  # this would correctly print dictionary 
    return DichistList

def main():
    DICHISTLIST = set_histograms()
    print(DICHISTLIST)  # this is printing "None", why?
Asked By: junho

||

Answers:

So, this might have to do with the fact that you’re using ROOT, and ROOT and python don’t play nice unless you use specific packages. I highly recommend checking out root_numpy, I used it extensively back when I was in particle physics.

Example: Reading in myfile.root which contains ttree mytree:

from root_numpy import root2array
myarray = root2array( 'myfile.root', 'mytree' ) )

You can immediately convert this into a pandas dataframe with:

df = pd.DataFrame( myarray )
Answered By: n3utrino
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.