Python PyROOT- reading value from Trees/Branches

  • Thread starter Thread starter ChrisVer
  • Start date Start date
  • Tags Tags
    Reading Value
Click For Summary
The discussion revolves around troubleshooting a Python function designed to extract weights from a tree structure based on two branches: Trigger and Variable. The user reports that while the Variable value is correctly retrieved, the Trigger value consistently returns 0. This issue is suspected to stem from either the incorrect population of the branches_list or the tree variable not containing the expected Trigger branch. Suggestions include using a debugger to inspect the tree and branches_list for proper initialization. The conversation also touches on the use of the getattr() function, with one participant sharing a test case that successfully retrieves attribute values from a class instance, contrasting with the original issue. Ultimately, the user resolves the problem by discovering that the tree was only addressing the Variable branch, highlighting the challenges of working with unfamiliar code, particularly in inheritance scenarios. Debugging and thorough code review are emphasized as essential practices for understanding and modifying existing code.
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
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 18 ·
Replies
18
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K