1

This might be a stupid question but unfortunately I have root files which contain keys with names such as "DQMInfo/runno". This is the the actual name. Because it represents a 2d histogram someone thought that it would be a good idea to indicate that with a slash.

Unfortunately, uproot thinks that this is part of a path so it is not able to access this key.

import uproot
f = uproot.open("myfile.root")
f['DQMInfo/runno']

gives the KeyInFileError: not found: 'DQMInfo' (with any cycle number) Available keys: 'DQMInfo/runno;1'

I tried to give it bytes instead of strings but I guess I made an error on the way since I never worked with bytes before...

keyname = 'DQMInfo/runno'
bytename= bytes(keyname,'utf-8')
f[bytename] 

gives the error TypeError: a bytes-like object is required, not 'str'

but print(type(bytename)) gives <class 'bytes'>?

I assume this would be the easiest work around. Another idea was to rename the keys but for this I somehow have to iterate through the keys without calling them by name and then rename them (which I also don't know how to do it) and if it is possible to call them without their name then I don't need the name in the first place.

So, is there a way to give uproot a keyname containing a '/' or do I have to some other solution to this?

  • I hate to have to recommend going through a back-door like this, but what about finding the name in `ReadOnlyDirectory._keys`, which is a list. When you find the one that matches the name, call `ReadOnlyKey.get()` on it: https://uproot.readthedocs.io/en/latest/uproot.reading.ReadOnlyKey.html#get – Jim Pivarski Jun 07 '22 at 16:38
  • Thank you for your answer :) I am not so sure if I understand your suggestion. Did you mean, that I get a list of keys and then call the keys through the list? I tried: 'fkeys=f.keys()' and then lets say my key is at position 560. So 'print(fkeys[560])' returns 'DQMInfo/runno;1' But again this is of type string and when I try to call 'f[fkeys[560]]' I get the same error as before. When I try 'f.__keys' I get AttributeError: 'ReadOnlyDirectory' object has no attribute '__keys'. So i guess I am also using an old version and I am unable to update... – Martin Sobotzik Jun 08 '22 at 14:49
  • Single underscore `f._keys`; it's a list, and each of the keys has a `name` property (or `fName`?). If the `name` is equal to the string you're looking for, call its `get()` method. That reads, decompresses, and interprets the data. – Jim Pivarski Jun 09 '22 at 13:40

0 Answers0