1

I've made root files with histograms through another program, and I'm trying to extract this histograms in a Jupyter notebook using Python3 to do various bits of analysis on these histograms.

In the past, this has been a non-issue, but I'm on a new device and things have been wonky.

I have a root file named root_file.root located at /location and inside this root file are a number of TDirectories. If numbers are important, the histograms I'm trying to access are 5 layers of TDirectories in, which I'll label as tdir, and the histogram as root_hist

My imports:

import numpy as np import matplotlib.pyplot as plt import math import pandas as pd import uproot 

To open this file I've been using

file = uproot.open("/location/root_file.root") 

I can then use file.keys() to see all the various histograms and TDirectories within the root file. I can get myself all the way to the point where I can see the histogram I want to access through file['tdir'].keys() and one of the keys is 'root_hist'. I can even run file['tdir'].classnames() and see that 'root_hist' is a TH2F.

The issue is that then when I try to actually access the histogram thought hist = file['tdir']['root_hist'] I get a Recursion Error (note that test[key0] is the same thing as file['tdir']['root_hist'] where test=file['tdir'] and key0='root_hist'):

RecursionError Traceback (most recent call last) Input In [27], in <cell line: 7>() 5 print(test.keys()) 6 print(key0) ----> 7 test[key0].all_members File ~/Library/Python/3.8/lib/python/site-packages/uproot/reading.py:2089, in ReadOnlyDirectory.__getitem__(self, where) 2087 else: 2088 last = step -> 2089 step = step[item] 2091 elif isinstance(step, uproot.behaviors.TBranch.HasBranches): 2092 return step["/".join(items[i:])] File ~/Library/Python/3.8/lib/python/site-packages/uproot/reading.py:2089, in ReadOnlyDirectory.__getitem__(self, where) 2087 else: 2088 last = step -> 2089 step = step[item] 2091 elif isinstance(step, uproot.behaviors.TBranch.HasBranches): 2092 return step["/".join(items[i:])] [... skipping similar frames: ReadOnlyDirectory.__getitem__ at line 2089 (2966 times)] File ~/Library/Python/3.8/lib/python/site-packages/uproot/reading.py:2089, in ReadOnlyDirectory.__getitem__(self, where) 2087 else: 2088 last = step -> 2089 step = step[item] 2091 elif isinstance(step, uproot.behaviors.TBranch.HasBranches): 2092 return step["/".join(items[i:])] File ~/Library/Python/3.8/lib/python/site-packages/uproot/reading.py:2072, in ReadOnlyDirectory.__getitem__(self, where) 2070 if item != "": 2071 if isinstance(step, ReadOnlyDirectory): -> 2072 if ":" in item and item not in step: 2073 index = item.index(":") 2074 head, tail = item[:index], item[index + 1 :] File ~/Library/Python/3.8/lib/python/site-packages/uproot/reading.py:1922, in ReadOnlyDirectory.__contains__(self, where) 1920 def __contains__(self, where): 1921 try: -> 1922 self.key(where) 1923 except KeyError: 1924 return False File ~/Library/Python/3.8/lib/python/site-packages/uproot/reading.py:2014, in ReadOnlyDirectory.key(self, where) 2000 def key(self, where): 2001 """ 2002 Returns a ``TKey`` (:doc:`uproot.reading.ReadOnlyKey`) for the object 2003 selected by ``where``. (...) 2012 Note that this does not read any data from the file. 2013 """ -> 2014 where = uproot._util.ensure_str(where) 2016 if "/" in where: 2017 items = where.split("/") File ~/Library/Python/3.8/lib/python/site-packages/uproot/_util.py:67, in ensure_str(x) 63 def ensure_str(x): 64 """ 65 Ensures that ``x`` is a string (decoding with 'surrogateescape' if necessary). 66 """ ---> 67 if isinstance(x, bytes): 68 return x.decode(errors="surrogateescape") 69 elif isinstance(x, str): RecursionError: maximum recursion depth exceeded while calling a Python object 

I don't understand myself to be doing any recursion here, but it is a consistent error that I get. I've tried upping the limit on recursion, but end up crashing my kernel before solving the problem. I can find no documentation that leads me to believe I'm trying to access the histogram in any way other than the intended method

Where am I going wrong?

3
  • This sounds like a bug; please report it here: github.com/scikit-hep/uproot5/issues . However, I made a ROOT file with the same nested directory structure that you describe and don't get the RecursionError. Try it on the latest version of Uproot before reporting. (Is your notebook on a machine with a different environment than when it had worked before?) Commented Aug 11, 2022 at 18:50
  • The confusing piece is that when I run the tutorial from uproot it reads the root file in perfectly fine: file2 = uproot.open("scikit-hep.org/uproot3/examples/hepdata-example.root") print(file2.classnames()) file2["hpxpy"].all_members This yields what I would expect and I can work with it. This is all within the same Jupyter Notebook too. I'm honestly not entirely sure how different the environments are given I can no longer check anything on my previous machine. It's also unclear how I would update uproot as I'm pretty sure this is the latest version Commented Aug 12, 2022 at 15:05
  • I confirmed that I'm currently using uproot 4.3.4 Commented Aug 12, 2022 at 16:07

1 Answer 1

1

Problem Resolved! The issue was the histogram name from my root file has a : in it, which is treated differently between uproot3 and 4. So to use the old way I can just pip install uproot3 and import that instead, but I'm probably going to simply change my histogram names to be able to use the more updated version.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.