#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"
//kobayashi
#include "TTree.h"
//~ #define USE_ZLIB
#ifdef USE_ZLIB
# include "zlib.h"
#endif
// ------------------------------------------------------------------------------
#define POSMARG 1000
#define READBUFFERLENGTH 500000
// data format
#define MAXDATA 64
#define NCH 32
#define ANALYSE_CH 32
#define MPPC_1_CH 0
#define MPPC_2_CH 1
#define TRIGGER_CH 31
//~ #define ANALYSE_CH 8
//~ #define MPPC_1_CH 2
//~ #define MPPC_2_CH 5
#define REF_CH 0
#define TDC_BIN (25./1000.) //1 TDC bin in ns
#define MIKRO_BIN 0.3595/1000. //1 mikro step in mm
#define RUNREC_ID 1
#define ENDREC_ID 2
#define POSREC_ID 3
#define EVTREC_ID 4
//define testpulse
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 d2r(char* dfile0="test", double qdcmi=0, double qdcma=4096, double tdcmi=-1.5, double tdcma=3, double adc_cor_min=200.0, double deltaT_ctdcdiff=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;
//Chanel information
//~ int chmapx[NCH]={7,6,5,4,3,2,1,0};
//~ int chmapx[NCH]={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19};
int chmapx[NCH]; for(int i=0; i<NCH; i++) {chmapx[i]=i;};
//~ double padcenter[NCH][2]={ {201000, 210000},
//~ {183000, 210000},
//~ {165000, 210000},
//~ {147000, 210000},
//~ {129000, 210000},
//~ {111000, 210000},
//~ { 93000, 210000},
//~ { 75000, 210000} };
//~ // double tdcoffset[NCH]={57.78, 57.28, 57.86, 57.64, 56.06, 56.46, 57.5, 57.41};
//~ double tdcoffset[NCH]={2311*TDC_BIN, 2291*TDC_BIN, 2314*TDC_BIN, 2306*TDC_BIN,
//~ 2242*TDC_BIN, 2258*TDC_BIN, 2300*TDC_BIN, 2296*TDC_BIN};
double padcenter[NCH][2]; for(int i=0; i<NCH; i++) {padcenter[i][0]=0; padcenter[i][1]=0;};
double tdcoffset[NCH]; for(int i=0; i<NCH; i++) {tdcoffset[i]=0;};
sprintf(fullname, "d2r.ini");
if( (fp=fopen(fullname, "rt")) == NULL )
printf("Cannot open pad centers file %s !!!\n", fullname);
else {
printf("Opened pad centers file %s\n", fullname);
fgets(sbuff,256, fp);printf("%s", sbuff);
for(int i=0; i<NCH; i++)
fscanf(fp, "%lf %lf %lf\n", &padcenter[i][0], &padcenter[i][1], &tdcoffset[i]);
fclose(fp);
}
for(int i=0; i<NCH; i++) printf("%.2lf %.2lf %.2lf\n", padcenter[i][0], padcenter[i][1], tdcoffset[i]);
//TDC correction parameters
//~ double corpar[NCH][3]={ {-0.8, 25, 0},
//~ {-0.8, 25, 0},
//~ {-0.8, 25, 0},
//~ {-0.8, 25, 0},
//~ {-0.8, 25, 0},
//~ {-0.8, 25, 0},
//~ {-0.8, 25, 0},
//~ {-0.8, 25, 0} };
double corpar[NCH][3]; for(int i=0; i<NCH; i++) {corpar[i][0]=0.0; corpar[i][1]=0.005; corpar[i][2]=0.0;};
sprintf(fullname, "data/%s_cor.txt", dfile0);
if( (fp=fopen(fullname, "rt")) == NULL )
printf("Cannot open parameter file %s !!!\n", fullname);
else {
printf("Opened parameter file %s\n", fullname);
for(int i=0; i<NCH; i++) {
fscanf(fp, "%lf %lf %lf\n", &corpar[i][0], &corpar[i][1], &corpar[i][2]);
/*// check if parameters make sense
if( (corpar[i][0] < (tdcmi-0.2*tdcmi)) || ((tdcma+0.2*tdcma) < corpar[i][0]) ||
(corpar[i][1] < 0) || (1e4*TDC_BIN < corpar[i][1]) ||
(1e4 < TMath::Abs(corpar[i][2])) ) {
printf("Warning: parameters for ch%d out of limits -> using default!\n", i);
corpar[i][0]=2200*TDC_BIN; corpar[i][1]=1000*TDC_BIN; corpar[i][2]=-100*TDC_BIN;
}*/
}
fclose(fp);
}
for(int i=0; i<NCH; i++) printf("%.2lf %.2lf %.2lf\n", corpar[i][0], corpar[i][1], corpar[i][2]);
//histograms
char hname[256], htitle[256];
double adc,tdc;
TH1F *hadc[NCH], *hadc_cut[NCH];
TH1F *htdc[NCH], *htdc_cut[NCH], *hctdc[NCH];
TH2F *hcor[NCH];
TH1F *htdcdiff, *htdcdiff_cut;
double ref_cut_1_lo = qdcmi;
double ref_cut_1_hi = qdcma;
double ref_cut_2_lo = tdcmi;
double ref_cut_2_hi = tdcma;
//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);
//opens ROOT file
TFile *rootfile; char fnameroot[256];
sprintf(fnameroot, "root/%s.root", dfile0);
//rootfile = (TFile *) gROOT->FindObjectAny(dfile0);
//if (rootfile!=NULL) {printf("!!!\n");rootfile->Close();}
//rootfile = new TFile(fnameroot);
//if(rootfile) rootfile->Close();
rootfile = new TFile(fnameroot,"RECREATE",dfile0);
// -----------------------------------------------
// 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];
//kobayashi
if(rec_id == 4) rec_len = 140;
//~ if(dbg) printf("-----------------------------------------------\n");
//~ if(dbg) printf("[%d] rec_id = %d | rec_len = %d\n", ceve, rec_id, rec_len);
//printf("[%d] rec_id = %d | rec_len = %u\n", ceve, readbuf[0], readbuf[ulsize]);
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;
}
//create histograms
for(int i=0; i<ANALYSE_CH; i++) {
sprintf(hname, "hadc%d", i);
hadc[i] = (TH1F*)gROOT->FindObject(hname);
if(hadc[i]) delete hadc[i];
hadc[i] = new TH1F(hname, hname, 4096, -0.5, 4095.5);
sprintf(hname, "hadc_cut%d", i);
hadc_cut[i] = (TH1F*)gROOT->FindObject(hname);
if(hadc_cut[i]) delete hadc_cut[i];
hadc_cut[i] = new TH1F(hname, hname, 4096, -0.5, 4095.5);
sprintf(hname, "htdc%d", i);
htdc[i] = (TH1F*)gROOT->FindObject(hname);
if(htdc[i]) delete htdc[i];
htdc[i] = new TH1F(hname, hname, 65536, -32768.5*TDC_BIN, 32767.5*TDC_BIN);
//~ htdc[i] = new TH1F(hname, hname, 65536, -0.5*TDC_BIN, 65535.5*TDC_BIN);
sprintf(hname, "htdc_cut%d", i);
htdc_cut[i] = (TH1F*)gROOT->FindObject(hname);
if(htdc_cut[i]) delete htdc_cut[i];
htdc_cut[i] = new TH1F(hname, hname, 65536, -32768.5*TDC_BIN, 32767.5*TDC_BIN);
//~ htdc_cut[i] = new TH1F(hname, hname, 65536, -0.5*TDC_BIN, 65535.5*TDC_BIN);
sprintf(hname, "hctdc%d", i);
hctdc[i] = (TH1F*)gROOT->FindObject(hname);
if(hctdc[i]) delete hctdc[i];
hctdc[i] = new TH1F(hname, hname, 65536, -32768.5*TDC_BIN, 32767.5*TDC_BIN);
//~ hctdc[i] = new TH1F(hname, hname, 65536, -0.5*TDC_BIN, 65535.5*TDC_BIN);
sprintf(hname, "hcor%d", i);
hcor[i] = (TH2F*)gROOT->FindObject(hname);
if(hcor[i]) delete hcor[i];
//~ hcor[i] = new TH2F(hname, hname, 512, 0-0.5, 1024-0.5, 160, -1.5-(TDC_BIN/2), 2.5-(TDC_BIN/2));
hcor[i] = new TH2F(hname, hname, 512, 0-0.5, 4096-0.5, (100/TDC_BIN)/8, -50-(TDC_BIN/2), 50-(TDC_BIN/2));
//~ hcor[i] = new TH2F(hname, hname, 512, 0-0.5, 4096-0.5, 256, -32768.5*TDC_BIN, 32767.5*TDC_BIN);
//~ hcor[i] = new TH2F(hname, hname, 512, 0-0.5, 4096-0.5, 256, -0.5*TDC_BIN, 65535.5*TDC_BIN);
}
sprintf(hname, "htdcdiff");
htdcdiff = (TH1F*)gROOT->FindObject(hname);
if(htdcdiff) delete htdcdiff;
htdcdiff = new TH1F(hname, hname, 4096, -2048.5*TDC_BIN, 2047.5*TDC_BIN);
sprintf(hname, "htdcdiff_cut");
htdcdiff_cut = (TH1F*)gROOT->FindObject(hname);
if(htdcdiff_cut) delete htdcdiff_cut;
htdcdiff_cut = new TH1F(hname, hname, 4096, -2048.5*TDC_BIN, 2047.5*TDC_BIN);
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;
}
// events ------------------------------------------------------------------------------------------
// fill histograms
double tdc_noise_cut;
//~ double adc_cor_min;
double tdca[ANALYSE_CH];
double ctdca[ANALYSE_CH];
double adca[ANALYSE_CH];
double ctdcdiff, tdcdiff;
tdc_noise_cut = 5.0;
//~ adc_cor_min = 200.0;
for(int i=0; i<ANALYSE_CH; i++) {
tdc=((double)evtrec->data[i])*TDC_BIN + tdcoffset[i];
adc=(double)evtrec->data[i+NCH];
tdca[i]=tdc;
adca[i]=adc;
}
for(int i=0; i<ANALYSE_CH; i++) {
//~ if(0 < adca[i])
hadc[i]->Fill(adca[i]);
//~ if( (qdcmi < adca[i]) && (adca[i] < qdcma) )
//~ if(0 < tdca[i]) htdc[i]->Fill(tdca[i]-tdca[TRIGGER_CH]);
//~ if(0 < tdca[i])
htdc[i]->Fill(tdca[i]-tdca[TRIGGER_CH]);
//~ hcor[i]->Fill(adca[i],tdca[i]);
hcor[i]->Fill(adca[i],tdca[i]-tdca[TRIGGER_CH]);
if(adca[i] > corpar[i][2]) {
ctdca[i] = tdca[i]-tdca[TRIGGER_CH] - (corpar[i][0] + corpar[i][1]/TMath::Sqrt(adca[i] - corpar[i][2]));
} else {
ctdca[i] = -999.9*(i+1);
}
//~ if(tdc_noise_cut < tdca[i])
//~ double adc_max_cut = 1000;
//~ if(adca[i] < adc_max_cut)
hctdc[i]->Fill( ctdca[i] );
//~ if(dbg && (i<4) && (1 < tdca[0] && (1 < tdca[1]))) printf("tdca[%d] = %lf | tdca[TRG] = %lf | ctdca[i] = %lf | adca[i] = %lf\n", i, tdca[i],tdca[TRIGGER_CH],ctdca[i],adca[i]);
if(dbg && (i<4) ) printf("[%4d] tdca[%d] = %lf | tdca[TRG] = %lf | ctdca[i] = %lf | adca[i] = %lf\n", ceve, i, tdca[i],tdca[TRIGGER_CH],ctdca[i],adca[i]);
}
//cut za timing:
#define CUT_1_CONDITION ( (qdcmi < adca[MPPC_1_CH]) && (adca[MPPC_1_CH] < qdcma) && (tdcmi < adca[MPPC_2_CH]) && (adca[MPPC_2_CH] < tdcma) )
//~ // cut za meritev efficiency:
//~ #define CUT_1_CONDITION ( (qdcmi < adca[MPPC_1_CH]) && (adca[MPPC_1_CH] < qdcma) )
//~ #define CUT_1_CONDITION ( (tdcmi < ctdcdiff) && (ctdcdiff < tdcma) )
//~ #define CUT_1_CONDITION ( (ref_cut_1_lo < adca[MPPC_1_CH]) && (adca[MPPC_1_CH] < ref_cut_1_hi) )
//~ #define CUT_1_CONDITION ( (qdcmi < tdca[MPPC_1_CH]) && (tdca[MPPC_1_CH] < qdcma) )
//~ #define CUT_1_CONDITION ( (ref_cut_1_lo < adca[REF_CH]) && (adca[REF_CH] < ref_cut_1_hi) && (ref_cut_1_lo < adca[MPPC_1_CH]) && (adca[MPPC_1_CH] < ref_cut_1_hi) )
// cut za meritev efficiency:
#define CUT_2_CONDITION ( (tdcmi < adca[REF_CH]) && (adca[REF_CH] < tdcma) )
//~ #define CUT_2_CONDITION ( (qdcmi < adca[MPPC_1_CH]) && (adca[MPPC_1_CH] < qdcma) && (tdcmi < adca[REF_CH]) && (adca[REF_CH] < tdcma) )
//~ #define CUT_2_CONDITION ( (ref_cut_1_lo < tdca[MPPC_1_CH]) && (tdca[MPPC_1_CH] < ref_cut_1_hi) )
//~ #define CUT_2_CONDITION ( (qdcmi < adca[REF_CH]) && (adca[REF_CH] < qdcma) && (tdcmi < tdca[REF_CH]) && (tdca[REF_CH] < tdcma) )
//~ #define CUT_2_CONDITION ( (ref_cut_1_lo < adca[MPPC_1_CH]) && (adca[MPPC_1_CH] < ref_cut_1_hi) && (ref_cut_2_lo < adca[MPPC_2_CH]) && (adca[MPPC_2_CH] < ref_cut_2_hi) )
//~ #define CUT_2_CONDITION ( (ref_cut_2_lo < adca[REF_CH]) && (adca[REF_CH] < ref_cut_2_hi) )
//~ #define CUT_2_CONDITION ( (ref_cut_1_lo < tdca[MPPC_1_CH]) && (tdca[MPPC_1_CH] < ref_cut_1_hi) && (ref_cut_2_lo < tdca[MPPC_2_CH]) && (tdca[MPPC_2_CH] < ref_cut_2_hi) )
//~ #define CUT_2_CONDITION ( (ref_cut_2_lo < tdca[REF_CH]) && (tdca[REF_CH] < ref_cut_2_hi) && (ref_cut_2_lo < tdca[MPPC_1_CH]) && (tdca[MPPC_1_CH] < ref_cut_2_hi) )
if( (tdc_noise_cut < tdca[MPPC_1_CH]) && (tdc_noise_cut < tdca[MPPC_2_CH])) {
//~ if( (qdcmi < adca[MPPC_1_CH]) && (tdcmi < adca[MPPC_2_CH]) )
htdcdiff->Fill(ctdcdiff);
if( CUT_1_CONDITION ) {
htdcdiff_cut->Fill(ctdcdiff);
//~ htdcdiff_cut->Fill(tdcdiff);
}
}
for(int i=0; i<ANALYSE_CH; i++) {
//~ if( CUT_1_CONDITION ) {
//~ if( ( (qdcmi < adca[REF_CH]) && (adca[REF_CH] < qdcma) ) ) {
//~ if( ( (-2 < ctdca[0]) && (ctdca[0] < 2) ) ) {
//~ htdc_cut[i]->Fill(tdca[i]-tdca[31]);
//~ htdc_cut[i]->Fill(ctdca[i]);
//~ hadc_cut[i]->Fill(adca[i]);
//~ }
if( (qdcmi < adca[i]) && (adca[i] < qdcma) && (adca[i] > corpar[i][2]) )
htdc_cut[i]->Fill(ctdca[i]);
if( (tdcmi < ctdca[i]) && (ctdca[i] < tdcma) )
hadc_cut[i]->Fill(adca[i]);
}
// events ------------------------------------------------------------------------------------------
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");
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
if(dbg) return 1;
if(rootfile) {
rootfile->Write();
//cout<<"maximum x value = " << htdcdiff->GetXaxis()->GetBinCenter( htdcdiff->GetMaximumBin()) <<endl;
printf("Saved to %s\n", fnameroot);
rootfile->Close();
}
gSystem->Exit(1);
return 1;
}