#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PMODE 0666 #define EVENT_INFO_LENGTH 68 // Information length for each event #define BIG 1500000 #define Odsize 2621440 #define TOdsize 2097152 //64 Channels (32 bit) #define dim 4096 ///////////////////////////////////////////////////////////////////// float Offset[256],Slope[256],Quad[256]; int OptGainMatch; void GainMatch(int Param); ////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ void ReadTrace() { int i, Rt,Odf,TOdf,Tdf,Tdf1; int Rtt; int counter ,channel; int j,l,k,m ; int jnum; int Param; unsigned long t1,t2,t3,t4; double tfpga,tcfd; unsigned long timespec; double timespec1; unsigned z; unsigned long z1; char ListFileName[150],OutFileName[150]; unsigned long eventdata, headerlength, eventlength, writedata; unsigned long eventlength1, eventlength2; unsigned long EVTTIME_LO, EVTTIME_HI, CFD_FRAC_TIME; unsigned long EVENTTIME_LO, EVENTTIME_HI; double EVENTTIME_LO1, EVENTTIME_HI1, EVENTTIME_LO2, EVENTTIME_HI2, CFD_TIME; int CFD_SOURCE, CFD_FORCE; double EVENTTIME_TOT1, EVENTTIME_TOT2, TDIFF, tstamp; double EVENTTIME_LO2w[100], EVENTTIME_HI2w[100], EVENTTIME_TOT2w[100]; double tlow; unsigned long TotalWords, TotalSkippedWords, NumEvents,NumEvents1; unsigned long TotNumEvents; unsigned long *Cha1,*En1; int FileNumber2bScanned,FileNumberScanned; long int rand_number; unsigned long delta12; double time0,deltat,CTm[64]; unsigned long CEvent,CEn[64]; double *tcfd1; int cla[16], clsum, clov1f,clov2f,clov3f; int clov4f,clov5f,clov6f,clov7f,clov8f,clov9f,clov10f,clov11f,clov12f,clov13f; int MergeFileNum; int FP , ngood; int Tdsize; unsigned *sp, *tsp; unsigned long En,GEn,GEn0,GEn1,GEn2,GEn3,GEnA,Channel,Slot, CrateID, Headerlength; int FinishCode; unsigned Slot1, Slot2, Channel1, Channel2,Channel3,Channel4,Channel5,Channel6,Channel7,Channel8,Channel9,Channel10,Channel11,Channel12,Channel13,Channel14,Channel15,Channel0,CrateID1, CrateID2, Headerlength1, Headerlength2; unsigned Slot2w[100], Channel2w[100]; unsigned clov[22]; unsigned long EventEnergy, TraceLength ; unsigned long EventEnergy1, EventEnergy2; unsigned long TraceLength1; unsigned long GEn01,GEn11,GEn21,GEn31,GEnA1; FILE *ListModeFile = NULL, *OutFile = NULL; FILE *Lf,*Lf1, *Lf2, *Lf3, *Lf4; int diffclover, diffclover1,diffclover2, sameclover, sameclover1, nevents, singleclover, singlecrystal, singlecrystal1, CLOVL, CLOVH ; int single[20]; char string[3]; float timewin; int nfiles; int itrace, ntrace; unsigned long trace[4096]; int nlines, nlines1, nlines2; int x[4096], y[4096]; int xcfd[4096], ycfd[4096]; int ff[4096]; int xff[4096], yff[4096]; int cfd[4096]; int optread=0; int cfd_delay, cfd_scale, cfd_th; int fl, fg; int ff_term1, ff_term2; float cfd_value; En1 = (unsigned long *)malloc(BIG); if (En1 == NULL) { printf("memory allocation problem\n"); exit(0);} Cha1 = (unsigned long *)malloc(BIG); if (Cha1 == NULL) { printf("memory allocation problem\n"); exit(0);} tcfd1 = (double *)malloc(BIG); if (tcfd1 == NULL) { printf("memory allocation problem\n"); exit(0);} sp = (unsigned int *)malloc(Odsize); for(i=0;i<(Odsize/4);i++) {sp[i]=0;} tsp = (unsigned int *)malloc(TOdsize); for(i=0;i<(TOdsize/4);i++) {tsp[i]=0;} Param = 64; nevents = 0 ; printf("set0\n"); system("clear"); printf("XIA List File Name ?\n"); scanf("%s",ListFileName); ListModeFile = fopen(ListFileName, "rb"); printf("Output File Name for Dumping Trace Data ?\n"); scanf("%s",OutFileName); OutFile = fopen(OutFileName, "w"); ntrace = 0; for(i=0; i<4096; i++){x[i] = 0; y[i] = 0; xcfd[i] = 0; ycfd[i] = 0; xff[i] = 0; yff[i] = 0; ff[i] = 0; cfd[i] = 0;} ff_term1 = 0; ff_term2 = 0; cfd_value = 0.; readdata: Channel1 = 0; Slot1 = 0; EVENTTIME_LO1 = 0.; EVENTTIME_HI1= 0.; EVENTTIME_TOT1 = 0.; EventEnergy1 =0; CFD_TIME = 0.; CFD_SOURCE = 0; CFD_FORCE = 0; itrace = 0; for(i=0; i<4096; i++){trace[i] = 0;} Rt =1; Rt=fread(&eventdata, 4, 1, ListModeFile); // Event # Channel1 = (eventdata & 0xF); // Slot # Slot1 = (eventdata & 0xF0) >> 4; // CrateID # CrateID1 = (eventdata & 0xF00) >> 8; // Header length Headerlength1 = (eventdata & 0x1F000) >> 12; // Event length eventlength1 = (eventdata & 0x7FFE0000) >> 17; // FinishCode FinishCode = (eventdata & 0x80000000) >> 31; // Reading EVTTIME_LO Rt=fread(&eventdata, 4, 1, ListModeFile); EVENTTIME_LO1 = eventdata; // Reading EVTHIGH & CFD Rt=fread(&eventdata, 4, 1, ListModeFile); EVENTTIME_HI1 = (eventdata & 0xFFFF); CFD_TIME = (eventdata & 0x3FFF0000) >> 16; CFD_SOURCE = (eventdata & 0x40000000) >> 30; CFD_FORCE = (eventdata & 0x80000000) >> 31; EVENTTIME_TOT1 = ((EVENTTIME_LO1 + EVENTTIME_HI1 * pow (2.0,32.0)) * 2.0 - CFD_SOURCE + (CFD_TIME/16384.))*4.0; // Reading Trace length & Event energy Rt=fread(&eventdata, 4, 1, ListModeFile); EventEnergy1 = (eventdata & 0xFFFF); TraceLength1 = (eventdata & 0x7FFF0000) >> 16; //Reading the Trace Points for(itrace = 0; itrace < TraceLength1; itrace++) { if(itrace%2 == 0) { Rt=fread(&eventdata, 4, 1, ListModeFile); trace[itrace] = (eventdata & 0xFFF); trace[itrace+1] = (eventdata & 0xFFF0000) >> 16; } } //CALCULATING FAST FILTER RESPONSE printf("THIS TRACE CORRESPONDS TO SLOT # %d CHANNEL # %d\n", Slot1, Channel1); printf("Please enter the fast length and the fast gap chosen for the fast filter\n"); scanf("%d %d", &fl, &fg); fl = fl/8; fg = fg/8; printf("Please enter the delay, the scale and the threshold (in ADC units) chosen for CFD\n"); scanf("%d %d %d", &cfd_delay, &cfd_scale, &cfd_th); cfd_delay = cfd_delay/8; printf("\n"); for(itrace = 0; itrace < TraceLength1; itrace++) { ff_term1 = 0; ff_term2 = 0; for(j=itrace-(fl-1); j<=itrace; j++){ff_term1 = ff_term1 + trace[j];} for(j=itrace-((2*fl)+fg-1); j<=itrace-(fl+fg); j++){ff_term2 = ff_term2 + trace[j];} if((itrace-((2*fl)+fg-1)>0)){ff[itrace] = ff_term1 - ff_term2;} // printf("%d %d FF[%d] = %d\n", ff_term1, ff_term2, itrace, ff[itrace]); } //CALCULATING CFD RESPONSE for(itrace = 0; itrace < TraceLength1; itrace++) { cfd[itrace+cfd_delay] = (ff[itrace+cfd_delay]*(1-(cfd_scale/8))) - (ff[itrace]); // printf("CFD Value = %d\n", cfd[itrace+cfd_delay]); } //CALCULATINF CFD VALUE cfd_value = 0.; for(itrace = 0; itrace < TraceLength1; itrace++) { // if(((cfd[itrace] > cfd_th) && (cfd[itrace] > 0)) && ((abs(cfd[itrace+1]) > cfd_th) && (cfd[itrace+1] < 0))) if(((cfd[itrace] > cfd_th) && (cfd[itrace] > 0)) && ((cfd[itrace+1] < 0))) { printf("I have hit a ZCP\n"); cfd_value = ((float) cfd[itrace]/((float) cfd[itrace] + (float) abs(cfd[itrace+1])))*16384.; } } //Plotting in ROOT nlines = 0; nlines1 = 0; nlines2 = 0; //printf("Starting output loop with trace length = %d words\n", TraceLength1); ntrace++; fprintf(OutFile, "Trace # %d (Channel # %d)\n", ntrace, Channel1); for(i=0; iDivide(1,3); TGraph* raw_trace = new TGraph(nlines,x,y); TGraph* ff_trace = new TGraph(nlines1,xff,yff); TGraph* cfd_trace = new TGraph(nlines2,xcfd,ycfd); draw_trace->cd(1); raw_trace -> SetMarkerStyle(7); raw_trace -> SetMarkerSize(5.0); raw_trace -> SetMarkerColor(4); raw_trace -> Draw("AP"); draw_trace->cd(2); ff_trace -> SetMarkerStyle(7); ff_trace -> SetMarkerSize(5.0); ff_trace -> SetMarkerColor(4); ff_trace -> Draw("AP"); draw_trace->cd(3); cfd_trace -> SetMarkerStyle(7); cfd_trace -> SetMarkerSize(5.0); cfd_trace -> SetMarkerColor(4); cfd_trace -> Draw("AP"); gPad->Modified(); gPad->Update(); gSystem->ProcessEvents(); gSystem->Sleep(100); gSystem->ProcessEvents(); gSystem->Sleep(100); gSystem->ProcessEvents(); Channel1 = 0; Slot1 = 0; EVENTTIME_LO1 = 0.; EVENTTIME_HI1= 0.; CFD_TIME = 0.; CFD_SOURCE = 0; CFD_FORCE = 0; EVENTTIME_TOT1 = 0.; EventEnergy1 =0; ff_term1 = 0; ff_term2 = 0; FinishCode = 0; printf("Do you want to read another trace ?\n"); scanf("%d", &optread); if(optread == 1) { goto readdata; } fclose(OutFile); } int main(int argc, char **argv) { TApplication theApp("App",&argc,argv); ReadTrace(); theApp.Run(); return 0; }