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
Merge requests
!22
WIP: Slicing and refactoring offline
Code
Review changes
Check out branch
Download
Patches
Plain diff
Closed
WIP: Slicing and refactoring offline
37-user-parameters-seem-to-be-transposed
into
master
Overview
1
Commits
40
Pipelines
38
Changes
6
Closed
Tamas Gal
requested to merge
37-user-parameters-seem-to-be-transposed
into
master
5 years ago
Overview
1
Commits
40
Pipelines
38
Changes
2
Expand
Closes
#37 (closed)
Edited
5 years ago
by
Tamas Gal
0
0
Merge request reports
Compare
version 28
version 36
7ff91cbb
5 years ago
version 35
c58bb150
5 years ago
version 34
abb0c333
5 years ago
version 33
b35d3e40
5 years ago
version 32
7c65ea9d
5 years ago
version 31
22002cfd
5 years ago
version 30
8494b87f
5 years ago
version 29
0ca80e58
5 years ago
version 28
653ee552
5 years ago
version 27
df3e03e6
5 years ago
version 26
79d0f15d
5 years ago
version 25
a256ebf4
5 years ago
version 24
62b46e26
5 years ago
version 23
d82c3c5d
5 years ago
version 22
f07f4a22
5 years ago
version 21
56ee4262
5 years ago
version 20
ffe46ae6
5 years ago
version 19
9343b7ae
5 years ago
version 18
24894e8f
5 years ago
version 17
18a43e75
5 years ago
version 16
d8a20d0c
5 years ago
version 15
a296c0dd
5 years ago
version 14
7e6573ff
5 years ago
version 13
68eb042c
5 years ago
version 12
f62bb448
5 years ago
version 11
57e2e0f1
5 years ago
version 10
7d990efd
5 years ago
version 9
d9de9393
5 years ago
version 8
f882f362
5 years ago
version 7
148419a7
5 years ago
version 6
16de656e
5 years ago
version 5
d79116fe
5 years ago
version 4
51c4f855
5 years ago
version 3
0aaeec1e
5 years ago
version 2
37c74e26
5 years ago
version 1
9b36e9f3
5 years ago
master (base)
and
version 32
latest version
490037fa
40 commits,
5 years ago
version 36
7ff91cbb
39 commits,
5 years ago
version 35
c58bb150
38 commits,
5 years ago
version 34
abb0c333
37 commits,
5 years ago
version 33
b35d3e40
36 commits,
5 years ago
version 32
7c65ea9d
35 commits,
5 years ago
version 31
22002cfd
34 commits,
5 years ago
version 30
8494b87f
33 commits,
5 years ago
version 29
0ca80e58
32 commits,
5 years ago
version 28
653ee552
31 commits,
5 years ago
version 27
df3e03e6
30 commits,
5 years ago
version 26
79d0f15d
29 commits,
5 years ago
version 25
a256ebf4
28 commits,
5 years ago
version 24
62b46e26
27 commits,
5 years ago
version 23
d82c3c5d
26 commits,
5 years ago
version 22
f07f4a22
25 commits,
5 years ago
version 21
56ee4262
24 commits,
5 years ago
version 20
ffe46ae6
23 commits,
5 years ago
version 19
9343b7ae
22 commits,
5 years ago
version 18
24894e8f
21 commits,
5 years ago
version 17
18a43e75
19 commits,
5 years ago
version 16
d8a20d0c
18 commits,
5 years ago
version 15
a296c0dd
17 commits,
5 years ago
version 14
7e6573ff
16 commits,
5 years ago
version 13
68eb042c
15 commits,
5 years ago
version 12
f62bb448
14 commits,
5 years ago
version 11
57e2e0f1
13 commits,
5 years ago
version 10
7d990efd
12 commits,
5 years ago
version 9
d9de9393
11 commits,
5 years ago
version 8
f882f362
9 commits,
5 years ago
version 7
148419a7
8 commits,
5 years ago
version 6
16de656e
7 commits,
5 years ago
version 5
d79116fe
6 commits,
5 years ago
version 4
51c4f855
4 commits,
5 years ago
version 3
0aaeec1e
3 commits,
5 years ago
version 2
37c74e26
2 commits,
5 years ago
version 1
9b36e9f3
1 commit,
5 years ago
Show latest version
2 files
+
19
−
485
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
km3io/offline.py
+
6
−
340
Options
@@ -103,12 +103,14 @@ class OfflineReader:
@cached_property
def
events
(
self
):
"""
The `E` branch, containing all offline events.
"""
return
Branch
(
self
.
_tree
,
mapper
=
EVENTS_MAP
,
subbranchmaps
=
SUBBRANCH_MAPS
)
@cached_property
def
header
(
self
):
"""
The file header
"""
if
'
Head
'
in
self
.
_fobj
:
header
=
{}
for
n
,
x
in
self
.
_fobj
[
'
Head
'
].
_map_3c_string_2c_string_3e_
.
items
(
@@ -118,351 +120,13 @@ class OfflineReader:
else
:
warnings
.
warn
(
"
Your file header has an unsupported format
"
)
def
get_best_reco
(
self
):
"""
returns the best reconstructed track fit data. The best fit is defined
as the track fit with the maximum reconstruction stages. When
"
nan
"
is
returned, it means that the reconstruction parameter of interest is not
found. for example, in the case of muon simulations: if [1, 2] are the
reconstruction stages, then only the fit parameters corresponding to the
stages [1, 2] are found in the Offline files, the remaining fit parameters
corresponding to the stages 3, 4, 5 are all filled with nan.
Returns
-------
numpy recarray
a recarray of the best track fit data (reconstruction data).
"""
keys
=
"
,
"
.
join
(
self
.
keys
.
fit_keys
[:
-
1
])
empty_fit_info
=
np
.
array
(
[
match
for
match
in
self
.
_find_empty
(
self
.
tracks
.
fitinf
)])
fit_info
=
[
i
for
i
,
j
in
zip
(
self
.
tracks
.
fitinf
,
empty_fit_info
[:,
1
])
if
j
is
not
None
]
stages
=
self
.
_get_max_reco_stages
(
self
.
tracks
.
rec_stages
)
fit_data
=
np
.
array
([
i
[
j
]
for
i
,
j
in
zip
(
fit_info
,
stages
[:,
2
])])
rows_size
=
len
(
max
(
fit_data
,
key
=
len
))
equal_size_data
=
np
.
vstack
([
np
.
hstack
([
i
,
np
.
zeros
(
rows_size
-
len
(
i
))
+
np
.
nan
])
for
i
in
fit_data
])
return
np
.
core
.
records
.
fromarrays
(
equal_size_data
.
transpose
(),
names
=
keys
)
def
_get_max_reco_stages
(
self
,
reco_stages
):
"""
find the longest reconstructed track based on the maximum size of
reconstructed stages.
Parameters
----------
reco_stages : chunked array
chunked array of all the reconstruction stages of all tracks.
In km3io, it is accessed with
km3io.OfflineReader(my_file).tracks.rec_stages .
Returns
-------
numpy array
array with 3 columns: *list of the maximum reco_stages
*lentgh of the maximum reco_stages
*position of the maximum reco_stages
"""
empty_reco_stages
=
np
.
array
(
[
match
for
match
in
self
.
_find_empty
(
reco_stages
)])
max_reco_stages
=
np
.
array
(
[[
max
(
i
,
key
=
len
),
len
(
max
(
i
,
key
=
len
)),
i
.
index
(
max
(
i
,
key
=
len
))]
for
i
,
j
in
zip
(
reco_stages
,
empty_reco_stages
[:,
1
])
if
j
is
not
None
])
return
max_reco_stages
def
get_reco_fit
(
self
,
stages
,
mc
=
False
):
"""
construct a numpy recarray of the fit information (reconstruction
data) of the tracks reconstructed following the reconstruction stages
of interest.
Parameters
----------
stages : list
list of reconstruction stages of interest. for example
[1, 2, 3, 4, 5].
mc : bool, optional
default is False to look for fit data in the tracks tree in offline files
(not the mc tracks tree). mc=True to look for fit data from the mc tracks
tree in offline files.
Returns
-------
numpy recarray
a recarray of the fit information (reconstruction data) of
the tracks of interest.
Raises
------
ValueError
ValueError raised when the reconstruction stages of interest
are not found in the file.
"""
keys
=
"
,
"
.
join
(
self
.
keys
.
fit_keys
[:
-
1
])
if
mc
is
False
:
rec_stages
=
np
.
array
(
[
match
for
match
in
self
.
_find_rec_stages
(
stages
,
mc
=
False
)])
fitinf
=
self
.
tracks
.
fitinf
if
mc
is
True
:
rec_stages
=
np
.
array
(
[
match
for
match
in
self
.
_find_rec_stages
(
stages
,
mc
=
True
)])
fitinf
=
self
.
mc_tracks
.
fitinf
mask
=
rec_stages
[:,
1
]
!=
None
if
np
.
all
(
rec_stages
[:,
1
]
==
None
):
raise
ValueError
(
"
The stages {} are not found in your file.
"
.
format
(
str
(
stages
)))
else
:
fit_data
=
np
.
array
(
[
i
[
k
]
for
i
,
k
in
zip
(
fitinf
[
mask
],
rec_stages
[:,
1
][
mask
])])
rec_array
=
np
.
core
.
records
.
fromarrays
(
fit_data
.
transpose
(),
names
=
keys
)
return
rec_array
def
get_reco_hits
(
self
,
stages
,
keys
,
mc
=
False
):
"""
construct a dictionary of hits class data based on the reconstruction
stages of interest. For example, if the reconstruction stages of interest
are [1, 2, 3, 4, 5], then get_reco_hits method will select the hits data
from the events that were reconstructed following these stages (i.e
[1, 2, 3, 4, 5]).
Parameters
----------
stages : list
list of reconstruction stages of interest. for example
[1, 2, 3, 4, 5].
keys : list of str
list of the hits class attributes.
mc : bool, optional
default is False to look for hits data in the hits tree in offline files
(not the mc_hits tree). mc=True to look for mc hits data in the mc hits
tree in offline files.
Returns
-------
dict
dictionary of lazyarrays containing data for each hits attribute requested.
Raises
------
ValueError
ValueError raised when the reconstruction stages of interest
are not found in the file.
"""
lazy_d
=
{}
if
mc
is
False
:
rec_stages
=
np
.
array
(
[
match
for
match
in
self
.
_find_rec_stages
(
stages
,
mc
=
False
)])
hits_data
=
self
.
hits
if
mc
is
True
:
rec_stages
=
np
.
array
(
[
match
for
match
in
self
.
_find_rec_stages
(
stages
,
mc
=
True
)])
hits_data
=
self
.
mc_hits
mask
=
rec_stages
[:,
1
]
!=
None
if
np
.
all
(
rec_stages
[:,
1
]
==
None
):
raise
ValueError
(
"
The stages {} are not found in your file.
"
.
format
(
str
(
stages
)))
else
:
for
key
in
keys
:
lazy_d
[
key
]
=
getattr
(
hits_data
,
key
)[
mask
]
return
lazy_d
def
get_reco_events
(
self
,
stages
,
keys
,
mc
=
False
):
"""
construct a dictionary of events class data based on the reconstruction
stages of interest. For example, if the reconstruction stages of interest
are [1, 2, 3, 4, 5], then get_reco_events method will select the events data
that were reconstructed following these stages (i.e [1, 2, 3, 4, 5]).
Parameters
----------
stages : list
list of reconstruction stages of interest. for example
[1, 2, 3, 4, 5].
keys : list of str
list of the events class attributes.
mc : bool, optional
default is False to look for the reconstruction stages in the tracks tree
in offline files (not the mc tracks tree). mc=True to look for the reconstruction
data in the mc tracks tree in offline files.
Returns
-------
dict
dictionary of lazyarrays containing data for each events attribute requested.
Raises
------
ValueError
ValueError raised when the reconstruction stages of interest
are not found in the file.
"""
lazy_d
=
{}
if
mc
is
False
:
rec_stages
=
np
.
array
(
[
match
for
match
in
self
.
_find_rec_stages
(
stages
,
mc
=
False
)])
if
mc
is
True
:
rec_stages
=
np
.
array
(
[
match
for
match
in
self
.
_find_rec_stages
(
stages
,
mc
=
True
)])
mask
=
rec_stages
[:,
1
]
!=
None
if
np
.
all
(
rec_stages
[:,
1
]
==
None
):
raise
ValueError
(
"
The stages {} are not found in your file.
"
.
format
(
str
(
stages
)))
else
:
for
key
in
keys
:
lazy_d
[
key
]
=
getattr
(
self
.
events
,
key
)[
mask
]
return
lazy_d
def
get_reco_tracks
(
self
,
stages
,
keys
,
mc
=
False
):
"""
construct a dictionary of tracks class data based on the reconstruction
stages of interest. For example, if the reconstruction stages of interest
are [1, 2, 3, 4, 5], then get_reco_tracks method will select tracks data
from the events that were reconstructed following these stages (i.e
[1, 2, 3, 4, 5]).
Parameters
----------
stages : list
list of reconstruction stages of interest. for example
[1, 2, 3, 4, 5].
keys : list of str
list of the tracks class attributes.
mc : bool, optional
default is False to look for tracks data in the tracks tree in offline files
(not the mc tracks tree). mc=True to look for tracks data in the mc tracks
tree in offline files.
Returns
-------
dict
dictionary of lazyarrays containing data for each tracks attribute requested.
Raises
------
ValueError
ValueError raised when the reconstruction stages of interest
are not found in the file.
"""
lazy_d
=
{}
if
mc
is
False
:
rec_stages
=
np
.
array
(
[
match
for
match
in
self
.
_find_rec_stages
(
stages
,
mc
=
False
)])
tracks_data
=
self
.
tracks
if
mc
is
True
:
rec_stages
=
np
.
array
(
[
match
for
match
in
self
.
_find_rec_stages
(
stages
,
mc
=
True
)])
tracks_data
=
self
.
mc_tracks
mask
=
rec_stages
[:,
1
]
!=
None
if
np
.
all
(
rec_stages
[:,
1
]
==
None
):
raise
ValueError
(
"
The stages {} are not found in your file.
"
.
format
(
str
(
stages
)))
else
:
for
key
in
keys
:
lazy_d
[
key
]
=
np
.
array
([
i
[
k
]
for
i
,
k
in
zip
(
getattr
(
tracks_data
,
key
)[
mask
],
rec_stages
[:,
1
][
mask
])
])
return
lazy_d
def
_find_rec_stages
(
self
,
stages
,
mc
=
False
):
"""
find the index of reconstruction stages of interest in a
list of multiple reconstruction stages.
Parameters
----------
stages : list
list of reconstruction stages of interest. for example
[1, 2, 3, 4, 5].
mc : bool, optional
default is False to look for reconstruction stages in the tracks tree in
offline files (not the mc tracks tree). mc=True to look for reconstruction
stages in the mc tracks tree in offline files.
Yields
------
generator
the track id and the index of the reconstruction stages of
interest if found. If the reconstruction stages of interest
are not found, None is returned as the stages index.
"""
if
mc
is
False
:
stages_data
=
self
.
events
.
tracks
.
rec_stages
if
mc
is
True
:
stages_data
=
self
.
events
.
mc_tracks
.
rec_stages
for
trk_index
,
rec_stages
in
enumerate
(
stages_data
):
try
:
stages_index
=
rec_stages
.
index
(
stages
)
except
ValueError
:
stages_index
=
None
yield
trk_index
,
stages_index
continue
yield
trk_index
,
stages_index
def
_find_empty
(
self
,
array
):
"""
finds empty lists/arrays in an awkward array
Parameters
----------
array : awkward array
Awkward array of data of interest. For example:
km3io.OfflineReader(my_file).tracks.fitinf .
Yields
------
generator
the empty list id and the index of the empty list. When
data structure (list) is simply empty, None is written in the
corresponding index. However, when data structure (list) is not
empty and does not contain an empty list, then False is written in the
corresponding index.
"""
for
i
,
rs
in
enumerate
(
array
):
try
:
if
len
(
rs
)
==
0
:
j
=
None
if
len
(
rs
)
!=
0
:
j
=
rs
.
index
([])
except
ValueError
:
j
=
False
# rs not empty but [] not found
yield
i
,
j
continue
yield
i
,
j
class
Usr
:
"""
Helper class to access AAObject `usr`` stuff
"""
def
__init__
(
self
,
name
,
tree
,
index
=
None
):
# Here, we assume that every event has the same names in the same order
# to massively increase the performance. This needs triple check if
it's
# always the case; the usr-format is simply a very bad design.
# to massively increase the performance. This needs triple check if
#
it's
always the case; the usr-format is simply a very bad design.
self
.
_name
=
name
try
:
tree
[
'
usr
'
]
# This will raise a KeyError in old aanet files
@@ -473,6 +137,8 @@ class Usr:
basketcache
=
BASKET_CACHE
)[
0
]
]
except
(
KeyError
,
IndexError
):
# e.g. old aanet files
print
(
"
The `usr` fields could not be parsed for the
'
{}
'
branch.
"
.
format
(
name
))
self
.
_usr_names
=
[]
else
:
self
.
_usr_idx_lookup
=
{
Loading