function [Y_new, P_mean] = beamWidthMean(Y,P,beamWidth,BO_plot)
%% beamWidthMean(Y,P,beamWidth)
%
% Moyenne la polaisation sur la largeur du faisceau
% Y : Positions en y
% P : Polarisation pour les positions en y
% beamWidth : Largeur du faisceau
%
%
% $Revision: 4 $
% $Author: $
% $Date: 2015-04-03 12:04:45 +0200 (ven., 03 avr. 2015) $

%%
y_step = Y(2)-Y(1);

% 
% PmeanPolarNeutron1 = polarNeutron;
%     for wl_index = 1:numel(WL_RATIO);
%         meanPolarNeutron1(:,wl_index) = meanPolarNeutron1(:,wl_index)*WL_RATIO(wl_index);
%     end   
%     meanPolarNeutron1 = sum(meanPolarNeutron1, 2);

    index = round(beamWidth/y_step);
    disp(strcat(['Beam integration on ', num2str(index), ' points']))
    if index > 1 && index < numel(P);
        P_mean = P(1:end - index+1);
        for j = 1:index-1;
            P_mean = P_mean + P(index-j +1 :end - j+1);
        end;
        
        % plot(Y(round(i/2):end - round(1+ i/2)+1),cos(meanPolarNeutron2/i));
        Y_new = Y(round(index/2): round(index/2) + numel(P_mean) -1);
        P_mean = P_mean/index;
        if nargin < 4 || BO_plot == 1;
            figure;
            plot(Y_new,cos(P_mean));
            ylim([-1.1,1.1])
        end;
        

    else
        disp('Not enougth points!')

    end