I am trying to read a branch of TH1D objects in uproot4. A sample rootfile can be created with:
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();
In uproot:
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)
However, when I try to read the array, it fails with a long Traceback. The last calls are:
~/.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
If I set SetMakeClass(0); at the creation of the file, the read fails instead with:
~/.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
Tested with ROOT 6.22/06 and also 5.34/21, uproot 4.0.5 and 4.0.6, using both python 2.7.18 and 3.8.6 interpreters. Am I doing something wrong?
You're not doing something wrong: it's a NotImplementedError because memberwise serialization has not been implemented in Uproot. That's Issue #38, which has been getting a lot of attention recently.
Other people finding this question, years later: check to see if Issue #38 has been resolved.