#!/usr/bin/python
#===============================================================================
# CERN (BE-CO-HT)
# Get the difference between two timetag buffer samples
#===============================================================================
# author: Theodor Stana (t.stana@cern.ch)
#
# date of creation:
#
# version: 1.0
#
# description:
#   Working together with the rdtb.py script and using an output file produced
#   by this script, this script reads out each sample and works out the
#   difference in time between two timetag samples, warning if these are lower
#   than the minimum expected time difference between two pulses, thus showing
#   which channel caused a pulse miss (PMISS) system error.
#
#===============================================================================
# GNU LESSER GENERAL PUBLIC LICENSE
#===============================================================================
# This source file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version. This source is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details. You should have
# received a copy of the GNU Lesser General Public License along with this
# source; if not, download it from http://www.gnu.org/licenses/lgpl-2.1.html
#===============================================================================
# last changes:
#===============================================================================
#  TODO: - 
#===============================================================================

if __name__ == "__main__":
    # seconds, nanoseconds
    s    = 0
    ns   = 0

    # previous seconds and nanoseconds
    prs  = 0
    prns = 0

    # FIFO sample number
    n    = 0

    # Get file name
    fname = raw_input("Input file name (with extension): ");

    # And start getting the samples and calculating the difference values
    with open(fname, 'r') as f:
        for l in f:
            t  = l.split()[3]

            ch = l.split()[1]
            if t == ':':
                t  = l.split()[4]
                ch = l.split()[2]

                #if t == ':':
                #    t = l.split()[5]
                #    if t == ':':
                #        t = l.split()[6]

            ch = int(ch, 2)

            # Get seconds and nanoseconds
            s   = int(t.split('.')[0])
            ns  = int(t.split('.')[1])

            # Calc seconds difference, adjust in case of overflow
            sd  = s  - prs
            nsd = ns - prns
            if sd < 0:
                sd = -sd
            if nsd < 0:
                nsd = -nsd

            # Print
            p = "(%s) %d.%09d" % ("{0:#08b}".format(ch)[2:], sd, nsd)
            if (sd == 0) and (nsd < 240000) and (ch & prch):
                p += " [!!!]"
            print(p)
            prch = ch
            prs  = s
            prns = ns
            n   += 1

