Reading nTuple in PyROOT - variables containing '.'

Dear all,

first of all, the search revealed this related topic:

Since it’s from 2006 I’ve made a new topic.

My setup:
I have variables stored in my nTuples of the type ‘Object.Property’, sometimes even ‘Object.SubObject.Property’.

Following the example from above thread I should be able to access it via:

[code]chain = TChain(“rootTupleTree/tree”);

chain.Add("nTuple.root");
chain.SetBranchStatus("*", 0);
chain.SetBranchStatus("Electron.Energy", 1);
chain.SetBranchStatus("run", 1);

print chain.GetEntries()

for event in chain:
    print event.run #works fine
    print event.Electron.Energy

#produces error: ‘TChain’ object has no attribute ‘Electron’
[/code]

However, if I try to access it’s attribute via

for event in chain: print event.__getattr__('run') #works fine print event.__getattr__('Electron.Energy') #this works

Is this indented or is it a missing feature?

Of course it gets complicated if one needs to create an extra layer for such variables, but on the other hand it would make it easy to read ‘objects’ in a ROOT file .

Hi,

is there an actual object of type Electron stored, or is it just in the labeling? The problem being that the meaning of ‘.’ changes if it’s just a label.

Cheers,
Wim

[quote=“wlav”]Hi,
is there an actual object of type Electron stored, or is it just in the labeling?
[/quote]

Sorry, I wasn’t clear. The ‘.’ is just the labeling, only plain numbers and vectors of them are stored in the above data.

Hi,

okay, then I’ll maintain that there’s a problem in automatic translation it in object-like structures, since ROOT I/O already assigns extra meaning to the ‘.’, see e.g. http://root.cern.ch/phpBB3//viewtopic.php?t=9450.

Cheers,
Wim

Hi,

this is what confuses me. If the automatic translation fails, which it should in this case, I would expect an error.

What happens instead is that ROOT makes an entry in the dictionary of the chain object (adds a variable) which contains a dot. This can’t be resolved by python, since it expects an object being saved as a variable.

This is why

fails and

works.

So in order for this to work, ROOT should translate and ‘.’ variables into simple objects (struct) such as
Electron.* -> Electron object if it can’t find the corresponding TObject and do it recursively (Electron.Track.d0 etc).

Anyway, I just wanted to summarise my findings, there is a workaround so it’s nothing major.

Thanks for the answers!

Hi,

wouldn’t think so … it is getattr of TTree that is implemented in Pythonize.cxx, so python first resolves the expression “chain.Electron.Energy” into “chain.Electron” and the latter isn’t found as a branch, resulting in an error.

Cheers,
Wim