Reading and printing data from a tree

Hello All

Fairly new to ROOT
I want to be able to read individual leaves from a ROOT file so that I can work on the data myself in Python.

The ROOT file is organised as:
tree is called cbmsim
that contains three branches, the one of interest is called MCTrack
MCTrack contains lots of leaves, but I am interested in the one called MCTrack.fPdgCode for now.

I currently have a script

import ROOT
f =ROOT.TFile.Open("DEC_12pythia8_Geant4_1_0.5_ID_0.root");
for event in f.cbmsim:
	for track in event.MCTrack:
		#here I want to say... if MCTrack.fPdgCode == 13 -> do other stuff
        #I want to save the values in leaves to python variable

How can I within these loops save the value stored in each leaf to a python variable so that I can play with it.

I have tried things like:

if track.fPdgCode == 13:

It may add to the complication that the leaves I refer to in the file are actually TBranchElements or that they are called MCTrack.fPdgCode not just fPdgCode.

Any ideas?

Thanks a lot

Hi Alex and welcome to the forum :slight_smile:

I see that you want to extract data from the TTree for use in Python but I fail to see what your problem is. Please elaborate. Do you get any error messages?

The simplest thing to do would be to try something along the lines of

... snip ...
if track.fPdgCode == 13:
1 Like

Hello and thanks for response

Yes with suggestion i do get error messages:

Traceback (most recent call last):
  File "", line 10, in <module>
    if track.fPdgCode == 13:
AttributeError: 'TObject' object has no attribute 'fPdgCode'

Problem may arise from track being a TObject instead of a Branch as may be expected

root file if you would perfer:

Thanks for all help


See answer in your new post (Reading tree into python leaf by leaf).


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