#define ROL_NAME__ "ROL2" //#define MAX_EVENT_LENGTH 10240 //#define MAX_EVENT_POOL 200 #define MAX_EVENT_POOL 5 #define MAX_EVENT_LENGTH 4096*200 /* Size in Bytes */ // 800 kb x 5 buffers; randy<=2 /* POLLING_MODE */ #define POLLING___ #define POLLING_MODE #define EVENT_MODE #define INIT_NAME rol2_list__init #include #include #include #include #define MAXBANKS 10 #define F1_BLOCK_HEAD 16 #define F1_BLOCK_TRAIL 17 #define F1_EVENT_HEAD 18 #define F1_TRIG_TIME 19 #define F1_MEAS 23 #define F1_CHIP_HEAD 24 #define F1_NOT_VALID 30 #define F1_FILLER 31 #define F1_CHIP_HEAD 24 #define F1_CHIP_TRIG_TIME 0xFF80 #define F1_CHIP_CHIP 0x38 #define F1_CHIP_CHAN 0x7 #define F1_SLOT 0x7C00000 #define F1_ID 0x3C0000 #define F1_BLOCK 0x3FF00 #define F1_NEVT_BLOCK 0xFF #define F1_TRIG_NUMB 0x3FFFFF #define F1_TDE 0xFFFF #define F1_CHIP 0x380000 #define F1_CHAN 0x70000 #define F1_TIME 0xFFFF #define FA250_BLOCK_HEAD 16 #define FA250_BLOCK_TRAIL 17 #define FA250_EVENT_HEAD 18 #define FA250_SLOT 0x7C00000 #define FA250_ID 0x3C0000 #define FA250_BLOCK 0x3FF00 #define FA250_NEVT_BLOCK 0xFF #define ROCLEN 80 int syncEvent = 0; static int f1_sync[21][8][8]; static int my_event; static int f1_sync_slot[21]; static int f1_sync_crate; char roc_name[ROCLEN]; // #undef SHM_BUF #ifdef SHM_BUF //-------------- RCM MONOTORING ------------------- #include "shmem_roc.h" static void shmem_get(); static int semid,shmid; static roc_shmem *shmem_ptr; unsigned int *HIST_buffer; int *skip_event_counter; int *HIST_FLAG; int *RUN_NUM; int *RUN_FLAG; int *buffer_size; //-- timer -- struct timeval tvA[10], tvB[10]; SHM_HIST *hrol2; SHM_HIST *herr; //----------------------------------------- typedef struct { unsigned int Size: 32; unsigned short Type: 16; unsigned short Pad: 2; unsigned short BOS: 6; unsigned short Nev: 8; unsigned short ID; // :8; unsigned short seg; // :8; unsigned short Len; // :16; } TriggerHeader; TriggerHeader trigger; trigger_decode(unsigned int *bank) { trigger.Size=bank[0]; trigger.Type=(bank[1]>>16)&0xFFFF; trigger.BOS=(bank[1]>>8)&0x3F; trigger.Nev=(bank[1])&0xFF; trigger.ID=(bank[2]>>24)&0xFF; trigger.seg=(bank[2]>>16)&0xFF; trigger.Len=(bank[2])&0xFFFF; } void trigger_print() { printf(" --> Trigger Header: BankType=0x%04X bos=0x%02X Size=%6d Nev=%d ID=0x%02x seg=0x%02X Len=%4d\n" ,trigger.Type ,trigger.BOS ,trigger.Size ,trigger.Nev ,trigger.ID ,trigger.seg, trigger.Len ); } #endif #ifdef DQM_HIST //-------------------------------------- ROL_2 MONOTORING ------------------- int init_hist(); int close_hist(); int fill_bank(unsigned int *bank, int size); int reset_hist(int flag) ; static int HIST_INIT; //------------------- pthread_t hist_thread; static void *fill_bank_thread (void *arg ) { unsigned int *buffer = (unsigned int*) arg; *HIST_FLAG=0; prctl(PR_SET_NAME,"hist_thread"); while(1) { if (*HIST_FLAG==1) { gettimeofday(&tvA[2], NULL); fill_bank (buffer,*buffer_size); gettimeofday(&tvB[2], NULL); hf1(hrol2,2, tvB[2].tv_usec - tvA[2].tv_usec + 1000000 * (tvB[2].tv_sec - tvA[2].tv_sec)); hf1(hrol2,3, (tvB[2].tv_usec - tvA[2].tv_usec + 1000000 * (tvB[2].tv_sec - tvA[2].tv_sec))/1000); //printf("hist process = %d \n",tvB[2].tv_usec - tvA[2].tv_usec + 1000000 * (tvB[2].tv_sec - tvA[2].tv_sec)); *HIST_FLAG=0; } else if (*HIST_FLAG==2) { daLogMsg("INFO","ROL_2 EXIT fill_bank_thread"); return 0; } else { usleep(100); } } } //---------------------------------------------------------------------- #endif static void __download() { daLogMsg("INFO","Readout list compiled %s", DAYTIME); #ifdef POLLING___ rol->poll = 1; #endif *(rol->async_roc) = 0; /* Normal ROC */ { /* begin user */ daLogMsg("INFO","User Download 2 Executed"); #ifdef SHM_BUF daLogMsg("INFO","ROL_2 SHMEM RCM ..."); shmem_get(); daLogMsg("INFO","ROL_2 SHMEM RCM attached "); HIST_buffer=shmem_ptr->EventBuffer; HIST_FLAG=&shmem_ptr->EventBufferFLAG; RUN_FLAG=&shmem_ptr->RUN_Flag; RUN_NUM=&shmem_ptr->RUN_Number; buffer_size=&shmem_ptr->EventLength; skip_event_counter=&shmem_ptr->EventSkipCounter; hrol2 = shmem_ptr->H_rol2; herr = shmem_ptr->H_err; hbook1(hrol2,1,"ROL_2 copy to buffer, [us] ",500,0.,500.); hbook1(hrol2,2,"ROL_2 hist processing, [us] ",500,0.,500.); hbook1(hrol2,3,"ROL_2 hist processing, [ms] ",50,0.,50.); hbook1(hrol2,11,"ROL_2 Trigger Types ",50,0.,50.); hbook1(herr,1,"ROL_2 Errors ",50,0.,50.); #endif #ifdef DQM_HIST daLogMsg("INFO","ROL_2 HIST_INIT, HIST_FLAG=%d ",*HIST_FLAG); printf(" ====== ROL_2 HIST_INIT = %d ====== \n",HIST_INIT); init_hist(); HIST_INIT=1; //-------------------------------------------------------------- daLogMsg("INFO","ROL_2 Start Hist Thread "); int ret = pthread_create (&hist_thread, 0, fill_bank_thread, HIST_buffer); if (ret) { perror ("pthread_create"); } #endif gethostname(roc_name,ROCLEN); } /* end user */ } /*end download */ static void __prestart() { CTRIGINIT; *(rol->nevents) = 0; { /* begin user */ unsigned long jj, adc_id; daLogMsg("INFO","Entering User Prestart 2"); EVENT_INIT; CTRIGRSS(EVENT,1,davetrig,davetrig_done); CRTTYPE(1,EVENT,1); rol->poll = 1; #ifdef DQM_HIST reset_hist(0); #endif #ifdef SHM_BUF *RUN_NUM=rol->runNumber; printf("ROL2 :: PRE: Run Number %d(%d), Config id = %d\n",rol->runNumber,*RUN_NUM,rol->runType); *HIST_FLAG=2; *skip_event_counter=0; #endif daLogMsg("INFO","User Prestart 2 executed"); } /* end user */ if (__the_event__) WRITE_EVENT_; *(rol->nevents) = 0; rol->recNb = 0; } /*end prestart */ static void __end() { { /* begin user */ daLogMsg("INFO","User End 2 Executed"); #ifdef SHM_BUF *HIST_FLAG=3; //-- save hist printf("ROL2 :: END Run Number %d, Config id = %d\n",rol->runNumber,rol->runType); //reset_hist(0); #endif } /* end user */ if (__the_event__) WRITE_EVENT_; } /* end end block */ static void __pause() { { /* begin user */ daLogMsg("INFO","User Pause 2 Executed"); } /* end user */ if (__the_event__) WRITE_EVENT_; } /*end pause */ static void __go() { { /* begin user */ daLogMsg("INFO","Entering User Go 2"); printf("ROL2 :: rocGo: Activating Run Number %d, Config id = %d\n",rol->runNumber,rol->runType); #ifdef SHM_BUF *RUN_NUM=rol->runNumber; #endif } /* end user */ if (__the_event__) WRITE_EVENT_; } void davetrig(unsigned long EVTYPE,unsigned long EVSOURCE) { int nbanks; unsigned int banktag[MAXBANKS]; unsigned int banknr[MAXBANKS]; unsigned int banknw[MAXBANKS]; unsigned int banktyp[MAXBANKS]; unsigned int bankpad[MAXBANKS]; uint32_t *bankdata[MAXBANKS]; long EVENT_LENGTH; { /* begin user */ int ii, it, FILL_EVENT=0; EVENT_GET; int debug_f1_bank = 0; int nw, jj; uint32_t *ptr, *ptrend; nbanks = 0; ptr = (uint32_t *)((rol->dabufpi - 2)); ptrend = ptr + *(rol->dabufpi - 2); ptr +=2; /*skip bank-of-banks header*/ if(debug_f1_bank) printf("BANKSCAN: tag=%d typ=%d num=%d\n",((*(ptr-1))>>16)&0xFF,((*(ptr-1))>>8)&0xFF,(*(ptr-1))&0xFF); // while(ptr < ptrend){ // int aa = (*ptr); // printf("TEST 0x%x ",aa); fflush(stdout); // ptr++; // } // printf("\n"); fflush(stdout); // ptr = (uint32_t *)((rol->dabufpi)); // ptrend = ptr + *(rol->dabufpi); while(ptr < ptrend) { if(debug_f1_bank) printf("BANKSCAN[%d]: while begin: ptr=0x%08x ptrenv=0x%08x (%d)\n",nbanks,ptr,ptrend,(ptrend-ptr));fflush(stdout); banknw[nbanks] = (*ptr) - 1; /*data length only*/ if(debug_f1_bank) printf("nw=%d\n",banknw[nbanks]);fflush(stdout); ptr ++; if(debug_f1_bank) printf("ptr tmp 0x%x\n",ptr); fflush(0); banktag[nbanks] = ((*ptr)>>16)&0xffff; if(debug_f1_bank) printf("tag=0x%08x\n",banktag[nbanks]);fflush(stdout); bankpad[nbanks] = ((*ptr)>>14)&0x3; if(debug_f1_bank) printf("pad=%d\n",bankpad[nbanks]);fflush(stdout); banktyp[nbanks] = ((*ptr)>>8)&0x3f; if(debug_f1_bank) printf("typ=0x%08x\n",banktyp[nbanks]);fflush(stdout); banknr[nbanks] = (*ptr)&0xff; if(debug_f1_bank) printf("nr=%d\n",banknr[nbanks]);fflush(stdout); ptr ++; if(debug_f1_bank) printf("ptr tmp 1 0x%x\n",ptr); fflush(0); bankdata[nbanks] = ptr; if(debug_f1_bank) printf("data(ptr)=0x%08x\n",bankdata[nbanks]);fflush(stdout); ptr += banknw[nbanks]; if(debug_f1_bank) printf("ptr tmp 2 0x%x\n",ptr); fflush(0); nbanks ++; if(nbanks >= MAXBANKS) {printf("rol.h ERROR: nbanks=%d - exit\n");fflush(stdout);break;}; // printf("BANKSCAN[%d]: while end: ptr=0x%08x ptrenv=0x%08x (%d)\n",nbanks,ptr,ptrend,(ptrend-ptr));fflush(stdout); } int debug_f1 = 0; int trig_time = 0; int slot = 0; int mod_id = 0; int block_numb = 0; int nevt_block = 0; int trig_numb = 0; int chip_stat = 0; int hit_fifo_over = 0; int out_fifo_over = 0; int chip_numb = 0; int chip_chan = 0; int time = 0; int channel = -1; int chip_head_time = -1; int chip_head_chip = -1; int chip_head_chan = -1; int block_head = 0; int time_in_block[21]; int block_checked[21]; f1_sync_crate = 0; // memset(f1_sync_slot,0,sizeof(f1_sync_slot)); memset(block_checked,0,sizeof(block_checked)); memset(time_in_block,0,sizeof(time_in_block)); for(jj = 0; jj < nbanks; jj++) { // printf("bankscan[%d]: tag 0x%08x typ=%d nr=%d nw=%d dataptr=0x%08x\n", // jj,banktag[jj],banktyp[jj],banknr[jj],banknw[jj],bankdata[jj]); if(banktag[jj] == 26){ my_event++; ptr = bankdata[jj]; ptrend = bankdata[jj] + banknw[jj] - 1; // printf("ptr = 0x%x ptrend = 0x%x \n", ptr, ptrend); while(ptr < ptrend){ int event_in_block; uint32_t data = LSWAP(*ptr); if( (data >> 27) == F1_BLOCK_HEAD){ if(block_head != 0){ printf(" FATAL: Missing global trailer \n "); } slot = (data & F1_SLOT) >> 22; mod_id = (data & F1_ID) >> 18; block_numb = (data & F1_BLOCK) >> 8; nevt_block = (data & F1_NEVT_BLOCK); if(debug_f1) printf("\n BLOCK HEADER: SLOT = %d ID = %d BLOCK NUMBER = %d NEVT_BLOCK = %d \n", slot, mod_id, block_numb, nevt_block); block_head = 1; } if( (data >> 27) == F1_EVENT_HEAD){ slot = (data & F1_SLOT) >> 22; trig_numb = (data & F1_TRIG_NUMB); event_in_block = (trig_numb % nevt_block); // if(debug_f1) // printf("\n EVENT HEADER: SLOT = %d Trigger number = %d\n",slot,trig_numb); } // Trigger Time if( (data >> 27) == F1_TRIG_TIME){ trig_time = (data & F1_TDE); // if(debug_f1) // printf("TRIGGER TIME DE = %d \n",trig_time); } if( (data >> 27) == F1_MEAS){ chip_stat = ((data >> 26) & 0x1); hit_fifo_over = ((data >> 25) & 0x1); out_fifo_over = ((data >> 24) & 0x1); chip_numb = (data & F1_CHIP) >> 19; chip_chan = (data & F1_CHAN) >> 16; time = (data & F1_TIME); int ch_tmp = -1; if( (chip_chan == 0) || (chip_chan == 1)) ch_tmp = 0; if( (chip_chan == 2) || (chip_chan == 3)) ch_tmp = 1; if( (chip_chan == 4) || (chip_chan == 5)) ch_tmp = 2; if( (chip_chan == 6) || (chip_chan == 7)) ch_tmp = 3; if( (ch_tmp >= 0) && (ch_tmp < 4) ){ channel = chip_numb*4 + ch_tmp; } if(debug_f1){ printf("\n HITS: Chip = %d Chip Chan = %d Channel = %d Time = %d Slot = %d \n", chip_numb, chip_chan, channel, time, slot ); // printf("Chip status = %d Hit FIFO over = %d Out FIFO over = %d\n", // chip_stat, hit_fifo_over, out_fifo_over); } } if( (data >> 27) == F1_CHIP_HEAD){ chip_head_time = (data & F1_CHIP_TRIG_TIME) >> 7; chip_head_chip = (data & F1_CHIP_CHIP) >> 3; chip_head_chan = (data & F1_CHIP_CHAN); if(event_in_block == 0){ if(block_checked[slot] == 0){ time_in_block[slot] = chip_head_time; block_checked[slot] = 1; } } if(debug_f1){ printf("\n CHIP HEADER: Chip = %d Chan = %d Time = %d \n", chip_head_chip, chip_head_chan, chip_head_time); } if(event_in_block == 0){ // Check Chip Time if(block_checked[slot] == 1){ if(abs(time_in_block[slot] - chip_head_time) > 2){ if( (time_in_block[slot] > 2) && (time_in_block[slot] < 500)){ f1_sync_slot[slot]++; if(debug_f1) printf("Compare time: Slot = %d Chip = % d Chan = %d Event in block = %d Expected time = %d Reported tme = %d \n", slot, chip_head_chip, chip_head_chan, event_in_block, time_in_block[slot], chip_head_time); // printf("\n F1TDC chip time mismatch: Expected chip time = %d Reported time = %d\n", time_in_block[slot], chip_head_time ); // daLogMsg("ERROR", "F1TDC chip time mismatch: Expected chip time = %d Reported time = %d \n ", time_in_block[slot], chip_head_time ); } } } } } // CHIP header if( (data >> 27) == F1_BLOCK_TRAIL){ if(block_head != 1){ printf(" FATAL: Missing global header \n "); } block_head = 0; } ptr++; } } // F1TDC bank #if 0 // FADC250 if(banktag[jj] == 6){ // Check trigger number and trigger time in first events in the block int fa250_block_head = 0; int fa250_slot = 0; int fa250_mod_id = 0; int fa250_block_numb = 0; int fa250_nevt_block = 0; int debug_fadc250 = 1; ptr = bankdata[jj]; ptrend = bankdata[jj] + banknw[jj] - 1; printf("ptr = 0x%x ptrend = 0x%x \n", ptr, ptrend); while(ptr < ptrend){ int event_in_block; uint32_t data = LSWAP(*ptr); if( (data >> 27) == FA250_BLOCK_HEAD){ if(fa250_block_head != 0){ printf(" FATAL: Missing global trailer \n "); } fa250_slot = (data & FA250_SLOT) >> 22; fa250_mod_id = (data & FA250_ID) >> 18; fa250_block_numb = (data & FA250_BLOCK) >> 8; fa250_nevt_block = (data & FA250_NEVT_BLOCK); if(debug_fadc250) printf("\n BLOCK HEADER: SLOT = %d ID = %d BLOCK NUMBER = %d NEVT_BLOCK = %d \n", fa250_slot, fa250_mod_id, fa250_block_numb, fa250_nevt_block); fa250_block_head = 1; } if( data >> 27 == FA250_EVENT_HEAD){ } if( (data >> 27) == FA250_BLOCK_TRAIL){ if(fa250_block_head != 1){ printf(" FATAL: Missing global header \n "); } fa250_block_head = 0; } ptr++; } } // FADC250 bank #endif } // Loop over banks #if 0 int bank, kk; for(bank = 0; bank < nbanks; bank++) { if(banktag[bank] != 26) continue; if( (my_event % 10) == 0 ){ printf(" \n Fraction of bad channels %d \n", my_event); for(ii = 3; ii < 14; ii++){ for(jj = 0; jj < 8; jj++){ for(kk = 0; kk < 8; kk++){ if(my_event > 0){ float ratio = (float) f1_sync[ii][jj][kk]/my_event; if(ratio > 0.001){ printf(" Slot = %d, Chip = %d Channel = %d Errors = %d Fraction = %f \n", ii, jj, kk, f1_sync[ii][jj][kk], ratio); } } } } } } } #endif int bank, kk; for(bank = 0; bank < nbanks; bank++) { if(banktag[bank] != 26) continue; if( (my_event % 10000) == 0 ){ printf(" \n Fraction of bad channels %d \n", my_event); for(ii = 3; ii < 14; ii++){ float ratio = (float) f1_sync_slot[ii]/10000.; printf(" Slot = %d, Errors = %d Fraction = %f Time in block = %d \n", ii, f1_sync_slot[ii], ratio, time_in_block[ii]); f1_sync_slot[ii] = 0; } } } {/* inline c-code */ gettimeofday(&tvA[1], NULL); if ((*HIST_FLAG)==0) FILL_EVENT=1; (*skip_event_counter)++; //--- event counter --- // int ic=0; #ifdef DQM_HIST printf("\n ROL2 copy: size=%d max=%d\n",EVENT_LENGTH+2,MAX_EVENT_LENGTH); #endif if (rol->dabufp != NULL) { /* Output Pointer should be set by CODA 2.1 ROC */ // printf(" EVENT_LENGTH = %d \n",EVENT_LENGTH);fflush(0); for (ii=-2;iidabufp++ = INPUT[ii]; // printf(" 0x%x ", INPUT[ii]); fflush(0); #ifdef SHM_BUF if ((FILL_EVENT==1) && (ii=LEventBuffer) { printf("ii=%d, EVENT LENGTH=%d .gt. %d (MAX_EV_ LEN=%d) \n",ii,EVENT_LENGTH,LEventBuffer,MAX_EVENT_LENGTH); } #endif } // printf("\n"); #ifdef SHM_BUF if (shmem_ptr) { //hf1(herr,1,0); //-- bin0 event counter trigger_decode(&INPUT[0]); // trigger_print(); if (trigger.Size<=EVENT_LENGTH+2) { for (it=0;it<8;it++) { if (!(trigger.ID & (1<EventLength=EVENT_LENGTH+2; shmem_ptr->EventBufferFLAG=1; //---- *buffer_size=EVENT_LENGTH+2; *HIST_FLAG=1; } } #endif #ifdef DQM_HIST if (*HIST_FLAG==0) { printf("=====================================================================================================\n"); printf("\n\n\n FADC DATA:ROL2: size=%d max=%d\n",EVENT_LENGTH+2,MAX_EVENT_LENGTH); //int ic; //for (ic=-2;ic<(((EVENT_LENGTH+2)>=20)?20:(EVENT_LENGTH+2));ic++) printf(" H%d=0x%08X \n",ic,INPUT[ic]); printf(" \n"); //fill_bank(HIST_buffer,EVENT_LENGTH+2); //fill_bank((INPUT+8),EVENT_LENGTH-8); } #endif }else{ printf("ROL2: ERROR rol->dabufp is NULL -- Event lost\n"); } gettimeofday(&tvB[1], NULL); int idiff=tvB[1].tv_usec - tvA[1].tv_usec + 1000000 * (tvB[1].tv_sec - tvA[1].tv_sec); hf1(hrol2,1,idiff); }/*end inline c-code */ } /* end user */ } /*end trigger */ void davetrig_done() { { /* begin user */ //printf(" ====== ROL_2 () davetrig_done() ====== \n"); } /* end user */ } /*end done */ void __done() { poolEmpty = 0; /* global Done, Buffers have been freed */ { /* begin user */ //printf(" ====== ROL_2 done() ====== \n"); } /* end user */ } /*end done */ static void __status() { { /* begin user */ printf(" ====== ROL_2 status() ====== \n"); } /* end user */ } /* end status */ static void __reset () { { /* begin user */ printf(" ====== ROL_2 reset () ====== \n"); #ifdef SHM_BUF *HIST_FLAG=4; #endif #ifdef DQM_HIST sleep(1); close_hist(); //--- fsv #endif } /* end user */ } /* end status */ static void shmem_get() { /*---------------------------------------------------------------------*/ /* Shared memory */ /*---------------------------------------------------------------------*/ if ( (semid=semget( SEM_ID1, 1, 0) ) < 0 ) { printf("shmem_ROL_2: can not get semaphore \n"); fflush(stdout); } if ( (shmid=shmget(SHM_ID1,sizeof(roc_shmem), 0))<0) { printf("==> shmem_ROL_2: shared memory 0x%x, size=%d get error=%d\n",SHM_ID1,sizeof(roc_shmem),shmid); fflush(stdout); exit(1); } if ( (shmem_ptr=(roc_shmem *) shmat (shmid, 0, 0))<0 ) { printf("==> shmem_ROL_2: shared memory attach error\n"); fflush(stdout); } printf("==> shmem_ROL_2: shared memory attached OK ptr=%p\n",shmem_ptr); fflush(stdout); printf("===== ROL_2 wait semaphore ====== \n"); fflush(stdout); //--- add new shmem client here ----------- //-- access to shmem -- //sem_wait(semid); if (shmem_ptr) shmem_ptr->rol2_hist=1; //sem_post(semid); }