program LR2
%%#include <math.h>
%{
extern double fabs();
extern double sqrt(); 
extern double sqr();
}%

short CALL[1048576];
assign CALL to "{USER}:ADC4:CALL";


short CH1[32768];
assign CH1 to "{USER}:ADC4:CH1";
short CH2[32768];
assign CH2 to "{USER}:ADC4:CH2";
short CH3[32768];
assign CH3 to "{USER}:ADC4:CH3";
short CH4[32768];
assign CH4 to "{USER}:ADC4:CH4";

double CH1_SIGMA;
double CH1_MEAN;
double CH2_SIGMA;
double CH2_MEAN;
double CH3_SIGMA;
double CH3_MEAN;
double CH4_SIGMA;
double CH4_MEAN;

double XPOS_MEAN;
double XPOS_SIGMA;
double YPOS_MEAN;
double YPOS_SIGMA;

%{ double ch1_mean, ch1_sigma, ch2_mean,ch2_sigma,
         ch3_mean, ch3_sigma, ch4_mean, ch4_sigma;
   double maxmem;
}%

assign CH1_SIGMA to "{USER}:ADC4:CH1:SIGMA";
assign CH2_SIGMA to "{USER}:ADC4:CH2:SIGMA";
assign CH3_SIGMA to "{USER}:ADC4:CH3:SIGMA";
assign CH4_SIGMA to "{USER}:ADC4:CH4:SIGMA";
assign CH1_MEAN to "{USER}:ADC4:CH1:MEAN";
assign CH2_MEAN to "{USER}:ADC4:CH2:MEAN";
assign CH3_MEAN to "{USER}:ADC4:CH3:MEAN";
assign CH4_MEAN to "{USER}:ADC4:CH4:MEAN";

assign XPOS_MEAN to "{USER}:ADC4:XPOS:MEAN";
assign XPOS_SIGMA to "{USER}:ADC4:XPOS:SIGMA";
assign YPOS_MEAN to "{USER}:ADC4:YPOS:MEAN";
assign YPOS_SIGMA to "{USER}:ADC4:YPOS:SIGMA";

int EVENT;
assign EVENT to "{USER}:ADC4:EVENT";
monitor EVENT;
int EVENT_OLD;
int i;
int j;

long memrep;
long kkk;
double fff;
double xpm;
double xps;
double ypm;
double yps;

%{
/*
double sqr(double a)
{
double b;

b= a*a;
return b;
}
*/
}%


ss state_adc1
{
state adc_start { when() { EVENT_OLD = EVENT; maxmem=32768.0;
} state adc_wait } state adc_wait { when(EVENT !=EVENT_OLD){
/*logMsg("ADC_WAIT\n"); */
EVENT_OLD = EVENT;
pvGet(CALL);
for (i=0;i<32768;i++){ CH1[i]=CALL[i]; CH3[i]=CALL[i+262144]; CH2[i]=CALL[i+524288]; CH4[i]=CALL[i+786432]; } pvPut(CH1); pvPut(CH2); pvPut(CH3); pvPut(CH4); /* logMsg("SIZE %ld\n",memrep); */ ch1_mean = 0.0e0; ch2_mean = 0.0e0; ch3_mean = 0.0e0; ch4_mean = 0.0e0; ch1_sigma = 0.0e0; ch2_sigma = 0.0e0; ch3_sigma = 0.0e0; ch4_sigma = 0.0e0;
xpm = 0.0;
xps = 0.0; ypm = 0.0; yps = 0.0; kkk = 0; XPOS_MEAN =0.0; YPOS_MEAN =0.0; XPOS_SIGMA= 0.0; YPOS_SIGMA =0.0;
for (i=0;i<32768;i++) {
%{ ch1_mean = ch1_mean + CH1[i]*1.0; ch1_sigma= ch1_sigma + sqr(1.0e0*CH1[i]);
ch2_mean = ch2_mean + CH2[i]*1.0;
ch2_sigma= ch2_sigma + sqr(1.0e0*CH2[i]);
ch3_mean = ch3_mean + CH3[i]*1.0;
ch3_sigma= ch3_sigma + sqr(1.0e0*CH3[i]);
ch4_mean = ch4_mean + CH4[i]*1.0;
ch4_sigma= ch4_sigma + sqr(1.0e0*CH4[i]);
}%
xpm = 0.356*((CH1[i]+CH2[i])-(CH3[i]+CH4[i]))/174.65;ypm = 0.348*((CH1[i]+CH4[i])-(CH2[i]+CH3[i]))/174.65; XPOS_MEAN = XPOS_MEAN + xpm; YPOS_MEAN = YPOS_MEAN + ypm; %{ XPOS_SIGMA = XPOS_SIGMA + sqr(xpm); YPOS_SIGMA = YPOS_SIGMA + sqr(ypm); }% }
%{ ch1_mean = ch1_mean / (1.0e0*maxmem); /* printf("MEAN SIGMA %lf %lf\n",ch1_mean,ch1_sigma); */
ch1_sigma = sqrt((ch1_sigma - 1.0e0*maxmem * sqr(ch1_mean))/(maxmem-1));
ch2_mean = ch2_mean / (1.0e0*maxmem);ch2_sigma = sqrt((ch2_sigma - 1.0e0*maxmem * sqr(ch2_mean))/(maxmem-1)); ch3_mean = ch3_mean / (1.0e0*maxmem); ch3_sigma = sqrt((ch3_sigma - 1.0e0*maxmem * sqr(ch3_mean))/(maxmem-1)); ch4_mean = ch4_mean / (1.0e0*maxmem); ch4_sigma = sqrt((ch4_sigma - 1.0e0*maxmem * sqr(ch4_mean))/(maxmem-1)); XPOS_MEAN = XPOS_MEAN/(1.0e0*maxmem); YPOS_MEAN = YPOS_MEAN/(1.0e0*maxmem); XPOS_SIGMA = sqrt((XPOS_SIGMA - 1.0e0*maxmem * sqr(XPOS_MEAN))/(maxmem-1)); YPOS_SIGMA = sqrt((YPOS_SIGMA - 1.0e0*maxmem * sqr(YPOS_MEAN))/(maxmem-1));
}% CH1_MEAN = ch1_mean; CH2_MEAN = ch2_mean; CH3_MEAN = ch3_mean; CH4_MEAN = ch4_mean; CH1_SIGMA = ch1_sigma; CH2_SIGMA = ch2_sigma; CH3_SIGMA = ch3_sigma; CH4_SIGMA = ch4_sigma;
pvPut(CH1_MEAN); pvPut(CH2_MEAN); pvPut(CH3_MEAN); pvPut(CH4_MEAN); pvPut(CH1_SIGMA); pvPut(CH2_SIGMA); pvPut(CH3_SIGMA); pvPut(CH4_SIGMA); pvPut(XPOS_MEAN); pvPut(XPOS_SIGMA); pvPut(YPOS_MEAN); pvPut(YPOS_SIGMA); }state adc_wait when(delay(.1)){ }state adc_wait
}}