clear all;close all;

dataname = ['F:\LIMP003\LIMP003_006_210413_112545', %% good ach and good CA1  optitrack         
           'F:\LIMP018\LIMP018_003_220127_150338', %% good ach and good CA1  
           'F:\LIMP011\LIMP011_008_210622_092807', %% good ach-signal but CA3 neurons   optitrack
           'F:\LIMP013\LIMP013_011_210630_142011', %% good ach-signal but CA3 neurons   optitrack
           'F:\LIMP016\LIMP016_005_220103_130720', %% need correction of signal but good CA1 neurons
           'F:\LIMP019\LIMP019_007_220307_155644'] %% no ach-signal but has CA1 neurons;

RipCh = [11,26,nan,nan,53,1]
%% inhibtion on ACh signal
for ii = [1,2,5,6]
    cd(dataname(ii,:))
    basepath = pwd;
    basename = bz_BasenameFromBasepath(basepath);
    load([basename,'.SleepState.states.mat']);
    load([basename,'.cell_metrics.cellinfo.mat']);
    load([basename,'.pulses.events.mat']);
    load([basename,'.spikes.cellinfo.mat']);

    
    pulse = pulses.intsPeriods;
    pul = pulses.intsPeriods;
    sws = SleepState.ints.NREMstate;
    pul_sws = [];
        for kk = 1:length(sws)
            pul_in = []
            pul_in = pul(find(pul(:,1)-10>sws(kk,1) & pul(:,2)+10<sws(kk,2)));     
            pul_sws = [pul_sws;pul_in];
        end      
    events = pul_sws;events(:,2) = events(:,1)+10;
    
    binSize = 0.001;
    spkMat = bz_SpktToSpkmat(spikes,'binSize',binSize,'overlap',1);
    types = convertCharsToStrings(cell_metrics.putativeCellType);
    nIntN = find(strcmp(types,'Narrow Interneuron')); % Narrow Interneuron % Pyramidal Cell
    PyN = find(strcmp(types,'Pyramidal Cell')); 
    wIntN = find(strcmp(types,'Wide Interneuron'));

    events = [];
    events = pul_sws;
    secondsBefore = 5;secondsAfter = 10;
    binWidth = 0.1;cellnums = nIntN;
    PETHSpikes_nIntN = bz_PETH_Spikes(events,'cellnums',cellnums,'secondsBefore',secondsBefore,'secondsAfter',secondsAfter,'binWidth',binWidth);

    binWidth = 0.1;cellnums =  PyN;
    PETHSpikes_PyN = bz_PETH_Spikes(events,'cellnums',cellnums,'secondsBefore',secondsBefore,'secondsAfter',secondsAfter,'binWidth',binWidth);

    binWidth = 0.1;cellnums =  wIntN;
    PETHSpikes_wIntN = bz_PETH_Spikes(events,'cellnums',cellnums,'secondsBefore',secondsBefore,'secondsAfter',secondsAfter,'binWidth',binWidth);
    
    PETHSpikes.nInN = PETHSpikes_nIntN;
    PETHSpikes.PyN = PETHSpikes_PyN;
    PETHSpikes.wInN = PETHSpikes_wIntN
    datpath = 'G:\My Drive\Paper_ACh_OXT\Fig5\Figure 5B\ACh_inhibition_spikes';
    save(fullfile(datpath,[basename,'_PETHSpikes.mat']),'PETHSpikes');
end
