# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, print_function

from icecube import icetray, dataclasses, clsim


class RemoveLatePhotons(icetray.I3Module):
    """
    Remove very late photons from the photon series generated by clsim

    """
    def __init__(self, context):
        icetray.I3Module.__init__(self, context)
        self.AddOutBox('OutBox')

        self.AddParameter('InputPhotonSeries',
                          'Name of the photon map',
                          'UnweightedPhotons')
        self.AddParameter('TimeLimit',
                          'Time after which the photons should be discarded',
                          1E5) # In nanoseconds

    def Configure(self):
        self.pmap_in = self.GetParameter('InputPhotonSeries')
        self.time_limit = self.GetParameter('TimeLimit')
        self.photons_dropped = 0

    def DAQ(self, frame):
        latePhotons = False

        # Check if there are any late photons, raise flag if there are
        for domkey, pseries in frame[self.pmap_in]:
            for photon in pseries:
                if photon.GetTime() > self.time_limit:
                    latePhotons = True
                    break

        # If there are NO late photons, just return the frame without touching it
        if not latePhotons:
            self.PushFrame(frame)
            return True

        # If there were late photons, remove them
        photonTuples = []
        for domkey, pseries in frame[self.pmap_in]:
            newPhotonList = []
            for photon in pseries:
                if photon.GetTime() <= self.time_limit:
                    newPhotonList.append(photon)
                else:
                    self.photons_dropped += 1 # Keeping track how many photons I discard
            # Finished checking the photons in the DOM, pass them to the new list
            if newPhotonList != []:
                photonTuples.append((domkey, newPhotonList))
        frame.Delete(self.pmap_in)
        frame[self.pmap_in] = clsim.I3PhotonSeriesMap(photonTuples)
        return True

    def Finish(self):
        # Inform how many photons were dropped in this file
        print('RemoveLatePhotons:', self.photons_dropped, ' photons removed from this file.')

# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, print_function

from icecube import icetray, dataclasses, clsim


class RemoveLatePhotons(icetray.I3Module):
    """
    Remove very late photons from the photon series generated by clsim

    """
    def __init__(self, context):
        icetray.I3Module.__init__(self, context)
        self.AddOutBox('OutBox')

        self.AddParameter('InputPhotonSeries',
                          'Name of the photon map',
                          'UnweightedPhotons')
        self.AddParameter('TimeLimit',
                          'Time after which the photons should be discarded',
                          1E5) # In nanoseconds

    def Configure(self):
        self.pmap_in = self.GetParameter('InputPhotonSeries')
        self.time_limit = self.GetParameter('TimeLimit')
        self.photons_dropped = 0

    def DAQ(self, frame):
        latePhotons = False

        # Check if there are any late photons, raise flag if there are
        for domkey, pseries in frame[self.pmap_in]:
            for photon in pseries:
                if photon.GetTime() > self.time_limit:
                    latePhotons = True
                    break

        # If there are NO late photons, just return the frame without touching it
        if not latePhotons:
            self.PushFrame(frame)
            return True

        # If there were late photons, remove them
        photonTuples = []
        for domkey, pseries in frame[self.pmap_in]:
            newPhotonList = []
            for photon in pseries:
                if photon.GetTime() <= self.time_limit:
                    newPhotonList.append(photon)
                else:
                    self.photons_dropped += 1 # Keeping track how many photons I discard
            # Finished checking the photons in the DOM, pass them to the new list
            if newPhotonList != []:
                photonTuples.append((domkey, newPhotonList))
        frame.Delete(self.pmap_in)
        frame[self.pmap_in] = clsim.I3PhotonSeriesMap(photonTuples)
        return True

    def Finish(self):
        # Inform how many photons were dropped in this file
        print('RemoveLatePhotons:', self.photons_dropped, ' photons removed from this file.')

# -*- coding: utf-8 -*- Remove very late photons from the photon series generated by clsim # In nanoseconds # Check if there are any late photons, raise flag if there are # If there are NO late photons, just return the frame without touching it # If there were late photons, remove them # Keeping track how many photons I discard # Finished checking the photons in the DOM, pass them to the new list # Inform how many photons were dropped in this file