/* * histos-hbook.c : utility to read histogram data from a text file * created by Jlab CASA group and store the data in * an hbook histogram file. * * Usage: histos-hbook * * Author: Richard Jones * Created: January 12, 2007 */ #define PAWC_SIZE 5000000 #include #include struct { int IQ[PAWC_SIZE]; } pawc_; int main(int argc, char *argv[]) { FILE *fhistos; FILE *fhbook; int lun,lrecl,istat; char chtop[] = "HBOOK"; char chopt[] = "NX"; char *title = 0; int ltitle = 0; int id; int cycle; int psize = PAWC_SIZE; hlimit_(&psize); if (argc != 3) { printf("Usage: histos-hbook \n"); exit(1); } fhistos = fopen(argv[1],"r"); if (fhistos == 0) { printf("Error: failed to open input histos file %s\n",argv[1]); exit(1); } lun = 1; lrecl = 4096; hropen_(&lun,chtop,argv[2],chopt,&lrecl,&istat, strlen(chtop),strlen(argv[2]),strlen(chopt)); if (istat != 0) { printf("Error: failed to open output hbook file %s\n",argv[2]); exit(1); } id = 0; while (getline(&title,<itle,fhistos) > 0) { title[strlen(title)-1] = 0; if (strstr(title,"1D") != 0) { int nbins; float xlim[2]; float *cont; float cap=0; int i; if (fscanf(fhistos,"%d %f %f\n",&nbins,&xlim[0],&xlim[1]) != 3) { printf("Error: input file not in expected format\n"); exit(1); } if ((cont = malloc(nbins*sizeof(float))) == 0) { printf("Error: memory allocation error\n"); exit(1); } for (i=0; i < nbins; ++i) { if (fscanf(fhistos,"%d ",&cont[i]) != 1) { printf("Error: histogram data format error\n"); exit(1); } } fscanf(fhistos,"\n"); ++id; hbook1_(&id,title,&nbins,&xlim[0],&xlim[1],&cap,strlen(title)); hpak_(&id,cont); fscanf(fhistos,"\n"); free(cont); } else if (strstr(title,"2D") != 0) { int nbin[2]; float xlim[2],ylim[2]; float *cont; float cap=0; int i; if (fscanf(fhistos,"%d %d %f %f %f %f",&nbin[0],&nbin[1], &xlim[0],&ylim[0],&xlim[1],&ylim[1]) != 6) { printf("Error: input file not in expected format\n"); exit(1); } if ((cont = malloc(nbin[0]*nbin[1]*sizeof(float))) == 0) { printf("Error: memory allocation error\n"); exit(1); } for (i=0; i < nbin[0]*nbin[1]; ++i) { if (fscanf(fhistos,"%d ",&cont[i]) != 1) { printf("Error: histogram data format error\n"); exit(1); } } ++id; hbook2_(&id,title,&nbin[0],&xlim[0],&xlim[1], &nbin[1],&ylim[0],&ylim[1],&cap,strlen(title)); hpak_(&id,cont); fscanf(fhistos,"\n"); free(cont); } else { printf("Error: input file contains unknown histogram type\n"); printf("\"%s\"\n",title); exit(1); } } id = 0; sprintf(chopt," "); hrout_(&id,&cycle,chopt,strlen(chopt)); hrend_(chtop,strlen(chtop)); }