1

我无法从 .root 文件中的 TProfile 对象加载 y 值。

似乎使用 file.pandas() 仅加载 x 值、计数和方差,但不加载特定的 y 值。

我也尝试过 file.values,它返回计数,但不返回 y 值

4

2 回答 2

0

仅当“方法”已包含在 uproot-methods 中时,从 ROOT 文件读取的对象才具有 Pythonic 解释。TProfile没有以这种方式处理 - 任何东西都适合你的唯一原因是因为TProfile它是 , 的子类TH1并且TH1有一个.pandas()方法。由于TH1用于一维直方图,因此它不包括处理第二维的代码。

uproot-methods 是用户贡献的。如果您需要TProfile方法,可以在此处添加一个并提交拉取请求:

https://github.com/scikit-hep/uproot-methods/tree/master/uproot_methods/classes

如果您在此处添加TProfile包含Methods类的模块,uproot 将自动加载它并将其应用于它TProfile从 ROOT 文件加载的对象。

对于任何类,即使是我们没有听说过的类,uproot 都会将所有私有成员数据加载到 Python 对象中。您会TProfile在以 开头的属性中找到所有数据_f,例如_fSumw2_fBinEntries。这些方法只是使用这些“内部”属性来呈现更用户友好的数据图片,例如 Pandas。

这是从 uninterpreted 获取数据的示例TProfile

>>> import numpy
>>> import uproot
>>> 
>>> file = uproot.open("http://scikit-hep.org/uproot/examples/hepdata-example.root")
>>> hprof = file["hprof"]
>>> 
>>> numpy.sqrt(numpy.array(hprof._fSumw2) / numpy.array(hprof._fBinEntries))

将打印

-c:1: RuntimeWarning: invalid value encountered in true_divide
array([18.00160401, 17.08522023, 16.98982401, 15.18948269, 13.73788834,
       13.37976188, 13.55597897, 12.66036748, 12.68400763, 11.86598111,
       11.69668773, 11.64276616, 10.08540753, 10.15367219,  9.76541727,
        8.84047502,  8.73065944,  8.24346727,  7.46907584,  7.6620407 ,
        7.09749961,  6.62763951,  6.42199904,  5.98234406,  5.78193984,
        5.14476043,  5.15432392,  4.715674  ,  4.50246509,  4.36212988,
        3.86741244,  3.80635409,  3.60625837,  3.29332404,  3.04594764,
        2.98292569,  2.74283755,  2.50385849,  2.50978034,  2.30446027,
        2.29145554,  2.18459822,  1.99270465,  1.92651016,  1.86253428,
        1.79821825,  1.807856  ,  1.80803939,  1.75249321,  1.81588997,
        1.8226282 ,  1.74867267,  1.79842962,  1.75790778,  1.70895758,
        1.859834  ,  1.82793384,  1.96163584,  1.81903189,  2.0223054 ,
        2.14521968,  2.24565426,  2.24184203,  2.48460961,  2.63208939,
        2.69851588,  2.98989799,  3.1141892 ,  3.25304525,  3.46517157,
        3.53320406,  3.98586013,  4.25062225,  4.57520817,  4.75338005,
        5.18494724,  5.387066  ,  5.6277353 ,  5.8802666 ,  6.34427442,
        6.51966721,  7.24680462,  7.33759813,  7.63198011,  8.34535604,
        9.30064575,  8.82698396,  9.4099613 ,  9.60905376, 10.31570735,
       11.17540473, 11.13947421, 12.78232904, 12.1993165 , 12.39763587,
       16.68535354, 13.30451531, 14.67711301, 14.96741772,         nan,
       18.32199478, 17.84275258])

这是 this 的 y 值TProfile。这些值的错误留给读者练习。

于 2019-10-24T13:28:44.173 回答
0

TProfile y 值 ( h) 和默认错误 ( err) 可以使用私有成员数据重建,根据

import numpy
import uproot 

file = uproot.open("somefile_w_TProfile.root")
hprof = file['hprof_title']

cont = numpy.array(hprof.values)
sumw = numpy.array(hprof._fBinEntries)[1:-1]
err2 = numpy.array(hprof._fSumw2)[1:-1]

h = cont/sumw
s = numpy.sqrt(err2/sumw - h**2)
err = s/numpy.sqrt(sumw)

注意 C++ TProfile 成员在根 TProfile 对象中.fArray被调用。.values

于 2020-07-11T22:59:20.800 回答