阅读TH1Ds的一个分支

2024-05-15 10:24:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图阅读《根除》中的一个分支。可以使用以下方法创建示例根文件:

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

使用ROOT 6.22/06和5.34/21进行测试,使用python 2.7.18和3.8.6解释器根除4.0.5和4.0.6。我做错什么了吗


Tags: inselfnewreadbasemodelbytesversion
2条回答

您没有做错什么:这是一个NotImplementedError,因为memberwise序列化尚未在Outlot中实现。这是Issue #38,最近受到了很多关注

多年后,其他人发现了这个问题:检查38号问题是否已经解决

请检查Jim的答案并按照其中的链接查看《根除》的Issue #38是否已修复

以下不是解决方案,而是一种变通方法。如果您有权访问ROOT,则可以从TH1D分支检索bin边缘和内容,并将它们保存为TArrayD的两个单独分支,可以通过rupot读取。执行此操作的示例宏是(参考原始问题中的变量名称):

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,包括下溢存储箱)。这些可以很容易地在连根拔起阅读

相关问题 更多 >