dataname = ['F:\LIMP003\LIMP003_007_210420_103320'  %%corrected   optitrack shank1-4 CA1
            'F:\LIMP014\LIMP014_012_211005_112625',    %%corrected   optitrack shank1-4 CA1
            'F:\LIMP014\LIMP014_014_211007_122021',   %%corrected   optitrack shank1-4 CA1 
            'F:\LIMP018\LIMP018_003_220127_150338',    %%corrected  all shanks CA1  exclude data from 5700 to 11250
            'F:\LIMP019\LIMP019_004_220303_115500']    %%corrected  shank1-4 CA1


for ii = 1:size(dataname,1)
    cd(dataname(ii,:))
    basepath = pwd;
    basename = bz_BasenameFromBasepath(basepath);
    load([basename,'.SleepState.states.mat']);
    
    if ii>0 & ii<4
        info = read_Intan_RHD2000_file(basepath);
        digitalin = read_Intan_digitalin_file('digitalin.dat',info.num_board_dig_in_channels)';
        lfp_fbtime = find(digitalin(1,:)>(max(digitalin)+min(digitalin))/2,1);
        if isempty(lfp_fbtime) == 1
            lfp_fbtime = [0]
        else lfp_fbtime(1,2) = find(digitalin(1,:)>(max(digitalin)+min(digitalin))/2,1,'last');
        end 
    elseif ii>3
        info = read_Intan_RHD2000_file(basepath);
        analogin= read_Intan_analogin_file('analogin.dat',info.num_board_adc_channels);
        DD = analogin(1,:);
        lfp_fbtime = find(DD>(max(DD)+min(DD))/2,1);
        if isempty(lfp_fbtime) == 1
            lfp_fbtime = [0]
        else lfp_fbtime(1,2) = find(DD>(max(DD)+min(DD))/2,1,'last');            
        end 
    end
    % sitmulation and signal
    if exist('dFoverF.mat')==0
        name = basename(1:11);
        ext = '.mat'
        if ii>0 & ii<4
            load([name,ext],[name,'_OpTrTTL']);
            ACh_sti = eval([name,'_OpTrTTL']);          
        elseif  ii>3
            load([name,ext],[name,'_AChSti']);
            ACh_sti = eval([name,'_AChSti']);
        end
        otr = 0.5;
        fbtime = ACh_sti.times(find(ACh_sti.values(:,1)>otr,1));fbtime(1,2) = ACh_sti.times(find(ACh_sti.values(:,1)>otr,1,'last'));
        fbIDX = round(fbtime*20000);
        load([name,ext],[name,'_AChSig1']);
        ACh_signal = eval([name,'_AChSig1']);
        ACh_signal.data = downsample(ACh_signal.values((fbIDX(1,1)):(fbIDX(1,2))),200);
        ACh_signal.time = downsample(ACh_signal.times((fbIDX(1,1)):(fbIDX(1,2))),200)- fbtime(1,1);
        p = [];
        p = polyfit(ACh_signal.time,ACh_signal.data,5);
        Ybaseline = polyval(p,ACh_signal.time);
        figure
        plot(ACh_signal.time, ACh_signal.data); hold on;
        plot(ACh_signal.time,Ybaseline);
        dFoverF = smoothdata((ACh_signal.data - Ybaseline)./Ybaseline,'movmean',10);
        hold on;
        plot(ACh_signal.time, dFoverF);xlim([0 inf]);
    else load('dFoverF.mat'); figure;plot(dFoverF);
    end
    
    %% get CA1 popRate
    load([basename,'.cell_metrics.cellinfo.mat']);
    load([basename,'.spikes.cellinfo.mat']);
    
    SWS =  round (SleepState.ints.NREMstate*100);
    WAKE =  round (SleepState.ints.WAKEstate*100);

    binSize = 0.01;
    spkMat = bz_SpktToSpkmat(spikes,'binSize',binSize,'overlap',1);
    smoothingWindow = 10;
    
    if ii~=4
        Spikes_CA1 = spkMat.data(:,cell_metrics.shankID<5);
    elseif ii == 4
        Spikes_CA1 = spkMat.data;
    end

    data = Spikes_CA1;
    popRate = zeros(length(data),1);
    Nber = size(data,2);
    for spk = 1:Nber
        popRate = popRate + zscore(Smooth(data(:,spk),smoothingWindow));
    end
    popRate = popRate./spk;
    popRate = popRate + abs(min(popRate));
    % popRate = smoothdata(popRate,'gaussian',250);a

    h = figure();
    plot(spkMat.timestamps,popRate);
    tim = [1:length(dFoverF)]/100;
    hold on;plot(tim+lfp_fbtime(1)/2e4, dFoverF*10);xlim([0 inf]);

    datpath = 'G:\My Drive\Paper_ACh_OXT\Fig2\Data\Figure2DEF\ACh_popRate';
    save(fullfile(datpath,[basename,'ACh_popRate.mat']),'dFoverF','popRate','lfp_fbtime')
    close all
    
end