% TILTTEST.M
%
%    TILTTEST - Test whether tilt on Pepperpot prediction makes emittance
%    shape change

    clear all ; close all ;

    plotcol = (['r+'; 'g+'; 'b+'; 'c+'; 'm+'; 'y+'; 'k+'; ...
        'r*'; 'g*'; 'b*'; 'c*'; 'm*'; 'y*'; 'k*'; ...
        'rx'; 'gx'; 'bx'; 'cx'; 'mx'; 'yx'; 'kx'; ...
        'ro'; 'go'; 'bo'; 'co'; 'mo'; 'yo'; 'ko']) ;

    midpos = [1115 980] ;
    midposb = [1118 983] ;
    lpos = [207 1795] ;
    rpos = [1945 1900] ;
    pplim = 105 ;
    width = 51 ;
    zlen = 13 ;
%    rotang = ( ( 90 + 11.25 )./360 ).*( 2.*pi ) ;
    rotang = 0 ;

    [ppimbg,stdmbg,pphistbg,ppbgminmax] = pepperplotfun('E:\pepperpot\04_05_06\500ms_bglight_10.tif',0,[2 0]) ;

    ppbgmin = ppbgminmax(1) ; ppbgmax = ppbgminmax(2) ;
    load E:\pepperpot\cal_data\pixcal.mat ;
    load E:\pepperpot\cal_data\ppcaldat_good.mat ;

    [midp,imtilt,rulen] = pptiltfind(ppimbg,lpos,rpos) ;

%    [ppimspots] = ppspotreco(ppcalmat,midpos,rulen,imtilt) ;
    [ppimspots] = ppspotreco(ppcalmat,midpos,rulen,0) ;
    [ppimspotsb] = ppspotreco(ppcalmat,midposb,rulen,-10e-3,1.02) ;
%    [ppimspotsb] = ppspotreco(ppcalmat,midposb,rulen,0,1.02) ;

    figure, imagesc(ppimbg,[ppbgmin ppbgmax]), axis image, hold on ;
    colormap(gray) ;
%    plot(midpos(1),midpos(2),'xr') ;    % Mark central point
    for i = 1:25
        for j = 1:25
            plot(ppimspots(i,j,1),ppimspots(i,j,2),'xc') ;
        end
    end
    hold off

    rupix = rulen./80 ;
    sppix = (rulen.*mlrulratio)./72 ;
    phospix = sppix.*perscale ;
    zlen = 13 ;
    invintense = squeeze(1./ppcalmat(:,:,3)) ;

%    Main hole positions in (X,Y) and (r,theta,phi)

    ppimspotsoff(:,:,1) = ppimspots(:,:,1) - ppimspots(13,13,1) ;
    ppimspotsoff(:,:,2) = ppimspots(:,:,2) - ppimspots(13,13,2) ;
    ppimdata = pprtpconv(ppimspotsoff,zlen,phospix) ;
    ppimspotsoffb(:,:,1) = ppimspotsb(:,:,1) - ppimspots(13,13,1) ;
    ppimspotsoffb(:,:,2) = ppimspotsb(:,:,2) - ppimspots(13,13,2) ;
    ppimdatab = pprtpconv(ppimspotsoffb,zlen,phospix) ;
    ppimoff(:,:,1) = ppimspotsb(:,:,1) - ppimspots(:,:,1) ;
    ppimoff(:,:,2) = ppimspotsb(:,:,2) - ppimspots(:,:,2) ;
    ppimdataoff = pprtpconv(ppimoff,zlen,phospix) ;

    figure, hold on ;
    for i = 1:25
        for j = 1:25
            plot(ppimdata(i,j,1),ppimdata(i,j,2),'xr') ;
            plot(ppimdatab(i,j,1),ppimdatab(i,j,2),'xb') ;
        end
    end
    hold off

    figure, hold on ;
    for i = 1:25
        for j = 1:25
            plot(ppimdataoff(i,j,1),ppimdataoff(i,j,2),'xk') ;
        end
    end
    hold off

%    Calculate emittance data from [r,theta,phi]

    [ppemitdata,ppposdat] = ppemitmat(ppimdata(:,:,3:5), ppimdataoff(:,:,3:5), rotang) ;

    figure, hold on ;
    for i = 1:25
        for j = 1:25
            if ppposdat(i,j,3) <= 41
%                plot(ppemitdata(i,j,1),ppemitdata(i,j,3),'xk') ;
                plot(ppemitdata(i,j,1),ppemitdata(i,j,3),plotcol(i,:)) ;
            end
        end
    end
    xlabel(['Horizontal position, x (mm)']), ylabel(['Horizontal divergence, x'' (mrad)']), ...
        title(['\bf Horizontal phase space distribution, x-x'', for test Pepperpot image']) ;
    hold off

    figure, hold on ;
    for i = 1:25
        for j = 1:25
            if ppposdat(i,j,3) <= 41
%                plot(ppemitdata(i,j,2),ppemitdata(i,j,4),'xk') ;
                plot(ppemitdata(i,j,2),ppemitdata(i,j,4),plotcol(j,:)) ;
            end
        end
    end
    xlabel(['Vertical position, y (mm)']), ylabel(['Vertical divergence, y'' (mrad)']), ...
        title(['\bf Vertical phase space distribution, y-y'', for test Pepperpot image']) ;
    hold off
