Subversion Repositories f9daq

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. //#include "tabela.h"
  2. //#include "proba.h"
  3.  
  4.  
  5. #include "vme.h"
  6.  
  7. //#define PTS_ADDR 0x025F0000
  8. //#define PTS_ADDR 0x26000000
  9.  
  10. #include "PtsModule.h"
  11. #define PTS_ADDR 0x61000000
  12. #include <windows.h>
  13. #include <formatio.h>
  14. #include <analysis.h>
  15. #include "toolbox.h"
  16. #include <cvirte.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19.  
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <stdint.h>
  23.  
  24. #include <signal.h>
  25. #include <utility.h>
  26. #include <ansi_c.h>
  27. #include <userint.h>
  28. #include "toolbox.h"
  29.  
  30. #include "sa02lib.h"
  31. #include "CAEN_V729.h"
  32. #include "CAEN_V729_CVI.h"
  33. #include "icfa.h"
  34.  
  35. #include "MIKRO.h"
  36. const int dsize = 144*4;
  37.  
  38. #include "H1D.h"
  39. #include "H2D.h"
  40. #include "H3D.h"
  41. #include "HDraw.h"
  42. #include "dataio.h"
  43. #include "sa02lib.h"
  44. #include "sa02_CVI.h"
  45.  
  46. int ProcessUserEvent(int pID, int rID,int mode);
  47.  
  48. const char  palaser[0xFF]="\"C:\\home\\software\\daq\\LaserDriver-Ver2.0\\Particulars LASER control.exe\"";
  49.  
  50. #define MAX_THREADS 10
  51. const char slowcname[20][20]= {"TMON0","TMON1","VDD","V2P","V2N","VSS","VTH1","VTH2", "VCC12", "VCC15" ,"VCC25", "V38P" };
  52. const int chart_control[4]= {P1_CHART_1,P1_CHART_2,P1_CHART_3,P1_CHART_4};
  53. const int hapd_serials [4]= { P1_SERIAL_1, P1_SERIAL_2, P1_SERIAL_3, P1_SERIAL_4 };
  54. const int fpga_serials [4]= { P1_SERIAL_FPGA_1, P1_SERIAL_FPGA_2, P1_SERIAL_FPGA_3, P1_SERIAL_FPGA_4 };
  55. const int hapd_onoff   [4]= { P1_ONOFF_1, P1_ONOFF_2, P1_ONOFF_3, P1_ONOFF_4 };
  56. const int p2graph   [4]= { P2_GRAPH_1, P2_GRAPH_2, P2_GRAPH_3, P2_GRAPH_4 };
  57.  
  58. #define PTS_ADDR 0x61000000
  59.  
  60. #define MIKRO_COM 4
  61. #define MIKRO_X 1
  62. #define MIKRO_Y 2
  63. #define MIKRO_Z 3
  64.  
  65. #define TRUE 1
  66. #define FALSE 0
  67. #define TIMEOUT 3
  68.  
  69. #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
  70. uint32_t gVME_CONTROLLER=WIENER_VMEMM;
  71. uint32_t gCAEN_V288=0;
  72. uint32_t gBELLEPTS=PTS_ADDR;
  73. uint32_t gMIKRO_PORT=MIKRO_COM;
  74. uint32_t gMIKRO_X=MIKRO_X, gMIKRO_Y=MIKRO_Y, gMIKRO_Z=MIKRO_Y;
  75. uint32_t gCENTER_X=176000, gCENTER_Y=170000;
  76. double gCENTER_KX=6./300, gCENTER_KY=3./300;
  77. uint32_t gCAEN_V729=0;
  78. uint32_t gCAEN_V1495=0;
  79. uint32_t gFEBParamRead=0;
  80. uint32_t gFEBMask=0xF;
  81. uint32_t gTriggerType=2;
  82. char gFEBParam[0xFF]="";
  83.  
  84. uint32_t uInterf = 0;
  85.  
  86. RUNINFO runinfo;
  87. RUNREC runrec;
  88. ENDREC endrec;
  89. POSREC posrec;
  90. EVTREC evtrec;
  91. EVTREC evtrec1;
  92. DATREC datrec;
  93. MONREC monrec;
  94. ITERATORREC iteratorrec;
  95.  
  96.  
  97. typedef struct {
  98.   int chip;
  99.   int ch;
  100.   int chx;
  101.   int chy;
  102. } EID2HAPDY;
  103.  
  104. EID2HAPDY eid2hapdxy[144];
  105.  
  106. // Variables
  107. static int p1h, pID, rID, tfID;
  108. static int p2h;
  109. static int p3h;
  110. static int p4h;
  111. static int p5h;
  112. static int pm1;
  113. static int daq_on,loop_on;
  114. static CmtThreadPoolHandle poolHandle = 0;
  115. static CmtTSQHandle  pTSQ = 0;
  116. static int pTSQData[2];
  117.  
  118. double datay[10000];
  119. double datax[10000];
  120. double datayfit[10000];
  121.  
  122. //const int mux_map[4]={ 1,3,0,2};
  123.  
  124. int ctrl_c=0;
  125. int integer_val=0;
  126. FILE *gFp=NULL;
  127.  
  128. //***********************VARIABLE ZA BRANJE NAPETOSTI***********************//
  129.  
  130. #include "CAENV288.h"
  131.  
  132. #define ReadOperationalParam  0x02 //N470
  133. #define TurnChanelOn          0x0a //N470  
  134. #define TurnChanelOff         0x0b //N470  
  135. #define ReadStatus            0x01 //SYS403
  136. #define ReadParameters        0x02 //SYS403
  137.  
  138. int hvmon=0;
  139. int hvmonFirst=0;
  140. int32_t vset[24], iset[24];
  141.  
  142.  
  143. // reads the run number from the first line in the file
  144. int GetRunNumberFromFile(char *fname) {
  145.   char line[MAX_PATHNAME_LEN];
  146.   int ndim= MAX_PATHNAME_LEN;
  147.   int current_run = -1;
  148.   FILE *fp = NULL;
  149.   ssize_t size;
  150.  
  151.   if ( GetFileInfo(fname,&size) ) fp = fopen(fname,"r");
  152.  
  153.   if (fp) {
  154.     if (fgets(line,ndim,fp)!= NULL) current_run = atoi(line);
  155.     fclose(fp);
  156.   }
  157.   return current_run;
  158.  
  159. }
  160.  
  161. int IncreaseRunNumberInFile(char *fname) {
  162.  
  163.   int current_run = GetRunNumberFromFile(fname);
  164.   FILE *fp = fopen(fname,"w");
  165.  
  166.   if (fp) {
  167.     fprintf(fp,"%d", current_run+1 );
  168.     fclose(fp);
  169.   }
  170.   return current_run+1;
  171. }
  172.  
  173.  
  174. //**************************** get station parameters from ini file
  175. int readIni(char *fname) {
  176.  
  177.   FILE *fp = NULL;
  178.   char cmd[0xFF],val[0xFF];
  179.   int ndim=400;
  180.   char line[ndim];
  181.   ssize_t size;
  182.  
  183.   if ( GetFileInfo(fname,&size) ) fp = fopen(fname,"r");
  184.   if (!fp) {
  185.     sprintf(line, "%s not found. Using default values.\n",fname);
  186.     MessagePopup ("Info", line);
  187.     return -1;
  188.   }
  189.   while (fgets(line,ndim,fp)!=NULL) {
  190.     sscanf(line,"%s%s",cmd, val);
  191.     if (cmd[0]=='#') continue;
  192.     if ( strstr(cmd,"VME_CONTROLLER")!= NULL ) {
  193.       if ( strstr(val,"WIENER_VMUSB")!= NULL ) gVME_CONTROLLER=WIENER_VMUSB;
  194.       if ( strstr(val,"WIENER_VMEMM")!= NULL ) gVME_CONTROLLER=WIENER_VMEMM;
  195.       if ( strstr(val,"CAEN_V1718")  != NULL ) gVME_CONTROLLER=CAEN_V1718;
  196.       if ( strstr(val,"SIS3153_USB")  != NULL ) gVME_CONTROLLER=SIS3153_USB;
  197.     }
  198.     if ( strstr(cmd,"CAEN_V288" )!= NULL ) gCAEN_V288 =strtoul(val,NULL,0);
  199.     if ( strstr(cmd,"BELLEPTS")  != NULL ) gBELLEPTS  =strtoul(val,NULL,0);
  200.     if ( strstr(cmd,"CAEN_V1495")  != NULL ) gCAEN_V1495  =strtoul(val,NULL,0);
  201.     if ( strstr(cmd,"MIKRO_PORT")!= NULL ) gMIKRO_PORT=strtoul(val,NULL,0);
  202.     if ( strstr(cmd,"MIKRO_X")   != NULL ) gMIKRO_X   =strtoul(val,NULL,0);
  203.     if ( strstr(cmd,"MIKRO_Y")   != NULL ) gMIKRO_Y   =strtoul(val,NULL,0);
  204.     if ( strstr(cmd,"MIKRO_Z")   != NULL ) gMIKRO_Z   =strtoul(val,NULL,0);
  205.     if ( strstr(cmd,"CENTER_X")   != NULL ) gCENTER_X   =strtoul(val,NULL,0);
  206.     if ( strstr(cmd,"CENTER_Y")   != NULL ) gCENTER_Y   =strtoul(val,NULL,0);
  207.     if ( strstr(cmd,"CENTER_KX")   != NULL ) gCENTER_KX   =strtod(val,NULL);
  208.     if ( strstr(cmd,"CENTER_KY")   != NULL ) gCENTER_KY   =strtod(val,NULL);
  209.     if ( strstr(cmd,"CAEN_V729") != NULL ) gCAEN_V729 =strtoul(val,NULL,0);
  210.     if ( strstr(cmd,"FEBParam") != NULL ) sprintf(gFEBParam,"%s", val);
  211.     if ( strstr(cmd,"UI") != NULL ) uInterf =strtoul(val,NULL,0);
  212.     if ( strstr(cmd,"FEBMask") != NULL ) gFEBMask =strtoul(val,NULL,0);
  213.     if ( strstr(cmd,"TriggerType") != NULL ) gTriggerType =strtoul(val,NULL,0);
  214.   }
  215.   fclose(fp);
  216.   return 0;
  217. }
  218. //**************************** missing declarations in the library
  219. char strbuf[0xFF];
  220.  
  221. int gLog=0;
  222. int sa02Printf(const char *format, ...) {
  223.   va_list aptr;
  224.   int ret;
  225.   FILE *flog;
  226.  
  227.   va_start(aptr, format);
  228.   ret = vsprintf(strbuf, format, aptr);
  229.   va_end(aptr);
  230.   SetCtrlVal(p1h,P1_STDIO,strbuf);
  231.  
  232.   if (gLog) {
  233.     flog = fopen ("stdio.log", "a");
  234.     fprintf (flog, "%s", strbuf);
  235.     fclose (flog);
  236.   }
  237.   return(ret);
  238. }
  239.  
  240.  
  241. int CVICALLBACK sa02timeout (int panel, int control, int event,
  242.                              void *callbackData, int eventData1, int eventData2) {
  243.   switch (event) {
  244.     case EVENT_TIMER_TICK:
  245.       sa02TimerOut = TRUE;
  246.       sa02Printf("sa02timeout\n");
  247.       //sa02Printf("TIMEOUT %s in line %d\n", __FILE__ , __LINE__);
  248.       break;
  249.   }
  250.   return 0;
  251. }
  252. void sa02Timerast (int signumber) {
  253.   sa02TimerOut = TRUE;
  254.   sa02Printf("TIMEOUT !!!\n");
  255.   SetCtrlAttribute (p1h, P1_DAQ_TIMEOUT, ATTR_ENABLED, 0);
  256. }
  257.  
  258. void sa02Tmlnk (int tout) {
  259.   sa02TimerOut = FALSE;
  260.   SetCtrlAttribute (p1h, P1_DAQ_TIMEOUT, ATTR_INTERVAL,
  261.                     (float)tout/100.);
  262.   SetCtrlAttribute (p1h, P1_DAQ_TIMEOUT, ATTR_ENABLED, 1);
  263.   //  ResetTimer (p1h, P1_DAQ_TIMEOUT);
  264. }
  265.  
  266. void sa02Tmulk ( void ) {
  267.   SetCtrlAttribute (p1h, P1_DAQ_TIMEOUT, ATTR_ENABLED, 0);
  268. }
  269. //****************************
  270.  
  271.  
  272. int EscapeString(const char *i, char *o) {
  273.   unsigned int iptr=0, optr = 0;
  274.   // walk though the input string
  275.   for (iptr = 0; iptr < strlen(i); iptr++, optr++) {
  276.     // search for
  277.     if ( (i[iptr]=='\\') ) {
  278.       sa02Printf("Escape String %d %d \n",i[iptr] , i[iptr]==92 ) ;
  279.       //sa02Printf("Escape String %d %d \n",i[iptr] , ( (i[iptr] == 92) || (i[iptr] == 42) )) ;
  280.       o[optr] = i[iptr];
  281.       optr++;
  282.     }
  283.  
  284.     o[optr] = i[iptr];
  285.   }
  286.  
  287.   o[optr] = '\0';
  288.  
  289.  
  290.   return 0;
  291. }
  292.  
  293.  
  294.  
  295. int p1plothandle=0;
  296. int p2plothandle[4]= {0,0,0,0};
  297. int p3plothandle=0;
  298.  
  299. int plothandle=0;
  300.  
  301. int phandle[4]= {0,0,0,0};
  302.  
  303.  
  304. int plothandle2dfit=0;
  305. int plothandleslix=0;
  306. int plothandlesliy=0;
  307. int plothandlefit=0;
  308.  
  309. void SigInt (int sig) {
  310.   ctrl_c = 1;
  311.   sa02TimerOut=1;
  312. }
  313.  
  314. //int sa02Verbose=0;
  315. int module_header(int recid,uint32_t *data,int len) {
  316.   data[0] = recid;
  317.   data[1] = (len >0)? len : 0 ;
  318.   return data[1]+2;
  319. }
  320.  
  321. int PrintData(uint32_t *rdata, int count) {
  322.   int j;
  323.   uint32_t i;
  324.   for (j=0; j<count; j++) {
  325.     uint32_t recid   = rdata[j++];
  326.     uint32_t len     = rdata[j++];
  327.     sa02Printf(" recid=0x%0x len=%d pos=%d(maxpos %d) val=",recid, len, j, count);
  328.     if (len>2) {
  329.       sa02Printf("\n");
  330.     }
  331.     for (i=0; i<len; i++) {
  332.       if (j< count) {
  333.         sa02Printf("0x%0x\t", rdata[j]);
  334.       }
  335.       if (i%4==3) {
  336.         sa02Printf("\n");
  337.       }
  338.       j++;
  339.     }
  340.     sa02Printf("\n");
  341.     if (len) {
  342.       j--;
  343.     }
  344.   }
  345.   return 0;
  346. }
  347.  
  348. //-------------------------------------------------------------------------
  349. int CVICALLBACK PlotSliceY (int panel, int control, int event,
  350.                             void *callbackData, int eventData1, int eventData2) {
  351.   int nslice,i;
  352.   int h2=(int) callbackData;
  353.  
  354.   switch (event) {
  355.     case EVENT_COMMIT: {
  356.       int ny       =  H2DGetNbinsY(h2);
  357.       double miny  =  H2DGetMinY(h2);
  358.       double stepy =  H2DGetStepY(h2);
  359.       GetCtrlVal(panel,P1_NSLIX,&nslice);
  360.  
  361.       for (i=0; i < ny; i++ ) {
  362.         datax[i]=H2DGetBinContent(h2,nslice,i);
  363.       }
  364.       if (ny>0) {
  365.         if (plothandleslix) {
  366.           DeleteGraphPlot (p1h, P1_GRAPHY, plothandleslix, VAL_IMMEDIATE_DRAW);
  367.         }
  368.  
  369.         plothandleslix = PlotWaveform (p1h, P1_GRAPHY, datax, ny,
  370.                                        VAL_DOUBLE, 1.0, 0.0, miny,
  371.                                        stepy, VAL_FAT_LINE,
  372.                                        VAL_EMPTY_SQUARE, VAL_SOLID, 1,
  373.                                        VAL_BLUE);
  374.       }
  375.  
  376.     }
  377.     break;
  378.   }
  379.   return 0;
  380. }
  381.  
  382.  
  383. int CVICALLBACK PlotSliceX (int panel, int control, int event,
  384.                             void *callbackData, int eventData1, int eventData2) {
  385.   int nslice,i;
  386.   int h2=(int) callbackData;
  387.  
  388.   switch (event) {
  389.     case EVENT_COMMIT: {
  390.       int nx       =  H2DGetNbinsX(h2);
  391.       double minx  =  H2DGetMinX(h2);
  392.       double stepx =  H2DGetStepX(h2);
  393.       GetCtrlVal(panel,P1_NSLIY,&nslice);
  394.       for (i=0; i < nx; i++ ) {
  395.         datax[i]=H2DGetBinContent(h2,i,nslice);
  396.       }
  397.       if (nx>0) {
  398.         if (plothandlesliy) {
  399.           DeleteGraphPlot (p1h, P1_GRAPHX, plothandlesliy, VAL_IMMEDIATE_DRAW);
  400.         }
  401.  
  402.         plothandlesliy = PlotWaveform (p1h, P1_GRAPHX, datax, nx,
  403.                                        VAL_DOUBLE, 1.0, 0.0, minx,
  404.                                        stepx, VAL_FAT_LINE,
  405.                                        VAL_EMPTY_SQUARE, VAL_SOLID, 1,
  406.                                        VAL_BLUE);
  407.       }
  408.  
  409.     }
  410.     break;
  411.   }
  412.   return 0;
  413. }
  414.  
  415.  
  416.  
  417. void plot1d(int npoints, double x0, double dx) {
  418.   sa02Printf("TEST %d\n", npoints);
  419.   if (npoints>0) {
  420.     if (plothandle) {
  421.       DeleteGraphPlot (p1h, P1_GRAPH, plothandle, VAL_IMMEDIATE_DRAW);
  422.     }
  423.     plothandle = PlotWaveform (p1h, P1_GRAPH, datay, npoints, VAL_DOUBLE,
  424.                                1.0, 0.0, x0, dx, VAL_CONNECTED_POINTS,
  425.                                VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE);
  426.   }
  427. }
  428.  
  429. /*
  430. void plotxy(int h2d){
  431.   if (plothandlexy > 0 ) DeleteGraphPlot (p2h, P2_GRAPHXY, plothandlexy, VAL_IMMEDIATE_DRAW);
  432.   RangeColors(H2DGetMin(h2d),H2DGetMax(h2d));
  433.   plothandlexy = PlotScaledIntensity (p2h, P2_GRAPHXY, H2DGetData(h2d),
  434.                    H2DGetNbinsX(h2d), H2DGetNbinsY(h2d), VAL_DOUBLE,
  435.                    H2DGetStepY(h2d),H2DGetMinY(h2d),H2DGetStepX(h2d),H2DGetMinX(h2d),
  436.                    colormap->array,
  437.                    colormap->HiColor,
  438.                    colormap->numberofColors, 1, 0);
  439.   ProcessSystemEvents ();
  440. }
  441. */
  442.  
  443. int CVICALLBACK ZoomToFebCB (int panel, int control, int event,
  444.                              void *callbackData, int eventData1, int eventData2) {
  445.   int feb=-1;
  446.   float xmin=0, xmax=0;
  447.   switch (event) {
  448.     case EVENT_COMMIT:
  449.       GetCtrlVal(panel,control,&feb);
  450.       switch (feb) {
  451.         case -1:
  452.           xmin=0;
  453.           xmax=144*4-1;
  454.           break;
  455.         default:
  456.           xmin =  feb *144;
  457.           xmax =xmin+144 -1;
  458.           break;
  459.       }
  460.       SetAxisRange (p1h, P1_GRAPH2D, VAL_MANUAL, xmin, xmax, VAL_NO_CHANGE, 0.0, 1.0);
  461.       break;
  462.   }
  463.   return 0;
  464. }
  465.  
  466.  
  467.  
  468.  
  469. void plot2d(int h2d, int nslice) {
  470.   if  (nslice < 0 ) {
  471.     nslice=0;
  472.   }
  473.   if (plothandle2dfit > 0 ) {
  474.     DeleteGraphPlot (p1h, P1_GRAPH2D, plothandle2dfit, VAL_IMMEDIATE_DRAW);
  475.     plothandle2dfit=0;
  476.   }
  477.   H2DDraw(h2d,p1h, P1_GRAPH2D, &p1plothandle);
  478.  
  479.   if (sa02Verbose > 2) {
  480.     sa02Printf("min %f max %f  x:min %f step%f y:min %f step%f\n",
  481.                H2DGetMin(h2d),H2DGetMax(h2d), H2DGetMinX(h2d),
  482.                H2DGetStepX(h2d),H2DGetMinY(h2d), H2DGetStepY(h2d));
  483.   }
  484.   SetCtrlVal(p1h, P1_NSLIY, nslice);
  485.   PlotSliceX(p1h,P1_GRAPHY,EVENT_COMMIT, NULL,0,0);
  486.   PlotSliceY(p1h,P1_GRAPHY,EVENT_COMMIT, NULL,0,0);
  487.   ProcessSystemEvents ();
  488. }
  489.  
  490. void SetDimming(int state) {
  491.   SetCtrlAttribute (p1h, P1_MULTIFPGAWRITE, ATTR_DIMMED, state);
  492.   SetCtrlAttribute (p1h, P1_DAQ, ATTR_DIMMED, state);
  493.   SetCtrlAttribute (p1h, P1_READOUT, ATTR_DIMMED, state);
  494.   SetCtrlAttribute (p1h, P1_FPGAWRITE, ATTR_DIMMED, state);
  495.   SetCtrlVal (p1h, P1_LED, state);
  496.   SetCtrlAttribute (p2h, P2_DAQ, ATTR_DIMMED, state);
  497.   SetCtrlAttribute (p2h, P2_RUNSCRIPT, ATTR_DIMMED, state);
  498.   SetCtrlAttribute (p3h, P3_DAQ, ATTR_DIMMED, state);
  499.   SetCtrlAttribute (p4h, P4_DAQ, ATTR_DIMMED, state);
  500.   SetCtrlAttribute (p1h, P1_STOP, ATTR_DIMMED, !state);
  501.   SetCtrlAttribute (p2h, P2_STOP, ATTR_DIMMED, !state);
  502.   SetCtrlAttribute (p3h, P3_STOP, ATTR_DIMMED, !state);
  503.   SetCtrlAttribute (p4h, P4_STOP, ATTR_DIMMED, !state);
  504. }
  505.  
  506.  
  507. uint16_t GetConnectedFebMask(void) {
  508.  
  509.   uint16_t mask=0;
  510.   int ison;
  511.   for (int i=0; i<4; i++) {
  512.     GetCtrlVal(p1h,hapd_onoff[i], &ison);
  513.     mask |= (ison << i);
  514.   }
  515.   sa02Printf("Connected FEBs mask, 0x%0x\n",mask);
  516.   return mask;
  517. }
  518.  
  519. int CVICALLBACK daq(void *functionData) {
  520.  
  521.   char title[256], serial[64];
  522.   int daqmode=1;
  523. #define MAXSIZE 10000
  524.   int h2=0;
  525.   int maxsize = MAXSIZE;
  526.   uint32_t *rdata;
  527.   uint32_t response[2]= {0,0};
  528.  
  529.   uint32_t fixeddata =0;
  530.   int testfixeddata =0;
  531.   uint32_t data    =0;
  532.   uint32_t data0    =0;
  533.   uint32_t dataoff =0;
  534.   uint32_t dataon =0;
  535.  
  536.   uint32_t cmd     =0;
  537.   uint32_t chip    =0;
  538.   uint32_t asicpar =0;
  539.   uint32_t asicshft=0;
  540.   uint32_t dstep   =0;
  541.   uint32_t sendswtrg  =0;
  542.   uint32_t tpenb   =0;
  543.  
  544.   uint32_t trglen;
  545.   uint16_t mask;
  546.   //uint32_t boardnumber_current=0;
  547.   // uint32_t hdr[10];
  548.  
  549.   unsigned int aborttimeout =0;
  550.   int append   =0;
  551.   int remap    =0;
  552.   int neve     =0;
  553.   int towrite  =0;
  554.   int toread   =0;
  555.   int ch       =0;
  556.   int output   =0;
  557.   int externalcmd   =0;
  558.   int i=0,j=0, ir;
  559.   int ich;
  560.   //int ison;
  561.  
  562.   char sfixeddata[0xFF]="";
  563.   char scmd[0xFF]="";
  564.  
  565.   char filename[0xFF]="";
  566.  
  567.   char externalcommand[0xFF]="";
  568.  
  569.   FILE *fp=NULL;
  570.   int nbtotal=0;
  571.   time_t t,told, tstart;
  572.   int ncount=0;
  573.   int nerrors=0;
  574.   int readerror=0;
  575.   //int writeevents=0;
  576.   double fraction=0;
  577.   int board;
  578.   double t0;
  579.  
  580.   rdata = malloc(sizeof(uint32_t)*maxsize);
  581.  
  582.   // intercept routine
  583.   if (signal (SIGINT, SigInt) == SIG_ERR) {
  584.     perror ("sigignore");
  585.   }
  586.  
  587.  
  588.   mask=GetConnectedFebMask();
  589.   GetCtrlVal(p1h,P1_OUTPUTFILE, filename);
  590.   GetCtrlVal(p1h,P1_APPEND,&append);    // append the data to the file filename
  591.   GetCtrlVal(p1h,P1_FRACTION, &fraction);
  592.  
  593.   GetCtrlVal(p1h,P1_ABORTTOUT, &aborttimeout);
  594.  
  595.   GetCtrlVal( p1h,P1_EXTERNALCMD, externalcommand);
  596.   GetCtrlVal( p1h,P1_EXTCMD, &externalcmd);
  597.  
  598.   GetCtrlVal(p1h,P1_FIXEDDATA, sfixeddata);
  599.   fixeddata =  strtoul (sfixeddata,NULL,0);
  600.   GetCtrlVal(p1h,P1_TESTFIXEDDATA, &testfixeddata);
  601.  
  602.  
  603.   GetCtrlVal(p1h,P1_TOREAD, &toread);
  604.  
  605.  
  606.   GetCtrlVal(p1h,P1_NEVE, &neve);
  607.   GetCtrlVal(p1h,P1_DSTEP, &dstep );
  608.  
  609.  
  610.   GetCtrlVal(p1h,P1_SENDSWTRIG,&sendswtrg);
  611.   GetCtrlVal(p1h,P1_INTTRGLEN, &trglen);
  612.   GetCtrlVal(p1h,P1_DAQMODE, &daqmode);
  613.   Sa02DaqMode (daqmode);
  614.   Sa02SelectTriggerWithMaskAndLength (sendswtrg, mask, trglen);
  615.  
  616.  
  617.   GetCtrlVal(p1h,P1_CHIP,&chip);
  618.   GetCtrlVal(p1h,P1_CHANNEL,&ch);
  619.   GetCtrlVal(p1h,P1_DATA,&data);
  620.   data0=data;
  621.   GetCtrlVal(p1h,P1_SCANTYPE,&towrite);
  622.   switch (towrite) {
  623.     case 1:
  624.       GetCtrlVal(p1h,P1_WRITECMD,scmd);
  625.       if (strcmp(scmd,"MUXASIC")==0) {
  626.         remap=1;
  627.       }
  628.       cmd =sa02GetCmdCode(scmd);
  629.       break;
  630.     case 3:
  631.       GetCtrlVal(p1h,P1_CHANNELOFF,scmd);
  632.       dataoff  = strtoul (scmd,NULL,0);
  633.       GetCtrlVal(p1h,P1_CHANNELON,scmd);
  634.       dataon  = strtoul (scmd,NULL,0);
  635.       chip    =data/36;
  636.       ch      =data%36;
  637.       sprintf(scmd,"channel");
  638.     case 2:
  639.       GetCtrlVal(p1h,P1_ASICPAR,scmd);
  640.       if (strlen(scmd))  cmd = sa02GetAsicCode(scmd,&asicpar, &asicshft);
  641.  
  642.       break;
  643.   }
  644.  
  645.  
  646.   if (remap) {
  647.     data = sa02MuxMap(data);
  648.     sa02Printf("MUX %d\n",data);
  649.   }
  650.  
  651.   GetCtrlVal(p1h,P1_TPENB,&tpenb);
  652.   //ReplaceAxisItem (p1h, P1_GRAPH2D, VAL_LEFT_YAXIS, 0, scmd, data0);
  653.  
  654.   if (strlen(filename)>0) {
  655.     if (sa02Verbose) {
  656.       sa02Printf("Data in the file:%s\n", filename);
  657.     }
  658.     if (gFp) {
  659.       fp=gFp;
  660.     } else {
  661.       if (append) {
  662.         fp=fopen(filename,"ab");
  663.       } else {
  664.         fp=fopen(filename,"wb");
  665.       }
  666.     }
  667.     output=1;
  668.     // run header
  669.     runinfo.id= RUNINFO_ID; // run record ID
  670.     runinfo.len= sizeof(runinfo);
  671.     runinfo.cmd= cmd;
  672.     runinfo.x0 = data;
  673.     runinfo.dx = dstep;
  674.     runinfo.nx = neve;
  675.     runinfo.chip= chip;
  676.     runinfo.ch  = ch;
  677.     runinfo.neve= toread;
  678.     runinfo.writemode= towrite;
  679.     sa02Printf("RUNINFO x0=%d nx=%d dx=%d\n", runinfo.x0,runinfo.dx,runinfo.nx);
  680.     nbtotal+=fwrite(&runinfo, 1,sizeof(runinfo),fp); //gzip
  681.   } else {
  682.     if (sa02Verbose) {
  683.       sa02Printf("Data are not written to the file!\n");
  684.     }
  685.   }
  686.  
  687.   if (!neve & (towrite || toread) ) {
  688.     neve=1;
  689.   }
  690.  
  691.  
  692.   time(&t);
  693.   tstart=t;
  694.   told=t;
  695.   t0 = Timer();
  696.  
  697.  
  698.   title[0] = 0;
  699.   for (i=0; i<4; i++) {
  700.     int febon=0;
  701.     Sa02TestPulseEnable(i, tpenb); // Enable/disable test pulse
  702.     sa02GetSerial(i, serial);
  703.     GetCtrlVal(p1h,hapd_onoff[i], &febon);
  704.     if (strstr(serial,"0x00000000000000")!=NULL && febon) {
  705.       char txt[0xFF];
  706.       sprintf(txt, "FEB %d not connected or firmware not loaded? Serial number error! Switch Off FEB!",i);
  707.       MessagePopup ("Error", txt);
  708.       return -1;
  709.     }
  710.     sprintf(title,"%s FEB%d=%s", title, i, serial);
  711.   }
  712.  
  713.   ///////////////////////////////////////////////////////////////
  714.   int icfa_visible=0;
  715.   int icfa_add=0;
  716.   GetPanelAttribute (p6h, ATTR_VISIBLE, &icfa_visible);
  717.   GetCtrlVal (p6h, MINIRICH_ADD, &icfa_add);
  718.   if (icfa_visible) {
  719.     icfa_Init();
  720.     icfa_Histo();
  721.   }  
  722.   ///////////////////////////////////////////////////////////////
  723.   SetCtrlAttribute  (p1h, P1_GRAPH2D, ATTR_LABEL_TEXT , title);
  724.   H2DInit(h2, "h2d",title, 144*4, 0,1,neve,data0, dstep);
  725.   H2DSetTitleY(h2,scmd);
  726.  
  727.   sa02Printf("H2D nx=%d ny=%d  i=%d neve=%d\n", H2DGetNbinsX(h2),H2DGetNbinsY(h2), i, neve);
  728.  
  729.  
  730.   plot2d(h2,1);
  731.   for (i=0; i<neve; i++) {
  732.     int nb = sizeof(uint32_t);
  733.     int count=0;
  734.  
  735.  
  736.     for (board=0; board<4; board++) {
  737.  
  738.       switch (towrite) {
  739.         case 1: {
  740.           rdata[count+2]= data;
  741.           rdata[count+3]= sa02Cmd(board, cmd, data, chip, ch,1,response);
  742.           count+=module_header(cmd,&rdata[count],2 );
  743.           break;
  744.         }
  745.         case 2: {
  746.           uint32_t datal = asicpar & (data << asicshft);
  747.           rdata[count+2] =  data;
  748.           rdata[count+3] =  sa02AsicWrite(board, cmd, datal, chip, ch, asicpar,asicshft);
  749.           count+=module_header(cmd ,&rdata[count],2); // recid cmd
  750.           break;
  751.         }
  752.         case 3: {
  753.           rdata[count+2] =  chip*36+ch;
  754.           rdata[count+3] =  sa02AsicWrite(board, cmd, dataon, chip, ch, asicpar,asicshft);  // switch on the channel
  755.           count+=module_header(cmd ,&rdata[count],2); // recid cmd
  756.           break;
  757.         }
  758.       }
  759.       if (towrite==3) {
  760.         SetCtrlVal(p1h,P1_SETVALUE, chip*36+ch );
  761.       } else {
  762.         SetCtrlVal(p1h,P1_SETVALUE, data);
  763.       }
  764.  
  765.  
  766.  
  767.     }
  768.  
  769.     Sa02SetNeve(toread);
  770.     sa02Reset();
  771.  
  772. //    for ( j=0; j< toread; j++)
  773.     {
  774.       int eventerror =  0;
  775.  
  776.       if ( (count +2 +dsize)  >= maxsize) {
  777.  
  778.         maxsize*=2;
  779.         sa02Printf("Increasing data buffer to %d elements\n", maxsize);
  780.         rdata=realloc(rdata ,sizeof(uint32_t)*maxsize);
  781.       }
  782.  
  783.       do {
  784.         if (sendswtrg == 1) Sa02SoftwareTrigger();
  785.         nb  = sa02Read(mask, &rdata[count+2] );
  786.       } while  ( nb==0 && !ctrl_c);
  787.       if (nb==0)  sa02Printf("nb==0 LINE=%d\n",__LINE__);
  788.  
  789.       if (testfixeddata && nb>0) {
  790.         int len = nb / sizeof(uint32_t);
  791.         for (ir=0; ir<len; ir++) {
  792.           if ( rdata[count+2+ir]!=fixeddata ) {
  793.             time(&t);
  794.             sa02Printf("INSERT INTO fixederror VALUES ( '%d', '%d','%d','%d','0x%08x','0x%08x' ) \n", t, t-tstart, i*toread+j, ir, rdata[count+2+ir], fixeddata );
  795.             eventerror++;
  796.           }
  797.         }
  798.       }
  799.       if (eventerror) {
  800.         readerror++;
  801.         if (readerror==3) {
  802.           ctrl_c = 1;
  803.           system("date >> firmware.lock");
  804.         }
  805.       } else {
  806.         readerror= 0;
  807.       }
  808.  
  809.       for (ich=0; ich<144*4; ich++) {
  810.         int xch = (143 - ich%144) + ich/144*144;
  811.         H2DFillBin(h2, xch,i,rdata[count+2+ich]);
  812.       }
  813.      
  814.       if (icfa_visible) icfa_Event(&rdata[count+2], icfa_add );
  815.        
  816.       /*
  817.       for (ich=0; ich<144; ich++) {
  818.       //        id=(35-ich/4)+gBoardNumber*36;
  819.       //        shft=(ich%4)*8;
  820.         id=(17-ich/8)+sa02BoardNumber*18;
  821.         shft=(ich%8)*4;
  822.  
  823.       //        if ( rdata[count+2+id]  & (0xFF <<shft)  ) {
  824.         if ( rdata[count+2+id]  & (0xF <<shft)  ) {
  825.           H2DFillBin(h2, ich,i,1);
  826.         }
  827.         if (sa02Verbose>2) {
  828.           sa02Printf("%02X ",(rdata[count+2+id]>> shft)& 0xF );
  829.       //        sa02Printf("%02X ",(rdata[count+2+id]>> shft)& 0xFF );
  830.         }
  831.       }
  832.       */
  833.  
  834.       if (sa02Verbose>2) sa02Printf("\n" );
  835.       if (nb>=0) count+=module_header(0x3,&rdata[count],nb/sizeof(uint32_t));  // recid 0x3
  836.  
  837.       if (ctrl_c) {
  838.         sa02Printf("ctrl_c detected ....\n");
  839.         break;
  840.       }
  841.  
  842.       time(&t);
  843.       if (t!=told ) {
  844.         double done= (double) (i)/neve;
  845.         EstimatedFinish(p1h, P1_PROGRESS, P1_ETA, tstart, done);
  846.         plot2d(h2,i-1);
  847.         GetPanelAttribute (p6h, ATTR_VISIBLE, &icfa_visible);
  848.         if (icfa_visible) icfa_Draw();
  849.         SetCtrlVal(p1h,P1_CUREVE, j);
  850.         sa02Printf("%d events in %2.2f min (%d s)  TIMEOUTS=%d %s",ncount, (double)(t-tstart)/60.,t-tstart,  nerrors, ctime(&t));
  851.       }
  852.       if (aborttimeout && (t-tstart)>aborttimeout) {
  853.         sa02Printf("Abort timeout reached ....\n");
  854.         ctrl_c=1;
  855.         break;
  856.       }
  857.       told=t;
  858.       if (nb==0) {
  859.         nerrors++;
  860.         //j--;    /// kako potem pride cez zeljeno stevil ozadetkov?
  861.       } else {
  862.         ncount++;
  863.       }
  864.     }
  865.     if (externalcmd) {
  866.       char ecmd[256];
  867.       sprintf(ecmd,"%s %u %u %u %u",externalcommand,(unsigned int)  tstart,data, rdata[2], rdata[3]);
  868.       if (sa02Verbose) {
  869.         sa02Printf("Executing external command %s\n",ecmd);
  870.       }
  871.       system(ecmd);
  872.     }
  873.  
  874.     if (output && Random(0,1)<fraction) {
  875.       evtrec.id = EVTREC_ID;
  876.       evtrec.len=count*sizeof(uint32_t)+ sizeof(evtrec);
  877.       evtrec.time= (uint32_t) time(NULL);
  878.       evtrec.nev=i;
  879.       nb = (int) fwrite( &evtrec,  1,  sizeof(evtrec),fp); //gzip
  880.       if (count) {
  881.         nb+= (int) fwrite(&rdata[0],1,count*sizeof(uint32_t),fp);  //gzip
  882.       }
  883.       if (nb!= (int) evtrec.len) {
  884.         sa02Printf("Error writing! %d!=%d\n",nb,evtrec.len);
  885.       }
  886.       nbtotal+= nb;
  887.     }
  888.  
  889.     if (sa02Verbose==1)  {
  890.       sa02Printf("[%d/%d] %u \t", i,count, (unsigned int) time(NULL));
  891.       PrintData(rdata,count);
  892.     }
  893.     if (towrite ==3) {
  894.       sa02AsicWrite(board, cmd, dataoff, chip, ch, asicpar, asicshft);
  895.       ch += dstep;
  896.       if (ch>35) {
  897.         ch-=36;
  898.         chip++;
  899.       }
  900.     } else {
  901.       data += dstep;
  902.     }
  903.     if (ctrl_c ) {
  904.       if (!testfixeddata) {
  905.         sa02Printf("Ctrl+C Program interrupted ....\n");
  906.       }
  907.       break;
  908.     }
  909.   }
  910.   plot2d(h2, i-1);
  911.   if (icfa_visible) icfa_Draw();
  912.  
  913.   if (output) {
  914.     sprintf(scmd,"channel;daqtime=%f", Timer()-t0 );
  915.     H2DSetTitleX(h2,scmd);
  916.     H2DWrite2File(h2,fp);
  917.     if (!gFp) {
  918.       fclose(fp);
  919.     }
  920.   }
  921.   if (sa02Verbose>1) sa02Printf("%d bytes written to file %s\n", nbtotal, filename);
  922.   time(&t);
  923.   if (toread && !testfixeddata) sa02Printf("%d events in %2.2f min  (%f s)  TIMEOUTS=%d  %s",ncount, (double)(Timer()-t0)/60.,Timer()-t0, nerrors, ctime(&t));
  924.   free(rdata);
  925.  
  926.   return 0;
  927. }
  928.  
  929. int CVICALLBACK quit (int panel, int event, void *callbackData,
  930.                       int eventData1, int eventData2) {
  931.   switch (event) {
  932.     case EVENT_CLOSE:
  933.       // Stop timer callbacks
  934.       SuspendTimerCallbacks();
  935.       QuitUserInterface (0);
  936.       break;
  937.   }
  938.   return 0;
  939. }
  940.  
  941. int CVICALLBACK Exit (int panel, int control, int event,
  942.                       void *callbackData, int eventData1, int eventData2) {
  943.   switch (event) {
  944.     case EVENT_COMMIT:
  945.       quit(0,0,NULL,0,0);
  946.       exit(0);
  947.   }
  948.   return 0;
  949. }
  950.  
  951. int SetParametersFromFile( const char *fname) {
  952.   uint32_t gdata;
  953.   uint32_t cdata;
  954.  
  955.   sa02AsicGlobalRegister  *greg = (sa02AsicGlobalRegister *) &gdata ;
  956.   sa02AsicChannelRegister *creg = (sa02AsicChannelRegister *) &cdata ;
  957.   unsigned short cregdata[8*144*4];
  958.   unsigned short gregdata[7*4*4];
  959. #define NDIM 400
  960.   int ndim=NDIM;
  961.   char line[NDIM];
  962.   char cmd[NDIM];
  963.   char sasic[NDIM];
  964.   char v0[NDIM];
  965.   char v1[NDIM];
  966.   int asic=0, ch=0;
  967.   int gval=0, cval=0;
  968.   int board=0;
  969.   int row=0;
  970.   uint32_t sa02code;
  971.   int i;
  972.   ssize_t size;
  973.   FILE *fp = NULL;
  974.   if ( GetFileInfo(fname,&size) ) fp = fopen(fname,"r");
  975.   if (!fp) {
  976.     sa02Printf("Error! Cannot open file %s\n",fname);
  977.     return -1;
  978.   }
  979.   for (i=0; i<8*144*4; i++) {
  980.     cregdata[i]=0;
  981.   }
  982.   for (i=0; i<7*4*4; i++) {
  983.     gregdata[i]=0;
  984.   }
  985.   gdata=0;
  986.   cdata=0;
  987.   ResetTextBox(p3h,P3_FPGAPAR,"");
  988.   while (fgets(line,ndim,fp)!=NULL) {
  989.     int nb = sscanf(line,"%s%s%s%s",cmd,sasic,v0,v1);
  990.     if (nb<1 || cmd[0]=='#') {
  991.       continue;
  992.     }
  993.     asic =   strtoul (sasic,NULL,0);
  994.     ch   =   strtoul (v0,NULL,0);
  995.     gval =   strtoul (v0,NULL,0);
  996.     cval =   strtoul (v1,NULL,0);
  997.     if (sa02Verbose>2) {
  998.       sa02Printf("%d %s",nb,line);
  999.     }
  1000.     sa02code = sa02GetCmdCode(cmd);
  1001.     if (strcmp(cmd,"MUXASIC")==0) {
  1002.       asic = sa02MuxMap(asic);
  1003.     }
  1004.     switch (nb) {
  1005.       case 1: {
  1006.         if ( sa02code ) {
  1007.           SetCtrlVal(p3h,P3_FPGAPAR,line);
  1008.  
  1009.           break;
  1010.         }
  1011.         break;
  1012.       }
  1013.       case 2: {
  1014.         if ( sa02code ) {
  1015.           SetCtrlVal(p3h,P3_FPGAPAR,line);
  1016.  
  1017.           break;
  1018.         }
  1019.         if (strcmp(cmd,"param_board")==0) {
  1020.           board= asic;
  1021.         }
  1022.         if (strcmp(cmd,"load_global")==0) {
  1023.           row= asic+1;
  1024.           greg->id=13;
  1025.           /*
  1026.           SetTableCellVal (p3h, P3_GREG, MakePoint (1,row), greg->phasecmps);
  1027.           SetTableCellVal (p3h, P3_GREG, MakePoint (2,row), greg->gain);
  1028.           SetTableCellVal (p3h, P3_GREG, MakePoint (3,row), greg->shapingtime);
  1029.           SetTableCellVal (p3h, P3_GREG, MakePoint (4,row), greg->comparator);
  1030.           SetTableCellVal (p3h, P3_GREG, MakePoint (5,row), greg->vrdrive);
  1031.           SetTableCellVal (p3h, P3_GREG, MakePoint (6,row), greg->monitor);
  1032.           SetTableCellVal (p3h, P3_GREG, MakePoint (7,row), greg->id);
  1033.           */
  1034.           gregdata[(board*4+asic)*7]   = greg->phasecmps;
  1035.           gregdata[(board*4+asic)*7+1] = greg->gain;
  1036.           gregdata[(board*4+asic)*7+2] = greg->shapingtime;
  1037.           gregdata[(board*4+asic)*7+3] = greg->comparator;
  1038.           gregdata[(board*4+asic)*7+4] = greg->vrdrive;
  1039.           gregdata[(board*4+asic)*7+5] = greg->monitor;
  1040.           gregdata[(board*4+asic)*7+6] = greg->id;
  1041.  
  1042.           break;
  1043.         }
  1044.         break;
  1045.       }
  1046.       case 3: {
  1047.         if ( sa02code) {
  1048.           SetCtrlVal(p3h,P3_FPGAPAR,line);
  1049.           break;
  1050.         }
  1051.         if (strcmp(cmd,"param_global")==0)    {
  1052.           gdata = 0;
  1053.           break;
  1054.         }
  1055.         if (strcmp(cmd,"phasecmps")==0)   {
  1056.           greg->phasecmps = gval;
  1057.           break;
  1058.         }
  1059.         if (strcmp(cmd,"gain")     ==0)   {
  1060.           greg->gain = gval;
  1061.           break;
  1062.         }
  1063.         if (strcmp(cmd,"shapingtime")==0) {
  1064.           greg->shapingtime = gval;
  1065.           break;
  1066.         }
  1067.         if (strcmp(cmd,"comparator")==0)  {
  1068.           greg->comparator = gval;
  1069.           break;
  1070.         }
  1071.         if (strcmp(cmd,"vrdrive")==0)     {
  1072.           greg->vrdrive = gval;
  1073.           break;
  1074.         }
  1075.         if (strcmp(cmd,"monitor")==0)     {
  1076.           greg->monitor = gval;
  1077.           break;
  1078.         }
  1079.         if (strcmp(cmd,"load_ch")==0)   {
  1080.           row = board*144+asic*36+ch+1;
  1081.           /*
  1082.           SetTableCellVal (p3h, P3_CREG, MakePoint (1,row), asic);
  1083.           SetTableCellVal (p3h, P3_CREG, MakePoint (2,row), ch);
  1084.           SetTableCellVal (p3h, P3_CREG, MakePoint (3,row), creg->decaytime);
  1085.           SetTableCellVal (p3h, P3_CREG, MakePoint (4,row), creg->offset);
  1086.           SetTableCellVal (p3h, P3_CREG, MakePoint (5,row), creg->fineadj_unipol);
  1087.           SetTableCellVal (p3h, P3_CREG, MakePoint (6,row), creg->fineadj_diff);
  1088.           SetTableCellVal (p3h, P3_CREG, MakePoint (7,row), creg->tpenb);
  1089.           SetTableCellVal (p3h, P3_CREG, MakePoint (8,row), creg->kill);
  1090.           */
  1091.  
  1092.           cregdata[(row -1)*8]   = (unsigned short) asic;
  1093.           cregdata[(row -1)*8+1] = (unsigned short) ch;
  1094.           cregdata[(row -1)*8+2] = creg->decaytime;
  1095.           cregdata[(row -1)*8+3] = creg->offset;
  1096.           cregdata[(row -1)*8+4] = creg->fineadj_unipol;
  1097.           cregdata[(row -1)*8+5] = creg->fineadj_diff;
  1098.           cregdata[(row -1)*8+6] = creg->tpenb;
  1099.           cregdata[(row -1)*8+7] = creg->kill;
  1100.           cdata=0;
  1101.           break;
  1102.         }
  1103.         if (strcmp(cmd,"select")==0)    {
  1104.           sa02Printf ("%s not implemeted yet\n", cmd);
  1105.           break;
  1106.         }
  1107.         break;
  1108.       }
  1109.       case 4: {
  1110.         if (strcmp(cmd,"param_ch")==0)    {
  1111.           cdata = 0;
  1112.           break;
  1113.         }
  1114.         if (strcmp(cmd,"decaytime")==0)  {
  1115.           creg->decaytime = cval;
  1116.           break;
  1117.         }
  1118.         if (strcmp(cmd,"offset")==0)     {
  1119.           creg->offset = cval;
  1120.           break;
  1121.         }
  1122.         if (strcmp(cmd,"fineadj_unipol")==0) {
  1123.           creg->fineadj_unipol = cval;
  1124.           break;
  1125.         }
  1126.         if (strcmp(cmd,"fineadj_diff")==0) {
  1127.           creg->fineadj_diff   = cval;
  1128.           break;
  1129.         }
  1130.         if (strcmp(cmd,"tpenb")==0)      {
  1131.           creg->tpenb = cval;
  1132.           break;
  1133.         }
  1134.         if (strcmp(cmd,"kill")==0)       {
  1135.           creg->kill  = cval;
  1136.           break;
  1137.         }
  1138.         break;
  1139.       }
  1140.     }
  1141.  
  1142.   }
  1143.   fclose(fp);
  1144.   SetTableCellRangeVals (p3h, P3_CREG, MakeRect (1, 1, 144*4, 8), cregdata, VAL_ROW_MAJOR);
  1145.   SetTableCellRangeVals (p3h, P3_GREG, MakeRect (1, 1, 4*4, 7)  , gregdata, VAL_ROW_MAJOR);
  1146.  
  1147.   sa02Printf("Parameters loaded from file %s to Parameter Panel \n", fname);
  1148.   return 0;
  1149. }
  1150.  
  1151. void CVICALLBACK MenuCB(int menubar, int menuItem, void *callbackData, int panel) {
  1152.   switch (menuItem) {
  1153.     case MENU_OPERATIO_CAEN_V729:
  1154.       DisplayPanel(p4h);
  1155.       break;
  1156.     case MENU_OPERATIO_SA02CFG :
  1157.       DisplayPanel(p3h);
  1158.       break;
  1159.     case MENU_OPERATIO_2DSCAN:
  1160.       DisplayPanel(p2h);
  1161.       break;
  1162.     case MENU_OPERATIO_LASER:
  1163.       DisplayPanel(p5h);
  1164.       break;
  1165.     case MENU_MINIRICH:
  1166.       DisplayPanel(p6h);
  1167.       break;  
  1168.     case MENU_OPERATIO_EXIT :
  1169.       quit(0,0,NULL,0,0);
  1170.       exit(0);
  1171.       //loop_on=0;
  1172.       break;
  1173.     case MENU_HELP :
  1174.       sa02Help();
  1175.       break;
  1176.   }
  1177. }
  1178.  
  1179. int CVICALLBACK daq_readonly(void *functionData) {
  1180.   int h2=0;
  1181.   uint32_t trglen;
  1182.   uint32_t data[10000];
  1183.   uint16_t mask;
  1184.   //int ison;
  1185.   //int *fdata= (int *) functionData;
  1186.   int nbit,neve,nb,ch,i,j,sendswtrg;
  1187.  
  1188.   time_t t,told, tstart;
  1189.   //int gBoardNumber=0;
  1190.   int ploteachevent =0;
  1191.  
  1192.   GetCtrlVal(p1h,P1_PLOTEACHEVENT, &ploteachevent);
  1193.   GetCtrlVal(p1h,P1_TOREAD , &neve);
  1194.   GetCtrlVal(p1h,P1_NSLIX , &nbit);
  1195.   if (nbit>7) {
  1196.     nbit=7;
  1197.   }
  1198.   GetCtrlVal(p1h,P1_SENDSWTRIG,&sendswtrg);
  1199.   GetCtrlVal(p1h,P1_INTTRGLEN, &trglen);
  1200.  
  1201.   mask=GetConnectedFebMask();
  1202.   Sa02SelectTriggerWithMaskAndLength (sendswtrg, mask, trglen);
  1203.   Sa02SetNeve(2);
  1204.  
  1205.   H2DInit(h2,"hbit","Bit Occupancy", 144*4, 0,1,8,0, 1);
  1206.   H2DSetTitleX(h2,"channel");
  1207.   H2DSetTitleY(h2,"Data bit");
  1208.   time(&t);
  1209.   tstart=t;
  1210.   told=t;
  1211.   for ( j=0; j< neve; j++) {
  1212.     do {
  1213.       if (sendswtrg==1) Sa02SoftwareTrigger();
  1214.       nb  = sa02Read(mask, data );
  1215.     } while  ( nb==0 && !ctrl_c);
  1216.     if (nb==0)  sa02Printf("nb==0 LINE=%d\n",__LINE__);
  1217.  
  1218.     if (ctrl_c==1) break;
  1219.  
  1220.     for (ch=0; ch<144*4; ch++) {
  1221.       int board = ch/144;
  1222.       int xch   = (143 - ch%144) + board *144;
  1223.       for (i=0; i<8; i++) if (data[ch] & (1<<i) ) H2DFillBin(h2,xch,i,1);
  1224.     }
  1225.     /*
  1226.     for (ch=0; ch<144*4; ch++)
  1227.     {
  1228.       id=(35-ch/4)+gBoardNumber*36;
  1229.       shft=(ch%4)*8;
  1230.       for (i=0; i<8; i++)
  1231.       {
  1232.         if (data[id] & (1<<(i+shft)) )
  1233.         {
  1234.           H2DFillBin(h2,ch,i,1);
  1235.         }
  1236.       }
  1237.       if (sa02Verbose>2)
  1238.       {
  1239.         sa02Printf("%02X ",(data[id]>> shft)& 0xFF );
  1240.       }
  1241.     }
  1242.     */
  1243.  
  1244.     if (sa02Verbose>2) {
  1245.       sa02Printf("\n" );
  1246.     }
  1247.  
  1248.     time(&t);
  1249.     if (t!=told || ploteachevent) {
  1250.       sa02Printf("%d events in %2.2f min (%d s) %s",j, (double)(t-tstart)/60.,t-tstart, ctime(&t));
  1251.       GetCtrlVal(p1h,P1_NSLIY,&nbit);
  1252.       plot2d(h2, nbit);
  1253.       GetCtrlVal(p1h,P1_PLOTEACHEVENT, &ploteachevent);
  1254.       SetCtrlVal(p1h,P1_CUREVE, j);
  1255.       ProcessSystemEvents ();
  1256.       ProcessSystemEvents ();
  1257.     }
  1258.  
  1259.     told=t;
  1260.  
  1261.   }
  1262.  
  1263.   plot2d(h2, nbit);
  1264.   SetCtrlVal(p1h,P1_CUREVE, j);
  1265.  
  1266.   return 0;
  1267. }
  1268.  
  1269. int CVICALLBACK LoadParameters (int panel, int control, int event,
  1270.                                 void *callbackData, int eventData1, int eventData2) {
  1271.   char fname[0xFF];
  1272.   uint16_t mask;
  1273.   switch (event) {
  1274.     case EVENT_COMMIT:
  1275.       mask=GetConnectedFebMask();
  1276.       sa02Printf("LoadParameters 0x%0x\n", mask);
  1277.       GetCtrlVal(p3h, P3_INPUTFILE, fname);
  1278.       GetCtrlVal(p1h,P1_BOARDTYPE,&sa02BoardType);
  1279.       SetCtrlAttribute (panel, control, ATTR_DIMMED, 1);
  1280.       sa02LoadParametersFromFile(fname, mask);
  1281.       SetCtrlAttribute (panel, control, ATTR_DIMMED, 0);
  1282.  
  1283.       break;
  1284.   }
  1285.   return 0;
  1286. }
  1287.  
  1288. int SlowControl(uint32_t board, FILE *fp) {
  1289.   uint32_t data;//,step,cmd;//,response[2]= {0,0};
  1290.   int chip,channel,i;
  1291.   //double doubleval;
  1292.   char saddress[0xFF];
  1293.   double sdata[20];
  1294.  
  1295.   chip=0;
  1296.   channel=0;
  1297.   data=0;
  1298.  
  1299.   GetCtrlVal(p1h,P1_BOARDTYPE,&sa02BoardType);
  1300.   sa02Status(board,saddress,sdata);
  1301.   SetCtrlVal(p1h,fpga_serials[board],saddress);
  1302.   if (fp) {
  1303.     fprintf(fp,"<slowc>\n<id>%d</id>\n", board );
  1304.     fprintf(fp,"<dna>%s</dna>\n",saddress );
  1305.     for (i=0; i<12; i++) fprintf(fp,"<%s>%f</%s>\n",slowcname[i],sdata[i],slowcname[i] );
  1306.     fprintf(fp,"</slowc>\n" );
  1307.   }
  1308.   PlotStripChart (p1h, chart_control[board], sdata, 12, 0, 0, VAL_DOUBLE);
  1309.   return 0;
  1310. }
  1311.  
  1312.  
  1313. int WriteChannelParameters ( int offset, int fine_offset, int tpenb) {
  1314.  
  1315.   uint32_t response[2]= {0,0};
  1316.   uint32_t cdata;
  1317.  
  1318.   sa02AsicChannelRegister *creg = (sa02AsicChannelRegister *) &cdata ;
  1319.   unsigned int i=0, j=0;
  1320.   int row=0;
  1321.   unsigned short val,asic,ch, board;
  1322.   uint16_t mask=GetConnectedFebMask();
  1323.   for (j=0; j<144*4; j++) {
  1324.     i = j%144;
  1325.     board = (unsigned short) j/144;
  1326.     asic=(unsigned short )(i/36);
  1327.     ch=(unsigned short )(i%36);
  1328.     row=j+1;
  1329.     GetTableCellVal (p3h, P3_CREG, MakePoint (1,row), &asic);
  1330.     GetTableCellVal (p3h, P3_CREG, MakePoint (2,row), &ch);
  1331.     GetTableCellVal (p3h, P3_CREG, MakePoint (3,row), &val);
  1332.     creg->decaytime=val;
  1333.     creg->offset = offset;
  1334.     creg->fineadj_unipol = fine_offset;
  1335.     GetTableCellVal (p3h, P3_CREG, MakePoint (6,row), &val);
  1336.     creg->fineadj_diff = val;
  1337.     creg->tpenb = tpenb;
  1338.     //sa02Printf("===%d   %d\n",i, tpenb);
  1339.     GetTableCellVal (p3h, P3_CREG, MakePoint (8,row), &val);
  1340.     creg->kill = val;
  1341.     creg->unused=0;
  1342.     if ( mask &(1<<board)) sa02Cmd(board,SA0x_ASIC0_CREG, cdata, asic, ch,1,response);
  1343.  
  1344.     if (ctrl_c) {
  1345.       break;
  1346.     }
  1347.   }
  1348.   return 0;
  1349. }
  1350.  
  1351. int FebTestAna(void) {
  1352.   int current_run;
  1353.   char cmdCommand[0xFF];
  1354.   current_run = GetRunNumberFromFile("current_run.txt");
  1355.   sprintf(cmdCommand ,"C:/root/bin/root.exe \"../macros/febreport.cxx(\\\"data\\\", %d )\"", current_run);
  1356.   sa02Printf("%s\n",cmdCommand);
  1357.   LaunchExecutable(cmdCommand);
  1358.   return 0;
  1359. }
  1360.  
  1361. int FebTest(void) {
  1362.   int board=0, j=0, value, step;
  1363.  
  1364.   FILE *fp;
  1365.   int ndim=400;
  1366.   char line[ndim];
  1367.   int current_run=1;
  1368.   time_t t;
  1369.   int start;
  1370.   int addheader=1;
  1371.   char filename[0xFF];
  1372.   char cmdCommand[0xFF];
  1373.  
  1374.   uint32_t mask=GetConnectedFebMask();
  1375.  
  1376.   LoadParameters(p3h, P3_LOADPAR,EVENT_COMMIT,NULL,0,0);
  1377.  
  1378.  
  1379.   fp = fopen ("current_run.txt","r");
  1380.   if (fp) {
  1381.     if (fgets(line,ndim,fp)!=NULL) current_run=atoi(line)+1;
  1382.     fclose(fp);
  1383.     fp= NULL;
  1384.   }
  1385.  
  1386.   sprintf(line,"data\\febtest_%04d.dat",current_run );
  1387.   SetCtrlVal(p1h,P1_OUTPUTFILE, line);
  1388.  
  1389.   SetCtrlVal(p1h,P1_SENDSWTRIG,3 ); // set internal trigger
  1390.   SendTriggerTypeCB(p1h,P1_TEST, EVENT_COMMIT,NULL, 0,0);
  1391.  
  1392.   ThresholdLinearityCB (p1h, P1_THRVSADC, EVENT_COMMIT, NULL, 0, 0 );
  1393.  
  1394.   GetCtrlVal(p1h,P1_OUTPUTFILE, filename);
  1395.   fp = fopen(line, "ab");
  1396.   if(fp) {
  1397.     for (board=0; board<4; board++) if ( mask &(1<<board))  H1DWrite2File(board,fp);
  1398.     fclose(fp);
  1399.   }
  1400.  
  1401.  
  1402.   sprintf(line,"data/febslowc_%04d.xml",current_run );
  1403. //  SetCtrlVal(p1h, P1_RUNID, current_run);
  1404.   fp = fopen (line,"w");
  1405.   fprintf(fp,"<febtest>\n");
  1406.   time(&t);
  1407.   fprintf(fp,"<time>%s</time>\n", ctime(&t));
  1408.  
  1409.  
  1410.  
  1411.   if (!ctrl_c)  for (board=0; board<4; board++) {
  1412.       if ( mask &(1<<board)) {
  1413.         for (j=0; j<10; j++) {
  1414.           SlowControl(board,fp);
  1415.           ProcessSystemEvents ();
  1416.           if (ctrl_c) break;
  1417.         }
  1418.       }
  1419.       if (ctrl_c) break;
  1420.     }
  1421.  
  1422.  
  1423.   fprintf(fp,"</febtest>\n");
  1424.   fclose(fp);
  1425.  
  1426.  
  1427.   fp = fopen ("current_run.txt","w");
  1428.   fprintf(fp,"%d\n", current_run);
  1429.   fclose(fp);
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.   WriteChannelParameters ( 0,0,0);   // Test pulse on all channels
  1436.   for (board=0; board<4; board++) if ( mask &(1<<board)) SlowControl(board,fp);
  1437.   SetCtrlVal(p1h,P1_TPENB,1);    // enable test pulse
  1438.  
  1439.   SetCtrlVal(p1h,P1_DATA, 350);
  1440.   SetCtrlVal(p1h,P1_NEVE, 150);
  1441.  
  1442.   GetCtrlVal(p1h,P1_DATA, &start);
  1443.  
  1444.   daq(NULL);
  1445.   SetCtrlVal(p1h,P1_TPENB,0);    // disable test pulse
  1446.  
  1447.  
  1448.   // coarse offset
  1449.   step=1;
  1450.   if (!ctrl_c)  for (value=0; value<16; value+=step) {
  1451.       if (addheader) {
  1452.         iteratorrec.id = ITERATORREC_ID;
  1453.         iteratorrec.len = sizeof(iteratorrec);
  1454.         iteratorrec.value = value;
  1455.         iteratorrec.n     = 16;
  1456.         iteratorrec.step  = step;
  1457.         iteratorrec.level = 1;
  1458.         GetCtrlVal(p1h,P1_OUTPUTFILE, filename);
  1459.         fp = fopen(filename, "ab");
  1460.         if(fp) {
  1461.           fwrite (&iteratorrec, 1, iteratorrec.len, fp);
  1462.           fclose(fp);
  1463.         }
  1464.       }
  1465.  
  1466.       WriteChannelParameters ( value,0,1);
  1467.  
  1468.       SetCtrlVal(p1h,P1_DATA,start - (value%8) * 13 + (value/8)*13*8 );
  1469.       sa02Printf("offset=>%d\n", value);
  1470.       daq(NULL);
  1471.       if (ctrl_c) break;
  1472.     }
  1473.  
  1474.   // fine offset
  1475.   if (!ctrl_c)  for (value=0; value<16; value+=step) {
  1476.       if (addheader) {
  1477.         iteratorrec.id = ITERATORREC_ID;
  1478.         iteratorrec.len = sizeof(iteratorrec);
  1479.         iteratorrec.value = value;
  1480.         iteratorrec.n     = 16;
  1481.         iteratorrec.step  = step;
  1482.         iteratorrec.level = 1;
  1483.         GetCtrlVal(p1h,P1_OUTPUTFILE, filename);
  1484.         fp = fopen(filename, "ab");
  1485.         if(fp) {
  1486.           fwrite (&iteratorrec, 1, iteratorrec.len, fp);
  1487.           fclose(fp);
  1488.         }
  1489.       }
  1490.  
  1491.  
  1492.       WriteChannelParameters (0, value,1);
  1493.  
  1494.       SetCtrlVal(p1h,P1_DATA, start - (value%8)  + (value/8)*8 );
  1495.       sa02Printf("offset=>%d\n", value);
  1496.       daq(NULL);
  1497.       if (ctrl_c) break;
  1498.     }
  1499.  
  1500.   SetCtrlVal(p1h,P1_DATA, start);
  1501.  
  1502.   GetCtrlVal(p1h,P1_OUTPUTFILE, filename);
  1503.  
  1504.   sprintf(cmdCommand,"..\\sa02read -i %s -o data\\febtest_%04d.root", filename, current_run);
  1505.   sa02Printf("%s\n",cmdCommand);
  1506.   LaunchExecutable(cmdCommand);
  1507.  
  1508.   //sprintf(cmdCommand ,"C:/root/bin/root.exe ../macros/febreport.cxx(\\\"data/febtest_%04d.root\\\")", current_run);
  1509.   sprintf(cmdCommand ,"C:/root/bin/root.exe \"../macros/febreport.cxx(\\\"data\\\", %d )\"", current_run);
  1510.   sa02Printf("%s\n",cmdCommand);
  1511.   LaunchExecutable(cmdCommand);
  1512.  
  1513.   sprintf(line,"%s", "test.dat");
  1514.   SetCtrlVal(p1h,P1_OUTPUTFILE, line);
  1515.   //daq(NULL);
  1516.   return 0;
  1517. }
  1518.  
  1519.  
  1520. int CVICALLBACK ThresholdLinearityCB (int panel, int control, int event,
  1521.                                       void *callbackData, int eventData1, int eventData2) {
  1522.   const int kmax= 1024;
  1523.   const int boardmax= 4;
  1524.  
  1525.   switch (event) {
  1526.     case EVENT_COMMIT: {
  1527.       uint32_t val,retval, board,data,response[2]= {0,0};
  1528.       int k;
  1529.       int chip,channel;
  1530.       double doubleval;
  1531.       time_t t,told;
  1532.       char title[0xFF];
  1533.       char name[0xFF];
  1534.       int h1=0;
  1535.       uint16_t mask=GetConnectedFebMask();
  1536.       chip=0;
  1537.       channel=0;
  1538.       GetCtrlVal(p1h,P1_BOARDTYPE,&sa02BoardType);
  1539.  
  1540.       for (board=0; board<boardmax; board++) if ( mask &(1<<board)) {
  1541.           char serial[0xFF];
  1542.           GetCtrlVal(p1h,hapd_serials[board],serial );
  1543.           sprintf(title,"VTH1 linearity, position  %d sn=%s", board, serial);
  1544.           sprintf(name,"vth1_%d", board);
  1545.           H1DInit(h1+board, name,title, kmax, 0 ,1);
  1546.           H1DSetTitleX(h1+board,"VTH1");
  1547.           H1DSetTitleY(h1+board,"ADC(VTH1)");
  1548.           SetCtrlAttribute  (p1h, P1_GRAPH, ATTR_XNAME, H1DGetTitleX(h1+board) );
  1549.           SetCtrlAttribute  (p1h, P1_GRAPH, ATTR_YNAME, H1DGetTitleY(h1+board) );
  1550.           //SetCtrlAttribute  (p1h, P1_GRAPH, ATTR_LABEL_TEXT , H1DGetTitle(h1+board) );
  1551.           if (phandle[board]> 0 ) DeleteGraphPlot (p1h, P1_GRAPH, phandle[board], VAL_IMMEDIATE_DRAW);
  1552.           phandle[board] = 0;
  1553.         }
  1554.       for (board=0; board<boardmax; board++) if ( mask &(1<<board)) {
  1555.           val=sa02Cmd(board, FEB_MUX, data=0x20, chip, channel ,1,response);  // set mux to  VTH1
  1556.           time(&t);
  1557.           told=t;
  1558.           if (ctrl_c) break;
  1559.           for (k=0; k<kmax; k++) {
  1560.             if (ctrl_c) break;
  1561.             SetCtrlVal(p1h,P1_SETSINGLEVALUE,k);
  1562.             SetCtrlVal(p1h,P1_CUREVE,k);
  1563.             retval=sa02Cmd(board, FEB_VTH1 , k  , chip, channel ,1, response);
  1564.             val=sa02Cmd(board, FEB_ADC_READ, data, chip, channel ,1,response);
  1565.             if (sa02BoardType>2)
  1566.               doubleval = (2*((val & 0xFFF) + 0.5)/0x1000 - 1.) * 3.3;
  1567.             else
  1568.               doubleval = ((val & 0xFFF) + 0.5)/0x1000 * 3.3;
  1569.  
  1570.  
  1571.             H1DFillBin(h1+board, k, doubleval);
  1572.             if (t!=told || k==kmax-1 ||k==0 ) {
  1573.               H1DDraw(h1+board,p1h, P1_GRAPH, &phandle[board]);
  1574.               sa02Printf("[%d] set VTH1=%d  ADC(VTH1)= %6.3f V\n",board, k,  doubleval);
  1575.               SetCtrlVal(p1h,P1_CUREVE, k);
  1576.               ProcessSystemEvents ();
  1577.             }
  1578.             told=t;
  1579.             time(&t);
  1580.           }
  1581.         }
  1582.       break;
  1583.     }
  1584.   }
  1585.   return 0;
  1586. }
  1587.  
  1588.  
  1589. int CVICALLBACK MultiFpgaWrite (void *functionData) {
  1590.   uint32_t val, board,data,step,cmd,response[2]= {0,0};
  1591.   char  scmd[0xFF],sdata[0xFF];
  1592.   int chip, channel;
  1593.   char filename[0xFF];
  1594.   double delay, doubleval=0, x0, dx;
  1595.   int neve,i,daqexe,addheader;
  1596.   time_t t,told, tstart;
  1597.   FILE *fp;
  1598.   //int h2=0;
  1599.   int *fdata= (int *) functionData;
  1600.  
  1601.   datrec.id = DATREC_ID;
  1602.   datrec.len = sizeof(datrec);
  1603.   GetCtrlVal(p1h,P1_MULTINEVE , &neve);
  1604.   GetCtrlVal(p1h,P1_WAIT , &delay);
  1605.   GetCtrlVal(p1h, P1_SINGLEWRITECMD, scmd);
  1606.   cmd=sa02GetCmdCode(scmd);
  1607.   GetCtrlVal(p1h,P1_BOARDNUMBER, &board);
  1608.   GetCtrlVal(p1h,P1_BOARDTYPE,&sa02BoardType);
  1609.   GetCtrlVal(p1h, P1_SINGLEDATA, sdata);
  1610.   data =  strtoul (sdata,NULL,0);
  1611. //        if (strcmp(scmd,"MUXASIC")==0) {
  1612. //               data = sa02MuxMap(data);
  1613. //               sa02Printf("MUX %d\n",data);
  1614. //        }
  1615.   GetCtrlVal(p1h, P1_SINGLEDATA, sdata);
  1616.   data =  strtoul (sdata,NULL,0);
  1617.   x0=(double)data;
  1618.   GetCtrlVal(p1h, P1_DSTEP0, &step);
  1619.   dx=(double)step;
  1620.   GetCtrlVal(p1h,P1_SINGLECHIP,&chip);
  1621.   GetCtrlVal(p1h,P1_SINGLECHANNEL,&channel);
  1622.   if (fdata[0]==P1_FPGAWRITE) {
  1623.     neve=1;
  1624.   }
  1625.   GetCtrlVal(p1h,P1_DAQEXE,&daqexe);
  1626.   GetCtrlVal(p1h,P1_ADDHEADER,&addheader);
  1627.   time(&t);
  1628.   tstart=t;
  1629.   told=t;
  1630.   if (addheader) {
  1631.     iteratorrec.id = ITERATORREC_ID;
  1632.     iteratorrec.len = sizeof(iteratorrec);
  1633.     iteratorrec.value = data;
  1634.     iteratorrec.n     = neve;
  1635.     iteratorrec.step  = step;
  1636.     iteratorrec.level = 2;
  1637.     GetCtrlVal(p1h,P1_OUTPUTFILE, filename);
  1638.     fp = fopen(filename, "ab");
  1639.     if(fp) {
  1640.       fwrite (&iteratorrec, 1, iteratorrec.len, fp);
  1641.       fclose(fp);
  1642.     }
  1643.   }
  1644.   for (i=0; i<neve; i++) {
  1645.     SetCtrlVal(p1h,P1_SETSINGLEVALUE,data);
  1646.     SetCtrlVal(p1h,P1_CUREVE,i);
  1647.     val=sa02Cmd(board, cmd, data, chip, channel ,1, response);
  1648.     if (addheader) {
  1649.       datrec.cmd    =cmd;
  1650.       datrec.data   =data;
  1651.       datrec.chip   =chip;
  1652.       datrec.channel=channel;
  1653.       datrec.retval =val;
  1654.       GetCtrlVal(p1h,P1_OUTPUTFILE, filename);
  1655.       fp = fopen(filename, "ab");
  1656.       if(fp) {
  1657.         fwrite (&datrec, 1, datrec.len, fp);
  1658.         fclose(fp);
  1659.       }
  1660.     }
  1661.     switch (cmd & (~ FEB_RO)) {
  1662.       case FEB_ADC_READ:
  1663.       case FEB_ADC_RESET:
  1664.         doubleval = ((val & 0xFFF) + 0.5)/0x1000 * 3.3;
  1665.         sa02Printf("ADC %f, 0x%08x\n", doubleval, val ) ;
  1666.         break;
  1667.       case FEB_TMON1:
  1668.       case FEB_TMON0:
  1669.         doubleval = ( (val >>2 ) & 0xFFF ) * 0.0625;
  1670.         if (val&0x4000) {
  1671.           doubleval=-doubleval;
  1672.         }
  1673.         sa02Printf("TMON %f 0x%x\n",doubleval, val ) ;
  1674.         break;
  1675.       case FEB_VTH1:
  1676.       case FEB_VTH2:
  1677.         doubleval = (val & 0xFFFF );
  1678.         if (cmd & 1)
  1679.           val=0x20;
  1680.         else
  1681.           val=0x30;
  1682.         val=sa02Cmd(board, FEB_MUX, val, 0, 0 ,1,response);
  1683.         val=sa02Cmd(board, FEB_ADC_READ, 0, 0, 0, 1,response);
  1684.         if (sa02BoardType>2)
  1685.           doubleval = (2*((val & 0xFFF) + 0.5)/0x1000 - 1.) * 3.3;
  1686.         else
  1687.           doubleval = ((val & 0xFFF) + 0.5)/0x1000 * 3.3;
  1688.         sa02Printf("%5d %6.3f\n",data, doubleval);
  1689.         break;
  1690.       case SA0x_ASIC0_GREG:
  1691.         if (cmd & FEB_RO) {
  1692.           sa02PrintGREG(&val,"Returned GREG data->");
  1693.         }
  1694.         break;
  1695.       case SA0x_ASIC0_CREG:
  1696.         if (cmd & FEB_RO) {
  1697.           sa02PrintCREG(&val,"Returned CREG data->");
  1698.         }
  1699.         break;
  1700.       default:
  1701.         sa02Printf("0x%08X\n",val);
  1702. //        break;
  1703.     }
  1704.     datay[i]=doubleval;
  1705.     if (delay>0 && fdata[0]==P1_MULTIFPGAWRITE) {
  1706.       Delay(delay);
  1707.     }
  1708.     if  (daqexe) {
  1709.       daq(NULL);
  1710.     }
  1711.     if (ctrl_c) {
  1712.       break;
  1713.     }
  1714.     if (t!=told  ) {
  1715.       plot1d(i,x0,dx);
  1716.       SetCtrlVal(p1h,P1_CUREVE, i);
  1717.       ProcessSystemEvents ();
  1718.     }
  1719.     data+=step;
  1720.     told=t;
  1721.     time(&t);
  1722.   }
  1723.   plot1d(i,x0,dx);
  1724.   SetCtrlVal(p1h,P1_CUREVE, i);
  1725.   return 0;
  1726. }
  1727.  
  1728. int CVICALLBACK SendSEUTRigger (int panel, int control, int event,
  1729.                                 void *callbackData, int eventData1, int eventData2) {
  1730.  
  1731.   switch (event) {
  1732.     case EVENT_COMMIT:
  1733.       Sa02SEUTrigger();
  1734.       break;
  1735.   }
  1736.   return 0;
  1737. }
  1738.  
  1739. int CVICALLBACK FSelect (int panel, int control, int event,
  1740.                          void *callbackData, int eventData1, int eventData2) {
  1741.   int status,fcontrol = 0;
  1742.   char pathName[MAX_PATHNAME_LEN];
  1743.   char defName[MAX_PATHNAME_LEN];
  1744.   char fileMessage[100] = "";
  1745.   switch (event) {
  1746.     case EVENT_COMMIT:
  1747.       switch (control) {
  1748.         case P3_FSELECT:
  1749.           fcontrol=P3_INPUTFILE;
  1750.           strcpy(fileMessage,"Select Parameter file");
  1751.           break;
  1752.         case P3_PTSFSELECT:
  1753.           fcontrol=P3_PTSFIRMWARE;
  1754.           strcpy(fileMessage,"Select PTS Firmware file");
  1755.           break;
  1756.       }
  1757.       sa02Printf("MAX_PATHNAME_LEN %d\n",MAX_PATHNAME_LEN);
  1758.       GetCtrlVal(panel,fcontrol,defName);
  1759.       status = FileSelectPopup ("",defName,"",
  1760.                                 fileMessage, VAL_LOAD_BUTTON, 0,
  1761.                                 0, 1, 0, pathName);
  1762.       if (status>0)
  1763.         SetCtrlVal(panel,fcontrol,pathName);
  1764.       break;
  1765.   }
  1766.   return 0;
  1767. }
  1768.  
  1769. int CVICALLBACK Verbose (int panel, int control, int event,
  1770.                          void *callbackData, int eventData1, int eventData2) {
  1771.   //int * data;
  1772.   switch (event) {
  1773.     case EVENT_COMMIT:
  1774.       GetCtrlVal(panel,control,&sa02Verbose);
  1775.       break;
  1776.   }
  1777.   return 0;
  1778. }
  1779.  
  1780. int CVICALLBACK Print (int panel, int control, int event,
  1781.                        void *callbackData, int eventData1, int eventData2) {
  1782.   char dfile[MAX_PATHNAME_LEN];
  1783.   char name[MAX_PATHNAME_LEN];
  1784.   int h2=0;
  1785.   switch (event) {
  1786.     case EVENT_COMMIT: {
  1787.       int hours, minutes, seconds;
  1788.       int month, day, year;
  1789.       int   id=-1;
  1790.       //int   bitmap = 0;
  1791.       if (panel == p1h) {
  1792.         switch (control) {
  1793.           case P1_PRINT:
  1794.             id= P1_GRAPH;
  1795.             sprintf(name,"G");
  1796.             break;
  1797.           case P1_PRINT2D:
  1798.             id= P1_GRAPH2D;
  1799.             sprintf(name,"2D");
  1800.             break;
  1801.           case P1_PRINTX:
  1802.             id= P1_GRAPHX;
  1803.             sprintf(name,"X");
  1804.             break;
  1805.           case P1_PRINTY:
  1806.             id= P1_GRAPHY;
  1807.             sprintf(name,"Y");
  1808.             break;
  1809.         }
  1810.         h2=0;
  1811.       } else {
  1812.         switch (control) {
  1813.           case P2_PRINTXY:
  1814.             id= P2_GRAPH_1;
  1815.             sprintf(name,"XY");
  1816.             break;
  1817.         }
  1818.         h2=1;
  1819.       }
  1820.       GetSystemDate (&month,&day ,&year );
  1821.       GetSystemTime(&hours, &minutes, &seconds);
  1822.       if (id>=0) {
  1823.         if( ( (panel == p1h)&&(control ==  P1_PRINT2D) ) || ((panel == p2h)&&(control ==  P2_GRAPH_1))) {
  1824.           sprintf(dfile ,"data/%d_%d_%d_%d_%d_%s.th2d",year,month,day,hours,minutes, name );
  1825.           H2DWrite(h2,dfile,"wb");
  1826.           sa02Printf("sizeof H2D %d \n",sizeof(H2D));
  1827.           sa02Printf("H2D image saved as %s\n", dfile);
  1828.           sprintf(name ,"C:\\root\\bin\\root.exe ..\\macros\\H2Dload.cxx(\\\"%s\\\")", dfile);
  1829.           LaunchExecutable(name);
  1830.         } else {
  1831.           sprintf(dfile ,"data/%d_%d_%d_%d_%d_%s.bmp",year,month,day,hours,minutes, name );
  1832.           SaveCtrlDisplayToFile (panel,id, 0, -1, -1, dfile);
  1833.           sa02Printf("bmp image saved as %s\n", dfile);
  1834.         }
  1835.       }
  1836.       //PrintCtrl (p1h, P1_GRAPH2D, "sa02_CVI", 1, 1);
  1837.     }
  1838.     break;
  1839.   }
  1840.   return 0;
  1841. }
  1842.  
  1843. int CVICALLBACK CursorCB (int panel, int control, int event,
  1844.                           void *callbackData, int eventData1, int eventData2) {
  1845.   double x=0,y=0;
  1846.   int ix=0,iy=0;
  1847.   int h2=(int) callbackData;
  1848.  
  1849.   switch (event) {
  1850.     case EVENT_COMMIT:
  1851.       GetGraphCursor (p1h, P1_GRAPH2D, 1, &x, &y);
  1852.       if(H2DGetStepX(h2)>0) {
  1853.         ix=(int)((x-H2DGetMinX(h2))/H2DGetStepX(h2));
  1854.       }
  1855.       if(H2DGetStepY(h2)>0) {
  1856.         iy=(int)((y-H2DGetMinY(h2))/H2DGetStepY(h2));
  1857.       }
  1858.       SetCtrlVal(panel,P1_NSLIX,ix);
  1859.       SetCtrlVal(panel,P1_NSLIY,iy);
  1860.       sa02Printf("Cursor %f %f h2=%d val=%f min=%f max=%f\n",
  1861.                  x,y,h2,H2DGetBinContent(h2,ix,iy),H2DGetMin(h2),
  1862.                  H2DGetMax(h2) );
  1863.       break;
  1864.   }
  1865.   return 0;
  1866. }
  1867.  
  1868.  
  1869. int CVICALLBACK HistoInfoCB (int panel, int control, int event,
  1870.                              void *callbackData, int eventData1, int eventData2) {
  1871.   double x=0,y=0;
  1872.   int ix=0,iy=0;
  1873.   int h2=(int) callbackData;
  1874.   int b=0;
  1875.   switch (event) {
  1876.     case EVENT_COMMIT:
  1877.  
  1878.       GetGraphCursor (panel, control, 1, &x, &y);
  1879.       if (panel == p2h ) {
  1880.         for (b=0; b<4; b++) {
  1881.           if (control==p2graph[b]) break;
  1882.         }
  1883.         h2=100+b;
  1884.         if(H2DGetStepX(h2)>0) {
  1885.           ix=(int)((x-H2DGetMinX(h2))/H2DGetStepX(h2));
  1886.         }
  1887.         if(H2DGetStepY(h2)>0) {
  1888.           iy=(int)((y-H2DGetMinY(h2))/H2DGetStepY(h2));
  1889.         }
  1890.  
  1891.         sa02Printf("Cursor %f %f h2=%d val=%f min=%f max=%f\n",
  1892.                    x,y,h2,H2DGetBinContent(h2,ix,iy),H2DGetMin(h2),
  1893.                    H2DGetMax(h2) );
  1894.       }
  1895.       break;
  1896.   }
  1897.   return 0;
  1898. }
  1899.  
  1900.  
  1901. int GetElectronicChannel(int x, int y, int *chip, int *ch) {
  1902.   int i=0;
  1903.   for (i=0; i<144; i++) {
  1904.     if (eid2hapdxy[i].chx == x && eid2hapdxy[i].chy == y) {
  1905.       *chip = eid2hapdxy[i].chip;
  1906.       *ch   = eid2hapdxy[i].ch  ;
  1907.       return 0;
  1908.     }
  1909.   }
  1910.   return -1;
  1911. }
  1912.  
  1913. int SetCMon(int chx,int chy) {
  1914.   int chip,ch,asic;
  1915.   uint32_t response[2]= {0,0};
  1916.   uint32_t board;
  1917.   uint16_t mask=GetConnectedFebMask();
  1918.  
  1919. //  GetCtrlVal(p1h,P1_BOARDNUMBER, &board);
  1920.   GetCtrlVal(p1h,P1_BOARDTYPE,&sa02BoardType);
  1921.   GetElectronicChannel(chx,chy,&chip,&ch);
  1922.   asic = sa02MuxMap(chip);
  1923.   sa02Printf("CHX %d CHY %d ",chx, chy);
  1924.   sa02Printf("MUX %d ",asic);
  1925.   sa02Printf("CMON ASIC: %d  ch: %d\n",chip, ch);
  1926.   for (board=0; board<4; board++) {
  1927.     if (mask & (1<<board)) {
  1928.       sa02Cmd(board,FEB_MUX , asic, 0, 0 ,2,response);
  1929.       sa02Cmd(board,SA0x_ASIC0_CMON,0,chip,ch,2,response);
  1930.     }
  1931.   }
  1932.  
  1933.   return 0;
  1934. }
  1935.  
  1936. int CVICALLBACK HDraw2DCB (int panel, int control, int event,
  1937.                            void *callbackData, int eventData1, int eventData2) {
  1938.   int ch=0;
  1939.   int ctrlID=0;
  1940.   int *plot=0;
  1941.   int graph=0;
  1942.   int board=0;
  1943.   switch (event) {
  1944.  
  1945.     case EVENT_COMMIT:
  1946.       for (board=0; board<4; board++) {
  1947. //        if (panel == p1h){ rID = P1_CH; graph = P1_GRAPH; plot =&p1plothandle;}
  1948.         if (panel == p2h) {
  1949.           ctrlID = P2_CH;
  1950.           graph = p2graph[board];
  1951.           plot =&p2plothandle[board];
  1952.         }
  1953. //        if (panel == p3h){ rID = P3_CH; graph = P3_GRAPH; plot =&p3plothandle;}
  1954. //        if (panel == p4h){ rID = P4_CH; graph = P4_GRAPH; plot =&p4plothandle;}
  1955.  
  1956.  
  1957.         GetCtrlVal(panel,ctrlID, &ch);
  1958.         if (ch) {
  1959.           H3DDrawSliceXY(100+board,ch-1,panel,graph, plot);
  1960.         } else {
  1961.           H2DDraw(100+board,panel,graph, plot);
  1962.         }
  1963.       }
  1964.       break;
  1965.   }
  1966.   return 0;
  1967. }
  1968.  
  1969. int GetHvMonitor( void ) {
  1970.   int N470mid   = 4;
  1971.   int SYS403mid = 2;
  1972.   unsigned short buf[256]= {0};
  1973.   int retval;
  1974.   int ii;
  1975. //                 G0,A0,B0,C0,D0,H0,G1,A1,B1,C1,D1,H1,G2,A2,B2,C2,D2,H2,G3,A3,B3,C3,D3,H3
  1976.   int channels[]= {24, 0, 1, 2, 3, 0,25, 4, 5, 6, 7, 1,26, 8, 9,10,11, 2,27,12,13,14,15, 3}; //channel for  SYS403 , then for  N470
  1977. //  int channels[]= {0,1,2,3,4,0,5,6,7,8,9,1,10,11,12,13,14,2,15,16,17,18,19,3}; //channel for  SYS403 , then for  N470
  1978.  
  1979.   for (ii=0; ii<256; ii++) buf[ii]=0;
  1980.   monrec.time = (uint32_t) time(NULL);
  1981.   for (ii=0; ii<=23; ii++) {
  1982.     if ((ii+1)%6==0 && ii!=0) {
  1983.       buf[0]=(unsigned short) (channels[ii]<<8) | ReadOperationalParam;
  1984.       if (gCAEN_V288) {
  1985.         V288_Send(0,N470mid,1,buf);
  1986.         retval=V288_Receive(0,255, buf);
  1987.       }
  1988.       monrec.status[ii] = buf[0];   //status
  1989.       monrec.vmon[ii] = buf[1]*1000;//zapisano v mV
  1990.       monrec.imon[ii] = buf[2]*1000;//zapisano v nA
  1991.       monrec.vset[ii] = buf[3]*1000;//zapisano v mV
  1992.       monrec.iset[ii] = buf[4]*1000;//zapisano v nA
  1993.     } else {
  1994.       buf[0]=(unsigned short) (channels[ii]<<8) | ReadStatus;
  1995.       if (gCAEN_V288) {
  1996.         V288_Send(0,SYS403mid,1,buf);
  1997.         retval=V288_Receive(0,255,buf);
  1998.       }
  1999.       monrec.vmon[ii] = buf[1]*10;//zapisano v mV
  2000.       monrec.imon[ii] = buf[2]*10;//zapisano v nA
  2001.       monrec.status[ii] = buf[3]; //status
  2002.  
  2003.       if (!hvmonFirst) {
  2004.         buf[0]=(unsigned short) (channels[ii]<<8) | ReadParameters;
  2005.         if (gCAEN_V288) {
  2006.           V288_Send(0,SYS403mid,1,buf);
  2007.           retval=V288_Receive(0,255,buf);
  2008.         }
  2009.         vset[ii] = monrec.vset[ii] = buf[7]*10;//zapisano v mV
  2010.         iset[ii] = monrec.iset[ii] = buf[10]*10;//zapisano v nA
  2011.       }
  2012.       monrec.vset[ii] = vset[ii];//zapisano v mV
  2013.       monrec.iset[ii] = iset[ii];//zapisano v nA
  2014.     }
  2015.     //sa02Printf("%04x \t %d \t %d \t %d \t %d *\n",monrec.status[ii],monrec.vmon[ii],monrec.vset[ii],monrec.imon[ii],monrec.iset[ii]);
  2016.   }
  2017.   hvmonFirst=1;
  2018.   return 0;
  2019. }
  2020.  
  2021. int CVICALLBACK daq_scan(void *functionData) {
  2022.   int ich,count=0, xyval[4]= {0,0,0,0};
  2023.   //int c[4]= {0,0,0,0};
  2024.   //int e[4]= {0,0,0,0};
  2025.   uint32_t daqmode=1;
  2026.   uint32_t trglen;
  2027.   int nb,dsave,fcount=0,fmax=0,status,ch;
  2028.   unsigned int i;
  2029.   int hxy=100;
  2030.   //int h2=0;
  2031.   time_t t,told, tstart, t0;
  2032.  
  2033.   uint32_t board;
  2034.   uint32_t tpenb   =0;
  2035.   uint32_t response[2]= {0,0};
  2036.   char title[0xFF];
  2037.   char dfile[MAX_PATHNAME_LEN],dfile0[MAX_PATHNAME_LEN];
  2038. #define MAXSIZE 10000
  2039.   int maxsize = MAXSIZE;
  2040.   uint32_t sendswtrg=0;
  2041.   int ncount=0;
  2042.   uint32_t *rdata;
  2043.   float dx,dy,gapx,gapy,addgap;
  2044.   int ix,iy,nx,ny;
  2045.   int scanunitsx=0,scanunitsy=0;
  2046.   int x0,y0;
  2047.   const float fx= (float)(1000 / 0.3595); // 4M scaling factor steps/mm
  2048.   int daqexe;
  2049.   int writeevents=0;
  2050.   double fraction=0;
  2051.   uint32_t *data;
  2052.   char serial[4][0xFF];
  2053.   char hname[0xFF];
  2054.   time_t cas;
  2055.  
  2056.   uint16_t mask=GetConnectedFebMask();
  2057.   runrec.id = RUNREC_ID;
  2058.   runrec.len = sizeof(runrec);
  2059.   endrec.id = ENDREC_ID;
  2060.   endrec.len = sizeof(endrec);
  2061.   posrec.id = POSREC_ID;
  2062.   posrec.len = sizeof(posrec);
  2063.   evtrec.id = EVTREC_ID;
  2064.   evtrec.len = sizeof(evtrec);
  2065.   datrec.id = DATREC_ID;
  2066.   datrec.len = sizeof(datrec);
  2067.   monrec.id = MONREC_ID;
  2068.   monrec.len = sizeof(monrec);
  2069.  
  2070.   SetCtrlAttribute (p2h, P2_DAQ, ATTR_DIMMED, 1);
  2071.   ctrl_c=0;
  2072.  
  2073.   GetCtrlVal(p1h,P1_BOARDTYPE,&sa02BoardType);
  2074.  
  2075.   GetCtrlVal (p1h, P1_SERIAL_1, serial[0]);
  2076.   GetCtrlVal (p1h, P1_SERIAL_2, serial[1]);
  2077.   GetCtrlVal (p1h, P1_SERIAL_3, serial[2]);
  2078.   GetCtrlVal (p1h, P1_SERIAL_4, serial[3]);
  2079.  
  2080.   sprintf(runrec.serial,"HAPD=%s,%s,%s,%s", serial[0],serial[1],serial[2],serial[3]);
  2081.  
  2082.   GetCtrlVal (p2h, P2_NEVE, &runrec.nev);
  2083.   GetCtrlVal (p2h, P2_PEDESTAL, &runrec.ped);
  2084.   GetCtrlVal (p2h, P2_NX, &runrec.nx);
  2085.   GetCtrlVal (p2h, P2_XSTEP, &runrec.dx);
  2086.   GetCtrlVal (p2h, P2_XMIN, &runrec.x0);
  2087.   GetCtrlVal (p2h, P2_NY, &runrec.ny);
  2088.   GetCtrlVal (p2h, P2_YSTEP, &runrec.dy);
  2089.   GetCtrlVal (p2h, P2_YMIN, &runrec.y0);
  2090.   GetCtrlVal ( p1h, P2_TPENB,&tpenb);
  2091.   GetCtrlVal ( p2h,P2_DAQEXE,&daqexe);
  2092.   GetCtrlVal(p2h,P2_FRACTION, &fraction);
  2093.   GetCtrlVal (p2h, P2_SCANUNITSX,  &scanunitsx);
  2094.   GetCtrlVal (p2h, P2_SCANUNITSY,  &scanunitsy);
  2095.   GetCtrlVal (p2h, P2_GAPX,  &gapx);
  2096.   GetCtrlVal (p2h, P2_GAPX,  &gapy);
  2097.   GetCtrlVal (p2h, P2_DX, &dx);
  2098.   GetCtrlVal (p2h, P2_DX, &dy);
  2099.  
  2100.   GetCtrlVal (p2h, P2_XC,  &x0);
  2101.   GetCtrlVal (p2h, P2_YC,  &y0);
  2102.  
  2103.   GetCtrlVal (p2h, P2_DIRECTION,  &runrec.direction);
  2104.  
  2105.  
  2106.   SetCtrlAttribute(p1h,P1_TRGHVMON,ATTR_ENABLED,1);
  2107.   hvmonFirst = 0;
  2108.  
  2109.  
  2110.   GetCtrlVal(p1h,P1_SENDSWTRIG,&sendswtrg);
  2111.   GetCtrlVal(p1h,P1_INTTRGLEN, &trglen);
  2112.   GetCtrlVal(p1h,P1_DAQMODE, &daqmode);
  2113.  
  2114.  
  2115.  
  2116.  
  2117.   runrec.fver = scanunitsx + scanunitsy*2 ;
  2118.   if (scanunitsx) {
  2119.     x0 = (int)(x0 - 5.5 * fx * dx  - gapx * fx * 0.5);
  2120.   }
  2121.   if (scanunitsy) {
  2122.     y0 = (int)(y0 - 5.5 * fx * dy  - gapy * fx * 0.5);
  2123.   }
  2124.  
  2125.   GetCtrlVal(p2h, P2_DSAVE, &dsave);
  2126.   if (dsave) {
  2127.     GetCtrlVal (p2h, P2_DFILE, dfile0);
  2128.     fcount=1;
  2129.     GetCtrlVal (p2h, P2_NEWF, &fmax);
  2130.     fmax*=1000000;//fmax in Mega Bytes
  2131.   }
  2132.   time (&t0);
  2133.   sa02Printf("---->daq_scan\n");
  2134.  
  2135.   if (dsave) {
  2136.     sprintf(dfile,"%s_file%02d.dat",dfile0,fcount);
  2137.     //gFp = fopen (dfile, "ab");
  2138.     gFp = fopen (dfile, "ab");
  2139.     if (gFp==NULL) sa02Printf("----> !!! gFp==NULL !!!\n");
  2140.     if (ferror (gFp)) sa02Printf("----> !!! ferror (gFp) after fopen !!!\n");    
  2141.     //time ((time_t *) &runrec.time);
  2142.     time (&cas);
  2143.     runrec.time = (uint32_t) cas;
  2144.     status = (int) fwrite (&runrec, 1, runrec.len, gFp);
  2145.     if(status != runrec.len) sa02Printf("----> !!! status != runrec.len (= %d) !!!\n", status);    
  2146.     if (ferror (gFp)) sa02Printf("----> !!! ferror (gFp) = %d !!!\n", ferror (gFp));  
  2147.    
  2148.   }
  2149.  
  2150.  
  2151.   for (i=0; i<4; i++) {
  2152.     sprintf(title,"HAPD%d Surface Scan single channels", i);
  2153.     sprintf(hname,"hxy%d", i);
  2154.     H3DInit(hxy+i,hname,title, runrec.nx,runrec.x0, runrec.dx,runrec.ny,runrec.y0, runrec.dy, 144, -0.5, 1);
  2155.     H3DSetTitleZ(hxy+i,"channel");
  2156.     sprintf(hname,"hxy%d_sum", i);
  2157.     H2DInit(hxy+i,hname,runrec.serial, runrec.nx,runrec.x0, runrec.dx,runrec.ny,runrec.y0, runrec.dy);
  2158.     if (scanunitsx) {
  2159.       H2DSetTitleX(hxy+i,"x (channels)");
  2160.       H3DSetTitleX(hxy+i,"x (channels)");
  2161.     } else {
  2162.       H2DSetTitleX(hxy+i,"x (stage steps)");
  2163.       H3DSetTitleX(hxy+i,"x (stage steps)");
  2164.     }
  2165.     if (scanunitsy) {
  2166.       H2DSetTitleY(hxy+i,"y (channels)");
  2167.       H3DSetTitleY(hxy+i,"y (channels)");
  2168.     } else {
  2169.       H2DSetTitleY(hxy+i,"y (stage steps)");
  2170.       H3DSetTitleY(hxy+i,"y (stage steps)");
  2171.     }
  2172.  
  2173.   }
  2174.   if (runrec.direction) {
  2175.     ny = runrec.ny;
  2176.     nx = runrec.nx;
  2177.   } else {
  2178.     ny = runrec.nx;
  2179.     nx = runrec.ny;
  2180.   }
  2181.   rdata = malloc(sizeof(uint32_t)*maxsize);
  2182.  
  2183.   time(&t);
  2184.   tstart=t;
  2185.   told=t-1;
  2186.  
  2187.   for (iy=0; iy<ny; iy++) {
  2188.  
  2189.     if (ctrl_c) break;
  2190.  
  2191.     if (runrec.direction) {
  2192.       posrec.iy = iy;
  2193.       posrec.yset=runrec.y0+posrec.iy*runrec.dy;
  2194.       if (scanunitsy) {
  2195.         if (posrec.yset>5) {
  2196.           addgap=gapy * fx;
  2197.         } else {
  2198.           addgap=0;
  2199.         }
  2200.         posrec.yset = (int32_t)(y0+dy *fx * posrec.yset + addgap);
  2201.       }
  2202.       if (gMIKRO_Y) {
  2203.         //sa02Printf("MIKRO_MoveTo (2, y);%d\n",y);
  2204.         MIKRO_MoveTo (MIKRO_Y, posrec.yset);
  2205.         sa02Printf("->MIKRO_MoveTo (2, y);%d\n",posrec.yset);
  2206.       }
  2207.  
  2208.       SetCtrlVal (p2h, P2_Y, posrec.yset);
  2209.       SetCtrlVal (p2h, P2_IY, posrec.iy);
  2210.     } else {
  2211.       posrec.ix = iy;
  2212.       posrec.xset=runrec.x0+posrec.ix*runrec.dx;
  2213.       if (scanunitsx) {
  2214.         if (posrec.xset>5) {
  2215.           addgap=gapx * fx;
  2216.         } else {
  2217.           addgap=0;
  2218.         }
  2219.         posrec.xset =  (int32_t)(x0+dx *fx * posrec.xset + addgap);
  2220.       }
  2221.       if (gMIKRO_Y) {
  2222.         //sa02Printf("MIKRO_MoveTo (2, y);%d\n",y);
  2223.         MIKRO_MoveTo (MIKRO_X, posrec.xset);
  2224.         sa02Printf("->MIKRO_MoveTo (1, x);%d\n",posrec.xset);
  2225.       }
  2226.       SetCtrlVal (p2h, P2_X, posrec.xset);
  2227.       SetCtrlVal (p2h, P2_IX, posrec.ix);
  2228.     }
  2229.     for (ix=0; ix<nx; ix++) {
  2230.       if (ctrl_c) break;
  2231.  
  2232.       if (runrec.direction) {
  2233.         posrec.ix = ix;
  2234.         posrec.xset=runrec.x0+posrec.ix*runrec.dx;
  2235.         if (scanunitsx) {
  2236.           if (posrec.xset>5) {
  2237.             addgap=gapx * fx;
  2238.           } else {
  2239.             addgap=0;
  2240.           }
  2241.           posrec.xset =  (int32_t)(x0+dx *fx * posrec.xset + addgap);
  2242.         }
  2243.         if (gMIKRO_X) {
  2244.           //sa02Printf("MIKRO_MoveTo (1, x);%d\n",posrec.x);
  2245.           MIKRO_MoveTo (MIKRO_X, posrec.xset);
  2246.           sa02Printf("->MIKRO_MoveTo (1, x);%d\n",posrec.xset);
  2247.         }
  2248.         SetCtrlVal (p2h, P2_X, posrec.xset);
  2249.         SetCtrlVal (p2h, P2_IX, posrec.ix);
  2250.       } else {
  2251.         posrec.iy = ix;
  2252.         posrec.yset=runrec.y0+posrec.iy*runrec.dy;
  2253.         if (scanunitsy) {
  2254.           if (posrec.yset>5) {
  2255.             addgap=gapy * fx;
  2256.           } else {
  2257.             addgap=0;
  2258.           }
  2259.           posrec.yset =  (int32_t)(y0+dy *fx * posrec.yset + addgap);
  2260.         }
  2261.         if (gMIKRO_X) {
  2262.           //sa02Printf("MIKRO_MoveTo (1, x);%d\n",posrec.y);
  2263.           MIKRO_MoveTo (MIKRO_Y, posrec.yset);
  2264.           sa02Printf("->MIKRO_MoveTo (2, y);%d\n",posrec.yset);
  2265.         }
  2266.         SetCtrlVal (p2h, P2_Y, posrec.yset);
  2267.         SetCtrlVal (p2h, P2_IY, posrec.iy);
  2268.       }
  2269.       if (scanunitsx && scanunitsy) {
  2270.         SetCMon(runrec.x0+posrec.ix*runrec.dx,runrec.y0+posrec.iy*runrec.dy);
  2271.       }
  2272.       if (dsave) {
  2273.         if (fmax && (ftell(gFp) > fmax)) {
  2274.           fcount+=1;
  2275.           sprintf(dfile,"%s_file%02d.dat",dfile0,fcount);
  2276.           fclose(gFp);
  2277.           gFp = fopen (dfile, "ab");
  2278.         }
  2279.         time (&cas);
  2280.         posrec.time = (uint32_t) cas;
  2281.         status = (int) fwrite (&posrec, 1, posrec.len, gFp);
  2282.       }
  2283.  
  2284.       Sa02DaqMode (daqmode);
  2285.       Sa02SelectTriggerWithMaskAndLength (sendswtrg, mask, trglen);
  2286.  
  2287.       for (board=0; board<4; board++) {
  2288.         if (mask & (1<<board)) {
  2289.           sa02Cmd( board, FEB_VTH1, runrec.ped, 0,0,1,response);
  2290.           Sa02TestPulseEnable(board,tpenb); // Enable/disable test pulse
  2291.         }
  2292.       }
  2293.  
  2294.       Sa02SetNeve(runrec.nev);
  2295.  
  2296.       evtrec.nev=1;
  2297.  
  2298.  
  2299.       sa02Reset();
  2300.  
  2301.       if (ctrl_c) break;
  2302.  
  2303.       if ( (count+2+dsize)  >= maxsize) {
  2304.         maxsize*=2;
  2305.         sa02Printf("Increasing data buffer to %d elements\n", maxsize);
  2306.         rdata=realloc(rdata ,sizeof(uint32_t)*maxsize);
  2307.       }
  2308.  
  2309.       data = &rdata[count+2];
  2310.       do {
  2311.         //int k;
  2312.         //uint32_t inputtriggers;
  2313.         if (sendswtrg == 1) Sa02SoftwareTrigger();
  2314.         nb  = sa02Read(mask, &rdata[count+2] );
  2315.         /*
  2316.         for (k=0; k<4; k++)  c[k] = Sa02GetCounter ( k ,&e[k]);
  2317.         for (k=0; k<4; k++)  sa02Printf(" CNTR%d=%d (ERR=%d)\t",k,c[k],e[k]);
  2318.         sa02Printf("\nsa02Read bytes =  %d mask=%d neve=%d (%d # %d)\n", nb, mask, runrec.nev, Sa02GetNeve( &inputtriggers ) , inputtriggers);
  2319.         */
  2320.       } while  ( nb==0 && !ctrl_c);
  2321.  
  2322.  
  2323.  
  2324.       if (sa02TimerOut || nb==0) sa02Printf("sa02TimerOut || nb==0\n");
  2325.  
  2326.       for (ich=0; ich<144*4; ich++) {
  2327.         int brd = ich/144;
  2328.         int xch   = 143 - ich%144;
  2329.         //sa02Printf("[%d] = %d \n", ich, data[ich]);
  2330.         if (data[ich]> xyval[brd])   xyval[brd] = data[ich];
  2331.         if (mask & (1<<brd)) {
  2332.           H3DFillBin(hxy+ brd, posrec.ix,posrec.iy,xch,data[ich]);
  2333.           H2DFillBin(hxy+ brd, posrec.ix,posrec.iy,data[ich]);
  2334.         }
  2335.       }
  2336.       sa02Printf("max couts = %d %d %d %d\n", xyval[0],xyval[1],xyval[2],xyval[3]);
  2337.       if (nb>=0) {
  2338.         count+=module_header(0x3,&rdata[count],nb/sizeof(uint32_t));
  2339.       }
  2340.  
  2341.  
  2342.       /*
  2343.       for (ich=0; ich<144; ich++) {
  2344.  
  2345.       //          uint32_t mask=0xFF;
  2346.       //          id=(35-ich/4);
  2347.       //          shft=(ich%4)*8;
  2348.         uint32_t mask=0xF;
  2349.             id=(17-ich/8)+board*18;
  2350.         shft=(ich%8)*4;
  2351.         if ( data[id]  & (mask <<shft)  )  {
  2352.           H3DFillBin(hxy, posrec.ix,posrec.iy,ich,1);
  2353.           H2DFillBin(hxy, posrec.ix,posrec.iy,1);
  2354.         }
  2355.       }
  2356.       */
  2357.       if (dsave) {
  2358.         if (Random(0,1)<fraction) {
  2359.           writeevents=1;
  2360.         } else {
  2361.           writeevents=0;
  2362.         }
  2363.         if (writeevents) {
  2364.           evtrec.id = EVTREC_ID;
  2365.           evtrec.len=count*sizeof(uint32_t)+ sizeof(evtrec);
  2366.  
  2367.           evtrec.time= (uint32_t) time(NULL);
  2368.           evtrec.nev=i;
  2369.           status = (int) fwrite( &evtrec,  1,  sizeof(evtrec),gFp);
  2370.           if (count) {
  2371.             status = (int) fwrite(rdata,1,count*sizeof(uint32_t),gFp);  //gzip
  2372.           }
  2373.         }
  2374.         if (hvmon) {
  2375.           time_t thv0,thv1;
  2376.           time(&thv0);
  2377.           GetHvMonitor();
  2378.           time(&thv1);
  2379.           status = (int) fwrite( &monrec,  1,  sizeof(monrec),gFp);
  2380.           sa02Printf("HvMonitor dt= %d s\n", thv1-thv0 );
  2381.           hvmon =0;
  2382.         }
  2383.  
  2384.         ncount++;
  2385.       }
  2386.  
  2387.       time(&t);
  2388.       if (t!=told ) {
  2389.         double done= (double) (posrec.ix+posrec.iy*runrec.nx)/(runrec.nx*runrec.ny);
  2390.         EstimatedFinish(p2h, P2_PROGRESS, P2_ETA, t0, done);
  2391.         GetCtrlVal(p2h,P2_CH, &ch);
  2392.         for (board=0; board<4; board++) {
  2393.           if (ch) {
  2394.             H3DDrawSliceXY(hxy+board,ch,p2h, p2graph[board], &p2plothandle[board]);
  2395.           } else {
  2396.             H2DDraw(hxy+board,p2h, p2graph[board], &p2plothandle[board]);
  2397.           }
  2398.         }
  2399.         ProcessSystemEvents ();
  2400.         SetCtrlVal(p2h,P2_CEVE, i);
  2401.         sa02Printf("%d events in %2.2f min (%d s) %s",ncount, (double)(t-tstart)/60.,t-tstart, ctime(&t));
  2402.  
  2403.       }
  2404.       told=t;
  2405.  
  2406.  
  2407.  
  2408.       if (ctrl_c) break;
  2409.  
  2410.       if (daqexe &0x1) {
  2411.         daq(NULL);
  2412.         sa02Cmd( board, FEB_VTH1, runrec.ped, 0,0,1,response);
  2413.       }
  2414.       if (daqexe &0x2) {
  2415.         V729_SetFilePointer(gFp);
  2416.         V729_daq(NULL);
  2417.       }
  2418.  
  2419.     }
  2420.   }
  2421.  
  2422.   if (gFp) {
  2423.     int ii=0, ison=0;
  2424.     for (ii=0; ii<4; ii++) {
  2425.       GetCtrlVal(p1h,hapd_onoff[ii], &ison);
  2426.       if (ison ) {
  2427.         H3DWrite2File(hxy+ii, gFp);
  2428.         H2DWrite2File(hxy+ii, gFp);
  2429.       }
  2430.     }
  2431.     fclose(gFp);
  2432.   }
  2433.   gFp=NULL;
  2434.   free(rdata);
  2435.  
  2436.   SetCtrlAttribute(p1h,P1_TRGHVMON,ATTR_ENABLED,0);
  2437.  
  2438.   return 0;
  2439. }
  2440.  
  2441. int CVICALLBACK Testing(void *functionData) {
  2442.   int i;
  2443.   int N470mid   = 4;
  2444.   int SYS403mid = 2;
  2445.   unsigned short buf[256]= {0};
  2446.   int odg;
  2447.   int channels[]= {0,1,2,3,4,0,5,6,7,8,9,1,10,11,12,13,14,2,15,16,17,18,19,3}; //channel for  SYS403 , then for  N470
  2448.   time_t t0,t1;
  2449.   time(&t0);
  2450.  
  2451.   hvmonFirst=0;
  2452.  
  2453.   for (i=0; i<256; i++) buf[i]=0;
  2454.   for (i=0; i<=23; i++) {
  2455.     if (((i+1)%6)==0&&i!=0) {
  2456.       buf[0]=(unsigned short) (channels[i]<<8) | ReadOperationalParam;
  2457.       if (gCAEN_V288) {
  2458.         V288_Send(0,N470mid,1,buf);
  2459.         odg=V288_Receive(0,255,buf);
  2460.       }
  2461.       monrec.status[i] = buf[0];   //status
  2462.       monrec.vmon[i] = buf[1]*1000;//zapisano v mV
  2463.       monrec.imon[i] = buf[2]*1000;//zapisano v nA
  2464.       monrec.vset[i] = buf[3]*1000;//zapisano v mV
  2465.       monrec.iset[i] = buf[4]*1000;//zapisano v nA
  2466.     } else {
  2467.       buf[0]=(unsigned short) (channels[i]<<8) | ReadStatus;
  2468.       if (gCAEN_V288) {
  2469.         V288_Send(0,SYS403mid,1,buf);
  2470.         odg=V288_Receive(0,255,buf);
  2471.       }
  2472.       monrec.vmon[i] = buf[1]*10;//zapisano v mV
  2473.       monrec.imon[i] = buf[2]*10;//zapisano v nA
  2474.       monrec.status[i] = buf[3]; //status
  2475.       if(!hvmonFirst) {
  2476.         buf[0]=(unsigned short) (channels[i]<<8) | ReadParameters;
  2477.         if (gCAEN_V288) {
  2478.           V288_Send(0,SYS403mid,1,buf);
  2479.           odg=V288_Receive(0,255,buf);
  2480.         }
  2481.         vset[i] = monrec.vset[i] = buf[7]*10;//zapisano v mV
  2482.         iset[i] = monrec.iset[i] = buf[10]*10;//zapisano v nA
  2483.       }
  2484.       monrec.vset[i] = vset[i];//zapisano v mV
  2485.       monrec.iset[i] = iset[i];//zapisano v nA
  2486.     }
  2487.     sa02Printf("%04x \t %.02f V \t %d V \t\t %.02f muA \t %d muA *\n",monrec.status[i],(float)monrec.vmon[i]/1000,monrec.vset[i]/1000,(float)monrec.imon[i]/1000,monrec.iset[i]/1000);
  2488.   }
  2489.   time (&t1);
  2490.   sa02Printf("Cas izvedbe:%g s.\n",difftime(t1,t0));
  2491.  
  2492.   return 0;
  2493. }
  2494.  
  2495. int CVICALLBACK set_dac(int OnOff) {
  2496.   int val;
  2497.   char name[MAX_PATHNAME_LEN];
  2498.   if (OnOff) {
  2499.     GetCtrlVal (p5h, P5_DAC_ON, &val);
  2500.     sprintf(name ,"%s -p %d",palaser, val);
  2501.     sa02Printf("%s\n", name);
  2502.     system(name);
  2503.     GetCtrlVal (p5h, P5_FREQUENCY_ON, &val);
  2504.     sprintf(name ,"%s -f %d",palaser, val);
  2505.     sa02Printf("%s\n", name);
  2506.     system(name);
  2507.   } else {
  2508.     GetCtrlVal (p5h, P5_DAC_OFF, &val);
  2509.     sprintf(name ,"%s -p %d",palaser, val);
  2510.     sa02Printf("%s\n", name);
  2511.     system(name);
  2512.     GetCtrlVal (p5h, P5_FREQUENCY_OFF, &val);
  2513.     sprintf(name ,"%s -f %d",palaser, val);
  2514.     sa02Printf("%s\n", name);
  2515.     system(name);
  2516.   }
  2517.   return 0;
  2518. }
  2519.  
  2520. int CVICALLBACK set_hv(int OnOff) {