% ppcuttest_30_07_08.m

    clear all ; close all ;

    cent = [1024 1024] ;
    width = 45 ;
    zlen = 10 ;
    rotang = 0 ;
    energy = 35e3 ;
%    plt = [2 2 1 1 1 2 1 0 1 2] ;
%    plt = [0 0 0 0 0 0 0 0 0 0 2] ;
%    plt = [0 0 0 0 0 0 0 0 0 0 0] ;
    plt = [0 0 0 0 0 0 1 0 1 2] ;
    calplt = 0 ;
    bitdiv = 4 ;
    alldat = 1 ;
    beamview = 'beam' ;

%    pplim = 114 ;
%    pplim = 119 ;
    pplimmat = 250 ;
%    pplimmat = [110:10:200] ;
%    pplimmat = [pplimmat [250:50:1000]] ;
%    pplimmat = [pplimmat [1200:200:4000]] ;

%    [ppdir,bsfs] = getppdir ;
    ppdir = [''] ;
    datdir = (['D:\pepperpot\Virginie\2008_07_30_pepperpot_openEnded_openSlit\2008_07_30_pepperpot']) ;

    fuldir = fullfile(ppdir, datdir) ;

    datfile = ['beam_17kV_260mm.tif'] ;

%    if calplt
%        [midpos,sp2sp,imtilt] = ppcalfindmm(fullfile(datdir, 'cali_17kV_260mm.tif'),[42 18],[2030 11],[52 2006],[2034 2005],126,calplt) ;
%    else
        [midpos,sp2sp,imtilt] = ppcalfindmm([],[42 18],[2030 11],[52 2006],[2034 2005],126,calplt) ;
%    end

    for i = 1:length(pplimmat)

        if alldat == 1

            [ppposdat,ppemitdat,ppim,pppixemit5d,pppixemit2d] = ...
                ppanalyse41(fullfile(fuldir, datfile),[],midpos,imtilt,sp2sp,pplimmat(i),width,zlen,rotang,energy,plt,bitdiv,beamview) ;
            xemiall(i) = ppemitdat(1).singlepix ;
            yemiall(i) = ppemitdat(2).singlepix ;

        else
            [ppposdat,ppemitdat,ppim] = ...
                ppanalyse41(fullfile(fuldir, datfile),[],midpos,imtilt,sp2sp,pplimmat(i),width,zlen,rotang,energy,plt,bitdiv,beamview) ;
        end

        ppcutmat = ( ppim > pplimmat(i) ) ;
        ppim = ( ppim - pplimmat(i) ).*ppcutmat ;
        sumint(i) = sum( sum( ppim ) ) ;
        maxes(i) = max(max(ppim)) ;
        xemi(i) = ppemitdat(1).avespot ;
        yemi(i) = ppemitdat(2).avespot ;

    end

    figure, plot(pplimmat,maxes,'x-k') ;
    xlabel(['Cut level']), ylabel(['Pixel Intensity']), ...
        title(['\bf Light intensity of the brightest pixel']) ;

    figure, plot(pplimmat,sumint,'x-k') ;
    xlabel(['Cut level']), ylabel(['Image Intensity']), ...
        title(['\bf Integrated light intensity for the total image (after cut)']) ;

    figure, hold on ;
    plot(pplimmat,xemi,'x-k') ; plot(pplimmat,yemi,'x-r') ;
    xlabel(['Cut level']), ylabel(['Horizontal/Vertical emittance (\pi mm mrad)']), ...
        title(['\bf Horizontal and Vertical emittance, \epsilon_x and \epsilon_y, for average pixel data']) ;
    legend(['\epsilon_x'],['\epsilon_y'],'Location','Best') ;
    hold off ;

    if alldat
        figure, hold on ;
        plot(pplimmat,xemiall,'x-k') ; plot(pplimmat,yemiall,'x-r') ;
        xlabel(['Cut level']), ylabel(['Horizontal/Vertical emittance (\pi mm mrad)']), ...
            title(['\bf Horizontal and Vertical emittance, \epsilon_x and \epsilon_y, for all pixel data']) ;
        legend(['\epsilon_x'],['\epsilon_y'],'Location','Best') ;
        hold off ;

%        save ppdat_06_07_07_1755cuttest.mat pplimmat maxes sumint xemi yemi xemiall yemiall
    end

    return



