from matplotlib import pyplot as plt
import numpy as np
import h5py 
import yaml
import datetime
import os
import h5py
from tqdm import tqdm
from sklearn.decomposition import PCA
import json
import time
from scipy.optimize import curve_fit
import argparse

gain = 4.522 #mV/ke-
sample_filename='packets-2023_12_06_08_15_39_PST.h5'
sample_datadir='/global/cfs/projectdirs/dune/www/data/LBL/SingleCube/Nov2023/ramp/'
filename=sample_datadir+sample_filename
output_filename = None
pedestal_filename='/global/cfs/projectdirs/dune/www/data/LBL/SingleCube/Nov2023/cold/packets-2023_12_05_14_32_11_PST.h5'
light_event_nhit=2
light_event_timeout=3
charge_event_timeout=20000 #clock cycles
light_charge_match_tdiff=20000 #clock cycles
vref_dac=223
vcm_dac=68
max_ts_diff = 2000


def get_list_of_h5_files(file_or_dir_name):
    if os.path.isfile(file_or_dir_name):
        return [file_or_dir_name]
    else:
        base = file_or_dir_name
        sorted_files = [base + "/" + name for name in os.listdir(base) if (name[-3:] == ".h5")]
        sorted_files.sort(key=lambda x: os.path.getmtime(x))
        return sorted_files

nonrouted_channels=[6,7,8,9,22,23,24,25,38,39,40,54,55,56,57]
routed_channels=[i for i in range(64) if i not in nonrouted_channels]

geo=None
geometrypath = 'layout-2.4.0.yaml'
with open(geometrypath) as fi:
    geo=yaml.full_load(fi)

chip_pix=dict([(chip_id, pix) for chip_id,pix in geo['chips']])

N_CHIPS=100
N_CHANNELS=64

all_uniques = np.zeros( N_CHIPS*N_CHANNELS )
pixel_x     = np.zeros( N_CHIPS*N_CHANNELS )
pixel_y     = np.zeros( N_CHIPS*N_CHANNELS )

def chip_channel_to_index(chip, channel):
    return (chip-11)*N_CHANNELS + channel

counter_used = set()

for chip in range(11, 111):
    for channel in range(64):
        pix=chip_pix[chip][channel]
        counter = chip_channel_to_index(chip, channel)
        if not pix is None:
            pixel_x[counter] = geo['pixels'][pix][1]
            pixel_y[counter] = geo['pixels'][pix][2]

#Construct Light Geometry
light_channels = [30, 20, 18, 16, 14, 12, 4, 2, 0, 62, 52, 50, 48, 46, 44, 32]
light_channel_index = np.ones(N_CHANNELS)*-1

for ich, channel in enumerate(light_channels):
    light_channel_index[channel] = ich

#Gives TOP to BOTTOM numbering of channels
light_channel_index = light_channel_index.astype(int)


sync_packet_type = 6
vd = 0.016 #cm/clock cycle

def packets_to_full_timestamp(packets):
    syncs = np.where(packets['packet_type']==6)[0]
    correction = np.zeros(len(packets))
    for i in tqdm(range(1, len(syncs)), desc='correcting timestamps'):
        correction[ syncs[i] :   ] += 1e7
    return packets['timestamp'] + correction

def find_events(packets, light_event_nhit, light_event_timeout):
    #get true timestamps accounting for sync
    true_timestamps = packets_to_full_timestamp(packets)

    no_sync_mask = true_timestamps % 1e7 > 0
    packets = packets[no_sync_mask]
    true_timestamps = true_timestamps[no_sync_mask]

    dmask = np.logical_and(packets['packet_type']==0, packets['valid_parity']==1)

    dtime, data = true_timestamps[dmask], packets[dmask]

    charge_mask = data['io_channel'] <  5
    light_mask  = data['io_channel'] == 5

    ctime, cdata = dtime[charge_mask], data[charge_mask]
    ltime, ldata = dtime[light_mask], data[light_mask]

    charge_clusters = (np.ones( len(cdata)  )*-1).astype(int)
    light_clusters =  (np.ones( len(ldata)  )*-1).astype(int)

    ievt = 0

    #cluster light

    if True:
        used = set()
        timestamps = set(ltime)

        for timestamp in tqdm(timestamps, desc='clustering light hits'):
            if timestamp in used: continue
            td = np.logical_and( ltime < timestamp+light_event_timeout, ltime > timestamp-light_event_timeout )

            if np.sum(td) < light_event_nhit: continue

            for __counter in range(light_event_timeout):
                used.add(timestamp+__counter)
                used.add(timestamp-__counter)

            light_clusters[td] = ievt
            ievt += 1

    #t0 for each light event
    evt_time = np.zeros(ievt)
    for evt in set(light_clusters):
        time = np.min( ltime[ light_clusters == evt ] )
        evt_time[evt] = time

    if True:
        timestamps = set(ctime)
        used = ctime < 0
        for timestamp in tqdm(timestamps, desc='clustering charge hits'):
            cdiff = ctime - timestamp
            cmask = np.absolute(cdiff) < charge_event_timeout
            if not np.any( np.logical_and( np.logical_not(used), cmask  ) ): continue

            used = np.logical_or(used, cmask)

            #see if any light triggers in this time
            min_time = min(ctime[cmask])

            l_ev_diff = -1*(evt_time - min_time)
            lmask = np.logical_and(l_ev_diff < light_charge_match_tdiff, l_ev_diff > -light_charge_match_tdiff)

            if np.any(lmask):
                evt = np.max( np.where(lmask) )
                charge_clusters[cmask] = evt
            else:
                charge_clusters[cmask] = ievt
                ievt +=1

    return dict( {
        'charge_data'     : cdata,
        'charge_time'     : ctime,
        'light_data'      : ldata,
        'light_time'      : ltime,
        'charge_clusters' : charge_clusters,
        'light_clusters'  : light_clusters,
    } )



def get_all_channel_mean(unique, data):
    u = np.array(list(set(unique)))
    d = np.zeros(len(u))
    iun=0
    for un in u:
        mask = unique==un
        d[iun] = np.mean(data[mask])
        iun+=1
        
    return u, d

def get_all_channel_std(unique, data):
    u = np.array(list(set(unique)))
    d = np.zeros(len(u))
    iun=0
    for un in u:
        mask = unique==un
        d[iun] = np.std(data[mask])
        iun+=1
        
    return u, d

def get_all_channel_count(unique, data):
    u = np.array(list(set(unique)))
    d = np.zeros(len(u))
    iun=0
    for un in u:
        mask = unique==un
        d[iun] = np.sum(mask.astype(int))
        iun+=1
        
def get_date_datafile(filename):
    dtime = ((filename.split('packets-')[-1]).split('.h5')[0]).split('_')
    year = int(dtime[0])
    month = int(dtime[1])
    day = int(dtime[2])
    hour = int(dtime[3])
    minute = int(dtime[4])
    second = int(dtime[5])
    return year, month, day, hour, minute, second 

def get_date_config(filename):
    dtime = (filename.split('asic_configs_')[-1]).split('_')
    year = int(dtime[0])
    month = int(dtime[1])
    day = int(dtime[2])
    hour = int(dtime[3])
    minute = int(dtime[4])
    second=0
    try:
        second = int(dtime[-1])
    except:
        second = 0
    return year, month, day, hour, minute, second 


def display_1d(c,title=None):
    fig = plt.figure(figsize=(12, 10) )
    ax = fig.add_subplot()
    ax.hist(c, bins=50, range=(0, 100))
    ax.set_xlabel('dataword [adc]', fontsize=12)
    ax.grid()
    if not title is None: ax.set_title(title)
    plt.show()

def display_2d(x, y, c, title=None, cut=None):
    fig = plt.figure(figsize=(12, 10) )
    ax = fig.add_subplot()
    mask = [True]*len(c)
    if not cut is None:
        mask = c < cut

    sc = ax.scatter(x[mask], y[mask], c=c[mask], marker='s', s=40)
    #ax.axhline(50, color='red')

    cbar = plt.colorbar(sc, ax=ax)
    cbar.set_label('dataword', rotation=270)
    if not title is None: ax.set_title(title)
    plt.show()

def display_3d(x, y, z, c, title=None, view=None):
    fig = plt.figure(figsize=(6, 5) )
    ax = fig.add_subplot(projection='3d')
    sc = ax.scatter(x, y, z, c=c, marker='.', s=20, alpha=0.85)
    cbar = plt.colorbar(sc, ax=ax)
    cbar.set_label('charge [ke-]', rotation=270, fontsize=14)
    if not view is None:
        ax.view_init(*view)

    ax.set_xlabel('x [mm]', fontsize=12)
    ax.set_ylabel('y [mm]', fontsize=12)
    ax.set_zlabel('z (drift) [mm]', fontsize=12)
    ax.set_xlim(-150, 150)
    ax.set_ylim(-150, 150)
    #ax.set_zlim(0, 100)
    #ax.set_zlim(0, 5)
    if not title is None: ax.set_title(title)
    plt.show()

def dword_to_mv(dword):
    vref = vref_dac/256*1800.
    vcm  = vcm_dac/256*1800
    return (dword/256.)*(vref-vcm) + vcm

def get_pedestals(filename):
    #Pedestal File
    f = h5py.File(filename)
    packets = f['packets']
    dmask = np.logical_and(packets['packet_type']==0, packets['valid_parity']==1)
    dmask = np.logical_and(dmask, packets['chip_id']<111)
    data = packets[dmask]

    q = data['dataword']
    indices = chip_channel_to_index(data['chip_id'].astype(int), data['channel_id'].astype(int))
    pedu, pedmean = get_all_channel_mean(indices, q)
    peds_ind = np.zeros(100*64)
    for chip in range(11, 111):
        for channel in range(64):
            counter = chip_channel_to_index(chip, channel)
            if counter in pedu:
                peds_ind[counter] = dword_to_mv(pedmean[np.where(pedu==counter)[0][0]])
    return peds_ind


def process_file(filename, pedestal_filename, light_event_nhit, light_event_timeout, output_filename=None):
    peds_ind = get_pedestals(pedestal_filename)
    if True:
        for ifile in list([filename]):
            if not output_filename is None:
                print('Creating file: {} ...'.format(output_filename) )
                if os.path.exists(output_filename):
                    print('Output file exists: {}'.format(fname))
                    continue

            f= h5py.File( filename )
            packets = f['packets']
            if len(packets) < 10:
                print('Fewer than 10 packets in this file--skipping')
                continue
            evs = find_events(packets, light_event_nhit, light_event_timeout)

            if output_filename is None: output_filename='.temp.h5'

            hf = h5py.File(output_filename, 'w')
            hf.create_dataset('light_ts', data=evs['light_time'])
            hf.create_dataset('charge_ts', data=evs['charge_time'])
            hf.create_dataset('charge_event', data=evs['charge_clusters'])
            hf.create_dataset('light_event', data=evs['light_clusters'])
            hf.create_dataset('charge_chipid', data=evs['charge_data']['chip_id'])
            hf.create_dataset('charge_chanid', data=evs['charge_data']['channel_id'])

            indices = chip_channel_to_index(evs['charge_data']['chip_id'].astype(int),
                                    evs['charge_data']['channel_id'].astype(int))

            hf.create_dataset('charge_q', data= (dword_to_mv(evs['charge_data']['dataword'])-peds_ind[indices])/gain )
            hf.create_dataset('charge_x', data=pixel_x[indices] )
            hf.create_dataset('charge_y', data=pixel_y[indices] )
            hf.create_dataset('light_chanid', data=evs['light_data']['channel_id'])
            hf.create_dataset('light_data', data=evs['light_data']['dataword'])
            hf.close()

    return output_filename


# Do t0 reco
def t0_reco(fname):
    f = h5py.File(fname)
    cz = -99*np.ones( len(f['charge_ts']) )
    #tmask = np.array(f['charge_ts'])%1e7 > 1
    for evt in set(f['light_event']):
            lmask = f['light_event']==evt
            t0 = np.min(f['light_ts'][lmask])
            mask = np.logical_and(f['charge_ts']-t0 < max_ts_diff, f['charge_ts']-t0 > -100 )
            n = sum(mask)
            if n > 0:
                cz[mask] = (f['charge_ts'][mask] - t0)*vd
    return cz, f['charge_q']

def exp_plus_offset(x, A, lamb, b):
        gz = x > 0.15
        ys = np.full(x.shape[0], b).astype(float)
        ys[gz] += A*np.exp( -(x[gz]/lamb) ).astype(float)
        return ys

def exp(x, A, lamb):
    gz = x > 0.15
    ys = np.full(x.shape[0], 0).astype(float)
    ys[gz] += A*np.exp( -(x[gz]/lamb) ).astype(float)
    return ys

def main(filename,\
         pedestal_filename,
         output_filename,
         light_event_nhit,
         light_event_timeout,
         plot):

    fname = process_file(filename, pedestal_filename, light_event_nhit, light_event_timeout)
    
    # Reconstruct lifetime
    cz, cq = t0_reco(fname)
    qs, bins = np.histogram(cz, weights=cq, bins=int(32/0.16), range=(-2,30));
    dl = (bins[:-1] + bins[1:])/2

    mm_to_us = 1.6  # mm/us
    cm_to_us = 0.16 # cm/us

    params, pcov = curve_fit(exp_plus_offset, dl, qs, p0=[10000, 1, 0])
    params_no, pcov_no = curve_fit(exp, dl, qs, p0=[10000, 1])

    lambda_us = round(params[1]/cm_to_us, 3)
    sigma_lambda_us = round(np.sqrt(pcov[1][1])/cm_to_us, 3)

    lambda_no_us = round(params_no[1]/cm_to_us, 3)
    sigma_lambda_no_us = round(np.sqrt(pcov_no[1][1])/cm_to_us, 3)
        
    print('fit 1: lambda={} +/- {} [us]'.format(lambda_us, sigma_lambda_us))
    
    if plot:
        fig = plt.figure()
        ax = fig.add_subplot()
        ax.grid(True)
        ax.plot(dl, qs, drawstyle='steps-mid', linestyle='', marker='.', label='Charge', color='black')
        ax.axvline(0-0.05, linestyle='--', color='black', label='t0', alpha=0.5)
        ax.axvline(30+0.05, linestyle='--', color='red', label='30cm drift', alpha=0.5)
        #ax.set_yscale('log')
        ax.set_ylabel('Charge [k$e^-$/mm]', fontsize=14)
        ax.set_xlabel('Drift length [cm]', fontsize=14)
        ax.plot(dl, exp_plus_offset(dl, *params), color='red', alpha=0.6, \
            label=r'fit $q=Q_0e^{-z_{drift}/\lambda}+b$'
            +'\n$\lambda={}\pm${} [$\mu s$]'.format(lambda_us, sigma_lambda_us) \
            #+ '\n$Q_0={}\pm${}'.format( round(params[0], 2), round(np.sqrt(pcov[0][0]), 3) ) \
            #+ '\n$b={}\pm${}'.format( round(params[2], 2), round(np.sqrt(pcov[2][2]), 3) )
           )

        ax.plot(dl, exp(dl, *params_no), color='orange', alpha=0.6, \
           label=r'fit $q=Q_0e^{-z_{drift}/\lambda}$'
            +'\n$\lambda={}\pm${} [$\mu s$]'.format(lambda_no_us, sigma_lambda_no_us) \
            #+ '\n$Q_0={}\pm${}'.format( round(params_no[0], 2), round(np.sqrt(pcov_no[0][0]), 3) )
           )
        ax.legend(fontsize=12)
        plt.show()


if __name__=='__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--filename', default=filename, \
                        type=str, help='''File to process''')
    parser.add_argument('--pedestal_filename', default=pedestal_filename, \
                        type=str, help='''Pedestal file for charge hit processing''')
    parser.add_argument('--output_filename', default=output_filename, \
                        type=str, help='''Name for simple-formatted data output''')
    parser.add_argument('--light_event_nhit', default=light_event_nhit, \
                        type=int, help='''Number of hits required for light event''')
    parser.add_argument('--light_event_timeout', default=light_event_timeout, \
                        type=int, help='''Symmetric half time-window to consider light hits for events ''')
    parser.add_argument('--plot', default=False, \
                        action='store_true', help='''Flag to plot fit''')
    args=parser.parse_args()
    main(**vars(args))

