PyROOT- reading value from Trees/Branches

  • Context: Python 
  • Thread starter Thread starter ChrisVer
  • Start date Start date
  • Tags Tags
    Reading Value
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to reading values from branches in a tree structure using PyROOT, a Python interface for ROOT, which is commonly used in data analysis in physics. Participants are trying to troubleshoot a function that retrieves values from specified branches and calculate weights based on those values.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes a function intended to return weights based on values from branches named Trigger and Variable, but encounters an issue where Trigger_pass always returns 0.
  • Another participant suggests that the problem may stem from either the branches_list not being populated correctly or the tree variable lacking the expected value for Trigger.
  • A participant questions the implementation of the getattr method and provides a test example to illustrate its use, noting an unexpected output for one of the attributes.
  • One participant mentions the need to learn Python and shares a resource about initializing object variables.
  • Another participant reports that they cannot reproduce the original output and provides their own corrected version of the test code, which yields the expected results.
  • A later reply suggests using a debugger to inspect the tree and branches_list variables for initialization issues, recommending the built-in Pdb debugger for this purpose.
  • One participant eventually resolves their issue, discovering that the tree was only addressing the Variable branch, and seeks advice on dealing with code involving inheritance from other classes.
  • Another participant expresses a lack of tips for navigating inherited classes, emphasizing the importance of understanding the original code and the utility of a debugger.

Areas of Agreement / Disagreement

Participants express varying levels of understanding and experience with Python and debugging. While some participants provide suggestions and insights, there is no consensus on the best approach to resolve the initial issue, as it appears to be context-dependent.

Contextual Notes

There are limitations regarding the understanding of how the tree variable is structured and how branches are populated, which may affect the retrieval of values. The discussion also highlights the challenges of working with inherited classes in Python, particularly when the original code is not well understood.

ChrisVer
Science Advisor
Messages
3,372
Reaction score
465
I am trying to make a part of my following code to work but so far I haven't been successful...
I have 1 tree file with several branches but the two I care about are named Trigger and Variable. I have calculated in two histograms h(Variable) the weights I want to obtain.
My function that I am trying to run is supposed to return me the weight of the given Variable value and Trigger (takes values 1 or 2).
Python:
def return_weight(tree, histo_list, branches_list):
    #tree containing the variables in branches
    #histo_list = [histo_triggerPass(Variable), histo_triggerFail(Variable) ]
    #branches_list = [Variable, Trigger]
   
    Variable_val = getattr( tree , branches_list[0])
    Trigger_pass= getattr( tree, branches_list[1])
    #Problem is supposed to be HERE ^
    print Variable_val, " \t ", Trigger_pass

     if Trigger_pass==1: ...
     else: ...

The printing output is telling me that the Variable_val is obtained correctly from the tree, but the Trigger_pass is not (it somehow always returns 0). Any help?
 
Technology news on Phys.org
I don't know python but it seems there are only two ways that it could break. Either branches_list[1] isn't populated correctly or the tree variable doesn't have the branches_list[1] value populated. Can you create a break point in your IDE and examine them?

Also, what does your getattr method look like?
 
Borg said:
Also, what does your getattr method look like?

I think getattr() is a built-in function coming with Python. It is supposed to return the value of an attribute of an object.

Also I am trying to understand how exactly it works. Because I did the following test-example:
Python:
class Box():
    def __init__(self,width,length,height):
        self.width = width
        self.length= length
        self.height= height

box = Box(3,5,2)
print "Width =",getattr(box, 'width')
print "Length=",getattr(box, 'length')
print "Height=",getattr(box, 'height')

the output is:

Code:
Width =3
Length=4
Height=2

which is wrong! (the length)
 
I did some searching but I don't think that I can help. I just confirmed that I really need to get around to learning python. I did find one article about initializing object variables that was similar to your last post but I don't know how much it would help.
 
I am not able to reproduce your output. I am using v3.4.2, while you are using v2.x, I believe. I made a slight change in the print statements like so:
Python:
class Box():
   def __init__(self,width,length,height):
     self.width = width
     self.length= length
     self.height= height

box = Box(3,5,2)
print ("Width =",getattr(box, 'width'))
print ("Length=",getattr(box, 'length'))
print ("Height=",getattr(box, 'height'))
Here is the output I get:
Code:
C:\Users\Mark\Documents\Python3.4.2>python test.py
Width = 3
Length= 5
Height= 2
As you can see, the length value is correctly reported.
 
ChrisVer said:
I am trying to make a part of my following code to work but so far I haven't been successful...
I have 1 tree file with several branches but the two I care about are named Trigger and Variable. I have calculated in two histograms h(Variable) the weights I want to obtain.
My function that I am trying to run is supposed to return me the weight of the given Variable value and Trigger (takes values 1 or 2).
Python:
def return_weight(tree, histo_list, branches_list):
    #tree containing the variables in branches
    #histo_list = [histo_triggerPass(Variable), histo_triggerFail(Variable) ]
    #branches_list = [Variable, Trigger]
  
    Variable_val = getattr( tree , branches_list[0])
    Trigger_pass= getattr( tree, branches_list[1])
    #Problem is supposed to be HERE ^
    print Variable_val, " \t ", Trigger_pass

     if Trigger_pass==1: ...
     else: ...

The printing output is telling me that the Variable_val is obtained correctly from the tree, but the Trigger_pass is not (it somehow always returns 0). Any help?
Are you using a debugger? Using a debugger you can inspect your tree and branches_list variables and see whether they are being initialized correctly. Without more information on your tree variable (a class?) and the branches_list list, I can't say anything about why Trigger_pass isn't being set correctly.

There is a simple debugger that comes with Python, Pdb. I wrote a pair of Insights articles on using this debugger. The first one is https://www.physicsforums.com/insights/simple-python-debugging-pdb-part-1/. The other article has Part 2 in its name.
 
I was able to solve the problem... for some reason the tree was addressing only the Variable branch, not all the branches... that's a problem I have when I have to deal with codes I didn't write myself, and especially when my target's to change stuff in a mixture of daughter and mother classes [inheritance]. Do you have any tips for doing that, except for experience?

As for the getattr() I don't know..for that reason I resorted to just typing tree.attributename instead.
 
ChrisVer said:
I was able to solve the problem... for some reason the tree was addressing only the Variable branch, not all the branches... that's a problem I have when I have to deal with codes I didn't write myself, and especially when my target's to change stuff in a mixture of daughter and mother classes [inheritance]. Do you have any tips for doing that, except for experience?
I don't have any tips -- Python is relatively new me. If you are writing code that uses classes that inherit from each other, it's more difficult when someone else wrote code, because you don't have the same understanding of that code as the person who wrote it. In such cases, you typically need to spend more time reading through that code, to get a better understanding of it. Using a debugger is very helpful, though.
ChrisVer said:
As for the getattr() I don't know..for that reason I resorted to just typing tree.attributename instead.
Sure. I think using the getattr() function is a good idea.
 

Similar threads

Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K