program LR

%{
extern double fabs();
extern double sqrt(); }%


short CH1F[524288];
short CH2F[524288];
short CH3F[524288];
short CH4F[524288];

short CH1[2048];
assign CH1 to "{USER}:ADC4:CH1";
int CH1INDX;
assign CH1INDX to "{USER}:ADC4:CH1.INDX";
int CH1PROC;
assign CH1PROC to "{USER}:ADC4:CH1.PROC";
short CH2[2048];
assign CH2 to "{USER}:ADC4:CH2";
int CH2INDX;
assign CH2INDX to "{USER}:ADC4:CH2.INDX";
int CH2PROC;
assign CH2PROC to "{USER}:ADC4:CH2.PROC";
short CH3[2048];
assign CH3 to "{USER}:ADC4:CH3";
int CH3INDX;
assign CH3INDX to "{USER}:ADC4:CH3.INDX";
int CH3PROC;
assign CH3PROC to "{USER}:ADC4:CH3.PROC";
short CH4[2048];
assign CH4 to "{USER}:ADC4:CH4";
int CH4INDX;
assign CH4INDX to "{USER}:ADC4:CH4.INDX";
int CH4PROC;
assign CH4PROC to "{USER}:ADC4:CH4.PROC";

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 ch1_mean, ch1_sigma, ch2_mean,ch2_sigma,
         ch3_mean, ch3_sigma, ch4_mean, ch4_sigma;
}%

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";

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

int MEMSIZE_R;
assign MEMSIZE_R to "{USER}:ADC4:MEMSIZE_R";
int MEMSIZE_R_PROC;
assign MEMSIZE_R_PROC to "{USER}:ADC4:MEMSIZE_R.PROC";
float maxmem;
long memrep;
long kkk;
double fff;
%{
double sqr(double a)
{
double b;

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


ss state_adc1
{
	state adc_start
	{
		when()
		{
		EVENT_OLD = EVENT;
		} state adc_wait
	}
	state adc_wait
	{
		when(EVENT !=EVENT_OLD){

		/*	logMsg("ADC_WAIT\n");  */
		EVENT_OLD = EVENT;	
		MEMSIZE_R_PROC =1;
		pvPut(MEMSIZE_R_PROC);
		pvGet(MEMSIZE_R);

		if (MEMSIZE_R == 0 ) {
		 memrep = 16;}
		else if (MEMSIZE_R == 1 ) {
		 memrep = 32; }
		else if (MEMSIZE_R == 2) {
		 memrep = 64; }
		else memrep = 128;
		maxmem = memrep*2048.0;

		/* 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;
		kkk = 0;
		for (j=0; j<memrep; j++)
		{
 		CH1INDX = j*2048;
 		CH2INDX = j*2048+524288;
 		CH3INDX = j*2048+262144;
 		CH4INDX = j*2048+786432;
		pvPut(CH1INDX);
		pvPut(CH2INDX);
		pvPut(CH3INDX);
		pvPut(CH4INDX);
		CH1PROC=1;
		CH2PROC=1;
		CH3PROC=1;
		CH4PROC=1;
		pvPut(CH1PROC);
		pvPut(CH2PROC);
		pvPut(CH3PROC);
		pvPut(CH4PROC);
		
		pvGet(CH1);
		pvGet(CH2);
		pvGet(CH3);
		pvGet(CH4);
		for (i=0;i<2048;i++)
		{ 
		/*
		CH1F[j*2048+i]=CH1[i];		
		CH2F[j*2048+i]=CH2[i];		
		CH3F[j*2048+i]=CH3[i];		
		CH4F[j*2048+i]=CH4[i]; 
		*/

		ch1_mean = ch1_mean + CH1[i];
		ch1_sigma= ch1_sigma + sqr(1.0e0*CH1[i]);
		
		ch2_mean = ch2_mean + CH2[i];		
		ch2_sigma= ch2_sigma + sqr(1.0e0*CH2[i]);		
		ch3_mean = ch3_mean + CH3[i];		
		ch3_sigma= ch3_sigma + (1.0e0*CH3[i])*(1.0e0*CH3[i]);		
		ch4_mean = ch4_mean + CH4[i];		
		ch4_sigma= ch4_sigma + sqr(1.0e0*CH4[i]);
				
		}
		}
		%{
		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 * (ch1_mean*ch1_mean))/(maxmem-1));
		ch2_mean = ch2_mean / (1.0e0*maxmem);
		ch2_sigma = sqrt((ch2_sigma - 1.0e0*maxmem * (ch2_mean*ch2_mean))/(maxmem-1));
		ch3_mean = ch3_mean / (1.0e0*maxmem);
		ch3_sigma = sqrt((ch3_sigma - 1.0e0*maxmem * (ch3_mean*ch3_mean))/(maxmem-1));
		ch4_mean = ch4_mean / (1.0e0*maxmem);
		ch4_sigma = sqrt((ch4_sigma - 1.0e0*maxmem * (ch4_mean*ch4_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);
		
		}state adc_wait

}
}


mailto: makoto.tobiyama@kek.jp
Last update: 10/Jul/2010