/* gcc -Wall -o csvzsupshmk csvzsupshmk.c */ #include #include #include #include #include #include #include #include #include #include #include #include char dirs[256]; int getParameters(int argc, char **argv); int csvZerosupGains(FILE *fin,float zerosupgains[][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 csvZerosupgains(FILE *fin,float zerosupgains[][4],float peaks[][4],int gain) { char buffer[512]; int channel; float peak0mean; float peak0rms; float peak0entries; float peak1mean; float peak1rms; float peak1entries; float peak2mean; float peak2rms; float peak2entries; 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,%d,%X,%X", &channel, &peak0mean,&peak0rms,&peak0entries, &peak1mean,&peak1rms,&peak1entries, &peak2mean,&peak2rms,&peak2entries, &cllengthmean,&cllengthrms,&cllengthentries, &Error,&Warning); zerosupgains[channel % 16][gain] = cllengthmean; 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 zerosupgains[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-zerosup-s0-p1-s%d-sh.csv",gain,source); fcsv[source] = fopen(filename,"w"); sprintf(filename,"saltro-mcm0-c7-zerosup-s0-p1-s%d-sh-err.csv",source); printf("FILE: %s %d\n",filename,source); fcsverr[source] = fopen(filename,"w"); fprintf(fcsv[source],"saltronb,channel,peak0,peak1,peak3,peak7,\ shaper0,shaper1,shaper3,shaper7,diff01,diff03,diff07,diff13,diff17,diff37,errcode,warning\n"); fprintf(fcsverr[source],"saltronb,channel,peak0,peak1,peak3,peak7,\ shaper0,shaper1,shaper3,shaper7,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++) { zerosupgains[i][sh] = 0.0; peaks[i][sh] = 0.0; } sprintf(filename,"%s/saltro%d-mcm0-c7-zerosup-g%d-s0-p1-s%d.csv",buffer,saltronb,gain,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); csvZerosupgains(fin,zerosupgains,peaks,sh); fclose(fin); } } if (nbfiles == 4) { float gainratio[7]; for (int ch = 0; ch < 16; ch++) { gainratio[0] = (zerosupgains[ch][0]-zerosupgains[ch][0]); gainratio[1] = (zerosupgains[ch][0]-zerosupgains[ch][1]); gainratio[2] = (zerosupgains[ch][0]-zerosupgains[ch][2]); gainratio[3] = (zerosupgains[ch][0]-zerosupgains[ch][3]); gainratio[4] = (zerosupgains[ch][1]-zerosupgains[ch][2]); gainratio[5] = (zerosupgains[ch][1]-zerosupgains[ch][3]); gainratio[6] = (zerosupgains[ch][2]-zerosupgains[ch][3]); errcode = 0; if (((source == 1) && ((ch & 1) == 0)) || ((source == 2) && ((ch & 1) == 1))) { if (peaks[ch][0] <= 250.0) errcode |= 0x0010; if (peaks[ch][1] <= 250.0) errcode |= 0x0020; if (peaks[ch][2] <= 250.0) errcode |= 0x0040; if (peaks[ch][3] <= 200.0) errcode |= 0x0080; if (errcode == 0) { } } fprintf(fcsv[source], "%d,%d,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.3f,%6.3f,%6.3f,%6.3f,%6.3f,%6.3f,%X,%X\n", saltronb,ch, peaks[ch][0], peaks[ch][1], peaks[ch][2], peaks[ch][3], zerosupgains[ch][0],zerosupgains[ch][1],zerosupgains[ch][2],zerosupgains[ch][3], gainratio[1],gainratio[2],gainratio[3],gainratio[4],gainratio[5],gainratio[6],errcode,warning); if (errcode != 0) { fprintf(fcsverr[source], "%d,%d,%6.2f,%6.2f,%6.2f, %6.2f,%6.2f,%6.2f,%6.2f,%6.2f,%6.3f,%6.3f,%6.3f,%6.3f,%6.3f,%6.3f,%X,%X\n", saltronb,ch, peaks[ch][0], peaks[ch][1], peaks[ch][2], peaks[ch][3], zerosupgains[ch][0],zerosupgains[ch][1],zerosupgains[ch][2],zerosupgains[ch][3], gainratio[1],gainratio[2],gainratio[3],gainratio[4],gainratio[5],gainratio[6],errcode,warning); } } } } } fclose(fcsv[1]); fclose(fcsverr[1]); fclose(fcsv[2]); fclose(fcsverr[2]); fclose(fdirs); }