function dop=dopBehSummary(dop)
% dopOSCCI Mark II: dopBehSummary ~ 19-11-2010 (last edit)
%
% check behavioural information:
%
% outputs:
% - behNames=beh.file.varNames;
% - fileReport=beh.file.report;
% - n=beh.file.n;
% - nVar=beh.file.nVar;
% - data=beh.file.data;
% - levels=beh.file.levels;
%
fprintf('%s\n',['Running ',mfilename]);
try
    if ~dop.sum.skip && ~dop.sum.quit && dop.def.behFileChk
        fprintf('\t%s\n','Calculating behavioural summaries:');
        %% match file
        dop.beh.match=0;
        %         for i=1:length(dop.beh.fileList)
        dop.beh.match=find(strcmp(dop.sum.fileNameLoc,dop.beh.fileList));
        %                 dop.beh.match=i;
        %                 break
        %             end
        %         end
        if isempty(dop.beh.match)
            fprtinf('\t%s\n\t%s\n\n\t%s\n','No behavioural match found',...
                'Therefore can not run behavioural summary.',...
                'Dummy values will be included in output.');
        end
        %% create filter/s
        % base upon behvaioural conditions
        if isfield(dop.beh,'filter'); dop.beh=rmfield(dop.beh,'filter');end 
        for i=1:length(dop.beh.behNames)
            dop.tmp.bn=dop.beh.behNames{i};
            for j=1:length(dop.beh.levels.(dop.tmp.bn));
                dop.tmp.lv=dop.beh.levels.(dop.tmp.bn)(j); % level value
                if ~isempty(dop.beh.match)
                    dop.beh.filter.(dop.tmp.bn)(:,j)=...
                        dop.beh.data.(dop.tmp.bn)(dop.beh.match,:)==dop.tmp.lv;
                else
                    dop.beh.filter.(dop.tmp.bn)(:,j)=...
                        zeros(1,size(dop.beh.data.(dop.tmp.bn),2));
                end
                
            end
        end
        %% session && last session?
        if dop.def.behFileChk && isfield(dop,'ses') && dop.ses.session==dop.ses.nSes+1
            for i=1:numel(dop.beh.behNames)
                dop.tmp.behFilter=dop.beh.filter.(dop.beh.behNames{i});
                for j=2:dop.ses.nSes
                    % stack them on top of each other
                    dop.beh.filter.(dop.beh.behNames{i})=vertcat(dop.beh.filter.(dop.beh.behNames{i}),dop.tmp.behFilter);
                end
            end
        end
                
        %% run summaries for whatever we want...
        dop.beh.ind=dopDataIndexCalcSetup(dop.stack,dop.filter,...
            'screen',dop.scrn,'sampleRate',dop.def.downHertz,...
            'evType',dop.def.evType,'evMatch',dop.def.evMatch,...
            'beh',dop.beh.filter);
        
        %% laterality indices
        dop.beh.li=dopDataLIcalcSetup(dop.stack,dop.filter,'actWin',dop.def.actWin,...
            'screen',dop.scrn,'sampleRate',dop.def.downHertz,...
            'evType',dop.def.evType,'beh',dop.beh.filter);
        %         else
        %             fprtinf('\t%s\n\t%s\n','No behavioural match found',...
        %                 'Therefore can not run behavioural summary.');
        %         end
    else
        fprintf('\t%s\n','Behavioural summary not requested');
    end
catch err
    %% catch dopOSCCI error
    % 
save(dopOSCCIdebug(mfilename));rethrow(err); 
%     dopOSCCIcrash(err,dop,mfilename);
end