Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

#define USE_DAQ

// Izberi ustrezni interface v meniju projektnega okna
//  Options->CompilerDefines (dodaj /DSISVME ali /DWIENVME)
#ifdef USE_DAQ
#  ifdef SISVME
#    include "sisvme_dll.h"
#  endif
#  ifdef WIENVME
#    include "wienvme_dll.h"
#  endif
#  ifdef WUSBVME
#    include "wusbvme_dll.h"
#  endif
#  include "CAENV673A.h"
#endif

#include <utility.h>
#include <userint.h>
#include <cvirte.h>    /* Needed if linking in external compiler; harmless otherwise */
#include "rich_ui.h"

#define V673A_BASE_A24 0x130000
#define USBVME_NAME "VM0120"

#define MAPFILE "icfa2013.txt"

#define MAXDATA 2000
unsigned long dataa[3078];
unsigned long data0[MAXDATA];
unsigned long datac[1000];


int p1h, p2h, p3h ;
int p1h,rph,rID;


int pmtmaparr[8][8] ;
int pmtmap_i[64] ;
int pmtmap_j[64] ;
int data[64][64] ;
int nndata[64] ;
unsigned short acum_allch[65536] ;       
unsigned short acum_snglch[64][512] ;
//int offset=512 ;
int offset=0 ;
int min_tdc, max_tdc ;
int min_tdcR, max_tdcR ;

int single_event ;

unsigned long pmtbitmap[2] ;
unsigned long pmtbitmap_win[2] ;

int minhit ;

//*****************************************************************************

int read_pmtmap ( ) {
  FILE *fp=fopen(MAPFILE,"r");
  int ch, i, j ;
 
  while ( EOF != fscanf(fp,"%d %d %d\n",&ch,&i,&j) ) {
    if ( i>7 || j>7 ) {
      printf ( "Index out of range!!!! (%d,%d,%d)\n",ch,i,j ) ;
      continue ;
    }
    pmtmaparr[i][j]=ch ;
    pmtmap_i[ch] = i ;
    pmtmap_j[ch] = j ;
  }

  return 0 ;
}

//*****************************************************************************

int draw_histos ( ) {
  int i, ch ;
  int harr[4]={HISTOS_H_1,HISTOS_H_2,HISTOS_H_3,HISTOS_H_4} ;
 
  for ( ch=0; ch<4; ch++ ) {
    DeleteGraphPlot (p2h, harr[ch], -1, VAL_IMMEDIATE_DRAW);
 
    for ( i=0 ; i<nndata[ch] ; i++ ) {
      if ( (data[ch][i]>>16)&(0x1) ) {
        //printf ( "falling\n" ) ;
            PlotPoint (p2h, harr[ch], data[ch][i]&0xFFFF, 1.0, VAL_SOLID_SQUARE, VAL_RED);
        PlotLine (p2h, harr[ch], data[ch][i]&0xFFFF, 1.0, data[ch][i]&0xFFFF, 0.0, VAL_RED);
      } else {
        //printf ( "rising %d %X\n", ch, data[ch][i]&0xFFFF ) ;
        PlotPoint (p2h, harr[ch], data[ch][i]&0xFFFF, 1.0, VAL_EMPTY_SQUARE, VAL_BLUE);
        PlotLine (p2h, harr[ch], data[ch][i]&0xFFFF, 1.0, data[ch][i]&0xFFFF, 0.0, VAL_BLUE);
      }
    }
       
  }

   
  return 0 ;
}

//*****************************************************************************

int draw_acum_histos() {
  int hsts[64]={P3_ACUM_1,P3_ACUM_2,P3_ACUM_3,P3_ACUM_4,P3_ACUM_5,P3_ACUM_6,P3_ACUM_7,P3_ACUM_8,P3_ACUM_9,P3_ACUM_10,
                                P3_ACUM_11,P3_ACUM_12,P3_ACUM_13,P3_ACUM_14,P3_ACUM_15,P3_ACUM_16,P3_ACUM_17,P3_ACUM_18,P3_ACUM_19,P3_ACUM_20,
                                P3_ACUM_21,P3_ACUM_22,P3_ACUM_23,P3_ACUM_24,P3_ACUM_25,P3_ACUM_26,P3_ACUM_27,P3_ACUM_28,P3_ACUM_29,P3_ACUM_30,
                                P3_ACUM_31,P3_ACUM_32,P3_ACUM_33,P3_ACUM_34,P3_ACUM_35,P3_ACUM_36,P3_ACUM_37,P3_ACUM_38,P3_ACUM_39,P3_ACUM_40,
                                P3_ACUM_41,P3_ACUM_42,P3_ACUM_43,P3_ACUM_44,P3_ACUM_45,P3_ACUM_46,P3_ACUM_47,P3_ACUM_48,P3_ACUM_49,P3_ACUM_50,
                                P3_ACUM_51,P3_ACUM_52,P3_ACUM_53,P3_ACUM_54,P3_ACUM_55,P3_ACUM_56,P3_ACUM_57,P3_ACUM_58,P3_ACUM_59,P3_ACUM_60,
                                P3_ACUM_61,P3_ACUM_62,P3_ACUM_63,P3_ACUM_64} ;
  int i;               
  double grmin,grmax;

  GetCtrlVal (p1h, P1_TDC_MAX, &max_tdc ) ;
  GetCtrlVal (p1h, P1_TDC_MIN, &min_tdc ) ;
  GetCtrlVal (p1h, P1_TDC_MAX_2, &max_tdcR ) ;
  GetCtrlVal (p1h, P1_TDC_MIN_2, &min_tdcR ) ;
  //SetAxisScalingMode (p1h, P1_ACUM_WIN, VAL_XAXIS, VAL_MANUAL, min_tdc, max_tdc);

  DeleteGraphPlot (p1h, P1_ACUM, -1, VAL_IMMEDIATE_DRAW);
  DeleteGraphPlot (p1h, P1_ACUM_WIN, -1, VAL_IMMEDIATE_DRAW);
         
  PlotY (p1h, P1_ACUM, acum_allch, /*65536*/ 65536, VAL_UNSIGNED_SHORT_INTEGER,
                 VAL_VERTICAL_BAR, VAL_NO_POINT, VAL_SOLID, 1, VAL_DK_BLUE);
  PlotY (p1h, P1_ACUM_WIN, acum_allch, /*65536*/ 65536, VAL_UNSIGNED_SHORT_INTEGER,
                 VAL_VERTICAL_BAR, VAL_NO_POINT, VAL_SOLID, 1, VAL_DK_BLUE);
                               
  GetAxisScalingMode (p1h, P1_ACUM_WIN, VAL_LEFT_YAXIS, 0, &grmin, &grmax );
 
  PlotLine (p1h, P1_ACUM_WIN, min_tdcR, 0, min_tdcR, grmax, VAL_RED);
  PlotLine (p1h, P1_ACUM_WIN, max_tdcR, 0, max_tdcR, grmax, VAL_RED);
  PlotLine (p1h, P1_ACUM_WIN, min_tdc, 0, min_tdc, grmax, VAL_GREEN);
  PlotLine (p1h, P1_ACUM_WIN, max_tdc, 0, max_tdc, grmax, VAL_GREEN);

  for ( i=0; i<64; i++ ) {
        DeleteGraphPlot (p3h, hsts[i], -1, VAL_IMMEDIATE_DRAW);
         
        PlotY (p3h, hsts[i], acum_snglch[i], 256, VAL_UNSIGNED_SHORT_INTEGER,
               VAL_VERTICAL_BAR, VAL_NO_POINT, VAL_SOLID, 1, VAL_DK_BLUE);
                         
  }

  return 0 ;
}

//*****************************************************************************

int draw_evnt() {
  int i,j,color,frame;
  int iijj[8]={1,3, 7,9, 13,15, 19,21} ;
//  int iijj[8]={1,3, 7,9, 13,15, 19,21} ;

  DeleteGraphPlot (p1h, P1_EV_DI, -1, VAL_IMMEDIATE_DRAW);
  for ( i=1; i<20; i+=6 ) {
    for ( j=1; j<20; j+=6 ) {
          PlotRectangle (p1h,P1_EV_DI,i,j,i+4,j+4,VAL_DK_GRAY,VAL_TRANSPARENT);
    }
  }  
  for ( i=0; i<64; i++ ) {
    if ( (pmtbitmap_win[i>>5]>>(i&0x1F))&0x1 ) {
      color = VAL_DK_GREEN;
      frame = VAL_DK_GREEN;
    } else if ( (pmtbitmap[i>>5]>>(i&0x1F))&0x1 ) {
      color = VAL_TRANSPARENT;
      frame = VAL_RED;
    } else {
      color = -1 ;
    }
   
    if ( color!=-1 ) {
          PlotRectangle (p1h, P1_EV_DI,
                         iijj[pmtmap_i[i]]+0.1,iijj[pmtmap_j[i]]+0.1,
                         iijj[pmtmap_i[i]]+1.9,iijj[pmtmap_j[i]]+1.9,
                         frame,color);
    }
  }
  return 0 ;
}

//*****************************************************************************

int num_gr_photons() {

  int rc=0;
  long bitmap[2];
  int i;
 
  bitmap[0]=pmtbitmap_win[0];
  bitmap[1]=pmtbitmap_win[1];
  for (i=0;i<32;i++) {
    if (bitmap[0]&0x1) rc++;
    if (bitmap[1]&0x1) rc++;
    bitmap[0]>>=1;
    bitmap[1]>>=1;
  }
  printf("%08X\t%08X\n",pmtbitmap_win[0],pmtbitmap_win[1]);
  printf("num_gr_photons %d\n",rc);    
  return rc;
}

//*****************************************************************************

int initvar(unsigned long xxx[],int n) {
  for (;--n>=0;xxx[n]=0);
  return 0;
}

//*****************************************************************************

int daq(){

  static int cdaq=0;
  int dum;
  int error,i,j,fcount,fcountold=-1,nauto,fauto;
  int nfirst[3],ntrig[3],ndata[3],ndatac,neve,ntrold;
  unsigned short *j16=(unsigned short *) &j;

  unsigned long ext_c;
 
  char filename[256],path[256], fname[256];
  int maxeve,print,neve_sel=0,nueve=0;
 
  unsigned int bsr ;
  unsigned short dataxxx ;
  unsigned short edge_type, overflow, channel, ev_dat, tdc_num, last_dat, nval_dat;
  unsigned short gerror ;
  int hits=0 ;
  //int last[2] ;
  int last_ev_no = -1 ;


  if (cdaq==1) return -1;
  else cdaq=1;

// initialize VME interface            
#ifdef USE_DAQ
  error = V673A_map (0,V673A_BASE_A24, 1);
  error = V673A_init(0);
  V673A_setreg(0,0x1a,0x003e);
  V673A_setreg(0,0x1e,0x003e);
  error = V673A_clallb(0);
#endif


  SetCtrlVal (p1h, P1_LED_DAQ,1);
  SetCtrlVal (p1h, P1_TRIG, 0 ) ;
  GetCtrlVal (p1h, P1_DEBUG, &print ) ;
  GetCtrlVal (p1h, P1_NAUTO, &nauto ) ;
  GetCtrlVal (p1h, P1_AUTOSAVE, &fauto ) ;  
  GetCtrlVal (p1h, P1_NEVNT, &maxeve ) ;
  GetCtrlVal (p1h, P1_FNAME, fname );
  GetCtrlVal (p1h, P1_PATH, path );
  GetCtrlVal (p1h, P1_TDC_MAX, &max_tdc ) ;
  GetCtrlVal (p1h, P1_TDC_MIN, &min_tdc ) ;
  GetCtrlVal (p1h, P1_TDC_MAX_2, &max_tdcR ) ;
  GetCtrlVal (p1h, P1_TDC_MIN_2, &min_tdcR ) ;
  GetCtrlVal (p1h, P1_SINGLE, &single_event ) ;
  //PlotLine (p1h, P1_ACUM_WIN, min_tdcR, 0, min_tdcR, 1000, VAL_RED);
  //PlotLine (p1h, P1_ACUM_WIN, max_tdcR, 0, max_tdcR, 1000, VAL_RED);
  //PlotLine (p1h, P1_ACUM_WIN, min_tdc, 0, min_tdc, 1000, VAL_GREEN);
  //PlotLine (p1h, P1_ACUM_WIN, max_tdc, 0, max_tdc, 1000, VAL_GREEN);
  //SetAxisScalingMode (p1h, P1_ACUM_WIN, VAL_XAXIS, VAL_MANUAL, min_tdc, max_tdc);
  // start measurement
  neve=0;
  neve_sel=0;
  ntrold=0;
  rID=0;

  do {
/*
    if (( neve%nauto==0 && fauto==1 ) || (neve==0 && fauto==1)){
       fcount=neve/nauto;
       if ( fcountold < fcount ) {
             sprintf(filename,"%s\\%s_file%d.dat",path,fname,fcount);
         printf("%s\n",filename);
         fcountold=fcount ;
       }
    }
*/
 
     
    if ( rID == P1_STOP ) break ;
#ifdef USE_DAQ
    ntrig[0] = V673A_ntrig (0);
#endif
    //printf ( "%d\t", ntrig[0] ) ;
    //if (ntrig[0]!=-1) {
    if (ntrig[0]>ntrold) {
      ntrold=ntrig[0]-1;
      initvar ( data0, 1024 ) ;
     
#ifdef USE_DAQ
      ndata[0] = V673A_getevnt (0,ntrold,data0,MAXDATA);
#endif
//printf ( "%d\t", ndata[0] ) ;
     
          for ( i=0;i<ndata[0];i++ ) {
            dataa[i]=0xc3ffffff & data0[i];
//        printf ( "%d\t", data0[i] ) ;
          }
      ntrold=(ntrold+1)&0xffff;

      if ( ndata[0] >0 ) {
        pmtbitmap[0] = 0 ;
            pmtbitmap[1] = 0 ;
        pmtbitmap_win[0] = 0 ;
            pmtbitmap_win[1] = 0 ;
     
        for (i=0;i<ndata[0];i++) {
          dataxxx   =  data0[i] & 0xFFFF ;
          edge_type = (data0[i]>>16)&0x1  ;
          overflow  = (data0[i]>>17)&0x1  ;
          tdc_num   = (data0[i]>>25)&0x1  ;
          channel   = (data0[i]>>18)&0x1F|tdc_num<<5 ;
          ev_dat    = (data0[i]>>23)&0x1  ;    
          last_dat  = (data0[i]>>30)&0x1  ;
          nval_dat  = (data0[i]>>31)&0x1  ;
          if (ev_dat == 0) { // event data
                hits++ ;
                    if ( nndata[channel] < 63 ) {
                      data[channel][nndata[channel]++] = dataxxx|edge_type<<16 ;
                     
                    }
                    if ((edge_type & 0x1)==0 ) { // rising edge
                      acum_allch[dataxxx]++ ;
                      if ((0<=(dataxxx-offset))&&((dataxxx-offset)<512) ) {   // siroko okno  
                        acum_snglch[channel][dataxxx-offset]++ ;
                      }
                      if ((min_tdcR<=(dataxxx-offset)) && ((dataxxx-offset)<max_tdcR)) {   // rdece okno  
                if ( channel>31 )
                      pmtbitmap[1] |= 1<<(channel-32) ;
                    else
                          pmtbitmap[0] |= 1<<(channel) ;
                  }  
                  if ( (min_tdc<=(dataxxx-offset)) && ((dataxxx-offset)<max_tdc) ) {  // zeleno okno  
                if ( channel>31 )
                          pmtbitmap_win[1] |= 1<<(channel-32);
                        else
                          pmtbitmap_win[0] |= 1<<(channel);
                  }
                        }
              } else if (last_dat==0) { //not last datum
                //pmtbitmap[0] = 0;
                    //pmtbitmap[1] = 0;
                    last_ev_no = dataxxx;
              }
        }

                if (num_gr_photons() >= minhit) {
          draw_evnt();
          //draw_histos();
          draw_acum_histos();
              neve_sel++;
                }

      }
      neve++;
    } else {
      error = V673A_clallb(0);
      ntrold=0;
        }
    SetCtrlVal (p1h, P1_PROC, (neve*100)/maxeve ) ;
    SetCtrlVal (p1h, P1_TRIG, neve ) ;        
    do {
      nueve=GetUserEvent (0, &rph, &rID);
    } while ( rID != P1_STOP && nueve>0 );
        if ( rID == P1_STOP ) break ;
  } while ( (neve < maxeve) && (neve_sel<1 || !single_event) ) ;

  SetCtrlVal (p1h, P1_LED_DAQ,0);
  cdaq=0;
  return 0;
}

//*****************************************************************************
//*************************** MAIN ********************************************
//*****************************************************************************

int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                           LPSTR lpszCmdLine, int nCmdShow)
{
  int error;
  int i,j;
  int nevnt,cnt;
  int pmtmap[32][32] ;
  int gray[16] = {0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
                  0x777777, 0x888888, 0x999999, 0xAAAAAA, 0xBBBBBB, 0xCCCCCC, 0xDDDDDD,
                  0xEEEEEE, 0xFFFFFF } ;
  double x, y, min_r, max_r, r1, r2 ;

  ColorMapEntry grCol[16] ;

  grCol[0].dataValue.valInt = 0 ;
  grCol[0].color = gray[0] ;
  grCol[1].dataValue.valInt = 100000 ;
  grCol[1].color = gray[15] ;

 
  read_pmtmap ( ) ;
 
  for ( i=0 ; i<32; i++ ) {
    for ( j=0 ; j<32; j++ ) {
      if ( ( (((i+2)/4)%2) != 0 ) && ( (((j+2)/4)%2) != 0 ) )
        pmtmap[i][j]=1000000 ;
      else pmtmap[i][j]=0 ;
      //pmtmap[i][j]=(i+1)*(i+1)*(i+1)*(j+1) ;
    }
  }

  p1h = LoadPanel (0, "rich_ui.uir", P1);
//  p2h = LoadPanel (0, "laprich.uir", HISTOS);
  p3h = LoadPanel (0, "rich_ui.uir", P3);
  DisplayPanel (p1h);
//  DisplayPanel (p2h);
  DisplayPanel (p3h);
  for ( i=1; i<20; i+=6 ) {
    for ( j=1; j<20; j+=6 ) {
          PlotRectangle (p1h,P1_EV_DI,i,j,i+4,j+4,VAL_DK_GRAY,VAL_TRANSPARENT);
    }
  }  

#ifdef USE_DAQ
#  ifdef WUSBVME
     VME_START(USBVME_NAME);
#   else
     VME_START(NULL);
#  endif
#endif  
 
  do {
    GetUserEvent (1, &rph, &rID);
        switch (rID){
      case P1_EV_PL:
//        PlotScaledIntensity (p1h, P1_EV_DI, pmtmap, 32, 32, VAL_INTEGER,
//                             1.0, 0.0, 1.0, 0.0, grCol, 0xff0000, 2, 1, 0);
            draw_evnt();
        break;
      case P1_READ_TDC:
        daq(); //read_tdc ( ) ;
        break;
      case P1_DRAWH:
//        draw_histos ( ) ;
        draw_acum_histos ( ) ;
        break;
      case P1_SINGLE:
        GetCtrlVal (p1h, P1_SINGLE, &single_event ) ;
        break;
      case P1_CONT_DAQ:
        nevnt=0;
        cnt=0;
        minhit=0;
        GetCtrlVal (p1h, P1_NEVNT, &nevnt );
        GetCtrlVal (p1h, P1_MINHIT, &minhit );
//        printf("%d %d\n", nevnt, minhit);
//        while((read_tdc()<minhit)&&(cnt++<nevnt)) ;
        daq();  
//        draw_evnt();
        break;
      case P1_EV_DI:
        GetCtrlVal (p1h, P1_R_MIN, &min_r ) ;
        GetCtrlVal (p1h, P1_R_MAX, &max_r ) ;
        r1=min_r*2.0 ;
        r2=max_r*2.0 ;
        GetGraphCursor (p1h, P1_EV_DI, 1, &x, &y);
        printf ( "x:%5.2f\ty:%5.2f\n", x, y ) ;
        draw_evnt();
            PlotOval (p1h, P1_EV_DI, x-r1, y-r1, x+r1, y+r1, VAL_RED, VAL_TRANSPARENT);
            PlotOval (p1h, P1_EV_DI, x-r2, y-r2, x+r2, y+r2, VAL_RED, VAL_TRANSPARENT);
        break;
      case P1_PRINT:
        SetCtrlVal (p1h, P1_PL,1);
                PrintCtrl (p1h, P1_EV_DI, "event.tif", 1, VAL_FULL_PANEL);
        SetCtrlVal (p1h, P1_PL,0);
        break;
      case P1_RESET:
        break;
//      case P1_PRINT:
//        break;
      default:
        break;
    }
//    if ( rID==P1_I_GATE ) init_gate ( ) ;
} while ( (rID!=P1_EXIT) || (rph!=p1h) ) ;

//  CloseHandle(nHandle);
#ifdef USE_DAQ
  VME_STOP();
#endif  
//  printf ( "XXXX\n" ) ;
  return 0;
}