Note
Go to the end to download the full example code
Reading Offline tracks¶
The following example shows how to access tracks data in an offline ROOT file.
Note: the offline files used here were intentionaly reduced to 10 events.
import km3io as ki
from km3net_testdata import data_path
We open the file using the
f = ki.OfflineReader(data_path("offline/numucc.root"))
To access offline tracks/mc_tracks data:
f.tracks
f.mc_tracks
<Branch [10] path='mc_trks'>
Note that no data is loaded in memory at this point, so printing tracks will only return how many sub-branches (corresponding to events) were found.
f.tracks
<Branch [10] path='trks'>
same for mc hits
f.mc_tracks
<Branch [10] path='mc_trks'>
Accessing the tracks/mc_tracks keys¶
to explore the reconstructed tracks fields:
f.tracks.fields
['id', 'pos_x', 'pos_y', 'pos_z', 'dir_x', 'dir_y', 'dir_z', 't', 'E', 'len', 'lik', 'rec_type', 'rec_stages', 'fitinf']
the same for MC tracks
f.mc_tracks.fields
['id', 'pos_x', 'pos_y', 'pos_z', 'dir_x', 'dir_y', 'dir_z', 'E', 't', 'len', 'pdgid', 'hit_ids', 'usr', 'usr_names']
Accessing tracks data¶
each field will return a nested awkward.Array and load everything into memory, so be careful if you are working with larger files.
f.tracks.E
<Array [[117, 117, 0, 0, 0, ... 0, 0, 0, 0, 0]] type='10 * var * float64'>
The z direction of all reconstructed tracks
f.tracks.dir_z
<Array [[0.213, 0.213, ... -0.158, -0.134]] type='10 * var * float64'>
The likelihoods
f.tracks.lik
<Array [[92.8, 92.8, 92.8, 92.8, ... 0, 0, 0]] type='10 * var * float64'>
To select just a single event or a subset of events, use the indices or slices. The following will access all tracks and their fields of the third event (0 is the first):
f[2].tracks
<Branch [1/10] path='trks'>
The z direction of all tracks in the third event:
f[2].tracks.dir_z
<Array [-0.982, -0.982, ... -0.0365, -0.981] type='38 * float64'>
while here, we select the first 3 events. Notice that all fields will return nested arrays, as we have seem above where all events were selected.
f[:3]
<OfflineReader [3/10] path='E/Evt'>
All tracks for the first three events
f[:3].tracks
<Branch [3/10] path='trks'>
The z directions of all tracks of the first three events
f[:3].tracks.dir_z
<Array [[0.213, 0.213, ... -0.0365, -0.981]] type='3 * var * float64'>
or events from 3 and 5 (again, 0 indexing):
f[2:5]
<OfflineReader [3/10] path='E/Evt'>
the tracks of those events
f[2:5].tracks
<Branch [3/10] path='trks'>
and just the z directions of those
f[2:5].tracks.dir_z
<Array [[-0.982, -0.982, -0.982, ... []] type='3 * var * float64'>
Total running time of the script: (0 minutes 5.351 seconds)
Estimated memory usage: 9 MB