/* Compile with: gcc -Wall -o ldsmalltpcped srufunctions.o ldsmalltpcped.c ./ldsmalltpcped -f -p -t pedestal=0, threshold=0 ./ldsmalltpcped -p 0 -t 0 pedestals subtraction from file, common threshold ./ldsmalltpcped -f -t pedestal subtraction and threshold from file ./ldsmalltpcped -f -r */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "srufunctions.h" int checkvalues; int pedvalue; int setpedvalue; int thrvalue; int setthrvalue; float rmsfactor; char csvfile[256]; int pedSmalltpc[256]; int rmsSmalltpc[256]; int getParameters(int argc, char **argv); int scanPedfile(); int getParameters(int argc, char **argv) { const char *short_options = "f:p:t:r:"; int option_index = 0; int c; int nbarg = 0; const struct option long_options[]= { {"filecsv",1,0,'f'}, {"pedestal",1,0,'p'}, {"threshold",1,0,'t'}, {"rmsfactor",1,0,'r'}, {0,0,0,0}, }; checkvalues = 1; // check the values pedvalue = 0; setpedvalue = 0; thrvalue = 0; setthrvalue = 0; rmsfactor = 3.0; if (argc > 1) { while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != EOF) { switch (c) { case 'f': nbarg++; strcpy(csvfile,optarg); break; case 'p': nbarg++; sscanf(optarg,"%d",&pedvalue); setpedvalue = 1; break; case 't': nbarg++; sscanf(optarg,"%d",&thrvalue); setthrvalue = 1; break; case 'r': nbarg++; sscanf(optarg,"%f",&rmsfactor); break; default: printf("%s: Unknown option %s\n",__FUNCTION__,argv[optind-1]); return 1; } } } if (nbarg == 0) { puts("Arguments:"); puts("--filecsv,-f "); puts("--pedestal,-p "); puts("--threshold,-t "); puts("--rmsfactor,-r "); return(1); } return 0; } int scanPedfile() { FILE *fped; char buffer[512]; fped = fopen(csvfile,"r"); memset(pedSmalltpc,0,sizeof(pedSmalltpc)); memset(rmsSmalltpc,0,sizeof(rmsSmalltpc)); int nChan; int nPed; float ped; float rms; float entries; if (fped == NULL) { printf("%s: file not found %s\n",__FUNCTION__,csvfile); return (1); } while (fgets(buffer,sizeof(buffer),fped)) { printf("%s",buffer); sscanf(buffer,"%d,%d,%f,%f,%f",&nChan,&nPed,&ped,&rms,&entries); pedSmalltpc[nChan] = nPed; rmsSmalltpc[nChan] = (int)(rms*rmsfactor); if (rms < 0.5) rmsSmalltpc[nChan] = (int)rmsfactor; if (setpedvalue != 0) pedSmalltpc[nChan] = pedvalue; if (setthrvalue != 0) rmsSmalltpc[nChan] = thrvalue; printf("%d,%d,%f,%f,%f,%d,%d\n",nChan,nPed,ped,rms,entries,pedSmalltpc[nChan],rmsSmalltpc[nChan]); } fclose(fped); return (0); } int main(int argc, char *argv[]) { SRU srus; int status; if (getParameters(argc,argv) != 0) return(1); printf("Arguments: -f %s\n",csvfile); scanPedfile(); strcpy(srus.sruserver.controlip,SRU_IP); srus.sruserver.controlport = 4097; srus.listenport = 4100; status = scSruConnect(&srus); if (status != 0) return(0); int fec = 0; int chip = 0; int chan = 0; unsigned int vfped = 0; unsigned int vfrms = 0; FILE *fped; fped = fopen("smalltpc.csv","w"); fprintf(fped,"fec,chip,,chan,wrped,rdped,wrthr,rdthr\n"); for (int j = 0; j < 256; j++) { fec = j / 128; chip = (j / 16) & 0x7; chan = j % 16; // if (setpedvalue != 0) pedSmalltpc[j] = pedvalue; // if (setthrvalue != 0) rmsSmalltpc[j] = thrvalue; // Write and read pedestal status = saltroWriteRegister(srus.controlsocket,&srus.sruserver,0,fec,chip,chan,ALTRO_VFPED, pedSmalltpc[j]); if (status != 0) { printf ("STATUS1 %d\n",status); fclose(fped); close(srus.controlsocket); return(status); } status = saltroReadRegister(srus.controlsocket,&srus.sruserver,fec,chip,chan,ALTRO_VFPED,&vfped); if (status != 0) { printf ("STATUS2 %d\n",status); fclose(fped); close(srus.controlsocket); return(status); } if ( vfped != pedSmalltpc[j]) { printf("ERR PED: %d %d %d loaded %d(%X) read %d (%X)\n", fec,chip,chan,pedSmalltpc[j],pedSmalltpc[j],vfped,vfped); } // Write and read threshold status = saltroWriteRegister(srus.controlsocket,&srus.sruserver,0,fec,chip,chan,ALTRO_ZSTHR, rmsSmalltpc[j]); if (status != 0) { printf ("ZSTHR WR STATUS1 %d\n",status); fclose(fped); close(srus.controlsocket); return(status); } status = saltroReadRegister(srus.controlsocket,&srus.sruserver,fec,chip,chan,ALTRO_ZSTHR,&vfrms); if (status != 0) { printf ("ZSTHR RD STATUS2 %d\n",status); fclose(fped); close(srus.controlsocket); return(status); } if ( vfrms != rmsSmalltpc[j]) { printf("ERR THR: %d %d %d loaded %d(%X) read %d (%X)\n", fec,chip,chan,rmsSmalltpc[j],rmsSmalltpc[j],vfrms,vfrms); } // Output values to logfile fprintf(fped,"%d,%d,%d,%d,%d,%d,%d\n",fec,chip,chan,pedSmalltpc[j],vfped,rmsSmalltpc[j],vfrms); } fclose(fped); close(srus.controlsocket); return (0); }