/* gcc -Wall -o csvgainsmk csvgainsmk.c */ #include #include #include #include #include #include #include #include #include #include #include #include char dirs[256]; int getParameters(int argc, char **argv); 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: ./csvgainsmk --dirs \n", __FUNCTION__); return 2; } return 0; } int csvPedestalGain(FILE *fin,float gainped[][4], int gain) { char buffer[512]; int channel; int entries; float ped,rms; unsigned int Error,Warning; int line = 0; while (fgets(buffer,sizeof(buffer),fin)) { if (line != 0) { sscanf(buffer,"%d,%f,%f,%d,%X,%X", &channel,&ped,&rms,&entries,&Error,&Warning); gainped[channel % 16][gain] = ped; } line++; } return 0; } int main(int argc, char *argv[]) { FILE *fin; FILE *fdirs; FILE *fcsv; int len; char buffer[512]; char filename[512]; float gainped[16][4]; unsigned int errcode = 0; unsigned int warning = 0; if (getParameters(argc,argv) != 0) return(1); printf("Arguments: directories %s\n",dirs); fdirs = fopen(dirs,"r"); if (fdirs == NULL) { printf("%s: could not open directory file %s\n",argv[0],dirs); fclose(fdirs); return 2; } int saltronb; fcsv = fopen("saltro-mcm0-c7-pedgains-s0-p1.csv","w"); fprintf(fcsv,"saltronb,channel,gain0,gain1,gain2,gain3,diff01,diff02,diff03,diff12,diff13,diff23,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); int nbfiles = 0; for (int gain=0; gain < 4; gain++) { for (int i = 0; i < 16; i++) gainped[i][gain] = 0.0; sprintf(filename,"%s/saltro%d-mcm0-c7-pedestal-g%d-s0-p1.csv",buffer,saltronb,gain); 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); csvPedestalGain(fin,gainped,gain); fclose(fin); } } if (nbfiles == 4) { float pedratio[7]; for (int ch = 0; ch < 16; ch++) { pedratio[0] = (gainped[ch][0]-gainped[ch][0]); pedratio[1] = (gainped[ch][0]-gainped[ch][1]); pedratio[2] = (gainped[ch][0]-gainped[ch][2]); pedratio[3] = (gainped[ch][0]-gainped[ch][3]); pedratio[4] = (gainped[ch][1]-gainped[ch][2]); pedratio[5] = (gainped[ch][1]-gainped[ch][3]); pedratio[6] = (gainped[ch][2]-gainped[ch][3]); errcode = 0; if (pedratio[1] < 10) errcode |= 0x0001; if (pedratio[2] < 5 ) errcode |= 0x0002; if (pedratio[3] < 10 ) errcode |= 0x0004; if (pedratio[4] > -5 ) errcode |= 0x0008; if (pedratio[5] < 5 ) errcode |= 0x0010; if (pedratio[6] < 10 ) errcode |= 0x0020; fprintf(fcsv,"%d,%d,%6.2f,%6.2f,%6.2f,%6.2f,%6.3f,%6.3f,%6.3f,%6.3f,%6.3f,%6.3f,%d,%d\n",saltronb,ch, gainped[ch][0],gainped[ch][1],gainped[ch][2],gainped[ch][3], pedratio[1],pedratio[2],pedratio[3],pedratio[4],pedratio[5],pedratio[6],errcode,warning); } } } fclose(fcsv); fclose(fdirs); }