PyROOT- reading value from Trees/Branches

In summary: 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?It sounds like you are trying to do something like inheritance in your code, which is not something that is supported in Python. You might want to look into using a different language for this task.In summary, the problem is that the tree variable is only looking at the Variable branch of the branches_list list, not all of them.
  • #1
ChrisVer
Gold Member
3,378
464
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
  • #2
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?
 
  • #3
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)
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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.
 

Related to PyROOT- reading value from Trees/Branches

1. What is PyROOT and how is it used for reading values from Trees/Branches?

PyROOT is a Python extension module that allows for easy access to the ROOT framework, which is a software toolkit commonly used in the scientific community for data analysis and visualization. PyROOT provides a Python interface to ROOT, allowing for efficient and flexible manipulation of ROOT objects, including Trees and Branches.

2. How do I access a specific value from a Tree/Branch in PyROOT?

To access a specific value from a Tree or Branch in PyROOT, you can use the "GetEntry()" function. This function takes in the index of the desired entry and returns a pointer to that entry. You can then use the "GetLeaf()" function to retrieve the value of a specific variable or branch within that entry.

3. Can I read multiple values from a Tree/Branch at once in PyROOT?

Yes, you can read multiple values from a Tree or Branch at once in PyROOT. You can use the "GetEntry()" function to retrieve a pointer to a specific entry, and then use the "GetLeaf()" function to retrieve the values of multiple variables or branches within that entry.

4. How can I filter or select specific entries from a Tree in PyROOT?

You can filter or select specific entries from a Tree in PyROOT by using the "Draw()" function. This function allows you to specify a selection criteria, such as a cut on a specific variable, and will return a Tree containing only the selected entries. You can then use the "GetEntry()" function to access the selected entries and retrieve their values.

5. Is there a limit to the size of Trees/Branches that can be read using PyROOT?

No, there is no inherent limit to the size of Trees or Branches that can be read using PyROOT. However, the size of the data being read may impact the performance of your code. It is recommended to use the "GetEntry()" function to read data in batches rather than all at once, in order to optimize performance.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
1
Views
974
  • Programming and Computer Science
Replies
18
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
823
  • Programming and Computer Science
Replies
3
Views
354
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top