我已经通过另一个程序用直方图制作了根文件,我正在尝试使用Python3在木星笔记本中提取这个直方图,以便对这些直方图进行各种分析。
在过去,这不是一个问题,但我在一个新的设备和事情一直不稳定。
我有一个名为root_file.root的根文件位于/location,在这个根文件中有许多TDirectories。如果数字很重要,我试图访问的直方图是5层TDirectories,我将将其标记为tdir,直方图为root_hist
我的进口品:
import numpy as np
import matplotlib.pyplot as plt
import math
import pandas as pd
import uproot打开这个我一直在使用的文件
file = uproot.open("/location/root_file.root")然后,我可以使用file.keys()查看根文件中的所有各种直方图和TDirectories。我可以通过file['tdir'].keys()看到想要访问的直方图,其中一个键是'root_hist‘。我甚至可以运行file['tdir'].classnames()并看到'root_hist‘是一个TH2F。
问题是,当我试图实际访问柱状图时--认为是hist = file['tdir']['root_hist'] --我会得到一个递归错误(请注意,test[key0]与file['tdir']['root_hist'] ( test=file['tdir']和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我不明白自己在这里做任何递归,但这是一个一致的错误,我得到。我尝试过提高递归的限制,但最终在解决问题之前破坏了我的内核。我找不到任何文档让我相信,我试图以任何方式访问柱状图,而不是预期的方法
我哪里出问题了?
发布于 2022-08-12 17:47:35
问题解决了!问题是我的根文件中的柱状图名称中有一个:,在uproot3和4之间有不同的处理方式。所以,要使用旧的方法,我可以只使用pip install uproot3并导入它,但是我可能会简单地更改我的直方图名称,以便能够使用更更新的版本。
https://stackoverflow.com/questions/73324950
复制相似问题