clear all;close all;

dataname = ['F:\oxytocin\OXYT014\OXYT014_005_220515_133849', %% good signal and good CA1/CA2/CA3  optitrack         
%             'F:\oxytocin\OXYT025\OXYT025_005_230110_110227', %% good signal and good CA1/CA2/CA3  optitrack   OXT025 NO response for OXT signa\l      
%             'F:\oxytocin\OXYT025\OXYT025_006_230116_123923', %% good signal and good CA1/CA2/CA3  optitrack         
%             'F:\oxytocin\OXYT025\OXYT025_010_230123_170927', %% good signal and good CA1/CA2/CA3  optitrack
%             'F:\oxytocin\OXYT025\OXYT025_013_230207_105757', %% good signal and good CA1/CA2/CA3  optitrack  
%             'F:\oxytocin\OXYT025\OXYT025_016_230215_163917', %% good signal and good CA3  optitrack OxtSig_H
            'F:\oxytocin\OXYT026\OXYT026_002_230103_172212', %%  good signal and good CA1/CA2/CA3  optitrack
            'F:\oxytocin\OXYT026\OXYT026_006_230114_141420', %%  good signal and good CA1/CA2/CA3  optitrack
            'F:\oxytocin\OXYT026\OXYT026_007_230117_135633', %%  good signal and good CA1/CA2/CA3  optitrack
            'F:\oxytocin\OXYT026\OXYT026_010_230208_180921', %%  good signal and good CA1/CA2/CA3  optitrack
            'F:\oxytocin\OXYT026\OXYT026_013_230220_120816', %%  good signal and good CA3  optitrack OxtSig_H
            'F:\oxytocin\OXYT027\OXYT027_002_230104_110308'] %%  good signal and good CA1/CA2/CA3  optitrack

RipCh = [93,8,8,27,46,42,11];
%% inhibtion on ACh signal
for ii = 1:size(dataname,1)
    cd(dataname(ii,:))
    basepath = pwd;
    basename = bz_BasenameFromBasepath(basepath);
    load([basename,'.pulses.events.mat']);
    load([basename,'.SleepState.states.mat']);
    if ii>1
        load([basename,'.accel.behavior.mat']);
    end
    
    load([basename,'.spikes.cellinfo.mat']);
    load([basename,'.cell_metrics.cellinfo.mat']);

    info = read_Intan_RHD2000_file(basepath);
    
    pulse = pulses.intsPeriods;
    pulses.duration = pulses.intsPeriods(:,2) - pulses.intsPeriods(:,1);
    idx = find(pulses.duration>4 & pulses.duration<6);
    pulse = pulse(idx,:)
    %% exclude the move-pulse
  
    pulse = pulses.intsPeriods;
    pul_NREM = [];
    
    if ii == 1
        SleepState.ints.NREMstate = SleepState.ints.NREMstate;
        NREM.opto = [];
        for jj = 1:length(SleepState.ints.NREMstate)    
            NREM.optoin = find(pulse(:,1)>SleepState.ints.NREMstate(jj,1)& pulse(:,2)< SleepState.ints.NREMstate(jj,2))
            NREM.opto = [NREM.opto;NREM.optoin];
        end 
        pul_NREM = pulse(NREM.opto,:);
    else    
        for jj = 1:length(pulse)
            win = round(pulse(jj,:))+[-10 20];
            pul_in = [];
            if mean(accel.acceleration.motion(win(1):win(2)))<100
                pul_in = pulse(jj,:);
                pul_NREM = [pul_NREM;pul_in];
            end 
        end
    end

    
    %% get spikes

    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 = pulse(:,1);
    secondsBefore = 10;secondsAfter = 20;
    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 5D\Oxt_activation\Oxt_activation_spikes';
    save(fullfile(datpath,[basename,'_PETHSpikes.mat']),'PETHSpikes');
   
end
