/* gcc -Wall -o csvpedsubshapersmk csvpedsubshapersmk.c */ #include #include #include #include #include #include #include #include #include #include #include #include char dirs[256]; int getParameters(int argc, char **argv); int csvPedsubShapers(FILE *fin,float clmean[][4], float clrms[][4],float pedsub[][4], float peaks[][4],int shaper); int getParameters(int argc, char **argv) { const char *short_options = "d:"; int option_index = 0; int c; int nbarg = 0; const struct option long_options[]= { {"dirs",1,0,'d'}, {0,0,0,0}, }; if (argc > 1) { while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != EOF) { switch (c) { case 'd': nbarg++; strcpy(dirs,optarg); break; default: printf("%s: Unknown option %s\n",__FUNCTION__,argv[optind-1]); return 1; } } } if (nbarg != 1) { printf("%s: wrong number of arguments %d\n",__FUNCTION__,nbarg); printf("%s: ./csvshapersmk --dirs \n", __FUNCTION__); return 2; } return 0; } int csvPedsubShapers(FILE *fin,float clmean[][4], float clrms[][4],float pedsub[][4], float peaks[][4],int shaper) { char buffer[512]; int channel; float peak0mean; float peak0rms; float peak0entries; float peak1mean; float peak1rms; float peak1entries; float peak2mean; float peak2rms; float peak2entries; float ped0mean; float ped0rms; float ped0entries; float ped1mean; float ped1rms; float ped1entries; float ped2mean; float ped2rms; float ped2entries; float cllengthmean; float cllengthrms; int cllengthentries; unsigned int Error,Warning; int line = 0; while (fgets(buffer,sizeof(buffer),fin)) { if (line != 0) { sscanf(buffer,"%d,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%d,%X,%X", &channel, &peak0mean,&peak0rms,&peak0entries, &peak1mean,&peak1rms,&peak1entries, &peak2mean,&peak2rms,&peak2entries, &ped0mean,&ped0rms,&ped0entries, &ped1mean,&ped1rms,&ped1entries, &ped2mean,&ped2rms,&ped2entries, &cllengthmean,&cllengthrms,&cllengthentries, &Error,&Warning); clmean[channel % 16][shaper] = cllengthmean; clrms[channel % 16][shaper] = cllengthrms; pedsub[channel % 16][shaper] = ped1mean; peaks[channel % 16][shaper] = peak1mean; } line++; } return 0; } int main(int argc, char *argv[]) { FILE *fin; FILE *fdirs; FILE *fcsv[3]; FILE *fcsverr[3]; int len; int shaper[4] = {0,1,3,7}; char buffer[512]; char filename[512]; float clmean[16][4]; float clrms[16][4]; float pedsub[16][4]; float peaks[16][4]; unsigned int errcode = 0; unsigned int warning = 0; if (getParameters(argc,argv) != 0) return(1); printf("Arguments: directories %s\n",dirs); shaper[0] = 0; shaper[1] = 1; shaper[2] = 3; shaper[3] = 7; fdirs = fopen(dirs,"r"); if (fdirs == NULL) { printf("%s: could not open directory file %s\n",argv[0],dirs); return 2; } int saltronb; for (int source = 1; source <=2; source++) { sprintf(filename,"saltro-mcm0-c7-pedsub-g3-p1-s%d-sh.csv",source); fcsv[source] = fopen(filename,"w"); sprintf(filename,"saltro-mcm0-c7-pedsub-g3-p1-s%d-sh-err.csv",source); printf("FILE: %s %d\n",filename,source); fcsverr[source] = fopen(filename,"w"); fprintf(fcsv[source],"saltronb,channel,ped0,ped1,ped3,ped7,peak0,peak1,peak3,peak7,\ clmean0,clmean1,clmean3,clmean7,clrms0,clrms1,clrms3,clrms7,\ diff01,diff03,diff07,diff13,diff17,diff37,errcode,warning\n"); fprintf(fcsverr[source],"saltronb,channel,ped0,ped1,ped3,ped7,peak0,peak1,peak3,peak7,\ clmean0,clmean1,clmean3,clmean7,clrms0,clrms1,clrms3,clrms7,\ diff01,diff03,diff07,diff13,diff17,diff37,errcode,warning\n"); } while (fgets(buffer,sizeof(buffer),fdirs)) { len = strlen(buffer); if (buffer[len-1] == '\n') buffer[len-1] = '\0'; sscanf(buffer,"%d/csv",&saltronb); for (int source = 1; source <=2; source++) { int nbfiles = 0; for (int sh=0; sh < 4; sh++) { for (int i = 0; i < 16; i++) { clmean[i][sh] = 0.0; clrms[i][sh] = 0.0; pedsub[i][sh] = 0.0; peaks[i][sh] = 0.0; } sprintf(filename,"%s/saltro%d-mcm0-c7-pedsub-g3-s%d-p1-s%d-sh.csv",buffer,saltronb,shaper[sh],source); fin = fopen(filename,"r"); if (fin == NULL) { printf("%s: could not open input file %s\n",argv[0],filename); } else { nbfiles++; printf("%s: could open input file %s\n",argv[0],filename); csvPedsubShapers(fin,clmean,clrms,pedsub,peaks,sh); fclose(fin); } } if (nbfiles == 4) { float shaperratio[7]; for (int ch = 0; ch < 16; ch++) { shaperratio[0] = (clmean[ch][0]-clmean[ch][0]); shaperratio[1] = (clmean[ch][0]-clmean[ch][1]); shaperratio[2] = (clmean[ch][0]-clmean[ch][2]); shaperratio[3] = (clmean[ch][0]-clmean[ch][3]); shaperratio[4] = (clmean[ch][1]-clmean[ch][2]); shaperratio[5] = (clmean[ch][1]-clmean[ch][3]); shaperratio[6] = (clmean[ch][2]-clmean[ch][3]); errcode = 0; if (((source == 1) && ((ch & 1) == 0)) || ((source == 2) && ((ch & 1) == 1))) { if (pedsub[ch][0] <= 5.0) errcode=0x0001; if (pedsub[ch][1] <= 5.0) errcode=0x0002; if (pedsub[ch][2] <= 5.0) errcode=0x0004; if (pedsub[ch][3] <= 5.0) errcode=0x0008; if (peaks[ch][0] <= 550.0) errcode |= 0x0010; if (peaks[ch][1] <= 550.0) errcode |= 0x0020; if (peaks[ch][2] <= 550.0) errcode |= 0x0040; if (peaks[ch][3] <= 500.0) errcode |= 0x0080; if (clmean[ch][0] < 1.0) errcode |= 0x0100; if (clmean[ch][1] < 1.0) errcode |= 0x0200; if (clmean[ch][2] < 1.0) errcode |= 0x0400; if (errcode == 0) { float ratio; ratio = shaperratio[1]/clmean[ch][0]; shaperratio[1] = ratio; if ((ratio<0.2) || (ratio>0.4)) errcode|=0x1000; ratio = shaperratio[2]/clmean[ch][0]; shaperratio[2] = ratio; if ((ratio<0.4) || (ratio>0.7)) errcode|=0x2000; ratio = shaperratio[3]/clmean[ch][0]; shaperratio[3] = ratio; if ((ratio<0.7) || (ratio>1.00)) { errcode|=0x4000; printf("RATIO %f %f %f errcode %X\n",ratio,shaperratio[3],peaks[ch][0],errcode); } ratio = shaperratio[4]/clmean[ch][1]; shaperratio[4] = ratio; ratio = shaperratio[5]/clmean[ch][1]; shaperratio[5] = ratio; ratio = shaperratio[6]/clmean[ch][2]; shaperratio[6] = ratio; } } /* if (shaperratio[1] < 10) errcode |= 0x0001; if (shaperratio[2] < 5 ) errcode |= 0x0002; if (shaperratio[3] < 10 ) errcode |= 0x0004; if (shaperratio[4] > -5 ) errcode |= 0x0008; if (shaperratio[5] < 5 ) errcode |= 0x0010; if (shaperratio[6] < 10 ) errcode |= 0x0020; */ fprintf(fcsv[source], "%d,%d,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%X,%X\n", saltronb,ch, pedsub[ch][0], pedsub[ch][1], pedsub[ch][2], pedsub[ch][3], peaks[ch][0], peaks[ch][1], peaks[ch][2], peaks[ch][3], clmean[ch][0],clmean[ch][1],clmean[ch][2],clmean[ch][3], clrms[ch][0],clrms[ch][1],clrms[ch][2],clrms[ch][3], shaperratio[1],shaperratio[2],shaperratio[3],shaperratio[4],shaperratio[5],shaperratio[6],errcode,warning); if (errcode != 0) { fprintf(fcsverr[source], "%d,%d,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%X,%X\n", saltronb,ch, pedsub[ch][0], pedsub[ch][1], pedsub[ch][2], pedsub[ch][3], peaks[ch][0], peaks[ch][1], peaks[ch][2], peaks[ch][3], clmean[ch][0],clmean[ch][1],clmean[ch][2],clmean[ch][3], clrms[ch][0],clrms[ch][1],clrms[ch][2],clrms[ch][3], shaperratio[1],shaperratio[2],shaperratio[3],shaperratio[4],shaperratio[5],shaperratio[6],errcode,warning); } } } } } fclose(fcsv[1]); fclose(fcsverr[1]); fclose(fcsv[2]); fclose(fcsverr[2]); fclose(fdirs); }