PyROOT tree reading

Hey there,

I am new to using PyROOT, but have been using the c++ version for quite a while. I was wondering how I can read in one of my trees generated in c++. This is a really simplified version of the tree definition:

class Cluster
		ModuleData mod;
		float            x;
		std::vector<int> pixelsCol;
// (...)
// The branching contains different kinds of data types:
	clusterTree -> Branch("event",        &eventField,                eventField.list.c_str()); // Other class
	clusterTree -> Branch("x",            &clusterField.x,            "x/F"); // Simple variable of the class 
	clusterTree -> Branch("mod",          &clusterField.mod,          ModuleData::list.c_str()); // Field that is a class itself
	clusterTree -> Branch("pixelsCol",     &clusterField.pixelsCol); // Array of variables in the cass

What can I do in PyROOT to read in the variables in the tree defined in this manner?
I’ve tried to add c++ code to the script like this (again, simplified):

    "struct ModuleData { \
            Int_t det; \
            Int_t layer; \
mod_on = ModuleData()```

This results an error message, telling me that 'name ModuleData() is not defined'.

Can you help me out?


you can perhaps simplify further the approach. An event loop in PyROOT looks like:

myfile = TFile("myfile.root")
mytree=myfile.mytree # this is the name of the tree in the file
for e in mytree:
   print e.x, e.event # x and event are names of branches of the tree


1 Like

What do I do for non-simple variables? I tried this:

for index, entry in enumerate(clusterTree):
    print entry.x # works
    print entry.mod_on # results AttributeError: 'TTree' object has no attribute 'mod_on'
    if index > 10: break

In your first post you have a variable in your TTree called mod, here you are trying to access a mod_on, which doesn’t exist given your previous definition?

While experimenting I found that the variables of structure types might (or might not) be accessed via their name inside the structure. The problem is, that I have more fields in my tree with the same structure. Can I somehow tell the program which complex structure to read, when accessing a variable by its name? In the sample file I want to use, I have two branches, both of type ModuleData called mod and mod_on. What can I do to access variable called layer in the first one and in the second one? Thanks!

Shaktal, sorry, as I mentioned I simplified the tree definition, I have both mod and mod_on, both of type ModuleData.


you can access methods and fields with the dot operator.


But how do I do that, if entry.mod and entry.mod_on both results AttributeError: 'TTree' object has no attribute 'mod' (or mod_on).

Could you share the code you are running and if possible the input file (or part of it)?


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.