

.. _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::

    -----------------------[Blob       0]-----------------------
    -----------------------[Blob     100]-----------------------
    -----------------------[Blob     200]-----------------------
    -----------------------[Blob     300]-----------------------
    -----------------------[Blob     400]-----------------------
    ============================================================
    ============================================================
    501 cycles drained in 7.18s (CPU 6.16s). Memory peak: 299.02 MB
    Statistics are based on the last 500 cycles.
      wall  mean: 0.01s  medi: 0.01s  min: 0.01s  max: 0.22s  std: 0.01s
      CPU   mean: 0.01s  medi: 0.01s  min: 0.01s  max: 0.17s  std: 0.01s




|


.. 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 import StatusBar
    from km3pipe.tools import pld3
    import km3pipe.style

    km3pipe.style.use('default')


    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 blob['McTracks'] if t.type == 5]
        blob["Muons"] = kp.dataclasses.TrackSeries(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  13.700 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 <http://sphinx-gallery.readthedocs.io>`_
