我正在尝试在TH1D中读取uproot4对象的一个分支。可以使用以下方法创建示例根文件:
TFile * f = new TFile("new.root","RECREATE");
TTree * t = new TTree("mytree","mytree");
t->SetMakeClass(1); //See note later
TH1D * histo;
t->Branch("myhisto","TH1D",&histo);
for(int i=0;i<100;i++){
t->GetEntry(i);
histo = new TH1D(Form("histo_%d",i),Form("histo_%d",i),100,0,100);
histo->Fill(i);
t->Fill();
}
t->Print();
t->Write();连根拔起:
Python 3.8.6 (default, Jan 27 2021, 15:42:20)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.17.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import uproot
In [2]: uproot.__version__
Out[2]: '4.0.5'
In [3]: uproot.open("new.root:mytree/myhisto")
Out[3]: <TBranchElement 'myhisto' at 0x7f91da583e50>
In [4]: uproot.open("new.root:mytree/myhisto").interpretation
Out[4]: AsObjects(Model_TH1D)但是,当我试图读取数组时,它会通过长时间的回溯失败。最后一次电话是:
~/.local/lib/python3.8/site-packages/uproot/model.py in read(cls, chunk, cursor, context, file, selffile, parent, concrete)
798 )
799
--> 800 self.read_members(chunk, cursor, context, file)
801
802 self.hook_after_read_members(
~/.local/lib/python3.8/site-packages/uproot/models/TArray.py in read_members(self, chunk, cursor, context, file)
41 )
42 self._members["fN"] = cursor.field(chunk, _tarray_format1, context)
---> 43 self._data = cursor.array(chunk, self._members["fN"], self.dtype, context)
44
45 def __array__(self, *args, **kwargs):
~/.local/lib/python3.8/site-packages/uproot/source/cursor.py in array(self, chunk, length, dtype, context, move)
308 if move:
309 self._index = stop
--> 310 return numpy.frombuffer(chunk.get(start, stop, self, context), dtype=dtype)
311
312 _u1 = numpy.dtype("u1")
~/.local/lib/python3.8/site-packages/uproot/source/chunk.py in get(self, start, stop, cursor, context)
366
367 else:
--> 368 raise uproot.deserialization.DeserializationError(
369 """attempting to get bytes {0}:{1}
370 outside expected range {2}:{3} for this Chunk""".format(
DeserializationError: while reading
TH1D version 8 as uproot.dynamic.Model_TH1D_v3 (514 bytes)
TH1 version 1 as uproot.dynamic.Model_TH1_v8 (18 bytes)
(base): <TNamed '' at 0x7f91da38d430>
(base): <TAttLine (version 2) at 0x7f91da38d700>
(base): <TAttFill (version 2) at 0x7f91da38da30>
(base): <TAttMarker (version 2) at 0x7f91da38dd90>
fNcells: 0
TAxis version 2 as uproot.dynamic.Model_TAxis_v10 (12 bytes)
(base): <TNamed '' title='\x00\x00' at 0x7f91da398910>
(base): <TAttAxis (version 4) at 0x7f91da398bb0>
fNbins: 81920
fXmin: 8.34406940932277e-309
fXmax: 2.0000190735445362
TArrayD version None as uproot.models.TArray.Model_TArrayD (? bytes)
fN: 81792
TH1D version 8 as uproot.dynamic.Model_TH1D_v3 (514 bytes)
TH1 version 1 as uproot.dynamic.Model_TH1_v8 (18 bytes)
(base): <TNamed '' at 0x7f91da495850>
(base): <TAttLine (version 2) at 0x7f91da398970>
(base): <TAttFill (version 2) at 0x7f91da48cdc0>
(base): <TAttMarker (version 2) at 0x7f91da3773d0>
fNcells: 0
TAxis version 2 as uproot.dynamic.Model_TAxis_v10 (12 bytes)
(base): <TNamed '' title='\x00\x00' at 0x7f91da3779d0>
(base): <TAttAxis (version 4) at 0x7f91da377d30>
fNbins: 81920
fXmin: 8.34406940932277e-309
fXmax: 2.0000190735445362
TArrayD version None as uproot.models.TArray.Model_TArrayD (? bytes)
fN: 81792
attempting to get bytes 58:654394
outside expected range 0:542 for this Chunk
in file new.root
in object /mytree;1如果我在创建文件时设置了SetMakeClass(0),那么读取失败的原因是:
~/.local/lib/python3.8/site-packages/uproot/model.py in read(cls, chunk, cursor, context, file, selffile, parent, concrete)
798 )
799
--> 800 self.read_members(chunk, cursor, context, file)
801
802 self.hook_after_read_members(
<dynamic> in read_members(self, chunk, cursor, context, file)
NotImplementedError: memberwise serialization of Model_TAxis_v10
in file new.root用根6.22/06和5.34/21进行测试,分别用python 2.7.18和3.8.6解释器连根4.0.5和4.0.6。我做错了什么吗?
发布于 2021-03-05 19:33:37
您并没有做错什么:这是一个NotImplementedError,因为按成员顺序序列化还没有在链接中实现。这是第38期,它最近得到了很多关注。
其他人在数年后发现了这个问题:检查第38号问题是否已经解决。
发布于 2021-03-06 14:37:35
请检查吉姆的答案,并按照其中的链接,看看第38期的连根拔起是否是固定的。
以下不是一个解决方案,而是一个解决办法。如果您可以访问根,您可以从TH1D分支中检索bin边和内容,并将它们保存为TArrayD的两个单独分支,这些分支可以通过TArrayD读取。这样做的一个示例宏是(引用原始问题中的变量名称):
void dumpTH1Array(){
//See the original question for the content of new.root
TFile * f = new TFile("new.root","UPDATE");
TTree * t = (TTree*)f->Get("mytree");
//Branch to read TH1D
TH1D * histo = 0;
TBranch * b_histo = 0;
t->SetBranchAddress("myhisto",&histo,&b_histo);
//Create new branches of TArrayD objects.
TArrayD * hx = new TArrayD();
TArrayD * hy = new TArrayD();
TBranch * b_hx = t->Branch("myhisto_x","TArrayD",&hx);
TBranch * b_hy = t->Branch("myhisto_y","TArrayD",&hy);
UInt_t nentries = t->GetEntries();
for(UInt_t i = 0; i<nentries; ++i){
//Get the stuff
Long64_t localEntry = t->LoadTree(i);
b_histo->GetEntry(localEntry);
b_hx->GetEntry(localEntry);
b_hy->GetEntry(localEntry);
//nbins includes the under- and overflow bins,
//so it is actually the user defined nbins+2.
UInt_t nbins = histo->GetSize();
//We suppose that the TH1D has fixed binning
//so histo->GetXaxis()->GetXbins() would just
//return a null pointer. We rebuild the edges
//array.
Double_t * binedges = new Double_t[nbins-1];
TAxis * xaxis = histo->GetXaxis();
xaxis->GetLowEdge(binedges);
binedges[nbins-2]=xaxis->GetBinUpEdge(nbins-2);
//Set them.
hx = new TArrayD(nbins-1,binedges);
hy = new TArrayD(nbins,histo->GetArray());
//Fill them back.
b_hx->Fill();
b_hy->Fill();
}
t->Write();
f->Close();
//Goodbye
}您将得到两个新分支,"myhisto_x“和"myhisto_y",包括按升序排列的桶边(大小:用户定义的桶+1)及其内容(大小:用户定义的桶+2,包括底流和溢出箱)。这些可以很容易地阅读连根拔起。
https://stackoverflow.com/questions/66497060
复制相似问题