CMU - Naomi's secondary event readout file

From GlueXWiki
Jump to: navigation, search
# 
#  EVENT readout list
#
#    Secondary readout list responsible for 
#    getting data from the primary readout list and then
#    passing it to the Output queue.
#
#    NSJ June 2013
#

readout list ROL2
#maximum 128,10 <-original value

maximum 37440, 100 #nchannels = 72  nsamples=128

#maximum 144,100 #1 channel, 10 samples 


polling
event readout


const ADC125_NCHANNELS=72
const ADC125_NSAMPLES=128    # even numbers only
const ADC125_MULTI=5
const ADC125_THRES=400   # 4 x (ped + 3 sig)
const ADC125_NTHRES=6  #min # consec samples over threshold

# pedestal is ~ 60 with sigma 15 to 20  all x 4 - because my decoding programs divide by 4
# ped + 5 sig is 60 + 75 = 148              x 4 = 584
# ped + 3 sig is 60 + 45 = 105              x 4 = 420


int ii;                   /*counter - input buffer*/
int x;                    /*pos in input buffer of start of channel*/

int ok[ADC125_NCHANNELS]; /*array to identify good channels*/
int multi;                /*number of good channels */
int count;                /*current tally of samples over threshold in current channel*/
int ndata;                /*size of data bank to output*/

int ch;                   /*counter - current adc channel number*/
int isamp;                /*counter - sample from current adc channel*/


begin download

  log inform "User Download 2 Executed"

end download
    
begin prestart

  variable jj, adc_id

  log inform "Entering User Prestart 2"

  init trig source EVENT
  link sync trig source EVENT 1 to ntrig and ntrig_done
  event type 1 then read EVENT 1

  rol->poll = 1;

  log inform "User Prestart 2 executed"

end prestart

begin end

  log inform "User End 2 Executed"

end end

begin pause

  log inform "User Pause 2 Executed"

end pause

begin go
  
  log inform "Entering User Go 2"
  
end go

begin trigger ntrig

#copy event
get event
    
%%

 /*printf("***event_list event length %i \n",EVENT_LENGTH); 

 for (ii=-2; ii<2; ii++) {

   printf("input[%i] %i  \n",ii,INPUT[ii]); 

 }

 */

 if (rol->dabufp != NULL) {          /* Output Pointer should be set by CODA 2.1 ROC */

  /* find out which are good channels */

  multi=0;

  for (ch=0; ch<ADC125_NCHANNELS; ch++) {
    
    ok[ch]=0;     //this channel above threshold
    count=0;  //count consecutive over-threshold samples
    
    x = ch*ADC125_NSAMPLES;

    for (isamp=0; isamp<ADC125_NSAMPLES; isamp++) {

      if ( (INPUT[x+isamp] & 0xfff) > ADC125_THRES) {
        count++;
        if (count>=ADC125_NTHRES) ok[ch]=1;
      } else {
        count=0;
      }

      if (ok[ch]==1) break;

      if ( (INPUT[x+isamp] & 0xfff0000) > ADC125_THRES) {
        count++;
        if (count>=ADC125_NTHRES) ok[ch]=1;
      } else {
        count=0;
      }
          
      if (ok[ch]==1) break;
     
    }

    if (ok[ch]==1) logMsg("  ch %i over threshold\n",ch);
    //if (ok==0) logMsg("  ch %i UNDER threshold\n",ch);

    if (ok[ch]==1) multi++;

  }


  logMsg("                 multiplicity %i\n",multi);


  /* write out event header */

  ndata = 0;

  if (multi >= ADC125_MULTI) ndata = 1 + multi*(1 + ADC125_NSAMPLES);

  *rol->dabufp++ = ndata + 2;    /* event header (size of databank to follow) */ 
  *rol->dabufp++ = INPUT[-1];    /* event header (event number) */
  *rol->dabufp = ndata;


  printf("output[-2] %i  \n", ndata + 2);
  printf("output[-1] %i  \n",INPUT[-1]);
  printf("output[0] %i  \n", ndata);



  /* now write out good channels */

  if (multi >= ADC125_MULTI) {

    rol->dabufp++;
    *rol->dabufp = multi;

    printf("output[1] %i \n",multi);

    for (ch=0; ch<ADC125_NCHANNELS; ch++) {

      if (ok[ch]==1) {

        rol->dabufp++;
        *rol->dabufp = ch;
  
        x = ch*ADC125_NSAMPLES;

        for (isamp=0; isamp<ADC125_NSAMPLES; isamp++) {

          rol->dabufp++;
          *rol->dabufp = INPUT[x+isamp];

        }

      }  /* end if ok */

    }  /* end for ch */

  }  /* end if multi */


  
//for (ii=-2;ii<EVENT_LENGTH;ii++)  /* Copy event, including Header from Input to Output */
//     *rol->dabufp++ = INPUT[ii];


 }else{
   printf("ROL2: ERROR rol->dabufp is NULL -- Event lost\n");
 }

%%

end trigger

begin done ntrig

end done

begin done

end done

begin status

end status