Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
km3io
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
km3py
km3io
Commits
bd53990f
Commit
bd53990f
authored
4 years ago
by
Tamas Gal
Browse files
Options
Downloads
Patches
Plain Diff
Introduce the Branch class to wrap awkward.Arrays
parent
a56ed3af
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!47
Resolve "uproot4 integration"
Pipeline
#16318
failed
4 years ago
Stage: test
Stage: coverage
Stage: doc
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
km3io/rootio.py
+54
-8
54 additions, 8 deletions
km3io/rootio.py
km3io/tools.py
+5
-14
5 additions, 14 deletions
km3io/tools.py
tests/test_offline.py
+1
-1
1 addition, 1 deletion
tests/test_offline.py
with
60 additions
and
23 deletions
km3io/rootio.py
+
54
−
8
View file @
bd53990f
...
@@ -143,10 +143,8 @@ class EventReader:
...
@@ -143,10 +143,8 @@ class EventReader:
keys
=
self
.
keys
(),
keys
=
self
.
keys
(),
event_ctor
=
self
.
_event_ctor
,
event_ctor
=
self
.
_event_ctor
,
)
)
# group counts, for e.g. n_events, n_hits etc.
if
isinstance
(
key
,
str
)
and
key
.
startswith
(
if
isinstance
(
key
,
str
)
and
key
.
startswith
(
"
n_
"
):
"
n_
"
):
# group counts, for e.g. n_events, n_hits etc.
key
=
self
.
_keyfor
(
key
.
split
(
"
n_
"
)[
1
])
key
=
self
.
_keyfor
(
key
.
split
(
"
n_
"
)[
1
])
arr
=
self
.
_fobj
[
self
.
event_path
][
key
].
array
(
uproot
.
AsDtype
(
"
>i4
"
))
arr
=
self
.
_fobj
[
self
.
event_path
][
key
].
array
(
uproot
.
AsDtype
(
"
>i4
"
))
return
unfold_indices
(
arr
,
self
.
_index_chain
)
return
unfold_indices
(
arr
,
self
.
_index_chain
)
...
@@ -163,11 +161,10 @@ class EventReader:
...
@@ -163,11 +161,10 @@ class EventReader:
if
from_field
in
branch
[
key
].
keys
():
if
from_field
in
branch
[
key
].
keys
():
fields
.
append
(
to_field
)
fields
.
append
(
to_field
)
log
.
debug
(
fields
)
log
.
debug
(
fields
)
out
=
branch
[
key
].
arrays
(
fields
,
aliases
=
self
.
special_branches
[
key
])
# out = branch[key].arrays(fields, aliases=self.special_branches[key])
return
Branch
(
branch
[
key
],
fields
,
self
.
special_branches
[
key
],
self
.
_index_chain
)
else
:
else
:
out
=
branch
[
self
.
aliases
.
get
(
key
,
key
)].
array
()
return
unfold_indices
(
branch
[
self
.
aliases
.
get
(
key
,
key
)].
array
(),
self
.
_index_chain
)
return
unfold_indices
(
out
,
self
.
_index_chain
)
def
__iter__
(
self
):
def
__iter__
(
self
):
self
.
_events
=
self
.
_event_generator
()
self
.
_events
=
self
.
_event_generator
()
...
@@ -269,3 +266,52 @@ class EventReader:
...
@@ -269,3 +266,52 @@ class EventReader:
def
__exit__
(
self
,
*
args
):
def
__exit__
(
self
,
*
args
):
self
.
close
()
self
.
close
()
class
Branch
:
"""
Helper class for nested branches likes tracks/hits
"""
def
__init__
(
self
,
branch
,
fields
,
aliases
,
index_chain
):
self
.
_branch
=
branch
self
.
fields
=
fields
self
.
_aliases
=
aliases
self
.
_index_chain
=
index_chain
def
__getattr__
(
self
,
attr
):
if
attr
not
in
self
.
_aliases
:
raise
AttributeError
(
f
"
No field named
{
attr
}
. Available fields:
{
self
.
fields
}
"
)
return
unfold_indices
(
self
.
_branch
[
self
.
_aliases
[
attr
]].
array
(),
self
.
_index_chain
)
def
__getitem__
(
self
,
key
):
return
self
.
__class__
(
self
.
_branch
,
self
.
fields
,
self
.
_aliases
,
self
.
_index_chain
+
[
key
])
def
__len__
(
self
):
if
not
self
.
_index_chain
:
return
self
.
_branch
.
num_entries
elif
isinstance
(
self
.
_index_chain
[
-
1
],
(
int
,
np
.
int32
,
np
.
int64
)):
if
len
(
self
.
_index_chain
)
==
1
:
return
1
# try:
# return len(self[:])
# except IndexError:
# return 1
return
1
else
:
# ignore the usual index magic and access `id` directly
return
len
(
self
.
id
)
def
__actual_len__
(
self
):
"""
The raw number of events without any indexing/slicing magic
"""
return
len
(
self
.
_branch
[
self
.
_aliases
[
"
id
"
]].
array
())
def
__repr__
(
self
):
length
=
len
(
self
)
actual_length
=
self
.
__actual_len__
()
return
f
"
{
self
.
__class__
.
__name__
}
(
{
length
}{
'
/
'
+
str
(
actual_length
)
if
length
<
actual_length
else
''
}
{
self
.
_branch
.
name
}
)
"
@property
def
ndim
(
self
):
if
not
self
.
_index_chain
:
return
2
elif
any
(
isinstance
(
i
,
(
int
,
np
.
int32
,
np
.
int64
))
for
i
in
self
.
_index_chain
):
return
1
return
2
This diff is collapsed.
Click to expand it.
km3io/tools.py
+
5
−
14
View file @
bd53990f
...
@@ -209,12 +209,7 @@ def get_multiplicity(tracks, rec_stages):
...
@@ -209,12 +209,7 @@ def get_multiplicity(tracks, rec_stages):
"""
"""
masked_tracks
=
tracks
[
mask
(
tracks
.
rec_stages
,
sequence
=
rec_stages
)]
masked_tracks
=
tracks
[
mask
(
tracks
.
rec_stages
,
sequence
=
rec_stages
)]
try
:
out
=
count_nested
(
masked_tracks
.
rec_stages
,
axis
=
tracks
.
ndim
-
1
)
axis
=
tracks
.
ndim
except
AttributeError
:
axis
=
0
out
=
count_nested
(
masked_tracks
.
rec_stages
,
axis
=
axis
)
return
out
return
out
...
@@ -269,10 +264,8 @@ def best_track(tracks, startend=None, minmax=None, stages=None):
...
@@ -269,10 +264,8 @@ def best_track(tracks, startend=None, minmax=None, stages=None):
if
minmax
is
not
None
:
if
minmax
is
not
None
:
m1
=
mask
(
tracks
.
rec_stages
,
minmax
=
minmax
)
m1
=
mask
(
tracks
.
rec_stages
,
minmax
=
minmax
)
try
:
original_ndim
=
tracks
.
ndim
axis
=
tracks
.
ndim
axis
=
1
if
original_ndim
==
2
else
0
except
AttributeError
:
axis
=
0
tracks
=
tracks
[
m1
]
tracks
=
tracks
[
m1
]
...
@@ -284,10 +277,8 @@ def best_track(tracks, startend=None, minmax=None, stages=None):
...
@@ -284,10 +277,8 @@ def best_track(tracks, startend=None, minmax=None, stages=None):
m3
=
ak
.
argmax
(
tracks
.
lik
,
axis
=
axis
,
keepdims
=
True
)
m3
=
ak
.
argmax
(
tracks
.
lik
,
axis
=
axis
,
keepdims
=
True
)
out
=
tracks
[
m3
]
out
=
tracks
[
m3
]
if
isinstance
(
out
,
ak
.
highlevel
.
Record
):
if
original_ndim
==
1
:
return
namedtuple
(
"
BestTrack
"
,
out
.
fields
)(
return
out
[
0
]
*
[
getattr
(
out
,
a
)[
0
]
for
a
in
out
.
fields
]
)
return
out
[:,
0
]
return
out
[:,
0
]
...
...
This diff is collapsed.
Click to expand it.
tests/test_offline.py
+
1
−
1
View file @
bd53990f
...
@@ -381,7 +381,7 @@ class TestOfflineTracks(unittest.TestCase):
...
@@ -381,7 +381,7 @@ class TestOfflineTracks(unittest.TestCase):
)
)
def
test_repr
(
self
):
def
test_repr
(
self
):
assert
"
10
*
"
in
repr
(
self
.
tracks
)
assert
"
10
"
in
repr
(
self
.
tracks
)
def
test_slicing
(
self
):
def
test_slicing
(
self
):
tracks
=
self
.
tracks
tracks
=
self
.
tracks
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment