search_obs.m 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
classdef search_obs
    %export TANGO_HOST=ebs-simu:10000

    %   Detailed explanation goes here
    
    properties
        CUR
        BLM
        ORBITH
        ORBITV
        LT
        RING
13 14 15 16 17 18 19 20 21 22
        MODEL
        HRESP
        VRESP
        HSTRENGTH
        VSTRENGTH
        INITSTRENGTH
        s_ok
    
    HSTNAME
    VSTNAME
23 24 25
    end
    
    methods
26
        function obj = search_obs()
27 28
            %UNTITLED Construct an instance of this class
            %   Detailed explanation goes here
perron's avatar
perron committed
29 30 31 32
            f=pwd;
            cd /machfs/perron/matlab/programs;
            addpathtom;
           cd(f); 
33 34 35 36 37
         
        curdir=pwd;
        cd /machfs/perron/matlab/programs;
        addpathtom;
        cd(curdir);
38
        obj.CUR=tango.Attribute('sr/d-ct/1/current');
perron's avatar
perron committed
39 40
        obj.BLM=1+mod([3:0.25:34.75],32);
        %obj.BLM=tango.Device('srdiag/beam-position/all');    
41 42 43 44
        obj.ORBITH=tango.Attribute('srdiag/beam-position/all/SA_HPositions');    
        obj.ORBITV=tango.Attribute('srdiag/beam-position/all/SA_VPositions');
        obj.LT=tango.Attribute('srdiag/beam-position/all/SA_HPositions_Peak');    
        obj.RING='ebs';
45 46 47 48 49
        dirname=uigetdir('/operation/appdata/ebs/optics/settings','optic to be loaded');
        obj.MODEL=ebs.model(dirname);
         obj.MODEL.directory=dirname;  
         obj.HRESP=load(fullfile(dirname,'h_resp.csv'));   
        obj.VRESP=load(fullfile(dirname,'v_resp.csv'));
50
            
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        obj.s_ok=true(288,1);
        if size(obj.HRESP,1)>288
            st=readtable(fullfile(dirname,'h_steerers.csv')); 
            isdq=strfind(table2array(st(:,1)),'dq');
                for i=1:numel(isdq);
                obj.s_ok(i)=isempty(isdq{i});
                end
        end
        in=obj.s_ok;
       
        obj.HRESP=obj.HRESP(:,in);
            
        obj.HSTRENGTH=tango.Attribute('srmag/hst/all/Strengths');
        obj.VSTRENGTH=tango.Attribute('srmag/vst/all/Strengths');
        obj.INITSTRENGTH.h=obj.HSTRENGTH.set;
        obj.INITSTRENGTH.v=obj.VSTRENGTH.set;
        obj.HSTNAME=tango.Attribute('srmag/hst/all/CorrectorNames');
        obj.VSTNAME=tango.Attribute('srmag/vst/all/CorrectorNames');
        
        
        end
        
        function reset_steer(obj)
        % resets steerers to the value they had when creating the instance
        % of the object.
                  
        obj.HSTRENGTH.set=obj.INITSTRENGTH.h;
        obj.VSTRENGTH.set=obj.INITSTRENGTH.v;
        
80
        end
81 82 83 84
        
        
        function [or_fit,err_exec]=mkbump(obj,bump_name,amplitude,numvect)
            %Makes a closed bump, using 30 correctors, on bpm corresponding to BUMP_NAME (formated like:'c02-h09') with the
85
            %amplitude given by BUMP_AMPLITUDE (m)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
            %NUMVECT eigenvectors used to compute the bump, should be lower than 30. (default 10)
            %
            %outputs:
            %OR_FIT: fited orbit as from matrix response
            %ERR_EXEC, steerer strength increment in radiants used to perform the bump.
           
            
            if nargin<4 %input variable checks
            numvect=10
            end
           
            if numvect>28
             fprintf('error: numvect should be lower than29\n');
              return
            end
            
           
           
           % loads response matrix, corrector indices and strengths depending on the needed plane.
           if strfind(bump_name,'h')
               resp=obj.HRESP;
               icors=obj.MODEL.get(0,'steerh');
           st_strength=obj.HSTRENGTH;
           stname=obj.HSTNAME;
           else
               resp=obj.VRESP;
               icors=obj.MODEL.get(0,'steerv');
           st_strength=obj.VSTRENGTH;
           stname=obj.VSTNAME;
           end
116
            
117 118 119
           %converts bump name to BPM name 
           bump_name=bump_name([1 2 3 4 6 7]);
           
120
           
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            
            
            % finds in the lattice structure lists of bpm and corrector 
            %to be considered for the given bump. 
            ibpms=obj.MODEL.get(0,'BPM');
            ibpm=findcells(obj.MODEL.ring,'Device',['srdiag/beam-position/' bump_name]);
            numbpm=find(ibpm<=ibpms,1)
            numcor=find(ibpm<icors,1);
               % if numcor>numel(icors); numcor=1; end
            numcor=[numcor-20:numcor+20];
            orb=zeros(numel(ibpms),1);
            orb(numbpm)=amplitude;
            bpmok=true(1,320);
            bpmok([numbpm-3 numbpm-2 numbpm-1 numbpm+3 numbpm+2 numbpm+1])=false;
            
            orb(~bpmok)=nan;
            numcor=mod(numcor-1,288)+1;
            bpmok=mod(bpmok-1,320)+1;
            
            %calculates the steerer strengths to be applied
            [or_fit,err_exec]=calc_correction(numvect,resp(:,numcor),orb,0);
            % checks if the steerer strength is resonable and if yes aplies it.    
            
            stit=zeros(288,1);
            stit(numcor)=err_exec;    
                if numel(st_strength.set)==288
                stfin=st_strength.set+stit';
               
                else
                size(st_strength.set(obj.s_ok)) 
                stfin(obj.s_ok)=stit'+st_strength.set(obj.s_ok);
                stfin(~obj.s_ok)=st_strength.set(~obj.s_ok);
                
                end
155
                
156 157 158 159 160 161 162 163 164 165 166 167
                if sum(stfin>4.42E-4)>0
                %fprintf('too strong steerers');
                steernok=find(stfin>4.42E-4);
                    for i=1:numel(steernok)
                        error=['Strength send to corrector ' stname.read{steernok(i)} ' out of range (' num2str(1000*stfin(steernok(i))) 'mrad)']
                       
                    end
                else
                    st_strength.set=stfin;
                end
                
        end
168 169 170 171 172 173 174
                
                
         function plot_or_cells(obj,cini,cend,plane)
             %Plots the orbit and the lattice structure between cell CINI
            %and CEND. PLANE=1 refers to horizontal, PLANE=2 refers to
            %vertical.
             
175
             mach=obj.MODEL.ring;
176 177 178 179 180 181 182 183 184 185 186 187 188
             
             mark=[findcells(mach,'FamName','ID\w*') sort([findcells(mach,'FamName','SDH\w*') findcells(mach,'FamName','SDL\w*')])]
             if numel(mark)==64
             mark=mark(2:2:end);
             end
             res=mach{mark(5)}
             if plane==1
                 orb=obj.ORBITH.read;
             elseif plane==2
                 orb=obj.ORBITV.read;
             end
             
             mach=circshift(mach,-mark(mod(cini-4,32)+1));
189
            orb=circshift(reshape(orb,[],32)',-mod(cini-3,32)+1)';
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
            orb=orb(:,[1:mod(cend-cini,32)]);
            
            orb=orb(:); 
            mark_shift=[findcells(mach,'FamName','ID\w*') sort([findcells(mach,'FamName','SDH\w*') findcells(mach,'FamName','SDL\w*')])]
            %mark_shift=sort([findcells(mach,'FamName','ID\w*')+2 findcells(mach,'FamName','DL\w*A\w*_2') findcells(mach,'FamName','DL\w*E\w*_2')...
             %   findcells(mach,'FamName','JL\w*_2') findcells(mach,'FamName','DL\w*D\w*_2')]);
            if numel(mark_shift)==64
             mark_shift=mark_shift(2:2:end);
            end
            
            cells=mach(1:mark_shift(mod(cend-cini,32)));
            bpm=[findcells(cells,'Class','Monitor') findcells(cells,'FamName','BPM')];
           
            figure(1);
            clf(1);
            
            
            atbaseplot(cells);
208
            
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
            if(numel(orb)==numel(bpm))
                spos=findspos(cells,bpm);
                hold on
                yyaxis right;
                %ylim([-2 2]);
                plot(spos,orb(:)*1000);
                legend(['orbit(mm) in cells: ' num2str([cini cend])])
                hold off;
            
            else
            error='orbit length different from BPM numbers'
            end
       end
         
       function plot_blm_cells(obj,cini,cend)
             %Plots the orbit and the lattice structure between cell CINI
            %and CEND. PLANE=1 refers to horizontal, PLANE=2 refers to
            %vertical.
             
228
             mach=obj.MODEL.ring;
229 230 231 232 233 234 235 236 237 238
             
             mark=[findcells(mach,'FamName','ID\w*') sort([findcells(mach,'FamName','SDH\w*') findcells(mach,'FamName','SDL\w*')])];
             if numel(mark)==64
             mark=mark(2:2:end);
             end
             %res=mach{mark(5)}
             
             %blm=[obj.BLM.SA_Incoherencies()];
             
             
239 240
             %blm=obj.BLM.All_Status.read()));
             blm=1+mod([3:0.25:34.75],32);
241 242 243 244 245 246 247
             %%if plane==2
               %%  orb=obj.ORBITV.read;
             %%elseif plane==1
             %%    orb=obj.ORBITH.read;
             %%end
            
             mach=circshift(mach,-mark(mod(cini-4,32)+1));
248
            blm=circshift(reshape(blm,[],32)',-mod(cini-3,32)+1)';
249 250 251 252 253 254 255 256 257 258 259 260 261 262
            blm=blm(:,[1:mod(cend-cini,32)]);
            
            blm=blm(:); 
            %mark_shift=sort([findcells(mach,'FamName','ID\w*')+2 findcells(mach,'FamName','DL\w*A\w*_2') findcells(mach,'FamName','DL\w*E\w*_2')...
             %   findcells(mach,'FamName','JL\w*_2') findcells(mach,'FamName','DL\w*D\w*_2')]);
            mark_shift=[findcells(mach,'FamName','ID\w*') sort([findcells(mach,'FamName','SDH\w*') findcells(mach,'FamName','SDL\w*')])];
             if numel(mark_shift)==64
             mark_shift=mark_shift(2:2:end);
             end
            
            
            cells=mach(1:mark_shift(mod(cend-cini,32)));
            iblm=sort([findcells(cells,'FamName','ID\w*') findcells(cells,'FamName','QF7') findcells(cells,'FamName','B1H') findcells(cells,'FamName','DL\w*A\w*_2') findcells(cells,'FamName','DL\w*E\w*_2')...
                findcells(cells,'FamName','JL\w*_2') findcells(cells,'FamName','DL\w*D\w*_2')]);
263 264
            figure(2);
            clf(2);
265 266
            
            atbaseplot(cells);
267
            
268 269 270 271 272 273 274 275 276 277 278 279
            if(numel(blm)==numel(iblm))
                spos=findspos(cells,iblm);
                hold on
                yyaxis right;
                %ylim([-2 2]);
                plot(spos,blm(:)*1000);
                legend(['blm reading in cells: ' num2str([cini cend])])
                hold off;
            
            else
            error='blm vector length different from bmls numbers'
            end
280 281
       end
       
perron's avatar
perron committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
       function resp=resp_or_blm(obj,plane,strength)
           if plane ==1
               for i=1:32
                  blmini=obj.BLM(:);
                  cell=i
                  obj.BLM=obj.BLM(:)+randn(numel(obj.BLM),1); 
                  sti=obj.HSTRENGTH.read;
                  stt=sti;
                  
                  stt((i-1)*12+1)=stt((i-1)*12+1)+strength;
                 
                  obj.HSTRENGTH.set(:)=stt(:);
                  pause(4)
                  resp.blm(:,i)=obj.BLM(:)-blmini(:);
                  resp.or(:,i)=obj.ORBITH.read(:);
                  resp.stt(:,i)=stt;
                  obj.HSTRENGTH.set=sti;
                pause(4)
               end
               figure(3)
               plot(sum(resp.blm,2));
           xlabel('BLM number');
           ylabel('BLM signal sumed over all orbits');
           grid on
           end
       
       
       
310
       end
311 312 313
    end
end