Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python PyROOT- reading value from Trees/Branches

  1. Apr 2, 2016 #1

    ChrisVer

    User Avatar
    Gold Member

    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).
    Code (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?
     
  2. jcsd
  3. Apr 2, 2016 #2

    Borg

    User Avatar
    Gold Member

    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?
     
  4. Apr 2, 2016 #3

    ChrisVer

    User Avatar
    Gold Member

    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:
    Code (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 (Text):

    Width =3
    Length=4
    Height=2
     
    which is wrong! (the length)
     
  5. Apr 2, 2016 #4

    Borg

    User Avatar
    Gold Member

    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.
     
  6. Apr 3, 2016 #5

    Mark44

    Staff: Mentor

    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:
    Code (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 (Text):

    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.
     
  7. Apr 3, 2016 #6

    Mark44

    Staff: Mentor

    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.
     
  8. Apr 3, 2016 #7

    ChrisVer

    User Avatar
    Gold Member

    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.
     
  9. Apr 3, 2016 #8

    Mark44

    Staff: Mentor

    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.
    Sure. I think using the getattr() function is a good idea.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: PyROOT- reading value from Trees/Branches
  1. Reading from file (Replies: 3)

Loading...