-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start work on interpretations; read at least one basket. (#16)
* Sketch of what's needed in the new Interpretation. * Filling in the AsDtype Interpretation. * The AsDtype is probably about right. * Cleaned up. * Tricky cache bug: don't take objects from cache whose files are closed. * Using the same code to read baskets as recovery-baskets. * Placeholder for compressed baskets. * Dealing with compressed buffers and baskets with offsets. * More conveniences: context managers pass down from all objects and access objects in files with bar syntax. * Reading embedded TBaskets can't be quite the same code path as regular TBaskets. * Basket recovery works. * Avoid pejorative language 'recover'; instead use 'embedded'. * Cleaned up; done with PR. * Fix flake8.
- Loading branch information
Showing
20 changed files
with
1,392 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot4/blob/master/LICENSE | ||
|
||
from __future__ import absolute_import | ||
|
||
import sys | ||
import json | ||
|
||
try: | ||
from io import StringIO | ||
except ImportError: | ||
from StringIO import StringIO | ||
|
||
import numpy | ||
import pytest | ||
import skhep_testdata | ||
|
||
import uproot4 | ||
|
||
|
||
def test_get_key(): | ||
with uproot4.open(skhep_testdata.data_path("uproot-mc10events.root")) as f: | ||
assert f["Events"].name == "Events" | ||
assert f["Events/Info"].name == "Info" | ||
assert f["Events/Info/evtNum"].name == "evtNum" | ||
assert f["Events"]["Info/evtNum"].name == "evtNum" | ||
assert f["Events"]["/Info/evtNum"].name == "evtNum" | ||
assert f["Events/evtNum"].name == "evtNum" | ||
assert f["Events"]["evtNum"].name == "evtNum" | ||
assert f["Events"]["/Info"].name == "Info" | ||
assert f["Events"]["/Info/"].name == "Info" | ||
with pytest.raises(KeyError): | ||
f["Events"]["/evtNum"] | ||
|
||
|
||
def test_basket_data(): | ||
with uproot4.open( | ||
skhep_testdata.data_path("uproot-sample-6.20.04-uncompressed.root") | ||
) as f: | ||
assert f["sample/i4"].basket_key(3).fSeekKey == 35042 | ||
assert f["sample/i4"].basket(3).data.view(">i4").tolist() == [ | ||
6, | ||
7, | ||
8, | ||
9, | ||
10, | ||
11, | ||
12, | ||
] | ||
|
||
|
||
def test_compressed(): | ||
with uproot4.open(skhep_testdata.data_path("uproot-mc10events.root")) as f: | ||
basket = f["Events/Muon.q"].basket(0) | ||
assert basket.data.view(">i4").tolist() == [ | ||
-1, | ||
-1, | ||
-1, | ||
1, | ||
1, | ||
-1, | ||
-1, | ||
-1, | ||
1, | ||
-1, | ||
-1, | ||
-1, | ||
-1, | ||
1, | ||
] | ||
assert basket.byte_offsets.tolist() == [0, 4, 4, 16, 28, 28, 32, 52, 52, 56, 56] | ||
|
||
|
||
def test_read_all(): | ||
filename = skhep_testdata.data_path("uproot-issue327.root") | ||
with uproot4.open(filename, minimal_ttree_metadata=False) as f: | ||
f["DstTree/fTracks.fCharge"] | ||
|
||
|
||
@pytest.mark.parametrize("mini", [False, True]) | ||
def test_recovery(mini): | ||
# flat array to recover: | ||
filename = skhep_testdata.data_path("uproot-issue21.root") | ||
with uproot4.open( | ||
"file:" + filename + " | nllscan/mH", minimal_ttree_metadata=mini | ||
) as branch: | ||
basket = branch.basket(0) | ||
assert basket.data.view(">f8").tolist()[:10] == [ | ||
124.0, | ||
124.09089660644531, | ||
124.18180084228516, | ||
124.27269744873047, | ||
124.36360168457031, | ||
124.45449829101562, | ||
124.54550170898438, | ||
124.63639831542969, | ||
124.72730255126953, | ||
124.81819915771484, | ||
] | ||
assert basket.byte_offsets is None | ||
assert branch.entry_offsets == [0, branch.num_entries] | ||
|
||
# jagged arrays to recover: | ||
|
||
# uproot-issue327.root DstTree: fTracks.fCharge | ||
# uproot-issue232.root fTreeV0: V0s.fV0pt MCparticles.nbodies | ||
# uproot-issue187.root fTreeV0: V0s.fV0pt MCparticles.nbodies | ||
# uproot-from-geant4.root Details: numgood, TrackedRays: Event phi | ||
filename = skhep_testdata.data_path("uproot-issue327.root") | ||
with uproot4.open( | ||
"file:" + filename + " | DstTree/fTracks.fCharge", minimal_ttree_metadata=mini | ||
) as branch: | ||
basket = branch.basket(0) | ||
assert basket.data.view("i1")[:10].tolist() == [ | ||
1, | ||
-1, | ||
1, | ||
1, | ||
-1, | ||
-1, | ||
1, | ||
-1, | ||
-1, | ||
-1, | ||
] | ||
assert basket.byte_offsets[:10].tolist() == [ | ||
0, | ||
2, | ||
37, | ||
56, | ||
60, | ||
81, | ||
82, | ||
112, | ||
112, | ||
112, | ||
] | ||
assert branch.entry_offsets == [0, branch.num_entries] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.