.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_calibrating_hits.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_calibrating_hits.py: ================== Calibrating Hits ================== Hits stored in ROOT and HDF5 files are usually not calibrated, which means that they have invalid positions, directions and uncorrected hit times. This example shows how to assign the PMT position and direction to each hit and applying a time correction to them. The KM3NeT offline format (derived from aanet) uses a single class for every hit type (regular hits, MC hits and their correspeonding calibrated and uncalibrated counter parts). In ROOT files, the actual struct/class definition is stored along the data, which means that all the attributes are accessible even when they are invalid or not instantiated. Positions and directions are also part of these attributes and they are initialised to `(0, 0, 0)`. The `km3pipe.calib.Calibration()` class can be used to load a calibration and the `.apply()` method to update the position, direction and correct the arrival time of hit. .. GENERATED FROM PYTHON SOURCE LINES 26-34 .. code-block:: Python # Author: Tamas Gal # License: BSD-3 import km3pipe as kp import km3io from km3net_testdata import data_path .. GENERATED FROM PYTHON SOURCE LINES 35-36 The `offline/km3net_offline.root` contains 10 events with hit information: .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python f = km3io.OfflineReader(data_path("offline/km3net_offline.root")) .. GENERATED FROM PYTHON SOURCE LINES 40-41 The corresponding calibration file is stored in `detx/km3net_offline.detx`: .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: Python calib = kp.calib.Calibration(filename=data_path("detx/km3net_offline.detx")) .. rst-class:: sphx-glr-script-out .. code-block:: none Detector: Parsing the DETX header Detector: Reading PMT information... Detector: Done. .. GENERATED FROM PYTHON SOURCE LINES 45-46 Let's grab the hits of the event with index `5`: .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python hits = f.events[5].hits .. GENERATED FROM PYTHON SOURCE LINES 50-54 The positions (pos_x, pos_y, pos_z) and directions (dir_x, dir_y, dir_z) are not available for uncalibrated hits, in contrast to aanet where each field is present and initialised to some magic value (e.g. 0). km3io hides all those fields and do not occupy additional memory for those. .. GENERATED FROM PYTHON SOURCE LINES 56-57 Here are the uncalibrated times: .. GENERATED FROM PYTHON SOURCE LINES 57-63 .. code-block:: Python n = 7 # just an arbitrary number to limit the output uncalibrated_times = hits.t print(uncalibrated_times[:n]) .. rst-class:: sphx-glr-script-out .. code-block:: none [8.19e+07, 8.19e+07, 8.19e+07, 8.19e+07, 8.19e+07, 8.19e+07, 8.19e+07] .. GENERATED FROM PYTHON SOURCE LINES 64-67 To calibrate the hits, use the `calib.apply()` method which will create a `km3pipe.Table`, retrieve the positions and directions of the corresponding PMTs, apply the time calibration and also do the PMT time slew correction. .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python calibrated_hits = calib.apply(hits) .. GENERATED FROM PYTHON SOURCE LINES 71-73 The calibrated hits are stored in a `kp.Table` which is a thin wrapper around a `numpy.record` array (a simple numpy array with named attributes): .. GENERATED FROM PYTHON SOURCE LINES 73-77 .. code-block:: Python print(calibrated_hits.dtype) .. rst-class:: sphx-glr-script-out .. code-block:: none (numpy.record, [('channel_id', '` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_calibrating_hits.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_