Accessing branch with whitespace in name

Hi all,

I have a branch that I naively named “Pt Rec” that I would like to access in a loop. Right now I have something like:

f = ROOT.TFile.Open("input.root", "read")
t = f.Get("t")

t.SetAlias("pt", "Pt Rec")

for event in t:
	ptrec = event.pt
	
...

However, I get the error that ‘Object TTree does not have an attribute named “pt”’. Besides setting an alias, is there any other way to access this branch? My last resort is simply deleting the tree and remaking it without the space, but that process takes a while and I should like to avoid it if at all possible.

Hi,

I’m afraid you will have to recreate the tree without whitespace…

Cheers, Bertrand.

I’m not sure but I think “SetBranchAddress” can use such a name with a space inside (of course, the best idea is to recreate the tree with “proper” names).

[quote=“bellenot”]Hi,

I’m afraid you will have to recreate the tree without whitespace…

Cheers, Bertrand.[/quote]

As I feared. I suppose this is a good lesson in being careful when naming objects!

All the best

Hi,

I support Bertrand’s answer: it’s a very good habit to choose wise names for branches and in general objects written in ROOT files.
On the other hand you can always obtain an attribute by its name python with the builtin function getattr.
For example:

f=ROOT.TFile("hsimple.root") # obtained with this ntuple description TNtuple *ntuple = new TNtuple("ntuple","Demo ntuple","px:py:pz:random num:i"); in hSimple.C
for e in f.ntuple:
    print getattr(f.ntuple,"random num")
    # equivalent to print f.ntuple.random , except that you can use spaces :)

But again, this is a kludge, I think it’s better to rewrite the name of the branch :slight_smile:

Cheers,
D

How about branch with [] in name?