

.. _sphx_glr_auto_examples_plot_dom_hits.py:


==================
DOM hits.
==================

Estimate track/DOM distances using the number of hits per DOM.




.. image:: /auto_examples/images/sphx_glr_plot_dom_hits_001.png
    :align: center


.. rst-class:: sphx-glr-script-out

 Out::

    Loading style definitions from '/home/moritz/pkg/km3pipe/km3pipe/kp-data/stylelib/km3pipe.mplstyle'
    Pipeline and module initialisation took 0.009s (CPU 0.009s).
    --------------------------[ Blob     100 ]---------------------------
    --------------------------[ Blob     200 ]---------------------------
    --------------------------[ Blob     300 ]---------------------------
    --------------------------[ Blob     400 ]---------------------------
    --------------------------[ Blob     500 ]---------------------------
    ================================[ . ]================================
    ============================================================
    500 cycles drained in 3.53s (CPU 3.53s). Memory peak: 288.28 MB
      wall  mean: 0.01s  medi: 0.01s  min: 0.00s  max: 0.10s  std: 0.00s
      CPU   mean: 0.01s  medi: 0.01s  min: 0.00s  max: 0.10s  std: 0.00s




|


.. code-block:: python


    # Author: Tamas Gal <tgal@km3net.de>
    # License: BSD-3

    from collections import defaultdict, Counter

    import km3pipe as kp
    import pandas as pd

    import matplotlib.pyplot as plt
    from matplotlib.colors import LogNorm

    from km3modules.common import StatusBar
    from km3pipe.math import pld3
    import km3pipe.style
    km3pipe.style.use("km3pipe")


    filename = "data/km3net_jul13_90m_muatm50T655.km3_v5r1.JTE_r2356.root.0-499.h5"
    geo = kp.Geometry(filename="data/km3net_jul13_90m_r1494_corrected.detx")


    def filter_muons(blob):
        """Write all muons from McTracks to Muons."""
        tracks = blob['McTracks']
        muons = [t for t in tracks if t.type == 5]
        blob["Muons"] = kp.dataclasses.McTrackSeries(muons, tracks.event_id)
        return blob


    class DOMHits(kp.Module):
        """Create histogram with n_hits and distance of hit to track."""
        def configure(self):
            self.hit_statistics = defaultdict(list)

        def process(self, blob):
            hits = blob['Hits']
            muons = blob['Muons']

            highest_energetic_muon = max(muons, key=lambda x: x.energy)
            muon = highest_energetic_muon

            triggered_hits = hits.triggered_hits
            dom_hits = Counter(triggered_hits.dom_id)
            for dom_id, n_hits in dom_hits.items():
                distance = pld3(geo.detector.dom_positions[dom_id],
                                muon.pos,
                                muon.dir)
                self.hit_statistics['n_hits'].append(n_hits)
                self.hit_statistics['distance'].append(distance)
            return blob

        def finish(self):
            df = pd.DataFrame(self.hit_statistics)
            sdf = df[(df['distance'] < 200) & (df['n_hits'] < 50)]
            plt.hist2d(sdf['distance'], sdf['n_hits'], cmap='plasma',
                       bins=(max(sdf['distance']) - 1, max(sdf['n_hits']) - 1),
                       norm=LogNorm())
            plt.xlabel('Distance between hit and muon track [m]')
            plt.ylabel('Number of hits on DOM')
            plt.show()


    pipe = kp.Pipeline()
    pipe.attach(kp.io.HDF5Pump, filename=filename)
    pipe.attach(StatusBar, every=100)
    pipe.attach(filter_muons)
    pipe.attach(DOMHits)
    pipe.drain()

**Total running time of the script:** ( 0 minutes  5.753 seconds)



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

     :download:`Download Python source code: plot_dom_hits.py <plot_dom_hits.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: plot_dom_hits.ipynb <plot_dom_hits.ipynb>`

.. rst-class:: sphx-glr-signature

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
