//####################################################### //# //# Control program for DAQ Beamtest March 2004 //# //# by Rok Pestotnik, Jozef STefan Institute,Ljubljana //# GPL licence //# //# Date: 1/3/2004 #define VERSION 0.1 //# Modifications: //# //####################################################### #include #include #include #ifdef Linux #include #endif #include "mycurses.h" #include "io.h" #include #include //#include #include #include char flogfilename[128]; char datapath[128]; #define ATTR A_BLINK #define MAXDATALEN 100000 int runno; // run number int neve; // required number of events int ieve; // event counter int okeve; // usefull events int blen; // buffer length int nrow,ncol; char data[MAXDATALEN]; // data buffer short * pdata; // pointer to data #define EVDATASIZE 10 // statistic structure int evdata[EVDATASIZE]; float evfrac[EVDATASIZE]; int refreshrate=0x100; FILE *fp; int mstime; // time in ms from daq start time_t sdtime; time_t dum; int camac_init( int *blen); int camac_begin( int *blen); int camac_event(int *mode,int *maxlen,int *blen,int* buf); int camac_end(int *mode); /************************************************/ # define timersubtract(a, b, result) \ do { \ (result).tv_sec = (a).tv_sec - (b).tv_sec; \ (result).tv_usec = (a).tv_usec - (b).tv_usec; \ if ((result).tv_usec < 0) { \ --(result).tv_sec; \ (result).tv_usec += 1000000; \ } \ } while (0) double Timer(){ // returns number of seconds since EPOH long sec,usec; struct timeval tp; struct timezone tzp; gettimeofday(&tp, &tzp); return tp.tv_sec+ 1e-6 *tp.tv_usec; } int MsTimer(){ static struct timeval firstcall; struct timeval dt,now; struct timezone tzp; static int first=1; if (first) { first=0; gettimeofday(&firstcall, &tzp); return 0;} gettimeofday(&now, &tzp); timersubtract(now, firstcall, dt); return ( dt.tv_usec/1000 + dt.tv_sec*1000 ); } int RefreshTimer(now,dt){ static int lasttime=0; if (now > lasttime+dt) { lasttime = now; return 1; } return 0; } /****************************************************************************** Function: fexist() Description: check the existence of file corresponding to Parameters: path Returns: 1, if file exists ******************************************************************************/ static int fexist( char * path){ struct stat sbuf; int res; if(!path || !*path) return 0; res=stat(path,&sbuf); if (res){ if (errno==ENOENT) { return 0; } else { return -1; } } return 1; } int main_init(){ char filename[128],lastrunfname[128]; char line[200]; int nr,newrunno,flag; FILE *flog=NULL; // retrive data runnumber information from file; sprintf(lastrunfname,"%s/last.run",datapath); flog=fopen(lastrunfname,"r"); if (flog==NULL) { runno=1; } else { nr=0; if ( fgets(line,200,flog)!=NULL) { nr=sscanf(line,"%d",&runno); } if (nr<=0) runno=1; else runno++; fclose(flog); } // write run information to stdout fprintf(stdout,"Enter the run number [%d] or confirm defaults with to start data acquisition\n",runno); // fscanf(stdin,"%s",line); gets(line); nr=sscanf(line,"%d",&newrunno); flag=0; if (nr==1){ runno=newrunno; flag=1; } else { fprintf(stdout,"%c\n", line[0]); //if (line[1]==0 && line[0]=='y' ) flag=1; if (line[0]==0) flag=1; } if (flag==0) exit(1); sprintf(filename,"%s/r%07d.dat",datapath,runno); if (fexist(filename)==1){ fprintf(stdout,"Error !\n"); fprintf(stdout,"Run %d with name %s already exist\n",runno,filename); fprintf(stdout,"Remove the the run and restart !!!\n"); exit(0); } // write current data runnumber to file; flog=fopen(lastrunfname,"w"); fprintf(flog,"%d",runno); fclose(flog); // write BEGIN_RECORD information to log flog=fopen(flogfilename,"a"); if (flog==NULL) { return -1; } else { sdtime = time(NULL); sprintf(line,"%s",ctime(&sdtime)); line[strlen(line)]=0; fprintf(flog,"Run %d %s START:%d COMPILED on %s %s ", runno,filename,sdtime,__DATE__,__TIME__); fprintf(stdout,"Run %d %s START:%d COMPILED on %s %s ", runno,filename,sdtime,__DATE__,__TIME__); fclose(flog); } // open data file fp=fopen(filename,"w"); if (fp==NULL) { fprintf(stderr,"File %s cannot be opened. Exiting...\n",filename); exit(-1); } // write BEGIN_RECORD information to file Ewrite(BEGIN_RECORD_TYPE,runno,neve,0,0,(void *) &nr,fp); // reset counters for (nr=0;nr0) { evfrac[3]= n / dt; } else { evfrac[3]=0; } mem[imem]=evfrac[3]; imem++; imem%=kmax; evfrac[8]=0; for( kk=0;kk 0){ evfrac[4]=(okeve)/curtime; } else { evfrac[4]=0; } mvprintw(1,ncol-30,"%5.1f s",curtime); sprintf(tmp,"%7d %7d %7d %7d %7d %5.2f %5.2f\n",ieve+1,blen, evdata[0],evdata[1],evdata[2],evfrac[8],evfrac[4]); mvprintw(y,x,tmp); printw("Fractions: %5.1f %5.1f %5.1f \n", 100*evfrac[0],100*evfrac[1],100*evfrac[2]); printw("Buffers: MCP: %5.1f RICH2: %5.1f MWPC: %5.1f",evfrac[5],evfrac[6],evfrac[7]); return 0; } int dumpdata(int n, void *mem){ // dump part of data on the screen short *ptr=(short*) data; int i,j; char tmp[256]; static int short_size=sizeof(short); const int nadc=4; const int nch=256; int of = 50 + 2*(ptr[5]+ptr[6]+ptr[7]); sprintf(tmp,"Bytes:%d\nAdc\n",n); printw(tmp); if ( n >= of+nadc*nch ) { for (i=0;i\n"); for (i=0;i< nhelp;i++) main_help(i,help[i],shelp[i]); return 0; } /* ------------------- CatchSig ----------------- */ int ctrlcflag=0; void CatchSig () { ctrlcflag = 1; } int main(int argc,char **argv){ int maxlen=MAXDATALEN; int waskey; int x[3]={0,15,0},y[3]={3,5,7}; // cursor positions int flag=0; int mode=0; int eveflag; char tmp[256]; time_t t; int n; // intercept routine if (signal (SIGINT, CatchSig) == SIG_ERR) { perror ("sigignore"); } open_inkey_(); n=get_term_size(&nrow, &ncol); close_inkey_(); // print usage if (argc<2){ fprintf(stdout,"Usage: %s \n",argv[0]); fprintf(stdout,"nevents < 0 take data run forever\n"); exit(0); } // decode program arguments if (argc>1) sprintf(datapath,"%s",argv[1]); if (argc>2) neve=atoi(argv[2]); else neve=50000; if (argc>3) refreshrate=atoi(argv[3]); else refreshrate=1000; sprintf(flogfilename,"%s/daq.log",datapath); // print welcome message time(&t); fprintf(stdout,"#############################################\n"); fprintf(stdout,"Program %s version %2.1f\n",argv[0], VERSION); fprintf(stdout,"Compiled on %s %s\n",__DATE__, __TIME__); fprintf(stdout,"Runtime %s \n",ctime(&t)); fprintf(stdout,"#############################################\n"); blen=0; pdata = (short *) data; main_init(); // initialize daq #ifdef SUN camac_init(&mode); camac_begin(&mode); #endif // screen and key manager initscr(); main_screen_redraw(); // event loop okeve=0; while ( ((okeve RAND_MAX/2) pdata[3]=256; #endif if( pdata[4]==256 ) { // complete buffer if (pdata[9] & 0x1) evdata[1]++; // data shifted else evdata[0]++; // data nonshifted } else evdata[2]++; // incomplete buffer evdata[5]=pdata[5]; evdata[6]=pdata[6]; evdata[7]=pdata[7]; mstime=MsTimer(); // mvprintw(nrow-2,0,"Time %d, %d",mstime,refreshrate); if ( RefreshTimer(mstime,refreshrate)){ main_statistics(y[0],x[0],evdata, evfrac, okeve); move(y[2],x[2]); dumpdata(blen,data); } eveflag= (pdata[4]==256); // usefull event flag if (eveflag){ Ewrite(EVENT_RECORD_TYPE,runno,ieve,mstime,blen,data,fp); okeve++; } ieve++; key_ready_ (&waskey); if (waskey) flag=main_keyboard_event(); } endwin(); if (ctrlcflag) printf("User Program termination CTRL-C\n"); #ifdef SUN camac_end(&mode); #endif main_end(); //system("reset"); return 0; }