#include "stdio.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TFile.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TF1.h"
#include "TMath.h"
#include "TStyle.h"
#include "TCanvas.h"
#include "TLine.h"
//~ #define USE_ZLIB
#ifdef USE_ZLIB
# include "zlib.h"
#endif
// ------------------------------------------------------------------------------
#define POSMARG 1000
#define READBUFFERLENGTH 50000
// data format
#define MAXDATA 40
#define RUNREC_ID 1
#define ENDREC_ID 2
#define POSREC_ID 3
#define EVTREC_ID 4
typedef struct {
unsigned long id,len;
unsigned long fver,time;
unsigned long nev,nch,ped,xy;
long nx,x0,dx,ny,y0,dy;
} RUNREC;
RUNREC *runrec;
RUNREC run;
typedef struct {
unsigned long id,len;
unsigned long time;
} ENDREC;
ENDREC *endrec;
typedef struct {
unsigned long id,len;
unsigned long time;
long ix,x,xset,iy,y,yset;
} POSREC;
POSREC *posrec;
POSREC pos;
typedef struct {
unsigned long id,len;
unsigned long nev;
unsigned short data[MAXDATA];
} EVTREC;
EVTREC *evtrec;
// ------------------------------------------------------------------------------
int d2time(char* dfile0="test")
{
unsigned long startTime = 0;
unsigned long endTime = 0;
int dbg=0;
printf(" dat 2 root conversion program\nUsage:\nd2r(input file -.dat, TDC min [ps], TDC max [ps], QDC min, QDC max)\n\n");
char fullname[256], sbuff[256];
FILE *fp;
//data buffer
char readbuf[READBUFFERLENGTH];
runrec = (RUNREC *) readbuf;
endrec = (ENDREC *) readbuf;
posrec = (POSREC *) readbuf;
evtrec = (EVTREC *) readbuf;
//data file
#ifdef USE_ZLIB
gzFile dfp;
#else
FILE *dfp;
#endif
char dfile[256];
int ftype=0, fcount=1;
do {
switch(ftype++) {
case 0:
sprintf(dfile, "data/%s_file%02d.dat", dfile0, fcount);
break;
case 1:
sprintf(dfile, "data/%s_file%02d.dat.gz", dfile0, fcount);
break;
case 2:
sprintf(dfile, "data/%s_file%02d.gz", dfile0, fcount);
break;
default:
printf(" Cannot find data file for %s !!!\n", dfile0);
return -1;
}
#ifdef USE_ZLIB
dfp=gzopen(dfile,"rb");
#else
dfp=fopen(dfile,"rb");
#endif
} while(!dfp);
printf("Opened data file %s\n", dfile);
// -----------------------------------------------
// loop trough records
UInt_t rec_id, rec_len;
unsigned ulsize = 4;
int ceve=0;
int end_of_file = 0;
while(1) {
#ifdef USE_ZLIB
if(gzeof(dfp)) end_of_file = 1;
#else
if(feof(dfp)) end_of_file = 1;
#endif
#ifdef USE_ZLIB
gzread(dfp, (voidp)&readbuf, 2*ulsize);
#else
fread( (void*)&readbuf, 2*ulsize, 1, dfp);
#endif
rec_id=readbuf[0];
rec_len=readbuf[ulsize];
if(dbg) printf("-----------------------------------------------\n");
if(dbg) printf("[%d] rec_id = %d | rec_len = %d\n", ceve, rec_id, rec_len);
switch(rec_id)
{
case RUNREC_ID:
#ifdef USE_ZLIB
gzread(dfp, (voidp)&readbuf[2*ulsize], (rec_len-2*ulsize));
#else
fread( (void*)&readbuf[2*ulsize], (rec_len-2*ulsize), 1, dfp);
#endif
run=*runrec;
if(dbg) {
printf("RUNREC_ID\n");
printf("id = %d, len = %d, time = %d\n", run.id, run.len, run.time);
printf("nev = %d, nch = %d\n", run.nev, run.nch);
printf("nx = %d, x0 = %d, dx = %d\n", run.nx, run.x0, run.dx);
printf("ny = %d, y0 = %d, dy = %d\n", run.ny, run.y0, run.dy);
break;
}
printf(">>>>>>>>>>>> RUNREC time = %u\n", run.time);
startTime = run.time;
break;
case POSREC_ID:
#ifdef USE_ZLIB
gzread(dfp, (voidp)&readbuf[2*ulsize], (rec_len-2*ulsize));
#else
fread( (void*)&readbuf[2*ulsize], (rec_len-2*ulsize), 1, dfp);
#endif
pos=*posrec;
if(dbg) {
printf("POSREC_ID\n");
printf("id = %d, len = %d, time = %d\n", posrec->id, posrec->len, posrec->time);
printf("ix = %d, x = %d, xset = %d\n", posrec->ix, posrec->x, posrec->xset);
printf("iy = %d, y = %d, yset = %d\n", posrec->iy, posrec->y, posrec->yset);
} else printf(" [%d,%d] %d, %d\n", pos.ix, pos.iy, pos.xset, pos.yset);
break;
case EVTREC_ID:
#ifdef USE_ZLIB
gzread(dfp, (voidp)&readbuf[2*ulsize], (rec_len-2*ulsize));
#else
fread( (void*)&readbuf[2*ulsize], (rec_len-2*ulsize), 1, dfp);
#endif
if(dbg) {
printf("EVTREC_ID\n");
printf("id = %d, len = %d, nev = %d\n", evtrec->id, evtrec->len, evtrec->nev);
//for(int datai = 0; datai < NCH; datai++) printf("%u ", evtrec->data[datai]);
//printf("\n");
//for(int datai = NCH; datai < NCH+NCH; datai++) printf("%u ", evtrec->data[datai]);
//printf("\n");
//break;
}
break;
case ENDREC_ID:
#ifdef USE_ZLIB
gzread(dfp, (voidp)&readbuf[2*ulsize], (rec_len-2*ulsize));
#else
fread( (void*)&readbuf[2*ulsize], (rec_len-2*ulsize), 1, dfp);
#endif
if(dbg) {
printf("ENDREC_ID\n");
printf("id = %d, len = %d, time = %d\n", endrec->id, endrec->len, endrec->time);
} else printf(" ENDREC\n");
printf(">>>>>>>>>>>> ENDREC time = %u\n", endrec->time);
endTime = endrec->time;
fcount++;
switch(ftype-1) {
case 0:
sprintf(dfile, "data/%s_file%02d.dat", dfile0, fcount);
break;
case 1:
sprintf(dfile, "data/%s_file%02d.dat.gz", dfile0, fcount);
break;
case 2:
sprintf(dfile, "data/%s_file%02d.gz", dfile0, fcount);
break;
}
#ifdef USE_ZLIB
if(dfp) gzclose(dfp);
dfp=gzopen(dfile,"rb");
#else
if(dfp) fclose(dfp);
dfp=fopen(dfile,"rb");
#endif
if(!dfp) {
printf(" Cannot open data file: %s ---> Exiting\n", dfile);
end_of_file = 1;
} else {
printf(" Opened data file: %s\n", dfile);
end_of_file = 0;
}
break;
default:
printf("switch(rec_id): default !!!\n");
end_of_file = 1;
break;
}
ceve++;
if( (ceve%50000) == 0) printf(" Current event = %d\n", ceve);
if(dbg) if( ceve>dbg ) break;
if(end_of_file) break;
}
#ifdef USE_ZLIB
if(dfp) gzclose(dfp);
#else
if(dfp) fclose(dfp);
#endif
printf(">>>>>>>>>>>> TIME = ENDREC.time - RUNREC.time = %u (s) = %.2lf (min)\n", endTime - startTime, (endTime - startTime)/60.);
if(dbg) return 1;
gSystem->Exit(1);
return 1;
}