从我的.root文件中从TProfile对象加载y值时遇到问题。
似乎使用file.pandas()只加载x值、计数和方差,而不加载特定的y值。
我也尝试过file.values,它返回计数,但不返回y值
发布于 2019-10-24 13:28:44
从根文件中读取的对象只有在“方法”包含在uproot method中时才有Pythonic式的解释。TProfile
没有以这种方式处理--唯一能为您工作的原因是因为TProfile
是TH1
的子类,而TH1
有一个.pandas()
方法。由于TH1
用于一维直方图,因此它不包含处理第二维的代码。
uproot methods是由用户贡献的。如果您需要TProfile
方法,可以在此处添加,并提交拉取请求:
https://github.com/scikit-hep/uproot-methods/tree/master/uproot_methods/classes
如果您在这里添加一个包含Methods
类的TProfile
模块,uproot将自动加载它,并将它应用于它从根文件加载的TProfile
对象。
对于任何类,甚至是我们没有听说过的类,uproot都会将所有私有成员数据加载到一个Python对象中。您将在以_f
开头的属性中找到所有的TProfile
数据,比如_fSumw2
和_fBinEntries
。这些方法只是使用这些“内部”属性来呈现更加用户友好的数据图片,比如Pandas。
下面是一个从未解释的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])
这是这个TProfile
的y值。这些值上的错误将留给读者作为练习。
发布于 2020-07-11 22:59:21
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成员.fArray
在C++ TProfile对象中称为.values
。
https://stackoverflow.com/questions/58541426
复制相似问题