diff --git a/.gitignore b/.gitignore index 427f7e4aa81c62a8f12eb895dba503ac9601d876..1e69f92bfa0862eeb16acb5d1cc7d7bdd063c61e 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ Pcie40Applications/pcie40_klmbytestream Pcie40Applications/pcie40_dma_check Pcie40Applications/pcie40_ulreset Pcie40Applications/pcie40_miniPod +Pcie40Applications/pcie40_send_ul *.d **/pcie40_reload.out diff --git a/Pcie40Applications/Makefile b/Pcie40Applications/Makefile index fbb1ce8086c7e05d494e5968fcdd854467fe81a3..c0ecff2f5f96d9c84f03653fb78be73b94fdb1e1 100644 --- a/Pcie40Applications/Makefile +++ b/Pcie40Applications/Makefile @@ -51,12 +51,27 @@ PCIE40_DAQ_CXXFLAGS =$(CFLAGS) -I$(TOP) -I$(TOP)/../Pcie40Driver PCIE40_DAQ_INSTALL =$(PREFIX)/bin PCIE40_DAQ_LDFLAGS = -L../Pcie40Libraries/lib -lpcie40 -L../Pcie40DriverLibraries/ -lpcie40driver_ecs +# +# Begin : Added by S. Yamada +# PCIE40_UL :=pcie40_ul PCIE40_UL_OBJS =main_pcie40_ul.o PCIE40_UL_CXXFLAGS =$(CFLAGS) -I$(TOP) -I$(TOP)/../Pcie40Driver PCIE40_UL_INSTALL =$(PREFIX)/bin PCIE40_UL_LDFLAGS = -L../Pcie40Libraries/lib -lpcie40 -L../Pcie40DriverLibraries/ -lpcie40driver_ecs +PCIE40_SEND_UL :=pcie40_send_ul +PCIE40_SEND_UL_OBJS =main_pcie40_send_ul.o +PCIE40_SEND_UL_CXXFLAGS =$(CFLAGS) -I$(TOP) -I$(TOP)/../Pcie40Driver +PCIE40_SEND_UL_INSTALL =$(PREFIX)/bin +PCIE40_SEND_UL_LDFLAGS = -L../Pcie40Libraries/lib -lpcie40 -L../Pcie40DriverLibraries/ -lpcie40driver_ecs + +PCIE40_UL_LIB :=pcie40_ul_lib +PCIE40_UL_LIB_OBJS =pcie40_ul_lib.o +PCIE40_UL_LIB_CXXFLAGS =$(CFLAGS) -I$(TOP) -I$(TOP)/../Pcie40Driver +PCIE40_UL_LIB_INSTALL =$(PREFIX)/bin +PCIE40_UL_LIB_LDFLAGS = -L../Pcie40Libraries/lib -lpcie40 -L../Pcie40DriverLibraries/ -lpcie40driver_ecs + PCIE40_DMA_CHECK :=pcie40_dma_check PCIE40_DMA_CHECK_OBJS =main_pcie40_dma_check.o PCIE40_DMA_CHECK_CXXFLAGS =$(CFLAGS) -I$(TOP) -I$(TOP)/../Pcie40Driver @@ -69,6 +84,10 @@ PCIE40_ULRESET_CXXFLAGS =$(CFLAGS) -I$(TOP) -I$(TOP)/../Pcie40Driver PCIE40_ULRESET_INSTALL =$(PREFIX)/bin PCIE40_ULRESET_LDFLAGS = -L../Pcie40Libraries/lib -lpcie40 -L../Pcie40DriverLibraries/ -lpcie40driver_ecs +# +# End : Added by S. Yamada +# + PCIE40_READFPGAVERSION :=pcie40_readFPGAversion PCIE40_READFPGAVERSION_OBJS =main_pcie40_readFPGAversion.o PCIE40_READFPGAVERSION_CXXFLAGS =$(CFLAGS) -I$(TOP) -I$(TOP)/../Pcie40Driver -I$(TOP)/../Pcie40DriverLibraries @@ -111,6 +130,8 @@ $(eval $(call ODIR_template,PCIE40_B2LRESET)) $(eval $(call ODIR_template,PCIE40_DMA)) $(eval $(call ODIR_template,PCIE40_DAQ)) $(eval $(call ODIR_template,PCIE40_UL)) +$(eval $(call ODIR_template,PCIE40_SEND_UL)) +#$(eval $(call ODIR_template,PCIE40_UL_LIB)) $(eval $(call ODIR_template,PCIE40_ULRESET)) $(eval $(call ODIR_template,PCIE40_DMA_CHECK)) $(eval $(call ODIR_template,PCIE40_READFPGAVERSION)) diff --git a/Pcie40Applications/main_pcie40_send_ul.cpp b/Pcie40Applications/main_pcie40_send_ul.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c68ed670879683be6eabe161c6d47b2caab3554 --- /dev/null +++ b/Pcie40Applications/main_pcie40_send_ul.cpp @@ -0,0 +1,629 @@ +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <time.h> +#include "altera_dma_regs.h" +#include <errno.h> +#include <string.h> + +// socket programming +#include <netdb.h> +#include <netinet/tcp.h> +#include <sys/mman.h> +#include <sys/socket.h> +#include <sys/uio.h> +#include <csignal> + + + +#define SENDBUF_WORDS 10000 +#define LOCAL_HOSTNAME "b3ropc02" +#define LISTEN_PORT 30001 + + +extern "C" int ecs_open(int dev, int bar); +extern "C" void ecs_close(int dev, int bar); +extern "C" int pcie40_b2dmabufferreset(int fd); +extern "C" int pcie40_b2dmapointerread(int fd , unsigned int ** pData ); +extern "C" int ecs_write(int dev, int bar, unsigned add, int val); + +//#define DEBUG + +const int CRC16_XMODEM_TABLE[] = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0, +} ; + + +int Accept() +{ + // + // Connect to cprtb01 + // + struct hostent* host; + host = gethostbyname( LOCAL_HOSTNAME ); + if (host == NULL) { + printf("[FATAL] hostname(%s) cannot be resolved(%s). Check /etc/hosts. Exiting...\n", + LOCAL_HOSTNAME, strerror(errno)); + fflush(stdout); + exit(1); + } + + // + // Bind and listen + // + int fd_listen; + struct sockaddr_in sock_listen; + sock_listen.sin_family = AF_INET; + sock_listen.sin_addr.s_addr = *(unsigned int*)host->h_addr_list[0]; + + socklen_t addrlen = sizeof(sock_listen); + sock_listen.sin_port = htons( LISTEN_PORT ); + fd_listen = socket(PF_INET, SOCK_STREAM, 0); + + int flags = 1; + int ret = setsockopt(fd_listen, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)sizeof(flags)); + if (ret < 0) { + perror("Failed to set REUSEADDR"); + } + + if (bind(fd_listen, (struct sockaddr*)&sock_listen, sizeof(struct sockaddr)) < 0) { + printf("[FATAL] Failed to bind. Maybe other programs have already occupied this port(%d). Exiting...\n", + LISTEN_PORT ); fflush(stdout); + // Check the process occupying the port 30000. + FILE* fp; + char buf[256]; + char cmdline[500]; + sprintf(cmdline, "/usr/sbin/ss -ap | grep %d", LISTEN_PORT); + if ((fp = popen(cmdline, "r")) == NULL) { + printf("[WARNING] Failed to run %s\n", cmdline); + } + while (fgets(buf, 256, fp) != NULL) { + printf("[ERROR] Failed to bind. output of ss(port %d) : %s\n", LISTEN_PORT, buf); fflush(stdout); + } + // Error message + fclose(fp); + printf("[FATAL] Failed to bind.(%s) Maybe other programs have already occupied this port(%d). Exiting...", + strerror(errno), LISTEN_PORT); + fflush(stdout); + exit(1); + } + + int val1 = 0; + setsockopt(fd_listen, IPPROTO_TCP, TCP_NODELAY, &val1, (socklen_t)sizeof(val1)); + int backlog = 1; + if (listen(fd_listen, backlog) < 0) { + char err_buf[500]; + printf("Failed in listen(%s). Exting...", strerror(errno)); + fflush(stdout); + exit(1); + } + + // + // Accept + // + int fd_accept; + struct sockaddr_in sock_accept; + printf("[DEBUG] Accepting... : port %d server %s\n", LISTEN_PORT, LOCAL_HOSTNAME ); + fflush(stdout); + + if ((fd_accept = accept(fd_listen, (struct sockaddr*) & (sock_accept), &addrlen)) == 0) { + char err_buf[500]; + printf( "[FATAL] Failed to accept(%s). Exiting...", strerror(errno)); + fflush(stdout); + exit(-1); + } else { + // B2INFO("Done."); + printf("[DEBUG] Done.\n"); fflush(stdout); + + // set timepout option + struct timeval timeout; + timeout.tv_sec = 1; + timeout.tv_usec = 0; + ret = setsockopt(fd_accept, SOL_SOCKET, SO_SNDTIMEO, &timeout, (socklen_t)sizeof(timeout)); + if (ret < 0) { + printf( "[FATAL] Failed to set TIMEOUT. Exiting..." ); + fflush(stdout); + exit(-1); + } + } + + close(fd_listen); + return fd_accept; + +} + + + +int crc_calc( int * crc, unsigned int * data ){ + int byte1, byte2, byte3, byte4 ; + byte1 = (*data) & 0xFF; + byte2 = ( (*data) & 0xFF00 ) >> 8; + byte3 = ( (*data) & 0xFF0000 ) >> 16; + byte4 = ( (*data) & 0xFF000000 ) >> 24; + *crc = ((((*crc))<<8)&0xff00) ^ CRC16_XMODEM_TABLE[((((*crc))>>8)&0xff)^byte4] ; + *crc = ((((*crc))<<8)&0xff00) ^ CRC16_XMODEM_TABLE[((((*crc))>>8)&0xff)^byte3] ; + *crc = ((((*crc))<<8)&0xff00) ^ CRC16_XMODEM_TABLE[((((*crc))>>8)&0xff)^byte2] ; + *crc = ((((*crc))<<8)&0xff00) ^ CRC16_XMODEM_TABLE[((((*crc))>>8)&0xff)^byte1] ; +} + + +int crc_calc_event( unsigned int* data, int start_pos ){ + int i = 0; + int cur_pos = start_pos; + int first_crc ; + int crc = 0xffff ; + int size = data[ cur_pos % DMASIZE ]; + + int good_flag = 0; + + if( size > 0xffff || size <= 0 ){ + printf("Invalid total event size %.8x\n", size); + return -1; + } + + /* printf( "TRL %.8x %.8x %.8x\n", */ + /* data[ ( cur_pos + size - 1 + DMASIZE ) % DMASIZE ], */ + /* data[ ( cur_pos + 1 + size ) % DMASIZE ], */ + /* data[ ( cur_pos + 2 + size ) % DMASIZE ] ); */ + + cur_pos = start_pos + 4; + unsigned int ctime = data[ cur_pos % DMASIZE ]; + + cur_pos = start_pos + 3; + unsigned int evenum = data[ cur_pos % DMASIZE ]; + cur_pos = start_pos + 5; + unsigned int utime = data[ cur_pos % DMASIZE ]; + cur_pos = start_pos + 2; + unsigned int exp_run = data[ cur_pos % DMASIZE ] ; + + int offset = 8; // size of ROB header [words] + + int link_num = 0; + + + crc_calc( &crc, &ctime ); + crc_calc( &crc, &evenum ); + crc_calc( &crc, &utime ); + crc_calc( &crc, &exp_run ); + first_crc = crc ; + + +#ifdef DEBUG + printf("Start\n"); + for( i = start_pos ; i < start_pos + size ; i++){ + printf("%.8x\n", data[ i % DMASIZE ] ); + } + printf("End\n"); +#endif + int bad = 0; + // printf("good flag %d\n", good_flag); fflush(stdout); + + while( offset + 4 < size ){ // 4 = ROB trailer size + cur_pos = start_pos + offset; + int size_link = data[ cur_pos % DMASIZE ] & 0x000ffff ; if ( 0 >= size_link || size_link > 10000000 ){ + printf("[ERROR] Invalid size per link (%.8x words): link num %d\n", size_link, link_num); + return -1 ; // it could be that the memory was read + } + + // + // Calculation of CRC + // + crc = first_crc ; +#ifdef DEBUG + printf("%.8x %.8x crc %.8x\n", data[ ( start_pos + offset ) % DMASIZE ], data[ ( start_pos + offset + 1 ) % DMASIZE ], crc ); +#endif + for ( cur_pos = start_pos + offset + 2 ; cur_pos < start_pos + offset + size_link -2; ++cur_pos ) { + crc_calc( &crc, &data[ cur_pos % DMASIZE ] ); +#ifdef DEBUG + printf("crc %.8x data %.8x\n", crc, data[ cur_pos % DMASIZE ] ); +#endif + } +#ifdef DEBUG + printf("%.8x %.8x\n", data[ ( cur_pos ) % DMASIZE ], data[ ( cur_pos + 1 ) % DMASIZE ]); +#endif + // cur_pos = start_pos + 8 + size_link -2; + cur_pos = start_pos + offset + size_link -2; +#ifdef DEBUG + printf("calcd %.4x data %.4x\n", crc, data[ cur_pos % DMASIZE ] & 0xffff ); +#endif + + // + // Check of CRC + // + if( ( crc & 0xffff ) != ( data[ cur_pos % DMASIZE ] & 0xffff ) ){ + printf("%.2d Bad ! calcd %.4x data %.4x offset %.4x size %.4x szlnk %.4x trl %.8x\n", + link_num, crc, data[ cur_pos % DMASIZE ] & 0xffff, offset, size, size_link, data[ (cur_pos+1) % DMASIZE ] ); + bad = 1; + + }else{ + good_flag = 1; + // printf("%.2d Good ! calcd %.4x data %.4x offset %.4x size %.4x szlnk %.4x trl %.8x\n", link_num, crc, data[ cur_pos % DMASIZE ] & 0xffff, offset, size, size_link, data[ (cur_pos+1) % DMASIZE ] ); + + + + } + link_num++; + offset += size_link; + } + + + if( bad ){ + // if( evenum > 50000 ){ + // if( 1 ){ + for( i = 0 ; i < size ; i++){ + printf("%.8x ", data[ ( start_pos + i ) % DMASIZE ] ); + if( i % 8 == 7 ) printf("\n"); + } + printf("\n"); + good_flag = -2; + } + return good_flag; + +} + + int decode_data_wUL( unsigned int * data , unsigned int * ce , int * crc , int * run , + int * bad , int *evn , int * retry , int * pctime , int * putime , + int * nmissed ) { + + int magic; + int size, size_link; + int ix ; + + int offset; + unsigned int exp_run, evenum, ctime, utime; + int start_pos = *ce; + int cnt = 0; + int cur_pos = *ce + 1; + int ret = 0; + + while( !( ( ( magic = data[ cur_pos % DMASIZE ] ) & 0xffff0000 ) != 0x7f7f0000 ) ){ + usleep(1000); + cnt++; + // + // Scan to find x"7f7f...." + // + if( cnt > 10 ){ + cnt = 0; + int j = 0 ; + for ( j = 0 ; j < DMASIZE ; ++j ) { + if ( ( ( data[ ( cur_pos + j ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ) ){ + evenum = data[ ( cur_pos + j + 2 ) % DMASIZE ]; + if( evenum > *evn ){ + *evn = evenum; + cur_pos = ( cur_pos + j - 1 + DMASIZE ) % DMASIZE; + size = data[ cur_pos ] ; + *ce = ( cur_pos + size ) % DMASIZE; + printf( "New Event number prev %d new %d pos %d size %d\n" , *evn , evenum, cur_pos, size ); + crc_calc_event( data, cur_pos ); + + }else{ + printf( "Old Event number prev %d new %d pos %d\n" , *evn , evenum, ( cur_pos + j - 1 + DMASIZE ) % DMASIZE ); + break; + } + }else{ + continue; + } + } + } + } + + return size ; + } + + int main (int argc ,char** argv) { + + + int send_buf[SENDBUF_WORDS]; + // Stop trigger + /* system( "ssh robbep@vme \"resetft -53\"" ) ; */ + /* sleep( 2 ) ; */ + + int res = ecs_open( 0 , 0 ) ; + if ( -1 == res ) printf("ERROR: Could not open device (BAR 0)\n") ; + else printf("SUCCESS: Device opened for DMA\n"); + res = ecs_open( 0 , 2 ) ; + // ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; + ecs_write( 0 , 2 , 0x000501A0 , 0x1 ) ; + pcie40_b2dmabufferreset( 0 ) ; + // Start trigger + // fp = popen( "ssh robbep@vme \"trigft -53 pulse 1000 -1\"" , "r" ) ; + // sleep( 2 ) ; + /* while( fgets( buf , sizeof(buf) , fp ) != NULL ) { */ + /* // printf( "A= %d\n" , strncmp( exp , buf , 3 ) ) ; */ + /* // printf( strncmp( &buf[0] , 'exp' , 1 ) ) ; */ + /* tmp = strdup( buf ) ; */ + /* if ( strncmp( exp , buf , 3 ) == 0 ) { */ + /* //printf( '%s\n' , buf ) ; */ + /* // printf( "ICI\n" ) ; */ + /* ptr = strtok( tmp , " " ) ; */ + /* ptr = strtok( NULL , " " ) ; */ + /* ptr = strtok( NULL , " " ) ; */ + /* ptr = strtok( NULL , " " ) ; */ + /* // while ( ptr != NULL ) { */ + /* run = atoi( ptr ) ; */ + /* // ptr = strtok( NULL , " " ) ; */ + /* } */ + /* //} */ + /* } */ + /* pclose( fp ) ; */ + //sleep( 2 ) ; + + int run = 0 ; + FILE* fp; + char * ptr ; + char * tmp ; + char buf[1000] ; + char exp[] = "exp" ; + + unsigned int * data ; + double time_spent ; + int retry ; + int bad ; + int evn ; + evn = 0 ; + bad = 0 ; + retry = 0 ; + int pctime, putime ; + pctime = 0 ; + putime = 0 ; + + int i ; + int size, size_256, currentEventIndex, crc, nErr, nEvt, nRetry, nMissed ; + currentEventIndex = 0 ; + nErr = 0 ; + nEvt = 0 ; + nRetry = 0 ; + nMissed = 0 ; + int read_dma = 1 ; + clock_t begin = clock() ; + int size_zero_cnt = 0; + + int start_flag = 1; + + int magic; + int size_link; + + int offset; + unsigned int next_exp_run, cur_exp_run = 0, evenum, ctime, utime, first_eve = 0; + int start_pos = 0; + int cnt = 0; + int eve_cnt = 0, first_event_flag = 0, crc_err_eve_cnt = 0; + + + int cur_pos = 0; + int ret = 0; + unsigned int cur_eve = 0, next_eve = 0; + int mod_pos = 0; + + int sent_bytes = 0; + int while_cnt = 0; + // if ( ( nEvt % 1000 == 1 ) && ( size != 0 ) ) printf( "Event number %d\n", nEvt ) ; + + + int m_socket_send = Accept(); + + // + // Read the 1st data + // + if ( read_dma == 1 ) { + // set busy + //res = ecs_write( 0 , 2 , 0x000501A0 , 0x1 ) ; + //sleep(5); + res = pcie40_b2dmapointerread( 0 , &data ) ; + if ( start_flag == 1 ){ + ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; + start_flag = 0; + } + //usleep( 5 ) ; + // release busy + //res = ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; + read_dma = 1 ; + } + + // + // Main loop + // + while( 1 ){ + while( 1 ){ + if( ( ( ( magic = data[ ( cur_pos + 1 ) % DMASIZE ] ) & 0xffff0000 ) == 0x7f7f0000 ) + && ( data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] == 0x7fff0006 ) + // && ( ( data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 4 + DMASIZE ) % DMASIZE ] & 0x0000ffff ) + // == ( data[ ( cur_pos + 3 ) % DMASIZE ] & 0x0000ffff ) ) + ){ + if( while_cnt % 1000 == 0 ){ + // printf("GOT %.8x %.8x %.8x %.8x\n", + // data[ ( cur_pos + 1 ) % DMASIZE ], + // data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ], + // data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 4 + DMASIZE ) % DMASIZE ], + // data[ ( cur_pos + 3 ) % DMASIZE ] + // ); + } + while_cnt++; + break; + }else{ + if( ( data[ ( cur_pos + 1 ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ){ + // printf("magic hdr %.8x trl %.8x eve hdr %.8x trl %8.x cnt %d\n", + // data[ ( cur_pos + 1 ) % DMASIZE ], + // data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ], + // data[ ( cur_pos + 3 ) % DMASIZE ], + // data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 4 + DMASIZE ) % DMASIZE ], + // eve_cnt + // ); + } + } + // printf("MAGIC %.8x\n", magic); + + // usleep(1000); // to avoid BUSY loop + + // + // Refresh DMA memory + // + res = pcie40_b2dmapointerread( 0 , &data ) ; + + + + int wait_flag = 0; + // + // Scan to find x"7f7f...." + // + if( cnt > 100 ){ + cnt = 0; + int j = 0 ; + + for ( j = 0 ; j < DMASIZE ; ++j ) { + +#ifdef DEBUG + printf("%.8x %d\n", data[ ( cur_pos + j + 1 ) % DMASIZE ], cur_pos + j ); +#endif + if ( ( ( data[ ( cur_pos + j + 1 ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ) ){ + next_eve = data[ ( cur_pos + j + 3 ) % DMASIZE ]; + next_exp_run = data[ ( cur_pos + j + 2 ) % DMASIZE ] ; + + // printf( "TRAILER %.8x %.8x %.8x\n",data[ ( cur_pos + j + data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] ); + + if( next_eve > cur_eve || next_exp_run > cur_exp_run ){ + /* if( data[ ( ( data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] ) - 1 + DMASIZE ) % DMASIZE ] != 0x7fff0006 ){ */ + /* usleep(1000); */ + /* break; */ + /* } */ + cur_pos = ( cur_pos + j + DMASIZE ) % DMASIZE; + size = data[ cur_pos ]; + // printf( "Detect new Event number prev %.8x new %.8x pos %.8x size %.8x cnt %d\n" , cur_eve , next_eve, cur_pos, size, eve_cnt ); + break; + }else{ + // printf( "Detect Old Event number prev %.8x new %.8x pos %d cnt %d\n" , cur_eve , next_eve, ( cur_pos + j + DMASIZE ) % DMASIZE, eve_cnt ); + break; + } + }else{ + continue; + } + } + } + // printf("Scanning... cnt=%d\n", cnt ); + cnt++; + } + size = data[ cur_pos ] ; + + next_eve = data[ ( cur_pos + 3 ) % DMASIZE ]; + next_exp_run = data[ ( cur_pos + 2 ) % DMASIZE ]; + + if( first_event_flag == 0 || cur_exp_run != next_exp_run ){ + first_event_flag = 1; + first_eve = next_eve; + crc_err_eve_cnt = 0; + eve_cnt = 0; + } + eve_cnt++; + ret = crc_calc_event( data, cur_pos ); + + if( ret != 1 ){ + crc_err_eve_cnt++; + } + + + // printf("Sending...\n"); fflush(stdout); + // + // Send data + // + int n = 0; + + //Fill iov info. + enum { + NUM_BUFFER = 1 + }; + struct iovec iov[ NUM_BUFFER ]; + iov[0].iov_len = sizeof(int) * size; + + + if( cur_pos + size > DMASIZE ){ + memcpy( send_buf, data + cur_pos, DMASIZE - cur_pos ); + memcpy( send_buf + ( DMASIZE - cur_pos ), data, size - ( DMASIZE - cur_pos ) ); + iov[0].iov_base = (char*)send_buf; + }else{ + iov[0].iov_base = (char*)(data + cur_pos); + } + + while (true) { + if ((n = writev( m_socket_send, iov, NUM_BUFFER)) < 0) { + if (errno == EINTR) { + continue; + } else if (errno == EAGAIN || errno == EWOULDBLOCK) { + continue; + } else { + printf("[WARNING] WRITEV error.(%s) : sent %d bytes, header %d bytes body %d trailer %d\n" , + strerror(errno), n, iov[0].iov_len, iov[1].iov_len, iov[2].iov_len); + fflush(stdout); + exit(1); + } + } + + break; + } + + sent_bytes += n; + + + + if(ret != 1 || eve_cnt % 1000 == 0 ){ + printf( "[INFO] Event number prev %.8x new %.8x pos %.8x size %.8x sent %.1lf[MB]\n" , cur_eve , next_eve, cur_pos, size , sent_bytes*1.e-6); + fflush(stdout); + } + cur_eve = next_eve; + cur_exp_run = next_exp_run; + if(ret != 1 || eve_cnt % 1000 == 0 ){ + printf("[INFO] STATUS : tot eve %d read eve %d err_eve %d run %d\n", next_eve - first_eve, eve_cnt, crc_err_eve_cnt, cur_exp_run ); + fflush(stdout); + } + mod_pos = ( cur_pos + size ) % 8; + if( mod_pos != 0 ){ + cur_pos = ( cur_pos + size + DMASIZE + ( 8 - mod_pos) ) % DMASIZE ; + }else{ + cur_pos = ( cur_pos + size + DMASIZE ) % DMASIZE ; + } + + } + + + clock_t end = clock(); + time_spent += (double)(end - begin) / CLOCKS_PER_SEC; + + ecs_close( 0 , 0 ) ; + + printf( "Timing = %f s\n" , time_spent ) ; + printf( "Number of events = %d \n", nEvt ) ; + printf( "Number of errors = %d \n", nErr ) ; + printf( "Number of retries = %d \n", nRetry ) ; + printf( "Number of missed = %d \n", nMissed ) ; + return 0 ; + } diff --git a/Pcie40Applications/main_pcie40_ul.cpp b/Pcie40Applications/main_pcie40_ul.cpp index 6e7f86a881f92eb0ec43c0ff510a1a76bd03d62f..c0a5014e7569b59d1b858fe8f63737c87fd27028 100644 --- a/Pcie40Applications/main_pcie40_ul.cpp +++ b/Pcie40Applications/main_pcie40_ul.cpp @@ -161,8 +161,6 @@ int crc_calc_event( unsigned int* data, int start_pos ){ } if( bad ){ - // if( evenum > 50000 ){ - // if( 1 ){ for( i = 0 ; i < size ; i++){ printf("%.8x ", data[ ( start_pos + i ) % DMASIZE ] ); if( i % 8 == 7 ) printf("\n"); @@ -174,54 +172,6 @@ int crc_calc_event( unsigned int* data, int start_pos ){ } - int decode_data_wUL( unsigned int * data , unsigned int * ce , int * crc , int * run , - int * bad , int *evn , int * retry , int * pctime , int * putime , - int * nmissed ) { - - int magic; - int size, size_link; - int ix ; - - int offset; - unsigned int exp_run, evenum, ctime, utime; - int start_pos = *ce; - int cnt = 0; - int cur_pos = *ce + 1; - int ret = 0; - - while( !( ( ( magic = data[ cur_pos % DMASIZE ] ) & 0xffff0000 ) != 0x7f7f0000 ) ){ - usleep(1000); - cnt++; - // - // Scan to find x"7f7f...." - // - if( cnt > 10 ){ - cnt = 0; - int j = 0 ; - for ( j = 0 ; j < DMASIZE ; ++j ) { - if ( ( ( data[ ( cur_pos + j ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ) ){ - evenum = data[ ( cur_pos + j + 2 ) % DMASIZE ]; - if( evenum > *evn ){ - *evn = evenum; - cur_pos = ( cur_pos + j - 1 + DMASIZE ) % DMASIZE; - size = data[ cur_pos ] ; - *ce = ( cur_pos + size ) % DMASIZE; - printf( "New Event number prev %d new %d pos %d size %d\n" , *evn , evenum, cur_pos, size ); - crc_calc_event( data, cur_pos ); - - }else{ - printf( "Old Event number prev %d new %d pos %d\n" , *evn , evenum, ( cur_pos + j - 1 + DMASIZE ) % DMASIZE ); - break; - } - }else{ - continue; - } - } - } - } - - return size ; - } int main (int argc ,char** argv) { // Stop trigger @@ -296,7 +246,7 @@ int crc_calc_event( unsigned int* data, int start_pos ){ int offset; unsigned int next_exp_run, cur_exp_run = 0, evenum, ctime, utime, first_eve = 0; int start_pos = 0; - int cnt = 0; + int cnt = 0, dma_cnt = 0; int eve_cnt = 0, first_event_flag = 0, crc_err_eve_cnt = 0; @@ -315,6 +265,7 @@ int crc_calc_event( unsigned int* data, int start_pos ){ //res = ecs_write( 0 , 2 , 0x000501A0 , 0x1 ) ; //sleep(5); res = pcie40_b2dmapointerread( 0 , &data ) ; + dma_cnt++; if ( start_flag == 1 ){ ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; start_flag = 0; @@ -330,19 +281,24 @@ int crc_calc_event( unsigned int* data, int start_pos ){ // while( 1 ){ while( 1 ){ + // if( ( data[ ( cur_pos + 1 ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ){ + // printf("HEAD %.8x TRAIL %.8x\n", data[ ( cur_pos + 1 ) % DMASIZE ], + // data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] ); + // } + // if( !( ( ( ( magic = data[ ( cur_pos + 1 ) % DMASIZE ] ) & 0xffff0000 ) != 0x7f7f0000 ) ) ){ if( !( ( ( ( magic = data[ ( cur_pos + 1 ) % DMASIZE ] ) & 0xffff0000 ) != 0x7f7f0000 ) - || data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] != 0x7fff0006 ) ){ + || data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] != 0x7fff0006 ) ){ break; } - // printf("MAGIC %.8x\n", magic); - // usleep(1000); // to avoid BUSY loop // // Refresh DMA memory // res = pcie40_b2dmapointerread( 0 , &data ) ; + dma_cnt++; int wait_flag = 0; + // // Scan to find x"7f7f...." // @@ -357,7 +313,11 @@ int crc_calc_event( unsigned int* data, int start_pos ){ next_eve = data[ ( cur_pos + j + 3 ) % DMASIZE ]; next_exp_run = data[ ( cur_pos + j + 2 ) % DMASIZE ] ; - // printf( "TRAILER %.8x %.8x %.8x\n",data[ ( cur_pos + j + data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] ); + if( dma_cnt % 1000 == 0 ){ + printf( "7f7f is found. TRAILER %.8x eve %.8x exprun %.8x pos %d dmacnt %d\n", + data[ ( cur_pos + j + data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ], + next_eve, next_exp_run, ( cur_pos + j + 1 ) % DMASIZE, dma_cnt ); + } if( next_eve > cur_eve && next_exp_run >= cur_exp_run ){ /* if( data[ ( ( data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] ) - 1 + DMASIZE ) % DMASIZE ] != 0x7fff0006 ){ */ @@ -366,10 +326,10 @@ int crc_calc_event( unsigned int* data, int start_pos ){ /* } */ cur_pos = ( cur_pos + j + DMASIZE ) % DMASIZE; size = data[ cur_pos ]; - // printf( "Detect new Event number prev %.8x new %.8x pos %.8x size %.8x\n" , cur_eve , next_eve, cur_pos, size ); + // printf( "Detect new Event number prev %.8x new %.8x pos %.8x size %.8x\n" , cur_eve , next_eve, cur_pos, size ); break; }else{ - // printf( "Detect Old Event number prev %.8x new %.8x pos %d\n" , cur_eve , next_eve, ( cur_pos + j + DMASIZE ) % DMASIZE ); + // printf( "Detect Old Event number prev %.8x new %.8x pos %d\n" , cur_eve , next_eve, ( cur_pos + j + DMASIZE ) % DMASIZE ); break; } }else{ @@ -380,18 +340,22 @@ int crc_calc_event( unsigned int* data, int start_pos ){ cnt++; } size = data[ cur_pos ] ; - + + // + // Check event-data + // next_eve = data[ ( cur_pos + 3 ) % DMASIZE ]; next_exp_run = data[ ( cur_pos + 2 ) % DMASIZE ]; if( first_event_flag == 0 || cur_exp_run != next_exp_run ){ first_event_flag = 1; first_eve = next_eve; - crc_err_eve_cnt = 0; - eve_cnt = 0; + // crc_err_eve_cnt = 0; + // eve_cnt = 0; } eve_cnt++; ret = crc_calc_event( data, cur_pos ); + // printf("crc ret %d cnt %d\n", eve_cnt); if( ret != 1 ){ crc_err_eve_cnt++; } @@ -401,7 +365,7 @@ int crc_calc_event( unsigned int* data, int start_pos ){ } cur_eve = next_eve; cur_exp_run = next_exp_run; - if(ret != 1 || eve_cnt % 10000 == 0 ){ + if(ret != 1 || eve_cnt % 100 == 0 ){ printf("STATUS : tot eve %d read eve %d err_eve %d run %d\n", next_eve - first_eve, eve_cnt, crc_err_eve_cnt, cur_exp_run ); } mod_pos = ( cur_pos + size ) % 8; diff --git a/Pcie40Applications/pcie40_ul_lib.cpp b/Pcie40Applications/pcie40_ul_lib.cpp new file mode 100644 index 0000000000000000000000000000000000000000..289d2f7bedf2c336f6be79d2e64c4e52b531147a --- /dev/null +++ b/Pcie40Applications/pcie40_ul_lib.cpp @@ -0,0 +1,380 @@ +#include "pcie40_ul_lib.h" + +int crc_calc( int * crc, unsigned int * data ){ + int byte1, byte2, byte3, byte4 ; + byte1 = (*data) & 0xFF; + byte2 = ( (*data) & 0xFF00 ) >> 8; + byte3 = ( (*data) & 0xFF0000 ) >> 16; + byte4 = ( (*data) & 0xFF000000 ) >> 24; + *crc = ((((*crc))<<8)&0xff00) ^ CRC16_XMODEM_TABLE[((((*crc))>>8)&0xff)^byte4] ; + *crc = ((((*crc))<<8)&0xff00) ^ CRC16_XMODEM_TABLE[((((*crc))>>8)&0xff)^byte3] ; + *crc = ((((*crc))<<8)&0xff00) ^ CRC16_XMODEM_TABLE[((((*crc))>>8)&0xff)^byte2] ; + *crc = ((((*crc))<<8)&0xff00) ^ CRC16_XMODEM_TABLE[((((*crc))>>8)&0xff)^byte1] ; +} + + +int crc_calc_event( unsigned int* data, int start_pos ){ + int i = 0; + int cur_pos = start_pos; + int first_crc ; + int crc = 0xffff ; + int size = data[ cur_pos % DMASIZE ]; + + int good_flag = 0; + + if( size > 0xffff || size <= 0 ){ + printf("Invalid total event size %.8x\n", size); + return -1; + } + + /* printf( "TRL %.8x %.8x %.8x\n", */ + /* data[ ( cur_pos + size - 1 + DMASIZE ) % DMASIZE ], */ + /* data[ ( cur_pos + 1 + size ) % DMASIZE ], */ + /* data[ ( cur_pos + 2 + size ) % DMASIZE ] ); */ + + cur_pos = start_pos + 4; + unsigned int ctime = data[ cur_pos % DMASIZE ]; + + cur_pos = start_pos + 3; + unsigned int evenum = data[ cur_pos % DMASIZE ]; + cur_pos = start_pos + 5; + unsigned int utime = data[ cur_pos % DMASIZE ]; + cur_pos = start_pos + 2; + unsigned int exp_run = data[ cur_pos % DMASIZE ] ; + + int offset = 8; // size of ROB header [words] + + int link_num = 0; + + + crc_calc( &crc, &ctime ); + crc_calc( &crc, &evenum ); + crc_calc( &crc, &utime ); + crc_calc( &crc, &exp_run ); + first_crc = crc ; + + +#ifdef DEBUG + printf("Start\n"); + for( i = start_pos ; i < start_pos + size ; i++){ + printf("%.8x\n", data[ i % DMASIZE ] ); + } + printf("End\n"); +#endif + int bad = 0; + + while( offset + 4 < size ){ // 4 = ROB trailer size + cur_pos = start_pos + offset; + int size_link = data[ cur_pos % DMASIZE ] & 0x000ffff ; + if ( 0 >= size_link || size_link > 10000000 ){ + printf("[ERROR] Invalid size per link (%.8x words): link num %d\n", size_link, link_num); + return -1 ; // it could be that the memory was read + } + + // + // Calculation of CRC + // + crc = first_crc ; +#ifdef DEBUG + printf("%.8x %.8x crc %.8x\n", data[ ( start_pos + offset ) % DMASIZE ], data[ ( start_pos + offset + 1 ) % DMASIZE ], crc ); +#endif + for ( cur_pos = start_pos + offset + 2 ; cur_pos < start_pos + offset + size_link -2; ++cur_pos ) { + crc_calc( &crc, &data[ cur_pos % DMASIZE ] ); +#ifdef DEBUG + printf("crc %.8x data %.8x\n", crc, data[ cur_pos % DMASIZE ] ); +#endif + } +#ifdef DEBUG + printf("%.8x %.8x\n", data[ ( cur_pos ) % DMASIZE ], data[ ( cur_pos + 1 ) % DMASIZE ]); +#endif + // cur_pos = start_pos + 8 + size_link -2; + cur_pos = start_pos + offset + size_link -2; +#ifdef DEBUG + printf("calcd %.4x data %.4x\n", crc, data[ cur_pos % DMASIZE ] & 0xffff ); +#endif + + // + // Check of CRC + // + if( ( crc & 0xffff ) != ( data[ cur_pos % DMASIZE ] & 0xffff ) ){ + printf("%.2d Bad ! calcd %.4x data %.4x offset %.4x size %.4x szlnk %.4x trl %.8x\n", + link_num, crc, data[ cur_pos % DMASIZE ] & 0xffff, offset, size, size_link, data[ (cur_pos+1) % DMASIZE ] ); + bad = 1; + + }else{ + good_flag = 1; + // printf("%.2d Good ! calcd %.4x data %.4x offset %.4x size %.4x szlnk %.4x trl %.8x\n", + // link_num, crc, data[ cur_pos % DMASIZE ] & 0xffff, offset, size, size_link, data[ (cur_pos+1) % DMASIZE ] ); + + } + link_num++; + offset += size_link; + } + + if( bad ){ + // if( evenum > 50000 ){ + // if( 1 ){ + for( i = 0 ; i < size ; i++){ + printf("%.8x ", data[ ( start_pos + i ) % DMASIZE ] ); + if( i % 8 == 7 ) printf("\n"); + } + printf("\n"); + good_flag = -2; + } + return good_flag; + +} + + int decode_data_wUL( unsigned int * data , unsigned int * ce , int * crc , int * run , + int * bad , int *evn , int * retry , int * pctime , int * putime , + int * nmissed ) { + + int magic; + int size, size_link; + int ix ; + + int offset; + unsigned int exp_run, evenum, ctime, utime; + int start_pos = *ce; + int cnt = 0; + int cur_pos = *ce + 1; + int ret = 0; + + while( !( ( ( magic = data[ cur_pos % DMASIZE ] ) & 0xffff0000 ) != 0x7f7f0000 ) ){ + usleep(1000); + cnt++; + // + // Scan to find x"7f7f...." + // + if( cnt > 10 ){ + cnt = 0; + int j = 0 ; + for ( j = 0 ; j < DMASIZE ; ++j ) { + if ( ( ( data[ ( cur_pos + j ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ) ){ + evenum = data[ ( cur_pos + j + 2 ) % DMASIZE ]; + if( evenum > *evn ){ + *evn = evenum; + cur_pos = ( cur_pos + j - 1 + DMASIZE ) % DMASIZE; + size = data[ cur_pos ] ; + *ce = ( cur_pos + size ) % DMASIZE; + printf( "New Event number prev %d new %d pos %d size %d\n" , *evn , evenum, cur_pos, size ); + crc_calc_event( data, cur_pos ); + + }else{ + printf( "Old Event number prev %d new %d pos %d\n" , *evn , evenum, ( cur_pos + j - 1 + DMASIZE ) % DMASIZE ); + break; + } + }else{ + continue; + } + } + } + } + + return size ; + } + +int readout(){ +// int main (int argc ,char** argv) { + // Stop trigger + /* system( "ssh robbep@vme \"resetft -53\"" ) ; */ + /* sleep( 2 ) ; */ + + int res = ecs_open( 0 , 0 ) ; + if ( -1 == res ) printf("ERROR: Could not open device (BAR 0)\n") ; + else printf("SUCCESS: Device opened for DMA\n"); + res = ecs_open( 0 , 2 ) ; + // ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; + ecs_write( 0 , 2 , 0x000501A0 , 0x1 ) ; + pcie40_b2dmabufferreset( 0 ) ; + // Start trigger + // fp = popen( "ssh robbep@vme \"trigft -53 pulse 1000 -1\"" , "r" ) ; + // sleep( 2 ) ; + /* while( fgets( buf , sizeof(buf) , fp ) != NULL ) { */ + /* // printf( "A= %d\n" , strncmp( exp , buf , 3 ) ) ; */ + /* // printf( strncmp( &buf[0] , 'exp' , 1 ) ) ; */ + /* tmp = strdup( buf ) ; */ + /* if ( strncmp( exp , buf , 3 ) == 0 ) { */ + /* //printf( '%s\n' , buf ) ; */ + /* // printf( "ICI\n" ) ; */ + /* ptr = strtok( tmp , " " ) ; */ + /* ptr = strtok( NULL , " " ) ; */ + /* ptr = strtok( NULL , " " ) ; */ + /* ptr = strtok( NULL , " " ) ; */ + /* // while ( ptr != NULL ) { */ + /* run = atoi( ptr ) ; */ + /* // ptr = strtok( NULL , " " ) ; */ + /* } */ + /* //} */ + /* } */ + /* pclose( fp ) ; */ + //sleep( 2 ) ; + + int run = 0 ; + FILE* fp; + char * ptr ; + char * tmp ; + char buf[1000] ; + char exp[] = "exp" ; + + unsigned int * data ; + double time_spent ; + int retry ; + int bad ; + int evn ; + evn = 0 ; + bad = 0 ; + retry = 0 ; + int pctime, putime ; + pctime = 0 ; + putime = 0 ; + + int i ; + int size, size_256, currentEventIndex, crc, nErr, nEvt, nRetry, nMissed ; + currentEventIndex = 0 ; + nErr = 0 ; + nEvt = 0 ; + nRetry = 0 ; + nMissed = 0 ; + int read_dma = 1 ; + clock_t begin = clock() ; + int size_zero_cnt = 0; + + int start_flag = 1; + + int magic; + int size_link; + + int offset; + unsigned int next_exp_run, cur_exp_run = 0, evenum, ctime, utime, first_eve = 0; + int start_pos = 0; + int cnt = 0; + int eve_cnt = 0, first_event_flag = 0, crc_err_eve_cnt = 0; + + + int cur_pos = 0; + int ret = 0; + unsigned int cur_eve = 0, next_eve = 0; + int mod_pos = 0; + + // if ( ( nEvt % 1000 == 1 ) && ( size != 0 ) ) printf( "Event number %d\n", nEvt ) ; + + // + // Read the 1st data + // + if ( read_dma == 1 ) { + // set busy + //res = ecs_write( 0 , 2 , 0x000501A0 , 0x1 ) ; + //sleep(5); + res = pcie40_b2dmapointerread( 0 , &data ) ; + if ( start_flag == 1 ){ + ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; + start_flag = 0; + } + //usleep( 5 ) ; + // release busy + //res = ecs_write( 0 , 2 , 0x000501A0 , 0x0 ) ; + read_dma = 1 ; + } + + // + // Main loop + // + while( 1 ){ + while( 1 ){ + if( !( ( ( ( magic = data[ ( cur_pos + 1 ) % DMASIZE ] ) & 0xffff0000 ) != 0x7f7f0000 ) + || data[ ( cur_pos + data[ cur_pos % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] != 0x7fff0006 ) ){ + break; + } + // printf("MAGIC %.8x\n", magic); + + // usleep(1000); // to avoid BUSY loop + + // + // Refresh DMA memory + // + res = pcie40_b2dmapointerread( 0 , &data ) ; + int wait_flag = 0; + // + // Scan to find x"7f7f...." + // + if( cnt > 100 ){ + cnt = 0; + int j = 0 ; + for ( j = 0 ; j < DMASIZE ; ++j ) { +#ifdef DEBUG + printf("%.8x %d\n", data[ ( cur_pos + j + 1 ) % DMASIZE ], cur_pos + j ); +#endif + if ( ( ( data[ ( cur_pos + j + 1 ) % DMASIZE ] & 0xffff0000 ) == 0x7f7f0000 ) ){ + next_eve = data[ ( cur_pos + j + 3 ) % DMASIZE ]; + next_exp_run = data[ ( cur_pos + j + 2 ) % DMASIZE ] ; + + // printf( "TRAILER %.8x %.8x %.8x\n",data[ ( cur_pos + j + data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] - 1 + DMASIZE ) % DMASIZE ] ); + + if( next_eve > cur_eve && next_exp_run >= cur_exp_run ){ + /* if( data[ ( ( data[ ( cur_pos + j + DMASIZE ) % DMASIZE ] ) - 1 + DMASIZE ) % DMASIZE ] != 0x7fff0006 ){ */ + /* usleep(1000); */ + /* break; */ + /* } */ + cur_pos = ( cur_pos + j + DMASIZE ) % DMASIZE; + size = data[ cur_pos ]; + // printf( "Detect new Event number prev %.8x new %.8x pos %.8x size %.8x\n" , cur_eve , next_eve, cur_pos, size ); + break; + }else{ + // printf( "Detect Old Event number prev %.8x new %.8x pos %d\n" , cur_eve , next_eve, ( cur_pos + j + DMASIZE ) % DMASIZE ); + break; + } + }else{ + continue; + } + } + } + cnt++; + } + size = data[ cur_pos ] ; + + next_eve = data[ ( cur_pos + 3 ) % DMASIZE ]; + next_exp_run = data[ ( cur_pos + 2 ) % DMASIZE ]; + + if( first_event_flag == 0 || cur_exp_run != next_exp_run ){ + first_event_flag = 1; + first_eve = next_eve; + crc_err_eve_cnt = 0; + eve_cnt = 0; + } + eve_cnt++; + ret = crc_calc_event( data, cur_pos ); + if( ret != 1 ){ + crc_err_eve_cnt++; + } + + if(ret != 1 || eve_cnt % 100 == 0 ){ + printf( "Event number prev %.8x new %.8x pos %.8x size %.8x\n" , cur_eve , next_eve, cur_pos, size ); + } + cur_eve = next_eve; + cur_exp_run = next_exp_run; + if(ret != 1 || eve_cnt % 10000 == 0 ){ + printf("STATUS : tot eve %d read eve %d err_eve %d run %d\n", next_eve - first_eve, eve_cnt, crc_err_eve_cnt, cur_exp_run ); + } + mod_pos = ( cur_pos + size ) % 8; + if( mod_pos != 0 ){ + cur_pos = ( cur_pos + size + DMASIZE + ( 8 - mod_pos) ) % DMASIZE ; + }else{ + cur_pos = ( cur_pos + size + DMASIZE ) % DMASIZE ; + } + + } + + + clock_t end = clock(); + time_spent += (double)(end - begin) / CLOCKS_PER_SEC; + + ecs_close( 0 , 0 ) ; + + printf( "Timing = %f s\n" , time_spent ) ; + printf( "Number of events = %d \n", nEvt ) ; + printf( "Number of errors = %d \n", nErr ) ; + printf( "Number of retries = %d \n", nRetry ) ; + printf( "Number of missed = %d \n", nMissed ) ; + return 0 ; + } diff --git a/Pcie40Applications/pcie40_ul_lib.h b/Pcie40Applications/pcie40_ul_lib.h new file mode 100644 index 0000000000000000000000000000000000000000..6f580bf8171d5bd42ddad2c7f00657df1860395b --- /dev/null +++ b/Pcie40Applications/pcie40_ul_lib.h @@ -0,0 +1,59 @@ +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <time.h> +#include "altera_dma_regs.h" + +extern "C" int ecs_open(int dev, int bar); +extern "C" void ecs_close(int dev, int bar); +extern "C" int pcie40_b2dmabufferreset(int fd); +extern "C" int pcie40_b2dmapointerread(int fd , unsigned int ** pData ); +extern "C" int ecs_write(int dev, int bar, unsigned add, int val); + +//#define DEBUG + +const int CRC16_XMODEM_TABLE[] = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0, +} ; + + + +int crc_calc( int * crc, unsigned int * data ); + +int crc_calc_event( unsigned int* data, int start_pos ); + +int decode_data_wUL( unsigned int * data , unsigned int * ce , int * crc , int * run , + int * bad , int *evn , int * retry , int * pctime , int * putime , + int * nmissed ); +int readout(); diff --git a/Pcie40Libraries/pcie40_b2slc.cpp b/Pcie40Libraries/pcie40_b2slc.cpp index a5f4603d0e578d718c623270371e2e54ac32b292..50793a175d3b666748304af488783fcb1832ca14 100644 --- a/Pcie40Libraries/pcie40_b2slc.cpp +++ b/Pcie40Libraries/pcie40_b2slc.cpp @@ -3,6 +3,7 @@ #include <unistd.h> #include <stdio.h> #include <vector> +#include <string.h> // ===== // CALL C functions @@ -42,13 +43,32 @@ int pcie40_resetWriteFifo( int dev , int ch ) { printf( "Timeout reset write\n" ) ; } - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD , 0 ) ; - if ( ret != 0 ) return -1 ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD , - 1 << ( SLC_WFIFO_RESET_BIT + ch ) ) ; - if ( ret != 0 ) return -1 ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD , 0 ) ; - if ( ret != 0 ) return -1 ; + + if ( ch < 24 ) { + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_0 , 0 ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_0 , + 1 << ( SLC_WFIFO_RESET_BIT + ch ) ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_0 , 0 ) ; + if ( ret != 0 ) return -1 ; + } else { + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_1 , 0 ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_1 , + 1 << ( SLC_WFIFO_RESET_BIT + ch - 24 ) ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_1 , 0 ) ; + if ( ret != 0 ) return -1 ; + } + + // Check that FIFO is really reset + wle = pcie40_writeFifoFillLevel( dev , ch ) ; + if ( 0 != wle ) { + printf( "Write FIFO not empty\n" ) ; + return -1 ; + } + return ret ; } @@ -67,46 +87,114 @@ int pcie40_resetReadFifo( int dev , int ch ) { printf( "Timeout reset read\n" ) ; } - ret = ecs_write( dev , SLC_BAR , SLC_RFIFO_RESET_ADD , 0 ) ; - if ( ret != 0 ) return -1 ; - ret = ecs_write( dev , SLC_BAR , SLC_RFIFO_RESET_ADD , - 1 << ( SLC_RFIFO_RESET_BIT + ch ) ) ; - if ( ret != 0 ) return -1 ; - ret = ecs_write( dev , SLC_BAR , SLC_RFIFO_RESET_ADD , 0 ) ; - if ( ret != 0 ) return -1 ; + if ( ch < 24 ) { + ret = ecs_write( dev , SLC_BAR , SLC_RFIFO_RESET_ADD_0 , 0 ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_RFIFO_RESET_ADD_0 , + 1 << ( SLC_RFIFO_RESET_BIT + ch ) ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_RFIFO_RESET_ADD_0 , 0 ) ; + if ( ret != 0 ) return -1 ; + } else { + ret = ecs_write( dev , SLC_BAR , SLC_RFIFO_RESET_ADD_1 , 0 ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_RFIFO_RESET_ADD_1 , + 1 << ( SLC_RFIFO_RESET_BIT + ch - 24 ) ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_RFIFO_RESET_ADD_1 , 0 ) ; + if ( ret != 0 ) return -1 ; + } + + // check that FIFO is empty + rle = pcie40_readFifoFillLevel( dev , ch ) ; + if ( 0 != rle ) { + printf( "Read FIFO not empty\n" ) ; + return -1 ; + } return ret ; } int pcie40_writeToFifo( int dev , int ch , std::vector< int > & data ) { std::vector< int >::iterator it ; unsigned ret = 0 ; - for ( it = data.begin() ; it != data.end() ; ++it ) { - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_ADD + ch * 0x20 , - (int) ( (*it) & 0xFFFFFFFF ) ) ; + unsigned fl = 0 ; + unsigned tries = 0 ; + while ( fl != data.size() ) { + for ( it = data.begin() ; it != data.end() ; ++it ) { + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_ADD + ch * 0x20 , + (int) ( (*it) & 0xFFFFFFFF ) ) ; + if ( ret != 0 ) return -1 ; + } + fl = pcie40_writeFifoFillLevel( dev , ch ) ; + if ( fl == data.size() ) return ret ; + + // Reset Write FIFO + if ( ch < 24 ) { + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_0 , 0 ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_0 , + 1 << ( SLC_WFIFO_RESET_BIT + ch ) ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_0 , 0 ) ; if ( ret != 0 ) return -1 ; + } else { + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_1 , 0 ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_1 , + 1 << ( SLC_WFIFO_RESET_BIT + ch - 24 ) ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD_1 , 0 ) ; + if ( ret != 0 ) return -1 ; + } + + tries++ ; + if ( tries == 10 ) { + printf( "Cannot write FIFO\n" ) ; + return -1 ; + } } return ret ; } -int pcie40_startEmit( int dev , int ch ) { +int pcie40_startEmit( int dev , int ch , int expected ) { unsigned ret = 0 ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD , 0 ) ; - if ( ret != 0 ) return -1 ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD , - 1 << ( SLC_WFIFO_EMIT_BIT + ch ) ) ; - if ( ret != 0 ) return -1 ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD , 0 ) ; - if ( ret != 0 ) return -1 ; + if ( expected != -1 ) { + // check that FIFO is written + ret = pcie40_writeFifoFillLevel( dev , ch ) ; + if ( expected != ret ) { + printf( "Write FIFO not written correctly\n" ) ; + return -1 ; + } + } + + if ( ch < 24 ) { + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD_0 , 0 ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD_0 , + 1 << ( SLC_WFIFO_EMIT_BIT + ch ) ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD_0 , 0 ) ; + if ( ret != 0 ) return -1 ; + } else { + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD_1 , 0 ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD_1 , + 1 << ( SLC_WFIFO_EMIT_BIT + ch - 24 ) ) ; + if ( ret != 0 ) return -1 ; + ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD_1 , 0 ) ; + if ( ret != 0 ) return -1 ; + } + return ret ; } int pcie40_waitRead( int dev , int ch , int length ) { unsigned ret = 0 ; int i ; - for ( i=0 ; i<10 ; i++ ) { + for ( i=0 ; i<100 ; i++ ) { ret = pcie40_readFifoFillLevel( dev , ch ) ; if ( ret == length ) break; - usleep( 10 ) ; //10 ms + usleep( 10 ) ; //10 us } if (i == 10) { @@ -119,10 +207,13 @@ int pcie40_waitRead( int dev , int ch , int length ) { int pcie40_readData( int dev , int ch , std::vector< int > & result , int length ) { unsigned ret = 0 ; int i = 0 ; + for ( i = 0 ; i < length ; ++i ) { ret = ecs_read( dev , SLC_BAR , SLC_RFIFO_ADD + ch * 0x20 ); result.push_back( ret ) ; } + ret = pcie40_readFifoFillLevel( dev , ch ) ; + if ( ret != 0 ) { printf( "Not all FIFO read\n" ) ; } return 0 ; } /* ---------------------------------------------------------------------- *\ @@ -153,7 +244,7 @@ int pcie40_readfee8( int dev , int ch , int adr) { if ( ret != 0 ) return ret ; // start emit - ret = pcie40_startEmit( dev , ch ) ; + ret = pcie40_startEmit( dev , ch , 5 ) ; if ( ret != 0 ) return -1 ; // Wait for the result to come back (40 words in the fifo) @@ -194,7 +285,7 @@ int pcie40_writefee8( int dev , int ch , int adr , int val ) { if ( ret != 0 ) return ret ; // start emit - ret = pcie40_startEmit( dev , ch ) ; + ret = pcie40_startEmit( dev , ch , 5 ) ; if ( ret != 0 ) return -1 ; return 0 ; @@ -226,7 +317,7 @@ unsigned long pcie40_readfee32( int dev , int ch , int adr ) { if ( ret != 0 ) return ret ; // start emit - ret = pcie40_startEmit( dev , ch ) ; + ret = pcie40_startEmit( dev , ch , 4 ) ; if ( ret != 0 ) return -1 ; // Wait for the result to come back @@ -325,7 +416,7 @@ int pcie40_writefee32( int dev , int ch , int adr , int val ) { if ( ret != 0 ) return ret ; // start emit - ret = pcie40_startEmit( dev , ch ) ; + ret = pcie40_startEmit( dev , ch , 8 ) ; if ( ret != 0 ) return -1 ; return ret; @@ -340,6 +431,16 @@ int pcie40_writestream_arich( int dev , int ch , char * filename ) { return pcie40_writestream_internal( dev , ch , filename , 1 ) ; } +int pcie40_writestream( int dev , int ch , char *filename , const char * detector ) { + if ( strcmp( detector , "KLM" ) == 0 ) { + return pcie40_writestream_klm( dev , ch , filename ) ; + } else if ( strcmp( detector , "ARICH" ) == 0 ) { + return pcie40_writestream_arich( dev , ch , filename ) ; + } + printf( "ERROR : detector not supported\n" ) ; + return -1 ; +} + int pcie40_writestream_internal( int dev , int ch , char * filename , int do_sleep ) { // PCIe40 // Check if the file exists @@ -378,7 +479,7 @@ int pcie40_writestream_internal( int dev , int ch , char * filename , int do_sle fclose( fp ) ; return -1 ; } - ret = pcie40_startEmit( dev , ch ) ; + ret = pcie40_startEmit( dev , ch , -1 ) ; if ( ret != 0 ) { fclose( fp ) ; return -1 ; @@ -408,7 +509,7 @@ int pcie40_writestream_internal( int dev , int ch , char * filename , int do_sle if ( ret != 0 ) return -1 ; // start emit - ret = pcie40_startEmit( dev , ch ) ; + ret = pcie40_startEmit( dev , ch , -1 ) ; if ( ret != 0 ) return -1 ; return ret ; @@ -416,49 +517,51 @@ int pcie40_writestream_internal( int dev , int ch , char * filename , int do_sle int pcie40_writebytestream( int dev, int ch, int len, const char *bytes ) { - - printf( "Desactivated for the moment \n") ; - return -1 ; - - // Reset the FIFO unsigned ret = 0 ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD , - 1 << ( SLC_WFIFO_RESET_BIT ) ) ; - if ( ret != 0 ) return -1 ; - - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_RESET_ADD , 0 ) ; - if ( ret != 0 ) return -1 ; + + // Read the file + int c = 0 ; + int i = 0 ; + int count = 0 ; + std::vector< int > data ; + while ( len != i ) { + if ( count == 0 ) // Stream write : insert FFFD + data.push_back( 0xFFFD ) ; + count++ ; + c = bytes[ i ] ; + i++ ; + data.push_back( (int) ( ( ( 0x70 << 8 ) | ( c & 0xFF ) ) & 0xFFFFFFFF ) ) ; - // Fill the FIFO with the requested information: write MSB first - // This is a stream write -> FFFD - int data_word_1 = 0xFFFD ; + if ( 6 == count ) { + data.push_back( 0xEEEE ) ; + // Reset the FIFO + ret = pcie40_resetWriteFifo( dev , ch ) ; + + if ( ret != 0 ) return -1 ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_ADD + ch * 0x20 , - (int) ( data_word_1 & 0xFFFFFFFF ) ) ; - if ( ret != 0 ) return -1 ; + ret = pcie40_writeToFifo( dev , ch , data ) ; + if ( ret != 0 ) return -1 ; + ret = pcie40_startEmit( dev , ch , -1 ) ; + if ( ret != 0 ) return -1 ; - // Read the file - int i ; - for ( i = 0 ; i < len ; ++i ) { - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_ADD + ch * 0x20 , - (int) ( ( ( 0x70 << 8 ) | ( bytes[i] & 0xFF ) ) & - 0xFFFFFFFF ) ) ; - if ( ret != 0 ) return -1 ; + count = 0 ; + data.clear() ; + } } - // End of the stream -> 0xEEEE - int data_word_3 = 0xEEEE ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_ADD + ch * 0x20 , - (int) ( data_word_3 & 0xFFFFFFFF ) ) ; - if ( ret != 0 ) return -1 ; + // End of the file -> 0xEEEE + data.push_back( 0xEEEE ) ; - // start emit - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD , 0 ) ; + // Reset the FIFO + ret = pcie40_resetWriteFifo( dev , ch ) ; + if ( ret != 0 ) return -1 ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD , - 1 << ( SLC_WFIFO_EMIT_BIT + ch ) ) ; + + ret = pcie40_writeToFifo( dev , ch , data ) ; if ( ret != 0 ) return -1 ; - ret = ecs_write( dev , SLC_BAR , SLC_WFIFO_START_ADD , 0 ) ; + + // start emit + ret = pcie40_startEmit( dev , ch , -1 ) ; if ( ret != 0 ) return -1 ; return ret ; diff --git a/Pcie40Libraries/pcie40_b2slc.h b/Pcie40Libraries/pcie40_b2slc.h index 877d3906b0812de3c290e8c0a830eeb27afd863f..8194bdaee6a8d38be5c0dd7349c0998a6a0c65aa 100644 --- a/Pcie40Libraries/pcie40_b2slc.h +++ b/Pcie40Libraries/pcie40_b2slc.h @@ -6,11 +6,14 @@ // BAR number for Slow control interface #define SLC_BAR 2 // Address of the register to reset the write FIFO and bit to use -#define SLC_WFIFO_RESET_ADD 0x00050560 +#define SLC_WFIFO_RESET_ADD_0 0x00050560 +#define SLC_WFIFO_RESET_ADD_1 0x00050580 #define SLC_WFIFO_RESET_BIT 0 -#define SLC_RFIFO_RESET_ADD 0x000505a0 +#define SLC_RFIFO_RESET_ADD_0 0x000505a0 +#define SLC_RFIFO_RESET_ADD_1 0x000505c0 #define SLC_RFIFO_RESET_BIT 0 -#define SLC_WFIFO_START_ADD 0x00050480 +#define SLC_WFIFO_START_ADD_0 0x00050480 +#define SLC_WFIFO_START_ADD_1 0x000504a0 #define SLC_WFIFO_EMIT_BIT 0 #define SLC_WFIFO_ADD 0x00060000 #define SLC_RFIFO_STATUS 0x00060010 @@ -30,6 +33,7 @@ int pcie40_writefee32(int dev, int ch, int adr, int val); int pcie40_writestream_klm(int dev, int ch, char *filename); int pcie40_writestream_arich(int dev, int ch, char *filename); +int pcie40_writestream( int dev , int ch , char *filename , const char * detector ) ; int pcie40_writebytestream(int dev, int ch, int len, const char *bytes); diff --git a/Python/components/fpga_comp.py b/Python/components/fpga_comp.py index a2ff5e5daf384619093e29fee52ee50c669a681a..2aee8e6f2b3cf5ed630efa3c92d1ef4e72379e62 100644 --- a/Python/components/fpga_comp.py +++ b/Python/components/fpga_comp.py @@ -588,34 +588,47 @@ class Arria10(object): return ( val & 0x1 ) >> 0 def rxReadyIndividual(self, i): - status, val = mem.read( self.dev , 0x00050160 ) - return ( val & ( 0x1 << i ) ) >> i + add = 0x00050160 + if ( i > 23 ): add = 0x00050180 + status, val = mem.read( self.dev , add ) + return ( val & ( 0x1 << (i%24) ) ) >> (i%24) def validChannel(self,i): - status, val = mem.read( self.dev , 0x00050520 ) - return ( val & ( 0x1 << i ) ) >> i + add = 0x00050520 + if ( i > 23 ): add = 0x00050540 + status, val = mem.read( self.dev , add ) + return ( val & ( 0x1 << (i%24) ) ) >> (i%24) def txReady(self): status, val = mem.read( self.dev , 0x00050000 ) return ( val & 0x2 ) >> 1 def txReadyIndividual(self, i): - status, val = mem.read( self.dev , 0x000501A0 ) - return ( val & ( 0x1 << i ) ) >> ( i ) + add = 0x000501a0 + if ( i > 23 ) : add = 0x000501C0 + status, val = mem.read( self.dev , add ) + return ( val & ( 0x1 << (i%24) ) ) >> ( i%24 ) def b2linkReady(self): status, val = mem.read( self.dev , 0x00050000 ) return ( val & 0x4 ) >> 2 def b2linkReadyIndividual(self, i): - status, val1 = mem.read( self.dev , 0x000500E0 ) - status, val2 = mem.read( self.dev , 0x00050120 ) - return ( ( ( val1 & ( 0x1 << i ) ) >> i ) & ( ( val2 & ( 0x1 << i ) ) >> i ) ) + add1 = 0x000500E0 + add2 = 0x00050120 + if ( i > 23 ): + add1 = 0x00050100 + add2 = 0x00050140 + status, val1 = mem.read( self.dev , add1 ) + status, val2 = mem.read( self.dev , add2 ) + return ( ( ( val1 & ( 0x1 << (i%24) ) ) >> (i%24) ) & ( ( val2 & ( 0x1 << (i%24) ) ) >> (i%24) ) ) def resynchronizeLink(self, i ): - mem.write( self.dev , 0x00050240 , 0x0 ) - mem.write( self.dev , 0x00050240 , 0x1 << i ) - mem.write( self.dev , 0x00050240 , 0x0 ) + add = 0x00050440 + if ( i > 23 ) : add = 0x00050460 + mem.write( self.dev , add , 0x0 ) + mem.write( self.dev , add , 0x1 << (i%24) ) + mem.write( self.dev , add , 0x0 ) def resetTriggerCounter(self): mem.write( self.dev , 0x00050100 , 0x0 ) diff --git a/ROPCApplications/daq/dataobjects/SConscript b/ROPCApplications/daq/dataobjects/SConscript new file mode 100644 index 0000000000000000000000000000000000000000..8add187a02b76b64493e5ca86560cc30dd60390a --- /dev/null +++ b/ROPCApplications/daq/dataobjects/SConscript @@ -0,0 +1,5 @@ +Import('env') + +env['LIBS'] = ['$ROOT_LIBS', 'rawdata_dataobjects'] + +Return('env') diff --git a/ROPCApplications/daq/dataobjects/include/SendHeader.h b/ROPCApplications/daq/dataobjects/include/SendHeader.h new file mode 100644 index 0000000000000000000000000000000000000000..3124e69f5e18d92d0c4bc72a38aa691908115335 --- /dev/null +++ b/ROPCApplications/daq/dataobjects/include/SendHeader.h @@ -0,0 +1,93 @@ +//+ +// File : SendHeader.h +// Description : Module to handle SendHeader attached by COPPER DAQ +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef SENDHEADER_H +#define SENDHEADER_H + +#include "rawdata/dataobjects/RawHeader_latest.h" + +namespace Belle2 { + + // class SendHeader : public TObject { + class SendHeader { + public: + + //! Default constructor + SendHeader(); + + //! Constructor using existing pointer to raw data buffer + SendHeader(int*); + //! Destructor + ~SendHeader(); + + //! Get Header contents + int* GetBuffer(void); + + //! set buffer + void SetBuffer(int* hdr); + + void Initialize(); //! initialize Header + + void SetNwords(int total_data_nwords); //! set contents of Header + + void SetNumEventsinPacket(int num_events); + void SetNumNodesinPacket(int num_nodes); + void SetEventNumber(int eve_num); + void SetNodeID(int node_id); + + void SetExpRunWord(unsigned int exp_run); + + + void SetRunNum(int run_num); //! + void SetSubRunNum(int subrun_num); //! + void SetExpNum(int exp_num); //! + + int GetTotalNwords(); //! get contents of Header + int GetHdrNwords(); //! get contents of Header + int GetNumEventsinPacket(); + int GetNumNodesinPacket(); + int GetEventNumber(); + int GetNodeID(); + + int GetRunNum(); //! + int GetSubRunNum(); //! + int GetExpNum(); //! + int GetRunNumSubRunNum(); //! + + RawHeader_latest tmp_header; //! + + /* Data Format : Node info */ + enum { + SENDHDR_NWORDS = 6 + }; + + private: + + /* Data Format : Fixed length part*/ + enum { + POS_NWORDS = 0, + POS_HDR_NWORDS = 1, + POS_NUM_EVE_NUM_NODES = 2, + POS_EXP_RUN_NUM = 3, + POS_EVE_NUM = 4, + POS_NODE_ID = 5 + }; + + + + int m_buffer[ SENDHDR_NWORDS ]; + + // ClassDef(SendHeader, 1); + }; + + + + +} + +#endif diff --git a/ROPCApplications/daq/dataobjects/include/SendTrailer.h b/ROPCApplications/daq/dataobjects/include/SendTrailer.h new file mode 100644 index 0000000000000000000000000000000000000000..ec08df6416165255be321a0fa6e4e204beb8d954 --- /dev/null +++ b/ROPCApplications/daq/dataobjects/include/SendTrailer.h @@ -0,0 +1,60 @@ +//+ +// File : SendTrailer.h +// Description : Module to handle SendTrailer attached by COPPER DAQ +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef SENDTRAILER_H +#define SENDTRAILER_H + +namespace Belle2 { + + // class SendTrailer{ + // class SendTrailer : public TObject { + class SendTrailer { + public: + //! Default constructor + SendTrailer(); + + //! Constructor using existing pointer to raw data buffer + SendTrailer(int*); + + //! Destructor + ~SendTrailer(); + + int* GetBuffer(); //! return buffer + + void SetBuffer(int* bufin); //! set buffer + + void Initialize(); //! initialize header + + void SetChksum(int chksum); //! initialize header + + void SetMagicWord(); //! + + int GetTrlNwords(); //! + + int GetMagicWord(); //! + + enum { + SENDTRL_NWORDS = 2 + }; + private: + + enum { + POS_CHKSUM = 0, + POS_TERM_WORD = 1 + }; + + enum { + MAGIC_WORD_SEND_TRAILER = 0x7fff0007 + }; + + int m_buffer[ SENDTRL_NWORDS ]; + // ClassDef(SendTrailer, 1); + }; +} + +#endif diff --git a/ROPCApplications/daq/dataobjects/include/SndHdrTemp.h b/ROPCApplications/daq/dataobjects/include/SndHdrTemp.h new file mode 100644 index 0000000000000000000000000000000000000000..154a9fd8568099be157f709d3f30a292ee7fc042 --- /dev/null +++ b/ROPCApplications/daq/dataobjects/include/SndHdrTemp.h @@ -0,0 +1,95 @@ +//+ +// File : SndHdrTemp.h +// Description : Module to handle SndHdrTemp attached by COPPER DAQ +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef SNDHDRTEMP_H +#define SNDHDRTEMP_H + +#include <TObject.h> + +#include "rawdata/dataobjects/RawHeader_latest.h" + +namespace Belle2 { + + class SndHdrTemp : public TObject { + // class SndHdrTemp { + public: + + //! Default constructor + SndHdrTemp(); + + //! Constructor using existing pointer to raw data buffer + SndHdrTemp(int*); + //! Destructor + ~SndHdrTemp(); + + //! Get Header contents + int* GetBuffer(void); + + //! set buffer + void SetBuffer(int* hdr); + + void Initialize(); //! initialize Header + + void SetNwords(int total_data_nwords); //! set contents of Header + + void SetNumEventsinPacket(int num_events); + void SetNumNodesinPacket(int num_nodes); + void SetEventNumber(int eve_num); + void SetNodeID(int node_id); + + void SetExpRunWord(unsigned int exp_run); + + + void SetRunNum(int run_num); //! + void SetSubRunNum(int subrun_num); //! + void SetExpNum(int exp_num); //! + + int GetTotalNwords(); //! get contents of Header + int GetHdrNwords(); //! get contents of Header + int GetNumEventsinPacket(); + int GetNumNodesinPacket(); + int GetEventNumber(); + int GetNodeID(); + + int GetRunNum(); //! + int GetSubRunNum(); //! + int GetExpNum(); //! + int GetRunNumSubRunNum(); //! + + RawHeader_latest tmp_header; //! + + /* Data Format : Node info */ + enum { + SENDHDR_NWORDS = 6 + }; + + private: + + /* Data Format : Fixed length part*/ + enum { + POS_NWORDS = 0, + POS_HDR_NWORDS = 1, + POS_NUM_EVE_NUM_NODES = 2, + POS_EXP_RUN_NUM = 3, + POS_EVE_NUM = 4, + POS_NODE_ID = 5 + }; + + + + int m_buffer[ SENDHDR_NWORDS ]; + + ClassDef(SndHdrTemp, 1); + }; + + + + +} + +#endif diff --git a/ROPCApplications/daq/dataobjects/include/linkdef.h b/ROPCApplications/daq/dataobjects/include/linkdef.h new file mode 100644 index 0000000000000000000000000000000000000000..cbd658a3d46c2df2a7a29257e8a6dd0fbadeaf7a --- /dev/null +++ b/ROPCApplications/daq/dataobjects/include/linkdef.h @@ -0,0 +1,11 @@ +#ifdef __CINT__ + +#pragma link off all globals; +#pragma link off all classes; +#pragma link off all functions; +#pragma link C++ nestedclasses; + +#pragma link C++ class Belle2::SendHeader+; +#pragma link C++ class Belle2::SendTrailer+; +#pragma link C++ class Belle2::SndHdrTemp+; +#endif diff --git a/ROPCApplications/daq/dataobjects/src/SendHeader.cc b/ROPCApplications/daq/dataobjects/src/SendHeader.cc new file mode 100644 index 0000000000000000000000000000000000000000..263328270aded199584c7e1329b983aa360f0b1a --- /dev/null +++ b/ROPCApplications/daq/dataobjects/src/SendHeader.cc @@ -0,0 +1,161 @@ +//+ +// File : SendHeader.cc +// Description : Module to handle SendHeader attached by COPPER DAQ +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include "daq/dataobjects/SendHeader.h" + +using namespace std; +using namespace Belle2; + +//ClassImp(SendHeader); + +SendHeader::SendHeader() +{ + Initialize(); + // cout << "SendHeader NULL constructor" << endl; +} + +SendHeader::SendHeader(int* buffer) +{ + // cout << "SendHeader constructor with buffer" << endl; + memcpy(m_buffer, buffer, SENDHDR_NWORDS * sizeof(int)); +} + +SendHeader::~SendHeader() +{ +} + +int* SendHeader::GetBuffer() +{ + return m_buffer; +} + +void SendHeader::SetBuffer(int* bufin) +{ + memcpy(m_buffer, bufin, SENDHDR_NWORDS * sizeof(int)); +} + +void SendHeader::Initialize() +{ + memset(m_buffer, 0, sizeof(int)* SENDHDR_NWORDS); + m_buffer[ POS_HDR_NWORDS ] = SENDHDR_NWORDS; +} + + +// Set Values + +void SendHeader::SetNwords(int total_data_nwords) +{ + m_buffer[ POS_NWORDS ] = total_data_nwords; +} + +void SendHeader::SetExpRunWord(unsigned int exp_run) +{ + m_buffer[ POS_EXP_RUN_NUM ] = exp_run; +} + +void SendHeader::SetNumEventsinPacket(int num_events) +{ + m_buffer[ POS_NUM_EVE_NUM_NODES ] = + (m_buffer[ POS_NUM_EVE_NUM_NODES ] & 0x0000FFFF) | + ((num_events << 16) & 0xFFFF0000); +} + +void SendHeader::SetNumNodesinPacket(int num_nodes) +{ + m_buffer[ POS_NUM_EVE_NUM_NODES ] = + (m_buffer[ POS_NUM_EVE_NUM_NODES ] & 0xFFFF0000) | + (num_nodes & 0x0000FFFF); +} + + +void SendHeader::SetEventNumber(int eve_num) +{ + m_buffer[ POS_EVE_NUM ] = eve_num; +} + + +void SendHeader::SetNodeID(int node_id) +{ + m_buffer[ POS_NODE_ID ] = node_id; +} + + +void SendHeader::SetRunNum(int run_num) +{ + + unsigned int inv_mask = ~((unsigned int)(tmp_header.RUNNO_MASK)); + m_buffer[ POS_EXP_RUN_NUM ] = + ((unsigned int)m_buffer[ POS_EXP_RUN_NUM ] & inv_mask) | + (((unsigned int)run_num << tmp_header.RUNNO_SHIFT) & tmp_header.RUNNO_MASK); + + return; +} + +void SendHeader::SetSubRunNum(int subrun_num) +{ + + unsigned int inv_mask = ~((unsigned int)(tmp_header.SUBRUNNO_MASK)); + m_buffer[ POS_EXP_RUN_NUM ] = + ((unsigned int)m_buffer[ POS_EXP_RUN_NUM ] & inv_mask) | ((unsigned int)subrun_num & tmp_header.SUBRUNNO_MASK); + return; +} + +void SendHeader::SetExpNum(int exp_num) +{ + + unsigned int inv_mask = ~((unsigned int)(tmp_header.EXP_MASK)); + m_buffer[ POS_EXP_RUN_NUM ] = + ((unsigned int)m_buffer[ POS_EXP_RUN_NUM ] & inv_mask) | + (((unsigned int)exp_num << tmp_header.EXP_SHIFT) & tmp_header.EXP_MASK); + + return; +} + + + +// Get Values + +int SendHeader::GetTotalNwords() { return m_buffer[ POS_NWORDS ];} +int SendHeader::GetHdrNwords() { return SENDHDR_NWORDS; } +int SendHeader::GetNumEventsinPacket() +{ + return + ((m_buffer[ POS_NUM_EVE_NUM_NODES ] & 0xFFFF0000) >> 16) & 0x0000FFFF; + +} +int SendHeader::GetNumNodesinPacket() { return m_buffer[ POS_NUM_EVE_NUM_NODES ] & 0x0000FFFF; } + +int SendHeader::GetEventNumber() { return m_buffer[ POS_EVE_NUM ]; } + +int SendHeader::GetNodeID() { return m_buffer[ POS_NODE_ID ]; } + +int SendHeader::GetRunNum() +{ + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NUM ]) & tmp_header.RUNNO_MASK) >> tmp_header.RUNNO_SHIFT); +} + +int SendHeader::GetSubRunNum() +{ + return ((unsigned int)(m_buffer[ POS_EXP_RUN_NUM ]) & tmp_header.SUBRUNNO_MASK); +} + +int SendHeader::GetRunNumSubRunNum() +{ + + return ((unsigned int)(m_buffer[ POS_EXP_RUN_NUM ]) & + (tmp_header.RUNNO_MASK | tmp_header.SUBRUNNO_MASK)); +} + + +int SendHeader::GetExpNum() +{ + + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NUM ]) & tmp_header.EXP_MASK) >> tmp_header.EXP_SHIFT); +} + + diff --git a/ROPCApplications/daq/dataobjects/src/SendTrailer.cc b/ROPCApplications/daq/dataobjects/src/SendTrailer.cc new file mode 100644 index 0000000000000000000000000000000000000000..50ff95919d8ed1b28673bcf0d8804cc713c78a20 --- /dev/null +++ b/ROPCApplications/daq/dataobjects/src/SendTrailer.cc @@ -0,0 +1,68 @@ +//+ +// File : SendTrailer.h +// Description : Module to handle SendTrailer attached by COPPER DAQ +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include "daq/dataobjects/SendTrailer.h" + +#include <cstring> + +using namespace std; +using namespace Belle2; + +//ClassImp(SendTrailer); + +SendTrailer::SendTrailer() +{ + Initialize(); + // cout << "SendTrailer NULL constructor" << endl; +} + +SendTrailer::SendTrailer(int* buffer) +{ + memcpy(m_buffer, buffer, SENDTRL_NWORDS * sizeof(int)); +} + +SendTrailer::~SendTrailer() +{ +} + +int* SendTrailer::GetBuffer() +{ + return m_buffer; +} + +void SendTrailer::SetBuffer(int* bufin) +{ + memcpy(m_buffer, bufin, SENDTRL_NWORDS * sizeof(int)); +} + +void SendTrailer::Initialize() +{ + memset(m_buffer, 0, SENDTRL_NWORDS * sizeof(int)); + SetMagicWord(); +} + + +void SendTrailer::SetChksum(int chksum) +{ + m_buffer[ POS_CHKSUM ] = chksum; +} + +void SendTrailer::SetMagicWord() +{ + m_buffer[ POS_TERM_WORD ] = MAGIC_WORD_SEND_TRAILER; +} + +int SendTrailer::GetMagicWord() +{ + return m_buffer[ POS_TERM_WORD ]; +} + +int SendTrailer::GetTrlNwords() +{ + return SENDTRL_NWORDS; +} diff --git a/ROPCApplications/daq/dataobjects/src/SndHdrTemp.cc b/ROPCApplications/daq/dataobjects/src/SndHdrTemp.cc new file mode 100644 index 0000000000000000000000000000000000000000..549f52489d4e2ac27279b564539f876eb559e0eb --- /dev/null +++ b/ROPCApplications/daq/dataobjects/src/SndHdrTemp.cc @@ -0,0 +1,161 @@ +//+ +// File : SendHeader.cc +// Description : Module to handle SendHeader attached by COPPER DAQ +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include "daq/dataobjects/SndHdrTemp.h" + +using namespace std; +using namespace Belle2; + +ClassImp(SndHdrTemp); + +SndHdrTemp::SndHdrTemp() +{ + Initialize(); + // cout << "SndHdrTemp NULL constructor" << endl; +} + +SndHdrTemp::SndHdrTemp(int* buffer) +{ + // cout << "SndHdrTemp constructor with buffer" << endl; + memcpy(m_buffer, buffer, SENDHDR_NWORDS * sizeof(int)); +} + +SndHdrTemp::~SndHdrTemp() +{ +} + +int* SndHdrTemp::GetBuffer() +{ + return m_buffer; +} + +void SndHdrTemp::SetBuffer(int* bufin) +{ + memcpy(m_buffer, bufin, SENDHDR_NWORDS * sizeof(int)); +} + +void SndHdrTemp::Initialize() +{ + memset(m_buffer, 0, sizeof(int)* SENDHDR_NWORDS); + m_buffer[ POS_HDR_NWORDS ] = SENDHDR_NWORDS; +} + + +// Set Values + +void SndHdrTemp::SetNwords(int total_data_nwords) +{ + m_buffer[ POS_NWORDS ] = total_data_nwords; +} + +void SndHdrTemp::SetExpRunWord(unsigned int exp_run) +{ + m_buffer[ POS_EXP_RUN_NUM ] = exp_run; +} + +void SndHdrTemp::SetNumEventsinPacket(int num_events) +{ + m_buffer[ POS_NUM_EVE_NUM_NODES ] = + (m_buffer[ POS_NUM_EVE_NUM_NODES ] & 0x0000FFFF) | + ((num_events << 16) & 0xFFFF0000); +} + +void SndHdrTemp::SetNumNodesinPacket(int num_nodes) +{ + m_buffer[ POS_NUM_EVE_NUM_NODES ] = + (m_buffer[ POS_NUM_EVE_NUM_NODES ] & 0xFFFF0000) | + (num_nodes & 0x0000FFFF); +} + + +void SndHdrTemp::SetEventNumber(int eve_num) +{ + m_buffer[ POS_EVE_NUM ] = eve_num; +} + + +void SndHdrTemp::SetNodeID(int node_id) +{ + m_buffer[ POS_NODE_ID ] = node_id; +} + + +void SndHdrTemp::SetRunNum(int run_num) +{ + + unsigned int inv_mask = ~((unsigned int)(tmp_header.RUNNO_MASK)); + m_buffer[ POS_EXP_RUN_NUM ] = + ((unsigned int)m_buffer[ POS_EXP_RUN_NUM ] & inv_mask) | + (((unsigned int)run_num << tmp_header.RUNNO_SHIFT) & tmp_header.RUNNO_MASK); + + return; +} + +void SndHdrTemp::SetSubRunNum(int subrun_num) +{ + + unsigned int inv_mask = ~((unsigned int)(tmp_header.SUBRUNNO_MASK)); + m_buffer[ POS_EXP_RUN_NUM ] = + ((unsigned int)m_buffer[ POS_EXP_RUN_NUM ] & inv_mask) | ((unsigned int)subrun_num & tmp_header.SUBRUNNO_MASK); + return; +} + +void SndHdrTemp::SetExpNum(int exp_num) +{ + + unsigned int inv_mask = ~((unsigned int)(tmp_header.EXP_MASK)); + m_buffer[ POS_EXP_RUN_NUM ] = + ((unsigned int)m_buffer[ POS_EXP_RUN_NUM ] & inv_mask) | + (((unsigned int)exp_num << tmp_header.EXP_SHIFT) & tmp_header.EXP_MASK); + + return; +} + + + +// Get Values + +int SndHdrTemp::GetTotalNwords() { return m_buffer[ POS_NWORDS ];} +int SndHdrTemp::GetHdrNwords() { return SENDHDR_NWORDS; } +int SndHdrTemp::GetNumEventsinPacket() +{ + return + ((m_buffer[ POS_NUM_EVE_NUM_NODES ] & 0xFFFF0000) >> 16) & 0x0000FFFF; + +} +int SndHdrTemp::GetNumNodesinPacket() { return m_buffer[ POS_NUM_EVE_NUM_NODES ] & 0x0000FFFF; } + +int SndHdrTemp::GetEventNumber() { return m_buffer[ POS_EVE_NUM ]; } + +int SndHdrTemp::GetNodeID() { return m_buffer[ POS_NODE_ID ]; } + +int SndHdrTemp::GetRunNum() +{ + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NUM ]) & tmp_header.RUNNO_MASK) >> tmp_header.RUNNO_SHIFT); +} + +int SndHdrTemp::GetSubRunNum() +{ + return ((unsigned int)(m_buffer[ POS_EXP_RUN_NUM ]) & tmp_header.SUBRUNNO_MASK); +} + +int SndHdrTemp::GetRunNumSubRunNum() +{ + + return ((unsigned int)(m_buffer[ POS_EXP_RUN_NUM ]) & + (tmp_header.RUNNO_MASK | tmp_header.SUBRUNNO_MASK)); +} + + +int SndHdrTemp::GetExpNum() +{ + + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NUM ]) & tmp_header.EXP_MASK) >> tmp_header.EXP_SHIFT); +} + + diff --git a/ROPCApplications/daq/rawdata/include/CprErrorMessage.h b/ROPCApplications/daq/rawdata/include/CprErrorMessage.h new file mode 100644 index 0000000000000000000000000000000000000000..03431ba9d0da547300a803e657670ae17832fa87 --- /dev/null +++ b/ROPCApplications/daq/rawdata/include/CprErrorMessage.h @@ -0,0 +1,49 @@ +//+ +// File : CprErrorMessage.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 29 - Dec - 2013 +//- + +#ifndef CPRERRORMESSAGE_H +#define CPRERRORMESSAGE_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <string> +#include <daq/slc/readout/RunInfoBuffer.h> + +namespace Belle2 { + + class CprErrorMessage { + public: + //! Default constructor + CprErrorMessage(); + + //! Default constructor + // CprErrorMessage( const char* entry_name ); + + + //! Destructor + virtual ~CprErrorMessage(); + + //! + void PrintError(char* err_message, const char* file, const char* func_name, const int line); + + //! + void PrintError(const int shmflag, RunInfoBuffer* nsm_status, char* err_message, const char* file, const char* func_name, + const int line); + + //! + void PrintError(const int shmflag, RunInfoBuffer* nsm_status, std::string err_str); + + //! + void PrintError(const char* err_message); + + + }; +} + +#endif diff --git a/ROPCApplications/daq/rawdata/include/DesSer.h b/ROPCApplications/daq/rawdata/include/DesSer.h new file mode 100644 index 0000000000000000000000000000000000000000..2d1b65d15042bb6dd6f1cd4199cad5397e85eda3 --- /dev/null +++ b/ROPCApplications/daq/rawdata/include/DesSer.h @@ -0,0 +1,324 @@ +//+ +// File : DesSer.h +// Description : Module to receive data from outside and store it to DataStore +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 4 - Sep - 2014 +//- + +#ifndef DESSER_H +#define DESSER_H + +#include <string> +#include <vector> + +#include <sys/time.h> +#include <errno.h> + +#include <daq/slc/readout/RunInfoBuffer.h> +#include <daq/rawdata/CprErrorMessage.h> +#include <daq/rawdata/modules/DAQConsts.h> +#include <rawdata/dataobjects/PreRawCOPPERFormat_latest.h> + +#include <daq/dataobjects/SendHeader.h> +#include <daq/dataobjects/SendTrailer.h> + +#define BUF_SIZE_WORD 4800 + +//////////////////////////////////////////// +// From Serializer.cc +//////////////////////////////////////////// + + +namespace Belle2 { + + /*! A class definition of an input module for Sequential ROOT I/O */ + + class DesSer { + + // Public functions + public: + + //! Constructor / Destructor + DesSer(); + + // DesSer(); + virtual ~DesSer(); + + void initialize(bool close_listen = true); + + //! Module functions to be called from event process + virtual void DataAcquisition() = 0; + + //! dump error data + void printData(int* buf, int nwords); + + //! # of events in a run + int max_nevt; + + //! time to stop a run + double max_seconds; + + //! Compression Level + int m_compressionLevel; + + //! No. of sent events + int n_basf2evt; + + //! No. of prev sent events + int m_prev_nevt; + + //! dump filename + std::string m_dump_fname; + + //! dump file descripter + FILE* m_fp_dump; + + //! buffer + int* m_buffer; + + //! buffer + int* m_bufary[ NUM_PREALLOC_BUF ]; + // int* m_bufary[NUM_EVT_PER_BASF2LOOP]; + + //! Getbuffer + int* getPreAllocBuf(); + + //! Getbuffer + int* getNewBuffer(int nwords, int* delete_flag); + + // For monitoring + timeval m_t0; + double m_recvd_totbytes; + double m_recvd_prev_totbytes; + double m_sent_totbytes; + double m_sent_prev_totbytes; + int m_ncycle; + //! for time monitoring + double cur_time; + + double m_start_time; + double m_prev_time; + int prev_event; + + //! store time info. + double getTimeSec(); + + //! store time info. + void recordTime(int event, double* array); + + //! calculate checksum + unsigned int calcSimpleChecksum(int* buf, int nwords); + + //! calculate checksum + unsigned int calcXORChecksum(int* buf, int nwords); + + + void clearNumUsedBuf() + { + m_num_usedbuf = 0; + return ; + } + + //! monitor rate + void RateMonitor(unsigned int nevt); + + + //! # of already used buffers + int m_num_usedbuf; + + //! Node Name for SLC + std::string m_nodename; + + //! Node ID for SLC + int m_nodeid; + + //! Run info buffer + RunInfoBuffer m_status; + + //! run no. + unsigned int m_exprunsubrun_no; + + //! run no. + unsigned int m_prev_exprunsubrun_no; + + //! exp no. + int m_exp_no; + + //! data type + int m_data_type; + + //! trunc mask + int m_trunc_mask; + + //! Use shared memory + int m_shmflag; + + //! open shared memory + void shmOpen(char* path_cfg, char* path_sta); + + //! Get shared memory + int* shmGet(int fd, int size_words); + + //! file descripter for shm + int m_shmfd_cfg; + + //! file descripter for shm + int m_shmfd_sta; + + //! buffer for shared memory + //int* m_cfg_buf; + //! buffer for shared memory + //int* m_cfg_sta; + + int monitor_numeve; + + //! start flag + int m_start_flag; + + //! wrapper for B2LOG system + CprErrorMessage print_err; + + //! report status to SLC + // static RunInfoBuffer g_status; + // RunInfoBuffer g_status; + + PreRawCOPPERFormat_latest m_pre_rawcpr; + + /////////////////////////////// + // From Serializer.cc + /////////////////////////////// + + //! Module functions to be called from main process + void initializeNode(); + + //! Module functions to be called from event process + void beginRun(); + + void endRun(); + void terminate(); + + // int sendByWriteV(RawDataBlock* rawdblk); + int sendByWriteV(RawDataBlockFormat* rawdblk); + // void Connect(const char* hostname, const int port); + void Accept(bool close_listen = true); + + // void fillSendHeaderTrailer(SendHeader* hdr, SendTrailer* trl, RawDataBlock* rawdblk); + void fillSendHeaderTrailer(SendHeader* hdr, SendTrailer* trl, RawDataBlockFormat* rawdblk); + + // Data members + + //! send buffer + int Send(int socket, char* buf, int size_bytes); + + + //! buffer for shared memory + int* m_cfg_buf; + + //! buffer for shared memory + int* m_cfg_sta; + + //! flag to show that run-controller pauses a run + int m_run_pause; + + //! flag to show that there is some errors with which DAQ cannot continue. + int m_run_error; + + protected : + //! check data contents + // StoreArray<RawDataBlock> raw_datablkarray; + + //! # of connections + int m_num_connections; + + //! Reciever basf2 Socket + // std::vector<EvtSocketRecv*> m_recv; + + //! hostname of upstream Data Sources + std::vector<std::string> m_hostname_from; + + //! port # to connect data sources + std::vector<int> m_port_from; + + std::vector<int> m_socket_recv; + + // StoreArray<RawFTSW> raw_ftswarray; + + // StoreArray<RawCOPPER> rawcprarray; + + // StoreArray<ReducedRawCOPPER> rawcprarray; + + int event_diff; + + unsigned int m_prev_copper_ctr; + + unsigned int m_prev_evenum; + + /////////////////////////////// + // From Serializer.cc + /////////////////////////////// + + //! Reciever Socket + int m_socket_send; + + enum { + COPPER = 1, + ROPC = 2 + }; + + //! Destination Host + std::string m_hostname_local; + + //! Destination port + int m_port_to; + + //! EvtSocket + // EvtSocketSend* m_sock; + + //! How to handle data + std::string p_method; + int p_method_val; + +#ifdef NONSTOP + //! for nonstop DAQ + int* m_ptr; + + //! run no. + int m_run_no; + + //! run no. + int m_prev_run_no; + + //! for nonstop DAQ + int checkRunPause(); + + //! for nonstop DAQ + int checkRunRecovery(); + + //! for nonstop DAQ + void openRunPauseNshm(); + + //! for nonstop DAQ + void resumeRun(); + + //! for nonstop DAQ + void pauseRun(); + + //! for nonstop DAQ + void waitResume(); + + //! call checkrunstop + void callCheckRunPause(std::string& err_str); + + //! Read data/check a connection + int CheckConnection(int socket); +#endif + + //! which format is used + RawHeader_latest tmp_header; + + }; + +} // end namespace Belle2 + +#endif // DESSERCOPPER_H diff --git a/ROPCApplications/daq/rawdata/include/DesSerCOPPER.h b/ROPCApplications/daq/rawdata/include/DesSerCOPPER.h new file mode 100644 index 0000000000000000000000000000000000000000..e3d205bf5f6a336171a501dec70c970180c2ed27 --- /dev/null +++ b/ROPCApplications/daq/rawdata/include/DesSerCOPPER.h @@ -0,0 +1,73 @@ +//+ +// File : DesSerCOPPER.h +// Description : Module to receive data from outside and store it to DataStore +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 4 - Sep - 2014 +//- + +#ifndef DESSERCOPPER_H +#define DESSERCOPPER_H + +#include <daq/rawdata/DesSer.h> + + +//////////////////////////////////////////// +// From Serializer.cc +//////////////////////////////////////////// + + +namespace Belle2 { + + /*! A class definition of an input module for Sequential ROOT I/O */ + + class DesSerCOPPER : public DesSer { + + // Public functions + public: + + //! Constructor / Destructor + DesSerCOPPER(std::string host_recv, int port_recv, std::string host_send, int port_send, int shmflag, + const std::string& nodename, int nodeid, int finesse_bitflag); + // DesSerCOPPER(); + virtual ~DesSerCOPPER(); + + //! Module functions to be called from event process + void DataAcquisition(); + + //! Read one event from COPPER + int* readOneEventFromCOPPERFIFO(const int entry, int* delete_flag, int* m_size_word); + + //! open COPPER + void openCOPPER(); + + //! Module functions to be called from main process + void initializeCOPPER(); + + //! receive data + int readFD(int fd, char* buf, int data_size_byte, int delete_flag); + + //! file descripter for COPPER + int m_cpr_fd; + + //! bit flag of FINESSE slots to use + int m_use_slot; + + //! bit flag of FINESSE slots to use + int m_finesse_bit_flag; + + protected : + + +#ifdef NONSTOP + //! for nonstop DAQ + void waitResume(); + + //! for nonstop DAQ + void resumeRun(); +#endif + }; + +} // end namespace Belle2 + +#endif // DESSERCOPPER_H diff --git a/ROPCApplications/daq/rawdata/include/DesSerPrePC.h b/ROPCApplications/daq/rawdata/include/DesSerPrePC.h new file mode 100644 index 0000000000000000000000000000000000000000..bd3742701373da6b0be4040d1947824e73fbaee0 --- /dev/null +++ b/ROPCApplications/daq/rawdata/include/DesSerPrePC.h @@ -0,0 +1,62 @@ +//+ +// File : DeSerializerPrePC.h +// Description : Module to receive data from outside and store it to DataStore +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 4 - Sep - 2014 +//- + +#ifndef DESSERPREPC_H +#define DESSERPREPC_H +#include <daq/rawdata/DesSer.h> + +//////////////////////////////////////////// +// From Serializer.cc +//////////////////////////////////////////// + + +namespace Belle2 { + + /*! A class definition of an input module for Sequential ROOT I/O */ + + class DesSerPrePC : public DesSer { + + // Public functions + public: + + //! Constructor / Destructor + DesSerPrePC(std::string host_recv, int port_recv, std::string host_send, int port_send, int shmflag, + const std::string& nodename, int nodeid); + // DesSerPrePC(); + virtual ~DesSerPrePC(); + + //! Module functions to be called from event process + void DataAcquisition(); + + protected : + //! Accept connection + int Connect(); + + //! receive data + int recvFD(int fd, char* buf, int data_size_byte, int flag); + + //! receive data + int* recvData(int* delete_flag, int* total_m_size_word, int* num_events_in_sendblock, int* num_nodes_in_sendblock); + + //! attach buffer to RawDataBlock + void setRecvdBuffer(RawDataBlockFormat* raw_datablk, int* delete_flag); + + //! check data contents + void checkData(RawDataBlockFormat* raw_datablk, unsigned int* eve_copper_0); + +#ifdef NONSTOP + + //! for nonstop DAQ + void waitResume(); +#endif + + }; + +} // end namespace Belle2 + +#endif // MODULEHELLO_H diff --git a/ROPCApplications/daq/rawdata/include/des_ser_prePC_main.h b/ROPCApplications/daq/rawdata/include/des_ser_prePC_main.h new file mode 100644 index 0000000000000000000000000000000000000000..77fda6db80ad8bfe07c74e12570dad7f15fb5c86 --- /dev/null +++ b/ROPCApplications/daq/rawdata/include/des_ser_prePC_main.h @@ -0,0 +1,106 @@ +//+ +// File : des_ser_prePC_main.h +// Description : Base class for Module to receive data from outside and store it to DataStore +// +// Author : Satoru Yamada Itoh, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef DES_SER_PREPC_MAIN_H +#define DES_SER_PREPC_MAIN_H + +#include <string> +#include <vector> +#include <stdlib.h> +/* #include <sys/time.h> */ + +/* #include <framework/core/Module.h> */ +/* #include <framework/pcore/EvtMessage.h> */ +/* #include <framework/pcore/MsgHandler.h> */ +/* #include <framework/datastore/DataStore.h> */ +/* #include <framework/datastore/StoreObjPtr.h> */ +/* #include <framework/datastore/StoreArray.h> */ +/* #include <framework/dataobjects/EventMetaData.h> */ + +/* #include <daq/dataflow/EvtSocket.h> */ +/* #include <daq/slc/readout/RunInfoBuffer.h> */ +/* #include <daq/rawdata/CprErrorMessage.h> */ +/* #include <daq/rawdata/modules/DAQConsts.h> */ + + +/* #include <rawdata/dataobjects/RawDataBlock.h> */ +//#include <rawdata/dataobjects/RawCOPPER.h> + +/* #include <sys/socket.h> */ +/* #include <sys/types.h> */ +/* #include <arpa/inet.h> */ +/* #include <netinet/in.h> */ +/* #include <netinet/tcp.h> */ + +/* #include <sys/mman.h> */ +/* #include <sys/stat.h> */ +/* #include <fcntl.h> */ + +/* using namespace std; */ +/* using namespace Belle2; */ + +/* namespace Belle2 { */ + +/* /\*! A class definition of an input module for Sequential ROOT I/O *\/ */ + +/* class des_ser_prePC_main{ */ + +/* // Public functions */ +/* public: */ + +/* //! Constructor / Destructor */ +/* des_ser_prePC_mainModule(); */ +/* virtual ~des_ser_prePC_mainModule(); */ + +/* //! Module functions to be called from main process */ +/* virtual void initialize(); */ + +/* //! Module functions to be called from main process */ +/* virtual void event(); */ + +/* protected : */ +/* //! hostname */ +/* string m_host_recv; */ + +/* //! hostname */ +/* string m_host_send; */ + +/* //! port number */ +/* int m_port_recv; */ + +/* //! port number */ +/* int m_port_send; */ + +/* //! Messaage handler */ +/* MsgHandler* m_msghandler; */ + +/* //! Compression Level */ +/* int m_compressionLevel; */ + +/* //! Node(PC or COPPER) ID */ +/* int m_nodeid; */ + +/* //! Node name */ +/* std::string m_nodename; */ + +/* //! Use shared memory -> 1; Without shm -> 0 */ +/* int m_shmflag; */ + +/* // Event Meta Data */ +/* StoreObjPtr<EventMetaData> m_eventMetaDataPtr; */ + +/* private: */ + + +/* public: */ + +/* }; */ + +/* } // end namespace Belle2 */ + +#endif // MODULEHELLO_H diff --git a/ROPCApplications/daq/rawdata/modules/include/DAQConsts.h b/ROPCApplications/daq/rawdata/modules/include/DAQConsts.h new file mode 100644 index 0000000000000000000000000000000000000000..4dc20119470d19b73ddc77fc65dc018dfbd4af23 --- /dev/null +++ b/ROPCApplications/daq/rawdata/modules/include/DAQConsts.h @@ -0,0 +1,138 @@ +//+ +// File : DAQConsts.h +// Description : Header file for constants common to DAQ modules +// +// Author : Satoru Yamada Itoh, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef DAQCONSTS_H +#define DAQCONSTS_H + +#define REDUCED_RAWCOPPER + +//#define NONSTOP +//#define NONSTOP_DEBUG +//#define NONSTOP_SLC + +//#define RAWDATASIZE 1 +/* #define WORD_SENDHEADER 1 */ +/* #define WORD_SENDTRAILER 1 */ +/* #define WORD_RAWCPRHEADER 16 */ + + +#define BASE_PORT_ROPC_COPPER 30000 +#define NUM_EVT_PER_BASF2LOOP_COPPER 50 +#define NUM_EVT_PER_BASF2LOOP_PC 1 +#define NUM_PREALLOC_BUF 500 + +// global variables for non-stop DAQ + + + +#ifdef NONSTOP +extern int g_run_pause; +extern int g_run_error; +extern int g_run_resuming; +#endif + +const int sp8test_run_starttime[93] = { + -1, 1381029053 + , 1381029116 + , 1381029212 + , 1381029294 + , 1381029461 + , 1381029515 + , 1381029585 + , 1381029634 + , 1381029779 + , 1381029884 + , 1381029967 + , 1381030359 + , 1381031134 + , 1381031221 + , 1381031325 + , 1381031418 + , 1381031554 + , 1381031621 + , 1381031836 + , 1381034808 + , 1381034941 + , 1381035044 + , 1381035354 + , 1381036072 + , 1381037030 + , 1381037146 + , 1381037345 + , 1381037638 + , 1381038699 + , 1381039391 + , 1381039816 + , 1381040036 + , 1381041389 + , 1381041442 + , 1381041497 + , 1381041762 + , 1381042464 + , 1381043331 + , 1381046109 + , 1381046342 + , 1381046390 + , 1381046571 + , 1381046589 + , 1381049032 + , 1381049196 + , 1381049867 + , 1381049925 + , 1381050883 + , 1381054508 + , 1381054875 + , 1381055316 + , 1381055550 + , 1381055772 + , 1381056812 + , 1381057213 + , 1381057307 + , 1381058937 + , 1381059752 + , 1381060015 + , 1381060050 + , 1381060524 + , 1381061531 + , 1381061572 + , 1381061648 + , 1381061749 + , 1381062569 + , 1381062687 + , 1381063320 + , 1381063720 + , 1381064134 + , 1381067028 + , 1381067698 + , 1381069387 + , 1381070333 + , 1381071744 + , 1381071838 + , 1381072694 + , 1381073760 + , 1381073790 + , 1381074369 + , 1381074621 + , 1381074946 + , 1381075404 + , 1381075516 + , 1381077354 + , 1381077382 + , 1381078479 + , 1381078693 + , 1381079579 + , 1381079710 + , 1381079865 + , 1381079906 +}; + +//#define WITH_HEADER_TRAILER + +//#define DUMMY + +#endif diff --git a/ROPCApplications/daq/rawdata/modules/include/copper.h b/ROPCApplications/daq/rawdata/modules/include/copper.h new file mode 100644 index 0000000000000000000000000000000000000000..97d5dc8e3e2166667b3e6ed57a633f6e7c22f9d6 --- /dev/null +++ b/ROPCApplications/daq/rawdata/modules/include/copper.h @@ -0,0 +1,221 @@ +#ifndef _COPPER_H_ +#define _COPPER_H_ + +#define COPPER_DRIVER_HEADER_MAGIC 0x7FFF0008 +#define COPPER_DRIVER_FOOTER_MAGIC 0x7FFF0009 + +struct copper_header { + int magic; + int event_number; + int subsys; + int crate; + int slot; + unsigned int ttrx[2]; +}; + +struct copper_footer { + int chksum_xor; + int magic; +}; + +#ifndef _LINUX_IOCTL_H +#include <linux/ioctl.h> +#endif + +int peek_next_event_size(void); +int shift_next_event_size(void); + +/* register positions */ +#define COPPER_FF_STA 0x00000000 // COPPER Event FIFO Status +#define COPPER_CONF_WA_AE 0x00000010 // COPPER Event FIFO Ach Almost Empty +#define COPPER_CONF_WB_AE 0x00000014 // COPPER Event FIFO Bch Almost Empty +#define COPPER_CONF_WC_AE 0x00000018 // COPPER Event FIFO Cch Almost Empty +#define COPPER_CONF_WD_AE 0x0000001C // COPPER Event FIFO Dch Almost Empty +#define COPPER_CONF_WA_FF 0x00000020 // COPPER Event FIFO Ach Defined Quantity +#define COPPER_CONF_WB_FF 0x00000024 // COPPER Event FIFO Bch Defined Quantity +#define COPPER_CONF_WC_FF 0x00000028 // COPPER Event FIFO Cch Defined Quantity +#define COPPER_CONF_WD_FF 0x0000002C // COPPER Event FIFO Dch Defined Quantity +#define COPPER_CONF_WA_AF 0x00000030 // COPPER Event FIFO Ach Almost Full +#define COPPER_CONF_WB_AF 0x00000034 // COPPER Event FIFO Bch Almost Full +#define COPPER_CONF_WC_AF 0x00000038 // COPPER Event FIFO Cch Almost Full +#define COPPER_CONF_WD_AF 0x0000003C // COPPER Event FIFO Dch Almost Full +#define COPPER_FF_RST 0x00000050 // COPPER Event FIFO & FINESSE Soft Reset +#define COPPER_FINESSE_STA 0x00000054 // FINESSE Status +#define COPPER_ALMFULL_ENB 0x00000058 // COPPER Almost Full Flag Enable +#define COPPER_VERSION 0x0000005C // COPPER firmware version +#define COPPER_TRGCM 0x00000060 // COPPER Trigger Command +#define COPPER_TRG_LENG 0x00000064 // COPPER Trigger Interval +#define COPPER_LEF_READSEL 0x00000068 // Length FIFO Read Select +#define COPPER_LEF_TOTAL 0x0000006C // Length FIFO Total Data +#define COPPER_LEF_AB 0x00000080 // COPPER Length FIFO A&Bch Data +#define COPPER_LEF_CD 0x00000084 // COPPER Length FIFO C&Dch Data +#define COPPER_LEF_STA 0x00000088 // COPPER Length FIFO Status +#define COPPER_LEF_WA_FF 0x00000090 // COPPER Length FIFO Ach Defined Quantity +#define COPPER_LEF_WB_FF 0x00000094 // COPPER Length FIFO Bch Defined Quantity +#define COPPER_LEF_WC_FF 0x00000098 // COPPER Length FIFO Cch Defined Quantity +#define COPPER_LEF_WD_FF 0x0000009C // COPPER Length FIFO Dch Defined Quantity +#define COPPER_LEF_WA_AF 0x000000A0 // COPPER Length FIFO Ach Almost Full +#define COPPER_LEF_WB_AF 0x000000A4 // COPPER Length FIFO Bch Almost Full +#define COPPER_LEF_WC_AF 0x000000A8 // COPPER Length FIFO Cch Almost Full +#define COPPER_LEF_WD_AF 0x000000AC // COPPER Length FIFO Dch Almost Full +#define COPPER_INT_STA 0x000000B0 // COPPER Interrupt Status +#define COPPER_INT_MASK 0x000000B4 // COPPER Interrupt Mask +#define COPPER_FF_RW 0x000000B8 // COPPER Event FIFO Single Read/Write Control +#define COPPER_INT_FACTOR 0x000000BC // COPPER Interruption Factor Reg. +#define COPPER_FF_WA 0x00000100 // COPPER Event FIFO Ach Single Write +#define COPPER_FF_WB 0x00000104 // COPPER Event FIFO Bch Single Write +#define COPPER_FF_WC 0x00000108 // COPPER Event FIFO Cch Single Write +#define COPPER_FF_WD 0x0000010C // COPPER Event FIFO Dch Single Write +#define COPPER_FF_RA 0x00000110 // COPPER Event FIFO Ach Single Read +#define COPPER_FF_RB 0x00000114 // COPPER Event FIFO Bch Single Read +#define COPPER_FF_RC 0x00000118 // COPPER Event FIFO Cch Single Read +#define COPPER_FF_RD 0x0000011C // COPPER Event FIFO Dch Single Read +#define COPPER_FF_DMA 0x00400000 // Event FIFO DMA Read Reg. + +// 2005/01/17 Append +#define COPPER_EWRP_A 0x00000140 // COPPER Event FIFO Ach Read/Write Pointer +#define COPPER_EWRP_B 0x00000144 // COPPER Event FIFO Bch Read/Write Pointer +#define COPPER_EWRP_C 0x00000148 // COPPER Event FIFO Cch Read/Write Pointer +#define COPPER_EWRP_D 0x0000014C // COPPER Event FIFO Dch Read/Write Pointer + +#define COPPER_WEA_COUNTER 0x00000150 // FINESSE Ach WE Counter +#define COPPER_WEB_COUNTER 0x00000154 // FINESSE Bch WE Counter +#define COPPER_WEC_COUNTER 0x00000158 // FINESSE Cch WE Counter +#define COPPER_WED_COUNTER 0x0000015C // FINESSE Dch WE Counter + +#define COPPER_LWRA_DMA 0x00000160 // COPPER Length FIFO Ach Read/Write Pointer for DMA +#define COPPER_LWRB_DMA 0x00000164 // COPPER Length FIFO Bch Read/Write Pointer for DMA +#define COPPER_LWRC_DMA 0x00000168 // COPPER Length FIFO Cch Read/Write Pointer for DMA +#define COPPER_LWRD_DMA 0x0000016C // COPPER Length FIFO Dch Read/Write Pointer for DMA + +#define COPPER_DMA_TRANS 0x00000170 // COPPER DMA Counter +#define COPPER_DMA_TS_ENB 0x00000174 // COPPER DMA Counter Control + +#define COPPER_RW_DEBUG 0x000000FC // Memory for Debug + +/* alias */ + +#define COPPER_LEF_WA_THR COPPER_LEF_WA_FF +#define COPPER_LEF_WB_THR COPPER_LEF_WB_FF +#define COPPER_LEF_WC_THR COPPER_LEF_WC_FF +#define COPPER_LEF_WD_THR COPPER_LEF_WD_FF + +/* ioctl cmds */ + +#define COPPER_MAGIC 0xdf + +#define CPRIOGET_FF_STA _IOR(COPPER_MAGIC, COPPER_FF_STA, sizeof(unsigned int)) +#define CPRIOGET_CONF_WA_AE _IOR(COPPER_MAGIC, COPPER_CONF_WA_AE, sizeof(unsigned int)) +#define CPRIOGET_CONF_WB_AE _IOR(COPPER_MAGIC, COPPER_CONF_WB_AE, sizeof(unsigned int)) +#define CPRIOGET_CONF_WC_AE _IOR(COPPER_MAGIC, COPPER_CONF_WC_AE, sizeof(unsigned int)) +#define CPRIOGET_CONF_WD_AE _IOR(COPPER_MAGIC, COPPER_CONF_WD_AE, sizeof(unsigned int)) +#define CPRIOGET_CONF_WA_FF _IOR(COPPER_MAGIC, COPPER_CONF_WA_FF, sizeof(unsigned int)) +#define CPRIOGET_CONF_WB_FF _IOR(COPPER_MAGIC, COPPER_CONF_WB_FF, sizeof(unsigned int)) +#define CPRIOGET_CONF_WC_FF _IOR(COPPER_MAGIC, COPPER_CONF_WC_FF, sizeof(unsigned int)) +#define CPRIOGET_CONF_WD_FF _IOR(COPPER_MAGIC, COPPER_CONF_WD_FF, sizeof(unsigned int)) +#define CPRIOGET_CONF_WA_AF _IOR(COPPER_MAGIC, COPPER_CONF_WA_AF, sizeof(unsigned int)) +#define CPRIOGET_CONF_WB_AF _IOR(COPPER_MAGIC, COPPER_CONF_WB_AF, sizeof(unsigned int)) +#define CPRIOGET_CONF_WC_AF _IOR(COPPER_MAGIC, COPPER_CONF_WC_AF, sizeof(unsigned int)) +#define CPRIOGET_CONF_WD_AF _IOR(COPPER_MAGIC, COPPER_CONF_WD_AF, sizeof(unsigned int)) +#define CPRIOGET_FF_RST _IOR(COPPER_MAGIC, COPPER_FF_RST, sizeof(unsigned int)) +#define CPRIOGET_FINESSE_STA _IOR(COPPER_MAGIC, COPPER_FINESSE_STA, sizeof(unsigned int)) +#define CPRIOGET_ALMFULL_ENB _IOR(COPPER_MAGIC, COPPER_ALMFULL_ENB, sizeof(unsigned int)) +#define CPRIOGET_VERSION _IOR(COPPER_MAGIC, COPPER_VERSION, sizeof(unsigned int)) +#define CPRIOGET_TRGCM _IOR(COPPER_MAGIC, COPPER_TRGCM, sizeof(unsigned int)) +#define CPRIOGET_TRG_LENG _IOR(COPPER_MAGIC, COPPER_TRG_LENG, sizeof(unsigned int)) +#define CPRIOGET_LEF_READSEL _IOR(COPPER_MAGIC, COPPER_LEF_READSEL, sizeof(unsigned int)) +#define CPRIOGET_LEF_TOTAL _IOR(COPPER_MAGIC, COPPER_LEF_TOTAL, sizeof(unsigned int)) +#define CPRIOGET_LEF_AB _IOR(COPPER_MAGIC, COPPER_LEF_AB, sizeof(unsigned int)) +#define CPRIOGET_LEF_CD _IOR(COPPER_MAGIC, COPPER_LEF_CD, sizeof(unsigned int)) +#define CPRIOGET_LEF_STA _IOR(COPPER_MAGIC, COPPER_LEF_STA, sizeof(unsigned int)) +#define CPRIOGET_LEF_WA_FF _IOR(COPPER_MAGIC, COPPER_LEF_WA_FF, sizeof(unsigned int)) +#define CPRIOGET_LEF_WB_FF _IOR(COPPER_MAGIC, COPPER_LEF_WB_FF, sizeof(unsigned int)) +#define CPRIOGET_LEF_WC_FF _IOR(COPPER_MAGIC, COPPER_LEF_WC_FF, sizeof(unsigned int)) +#define CPRIOGET_LEF_WD_FF _IOR(COPPER_MAGIC, COPPER_LEF_WD_FF, sizeof(unsigned int)) +#define CPRIOGET_LEF_WA_AF _IOR(COPPER_MAGIC, COPPER_LEF_WA_AF, sizeof(unsigned int)) +#define CPRIOGET_LEF_WB_AF _IOR(COPPER_MAGIC, COPPER_LEF_WB_AF, sizeof(unsigned int)) +#define CPRIOGET_LEF_WC_AF _IOR(COPPER_MAGIC, COPPER_LEF_WC_AF, sizeof(unsigned int)) +#define CPRIOGET_LEF_WD_AF _IOR(COPPER_MAGIC, COPPER_LEF_WD_AF, sizeof(unsigned int)) +#define CPRIOGET_INT_STA _IOR(COPPER_MAGIC, COPPER_INT_STA, sizeof(unsigned int)) +#define CPRIOGET_INT_MASK _IOR(COPPER_MAGIC, COPPER_INT_MASK, sizeof(unsigned int)) +#define CPRIOGET_FF_RW _IOR(COPPER_MAGIC, COPPER_FF_RW, sizeof(unsigned int)) +#define CPRIOGET_INT_FACTOR _IOR(COPPER_MAGIC, COPPER_INT_FACTOR, sizeof(unsigned int)) +#define CPRIOGET_FF_WA _IOR(COPPER_MAGIC, COPPER_FF_WA, sizeof(unsigned int)) +#define CPRIOGET_FF_WB _IOR(COPPER_MAGIC, COPPER_FF_WB, sizeof(unsigned int)) +#define CPRIOGET_FF_WC _IOR(COPPER_MAGIC, COPPER_FF_WC, sizeof(unsigned int)) +#define CPRIOGET_FF_WD _IOR(COPPER_MAGIC, COPPER_FF_WD, sizeof(unsigned int)) +#define CPRIOGET_FF_RA _IOR(COPPER_MAGIC, COPPER_FF_RA, sizeof(unsigned int)) +#define CPRIOGET_FF_RB _IOR(COPPER_MAGIC, COPPER_FF_RB, sizeof(unsigned int)) +#define CPRIOGET_FF_RC _IOR(COPPER_MAGIC, COPPER_FF_RC, sizeof(unsigned int)) +#define CPRIOGET_FF_RD _IOR(COPPER_MAGIC, COPPER_FF_RD, sizeof(unsigned int)) +#define CPRIOGET_FF_DMA _IOR(COPPER_MAGIC, COPPER_FF_DMA, sizeof(unsigned int)) + +#define CPRIOSET_FF_STA _IOW(COPPER_MAGIC, COPPER_FF_STA, sizeof(unsigned int)) +#define CPRIOSET_CONF_WA_AE _IOW(COPPER_MAGIC, COPPER_CONF_WA_AE, sizeof(unsigned int)) +#define CPRIOSET_CONF_WB_AE _IOW(COPPER_MAGIC, COPPER_CONF_WB_AE, sizeof(unsigned int)) +#define CPRIOSET_CONF_WC_AE _IOW(COPPER_MAGIC, COPPER_CONF_WC_AE, sizeof(unsigned int)) +#define CPRIOSET_CONF_WD_AE _IOW(COPPER_MAGIC, COPPER_CONF_WD_AE, sizeof(unsigned int)) +#define CPRIOSET_CONF_WA_FF _IOW(COPPER_MAGIC, COPPER_CONF_WA_FF, sizeof(unsigned int)) +#define CPRIOSET_CONF_WB_FF _IOW(COPPER_MAGIC, COPPER_CONF_WB_FF, sizeof(unsigned int)) +#define CPRIOSET_CONF_WC_FF _IOW(COPPER_MAGIC, COPPER_CONF_WC_FF, sizeof(unsigned int)) +#define CPRIOSET_CONF_WD_FF _IOW(COPPER_MAGIC, COPPER_CONF_WD_FF, sizeof(unsigned int)) +#define CPRIOSET_CONF_WA_AF _IOW(COPPER_MAGIC, COPPER_CONF_WA_AF, sizeof(unsigned int)) +#define CPRIOSET_CONF_WB_AF _IOW(COPPER_MAGIC, COPPER_CONF_WB_AF, sizeof(unsigned int)) +#define CPRIOSET_CONF_WC_AF _IOW(COPPER_MAGIC, COPPER_CONF_WC_AF, sizeof(unsigned int)) +#define CPRIOSET_CONF_WD_AF _IOW(COPPER_MAGIC, COPPER_CONF_WD_AF, sizeof(unsigned int)) +#define CPRIOSET_FF_RST _IOW(COPPER_MAGIC, COPPER_FF_RST, sizeof(unsigned int)) +#define CPRIOSET_FINESSE_STA _IOW(COPPER_MAGIC, COPPER_FINESSE_STA, sizeof(unsigned int)) +#define CPRIOSET_ALMFULL_ENB _IOW(COPPER_MAGIC, COPPER_ALMFULL_ENB, sizeof(unsigned int)) +#define CPRIOSET_TRGCM _IOW(COPPER_MAGIC, COPPER_TRGCM, sizeof(unsigned int)) +#define CPRIOSET_TRG_LENG _IOW(COPPER_MAGIC, COPPER_TRG_LENG, sizeof(unsigned int)) +#define CPRIOSET_LEF_READSEL _IOW(COPPER_MAGIC, COPPER_LEF_READSEL, sizeof(unsigned int)) +#define CPRIOSET_LEF_TOTAL _IOW(COPPER_MAGIC, COPPER_LEF_TOTAL, sizeof(unsigned int)) +#define CPRIOSET_LEF_AB _IOW(COPPER_MAGIC, COPPER_LEF_AB, sizeof(unsigned int)) +#define CPRIOSET_LEF_CD _IOW(COPPER_MAGIC, COPPER_LEF_CD, sizeof(unsigned int)) +#define CPRIOSET_LEF_STA _IOW(COPPER_MAGIC, COPPER_LEF_STA, sizeof(unsigned int)) +#define CPRIOSET_LEF_WA_FF _IOW(COPPER_MAGIC, COPPER_LEF_WA_FF, sizeof(unsigned int)) +#define CPRIOSET_LEF_WB_FF _IOW(COPPER_MAGIC, COPPER_LEF_WB_FF, sizeof(unsigned int)) +#define CPRIOSET_LEF_WC_FF _IOW(COPPER_MAGIC, COPPER_LEF_WC_FF, sizeof(unsigned int)) +#define CPRIOSET_LEF_WD_FF _IOW(COPPER_MAGIC, COPPER_LEF_WD_FF, sizeof(unsigned int)) +#define CPRIOSET_LEF_WA_AF _IOW(COPPER_MAGIC, COPPER_LEF_WA_AF, sizeof(unsigned int)) +#define CPRIOSET_LEF_WB_AF _IOW(COPPER_MAGIC, COPPER_LEF_WB_AF, sizeof(unsigned int)) +#define CPRIOSET_LEF_WC_AF _IOW(COPPER_MAGIC, COPPER_LEF_WC_AF, sizeof(unsigned int)) +#define CPRIOSET_LEF_WD_AF _IOW(COPPER_MAGIC, COPPER_LEF_WD_AF, sizeof(unsigned int)) +#define CPRIOSET_INT_STA _IOW(COPPER_MAGIC, COPPER_INT_STA, sizeof(unsigned int)) +#define CPRIOSET_INT_MASK _IOW(COPPER_MAGIC, COPPER_INT_MASK, sizeof(unsigned int)) +#define CPRIOSET_FF_RW _IOW(COPPER_MAGIC, COPPER_FF_RW, sizeof(unsigned int)) +#define CPRIOSET_INT_FACTOR _IOW(COPPER_MAGIC, COPPER_INT_FACTOR, sizeof(unsigned int)) +#define CPRIOSET_FF_WA _IOW(COPPER_MAGIC, COPPER_FF_WA, sizeof(unsigned int)) +#define CPRIOSET_FF_WB _IOW(COPPER_MAGIC, COPPER_FF_WB, sizeof(unsigned int)) +#define CPRIOSET_FF_WC _IOW(COPPER_MAGIC, COPPER_FF_WC, sizeof(unsigned int)) +#define CPRIOSET_FF_WD _IOW(COPPER_MAGIC, COPPER_FF_WD, sizeof(unsigned int)) +#define CPRIOSET_FF_RA _IOW(COPPER_MAGIC, COPPER_FF_RA, sizeof(unsigned int)) +#define CPRIOSET_FF_RB _IOW(COPPER_MAGIC, COPPER_FF_RB, sizeof(unsigned int)) +#define CPRIOSET_FF_RC _IOW(COPPER_MAGIC, COPPER_FF_RC, sizeof(unsigned int)) +#define CPRIOSET_FF_RD _IOW(COPPER_MAGIC, COPPER_FF_RD, sizeof(unsigned int)) +#define CPRIOSET_FF_DMA _IOW(COPPER_MAGIC, COPPER_FF_DMA, sizeof(unsigned int)) + +/* alias */ + +#define CPRIOSET_LEF_WA_THR CPRIOSET_LEF_WA_FF +#define CPRIOSET_LEF_WB_THR CPRIOSET_LEF_WB_FF +#define CPRIOSET_LEF_WC_THR CPRIOSET_LEF_WC_FF +#define CPRIOSET_LEF_WD_THR CPRIOSET_LEF_WD_FF + +#define CPRIOGET_LEF_WA_THR CPRIOGET_LEF_WA_FF +#define CPRIOGET_LEF_WB_THR CPRIOGET_LEF_WB_FF +#define CPRIOGET_LEF_WC_THR CPRIOGET_LEF_WC_FF +#define CPRIOGET_LEF_WD_THR CPRIOGET_LEF_WD_FF + +/* cmds */ + +#define CPRIO_RESET_FINESSE _IO(COPPER_MAGIC, 1) +#define CPRIO_INIT_RUN _IO(COPPER_MAGIC, 2) +#define CPRIO_RESET_COPPER _IO(COPPER_MAGIC, 3) +#define CPRIO_SET_SUBSYS _IO(COPPER_MAGIC, 4) +#define CPRIO_SET_CRATE _IO(COPPER_MAGIC, 5) +#define CPRIO_SET_SLOT _IO(COPPER_MAGIC, 6) +#define CPRIO_FORCE_DMA _IO(COPPER_MAGIC, 7) +#define CPRIO_END_RUN _IO(COPPER_MAGIC, 8) + +#endif /* _COPPER_H_ */ diff --git a/ROPCApplications/daq/rawdata/src/CprErrorMessage.cc b/ROPCApplications/daq/rawdata/src/CprErrorMessage.cc new file mode 100644 index 0000000000000000000000000000000000000000..7d983dbac9e29416136aefde2da35439c5ff48d1 --- /dev/null +++ b/ROPCApplications/daq/rawdata/src/CprErrorMessage.cc @@ -0,0 +1,99 @@ +//+ +// File : ErrorLog.h +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- +//#include <framework/logging/Logger.h> +#include <daq/rawdata/CprErrorMessage.h> +// #include <time.h> +#include <syslog.h> +// #include <framework/core/Module.h> + +using namespace std; +using namespace Belle2; + +//REG_MODULE(CprErrorMessage) +// CprErrorMessage::CprErrorMessage( const char* entry_name ) +// { +// // openlog( entry_name, LOG_PERROR, LOG_LOCAL0 ); +// } + +CprErrorMessage::CprErrorMessage() +{ + openlog("", LOG_PERROR , LOG_LOCAL0); +} + +CprErrorMessage::~CprErrorMessage() +{ + closelog(); +} + + +void CprErrorMessage::PrintError(const int shmflag, RunInfoBuffer* nsm_status, string err_str) +{ + if (shmflag > 0) { + nsm_status->reportError(RunInfoBuffer::CPRFIFO_FULL);//need to implement error flag + } + PrintError(err_str.c_str()); +} + +void CprErrorMessage::PrintError(const int shmflag, RunInfoBuffer* nsm_status, char* err_message, + const char* file, const char* func_name, const int line) +{ + string err_str = err_message; + if (shmflag > 0) { + nsm_status->reportError(RunInfoBuffer::CPRFIFO_FULL);//need to implement error flag + } + PrintError(err_message, file, func_name, line); +} + + +void CprErrorMessage::PrintError(char* err_message, const char* file, const char* func_name, const int line) +{ + char err_buf[500]; + sprintf(err_buf, "%s : %s %s %d", err_message, file, func_name, line); + // fprintf( stderr, "[FATAL] %s", err_message); + printf("%s", err_buf); fflush(stdout); + // B2FATAL(err_buf); + // printf("AL] %s\n", err_buf); + exit(1); + +// time_t current; +// time(¤t); +// printf("\033[31m"); +// perror("[ERROR] 0: "); +// printf("[ERROR] 1: %s", ctime(¤t)); +// printf("[ERROR] 2: %s\n", err_message); +// printf("[ERROR] 3: [file] %s [Line] %d\n", file, line); +// printf("[ERROR] 4: [function] %s\n", func_name); +// printf("\033[0m"); +// fflush(stdout); +// errmsg(LOG_LOCAL0|LOG_ERR, +// "CRITICAL : %s : init_shm() failed to get shmhead ( %p )\n", +// __PRETTY_FUNCTION__, shmhead ); + + return; +} + + + +void CprErrorMessage::PrintError(const char* err_message) +{ + printf("[DEBUG] %s", err_message); + fflush(stdout); + // B2FATAL(err_message); + printf("[FATAL] %s\n", err_message); exit(1); + +// printf("\033[31m"); +// printf("\033[47m"); +// // B2FATAL(err_message); +// perror("[ERROR] 0: "); +// printf("[ERROR] 1: %s", ctime(¤t)); +// printf("[ERROR] 2: %s\n", err_message); +// printf("\033[0m"); +// printf("\033[40m"); +// fflush(stdout); + return; +} diff --git a/ROPCApplications/daq/rawdata/src/DesSer.cc b/ROPCApplications/daq/rawdata/src/DesSer.cc new file mode 100644 index 0000000000000000000000000000000000000000..a0612b1e054ff2b9e05ace1f0183217e54fa24f6 --- /dev/null +++ b/ROPCApplications/daq/rawdata/src/DesSer.cc @@ -0,0 +1,717 @@ +//+ +// File : DeSerializerPC.cc +// Description : Module to receive data from eb0 and send it to eb1tx +// +// Author : Satoru Yamada Itoh, IPNS, KEK +// Date : 2 - Aug - 2013 +//- +#include <daq/rawdata/DesSer.h> + +#include <netdb.h> +#include <netinet/tcp.h> + +#include <sys/mman.h> +#include <sys/socket.h> +#include <sys/uio.h> + +#include <csignal> + + + +//#define DEBUG +using namespace std; +using namespace Belle2; + +//---------------------------------------------------------------- +// Implementation +//---------------------------------------------------------------- + +DesSer::DesSer() +{ + + m_num_connections = 1; + + m_exprunsubrun_no = 0; // will obtain info from data + + m_prev_exprunsubrun_no = 0xFFFFFFFF; + +#ifdef NONSTOP + m_run_pause = 0; + + m_run_error = 0; +#endif + + // B2INFO("DeSerializerPrePC: Constructor done."); + printf("[DEBUG] DesSer: Constructor done.\n"); fflush(stdout); +} + + + +DesSer::~DesSer() +{ + +} + + +int* DesSer::getPreAllocBuf() +{ + int* tempbuf = 0; + if (m_num_usedbuf < NUM_PREALLOC_BUF) { + tempbuf = m_bufary[ m_num_usedbuf ]; + m_num_usedbuf++; + } else { + char err_buf[500]; + sprintf(err_buf, + "No pre-allocated buffers are left. %d > %d. Not enough buffers are allocated or " + "memory leak or forget to call ClearNumUsedBuf every event loop. Exting...", + m_num_usedbuf, NUM_PREALLOC_BUF); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(1); + } + return tempbuf; +} + + +int* DesSer::getNewBuffer(int nwords, int* delete_flag) +{ + + int* temp_buf = NULL; + // Prepare buffer + if (nwords > BUF_SIZE_WORD) { + *delete_flag = 1; + temp_buf = new int[ nwords ]; + } else { + if ((temp_buf = getPreAllocBuf()) == 0x0) { + char err_buf[500]; + sprintf(err_buf, "Null pointer from GetPreALlocBuf(). Exting...\n"); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(1); + } else { + *delete_flag = 0; + } + } + + return temp_buf; + +} + +void DesSer::initialize(bool close_listen) +{ + printf("[DEBUG] DesSer: initialize() started.\n"); fflush(stdout); + signal(SIGPIPE , SIG_IGN); + + // + // initialize Rx part from DeSerializer**.cc + // + + // allocate buffer + for (int i = 0 ; i < NUM_PREALLOC_BUF; i++) { + m_bufary[i] = new int[ BUF_SIZE_WORD ]; + } + m_buffer = new int[ BUF_SIZE_WORD ]; + + // initialize buffer + for (int i = 0 ; i < NUM_PREALLOC_BUF; i++) { + memset(m_bufary[i], 0, BUF_SIZE_WORD * sizeof(int)); + } + + // Open message handler + clearNumUsedBuf(); + // Shared memory + if (m_shmflag > 0) { + if (m_nodename.size() == 0 || m_nodeid < 0) { + m_shmflag = 0; + } else { + m_status.open(m_nodename, m_nodeid); + m_status.reportReady(); + } + } + + event_diff = 0; + + m_prev_copper_ctr = 0xFFFFFFFF; + m_prev_evenum = 0xFFFFFFFF; + + + // + // initialize Rx part from DeSerializer**.cc + // + m_start_flag = 0; + n_basf2evt = -1; + m_compressionLevel = 0; + +#ifdef DUMMY + m_buffer = new int[ BUF_SIZE_WORD ]; +#endif + Accept(close_listen); +#ifdef NONSTOP + openRunPauseNshm(); +#endif + + + // For monitor + if (m_status.isAvailable()) { + m_status.setOutputNBytes(0); + m_status.setOutputCount(0); + } + // B2INFO("DesSer: initialize() was done."); + printf("[DEBUG] DesSer: initialize() was done.\n"); fflush(stdout); + +} + + +///////////////////////////////////////////////////// +// From Serializer.cc +///////////////////////////////////////////////////// + +//void DesSer::fillSendHeaderTrailer(SendHeader* hdr, SendTrailer* trl, RawDataBlock* rawdblk) +void DesSer::fillSendHeaderTrailer(SendHeader* hdr, SendTrailer* trl, RawDataBlockFormat* rawdblk) +{ + + int total_send_nwords = + hdr->GetHdrNwords() + + rawdblk->TotalBufNwords() + + // rawhdr.GetNwords() + + trl->GetTrlNwords(); + + hdr->SetNwords(total_send_nwords); + hdr->SetNumEventsinPacket(rawdblk->GetNumEvents()); + hdr->SetNumNodesinPacket(rawdblk->GetNumNodes()); + + // + // For bug check + // + if (rawdblk->GetNumEntries() == 1) { + if (total_send_nwords != (rawdblk->GetBuffer(0))[ 0 ] + 8) { + char err_buf[500]; + sprintf(err_buf, "Length error. total length %d rawdblk length %d. Exting...\n" , + total_send_nwords, (rawdblk->GetBuffer(0))[ 0 ]); + printData(rawdblk->GetBuffer(0), rawdblk->TotalBufNwords()); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(-1); + } + } + + + for (int i = 0; i < rawdblk->GetNumEntries(); i++) { + + //copy event # from a tonp COPPER block + if (!(rawdblk->CheckFTSWID(i)) && !(rawdblk->CheckTLUID(i))) { + tmp_header.SetBuffer(rawdblk->GetBuffer(i)); + hdr->SetEventNumber(tmp_header.GetEveNo()); + hdr->SetNodeID(tmp_header.GetNodeID()); + hdr->SetExpRunWord(tmp_header.GetExpRunSubrun()); + break; + } + + //Error if you cannot find any COPPER block + if (i == (rawdblk->GetNumEntries() - 1)) { + printf("[DEBUG] i= %d : num entries %d : Tot words %d\n", i , rawdblk->GetNumEntries(), rawdblk->TotalBufNwords()); + printData(rawdblk->GetBuffer(0), rawdblk->TotalBufNwords()); + + char err_buf[500] = "[FATAL] CORRUPTED DATA: No COPPER blocks in RawDataBlock. Exiting..."; + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(-1); + } + } + return; +} + + +//int DesSer::sendByWriteV(RawDataBlock* rawdblk) +int DesSer::sendByWriteV(RawDataBlockFormat* rawdblk) +{ + SendHeader send_header; + SendTrailer send_trailer; + fillSendHeaderTrailer(&send_header, &send_trailer, rawdblk); + + enum { + NUM_BUFFER = 3 + }; + struct iovec iov[ NUM_BUFFER ]; + + // check Body data size + int rawcopper_nwords = rawdblk->TotalBufNwords(); + + //Fill iov info. + iov[0].iov_base = (char*)send_header.GetBuffer(); + iov[0].iov_len = sizeof(int) * send_header.GetHdrNwords(); + + iov[1].iov_base = (char*)rawdblk->GetWholeBuffer(); + iov[1].iov_len = sizeof(int) * rawcopper_nwords; + + iov[2].iov_base = (char*)send_trailer.GetBuffer(); + iov[2].iov_len = sizeof(int) * send_trailer.GetTrlNwords(); + + // Send Multiple buffers + int n = 0; + while (true) { + if ((n = writev(m_socket_send, iov, NUM_BUFFER)) < 0) { + if (errno == EINTR) { + continue; + } else if (errno == EAGAIN || errno == EWOULDBLOCK) { +#ifdef NONSTOP + string err_str; + callCheckRunPause(err_str); +#endif + continue; + } else { + char err_buf[500]; + sprintf(err_buf, "[WARNING] WRITEV error.(%s) : sent %d bytes, header %d bytes body %d trailer %d\n" , + strerror(errno), n, iov[0].iov_len, iov[1].iov_len, iov[2].iov_len); +#ifdef NONSTOP + m_run_error = 1; + // B2ERROR(err_buf); + printf("%s\n", err_buf); fflush(stdout); + string err_str = "RUN_ERROR"; + throw (err_str); // To exit this module, go to DeSerializer** and wait for run-resume. +#else + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); +#endif + } + } + break; + } + +#ifdef DEBUG + printf("[DEBUG] *******BODY**********\n"); + printf("[DEBUG] \n%.8d : ", 0); + printData((int*)(iov[1].iov_base), iov[1].iov_len); +#endif + + + int total_send_bytes = sizeof(int) * send_header.GetTotalNwords(); + + + // + // Retry sending + // + if (n != total_send_bytes) { + // B2WARNING("Serializer: Sent byte(" << n << "bytes) is not same as the event size (" << total_send_bytes << "bytes). Retrying..."); + printf("[WARNING] Serializer: Sent byte( %d bytes) is not same as the event size ( %d bytes). Retrying...\n", n, + total_send_bytes); // back to WARNING due to request from Konno-san on 2018/7/3 + fflush(stdout); + + double retry_start = getTimeSec(); + // Send Header + if (n < (int)(iov[ 0 ].iov_len)) { + n += Send(m_socket_send, (char*)iov[ 0 ].iov_base + n, iov[ 0 ].iov_len - n); + } + + if (n < (int)(iov[ 0 ].iov_len + iov[ 1 ].iov_len)) { + n += Send(m_socket_send, (char*)iov[ 1 ].iov_base + (n - iov[ 0 ].iov_len), iov[ 1 ].iov_len - (n - iov[ 0 ].iov_len)); + } + + if (n < (int)(iov[ 0 ].iov_len + iov[ 1 ].iov_len + iov[ 2 ].iov_len)) { + n += Send(m_socket_send, (char*)iov[ 2 ].iov_base + (n - iov[ 0 ].iov_len - iov[ 1 ].iov_len), + iov[ 2 ].iov_len - (n - iov[ 0 ].iov_len - iov[ 1 ].iov_len)); + } + double retry_end = getTimeSec(); + // B2WARNING("Resending ends. It takes " << retry_end - retry_start << "(s)"); + printf("[WARNING] Resending ends. It takes %lf (s)\n", retry_end - retry_start); + fflush(stdout); // back to WARNING due to request from Konno-san on 2018/7/3 + } + // printf( "[DEBUG] n %d total %d\n", n, total_send_bytes); + // delete temp_buf; + + return total_send_bytes; + +} + + +int DesSer::Send(int socket, char* buf, int size_bytes) +{ + int sent_bytes = 0; + while (true) { + int ret = 0; + if ((ret = send(socket, + buf + sent_bytes, size_bytes - sent_bytes, MSG_NOSIGNAL)) < 0) { + if (errno == EINTR) { + continue; + } else if (errno == EAGAIN || errno == EWOULDBLOCK) { +#ifdef NONSTOP + string err_str; + callCheckRunPause(err_str); +#endif + continue; + } else { + char err_buf[500]; + sprintf(err_buf, "[WARNING] SEND ERROR.(%s)", strerror(errno)); +#ifdef NONSTOP + m_run_error = 1; + // B2ERROR(err_buf); + printf("%s\n", err_buf); fflush(stdout); + string err_str = "RUN_ERROR"; + throw (err_str); // To exit this module, go to DeSerializer** and wait for run-resume. +#else + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); +#endif + } + } + sent_bytes += ret; + if (sent_bytes == size_bytes) break; + } + return sent_bytes; +} + +void DesSer::Accept(bool close_listen) +{ + // + // Connect to cprtb01 + // + struct hostent* host; + host = gethostbyname(m_hostname_local.c_str()); + if (host == NULL) { + char temp_buf[500]; + sprintf(temp_buf, "[FATAL] hostname(%s) cannot be resolved(%s). Check /etc/hosts. Exiting...\n", + m_hostname_local.c_str(), strerror(errno)); + print_err.PrintError(temp_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); + } + + // + // Bind and listen + // + int fd_listen; + struct sockaddr_in sock_listen; + sock_listen.sin_family = AF_INET; + sock_listen.sin_addr.s_addr = *(unsigned int*)host->h_addr_list[0]; + + socklen_t addrlen = sizeof(sock_listen); + sock_listen.sin_port = htons(m_port_to); + fd_listen = socket(PF_INET, SOCK_STREAM, 0); + + int flags = 1; + int ret = setsockopt(fd_listen, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)sizeof(flags)); + if (ret < 0) { + perror("Failed to set REUSEADDR"); + } + + if (bind(fd_listen, (struct sockaddr*)&sock_listen, sizeof(struct sockaddr)) < 0) { + printf("[FATAL] Failed to bind. Maybe other programs have already occupied this port(%d). Exiting...\n", + m_port_to); fflush(stdout); + // Check the process occupying the port 30000. + FILE* fp; + char buf[256]; + char cmdline[500]; + sprintf(cmdline, "/usr/sbin/ss -ap | grep %d", m_port_to); + if ((fp = popen(cmdline, "r")) == NULL) { + printf("[WARNING] Failed to run %s\n", cmdline); + } + while (fgets(buf, 256, fp) != NULL) { + printf("[ERROR] Failed to bind. output of ss(port %d) : %s\n", m_port_to, buf); fflush(stdout); + } + // Error message + fclose(fp); + char temp_char[500]; + sprintf(temp_char, "[FATAL] Failed to bind.(%s) Maybe other programs have already occupied this port(%d). Exiting...", + strerror(errno), m_port_to); + print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); + } + + int val1 = 0; + setsockopt(fd_listen, IPPROTO_TCP, TCP_NODELAY, &val1, (socklen_t)sizeof(val1)); + int backlog = 1; + if (listen(fd_listen, backlog) < 0) { + char err_buf[500]; + sprintf(err_buf, "Failed in listen(%s). Exting...", strerror(errno)); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } + + // + // Accept + // + int fd_accept; + struct sockaddr_in sock_accept; + printf("[DEBUG] Accepting... : port %d server %s\n", m_port_to, m_hostname_local.c_str()); + fflush(stdout); + + if ((fd_accept = accept(fd_listen, (struct sockaddr*) & (sock_accept), &addrlen)) == 0) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] Failed to accept(%s). Exiting...", strerror(errno)); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } else { + // B2INFO("Done."); + printf("[DEBUG] Done.\n"); fflush(stdout); + + // set timepout option + struct timeval timeout; + timeout.tv_sec = 1; + timeout.tv_usec = 0; + ret = setsockopt(fd_accept, SOL_SOCKET, SO_SNDTIMEO, &timeout, (socklen_t)sizeof(timeout)); + if (ret < 0) { + char temp_char[100] = "[FATAL] Failed to set TIMEOUT. Exiting..."; + print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } + } + + if (close_listen) { + close(fd_listen); + } + + // int flag = 1; + // ret = setsockopt(fd_accept, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(flag) ); + m_socket_send = fd_accept; + + if (m_status.isAvailable()) { + m_status.setOutputPort(ntohs(sock_listen.sin_port)); + m_status.setOutputAddress(sock_listen.sin_addr.s_addr); + // B2INFO("Accepted " << (int)ntohs(sock_listen.sin_port) << " " << (int)sock_listen.sin_addr.s_addr); + printf("Accepted. port %d address %u\n", (int)ntohs(sock_listen.sin_port), (int)sock_listen.sin_addr.s_addr); fflush(stdout); + } + + return; + +} + +double DesSer::getTimeSec() +{ + struct timeval t; + gettimeofday(&t, NULL); + return (t.tv_sec + t.tv_usec * 1.e-6); +} + + +void DesSer::recordTime(int event, double* array) +{ + if (event >= 50000 && event < 50500) { + array[ event - 50000 ] = getTimeSec() - m_start_time; + } + return; +} + + +unsigned int DesSer::calcXORChecksum(int* buf, int nwords) +{ + unsigned int checksum = 0; + for (int i = 0; i < nwords; i++) { + checksum = checksum ^ buf[ i ]; + } + return checksum; +} + + +void DesSer::printData(int* buf, int nwords) +{ + printf("[DEBUG]"); + for (int i = 0; i < nwords; i++) { + printf("%.8x ", buf[ i ]); + if (i % 10 == 9) printf("\n[DEBUG]"); + } + printf("\n[DEBUG]"); + printf("\n"); + return; +} + + +#ifdef NONSTOP +void DesSer::openRunPauseNshm() +{ + char path_shm[100] = "/cpr_pause_resume"; + int fd = shm_open(path_shm, O_RDONLY, 0666); + if (fd < 0) { + printf("[DEBUG] %s\n", path_shm); + perror("[ERROR] shm_open2"); + exit(1); + } + m_ptr = (int*)mmap(NULL, sizeof(int), PROT_READ, MAP_SHARED, fd, 0); + return; +} + +int DesSer::checkRunPause() +{ +#ifdef NONSTOP_SLC + if (m_status.getState() == m_status.PAUSING) { +#else + if (*m_ptr) { +#endif + return 1; + } else { + return 0; + } +} + +int DesSer::checkRunRecovery() +{ +#ifdef NONSTOP_SLC + if (m_status.getState() == m_status.RESUMING) { +#else + if (*m_ptr) { +#endif + return 0; + } else { + return 1; + } +} + +void DesSer::resumeRun() +{ +#ifdef NONSTOP_DEBUG + printf("\033[34m"); + printf("###########(Ser) the 1st event sicne the resume ###############\n"); + fflush(stdout); + printf("\033[0m"); +#endif + m_run_error = 0; + m_run_pause = 0; + return; +} + + +void DesSer::pauseRun() +{ + m_run_pause = 1; +#ifdef NONSTOP_DEBUG + printf("###########(Ser) Pause the run ###############\n"); + fflush(stdout); +#endif + return; +} + + + + +void DesSer::callCheckRunPause(string& err_str) +{ +#ifdef NONSTOP_DEBUG + printf("\033[34m"); + printf("###########(DesSer) TIMEOUT. ###############\n"); + fflush(stdout); + printf("\033[0m"); +#endif + if (checkRunPause()) { +#ifdef NONSTOP_DEBUG + printf("\033[31m"); + printf("###########(DesSer) Pause is detected. ###############\n"); + fflush(stdout); + printf("\033[0m"); +#endif + m_run_pause = 1; + throw (err_str); + } + return; +} + + +int DesSer::CheckConnection(int socket) +{ + // Modify Yamagata-san's eb/iseof.cc + + + int ret; + char buffer[100000]; + int eagain_cnt = 0; + int tot_ret = 0; + printf("CC1\n"); fflush(stdout); + ret = send(socket, buffer, 0, MSG_DONTWAIT); + printf("CC2\n"); fflush(stdout); + switch (ret) { + case 0: + break; + case -1: + if (errno == EAGAIN) { + printf("EAGAIN %d cnt %d recvd %d\n", socket, eagain_cnt, tot_ret); fflush(stdout); + /* not EOF, no data in queue */ + if (eagain_cnt > 100) { + return 0; + } + usleep(10000); + eagain_cnt++; + } else { + printf("ERROR %d errno %d err %s\n", socket , errno, strerror(errno)); fflush(stdout); + close(socket); + return -1; + } + default: + printf("Return value %d of send is strange. Exting...\n", ret); + fflush(stdout); + exit(1); + } + + while (true) { + + // + // Extract data in the socket buffer of a peer + // + // ret = recv( socket, buffer, sizeof(buffer), MSG_PEEK|MSG_DONTWAIT ); + ret = recv(socket, buffer, sizeof(buffer), MSG_DONTWAIT); + switch (ret) { + case 0: /* EOF */ + printf("EOF %d\n", socket); fflush(stdout); + close(socket); + return -1; + case -1: + if (errno == EAGAIN) { + printf("EAGAIN %d cnt %d recvd %d\n", socket, eagain_cnt, tot_ret); fflush(stdout); + /* not EOF, no data in queue */ + if (eagain_cnt > 100) { + return 0; + } + usleep(10000); + eagain_cnt++; + } else { + printf("ERROR %d errno %d err %s\n", socket , errno, strerror(errno)); fflush(stdout); + close(socket); + return -1; + } + break; + default: + tot_ret += ret; + printf("Flushing data in socket buffer : sockid = %d %d bytes tot %d bytes\n", socket, ret, tot_ret); fflush(stdout); + } + } +} + + +#endif + +void DesSer::shmOpen(char*, char*) +//(char* path_cfg, char* path_sta) +{ + errno = 0; + /*m_shmfd_cfg = shm_open( "/cpr_config2", O_CREAT | O_EXCL | O_RDWR, 0666); + if (m_shmfd_cfg < 0) { + if (errno != EEXIST) { + perror("shm_open1"); + exit(1); + } + m_shmfd_cfg = shm_open(path_cfg, O_RDWR, 0666); + if (m_shmfd_cfg < 0) { + printf( "[DEBUG] %s\n", path_cfg); + perror("[ERROR] shm_open2"); + exit(1); + } + */ + //} + /* + m_shmfd_sta = shm_open( "/cpr_status2", O_CREAT | O_EXCL | O_RDWR, 0666); + if (m_shmfd_sta < 0) { + if (errno != EEXIST) { + perror("shm_open1"); + exit(1); + } + m_shmfd_sta = shm_open(path_sta , O_RDWR, 0666); + if (m_shmfd_sta < 0) { + printf( "[DEBUG] %s\n", path_sta); + perror("[ERROR] shm_open2"); + exit(1); + } + //} + int size = 4 * sizeof(int); + ftruncate(m_shmfd_cfg, size); + ftruncate(m_shmfd_sta, size); + */ +} + + + diff --git a/ROPCApplications/daq/rawdata/src/DesSerCOPPER.cc b/ROPCApplications/daq/rawdata/src/DesSerCOPPER.cc new file mode 100644 index 0000000000000000000000000000000000000000..4681734150a558ca15927b3c74b6ac32fcd1b4e1 --- /dev/null +++ b/ROPCApplications/daq/rawdata/src/DesSerCOPPER.cc @@ -0,0 +1,598 @@ +//+ +// File : DesSerCOPPER.cc +// Description : Module to receive data from eb0 and send it to eb1tx +// +// Author : Satoru Yamada Itoh, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <daq/rawdata/modules/copper.h> +#include <daq/rawdata/DesSerCOPPER.h> + +#include <fcntl.h> +#include <sys/ioctl.h> + +using namespace std; +using namespace Belle2; + + + +//---------------------------------------------------------------- +// Implementation +//---------------------------------------------------------------- + +DesSerCOPPER::DesSerCOPPER(string host_recv, int port_recv, string host_send, int port_send, int shmflag, + const std::string& nodename, int nodeid, int finesse_bitflag) +{ + m_finesse_bit_flag = finesse_bitflag; + + for (int i = 0 ; i < m_num_connections; i++) { + // m_hostname_from.push_back( "localhost"); + m_hostname_from.push_back(host_recv); + // m_port_from.push_back(30000); + m_port_from.push_back(port_recv) ; + m_socket_recv.push_back(-1); + } + + // m_port_to = 31001; + m_port_to = port_send; + // m_hostname_local = "localhost"; + m_hostname_local = host_send; + m_nodename = nodename; + + m_shmflag = shmflag; + + m_nodeid = nodeid; + + // B2INFO("DeSerializerPrePC: Constructor done."); + printf("[DEBUG] DeSerializerPrePC: Constructor done.\n"); fflush(stdout); +} + + + +DesSerCOPPER::~DesSerCOPPER() +{ +} + +void DesSerCOPPER::DataAcquisition() +{ + // For data check + unsigned int eve_copper_0 = 0; + // B2INFO("initializing..."); + printf("[DEBUG] initializing...\n"); fflush(stdout); + initialize(false); + + + // B2INFO("Done."); + printf("[DEBUG] Done.\n"); fflush(stdout); + + unsigned int m_prev_ftsweve32 = 0xffffffff; + + if (m_start_flag == 0) { + // + // Connect to eb0: This should be here because we want Serializer first to accept connection from eb1tx + // + initializeCOPPER(); + // Connect(); + + if (m_status.isAvailable()) { + // B2INFO("DeSerializerPrePC: Waiting for Start...\n"); + printf("[DEBUG] DeSerializerPrePC: Waiting for Start...\n"); fflush(stdout); + m_status.reportRunning(); + } + m_start_time = getTimeSec(); + n_basf2evt = 0; + } + + // + // Main loop + // + while (1) { + // + // Stand-by loop + // +#ifdef NONSTOP + if (m_run_pause > 0 || m_run_error > 0) { + waitResume(); + } +#endif + + clearNumUsedBuf(); + // RawDataBlock raw_datablk[ NUM_EVT_PER_BASF2LOOP_PC ]; + RawDataBlockFormat raw_datablk[ NUM_EVT_PER_BASF2LOOP_PC ]; + + + // + // Recv loop + // + for (int j = 0; j < NUM_EVT_PER_BASF2LOOP_PC; j++) { + // + // Receive data from COPPER + // + eve_copper_0 = 0; + int delete_flag_from = + 0; // Delete flag for temp_rawdatablk.It can be set to 1 by setRecvdBuffer if the buffer size is larger than that of pre-allocated buffer. + int delete_flag_to = + 0; // Delete flag for raw_datablk[i]. It can be set to 1 by getNewBuffer if the buffer size is larger than that of pre-allocated buffer. + RawDataBlockFormat temp_rawdatablk; + try { + + int delete_flag = 0, m_size_word = 0; + + if (m_start_flag == 0) { + printf("[DEBUG] DeSerializerCOPPER: Reading the 1st event from COPPER FIFO... run %.8x eve %.8x\n", m_prev_exprunsubrun_no, + m_prev_ftsweve32); fflush(stdout); + } + int* temp_buf; + + temp_buf = readOneEventFromCOPPERFIFO(j, &delete_flag, &m_size_word); + m_status.copyEventHeader(temp_buf); + if (m_start_flag == 0) { + printf("[DEBUG] DeSerializerCOPPER: Done. the size of the 1st event is %d words\n", m_size_word); fflush(stdout); + } + const int num_nodes = 1; + const int num_events = 1; + PreRawCOPPERFormat_latest temp_rawcopper; + temp_rawcopper.SetBuffer(temp_buf, m_size_word, 0, num_events, num_nodes); + // Fill header and trailer + try { + m_prev_ftsweve32 = temp_rawcopper.FillTopBlockRawHeader(m_nodeid, m_prev_ftsweve32, m_prev_exprunsubrun_no, &m_exprunsubrun_no); + m_prev_exprunsubrun_no = m_exprunsubrun_no; + // fillNewRawCOPPERHeader( &temp_rawcopper ); + } catch (string err_str) { + print_err.PrintError(m_shmflag, &m_status, err_str); + exit(1); + } + +// if (m_dump_fname.size() > 0) { +// dumpData((char*)temp_buf, m_size_word * sizeof(int)); +// } + m_recvd_totbytes += m_size_word * sizeof(int); + + // temp_rawdblk = raw_dblkarray.appendNew(); + // temp_rawdatablk->SetBuffer(temp_buf, m_size_word, delete_flag, num_events, num_nodes); + raw_datablk[ j ].SetBuffer(temp_buf, m_size_word, delete_flag, num_events, num_nodes); + + } catch (string err_str) { + printf("Error was detected\n"); fflush(stdout); + break; + } + } + +#ifdef NONSTOP + // Goto Stand-by loop when run is paused or stopped by error + if (m_run_pause != 0 || m_run_error != 0) continue; +#endif + + + /////////////////////////////////////////////////////////////// + // From Serializer.cc + ////////////////////////////////////////////////////////////// + if (m_start_flag == 0) { + m_start_time = getTimeSec(); + n_basf2evt = 0; + } + + // + // Send data + // + for (int j = 0; j < NUM_EVT_PER_BASF2LOOP_PC; j++) { + if (m_start_flag == 0) { + // B2INFO("SerializerPC: Sending the 1st packet..."); + printf("[DEBUG] SerializerPC: Sending the 1st packet...\n"); fflush(stdout); + } + try { + m_sent_totbytes += sendByWriteV(&(raw_datablk[ j ])); + } catch (string err_str) { +#ifdef NONSTOP + break; +#endif + print_err.PrintError((char*)err_str.c_str(), __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); + } + if (m_start_flag == 0) { + // B2INFO("Done. "); + printf("[DEBUG] Done.\n"); fflush(stdout); + m_start_flag = 1; + } + } + +#ifdef NONSTOP + // Goto Stand-by loop when run is paused or stopped by error + if (m_run_pause != 0 || m_run_error != 0) continue; +#endif + + // + // Monitor + // + if (max_nevt >= 0 || max_seconds >= 0.) { +#ifdef AIUEO + if (n_basf2evt % 10000 == 0) { +// if ((n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC >= max_nevt && max_nevt > 0) +// || (getTimeSec() - m_start_time > max_seconds && max_seconds > 0.)) { + printf("[DEBUG] RunStop was detected. ( Setting: Max event # %d MaxTime %lf ) Processed Event %d Elapsed Time %lf[s]\n", + max_nevt , max_seconds, n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC, getTimeSec() - m_start_time); + } +#endif + } + + if ((n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC) % 100000 == 0) { + double interval = cur_time - m_prev_time; + double total_time = cur_time - m_start_time; + printf("[DEBUG] Event %12d Rate %6.2lf[kHz] Recvd %6.2lf[MB/s] sent %6.2lf[MB/s] RunTime %8.2lf[s] interval %8.4lf[s]\n", + n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC, + (n_basf2evt - m_prev_nevt)*NUM_EVT_PER_BASF2LOOP_PC / interval / 1.e3, + (m_recvd_totbytes - m_recvd_prev_totbytes) / interval / 1.e6, + (m_sent_totbytes - m_sent_prev_totbytes) / interval / 1.e6, + total_time, + interval); + fflush(stdout); + + m_prev_time = cur_time; + m_recvd_prev_totbytes = m_recvd_totbytes; + m_sent_prev_totbytes = m_sent_totbytes; + m_prev_nevt = n_basf2evt; + cur_time = getTimeSec(); + } + + n_basf2evt++; + + if (m_status.isAvailable()) { + m_status.setOutputNBytes(m_sent_totbytes); + m_status.setOutputCount(n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC); + } + + } + + return; +} + +///////////////////////////////////////////////////// +// From Serializer.cc +///////////////////////////////////////////////////// + +#ifdef NONSTOP +void DesSerCOPPER::waitResume() +{ + if (m_run_pause == 0) { + while (true) { + if (checkRunPause()) break; +#ifdef NONSTOP_DEBUG + printf("\033[31m"); + printf("###########(DesSerCOPPER) Waiting for Runstop() ###############\n"); + fflush(stdout); + printf("\033[0m"); +#endif + sleep(1); + } + } + + // close COPPER FIFO + if (m_cpr_fd != -1) close(m_cpr_fd); + m_cpr_fd = -1; + + while (true) { +#ifdef NONSTOP_DEBUG + printf("\033[31m"); + printf("###########(Ser) Waiting for Resume ###############\n"); + fflush(stdout); + printf("\033[0m"); +#endif + if (checkRunRecovery()) { + m_run_pause = 0; + m_run_error = 0; + + break; + } + sleep(1); + } + + printf("Done!\n"); fflush(stdout); + + if (CheckConnection(m_socket_send) < 0) { + printf("Trying Accept1\n"); fflush(stdout); + Accept(false); + printf("Trying Accept2\n"); fflush(stdout); + } + + resumeRun(); + return; +} + + + +void DesSerCOPPER::resumeRun() +{ + initializeCOPPER(); +#ifdef NONSTOP_DEBUG + printf("\033[34m"); + printf("###########(Ser) the 1st event sicne the resume ###############\n"); + fflush(stdout); + printf("\033[0m"); +#endif + m_run_error = 0; + m_run_pause = 0; + return; +} + + + +#endif + + + +int* DesSerCOPPER::readOneEventFromCOPPERFIFO(const int entry, int* delete_flag, int* m_size_word) +{ + + // prepare buffer + *m_size_word = 0; + int* temp_buf = m_bufary[ entry ]; + temp_buf[0] = BUF_SIZE_WORD ; + *delete_flag = 0; + + // + // Read data from HSLB + // +#ifndef DUMMY + int recvd_byte = (m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS) * sizeof(int); + // Firstly, read data with an allocated buffer. + while (1) { + int read_size = 0; + if ((read_size = read(m_cpr_fd, (char*)m_bufary[entry] + recvd_byte, sizeof(int) * BUF_SIZE_WORD - recvd_byte)) < 0) { + if (errno == EINTR) { + continue; + } else if (errno == EAGAIN || errno == EWOULDBLOCK) { + + if (recvd_byte > (int)((m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS) * sizeof(int))) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] EAGAIN return in the middle of an event( COPPER driver should't do this.). Exting..."); + print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } + +#ifdef NONSTOP + // Check run-pause request from SLC + string err_str; + callCheckRunPause(err_str); +#endif + continue; + + } else { + char err_buf[500]; + sprintf(err_buf, "[FATAL] Failed to read data from COPPER. Exiting..."); + print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } + } else { + recvd_byte += read_size; + if (recvd_byte - (m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS) * sizeof(int) > (int)(sizeof(int) * + (m_pre_rawcpr.POS_DATA_LENGTH + 1)))break; + } + } + + // + // Calcurate data size + // + *m_size_word = m_bufary[ entry ][ m_pre_rawcpr.POS_DATA_LENGTH + (m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS) ] + + m_pre_rawcpr.SIZE_COPPER_DRIVER_HEADER + m_pre_rawcpr.SIZE_COPPER_DRIVER_TRAILER + + m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS + + m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS; // 9 words are COPPER haeder and trailer size. + + // + // If there are data remaining to be read, continue reading + // + if ((int)((*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) * sizeof(int)) > recvd_byte) { + + // If event size is larger than BUF_SIZE_WORD, allocate a new buffer + if (*m_size_word > BUF_SIZE_WORD) { + *delete_flag = 1; + temp_buf = new int[ *m_size_word ]; + memcpy(temp_buf, m_bufary[ entry ], recvd_byte); + recvd_byte += readFD(m_cpr_fd, (char*)temp_buf + recvd_byte, + (*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) * sizeof(int) - recvd_byte, *delete_flag); + } else { + + recvd_byte += readFD(m_cpr_fd, (char*)(m_bufary[ entry ]) + recvd_byte, + (*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) * sizeof(int) - recvd_byte, *delete_flag); + } + + if ((int)((*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) * sizeof(int)) != recvd_byte) { + char err_buf[500]; + + sprintf(err_buf, "[FATAL] CORRUPTED DATA: Read less bytes(%d) than expected(%d:%d). Exiting...\n", + recvd_byte, + *m_size_word * sizeof(int) - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS * sizeof(int), + m_bufary[ entry ][ m_pre_rawcpr.POS_DATA_LENGTH ]); + print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } + } else if ((int)((*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) * sizeof(int)) < recvd_byte) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] CORRUPTED DATA: Read more than data size. Exiting...: %d %d %d %d %d\n", + recvd_byte, *m_size_word * sizeof(int) , m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS * sizeof(int), + m_bufary[ entry ][ m_pre_rawcpr.POS_DATA_LENGTH ], m_pre_rawcpr.POS_DATA_LENGTH); + print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } +#else + // + // Make dummy data + // + *m_size_word = 256 + entry; + m_bufary[entry][0] = *m_size_word; +#endif + + // + // Fill Data length + // + temp_buf[ 0 ] = *m_size_word; + + +#ifdef TIME_MONITOR + if (n_basf2evt >= 50000 && n_basf2evt < 50500) { + cur_time = getTimeSec(); + time_array2[ n_basf2evt - 50000 ] = cur_time - m_start_time; + } +#endif + +#ifdef CHECK_SUM + unsigned int checksum = 0; + for (int i = 0; i < m_bufary[entry][0]; i++) { + if (i != 2) checksum += m_bufary[entry][i]; + } + m_bufary[entry][2] = checksum; +#endif + return temp_buf; +} + + + +void DesSerCOPPER::initializeCOPPER() +{ + +#ifndef DUMMY + m_use_slot = 0; /* bit mask */ + int slot_shift; + + if ((m_finesse_bit_flag & 0x1) == 1) { + slot_shift = 0; // a:0, b:1, c:2, d:3 + m_use_slot |= 1 << slot_shift; // + } + + if (((m_finesse_bit_flag >> 1) & 0x1) == 1) { + slot_shift = 1; // a:0, b:1, c:2, d:3 + m_use_slot |= 1 << slot_shift; // + } + + if (((m_finesse_bit_flag >> 2) & 0x1) == 1) { + slot_shift = 2; // a:0, b:1, c:2, d:3 + m_use_slot |= 1 << slot_shift; // + } + + if (((m_finesse_bit_flag >> 3) & 0x1) == 1) { + slot_shift = 3; // a:0, b:1, c:2, d:3 + m_use_slot |= 1 << slot_shift; // + } + // + // Present slots to use + // + if (! m_use_slot) { + char err_buf[100] = "[FATAL] Slot is not specified. Exiting..."; + print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); + } else { + int slot; + printf("[DEBUG] "); + for (slot = 0; slot < 4; slot++) { + if (m_use_slot & (1 << slot)) printf(" %c", 'A' + slot); + } + printf("\n"); + } + + +#endif + +#ifndef YAMADA_DUMMY + // B2INFO("Opening COPPER..."); fflush(stderr); + printf("[DEBUG] Opening COPPER...\n"); fflush(stdout); + openCOPPER(); + // B2INFO("Done.\n"); fflush(stderr); + printf("[DEBUG] Done.\n"); fflush(stdout); +#endif +} + + +void DesSerCOPPER::openCOPPER() +{ + + if (m_cpr_fd != -1) { + close(m_cpr_fd); + m_cpr_fd = -1; + } + // + // Open a finesse device + // + if ((m_cpr_fd = open("/dev/copper/copper", O_RDONLY)) == -1) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] Failed to open /dev/copper/copper. Exiting... "); + print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); + } + + int set_regval = 15; // How many events to be stored in COPPER FIFO before request for DMA + // int set_regval=1; + ioctl(m_cpr_fd, CPRIOSET_LEF_WA_FF, &set_regval); + ioctl(m_cpr_fd, CPRIOSET_LEF_WB_FF, &set_regval); + ioctl(m_cpr_fd, CPRIOSET_LEF_WC_FF, &set_regval); + ioctl(m_cpr_fd, CPRIOSET_LEF_WD_FF, &set_regval); + ioctl(m_cpr_fd, CPRIOSET_FINESSE_STA, &m_use_slot, sizeof(m_use_slot)); + + int v = 511 - 32; + + ioctl(m_cpr_fd, CPRIOSET_LEF_WA_AF, &v, sizeof(v)); + ioctl(m_cpr_fd, CPRIOSET_LEF_WB_AF, &v, sizeof(v)); + ioctl(m_cpr_fd, CPRIOSET_LEF_WC_AF, &v, sizeof(v)); + ioctl(m_cpr_fd, CPRIOSET_LEF_WD_AF, &v, sizeof(v)); + + + // B2INFO("DeSerializerCOPPER: openCOPPER() done."); + printf("[DEBUG] DeSerializerCOPPER: openCOPPER() done.\n"); fflush(stdout); + +} + + + +int DesSerCOPPER::readFD(int fd, char* buf, int data_size_byte, int delete_flag) +{ + + int n = 0; + int read_size = 0; + while (1) { + if ((read_size = read(fd, (char*)buf + n, data_size_byte - n)) < 0) { + if (errno == EINTR) { + continue; + } else if (errno == EAGAIN || errno == EWOULDBLOCK) { + if (n > 0) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] Return due to EAGAIN in the middle of an event( COPPER driver would't do this.). Exting..."); + print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } +#ifdef NONSTOP + // Check run-pause request from SLC + string err_str; + try { + callCheckRunPause(err_str); + } catch (string err_str) { + if (delete_flag) { + // Delete allocated buffer + // B2WARNING("Delete buffer before going to Run-pause state"); + printf("[WARNING] Delete buffer before going to Run-pause state\n"); fflush(stdout); + delete buf; + } + throw (err_str); + } +#endif + continue; + } else { +#ifdef NONSTOP + m_run_error = 1; + // B2ERROR(err_buf); + printf("[ERROR] Failed to read data from COPPER. %s %s %d", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + string err_str = "RUN_ERROR"; + throw (err_str); +#endif + char err_buf[500]; + sprintf(err_buf, "[FATAL] Failed to read data from COPPER. %s %s %d", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } + } else { + n += read_size; + if (n == data_size_byte)break; + } + } + return n; +} diff --git a/ROPCApplications/daq/rawdata/src/DesSerPrePC.cc b/ROPCApplications/daq/rawdata/src/DesSerPrePC.cc new file mode 100644 index 0000000000000000000000000000000000000000..fff12ba885a337c4bba0131b3dba4fa9a1c2db7e --- /dev/null +++ b/ROPCApplications/daq/rawdata/src/DesSerPrePC.cc @@ -0,0 +1,831 @@ +//+ +// File : DesSerPrePC.cc +// Description : Module to receive data from eb0 and send it to eb1tx +// +// Author : Satoru Yamada Itoh, IPNS, KEK +// Date : 2 - Aug - 2013 +//- +#include <daq/rawdata/DesSerPrePC.h> +#include <rawdata/dataobjects/RawFTSWFormat_latest.h> +#include <rawdata/dataobjects/RawTLUFormat.h> + +#include <arpa/inet.h> +#include <netdb.h> +#include <netinet/tcp.h> + +#include <sys/socket.h> + +//#define NO_DATA_CHECK +//#define DUMHSLB + +using namespace std; +using namespace Belle2; + +//---------------------------------------------------------------- +// Implementation +//---------------------------------------------------------------- +DesSerPrePC::DesSerPrePC(string host_recv, int port_recv, string host_send, int port_send, int shmflag, + const std::string& nodename, int nodeid) +{ + + for (int i = 0 ; i < m_num_connections; i++) { + // m_hostname_from.push_back( "localhost"); + m_hostname_from.push_back(host_recv); + // m_port_from.push_back(30000); + m_port_from.push_back(port_recv) ; + m_socket_recv.push_back(-1); + } + + // m_port_to = 31001; + m_port_to = port_send; + // m_hostname_local = "localhost"; + m_hostname_local = host_send; + m_nodename = nodename; + + m_shmflag = shmflag; + + // B2INFO("DeSerializerPrePC: Constructor done."); + printf("[INFO] DeSerializerPrePC: Constructor done.\n"); fflush(stdout); +} + + + +DesSerPrePC::~DesSerPrePC() +{ +} + + + +int DesSerPrePC::recvFD(int sock, char* buf, int data_size_byte, int flag) +{ + int n = 0; + int read_size = 0; + while (1) { + if ((read_size = recv(sock, (char*)buf + n, data_size_byte - n , flag)) < 0) { + if (errno == EINTR) { + continue; + } else if (errno == EAGAIN || errno == EWOULDBLOCK) { +#ifdef NONSTOP + string err_str; + callCheckRunPause(err_str); +#endif + continue; + } else { + perror("[WARNING]"); + char err_buf[500]; + sprintf(err_buf, "recv() returned error; ret = %d. : %s %s %d", + read_size, __FILE__, __PRETTY_FUNCTION__, __LINE__); +#ifdef NONSTOP + m_run_error = 1; + // B2ERROR(err_buf); + printf("[WARNING] %s\n", err_buf); fflush(stdout); + string err_str = "RUN_ERROR"; + printf("AIUEO********************\n"); fflush(stdout); + throw (err_str); +#endif + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); + } + } else if (read_size == 0) { + // Connection is closed ( error ) + char err_buf[500]; + sprintf(err_buf, "[WARNING] Connection is closed by peer(%s). readsize = %d %d. : %s %s %d", + strerror(errno), read_size, errno, __FILE__, __PRETTY_FUNCTION__, __LINE__); +#ifdef NONSTOP + m_run_error = 1; + // B2ERROR(err_buf); + printf("%s\n", err_buf); fflush(stdout); + string err_str = "RUN_ERROR"; + throw (err_str); +#else + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(-1); +#endif + } else { + n += read_size; + if (n == data_size_byte)break; + } + } + return n; +} + + +int DesSerPrePC::Connect() +{ + + for (int i = 0; i < m_num_connections; i++) { + + if (m_socket_recv[ i ] >= 0) continue; // Already have an established socket + + // + // Connect to a downstream node + // + struct hostent* host; + host = gethostbyname(m_hostname_from[ i ].c_str()); + if (host == NULL) { + char err_buf[100]; + sprintf(err_buf, "[FATAL] hostname(%s) cannot be resolved(%s). Check /etc/hosts. Exiting...", m_hostname_from[ i ].c_str(), + strerror(errno)); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(1); + } + + struct sockaddr_in socPC; + socPC.sin_family = AF_INET; + socPC.sin_addr.s_addr = *(unsigned int*)host->h_addr_list[0]; + socPC.sin_port = htons(m_port_from[ i ]); + int sd = socket(PF_INET, SOCK_STREAM, 0); + int val1 = 0; + setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, &val1, sizeof(val1)); + + struct timeval timeout; + timeout.tv_sec = 1; + timeout.tv_usec = 0; + setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, (socklen_t)sizeof(timeout)); + + printf("[DEBUG] Connecting to %s port %d\n" , m_hostname_from[ i ].c_str(), m_port_from[ i ]); fflush(stdout); + + while (1) { + if (connect(sd, (struct sockaddr*)(&socPC), sizeof(socPC)) < 0) { + perror("Failed to connect. Retrying..."); + usleep(500000); + } else { + // B2INFO("Done"); + printf("[DEBUG] Done\n"); fflush(stdout); + break; + } + } + // m_socket_recv.push_back(sd); + m_socket_recv[ i ] = sd; + + // check socket paramters + int val, len; + len = sizeof(val); + getsockopt(m_socket_recv[ i ], SOL_SOCKET, SO_RCVBUF, &val, (socklen_t*)&len); + // B2INFO("SO_RCVBUF" << val); + getsockopt(m_socket_recv[ i ], SOL_SOCKET, SO_SNDBUF, &val, (socklen_t*)&len); + // B2DEBUG("SO_SNDBUF" << val); + getsockopt(m_socket_recv[ i ], IPPROTO_TCP, TCP_MAXSEG, &val, (socklen_t*)&len); + // B2DEBUG("TCP_MAXSEG" << val); + getsockopt(m_socket_recv[ i ], IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t*)&len); + // B2DEBUG("TCP_NODELAY" << val); + + if (m_status.isAvailable()) { + sockaddr_in sa; + memset(&sa, 0, sizeof(sockaddr_in)); + socklen_t sa_len = sizeof(sa); + if (getsockname(m_socket_recv[i], (struct sockaddr*)&sa, (socklen_t*)&sa_len) == 0) { + m_status.setInputPort(ntohs(sa.sin_port)); + m_status.setInputAddress(sa.sin_addr.s_addr); + } + } + + } + // B2INFO("[DEBUG] Initialization finished"); + printf("[DEBUG] Initialization finished\n"); fflush(stdout); + return 0; +} + + + +int* DesSerPrePC::recvData(int* delete_flag, int* total_buf_nwords, int* num_events_in_sendblock, int* num_nodes_in_sendblock) +{ + int* temp_buf = NULL; // buffer for data-body + int flag = 0; + + vector <int> each_buf_nwords; + each_buf_nwords.clear(); + vector <int> each_buf_nodes; + each_buf_nodes.clear(); + vector <int> each_buf_events; + each_buf_events.clear(); + + *total_buf_nwords = 0; + *num_nodes_in_sendblock = 0; + *num_events_in_sendblock = 0; + + // + // Read Header and obtain data size + // + int send_hdr_buf[ SendHeader::SENDHDR_NWORDS ]; + int temp_num_events = 0; + int temp_num_nodes = 0; + + // Read header + for (int i = 0; i < (int)(m_socket_recv.size()); i++) { + + recvFD(m_socket_recv[ i ], (char*)send_hdr_buf, sizeof(int)*SendHeader::SENDHDR_NWORDS, flag); + + SendHeader send_hdr; + send_hdr.SetBuffer(send_hdr_buf); + + temp_num_events = send_hdr.GetNumEventsinPacket(); + temp_num_nodes = send_hdr.GetNumNodesinPacket(); + + if (i == 0) { + *num_events_in_sendblock = temp_num_events; + } else if (*num_events_in_sendblock != temp_num_events) { +#ifndef NO_DATA_CHECK + char err_buf[500]; + sprintf(err_buf, + "[FATAL] CORRUPTED DATA: Different # of events or nodes in SendBlocks( # of eve : %d(socket 0) %d(socket %d), # of nodes: %d(socket 0) %d(socket %d). Exiting...\n", + *num_events_in_sendblock , temp_num_events, i, *num_nodes_in_sendblock , temp_num_nodes, i); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(1); +#endif + } + + *num_nodes_in_sendblock += temp_num_nodes; + + int rawblk_nwords = send_hdr.GetTotalNwords() + - SendHeader::SENDHDR_NWORDS + - SendTrailer::SENDTRL_NWORDS; + *total_buf_nwords += rawblk_nwords; + + // + // Data size check1 + // + if (rawblk_nwords > (int)(2.5e6) || rawblk_nwords <= 0) { + printData(send_hdr_buf, SendHeader::SENDHDR_NWORDS); + char err_buf[500]; + sprintf(err_buf, "CORRUPTED DATA: Too large event : Header %d %d %d %d :block size %d words\n", i, temp_num_events, temp_num_nodes, + send_hdr.GetTotalNwords(), rawblk_nwords); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(123456); + exit(1); + + } + + each_buf_nwords.push_back(rawblk_nwords); + each_buf_events.push_back(temp_num_events); + each_buf_nodes.push_back(temp_num_nodes); + + } + + + temp_buf = getNewBuffer(*total_buf_nwords, delete_flag); // this include only data body + // + // Read body + // + int total_recvd_byte = 0; + for (int i = 0; i < (int)(m_socket_recv.size()); i++) { + + try { + total_recvd_byte += recvFD(m_socket_recv[ i ], (char*)temp_buf + total_recvd_byte, + each_buf_nwords[ i ] * sizeof(int), flag); + } catch (string err_str) { + if (*delete_flag) { + // B2WARNING("Delete buffer before going to Run-pause state"); + printf("[WARNING] Delete buffer before going to Run-pause state\n"); fflush(stdout); + delete temp_buf; + } + throw (err_str); + } + // + // Data length check + // + int temp_length = 0; + for (int j = 0; j < each_buf_nodes[ i ] * each_buf_events[ i ]; j++) { + int this_length = *((int*)((char*)temp_buf + total_recvd_byte - each_buf_nwords[ i ] * sizeof(int) + temp_length)); + temp_length += this_length * sizeof(int); + } + if (temp_length != (int)(each_buf_nwords[ i ] * sizeof(int))) { + printf("[DEBUG]*******SENDHDR*********** \n"); + printData(send_hdr_buf, SendHeader::SENDHDR_NWORDS); + printf("[DEBUG]*******BODY***********\n "); + printData(temp_buf, (int)(total_recvd_byte / sizeof(int))); + char err_buf[500]; + sprintf(err_buf, "CORRUPTED DATA: Length written on SendHeader(%d) is invalid. Actual data size is %d. Exting...", + (int)(*total_buf_nwords * sizeof(int)), temp_length); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(-1); + } + + } + + if ((int)(*total_buf_nwords * sizeof(int)) != total_recvd_byte) { + char err_buf[500]; + sprintf(err_buf, "CORRUPTED DATA: Received data size (%d byte) is not same as expected one (%d) from Sendheader. Exting...", + total_recvd_byte, (int)(*total_buf_nwords * sizeof(int))); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(-1); + } + + // Read Traeiler + int send_trl_buf[(unsigned int)(SendTrailer::SENDTRL_NWORDS) ]; + for (int i = 0; i < (int)(m_socket_recv.size()); i++) { + try { + recvFD(m_socket_recv[ i ], (char*)send_trl_buf, SendTrailer::SENDTRL_NWORDS * sizeof(int), flag); + } catch (string err_str) { + if (*delete_flag) { + // B2WARNING("Delete buffer before going to Run-pause state"); + printf("[WARNING] Delete buffer before going to Run-pause state\n"); fflush(stdout); + delete temp_buf; + } + throw (err_str); + } + } + + return temp_buf; +} + + +void DesSerPrePC::setRecvdBuffer(RawDataBlockFormat* temp_raw_datablk, int* delete_flag) +{ + // + // Get data from socket + // + int total_buf_nwords = 0 ; + int num_events_in_sendblock = 0; + int num_nodes_in_sendblock = 0; + + if (m_start_flag == 0) { + // B2INFO("DeSerializerPrePC: Reading the 1st packet from eb0..."); + printf("DeSerializerPrePC: Reading the 1st packet from eb0...\n"); fflush(stdout); + } + int* temp_buf = recvData(delete_flag, &total_buf_nwords, &num_events_in_sendblock, + &num_nodes_in_sendblock); + if (m_start_flag == 0) { + // B2INFO("DeSerializerPrePC: Done. the size of the 1st packet " << total_buf_nwords << " words"); + printf("DeSerializerPrePC: Done. the size of the 1st packet %d words\n", total_buf_nwords); fflush(stdout); + m_start_flag = 1; + } + m_recvd_totbytes += total_buf_nwords * sizeof(int); + + temp_raw_datablk->SetBuffer((int*)temp_buf, total_buf_nwords, *delete_flag, + num_events_in_sendblock, num_nodes_in_sendblock); + + // + // check even # and node # in one Sendblock + // + int num_entries = temp_raw_datablk->GetNumEntries(); + if (num_entries != num_events_in_sendblock * num_nodes_in_sendblock) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] CORRUPTED DATA: Inconsistent SendHeader value. # of nodes(%d) times # of events(%d) differs from # of entries(%d). Exiting...", + num_nodes_in_sendblock, num_events_in_sendblock, num_entries); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(-1); + } + return; + +} + + + + +void DesSerPrePC::checkData(RawDataBlockFormat* raw_datablk, unsigned int* eve_copper_0) +{ + // int data_size_copper_0 = -1; + // int data_size_copper_1 = -1; + + // + // Data check + // + int* temp_buf = raw_datablk->GetBuffer(0); + int cpr_num = 0; + unsigned int cur_evenum = 0, cur_copper_ctr = 0; + unsigned int eve_array[32]; // # of noeds is less than 17 + unsigned int utime_array[32];// # of noeds is less than 17 + unsigned int ctime_type_array[32];// # of noeds is less than 17 + +#ifdef DUMHSLB + unsigned int exp_run_ftsw = 0, ctime_trgtype_ftsw = 0, utime_ftsw = 0; +#endif + + for (int k = 0; k < raw_datablk->GetNumEvents(); k++) { + memset(eve_array, 0, sizeof(eve_array)); + memset(utime_array, 0, sizeof(utime_array)); + memset(ctime_type_array, 0, sizeof(ctime_type_array)); + + int num_nodes_in_sendblock = raw_datablk->GetNumNodes(); + for (int l = 0; l < num_nodes_in_sendblock; l++) { + int entry_id = l + k * num_nodes_in_sendblock; + + // + // RawFTSW + // + if (raw_datablk->CheckFTSWID(entry_id)) { + RawFTSWFormat_latest* temp_rawftsw = new RawFTSWFormat_latest; + int block_id = 0; + temp_rawftsw->SetBuffer((int*)temp_buf + raw_datablk->GetBufferPos(entry_id), + raw_datablk->GetBlockNwords(entry_id), 0, 1, 1); + if (temp_rawftsw->GetEveNo(block_id) < 10) { + printf("[DEBUG] ######FTSW#########\n"); + printData((int*)temp_buf + raw_datablk->GetBufferPos(entry_id), raw_datablk->GetBlockNwords(entry_id)); + } + +#ifdef DUMHSLB + exp_run_ftsw = temp_rawftsw->GetExpRunSubrun(block_id); + ctime_trgtype_ftsw = temp_rawftsw->GetTTCtimeTRGType(block_id); + utime_ftsw = temp_rawftsw->GetTTUtime(block_id); +#endif + + +#ifndef NO_DATA_CHECK + try { + temp_rawftsw->CheckData(0, m_prev_evenum, &cur_evenum, m_prev_exprunsubrun_no, &m_exprunsubrun_no); + eve_array[ entry_id ] = cur_evenum; + } catch (string err_str) { + char err_buf[500]; + strcpy(err_buf, err_str.c_str()); + print_err.PrintError(err_buf , __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); + } +#endif + utime_array[ entry_id ] = temp_rawftsw->GetTTUtime(block_id); + ctime_type_array[ entry_id ] = temp_rawftsw->GetTTCtimeTRGType(block_id); + delete temp_rawftsw; + + // + // RawTLU + // + } else if (raw_datablk->CheckTLUID(entry_id)) { + + RawTLUFormat* temp_rawtlu = new RawTLUFormat; + temp_rawtlu->SetBuffer((int*)temp_buf + raw_datablk->GetBufferPos(entry_id), + raw_datablk->GetBlockNwords(entry_id), 0, 1, 1); + if (temp_rawtlu->GetEveNo(0) < 10 + ) { + printf("[DEBUG] ######TLU#########\n"); + printData((int*)temp_buf + raw_datablk->GetBufferPos(entry_id), raw_datablk->GetBlockNwords(entry_id)); + } + +#ifndef NO_DATA_CHECK + try { + temp_rawtlu->CheckData(0, m_prev_evenum, &cur_evenum); + eve_array[ entry_id ] = cur_evenum; + } catch (string err_str) { + char err_buf[500]; + strcpy(err_buf, err_str.c_str()); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); + } +#endif + delete temp_rawtlu; + } else { + // + // RawCOPPER + // + PreRawCOPPERFormat_latest* pre_rawcpr_fmt = new PreRawCOPPERFormat_latest; + pre_rawcpr_fmt->SetBuffer((int*)temp_buf + raw_datablk->GetBufferPos(entry_id), + raw_datablk->GetBlockNwords(entry_id), 0, 1, 1); + +#ifdef DUMHSLB + int block_id = 0; + "do not use the following for actual DAQ" + (pre_rawcpr_fmt->GetBuffer(block_id))[ RawHeader_latest::POS_EXP_RUN_NO ] = exp_run_ftsw; + (pre_rawcpr_fmt->GetBuffer(block_id))[ RawHeader_latest::POS_TTCTIME_TRGTYPE ] = ctime_trgtype_ftsw; + (pre_rawcpr_fmt->GetBuffer(block_id))[ RawHeader_latest::POS_TTUTIME ] = utime_ftsw; +#endif + +#ifndef NO_DATA_CHECK + try { + pre_rawcpr_fmt->CheckData(0, m_prev_evenum, &cur_evenum, + m_prev_copper_ctr, &cur_copper_ctr, + m_prev_exprunsubrun_no, &m_exprunsubrun_no); + eve_array[ entry_id ] = cur_evenum; + } catch (string err_str) { + exit(1); // Error in the contents of an event was detected + } +#endif + + utime_array[ entry_id ] = pre_rawcpr_fmt->GetTTUtime(0); + ctime_type_array[ entry_id ] = pre_rawcpr_fmt->GetTTCtimeTRGType(0); + + if (cpr_num == 0) { + // data_size_copper_0 = raw_datablk->GetBlockNwords(entry_id); + *eve_copper_0 = (raw_datablk->GetBuffer(entry_id))[ 3 ]; + } else if (cpr_num == 1) { + // data_size_copper_1 = raw_datablk->GetBlockNwords(entry_id); + } + cpr_num++; + delete pre_rawcpr_fmt; + } + } + +#ifndef NO_DATA_CHECK + // event #, ctime, utime over nodes + for (int l = 1; l < num_nodes_in_sendblock; l++) { + if (eve_array[ 0 ] != eve_array[ l ] || + utime_array[ 0 ] != utime_array[ l ] || + ctime_type_array[ 0 ] != ctime_type_array[ l ]) { + char err_buf[500]; + for (int m = 0; m < num_nodes_in_sendblock; m++) { + printf("[DEBUG] node %d eve # %d utime %x ctime %x\n", + m, eve_array[ m ], utime_array[ m ], ctime_type_array[ m ]); + } + sprintf(err_buf, "[FATAL] CORRUPTED DATA: Event or Time record mismatch. Exiting..."); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + sleep(1234567); + exit(-1); + } + } +#endif + + // Event # monitor in runchange +// if (m_prev_runsubrun_no != m_runsubrun_no) { +// printf("[DEBUG] ##############################################\n"); +// for (int m = 0; m < raw_datablk->GetNumEntries(); m++) { +// printf("[DEBUG] %d eve %u prev %u\n", m, eve_array[ m ], m_prev_evenum); +// } +// printf("[DEBUG] ##############################################\n"); +// fflush(stderr); +// } + m_prev_evenum = cur_evenum; + m_prev_copper_ctr = cur_copper_ctr; + m_prev_exprunsubrun_no = m_exprunsubrun_no; + } + return; +} + + + +void DesSerPrePC::DataAcquisition() +{ + // For data check + unsigned int eve_copper_0 = 0; + // B2INFO("initializing..."); + printf("[DEBUG] initializing...\n"); fflush(stdout); + initialize(); + + // B2INFO("Done."); + printf("[DEBUG] Done.\n"); fflush(stdout); + + if (m_start_flag == 0) { + // + // Connect to eb0: This should be here because we want Serializer first to accept connection from eb1tx + // + Connect(); + if (m_status.isAvailable()) { + // B2INFO("DeSerializerPrePC: Waiting for Start...\n"); + printf("[DEBUG] DeSerializerPrePC: Waiting for Start...\n"); fflush(stdout); + m_status.reportRunning(); + } + m_start_time = getTimeSec(); + n_basf2evt = 0; + } + + // + // Main loop + // + while (1) { + // + // Stand-by loop + // +#ifdef NONSTOP + if (m_run_pause > 0 || m_run_error > 0) { + waitResume(); + } +#endif + + clearNumUsedBuf(); + // RawDataBlock raw_datablk[ NUM_EVT_PER_BASF2LOOP_PC ]; + RawDataBlockFormat raw_datablk[ NUM_EVT_PER_BASF2LOOP_PC ]; + + // + // Recv loop + // + for (int j = 0; j < NUM_EVT_PER_BASF2LOOP_PC; j++) { + // + // Receive data from COPPER + // + eve_copper_0 = 0; + int delete_flag_from = + 0; // Delete flag for temp_rawdatablk.It can be set to 1 by setRecvdBuffer if the buffer size is larger than that of pre-allocated buffer. + int delete_flag_to = + 0; // Delete flag for raw_datablk[i]. It can be set to 1 by getNewBuffer if the buffer size is larger than that of pre-allocated buffer. + RawDataBlockFormat temp_rawdatablk; + try { + setRecvdBuffer(&temp_rawdatablk, &delete_flag_from); + checkData(&temp_rawdatablk, &eve_copper_0); + } catch (string err_str) { + printf("Error was detected\n"); fflush(stdout); + break; + } + // PreRawCOPPERFormat_latest pre_rawcopper_latest; + // pre_rawcopper_latest.SetBuffer((int*)temp_rawdatablk.GetWholeBuffer(), temp_rawdatablk.TotalBufNwords(), + // 0, temp_rawdatablk.GetNumEvents(), temp_rawdatablk.GetNumNodes()); + //// pre_rawcopper_latest.CheckCRC16( 0, 0 ); + + int temp_num_events, temp_num_nodes; + int temp_nwords_to; + int* buf_to = NULL; +#ifdef REDUCED_RAWCOPPER + // + // Copy reduced buffer + // + // int* buf_to = getNewBuffer(m_pre_rawcpr.CalcReducedDataSize(&temp_rawdatablk), &delete_flag_to); // basf2-dependent style + int* temp_bufin = temp_rawdatablk.GetWholeBuffer(); + int temp_nwords_from = temp_rawdatablk.TotalBufNwords(); + temp_num_events = temp_rawdatablk.GetNumEvents(); + temp_num_nodes = temp_rawdatablk.GetNumNodes(); + int calced_temp_nwords_to = m_pre_rawcpr.CalcReducedDataSize(temp_bufin, temp_nwords_from, temp_num_events, temp_num_nodes); + buf_to = getNewBuffer(calced_temp_nwords_to, &delete_flag_to); + + // m_pre_rawcpr.CopyReducedData(&temp_rawdatablk, buf_to, delete_flag_from); // basf2-dependent style + m_pre_rawcpr.CopyReducedData(temp_bufin, temp_nwords_from, temp_num_events, temp_num_nodes, buf_to, &temp_nwords_to); + if (calced_temp_nwords_to != temp_nwords_to) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] CORRUPTED DATA: Estimations of reduced event size are inconsistent. CalcReducedDataSize = %d. CopyReducedData = %d. Exiting...", + calced_temp_nwords_to, temp_nwords_to); + print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); + } + m_status.copyEventHeader(buf_to); +#else + buf_to = temp_rawdatablk.GetWholeBuffer(); + temp_nwords_to = temp_rawdatablk.TotalBufNwords(); + temp_num_events = temp_rawdatablk.GetNumEvents(); + temp_num_nodes = temp_rawdatablk.GetNumNodes(); + delete_flag_to = delete_flag_from; + delete_flag_from = 0; // to avoid double delete +#endif + + // + // Set buffer to the RawData class stored in DataStore + // +// raw_datablk[ j ].SetBuffer( (int*)temp_rawdatablk.GetWholeBuffer(), temp_rawdatablk.TotalBufNwords(), +// delete_flag_to, temp_rawdatablk.GetNumEvents(), +// temp_rawdatablk.GetNumNodes()); + raw_datablk[ j ].SetBuffer(buf_to, temp_nwords_to, delete_flag_to, temp_num_events, temp_num_nodes); + + + // + // CRC16 check after data reduction + // +#ifdef REDUCED_RAWCOPPER + PostRawCOPPERFormat_latest post_rawcopper_latest; + +// post_rawcopper_latest.SetBuffer((int*)temp_rawdatablk.GetWholeBuffer(), temp_rawdatablk.TotalBufNwords(), +// 0, temp_rawdatablk.GetNumEvents(), temp_rawdatablk.GetNumNodes()); + post_rawcopper_latest.SetBuffer(raw_datablk[ j ].GetWholeBuffer(), raw_datablk[ j ].TotalBufNwords(), + 0, raw_datablk[ j ].GetNumEvents(), raw_datablk[ j ].GetNumNodes()); + + for (int i_finesse_num = 0; i_finesse_num < 4; i_finesse_num ++) { + int block_num = 0; + if (post_rawcopper_latest.GetFINESSENwords(block_num, i_finesse_num) > 0) { + post_rawcopper_latest.CheckCRC16(block_num, i_finesse_num); + } + } + +#endif + } + +#ifdef NONSTOP + // Goto Stand-by loop when run is paused or stopped by error + if (m_run_pause != 0 || m_run_error != 0) continue; +#endif + + + /////////////////////////////////////////////////////////////// + // From Serializer.cc + ////////////////////////////////////////////////////////////// + if (m_start_flag == 0) { + m_start_time = getTimeSec(); + n_basf2evt = 0; + } + // StoreArray<RawCOPPER> rawcprarray; + // StoreArray<RawDataBlock> raw_dblkarray; + + for (int j = 0; j < NUM_EVT_PER_BASF2LOOP_PC; j++) { + + // + // Send data + // + if (m_start_flag == 0) { + // B2INFO("SerializerPC: Sending the 1st packet..."); + printf("[DEBUG] SerializerPC: Sending the 1st packet...\n"); fflush(stdout); + } + + try { + m_sent_totbytes += sendByWriteV(&(raw_datablk[ j ])); + } catch (string err_str) { +#ifdef NONSTOP + break; +#endif + print_err.PrintError((char*)err_str.c_str(), __FILE__, __PRETTY_FUNCTION__, __LINE__); + exit(1); + } + if (m_start_flag == 0) { + // B2INFO("Done. "); + printf("[DEBUG] Done.\n"); fflush(stdout); + m_start_flag = 1; + } + } + +#ifdef NONSTOP + // Goto Stand-by loop when run is paused or stopped by error + if (m_run_pause != 0 || m_run_error != 0) continue; +#endif + + // + // Monitor + // + if (max_nevt >= 0 || max_seconds >= 0.) { +#ifdef AIUEO + if (n_basf2evt % 10000 == 0) { +// if ((n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC >= max_nevt && max_nevt > 0) +// || (getTimeSec() - m_start_time > max_seconds && max_seconds > 0.)) { + printf("[DEBUG] RunStop was detected. ( Setting: Max event # %d MaxTime %lf ) Processed Event %d Elapsed Time %lf[s]\n", + max_nevt , max_seconds, n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC, getTimeSec() - m_start_time); + } +#endif + } + + if ((n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC) % 100000 == 0) { + double interval = cur_time - m_prev_time; + double total_time = cur_time - m_start_time; + printf("[DEBUG] Event %12d Rate %6.2lf[kHz] Recvd %6.2lf[MB/s] sent %6.2lf[MB/s] RunTime %8.2lf[s] interval %8.4lf[s]\n", + n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC, + (n_basf2evt - m_prev_nevt)*NUM_EVT_PER_BASF2LOOP_PC / interval / 1.e3, + (m_recvd_totbytes - m_recvd_prev_totbytes) / interval / 1.e6, + (m_sent_totbytes - m_sent_prev_totbytes) / interval / 1.e6, + total_time, + interval); + fflush(stdout); + + m_prev_time = cur_time; + m_recvd_prev_totbytes = m_recvd_totbytes; + m_sent_prev_totbytes = m_sent_totbytes; + m_prev_nevt = n_basf2evt; + cur_time = getTimeSec(); + } + + n_basf2evt++; + + if (m_status.isAvailable()) { + m_status.setOutputNBytes(m_sent_totbytes); + m_status.setOutputCount(n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC); + } + + } + + return; +} + +///////////////////////////////////////////////////// +// From Serializer.cc +///////////////////////////////////////////////////// + + +#ifdef NONSTOP +void DesSerPrePC::waitResume() +{ + if (m_run_pause == 0) { + while (true) { + if (checkRunPause()) break; +#ifdef NONSTOP_DEBUG + printf("\033[31m"); + printf("###########(DesSerPrePC) Waiting for Runstop() ###############\n"); + fflush(stdout); + printf("\033[0m"); +#endif + sleep(1); + } + } + + while (true) { +#ifdef NONSTOP_DEBUG + printf("\033[31m"); + printf("###########(Ser) Waiting for Resume ###############\n"); + fflush(stdout); + printf("\033[0m"); +#endif + if (checkRunRecovery()) { + m_run_pause = 0; + break; + } + sleep(1); + } + + + printf("Done!\n"); fflush(stdout); + + printf("Checking connection to eb0\n"); fflush(stdout); + if (CheckConnection(m_socket_send) < 0) { + printf("Trying Accept1\n"); fflush(stdout); + Accept(); + printf("Trying Accept2\n"); fflush(stdout); + } + + printf("Checking connection to COPPER\n"); fflush(stdout); + for (int i = 0; i < m_num_connections; i++) { + if (CheckConnection(m_socket_recv[ i ]) < 0) m_socket_recv[ i ] = -1; + } + Connect(); + + resumeRun(); + return; +} + + + +#endif + diff --git a/ROPCApplications/daq/ropc/Makefile b/ROPCApplications/daq/ropc/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..23647c65ded1e8ce259fcfa73f7fe73f9eff1da2 --- /dev/null +++ b/ROPCApplications/daq/ropc/Makefile @@ -0,0 +1,102 @@ +TEMP_LOCAL_DIR=$(PWD)/../../ + + +CXX = g++ +INCDIR = ./include +PCIE40_INCDIR = -I$(TEMP_LOCAL_DIR)/../Pcie40Driver -I$(TEMP_LOCAL_DIR)/../Pcie40Applications +CFLAGS = -g -O3 -Wall -fPIC -I${INCDIR} ${PCIE40_INCDIR} +#LOCALLIB = CPRROPCDAQ +LIBFLAG = -lrt -lpthread#-l${LOCALLIB} + +RAWDATAPATH=${TEMP_LOCAL_DIR}/rawdata/src:${TEMP_LOCAL_DIR}/rawdata/dataobjects/src +DAQPATH=${TEMP_LOCAL_DIR}/daq/dataobjects/src:${TEMP_LOCAL_DIR}/daq/rawdata/src +SLCPATH=${TEMP_LOCAL_DIR}/daq/slc/readout/src:${TEMP_LOCAL_DIR}/daq/slc/system/src:${TEMP_LOCAL_DIR}/daq/slc/base/src +VPATH = ${RAWDATAPATH}:${DAQPATH}:${SLCPATH} + +RAWDATAOBJS = RawDataBlockFormat.o RawCOPPERFormat.o RawCOPPERFormat_latest.o PreRawCOPPERFormat_latest.o PostRawCOPPERFormat_latest.o RawHeader_latest.o RawTrailer_latest.o RawTLUFormat.o RawFTSWFormat.o RawFTSWFormat_latest.o CRCCalculator.o +DAQOBJS = DesSer.o DesSerPrePC.o DesSerCOPPER.o CprErrorMessage.o SendHeader.o SendTrailer.o +SLCOBJS = RunInfoBuffer.o SharedMemory.o MMutex.o MCond.o LogFile.o Mutex.o Date.o ConfigFile.o StringUtil.o +OBJS = ${RAWDATAOBJS} ${DAQOBJS} ${SLCOBJS} + +LIBDIR=$(BELLE2_LOCAL_DIR)/lib + +TARGET1=des_ser_ROPC_main +TARGET2=des_ser_COPPER_main + +# +# PCIe40 libraries +# +PCIE40OBJS = ../../../Pcie40Applications/pcie40_ul_lib.out/pcie40_ul_lib.o +PCIE40_LIBFLAGS = -L../../../Pcie40Libraries/lib -lpcie40 -L../../../Pcie40DriverLibraries/ -lpcie40driver_ecs + + +all: clean link ${TARGET1} ${TARGET2} cleanobj + +ropc: clean_ropc link ${TARGET1} cleanobj + +copper: clean_copper link ${TARGET2} cleanobj + @ echo "!!!!!!! Martin added overrides to reduce cppcheck warnings (https://stash.desy.de/projects/B2/repos/software/pull-requests/2825/overview), which leads to failure of compiling on COPPER. To compile on a COPPER, please run the following commands to remove override in rawdata classes" + @ echo "!!!!!!! cd rawdata/dataobjects/include; sed -i -e 's/ override;/;/g' *.h" + + +${TARGET1}: ${TARGET1}.cc ${OBJS} ${PCIE40OBJS} + $(CXX) $(CFLAGS) ${LIBFLAG} ${PCIE40_LIBFLAGS} -L./ $^ -o $@ + +${TARGET2}: ${TARGET2}.cc ${OBJS} ${PCIE40OBJS} + $(CXX) $(CFLAGS) ${LIBFLAG} ${PCIE40_LIBFLAGS} -L./ $^ -o $@ + +$(OBJS): %.o : %.cc + @ echo "<< compiling $@ >>" + @ $(CXX) -c $(CFLAGS) $< + + +#${LOCALLIB}: $(OBJS) +# @ echo "<< linking shared library $@ >>" +# @ $(CXX) -shared $(CFLAGS) $^ -o lib$@.so + +#.c.o: +# $(CXX) $(CFLAGS) -c ${INC} $< + +.PHONY: link +link: + @ echo "<< making smylink $@ >>" + @if [ ! -d ${INCDIR} ]; then \ + mkdir -p include/daq/slc/system ; \ + mkdir -p include/daq/slc/readout ; \ + mkdir -p include/daq/slc/base ; \ + mkdir -p include/daq/rawdata ; \ + mkdir -p include/rawdata/dataobjects ; \ + mkdir -p include/daq/rawdata/modules ; \ + mkdir -p include/daq/dataobjects ; \ + ln -s ${TEMP_LOCAL_DIR}/rawdata/dataobjects/include/*.h ./include/rawdata/dataobjects/ ; \ + ln -s ${TEMP_LOCAL_DIR}/rawdata/include/*.h ./include/rawdata/ ; \ + ln -s ${TEMP_LOCAL_DIR}/daq/rawdata/include/*.h ./include/daq/rawdata/ ; \ + ln -s ${TEMP_LOCAL_DIR}/daq/rawdata/modules/include/*.h ./include/daq/rawdata/modules/ ; \ + ln -s ${TEMP_LOCAL_DIR}/daq/dataobjects/include/*.h ./include/daq/dataobjects/ ; \ + ln -s ${TEMP_LOCAL_DIR}/daq/slc/system/include/*.h ./include/daq/slc/system/ ; \ + ln -s ${TEMP_LOCAL_DIR}/daq/slc/readout/include/*.h ./include/daq/slc/readout/ ; \ + ln -s ${TEMP_LOCAL_DIR}/daq/slc/base/include/*.h ./include/daq/slc/base/ ; \ + fi + +.PHONY: clean +clean: + @ echo "<< clearing directry >>" + @ rm -rf ${TARGET1} ${TARGET2} $(OBJS) lib${LOCALLIB}.so *~ .*~ \#*~ + @ rm -rf ${INCDIR} + +.PHONY: clean_ropc +clean_ropc: + @ echo "<< clearing directry >>" + @ rm -rf ${TARGET1} $(OBJS) lib${LOCALLIB}.so *~ .*~ \#*~ + @ rm -rf ${INCDIR} + +.PHONY: clean_copper +clean_copper: + @ echo "<< clearing directry >>" + @ rm -rf ${TARGET2} $(OBJS) lib${LOCALLIB}.so *~ .*~ \#*~ + @ rm -rf ${INCDIR} + +.PHONY: cleanobj +cleanobj: + @ echo "<< clearing object files >>" + @ rm -rf $(OBJS) diff --git a/ROPCApplications/daq/ropc/SConscript b/ROPCApplications/daq/ropc/SConscript new file mode 100644 index 0000000000000000000000000000000000000000..d35e248f87c2f3fb36239cc21a2ea7398fc2173e --- /dev/null +++ b/ROPCApplications/daq/ropc/SConscript @@ -0,0 +1,5 @@ +Import('env') + +env['CONTINUE'] = False + +Return('env') diff --git a/ROPCApplications/daq/ropc/des_ser_COPPER_main.cc b/ROPCApplications/daq/ropc/des_ser_COPPER_main.cc new file mode 100644 index 0000000000000000000000000000000000000000..1be33d931b0b2a8da2c81d162a9b189401cbfb42 --- /dev/null +++ b/ROPCApplications/daq/ropc/des_ser_COPPER_main.cc @@ -0,0 +1,59 @@ +//+ +// File : DAQmain.cc +// Description : Base class for Module to receive data from outside and store it to DataStore +// +// Author : Satoru Yamada Itoh, IPNS, KEK +// Date : 2 - Aug - 2013 +//- +//#include <daq/rawdata/modules/DAQConsts.h> +//#include <daq/rawdata/des_ser_COPPER_main.h> +#include "des_ser_COPPER_main.h" +//#include <daq/rawdata/modules/DesSerPrePC.h> +//#include <daq/rawdata/DesSerPrePC.h> +#include <daq/rawdata/DesSerCOPPER.h> +//#define MAXEVTSIZE 400000000 +#include "pcie40_ul_lib.h" + +using namespace std; +using namespace Belle2; + + +int main(int argc, char** argv) +{ + + readout(); + return 1; + + if (argc != 6) { + printf("[FATAL] Usage : %s <COPPER hostname> <COPPER node ID> <bit flag of FINNESEs> <Use NSM(Network Shared Memory)? yes=1/no=0> <NSM nodename>", + argv[0]); + // printf("Usage : %s <COPPER hostname> <Use NSM(Network Shared Memory)? yes=1/no=0> <port # to eb0> <NSM nodename>", argv[0]);; + exit(1); + } + + string host_recv = argv[1]; //! hostname + int shmflag = atoi(argv[4]); //! Use shared memory -> 1; Without shm -> 0 + int port_send = 30000; //! port number + // int port_send = atoi(argv[3]); //! port number + string nodename = argv[5]; //! Node name + string host_send = "0.0.0.0"; //! hostname + int port_recv = 30000; //! port number + int nodeid = atoi(argv[2]); + int finesse_bitflag = atoi(argv[3]); + DesSerCOPPER des_ser_copper(host_recv, port_recv, + host_send, port_send, shmflag, + nodename, nodeid, finesse_bitflag); + des_ser_copper.DataAcquisition(); + + + // + // Update EventMetaData + // +// m_eventMetaDataPtr.create(); +// m_eventMetaDataPtr->setExperiment(1); +// m_eventMetaDataPtr->setRun(1); +// m_eventMetaDataPtr->setEvent(1); + +} +//} + diff --git a/ROPCApplications/daq/ropc/des_ser_COPPER_main.h b/ROPCApplications/daq/ropc/des_ser_COPPER_main.h new file mode 100644 index 0000000000000000000000000000000000000000..6a8d0aa342856d5b8fcf6b73b7ef4648ab36b866 --- /dev/null +++ b/ROPCApplications/daq/ropc/des_ser_COPPER_main.h @@ -0,0 +1,106 @@ +//+ +// File : des_ser_COPPER_main.h +// Description : Base class for Module to receive data from outside and store it to DataStore +// +// Author : Satoru Yamada Itoh, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef DES_SER_COPPER_MAIN_H +#define DES_SER_COPPER_MAIN_H + +#include <string> +#include <vector> +#include <stdlib.h> +/* #include <sys/time.h> */ + +/* #include <framework/core/Module.h> */ +/* #include <framework/pcore/EvtMessage.h> */ +/* #include <framework/pcore/MsgHandler.h> */ +/* #include <framework/datastore/DataStore.h> */ +/* #include <framework/datastore/StoreObjPtr.h> */ +/* #include <framework/datastore/StoreArray.h> */ +/* #include <framework/dataobjects/EventMetaData.h> */ + +/* #include <daq/dataflow/EvtSocket.h> */ +/* #include <daq/slc/readout/RunInfoBuffer.h> */ +/* #include <daq/rawdata/CprErrorMessage.h> */ +/* #include <daq/rawdata/modules/DAQConsts.h> */ + + +/* #include <rawdata/dataobjects/RawDataBlock.h> */ +//#include <rawdata/dataobjects/RawCOPPER.h> + +/* #include <sys/socket.h> */ +/* #include <sys/types.h> */ +/* #include <arpa/inet.h> */ +/* #include <netinet/in.h> */ +/* #include <netinet/tcp.h> */ + +/* #include <sys/mman.h> */ +/* #include <sys/stat.h> */ +/* #include <fcntl.h> */ + +/* using namespace std; */ +/* using namespace Belle2; */ + +/* namespace Belle2 { */ + +/* /\*! A class definition of an input module for Sequential ROOT I/O *\/ */ + +/* class des_ser_COPPER_main{ */ + +/* // Public functions */ +/* public: */ + +/* //! Constructor / Destructor */ +/* des_ser_COPPER_mainModule(); */ +/* virtual ~des_ser_COPPER_mainModule(); */ + +/* //! Module functions to be called from main process */ +/* virtual void initialize(); */ + +/* //! Module functions to be called from main process */ +/* virtual void event(); */ + +/* protected : */ +/* //! hostname */ +/* string m_host_recv; */ + +/* //! hostname */ +/* string m_host_send; */ + +/* //! port number */ +/* int m_port_recv; */ + +/* //! port number */ +/* int m_port_send; */ + +/* //! Messaage handler */ +/* MsgHandler* m_msghandler; */ + +/* //! Compression Level */ +/* int m_compressionLevel; */ + +/* //! Node(PC or COPPER) ID */ +/* int m_nodeid; */ + +/* //! Node name */ +/* std::string m_nodename; */ + +/* //! Use shared memory -> 1; Without shm -> 0 */ +/* int m_shmflag; */ + +/* // Event Meta Data */ +/* StoreObjPtr<EventMetaData> m_eventMetaDataPtr; */ + +/* private: */ + + +/* public: */ + +/* }; */ + +/* } // end namespace Belle2 */ + +#endif // MODULEHELLO_H diff --git a/ROPCApplications/daq/ropc/des_ser_ROPC_main.cc b/ROPCApplications/daq/ropc/des_ser_ROPC_main.cc new file mode 100644 index 0000000000000000000000000000000000000000..e53501b6961f19b1748231278c938f77ad9197a4 --- /dev/null +++ b/ROPCApplications/daq/ropc/des_ser_ROPC_main.cc @@ -0,0 +1,49 @@ +//+ +// File : DAQmain.cc +// Description : Base class for Module to receive data from outside and store it to DataStore +// +// Author : Satoru Yamada Itoh, IPNS, KEK +// Date : 2 - Aug - 2013 +//- +//#include <daq/rawdata/modules/DAQConsts.h> +#include "des_ser_ROPC_main.h" +//#include <daq/rawdata/modules/DesSerPrePC.h> +//#include <daq/rawdata/DesSerPrePC.h> +#include <daq/rawdata/DesSerPrePC.h> +//#define MAXEVTSIZE 400000000 + +using namespace std; +using namespace Belle2; + + +//----------------------------------------------------------------- +// Implementation +//----------------------------------------------------------------- + +int main(int argc, char** argv) +{ + + if (argc != 5) { + + printf("[FATAL] Usage : %s <COPPER hostname> <Use NSM(Network Shared Memory)? yes=1/no=0> <port # to eb0> <NSM nodename>", argv[0]); + ; + exit(1); + } + + string host_recv = argv[1]; //! hostname + int shmflag = atoi(argv[2]); //! Use shared memory -> 1; Without shm -> 0 + int port_send = atoi(argv[3]); //! port number + string nodename = argv[4]; //! Node name + + string host_send = "0.0.0.0"; //! hostname + int port_recv = 30000; //! port number + int nodeid = 0; + DesSerPrePC des_ser_prepc(host_recv, port_recv, + host_send, port_send, shmflag, + nodename, nodeid); + des_ser_prepc.DataAcquisition(); + + +} + + diff --git a/ROPCApplications/daq/ropc/des_ser_ROPC_main.h b/ROPCApplications/daq/ropc/des_ser_ROPC_main.h new file mode 100644 index 0000000000000000000000000000000000000000..77fda6db80ad8bfe07c74e12570dad7f15fb5c86 --- /dev/null +++ b/ROPCApplications/daq/ropc/des_ser_ROPC_main.h @@ -0,0 +1,106 @@ +//+ +// File : des_ser_prePC_main.h +// Description : Base class for Module to receive data from outside and store it to DataStore +// +// Author : Satoru Yamada Itoh, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef DES_SER_PREPC_MAIN_H +#define DES_SER_PREPC_MAIN_H + +#include <string> +#include <vector> +#include <stdlib.h> +/* #include <sys/time.h> */ + +/* #include <framework/core/Module.h> */ +/* #include <framework/pcore/EvtMessage.h> */ +/* #include <framework/pcore/MsgHandler.h> */ +/* #include <framework/datastore/DataStore.h> */ +/* #include <framework/datastore/StoreObjPtr.h> */ +/* #include <framework/datastore/StoreArray.h> */ +/* #include <framework/dataobjects/EventMetaData.h> */ + +/* #include <daq/dataflow/EvtSocket.h> */ +/* #include <daq/slc/readout/RunInfoBuffer.h> */ +/* #include <daq/rawdata/CprErrorMessage.h> */ +/* #include <daq/rawdata/modules/DAQConsts.h> */ + + +/* #include <rawdata/dataobjects/RawDataBlock.h> */ +//#include <rawdata/dataobjects/RawCOPPER.h> + +/* #include <sys/socket.h> */ +/* #include <sys/types.h> */ +/* #include <arpa/inet.h> */ +/* #include <netinet/in.h> */ +/* #include <netinet/tcp.h> */ + +/* #include <sys/mman.h> */ +/* #include <sys/stat.h> */ +/* #include <fcntl.h> */ + +/* using namespace std; */ +/* using namespace Belle2; */ + +/* namespace Belle2 { */ + +/* /\*! A class definition of an input module for Sequential ROOT I/O *\/ */ + +/* class des_ser_prePC_main{ */ + +/* // Public functions */ +/* public: */ + +/* //! Constructor / Destructor */ +/* des_ser_prePC_mainModule(); */ +/* virtual ~des_ser_prePC_mainModule(); */ + +/* //! Module functions to be called from main process */ +/* virtual void initialize(); */ + +/* //! Module functions to be called from main process */ +/* virtual void event(); */ + +/* protected : */ +/* //! hostname */ +/* string m_host_recv; */ + +/* //! hostname */ +/* string m_host_send; */ + +/* //! port number */ +/* int m_port_recv; */ + +/* //! port number */ +/* int m_port_send; */ + +/* //! Messaage handler */ +/* MsgHandler* m_msghandler; */ + +/* //! Compression Level */ +/* int m_compressionLevel; */ + +/* //! Node(PC or COPPER) ID */ +/* int m_nodeid; */ + +/* //! Node name */ +/* std::string m_nodename; */ + +/* //! Use shared memory -> 1; Without shm -> 0 */ +/* int m_shmflag; */ + +/* // Event Meta Data */ +/* StoreObjPtr<EventMetaData> m_eventMetaDataPtr; */ + +/* private: */ + + +/* public: */ + +/* }; */ + +/* } // end namespace Belle2 */ + +#endif // MODULEHELLO_H diff --git a/ROPCApplications/daq/ropc/scripts/chage_all.sh b/ROPCApplications/daq/ropc/scripts/chage_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..e6875c2e9d817e5a0aa2e297f5b47aa6122201c7 --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/chage_all.sh @@ -0,0 +1,17 @@ +#!/bin/sh +ssh -XY klm03 'chage b2daq -E -1' +ssh -XY klm04 'chage b2daq -E -1' +#ssh -XY svd05 'chage b2daq -E -1' +#ssh -XY svd06 'chage b2daq -E -1' +#ssh -XY cdc06 'chage b2daq -E -1' +#ssh -XY top03 'chage b2daq -E -1' +#ssh -XY top04 'chage b2daq -E -1' +#ssh -XY arich02 'chage b2daq -E -1' +#ssh -XY ecl02 'chage b2daq -E -1' +#ssh -XY ecl03 'chage b2daq -E -1' +#ssh -XY ecl05 'chage b2daq -E -1' +#ssh -XY ecl06 'chage b2daq -E -1' + + + + diff --git a/ROPCApplications/daq/ropc/scripts/check_FFSTA_all.sh b/ROPCApplications/daq/ropc/scripts/check_FFSTA_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..87bbb7bde7395e13803807751fd5f61323cbf74a --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/check_FFSTA_all.sh @@ -0,0 +1,14 @@ +#!/bin/sh +ssh -XY cpr5001 ~/bin/check_FFSTA.sh +ssh -XY cpr5002 ~/bin/check_FFSTA.sh +ssh -XY cpr5003 ~/bin/check_FFSTA.sh +ssh -XY cpr5004 ~/bin/check_FFSTA.sh +ssh -XY cpr5005 ~/bin/check_FFSTA.sh +ssh -XY cpr5006 ~/bin/check_FFSTA.sh +ssh -XY cpr5007 ~/bin/check_FFSTA.sh +ssh -XY cpr5008 ~/bin/check_FFSTA.sh +ssh -XY cpr5009 ~/bin/check_FFSTA.sh +ssh -XY cpr5010 ~/bin/check_FFSTA.sh +ssh -XY cpr5011 ~/bin/check_FFSTA.sh +ssh -XY cpr5012 ~/bin/check_FFSTA.sh +ssh -XY cpr5013 ~/bin/check_FFSTA.sh diff --git a/ROPCApplications/daq/ropc/scripts/check_all.sh b/ROPCApplications/daq/ropc/scripts/check_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..aa2c78b4dd8ad76bf87b42731a5a48dcc5dd5c1e --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/check_all.sh @@ -0,0 +1,46 @@ +#!/bin/sh +ssh -XY svd01 $1 +ssh -XY svd02 $1 +ssh -XY svd03 $1 +ssh -XY svd04 $1 +ssh -XY svd05 $1 +ssh -XY svd06 $1 +ssh -XY svd07 $1 +ssh -XY svd08 $1 +ssh -XY svd09 $1 +ssh -XY svd10 $1 +ssh -XY svd11 $1 + +ssh -XY cdc01 $1 +ssh -XY cdc02 $1 +ssh -XY cdc03 $1 +ssh -XY cdc04 $1 +ssh -XY cdc05 $1 +ssh -XY cdc06 $1 +ssh -XY cdc07 $1 +ssh -XY cdc08 $1 +ssh -XY cdc09 $1 +ssh -XY cdc10 $1 + +ssh -XY top01 $1 +ssh -XY top02 $1 +ssh -XY top03 $1 +ssh -XY top04 $1 + +ssh -XY arich01 $1 +ssh -XY arich02 $1 +ssh -XY arich03 $1 + +ssh -XY ecl01 $1 +ssh -XY ecl02 $1 +ssh -XY ecl03 $1 +ssh -XY ecl04 $1 +ssh -XY ecl05 $1 +ssh -XY ecl06 $1 +ssh -XY ecl07 $1 + +ssh -XY klm01 $1 +ssh -XY klm02 $1 +ssh -XY klm03 $1 +ssh -XY klm04 $1 + diff --git a/ROPCApplications/daq/ropc/scripts/copy_etcfiles.sh b/ROPCApplications/daq/ropc/scripts/copy_etcfiles.sh new file mode 100755 index 0000000000000000000000000000000000000000..1ca888ddeacecec2f255fb7376b01877848c820a --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/copy_etcfiles.sh @@ -0,0 +1,142 @@ +#!/bin/sh +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@klm03:/etc +scp /etc/openldap/ldap.conf root@klm03:/etc/openldap +scp /etc/pam.d/system-auth-ac root@klm03:/etc/pam.d + +exit +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@svd01:/etc +scp /etc/openldap/ldap.conf root@svd01:/etc/openldap +scp /etc/pam.d/system-auth-ac root@svd01:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@svd03:/etc +scp /etc/openldap/ldap.conf root@svd03:/etc/openldap +scp /etc/pam.d/system-auth-ac root@svd03:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@svd08:/etc +scp /etc/openldap/ldap.conf root@svd08:/etc/openldap +scp /etc/pam.d/system-auth-ac root@svd08:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@arich01:/etc +scp /etc/openldap/ldap.conf root@arich01:/etc/openldap +scp /etc/pam.d/system-auth-ac root@arich01:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@arich03:/etc +scp /etc/openldap/ldap.conf root@arich03:/etc/openldap +scp /etc/pam.d/system-auth-ac root@arich03:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@ecl07:/etc +scp /etc/openldap/ldap.conf root@ecl07:/etc/openldap +scp /etc/pam.d/system-auth-ac root@ecl07:/etc/pam.d + + +exit + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@svd02:/etc +scp /etc/openldap/ldap.conf root@svd02:/etc/openldap +scp /etc/pam.d/system-auth-ac root@svd02:/etc/pam.d + + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@svd03:/etc +scp /etc/openldap/ldap.conf root@svd03:/etc/openldap +scp /etc/pam.d/system-auth-ac root@svd03:/etc/pam.d + + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc01:/etc +scp /etc/openldap/ldap.conf root@cdc01:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc01:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc02:/etc +scp /etc/openldap/ldap.conf root@cdc02:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc02:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc03:/etc +scp /etc/openldap/ldap.conf root@cdc03:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc03:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc05:/etc +scp /etc/openldap/ldap.conf root@cdc05:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc05:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc07:/etc +scp /etc/openldap/ldap.conf root@cdc07:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc07:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc08:/etc +scp /etc/openldap/ldap.conf root@cdc08:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc08:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc09:/etc +scp /etc/openldap/ldap.conf root@cdc09:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc09:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc10:/etc +scp /etc/openldap/ldap.conf root@cdc10:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc10:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@top02:/etc +scp /etc/openldap/ldap.conf root@top02:/etc/openldap +scp /etc/pam.d/system-auth-ac root@top02:/etc/pam.d + + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@klm02:/etc +scp /etc/openldap/ldap.conf root@klm02:/etc/openldap +scp /etc/pam.d/system-auth-ac root@klm02:/etc/pam.d + +exit + + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@top01:/etc +scp /etc/openldap/ldap.conf root@top01:/etc/openldap +scp /etc/pam.d/system-auth-ac root@top01:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@svd04:/etc +scp /etc/openldap/ldap.conf root@svd04:/etc/openldap +scp /etc/pam.d/system-auth-ac root@svd04:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@svd05:/etc +scp /etc/openldap/ldap.conf root@svd05:/etc/openldap +scp /etc/pam.d/system-auth-ac root@svd05:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@svd06:/etc +scp /etc/openldap/ldap.conf root@svd06:/etc/openldap +scp /etc/pam.d/system-auth-ac root@svd06:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc04:/etc +scp /etc/openldap/ldap.conf root@cdc04:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc04:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@cdc06:/etc +scp /etc/openldap/ldap.conf root@cdc06:/etc/openldap +scp /etc/pam.d/system-auth-ac root@cdc06:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@top03:/etc +scp /etc/openldap/ldap.conf root@top03:/etc/openldap +scp /etc/pam.d/system-auth-ac root@top03:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@top04:/etc +scp /etc/openldap/ldap.conf root@top04:/etc/openldap +scp /etc/pam.d/system-auth-ac root@top04:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@arich02:/etc +scp /etc/openldap/ldap.conf root@arich02:/etc/openldap +scp /etc/pam.d/system-auth-ac root@arich02:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@ecl02:/etc +scp /etc/openldap/ldap.conf root@ecl02:/etc/openldap +scp /etc/pam.d/system-auth-ac root@ecl02:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@ecl03:/etc +scp /etc/openldap/ldap.conf root@ecl03:/etc/openldap +scp /etc/pam.d/system-auth-ac root@ecl03:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@ecl05:/etc +scp /etc/openldap/ldap.conf root@ecl05:/etc/openldap +scp /etc/pam.d/system-auth-ac root@ecl05:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@ecl06:/etc +scp /etc/openldap/ldap.conf root@ecl06:/etc/openldap +scp /etc/pam.d/system-auth-ac root@ecl06:/etc/pam.d + +scp /etc/resolv.conf /etc/exports /etc/rc.local /etc/ntp.conf /etc/yum.conf /etc/ldap.conf /etc/nsswitch.conf root@klm03:/etc +scp /etc/openldap/ldap.conf root@klm03:/etc/openldap +scp /etc/pam.d/system-auth-ac root@klm03:/etc/pam.d diff --git a/ROPCApplications/daq/ropc/scripts/copy_ldap_files.sh b/ROPCApplications/daq/ropc/scripts/copy_ldap_files.sh new file mode 100755 index 0000000000000000000000000000000000000000..e51b909e48d6d05ba5b1c8543b1ffb7b8d82c6d7 --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/copy_ldap_files.sh @@ -0,0 +1,5 @@ +#!/bin/sh +scp /etc/ldap.conf root@klm02:/etc +scp /etc/openldap/ldap.conf root@klm02:/etc/openldap +scp /etc/pam.d/system-auth-ac root@klm02:/etc/pam.d +scp /etc/nsswitch.conf root@klm02:/etc diff --git a/ROPCApplications/daq/ropc/scripts/copy_pub_all.sh b/ROPCApplications/daq/ropc/scripts/copy_pub_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..7139d11be1b7c5ee3737d9da2fe006ab30aa6477 --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/copy_pub_all.sh @@ -0,0 +1,49 @@ +#!/bin/sh + +scp ~/.ssh/id_rsa.pub svd01:~/; ssh svd01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd02:~/; ssh svd02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd03:~/; ssh svd03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd04:~/; ssh svd04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd05:~/; ssh svd05 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd06:~/; ssh svd06 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd07:~/; ssh svd07 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd08:~/; ssh svd08 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd09:~/; ssh svd09 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd10:~/; ssh svd10 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd11:~/; ssh svd11 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc01:~/; ssh cdc01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc02:~/; ssh cdc02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc03:~/; ssh cdc03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc04:~/; ssh cdc04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc05:~/; ssh cdc05 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc06:~/; ssh cdc06 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc07:~/; ssh cdc07 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc08:~/; ssh cdc08 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc09:~/; ssh cdc09 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub cdc10:~/; ssh cdc10 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + +#scp ~/.ssh/id_rsa.pub top01:~/; ssh top01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub top02:~/; ssh top02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub top03:~/; ssh top03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub top04:~/; ssh top04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + +scp ~/.ssh/id_rsa.pub arich01:~/; ssh arich01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub arich02:~/; ssh arich02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub arich03:~/; ssh arich03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub arich04:~/; ssh arich04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + + +scp ~/.ssh/id_rsa.pub ecl01:~/; ssh ecl01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub ecl02:~/; ssh ecl02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub ecl03:~/; ssh ecl03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub ecl04:~/; ssh ecl04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub ecl05:~/; ssh ecl05 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub ecl06:~/; ssh ecl06 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub ecl07:~/; ssh ecl07 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + +scp ~/.ssh/id_rsa.pub klm01:~/; ssh klm01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub klm02:~/; ssh klm02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub klm03:~/; ssh klm03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub klm04:~/; ssh klm04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + + diff --git a/ROPCApplications/daq/ropc/scripts/copy_pub_all2.sh b/ROPCApplications/daq/ropc/scripts/copy_pub_all2.sh new file mode 100755 index 0000000000000000000000000000000000000000..c1fdb48abaf6c6019e9325a4e999232d8bc04177 --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/copy_pub_all2.sh @@ -0,0 +1,51 @@ +#!/bin/sh +scp ~/.ssh/id_rsa.pub klm03:~/; ssh klm03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + +exit +scp ~/.ssh/id_rsa.pub svd01:~/; ssh svd01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub svd02:~/; ssh svd02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd03:~/; ssh svd03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub svd04:~/; ssh svd04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub svd05:~/; ssh svd05 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub svd06:~/; ssh svd06 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub svd07:~/; ssh svd07 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub svd08:~/; ssh svd08 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub svd09:~/; ssh svd09 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub svd10:~/; ssh svd10 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub svd11:~/; ssh svd11 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc01:~/; ssh cdc01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc02:~/; ssh cdc02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc03:~/; ssh cdc03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc04:~/; ssh cdc04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc05:~/; ssh cdc05 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc06:~/; ssh cdc06 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc07:~/; ssh cdc07 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc08:~/; ssh cdc08 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc09:~/; ssh cdc09 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub cdc10:~/; ssh cdc10 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + +#scp ~/.ssh/id_rsa.pub top01:~/; ssh top01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub top02:~/; ssh top02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub top03:~/; ssh top03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub top04:~/; ssh top04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + +scp ~/.ssh/id_rsa.pub arich01:~/; ssh arich01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub arich02:~/; ssh arich02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub arich03:~/; ssh arich03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub arich04:~/; ssh arich04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + + +#scp ~/.ssh/id_rsa.pub ecl01:~/; ssh ecl01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub ecl02:~/; ssh ecl02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub ecl03:~/; ssh ecl03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub ecl04:~/; ssh ecl04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub ecl05:~/; ssh ecl05 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub ecl06:~/; ssh ecl06 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +scp ~/.ssh/id_rsa.pub ecl07:~/; ssh ecl07 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + +#scp ~/.ssh/id_rsa.pub klm01:~/; ssh klm01 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub klm02:~/; ssh klm02 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub klm03:~/; ssh klm03 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' +#scp ~/.ssh/id_rsa.pub klm04:~/; ssh klm04 'ssh 172.22.16.4; cat ~/id_rsa.pub > ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys;' + + diff --git a/ROPCApplications/daq/ropc/scripts/dist_src.sh b/ROPCApplications/daq/ropc/scripts/dist_src.sh new file mode 100755 index 0000000000000000000000000000000000000000..5f78eebf2d79772b64e1f79e19dace0a1ce21f8b --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/dist_src.sh @@ -0,0 +1,62 @@ +#!/bin/sh +#ssh -n -n -XY root@svd04 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +#ssh -n -n -XY root@svd05 " passwd yamadas;" +#ssh -n -n -XY root@svd06 " passwd yamadas;" +#ssh -n -n -XY root@cdc04 " passwd yamadas;" +#ssh -n -n -XY root@cdc06 " passwd yamadas;" +#ssh -n -n -XY root@top03 " passwd yamadas;" +#ssh -n -n -XY root@top04 " passwd yamadas;" +#ssh -n -n -XY root@arich02 " passwd yamadas;" +#ssh -n -n -XY root@ecl02 " passwd yamadas;" +#ssh -n -n -XY root@ecl03 " passwd yamadas;" +#ssh -n -n -XY root@ecl05 " passwd yamadas;" +#ssh -n -n -XY root@ecl06 " passwd yamadas;" +#ssh -n -n -XY root@klm03 " passwd yamadas;" + + +#ssh -n -n -XY root@svd01 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" +#ssh -n -n -XY root@svd03 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" +#ssh -n -n -XY root@svd08 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" +#ssh -n -n -XY root@arich01 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" +#ssh -n -n -XY root@ecl07 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" + +ssh -n -n -XY svd02 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY svd03 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY svd04 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY svd05 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY svd06 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY svd07 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY svd08 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" + +ssh -n -n -XY cdc01 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY cdc02 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY cdc03 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY cdc04 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY cdc05 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY cdc06 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY cdc07 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY cdc08 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY cdc09 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY cdc10 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" + +ssh -n -n -XY top01 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY top02 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY top03 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY top04 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" + +ssh -n -n -XY arich01 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY arich02 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" + + +ssh -n -n -XY ecl01 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY ecl02 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY ecl03 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY ecl04 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY ecl05 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY ecl06 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" + +ssh -n -n -XY klm01 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY klm02 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY klm03 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" +ssh -n -n -XY ecl07 "echo \${HOSTNAME}; cd ~/basf2/release/daq/rawdata/src; svn update; g++ -o dummy_data_src dummy_data_src.cc;" diff --git a/ROPCApplications/daq/ropc/scripts/dummy_start_all.sh b/ROPCApplications/daq/ropc/scripts/dummy_start_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..458cf1a6d4d7a8b12dc455ee21805bbf758f4bc9 --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/dummy_start_all.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# +# For KLM, CDC, ECL COPPERs slot A,B +# +# print 'Usage : RecvSendCOPPER.py <COPPER hostname> <COPPER node ID> <bit flag of FINNESEs> <Use NSM(Network Shared Memory)? yes=1/no=0> <NSM nodename>' +# bit flag of FINESSE +# slot a : 1, slot b : 2, slot c : 4, slot d : 8 +# e.g. slot abcd -> bitflag=15, slot bd -> bit flag=10 + +# SVD +/usr/bin/xterm -fn 7x14 -geometry 40x7+30+30 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh svd01 0x01000001 9001 115 6 1;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+30+150 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh svd02 0x01000007 9001 115 6 1;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+30+270 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh svd03 0x0100000d 9001 115 6 1;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+30+390 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh svd04 0x01000013 9001 115 6 1;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+30+510 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh svd05 0x01000019 9001 115 6 1;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+30+630 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh svd06 0x0100001f 9001 115 6 1;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+340+30 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh svd07 0x01000025 9001 115 6 1;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+340+150 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh svd08 0x0100002b 9001 115 6 1;" & +#CDC +/usr/bin/xterm -fn 7x14 -geometry 40x7+340+270 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc01 0x02000001 9001 10 9 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+340+390 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc02 0x0200000a 9001 10 9 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+340+510 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc03 0x02000013 9001 10 9 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+340+630 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc04 0x0200001c 9001 10 8 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+180+30 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc05 0x02000022 9001 10 8 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+180+150 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc06 0x02000028 9001 10 8 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+180+270 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc07 0x0200002e 9001 10 8 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+180+390 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc08 0x02000036 9001 10 8 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+180+510 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc09 0x0200003c 9001 10 8 4;" & +#TOP +/usr/bin/xterm -fn 7x14 -geometry 40x7+180+630 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh top01 0x03000001 9001 13 4 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+790+30 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh top02 0x03000005 9001 13 4 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+790+150 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh top03 0x03000009 9001 13 4 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+790+270 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh top04 0x0300000d 9001 13 4 4;" & +#ARICH +/usr/bin/xterm -fn 7x14 -geometry 40x7+790+390 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh arich01 0x04000001 9001 12 7 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+790+510 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh arich02 0x04000008 9001 12 7 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+790+630 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh cdc10 0x0400000f 9001 12 6 4;" & +#ECL +/usr/bin/xterm -fn 7x14 -geometry 40x7+655+30 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh ecl01 0x05000001 9001 58 5 2;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+655+150 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh ecl02 0x05000006 9001 58 4 2;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+655+270 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh ecl03 0x0500000a 9001 58 4 2;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+655+390 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh ecl04 0x0500000e 9001 58 5 2;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+655+510 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh ecl05 0x06000001 9001 58 4 2;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+655+630 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh ecl06 0x06000005 9001 58 4 2;" & +#KLM +/usr/bin/xterm -fn 7x14 -geometry 40x7+50+100 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh klm01 0x07000001 9001 21 3 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+50+220 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh klm02 0x07000004 9001 21 3 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+50+370 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh klm03 0x08000001 9001 10 5 4;" & +/usr/bin/xterm -fn 7x14 -geometry 40x7+50+490 -e "/home/usr/yamadas/basf2/release/daq/rawdata/src/dummy_start.sh ecl07 0x08000006 9001 10 4 4;" & + + diff --git a/ROPCApplications/daq/ropc/scripts/kill_dummy_all.sh b/ROPCApplications/daq/ropc/scripts/kill_dummy_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..28df6ed98727c64733adafeb5c95d60f9babc91e --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/kill_dummy_all.sh @@ -0,0 +1,49 @@ +#!/bin/sh +# +# For KLM, CDC, ECL COPPERs slot A,B +# +# print 'Usage : RecvSendCOPPER.py <COPPER hostname> <COPPER node ID> <bit flag of FINNESEs> <Use NSM(Network Shared Memory)? yes=1/no=0> <NSM nodename>' +# bit flag of FINESSE +# slot a : 1, slot b : 2, slot c : 4, slot d : 8 +# e.g. slot abcd -> bitflag=15, slot bd -> bit flag=10 + +ssh svd01 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh svd02 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh svd03 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh svd04 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh svd05 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh svd06 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh svd07 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh svd08 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; + +ssh cdc01 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh cdc02 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh cdc03 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh cdc04 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh cdc05 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh cdc06 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh cdc07 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh cdc08 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh cdc09 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh cdc10 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; + +ssh top01 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh top02 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh top03 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh top04 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; + +ssh arich01 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh arich02 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; + +ssh ecl01 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh ecl02 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh ecl03 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh ecl04 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh ecl05 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh ecl06 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh ecl07 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; + +ssh klm01 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh klm02 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; +ssh klm03 "echo \$HOSTNAME;pgrep dummy_data_src | xargs kill -KILL"; + diff --git a/ROPCApplications/daq/ropc/scripts/make_user_all.sh b/ROPCApplications/daq/ropc/scripts/make_user_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..76b9fa0906cead5bb3bfad92a6088f00e5f00634 --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/make_user_all.sh @@ -0,0 +1,28 @@ +#!/bin/sh +#ssh -n -n -XY root@svd04 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +#ssh -n -n -XY root@svd05 " passwd yamadas;" +#ssh -n -n -XY root@svd06 " passwd yamadas;" +#ssh -n -n -XY root@cdc04 " passwd yamadas;" +#ssh -n -n -XY root@cdc06 " passwd yamadas;" +#ssh -n -n -XY root@top03 " passwd yamadas;" +#ssh -n -n -XY root@top04 " passwd yamadas;" +#ssh -n -n -XY root@arich02 " passwd yamadas;" +#ssh -n -n -XY root@ecl02 " passwd yamadas;" +#ssh -n -n -XY root@ecl03 " passwd yamadas;" +#ssh -n -n -XY root@ecl05 " passwd yamadas;" +#ssh -n -n -XY root@ecl06 " passwd yamadas;" +#ssh -n -n -XY root@klm03 " passwd yamadas;" + + +#ssh -n -n -XY root@svd01 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" +#ssh -n -n -XY root@svd03 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" +#ssh -n -n -XY root@svd08 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" +#ssh -n -n -XY root@arich01 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" +#ssh -n -n -XY root@ecl07 "mkdir /home/usr; useradd -m -d /home/usr/yamadas -u 14252 yamadas; useradd -m -d /home/usr/b2daq -u 3001 b2daq;" + +ssh -n -n -XY root@svd01 "mkdir /home/usr/yamadas; mkdir /home/usr/b2daq; cd /home/usr; chmod a+rx *;" +ssh -n -n -XY root@svd03 "mkdir /home/usr/yamadas; mkdir /home/usr/b2daq; cd /home/usr; chmod a+rx *;" +ssh -n -n -XY root@svd08 "mkdir /home/usr/yamadas; mkdir /home/usr/b2daq; cd /home/usr; chmod a+rx *;" +ssh -n -n -XY root@arich01 "mkdir /home/usr/yamadas; mkdir /home/usr/b2daq; cd /home/usr; chmod a+rx *;" +ssh -n -n -XY root@ecl07 "mkdir /home/usr/yamadas; mkdir /home/usr/b2daq; cd /home/usr; chmod a+rx *;" diff --git a/ROPCApplications/daq/ropc/scripts/rsync_all.sh b/ROPCApplications/daq/ropc/scripts/rsync_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..eacba548258495e1cbb59b03886ca7672d24b684 --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/rsync_all.sh @@ -0,0 +1,28 @@ +#!/bin/sh +ssh -n -n -XY root@svd04 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@svd05 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@svd06 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@cdc04 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@cdc06 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@top03 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@top04 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@arich02 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@ecl02 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@ecl03 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@ecl05 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@ecl06 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@klm03 "rsync -a rsync://172.22.19.61/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; + diff --git a/ROPCApplications/daq/ropc/scripts/rsync_home_all.sh b/ROPCApplications/daq/ropc/scripts/rsync_home_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..722f6ed89422e21d6f5a6ef4ec00f78577f38332 --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/rsync_home_all.sh @@ -0,0 +1,28 @@ +#!/bin/sh +ssh -n -n -XY root@svd04 "rsync -avtpog rsync://172.22.19.41/export/home/usr/b2daq /home/usr;rsync -avtpog rsync://172.22.19.41/export/home/usr/yamadas /home/usr" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@svd05 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@svd06 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@cdc04 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@cdc06 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@top03 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@top04 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@arich02 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@ecl02 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@ecl03 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@ecl05 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@ecl06 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; +ssh -n -n -XY root@klm03 "rsync -avtpog rsync://172.22.19.41/export/tftpboot /" +echo "HIT ANY KEY"; read TEMP; + diff --git a/ROPCApplications/daq/ropc/scripts/service_start_all.sh b/ROPCApplications/daq/ropc/scripts/service_start_all.sh new file mode 100755 index 0000000000000000000000000000000000000000..8ebdeba0e813a32ccf6cce4260626073cd9a5ec6 --- /dev/null +++ b/ROPCApplications/daq/ropc/scripts/service_start_all.sh @@ -0,0 +1,64 @@ +#!/bin/sh +ssh -n -n -XY root@klm03 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +exit +ssh -n -n -XY root@svd01 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@svd03 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@svd08 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@arich01 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@ecl07 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + + +exit + +ssh -n -n -XY root@svd02 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@svd03 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@cdc01 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@cdc02 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@cdc03 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@cdc05 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@cdc07 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@cdc08 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@cdc09 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" +ssh -n -n -XY root@cdc10 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@top02 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@klm02 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + + +exit + +ssh -n -n -XY root@svd05 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; /sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@svd06 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; /sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@cdc04 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; +/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@cdc06 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; +/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@top03 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; +/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@top04 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; +/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@arich02 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; +/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@ecl02 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; +/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@ecl03 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; +/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@ecl05 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; +/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@ecl06 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on;/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" + +ssh -n -n -XY root@klm03 "http_proxy=http://b2stone.daqnet.kek.jp:8080/; https_proxy=http://b2stone.daqnet.kek.jp:8080/; ftp_proxy=http://b2stone.daqnet.kek.jp:8080/; export http_proxy https_proxy ftp_proxy; yum -y install emacs ; yum -y install openldap-clients ; /sbin/chkconfig ntpd on; +/sbin/chkconfig tftp on; /sbin/chkconfig nfs on; /sbin/service ntpd restart; /sbin/service xinetd restart;" diff --git a/ROPCApplications/daq/slc/base/include/AbstractDBObject.h b/ROPCApplications/daq/slc/base/include/AbstractDBObject.h new file mode 100644 index 0000000000000000000000000000000000000000..e9ad217c60e5ef92b7bdafa0ed02efeeadbdfc2f --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/AbstractDBObject.h @@ -0,0 +1,212 @@ +#ifndef _Belle2_AbstractDBObject_hh +#define _Belle2_AbstractDBObject_hh + +#include <daq/slc/base/FieldProperty.h> + +#include <daq/slc/base/Serializable.h> + +#include <string> +#include <stdexcept> + +namespace Belle2 { + + + class AbstractDBObject : public Serializable { + + public: + AbstractDBObject(); + AbstractDBObject(const AbstractDBObject& obj); + virtual ~AbstractDBObject(); + + public: + const std::string& getPath() const { return m_path; } + void setPath(const std::string& path) { m_path = path; } + int getId() const { return m_id; } + void setId(int id) { m_id = id; } + const std::string& getName() const { return m_name; } + void setName(const std::string& name) { m_name = name; } + int getIndex() const { return m_index; } + void setIndex(int index) { m_index = index; } + DBField::NameList& getFieldNames() { return m_name_v; } + const DBField::NameList& getFieldNames() const { return m_name_v; } + DBField::Property getProperty(const std::string& name) const; + bool hasField(const std::string& name) const; + bool hasValue(const std::string& name) const; + bool hasText(const std::string& name) const; + bool hasObject(const std::string& name) const; + void add(const std::string& name, DBField::Property pro); + void setValue(const std::string& name, const std::string& value); + void setText(const std::string& name, const std::string& value) { addText(name, value); } + + public: + bool getBool(const std::string& name, int index = 0) const; + char getChar(const std::string& name, int index = 0) const; + short getShort(const std::string& name, int index = 0) const; + int getInt(const std::string& name, int index = 0) const; + long long getLong(const std::string& name, int index = 0) const; + float getFloat(const std::string& name, int index = 0) const; + double getDouble(const std::string& name, int index = 0) const; + void addBool(const std::string& name, bool value); + void addChar(const std::string& name, char value); + void addShort(const std::string& name, short value); + void addInt(const std::string& name, int value); + void addLong(const std::string& name, long long value); + void addFloat(const std::string& name, float value); + void addDouble(const std::string& name, double value); + void setBool(const std::string& name, bool value, int index = 0); + void setChar(const std::string& name, int value, int index = 0); + void setShort(const std::string& name, int value, int index = 0); + void setInt(const std::string& name, int value, int index = 0); + void setLong(const std::string& name, long long value, int index = 0); + void setFloat(const std::string& name, float value, int index = 0); + void setDouble(const std::string& name, double value, int index = 0); + const std::string getValueText(const std::string& name) const; + + public: + virtual const void* getValue(const std::string& name) const = 0; + virtual const std::string& getText(const std::string& name) const = 0; + virtual void addText(const std::string& name, const std::string& value) = 0; + virtual void addValue(const std::string& name, const void* value, + DBField::Type type, int length) = 0; + virtual void setValue(const std::string& name, const void* value, int index) = 0; + virtual void setValueText(const std::string& name, const std::string& value); + + protected: + virtual void reset(); + + private: + int m_index; + std::string m_path; + int m_id; + std::string m_name; + DBField::NameList m_name_v; + DBField::PropertyList m_pro_m; + + private: + template<typename T> + T getD(const std::string& name, int index = 0) const + { + const void* value = getValue(name); + if (value == NULL/* || index >= getProperty(name).getLength()*/) { + throw (std::out_of_range(name + " not found")); + } + DBField::Type type(getProperty(name).getType()); + if (type == DBField::INT) return (T)(((int*)value)[index]); + if (type == DBField::FLOAT) return (T)(((float*)value)[index]); + if (type == DBField::DOUBLE) return (T)(((double*)value)[index]); + if (type == DBField::SHORT) return (T)(((short*)value)[index]); + if (type == DBField::CHAR) return (T)(((char*)value)[index]); + if (type == DBField::LONG) return (T)(((long*)value)[index]); + return ((T*)value)[index]; + } + + }; + + inline bool AbstractDBObject::getBool(const std::string& name, int index) const + { + return getD<bool>(name, index); + } + + inline char AbstractDBObject::getChar(const std::string& name, int index) const + { + return getD<char>(name, index); + } + + inline short AbstractDBObject::getShort(const std::string& name, int index) const + { + return getD<short>(name, index); + } + + inline int AbstractDBObject::getInt(const std::string& name, int index) const + { + return getD<int>(name, index); + } + + inline long long AbstractDBObject::getLong(const std::string& name, int index) const + { + return getD<long long>(name, index); + } + + inline float AbstractDBObject::getFloat(const std::string& name, int index) const + { + return getD<float>(name, index); + } + + inline double AbstractDBObject::getDouble(const std::string& name, int index) const + { + return getD<double>(name, index); + } + + inline void AbstractDBObject::setBool(const std::string& name, bool value, int index) + { + setValue(name, &value, index); + } + + inline void AbstractDBObject::setChar(const std::string& name, int value, int index) + { + setValue(name, &value, index); + } + + inline void AbstractDBObject::setShort(const std::string& name, int value, int index) + { + setValue(name, &value, index); + } + + inline void AbstractDBObject::setInt(const std::string& name, int value, int index) + { + setValue(name, &value, index); + } + + inline void AbstractDBObject::setLong(const std::string& name, long long value, int index) + { + setValue(name, &value, index); + } + + inline void AbstractDBObject::setFloat(const std::string& name, float value, int index) + { + setValue(name, &value, index); + } + + inline void AbstractDBObject::setDouble(const std::string& name, double value, int index) + { + setValue(name, &value, index); + } + + inline void AbstractDBObject::addBool(const std::string& name, bool value) + { + addValue(name, &value, DBField::BOOL, 0); + } + + inline void AbstractDBObject::addChar(const std::string& name, char value) + { + addValue(name, &value, DBField::CHAR, 0); + } + + inline void AbstractDBObject::addShort(const std::string& name, short value) + { + addValue(name, &value, DBField::SHORT, 0); + } + + inline void AbstractDBObject::addInt(const std::string& name, int value) + { + addValue(name, &value, DBField::INT, 0); + } + + inline void AbstractDBObject::addLong(const std::string& name, long long value) + { + addValue(name, &value, DBField::LONG, 0); + } + + inline void AbstractDBObject::addFloat(const std::string& name, float value) + { + addValue(name, &value, DBField::FLOAT, 0); + } + + inline void AbstractDBObject::addDouble(const std::string& name, double value) + { + addValue(name, &value, DBField::DOUBLE, 0); + } + +} + +#endif diff --git a/ROPCApplications/daq/slc/base/include/ConfigFile.h b/ROPCApplications/daq/slc/base/include/ConfigFile.h new file mode 100644 index 0000000000000000000000000000000000000000..aefac1af5f1f11514b973f3ea648ba4adc373770 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/ConfigFile.h @@ -0,0 +1,75 @@ +#ifndef _Belle2_ConfigFile_h +#define _Belle2_ConfigFile_h + +#include <string> +#include <map> +#include <vector> +#include <istream> + +namespace Belle2 { + + class ConfigFile { + public: + typedef std::map<std::string, std::string> ValueList; + + public: + ConfigFile() {} + ConfigFile(const std::string& file1) + { + read(file1, true); + } + ConfigFile(const std::string& file1, const std::string& file2) + { + read(file1, true); + read(file2, true); + } + ConfigFile(const std::string& file1, + const std::string& file2, + const std::string& file3) + { + read(file1, true); + read(file2, true); + read(file3, true); + } + ConfigFile(std::istream& is) + { + read(is); + } + ~ConfigFile() {} + + public: + void clear(); + void read(const std::string& filename, bool overload = true); + void read(std::istream& is, bool overload = true); + bool hasKey(const std::string& label); + const std::string get(const std::string& label); + int getInt(const std::string& label); + bool getBool(const std::string& label); + double getFloat(const std::string& label); + ValueList& getValues() { return m_value_m; } + std::vector<std::string>& getLabels() { return m_label_v; } + void add(const std::string& label, + const std::string& value, bool overload); + void write(const std::string& path); + void print(); + + private: + const std::string getFilePath(const std::string& filename); + + private: + ValueList m_value_m; + std::vector<std::string> m_label_v; + std::string m_dir; + std::map<std::string, int> m_count; + + }; + + inline bool ConfigFile::hasKey(const std::string& label) + { + return m_value_m.find((m_dir.size() > 0) ? m_dir + "." + label : label) + != m_value_m.end(); + } + +}; + +#endif diff --git a/ROPCApplications/daq/slc/base/include/Connection.h b/ROPCApplications/daq/slc/base/include/Connection.h new file mode 100644 index 0000000000000000000000000000000000000000..9621aa61b6168144b407c97290730edf9c590e57 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/Connection.h @@ -0,0 +1,46 @@ +#ifndef _Belle2_Connection_hh +#define _Belle2_Connection_hh + +#include "daq/slc/base/Enum.h" + +namespace Belle2 { + + class Connection : public Enum { + + public: + static const Connection ONLINE; + static const Connection OFFLINE; + + public: + Connection(); + Connection(const Enum& st) ; + Connection(const Connection& st) ; + Connection(const std::string& st) { *this = st; } + Connection(int id) { *this = id; } + ~Connection() ; + + private: + Connection(int id, const char* label); + + public: + bool operator==(const Connection& st) const; + bool operator!=(const Connection& st) const; + const Connection& operator=(const std::string& msg); + const Connection& operator=(const char* msg); + const Connection& operator=(int i); + + }; + + inline bool Connection::operator==(const Connection& st) const + { + return (getId() == st.getId()); + } + + inline bool Connection::operator!=(const Connection& st) const + { + return (getId() != st.getId()); + } + +} + +#endif diff --git a/ROPCApplications/daq/slc/base/include/Date.h b/ROPCApplications/daq/slc/base/include/Date.h new file mode 100644 index 0000000000000000000000000000000000000000..978d002e691399746bd778e51c8534b4f0428338 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/Date.h @@ -0,0 +1,44 @@ +#ifndef _Belle2_Date_hh +#define _Belle2_Date_hh + +#include <time.h> + +namespace Belle2 { + + class Date { + + public: + Date(time_t time); + Date(const Date& date); + Date(); + ~Date(); + + public: + void set(); + void set(time_t time); + time_t get() const { return m_time; } + int getSecond() const; + int getMinitue() const; + int getHour() const; + int getDay() const; + int getMonth() const; + int getYear() const; + const char* toString(const char* format = NULL) const; + + public: + const Date& operator=(const Date& date) + { + set(date.m_time); + return *this; + } + + private: + time_t m_time; + struct tm* m_tm; + mutable char m_str[31]; + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/base/include/ERRORNo.h b/ROPCApplications/daq/slc/base/include/ERRORNo.h new file mode 100644 index 0000000000000000000000000000000000000000..9edfefeaba6549d6d8fd3042de374ce93f96a59b --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/ERRORNo.h @@ -0,0 +1,39 @@ +#ifndef _Belle2_ERRORNo_hh +#define _Belle2_ERRORNo_hh + +#include "daq/slc/base/Enum.h" + +namespace Belle2 { + + class ERRORNo : public Enum { + + public: + static const ERRORNo UNKNOWN; + static const ERRORNo NSMONLINE; + static const ERRORNo NSMSENDREQ; + static const ERRORNo NSMMEMOPEN; + static const ERRORNo NSMINIT; + static const ERRORNo DATABASE; + + public: + ERRORNo() {} + ERRORNo(const Enum& e) : Enum(e) {} + ERRORNo(const ERRORNo& cmd) : Enum(cmd) {} + ERRORNo(const char* label) { *this = label; } + ERRORNo(int id) { *this = id; } + ~ERRORNo() {} + + protected: + ERRORNo(int id, const char* label) + : Enum(id, label) {} + + public: + const ERRORNo& operator=(const std::string& label); + const ERRORNo& operator=(const char* label); + const ERRORNo& operator=(int id); + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/base/include/Enum.h b/ROPCApplications/daq/slc/base/include/Enum.h new file mode 100644 index 0000000000000000000000000000000000000000..dcd86ce1860e7ee2acbd99d3cf4db2df2863ff08 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/Enum.h @@ -0,0 +1,67 @@ +#ifndef _Belle2_Enum_hh +#define _Belle2_Enum_hh + +#include <string> + +namespace Belle2 { + + class Enum { + + public: + const static Enum UNKNOWN; + + public: + ~Enum() {} + + protected: + Enum(int id, const std::string& label) + : m_id(id), m_label(label) {} + Enum(const Enum& e) : + m_id(e.m_id), m_label(e.m_label) {} + Enum() : m_id(UNKNOWN.m_id), + m_label(UNKNOWN.m_label) {} + + public: + int getId() const { return m_id; } + const char* getLabel() const { return m_label.c_str(); } + const std::string& getLabelString() const { return m_label; } + + public: + bool operator==(const Enum& e) const; + bool operator!=(const Enum& e) const; + const Enum& operator=(const Enum& e); + bool operator==(const std::string& label) const; + bool operator!=(const std::string& label) const; + + private: + int m_id; + std::string m_label; + + }; + + inline bool Enum::operator==(const Enum& e) const + { + return (m_id == e.m_id); + } + inline bool Enum::operator!=(const Enum& e) const + { + return (m_id != e.m_id); + } + inline const Enum& Enum::operator=(const Enum& e) + { + m_id = e.m_id; + m_label = e.m_label; + return *this; + } + inline bool Enum::operator==(const std::string& label) const + { + return m_label == label; + } + inline bool Enum::operator!=(const std::string& label) const + { + return m_label != label; + } + +} + +#endif diff --git a/ROPCApplications/daq/slc/base/include/Exception.h b/ROPCApplications/daq/slc/base/include/Exception.h new file mode 100644 index 0000000000000000000000000000000000000000..8e72adfc1c457d4631980d4bc6f4728eeed45a46 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/Exception.h @@ -0,0 +1,37 @@ +#ifndef _Belle2_Exception_hh +#define _Belle2_Exception_hh + +#include <exception> +#include <string> + +namespace Belle2 { + + class Exception : public std::exception { + + public: + Exception(); + Exception(const std::string&); + Exception(const std::string&, int err); +#if __GNUC__ >= 7 + virtual ~Exception() {} +#else + virtual ~Exception() throw() {} +#endif + + public: +#if __GNUC__ >= 7 + virtual const char* what() const noexcept; +#else + virtual const char* what() const throw(); +#endif + int err() const { return m_err; } + + protected: + std::string m_comment; + int m_err; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/base/include/FieldProperty.h b/ROPCApplications/daq/slc/base/include/FieldProperty.h new file mode 100644 index 0000000000000000000000000000000000000000..7ecb6afd358effe9766fe751f0d2b0613d3544c9 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/FieldProperty.h @@ -0,0 +1,88 @@ +#ifndef _Belle2_FieldProperty_hh +#define _Belle2_FieldProperty_hh + +#include <string> +#include <map> +#include <vector> + +namespace Belle2 { + + namespace DBField { + + enum Type { + UNKNOWN = 0, + BOOL = 1, + CHAR, + SHORT, + INT, + LONG, + FLOAT, + DOUBLE, + TEXT, + OBJECT + }; + + class Property { + + public: + Property(): m_type(), m_length(0), m_offset(0) {} + Property(Type type, int length, int offset = 0) + : m_type(type), m_length(length), m_offset(offset) {} + Property(const Property& pro) + : m_type(pro.m_type), m_length(pro.m_length), m_offset(pro.m_offset) {} + ~Property() {} + + public: + Type getType() const { return m_type; } + int getLength() const { return m_length; } + int getOffset() const { return m_offset; } + void setType(Type type) { m_type = type; } + void setLength(int length) { m_length = length; } + + int getTypeSize() const + { + switch (m_type) { + case BOOL: return sizeof(bool); + case CHAR: return sizeof(char); + case SHORT: return sizeof(short); + case INT: return sizeof(int); + case LONG: return sizeof(long long); + case FLOAT: return sizeof(float); + case DOUBLE: return sizeof(double); + default: break; + } + return 0; + } + + const std::string getTypeText() const + { + switch (m_type) { + case BOOL: return "bool"; + case CHAR: return "char"; + case SHORT: return "short"; + case INT: return "int"; + case LONG: return "long"; + case FLOAT: return "float"; + case DOUBLE: return "double"; + case TEXT: return "text"; + case OBJECT: return "object"; + default: break; + } + return ""; + } + + private: + Type m_type; + int m_length; + int m_offset; + + }; + + typedef std::vector<std::string> NameList; + typedef std::map<std::string, DBField::Property> PropertyList; + + } + +} + +#endif diff --git a/ROPCApplications/daq/slc/base/include/IOException.h b/ROPCApplications/daq/slc/base/include/IOException.h new file mode 100644 index 0000000000000000000000000000000000000000..2204de3574931c407985f9f766c7cfb50fe9b623 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/IOException.h @@ -0,0 +1,24 @@ +#ifndef _Belle2_IOException_hh +#define _Belle2_IOException_hh + +#include "daq/slc/base/Exception.h" + +namespace Belle2 { + + class IOException : public Exception { + + public: + IOException() {} + IOException(const std::string& comment, ...); + IOException(int err, const std::string& comment, ...); +#if __GNUC__ >= 7 + virtual ~IOException() {} +#else + virtual ~IOException() throw() {} +#endif + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/base/include/Reader.h b/ROPCApplications/daq/slc/base/include/Reader.h new file mode 100644 index 0000000000000000000000000000000000000000..aa46197465ced965f7ad2331ca3cdd3d5cc8ba8c --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/Reader.h @@ -0,0 +1,42 @@ +#ifndef _Belle2_Reader_hh +#define _Belle2_Reader_hh + +#include <cstddef> +#include <string> + +namespace Belle2 { + + class Serializable; + + class Reader { + + public: + virtual ~Reader() {} + + public: + virtual size_t read(void*, size_t) = 0; + virtual bool available() = 0; + + protected: + void reverse(void* buf, size_t len); + + public: + virtual bool readBool(); + virtual char readChar(); + virtual short readShort(); + virtual int readInt(); + virtual long long readLong(); + virtual unsigned char readUChar(); + virtual unsigned short readUShort(); + virtual unsigned int readUInt(); + virtual unsigned long long readULong(); + virtual float readFloat(); + virtual double readDouble(); + virtual const std::string readString(); + virtual void readObject(Serializable&); + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/base/include/Serializable.h b/ROPCApplications/daq/slc/base/include/Serializable.h new file mode 100644 index 0000000000000000000000000000000000000000..c09c89767b141a21eebf622b8b5b04f97ccd2af8 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/Serializable.h @@ -0,0 +1,22 @@ +#ifndef _Belle2_Serializable_hh +#define _Belle2_Serializable_hh + +namespace Belle2 { + + class Reader; + class Writer; + + class Serializable { + + public: + virtual ~Serializable() {} + + public: + virtual void readObject(Reader&) = 0; + virtual void writeObject(Writer&) const = 0; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/base/include/StringUtil.h b/ROPCApplications/daq/slc/base/include/StringUtil.h new file mode 100644 index 0000000000000000000000000000000000000000..defe2ad24f03651f7f5830864944e10da788168e --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/StringUtil.h @@ -0,0 +1,33 @@ +#ifndef _Belle2_StringUtil_hh +#define _Belle2_StringUtil_hh + +#include <string> +#include <vector> + +namespace Belle2 { + + typedef std::vector<std::string> StringList; + + struct StringUtil { + + static StringList split(const std::string& str, + const char type, size_t max = 0); + static std::string join(StringList str_v, const std::string& s, + size_t start = 0, size_t end = 0); + static std::string replace(const std::string& source, + const std::string& pattern, + const std::string& placement); + static std::string form(const std::string& string, ...); + static std::string toupper(const std::string& str); + static std::string tolower(const std::string& str); + static int atoi(const std::string str); + static double atof(const std::string str); + static long long atoll(const std::string str); + static bool find(const std::string& s, const std::string& str); + static bool isdigit(const std::string& s); + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/base/include/TimeoutException.h b/ROPCApplications/daq/slc/base/include/TimeoutException.h new file mode 100644 index 0000000000000000000000000000000000000000..27af658d3fd1d2c4dab025f0bceab9d5a4455e73 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/TimeoutException.h @@ -0,0 +1,24 @@ +#ifndef _Belle2_TimeoutException_hh +#define _Belle2_TimeoutException_hh + +#include "daq/slc/base/IOException.h" + +namespace Belle2 { + + class TimeoutException : public IOException { + + public: + TimeoutException() {} + TimeoutException(const std::string& comment, ...); + TimeoutException(int err, const std::string& comment, ...); +#if __GNUC__ >= 7 + ~TimeoutException() {} +#else + ~TimeoutException() throw() {} +#endif + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/base/include/Writer.h b/ROPCApplications/daq/slc/base/include/Writer.h new file mode 100644 index 0000000000000000000000000000000000000000..3241ed7c937fc58d6103ff5fa9ebdc676e1a4fc8 --- /dev/null +++ b/ROPCApplications/daq/slc/base/include/Writer.h @@ -0,0 +1,42 @@ +#ifndef _Belle2_Writer_hh +#define _Belle2_Writer_hh + +#include <cstddef> +#include <string> + +namespace Belle2 { + + class Serializable; + + class Writer { + + public: + virtual ~Writer() {} + + public: + virtual size_t write(const void*, size_t) = 0; + virtual bool available() = 0; + + protected: + void reverse(void* buf, size_t len); + + public: + virtual void writeBool(bool); + virtual void writeChar(char); + virtual void writeShort(short); + virtual void writeInt(int); + virtual void writeLong(long long); + virtual void writeUChar(unsigned char); + virtual void writeUShort(unsigned short); + virtual void writeUInt(unsigned int); + virtual void writeULong(unsigned long long); + virtual void writeFloat(float); + virtual void writeDouble(double); + virtual void writeString(const std::string&); + virtual void writeObject(const Serializable&); + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/base/src/AbstractDBObject.cc b/ROPCApplications/daq/slc/base/src/AbstractDBObject.cc new file mode 100644 index 0000000000000000000000000000000000000000..9de4ea108ac98530726f2e06f17286a64599ce3c --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/AbstractDBObject.cc @@ -0,0 +1,128 @@ +#include "daq/slc/base/AbstractDBObject.h" + +#include <daq/slc/base/StringUtil.h> + +#include <cstdlib> + +using namespace Belle2; + +AbstractDBObject::AbstractDBObject() : m_index(0), m_path(), m_id(0) +{ + +} + +AbstractDBObject::AbstractDBObject(const AbstractDBObject& obj) + : m_index(obj.m_index), m_path(obj.m_path), + m_id(obj.m_id), + m_name(obj.m_name) {} + +AbstractDBObject::~AbstractDBObject() +{ + reset(); +} + +void AbstractDBObject::reset() +{ + m_path = ""; + m_id = 0; + m_index = 0; + m_name_v = DBField::NameList(); + m_pro_m = DBField::PropertyList(); +} + +DBField::Property AbstractDBObject::getProperty(const std::string& name) const +{ + DBField::PropertyList::const_iterator it = m_pro_m.find(name); + if (it != m_pro_m.end()) return it->second; + return DBField::Property(); +} + +bool AbstractDBObject::hasField(const std::string& name) const +{ + DBField::PropertyList::const_iterator it = m_pro_m.find(name); + return (it != m_pro_m.end()); +} + +bool AbstractDBObject::hasValue(const std::string& name) const +{ + DBField::PropertyList::const_iterator it = m_pro_m.find(name); + return hasField(name) && + it->second.getType() != DBField::TEXT && + it->second.getType() != DBField::OBJECT; +} + +bool AbstractDBObject::hasText(const std::string& name) const +{ + DBField::PropertyList::const_iterator it = m_pro_m.find(name); + return hasField(name) && it->second.getType() == DBField::TEXT; +} + +bool AbstractDBObject::hasObject(const std::string& name) const +{ + DBField::PropertyList::const_iterator it = m_pro_m.find(name); + return hasField(name) && + it->second.getType() == DBField::OBJECT; +} + +void AbstractDBObject::add(const std::string& name, DBField::Property pro) +{ + if (!hasField(name)) { + m_name_v.push_back(name); + m_pro_m.insert(DBField::PropertyList::value_type(name, pro)); + } +} + +const std::string AbstractDBObject::getValueText(const std::string& name) +const +{ + if (hasField(name)) { + switch (getProperty(name).getType()) { + case DBField::BOOL: return getBool(name) ? "true" : "false"; + case DBField::CHAR: return StringUtil::form("%d", (int)getChar(name)); + case DBField::SHORT: return StringUtil::form("%d", (int)getShort(name)); + case DBField::INT: return StringUtil::form("%d", (int)getInt(name)); + case DBField::LONG: return StringUtil::form("%ld", getLong(name)); + case DBField::FLOAT: return StringUtil::form("%f", getFloat(name)); + case DBField::DOUBLE: return StringUtil::form("%f", getDouble(name)); + case DBField::TEXT: return getText(name); + default: break; + } + } + throw (std::out_of_range(name + " not found")); +} + +void AbstractDBObject::setValueText(const std::string& name, + const std::string& value) +{ + if (hasField(name)) { + switch (getProperty(name).getType()) { + case DBField::BOOL: setBool(name, value == "true" || value == "t"); break; + case DBField::CHAR: { + if (StringUtil::find(value, "0x")) + setChar(name, (char)strtol(value.c_str(), NULL, 0)); + else + setChar(name, (char)atoi(value.c_str())); + } break; + case DBField::SHORT: { + if (StringUtil::find(value, "0x")) + setShort(name, (short)strtol(value.c_str(), NULL, 0)); + else + setShort(name, (short)atoi(value.c_str())); + } break; + case DBField::INT: { + if (StringUtil::find(value, "0x")) + setInt(name, (int)strtol(value.c_str(), NULL, 0)); + else + setInt(name, (int)atoi(value.c_str())); + } break; + case DBField::LONG: setLong(name, (long long)atoi(value.c_str())); break; + case DBField::FLOAT: setFloat(name, atof(value.c_str())); break; + case DBField::DOUBLE: setDouble(name, atof(value.c_str())); break; + case DBField::TEXT: setText(name, value); break; + default: break; + } + return; + } + throw (std::out_of_range(name + " not found")); +} + diff --git a/ROPCApplications/daq/slc/base/src/ConfigFile.cc b/ROPCApplications/daq/slc/base/src/ConfigFile.cc new file mode 100644 index 0000000000000000000000000000000000000000..11b6f85ddbe0bb435c444e17ed74948d6a4e5722 --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/ConfigFile.cc @@ -0,0 +1,208 @@ +#include "daq/slc/base/ConfigFile.h" + +#include "daq/slc/base/StringUtil.h" + +#include <cstdlib> +#include <cstring> +#include <fstream> +#include <iostream> +#include <sstream> + +using namespace Belle2; + +const std::string ConfigFile::getFilePath(const std::string& filename_in) +{ + std::string filename = filename_in; + if (filename == "slowcontrol") { + /* + char* hostname = getenv("HOSTNAME"); + if (hostname != NULL && std::string(hostname).find("hlt") != std::string::npos) { + filename = "slowcontrol.hlt"; + } + */ + char* slcfile = getenv("BELLE2_SLC_FILE"); + if (slcfile != NULL && strlen(slcfile) > 0) { + filename = slcfile; + } + } + std::string file_path; + if (filename.size() > std::string(".conf").size() + 1 && + StringUtil::find(filename, ".conf")) { + return filename; + } + if (filename.at(0) != '/') { + char* path = getenv("BELLE2_DAQ_SLC"); + if (path == NULL) { + path = getenv("BELLE2_LOCAL_DIR"); + if (path == NULL) { + exit(1); + } + file_path = path; + file_path += "/daq/slc/"; + } else { + file_path = path; + } + if (filename.find("/") == std::string::npos) { + file_path += "/data/config/" + filename + ".conf"; + } else { + file_path += "/data/" + filename + ".conf"; + } + } else { + file_path = filename; + } + return file_path; +} + +void ConfigFile::read(const std::string& filename, bool overload) +{ + if (filename.size() == 0) return; + std::ifstream fin(getFilePath(filename).c_str()); + read(fin, overload); + fin.close(); +} + +void ConfigFile::read(std::istream& is, bool overload) +{ + std::string s; + std::string dir = ""; + while (is && getline(is, s)) { + if (s.size() == 0 || s.at(0) == '#') continue; + std::vector<std::string> str_v = StringUtil::split(s, ':'); + if (str_v.size() >= 2) { + std::string label = StringUtil::replace(StringUtil::replace(str_v[0], + " ", ""), "\t", ""); + if (label.find("[]") != std::string::npos) { + if (m_count.find(label) == m_count.end()) { + m_count.insert(std::pair<std::string, int>(label, 0)); + } + std::string l = label; + label = StringUtil::replace(label, "[]", StringUtil::form("[%d]", m_count[l])); + m_count[l] = m_count[l] + 1; + } + if (str_v.size() > 2) { + for (size_t i = 2; i < str_v.size(); i++) { + str_v[1].append(":"); + str_v[1].append(str_v[i]); + } + } + std::string value = ""; + size_t i = 0; + std::stringstream ss; + for (; i < str_v[1].size(); i++) { + if (str_v[1].at(i) == '#' || str_v[1].at(i) == '\n') break; + if (str_v[1].at(i) == ' ' || str_v[1].at(i) == '\t') continue; + if (str_v[1].at(i) == '"') { + for (i++ ; i < str_v[1].size(); i++) { + if (str_v[1].at(i) == '"') break; + ss << str_v[1].at(i); + } + break; + } + if (str_v[1].at(i) == '$') { + i++; + if (str_v[1].at(i) == '{') { + for (i++ ; i < str_v[1].size(); i++) { + if (str_v[1].at(i) == '}') break; + ss << str_v[1].at(i); + } + } + std::string tmp = ss.str(); + const char* env = getenv(tmp.c_str()); + ss.str(""); + if (env != NULL) { + ss << env; + } else if (m_value_m.find(tmp) != m_value_m.end()) { + ss << m_value_m[tmp]; + } + continue; + } + ss << str_v[1].at(i); + } + add(label, ss.str(), overload); + } + } +} + +void ConfigFile::clear() +{ + m_value_m.clear(); +} + +const std::string ConfigFile::get(const std::string& label) +{ + if (m_value_m.find(label) != m_value_m.end()) { + return m_value_m[label]; + } + return ""; +} + +int ConfigFile::getInt(const std::string& label) +{ + std::string value = get(label); + if (value.size() > 0) return atoi(value.c_str()); + else return 0; +} + +bool ConfigFile::getBool(const std::string& label) +{ + std::string value = get(label); + if (value.size() > 0) return StringUtil::tolower(value) == "true"; + else return false; +} + +double ConfigFile::getFloat(const std::string& label) +{ + std::string value = get(label); + if (value.size() > 0) return atof(value.c_str()); + else return 0; +} + +void ConfigFile::add(const std::string& label, + const std::string& value, bool overload) +{ + if (m_value_m.find(label) == m_value_m.end()) { + m_value_m.insert(ValueList::value_type(label, value)); + m_label_v.push_back(label); + } else if (overload) { + m_value_m[label] = value; + } +} + +void ConfigFile::write(const std::string& path) +{ + std::stringstream ss; + ss << "#" << std::endl + << "#" << std::endl + << "#" << std::endl + << "" << std::endl; + for (std::vector<std::string>::iterator it = m_label_v.begin(); + it != m_label_v.end(); it++) { + std::string& label(*it); + std::string& value(m_value_m[label]); + ss << label << " : " << value << std::endl; + } + ss << "" << std::endl + << "#" << std::endl + << "#" << std::endl + << "#" << std::endl; + std::ofstream fout(getFilePath(path).c_str()); + fout << ss.str(); +} + +void ConfigFile::print() +{ + std::cout << "#" << std::endl + << "#" << std::endl + << "#" << std::endl + << "" << std::endl; + for (std::vector<std::string>::iterator it = m_label_v.begin(); + it != m_label_v.end(); it++) { + std::string& label(*it); + std::string& value(m_value_m[label]); + std::cout << label << " : " << value << std::endl; + } + std::cout << "" << std::endl + << "#" << std::endl + << "#" << std::endl + << "#" << std::endl; +} diff --git a/ROPCApplications/daq/slc/base/src/Connection.cc b/ROPCApplications/daq/slc/base/src/Connection.cc new file mode 100644 index 0000000000000000000000000000000000000000..73dd1e21687a091e18fe19be63b59eb0c27dfb26 --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/Connection.cc @@ -0,0 +1,58 @@ +#include "daq/slc/base/Connection.h" + +using namespace Belle2; + +const Connection Connection::ONLINE(10000, "ONLINE"); +const Connection Connection::OFFLINE(20000, "OFFLINE"); + +Connection::Connection() : Enum() +{ + +} + +Connection::Connection(const Enum& st) + : Enum(st.getId(), st.getLabel()) +{ + +} + +Connection::Connection(const Connection& st) + : Enum(st.getId(), st.getLabel()) +{ + +} + +Connection::Connection(int id, const char* label) + : Enum(id, label) +{ + +} + +Connection::~Connection() +{ + +} + +const Connection& Connection::operator=(const std::string& msg) +{ + if (msg == ONLINE.getLabel()) *this = ONLINE; + else if (msg == OFFLINE.getLabel()) *this = OFFLINE; + else *this = UNKNOWN; + return *this; +} + +const Connection& Connection::operator=(const char* msg) +{ + if (msg != NULL) *this = std::string(msg); + else *this = UNKNOWN; + return *this; +} + +const Connection& Connection::operator=(int i) +{ + if (i == ONLINE.getId()) *this = ONLINE; + else if (i == OFFLINE.getId()) *this = OFFLINE; + else *this = UNKNOWN; + return *this; +} + diff --git a/ROPCApplications/daq/slc/base/src/Date.cc b/ROPCApplications/daq/slc/base/src/Date.cc new file mode 100644 index 0000000000000000000000000000000000000000..b76657c2f9a8643c746ae28420dfc2cc2694f265 --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/Date.cc @@ -0,0 +1,75 @@ +#include "daq/slc/base/Date.h" + +#include <cstring> + +using namespace Belle2; + +Date::Date(time_t time) +{ + set(time); +} + +Date::Date(const Date& date) +{ + set(date.m_time); +} + +Date::Date() +{ + set(); +} + +Date::~Date() {} + +void Date::set() +{ + m_time = time(NULL); + m_tm = localtime(&m_time); +} + +void Date::set(time_t time) +{ + m_time = time; + m_tm = localtime(&m_time); +} + +int Date::getSecond() const +{ + return m_tm->tm_sec; +} + +int Date::getMinitue() const +{ + return m_tm->tm_min; +} + +int Date::getHour() const +{ + return m_tm->tm_hour; +} + +int Date::getDay() const +{ + return m_tm->tm_mday; +} + +int Date::getMonth() const +{ + return m_tm->tm_mon + 1; +} + +int Date::getYear() const +{ + return m_tm->tm_year + 1900; +} + +const char* Date::toString(const char* format) const +{ + memset(m_str, 0, sizeof(m_str)); + if (format == NULL) { + strftime(m_str, 31, "%Y-%m-%d %H:%M:%S", m_tm); + } else { + strftime(m_str, 31, format, m_tm); + } + return m_str; +} diff --git a/ROPCApplications/daq/slc/base/src/ERRORNo.cc b/ROPCApplications/daq/slc/base/src/ERRORNo.cc new file mode 100644 index 0000000000000000000000000000000000000000..963cef5151dda2e32d99001211a6df595371b97b --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/ERRORNo.cc @@ -0,0 +1,51 @@ +#include "daq/slc/base/ERRORNo.h" + +using namespace Belle2; + +const ERRORNo ERRORNo::UNKNOWN(0, "UNKNOWN"); +const ERRORNo ERRORNo::NSMONLINE(11, "NSMONLINE"); +const ERRORNo ERRORNo::NSMSENDREQ(12, "NSMSENDREQ"); +const ERRORNo ERRORNo::NSMMEMOPEN(13, "SENOPEN"); +const ERRORNo ERRORNo::NSMINIT(14, "NSMINIT"); +const ERRORNo ERRORNo::DATABASE(31, "DATABASE"); + +const ERRORNo& ERRORNo::operator=(const std::string& label) +{ + if (label == NSMSENDREQ.getLabel()) { + return NSMSENDREQ; + } else if (label == NSMONLINE.getLabel()) { + return NSMONLINE; + } else if (label == NSMMEMOPEN.getLabel()) { + return NSMMEMOPEN; + } else if (label == NSMINIT.getLabel()) { + return NSMINIT; + } else if (label == DATABASE.getLabel()) { + return DATABASE; + } else { + return UNKNOWN; + } +} + +const ERRORNo& ERRORNo::operator=(int id) +{ + if (id == NSMSENDREQ.getId()) { + return NSMSENDREQ; + } else if (id == NSMONLINE.getId()) { + return NSMONLINE; + } else if (id == NSMMEMOPEN.getId()) { + return NSMMEMOPEN; + } else if (id == NSMINIT.getId()) { + return NSMINIT; + } else if (id == DATABASE.getId()) { + return DATABASE; + } + return UNKNOWN; +} + +const ERRORNo& ERRORNo::operator=(const char* label) +{ + if (label != NULL) *this = std::string(label); + else *this = Enum::UNKNOWN; + return *this; +} + diff --git a/ROPCApplications/daq/slc/base/src/Enum.cc b/ROPCApplications/daq/slc/base/src/Enum.cc new file mode 100644 index 0000000000000000000000000000000000000000..dc8962df990ae96660949ebd961726723fbce360 --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/Enum.cc @@ -0,0 +1,5 @@ +#include "daq/slc/base/Enum.h" + +using namespace Belle2; + +const Enum Enum::UNKNOWN(0, "UNKNOWN"); diff --git a/ROPCApplications/daq/slc/base/src/Exception.cc b/ROPCApplications/daq/slc/base/src/Exception.cc new file mode 100644 index 0000000000000000000000000000000000000000..e0b3078c9a5dae6d268fc7e7a02771ad417b441b --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/Exception.cc @@ -0,0 +1,28 @@ +#include "daq/slc/base/Exception.h" + +using namespace Belle2; + +Exception::Exception() + : m_comment(""), m_err(0) +{ +} + +Exception::Exception(const std::string& comment) + : m_comment(comment), m_err(0) +{ +} + +Exception::Exception(const std::string& comment, + int err) + : m_comment(comment), m_err(err) +{ +} + +#if __GNUC__ >= 7 +const char* Exception::what() const noexcept +#else +const char* Exception::what() const throw() +#endif +{ + return m_comment.c_str(); +} diff --git a/ROPCApplications/daq/slc/base/src/IOException.cc b/ROPCApplications/daq/slc/base/src/IOException.cc new file mode 100644 index 0000000000000000000000000000000000000000..0921af40ec9d6530266f4f81b2bb8f7d48ce9557 --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/IOException.cc @@ -0,0 +1,28 @@ +#include "daq/slc/base/IOException.h" + +#include <cstdarg> +#include <cstdio> + +using namespace Belle2; + +IOException::IOException(const std::string& format, ...) +{ + va_list ap; + char ss[1024 * 100]; + va_start(ap, format); + vsprintf(ss, format.c_str(), ap); + va_end(ap); + m_comment = ss; +} + +IOException::IOException(int err, const std::string& format, ...) +{ + m_err = err; + va_list ap; + char ss[1024 * 100]; + va_start(ap, format); + vsprintf(ss, format.c_str(), ap); + va_end(ap); + m_comment = ss; +} + diff --git a/ROPCApplications/daq/slc/base/src/Reader.cc b/ROPCApplications/daq/slc/base/src/Reader.cc new file mode 100644 index 0000000000000000000000000000000000000000..99ee127e3de6aa327ae70c7611e3a12b197c1ba8 --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/Reader.cc @@ -0,0 +1,139 @@ +#include "daq/slc/base/Reader.h" + +#include "daq/slc/base/Serializable.h" + +#include <unistd.h> + +#define __ENDIAN_L__ + +using namespace Belle2; + +void Reader::reverse(void* buf, size_t len) +{ + char tmp; + for (int c = 0 ; c < (int)len / 2 ; c ++) { + tmp = *((char*)buf + c); + *((char*)buf + c) = *((char*)buf + len - 1 - c); + *((char*)buf + len - 1 - c) = tmp; + } +} + +bool Reader::readBool() +{ + bool v; + read(&v, sizeof(bool)); + return v; +} + +char Reader::readChar() +{ + char v; + read(&v, sizeof(char)); + return v; +} + +unsigned char Reader::readUChar() +{ + unsigned char v; + read(&v, sizeof(unsigned char)); + return v; +} + +short Reader::readShort() +{ + short v; + read(&v, sizeof(short)); +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(short)); +#endif + return v; +} + +unsigned short Reader::readUShort() +{ + unsigned short v; + read(&v, sizeof(unsigned short)); +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(unsigned short)); +#endif + return v; +} + +int Reader::readInt() +{ + int v; + read(&v, sizeof(int)); +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(int)); +#endif + return v; +} + +unsigned int Reader::readUInt() +{ + unsigned int v; + read(&v, sizeof(unsigned int)); +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(unsigned int)); +#endif + return v; +} + +long long Reader::readLong() +{ + long long v; + read(&v, sizeof(long long)); +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(long long)); +#endif + return v; +} + +unsigned long long Reader::readULong() +{ + unsigned long long v; + read(&v, sizeof(unsigned long long)); +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(unsigned long long)); +#endif + return v; +} + +float Reader::readFloat() +{ + float v; + read(&v, sizeof(float)); +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(float)); +#endif + return v; +} + +double Reader::readDouble() +{ + double v; + read(&v, sizeof(double)); +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(double)); +#endif + return v; +} + +const std::string Reader::readString() +{ + int count = readInt(); + char str[1025]; + std::string v; + while (count > 0) { + int c = (count > 1024) ? 1024 : count; + read(str, c); + v.append(str, 0, c); + count -= c; + } + return v; +} + +void Reader::readObject(Serializable& v) +{ + v.readObject(*this); +} diff --git a/ROPCApplications/daq/slc/base/src/StringUtil.cc b/ROPCApplications/daq/slc/base/src/StringUtil.cc new file mode 100644 index 0000000000000000000000000000000000000000..6f3f624d8badd3cafc92c07d5c17e19b1275ff5f --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/StringUtil.cc @@ -0,0 +1,110 @@ +#include "daq/slc/base/StringUtil.h" + +#include <cstdarg> +#include <cstdio> +#include <sstream> +#include <cctype> +#include <algorithm> + +using namespace Belle2; + +StringList StringUtil::split(const std::string& str, const char type, size_t max) +{ + StringList str_v; + size_t current = 0, found; + while ((found = str.find_first_of(type, current)) != std::string::npos) { + str_v.push_back(std::string(str, current, found - current)); + current = found + 1; + } + if (str.size() - current > 0) { + str_v.push_back(std::string(str, current, str.size() - current)); + } + while (max > 0 && str_v.size() < max) { + str_v.push_back(""); + } + return str_v; +} + +std::string StringUtil::join(StringList str_v, const std::string& s, size_t start, size_t end) +{ + std::stringstream ss; + for (size_t i = start; i < str_v.size();) { + ss << str_v[i]; + i++; + if ((end > 0 && i == end) || i == str_v.size()) break; + ss << s; + } + return ss.str(); +} + +std::string StringUtil::replace(const std::string& source, + const std::string& pattern, + const std::string& placement) +{ + std::string result; + std::string::size_type pos_before = 0; + std::string::size_type pos = 0; + std::string::size_type len = pattern.size(); + while ((pos = source.find(pattern, pos)) != std::string::npos) { + result.append(source, pos_before, pos - pos_before); + result.append(placement); + pos += len; + pos_before = pos; + } + result.append(source, pos_before, source.size() - pos_before); + return result; +} + +std::string StringUtil::form(const std::string& str, ...) +{ + va_list ap; + static char ss[1024 * 10]; + va_start(ap, str); + vsprintf(ss, str.c_str(), ap); + va_end(ap); + return ss; +} + +std::string StringUtil::toupper(const std::string& str) +{ + std::string s = str; + transform(s.begin(), s.end(), s.begin(), ::toupper); + return s; +} + +std::string StringUtil::tolower(const std::string& str) +{ + std::string s = str; + transform(s.begin(), s.end(), s.begin(), ::tolower); + return s; +} + +int StringUtil::atoi(const std::string str) +{ + return atoi(str.c_str()); +} + +double StringUtil::atof(const std::string str) +{ + return atof(str.c_str()); +} + +long long StringUtil::atoll(const std::string str) +{ + return atoll(str.c_str()); +} + +bool StringUtil::find(const std::string& s, const std::string& str) +{ + return s.find(str) != std::string::npos; +} + +bool StringUtil::isdigit(const std::string& s) +{ + if (s.find("0x") == 0) return true; + for (size_t i = 0; i < s.size(); i++) { + if (i == 0 && s.at(0) == '-') continue; + if (!::isdigit(s.at(i))) return false; + } + return s.size() > 0 && s != "-"; +} diff --git a/ROPCApplications/daq/slc/base/src/TimeoutException.cc b/ROPCApplications/daq/slc/base/src/TimeoutException.cc new file mode 100644 index 0000000000000000000000000000000000000000..d43516cfe13d5bae209397d8281c86789e54a675 --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/TimeoutException.cc @@ -0,0 +1,28 @@ +#include "daq/slc/base/TimeoutException.h" + +#include <cstdarg> +#include <cstdio> + +using namespace Belle2; + +TimeoutException::TimeoutException(const std::string& format, ...) +{ + va_list ap; + char ss[1024 * 100]; + va_start(ap, format); + vsprintf(ss, format.c_str(), ap); + va_end(ap); + m_comment = ss; +} + +TimeoutException::TimeoutException(int err, const std::string& format, ...) +{ + m_err = err; + va_list ap; + char ss[1024 * 100]; + va_start(ap, format); + vsprintf(ss, format.c_str(), ap); + va_end(ap); + m_comment = ss; +} + diff --git a/ROPCApplications/daq/slc/base/src/Writer.cc b/ROPCApplications/daq/slc/base/src/Writer.cc new file mode 100644 index 0000000000000000000000000000000000000000..d764e6d9fba331406df1c635f49b87f29e3a6aab --- /dev/null +++ b/ROPCApplications/daq/slc/base/src/Writer.cc @@ -0,0 +1,109 @@ +#include "daq/slc/base/Writer.h" + +#include "daq/slc/base/Serializable.h" + +#include <unistd.h> + +#define __ENDIAN_L__ + +using namespace Belle2; + +void Writer::reverse(void* buf, size_t len) +{ + char tmp; + for (int c = 0 ; c < (int)len / 2 ; c ++) { + tmp = *((char*)buf + c); + *((char*)buf + c) = *((char*)buf + len - 1 - c); + *((char*)buf + len - 1 - c) = tmp; + } +} + +void Writer::writeBool(bool v) +{ + write(&v, sizeof(bool)); +} + +void Writer::writeChar(char v) +{ + write(&v, sizeof(char)); +} + +void Writer::writeUChar(unsigned char v) +{ + write(&v, sizeof(unsigned char)); +} + +void Writer::writeShort(short v) +{ +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(short)); +#endif + write(&v, sizeof(short)); +} + +void Writer::writeUShort(unsigned short v) +{ +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(unsigned short)); +#endif + write(&v, sizeof(unsigned short)); +} + +void Writer::writeInt(int v) +{ +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(int)); +#endif + write(&v, sizeof(int)); +} + +void Writer::writeUInt(unsigned int v) +{ +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(unsigned int)); +#endif + write(&v, sizeof(unsigned int)); +} + +void Writer::writeLong(long long v) +{ +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(long long)); +#endif + write(&v, sizeof(long long)); +} + +void Writer::writeULong(unsigned long long v) +{ +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(unsigned long long)); +#endif + write(&v, sizeof(unsigned long long)); +} + +void Writer::writeFloat(float v) +{ +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(float)); +#endif + write(&v, sizeof(float)); +} + +void Writer::writeDouble(double v) +{ +#if defined(__ENDIAN_L__) + reverse(&v, sizeof(double)); +#endif + write(&v, sizeof(double)); +} + +void Writer::writeString(const std::string& v) +{ + writeInt((int)v.size()); + write(v.data(), v.size()); +} + +void Writer::writeObject(const Serializable& v) +{ + return v.writeObject(*this); +} diff --git a/ROPCApplications/daq/slc/readout/include/FlowMonitor.h b/ROPCApplications/daq/slc/readout/include/FlowMonitor.h new file mode 100644 index 0000000000000000000000000000000000000000..9663de6c87355d188842df6711d88f6e311d807b --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/FlowMonitor.h @@ -0,0 +1,43 @@ +#ifndef _Belle2_FlowMonitor_h +#define _Belle2_FlowMonitor_h + +#include <daq/slc/readout/RunInfoBuffer.h> +#include <daq/slc/readout/IOInfo.h> +#include <daq/slc/readout/ronode_status.h> + +namespace Belle2 { + + class FlowMonitor { + + public: + FlowMonitor() + { + m_ioinfo.push_back(IOInfo()); + m_ioinfo.push_back(IOInfo()); + m_info = NULL; + m_status.connection_in = 0; + m_status.connection_out = 0; + } + ~FlowMonitor() {} + + public: + bool open(RunInfoBuffer* m_info); + bool close(); + ronode_status& monitor(); + const ronode_status& getStatus() const { return m_status; } + ronode_status& getStatus() { return m_status; } + bool isAvailable() const { return m_info != NULL; } + + private: + std::string m_nodename; + RunInfoBuffer* m_info; + std::vector<IOInfo> m_ioinfo; + std::string m_ip[2]; + unsigned long long m_nbyte[2]; + ronode_status m_status; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/readout/include/IOInfo.h b/ROPCApplications/daq/slc/readout/include/IOInfo.h new file mode 100644 index 0000000000000000000000000000000000000000..0c1e2f7728e2d24db4a1a1ee956868c640b8ce91 --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/IOInfo.h @@ -0,0 +1,48 @@ +#ifndef _Belle2_IOInfo_h +#define _Belle2_IOInfo_h + +#include <vector> + +namespace Belle2 { + + class IOInfo { + public: + static int checkTCP(std::vector<IOInfo>& info); + static int checkTCP(IOInfo& info); + + public: + IOInfo() {} + ~IOInfo() {} + + public: + const char* getLocalIP() const; + unsigned int getLocalAddress() const { return m_local_addr; } + int getLocalPort() const { return m_local_port; } + const char* getRemoteIP() const; + unsigned int getRemoteAddress() const { return m_remote_addr; } + int getRemotePort() const { return m_remote_port; } + int getState() const { return m_state; } + int getTXQueue() const { return m_tx_queue; } + int getRXQueue() const { return m_rx_queue; } + void setLocalAddress(unsigned int addr) { m_local_addr = addr; } + void setLocalPort(int port) { m_local_port = port; } + void setRemoteAddress(unsigned int addr) { m_remote_addr = addr; } + void setRemotePort(int port) { m_remote_port = port; } + void setState(int state) { m_state = state; } + void setTXQueue(int queue) { m_tx_queue = queue; } + void setRXQueue(int queue) { m_rx_queue = queue; } + + private: + unsigned int m_local_addr; + int m_local_port; + unsigned int m_remote_addr; + int m_remote_port; + int m_state; + int m_tx_queue; + int m_rx_queue; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/readout/include/LogListener.h b/ROPCApplications/daq/slc/readout/include/LogListener.h new file mode 100644 index 0000000000000000000000000000000000000000..0afac538dc532ae2feb3af74a6e8c506bc27241d --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/LogListener.h @@ -0,0 +1,31 @@ +#ifndef _Belle2_LogListener_hh +#define _Belle2_LogListener_hh + +namespace Belle2 { + + class ProcessController; + + class LogListener { + + public: + LogListener(ProcessController* con, int pipe[2]) + : m_con(con) + { + m_pipe[0] = pipe[0]; + m_pipe[1] = pipe[1]; + } + ~LogListener() {} + + public: + void run(); + + private: + ProcessController* m_con; + int m_pipe[2]; + + }; + +} + +#endif + diff --git a/ROPCApplications/daq/slc/readout/include/ProcessController.h b/ROPCApplications/daq/slc/readout/include/ProcessController.h new file mode 100644 index 0000000000000000000000000000000000000000..e0449174e7d11fcbbf0189bbb88b42e30241b347 --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/ProcessController.h @@ -0,0 +1,110 @@ +#ifndef _Belle2_ProcessController_h +#define _Belle2_ProcessController_h + +#include "daq/slc/readout/RunInfoBuffer.h" + +#include "daq/slc/runcontrol/RCCallback.h" + +#include "daq/slc/system/PThread.h" +#include "daq/slc/system/Process.h" +#include "daq/slc/system/Mutex.h" + +#include <vector> +#include <sstream> + +namespace Belle2 { + + class ProcessController { + + friend class ProcessSubmitter; + + public: + ProcessController() + { + m_callback = NULL; + } + ProcessController(RCCallback* callback) + { + m_callback = callback; + } + ~ProcessController() + { + m_info.close(); + } + + public: + bool init(const std::string& parname, int nodeid = 0); + void clear(); + bool load(int timeout); + bool start(int expno, int runno); + bool pause(); + bool resume(); + bool stop(); + bool abort(); + const std::string& getName() { return m_name; } + const std::string& getParName() { return m_parname; } + const std::string& getExecutable() { return m_exename; } + RunInfoBuffer& getInfo() { return m_info; } + RCCallback* getCallback() { return m_callback; } + const Process& getProcess() const { return m_process; } + Process& getProcess() { return m_process; } + void setCallback(RCCallback* callback) { m_callback = callback; } + void setName(const std::string& name) { m_name = name; } + void setExecutable(const std::string& exe) { m_exename = exe; } + void addArgument(const std::string& arg) { m_arg_v.push_back(arg); } + void addArgument(const char* format, ...); + template<typename T> + void addArgument(T arg); + void clearArguments() { m_arg_v = std::vector<std::string>(); } + bool isAlive() { return m_process.isAlive(); } + bool waitReady(int timeout); + + public: + void lock() { m_mutex.lock(); } + void unlock() { m_mutex.unlock(); } + + private: + RunInfoBuffer m_info; + std::string m_name; + std::string m_parname; + RCCallback* m_callback; + std::string m_exename; + std::vector<std::string> m_arg_v; + Process m_process; + Mutex m_mutex; + std::string m_message; + PThread m_th_log; + PThread m_th_process; + int m_iopipe[2]; + + }; + + template<typename T> + inline void ProcessController::addArgument(T arg) + { + std::stringstream ss; ss << arg; + m_arg_v.push_back(ss.str()); + } + + class ProcessSubmitter { + + public: + ProcessSubmitter(ProcessController* con, int iopipe[2]) + : m_con(con) + { + m_iopipe[0] = iopipe[0]; + m_iopipe[1] = iopipe[1]; + } + + public: + void run(); + + private: + ProcessController* m_con; + int m_iopipe[2]; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/readout/include/ProcessListener.h b/ROPCApplications/daq/slc/readout/include/ProcessListener.h new file mode 100644 index 0000000000000000000000000000000000000000..caae09c66a0f3d19f8d1e426ada7144094e852cf --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/ProcessListener.h @@ -0,0 +1,26 @@ +#ifndef _Belle2_ProcessListener_hh +#define _Belle2_ProcessListener_hh + +namespace Belle2 { + + class ProcessController; + + class ProcessListener { + + public: + ProcessListener(ProcessController* con) + : m_con(con) {} + ~ProcessListener() {} + + public: + void run(); + + private: + ProcessController* m_con; + + }; + +} + +#endif + diff --git a/ROPCApplications/daq/slc/readout/include/RunInfoBuffer.h b/ROPCApplications/daq/slc/readout/include/RunInfoBuffer.h new file mode 100644 index 0000000000000000000000000000000000000000..5359fd950869ba1b0b8ca8e5e68081575e31c111 --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/RunInfoBuffer.h @@ -0,0 +1,122 @@ +#ifndef _Belle2_RunInfoBuffer_h +#define _Belle2_RunInfoBuffer_h + +#include "daq/slc/system/SharedMemory.h" +#include "daq/slc/system/MMutex.h" +#include "daq/slc/system/MCond.h" +#include "daq/slc/readout/ronode_info.h" + +namespace Belle2 { + + class RunInfoBuffer { + + public: + enum State { + NOTREADY = 0, + READY = 1, + RUNNING = 2, + PAUSING = 3, + PAUSED = 4, + RESUMING = 5 + }; + + public: + enum EFlag { + NOERRO, + PROCESS_DOWN = 0x01, + SOCKET_BIND = 0x02, + SOCKET_IN = 0x04, + SOCKET_OUT = 0x08, + EVENTFORMAT = 0x10, + CPRFIFO_EMPTY = 0x20, + CPRFIFO_FULL = 0x40, + CPRFIFO_LEF_FULL = 0x80 + }; + + public: + RunInfoBuffer() + { + m_info = NULL; + } + ~RunInfoBuffer() {} + + public: + size_t size() ; + bool open(const std::string& nodename, + int nodeid = 0, bool recreate = false); + bool init(); + bool close(); + bool unlink(); + bool lock() ; + bool unlock() ; + bool wait() ; + bool wait(int time) ; + bool notify() ; + void clear(); + + public: + const std::string getName() const { return m_nodename; } + const std::string getPath() const { return m_path; } + bool isAvailable() const { return m_info != NULL; } + ronode_info* get() { return m_info; } + unsigned int getNodeId() const { return m_info->nodeid; } + unsigned int getState() const { return m_info->state; } + unsigned int getErrorFlag() const { return m_info->eflag; } + unsigned int getExpNumber() const { return m_info->expno; } + unsigned int getRunNumber() const { return m_info->runno; } + unsigned int getSubNumber() const { return m_info->subno; } + int getInputPort() const { return m_info->io[0].port; } + int getInputAddress() const { return m_info->io[0].addr; } + unsigned int getInputCount() const { return m_info->io[0].count; } + unsigned long long getInputNBytes() const { return m_info->io[0].nbyte; } + int getOutputPort() const { return m_info->io[1].port; } + int getOutputAddress() const { return m_info->io[1].addr; } + unsigned int getOutputCount() const { return m_info->io[1].count; } + unsigned long long getOutputNBytes() const { return m_info->io[1].nbyte; } + void setNodeId(unsigned int id) { m_info->nodeid = id; } + void setState(State state) { m_info->state = (unsigned int)state; } + void setErrorFlag(EFlag eflag) { m_info->eflag = (unsigned int)eflag; } + void setExpNumber(unsigned int expno) { m_info->expno = expno; } + void setRunNumber(unsigned int runno) { m_info->runno = runno; } + void setSubNumber(unsigned int subno) { m_info->subno = subno; } + void setInputPort(int port) { m_info->io[0].port = port; } + void setInputAddress(int addr) { m_info->io[0].addr = addr; } + void setInputCount(unsigned int count) { m_info->io[0].count = count; } + void setInputNBytes(unsigned long long nbyte) { m_info->io[0].nbyte = nbyte; } + void addInputCount(unsigned int count) { m_info->io[0].count += count; } + void addInputNBytes(unsigned long long nbyte) { m_info->io[0].nbyte += nbyte; } + void setOutputPort(int port) { m_info->io[1].port = port; } + void setOutputAddress(int addr) { m_info->io[1].addr = addr; } + void setOutputCount(unsigned int count) { m_info->io[1].count = count; } + void setOutputNBytes(unsigned long long nbyte) { m_info->io[1].nbyte = nbyte; } + void addOutputCount(unsigned int count) { m_info->io[1].count += count; } + void addOutputNBytes(unsigned long long nbyte) { m_info->io[1].nbyte += nbyte; } + event_header& getEventHeader() { return m_info->header; } + void copyEventHeader(int* buf); + bool isNotReady() { return (m_info) && m_info->state == NOTREADY; } + bool isReady() { return (m_info) && m_info->state == READY; } + bool isRunning() { return (m_info) && m_info->state == RUNNING; } + bool isPausing() { return (m_info) && m_info->state == PAUSING; } + bool isPaused() { return (m_info) && m_info->state == PAUSED; } + bool isResuming() { return (m_info) && m_info->state == RESUMING; } + + bool waitRunning(int timeout); + bool waitReady(int timeout); + bool reportRunning(); + bool reportError(EFlag eflag); + bool reportReady(); + bool reportNotReady(); + + private: + std::string m_nodename; + std::string m_path; + SharedMemory m_memory; + ronode_info* m_info; + MMutex m_mutex; + MCond m_cond; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/readout/include/mmap_statistics.h b/ROPCApplications/daq/slc/readout/include/mmap_statistics.h new file mode 100644 index 0000000000000000000000000000000000000000..8764663abf740bbf717199861c42eab9e7b2fdbf --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/mmap_statistics.h @@ -0,0 +1,96 @@ +#ifndef MMAP_STATISTICS_H +#define MMAP_STATISTICS_H + +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <assert.h> +#include <stdint.h> +#include <sys/mman.h> + +#ifndef O_NOATIME +#define O_NOATIME 0 +#endif + +struct stream_statistics { + uint32_t addr; + uint32_t port; + uint64_t event; + uint64_t byte; + uint64_t total_byte; +}; + +class eb_statistics { +private: + int m_n_u; + int m_n_d; + int m_fd_u; + int m_fd_d; + int m_len_u; + int m_len_d; + stream_statistics* m_stat_u; + stream_statistics* m_stat_d; + +public: + eb_statistics(const char* file_up, int nup, const char* file_down, int ndown) + { + m_n_u = nup; + m_n_d = ndown; + + m_len_u = sizeof(stream_statistics) * m_n_u; + m_len_d = sizeof(stream_statistics) * m_n_d; + + m_fd_u = open(file_up, O_RDWR | O_CREAT | O_NOATIME, 0644); + assert(m_fd_u >= 0); + m_fd_d = open(file_down, O_RDWR | O_CREAT | O_NOATIME, 0644); + assert(m_fd_d >= 0); + + stream_statistics buf_u[m_n_u]; + stream_statistics buf_d[m_n_d]; + + memset(buf_u, 0, m_len_u); + memset(buf_d, 0, m_len_d); + + int ret; + + ret = write(m_fd_u, buf_u, m_len_u); + assert(ret == m_len_u); + ret = write(m_fd_d, buf_d, m_len_d); + assert(ret == m_len_d); + + m_stat_u = (stream_statistics*)mmap(0, m_len_u, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd_u, 0); + assert(m_stat_u != MAP_FAILED); + m_stat_d = (stream_statistics*)mmap(0, m_len_d, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd_d, 0); + assert(m_stat_d != MAP_FAILED); + }; + + ~eb_statistics() + { + munmap(m_stat_u, m_len_u); + munmap(m_stat_d, m_len_d); + close(m_fd_u); + close(m_fd_d); + }; + + stream_statistics* up() + { + return m_stat_u; + }; + + stream_statistics& up(int i) + { + return m_stat_u[i]; + }; + + stream_statistics* down() + { + return m_stat_d; + }; + + stream_statistics& down(int i) + { + return m_stat_d[i]; + }; +}; + +#endif diff --git a/ROPCApplications/daq/slc/readout/include/mybuf.h b/ROPCApplications/daq/slc/readout/include/mybuf.h new file mode 100644 index 0000000000000000000000000000000000000000..9de1446b8dd6362c4516ea924653ac304becfe2e --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/mybuf.h @@ -0,0 +1,28 @@ +#ifndef mybuf_h +#define mybuf_h + +const int mybuf_revision = 3; + +struct mybuf { + uint32 reserved[300000]; + uint32 nodeid; + uint32 state; + uint32 ctime; + uint32 eflag; + uint32 expno; + uint32 runno; + uint32 subno; + uint32 reserved2[2]; + struct io_status { + uint32 nqueue; + uint32 state; + uint32 count; + uint32 reserved_i; + float freq; + float evtsize; + float rate; + float reserved_f; + } io[2]; +}; + +#endif diff --git a/ROPCApplications/daq/slc/readout/include/ro_summary.h b/ROPCApplications/daq/slc/readout/include/ro_summary.h new file mode 100644 index 0000000000000000000000000000000000000000..1945f8560699512fac221e731824a6ddc6c0f182 --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/ro_summary.h @@ -0,0 +1,50 @@ +#ifndef _Belle2_ro_summary_h +#define _Belle2_ro_summary_h + +extern "C" { +#include <nsm2/nsm2.h> +} + +namespace Belle2 { + + const int ro_summary_revision = 1; + + struct ro_summary { + uint32 nodeid; + uint32 state; + uint32 eflag; + uint32 expno; + uint32 runno; + uint32 subno; + uint32 reserved_i[2]; + uint32 stime; + uint32 ctime; + uint32 nevent_in; + uint32 nqueue_in; + uint32 connection_in; + uint32 connection_out; + uint32 nevent_out; + uint32 nqueue_out; + float evtrate_in; + float evtsize_in; + float flowrate_in; + float evtrate_out; + float evtsize_out; + float flowrate_out; + float loadavg; + float reserved_f[5]; + struct event_header { + uint32 nword; + uint32 format; + uint32 exp_run; + uint32 evtno; + uint32 ctime_trgtype; + uint32 utime; + uint32 nodeid; + uint32 crc_err; + } header[25]; + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/readout/include/ronode_info.h b/ROPCApplications/daq/slc/readout/include/ronode_info.h new file mode 100644 index 0000000000000000000000000000000000000000..9292e6283f6088f8ae470931cfdb3787a6f4e06f --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/ronode_info.h @@ -0,0 +1,39 @@ +#ifndef _Belle2_ronode_info_h +#define _Belle2_ronode_info_h + +const int ronode_info_revision = 1; + +namespace Belle2 { + + struct event_header { + unsigned int nword; + unsigned int format; + unsigned int exp_run; + unsigned int evtno; + unsigned int ctime_trgtype; + unsigned int utime; + unsigned int nodeid; + unsigned int crc_err; + }; + + struct ronode_info { + unsigned int nodeid; + unsigned int state; + unsigned int eflag; + unsigned int expno; + unsigned int runno; + unsigned int subno; + unsigned int reserved[2]; + float reserved_f[5]; + struct io_info { + int port; + unsigned int addr; + unsigned long long nbyte; + unsigned int count; + } io[2]; + event_header header; + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/readout/include/ronode_status.h b/ROPCApplications/daq/slc/readout/include/ronode_status.h new file mode 100644 index 0000000000000000000000000000000000000000..846ef2b14f0df34664bb850926b1406650a71933 --- /dev/null +++ b/ROPCApplications/daq/slc/readout/include/ronode_status.h @@ -0,0 +1,50 @@ +#ifndef _Belle2_ronode_status_h +#define _Belle2_ronode_status_h + +extern "C" { +#include <nsm2/nsm2.h> +} + +namespace Belle2 { + + const int ronode_status_revision = 1; + + struct ronode_status { + uint32 nodeid; + uint32 state; + uint32 eflag; + uint32 expno; + uint32 runno; + uint32 subno; + uint32 reserved_i[2]; + uint32 stime; + uint32 ctime; + uint32 nevent_in; + uint32 nqueue_in; + uint32 connection_in; + uint32 connection_out; + uint32 nevent_out; + uint32 nqueue_out; + float evtrate_in; + float evtsize_in; + float flowrate_in; + float evtrate_out; + float evtsize_out; + float flowrate_out; + float loadavg; + float reserved_f[5]; + struct event_header { + uint32 nword; + uint32 format; + uint32 exp_run; + uint32 evtno; + uint32 ctime_trgtype; + uint32 utime; + uint32 nodeid; + uint32 crc_err; + } header; + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/readout/src/FlowMonitor.cc b/ROPCApplications/daq/slc/readout/src/FlowMonitor.cc new file mode 100644 index 0000000000000000000000000000000000000000..2b68d42e2a02981712390f0d75b2c9a7805b16ff --- /dev/null +++ b/ROPCApplications/daq/slc/readout/src/FlowMonitor.cc @@ -0,0 +1,98 @@ +#include "daq/slc/readout/FlowMonitor.h" + +#include <daq/slc/system/Time.h> + +#include <cstring> + +using namespace Belle2; + +bool FlowMonitor::open(RunInfoBuffer* info) +{ + m_info = info; + if (m_info->isAvailable()) { + m_status.nodeid = m_info->getNodeId(); + m_nbyte[0] = m_nbyte[1] = 0; + m_ioinfo[0].setLocalPort(m_info->getInputPort()); + m_ioinfo[0].setLocalAddress(m_info->getInputAddress()); + m_ip[0] = m_ioinfo[0].getLocalIP(); + m_ioinfo[1].setLocalPort(m_info->getOutputPort()); + m_ioinfo[1].setLocalAddress(m_info->getOutputAddress()); + m_ip[1] = m_ioinfo[1].getLocalIP(); + } + return true; +} + +bool FlowMonitor::close() +{ + return true; +} + +ronode_status& FlowMonitor::monitor() +{ + if (m_info->isAvailable()) { + m_ioinfo[0].setLocalAddress(m_info->getInputAddress()); + m_ioinfo[0].setLocalPort(m_info->getInputPort()); + m_ioinfo[1].setLocalAddress(m_info->getOutputAddress()); + m_ioinfo[1].setLocalPort(m_info->getOutputPort()); + IOInfo::checkTCP(m_ioinfo); + unsigned int ctime = Time().getSecond(); + ronode_info info; + memcpy(&info, m_info->get(), sizeof(ronode_info)); + memcpy(&(m_status.header), &(info.header), sizeof(event_header)); + double length = ctime - m_status.ctime; + m_status.eflag = info.eflag; + m_status.state = info.state; + m_status.expno = info.expno; + m_status.runno = info.runno; + m_status.subno = info.subno; + m_status.reserved_i[0] = info.reserved[0]; + m_status.reserved_i[1] = info.reserved[1]; + m_status.reserved_f[0] = info.reserved_f[0]; + m_status.reserved_f[1] = info.reserved_f[1]; + m_status.reserved_f[2] = info.reserved_f[2]; + m_status.reserved_f[3] = info.reserved_f[3]; + m_status.ctime = ctime; + + double dcount; + double dnbyte; + m_status.evtrate_in = 0; + m_status.evtsize_in = 0; + m_status.flowrate_in = 0; + m_status.evtrate_out = 0; + m_status.evtsize_out = 0; + m_status.flowrate_out = 0; + if (m_ioinfo[0].getLocalPort() > 0) { + m_status.connection_in = m_ioinfo[0].getState() == 1; + m_status.nqueue_in = m_ioinfo[0].getRXQueue(); + } + if (m_ioinfo[1].getLocalPort() > 0) { + m_status.connection_out = m_ioinfo[1].getState() == 1; + m_status.nqueue_out = m_ioinfo[1].getRXQueue(); + } + if ((dcount = info.io[0].count - m_status.nevent_in) > 0) { + dnbyte = info.io[0].nbyte - m_nbyte[0]; + m_status.evtrate_in = dcount / length / 1000.; + m_status.evtsize_in = dnbyte / dcount / 1000.; + m_status.flowrate_in = dnbyte / length / 1000000.; + m_status.nevent_in = info.io[0].count; + m_nbyte[0] = info.io[0].nbyte; + } else { + m_status.evtrate_in = 0; + m_status.evtsize_in = 0; + m_status.flowrate_in = 0; + } + if ((dcount = info.io[1].count - m_status.nevent_out) > 0) { + dnbyte = info.io[1].nbyte - m_nbyte[1]; + m_status.evtrate_out = dcount / length / 1000.; + m_status.evtsize_out = dnbyte / dcount / 1000.; + m_status.flowrate_out = dnbyte / length / 1000000.; + m_status.nevent_out = info.io[1].count; + m_nbyte[1] = info.io[1].nbyte; + } else { + m_status.evtrate_out = 0; + m_status.evtsize_out = 0; + m_status.flowrate_out = 0; + } + } + return m_status; +} diff --git a/ROPCApplications/daq/slc/readout/src/IOInfo.cc b/ROPCApplications/daq/slc/readout/src/IOInfo.cc new file mode 100644 index 0000000000000000000000000000000000000000..7bbf93dc7cde61f6b510fb2541b439f0ba3e6832 --- /dev/null +++ b/ROPCApplications/daq/slc/readout/src/IOInfo.cc @@ -0,0 +1,81 @@ +#include "daq/slc/readout/IOInfo.h" + +#include <cstdlib> +#include <cstdio> + +#include <fstream> +#include <sstream> +#include <arpa/inet.h> + +using namespace Belle2; + +int IOInfo::checkTCP(IOInfo& info) +{ + std::vector<IOInfo> info_v; + info_v.push_back(info); + int ret = checkTCP(info_v); + info = info_v[0]; + return ret; +} + +int IOInfo::checkTCP(std::vector<IOInfo>& info) +{ + size_t count = 0; + for (size_t i = 0; i < info.size(); i++) { + info[i].setState(0); + info[i].setTXQueue(0); + info[i].setRXQueue(0); + } + count = 0; + std::string line; + std::stringstream ss; + std::string sl, local_address, rem_address, st, queue; + std::ifstream fin("/proc/net/tcp"); + getline(fin, line); + while (fin && getline(fin, line)) { + ss.str(""); + ss << line; + ss >> sl >> local_address >> rem_address >> st >> queue; + unsigned int addr = strtoul(local_address.substr(0, 8).c_str(), NULL, 16); + int port = strtoul(local_address.substr(9).c_str(), NULL, 16); + unsigned int raddr = strtoul(rem_address.substr(0, 8).c_str(), NULL, 16); + int rport = strtoul(rem_address.substr(9).c_str(), NULL, 16); + for (size_t i = 0; i < info.size(); i++) { + if (info[i].getState() > 0) continue; + int sti = strtoul(st.substr(0).c_str(), NULL, 16); + if (//addr == info[i].getLocalAddress() && + port == info[i].getLocalPort() && sti == 1) { + info[i].setRemoteAddress(raddr); + info[i].setRemotePort(rport); + info[i].setState(sti); + info[i].setTXQueue(strtoul(queue.substr(0, 8).c_str(), NULL, 16)); + info[i].setRXQueue(strtoul(queue.substr(9).c_str(), NULL, 16)); + count++; + } else if (raddr == info[i].getRemoteAddress() && + rport == info[i].getRemotePort() && sti == 1) { + info[i].setLocalAddress(addr); + info[i].setLocalPort(port); + info[i].setState(sti); + info[i].setTXQueue(strtoul(queue.substr(0, 8).c_str(), NULL, 16)); + info[i].setRXQueue(strtoul(queue.substr(9).c_str(), NULL, 16)); + count++; + } + if (count == info.size()) return count; + } + } + return count; +} + +const char* IOInfo::getLocalIP() const +{ + sockaddr_in addr; + addr.sin_addr.s_addr = m_local_addr; + return inet_ntoa(addr.sin_addr); +} + +const char* IOInfo::getRemoteIP() const +{ + sockaddr_in addr; + addr.sin_addr.s_addr = m_remote_addr; + return inet_ntoa(addr.sin_addr); +} diff --git a/ROPCApplications/daq/slc/readout/src/LogListener.cc b/ROPCApplications/daq/slc/readout/src/LogListener.cc new file mode 100644 index 0000000000000000000000000000000000000000..6edd9df2de19c8b854a0c78bd59cda5c28cfa8e0 --- /dev/null +++ b/ROPCApplications/daq/slc/readout/src/LogListener.cc @@ -0,0 +1,131 @@ +#include "daq/slc/readout/LogListener.h" + +#include "daq/slc/readout/ProcessController.h" + +#include <daq/slc/runcontrol/RCCallback.h> +#include <daq/slc/runcontrol/RCCommand.h> +#include <daq/slc/nsm/NSMCommunicator.h> + +#include <daq/slc/system/File.h> +#include <daq/slc/system/FileReader.h> + +#include <daq/slc/base/IOException.h> +#include <daq/slc/base/StringUtil.h> +#include <daq/slc/system/LogFile.h> + +#include <iostream> +#include <sstream> +#include <ctype.h> + +namespace Belle2 { + + typedef FileReader<File> PipeReader; + +} + +using namespace Belle2; + +void LogListener::run() +{ + File fd(m_pipe[0]); + PipeReader preader(fd); + char c; + std::stringstream ss; + std::string s; + LogFile::Priority priority = LogFile::UNKNOWN; + int count = 0; + try { + while (true) { + c = preader.readChar(); + if (c != '\n' && iscntrl(c)) continue; + if (c == '\n' && count > 0) { + s = m_con->getParName() + " : " + ss.str(); + ss.str(""); + //m_con->lock(); + if (priority == LogFile::UNKNOWN) { + priority = LogFile::DEBUG; + } + if (priority > LogFile::DEBUG) { + m_con->getCallback()->log(priority, s); + } else { + LogFile::debug(s); + } + if (m_con->getCallback()->getNode().getState() == RCState::RUNNING_S) { + if (priority == LogFile::ERROR) { + // m_con->getCallback()->log(LogFile::ERROR, s); + } else if (priority == LogFile::FATAL) { + // m_con->getCallback()->log(LogFile::FATAL, s)); + m_con->getCallback()->setState(RCState::ERROR_ES); + } + } + //m_con->unlock(); + count = 0; + priority = LogFile::UNKNOWN; + } else if (isprint(c)) { + if (count == 0 && c == '[') { + ss << c; + while (true) { + c = preader.readChar(); + if (c == ']') { + ss << c; + s = ss.str(); + if (s == "[DEBUG]") priority = LogFile::DEBUG; + else if (s == "[INFO]") priority = LogFile::INFO; + else if (s == "[NOTICE]") priority = LogFile::NOTICE; + else if (s == "[WARNING]") priority = LogFile::WARNING; + else if (s == "[ERROR]") priority = LogFile::ERROR; + else if (s == "[FATAL]") priority = LogFile::FATAL; + else if (s.find("STOP") != std::string::npos) { + StringList sl = StringUtil::split(s, '='); + if (sl.size() > 1) { + std::string nodename = StringUtil::replace(sl[1], "]", ""); + try { + NSMCommunicator::send(NSMMessage(NSMNode(nodename), RCCommand::STOP)); + } catch (const std::exception& e) { + } + } + count = 0; + ss.str(""); + break; + } + if (priority > 0) { + count = 0; + ss.str(""); + } else { + priority = LogFile::DEBUG; + } + break; + } + ss << c; + } + } else { + if (c != ' ' || count > 0) { + ss << c; + count++; + } + } + } + } + } catch (const IOException& e) { + LogFile::debug(e.what()); + if (count > 0) { + s = m_con->getParName() + " : " + ss.str(); + ss.str(""); + //m_con->lock(); + if (priority == LogFile::UNKNOWN) { + priority = LogFile::DEBUG; + } + m_con->getCallback()->log(priority, s); + if (m_con->getCallback()->getNode().getState() == RCState::RUNNING_S) { + if (priority == LogFile::ERROR) { + //m_con->getCallback()->reply(NSMMessage(NSMCommand::ERROR, s)); + } else if (priority == LogFile::FATAL) { + //m_con->getCallback()->reply(NSMMessage(NSMCommand::ERROR, s)); + m_con->getCallback()->setState(RCState::ERROR_ES); + } + } + //m_con->unlock(); + } + } + close(m_pipe[0]); +} diff --git a/ROPCApplications/daq/slc/readout/src/ProcessController.cc b/ROPCApplications/daq/slc/readout/src/ProcessController.cc new file mode 100644 index 0000000000000000000000000000000000000000..c5ec3219c8bf3bc150cf2e99d4b90c70709bc061 --- /dev/null +++ b/ROPCApplications/daq/slc/readout/src/ProcessController.cc @@ -0,0 +1,162 @@ +#include "daq/slc/readout/ProcessController.h" + +#include "daq/slc/readout/ProcessListener.h" +#include "daq/slc/readout/LogListener.h" +#include "daq/slc/runcontrol/RCHandlerException.h" + +#include "daq/slc/system/Executor.h" +#include "daq/slc/system/PThread.h" +#include "daq/slc/system/LogFile.h" + +#include "daq/slc/base/StringUtil.h" +#include <daq/slc/system/LockGuard.h> + +#include <cstdio> +#include <unistd.h> + + +using namespace Belle2; + +bool ProcessController::init(const std::string& parname, int nodeid) +{ + m_name = StringUtil::tolower(m_callback->getNode().getName()); + m_parname = parname; + LogFile::open(m_name + "_" + m_parname); + if (!m_info.open(m_name + "_" + m_parname, nodeid, true)) { + return false; + } + m_callback->add(new NSMVHandlerInt(m_parname + ".pid", true, false, 0), false, false); + return true; +} + +void ProcessController::clear() +{ + m_info.clear(); +} + +bool ProcessController::waitReady(int timeout) +{ + if (!m_info.waitReady(timeout)) { + return false; + } + return true; +} + +bool ProcessController::load(int timeout) +{ + m_info.clear(); + m_process.cancel(); + m_process.kill(SIGQUIT); + if (pipe(m_iopipe) < 0) { + perror("pipe"); + return false; + } + m_process = Process(new ProcessSubmitter(this, m_iopipe)); + m_th_log = PThread(new LogListener(this, m_iopipe)); + m_th_process = PThread(new ProcessListener(this)); + //close(iopipe[1]); + if (timeout > 0) { + if (!m_info.waitReady(timeout)) { + throw (RCHandlerException("Failed to boot " + m_parname)); + return false; + } + } + m_callback->set(m_parname + ".pid", m_process.get_id()); + return true; +} + +bool ProcessController::start(int expno, int runno) +{ + GenericLockGuard<RunInfoBuffer> lockGuard(m_info); + m_info.setExpNumber(expno); + m_info.setRunNumber(runno); + m_info.setSubNumber(0); + /* + if (m_info.getState() != RunInfoBuffer::RUNNING) { + throw (RCHandlerException(m_parname + " is not running")); + } + */ + return true; +} + +bool ProcessController::stop() +{ + GenericLockGuard<RunInfoBuffer> lockGuard(m_info); + m_info.setExpNumber(0); + m_info.setRunNumber(0); + m_info.setSubNumber(0); + m_info.setInputCount(0); + m_info.setInputNBytes(0); + m_info.setOutputCount(0); + m_info.setOutputNBytes(0); + return true; +} + +bool ProcessController::pause() +{ + GenericLockGuard<RunInfoBuffer> lockGuard(m_info); + if (m_info.isRunning()) { + m_info.setState(RunInfoBuffer::PAUSING); + } else { + LogFile::warning("Process is not running. Pause request was ignored."); + } + return true; +} + +bool ProcessController::resume() +{ + GenericLockGuard<RunInfoBuffer> lockGuard(m_info); + if (m_info.isPaused()) { + m_info.setState(RunInfoBuffer::RESUMING); + } else { + LogFile::warning("Process is not paused. Resume request was ignored."); + } + return true; +} + +bool ProcessController::abort() +{ + m_info.clear(); + m_process.kill(SIGINT); + if (getExecutable() == "basf2") { + usleep(100000); + m_process.kill(SIGQUIT); + m_process.kill(SIGKILL); + } + if (m_callback != NULL) + m_callback->set(m_parname + ".pid", -1); + m_process.wait(); + m_th_log.cancel(); + m_th_process.cancel(); + close(m_iopipe[1]); + close(m_iopipe[0]); + return true; +} + +void ProcessSubmitter::run() +{ + //close(1); + dup2(m_iopipe[1], 1); + //close(2); + dup2(m_iopipe[1], 2); + close(m_iopipe[0]); + Executor executor; + if (m_con->getExecutable().size() == 0) { + m_con->setExecutable("basf2"); + } + executor.setExecutable(m_con->getExecutable()); + for (size_t i = 0; i < m_con->m_arg_v.size(); i++) { + executor.addArg(m_con->m_arg_v[i]); + } + executor.execute(); +} + +void ProcessController::addArgument(const char* format, ...) +{ + va_list ap; + static char ss[1024]; + va_start(ap, format); + vsprintf(ss, format, ap); + va_end(ap); + m_arg_v.push_back(ss); +} diff --git a/ROPCApplications/daq/slc/readout/src/ProcessListener.cc b/ROPCApplications/daq/slc/readout/src/ProcessListener.cc new file mode 100644 index 0000000000000000000000000000000000000000..133ec431f683c8c78c785c6abc87b1d2332cc8a6 --- /dev/null +++ b/ROPCApplications/daq/slc/readout/src/ProcessListener.cc @@ -0,0 +1,37 @@ +#include "daq/slc/readout/ProcessListener.h" +#include "daq/slc/readout/ProcessController.h" + +#include <daq/slc/runcontrol/RCCallback.h> + +#include <daq/slc/system/LogFile.h> +#include <daq/slc/system/Process.h> + +using namespace Belle2; + +void ProcessListener::run() +{ + Process& process(m_con->getProcess()); + std::string process_name = m_con->getParName(); + RCCallback& callback(*(m_con->getCallback())); + if (not process.wait()) { + callback.log(LogFile::FATAL, "Failed to wait processed process %s", + process_name.c_str()); + return; + } + callback.log(LogFile::DEBUG, process_name + " : termineted"); + callback.log(LogFile::DEBUG, "%s : waitpid_returned %d status = %d", process_name.c_str(), process.waitpid_result(), + process.waitpid_status()); + process.set_id(-1); + callback.set(m_con->getParName() + ".pid", -1); + /* + try { + callback.monitor(); + } catch (const RCHandlerFatalException& e) { + callback.log(LogFile::FATAL, e.what()); + } catch (const RCHandlerException& e) { + callback.log(LogFile::ERROR, e.what()); + } catch (const std::exception& e) { + callback.log(LogFile::FATAL, "Unknown exception: %s. terminating process", e.what()); + } + */ +} diff --git a/ROPCApplications/daq/slc/readout/src/RunInfoBuffer.cc b/ROPCApplications/daq/slc/readout/src/RunInfoBuffer.cc new file mode 100644 index 0000000000000000000000000000000000000000..e15f59dd04e0e33fe67c41a11a6af165edfac00a --- /dev/null +++ b/ROPCApplications/daq/slc/readout/src/RunInfoBuffer.cc @@ -0,0 +1,170 @@ +#include "daq/slc/readout/RunInfoBuffer.h" + +#include <daq/slc/system/LogFile.h> +#include <daq/slc/system/LockGuard.h> + +#include <cstring> +#include <cstdlib> + +using namespace Belle2; + +size_t RunInfoBuffer::size() +{ + return m_mutex.size() + m_cond.size() + + sizeof(ronode_info); +} + +bool RunInfoBuffer::open(const std::string& nodename, + int nodeid, bool recreate) +{ + m_nodename = nodename; + std::string username = getenv("USER"); + m_path = "/run_info_" + username + "_" + nodename; + if (!m_memory.open(m_path, size())) { + perror("shm_open"); + LogFile::fatal("Failed to open %s", m_path.c_str()); + return false; + } + char* buf = (char*)m_memory.map(0, size()); + if (buf == NULL) { + return false; + } + m_mutex = MMutex(buf); + buf += m_mutex.size(); + m_cond = MCond(buf); + buf += m_cond.size(); + m_info = (ronode_info*)buf; + if (recreate) init(); + if (nodeid > 0) setNodeId(nodeid); + return true; +} + +bool RunInfoBuffer::init() +{ + if (m_info == NULL) return false; + m_mutex.init(); + m_cond.init(); + memset(m_info, 0, sizeof(ronode_info)); + LogFile::debug("Initialized %s", m_path.c_str()); + return true; +} + +void RunInfoBuffer::clear() +{ + if (m_info == NULL) return; + MLockGuard lockGuard(m_mutex); + memset(m_info, 0, sizeof(ronode_info)); +} + +bool RunInfoBuffer::close() +{ + m_memory.close(); + return true; +} + +bool RunInfoBuffer::unlink() +{ + m_memory.unlink(); + m_memory.close(); + return true; +} + +bool RunInfoBuffer::lock() +{ + if (m_info == NULL) return false; + return m_mutex.lock(); +} + +bool RunInfoBuffer::unlock() +{ + if (m_info == NULL) return false; + return m_mutex.unlock(); +} + +bool RunInfoBuffer::wait() +{ + if (m_info == NULL) return false; + return m_cond.wait(m_mutex); +} + +bool RunInfoBuffer::wait(int time) +{ + if (m_info == NULL) return false; + return m_cond.wait(m_mutex, time, 0); +} + +bool RunInfoBuffer::notify() +{ + if (m_info == NULL) return false; + return m_cond.broadcast(); +} + +bool RunInfoBuffer::waitRunning(int timeout) +{ + if (m_info == NULL) return false; + MLockGuard lockGuard(m_mutex); + if (getState() != RunInfoBuffer::RUNNING) { + if (!wait(timeout)) { + return false; + } + } + return true; +} + +bool RunInfoBuffer::waitReady(int timeout) +{ + if (m_info == NULL) return false; + MLockGuard lockGuard(m_mutex); + if (getState() != RunInfoBuffer::READY && + getState() != RunInfoBuffer::RUNNING) { + if (!wait(timeout)) { + if (getState() != RunInfoBuffer::READY && + getState() != RunInfoBuffer::RUNNING) { + return false; + } + } + } + return true; +} + +bool RunInfoBuffer::reportRunning() +{ + if (m_info == NULL) return false; + MLockGuard lockGuard(m_mutex); + setState(RunInfoBuffer::RUNNING); + notify(); + return true; +} + +bool RunInfoBuffer::reportError(EFlag eflag) +{ + if (m_info == NULL) return false; + MLockGuard lockGuard(m_mutex); + setErrorFlag(eflag); + notify(); + return true; +} + +bool RunInfoBuffer::reportReady() +{ + if (m_info == NULL) return false; + MLockGuard lockGuard(m_mutex); + setState(RunInfoBuffer::READY); + notify(); + return true; +} + +bool RunInfoBuffer::reportNotReady() +{ + if (m_info == NULL) return false; + MLockGuard lockGuard(m_mutex); + setState(RunInfoBuffer::NOTREADY); + notify(); + return true; +} + +void RunInfoBuffer::copyEventHeader(int* buf) +{ + if (m_info == NULL) return; + memcpy(&(m_info->header), buf, sizeof(event_header)); +} diff --git a/ROPCApplications/daq/slc/system/include/Buffer.h b/ROPCApplications/daq/slc/system/include/Buffer.h new file mode 100644 index 0000000000000000000000000000000000000000..1bffcfee19330f058884eac0c2978e7c5c32b73b --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Buffer.h @@ -0,0 +1,38 @@ +#ifndef _Belle2_Buffer_hh +#define _Belle2_Buffer_hh + +#include <cstddef> + +namespace Belle2 { + + class Buffer { + + // constructors & destructors + public: + Buffer(); + Buffer(unsigned int size, unsigned char* memory = NULL); + Buffer(const Buffer& w); + virtual ~Buffer(); + + // member methods implemented + public: + unsigned char* ptr() { return m_memory; } + const unsigned char* ptr() const { return m_memory; } + unsigned int size() const { return m_size; } + virtual bool available() { return m_memory != NULL; } + + // operators + public: + const Buffer& operator = (const Buffer&); + + // member data + protected: + unsigned char* m_memory; + unsigned int m_size; + bool m_allocated; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/BufferedReader.h b/ROPCApplications/daq/slc/system/include/BufferedReader.h new file mode 100644 index 0000000000000000000000000000000000000000..9884994ccd6a79fbf13ea52eea9fbfcecf35906b --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/BufferedReader.h @@ -0,0 +1,41 @@ +#ifndef _Belle2_BufferedReader_hh +#define _Belle2_BufferedReader_hh + +#include "daq/slc/base/Reader.h" + +namespace Belle2 { + + class BufferedReader : public Reader { + + // constructors & destructors + public: + BufferedReader(); + BufferedReader(size_t size, unsigned char* memory = NULL); + BufferedReader(const BufferedReader& r); + virtual ~BufferedReader(); + + // member methods implemented + public: + void seekTo(size_t pos) { m_pos = pos; } + unsigned char* ptr() { return m_memory; } + size_t count() const { return m_pos; } + size_t size() const { return m_size; } + void copy(const void*, size_t); + virtual size_t read(void*, size_t); + virtual bool available() { return true; } + + public: + const BufferedReader& operator = (const BufferedReader&); + + // member data + protected: + unsigned char* m_memory; + size_t m_size; + size_t m_pos; + bool m_allocated; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/BufferedWriter.h b/ROPCApplications/daq/slc/system/include/BufferedWriter.h new file mode 100644 index 0000000000000000000000000000000000000000..7abab62d293c76463980dcbd545568b8fd1a4340 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/BufferedWriter.h @@ -0,0 +1,41 @@ +#ifndef _Belle2_BufferedWriter_hh +#define _Belle2_BufferedWriter_hh + +#include "daq/slc/base/Writer.h" + +namespace Belle2 { + + class BufferedWriter : public Writer { + + // constructors & destructors + public: + BufferedWriter(); + BufferedWriter(size_t size, unsigned char* memory = NULL); + BufferedWriter(const BufferedWriter& w); + virtual ~BufferedWriter(); + + // member methods implemented + public: + void seekTo(size_t pos) { m_pos = pos; } + unsigned char* ptr() { return m_memory; } + size_t count() const { return m_pos; } + size_t size() const { return m_size; } + virtual size_t write(const void*, size_t); + virtual bool available() { return true; } + + // operators + public: + const BufferedWriter& operator = (const BufferedWriter&); + + // member data + protected: + unsigned char* m_memory; + size_t m_size; + size_t m_pos; + bool m_allocated; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/Cond.h b/ROPCApplications/daq/slc/system/include/Cond.h new file mode 100644 index 0000000000000000000000000000000000000000..08d2b5bcf01330561b63c2b61d06eb521b48bae4 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Cond.h @@ -0,0 +1,29 @@ +#ifndef _Belle2_Cond_hh +#define _Belle2_Cond_hh + +#include "daq/slc/system/Mutex.h" + +namespace Belle2 { + + class Cond { + + public: + Cond(); + Cond(const Cond& cond); + ~Cond() {}; + + public: + bool init(); + bool signal(); + bool broadcast(); + bool wait(Mutex& mutex); + bool wait(Mutex& mutex, const unsigned int sec, + const unsigned int msec = 0); + bool destroy(); + + private: + pthread_cond_t m_cond_t; + + }; +}; +#endif diff --git a/ROPCApplications/daq/slc/system/include/Daemon.h b/ROPCApplications/daq/slc/system/include/Daemon.h new file mode 100644 index 0000000000000000000000000000000000000000..397d1eeff28ef79c623e358b07fc2ee027ea136b --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Daemon.h @@ -0,0 +1,23 @@ +#ifndef _Daemon_hh +#define _Daemon_hh + +namespace Belle2 { + + class Daemon { + + public: + static bool start(const char* title, + int argc, char** argv, + int nargc, const char* msg); + + static bool restart(); + + private: + static int g_argc; + static char** g_argv; + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/system/include/DynamicLoadException.h b/ROPCApplications/daq/slc/system/include/DynamicLoadException.h new file mode 100644 index 0000000000000000000000000000000000000000..617dea05a476d7034c69bbe32da5109378098bc1 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/DynamicLoadException.h @@ -0,0 +1,17 @@ +#ifndef _Belle2_DynamicLoadException_hh +#define _Belle2_DynamicLoadException_hh + +#include "daq/slc/base/Exception.h" + +namespace Belle2 { + + class DynamicLoadException : public Exception { + + public: + DynamicLoadException(const std::string& format, ...); + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/DynamicLoader.h b/ROPCApplications/daq/slc/system/include/DynamicLoader.h new file mode 100644 index 0000000000000000000000000000000000000000..4540a4850b03c6f3db9b08b6e9173da99c3c16ab --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/DynamicLoader.h @@ -0,0 +1,30 @@ +#ifndef _Belle2_DynamicLoader_h +#define _Belle2_DynamicLoader_h + +#include <string> + +namespace Belle2 { + + class DynamicLoader { + + public: + DynamicLoader() {} + DynamicLoader(const std::string& path) + { + open(path); + } + ~DynamicLoader(); + + public: + void* open(const std::string& lib_path); + void* load(const std::string& func_name); + void close(); + + private: + void* m_handle; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/Executor.h b/ROPCApplications/daq/slc/system/include/Executor.h new file mode 100644 index 0000000000000000000000000000000000000000..5f29f831f76b8cf0d6728194f4c3f68832c9a4a3 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Executor.h @@ -0,0 +1,35 @@ +#ifndef _Belle2_Executor_h +#define _Belle2_Executor_h + +#include <string> +#include <vector> + +namespace Belle2 { + + class Executor { + + public: + Executor() {} + Executor(const std::string& path) : m_path(path) {} + + public: + void setExecutable(const char* format, ...); + void setExecutable(const std::string& path); + void addArg(const char* format, ...); + void addArg(const std::string& arg); + void addArg(int arg) { addArg("%d", arg); } + void addArg(short arg) { addArg("%d", arg); } + void addArg(float arg) { addArg("%f", arg); } + void addArg(double arg) { addArg("%f", arg); } + bool execute(bool isdaemon = false); + + private: + std::string m_path; + std::vector<std::string> m_arg_v; + + }; + +} + +#endif + diff --git a/ROPCApplications/daq/slc/system/include/Fifo.h b/ROPCApplications/daq/slc/system/include/Fifo.h new file mode 100644 index 0000000000000000000000000000000000000000..6d5e2e03daee3982f4c4d14c2497e727d19677a7 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Fifo.h @@ -0,0 +1,31 @@ +#ifndef _Belle2_Fifo_hh +#define _Belle2_Fifo_hh + +#include "daq/slc/system/FileDescriptor.h" + +#include <cstddef> +#include <string> + +namespace Belle2 { + + class Fifo : public FileDescriptor { + + public: + static Fifo mkfifo(const std::string& path); + + public: + Fifo() {} + virtual ~Fifo() {} + + public: + void open(const std::string& path, + const std::string& mode = "r"); + void unlink(const std::string& path); + virtual size_t write(const void* v, size_t count); + virtual size_t read(void* v, size_t count); + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/FifoReader.h b/ROPCApplications/daq/slc/system/include/FifoReader.h new file mode 100644 index 0000000000000000000000000000000000000000..8754372b8ea5bc1bb424e702508fb17606a89e5e --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/FifoReader.h @@ -0,0 +1,13 @@ +#ifndef _Belle2_FifoReader_hh +#define _Belle2_FifoReader_hh + +#include "daq/slc/system/FileReader.h" +#include "daq/slc/system/Fifo.h" + +namespace Belle2 { + + typedef FileReader<Fifo> FifoReader; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/FifoWriter.h b/ROPCApplications/daq/slc/system/include/FifoWriter.h new file mode 100644 index 0000000000000000000000000000000000000000..3f1c23e49e82a25fdd0c0efd244597649955b48e --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/FifoWriter.h @@ -0,0 +1,13 @@ +#ifndef _Belle2_FifoWriter_hh +#define _Belle2_FifoWriter_hh + +#include "daq/slc/system/FileWriter.h" +#include "daq/slc/system/Fifo.h" + +namespace Belle2 { + + typedef FileWriter<Fifo> FifoWriter; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/File.h b/ROPCApplications/daq/slc/system/include/File.h new file mode 100644 index 0000000000000000000000000000000000000000..992edd5d25a3f2756904f4cb8d157102ccacc98c --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/File.h @@ -0,0 +1,33 @@ +#ifndef _Belle2_File_hh +#define _Belle2_File_hh + +#include "daq/slc/system/FileDescriptor.h" + +#include <string> + +namespace Belle2 { + + class File : public FileDescriptor { + + public: + static bool exist(const std::string& filename); + + public: + File() {} + File(int fd) : FileDescriptor(fd) {} + File(const std::string& path, + const std::string& mode = "r") { open(path, mode); } + virtual ~File() {} + + public: + void open(const std::string& path, + const std::string& mode = "r"); + void unlink(const std::string& path); + virtual size_t write(const void* v, size_t count); + virtual size_t read(void* v, size_t count); + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/FileDescriptor.h b/ROPCApplications/daq/slc/system/include/FileDescriptor.h new file mode 100644 index 0000000000000000000000000000000000000000..bc2b123784fcb82e4f4ffbd379c95b0279800d8a --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/FileDescriptor.h @@ -0,0 +1,27 @@ +#ifndef _Belle2_FileDescriptor_hh +#define _Belle2_FileDescriptor_hh + +namespace Belle2 { + + class FileDescriptor { + + public: + FileDescriptor(); + FileDescriptor(const FileDescriptor&); + FileDescriptor(int fd); + virtual ~FileDescriptor(); + + public: + int get_fd() const; + bool select(int sec = -1, int usec = -1); + bool select2(int sec = -1, int usec = -1); + bool close(); + + protected: + int m_fd; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/FileReader.h b/ROPCApplications/daq/slc/system/include/FileReader.h new file mode 100644 index 0000000000000000000000000000000000000000..4842154dc97ddae9e746a14cc4f2d5e55e91cd33 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/FileReader.h @@ -0,0 +1,27 @@ +#ifndef _Belle2_FileReader_hh +#define _Belle2_FileReader_hh + +#include "daq/slc/base/Reader.h" + +namespace Belle2 { + + template <class FD> + class FileReader : public Reader { + + public: + FileReader() {} + FileReader(const FD& fd) : _fd(fd) {} + virtual ~FileReader() {} + + public: + virtual size_t read(void* v, size_t count) { return _fd.read(v, count); } + virtual bool available() { return _fd.select(); } + + private: + FD _fd; + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/system/include/FileWriter.h b/ROPCApplications/daq/slc/system/include/FileWriter.h new file mode 100644 index 0000000000000000000000000000000000000000..a38d57ab70137b18d7cfaff2d9ec0d22324a9fdd --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/FileWriter.h @@ -0,0 +1,33 @@ +#ifndef _Belle2_FileWriter_hh +#define _Belle2_FileWriter_hh + +#include "daq/slc/base/Writer.h" + +namespace Belle2 { + + template <class FD> + class FileWriter : public Writer { + + public: + FileWriter() {} + FileWriter(const FD& fd) : _fd(fd) {} + virtual ~FileWriter() {} + + public: + virtual size_t write(const void* v, size_t count) + { + return _fd.write(v, count); + } + virtual bool available() + { + return _fd.select(); + } + + private: + FD _fd; + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/system/include/Fork.h b/ROPCApplications/daq/slc/system/include/Fork.h new file mode 100644 index 0000000000000000000000000000000000000000..86b70ce7f674223a41aaee6ea59885a99c894406 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Fork.h @@ -0,0 +1,68 @@ +#ifndef _Belle2_Fork_hh +#define _Belle2_Fork_hh + +#include <unistd.h> +#include <signal.h> +#include <sys/wait.h> +#include <stdlib.h> + +namespace Belle2 { + + class Fork { + + public: + template <class WORKER> + static void g_handler_exit(int, void* worker) + { + delete(WORKER*)worker; + exit(0); + } + static void g_handler_int(int) { exit(0); } + + public: + Fork() : m_pid(-1) {} + + template<class WORKER> + Fork(WORKER* worker, bool detached = true) + { + m_pid = fork(); + if (m_pid == 0) { + signal(SIGINT, g_handler_int); + if (detached) { + on_exit(g_handler_exit<WORKER>, (void*)worker); + } + worker->run(); + exit(0); + } else if (detached) { + delete worker; + } + } + ~Fork() {} + + public: + pid_t get_id() const { return m_pid; } + void set_id(pid_t id) { m_pid = id; } + bool isAlive() const { return kill(0); } + bool kill(int signo) const + { + if (m_pid < 0) return false; + return ::kill(m_pid, signo) == 0; + } + bool wait(int opt = 0) + { + if (m_pid < 0) return false; + if (::waitpid(m_pid, NULL, opt)) { + m_pid = -1; + } + return true; + } + bool cancel() { return kill(SIGINT); } + + private: + pid_t m_pid; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/Inotify.h b/ROPCApplications/daq/slc/system/include/Inotify.h new file mode 100644 index 0000000000000000000000000000000000000000..3ff75a9ffe3d3f123cd9173b8bb2bbedf14c0492 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Inotify.h @@ -0,0 +1,64 @@ +#ifndef _Belle2_Inotify_hh +#define _Belle2_Inotify_hh + +#include "daq/slc/system/FileDescriptor.h" + +#include <string> +#include <vector> + +namespace Belle2 { + + class InotifyEvent { + + friend class Inotify; + + public: + InotifyEvent() + : m_wd(-1), m_mask(-1), m_name() {} + ~InotifyEvent() {} + + private: + InotifyEvent(int wd, unsigned long mask, const std::string& name) + : m_wd(wd), m_mask(mask), m_name(name) {} + + public: + int get_wd() const { return m_wd;} + unsigned long getMask() const { return m_mask; } + const std::string& getName() const { return m_name; } + + private: + int m_wd; + unsigned long m_mask; + std::string m_name; + + }; + + typedef std::vector<InotifyEvent> InotifyEventList; + + class Inotify : public FileDescriptor { + + public: + static const unsigned long FILE_CREATE; + static const unsigned long FILE_OPEN; + static const unsigned long FILE_CLOSE_WRITE; + static const unsigned long FILE_CLOSE_NOWRITE; + static const unsigned long FILE_DELETE; + static const unsigned long FILE_MODIFY; + static const unsigned long FILE_ACCESS; + static const unsigned long FILE_ATTRIB; + + public: + Inotify() {} + virtual ~Inotify() {} + + public: + void open(); + int add(const std::string& path, unsigned long mask); + void remove(int wd); + InotifyEventList wait(int sec); + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/LockGuard.h b/ROPCApplications/daq/slc/system/include/LockGuard.h new file mode 100644 index 0000000000000000000000000000000000000000..87c4b1af8f86a84cd577a8b2823b2933adf0991d --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/LockGuard.h @@ -0,0 +1,65 @@ +#ifndef _Belle2_LockGuard_hh +#define _Belle2_LockGuard_hh + +#include <daq/slc/system/Mutex.h> +#include "MMutex.h" + +namespace Belle2 { + /** + * Lock Guard for a Mutex instance. + * If a new instance of this class is generated, + * the mutex is locked. On destruction, it is + * automatically unlocked. + * This allows to write very simple functions with locks, e.g. + * + * Mutex m; + * + * LockGuard lock(m); + * try { + * throw std::exception(); + * // or + * return some_value; + * } catch(...) { + * std::cout << "Some exception" << std::endl; + * } + * + * without the needs to think about unlocking it, as it is done automatically. + * It toughly follows the C++11 lock_guard and RIIA (https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization) + * + * WARNING: the lock can not be unlocked on uncaught exceptions (because they terminate the program by definition). + * This is not a problem with the mutex, as it is released by the operation system on program termination. + * + * The GenericLockGuard is a templated version of the lock guard. Use the specialised + * LockGuard of you want to lock a Mutex or the MLockGuard for an MMutex. + */ + template<class AMutex> + class GenericLockGuard { + public: + /// Construct a new LockGuard locking the mutex + explicit GenericLockGuard(AMutex& mutex) : m_mutex(mutex) + { + m_mutex.lock(); + } + + /// Automatically release the lock on destruction + ~GenericLockGuard() + { + m_mutex.unlock(); + } + + private: + /// Do not allow to copy a lock guard + GenericLockGuard(const GenericLockGuard&); + + /// Do not allow to copy a lock guard + GenericLockGuard& operator=(const GenericLockGuard&); + + /// The mutex hold by this lock guard + AMutex& m_mutex; + }; + + typedef GenericLockGuard<Mutex> LockGuard; + typedef GenericLockGuard<MMutex> MLockGuard; +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/LogFile.h b/ROPCApplications/daq/slc/system/include/LogFile.h new file mode 100644 index 0000000000000000000000000000000000000000..35e13f8bed71c1e66e64f3278ef4f24c75bf0167 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/LogFile.h @@ -0,0 +1,65 @@ +#ifndef _Belle2_LogFile_h +#define _Belle2_LogFile_h + +#include <daq/slc/system/Mutex.h> + +#include <daq/slc/base/Date.h> + +#include <string> +#include <fstream> +#include <stdarg.h> + +namespace Belle2 { + + struct LogFile { + + public: + enum Priority { + UNKNOWN = 0, DEBUG, INFO, NOTICE, WARNING, ERROR, FATAL + }; + + public: + static Priority getPriority(const std::string& str); + + private: + LogFile() {} + ~LogFile() {} + + private: + static bool g_stderr; + static bool g_opened; + static std::string g_filepath; + static std::string g_linkpath; + static std::ofstream g_stream; + static unsigned int g_filesize; + static Mutex g_mutex; + static Priority g_threshold; + static std::string g_filename; + static Date g_date; + + // member functions + public: + static void open(const std::string& filename, + Priority priority = UNKNOWN); + static void open(); + static void close(); + static void debug(const std::string& msg, ...); + static void info(const std::string& msg, ...); + static void notice(const std::string& msg, ...); + static void warning(const std::string& msg, ...); + static void error(const std::string& msg, ...); + static void fatal(const std::string& msg, ...); + static void put(Priority priority, const std::string& msg, ...); + static void setStdErr(bool stderr) + { + g_stderr = stderr; + } + + private: + static int put_impl(const std::string& msg, Priority priority, va_list ap); + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/system/include/MCond.h b/ROPCApplications/daq/slc/system/include/MCond.h new file mode 100644 index 0000000000000000000000000000000000000000..a640958645283a5653d29ff6486e472ff89b6229 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/MCond.h @@ -0,0 +1,45 @@ +#ifndef _Belle2_MCond_h +#define _Belle2_MCond_h + +#include "daq/slc/system/MMutex.h" + +#include <pthread.h> + +namespace Belle2 { + + class MCond { + + public: + static size_t size() { return sizeof(pthread_cond_t); } + + // constructors and destructor + public: + MCond(); + MCond(const MCond&); + MCond(void*); + ~MCond(); + + // member functions; + public: + bool init(void*); + bool init(); + bool set(void*); + bool signal(); + bool broadcast(); + bool wait(MMutex& mutex); + bool wait(MMutex& mutex, const unsigned int sec, + const unsigned int msec = 0); + bool destroy(); + + public: + const MCond& operator=(const MCond&); + + // data members; + private: + pthread_cond_t* m_cond; + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/system/include/MMutex.h b/ROPCApplications/daq/slc/system/include/MMutex.h new file mode 100644 index 0000000000000000000000000000000000000000..48a9cb2033cea92e98bac029f74e113b3a0b3e5b --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/MMutex.h @@ -0,0 +1,50 @@ +#ifndef _Belle2_MMutex_h +#define _Belle2_MMutex_h + +#include <pthread.h> + +#ifdef __CINT__ +#undef __GNUC__ +#define _SYS__SELECT_H_ +struct pthread_cond_t; +struct pthread_mutex_t; +#endif + +namespace Belle2 { + + class MMutex { + + friend class MCond; + + public: + static size_t size() { return sizeof(pthread_mutex_t); } + + // constructors and destructor + public: + MMutex(); + MMutex(void*); + MMutex(const MMutex&); + ~MMutex(); + + // member functions; + public: + bool init(void*); + bool init(); + bool set(void*); + bool lock(); + bool trylock(); + bool unlock(); + bool destroy(); + + public: + const MMutex& operator=(const MMutex&); + + // data members; + private: + pthread_mutex_t* m_mu; + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/system/include/Mutex.h b/ROPCApplications/daq/slc/system/include/Mutex.h new file mode 100644 index 0000000000000000000000000000000000000000..d0ac88760d5c69cd6912576daff5b9d7e6f55a1b --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Mutex.h @@ -0,0 +1,31 @@ +#ifndef _Belle2_Mutex_hh +#define _Belle2_Mutex_hh + +#include <pthread.h> + +namespace Belle2 { + + class Mutex { + + friend class Cond; + + // constructors & destructors + public: + Mutex(); + ~Mutex(); + + // member methods + public: + bool lock(); + bool unlock(); + + // member data + private: + pthread_mutex_t m_mu; + pthread_mutexattr_t m_attr; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/PThread.h b/ROPCApplications/daq/slc/system/include/PThread.h new file mode 100644 index 0000000000000000000000000000000000000000..73a818122bfc835bf83a339b4566a39ea907061b --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/PThread.h @@ -0,0 +1,110 @@ +#ifndef _Belle2_PThread_hh +#define _Belle2_PThread_hh + +#include <daq/slc/system/LogFile.h> + +#include <pthread.h> +#include <signal.h> +#include <cstdio> + +namespace Belle2 { + + class PThread { + + private: + template<class WORKER> + static void destroy(void* arg) + { + WORKER* worker = (WORKER*)arg; + delete worker; + worker = NULL; + } + template<class WORKER> + static void* create_destroy(void* arg) + { + WORKER* worker = (WORKER*)arg; + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + pthread_cleanup_push(PThread::destroy<WORKER>, arg); + try { + worker->run(); + } catch (const std::exception& e) { + LogFile::fatal(e.what()); + } + pthread_cleanup_pop(1); + return NULL; + } + template<class WORKER> + static void* create(void* arg) + { + WORKER* worker = (WORKER*)arg; + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + try { + worker->run(); + } catch (const std::exception& e) { + LogFile::fatal(e.what()); + } + return NULL; + } + + public: + static void exit() { pthread_exit(NULL); } + + public: + PThread() : m_th(0) {} + template<class WORKER> + PThread(WORKER* worker, bool destroyed = true, bool detached = true) + { + m_th = 0; + if (destroyed) { + if (pthread_create(&m_th, NULL, PThread::create_destroy<WORKER>, + (void*)worker) != 0) { + m_th = 0; + } + } else { + if (pthread_create(&m_th, NULL, PThread::create<WORKER>, + (void*)worker) != 0) { + m_th = 0; + } + } + if (detached) { + detach(); + m_th = 0; + } + } + ~PThread() {} + + public: + pthread_t id() { return m_th; } + pthread_t get_id() { return m_th; } + bool kill(int signo) + { + if (m_th == 0) return false; + return ::pthread_kill(m_th, signo) == 0; + } + bool is_alive() { return this->kill(0); } + bool detach() + { + if (m_th == 0) return false; + return ::pthread_detach(m_th) == 0; + } + bool join() + { + if (m_th == 0) return false; + return ::pthread_join(m_th, NULL) == 0; + } + bool cancel() + { + if (m_th == 0) return false; + return ::pthread_cancel(m_th) == 0; + } + + private: + pthread_t m_th; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/Process.h b/ROPCApplications/daq/slc/system/include/Process.h new file mode 100644 index 0000000000000000000000000000000000000000..72c242e4e2defd77c9ffada50541a2289459f1ec --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Process.h @@ -0,0 +1,79 @@ +#ifndef _Belle2_Process_h +#define _Belle2_Process_h + +#include <unistd.h> +#include <signal.h> +#include <sys/wait.h> +#include <stdlib.h> + +namespace Belle2 { + + class Process { + + public: + template <class WORKER> + static void g_handler_exit(int, void* worker) + { + delete(WORKER*)worker; + exit(0); + } + static void g_handler_int(int) { exit(0); } + + public: + Process() : m_pid(-1) {} + + template<class WORKER> + Process(WORKER* worker, bool detached = true) + { + m_pid = fork(); + if (m_pid == 0) { + signal(SIGINT, g_handler_int); + if (detached) { + on_exit(g_handler_exit<WORKER>, (void*)worker); + } + worker->run(); + exit(0); + } else if (detached) { + delete worker; + } + } + ~Process() {} + + public: + pid_t get_id() const { return m_pid; } + pid_t id() const { return m_pid; } + void set_id(pid_t id) { m_pid = id; } + bool isAlive() const { return kill(0); } + bool kill(int signo) const + { + if (m_pid <= 0) return false; + return ::kill(m_pid, signo) == 0; + } + bool wait(int opt = 0) + { + if (m_pid < 0) return false; + if (m_waitpid_result = ::waitpid(m_pid, &m_waitpid_status, opt)) { + m_pid = -1; + } + return true; + } + bool cancel() { return kill(SIGINT); } + int waitpid_result() const + { + return m_waitpid_result; + }; + int waitpid_status() const + { + return m_waitpid_status; + }; + + private: + pid_t m_pid; + int m_waitpid_result; + int m_waitpid_status; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/RWLock.h b/ROPCApplications/daq/slc/system/include/RWLock.h new file mode 100644 index 0000000000000000000000000000000000000000..7f9b823a9f73a871d306844cdbf6aed15188be82 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/RWLock.h @@ -0,0 +1,37 @@ +#ifndef _Belle2_RWLock_hh +#define _Belle2_RWLock_hh + +#include <pthread.h> + +namespace Belle2 { + + class RWLock { + + public: + static size_t size() + { + return sizeof(pthread_rwlock_t); + } + + // constructors and destructor + public: + RWLock(); + ~RWLock(); + + // member functions; + public: + bool init(); + bool rdlock(); + bool wrlock(); + bool unlock(); + bool destroy(); + + // data members; + private: + pthread_rwlock_t m_lock; + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/system/include/SharedMemory.h b/ROPCApplications/daq/slc/system/include/SharedMemory.h new file mode 100644 index 0000000000000000000000000000000000000000..7e09380570cb7fd33bcd043c5e168407b135532a --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/SharedMemory.h @@ -0,0 +1,51 @@ +#ifndef _Belle2_SharedMemory_h +#define _Belle2_SharedMemory_h + +#include <string> + +namespace Belle2 { + + class SharedMemory { + + public: + static bool unlink(const std::string& path); + + // constructors and destructor + public: + SharedMemory(); + SharedMemory(const std::string& path, size_t size); + SharedMemory(const SharedMemory&); + ~SharedMemory(); + + // member functions; + public: + bool open(const std::string& path, size_t size); + bool open(); + void close(); + bool unlink(); + bool seekTo(size_t); + bool seekBy(size_t); + void* map(size_t, size_t); + void* map(); + bool isOpened(); + const std::string& getPath() const { return m_path; } + size_t size() const { return m_size; } + int getfd() const { return m_fd; } + bool truncate(size_t size); + + // operators + public: + const SharedMemory& operator=(const SharedMemory&); + + // data members; + private: + int m_fd; + std::string m_path; + size_t m_size; + void* m_addr; + + }; + +}; + +#endif diff --git a/ROPCApplications/daq/slc/system/include/StreamSizeCounter.h b/ROPCApplications/daq/slc/system/include/StreamSizeCounter.h new file mode 100644 index 0000000000000000000000000000000000000000..ebd7dc3fb89730f31a16ad40921c93969aadc43c --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/StreamSizeCounter.h @@ -0,0 +1,28 @@ +#ifndef _Belle2_StreamSizeCounter_hh +#define _Belle2_StreamSizeCounter_hh + +#include "daq/slc/base//Writer.h" + +namespace Belle2 { + + class StreamSizeCounter : public Writer { + + public: + StreamSizeCounter() : m_count(0) {} + virtual ~StreamSizeCounter() {} + + public: + virtual size_t write(const void*, size_t); + virtual bool available() { return true; } + void reset() { m_count = 0; } + int count() const { return m_count; } + + private: + size_t m_count; + + }; + +} + +#endif + diff --git a/ROPCApplications/daq/slc/system/include/TCPServerSocket.h b/ROPCApplications/daq/slc/system/include/TCPServerSocket.h new file mode 100644 index 0000000000000000000000000000000000000000..0cc291d524594dd4f715de42c9a1cce21dd08829 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/TCPServerSocket.h @@ -0,0 +1,36 @@ +#ifndef _B2ARICH_TCPServerSocket_hh_ +#define _B2ARICH_TCPServerSocket_hh_ + +#include "daq/slc/system/TCPSocket.h" +#include "daq/slc/system/FileDescriptor.h" + +namespace Belle2 { + + class TCPServerSocket : public FileDescriptor { + + public: + TCPServerSocket() + : m_ip(""), m_port(0) {} + TCPServerSocket(const std::string& ip, unsigned short port) + : m_ip(ip), m_port(port) {} + virtual ~TCPServerSocket() {} + + public: + void setIP(const std::string& ip) { m_ip = ip; } + void setPort(unsigned short port) { m_port = port; } + const std::string& getIP() const { return m_ip; } + unsigned short getPort() const { return m_port; } + int open(int nqueue = 5); + int open(const std::string& ip, unsigned short port, int nqueue = 5); + TCPSocket accept(); + + private: + std::string m_ip; + unsigned short m_port; + + }; + +} + +#endif + diff --git a/ROPCApplications/daq/slc/system/include/TCPSocket.h b/ROPCApplications/daq/slc/system/include/TCPSocket.h new file mode 100644 index 0000000000000000000000000000000000000000..9557509a8742ad2616e2ab2875199fdad9106b51 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/TCPSocket.h @@ -0,0 +1,51 @@ +#ifndef _B2ARICH_TCPSocket_hh_ +#define _B2ARICH_TCPSocket_hh_ + +#include <string> + +#include "daq/slc/system/FileDescriptor.h" + +namespace Belle2 { + + class TCPSocket : public FileDescriptor { + + friend class TCPServerSocket; + + public: + TCPSocket() : m_ip(""), m_port(0) {} + TCPSocket(const std::string& ip, unsigned short port) + : m_ip(ip), m_port(port) {} + virtual ~TCPSocket() {} + + private: + TCPSocket(int fd) : FileDescriptor(fd), m_ip(""), m_port(0) {} + + public: + void setIP(const std::string& ip) { m_ip = ip; } + void setPort(unsigned short port) { m_port = port; } + const std::string& getIP() const { return m_ip; } + unsigned short getPort() const { return m_port; } + int connect(); + int connect(const std::string& ip, unsigned short port); + void setBufferSize(int size); + void print(); + const std::string getLocalIP(); + int getLocalAddress(); + int getLocalPort(); + unsigned int getAddress(); + + public: + virtual size_t write(const void* v, size_t count); + virtual size_t read(void* v, size_t count); + size_t read_once(void* v, size_t count); + + private: + std::string m_ip; + unsigned short m_port; + + }; + +} + +#endif + diff --git a/ROPCApplications/daq/slc/system/include/TCPSocketReader.h b/ROPCApplications/daq/slc/system/include/TCPSocketReader.h new file mode 100644 index 0000000000000000000000000000000000000000..c1aa6e284105895fa63250dbbaf5f05d750075b1 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/TCPSocketReader.h @@ -0,0 +1,13 @@ +#ifndef _Belle2_TCPSocketReader_hh +#define _Belle2_TCPSocketReader_hh + +#include "daq/slc/system/FileReader.h" +#include "daq/slc/system/TCPSocket.h" + +namespace Belle2 { + + typedef FileReader<TCPSocket> TCPSocketReader; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/TCPSocketWriter.h b/ROPCApplications/daq/slc/system/include/TCPSocketWriter.h new file mode 100644 index 0000000000000000000000000000000000000000..b9fc1a9a525b11731c76d4003f14ad5f7dad9ace --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/TCPSocketWriter.h @@ -0,0 +1,13 @@ +#ifndef _Belle2_TCPSocketWriter_hh +#define _Belle2_TCPSocketWriter_hh + +#include "daq/slc/system/FileWriter.h" +#include "daq/slc/system/TCPSocket.h" + +namespace Belle2 { + + typedef FileWriter<TCPSocket> TCPSocketWriter; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/Time.h b/ROPCApplications/daq/slc/system/include/Time.h new file mode 100644 index 0000000000000000000000000000000000000000..aafc87b5841582de1aa9410e9095149fb9022d3c --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/Time.h @@ -0,0 +1,54 @@ +#ifndef Time_hh +#define Time_hh + +#include "daq/slc/base/Serializable.h" + +#include <string> + +namespace Belle2 { + + class Time : public Serializable { + + // constructors & destructors + public: + explicit Time(); + Time(const double); + Time(const long, const long); + ~Time(); + + // member methods + private: + void adjust(); + + public: + void clear(); + void set(); + void set(const double); + void set(const long, const long); + double get() const; + long getSecond() const; + long getMicroSecond() const; + std::string toString() const; + + // for serialization + public: + virtual void readObject(Reader&); + virtual void writeObject(Writer&) const; + + // operators + public: + Time& operator = (const Time&); + bool operator == (const Time&) const; + Time operator + (const Time&) const; + Time operator - (const Time&) const; + + // member data + private: + long m_s; + long m_us; + + }; + +} + +#endif diff --git a/ROPCApplications/daq/slc/system/include/UDPSocket.h b/ROPCApplications/daq/slc/system/include/UDPSocket.h new file mode 100644 index 0000000000000000000000000000000000000000..c7aac6da020c8c811f377bbc37902ac1679269d0 --- /dev/null +++ b/ROPCApplications/daq/slc/system/include/UDPSocket.h @@ -0,0 +1,55 @@ +#ifndef _B2ARICH_UDPSocket_hh_ +#define _B2ARICH_UDPSocket_hh_ + +#include <string> + +#include <netinet/in.h> + +#include "daq/slc/system/FileDescriptor.h" + +namespace Belle2 { + + class UDPSocket : public FileDescriptor { + + friend class TCPServerSocket; + + public: + UDPSocket(); + UDPSocket(unsigned int port); + UDPSocket(unsigned int port, + const std::string& hostname, + bool boardcast = false); + UDPSocket(unsigned int port, + unsigned int addr, + bool boardcast = false); + virtual ~UDPSocket() {} + + public: + int bind(); + int bind(unsigned int port, + const std::string& hostname = "", + bool broadcast = true); + const std::string getHostName() const; + unsigned int getPort() const; + unsigned int getAddress() const; + const std::string getRemoteHostName() const; + unsigned int getRemotePort() const; + unsigned int getRemoteAddress() const; + + public: + virtual size_t write(const void* v, size_t count); + virtual size_t read(void* v, size_t count); + + private: + struct sockaddr_in m_addr; + struct sockaddr_in m_remote_addr; + + private: + static unsigned int findSubnet(unsigned int addr); + + }; + +} + +#endif + diff --git a/ROPCApplications/daq/slc/system/src/Buffer.cc b/ROPCApplications/daq/slc/system/src/Buffer.cc new file mode 100644 index 0000000000000000000000000000000000000000..a795358d0363848193d1e9153bc6d0584dc8366b --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/Buffer.cc @@ -0,0 +1,57 @@ +#include "daq/slc/system/Buffer.h" + +#include <cstring> + +using namespace Belle2; + +Buffer::Buffer() : m_memory(NULL), m_size(0), m_allocated(false) {} + +Buffer::Buffer(unsigned int size, unsigned char* memory) + : m_memory(memory), m_size(size), m_allocated(false) +{ + if (memory == NULL && size > 0) { + m_memory = new unsigned char[size]; + ::memset(m_memory, 0, size); + m_allocated = true; + } +} + +Buffer::Buffer(const Buffer& buf) + : m_memory(NULL), m_size(buf.m_size), + m_allocated(buf.m_allocated) +{ + if (m_allocated) { + m_memory = new unsigned char [buf.m_size]; + ::memset(m_memory, 0, buf.m_size); + ::memcpy(m_memory, buf.m_memory, buf.m_size); + } else { + m_memory = buf.m_memory; + } + m_size = buf.m_size; +} + +Buffer::~Buffer() +{ + if (m_allocated && m_memory != NULL) { + delete [] m_memory; + m_allocated = false; + m_memory = NULL; + } +} + +const Buffer& Buffer::operator=(const Buffer& buf) +{ + if (m_allocated) { + delete [] m_memory; + } + m_allocated = buf.m_allocated; + if (m_allocated) { + m_memory = new unsigned char [buf.m_size]; + ::memset(m_memory, 0, buf.m_size); + ::memcpy(m_memory, buf.m_memory, buf.m_size); + } else { + m_memory = buf.m_memory; + } + m_size = buf.m_size; + return *this; +} diff --git a/ROPCApplications/daq/slc/system/src/BufferedReader.cc b/ROPCApplications/daq/slc/system/src/BufferedReader.cc new file mode 100644 index 0000000000000000000000000000000000000000..62332a0f0d958daf73d9fd3e0cb06d905c062be1 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/BufferedReader.cc @@ -0,0 +1,80 @@ +#include "daq/slc/system/BufferedReader.h" + +#include <daq/slc/base/IOException.h> + +#include <cstring> + +using namespace Belle2; + +BufferedReader::BufferedReader() + : m_memory(NULL), m_size(0), m_pos(0), m_allocated(false) {} + +BufferedReader::BufferedReader(const BufferedReader& reader) + : m_memory(NULL), m_size(reader.m_size), + m_pos(reader.m_pos), m_allocated(reader.m_allocated) +{ + if (m_allocated) { + m_memory = new unsigned char [reader.m_size]; + ::memcpy(m_memory, reader.m_memory, reader.m_size); + } else { + m_memory = reader.m_memory; + } + m_size = reader.m_size; + m_pos = reader.m_pos; +} + +BufferedReader::BufferedReader(size_t size, unsigned char* memory) + : m_memory(memory), m_size(size), m_pos(0), m_allocated(false) +{ + if (memory == NULL) { + m_memory = new unsigned char[size]; + m_allocated = true; + } +} + +BufferedReader::~BufferedReader() +{ + if (m_allocated && m_memory != NULL) { + delete [] m_memory; + } +} + +const BufferedReader& BufferedReader::operator=(const BufferedReader& reader) +{ + if (m_allocated) { + delete [] m_memory; + } + m_allocated = reader.m_allocated; + if (m_allocated) { + m_memory = new unsigned char [reader.m_size]; + ::memcpy(m_memory, reader.m_memory, reader.m_size); + } else { + m_memory = reader.m_memory; + } + m_size = reader.m_size; + m_pos = reader.m_pos; + return *this; +} + +void BufferedReader::copy(const void* buffer, size_t count) +{ + if (count < m_size) { + memcpy(m_memory, buffer, count); + m_pos = 0; + } else { + throw (IOException("Out of buffer range: %d > %d", + (int)count, (int)m_size)); + } +} + +size_t BufferedReader::read(void* buf, const size_t count) +{ + if (m_pos + count > size()) { + throw (IOException("Out of buffer range: %d + %d > %d", + (int)m_pos, (int)count, (int)m_size)); + } + memcpy(buf, (m_memory + m_pos), count); + m_pos += count; + return count; +} + diff --git a/ROPCApplications/daq/slc/system/src/BufferedWriter.cc b/ROPCApplications/daq/slc/system/src/BufferedWriter.cc new file mode 100644 index 0000000000000000000000000000000000000000..9f9b6a0057be833931701e1d395fc6473337bbe4 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/BufferedWriter.cc @@ -0,0 +1,67 @@ +#include "daq/slc/system/BufferedWriter.h" + +#include <daq/slc/base/IOException.h> + +#include <cstring> + +using namespace Belle2; + +BufferedWriter::BufferedWriter() + : m_memory(NULL), m_size(0), m_pos(0), m_allocated(false) {} + +BufferedWriter::BufferedWriter(size_t size, unsigned char* memory) + : m_memory(memory), m_size(size), m_pos(0), m_allocated(false) +{ + if (memory == NULL) { + m_memory = new unsigned char[size]; + m_allocated = true; + } +} + +BufferedWriter::BufferedWriter(const BufferedWriter& writer) + : m_memory(NULL), m_size(writer.m_size), + m_pos(writer.m_pos), m_allocated(writer.m_allocated) +{ + if (m_allocated) { + m_memory = new unsigned char [writer.m_size]; + ::memcpy(m_memory, writer.m_memory, writer.m_size); + } else { + m_memory = writer.m_memory; + } + m_size = writer.m_size; + m_pos = writer.m_pos; +} + +BufferedWriter::~BufferedWriter() +{ + if (m_allocated && m_memory != NULL) delete [] m_memory; +} + +const BufferedWriter& BufferedWriter::operator=(const BufferedWriter& writer) +{ + if (m_allocated) { + delete [] m_memory; + } + m_allocated = writer.m_allocated; + if (m_allocated) { + m_memory = new unsigned char [writer.m_size]; + ::memcpy(m_memory, writer.m_memory, writer.m_size); + } else { + m_memory = writer.m_memory; + } + m_size = writer.m_size; + m_pos = writer.m_pos; + return *this; +} + +size_t BufferedWriter::write(const void* buf, const size_t count) +{ + if (m_pos + count > m_size) { + throw (IOException("out of buffer range: %d+%d>%d", + (int)m_pos, (int)count, (int)m_size)); + } + memcpy((m_memory + m_pos), buf, count); + m_pos += count; + return count; +} + diff --git a/ROPCApplications/daq/slc/system/src/Cond.cc b/ROPCApplications/daq/slc/system/src/Cond.cc new file mode 100644 index 0000000000000000000000000000000000000000..76d3c2beae5d7d6a8b4bfadaf1d2e99bed12f9e2 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/Cond.cc @@ -0,0 +1,70 @@ +#include "daq/slc/system/Cond.h" + +#include <pthread.h> +#include <sys/time.h> + +using namespace Belle2; + +Cond::Cond() +{ + init(); +} + +Cond::Cond(const Cond& cond) +{ + m_cond_t = cond.m_cond_t; +} + +bool Cond::init() +{ + pthread_condattr_t mattr; + pthread_condattr_init(&mattr); + pthread_condattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); + if (pthread_cond_init(&m_cond_t, &mattr) != 0) { + return false; + } + pthread_condattr_destroy(&mattr); + + return true; +} + +bool Cond::signal() +{ + if (pthread_cond_signal(&m_cond_t) == 0) return true; + else return false; +} + +bool Cond::broadcast() +{ + if (pthread_cond_broadcast(&m_cond_t) == 0) return true; + else return false; +} + +bool Cond::wait(Mutex& mutex) +{ + if (pthread_cond_wait(&m_cond_t, &mutex.m_mu) != 0) { + return false; + } + return true; +} + +bool Cond::wait(Mutex& mutex, const unsigned int sec, const unsigned int msec) +{ + struct timeval now; + struct timespec timeout; + + gettimeofday(&now, NULL); + timeout.tv_sec = now.tv_sec + sec; + timeout.tv_nsec = now.tv_usec * 1000 + msec; + int stat = 0; + if ((stat = pthread_cond_timedwait(&m_cond_t, &mutex.m_mu, &timeout)) != 0) { + return false; + } + return true; +} + +bool Cond::destroy() +{ + if (pthread_cond_destroy(&m_cond_t) == 0) return true; + else return false; +} diff --git a/ROPCApplications/daq/slc/system/src/Daemon.cc b/ROPCApplications/daq/slc/system/src/Daemon.cc new file mode 100644 index 0000000000000000000000000000000000000000..38f84601535ba2e72fb50ba29c925ba036a0ece9 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/Daemon.cc @@ -0,0 +1,46 @@ +#include "daq/slc/system/Daemon.h" + +#include <daq/slc/system/LogFile.h> + +#include <daq/slc/base/StringUtil.h> + +#include <unistd.h> +#include <cstring> + +using namespace Belle2; + +bool Daemon::start(const char* title, + int argc, char** argv, + int nargc, const char* msg) +{ + g_argc = argc; + g_argv = argv; + bool isdaemon = false; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "-d") == 0) { + isdaemon = true; + nargc++; + } else if (strcmp(argv[i], "-h") == 0) { + LogFile::debug("Usage : %s %s [-d]", argv[0], msg); + return false; + } + } + if (argc < nargc + 1) { + LogFile::debug("Usage : %s %s [-d]", argv[0], msg); + return false; + } + LogFile::open(StringUtil::form("%s/%s", argv[0], title)); + if (isdaemon) { + daemon(0, 0); + } + return true; +} + + +bool Daemon::restart() +{ + return execv(g_argv[0], g_argv) != -1; +} + +int Daemon::g_argc = 0; +char** Daemon::g_argv = NULL; diff --git a/ROPCApplications/daq/slc/system/src/DynamicLoadException.cc b/ROPCApplications/daq/slc/system/src/DynamicLoadException.cc new file mode 100644 index 0000000000000000000000000000000000000000..0fcf2eacd5eea0b373b32669372adfd418874163 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/DynamicLoadException.cc @@ -0,0 +1,17 @@ +#include "daq/slc/system/DynamicLoadException.h" + +#include <cstdarg> +#include <cstdio> + +using namespace Belle2; + +DynamicLoadException::DynamicLoadException(const std::string& format, ...) +{ + va_list ap; + char ss[1024 * 100]; + va_start(ap, format); + vsprintf(ss, format.c_str(), ap); + va_end(ap); + m_comment = ss; +} + diff --git a/ROPCApplications/daq/slc/system/src/DynamicLoader.cc b/ROPCApplications/daq/slc/system/src/DynamicLoader.cc new file mode 100644 index 0000000000000000000000000000000000000000..62be20d0c3a3a7c3699d2d91564584c3331f47d7 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/DynamicLoader.cc @@ -0,0 +1,38 @@ +#include "daq/slc/system/DynamicLoader.h" +#include "daq/slc/system/DynamicLoadException.h" + +#include <dlfcn.h> + +using namespace Belle2; + +DynamicLoader::~DynamicLoader() +{ +} + +void DynamicLoader::close() +{ + if (m_handle != NULL) { + dlclose(m_handle); + m_handle = NULL; + } +} + +void* DynamicLoader::open(const std::string& lib_path) +{ + void* handle = dlopen(lib_path.c_str(), RTLD_LAZY); + if (!handle) { + throw (DynamicLoadException("dlopen: %s", dlerror())); + } + m_handle = handle; + return handle; +} + +void* DynamicLoader::load(const std::string& funcname) +{ + char* error = NULL; + void* func = dlsym(m_handle, funcname.c_str()); + if ((error = dlerror()) != NULL) { + throw (DynamicLoadException("dlopen: %s", error)); + } + return func; +} diff --git a/ROPCApplications/daq/slc/system/src/Executor.cc b/ROPCApplications/daq/slc/system/src/Executor.cc new file mode 100644 index 0000000000000000000000000000000000000000..4c48b6c3a80094a57713c0e24ae2885a86c2dac7 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/Executor.cc @@ -0,0 +1,59 @@ +#include "daq/slc/system/Executor.h" +#include "daq/slc/system/LogFile.h" + +#include <iostream> +#include <cstdarg> +#include <cstdio> +#include <unistd.h> + +using namespace Belle2; + +void Executor::setExecutable(const char* format, ...) +{ + va_list ap; + static char ss[1024]; + va_start(ap, format); + vsprintf(ss, format, ap); + va_end(ap); + m_path = ss; +} + +void Executor::setExecutable(const std::string& path) +{ + m_path = path; +} + +void Executor::addArg(const char* format, ...) +{ + va_list ap; + static char ss[1024]; + va_start(ap, format); + vsprintf(ss, format, ap); + va_end(ap); + m_arg_v.push_back(ss); +} + +void Executor::addArg(const std::string& arg) +{ + m_arg_v.push_back(arg); +} + +bool Executor::execute(bool isdaemon) +{ + char* argv[256]; + argv[0] = (char*)m_path.c_str(); + std::cerr << "" << argv[0] << " "; + for (size_t i = 0; i < m_arg_v.size(); i++) { + argv[i + 1] = (char*)m_arg_v[i].c_str(); + std::cerr << "" << argv[i + 1] << " "; + } + std::cerr << std::endl; + argv[m_arg_v.size() + 1] = NULL; + if (isdaemon) daemon(0, 0); + if (execvp(m_path.c_str(), argv) == -1) { + LogFile::error("Faield to start %s", argv[0]); + return false; + } + + return true; +} diff --git a/ROPCApplications/daq/slc/system/src/Fifo.cc b/ROPCApplications/daq/slc/system/src/Fifo.cc new file mode 100644 index 0000000000000000000000000000000000000000..ac3836059ce6b4dda8a3c01990be263fb3dc40ba --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/Fifo.cc @@ -0,0 +1,52 @@ +#include "daq/slc/system/Fifo.h" + +#include <daq/slc/base/IOException.h> + +#include <unistd.h> +#include <sys/stat.h> +#include <fcntl.h> + +#include <cstdio> + +using namespace Belle2; + +Fifo Fifo::mkfifo(const std::string& path) +{ + ::mkfifo(path.c_str(), 0666); + Fifo fifo; + fifo.open(path.c_str()); + return fifo; +} + +void Fifo::open(const std::string& path, const std::string& mode_s) +{ + int mode = O_RDONLY; + if (mode_s.find("w") != std::string::npos) { + mode = O_WRONLY; + if (mode_s.find("r") != std::string::npos) { + mode = O_RDWR; + } + } + if ((m_fd = ::open(path.c_str(), mode)) < 0) { + perror("open"); + throw (IOException("Failed to open fifo.")); + } +} + +void Fifo::unlink(const std::string& path) +{ + if ((::unlink(path.c_str())) < 0) { + perror("unlink"); + } + close(); +} + +size_t Fifo::write(const void* buf, size_t count) +{ + return ::write(m_fd, buf, count); +} + +size_t Fifo::read(void* buf, size_t count) +{ + return ::read(m_fd, buf, count); +} diff --git a/ROPCApplications/daq/slc/system/src/File.cc b/ROPCApplications/daq/slc/system/src/File.cc new file mode 100644 index 0000000000000000000000000000000000000000..53e14be70861a54b9331d25581e8e35d6e949019 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/File.cc @@ -0,0 +1,97 @@ +#include "daq/slc/system/File.h" + +#include <daq/slc/base/IOException.h> + +#include <unistd.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> + +#include <cstdio> + +using namespace Belle2; + +void File::open(const std::string& path, const std::string& mode_s) +{ + int mode = O_RDONLY; + if (mode_s.find("w") != std::string::npos) { + mode = O_WRONLY; + if (mode_s.find("r") != std::string::npos) { + mode = O_RDWR; + } else { + mode |= O_CREAT; + } + } + if ((m_fd = ::open(path.c_str(), mode, 0666)) < 0) { + perror("open"); + throw (IOException("Failed to open file : %s", path.c_str())); + } +} + +void File::unlink(const std::string& path) +{ + if ((::unlink(path.c_str())) < 0) { + perror("unlink"); + } + close(); +} + +size_t File::write(const void* buf, size_t count) +{ + size_t c = 0; + int ret; + while (c < count) { + errno = 0; + ret = ::write(m_fd, ((unsigned char*)buf + c), (count - c)); + if (ret <= 0) { + switch (errno) { + case EINTR: continue; + case ENETUNREACH: + case EHOSTUNREACH: + case ETIMEDOUT: + usleep(500); + continue; + default: + throw (IOException("Error while writing")); + } + } + c += ret; + } + return c; +} + +size_t File::read(void* buf, size_t count) +{ + size_t c = 0; + int ret; + while (c < count) { + errno = 0; + ret = ::read(m_fd, ((unsigned char*)buf + c), (count - c)); + if (ret <= 0) { + switch (errno) { + case EINTR: continue; + case EAGAIN: continue; + default: + throw (IOException("Error while reading. %d", errno)); + } + } + c += ret; + } + return c; +} + +bool File::exist(const std::string& filename) +{ + struct stat st; + if (stat(filename.c_str(), &st) != 0) { + return false; + } else { + mode_t m = st.st_mode; + if (S_ISDIR(m)) { + return false; + } else { + return true; + } + } + return false; +} diff --git a/ROPCApplications/daq/slc/system/src/FileDescriptor.cc b/ROPCApplications/daq/slc/system/src/FileDescriptor.cc new file mode 100644 index 0000000000000000000000000000000000000000..c5550ad85900a568861ce86198416c0ac3210229 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/FileDescriptor.cc @@ -0,0 +1,97 @@ +#include "daq/slc/system/FileDescriptor.h" + +#include <daq/slc/base/IOException.h> + +#include <unistd.h> +#include <sys/select.h> +#include <stdio.h> + +using namespace Belle2; + +FileDescriptor::FileDescriptor() +{ + m_fd = -1; +} + +FileDescriptor::FileDescriptor(const FileDescriptor& fd) +{ + m_fd = fd.m_fd; +} + +FileDescriptor::FileDescriptor(int fd) +{ + m_fd = fd; +} + +FileDescriptor::~FileDescriptor() +{ + +} + +int FileDescriptor::get_fd() const +{ + return m_fd; +} + +bool FileDescriptor::select(int sec, int usec) +{ + if (m_fd <= 0) { + return false; + } + fd_set fds; + FD_ZERO(&fds); + FD_SET(m_fd, &fds); + int ret; + if (sec >= 0 && usec >= 0) { + timeval t = {sec, usec}; + ret = ::select(FD_SETSIZE, &fds, NULL, NULL, &t); + } else { + ret = ::select(FD_SETSIZE, &fds, NULL, NULL, NULL); + } + if (ret < 0) { + perror("select"); + throw (IOException("Failed to select")); + } + if (FD_ISSET(m_fd, &fds)) { + return true; + } else { + return false; + } +} + +bool FileDescriptor::select2(int sec, int usec) +{ + if (m_fd <= 0) { + return false; + } + fd_set fds; + FD_ZERO(&fds); + FD_SET(m_fd, &fds); + int ret; + if (sec >= 0 && usec >= 0) { + timeval t = {sec, usec}; + ret = ::select(FD_SETSIZE, NULL, &fds, NULL, &t); + } else { + ret = ::select(FD_SETSIZE, NULL, &fds, NULL, NULL); + } + if (ret < 0) { + perror("select"); + throw (IOException("Failed to select")); + } + if (FD_ISSET(m_fd, &fds)) { + return true; + } else { + return false; + } +} + +bool FileDescriptor::close() +{ + if (m_fd > 0) { + if (::close(m_fd) != 0) { + return false; + } + } + m_fd = -1; + return true; +} diff --git a/ROPCApplications/daq/slc/system/src/Inotify.cc b/ROPCApplications/daq/slc/system/src/Inotify.cc new file mode 100644 index 0000000000000000000000000000000000000000..3a200a49566ae74c9302c87670f1295aa9a146e3 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/Inotify.cc @@ -0,0 +1,62 @@ +#include "daq/slc/system/Inotify.h" + +#include <daq/slc/base/IOException.h> + +#include <sys/inotify.h> +#include <unistd.h> + +using namespace Belle2; + +const unsigned long Inotify::FILE_CREATE(IN_CREATE); +const unsigned long Inotify::FILE_OPEN(IN_OPEN); +const unsigned long Inotify::FILE_CLOSE_WRITE(IN_CLOSE_WRITE); +const unsigned long Inotify::FILE_CLOSE_NOWRITE(IN_CLOSE_NOWRITE); +const unsigned long Inotify::FILE_DELETE(IN_DELETE); +const unsigned long Inotify::FILE_MODIFY(IN_MODIFY); +const unsigned long Inotify::FILE_ACCESS(IN_ACCESS); +const unsigned long Inotify::FILE_ATTRIB(IN_ATTRIB); + +void Inotify::open() +{ + if ((m_fd = ::inotify_init()) < 0) { + throw (IOException("Failed to initialize inotify.")); + } +} + +int Inotify::add(const std::string& path, unsigned long mask) +{ + int wd = inotify_add_watch(m_fd, path.c_str(), mask); + if (wd < 0) { + throw (IOException("Failed to add a path")); + } + return wd; +} + +void Inotify::remove(int wd) +{ + if (::inotify_rm_watch(m_fd, wd) < 0) { + throw (IOException("Failed to remove a path")); + } +} + +InotifyEventList Inotify::wait(int sec) +{ + InotifyEventList ievent_v; + //try { + if (select(sec, 0)) { + char buf[16384]; + int r = read(m_fd, buf, 16384); + if (r <= 0) return ievent_v; + int index = 0; + while (index < r) { + inotify_event* ev = (inotify_event*)&buf[index]; + int event_size = sizeof(inotify_event) + ev->len; + index += event_size; + ievent_v.push_back(InotifyEvent(ev->wd, ev->mask, ev->name)); + } + } + //} catch (const IOException& e) { + //std::cerr << "Failed to select" << std::endl; + //} + return ievent_v; +} diff --git a/ROPCApplications/daq/slc/system/src/LogFile.cc b/ROPCApplications/daq/slc/system/src/LogFile.cc new file mode 100644 index 0000000000000000000000000000000000000000..98f45356138126d963cb83b921ba632d7cac96b0 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/LogFile.cc @@ -0,0 +1,180 @@ +#include "daq/slc/system/LogFile.h" + +#include <daq/slc/base/ConfigFile.h> +#include <daq/slc/base/StringUtil.h> + +#include <iostream> +#include <sstream> +#include <cstdio> + +#include <sys/stat.h> +#include <cstdlib> +#include <daq/slc/system/LockGuard.h> + +using namespace Belle2; + +bool LogFile::g_stderr = true; +bool LogFile::g_opened = false; +std::string LogFile::g_filepath; +std::string LogFile::g_linkpath; +std::ofstream LogFile::g_stream; +unsigned int LogFile::g_filesize = 0; +Mutex LogFile::g_mutex; +LogFile::Priority LogFile::g_threshold; +std::string LogFile::g_filename; +Date LogFile::g_date; + +LogFile::Priority LogFile::getPriority(const std::string& str) +{ + const std::string s = StringUtil::toupper(str); + if (s == "DEBUG") { + return DEBUG; + } else if (s == "INFO") { + return INFO; + } else if (s == "NOTICE") { + return NOTICE; + } else if (s == "WARNING") { + return WARNING; + } else if (s == "ERROR") { + return ERROR; + } else if (s == "FATAL") { + return FATAL; + } + return UNKNOWN; +} + +void LogFile::open(const std::string& filename, Priority threshold) +{ + if (!g_opened) { + ConfigFile config("slowcontrol"); + std::string path = config.get("log.dir"); + //if (path.size() == 0) path = config.get("logfile.dir"); + system(("mkdir -p " + path + "/" + filename).c_str()); + g_filename = filename; + g_date = Date(); + g_filepath = path + StringUtil::form("/%s/%s.log", filename.c_str(), g_date.toString("%Y.%m.%d")); + g_linkpath = path + StringUtil::form("/%s/latest.log", filename.c_str()); + //"/latest.log"; + g_threshold = threshold; + g_opened = true; + open(); + } +} + +void LogFile::open() +{ + if (!g_opened) return; + struct stat st; + if (stat(g_filepath.c_str(), &st) == 0) { + g_filesize = st.st_size; + g_stream.open(g_filepath.c_str(), std::ios::out | std::ios::app); + } else { + g_filesize = 0; + g_stream.open(g_filepath.c_str(), std::ios::out); + } + debug("/* ---------- log file opened ---------- */"); + debug("log file : %s (%d) ", g_filepath.c_str(), g_filesize); + std::string cmd = "ln -sf " + g_filepath + " " + g_linkpath; + system(cmd.c_str()); + debug("sym link : %s", g_linkpath.c_str()); +} + +void LogFile::close() +{ + if (!g_opened) return; + g_stream.close(); + g_opened = false; +} + +void LogFile::debug(const std::string& msg, ...) +{ + va_list ap; + va_start(ap, msg); + put_impl(msg, DEBUG, ap); + va_end(ap); +} + +void LogFile::info(const std::string& msg, ...) +{ + va_list ap; + va_start(ap, msg); + put_impl(msg, INFO, ap); + va_end(ap); +} + +void LogFile::notice(const std::string& msg, ...) +{ + va_list ap; + va_start(ap, msg); + put_impl(msg, NOTICE, ap); + va_end(ap); +} + +void LogFile::warning(const std::string& msg, ...) +{ + va_list ap; + va_start(ap, msg); + put_impl(msg, WARNING, ap); + va_end(ap); +} + +void LogFile::error(const std::string& msg, ...) +{ + va_list ap; + va_start(ap, msg); + put_impl(msg, ERROR, ap); + va_end(ap); +} + +void LogFile::fatal(const std::string& msg, ...) +{ + va_list ap; + va_start(ap, msg); + put_impl(msg, FATAL, ap); + va_end(ap); +} + + +void LogFile::put(Priority priority, const std::string& msg, ...) +{ + va_list ap; + va_start(ap, msg); + put_impl(msg, priority, ap); + va_end(ap); +} + +int LogFile::put_impl(const std::string& msg, Priority priority, va_list ap) +{ + LockGuard lockGuard(g_mutex); + if (g_threshold > priority) { + return 0; + } + Date date; + if (g_date.getDay() != date.getDay()) { + g_stream.close(); + open(); + } + std::stringstream ss; + ss << "[" << date.toString(); + std::string color = "\x1b[49m\x1b[39m"; + switch (priority) { + case DEBUG: color = "\x1b[49m\x1b[39m"; ss << "] [DEBUG] "; break; + case INFO: color = "\x1b[49m\x1b[32m"; ss << "] [INFO] "; break; + case NOTICE: color = "\x1b[49m\x1b[34m"; ss << "] [NOTICE] "; break; + case WARNING: color = "\x1b[49m\x1b[35m"; ss << "] [WARNING] "; break; + case ERROR: color = "\x1b[49m\x1b[31m"; ss << "] [ERROR] "; break; + case FATAL: color = "\x1b[41m\x1b[37m"; ss << "] [FATAL] "; break; + default: ss << "] [UNKNOWN] "; break; + } + static char* s = new char[1024 * 1024 * 5]; + vsprintf(s, msg.c_str(), ap); + ss << s << std::endl; + std::string str = ss.str(); + std::cerr << color << str << "\x1b[49m\x1b[39m"; + if (g_opened) { + g_stream << str; + g_stream.flush(); + g_filesize += str.size(); + } + return (int) str.size(); +} diff --git a/ROPCApplications/daq/slc/system/src/MCond.cc b/ROPCApplications/daq/slc/system/src/MCond.cc new file mode 100644 index 0000000000000000000000000000000000000000..32a898455e24ad0ba5ba57d54885e03a4587b671 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/MCond.cc @@ -0,0 +1,89 @@ +#include "daq/slc/system/MCond.h" + +#include <sys/time.h> + +using namespace Belle2; + +MCond::MCond() {} + +MCond::MCond(const MCond& cond) +{ + *this = cond; +} + +MCond::MCond(void* addr) +{ + set((pthread_cond_t*)addr); +} + +MCond::~MCond() {} + +bool MCond::init(void* addr) +{ + set(addr); + init(); + return true; +} + +bool MCond::init() +{ + pthread_condattr_t attr; + pthread_condattr_init(&attr); + pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + pthread_cond_init(m_cond, &attr); + pthread_condattr_destroy(&attr); + return true; +} + +bool MCond::set(void* addr) +{ + m_cond = (pthread_cond_t*)addr; + return true; +} + +bool MCond::destroy() +{ + pthread_cond_destroy(m_cond); + return true; +} + +bool MCond::signal() +{ + if (pthread_cond_signal(m_cond) == 0) return true; + else return false; +} + +bool MCond::broadcast() +{ + if (pthread_cond_broadcast(m_cond) == 0) return true; + else return false; +} + +bool MCond::wait(MMutex& cond) +{ + if (pthread_cond_wait(m_cond, cond.m_mu) != 0) { + return false; + } + return true; +} + +bool MCond::wait(MMutex& mutex, const unsigned int sec, + const unsigned int msec) +{ + struct timeval now; + struct timespec timeout; + + gettimeofday(&now, NULL); + timeout.tv_sec = now.tv_sec + sec; + timeout.tv_nsec = now.tv_usec * 1000 + msec; + if (pthread_cond_timedwait(m_cond, mutex.m_mu, &timeout) != 0) { + return false; + } + return true; +} + +const MCond& MCond::operator=(const MCond& cond) +{ + m_cond = cond.m_cond; + return *this; +} diff --git a/ROPCApplications/daq/slc/system/src/MMutex.cc b/ROPCApplications/daq/slc/system/src/MMutex.cc new file mode 100644 index 0000000000000000000000000000000000000000..c7262d83b525c82c68ac4059daf402e36b5d8784 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/MMutex.cc @@ -0,0 +1,81 @@ +#include "daq/slc/system/MMutex.h" + +using namespace Belle2; + +MMutex::MMutex() {} + +MMutex::MMutex(const MMutex& mutex) +{ + *this = mutex; +} + +MMutex::MMutex(void* addr) +{ + set((pthread_mutex_t*)addr); +} + +MMutex::~MMutex() {} + +bool MMutex::init(void* addr) +{ + set(addr); + init(); + return true; +} + +bool MMutex::init() +{ + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + pthread_mutex_init(m_mu, &attr); + pthread_mutexattr_destroy(&attr); + return true; +} + +bool MMutex::set(void* addr) +{ + m_mu = (pthread_mutex_t*)addr; + return true; +} + +bool MMutex::lock() +{ + if (pthread_mutex_lock(m_mu) != 0) { + return false; + } else { + return true; + } +} + +bool MMutex::trylock() +{ + if (pthread_mutex_lock(m_mu) != 0) { + return false; + } + return true; +} + +bool MMutex::unlock() +{ + if (pthread_mutex_unlock(m_mu) != 0) { + return true; + } else { + return false; + } +} + +bool MMutex::destroy() +{ + if (pthread_mutex_destroy(m_mu) != 0) { + return true; + } else { + return false; + } +} + +const MMutex& MMutex::operator=(const MMutex& mutex) +{ + m_mu = mutex.m_mu; + return *this; +} diff --git a/ROPCApplications/daq/slc/system/src/Mutex.cc b/ROPCApplications/daq/slc/system/src/Mutex.cc new file mode 100644 index 0000000000000000000000000000000000000000..5d79b22dddb982faaa4c293f6127d7b9d2f3b1e7 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/Mutex.cc @@ -0,0 +1,29 @@ +#include "daq/slc/system/Mutex.h" + +using namespace Belle2; + +Mutex::Mutex() +{ + pthread_mutexattr_init(&m_attr); + pthread_mutex_init(&m_mu, &m_attr); +} + +Mutex::~Mutex() +{ +} + +bool Mutex::lock() +{ + if (pthread_mutex_lock(&m_mu) != 0) { + return false; + } + return true; +} + +bool Mutex::unlock() +{ + if (pthread_mutex_unlock(&m_mu) != 0) { + return false; + } + return true; +} diff --git a/ROPCApplications/daq/slc/system/src/RWLock.cc b/ROPCApplications/daq/slc/system/src/RWLock.cc new file mode 100644 index 0000000000000000000000000000000000000000..b6697bd43b72261e160679614de1668c7781c0cb --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/RWLock.cc @@ -0,0 +1,53 @@ +#include "daq/slc/system/RWLock.h" + +using namespace Belle2; + +RWLock::RWLock() : m_lock() {} + +RWLock::~RWLock() {} + +bool RWLock::init() +{ + pthread_rwlockattr_t attr; + pthread_rwlockattr_init(&attr); + pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + pthread_rwlock_init(&m_lock, &attr); + pthread_rwlockattr_destroy(&attr); + return true; +} + +bool RWLock::rdlock() +{ + if (pthread_rwlock_rdlock(&m_lock) != 0) { + return false; + } else { + return true; + } +} + +bool RWLock::wrlock() +{ + if (pthread_rwlock_wrlock(&m_lock) != 0) { + return false; + } + return true; +} + +bool RWLock::unlock() +{ + if (pthread_rwlock_unlock(&m_lock) != 0) { + return true; + } else { + return false; + } +} + +bool RWLock::destroy() +{ + if (pthread_rwlock_destroy(&m_lock) != 0) { + return true; + } else { + return false; + } +} + diff --git a/ROPCApplications/daq/slc/system/src/SharedMemory.cc b/ROPCApplications/daq/slc/system/src/SharedMemory.cc new file mode 100644 index 0000000000000000000000000000000000000000..c04e3fab17e1805e81b5c7cbb0433a067b215819 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/SharedMemory.cc @@ -0,0 +1,130 @@ +#include "daq/slc/system/SharedMemory.h" + +#include <sys/stat.h> +#include <unistd.h> +#include <stdio.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <errno.h> + +using namespace Belle2; + +bool SharedMemory::unlink(const std::string& path) +{ + return (::shm_unlink(path.c_str()) == -1); +} + +SharedMemory::SharedMemory() + : m_fd(-1), m_path(), m_size(0), m_addr(NULL) {} + +SharedMemory::SharedMemory(const std::string& path, size_t size) + : m_fd(-1), m_path(path), m_size(size), m_addr(NULL) +{ +} + +SharedMemory::SharedMemory(const SharedMemory& file) + : m_fd(file.m_fd), m_path(file.m_path), + m_size(file.m_size), m_addr(file.m_addr) {} + +SharedMemory::~SharedMemory() {} + +bool SharedMemory::open(const std::string& path, size_t size) +{ + errno = 0; + int fd = ::shm_open(path.c_str(), O_CREAT | O_EXCL | O_RDWR, 0666); + if (fd < 0) { + if (errno != EEXIST) { + perror("shm_oepn"); + return false; + } + fd = ::shm_open(path.c_str(), O_CREAT | O_RDWR, 0666); + if (fd < 0) { + perror("shm_oepn"); + return false; + } + } + m_fd = fd; + m_path = path; + truncate(size); + return true; +} + +bool SharedMemory::open() +{ + return open(m_path, m_size); +} + +void SharedMemory::close() +{ + if (m_fd > 0) { + if (m_addr != NULL) munmap(m_addr, m_size); + ::close(m_fd); + m_fd = 0; + } +} + +bool SharedMemory::truncate(size_t size) +{ + if (size > 0) { + ::ftruncate(m_fd, size); + m_size = size; + return true; + } else { + struct stat st; + fstat(m_fd, &st); + m_size = st.st_size; + } + return false; +} + + +void* SharedMemory::map(size_t offset, size_t size) +{ + errno = 0; + void* addr = ::mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_SHARED, m_fd, offset); + if (addr == MAP_FAILED) { + perror("mmap"); + addr = NULL; + } + m_addr = addr; + m_size = size; + return addr; +} + +void* SharedMemory::map() +{ + if (m_addr == NULL) m_addr = map(0, m_size); + return m_addr; +} + +bool SharedMemory::unlink() +{ + close(); + return (unlink(m_path)); +} + +bool SharedMemory::seekTo(size_t offset) +{ + return (lseek(m_fd, offset, SEEK_SET) == -1); +} + +bool SharedMemory::seekBy(size_t offset) +{ + return (lseek(m_fd, offset, SEEK_CUR) == -1); +} + +bool SharedMemory::isOpened() +{ + return (m_fd != 0); +} + +const SharedMemory& SharedMemory::operator=(const SharedMemory& file) +{ + m_fd = file.m_fd; + m_path = file.m_path; + m_size = file.m_size; + m_addr = file.m_addr; + return *this; +} + diff --git a/ROPCApplications/daq/slc/system/src/StreamSizeCounter.cc b/ROPCApplications/daq/slc/system/src/StreamSizeCounter.cc new file mode 100644 index 0000000000000000000000000000000000000000..6828bd474c20240f3dd6c356ae4c5bbca591ad73 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/StreamSizeCounter.cc @@ -0,0 +1,9 @@ +#include "daq/slc/system/StreamSizeCounter.h" + +using namespace Belle2; + +size_t StreamSizeCounter::write(const void*, size_t count) +{ + m_count += count; + return count; +} diff --git a/ROPCApplications/daq/slc/system/src/TCPServerSocket.cc b/ROPCApplications/daq/slc/system/src/TCPServerSocket.cc new file mode 100644 index 0000000000000000000000000000000000000000..d1bc9aa0a863617fe3f5f7c57893af28db90c880 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/TCPServerSocket.cc @@ -0,0 +1,97 @@ +#include "daq/slc/system/TCPServerSocket.h" + +#include <daq/slc/base/IOException.h> + +#include <cstdio> +#include <cstring> + +#include <sys/socket.h> +#include <arpa/inet.h> +#include <errno.h> + +#include <netinet/in.h> +#include <netdb.h> + +using namespace Belle2; + +int TCPServerSocket::open(const std::string& ip, unsigned short port, + int nqueue) +{ + m_ip = ip; + m_port = port; + return open(nqueue); +} + +int TCPServerSocket::open(int nqueue) +{ + if (m_fd > 0) { + throw (IOException("Socket is working already.")); + } + sockaddr_in addr; + memset(&addr, 0, sizeof(sockaddr_in)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(m_port); + + m_fd = socket(PF_INET, SOCK_STREAM, 0); + if (m_fd == -1) { + m_fd = 0; + throw (IOException("Fail to create a server socket.")); + } + int enable = 1; + if (setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1) { + m_fd = 0; + throw (IOException("Fail to set resue address for the socket.")); + } + struct hostent* host = NULL; + host = gethostbyname(m_ip.c_str()); + if (host == NULL) { + unsigned long ip_address = inet_addr(m_ip.c_str()); + if ((signed long) ip_address < 0) { + throw (std::exception()); + throw (IOException("Wrong host name or ip")); + } else { + host = gethostbyaddr((char*)&ip_address, sizeof(ip_address), AF_INET); + } + } + if (host == NULL) { + throw (IOException("Fail to get host ip: %s", m_ip.c_str())); + } + addr.sin_addr.s_addr = (*(unsigned long*)host->h_addr_list[0]); + + if (bind(m_fd, (const sockaddr*) & (addr), sizeof(sockaddr_in)) != 0) { + throw (IOException("Fail to bind the socket. %s:%d", m_ip.c_str(), m_port)); + } + if (listen(m_fd, nqueue) != 0) { + throw (IOException("Fail to listen to the socket.")); + } + return m_fd; +} + +TCPSocket TCPServerSocket::accept() +{ + socklen_t len = sizeof(sockaddr_in); + sockaddr_in addr; + memset(&addr, 0, sizeof(sockaddr_in)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(m_port); + int fd; + errno = 0; + while (true) { + if ((fd = ::accept(m_fd, (sockaddr*) & (addr), &len)) == -1) { + switch (errno) { + case EINTR: continue; + case EAGAIN: continue; + default: + perror("accept"); + throw (IOException("Fail to accept.")); + } + } + break; + } + TCPSocket s(fd); + s.m_ip = inet_ntoa(addr.sin_addr); + s.m_port = ntohs(addr.sin_port); + return s; +} diff --git a/ROPCApplications/daq/slc/system/src/TCPSocket.cc b/ROPCApplications/daq/slc/system/src/TCPSocket.cc new file mode 100644 index 0000000000000000000000000000000000000000..7974c1ff2b900d1064888cddc0af20d9286595b3 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/TCPSocket.cc @@ -0,0 +1,199 @@ +#include "daq/slc/system/TCPSocket.h" + +#include <daq/slc/base/IOException.h> + +#include <cstdio> +#include <cstring> + +#include <sys/socket.h> +#include <arpa/inet.h> + +#include <unistd.h> +#include <netinet/in.h> +#include <errno.h> +#include <netdb.h> + +using namespace Belle2; + +int TCPSocket::connect(const std::string& ip, unsigned short port) +{ + m_ip = ip; + m_port = port; + return connect(); +} + +int TCPSocket::connect() +{ + if (m_fd > 0) { + throw (IOException("Socket is working already.")); + } + sockaddr_in addr; + memset(&addr, 0, sizeof(sockaddr_in)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(m_port); + + if ((m_fd = ::socket(PF_INET, SOCK_STREAM, 0)) < 0) { + throw (IOException("Failed to create socket")); + } + struct hostent* host = NULL; + host = gethostbyname(m_ip.c_str()); + if (host == NULL) { + unsigned long ip_address = inet_addr(m_ip.c_str()); + if ((signed long) ip_address < 0) { + throw (IOException("Wrong host name or ip")); + } else { + host = gethostbyaddr((char*)&ip_address, sizeof(ip_address), AF_INET); + } + } + if (host == NULL) { + close(); + throw (IOException("Failed to connect host %s:%d", + m_ip.c_str(), m_port)); + } + addr.sin_addr.s_addr = (*(unsigned long*) host->h_addr_list[0]); + + if (::connect(m_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + close(); + throw (IOException("Failed to connect host %s:%d", + m_ip.c_str(), m_port)); + } + + return m_fd; +} + +void TCPSocket::setBufferSize(int size) +{ + if (size > 0) { + if (setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) != 0) { + throw (IOException("failed to SO_SNDBUF: %s\n", strerror(errno))); + } + if (setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) != 0) { + throw (IOException("error on SO_RCVBUF: %s\n", strerror(errno))); + } + } +} + +size_t TCPSocket::write(const void* buf, size_t count) +{ + size_t c = 0; + int ret; + while (c < count) { + errno = 0; + ret = send(m_fd, ((unsigned char*)buf + c), (count - c), MSG_NOSIGNAL); + if (ret <= 0) { + switch (errno) { + case EINTR: continue; + case ENETUNREACH: + case EHOSTUNREACH: + case ETIMEDOUT: + usleep(500); + continue; + default: + throw (IOException("Error while writing")); + } + } + c += ret; + } + return c; +} + +size_t TCPSocket::read(void* buf, size_t count) +{ + size_t c = 0; + int ret; + while (c < count) { + errno = 0; + ret = recv(m_fd, ((unsigned char*)buf + c), (count - c), 0); + if (ret <= 0) { + switch (errno) { + case EINTR: continue; + case EAGAIN: continue; + default: + throw (IOException("TCPSocket::read Error while reading.")); + } + } + c += ret; + } + return c; +} + +size_t TCPSocket::read_once(void* buf, size_t count) +{ + int ret; + errno = 0; + while (true) { + ret = recv(m_fd, buf, count, 0); + if (ret <= 0) { + switch (errno) { + case EINTR: continue; + case EAGAIN: continue; + default: + throw (IOException("TCPSocket::read_once Error while reading.")); + } + } + break; + } + return ret; +} + +void TCPSocket::print() +{ + sockaddr_in sa; + memset(&sa, 0, sizeof(sockaddr_in)); + socklen_t sa_len = sizeof(sa); + if (getsockname(m_fd, (struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) { + perror("getsockname"); + } + printf("Local IP address is: %s\n", inet_ntoa(sa.sin_addr)); + printf("Local port is: %d\n", (int) ntohs(sa.sin_port)); +} + +const std::string TCPSocket::getLocalIP() +{ + sockaddr_in sa; + memset(&sa, 0, sizeof(sockaddr_in)); + socklen_t sa_len = sizeof(sa); + if (getsockname(m_fd, (struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) { + return ""; + } + return inet_ntoa(sa.sin_addr); +} + +int TCPSocket::getLocalAddress() +{ + sockaddr_in sa; + memset(&sa, 0, sizeof(sockaddr_in)); + socklen_t sa_len = sizeof(sa); + if (getsockname(m_fd, (struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) { + return 0; + } + return sa.sin_addr.s_addr; +} + +int TCPSocket::getLocalPort() +{ + sockaddr_in sa; + memset(&sa, 0, sizeof(sockaddr_in)); + socklen_t sa_len = sizeof(sa); + if (getsockname(m_fd, (struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) { + return 0; + } + return ntohs(sa.sin_port); +} + +unsigned int TCPSocket::getAddress() +{ + struct hostent* host = NULL; + host = gethostbyname(m_ip.c_str()); + if (host == NULL) { + unsigned long ip_address = inet_addr(m_ip.c_str()); + if ((signed long) ip_address < 0) { + throw (std::exception()); + throw (IOException("Wrong host name or ip")); + } else { + host = gethostbyaddr((char*)&ip_address, sizeof(ip_address), AF_INET); + } + } + return (*(unsigned long*) host->h_addr_list[0]); +} diff --git a/ROPCApplications/daq/slc/system/src/Time.cc b/ROPCApplications/daq/slc/system/src/Time.cc new file mode 100644 index 0000000000000000000000000000000000000000..8d4d066a8a7e64414cc0509cbab9bef010c8f8a7 --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/Time.cc @@ -0,0 +1,145 @@ +#include "daq/slc/system/Time.h" + +#include "daq/slc/base/Reader.h" +#include "daq/slc/base/Writer.h" + +#include <sys/time.h> + +#include <cmath> +#include <cstdlib> +#include <sstream> + +using namespace Belle2; + +#define MEGA 1000000 + +Time::Time() +{ + timeval tv; + gettimeofday(&tv, 0); + m_s = tv.tv_sec; + m_us = tv.tv_usec; +} + +Time::Time(const double t) +{ + set(t); +} + +Time::Time(const long s, const long us) + : m_s(s), m_us(us) +{ + adjust(); +} + +Time::~Time() +{ +} + +void Time::adjust() +{ + if (MEGA <= labs(m_us)) { + m_s += m_us / MEGA; + m_us = m_us - (m_us / MEGA) * MEGA; + } + if (0 < m_s && m_us < 0) { + m_s--; + m_us += MEGA; + } + if (m_s < 0 && 0 < m_us) { + m_s++; + m_us -= MEGA; + } +} + +void Time::clear() +{ + m_s = m_us = 0; +} + +void Time::set() +{ + timeval tv; + gettimeofday(&tv, 0); + m_s = tv.tv_sec; + m_us = tv.tv_usec; +} + +void Time::set(const double t) +{ + double s, us; + us = modf(t, &s); + m_s = (long)s; + m_us = (long)(us * 1000000); + adjust(); +} + +void Time::set(const long s, const long us) +{ + m_s = s; + m_us = us; + adjust(); +} + +double Time::get() const +{ + return (double)m_s + ((double)m_us) / 1000000.; +} + +long Time::getSecond() const +{ + return m_s; +} + +long Time::getMicroSecond() const +{ + return m_us; +} + +std::string Time::toString() const +{ + std::stringstream ss; + ss << m_s << "." << m_us; + return ss.str(); +} + +void Time::readObject(Reader& r) +{ + m_s = r.readLong(); + m_us = r.readLong(); +} + +void Time::writeObject(Writer& w) const +{ + w.writeLong(m_s); + w.writeLong(m_us); +} + +Time& Time::operator = (const Time& t) +{ + m_s = t.m_s; + m_us = t.m_us; + return *this; +} + +bool Time::operator == (const Time& t) const +{ + return (m_s == t.m_s) && (m_us == t.m_us); +} + +Time Time::operator + (const Time& t) const +{ + Time tt = *this; + tt.m_s += t.m_s; + tt.m_us += t.m_us; + return tt; +} + +Time Time::operator - (const Time& t) const +{ + Time tt = *this; + tt.m_s -= t.m_s; + tt.m_us -= t.m_us; + return tt; +} + diff --git a/ROPCApplications/daq/slc/system/src/UDPSocket.cc b/ROPCApplications/daq/slc/system/src/UDPSocket.cc new file mode 100644 index 0000000000000000000000000000000000000000..d85817e0c3afed3a017c9e26f1eca87b4151b4ae --- /dev/null +++ b/ROPCApplications/daq/slc/system/src/UDPSocket.cc @@ -0,0 +1,225 @@ +#include "daq/slc/system/UDPSocket.h" + +#include <daq/slc/base/IOException.h> + +#include <cstdio> +#include <cstring> + +#include <unistd.h> +#include <errno.h> + +#include <sys/socket.h> +#include <netinet/in.h> +#include <netdb.h> +#include <arpa/inet.h> +#include <ifaddrs.h> + +using namespace Belle2; + +unsigned int UDPSocket::findSubnet(unsigned int addr) +{ + struct ifaddrs* ifa_list; + struct ifaddrs* ifa; + char addrstr[256], netmaskstr[256]; + if (getifaddrs(&ifa_list) != 0) return 0; + for (ifa = ifa_list; ifa != NULL; ifa = ifa->ifa_next) { + memset(addrstr, 0, sizeof(addrstr)); + memset(netmaskstr, 0, sizeof(netmaskstr)); + if (ifa->ifa_addr->sa_family == AF_INET && + addr == ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr) { + addr |= ~(((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr); + } + } + freeifaddrs(ifa_list); + return addr; +} + +UDPSocket::UDPSocket() +{ + m_addr.sin_family = AF_INET; + m_addr.sin_addr.s_addr = INADDR_ANY; +} + +UDPSocket::UDPSocket(unsigned int port) +{ + m_addr.sin_port = htons(port); + m_addr.sin_family = AF_INET; + m_addr.sin_addr.s_addr = INADDR_ANY; + if ((m_fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + throw (IOException("Failed to create socket")); + } +} + +UDPSocket::UDPSocket(unsigned int port, + const std::string& hostname, + bool boardcast) +{ + m_addr.sin_port = htons(port); + m_addr.sin_family = AF_INET; + if (hostname.size() > 0) { + struct hostent* host = NULL; + host = gethostbyname(hostname.c_str()); + if (host == NULL) { + unsigned long addr = inet_addr(hostname.c_str()); + if ((signed long) addr < 0) { + throw (std::exception()); + throw (IOException("Wrong host name or ip")); + } else { + host = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET); + } + } + m_addr.sin_addr.s_addr = (*(unsigned long*) host->h_addr_list[0]); + } + if ((m_fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + throw (IOException("Failed to create socket")); + } + if (boardcast) { + unsigned int addr = m_addr.sin_addr.s_addr; + if ((addr = findSubnet(addr)) > 0) { + m_addr.sin_addr.s_addr = addr; + } + int yes = 1; + setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST, + (char*)&yes, sizeof(yes)); + } +} + +UDPSocket::UDPSocket(unsigned int port, + unsigned int addr, + bool boardcast) +{ + m_addr.sin_port = htons(port); + m_addr.sin_family = AF_INET; + m_addr.sin_addr.s_addr = addr; + if ((m_fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + throw (IOException("Failed to create socket")); + } + if (boardcast) { + addr = m_addr.sin_addr.s_addr; + if ((addr = findSubnet(addr)) > 0) { + m_addr.sin_addr.s_addr = addr; + } + int yes = 1; + setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST, + (char*)&yes, sizeof(yes)); + } +} + +int UDPSocket::bind(unsigned int port, + const std::string& hostname, bool broadcast) +{ + if (m_fd <= 0 && (m_fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + throw (IOException("Failed to create socket")); + } + m_addr.sin_port = htons(port); + if (hostname.size() > 0) { + struct hostent* host = NULL; + host = gethostbyname(hostname.c_str()); + if (host == NULL) { + unsigned long addr = inet_addr(hostname.c_str()); + if ((signed long) addr < 0) { + throw (std::exception()); + throw (IOException("Wrong host name or ip")); + } else { + host = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET); + } + } + m_addr.sin_addr.s_addr = (*(unsigned long*) host->h_addr_list[0]); + if (broadcast) { + unsigned int addr = m_addr.sin_addr.s_addr; + if ((addr = findSubnet(addr)) > 0) { + m_addr.sin_addr.s_addr = addr; + } + } + } + return bind(); +} + +int UDPSocket::bind() +{ + if (::bind(m_fd, (struct sockaddr*)&m_addr, sizeof(m_addr)) < 0) { + close(); + throw (IOException("Failed to bind socket port=%d", + ntohs(m_addr.sin_port))); + } + return m_fd; +} + +size_t UDPSocket::write(const void* buf, size_t count) +{ + size_t c = 0; + int ret; + while (c < count) { + errno = 0; + ret = sendto(m_fd, ((unsigned char*)buf + c), + (count - c), 0, + (struct sockaddr*)&m_addr, sizeof(m_addr)); + if (ret <= 0) { + switch (errno) { + case EINTR: continue; + case ENETUNREACH: + case EHOSTUNREACH: + case ETIMEDOUT: + usleep(500); + continue; + default: + throw (IOException("Error while writing")); + } + } + c += ret; + } + return c; +} + +size_t UDPSocket::read(void* buf, size_t count) +{ + size_t c = 0; + int ret; + socklen_t addrlen = sizeof(m_remote_addr); + while (true) { + errno = 0; + ret = recvfrom(m_fd, ((unsigned char*)buf + c), (count - c), 0, + (struct sockaddr*)&m_remote_addr, &addrlen); + if (ret <= 0) { + switch (errno) { + case EINTR: continue; + case EAGAIN: continue; + default: + throw (IOException("Error while reading.")); + } + } + c += ret; + break; + } + return c; +} + +const std::string UDPSocket::getHostName() const +{ + return inet_ntoa(m_addr.sin_addr); +} + +unsigned int UDPSocket::getAddress() const +{ + return m_addr.sin_addr.s_addr; +} + +unsigned int UDPSocket::getPort() const +{ + return ntohs(m_addr.sin_port); +} + +const std::string UDPSocket::getRemoteHostName() const +{ + return inet_ntoa(m_remote_addr.sin_addr); +} + +unsigned int UDPSocket::getRemoteAddress() const +{ + return m_remote_addr.sin_addr.s_addr; +} + +unsigned int UDPSocket::getRemotePort() const +{ + return ntohs(m_remote_addr.sin_port); +} diff --git a/ROPCApplications/rawdata/dataobjects/SConscript b/ROPCApplications/rawdata/dataobjects/SConscript new file mode 100644 index 0000000000000000000000000000000000000000..d688e8ce5d70876d7a361e77f28ab30fd603738c --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/SConscript @@ -0,0 +1,5 @@ +Import('env') + +env['LIBS'] = ['framework', 'rawdata', 'Core'] + +Return('env') diff --git a/ROPCApplications/rawdata/dataobjects/include/PostRawCOPPERFormat_latest.h b/ROPCApplications/rawdata/dataobjects/include/PostRawCOPPERFormat_latest.h new file mode 100644 index 0000000000000000000000000000000000000000..ff8b5d2b5d4168a008899396e5833b511afd253d --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/PostRawCOPPERFormat_latest.h @@ -0,0 +1,347 @@ +//+ +// File : PostRawCOPPERFormat_latest.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef POSTRAWCOPPERFORMAT_LATEST_H +#define POSTRAWCOPPERFORMAT_LATEST_H + +// Includes +/* #include <stdio.h> */ +/* #include <stdlib.h> */ +/* #include <string> */ +/* #include <sys/time.h> */ + +#include <rawdata/dataobjects/RawCOPPERFormat_latest.h> +#include <rawdata/CRCCalculator.h> + +//#include <framework/datastore/DataStore.h> +//#include <TObject.h> + +// version # +#define POST_RAWCOPPER_FORMAT_VER1 1 + +//#define USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + +namespace Belle2 { + + /** + * The Raw COPPER class ver.1 ( the latest version since May, 2014 ) + * This class stores data received by COPPER via belle2linkt + * Data from all detectors except PXD are stored in this class + */ + class PostRawCOPPERFormat_latest : public RawCOPPERFormat_latest { + public: + //! Default constructor + PostRawCOPPERFormat_latest(); + + //! Constructor using existing pointer to raw data buffer + //PostRawCOPPERFormat_latest(int* bufin, int nwords); + //! Destructor + virtual ~PostRawCOPPERFormat_latest(); + + // + // Get position of or pointer to data + // + /////////////////////////////////////////////////////////////////////////////////////// + // POINTER TO "DETECTOR BUFFER" + //( after removing "B2link headers" from "FINESSE buffer". THIS IS THE RAW DATA FROM A DETECTOR + /////////////////////////////////////////////////////////////////////////////////////// + + //! get Detector buffer length + int GetDetectorNwords(int n, int finesse_num) OVERRIDE_CPP17; + + //! get Detector buffer of slot A + int* Get1stDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot B + int* Get2ndDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot C + int* Get3rdDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot D + int* Get4thDetectorBuffer(int n) OVERRIDE_CPP17; + /////////////////////////////////////////////////////////////////////////////////////// + + //! get posistion of COPPER block in unit of word + // virtual int GetBufferPos(int n); + + // + // Get information from 13words "COPPER header" attached by COPPER board + // + //! get COPPER counter(not event number) + unsigned int GetCOPPERCounter(int n) OVERRIDE_CPP17; + + //! get # of offset words for FINESSE slot A buffer position + int GetOffset1stFINESSE(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE buffer + int GetFINESSENwords(int n, int finesse) OVERRIDE_CPP17; + + // + // Get information from "B2link(attached by FEE and HLSB) header" + // + //! get b2l block from "FEE b2link header" + // virtual int* GetFTSW2Words(int n); + + //! get b2l block from "FEE b2link header" + virtual int* GetExpRunSubrunBuf(int n) OVERRIDE_CPP17; + + //! get b2l block from "FEE b2link header" + virtual unsigned int GetB2LFEE32bitEventNumber(int n) OVERRIDE_CPP17; + + // + // read magic word to check data + // + //! get magic word of COPPER driver header + unsigned int GetMagicDriverHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA header + unsigned int GetMagicFPGAHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA trailer + unsigned int GetMagicFPGATrailer(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER driver trailer + unsigned int GetMagicDriverTrailer(int n) OVERRIDE_CPP17; + + //! Get checksum in RawTrailer + unsigned int GetTrailerChksum(int n) OVERRIDE_CPP17; + + //! Get CRC16 value for an event + int GetEventCRC16Value(int n, int finesse_num) OVERRIDE_CPP17; + + //! Check if COPPER Magic words are correct + bool CheckCOPPERMagic(int n) OVERRIDE_CPP17; + + //! should be called by DeSerializerCOPPER.cc and fill contents in RawHeader + unsigned int FillTopBlockRawHeader(unsigned int m_node_id, + unsigned int prev_eve32, unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! read COPPER driver's checksum value + unsigned int GetDriverChkSum(int n) OVERRIDE_CPP17; + + //! calc COPPER driver's checksum value + unsigned int CalcDriverChkSum(int n) OVERRIDE_CPP17; + + //! check data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! check data contents + void CheckUtimeCtimeTRGType(int n) OVERRIDE_CPP17; + + //! check magic words + int CheckB2LHSLBMagicWords(int* finesse_buf, int finesse_nwords); + + //! check magic words + int CheckCRC16(int n, int finesse_num); + + //! Pack data (format ver. = -1 -> Select the latest format version) + int* PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info) OVERRIDE_CPP17; + + // + // size of "COPPER front header" and "COPPER trailer" + // + //! Copper data words = ( total_data_length in COPPER header ) + COPPER_HEADER_TRAILER_NWORDS + + enum { + SIZE_COPPER_DRIVER_HEADER = 0, + SIZE_COPPER_DRIVER_TRAILER = 0 + }; + + // Data Format : "COPPER header" + enum { + SIZE_COPPER_HEADER = 0 + }; + + // Data Format : "COPPER Trailer" + enum { + SIZE_COPPER_TRAILER = 0 + }; + + // Data Format : "B2Link HSLB Header" + enum { + POS_B2LHSLB_MAGIC = 0, + SIZE_B2LHSLB_HEADER = 1 + }; + + + // Data Format : "B2Link FEE Header" + enum { + POS_B2L_CTIME = 0, + SIZE_B2LFEE_HEADER = 1 + }; + + // Data Format : B2Link FEE Trailer + enum { + POS_B2LFEE_ERRCNT_CRC16 = 0, + SIZE_B2LFEE_TRAILER = 1 + }; + + // + // Data Format : "B2Link HSLB Trailer" + // + enum { + SIZE_B2LHSLB_TRAILER = 0 + }; + + + + protected : + ///ver.2 Change FEE format as presented at B2GM in Nov.2013 ( Nov.20, 2013) + // ClassDefOverride(PostRawCOPPERFormat_latest, 2); + + }; + + inline int PostRawCOPPERFormat_latest::GetOffset1stFINESSE(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + return pos_nwords; + } + + inline int* PostRawCOPPERFormat_latest::Get1stDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get1stFINESSENwords(n) > 0) { + int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PostRawCOPPERFormat_latest::Get2ndDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get2ndFINESSENwords(n) > 0) { + int pos_nwords = GetOffset2ndFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PostRawCOPPERFormat_latest::Get3rdDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get3rdFINESSENwords(n) > 0) { + int pos_nwords = GetOffset3rdFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PostRawCOPPERFormat_latest::Get4thDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get4thFINESSENwords(n) > 0) { + int pos_nwords = GetOffset4thFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PostRawCOPPERFormat_latest::GetExpRunSubrunBuf(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.POS_EXP_RUN_NO; + return &(m_buffer[ pos_nwords ]); + /* #ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 */ + /* CheckB2LFEEHeaderVersion(n); */ + /* #endif */ + /* int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + POS_EXP_RUN; */ + /* return &(m_buffer[ pos_nwords ]); */ + } + + inline unsigned int PostRawCOPPERFormat_latest::GetDriverChkSum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) + - tmp_trailer.RAWTRAILER_NWORDS - SIZE_COPPER_DRIVER_TRAILER; + return m_buffer[ pos_nwords ]; + } + + + inline unsigned int PostRawCOPPERFormat_latest::GetCOPPERCounter(int n) + { + char err_buf[500]; + sprintf(err_buf, "[FATAL] This data format does not have COPPER counter.(block %d) Exiting...\n %s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + std::string err_str = err_buf; throw (err_str); + return 0; + } + + inline unsigned int PostRawCOPPERFormat_latest::GetMagicDriverHeader(int n) + { + char err_buf[500]; + sprintf(err_buf, "[FATAL] This function is not supported. (block %d) Exiting...\n %s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + std::string err_str = err_buf; throw (err_str); + return 0; + } + + inline unsigned int PostRawCOPPERFormat_latest::GetMagicFPGAHeader(int n) + { + char err_buf[500]; + sprintf(err_buf, "[FATAL] This function is not supported. (block %d) Exiting...\n %s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + std::string err_str = err_buf; throw (err_str); + return 0; + } + + + inline unsigned int PostRawCOPPERFormat_latest::GetMagicFPGATrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 3; + + // printf( "[DEBUG] 1 %d 2 %d 3 %d\n", GetBufferPos(n), GetBlockNwords(n), tmp_trailer.GetTrlNwords()); + + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int PostRawCOPPERFormat_latest::GetMagicDriverTrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 1; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + inline unsigned int PostRawCOPPERFormat_latest::GetTrailerChksum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline int PostRawCOPPERFormat_latest::GetEventCRC16Value(int n, int finesse_num) + { + int fin_nwords = GetFINESSENwords(n, finesse_num); + if (fin_nwords > 0) { + int* buf = GetFINESSEBuffer(n, finesse_num) + fin_nwords + - ((SIZE_B2LFEE_TRAILER - POS_B2LFEE_ERRCNT_CRC16) + SIZE_B2LHSLB_TRAILER) ; + return (int)(*buf & 0xffff); + } + return -1; + } + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/PostRawCOPPERFormat_v1.h b/ROPCApplications/rawdata/dataobjects/include/PostRawCOPPERFormat_v1.h new file mode 100644 index 0000000000000000000000000000000000000000..f404a141f8e90dedcc9ee3ee6d1ad5677b75f536 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/PostRawCOPPERFormat_v1.h @@ -0,0 +1,333 @@ +//+ +// File : PostRawCOPPERFormat_v1.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef POSTRAWCOPPERFORMAT_V1_H +#define POSTRAWCOPPERFORMAT_V1_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <string> +#include <sys/time.h> + +#include <rawdata/dataobjects/RawCOPPERFormat_v1.h> +#include <rawdata/CRCCalculator.h> + + +#include <TObject.h> + +// version # +#define POST_RAWCOPPER_FORMAT_VER1 1 + +//#define USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + +namespace Belle2 { + + /** + * The Raw COPPER class ver.1 ( the latest version since May, 2014 ) + * This class stores data received by COPPER via belle2linkt + * Data from all detectors except PXD are stored in this class + */ + class PostRawCOPPERFormat_v1 : public RawCOPPERFormat_v1 { + public: + //! Default constructor + PostRawCOPPERFormat_v1(); + + //! Constructor using existing pointer to raw data buffer + //PostRawCOPPERFormat_v1(int* bufin, int nwords); + //! Destructor + virtual ~PostRawCOPPERFormat_v1(); + + // + // Get position of or pointer to data + // + /////////////////////////////////////////////////////////////////////////////////////// + // POINTER TO "DETECTOR BUFFER" + //( after removing "B2link headers" from "FINESSE buffer". THIS IS THE RAW DATA FROM A DETECTOR + /////////////////////////////////////////////////////////////////////////////////////// + + //! get Detector buffer length + int GetDetectorNwords(int n, int finesse_num) OVERRIDE_CPP17; + + //! get Detector buffer of slot A + int* Get1stDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot B + int* Get2ndDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot C + int* Get3rdDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot D + int* Get4thDetectorBuffer(int n) OVERRIDE_CPP17; + /////////////////////////////////////////////////////////////////////////////////////// + + //! get posistion of COPPER block in unit of word + // virtual int GetBufferPos(int n); + + // + // Get information from 13words "COPPER header" attached by COPPER board + // + //! get COPPER counter(not event number) + unsigned int GetCOPPERCounter(int n) OVERRIDE_CPP17; + + //! get # of offset words for FINESSE slot A buffer position + int GetOffset1stFINESSE(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE buffer + int GetFINESSENwords(int n, int finesse) OVERRIDE_CPP17; + + // + // Get information from "B2link(attached by FEE and HLSB) header" + // + //! get b2l block from "FEE b2link header" + // virtual int* GetFTSW2Words(int n); + + //! get b2l block from "FEE b2link header" + virtual int* GetExpRunSubrunBuf(int n) OVERRIDE_CPP17; + + //! get b2l block from "FEE b2link header" + virtual unsigned int GetB2LFEE32bitEventNumber(int n) OVERRIDE_CPP17; + + // + // read magic word to check data + // + //! get magic word of COPPER driver header + unsigned int GetMagicDriverHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA header + unsigned int GetMagicFPGAHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA trailer + unsigned int GetMagicFPGATrailer(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER driver trailer + unsigned int GetMagicDriverTrailer(int n) OVERRIDE_CPP17; + + //! Get checksum in RawTrailer + unsigned int GetTrailerChksum(int n) OVERRIDE_CPP17; + + //! Check if COPPER Magic words are correct + bool CheckCOPPERMagic(int n) OVERRIDE_CPP17; + + //! should be called by DeSerializerCOPPER.cc and fill contents in RawHeader + unsigned int FillTopBlockRawHeader(unsigned int m_node_id, unsigned int prev_eve32, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! read COPPER driver's checksum value + unsigned int GetDriverChkSum(int n) OVERRIDE_CPP17; + + //! calc COPPER driver's checksum value + unsigned int CalcDriverChkSum(int n) OVERRIDE_CPP17; + + //! check data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! check data contents + void CheckUtimeCtimeTRGType(int n) OVERRIDE_CPP17; + + //! check magic words + int CheckB2LHSLBMagicWords(int* finesse_buf, int finesse_nwords); + + //! check magic words + int CheckCRC16(int n, int finesse_num); + + //! Pack data (format ver. = -1 -> Select the latest format version) + int* PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info) OVERRIDE_CPP17; + + // + // size of "COPPER front header" and "COPPER trailer" + // + //! Copper data words = ( total_data_length in COPPER header ) + COPPER_HEADER_TRAILER_NWORDS + + enum { + SIZE_COPPER_DRIVER_HEADER = 0, + SIZE_COPPER_DRIVER_TRAILER = 0 + }; + + // Data Format : "COPPER header" + enum { + SIZE_COPPER_HEADER = 0 + }; + + // Data Format : "COPPER Trailer" + enum { + SIZE_COPPER_TRAILER = 0 + }; + + // Data Format : "B2Link HSLB Header" + enum { + POS_B2LHSLB_MAGIC = 0, + SIZE_B2LHSLB_HEADER = 1 + }; + + + // Data Format : "B2Link FEE Header" + enum { + POS_B2L_CTIME = 0, + SIZE_B2LFEE_HEADER = 1 + }; + + // Data Format : B2Link FEE Trailer + enum { + POS_B2LFEE_CRC16 = 0, + SIZE_B2LFEE_TRAILER = 1 + }; + + // + // Data Format : "B2Link HSLB Trailer" + // + enum { + SIZE_B2LHSLB_TRAILER = 0 + }; + + + + protected : + ///ver.2 Change FEE format as presented at B2GM in Nov.2013 ( Nov.20, 2013) + // ClassDefOverride(PostRawCOPPERFormat_v1, 2); + + }; + + inline int PostRawCOPPERFormat_v1::GetOffset1stFINESSE(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + return pos_nwords; + } + + inline int* PostRawCOPPERFormat_v1::Get1stDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get1stFINESSENwords(n) > 0) { + int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PostRawCOPPERFormat_v1::Get2ndDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get2ndFINESSENwords(n) > 0) { + int pos_nwords = GetOffset2ndFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PostRawCOPPERFormat_v1::Get3rdDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get3rdFINESSENwords(n) > 0) { + int pos_nwords = GetOffset3rdFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PostRawCOPPERFormat_v1::Get4thDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get4thFINESSENwords(n) > 0) { + int pos_nwords = GetOffset4thFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PostRawCOPPERFormat_v1::GetExpRunSubrunBuf(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.POS_EXP_RUN_NO; + return &(m_buffer[ pos_nwords ]); + /* #ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 */ + /* CheckB2LFEEHeaderVersion(n); */ + /* #endif */ + /* int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + POS_EXP_RUN; */ + /* return &(m_buffer[ pos_nwords ]); */ + } + + inline unsigned int PostRawCOPPERFormat_v1::GetDriverChkSum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) + - tmp_trailer.RAWTRAILER_NWORDS - SIZE_COPPER_DRIVER_TRAILER; + return m_buffer[ pos_nwords ]; + } + + + inline unsigned int PostRawCOPPERFormat_v1::GetCOPPERCounter(int n) + { + char err_buf[500]; + sprintf(err_buf, "This data format does not have COPPER counter.(block %d) Exiting...\n %s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + std::string err_str = err_buf; throw (err_str); + return 0; + } + + inline unsigned int PostRawCOPPERFormat_v1::GetMagicDriverHeader(int n) + { + char err_buf[500]; + sprintf(err_buf, "This function is not supported. (block %d) Exiting...\n %s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + std::string err_str = err_buf; throw (err_str); + return 0; + } + + inline unsigned int PostRawCOPPERFormat_v1::GetMagicFPGAHeader(int n) + { + char err_buf[500]; + sprintf(err_buf, "This function is not supported. (block %d) Exiting...\n %s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + std::string err_str = err_buf; throw (err_str); + return 0; + } + + + inline unsigned int PostRawCOPPERFormat_v1::GetMagicFPGATrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 3; + + // printf( "[DEBUG] 1 %d 2 %d 3 %d\n", GetBufferPos(n), GetBlockNwords(n), tmp_trailer.GetTrlNwords()); + + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int PostRawCOPPERFormat_v1::GetMagicDriverTrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 1; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + inline unsigned int PostRawCOPPERFormat_v1::GetTrailerChksum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/PreRawCOPPERFormat_latest.h b/ROPCApplications/rawdata/dataobjects/include/PreRawCOPPERFormat_latest.h new file mode 100644 index 0000000000000000000000000000000000000000..d8b34e905ec0ae250498c8ed7b12f0a898670a57 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/PreRawCOPPERFormat_latest.h @@ -0,0 +1,396 @@ +//+ +// File : PreRawCOPPERFormat_latest.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef PRERAWCOPPERFORMAT_LATEST_H +#define PRERAWCOPPERFORMAT_LATEST_H + +// Includes +//#include <framework/datastore/DataStore.h> +//#include <rawdata/dataobjects/RawDataBlock.h> +#include <rawdata/dataobjects/PostRawCOPPERFormat_latest.h> +#include <rawdata/CRCCalculator.h> + +//#include <TObject.h> +//#define USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + + +namespace Belle2 { + + /** + * The Raw COPPER class ver.1 ( the latest version since May, 2014 ) + * This class stores data received by COPPER via belle2linkt + * Data from all detectors except PXD are stored in this class + */ + class PreRawCOPPERFormat_latest : public RawCOPPERFormat_latest { + public: + //! Default constructor + PreRawCOPPERFormat_latest(); + + //! Constructor using existing pointer to raw data buffer + //PreRawCOPPERFormat_latest(int* bufin, int nwords); + //! Destructor + virtual ~PreRawCOPPERFormat_latest(); + + // + // Get position of or pointer to data + // + /////////////////////////////////////////////////////////////////////////////////////// + // POINTER TO "DETECTOR BUFFER" + //( after removing "B2link headers" from "FINESSE buffer". THIS IS THE RAW DATA FROM A DETECTOR + /////////////////////////////////////////////////////////////////////////////////////// + + + //! get Detector buffer length + int GetDetectorNwords(int n, int finesse_num) OVERRIDE_CPP17; + + //! get Detector buffer of slot A + int* Get1stDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot B + int* Get2ndDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot C + int* Get3rdDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot D + int* Get4thDetectorBuffer(int n) OVERRIDE_CPP17; + /////////////////////////////////////////////////////////////////////////////////////// + + //! get posistion of COPPER block in unit of word + // virtual int GetBufferPos(int n); + + // + // Get information from 13words "COPPER header" attached by COPPER board + // + //! get COPPER counter(not event number) + unsigned int GetCOPPERCounter(int n) OVERRIDE_CPP17; + + //! get # of offset words for FINESSE slot A buffer position + int GetOffset1stFINESSE(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE buffer + int GetFINESSENwords(int n, int finesse) OVERRIDE_CPP17; + + // + // Get information from "B2link(attached by FEE and HLSB) header" + // + //! get b2l block from "FEE b2link header" + // virtual int* GetFTSW2Words(int n); + + //! get b2l block from "FEE b2link header" + virtual int* GetExpRunSubrunBuf(int n) OVERRIDE_CPP17; + + //! get b2l block from "FEE b2link header" + virtual unsigned int GetB2LFEE32bitEventNumber(int n) OVERRIDE_CPP17; + + // + // read magic word to check data + // + //! get magic word of COPPER driver header + unsigned int GetMagicDriverHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA header + unsigned int GetMagicFPGAHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA trailer + unsigned int GetMagicFPGATrailer(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER driver trailer + unsigned int GetMagicDriverTrailer(int n) OVERRIDE_CPP17; + + //! get a checksum on trailer + unsigned int GetTrailerChksum(int n) OVERRIDE_CPP17; + + //! Check if COPPER Magic words are correct + bool CheckCOPPERMagic(int n) OVERRIDE_CPP17; + + //! should be called by DeSerializerCOPPER.cc and fill contents in RawHeader + unsigned int FillTopBlockRawHeader(unsigned int m_node_id, + unsigned int prev_eve32, unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! read COPPER driver's checksum value + unsigned int GetDriverChkSum(int n) OVERRIDE_CPP17; + + //! calc COPPER driver's checksum value + unsigned int CalcDriverChkSum(int n) OVERRIDE_CPP17; + + //! check data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! check data contents + void CheckUtimeCtimeTRGType(int n) OVERRIDE_CPP17; + + //! check magic words of B2link HSLB header/trailer + int CheckB2LHSLBMagicWords(int* finesse_buf, int finesse_nwords); + + //! reduce and merge header/trailer + int CalcReducedDataSize(int* bufin, int nwords, int num_events, int num_nodes); + // int CalcReducedDataSize(RawDataBlock* raw_datablk); + + //! reduce and merge header/trailer + void CopyReducedData(int* bufin, int nwords, int num_events, int num_nodes, int* buf_to, int* nwords_to); + // void CopyReducedData(RawDataBlock* raw_datablk, int* buf_to, int delete_flag_from); + + //! calculate reduced data size + int CalcReducedNwords(int n); + + //! copy data to reduced buffer + int CopyReducedBuffer(int n, int* buf_to); + + //! check CRC16 in B2LFEE trailer + int CheckCRC16(int n, int finesse_num); + + //! Pack data (format ver. = -1 -> Select the latest format version) + int* PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info) OVERRIDE_CPP17; + + //! Copy one datablock to buffer + + + // + // size of "COPPER front header" and "COPPER trailer" + // + //! Copper data words = ( total_data_length in COPPER header ) + COPPER_HEADER_TRAILER_NWORDS + enum { + SIZE_COPPER_DRIVER_HEADER = 7, + SIZE_COPPER_DRIVER_TRAILER = 2 + }; + + // + // Data Format : "COPPER header" + // + enum { + POS_MAGIC_COPPER_1 = 0, + POS_EVE_NUM_COPPER = 1, + POS_SUBSYSTEM_ID = 2, + POS_CRATE_ID = 3, + POS_SLOT_ID = 4, + POS_MAGIC_COPPER_2 = 7, + POS_DATA_LENGTH = 8, + POS_CH_A_DATA_LENGTH = 9, + POS_CH_B_DATA_LENGTH = 10, + POS_CH_C_DATA_LENGTH = 11, + POS_CH_D_DATA_LENGTH = 12, + + SIZE_COPPER_HEADER = 13 + }; + + + + // + // Data Format : "COPPER Trailer" + // + enum { + POS_MAGIC_COPPER_3 = 0, + POS_CHKSUM_COPPER = 1, + POS_MAGIC_COPPER_4 = 2, + + SIZE_COPPER_TRAILER = 3 + }; + + // + // Data Format : "B2Link HSLB Header" + // + enum { + POS_MAGIC_B2LHSLB = 0, + // POS_EVE_CNT_B2LHSLB = 1, + SIZE_B2LHSLB_HEADER = 1 + }; + + // Data Format : "B2Link FEE Header" + // modified by Nov. 21, 2013, Nakao-san's New firmware? + enum { + POS_TT_CTIME_TYPE = 0, + POS_TT_TAG = 1, + POS_TT_UTIME = 2, + POS_EXP_RUN = 3, + POS_B2L_CTIME = 4, + SIZE_B2LFEE_HEADER = 5 + }; + + + // + // Data Format : B2Link FEE Trailer + // + enum { + POS_TT_CTIME_B2LFEE = 0, + POS_CHKSUM_B2LFEE = 1, + SIZE_B2LFEE_TRAILER = 2 + }; + + + // + // Data Format : "B2Link HSLB Trailer" + // + enum { + POS_CHKSUM_B2LHSLB = 0, + SIZE_B2LHSLB_TRAILER = 1 + }; + + + + // + // COPPER magic words + // + enum { + COPPER_MAGIC_DRIVER_HEADER = 0x7FFF0008, + COPPER_MAGIC_FPGA_HEADER = 0xFFFFFAFA, + COPPER_MAGIC_FPGA_TRAILER = 0xFFFFF5F5, + COPPER_MAGIC_DRIVER_TRAILER = 0x7FFF0009 + }; + + + // + // magic words attached by HSLB + // + enum { + B2LHSLB_HEADER_MAGIC = 0xFFAA0000, + B2LHSLB_TRAILER_MAGIC = 0xFF550000 + }; + + //! data fromat after size reduction + PostRawCOPPERFormat_latest m_reduced_rawcpr; //! not record + + protected : + ///ver.2 Change FEE format as presented at B2GM in Nov.2013 ( Nov.20, 2013) + // ClassDefOverride(PreRawCOPPERFormat_latest, 2); + + }; + + + inline int PreRawCOPPERFormat_latest::GetOffset1stFINESSE(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + return pos_nwords; + } + + + + inline int* PreRawCOPPERFormat_latest::Get1stDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get1stFINESSENwords(n) > 0) { + int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PreRawCOPPERFormat_latest::Get2ndDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get2ndFINESSENwords(n) > 0) { + int pos_nwords = GetOffset2ndFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PreRawCOPPERFormat_latest::Get3rdDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get3rdFINESSENwords(n) > 0) { + int pos_nwords = GetOffset3rdFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PreRawCOPPERFormat_latest::Get4thDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get4thFINESSENwords(n) > 0) { + int pos_nwords = GetOffset4thFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PreRawCOPPERFormat_latest::GetExpRunSubrunBuf(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + POS_EXP_RUN; + return &(m_buffer[ pos_nwords ]); + } + + + + inline unsigned int PreRawCOPPERFormat_latest::GetMagicDriverHeader(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_MAGIC_COPPER_1; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int PreRawCOPPERFormat_latest::GetMagicFPGAHeader(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_MAGIC_COPPER_2; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int PreRawCOPPERFormat_latest::GetMagicFPGATrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 3; + + // printf( "[DEBUG] 1 %d 2 %d 3 %d\n", GetBufferPos(n), GetBlockNwords(n), trl.GetTrlNwords()); + + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int PreRawCOPPERFormat_latest::GetMagicDriverTrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 1; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + inline unsigned int PreRawCOPPERFormat_latest::GetDriverChkSum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) + - tmp_trailer.RAWTRAILER_NWORDS - SIZE_COPPER_DRIVER_TRAILER; + return m_buffer[ pos_nwords ]; + } + + + inline unsigned int PreRawCOPPERFormat_latest::GetCOPPERCounter(int n) + { + int pos_nwords = GetBufferPos(n) + POS_EVE_NUM_COPPER + tmp_header.RAWHEADER_NWORDS; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + + + inline unsigned int PreRawCOPPERFormat_latest::GetTrailerChksum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + + + +} +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/PreRawCOPPERFormat_v1.h b/ROPCApplications/rawdata/dataobjects/include/PreRawCOPPERFormat_v1.h new file mode 100644 index 0000000000000000000000000000000000000000..be05a78feb286af32696b0ff011e14782373ec26 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/PreRawCOPPERFormat_v1.h @@ -0,0 +1,393 @@ +//+ +// File : PreRawCOPPERFormat_v1.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef PRERAWCOPPERFORMAT_V1_H +#define PRERAWCOPPERFORMAT_V1_H + +// Includes +//#include <framework/datastore/DataStore.h> +//#include <rawdata/dataobjects/RawDataBlock.h> +#include <rawdata/dataobjects/PostRawCOPPERFormat_v1.h> +#include <rawdata/CRCCalculator.h> + +//#include <TObject.h> +//#define USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + + +namespace Belle2 { + + /** + * The Raw COPPER class ver.1 ( the latest version since May, 2014 ) + * This class stores data received by COPPER via belle2linkt + * Data from all detectors except PXD are stored in this class + */ + class PreRawCOPPERFormat_v1 : public RawCOPPERFormat_v1 { + public: + //! Default constructor + PreRawCOPPERFormat_v1(); + + //! Constructor using existing pointer to raw data buffer + //PreRawCOPPERFormat_v1(int* bufin, int nwords); + //! Destructor + virtual ~PreRawCOPPERFormat_v1(); + + // + // Get position of or pointer to data + // + /////////////////////////////////////////////////////////////////////////////////////// + // POINTER TO "DETECTOR BUFFER" + //( after removing "B2link headers" from "FINESSE buffer". THIS IS THE RAW DATA FROM A DETECTOR + /////////////////////////////////////////////////////////////////////////////////////// + + + //! get Detector buffer length + int GetDetectorNwords(int n, int finesse_num) OVERRIDE_CPP17; + + //! get Detector buffer of slot A + int* Get1stDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot B + int* Get2ndDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot C + int* Get3rdDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot D + int* Get4thDetectorBuffer(int n) OVERRIDE_CPP17; + /////////////////////////////////////////////////////////////////////////////////////// + + //! get posistion of COPPER block in unit of word + // virtual int GetBufferPos(int n); + + // + // Get information from 13words "COPPER header" attached by COPPER board + // + //! get COPPER counter(not event number) + unsigned int GetCOPPERCounter(int n) OVERRIDE_CPP17; + + //! get # of offset words for FINESSE slot A buffer position + int GetOffset1stFINESSE(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE buffer + int GetFINESSENwords(int n, int finesse) OVERRIDE_CPP17; + + // + // Get information from "B2link(attached by FEE and HLSB) header" + // + //! get b2l block from "FEE b2link header" + // virtual int* GetFTSW2Words(int n); + + //! get b2l block from "FEE b2link header" + virtual int* GetExpRunSubrunBuf(int n) OVERRIDE_CPP17; + + //! get b2l block from "FEE b2link header" + virtual unsigned int GetB2LFEE32bitEventNumber(int n) OVERRIDE_CPP17; + + // + // read magic word to check data + // + //! get magic word of COPPER driver header + unsigned int GetMagicDriverHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA header + unsigned int GetMagicFPGAHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA trailer + unsigned int GetMagicFPGATrailer(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER driver trailer + unsigned int GetMagicDriverTrailer(int n) OVERRIDE_CPP17; + + //! get a checksum on trailer + unsigned int GetTrailerChksum(int n) OVERRIDE_CPP17; + + //! Check if COPPER Magic words are correct + bool CheckCOPPERMagic(int n) OVERRIDE_CPP17; + + //! should be called by DeSerializerCOPPER.cc and fill contents in RawHeader + unsigned int FillTopBlockRawHeader(unsigned int m_node_id, + unsigned int prev_eve32, unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! read COPPER driver's checksum value + unsigned int GetDriverChkSum(int n) OVERRIDE_CPP17; + + //! calc COPPER driver's checksum value + unsigned int CalcDriverChkSum(int n) OVERRIDE_CPP17; + + //! check data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! check data contents + void CheckUtimeCtimeTRGType(int n) OVERRIDE_CPP17; + + //! check magic words of B2link HSLB header/trailer + int CheckB2LHSLBMagicWords(int* finesse_buf, int finesse_nwords); + + //! reduce and merge header/trailer + int CalcReducedDataSize(int* bufin, int nwords, int num_events, int num_nodes); + // int CalcReducedDataSize(RawDataBlock* raw_datablk); + + //! reduce and merge header/trailer + void CopyReducedData(int* bufin, int nwords, int num_events, int num_nodes, int* buf_to, int* nwords_to); + // void CopyReducedData(RawDataBlock* raw_datablk, int* buf_to, int delete_flag_from); + + //! calculate reduced data size + int CalcReducedNwords(int n); + + //! copy data to reduced buffer + int CopyReducedBuffer(int n, int* buf_to); + + //! check CRC16 in B2LFEE trailer + int CheckCRC16(int n, int finesse_num); + + //! Pack data (format ver. = -1 -> Select the latest format version) + int* PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info) OVERRIDE_CPP17; + + // + // size of "COPPER front header" and "COPPER trailer" + // + //! Copper data words = ( total_data_length in COPPER header ) + COPPER_HEADER_TRAILER_NWORDS + enum { + SIZE_COPPER_DRIVER_HEADER = 7, + SIZE_COPPER_DRIVER_TRAILER = 2 + }; + + // + // Data Format : "COPPER header" + // + enum { + POS_MAGIC_COPPER_1 = 0, + POS_EVE_NUM_COPPER = 1, + POS_SUBSYSTEM_ID = 2, + POS_CRATE_ID = 3, + POS_SLOT_ID = 4, + POS_MAGIC_COPPER_2 = 7, + POS_DATA_LENGTH = 8, + POS_CH_A_DATA_LENGTH = 9, + POS_CH_B_DATA_LENGTH = 10, + POS_CH_C_DATA_LENGTH = 11, + POS_CH_D_DATA_LENGTH = 12, + + SIZE_COPPER_HEADER = 13 + }; + + + + // + // Data Format : "COPPER Trailer" + // + enum { + POS_MAGIC_COPPER_3 = 0, + POS_CHKSUM_COPPER = 1, + POS_MAGIC_COPPER_4 = 2, + + SIZE_COPPER_TRAILER = 3 + }; + + // + // Data Format : "B2Link HSLB Header" + // + enum { + POS_MAGIC_B2LHSLB = 0, + // POS_EVE_CNT_B2LHSLB = 1, + SIZE_B2LHSLB_HEADER = 1 + }; + + // Data Format : "B2Link FEE Header" + // modified by Nov. 21, 2013, Nakao-san's New firmware? + enum { + POS_TT_CTIME_TYPE = 0, + POS_TT_TAG = 1, + POS_TT_UTIME = 2, + POS_EXP_RUN = 3, + POS_B2L_CTIME = 4, + SIZE_B2LFEE_HEADER = 5 + }; + + + // + // Data Format : B2Link FEE Trailer + // + enum { + POS_TT_CTIME_B2LFEE = 0, + POS_CHKSUM_B2LFEE = 1, + SIZE_B2LFEE_TRAILER = 2 + }; + + + // + // Data Format : "B2Link HSLB Trailer" + // + enum { + POS_CHKSUM_B2LHSLB = 0, + SIZE_B2LHSLB_TRAILER = 1 + }; + + + + // + // COPPER magic words + // + enum { + COPPER_MAGIC_DRIVER_HEADER = 0x7FFF0008, + COPPER_MAGIC_FPGA_HEADER = 0xFFFFFAFA, + COPPER_MAGIC_FPGA_TRAILER = 0xFFFFF5F5, + COPPER_MAGIC_DRIVER_TRAILER = 0x7FFF0009 + }; + + + // + // magic words attached by HSLB + // + enum { + B2LHSLB_HEADER_MAGIC = 0xFFAA0000, + B2LHSLB_TRAILER_MAGIC = 0xFF550000 + }; + + //! data fromat after size reduction + PostRawCOPPERFormat_v1 m_reduced_rawcpr; //! not record + + protected : + ///ver.2 Change FEE format as presented at B2GM in Nov.2013 ( Nov.20, 2013) + // ClassDefOverride(PreRawCOPPERFormat_v1, 2); + + }; + + + inline int PreRawCOPPERFormat_v1::GetOffset1stFINESSE(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + return pos_nwords; + } + + + + inline int* PreRawCOPPERFormat_v1::Get1stDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get1stFINESSENwords(n) > 0) { + int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PreRawCOPPERFormat_v1::Get2ndDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get2ndFINESSENwords(n) > 0) { + int pos_nwords = GetOffset2ndFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PreRawCOPPERFormat_v1::Get3rdDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get3rdFINESSENwords(n) > 0) { + int pos_nwords = GetOffset3rdFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PreRawCOPPERFormat_v1::Get4thDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get4thFINESSENwords(n) > 0) { + int pos_nwords = GetOffset4thFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* PreRawCOPPERFormat_v1::GetExpRunSubrunBuf(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + POS_EXP_RUN; + return &(m_buffer[ pos_nwords ]); + } + + + + inline unsigned int PreRawCOPPERFormat_v1::GetMagicDriverHeader(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_MAGIC_COPPER_1; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int PreRawCOPPERFormat_v1::GetMagicFPGAHeader(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_MAGIC_COPPER_2; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int PreRawCOPPERFormat_v1::GetMagicFPGATrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 3; + + // printf( "[DEBUG] 1 %d 2 %d 3 %d\n", GetBufferPos(n), GetBlockNwords(n), trl.GetTrlNwords()); + + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int PreRawCOPPERFormat_v1::GetMagicDriverTrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 1; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + inline unsigned int PreRawCOPPERFormat_v1::GetDriverChkSum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) + - tmp_trailer.RAWTRAILER_NWORDS - SIZE_COPPER_DRIVER_TRAILER; + return m_buffer[ pos_nwords ]; + } + + + inline unsigned int PreRawCOPPERFormat_v1::GetCOPPERCounter(int n) + { + int pos_nwords = GetBufferPos(n) + POS_EVE_NUM_COPPER + tmp_header.RAWHEADER_NWORDS; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + + + inline unsigned int PreRawCOPPERFormat_v1::GetTrailerChksum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + + + +} +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawARICH.h b/ROPCApplications/rawdata/dataobjects/include/RawARICH.h new file mode 100644 index 0000000000000000000000000000000000000000..20129446ac54e90f19f1da04b91ff73c1553c6e4 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawARICH.h @@ -0,0 +1,39 @@ +//+ +// File : RawCOPPER.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWARICH_H +#define RAWARICH_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <rawdata/dataobjects/RawCOPPER.h> +#include <framework/datastore/DataStore.h> + +#include <TObject.h> + +namespace Belle2 { + + /** + * The Raw ARICH class + * Class for RawCOPPER class data taken by ARICH + * Currently, this class is almost same as RawCOPPER class. + */ + + class RawARICH : public RawCOPPER { + public: + //! Default constructor + RawARICH(); + //! Destructor + virtual ~RawARICH(); + /// To derive from TObject + ClassDef(RawARICH, 1); + }; +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawCDC.h b/ROPCApplications/rawdata/dataobjects/include/RawCDC.h new file mode 100644 index 0000000000000000000000000000000000000000..423bc0bb9919d4369f2a944ccd8e623d5bef2bd6 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawCDC.h @@ -0,0 +1,39 @@ +//+ +// File : RawCOPPER.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWCDC_H +#define RAWCDC_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <rawdata/dataobjects/RawCOPPER.h> +#include <framework/datastore/DataStore.h> + +#include <TObject.h> + +namespace Belle2 { + + /** + * The Raw CDC class + * Class for RawCOPPER class data taken by CDC + * Currently, this class is almost same as RawCOPPER class. + */ + + class RawCDC : public RawCOPPER { + public: + //! Default constructor + RawCDC(); + //! Destructor + virtual ~RawCDC(); + /// To derive from TObject + ClassDef(RawCDC, 1); + }; +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawCOPPER.h b/ROPCApplications/rawdata/dataobjects/include/RawCOPPER.h new file mode 100644 index 0000000000000000000000000000000000000000..efc88e08b1ba380e30e2f96fcdcfed0c8c838e9b --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawCOPPER.h @@ -0,0 +1,743 @@ +//+ +// File : RawCOPPER.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWCOPPER_H +#define RAWCOPPER_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> + +#include <rawdata/dataobjects/RawDataBlock.h> +#include <rawdata/dataobjects/RawDataBlockFormat.h> +#include <rawdata/dataobjects/RawCOPPERFormat.h> +#include <rawdata/dataobjects/RawCOPPERFormat_latest.h> +#include <rawdata/dataobjects/RawCOPPERFormat_v0.h> +#include <rawdata/dataobjects/RawCOPPERFormat_v1.h> +#include <rawdata/dataobjects/PreRawCOPPERFormat_v1.h> +#include <rawdata/dataobjects/PreRawCOPPERFormat_latest.h> +#include <rawdata/RawCOPPERPackerInfo.h> +#include <TObject.h> + +//#define USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + + +// Divide ECL and KLM to barrel and endcap categories from Itoh-san's suggestion +// Updated on May 9, 2014 + +//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +// +// The latest DAQformat version number +#define LATEST_POSTREDUCTION_FORMAT_VER 2 // Since Apr. 21, 2015 +// +//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + +namespace Belle2 { + + /** + * The Raw COPPER class + * This class stores data received by COPPER via belle2linkt + * Data from all detectors except PXD are stored in this class + */ + class RawCOPPER : public RawDataBlock { + public: + //! Default constructor + RawCOPPER(); + + //! Constructor using existing pointer to raw data buffer + //RawCOPPER(int* bufin, int nwords); + //! Destructor + virtual ~RawCOPPER(); + + // + // Get position of or pointer to data + // + + //! set buffer ( delete_flag : m_buffer is freeed( = 0 )/ not freeed( = 1 ) in Destructer ) + void SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes) OVERRIDE_CPP17; + + /////////////////////////////////////////////////////////////////////////////////////// + // POINTER TO "DETECTOR BUFFER" + //( after removing "B2link headers" from "FINESSE buffer". THIS IS THE RAW DATA FROM A DETECTOR + /////////////////////////////////////////////////////////////////////////////////////// + //! get Detector buffer length + int GetDetectorNwords(int n, int finesse_num); + + //! get Detector buffer length of slot A + int Get1stDetectorNwords(int n); + + //! get Detector buffer length of slot B + int Get2ndDetectorNwords(int n); + + //! get Detector buffer length of slot C + int Get3rdDetectorNwords(int n); + + //! get Detector buffer length of slot D + int Get4thDetectorNwords(int n); + + //! get Detector buffer + int* GetDetectorBuffer(int n, int finesse_num); + + //! get Detector buffer of slot A + int* Get1stDetectorBuffer(int n); + + //! get Detector Buffer of slot B + int* Get2ndDetectorBuffer(int n); + + //! get Detector Buffer of slot C + int* Get3rdDetectorBuffer(int n); + + //! get Detector Buffer of slot D + int* Get4thDetectorBuffer(int n); + /////////////////////////////////////////////////////////////////////////////////////// + + //! get posistion of COPPER block in unit of word + virtual int GetBufferPos(int n) OVERRIDE_CPP17; + + //! get buffer pointer of rawcopper header(Currently same as GetBufferPos) + virtual int* GetRawHdrBufPtr(int n); + + //! get buffer pointer of rawcopper trailer + virtual int* GetRawTrlBufPtr(int n); + + //! get FINESSE buffer pointer + int* GetFINESSEBuffer(int n, int finesse_num); + + //! get FINESSE buffer pointer for slot A + int* Get1stFINESSEBuffer(int n); + + //! get FINESSE buffer pointer for slot B + int* Get2ndFINESSEBuffer(int n); + + //! get FINESSE buffer pointer for slot C + int* Get3rdFINESSEBuffer(int n); + + //! get FINESSE buffer pointer for slot D + int* Get4thFINESSEBuffer(int n); + + + // + // Get information from "RawCOPPER header" attached by DAQ software + // + + //! get Experimental # from header + int GetExpNo(int n); //! get Experimental # from header + + unsigned int GetExpRunSubrun(int n); //! Exp# (10bit) run# (14bit) restart # (8bit) + + int GetRunNo(int n); //! get run # (14bit) + + int GetSubRunNo(int n); //! get subrun #(8bit) + + unsigned int GetEveNo(int n); //! get contents of header + + int GetDataType(int n); //! get contents of header + + int GetTruncMask(int n); //! get contents of header + + //! Get Detected Error bitflag + unsigned int GetErrorBitFlag(int n); + + //! Get Detected Error bitflag + void AddErrorBitFlag(int n, unsigned int error_bit_flag); + + + //! check CRC packet Error + int GetPacketCRCError(int n); + + //! check CRC event Error + int GetEventCRCError(int n); + + //! Get Event CRC16 value + int GetEventCRC16Value(int n, int finesse_num); + + //! get node-ID from data + unsigned int GetNodeID(int n); + + // + // Get information from 13words "COPPER header" attached by COPPER board + // + //! get COPPER counter(not event number) + virtual unsigned int GetCOPPERCounter(int n); + + //! get # of FINNESEs which contains data + virtual int GetNumFINESSEBlock(int n); + + //! get # of offset words + int GetOffsetFINESSE(int n, int finesse); + + //! get # of offset words for FINESSE slot A buffer position + int GetOffset1stFINESSE(int n); + + //! get # of offset words for FINESSE slot B buffer position + int GetOffset2ndFINESSE(int n); + + //! get # of offset words for FINESSE slot C buffer position + int GetOffset3rdFINESSE(int n); + + //! get # of offset words for FINESSE slot D buffer position + int GetOffset4thFINESSE(int n); + + //! get data size of FINESSE buffer + int GetFINESSENwords(int n, int finesse); + + //! get data size of FINESSE slot A buffer + int Get1stFINESSENwords(int n); + + //! get data size of FINESSE slot B buffer + int Get2ndFINESSENwords(int n); + + //! get data size of FINESSE slot C buffer + int Get3rdFINESSENwords(int n); + + //! get data size of FINESSE slot D buffer + int Get4thFINESSENwords(int n); + + //! For copying FTSW word1 (FEE header) + unsigned int GetB2LHeaderWord(int n, int finesse_buffer_pos); + + // + // Get information from "B2link(attached by FEE and HLSB) header" + // + //! get b2l block from "FEE b2link header" + // virtual int* GetFTSW2Words(int n); + + //! get b2l block from "FEE b2link header" + virtual int* GetExpRunSubrunBuf(int n); + + //! get b2l block from "FEE b2link header" + virtual unsigned int GetB2LFEE32bitEventNumber(int n); + + // + // read magic word to check data + // + //! get magic word of COPPER driver header + unsigned int GetMagicDriverHeader(int n); + + //! get magic word of COPPER FPGA header + unsigned int GetMagicFPGAHeader(int n); + + //! get magic word of COPPER FPGA trailer + unsigned int GetMagicFPGATrailer(int n); + + //! get magic word of COPPER driver trailer + unsigned int GetMagicDriverTrailer(int n); + + //! Get checksum in RawTrailer + unsigned int GetTrailerChksum(int n); + + //! Check if COPPER Magic words are correct + bool CheckCOPPERMagic(int n); + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + //! Check B2LFEE header version + void CheckB2LFEEHeaderVersion(int n); +#endif + + //! Check if COPPER Magic words are correct + unsigned int GetTTCtimeTRGType(int n); + + //! Check if COPPER Magic words are correct + unsigned int GetTTUtime(int n); + + //! should be called by DeSerializerCOPPER.cc and fill contents in RawHeader + unsigned int FillTopBlockRawHeader(unsigned int m_node_id, + unsigned int prev_eve32, unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no); + + //! read COPPER driver's checksum value + unsigned int GetDriverChkSum(int n); + + //! calc COPPER driver's checksum value + unsigned int CalcDriverChkSum(int n); + + //! calc XOR checksum + unsigned int CalcXORChecksum(int* buf, int nwords); + + //! check data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no); + + //! check data contents + void CheckUtimeCtimeTRGType(int n); + + //! Get ctime + int GetTTCtime(int n); + + //! Get trgtype + int GetTRGType(int n); + + //! Get timeval + void GetTTTimeVal(int n, struct timeval* tv); + + //! read data, detect and set the version number of the data format + void SetVersion(); + + //! Get timeval + void SetVersion(std::string class_name); + + //! Check the version number of data format + void CheckVersionSetBuffer(); + + //! show m_buffer + void ShowBuffer(); + + //! Packer for RawCOPPER class + //! Pack data (format ver. = -1 -> Select the latest format version) + void PackDetectorBuf(int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info); + + //! Pack dummy data (format ver. = -1 -> Select the latest format version) + void PackDetectorBuf4DummyData(int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info); + + + /** Return a short summary of this object's contents in HTML format. */ + std::string getInfoHTML() const; + + enum { + POS_FORMAT_VERSION = 1, + FORMAT_MASK = 0x0000FF00 + }; + + //! class to access + RawCOPPERFormat* m_access; //! do not record + + /// Version of the format + int m_version; //! do not record + + protected : + ///ver.2 Change FEE format as presented at B2GM in Nov.2013 ( Nov.20, 2013) + ///ver.3 Change FEE format as presented at B2GM in Nov.2013 ( May 1, 2014) + ///ver.4 Do not record m_access pointer ( Dec 19, 2014) + ClassDefOverride(RawCOPPER, 3); + + }; + + + inline int* RawCOPPER::GetRawHdrBufPtr(int n) + { + CheckVersionSetBuffer(); + return m_access->GetRawHdrBufPtr(n); + } + + inline int* RawCOPPER::GetRawTrlBufPtr(int n) + { + CheckVersionSetBuffer(); + return m_access->GetRawTrlBufPtr(n); + } + + inline unsigned int RawCOPPER::GetDriverChkSum(int n) + { + CheckVersionSetBuffer(); + return m_access->GetDriverChkSum(n); + } + + inline int RawCOPPER::GetExpNo(int n) + { + CheckVersionSetBuffer(); + return m_access->GetExpNo(n); + } + + inline unsigned int RawCOPPER::GetExpRunSubrun(int n) + { + CheckVersionSetBuffer(); + return m_access->GetExpRunSubrun(n); + } + + inline int RawCOPPER::GetRunNo(int n) + { + CheckVersionSetBuffer(); + return m_access->GetRunNo(n); + } + + + inline int RawCOPPER::GetSubRunNo(int n) + { + CheckVersionSetBuffer(); + return m_access->GetSubRunNo(n); + } + + inline unsigned int RawCOPPER::GetEveNo(int n) + { + CheckVersionSetBuffer(); + return m_access->GetEveNo(n); + } + + + inline unsigned int RawCOPPER::GetNodeID(int n) + { + CheckVersionSetBuffer(); + return m_access->GetNodeID(n); + } + + + inline int RawCOPPER::GetDataType(int n) + { + CheckVersionSetBuffer(); + return m_access->GetDataType(n); + } + + inline int RawCOPPER::GetTruncMask(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTruncMask(n); + } + + inline unsigned int RawCOPPER::GetErrorBitFlag(int n) + { + CheckVersionSetBuffer(); + return m_access->GetErrorBitFlag(n); + } + + inline void RawCOPPER::AddErrorBitFlag(int n, unsigned int error_bit_flag) + { + CheckVersionSetBuffer(); + return m_access->AddErrorBitFlag(n, error_bit_flag); + } + + inline int RawCOPPER::GetPacketCRCError(int n) + { + CheckVersionSetBuffer(); + return m_access->GetPacketCRCError(n); + } + + inline int RawCOPPER::GetEventCRCError(int n) + { + CheckVersionSetBuffer(); + return m_access->GetEventCRCError(n); + } + + inline int RawCOPPER::GetEventCRC16Value(int n, int finesse_num) + { + CheckVersionSetBuffer(); + return m_access->GetEventCRC16Value(n, finesse_num); + } + + inline unsigned int RawCOPPER::GetCOPPERCounter(int n) + { + CheckVersionSetBuffer(); + return m_access->GetCOPPERCounter(n); + } + + inline int RawCOPPER::Get1stDetectorNwords(int n) + { + CheckVersionSetBuffer(); + return m_access->Get1stDetectorNwords(n); + } + + inline int RawCOPPER::Get2ndDetectorNwords(int n) + { + CheckVersionSetBuffer(); + return m_access->Get2ndDetectorNwords(n); + } + + inline int RawCOPPER::Get3rdDetectorNwords(int n) + { + CheckVersionSetBuffer(); + return m_access->Get3rdDetectorNwords(n); + } + + inline int RawCOPPER::Get4thDetectorNwords(int n) + { + CheckVersionSetBuffer(); + return m_access->Get4thDetectorNwords(n); + } + + inline int RawCOPPER::Get1stFINESSENwords(int n) + { + CheckVersionSetBuffer(); + return m_access->Get1stFINESSENwords(n); + } + + inline int RawCOPPER::Get2ndFINESSENwords(int n) + { + CheckVersionSetBuffer(); + return m_access->Get2ndFINESSENwords(n); + } + + inline int RawCOPPER::Get3rdFINESSENwords(int n) + { + CheckVersionSetBuffer(); + return m_access->Get3rdFINESSENwords(n); + } + + inline int RawCOPPER::Get4thFINESSENwords(int n) + { + CheckVersionSetBuffer(); + return m_access->Get4thFINESSENwords(n); + } + + inline int RawCOPPER::GetOffset1stFINESSE(int n) + { + CheckVersionSetBuffer(); + return m_access->GetOffset1stFINESSE(n); + } + + inline int RawCOPPER::GetOffset2ndFINESSE(int n) + { + CheckVersionSetBuffer(); + return m_access->GetOffset2ndFINESSE(n); + } + + inline int RawCOPPER::GetOffset3rdFINESSE(int n) + { + CheckVersionSetBuffer(); + return m_access->GetOffset3rdFINESSE(n); + } + + inline int RawCOPPER::GetOffset4thFINESSE(int n) + { + CheckVersionSetBuffer(); + return m_access->GetOffset4thFINESSE(n); + } + + inline int* RawCOPPER::Get1stFINESSEBuffer(int n) + { + CheckVersionSetBuffer(); + return m_access->Get1stFINESSEBuffer(n); + } + + inline int* RawCOPPER::Get2ndFINESSEBuffer(int n) + { + CheckVersionSetBuffer(); + return m_access->Get2ndFINESSEBuffer(n); + } + + inline int* RawCOPPER::Get3rdFINESSEBuffer(int n) + { + CheckVersionSetBuffer(); + return m_access->Get3rdFINESSEBuffer(n); + } + + inline int* RawCOPPER::Get4thFINESSEBuffer(int n) + { + CheckVersionSetBuffer(); + return m_access->Get4thFINESSEBuffer(n); + } + + inline int* RawCOPPER::Get1stDetectorBuffer(int n) + { + CheckVersionSetBuffer(); + return m_access->Get1stDetectorBuffer(n); + } + + inline int* RawCOPPER::Get2ndDetectorBuffer(int n) + { + CheckVersionSetBuffer(); + return m_access->Get2ndDetectorBuffer(n); + } + + inline int* RawCOPPER::Get3rdDetectorBuffer(int n) + { + CheckVersionSetBuffer(); + return m_access->Get3rdDetectorBuffer(n); + } + + inline int* RawCOPPER::Get4thDetectorBuffer(int n) + { + CheckVersionSetBuffer(); + return m_access->Get4thDetectorBuffer(n); + } + + inline int* RawCOPPER::GetExpRunSubrunBuf(int n) + { + CheckVersionSetBuffer(); + return m_access->GetExpRunSubrunBuf(n); + } + + inline unsigned int RawCOPPER::GetMagicDriverHeader(int n) + { + CheckVersionSetBuffer(); + return m_access->GetMagicDriverHeader(n); + } + + inline unsigned int RawCOPPER::GetMagicFPGAHeader(int n) + { + CheckVersionSetBuffer(); + return m_access->GetMagicFPGAHeader(n); + } + + inline unsigned int RawCOPPER::GetMagicFPGATrailer(int n) + { + CheckVersionSetBuffer(); + return m_access->GetMagicFPGATrailer(n); + } + + inline unsigned int RawCOPPER::GetMagicDriverTrailer(int n) + { + CheckVersionSetBuffer(); + return m_access->GetMagicDriverTrailer(n); + } + + inline unsigned int RawCOPPER::GetTrailerChksum(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTrailerChksum(n); + } + + inline unsigned int RawCOPPER::GetTTCtimeTRGType(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTTCtimeTRGType(n); + } + + inline unsigned int RawCOPPER::GetTTUtime(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTTUtime(n); + } + + inline int RawCOPPER::GetTTCtime(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTTCtime(n); + } + + inline int RawCOPPER::GetTRGType(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTRGType(n); + } + + inline void RawCOPPER::GetTTTimeVal(int n, struct timeval* tv) + { + CheckVersionSetBuffer(); + return m_access->GetTTTimeVal(n, tv); + } + + inline int RawCOPPER::GetBufferPos(int n) + { + CheckVersionSetBuffer(); + return m_access->GetBufferPos(n); + } + + inline unsigned int RawCOPPER::CalcDriverChkSum(int n) + { + CheckVersionSetBuffer(); + return m_access->CalcDriverChkSum(n); + } + + + inline int RawCOPPER::GetNumFINESSEBlock(int n) + { + CheckVersionSetBuffer(); + return m_access->GetNumFINESSEBlock(n); + } + + inline int RawCOPPER::GetDetectorNwords(int n, int finesse_num) + { + CheckVersionSetBuffer(); + return m_access->GetDetectorNwords(n, finesse_num); + } + + inline int RawCOPPER::GetFINESSENwords(int n, int finesse_num) + { + CheckVersionSetBuffer(); + return m_access->GetFINESSENwords(n, finesse_num); + } + + inline int RawCOPPER::GetOffsetFINESSE(int n, int finesse_num) + { + CheckVersionSetBuffer(); + return m_access->GetOffsetFINESSE(n, finesse_num); + } + + inline int* RawCOPPER::GetFINESSEBuffer(int n, int finesse_num) + { + CheckVersionSetBuffer(); + return m_access->GetFINESSEBuffer(n, finesse_num); + } + + inline int* RawCOPPER::GetDetectorBuffer(int n, int finesse_num) + { + CheckVersionSetBuffer(); + return m_access->GetDetectorBuffer(n, finesse_num); + } + + inline unsigned int RawCOPPER::GetB2LFEE32bitEventNumber(int n) + { + CheckVersionSetBuffer(); + return m_access->GetB2LFEE32bitEventNumber(n); + } + + inline unsigned int RawCOPPER::CalcXORChecksum(int* buf, int nwords) + { + CheckVersionSetBuffer(); + return m_access->CalcXORChecksum(buf, nwords); + } + + inline void RawCOPPER::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum_rawcprhdr, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) + { + + CheckVersionSetBuffer(); + m_access->CheckData(n, + prev_evenum, cur_evenum_rawcprhdr, + prev_copper_ctr, cur_copper_ctr, + prev_exprunsubrun_no, cur_exprunsubrun_no); + return; + } + + inline bool RawCOPPER::CheckCOPPERMagic(int n) + { + CheckVersionSetBuffer(); + return m_access->CheckCOPPERMagic(n); + } + + inline void RawCOPPER::CheckUtimeCtimeTRGType(int n) + { + CheckVersionSetBuffer(); + return m_access->CheckUtimeCtimeTRGType(n); + } + + inline unsigned int RawCOPPER::GetB2LHeaderWord(int n, int finesse_buffer_pos) + { + CheckVersionSetBuffer(); + return m_access->GetB2LHeaderWord(n, finesse_buffer_pos); + } + + inline unsigned int RawCOPPER::FillTopBlockRawHeader(unsigned int m_node_id, unsigned int prev_eve32, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) + { + CheckVersionSetBuffer(); + return m_access->FillTopBlockRawHeader(m_node_id, prev_eve32, + prev_exprunsubrun_no, cur_exprunsubrun_no); + } + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + inline void RawCOPPER::CheckB2LFEEHeaderVersion(int n) + { + CheckVersionSetBuffer(); + return m_access->CheckB2LFEEHeaderVersion(n); + } +#endif + + inline void RawCOPPER::CheckVersionSetBuffer() + { + if (((m_buffer[ POS_FORMAT_VERSION ] & FORMAT_MASK) >> 8) != m_version + || m_access == NULL) { + SetVersion(); + } + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + } +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat.h b/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat.h new file mode 100644 index 0000000000000000000000000000000000000000..df69a53043c06a743126630db7119fce564f0fb7 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat.h @@ -0,0 +1,434 @@ +//+ +// File : RawCOPPERFormat.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- +#ifndef RAWCOPPERFORMAT_H +#define RAWCOPPERFORMAT_H + +// Includes +#include <stdio.h> +#include <string> +#include <stdlib.h> +#include <sys/time.h> + +#include <rawdata/RawCOPPERPackerInfo.h> +#include <rawdata/dataobjects/RawDataBlockFormat.h> + +/* #include <framework/datastore/DataStore.h> */ +/* #include <TObject.h> */ + +//#define USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + +#define DETECTOR_MASK 0xFF000000 // tentative +#define COPPERID_MASK 0x00FFFFFF // tentative +#define SVD_ID 0x01000000 // tentative +#define CDC_ID 0x02000000 // tentative +#define TOP_ID 0x03000000 // tentative +#define ARICH_ID 0x04000000 // tentative +#define BECL_ID 0x05000000 // tentative +#define EECL_ID 0x06000000 // tentative +#define BKLM_ID 0x07000000 // tentative +#define EKLM_ID 0x08000000 // tentative +#define TRGDATA_ID 0x10000000 // tentative +#define CDCTRGDATA_ID 0x11000000 // tentative +#define TOPTRGDATA_ID 0x12000000 // tentative +#define ECLTRGDATA_ID 0x13000000 // tentative +#define KLMTRGDATA_ID 0x14000000 // tentative +#define GDLTRGDATA_ID 0x15000000 // tentative + + +namespace Belle2 { + + /** + * The Raw COPPER class + * This class stores data received by COPPER via belle2linkt + * Data from all detectors except PXD are stored in this class + */ + // class RawCOPPERFormat : public TObject { + class RawCOPPERFormat : public RawDataBlockFormat { + public: + /* //! Default constructor */ + RawCOPPERFormat(); + + //! Constructor using existing pointer to raw data buffer + //RawCOPPERFormat(int* bufin, int nwords); + //! Destructor + virtual ~RawCOPPERFormat() {} + + // + // Functions for RawDataBlock + // + //! set buffer ( delete_flag : m_buffer is freeed( = 0 )/ not freeed( = 1 ) in Destructer ) + /* void SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes); */ + + /* //! Get total length of m_buffer */ + /* virtual int TotalBufNwords(); */ + + /* //! get nth buffer pointer */ + /* virtual int* GetBuffer(int n); */ + + /* //! get pointer to buffer(m_buffer) */ + /* virtual int* GetWholeBuffer(); */ + + /* //! get # of data blocks = (# of nodes)*(# of events) */ + /* virtual int GetNumEntries() { return m_num_events * m_num_nodes; } */ + + /* //! get # of data sources(e.g. # of COPPER boards) in m_buffer */ + /* virtual int GetNumNodes() { return m_num_nodes; } */ + + /* //! get # of events in m_buffer */ + /* virtual int GetNumEvents() { return m_num_events; } */ + + /* //! get size of a data block */ + /* virtual int GetBlockNwords(int n); */ + + /* //! print out data */ + /* virtual void PrintData(int* buf, int nwords); */ + + /* enum { */ + /* POS_NWORDS = 0, */ + /* POS_NODE_ID = 6 */ + /* }; */ + + /* enum { */ + /* // Tentatively needed to distinguish new and old FTSW format, which will be changed in Nov. 2013 */ + /* POS_FTSW_ID_OLD = 5, */ + /* TEMP_POS_NWORDS_HEADER = 1, */ + /* OLD_FTSW_NWORDS_HEADER = 6 */ + /* }; */ + + // + // Functions for RawCOPPER + // + + // POINTER TO "DETECTOR BUFFER" + //( after removing "B2link headers" from "FINESSE buffer". THIS IS THE RAW DATA FROM A DETECTOR + + //! get Detector buffer length + virtual int GetDetectorNwords(int n, int finesse_num) = 0; + + //! get Detector buffer length of slot A + virtual int Get1stDetectorNwords(int n) = 0; + + //! get Detector buffer length of slot B + virtual int Get2ndDetectorNwords(int n) = 0; + + //! get Detector buffer length of slot C + virtual int Get3rdDetectorNwords(int n) = 0; + + //! get Detector buffer length of slot D + virtual int Get4thDetectorNwords(int n) = 0; + + //! get Detector buffer + virtual int* GetDetectorBuffer(int n, int finesse_num); + + //! get Detector buffer of slot A + virtual int* Get1stDetectorBuffer(int n) = 0; + + //! get Detector Buffer of slot B + virtual int* Get2ndDetectorBuffer(int n) = 0; + + //! get Detector Buffer of slot C + virtual int* Get3rdDetectorBuffer(int n) = 0; + + //! get Detector Buffer of slot D + virtual int* Get4thDetectorBuffer(int n) = 0; + /////////////////////////////////////////////////////////////////////////////////////// + + /* //! get posistion of COPPER block in unit of word */ + /* virtual int GetBufferPos(int n) = 0; */ + + //! get buffer pointer of rawcopper header(Currently same as GetBufferPos) + virtual int* GetRawHdrBufPtr(int n); + + //! get buffer pointer of rawcopper trailer + virtual int* GetRawTrlBufPtr(int n) = 0; + + //! get FINESSE buffer pointer + virtual int* GetFINESSEBuffer(int n, int finesse_num); + + //! get FINESSE buffer pointer for slot A + virtual int* Get1stFINESSEBuffer(int n); + + //! get FINESSE buffer pointer for slot B + virtual int* Get2ndFINESSEBuffer(int n); + + //! get FINESSE buffer pointer for slot C + virtual int* Get3rdFINESSEBuffer(int n); + + //! get FINESSE buffer pointer for slot D + virtual int* Get4thFINESSEBuffer(int n); + + + + // + // Get information from "RawCOPPERFormat header" attached by DAQ software + // + + /// get Experimental # from header + virtual int GetExpNo(int n) = 0; //! get Experimental # from header + + virtual unsigned int GetExpRunSubrun(int n) = 0; //! Exp# (10bit) run# (14bit) restart # (8bit) + + virtual int GetRunNo(int n) = 0; //! get run # (14bit) + + virtual int GetSubRunNo(int n) = 0; //! get subrun #(8bit) + + virtual unsigned int GetEveNo(int n) = 0; //! get contents of header + + virtual int GetDataType(int n) = 0; //! get contents of header + + virtual int GetTruncMask(int n) = 0; //! get contents of header + + //! Get Detected Error bitflag + virtual unsigned int GetErrorBitFlag(int n) = 0; + + //! Add Detected Error bitflag + virtual void AddErrorBitFlag(int n, unsigned int error_bit_flag) = 0; + + //! check CRC packet Error + virtual int GetPacketCRCError(int n); + + //! check CRC event Error + virtual int GetEventCRCError(int n); + + //! Get Event CRC16 value + virtual int GetEventCRC16Value(int n, int finesse_num); + + //! get node-ID from data + virtual unsigned int GetNodeID(int n) = 0; + + // + // Get information from 13words "COPPER header" attached by COPPER board + // + //! get COPPER counter(not event number) + virtual unsigned int GetCOPPERCounter(int n) = 0; + + //! get # of FINNESEs which contains data + virtual int GetNumFINESSEBlock(int n) = 0; + + //! get # of offset words + virtual int GetOffsetFINESSE(int n, int finesse); + + //! get # of offset words for FINESSE slot A buffer position + virtual int GetOffset1stFINESSE(int n) = 0; + + //! get # of offset words for FINESSE slot B buffer position + virtual int GetOffset2ndFINESSE(int n); + + //! get # of offset words for FINESSE slot C buffer position + virtual int GetOffset3rdFINESSE(int n); + + //! get # of offset words for FINESSE slot D buffer position + virtual int GetOffset4thFINESSE(int n); + + //! get data size of FINESSE buffer + virtual int GetFINESSENwords(int n, int finesse) = 0; + + //! get data size of FINESSE slot A buffer + virtual int Get1stFINESSENwords(int n) = 0; + + //! get data size of FINESSE slot B buffer + virtual int Get2ndFINESSENwords(int n) = 0; + + //! get data size of FINESSE slot C buffer + virtual int Get3rdFINESSENwords(int n) = 0; + + //! get data size of FINESSE slot D buffer + virtual int Get4thFINESSENwords(int n) = 0; + + //! For copying FTSW word1 (FEE header) + virtual unsigned int GetB2LHeaderWord(int n, int finesse_buffer_pos); + + // + // Get information from "B2link(attached by FEE and HLSB) header" + // + //! get b2l block from "FEE b2link header" + // virtual int* GetFTSW2Words(int n) = 0; + + //! get b2l block from "FEE b2link header" + virtual int* GetExpRunSubrunBuf(int n) = 0; + + //! get b2l block from "FEE b2link header" + virtual unsigned int GetB2LFEE32bitEventNumber(int n) = 0; + + // + // read magic word to check data + // + //! get magic word of COPPER driver header + virtual unsigned int GetMagicDriverHeader(int n) = 0; + + //! get magic word of COPPER FPGA header + virtual unsigned int GetMagicFPGAHeader(int n) = 0; + + //! get magic word of COPPER FPGA trailer + virtual unsigned int GetMagicFPGATrailer(int n) = 0; + + //! get magic word of COPPER driver trailer + virtual unsigned int GetMagicDriverTrailer(int n) = 0; + + //! Get checksum in RawTrailer + virtual unsigned int GetTrailerChksum(int n) = 0 ; + + //! Check if COPPER Magic words are correct + virtual bool CheckCOPPERMagic(int n) = 0; + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + //! Check B2LFEE header version + virtual void CheckB2LFEEHeaderVersion(int n) = 0; +#endif + + //! Check if COPPER Magic words are correct + virtual unsigned int GetTTCtimeTRGType(int n) = 0; + + //! Check if COPPER Magic words are correct + virtual unsigned int GetTTUtime(int n) = 0; + + //! should be called by DeSerializerCOPPER.cc and fill contents in RawHeader + virtual unsigned int FillTopBlockRawHeader(unsigned int m_node_id, + unsigned int prev_eve32, unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) = 0; + + //! read COPPER driver's checksum value + virtual unsigned int GetDriverChkSum(int n) = 0; + + //! calc COPPER driver's checksum value + virtual unsigned int CalcDriverChkSum(int n) = 0; + + + //! check data contents + virtual void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) = 0; + + //! check data contents + virtual void CheckUtimeCtimeTRGType(int n) = 0; + + //! Get ctime + virtual int GetTTCtime(int n) = 0; + + //! Get trgtype + virtual int GetTRGType(int n) = 0; + + //! Get timeval + virtual void GetTTTimeVal(int n, struct timeval* tv) = 0; + + //! calc XOR checksum + virtual unsigned int CalcXORChecksum(int* buf, int nwords); + + //! Pack data (format ver. = -1 -> Select the latest format version) + virtual int* PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info) = 0; + + protected : + + /* /// number of words of buffer */ + /* int m_nwords; */ + + /* /// number of nodes in this object */ + /* int m_num_nodes; */ + + /* /// number of events in this object */ + /* int m_num_events; */ + + /* /// Buffer to access data */ + /* int* m_buffer; //! not recorded */ + + ///ver.3 : Separate from RawDataBLock to avoid a memory leak ( July 24, 2014) + // ClassDef(RawCOPPERFormat, 3); + + }; + + + + inline int* RawCOPPERFormat::GetRawHdrBufPtr(int n) + { + int pos_nwords = GetBufferPos(n); + return &(m_buffer[ pos_nwords ]); + } + + + inline int RawCOPPERFormat::GetOffset2ndFINESSE(int n) + { + return GetOffset1stFINESSE(n) + Get1stFINESSENwords(n); + } + + inline int RawCOPPERFormat::GetOffset3rdFINESSE(int n) + { + return GetOffset2ndFINESSE(n) + Get2ndFINESSENwords(n); + } + + inline int RawCOPPERFormat::GetOffset4thFINESSE(int n) + { + return GetOffset3rdFINESSE(n) + Get3rdFINESSENwords(n); + } + + + + inline int* RawCOPPERFormat::Get1stFINESSEBuffer(int n) + { + int pos_nwords = GetOffset1stFINESSE(n); + if (pos_nwords >= m_nwords) { + printf("[DEBUG] Data size(0x%.8x) is smaller than data position info(0x%.8x). Exiting...\n", m_nwords, pos_nwords); + for (int i = 0; i < m_nwords; i++) { + printf("%.8x ", m_buffer[ i ]); + if ((i % 10) == 9) printf("\n"); + } + B2FATAL("Data size is smaller than data position info. Exiting..."); + } + return &(m_buffer[ pos_nwords]); + } + + inline int* RawCOPPERFormat::Get2ndFINESSEBuffer(int n) + { + int pos_nwords = GetOffset2ndFINESSE(n); + if (pos_nwords >= m_nwords) { + printf("[DEBUG] Data size(0x%.8x) is smaller than data position info(0x%.8x). Exiting...\n", m_nwords, pos_nwords); + for (int i = 0; i < m_nwords; i++) { + printf("%.8x ", m_buffer[ i ]); + if ((i % 10) == 9) printf("\n"); + } + B2FATAL("Data size is smaller than data position info. Exiting..."); + } + return &(m_buffer[ pos_nwords]); + } + + inline int* RawCOPPERFormat::Get3rdFINESSEBuffer(int n) + { + int pos_nwords = GetOffset3rdFINESSE(n); + if (pos_nwords >= m_nwords) { + printf("[DEBUG] Data size(0x%.8x) is smaller than data position info(0x%.8x). Exiting...\n", m_nwords, pos_nwords); + for (int i = 0; i < m_nwords; i++) { + printf("%.8x ", m_buffer[ i ]); + if ((i % 10) == 9) printf("\n"); + } + B2FATAL("Data size is smaller than data position info. Exiting..."); + } + return &(m_buffer[ pos_nwords]); + } + + inline int* RawCOPPERFormat::Get4thFINESSEBuffer(int n) + { + int pos_nwords = GetOffset4thFINESSE(n); + if (pos_nwords >= m_nwords) { + printf("[DEBUG] Data size(0x%.8x) is smaller than data position info(0x%.8x). Exiting...\n", m_nwords, pos_nwords); + for (int i = 0; i < m_nwords; i++) { + printf("%.8x ", m_buffer[ i ]); + if ((i % 10) == 9) printf("\n"); + } + B2FATAL("Data size is smaller than data position info. Exiting..."); + } + return &(m_buffer[ pos_nwords]); + } + + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat_latest.h b/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat_latest.h new file mode 100644 index 0000000000000000000000000000000000000000..9668291ff9e86d84f34490fac6b39255074f2cc7 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat_latest.h @@ -0,0 +1,335 @@ +//+ +// File : RawCOPPERFormat_latest.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWCOPPERFORMAT_LATEST_H +#define RAWCOPPERFORMAT_LATEST_H + +// Includes +#include <rawdata/dataobjects/RawCOPPERFormat.h> +#include <rawdata/dataobjects/RawHeader_latest.h> +#include <rawdata/dataobjects/RawTrailer_latest.h> +#include <rawdata/CRCCalculator.h> + +/* #include <framework/datastore/DataStore.h> */ +/* #include <TObject.h> */ + +//#define USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + + +namespace Belle2 { + + /** + * The Raw COPPER class ver.1 ( the latest version since May, 2014 ) + * This class stores data received by COPPER via belle2linkt + * Data from all detectors except PXD are stored in this class + */ + + class RawCOPPERFormat_latest : public RawCOPPERFormat { + public: + //! Default constructor + RawCOPPERFormat_latest(); + + //! Constructor using existing pointer to raw data buffer + //RawCOPPERFormat_latest(int* bufin, int nwords); + //! Destructor + virtual ~RawCOPPERFormat_latest(); + + // + // Get position of or pointer to data + // + /////////////////////////////////////////////////////////////////////////////////////// + // POINTER TO "DETECTOR BUFFER" + //( after removing "B2link headers" from "FINESSE buffer". THIS IS THE RAW DATA FROM A DETECTOR + /////////////////////////////////////////////////////////////////////////////////////// + + + //! get Detector buffer length of slot A + int Get1stDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer length of slot B + int Get2ndDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer length of slot C + int Get3rdDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer length of slot D + int Get4thDetectorNwords(int n) OVERRIDE_CPP17; + + /////////////////////////////////////////////////////////////////////////////////////// + + //! get buffer pointer of rawcopper trailer + int* GetRawTrlBufPtr(int n) OVERRIDE_CPP17; + + // + // Get information from "RawCOPPERFormat_latest header" attached by DAQ software + // + + int GetExpNo(int n) OVERRIDE_CPP17; //! get Experimental # from header + + unsigned int GetExpRunSubrun(int n) OVERRIDE_CPP17; //! Exp# (10bit) run# (14bit) restart # (8bit) + + int GetRunNo(int n) OVERRIDE_CPP17; //! get run # (14bit) + + int GetSubRunNo(int n) OVERRIDE_CPP17; //! get subrun #(8bit) + + unsigned int GetEveNo(int n) OVERRIDE_CPP17; //! get contents of header + + int GetDataType(int n) OVERRIDE_CPP17; //! get contents of header + + int GetTruncMask(int n) OVERRIDE_CPP17; //! get contents of header + + //! Get Detected Error bitflag + unsigned int GetErrorBitFlag(int n) OVERRIDE_CPP17; + + //! Add Detected Error bitflag + void AddErrorBitFlag(int n, unsigned int error_bit_flag) OVERRIDE_CPP17; + + //! check CRC packet Error + int GetPacketCRCError(int n) OVERRIDE_CPP17; + + //! check CRC event Error + int GetEventCRCError(int n) OVERRIDE_CPP17; + + //! get node-ID from data + unsigned int GetNodeID(int n) OVERRIDE_CPP17; + + // + // Get information from 13words "COPPER header" attached by COPPER board + // + + //! get # of FINNESEs which contains data + virtual int GetNumFINESSEBlock(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot A buffer + int Get1stFINESSENwords(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot B buffer + int Get2ndFINESSENwords(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot C buffer + int Get3rdFINESSENwords(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot D buffer + int Get4thFINESSENwords(int n) OVERRIDE_CPP17; + + // + // Get information from "B2link(attached by FEE and HLSB) header" + // + //! get b2l block from "FEE b2link header" + // virtual int* GetFTSW2Words(int n); + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + //! Check B2LFEE header version + void CheckB2LFEEHeaderVersion(int n) OVERRIDE_CPP17; +#endif + + //! Check if COPPER Magic words are correct + unsigned int GetTTCtimeTRGType(int n) OVERRIDE_CPP17; + + //! Check if COPPER Magic words are correct + unsigned int GetTTUtime(int n) OVERRIDE_CPP17; + + /* //! calc COPPER driver's checksum value */ + /* virtual unsigned int CalcDriverChkSum(int n) = 0; */ + + + //! Get ctime + int GetTTCtime(int n) OVERRIDE_CPP17; + + //! Get trgtype + int GetTRGType(int n) OVERRIDE_CPP17; + + //! Get timeval + void GetTTTimeVal(int n, struct timeval* tv) OVERRIDE_CPP17; + + /// Format version number + enum { + DATA_FORMAT_VERSION = 2 + }; + + //! header ( not recorded ) + RawHeader_latest tmp_header; + + //! trailer ( not recorded ) + RawTrailer_latest tmp_trailer; + + + protected : + ///ver.2 Change FEE format as presented at B2GM in Nov.2013 ( Nov.20, 2013) + // ClassDefOverride(RawCOPPERFormat_latest, 2); + }; + + + + + inline int* RawCOPPERFormat_latest::GetRawTrlBufPtr(int n) + { + int pos_nwords; + if (n == (m_num_events * m_num_nodes) - 1) { + pos_nwords = m_nwords - tmp_trailer.GetTrlNwords(); + } else { + pos_nwords = GetBufferPos(n + 1) - tmp_trailer.GetTrlNwords(); + } + return &(m_buffer[ pos_nwords ]); + } + + inline int RawCOPPERFormat_latest::GetExpNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetExpNo(); + } + + + inline unsigned int RawCOPPERFormat_latest::GetExpRunSubrun(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetExpRunSubrun(); + } + + + inline int RawCOPPERFormat_latest::GetRunNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetRunNo(); + } + + + inline int RawCOPPERFormat_latest::GetSubRunNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetSubRunNo(); + } + + inline unsigned int RawCOPPERFormat_latest::GetEveNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetEveNo(); + } + + + inline unsigned int RawCOPPERFormat_latest::GetNodeID(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetNodeID(); + } + + + inline int RawCOPPERFormat_latest::GetDataType(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetDataType(); + } + + inline int RawCOPPERFormat_latest::GetTruncMask(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTruncMask(); + } + + inline unsigned int RawCOPPERFormat_latest::GetErrorBitFlag(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetErrorBitFlag(); + } + + inline void RawCOPPERFormat_latest::AddErrorBitFlag(int n, unsigned int error_bit_flag) + { + tmp_header.SetBuffer(GetBuffer(n)); + tmp_header.AddErrorBitFlag(error_bit_flag); + return; + } + + inline int RawCOPPERFormat_latest::GetPacketCRCError(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetPacketCRCError(); + } + + inline int RawCOPPERFormat_latest::GetEventCRCError(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetEventCRCError(); + } + + + inline int RawCOPPERFormat_latest::Get1stDetectorNwords(int n) + { + return GetDetectorNwords(n, 0); + } + + inline int RawCOPPERFormat_latest::Get2ndDetectorNwords(int n) + { + return GetDetectorNwords(n, 1); + } + + inline int RawCOPPERFormat_latest::Get3rdDetectorNwords(int n) + { + return GetDetectorNwords(n, 2); + } + + inline int RawCOPPERFormat_latest::Get4thDetectorNwords(int n) + { + return GetDetectorNwords(n, 3); + } + + + + inline int RawCOPPERFormat_latest::Get1stFINESSENwords(int n) + { + return GetFINESSENwords(n, 0); + } + + inline int RawCOPPERFormat_latest::Get2ndFINESSENwords(int n) + { + return GetFINESSENwords(n, 1); + } + + inline int RawCOPPERFormat_latest::Get3rdFINESSENwords(int n) + { + return GetFINESSENwords(n, 2); + } + + inline int RawCOPPERFormat_latest::Get4thFINESSENwords(int n) + { + return GetFINESSENwords(n, 3); + } + + + + inline unsigned int RawCOPPERFormat_latest::GetTTCtimeTRGType(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTTCtimeTRGType(); + } + + inline unsigned int RawCOPPERFormat_latest::GetTTUtime(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTTUtime(); + } + + inline int RawCOPPERFormat_latest::GetTTCtime(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTTCtime(); + } + + inline int RawCOPPERFormat_latest::GetTRGType(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTRGType(); + } + + inline void RawCOPPERFormat_latest::GetTTTimeVal(int n, struct timeval* tv) + { + tmp_header.SetBuffer(GetBuffer(n)); + tmp_header.GetTTTimeVal(tv); + return ; + } +} +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat_v0.h b/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat_v0.h new file mode 100644 index 0000000000000000000000000000000000000000..ebf95e33635f79765f6a57ac89f45aa6964fc656 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat_v0.h @@ -0,0 +1,635 @@ +//+ +// File : RawCOPPERFormat_v0.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWCOPPERFORMAT_V0_H +#define RAWCOPPERFORMAT_V0_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <sys/time.h> + +#include <rawdata/dataobjects/RawCOPPERFormat.h> +#include <rawdata/dataobjects/RawHeader_v0.h> +#include <rawdata/dataobjects/RawTrailer_v0.h> + +/* #include <framework/datastore/DataStore.h> */ +/* #include <TObject.h> */ + +//#define USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + +namespace Belle2 { + + /** + * The Raw COPPER class ver.0 ( from August, 2013 to April, 2014 ) + * This class stores data received by COPPER via belle2linkt + * Data from all detectors except PXD are stored in this class + */ + class RawCOPPERFormat_v0 : public RawCOPPERFormat { + public: + //! Default constructor + RawCOPPERFormat_v0(); + + //! Constructor using existing pointer to raw data buffer + //RawCOPPERFormat_v0(int* bufin, int nwords); + //! Destructor + virtual ~RawCOPPERFormat_v0(); + + // + // Get position of or pointer to data + // + /////////////////////////////////////////////////////////////////////////////////////// + // POINTER TO "DETECTOR BUFFER" + //( after removing "B2link headers" from "FINESSE buffer". THIS IS THE RAW DATA FROM A DETECTOR + /////////////////////////////////////////////////////////////////////////////////////// + + + //! get Detector buffer length + int GetDetectorNwords(int n, int finesse_num) OVERRIDE_CPP17; + + //! get Detector buffer length of slot A + int Get1stDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer length of slot B + int Get2ndDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer length of slot C + int Get3rdDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer length of slot D + int Get4thDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer of slot A + int* Get1stDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot B + int* Get2ndDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot C + int* Get3rdDetectorBuffer(int n) OVERRIDE_CPP17; + + //! get Detector Buffer of slot D + int* Get4thDetectorBuffer(int n) OVERRIDE_CPP17; + /////////////////////////////////////////////////////////////////////////////////////// + + /* //! get posistion of COPPER block in unit of word */ + /* virtual int GetBufferPos(int n); */ + + //! get buffer pointer of rawcopper trailer + virtual int* GetRawTrlBufPtr(int n) OVERRIDE_CPP17; + + // + // Get information from "RawCOPPERFormat_v0 header" attached by DAQ software + // + + int GetExpNo(int n) OVERRIDE_CPP17; //! get Experimental # from header + + unsigned int GetExpRunSubrun(int n) OVERRIDE_CPP17; //! Exp# (10bit) run# (14bit) restart # (8bit) + + int GetRunNo(int n) OVERRIDE_CPP17; //! get run # (14bit) + + int GetSubRunNo(int n) OVERRIDE_CPP17; //! get subrun #(8bit) + + unsigned int GetEveNo(int n) OVERRIDE_CPP17; //! get contents of header + + int GetDataType(int n) OVERRIDE_CPP17; //! get contents of header + + int GetTruncMask(int n) OVERRIDE_CPP17; //! get contents of header + + //! Get Detected Error bitflag + unsigned int GetErrorBitFlag(int n) OVERRIDE_CPP17; + + //! Add Detected Error bitflag + void AddErrorBitFlag(int n, unsigned int error_bit_flag) OVERRIDE_CPP17; + + //! get node-ID from data + unsigned int GetNodeID(int n) OVERRIDE_CPP17; + + // + // Get information from 13words "COPPER header" attached by COPPER board + // + //! get COPPER counter(not event number) + virtual unsigned int GetCOPPERCounter(int n) OVERRIDE_CPP17; + + //! get # of FINNESEs which contains data + virtual int GetNumFINESSEBlock(int n) OVERRIDE_CPP17; + + //! get # of offset words for FINESSE slot A buffer position + int GetOffset1stFINESSE(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE buffer + int GetFINESSENwords(int n, int finesse) OVERRIDE_CPP17; + + //! get data size of FINESSE slot A buffer + int Get1stFINESSENwords(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot B buffer + int Get2ndFINESSENwords(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot C buffer + int Get3rdFINESSENwords(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot D buffer + int Get4thFINESSENwords(int n) OVERRIDE_CPP17; + + // + // Get information from "B2link(attached by FEE and HLSB) header" + // + //! get b2l block from "FEE b2link header" + // virtual int* GetFTSW2Words(int n); + + //! get b2l block from "FEE b2link header" + virtual int* GetExpRunSubrunBuf(int n) OVERRIDE_CPP17; + + //! get b2l block from "FEE b2link header" + virtual unsigned int GetB2LFEE32bitEventNumber(int n) OVERRIDE_CPP17; + + // + // read magic word to check data + // + //! get magic word of COPPER driver header + unsigned int GetMagicDriverHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA header + unsigned int GetMagicFPGAHeader(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER FPGA trailer + unsigned int GetMagicFPGATrailer(int n) OVERRIDE_CPP17; + + //! get magic word of COPPER driver trailer + unsigned int GetMagicDriverTrailer(int n) OVERRIDE_CPP17; + + //! Get checksum in RawTrailer + unsigned int GetTrailerChksum(int n) OVERRIDE_CPP17; + + //! Check if COPPER Magic words are correct + bool CheckCOPPERMagic(int n) OVERRIDE_CPP17; + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + //! Check B2LFEE header version + void CheckB2LFEEHeaderVersion(int n) OVERRIDE_CPP17; +#endif + + //! Check if COPPER Magic words are correct + unsigned int GetTTCtimeTRGType(int n) OVERRIDE_CPP17; + + //! Check if COPPER Magic words are correct + unsigned int GetTTUtime(int n) OVERRIDE_CPP17; + + //! should be called by DeSerializerCOPPER.cc and fill contents in RawHeader + unsigned int FillTopBlockRawHeader(unsigned int m_node_id, unsigned int prev_eve32, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! read COPPER driver's checksum value + unsigned int GetDriverChkSum(int n) OVERRIDE_CPP17; + + //! calc COPPER driver's checksum value + unsigned int CalcDriverChkSum(int n) OVERRIDE_CPP17; + + //! check data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! check data contents + void CheckUtimeCtimeTRGType(int n) OVERRIDE_CPP17; + + //! Get ctime + int GetTTCtime(int n) OVERRIDE_CPP17; + + //! Get trgtype + int GetTRGType(int n) OVERRIDE_CPP17; + + //! Get timeval + void GetTTTimeVal(int n, struct timeval* tv) OVERRIDE_CPP17; + + //! Pack data (format ver. = -1 -> Select the latest format version) + int* PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info) OVERRIDE_CPP17; + + enum { + DATA_FORMAT_VERSION = 0 + }; + + // + // size of "COPPER front header" and "COPPER trailer" + // + //! Copper data words = ( total_data_length in COPPER header ) + COPPER_HEADER_TRAILER_NWORDS + enum { + SIZE_COPPER_DRIVER_HEADER = 7, + SIZE_COPPER_DRIVER_TRAILER = 2 + }; + + // + // Data Format : "COPPER header" + // + enum { + POS_MAGIC_COPPER_1 = 0, + POS_EVE_NUM_COPPER = 1, + POS_SUBSYSTEM_ID = 2, + POS_CRATE_ID = 3, + POS_SLOT_ID = 4, + POS_MAGIC_COPPER_2 = 7, + POS_DATA_LENGTH = 8, + POS_CH_A_DATA_LENGTH = 9, + POS_CH_B_DATA_LENGTH = 10, + POS_CH_C_DATA_LENGTH = 11, + POS_CH_D_DATA_LENGTH = 12, + + SIZE_COPPER_HEADER = 13 + }; + + + + // + // Data Format : "COPPER Trailer" + // + enum { + POS_MAGIC_COPPER_3 = 0, + POS_CHKSUM_COPPER = 1, + POS_MAGIC_COPPER_4 = 2, + + SIZE_COPPER_TRAILER = 3 + }; + + // + // Data Format : "B2Link HSLB Header" + // + enum { + POS_MAGIC_B2LHSLB = 0, + // POS_EVE_CNT_B2LHSLB = 1, + SIZE_B2LHSLB_HEADER = 1 + }; + + // + // Data Format : "B2Link HSLB Trailer" + // + enum { + POS_CHKSUM_B2LHSLB = 0, + SIZE_B2LHSLB_TRAILER = 1 + }; + + + // Data Format : "B2Link FEE Header" + // modified by Nov. 21, 2013, Nakao-san's New firmware? + enum { + POS_TT_CTIME_TYPE = 0, + POS_TT_TAG = 1, + POS_TT_UTIME = 2, + POS_EXP_RUN = 3, + POS_B2L_CTIME = 4, + SIZE_B2LFEE_HEADER = 5 + }; + + + // + // Data Format : B2Link FEE Trailer + // + enum { + POS_CHKSUM_B2LFEE = 0, + SIZE_B2LFEE_TRAILER = 1 + }; + + // + // COPPER magic words + // + enum { + COPPER_MAGIC_DRIVER_HEADER = 0x7FFF0008, + COPPER_MAGIC_FPGA_HEADER = 0xFFFFFAFA, + COPPER_MAGIC_FPGA_TRAILER = 0xFFFFF5F5, + COPPER_MAGIC_DRIVER_TRAILER = 0x7FFF0009 + }; + + //! header ( not recorded ) + RawHeader_v0 tmp_header; + + //! trailer ( not recorded ) + RawTrailer_v0 tmp_trailer; + + protected : + ///ver.2 Change FEE format as presented at B2GM in Nov.2013 ( Nov.20, 2013) + // ClassDefOverride(RawCOPPERFormat_v0, 2); + + }; + + + + + inline int* RawCOPPERFormat_v0::GetRawTrlBufPtr(int n) + { + int pos_nwords; + if (n == (m_num_events * m_num_nodes) - 1) { + pos_nwords = m_nwords - tmp_trailer.GetTrlNwords(); + } else { + pos_nwords = GetBufferPos(n + 1) - tmp_trailer.GetTrlNwords(); + } + return &(m_buffer[ pos_nwords ]); + } + + inline unsigned int RawCOPPERFormat_v0::GetDriverChkSum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) + - tmp_trailer.RAWTRAILER_NWORDS - SIZE_COPPER_DRIVER_TRAILER; + return m_buffer[ pos_nwords ]; + } + + inline int RawCOPPERFormat_v0::GetExpNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetExpNo(); + } + + inline unsigned int RawCOPPERFormat_v0::GetExpRunSubrun(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetExpRunSubrun(); + } + + inline int RawCOPPERFormat_v0::GetRunNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetRunNo(); + } + + + inline int RawCOPPERFormat_v0::GetSubRunNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetSubRunNo(); + } + + inline unsigned int RawCOPPERFormat_v0::GetEveNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetEveNo(); + } + + + inline unsigned int RawCOPPERFormat_v0::GetNodeID(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetNodeID(); + } + + + inline int RawCOPPERFormat_v0::GetDataType(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetDataType(); + } + + inline int RawCOPPERFormat_v0::GetTruncMask(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTruncMask(); + } + + inline unsigned int RawCOPPERFormat_v0::GetErrorBitFlag(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetErrorBitFlag(); + } + + inline void RawCOPPERFormat_v0::AddErrorBitFlag(int n, unsigned int error_bit_flag) + { + tmp_header.SetBuffer(GetBuffer(n)); + tmp_header.AddErrorBitFlag(error_bit_flag); + return; + } + + inline unsigned int RawCOPPERFormat_v0::GetCOPPERCounter(int n) + { + int pos_nwords = GetBufferPos(n) + POS_EVE_NUM_COPPER + tmp_header.RAWHEADER_NWORDS; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + + + inline int RawCOPPERFormat_v0::Get1stDetectorNwords(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int nwords = 0; + if (Get1stFINESSENwords(n) > 0) { + nwords = Get1stFINESSENwords(n) - SIZE_B2LHSLB_HEADER - SIZE_B2LFEE_HEADER + - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER; + } + return nwords; + } + + inline int RawCOPPERFormat_v0::Get2ndDetectorNwords(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int nwords = 0; + if (Get2ndFINESSENwords(n) > 0) { + nwords = Get2ndFINESSENwords(n) - SIZE_B2LHSLB_HEADER - + SIZE_B2LFEE_HEADER - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER; + } + return nwords; + } + + inline int RawCOPPERFormat_v0::Get3rdDetectorNwords(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int nwords = 0; + if (Get3rdFINESSENwords(n) > 0) { + nwords = Get3rdFINESSENwords(n) - SIZE_B2LHSLB_HEADER - + SIZE_B2LFEE_HEADER - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER; + } + return nwords; + } + + inline int RawCOPPERFormat_v0::Get4thDetectorNwords(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int nwords = 0; + if (Get4thFINESSENwords(n) > 0) { + nwords = Get4thFINESSENwords(n) - SIZE_B2LHSLB_HEADER - + SIZE_B2LFEE_HEADER - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER; + } + return nwords; + } + + + + inline int RawCOPPERFormat_v0::Get1stFINESSENwords(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_A_DATA_LENGTH; + return m_buffer[ pos_nwords ]; + } + + inline int RawCOPPERFormat_v0::Get2ndFINESSENwords(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_B_DATA_LENGTH; + return m_buffer[ pos_nwords ]; + } + + inline int RawCOPPERFormat_v0::Get3rdFINESSENwords(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_C_DATA_LENGTH; + return m_buffer[ pos_nwords ]; + } + + inline int RawCOPPERFormat_v0::Get4thFINESSENwords(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_D_DATA_LENGTH; + return m_buffer[ pos_nwords ]; + } + + + inline int RawCOPPERFormat_v0::GetOffset1stFINESSE(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + return pos_nwords; + } + + + inline int* RawCOPPERFormat_v0::Get1stDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get1stFINESSENwords(n) > 0) { + int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* RawCOPPERFormat_v0::Get2ndDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get2ndFINESSENwords(n) > 0) { + int pos_nwords = GetOffset2ndFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* RawCOPPERFormat_v0::Get3rdDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get3rdFINESSENwords(n) > 0) { + int pos_nwords = GetOffset3rdFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* RawCOPPERFormat_v0::Get4thDetectorBuffer(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + if (Get4thFINESSENwords(n) > 0) { + int pos_nwords = GetOffset4thFINESSE(n) + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER; + return &(m_buffer[ pos_nwords ]); + } + return NULL; + } + + inline int* RawCOPPERFormat_v0::GetExpRunSubrunBuf(int n) + { +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int pos_nwords = GetOffset1stFINESSE(n) + SIZE_B2LHSLB_HEADER + POS_EXP_RUN; + return &(m_buffer[ pos_nwords ]); + } + + + + inline unsigned int RawCOPPERFormat_v0::GetMagicDriverHeader(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_MAGIC_COPPER_1; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int RawCOPPERFormat_v0::GetMagicFPGAHeader(int n) + { + int pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_MAGIC_COPPER_2; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int RawCOPPERFormat_v0::GetMagicFPGATrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 3; + + // printf( "[DEBUG] 1 %d 2 %d 3 %d\n", GetBufferPos(n), GetBlockNwords(n), tmp_trailer.GetTrlNwords()); + + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + inline unsigned int RawCOPPERFormat_v0::GetMagicDriverTrailer(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() - 1; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + inline unsigned int RawCOPPERFormat_v0::GetTrailerChksum(int n) + { + int pos_nwords = GetBufferPos(n) + GetBlockNwords(n) - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM; + return (unsigned int)(m_buffer[ pos_nwords ]); + } + + + inline unsigned int RawCOPPERFormat_v0::GetTTCtimeTRGType(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTTCtimeTRGType(); + } + + inline unsigned int RawCOPPERFormat_v0::GetTTUtime(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTTUtime(); + } + + + inline int RawCOPPERFormat_v0::GetTTCtime(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTTCtime(); + } + + inline int RawCOPPERFormat_v0::GetTRGType(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTRGType(); + } + + inline void RawCOPPERFormat_v0::GetTTTimeVal(int n, struct timeval* tv) + { + tmp_header.SetBuffer(GetBuffer(n)); + tmp_header.GetTTTimeVal(tv); + return ; + } + + + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat_v1.h b/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat_v1.h new file mode 100644 index 0000000000000000000000000000000000000000..6f57f44105a8e5bc7bd030aa2061344eb0b14d20 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawCOPPERFormat_v1.h @@ -0,0 +1,313 @@ +//+ +// File : RawCOPPERFormat_v1.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWCOPPERFORMAT_V1_H +#define RAWCOPPERFORMAT_V1_H + +// Includes +#include <rawdata/dataobjects/RawCOPPERFormat.h> +#include <rawdata/dataobjects/RawHeader_v1.h> +#include <rawdata/dataobjects/RawTrailer_v1.h> +#include <rawdata/CRCCalculator.h> + +/* #include <framework/datastore/DataStore.h> */ +/* #include <TObject.h> */ + +//#define USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + + +namespace Belle2 { + + /** + * The Raw COPPER class ver.1 ( the latest version since May, 2014 ) + * This class stores data received by COPPER via belle2linkt + * Data from all detectors except PXD are stored in this class + */ + class RawCOPPERFormat_v1 : public RawCOPPERFormat { + public: + //! Default constructor + RawCOPPERFormat_v1(); + + //! Constructor using existing pointer to raw data buffer + //RawCOPPERFormat_v1(int* bufin, int nwords); + //! Destructor + virtual ~RawCOPPERFormat_v1(); + + // + // Get position of or pointer to data + // + /////////////////////////////////////////////////////////////////////////////////////// + // POINTER TO "DETECTOR BUFFER" + //( after removing "B2link headers" from "FINESSE buffer". THIS IS THE RAW DATA FROM A DETECTOR + /////////////////////////////////////////////////////////////////////////////////////// + + + //! get Detector buffer length of slot A + int Get1stDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer length of slot B + int Get2ndDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer length of slot C + int Get3rdDetectorNwords(int n) OVERRIDE_CPP17; + + //! get Detector buffer length of slot D + int Get4thDetectorNwords(int n) OVERRIDE_CPP17; + + /////////////////////////////////////////////////////////////////////////////////////// + + //! get buffer pointer of rawcopper trailer + int* GetRawTrlBufPtr(int n) OVERRIDE_CPP17; + + // + // Get information from "RawCOPPERFormat_v1 header" attached by DAQ software + // + + int GetExpNo(int n) OVERRIDE_CPP17; //! get Experimental # from header + + unsigned int GetExpRunSubrun(int n) OVERRIDE_CPP17; //! Exp# (10bit) run# (14bit) restart # (8bit) + + int GetRunNo(int n) OVERRIDE_CPP17; //! get run # (14bit) + + int GetSubRunNo(int n) OVERRIDE_CPP17; //! get subrun #(8bit) + + unsigned int GetEveNo(int n) OVERRIDE_CPP17; //! get contents of header + + int GetDataType(int n) OVERRIDE_CPP17; //! get contents of header + + int GetTruncMask(int n) OVERRIDE_CPP17; //! get contents of header + + //! Get Detected Error bitflag + unsigned int GetErrorBitFlag(int n) OVERRIDE_CPP17; + + //! Add Detected Error bitflag + void AddErrorBitFlag(int n, unsigned int error_bit_flag) OVERRIDE_CPP17; + + //! get node-ID from data + unsigned int GetNodeID(int n) OVERRIDE_CPP17; + + // + // Get information from 13words "COPPER header" attached by COPPER board + // + + //! get # of FINNESEs which contains data + virtual int GetNumFINESSEBlock(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot A buffer + int Get1stFINESSENwords(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot B buffer + int Get2ndFINESSENwords(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot C buffer + int Get3rdFINESSENwords(int n) OVERRIDE_CPP17; + + //! get data size of FINESSE slot D buffer + int Get4thFINESSENwords(int n) OVERRIDE_CPP17; + + // + // Get information from "B2link(attached by FEE and HLSB) header" + // + //! get b2l block from "FEE b2link header" + // virtual int* GetFTSW2Words(int n); + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + //! Check B2LFEE header version + void CheckB2LFEEHeaderVersion(int n) OVERRIDE_CPP17; +#endif + + //! Check if COPPER Magic words are correct + unsigned int GetTTCtimeTRGType(int n) OVERRIDE_CPP17; + + //! Check if COPPER Magic words are correct + unsigned int GetTTUtime(int n) OVERRIDE_CPP17; + + /* //! calc COPPER driver's checksum value */ + /* virtual unsigned int CalcDriverChkSum(int n) = 0; */ + + + //! Get ctime + int GetTTCtime(int n) OVERRIDE_CPP17; + + //! Get trgtype + int GetTRGType(int n) OVERRIDE_CPP17; + + //! Get timeval + void GetTTTimeVal(int n, struct timeval* tv) OVERRIDE_CPP17; + + /// Format version number + enum { + DATA_FORMAT_VERSION = 1 + }; + + //! header ( not recorded ) + RawHeader_v1 tmp_header; + + //! trailer ( not recorded ) + RawTrailer_v1 tmp_trailer; + + + protected : + ///ver.2 Change FEE format as presented at B2GM in Nov.2013 ( Nov.20, 2013) + // ClassDefOverride(RawCOPPERFormat_v1, 2); + }; + + + + + inline int* RawCOPPERFormat_v1::GetRawTrlBufPtr(int n) + { + int pos_nwords; + if (n == (m_num_events * m_num_nodes) - 1) { + pos_nwords = m_nwords - tmp_trailer.GetTrlNwords(); + } else { + pos_nwords = GetBufferPos(n + 1) - tmp_trailer.GetTrlNwords(); + } + return &(m_buffer[ pos_nwords ]); + } + + inline int RawCOPPERFormat_v1::GetExpNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetExpNo(); + } + + inline unsigned int RawCOPPERFormat_v1::GetExpRunSubrun(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetExpRunSubrun(); + } + + inline int RawCOPPERFormat_v1::GetRunNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetRunNo(); + } + + + inline int RawCOPPERFormat_v1::GetSubRunNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetSubRunNo(); + } + + inline unsigned int RawCOPPERFormat_v1::GetEveNo(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetEveNo(); + } + + + inline unsigned int RawCOPPERFormat_v1::GetNodeID(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetNodeID(); + } + + + inline int RawCOPPERFormat_v1::GetDataType(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetDataType(); + } + + inline int RawCOPPERFormat_v1::GetTruncMask(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTruncMask(); + } + + inline unsigned int RawCOPPERFormat_v1::GetErrorBitFlag(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetErrorBitFlag(); + } + + inline void RawCOPPERFormat_v1::AddErrorBitFlag(int n, unsigned int error_bit_flag) + { + tmp_header.SetBuffer(GetBuffer(n)); + tmp_header.AddErrorBitFlag(error_bit_flag); + return; + } + + inline int RawCOPPERFormat_v1::Get1stDetectorNwords(int n) + { + return GetDetectorNwords(n, 0); + } + + inline int RawCOPPERFormat_v1::Get2ndDetectorNwords(int n) + { + return GetDetectorNwords(n, 1); + } + + inline int RawCOPPERFormat_v1::Get3rdDetectorNwords(int n) + { + return GetDetectorNwords(n, 2); + } + + inline int RawCOPPERFormat_v1::Get4thDetectorNwords(int n) + { + return GetDetectorNwords(n, 3); + } + + + + inline int RawCOPPERFormat_v1::Get1stFINESSENwords(int n) + { + return GetFINESSENwords(n, 0); + } + + inline int RawCOPPERFormat_v1::Get2ndFINESSENwords(int n) + { + return GetFINESSENwords(n, 1); + } + + inline int RawCOPPERFormat_v1::Get3rdFINESSENwords(int n) + { + return GetFINESSENwords(n, 2); + } + + inline int RawCOPPERFormat_v1::Get4thFINESSENwords(int n) + { + return GetFINESSENwords(n, 3); + } + + + + inline unsigned int RawCOPPERFormat_v1::GetTTCtimeTRGType(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTTCtimeTRGType(); + } + + inline unsigned int RawCOPPERFormat_v1::GetTTUtime(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTTUtime(); + } + + inline int RawCOPPERFormat_v1::GetTTCtime(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTTCtime(); + } + + inline int RawCOPPERFormat_v1::GetTRGType(int n) + { + tmp_header.SetBuffer(GetBuffer(n)); + return tmp_header.GetTRGType(); + } + + inline void RawCOPPERFormat_v1::GetTTTimeVal(int n, struct timeval* tv) + { + tmp_header.SetBuffer(GetBuffer(n)); + tmp_header.GetTTTimeVal(tv); + return ; + } +} +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawDataBlock.h b/ROPCApplications/rawdata/dataobjects/include/RawDataBlock.h new file mode 100644 index 0000000000000000000000000000000000000000..c73bea9609730ac5ed268cf8f2067c0cae9069f9 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawDataBlock.h @@ -0,0 +1,165 @@ +//+ +// File : RawDataBlock.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWDATABLOCK_H +#define RAWDATABLOCK_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <framework/datastore/DataStore.h> +#include <TObject.h> +#include <rawdata/dataobjects/RawDataBlockFormat.h> + + +namespace Belle2 { + + /** + * The RawDataBlock class + * Base class for rawdata handling + */ + class RawDataBlock : public TObject { + public: + //! Default constructor + RawDataBlock(); + + //! Destructor + virtual ~RawDataBlock(); + + //! set buffer ( delete_flag : m_buffer is freeed( = 0 )/ not freeed( = 1 ) in Destructer ) + virtual void SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes); + + //! Get total length of m_buffer + virtual int TotalBufNwords() + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->TotalBufNwords(); + } + + //! get position of data block in word + virtual int GetBufferPos(int n) + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->GetBufferPos(n); + } + + //! get nth buffer pointer + virtual int* GetBuffer(int n) + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->GetBuffer(n); + } + + //! get pointer to buffer(m_buffer) + virtual int* GetWholeBuffer() + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->GetWholeBuffer(); + } + + //! get # of data blocks = (# of nodes)*(# of events) + virtual int GetNumEntries() + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->GetNumEntries(); + } + + //! get # of data sources(e.g. # of COPPER boards) in m_buffer + virtual int GetNumNodes() + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->GetNumNodes(); + } + + //! get # of events in m_buffer + virtual int GetNumEvents() + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->GetNumEvents(); + } + + //! get malloc_flag + virtual int GetPreAllocFlag() + { + return m_use_prealloc_buf; + } + + //! get size of a data block + virtual int GetBlockNwords(int n) + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->GetBlockNwords(n); + } + + //! get FTSW ID to check whether this data block is FTSW data or not + virtual int CheckFTSWID(int n) + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->CheckFTSWID(n); + } + + //! get FTSW ID to check whether this data block is FTSW data or not + virtual int CheckTLUID(int n) + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->CheckTLUID(n); + } + + //! Copy one datablock to buffer + virtual void CopyBlock(int n, int* buf_to) + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->CopyBlock(n, buf_to); + } + + //! print data + virtual void PrintData(int* buf, int nwords) + { + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access_dblk->PrintData(buf, nwords); + } + + /* enum { */ + /* POS_NWORDS = 0, */ + /* POS_NODE_ID = 6 */ + /* }; */ + + /* enum { */ + /* // Tentatively needed to distinguish new and old FTSW format, which will be changed in Nov. 2013 */ + /* POS_FTSW_ID_OLD = 5, */ + /* TEMP_POS_NWORDS_HEADER = 1, */ + /* OLD_FTSW_NWORDS_HEADER = 6 */ + /* }; */ + + //! class to access + RawDataBlockFormat* m_access_dblk; //! do not record + + protected : + /// number of words of buffer + int m_nwords; + + /// number of nodes in this object + int m_num_nodes; + + /// number of events in this object + int m_num_events; + + /// Buffer + int* m_buffer; //[m_nwords] + + //! flag for deleting m_buffer in destructer( 0:delete, 1: not delete) + //! When using pre-allocated buffer, the buffer should be reused and not deleted in the destructer + int m_use_prealloc_buf; //! not record + + /// To drive from TObject + ClassDef(RawDataBlock, 3); + }; + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawDataBlockFormat.h b/ROPCApplications/rawdata/dataobjects/include/RawDataBlockFormat.h new file mode 100644 index 0000000000000000000000000000000000000000..13a6a774170b1c28e7b61dab3ce650df2fef1d0b --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawDataBlockFormat.h @@ -0,0 +1,110 @@ +//+ +// File : RawDataBlockFormat.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWDATABLOCKFORMAT_H +#define RAWDATABLOCKFORMAT_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <string> +#include <string.h> +#include <rawdata/switch_basf2_standalone.h> + +namespace Belle2 { + + /** + * The RawDataBlockFormat class + * Format information for rawdata handling + */ + class RawDataBlockFormat { + public: + //! Default constructor + RawDataBlockFormat(); + + //! Destructor + virtual ~RawDataBlockFormat(); + + //! set buffer ( delete_flag : m_buffer is freeed( = 0 )/ not freeed( = 1 ) in Destructer ) + virtual void SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes); + + //! Get total length of m_buffer + virtual int TotalBufNwords(); + + //! get position of data block in word + virtual int GetBufferPos(int n); + + //! get nth buffer pointer + virtual int* GetBuffer(int n); + + //! get pointer to buffer(m_buffer) + virtual int* GetWholeBuffer(); + + //! get # of data blocks = (# of nodes)*(# of events) + virtual int GetNumEntries() { return m_num_events * m_num_nodes; } + + //! get # of data sources(e.g. # of COPPER boards) in m_buffer + virtual int GetNumNodes() { return m_num_nodes; } + + //! get # of events in m_buffer + virtual int GetNumEvents() { return m_num_events; } + + //! get malloc_flag + virtual int GetPreAllocFlag() { return m_use_prealloc_buf; } + + //! get size of a data block + virtual int GetBlockNwords(int n); + + //! get FTSW ID to check whether this data block is FTSW data or not + virtual int CheckFTSWID(int n); + + //! get FTSW ID to check whether this data block is FTSW data or not + virtual int CheckTLUID(int n); + + //! Copy one datablock to buffer + virtual void CopyBlock(int n, int* buf_to); + + //! print data + virtual void PrintData(int* buf, int nwords); + + enum { + POS_NWORDS = 0, + POS_NODE_ID = 6 + }; + + enum { + // Tentatively needed to distinguish new and old FTSW format, which will be changed in Nov. 2013 + POS_FTSW_ID_OLD = 5, + TEMP_POS_NWORDS_HEADER = 1, + OLD_FTSW_NWORDS_HEADER = 6 + }; + + protected : + /// number of words of buffer + int m_nwords; + + /// number of nodes in this object + int m_num_nodes; + + /// number of events in this object + int m_num_events; + + /// Buffer + int* m_buffer; //! not recorded + + //! flag for deleting m_buffer in destructer( 0:delete, 1: not delete) + //! When using pre-allocated buffer, the buffer should be reused and not deleted in the destructer + int m_use_prealloc_buf; //! not record + + /* /// To drive from TObject */ + /* ClassDef(RawDataBlockFormat, 1); */ + }; + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawECL.h b/ROPCApplications/rawdata/dataobjects/include/RawECL.h new file mode 100644 index 0000000000000000000000000000000000000000..5402604e9c02fb4fd8ca2f0ec6b8586b4df47123 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawECL.h @@ -0,0 +1,38 @@ +//+ +// File : RawCOPPER.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWECL_H +#define RAWECL_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <rawdata/dataobjects/RawCOPPER.h> +#include <framework/datastore/DataStore.h> + +#include <TObject.h> + +namespace Belle2 { + + /** + * The Raw ECL class + * Class for RawCOPPER class data taken by ECL + * Currently, this class is almost same as RawCOPPER class. + */ + class RawECL : public RawCOPPER { + public: + //! Default constructor + RawECL(); + //! Destructor + virtual ~RawECL(); + /// To derive from TObject + ClassDef(RawECL, 1); + }; +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawFTSW.h b/ROPCApplications/rawdata/dataobjects/include/RawFTSW.h new file mode 100644 index 0000000000000000000000000000000000000000..64c82d773917cbc4820f90fe0a1c665d1506ec90 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawFTSW.h @@ -0,0 +1,253 @@ +//+ +// File : RawFTSW.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWFTSW_H +#define RAWFTSW_H + +// Includes +#include <rawdata/dataobjects/RawDataBlock.h> +#include <rawdata/dataobjects/RawFTSWFormat.h> +#include <rawdata/dataobjects/RawFTSWFormat_v1.h> +#include <rawdata/dataobjects/RawFTSWFormat_v2.h> +#include <rawdata/dataobjects/RawFTSWFormat_latest.h> + +namespace Belle2 { + + /** + * The Raw FTSW class. + * + * This class stores the RAW data containing FTSW data(event #, trg time ). + */ + class RawFTSW : public RawDataBlock { + public: + //! Default constructor + RawFTSW(); + + //! Constructor using existing pointer to raw data buffer + //RawFTSW(int* bufin, int nwords); + //! Destructor + virtual ~RawFTSW(); + + //! set buffer ( delete_flag : m_buffer is freeed( = 0 )/ not freeed( = 1 ) in Destructer ) + void SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes) OVERRIDE_CPP17; + + //! read data, detect and set the version number of the data format + void SetVersion(); + + //! Check the version number of data format + void CheckVersionSetBuffer(); + + //! Get # of words of header + int GetNwordsHeader(int n) + { + CheckVersionSetBuffer(); + return m_access->GetNwordsHeader(n); + } + + //! Get Node # ( should be "TTD " ) + unsigned int GetFTSWNodeID(int n) + { + CheckVersionSetBuffer(); + return m_access->GetFTSWNodeID(n); + } + + //! Get event # + unsigned int GetEveNo(int n) + { + CheckVersionSetBuffer(); + return m_access->GetEveNo(n); + } + + //! Get a word containing ctime and trigger type info + unsigned int GetTTCtimeTRGType(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTTCtimeTRGType(n); + } + + //! get unixtime of the trigger + unsigned int GetTTUtime(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTTUtime(n); + } + + //! Get ctime of the trigger + int GetTTCtime(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTTCtime(n); + } + + //! Get trgtype + int GetTRGType(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTRGType(n); + } + + //! Get timeval from ctime and utime + void GetTTTimeVal(int n, struct timeval* tv) + { + CheckVersionSetBuffer(); + return m_access->GetTTTimeVal(n, tv); + } + + //! Get timeval obtained from gettimeval() + void GetPCTimeVal(int n, struct timeval* tv) + { + CheckVersionSetBuffer(); + return m_access->GetPCTimeVal(n, tv); + } + + //! Get magic number for data corruption check + unsigned int GetMagicTrailer(int n) + { + CheckVersionSetBuffer(); + return m_access->GetMagicTrailer(n); + } + + //! check the data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) + { + CheckVersionSetBuffer(); + return m_access->CheckData(n, prev_evenum, cur_evenum, prev_exprunsubrun_no, cur_exprunsubrun_no); + } + + //! Exp# (10bit) run# (14bit) restart # (8bit) + unsigned int GetExpRunSubrun(int n) + { + CheckVersionSetBuffer(); + return m_access->GetExpRunSubrun(n); + } + + //! Get run # + int GetRunNo(int n) + { + CheckVersionSetBuffer(); + return m_access->GetRunNo(n); + } + + + //! Get subrun # + int GetSubRunNo(int n) + { + CheckVersionSetBuffer(); + return m_access->GetSubRunNo(n); + } + + //! get a word cotaining run # and subrun # + int GetRunNoSubRunNo(int n) + { + CheckVersionSetBuffer(); + return m_access->GetRunNoSubRunNo(n); + } + + //! Get Exp # + int GetExpNo(int n) + { + CheckVersionSetBuffer(); + return m_access->GetExpNo(n); + } + + //! DESY test only + int Get15bitTLUTag(int n) + { + CheckVersionSetBuffer(); + return m_access->Get15bitTLUTag(n); + } + + //! HER injection = 1 or LER injection = 0 + int GetIsHER(int n) + { + CheckVersionSetBuffer(); + return m_access->GetIsHER(n); + } + + //! Get time since the last injection + unsigned int GetTimeSinceLastInjection(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTimeSinceLastInjection(n); + } + + //! Get time since the previous trigger + unsigned int GetTimeSincePrevTrigger(int n) + { + CheckVersionSetBuffer(); + return m_access->GetTimeSincePrevTrigger(n); + } + + //! Get a bunch number + unsigned int GetBunchNumber(int n) + { + CheckVersionSetBuffer(); + return m_access->GetBunchNumber(n); + } + + //! Get a frame count + unsigned int GetFrameCount(int n) + { + CheckVersionSetBuffer(); + return m_access->GetFrameCount(n); + } + + enum { + /// Added to distinguish different version. + /// ver.0 : early DESY-beam-test version( Node ID 0x545444** is at 5th word (0,1,2,..5) + /// : Please ask Nakao-san about his e-mail ( [daq-experts:8055] FEE data header and FTSW data format) + /// ver.1 : late DESY-beam-test version( Node ID 0x545444** was moved to 6th word (0,1,2,..5) + /// : header size is ? words ( I need to ask Nakao-san because it will be used for distinguish between 0,1, and 2 + /// ver 2 : phase II ? version + /// : header size is 8 words + /// : Format is written by Nakao-san in ver.26(as of 2019.3.1 ) of https://confluence.desy.de/pages/viewpage.action?spaceKey=BI&title=DAQ+TimingDistribution#DAQTimingDistribution-InformationsentfromTTDtodatastream + //! 0x545452?? up to ver.2 this number is more or less same. So, it cannot be used to distinguish different version numbers + POS_NODE_FORMAT_ID = 6, // Position of format ID in header + FTSW_FORMAT_MASK = 0x000000FF, + + FORMAT_ID_VER_0TO2 = 0x54544420, + FORMAT_ID_VER_0TO3 = 0x54544431, + + POS_HEADER_SIZE = 1, //! The same number of this information must appear in RawFTSWFormat*.h. Information should be placed in one place but I need to put this number in RawFTSW.h because it is used to distinguish different version numbers. + // header size is used to distinguish different version number for ver. 0, 1, and 2 + VER_0_HEADER_SIZE = 0, //! Unpacker for ver.0(early DESY version) is not available. + VER_1_HEADER_SIZE = 0, // ! Unpacker for ver.1(late DESY version) is available but it is unlikely for a user to read this ver. Until there is a request to read ver.1, it is closed. + VER_2_HEADER_SIZE = 8, + VER_3_HEADER_SIZE = 8 + }; + + //! class to access + RawFTSWFormat* m_access{}; //! do not record + + /// Version of the format + int m_version = -1; //! do not record + + protected : + /// To derive from TObject + /// ver.2 Remove m_FTSW_header and introduce a new data format on Nov. 20, 2013 + /// ver.3 Add m_access on Mar. 7 2016 but Classded stays 2 for some(?) reason + /// ver.4 Add m_version on Feb. 18 2019 for the new format version defined by Nakao-san + /// Reason to stay at *2* is that the persistent content did *not change* + /// If necessary: be aware that we have to change this in online and offline at the same time! + ClassDef(RawFTSW, 2); + }; + + inline void RawFTSW::CheckVersionSetBuffer() + { + if (m_version < 0 || m_access == NULL) { + // Since both ver.0, 1 and ver.2 will show m_buffer[ POS_NODE_ID ] & FORMAT_MASK == 0x0, I need to ignore the check for ver.0 and 1 + SetVersion(); + } + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + } + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat.h b/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat.h new file mode 100644 index 0000000000000000000000000000000000000000..7644092327f59db31549ff1d0d5e2c452510a11c --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat.h @@ -0,0 +1,115 @@ +//+ +// File : RawFTSWFormat.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 7 - Mar - 2016 +//- + +#ifndef RAWFTSWFORMAT_H +#define RAWFTSWFORMAT_H +#include <rawdata/dataobjects/RawDataBlockFormat.h> + +namespace Belle2 { + + /** + * The Raw FTSW class. + * + * This class stores the RAW data containing FTSW data(event #, trg time ). + */ + class RawFTSWFormat : public RawDataBlockFormat { + public: + + //! Default constructor + RawFTSWFormat(); + //! Constructor using existing pointer to raw data buffer + //RawFTSWFormat(int* bufin, int nwords); + //! Destructor + virtual ~RawFTSWFormat(); + + /* //! Get # of words in this buffer */ + /* int GetNwords(int n); */ + + //! Get # of words of header + virtual int GetNwordsHeader(int n) = 0; + + //! Get Node # ( should be "TTD " ) + virtual unsigned int GetFTSWNodeID(int n) = 0; + + //! Get event # + virtual unsigned int GetEveNo(int n) = 0; + + //! Get a word containing ctime and trigger type info + virtual unsigned int GetTTCtimeTRGType(int n) = 0; + + //! get unixtime of the trigger + virtual unsigned int GetTTUtime(int n) = 0; + + //! Get ctime of the trigger + virtual int GetTTCtime(int n) = 0; + + //! Get trgtype + virtual int GetTRGType(int n) = 0; + + //! Get timeval from ctime and utime + virtual void GetTTTimeVal(int n, struct timeval* tv) = 0; + + //! Get timespec from ctime and utime + virtual void GetTTTimeSpec(int n, struct timespec* ts) = 0; + + //! Get time in ns since epoch from ctime and utime + virtual unsigned long long int GetTTTimeNs(int n) = 0; + + //! Get magic number for data corruption check + virtual unsigned int GetMagicTrailer(int n) = 0; + + //! Get stored information of getimeofday in a PC + virtual void GetPCTimeVal(int n, struct timeval* tv); + + + + //! check the data contents + virtual void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) = 0; + + //! Exp# (10bit) run# (14bit) restart # (8bit) + virtual unsigned int GetExpRunSubrun(int n) = 0; + + //! Get run # + virtual int GetRunNo(int n) = 0; + + //! Get subrun # + virtual int GetSubRunNo(int n) = 0; + + //! get a word cotaining run # and subrun # + virtual int GetRunNoSubRunNo(int n) = 0; + + //! Get Exp # + virtual int GetExpNo(int n) = 0; + + //! DESY test only (ver.1) + virtual int Get15bitTLUTag(int n); + + //! HER injection = 1 or LER injection = 0 + virtual int GetIsHER(int n); + + //! Get time since the last injection + virtual unsigned int GetTimeSinceLastInjection(int n); + + //! Get time since the previous trigger + virtual unsigned int GetTimeSincePrevTrigger(int n); + + //! Get a bunch number + virtual unsigned int GetBunchNumber(int n); + + //! Get a frame count + virtual unsigned int GetFrameCount(int n); + + protected : + + }; + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat_latest.h b/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat_latest.h new file mode 100644 index 0000000000000000000000000000000000000000..01fbd943e182000373f45a0fa02c911c7feebefa --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat_latest.h @@ -0,0 +1,198 @@ +//+ +// File : RawFTSWFormat_latest.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 7 - Mar - 2016 +//- + +#ifndef RAWFTSWFORMAT_LATEST_H +#define RAWFTSWFORMAT_LATEST_H +#include <rawdata/dataobjects/RawFTSWFormat.h> + +namespace Belle2 { + + /** + * The Raw FTSW class 3 ( 2019.8.20 ) + * + * This class stores the RAW data containing FTSW data(event #, trg time ). + */ + class RawFTSWFormat_latest : public RawFTSWFormat { + public: + + //! Default constructor + RawFTSWFormat_latest(); + //! Constructor using existing pointer to raw data buffer + //RawFTSWFormat_latest(int* bufin, int nwords); + //! Destructor + virtual ~RawFTSWFormat_latest(); + + /* //! Get # of words in this buffer */ + /* int GetNwords(int n); */ + + //! Get # of words of header + int GetNwordsHeader(int n) OVERRIDE_CPP17; + + //! Get Node # ( should be "TTD " ) + unsigned int GetFTSWNodeID(int n) OVERRIDE_CPP17; + + //! Get event # + unsigned int GetEveNo(int n) OVERRIDE_CPP17; + + //! Get a word containing ctime and trigger type info + unsigned int GetTTCtimeTRGType(int n) OVERRIDE_CPP17; + + //! get unixtime of the trigger + unsigned int GetTTUtime(int n) OVERRIDE_CPP17; + + //! Get ctime of the trigger + int GetTTCtime(int n) OVERRIDE_CPP17; + + //! Get trgtype + int GetTRGType(int n) OVERRIDE_CPP17; + + //! Get timeval from ctime and utime + void GetTTTimeVal(int n, struct timeval* tv) OVERRIDE_CPP17; + + //! Get timespec from ctime and utime + void GetTTTimeSpec(int n, struct timespec* ts) OVERRIDE_CPP17; + + //! Get time in ns since epoch from ctime and utime + unsigned long long int GetTTTimeNs(int n) OVERRIDE_CPP17; + + //! Get magic number for data corruption check + unsigned int GetMagicTrailer(int n) OVERRIDE_CPP17; + + //! Get stored information of getimeofday in a PC + void GetPCTimeVal(int n, struct timeval* tv) OVERRIDE_CPP17; + + //! check the data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! Exp# (10bit) run# (14bit) restart # (8bit) + unsigned int GetExpRunSubrun(int n) OVERRIDE_CPP17; + + //! Get run # + int GetRunNo(int n) OVERRIDE_CPP17; + + + //! Get subrun # + int GetSubRunNo(int n) OVERRIDE_CPP17; + + //! get a word cotaining run # and subrun # + int GetRunNoSubRunNo(int n) OVERRIDE_CPP17; + + //! Get Exp # + int GetExpNo(int n) OVERRIDE_CPP17; + + //! HER injection = 1 or LER injection = 0 + int GetIsHER(int n) OVERRIDE_CPP17; + + //! Get time since the last injection + unsigned int GetTimeSinceLastInjection(int n) OVERRIDE_CPP17; + + //! Get time since the previous trigger + unsigned int GetTimeSincePrevTrigger(int n) OVERRIDE_CPP17; + + //! Get a bunch number + unsigned int GetBunchNumber(int n) OVERRIDE_CPP17; + + //! Get a frame count + unsigned int GetFrameCount(int n) OVERRIDE_CPP17; + + //!header format + enum { + POS_NWORDS = 0, // same as v.1( late-DESY ver.) + POS_HDR_NWORDS = 1, // same as v.1 + POS_NUMEVE_NUMNODES = 2, // same as v.1 + POS_EXP_RUN_NO = 3, // same as v.1 + POS_EVE_NO_1 = 4, // same as v.1 + POS_NA_1 = 5, // same as v.1 + POS_NODE_ID = 6, // same as v.1 + POS_NA_2 = 7 // same as v.1 + }; + + //!data format + enum { + POS_TT_CTIME_TRGTYPE = 8, // same as v.1 + POS_TT_UTIME = 9, // same as v.1 + POS_EVE_NO_2 = 10, + POS_FRAME_COUNT = 11, + POS_TIME_SINCE_PREV_TRG = 12, + POS_INJECTION_INFO = 13, + POS_BUNCH_NUM = 14, + POS_TVSEC_FROM_PC = 15, + POS_TVUSEC_FROM_PC = 16 + }; + + //!trailer format + enum { + POS_RSVD_1 = 20, // same as v.1 but position is shifted + POS_MAGIC_1 = 21 // same as v.1 but position is shifted + }; + + enum { + INJ_HER_LER_MASK = 0x80000000, + INJ_HER_LER_SHIFT = 31, + INJ_TIME_MASK = 0x7FFFFFFF, + INJ_TIME_SHIFT = 0, + INJ_BUNCH_MASK = 0x000007FF, + INJ_BUNCH_SHIFT = 0, + EXP_MASK = 0xFFC00000, + EXP_SHIFT = 22, + RUNNO_MASK = 0x003FFF00, + RUNNO_SHIFT = 8, + SUBRUNNO_MASK = 0x000000FF, + TTCTIME_MASK = 0x7FFFFFF0, + TTCTIME_SHIFT = 4, + TRGTYPE_MASK = 0xF + }; + + enum { + SIZE_FTSW_PACKET = 22 + }; + //#endif + + enum { + FTSW_MAGIC_TRAILER = 0x7FFF0000 + }; + + protected : + + }; + + inline unsigned int RawFTSWFormat_latest::GetExpRunSubrun(int n) + { + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]); + + } + + + inline int RawFTSWFormat_latest::GetRunNo(int n) + { + return (((unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]) & RUNNO_MASK) + >> RUNNO_SHIFT); + } + + inline int RawFTSWFormat_latest::GetSubRunNo(int n) + { + return (m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ] & SUBRUNNO_MASK); + } + + inline int RawFTSWFormat_latest::GetRunNoSubRunNo(int n) + { + return ((unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]) & + (RUNNO_MASK | SUBRUNNO_MASK)); + } + + inline int RawFTSWFormat_latest::GetExpNo(int n) + { + return (((unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]) & EXP_MASK) + >> EXP_SHIFT); + } + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat_v1.h b/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat_v1.h new file mode 100644 index 0000000000000000000000000000000000000000..e371f4a935a1052eb44be992e3a6a087b8fa5f63 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat_v1.h @@ -0,0 +1,163 @@ +//+ +// File : RawFTSWFormat_v1.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 7 - Mar - 2016 +//- + +#ifndef RAWFTSWFORMAT_V1_H +#define RAWFTSWFORMAT_V1_H +#include <rawdata/dataobjects/RawFTSWFormat.h> + +namespace Belle2 { + + /** + * The Raw FTSW class ver.1 . + * + * This class stores the RAW data containing FTSW data(event #, trg time ). + */ + class RawFTSWFormat_v1 : public RawFTSWFormat { + public: + + //! Default constructor + RawFTSWFormat_v1(); + //! Constructor using existing pointer to raw data buffer + //RawFTSWFormat_v1(int* bufin, int nwords); + //! Destructor + virtual ~RawFTSWFormat_v1(); + + /* //! Get # of words in this buffer */ + /* int GetNwords(int n); */ + + //! Get # of words of header + int GetNwordsHeader(int n) OVERRIDE_CPP17; + + //! Get Node # ( should be "TTD " ) + unsigned int GetFTSWNodeID(int n) OVERRIDE_CPP17; + + //! Get event # + unsigned int GetEveNo(int n) OVERRIDE_CPP17; + + //! Get a word containing ctime and trigger type info + unsigned int GetTTCtimeTRGType(int n) OVERRIDE_CPP17; + + //! get unixtime of the trigger + unsigned int GetTTUtime(int n) OVERRIDE_CPP17; + + //! Get ctime of the trigger + int GetTTCtime(int n) OVERRIDE_CPP17; + + //! Get trgtype + int GetTRGType(int n) OVERRIDE_CPP17; + + //! Get timeval from ctime and utime + void GetTTTimeVal(int n, struct timeval* tv) OVERRIDE_CPP17; + + //! Get timespec from ctime and utime + void GetTTTimeSpec(int n, struct timespec* ts) OVERRIDE_CPP17; + + //! Get time in ns since epoch from ctime and utime + unsigned long long int GetTTTimeNs(int n) OVERRIDE_CPP17; + + //! Get magic number for data corruption check + unsigned int GetMagicTrailer(int n) OVERRIDE_CPP17; + + //! check the data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! Exp# (10bit) run# (14bit) restart # (8bit) + unsigned int GetExpRunSubrun(int n) OVERRIDE_CPP17; + + //! Get run # + int GetRunNo(int n) OVERRIDE_CPP17; + + + //! Get subrun # + int GetSubRunNo(int n) OVERRIDE_CPP17; + + //! get a word cotaining run # and subrun # + int GetRunNoSubRunNo(int n) OVERRIDE_CPP17; + + //! Get Exp # + int GetExpNo(int n) OVERRIDE_CPP17; + + //! DESY test only + int Get15bitTLUTag(int n) OVERRIDE_CPP17; + + enum { + POS_NWORDS = 0, + POS_HDR_NWORDS = 1, + POS_NUMEVE_NUMNODES = 2, + POS_EXP_RUN_NO = 3, + POS_EVE_NO = 4, + POS_NA_1 = 5, + POS_NODE_ID = 6, + POS_NA_2 = 7, + POS_TT_CTIME_TRGTYPE = 8, + POS_TT_UTIME = 9, + POS_FTSW_3 = 10, + POS_FTSW_4 = 11, + POS_RSVD_1 = 12, + POS_MAGIC_1 = 13 + }; + + enum { + EXP_MASK = 0xFFC00000, + EXP_SHIFT = 22, + RUNNO_MASK = 0x003FFF00, + RUNNO_SHIFT = 8, + SUBRUNNO_MASK = 0x000000FF, + TTCTIME_MASK = 0x7FFFFFF0, + TTCTIME_SHIFT = 4, + TRGTYPE_MASK = 0xF + }; + + enum { + SIZE_FTSW_PACKET = 14 + }; + //#endif + + enum { + FTSW_MAGIC_TRAILER = 0x7FFF0000 + }; + + protected : + + }; + + inline unsigned int RawFTSWFormat_v1::GetExpRunSubrun(int n) + { + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]); + + } + + + inline int RawFTSWFormat_v1::GetRunNo(int n) + { + return (((unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]) & RUNNO_MASK) + >> RUNNO_SHIFT); + } + + inline int RawFTSWFormat_v1::GetSubRunNo(int n) + { + return (m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ] & SUBRUNNO_MASK); + } + + inline int RawFTSWFormat_v1::GetRunNoSubRunNo(int n) + { + return ((unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]) & + (RUNNO_MASK | SUBRUNNO_MASK)); + } + + inline int RawFTSWFormat_v1::GetExpNo(int n) + { + return (((unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]) & EXP_MASK) + >> EXP_SHIFT); + } + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat_v2.h b/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat_v2.h new file mode 100644 index 0000000000000000000000000000000000000000..17bd9f068a3a71820d3246ad09cc7b914bce5743 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawFTSWFormat_v2.h @@ -0,0 +1,196 @@ +//+ +// File : RawFTSWFormat_v2.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 7 - Mar - 2016 +//- + +#ifndef RAWFTSWFORMAT_V2_H +#define RAWFTSWFORMAT_V2_H +#include <rawdata/dataobjects/RawFTSWFormat.h> +#include "rawdata/switch_basf2_standalone.h" + +namespace Belle2 { + + /** + * The Raw FTSW class ver.2 . + * + * Nakao-san will add additional data in FTSW format + */ + class RawFTSWFormat_v2 : public RawFTSWFormat { + public: + + //! Default constructor + RawFTSWFormat_v2(); + //! Constructor using existing pointer to raw data buffer + //RawFTSWFormat_v2(int* bufin, int nwords); + //! Destructor + virtual ~RawFTSWFormat_v2(); + + /* //! Get # of words in this buffer */ + /* int GetNwords(int n); */ + + //! Get # of words of header + int GetNwordsHeader(int n) OVERRIDE_CPP17; + + //! Get Node # ( should be "TTD " ) + unsigned int GetFTSWNodeID(int n) OVERRIDE_CPP17; + + //! Get event # + unsigned int GetEveNo(int n) OVERRIDE_CPP17; + + //! Get a word containing ctime and trigger type info + unsigned int GetTTCtimeTRGType(int n) OVERRIDE_CPP17; + + //! get unixtime of the trigger + unsigned int GetTTUtime(int n) OVERRIDE_CPP17; + + //! Get ctime of the trigger + int GetTTCtime(int n) OVERRIDE_CPP17; + + //! Get trgtype + int GetTRGType(int n) OVERRIDE_CPP17; + + //! Get timeval from ctime and utime + void GetTTTimeVal(int n, struct timeval* tv) OVERRIDE_CPP17; + + //! Get timespec from ctime and utime + void GetTTTimeSpec(int n, struct timespec* ts) OVERRIDE_CPP17; + + //! Get time in ns since epoch from ctime and utime + unsigned long long int GetTTTimeNs(int n) OVERRIDE_CPP17; + + //! Get magic number for data corruption check + unsigned int GetMagicTrailer(int n) OVERRIDE_CPP17; + + + + //! check the data contents + void CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) OVERRIDE_CPP17; + + //! Exp# (10bit) run# (14bit) restart # (8bit) + unsigned int GetExpRunSubrun(int n) OVERRIDE_CPP17; + + //! Get run # + int GetRunNo(int n) OVERRIDE_CPP17; + + + //! Get subrun # + int GetSubRunNo(int n) OVERRIDE_CPP17; + + //! get a word cotaining run # and subrun # + int GetRunNoSubRunNo(int n) OVERRIDE_CPP17; + + //! Get Exp # + int GetExpNo(int n) OVERRIDE_CPP17; + + //! HER injection = 1 or LER injection = 0 + int GetIsHER(int n) OVERRIDE_CPP17; + + //! Get time since the last injection + unsigned int GetTimeSinceLastInjection(int n) OVERRIDE_CPP17; + + //! Get time since the previous trigger + unsigned int GetTimeSincePrevTrigger(int n) OVERRIDE_CPP17; + + //! Get a bunch number + unsigned int GetBunchNumber(int n) OVERRIDE_CPP17; + + //! Get a frame count + unsigned int GetFrameCount(int n) OVERRIDE_CPP17; + + //!header format + enum { + POS_NWORDS = 0, // same as v.1( late-DESY ver.) + POS_HDR_NWORDS = 1, // same as v.1 + POS_NUMEVE_NUMNODES = 2, // same as v.1 + POS_EXP_RUN_NO = 3, // same as v.1 + POS_EVE_NO_1 = 4, // same as v.1 + POS_NA_1 = 5, // same as v.1 + POS_NODE_ID = 6, // same as v.1 + POS_NA_2 = 7 // same as v.1 + }; + + //!data format + enum { + POS_TT_CTIME_TRGTYPE = 8, // same as v.1 + POS_TT_UTIME = 9, // same as v.1 + POS_EVE_NO_2 = 10, + POS_FRAME_COUNT = 11, + POS_TIME_SINCE_PREV_TRG = 12, + POS_INJECTION_INFO = 13, + POS_BUNCH_NUM = 14 + }; + + //!trailer format + enum { + POS_RSVD_1 = 20, // same as v.1 but position is shifted + POS_MAGIC_1 = 21 // same as v.1 but position is shifted + }; + + enum { + INJ_HER_LER_MASK = 0x80000000, + INJ_HER_LER_SHIFT = 31, + INJ_TIME_MASK = 0x7FFFFFFF, + INJ_TIME_SHIFT = 0, + INJ_BUNCH_MASK = 0x000007FF, + INJ_BUNCH_SHIFT = 0, + EXP_MASK = 0xFFC00000, + EXP_SHIFT = 22, + RUNNO_MASK = 0x003FFF00, + RUNNO_SHIFT = 8, + SUBRUNNO_MASK = 0x000000FF, + TTCTIME_MASK = 0x7FFFFFF0, + TTCTIME_SHIFT = 4, + TRGTYPE_MASK = 0xF + }; + + enum { + SIZE_FTSW_PACKET = 22 + }; + //#endif + + enum { + FTSW_MAGIC_TRAILER = 0x7FFF0000 + }; + + protected : + + }; + + inline unsigned int RawFTSWFormat_v2::GetExpRunSubrun(int n) + { + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]); + + } + + + inline int RawFTSWFormat_v2::GetRunNo(int n) + { + return (((unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]) & RUNNO_MASK) + >> RUNNO_SHIFT); + } + + inline int RawFTSWFormat_v2::GetSubRunNo(int n) + { + return (m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ] & SUBRUNNO_MASK); + } + + inline int RawFTSWFormat_v2::GetRunNoSubRunNo(int n) + { + return ((unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]) & + (RUNNO_MASK | SUBRUNNO_MASK)); + } + + inline int RawFTSWFormat_v2::GetExpNo(int n) + { + return (((unsigned int)(m_buffer[ GetBufferPos(n) + POS_EXP_RUN_NO ]) & EXP_MASK) + >> EXP_SHIFT); + } + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawHeader_latest.h b/ROPCApplications/rawdata/dataobjects/include/RawHeader_latest.h new file mode 100644 index 0000000000000000000000000000000000000000..4e468443ec63471e0f7b46f4735313f6626af6b5 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawHeader_latest.h @@ -0,0 +1,526 @@ +//+ +// File : RawHeader_latest.h +// Description : Module to handle RawHeader_latest attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWHEADER_LATEST_H +#define RAWHEADER_LATEST_H + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +//#include <TObject.h> +#include <rawdata/switch_basf2_standalone.h> + +//#include <framework/datastore/DataStore.h> + + +//#define HEADER_SIZE 16 + +namespace Belle2 { + + /** + * The Raw Header class ver.1 ( the latest version since May, 2014 ) + * This class defines the format of the header of RawCOPPER class data + * and used for extracting header info from RawCOPPER object + */ + + // class RawHeader_latest : public TObject { + class RawHeader_latest { + public: + //! Default constructor + RawHeader_latest(); + + //! Constructor using existing pointer to raw data buffer + RawHeader_latest(int*); + + //! Destructor + ~RawHeader_latest(); + + //! Get header contents + int* GetBuffer() { return m_buffer; } + + //! set buffer + void SetBuffer(int* bufin) { m_buffer = bufin; } + + //! initialize header + void CheckSetBuffer(); + + //! check if m_buffer exists + void CheckGetBuffer(); + + //! set contents of header + void SetNwords(int nwords); + + //! set contents of header + void SetEveNo(unsigned int eve_no); + + //! set contents of header + void SetNodeID(unsigned int node_id); + + //! set contents of header + void SetDataType(int data_type); + + //! set contents of header + void SetTruncMask(int trunc_mask); + + // void SetB2LFEEHdrPart(unsigned int word1, unsigned int word2); //! set contents of header + // void SetFTSW2Words(int* ftsw_buf); + + /// Set values of FTSW info( trigger timing) + void SetFTSW2Words(unsigned int word1, unsigned int word2); + + /// Set a word consists of exp #, run # and subrun # + void SetExpRunNumber(int* exprun_buf); + + //! set contents of header + void SetOffset1stFINESSE(int offset_1st_FINESSE); //! set contents of header + + void SetOffset2ndFINESSE(int offset_2nd_FINESSE); //! set contents of header + + void SetOffset3rdFINESSE(int offset_3rd_FINESSE); //! set contents of header + + void SetOffset4thFINESSE(int offset_4th_FINESSE); //! set contents of header + + // void SetMagicWordEntireHeader(); //! set magic words; + + /// Add nodeinfo in trace area + // int AddNodeInfo(int node_id); + + int GetNwords(); //! get contents of header + + int GetHdrNwords(); //! get contents of header + + unsigned int GetExpRunSubrun(); //! get a run/exp number combined word + + int GetExpNo(); //! get contents of header + + int GetRunNo(); //! get run # (14bit) + + int GetSubRunNo(); //! get restart #(8bit) + + unsigned int GetEveNo(); //! get contents of header + + unsigned int GetNodeID(); //! get contents of header + + int GetDataType(); //! get contents of header + + int GetTruncMask(); //! get contents of header + + //! Get Detected Error bitflag + unsigned int GetErrorBitFlag(); + + //! Add Detected Error bitflag + void AddErrorBitFlag(unsigned int error_bit_flag); + + //! Get Packet CRC error flag + int GetPacketCRCError(); + + //! Get Detected Error bitflag + int GetEventCRCError(); + + //! get contents of header + int GetOffset1stFINESSE(); + + //! get contents of header + int GetOffset2ndFINESSE(); + + //! get contents of header + int GetOffset3rdFINESSE(); + + //! get contents of header + int GetOffset4thFINESSE(); + + // int GetNumNodes(); //! get contents of header + + // int GetNodeInfo(int node_no, int* node_id); //! get contents of header + + //! get contents of header + unsigned int GetTTCtimeTRGType(); + + //! get contents of header + int GetTTCtime(); + + //! get TRGType + int GetTRGType(); + + //! get contents of header + unsigned int GetTTUtime(); + + //! get contents of header + void GetTTTimeVal(struct timeval* tv); + + /// Get magic word to check the data corruption + // unsigned int GetMagicWordEntireHeader(); + + //! check the contents of header + void CheckHeader(int* buf); + + /* + Experimental(10bit) #, Run#(14bit), restat# bit mask(8bit) + */ + enum { + MAGIC_WORD = 0x7F7F0000, + MAGIC_MASK = 0xFFFF0000, + MAGIC_SHIFT = 16, + FORMAT_VERSION__MASK = 0x0000FF00, + FORMAT_VERSION_SHIFT = 8 + }; + + enum { + EXP_MASK = 0xFFC00000, + EXP_SHIFT = 22, + RUNNO_MASK = 0x003FFF00, + RUNNO_SHIFT = 8, + SUBRUNNO_MASK = 0x000000FF + }; + + enum { + RAWHEADER_NWORDS = 12 + }; + + enum { + HDR_NWORDS_MASK = 0x000000FF + }; + + /* Data Format : Fixed length part*/ + enum { + POS_NWORDS = 0, + POS_VERSION_HDRNWORDS = 1, + POS_EXP_RUN_NO = 2, + POS_EVE_NO = 3, + POS_TTCTIME_TRGTYPE = 4, + POS_TTUTIME = 5, + POS_NODE_ID = 6, + POS_TRUNC_MASK_DATATYPE = 7, + POS_OFFSET_1ST_FINESSE = 8, + POS_OFFSET_2ND_FINESSE = 9, + POS_OFFSET_3RD_FINESSE = 10, + POS_OFFSET_4TH_FINESSE = 11, + }; + + + /* Data Format : Node info */ + /* enum { */ + /* NUM_MAX_NODES = 4 /\* (NUM_MAX_NODES + 1) slots are available in m_buffer. */ + /* (NUM_MAX_NODES +1 )th slot is filled with -1, when the number of total nodes */ + /* exceeds NUM_MAX_NODES *\/ */ + /* }; */ + + /* /\* Data Format : Magic word *\/ */ + /* enum { */ + /* MAGIC_WORD_TERM_HEADER = 0x7fff0005 */ + /* }; */ + + + + /* To extract ctime */ + enum { + TTCTIME_MASK = 0x7FFFFFF0, + TTCTIME_SHIFT = 4, + TRGTYPE_MASK = 0xF + }; + + /* Error bit in POS_TRUNC_MASK_DATATYPE */ + /* Changed from the position of error bit to actual error value ( 0 -> 0x1, 1 -> 0x2 ) */ + enum { + B2LINK_PACKET_CRC_ERROR = 0x1, + B2LINK_EVENT_CRC_ERROR = 0x2 + }; + + private: + //! do not record buffer ( RawCOPPER includes buffer of RawHeader_latest and RawTrailer ) + int* m_buffer; //! do not record + + /// To derive from TObject + // ver.2 Do not record m_buffer pointer. (Dec.19, 2014) + // ClassDef(RawHeader_latest, 1); + }; + + + inline void RawHeader_latest::CheckSetBuffer() + { + if (m_buffer == NULL) { + B2FATAL("m_buffer is NULL. Exiting..."); + } + } + + inline void RawHeader_latest::CheckGetBuffer() + { + if (m_buffer == NULL) { + B2FATAL("m_buffer is NULL. Data is corrupted or header info has not yet filled. Exiting..."); +// } else if (m_buffer[ POS_TERM_HEADER ] != MAGIC_WORD_TERM_HEADER) { +// B2FATAL("magic word is invalid(0x"<< std::hex() << m_buffer[ POS_TERM_HEADER ] <<"). Data is corrupted or header info has not yet filled. Exiting..."); + } + } + + + inline void RawHeader_latest::SetNwords(int nwords) + { + CheckSetBuffer(); + m_buffer[ POS_NWORDS ] = nwords; + + } + + inline void RawHeader_latest::SetEveNo(unsigned int eve_no) + { + CheckSetBuffer(); + m_buffer[ POS_EVE_NO ] = eve_no; + } + + inline void RawHeader_latest::SetNodeID(unsigned int node_id) + { + CheckSetBuffer(); + m_buffer[ POS_NODE_ID ] = (int)node_id; + } + + inline void RawHeader_latest::SetDataType(int data_type) + { + CheckSetBuffer(); + m_buffer[ POS_TRUNC_MASK_DATATYPE ] = + (data_type & 0x7FFFFFFF) | (m_buffer[ POS_TRUNC_MASK_DATATYPE ] & 0x80000000); + } + + inline void RawHeader_latest::SetTruncMask(int trunc_mask) + { + CheckSetBuffer(); + m_buffer[ POS_TRUNC_MASK_DATATYPE ] = (trunc_mask << 31) | (m_buffer[ POS_TRUNC_MASK_DATATYPE ] & 0x7FFFFFFF); + } + + /* inline void RawHeader_latest::SetB2LFEEHdrPart(unsigned int word1, unsigned int word2) */ + /* { */ + /* m_buffer[ POS_HSLB_1 ] = word1; */ + /* m_buffer[ POS_HSLB_2 ] = word2; */ + /* } */ + + + inline void RawHeader_latest::SetOffset1stFINESSE(int offset_1st_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_1ST_FINESSE ] = offset_1st_FINESSE; + } + + inline void RawHeader_latest::SetOffset2ndFINESSE(int offset_2nd_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_2ND_FINESSE ] = offset_2nd_FINESSE; + } + + inline void RawHeader_latest::SetOffset3rdFINESSE(int offset_3rd_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_3RD_FINESSE ] = offset_3rd_FINESSE; + } + + inline void RawHeader_latest::SetOffset4thFINESSE(int offset_4th_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_4TH_FINESSE ] = offset_4th_FINESSE; + } + + /* inline void RawHeader_latest::SetFTSW2Words(int* ftsw_buf) */ + /* { */ + /* CheckSetBuffer(); */ + /* memcpy(&(m_buffer[ POS_HSLB_1 ]), (char*)ftsw_buf, sizeof(int) * 2); */ + /* return; */ + /* } */ + + inline void RawHeader_latest::SetFTSW2Words(unsigned int word1, + unsigned int word2) + { + CheckSetBuffer(); + m_buffer[ POS_TTCTIME_TRGTYPE ] = word1; + m_buffer[ POS_TTUTIME ] = word2; + return; + } + + + inline void RawHeader_latest::SetExpRunNumber(int* exprun_buf) + { + CheckSetBuffer(); + memcpy(&(m_buffer[ POS_EXP_RUN_NO ]), (char*)exprun_buf, sizeof(int) * 1); + return; + } + + +// +// Obtain info +// + + inline int RawHeader_latest::GetNwords() + { + CheckGetBuffer(); + return m_buffer[ POS_NWORDS ]; + } + + inline int RawHeader_latest::GetHdrNwords() + { + + // CheckGetBuffer(); + // return m_buffer[ POS_HDR_NWORDS ]; + return RAWHEADER_NWORDS; + } + + inline int RawHeader_latest::GetExpNo() + { + CheckGetBuffer(); + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NO ]) & EXP_MASK) + >> EXP_SHIFT); + } + + + inline int RawHeader_latest::GetRunNo() + { + CheckGetBuffer(); + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NO ]) & RUNNO_MASK) + >> RUNNO_SHIFT); + } + + inline int RawHeader_latest::GetSubRunNo() + { + CheckGetBuffer(); + return (m_buffer[ POS_EXP_RUN_NO ] & SUBRUNNO_MASK); + } + + inline unsigned int RawHeader_latest::GetExpRunSubrun() + { + CheckGetBuffer(); + return ((unsigned int)(m_buffer[ POS_EXP_RUN_NO ])); + } + + + inline unsigned int RawHeader_latest::GetEveNo() + { + CheckGetBuffer(); + return m_buffer[ POS_EVE_NO ]; + } + + inline unsigned int RawHeader_latest::GetNodeID() + { + CheckGetBuffer(); + return (unsigned int)m_buffer[ POS_NODE_ID ]; + } + + inline int RawHeader_latest::GetDataType() + { + CheckGetBuffer(); + return (m_buffer[ POS_TRUNC_MASK_DATATYPE ] & 0x7FFFFFFF); + } + + inline int RawHeader_latest::GetTruncMask() + { + CheckGetBuffer(); + return (m_buffer[ POS_TRUNC_MASK_DATATYPE ] >> 23) & 0x1; + } + + inline unsigned int RawHeader_latest::GetErrorBitFlag() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_TRUNC_MASK_DATATYPE ]); + } + + inline void RawHeader_latest::AddErrorBitFlag(unsigned int error_bit_flag) + { + CheckGetBuffer(); + m_buffer[ POS_TRUNC_MASK_DATATYPE ] |= (int)error_bit_flag; + return; + } + + inline int RawHeader_latest::GetPacketCRCError() + { + CheckGetBuffer(); + unsigned int temp_err_flag = GetErrorBitFlag(); + if ((temp_err_flag & B2LINK_PACKET_CRC_ERROR) == 0) { + return 0; + } + return 1; + } + + inline int RawHeader_latest::GetEventCRCError() + { + CheckGetBuffer(); + unsigned int temp_err_flag = GetErrorBitFlag(); + if ((temp_err_flag & B2LINK_EVENT_CRC_ERROR) == 0) { + return 0; + } + return 1; + } + + inline int RawHeader_latest::GetOffset1stFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_1ST_FINESSE ]; + } + + inline int RawHeader_latest::GetOffset2ndFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_2ND_FINESSE ]; + } + + inline int RawHeader_latest::GetOffset3rdFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_3RD_FINESSE ]; + } + + inline int RawHeader_latest::GetOffset4thFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_4TH_FINESSE ]; + } + + /* inline int RawHeader_latest::GetNumNodes() */ + /* { */ + /* CheckGetBuffer(); */ + /* return m_buffer[ POS_NUM_NODES ]; */ + /* } */ + + inline unsigned int RawHeader_latest::GetTTCtimeTRGType() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_TTCTIME_TRGTYPE ]); + } + + inline int RawHeader_latest::GetTTCtime() + { + CheckGetBuffer(); + return (int)((GetTTCtimeTRGType() & TTCTIME_MASK) >> TTCTIME_SHIFT); + } + + inline int RawHeader_latest::GetTRGType() + { + CheckGetBuffer(); + return (int)(GetTTCtimeTRGType() & TRGTYPE_MASK); + } + + inline unsigned int RawHeader_latest::GetTTUtime() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_TTUTIME ]); + } + + inline void RawHeader_latest::GetTTTimeVal(struct timeval* tv) + { + tv->tv_sec = GetTTUtime(); + tv->tv_usec = (int)(((double)GetTTCtime()) / 127.216); + return ; + } + + + /* inline unsigned int RawHeader_latest::GetMagicWordEntireHeader() */ + /* { */ + /* CheckGetBuffer(); */ + /* return m_buffer[ POS_TERM_HEADER ]; */ + /* } */ + + + + + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawHeader_v0.h b/ROPCApplications/rawdata/dataobjects/include/RawHeader_v0.h new file mode 100644 index 0000000000000000000000000000000000000000..eb942b3d6be78e50837e4b9eae30f16b09565914 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawHeader_v0.h @@ -0,0 +1,511 @@ +//+ +// File : RawHeader_v0.h +// Description : Module to handle RawHeader_v0 attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWHEADER_V0_H +#define RAWHEADER_V0_H + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +//#include <TObject.h> +#include <rawdata/switch_basf2_standalone.h> + +//#include <framework/datastore/DataStore.h> + + +//#define HEADER_SIZE 16 + +namespace Belle2 { + + /** + * The Raw Header class ver.0 ( from August, 2013 to April, 2014 ) + * This class defines the format of the header of RawCOPPER class data + * and used for extracting header info from RawCOPPER object + */ + + // class RawHeader_v0 : public TObject { + class RawHeader_v0 { + public: + //! Default constructor + RawHeader_v0(); + + //! Constructor using existing pointer to raw data buffer + RawHeader_v0(int*); + + //! Destructor + ~RawHeader_v0(); + + //! Get header contents + int* GetBuffer() { return m_buffer; } + + //! set buffer + void SetBuffer(int* bufin) { m_buffer = bufin; } + + //! initialize header + void CheckSetBuffer(); + + //! check if m_buffer exists + void CheckGetBuffer(); + + //! initialize header + void Initialize(); + + //! set contents of header + void SetNwords(int nwords); + + //! set contents of header + void SetEveNo(unsigned int eve_no); //! set contents of header + + void SetNodeID(unsigned int node_id); //! set contents of header + + void SetDataType(int data_type); //! set contents of header + + void SetTruncMask(int trunc_mask); //! set contents of header + + // void SetB2LFEEHdrPart(unsigned int word1, unsigned int word2); //! set contents of header + // void SetFTSW2Words(int* ftsw_buf); + + /// Set values of FTSW info( trigger timing) + void SetFTSW2Words(unsigned int word1, unsigned int word2); + + /// Set a word consists of exp #, run # and subrun # + void SetExpRunNumber(int* exprun_buf); + + //! set contents of header + void SetOffset1stFINESSE(int offset_1st_FINESSE); //! set contents of header + + void SetOffset2ndFINESSE(int offset_2nd_FINESSE); //! set contents of header + + void SetOffset3rdFINESSE(int offset_3rd_FINESSE); //! set contents of header + + void SetOffset4thFINESSE(int offset_4th_FINESSE); //! set contents of header + + void SetMagicWordEntireHeader(); //! set magic words; + + /// Add nodeinfo in trace area + int AddNodeInfo(int node_id); + + //! get contents of header + int GetNwords(); //! get contents of header + + int GetHdrNwords(); //! get contents of header + + unsigned int GetExpRunSubrun(); //! get a run/exp number combined word + + int GetExpNo(); //! get contents of header + + int GetRunNo(); //! get run # (14bit) + + int GetSubRunNo(); //! get restart #(8bit) + + unsigned int GetEveNo(); //! get contents of header + + unsigned int GetNodeID(); //! get contents of header + + int GetDataType(); //! get contents of header + + int GetTruncMask(); //! get contents of header + + //! Get Detected Error bitflag + unsigned int GetErrorBitFlag(); + + //! Add Detected Error bitflag + void AddErrorBitFlag(unsigned int error_bit_flag); + + //! get contents of header + int GetOffset1stFINESSE(); + + //! get contents of header + int GetOffset2ndFINESSE(); + + //! get contents of header + int GetOffset3rdFINESSE(); + + //! get contents of header + int GetOffset4thFINESSE(); + + //! get contents of header + int GetNumNodes(); + + //! get contents of header + int GetNodeInfo(int node_no, int* node_id); + + //! get contents of header + unsigned int GetTTCtimeTRGType(); + + //! get contents of header + int GetTTCtime(); + + //! get TRGType + int GetTRGType(); + + //! get contents of header + unsigned int GetTTUtime(); + + //! get contents of header + void GetTTTimeVal(struct timeval* tv); + + /// Get magic word to check the data corruption + unsigned int GetMagicWordEntireHeader(); + + /* + Experimental(10bit) #, Run#(14bit), restat# bit mask(8bit) + */ + enum { + FORMAT_VERSION__MASK = 0x0000FF00, + FORMAT_VERSION_SHIFT = 8 + }; + + enum { + EXP_MASK = 0xFFC00000, + EXP_SHIFT = 22, + RUNNO_MASK = 0x003FFF00, + RUNNO_SHIFT = 8, + SUBRUNNO_MASK = 0x000000FF + }; + + enum { + RAWHEADER_NWORDS = 20 + }; + + /* Data Format : Fixed length part*/ + enum { + POS_NWORDS = 0, + POS_HDR_NWORDS = 1, + POS_EXP_RUN_NO = 2, + POS_EVE_NO = 3, + POS_TTCTIME_TRGTYPE = 4, + POS_TTUTIME = 5, + POS_NODE_ID = 6, + POS_TRUNC_MASK_DATATYPE = 7, + POS_OFFSET_1ST_FINESSE = 8, + POS_OFFSET_2ND_FINESSE = 9, + POS_OFFSET_3RD_FINESSE = 10, + POS_OFFSET_4TH_FINESSE = 11, + POS_RESVD_1 = 12, + POS_RESVD_2 = 13, + POS_NUM_NODES = 14, + POS_NODES_1 = 15, + POS_NODES_2 = 16, + POS_NODES_3 = 17, + POS_NODES_4 = 18, + POS_TERM_HEADER = 19 + }; + /* Data Format : Node info */ + enum { + NUM_MAX_NODES = 4 /* (NUM_MAX_NODES + 1) slots are available in m_buffer. + (NUM_MAX_NODES +1 )th slot is filled with -1, when the number of total nodes + exceeds NUM_MAX_NODES */ + }; + + /* Data Format : Magic word */ + enum { + MAGIC_WORD_TERM_HEADER = 0x7fff0005 + }; + + /* To extract ctime */ + enum { + TTCTIME_MASK = 0x7FFFFFF0, + TTCTIME_SHIFT = 4, + TRGTYPE_MASK = 0xF + }; + + private: + //! do not record buffer ( RawCOPPER includes buffer of RawHeader_v0 and RawTrailer ) + int* m_buffer; //! do not record + + /// To derive from TObject + // ver.2 Do not record m_buffer pointer. (Dec.19, 2014) + // ClassDef(RawHeader_v0, 2); + }; + + + inline void RawHeader_v0::CheckSetBuffer() + { + if (m_buffer == NULL) { + B2FATAL("m_buffer is NULL. Exiting..."); + } + } + + inline void RawHeader_v0::CheckGetBuffer() + { + if (m_buffer == NULL) { + B2FATAL("m_buffer is NULL. Data is corrupted or header info has not yet filled. Exiting..."); + } else if (m_buffer[ POS_TERM_HEADER ] != MAGIC_WORD_TERM_HEADER) { + B2FATAL("magic word is invalid(0x" << std::hex << m_buffer[ POS_TERM_HEADER ] << "," << m_buffer << " pos " << std::dec << + POS_TERM_HEADER + << "). Data is corrupted or header info has not yet filled. Exiting..."); + } + } + + + +// +// Set info. +// + inline void RawHeader_v0::Initialize() + { + CheckSetBuffer(); + memset(m_buffer, 0, sizeof(int)*RAWHEADER_NWORDS); + m_buffer[ POS_HDR_NWORDS ] = RAWHEADER_NWORDS; + m_buffer[ POS_NUM_NODES ] = 0; + + } + + inline void RawHeader_v0::SetMagicWordEntireHeader() + { + m_buffer[ POS_TERM_HEADER ] = MAGIC_WORD_TERM_HEADER; + } + + inline void RawHeader_v0::SetNwords(int nwords) + { + CheckSetBuffer(); + m_buffer[ POS_NWORDS ] = nwords; + + } + + inline void RawHeader_v0::SetEveNo(unsigned int eve_no) + { + CheckSetBuffer(); + m_buffer[ POS_EVE_NO ] = eve_no; + } + + inline void RawHeader_v0::SetNodeID(unsigned int node_id) + { + CheckSetBuffer(); + m_buffer[ POS_NODE_ID ] = node_id; + } + + inline void RawHeader_v0::SetDataType(int data_type) + { + CheckSetBuffer(); + m_buffer[ POS_TRUNC_MASK_DATATYPE ] = + (data_type & 0x7FFFFFFF) | (m_buffer[ POS_TRUNC_MASK_DATATYPE ] & 0x80000000); + } + + inline void RawHeader_v0::SetTruncMask(int trunc_mask) + { + CheckSetBuffer(); + m_buffer[ POS_TRUNC_MASK_DATATYPE ] = (trunc_mask << 31) | (m_buffer[ POS_TRUNC_MASK_DATATYPE ] & 0x7FFFFFFF); + } + + /* inline void RawHeader_v0::SetB2LFEEHdrPart(unsigned int word1, unsigned int word2) */ + /* { */ + /* m_buffer[ POS_HSLB_1 ] = word1; */ + /* m_buffer[ POS_HSLB_2 ] = word2; */ + /* } */ + + + inline void RawHeader_v0::SetOffset1stFINESSE(int offset_1st_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_1ST_FINESSE ] = offset_1st_FINESSE; + } + + inline void RawHeader_v0::SetOffset2ndFINESSE(int offset_2nd_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_2ND_FINESSE ] = offset_2nd_FINESSE; + } + + inline void RawHeader_v0::SetOffset3rdFINESSE(int offset_3rd_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_3RD_FINESSE ] = offset_3rd_FINESSE; + } + + inline void RawHeader_v0::SetOffset4thFINESSE(int offset_4th_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_4TH_FINESSE ] = offset_4th_FINESSE; + } + + /* inline void RawHeader_v0::SetFTSW2Words(int* ftsw_buf) */ + /* { */ + /* CheckSetBuffer(); */ + /* memcpy(&(m_buffer[ POS_HSLB_1 ]), (char*)ftsw_buf, sizeof(int) * 2); */ + /* return; */ + /* } */ + + inline void RawHeader_v0::SetFTSW2Words(unsigned int word1, + unsigned int word2) + { + CheckSetBuffer(); + m_buffer[ POS_TTCTIME_TRGTYPE ] = word1; + m_buffer[ POS_TTUTIME ] = word2; + return; + } + + + inline void RawHeader_v0::SetExpRunNumber(int* exprun_buf) + { + CheckSetBuffer(); + memcpy(&(m_buffer[ POS_EXP_RUN_NO ]), (char*)exprun_buf, sizeof(int) * 1); + return; + } + + +// +// Obtain info +// + + inline int RawHeader_v0::GetNwords() + { + CheckGetBuffer(); + return m_buffer[ POS_NWORDS ]; + } + + inline int RawHeader_v0::GetHdrNwords() + { + + CheckGetBuffer(); + + return m_buffer[ POS_HDR_NWORDS ]; + } + + inline int RawHeader_v0::GetExpNo() + { + CheckGetBuffer(); + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NO ]) & EXP_MASK) + >> EXP_SHIFT); + } + + + inline int RawHeader_v0::GetRunNo() + { + CheckGetBuffer(); + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NO ]) & RUNNO_MASK) + >> RUNNO_SHIFT); + } + + inline int RawHeader_v0::GetSubRunNo() + { + CheckGetBuffer(); + return (m_buffer[ POS_EXP_RUN_NO ] & SUBRUNNO_MASK); + } + + inline unsigned int RawHeader_v0::GetExpRunSubrun() + { + CheckGetBuffer(); + return ((unsigned int)(m_buffer[ POS_EXP_RUN_NO ])); + } + + + inline unsigned int RawHeader_v0::GetEveNo() + { + CheckGetBuffer(); + return m_buffer[ POS_EVE_NO ]; + } + + inline unsigned int RawHeader_v0::GetNodeID() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_NODE_ID ]); + } + + inline int RawHeader_v0::GetDataType() + { + CheckGetBuffer(); + return (m_buffer[ POS_TRUNC_MASK_DATATYPE ] & 0x7FFFFFFF); + } + + inline int RawHeader_v0::GetTruncMask() + { + CheckGetBuffer(); + return (m_buffer[ POS_TRUNC_MASK_DATATYPE ] >> 23) & 0x1; + } + + inline unsigned int RawHeader_v0::GetErrorBitFlag() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_TRUNC_MASK_DATATYPE ]); + } + + inline void RawHeader_v0::AddErrorBitFlag(unsigned int error_bit_flag) + { + CheckGetBuffer(); + m_buffer[ POS_TRUNC_MASK_DATATYPE ] |= (int)error_bit_flag; + return; + } + + inline int RawHeader_v0::GetOffset1stFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_1ST_FINESSE ]; + } + + inline int RawHeader_v0::GetOffset2ndFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_2ND_FINESSE ]; + } + + inline int RawHeader_v0::GetOffset3rdFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_3RD_FINESSE ]; + } + + inline int RawHeader_v0::GetOffset4thFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_4TH_FINESSE ]; + } + + inline int RawHeader_v0::GetNumNodes() + { + CheckGetBuffer(); + return m_buffer[ POS_NUM_NODES ]; + } + + inline unsigned int RawHeader_v0::GetTTCtimeTRGType() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_TTCTIME_TRGTYPE ]); + } + + inline int RawHeader_v0::GetTTCtime() + { + CheckGetBuffer(); + return (int)((GetTTCtimeTRGType() & TTCTIME_MASK) >> TTCTIME_SHIFT); + } + + inline int RawHeader_v0::GetTRGType() + { + CheckGetBuffer(); + return (int)(GetTTCtimeTRGType() & TRGTYPE_MASK); + } + + + inline unsigned int RawHeader_v0::GetTTUtime() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_TTUTIME ]); + } + + inline void RawHeader_v0::GetTTTimeVal(struct timeval* tv) + { + tv->tv_sec = GetTTUtime(); + tv->tv_usec = (int)(((double)GetTTCtime()) / 127.216); + return ; + } + + + inline unsigned int RawHeader_v0::GetMagicWordEntireHeader() + { + CheckGetBuffer(); + return m_buffer[ POS_TERM_HEADER ]; + } + + + + + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawHeader_v1.h b/ROPCApplications/rawdata/dataobjects/include/RawHeader_v1.h new file mode 100644 index 0000000000000000000000000000000000000000..1d08da328fecf48a8a25c0ebcc154c3b01d609fc --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawHeader_v1.h @@ -0,0 +1,491 @@ +//+ +// File : RawHeader_v1.h +// Description : Module to handle RawHeader_v1 attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWHEADER_V1_H +#define RAWHEADER_V1_H + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +//#include <TObject.h> +#include <rawdata/switch_basf2_standalone.h> + +//#include <framework/datastore/DataStore.h> + + +//#define HEADER_SIZE 16 + +namespace Belle2 { + + /** + * The Raw Header class ver.1 ( the latest version since May, 2014 ) + * This class defines the format of the header of RawCOPPER class data + * and used for extracting header info from RawCOPPER object + */ + + class RawHeader_v1 { + // class RawHeader_v1 : public TObject { + public: + //! Default constructor + RawHeader_v1(); + + //! Constructor using existing pointer to raw data buffer + RawHeader_v1(int*); + + //! Destructor + ~RawHeader_v1(); + + //! Get header contents + int* GetBuffer() { return m_buffer; } + + //! set buffer + void SetBuffer(int* bufin) { m_buffer = bufin; } + + //! initialize header + void CheckSetBuffer(); + + //! check if m_buffer exists + void CheckGetBuffer(); + + //! set contents of header + void SetNwords(int nwords); + + //! set contents of header + void SetEveNo(unsigned int eve_no); + + //! set contents of header + void SetNodeID(unsigned int node_id); + + //! set contents of header + void SetDataType(int data_type); + + //! set contents of header + void SetTruncMask(int trunc_mask); + + // void SetB2LFEEHdrPart(unsigned int word1, unsigned int word2); //! set contents of header + // void SetFTSW2Words(int* ftsw_buf); + + /// Set values of FTSW info( trigger timing) + void SetFTSW2Words(unsigned int word1, unsigned int word2); + + /// Set a word consists of exp #, run # and subrun # + void SetExpRunNumber(int* exprun_buf); + + //! set contents of header + void SetOffset1stFINESSE(int offset_1st_FINESSE); //! set contents of header + + void SetOffset2ndFINESSE(int offset_2nd_FINESSE); //! set contents of header + + void SetOffset3rdFINESSE(int offset_3rd_FINESSE); //! set contents of header + + void SetOffset4thFINESSE(int offset_4th_FINESSE); //! set contents of header + + // void SetMagicWordEntireHeader(); //! set magic words; + + /// Add nodeinfo in trace area + // int AddNodeInfo(int node_id); + + int GetNwords(); //! get contents of header + + int GetHdrNwords(); //! get contents of header + + unsigned int GetExpRunSubrun(); //! get a run/exp number combined word + + int GetExpNo(); //! get contents of header + + int GetRunNo(); //! get run # (14bit) + + int GetSubRunNo(); //! get restart #(8bit) + + unsigned int GetEveNo(); //! get contents of header + + unsigned int GetNodeID(); //! get contents of header + + int GetDataType(); //! get contents of header + + int GetTruncMask(); //! get contents of header + + //! Get Detected Error bitflag + unsigned int GetErrorBitFlag(); + + //! Add Detected Error bitflag + void AddErrorBitFlag(unsigned int error_bit_flag); + + //! get contents of header + int GetOffset1stFINESSE(); + + //! get contents of header + int GetOffset2ndFINESSE(); + + //! get contents of header + int GetOffset3rdFINESSE(); + + //! get contents of header + int GetOffset4thFINESSE(); + + // int GetNumNodes(); //! get contents of header + + // int GetNodeInfo(int node_no, int* node_id); //! get contents of header + + //! get contents of header + unsigned int GetTTCtimeTRGType(); + + //! get contents of header + int GetTTCtime(); + + //! get TRGType + int GetTRGType(); + + //! get contents of header + unsigned int GetTTUtime(); + + //! get contents of header + void GetTTTimeVal(struct timeval* tv); + + /// Get magic word to check the data corruption + // unsigned int GetMagicWordEntireHeader(); + + //! check the contents of header + void CheckHeader(int* buf); + + /* + Experimental(10bit) #, Run#(14bit), restat# bit mask(8bit) + */ + enum { + MAGIC_WORD = 0x7F7F0000, + MAGIC_MASK = 0xFFFF0000, + MAGIC_SHIFT = 16, + FORMAT_VERSION__MASK = 0x0000FF00, + FORMAT_VERSION_SHIFT = 8 + }; + + enum { + EXP_MASK = 0xFFC00000, + EXP_SHIFT = 22, + RUNNO_MASK = 0x003FFF00, + RUNNO_SHIFT = 8, + SUBRUNNO_MASK = 0x000000FF + }; + + enum { + RAWHEADER_NWORDS = 12 + }; + + enum { + HDR_NWORDS_MASK = 0x000000FF + }; + + /* Data Format : Fixed length part*/ + enum { + POS_NWORDS = 0, + POS_VERSION_HDRNWORDS = 1, + POS_EXP_RUN_NO = 2, + POS_EVE_NO = 3, + POS_TTCTIME_TRGTYPE = 4, + POS_TTUTIME = 5, + POS_NODE_ID = 6, + POS_TRUNC_MASK_DATATYPE = 7, + POS_OFFSET_1ST_FINESSE = 8, + POS_OFFSET_2ND_FINESSE = 9, + POS_OFFSET_3RD_FINESSE = 10, + POS_OFFSET_4TH_FINESSE = 11, + }; + /* Data Format : Node info */ + /* enum { */ + /* NUM_MAX_NODES = 4 /\* (NUM_MAX_NODES + 1) slots are available in m_buffer. */ + /* (NUM_MAX_NODES +1 )th slot is filled with -1, when the number of total nodes */ + /* exceeds NUM_MAX_NODES *\/ */ + /* }; */ + + /* /\* Data Format : Magic word *\/ */ + /* enum { */ + /* MAGIC_WORD_TERM_HEADER = 0x7fff0005 */ + /* }; */ + + /* To extract ctime */ + enum { + TTCTIME_MASK = 0x7FFFFFF0, + TTCTIME_SHIFT = 4, + TRGTYPE_MASK = 0xF + }; + + private: + //! do not record buffer ( RawCOPPER includes buffer of RawHeader_v1 and RawTrailer ) + int* m_buffer; //! do not record + + /// To derive from TObject + // ver.2 Do not record m_buffer pointer. (Dec.19, 2014) + // ClassDef(RawHeader_v1, 1); + }; + + + inline void RawHeader_v1::CheckSetBuffer() + { + if (m_buffer == NULL) { + B2FATAL("m_buffer is NULL. Exiting..."); + } + } + + inline void RawHeader_v1::CheckGetBuffer() + { + + if (m_buffer == NULL) { + B2FATAL("m_buffer is NULL. Data is corrupted or header info has not yet filled. Exiting..."); +// } else if (m_buffer[ POS_TERM_HEADER ] != MAGIC_WORD_TERM_HEADER) { */ +// B2FATAL("[DEBUG] magic word is invalid(0x" << hex() << m_buffer[ POS_TERM_HEADER ] << "). Data is corrupted or header info has not yet filled. Exiting..."); + } + + + + } + + inline void RawHeader_v1::SetNwords(int nwords) + { + CheckSetBuffer(); + m_buffer[ POS_NWORDS ] = nwords; + + } + + inline void RawHeader_v1::SetEveNo(unsigned int eve_no) + { + CheckSetBuffer(); + m_buffer[ POS_EVE_NO ] = eve_no; + } + + inline void RawHeader_v1::SetNodeID(unsigned int node_id) + { + CheckSetBuffer(); + m_buffer[ POS_NODE_ID ] = (int)node_id; + } + + inline void RawHeader_v1::SetDataType(int data_type) + { + CheckSetBuffer(); + m_buffer[ POS_TRUNC_MASK_DATATYPE ] = + (data_type & 0x7FFFFFFF) | (m_buffer[ POS_TRUNC_MASK_DATATYPE ] & 0x80000000); + } + + inline void RawHeader_v1::SetTruncMask(int trunc_mask) + { + CheckSetBuffer(); + m_buffer[ POS_TRUNC_MASK_DATATYPE ] = (trunc_mask << 31) | (m_buffer[ POS_TRUNC_MASK_DATATYPE ] & 0x7FFFFFFF); + } + + /* inline void RawHeader_v1::SetB2LFEEHdrPart(unsigned int word1, unsigned int word2) */ + /* { */ + /* m_buffer[ POS_HSLB_1 ] = word1; */ + /* m_buffer[ POS_HSLB_2 ] = word2; */ + /* } */ + + + inline void RawHeader_v1::SetOffset1stFINESSE(int offset_1st_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_1ST_FINESSE ] = offset_1st_FINESSE; + } + + inline void RawHeader_v1::SetOffset2ndFINESSE(int offset_2nd_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_2ND_FINESSE ] = offset_2nd_FINESSE; + } + + inline void RawHeader_v1::SetOffset3rdFINESSE(int offset_3rd_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_3RD_FINESSE ] = offset_3rd_FINESSE; + } + + inline void RawHeader_v1::SetOffset4thFINESSE(int offset_4th_FINESSE) + { + CheckSetBuffer(); + m_buffer[ POS_OFFSET_4TH_FINESSE ] = offset_4th_FINESSE; + } + + /* inline void RawHeader_v1::SetFTSW2Words(int* ftsw_buf) */ + /* { */ + /* CheckSetBuffer(); */ + /* memcpy(&(m_buffer[ POS_HSLB_1 ]), (char*)ftsw_buf, sizeof(int) * 2); */ + /* return; */ + /* } */ + + inline void RawHeader_v1::SetFTSW2Words(unsigned int word1, + unsigned int word2) + { + CheckSetBuffer(); + m_buffer[ POS_TTCTIME_TRGTYPE ] = word1; + m_buffer[ POS_TTUTIME ] = word2; + return; + } + + + inline void RawHeader_v1::SetExpRunNumber(int* exprun_buf) + { + CheckSetBuffer(); + memcpy(&(m_buffer[ POS_EXP_RUN_NO ]), (char*)exprun_buf, sizeof(int) * 1); + return; + } + + +// +// Obtain info +// + + inline int RawHeader_v1::GetNwords() + { + CheckGetBuffer(); + return m_buffer[ POS_NWORDS ]; + } + + inline int RawHeader_v1::GetHdrNwords() + { + + // CheckGetBuffer(); + // return m_buffer[ POS_HDR_NWORDS ]; + return RAWHEADER_NWORDS; + } + + inline int RawHeader_v1::GetExpNo() + { + CheckGetBuffer(); + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NO ]) & EXP_MASK) + >> EXP_SHIFT); + } + + inline int RawHeader_v1::GetRunNo() + { + CheckGetBuffer(); + return (((unsigned int)(m_buffer[ POS_EXP_RUN_NO ]) & RUNNO_MASK) + >> RUNNO_SHIFT); + } + + inline int RawHeader_v1::GetSubRunNo() + { + CheckGetBuffer(); + return (m_buffer[ POS_EXP_RUN_NO ] & SUBRUNNO_MASK); + } + + inline unsigned int RawHeader_v1::GetExpRunSubrun() + { + CheckGetBuffer(); + return ((unsigned int)(m_buffer[ POS_EXP_RUN_NO ])); + } + + + inline unsigned int RawHeader_v1::GetEveNo() + { + CheckGetBuffer(); + return m_buffer[ POS_EVE_NO ]; + } + + inline unsigned int RawHeader_v1::GetNodeID() + { + CheckGetBuffer(); + return (unsigned int)m_buffer[ POS_NODE_ID ]; + } + + inline int RawHeader_v1::GetDataType() + { + CheckGetBuffer(); + return (m_buffer[ POS_TRUNC_MASK_DATATYPE ] & 0x7FFFFFFF); + } + + inline int RawHeader_v1::GetTruncMask() + { + CheckGetBuffer(); + return (m_buffer[ POS_TRUNC_MASK_DATATYPE ] >> 23) & 0x1; + } + + inline unsigned int RawHeader_v1::GetErrorBitFlag() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_TRUNC_MASK_DATATYPE ]); + } + + inline void RawHeader_v1::AddErrorBitFlag(unsigned int error_bit_flag) + { + CheckGetBuffer(); + m_buffer[ POS_TRUNC_MASK_DATATYPE ] |= (int)error_bit_flag; + return; + } + + inline int RawHeader_v1::GetOffset1stFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_1ST_FINESSE ]; + } + + inline int RawHeader_v1::GetOffset2ndFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_2ND_FINESSE ]; + } + + inline int RawHeader_v1::GetOffset3rdFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_3RD_FINESSE ]; + } + + inline int RawHeader_v1::GetOffset4thFINESSE() + { + CheckGetBuffer(); + return m_buffer[ POS_OFFSET_4TH_FINESSE ]; + } + + /* inline int RawHeader_v1::GetNumNodes() */ + /* { */ + /* CheckGetBuffer(); */ + /* return m_buffer[ POS_NUM_NODES ]; */ + /* } */ + + inline unsigned int RawHeader_v1::GetTTCtimeTRGType() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_TTCTIME_TRGTYPE ]); + } + + inline int RawHeader_v1::GetTTCtime() + { + CheckGetBuffer(); + return (int)((GetTTCtimeTRGType() & TTCTIME_MASK) >> TTCTIME_SHIFT); + } + + inline int RawHeader_v1::GetTRGType() + { + CheckGetBuffer(); + return (int)(GetTTCtimeTRGType() & TRGTYPE_MASK); + } + + inline unsigned int RawHeader_v1::GetTTUtime() + { + CheckGetBuffer(); + return (unsigned int)(m_buffer[ POS_TTUTIME ]); + } + + inline void RawHeader_v1::GetTTTimeVal(struct timeval* tv) + { + tv->tv_sec = GetTTUtime(); + tv->tv_usec = (int)(((double)GetTTCtime()) / 127.216); + return ; + } + + + /* inline unsigned int RawHeader_v1::GetMagicWordEntireHeader() */ + /* { */ + /* CheckGetBuffer(); */ + /* return m_buffer[ POS_TERM_HEADER ]; */ + /* } */ + + + + + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawKLM.h b/ROPCApplications/rawdata/dataobjects/include/RawKLM.h new file mode 100644 index 0000000000000000000000000000000000000000..8c88c6430fb2511951476a8e1dd732acdbcf0e56 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawKLM.h @@ -0,0 +1,39 @@ +//+ +// File : RawCOPPER.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWKLM_H +#define RAWKLM_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <rawdata/dataobjects/RawCOPPER.h> +#include <framework/datastore/DataStore.h> + +#include <TObject.h> + +namespace Belle2 { + + /** + * The Raw KLM class + * Class for RawCOPPER class data taken by KLM. + * Currently, this class is almost same as RawCOPPER class. + */ + + class RawKLM : public RawCOPPER { + public: + //! Default constructor + RawKLM(); + //! Destructor + virtual ~RawKLM(); + /// To derive from TObject + ClassDef(RawKLM, 1); + }; +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawPXD.h b/ROPCApplications/rawdata/dataobjects/include/RawPXD.h new file mode 100644 index 0000000000000000000000000000000000000000..fa4277a218b6ba94a8b74b522f933cf3f671e0b5 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawPXD.h @@ -0,0 +1,68 @@ +//+ +// File : RawPXD.h +// Description : pseudo raw data of PXD for test +// +// Author : Bjoern Spruck / Klemens Lautenbach +// Date : 13 - Jul - 2013 +// Updated 17.7.2015 +//- + +#ifndef RAWPXD_H +#define RAWPXD_H + +#include <TObject.h> + +namespace Belle2 { + + /** + * The Raw PXD class. + * + * This class stores the RAW data received from the ONSEN system. + * This data is then decoded by the pxdUnpacker module. + * For system simulation, pxdPacker writes out in this objects. + */ + class RawPXD : public TObject { + public: + + //! Default constructor + RawPXD(); + + //! Constructor using existing pointer to raw data buffer which needs to be _copied_ + RawPXD(int*, int); + + //! Constructor using existing data which needs to be _copied_ to a new raw data buffer, only used by PXDPacker + RawPXD(const std::vector <unsigned int>& header, const std::vector <std::vector <unsigned char>>& payload); + + //! Destructor + virtual ~RawPXD(); + + //! get size of buffer in 32 Bit words + virtual int size() const; + /*at the moment not used + //! allocate buffer + virtual int* allocate_buffer(int nwords); + + //! set data size in 32 Bit words + virtual void data(int nwords, int*); + */ + //! get pointer to data + virtual int* data(void); + + /** Return a short summary of this object's contents in HTML format. */ + std::string getInfoHTML() const; + + private: + int m_nwords;/**< Number of (32bit) Words stored in the buffer. There might be unsused bytes in the last word.*/ + /// Raw dump of ONSEN data. buffer of size m_nwords (32bit int) + int* m_buffer; //[m_nwords] /**< Buffer of size m_nwords */ + + //! Endian swap a int32 + static unsigned int endian_swap(unsigned int x); + + ClassDef(RawPXD, 2) + }; // class RawPXD + + +} // end namespace Belle2 + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawSVD.h b/ROPCApplications/rawdata/dataobjects/include/RawSVD.h new file mode 100644 index 0000000000000000000000000000000000000000..477710d1f28c3c2a60a923794d404b1a56c900f7 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawSVD.h @@ -0,0 +1,38 @@ +//+ +// File : RawCOPPER.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWSVD_H +#define RAWSVD_H + +// Includes +#include <stdio.h> +#include <stdlib.h> +#include <rawdata/dataobjects/RawCOPPER.h> +#include <framework/datastore/DataStore.h> + +#include <TObject.h> + +namespace Belle2 { + + /** + * The Raw SVD class + * Class for RawCOPPER class data taken by SVD + * Currently, this class is almost same as RawCOPPER class. + */ + class RawSVD : public RawCOPPER { + public: + //! Default constructor + RawSVD(); + //! Destructor + virtual ~RawSVD(); + /// To derive from TObject + ClassDef(RawSVD, 1); + }; +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawTLU.h b/ROPCApplications/rawdata/dataobjects/include/RawTLU.h new file mode 100644 index 0000000000000000000000000000000000000000..d56aed58078127202d7acfa2ed080e708643f1bd --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawTLU.h @@ -0,0 +1,101 @@ +//+ +// File : RawTLU.h +// Description : Module to handle raw data from TLU(for the telescope beam test) +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Jan - 2014 +//- + +#ifndef RAWTLU_H +#define RAWTLU_H + +#include <stdio.h> +#include <stdlib.h> +#include <sys/time.h> + +#include <rawdata/dataobjects/RawDataBlock.h> +#include <rawdata/dataobjects/RawTLUFormat.h> +#include <framework/datastore/DataStore.h> + +#include <TObject.h> + +namespace Belle2 { + + /** + * The Raw TLU class + * Class for data from DAQ PC for TLU(Trigger Logic Unit) + * It is supposed to be used only in the DESY beam test + */ + + class RawTLU : public RawDataBlock { + public: + //! Default constructor + RawTLU(); + + //! Destructor + virtual ~RawTLU(); + + /* //! Get # of words in this data packet */ + /* int GetNwords(int n); */ + + //!Get the size of the header + int GetNwordsHeader(int n) + { + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access->GetNwordsHeader(n); + } + + //! Get Node ID + unsigned int GetNodeID(int n) + { + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access->GetNodeID(n); + } + + //! Get Event # + unsigned int GetEveNo(int n) + { + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access->GetEveNo(n); + } + + //! Get Magic Trailer # + unsigned int GetMagicTrailer(int n) + { + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access->GetMagicTrailer(n); + } + + //! Get Run # + int GetRunNo(int n) + { + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access->GetRunNo(n); + } + + //! Get TLU event tag + unsigned int GetTLUEventTag(int n) + { + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access->GetTLUEventTag(n); + } + + //! check data contents + void CheckData(int n, unsigned int prev_evenum, unsigned int* cur_evenum) + { + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + return m_access->CheckData(n, prev_evenum, cur_evenum); + } + + //! class to access + RawTLUFormat* m_access; //! do not record + + protected : + + /// To derive from TObject + ClassDef(RawTLU, 1); + + }; +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawTLUFormat.h b/ROPCApplications/rawdata/dataobjects/include/RawTLUFormat.h new file mode 100644 index 0000000000000000000000000000000000000000..cb399840f77c34345dd02098a3eef5d76b8a2b50 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawTLUFormat.h @@ -0,0 +1,83 @@ +//+ +// File : RawTLUFormat.h +// Description : Module to handle raw data from TLU(for the telescope beam test) +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Jan - 2014 +//- + +#ifndef RAWTLUFORMAT_H +#define RAWTLUFORMAT_H +#include <rawdata/dataobjects/RawDataBlockFormat.h> + +namespace Belle2 { + + /** + * The Raw TLU class + * Class for data from DAQ PC for TLU(Trigger Logic Unit) + * It is supposed to be used only in the DESY beam test + */ + + class RawTLUFormat : public RawDataBlockFormat { + public: + //! Default constructor + RawTLUFormat(); + + //! Destructor + virtual ~RawTLUFormat(); + + /* //! Get # of words in this data packet */ + /* int GetNwords(int n); */ + + //!Get the size of the header + int GetNwordsHeader(int n); + + //! Get Node ID + unsigned int GetNodeID(int n); + + //! Get Event # + unsigned int GetEveNo(int n); + + //! Get Magic Trailer # + unsigned int GetMagicTrailer(int n); + + //! Get Run # + int GetRunNo(int n); + + //! Get TLU event tag + unsigned int GetTLUEventTag(int n); + + //! check data contents + void CheckData(int n, unsigned int prev_evenum, unsigned int* cur_evenum); + + + enum { + POS_NWORDS = 0, + POS_HDR_NWORDS = 1, + POS_NUMEVE_NUMNODES = 2, + POS_RUN_NO = 3, + POS_EVE_NO = 4, + POS_NA_1 = 5, + POS_NODE_ID = 6, + POS_NA_2 = 7, + POS_TLU_EVENTTAG = 8, + POS_NA_3 = 9, + POS_TLU_TIME_1 = 10, + POS_TLU_TIME_2 = 11, + POS_RSVD_1 = 12, + POS_MAGIC_1 = 13 + }; + + enum { + SIZE_TLU_PACKET = 14 + }; + + enum { + TLU_MAGIC_TRAILER = 0x7FFF0000 + }; + + protected : + }; +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawTOP.h b/ROPCApplications/rawdata/dataobjects/include/RawTOP.h new file mode 100644 index 0000000000000000000000000000000000000000..d07d8b11725f0c72ba0f3d9cb05abfff761c4274 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawTOP.h @@ -0,0 +1,41 @@ +//+ +// File : RawCOPPER.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWTOP_H +#define RAWTOP_H + +// Includes +#include <stdio.h> +#include <stdlib.h> + +#include <rawdata/dataobjects/RawCOPPER.h> +#include <framework/datastore/DataStore.h> + +#include <TObject.h> + +namespace Belle2 { + + /** + * The Raw TOP class + * Class for RawCOPPER class data taken by TOP + * Currently, this class is almost same as RawCOPPER class. + */ + + class RawTOP : public RawCOPPER { + public: + //! Default constructor + RawTOP(); + //! Destructor + virtual ~RawTOP(); + + /// To derive from TObject + ClassDef(RawTOP, 1); + }; +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawTRG.h b/ROPCApplications/rawdata/dataobjects/include/RawTRG.h new file mode 100644 index 0000000000000000000000000000000000000000..795b3c125657148b53ff562db46ed7ec3ede202c --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawTRG.h @@ -0,0 +1,41 @@ +//+ +// File : RawTRG.h +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWTRG_H +#define RAWTRG_H + +// Includes +#include <stdio.h> +#include <stdlib.h> + +#include <rawdata/dataobjects/RawCOPPER.h> +#include <framework/datastore/DataStore.h> + +#include <TObject.h> + +namespace Belle2 { + + /** + * The Raw TOP class + * Class for RawCOPPER class data taken by TOP + * Currently, this class is almost same as RawCOPPER class. + */ + + class RawTRG : public RawCOPPER { + public: + //! Default constructor + RawTRG(); + //! Destructor + virtual ~RawTRG(); + + /// To derive from TObject + ClassDef(RawTRG, 1); + }; +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawTrailer_latest.h b/ROPCApplications/rawdata/dataobjects/include/RawTrailer_latest.h new file mode 100644 index 0000000000000000000000000000000000000000..0c7456c15d745a0ec2c13d80e0b9653aa1088cd0 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawTrailer_latest.h @@ -0,0 +1,140 @@ +//+ +// File : RawTrailer_latest.cc +// Description : Module to handle RawTraeiler attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWTRAILER_LATEST_H +#define RAWTRAILER_LATEST_H + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +/* #include <framework/datastore/DataStore.h> */ +/* #include <TObject.h> */ +#include <rawdata/switch_basf2_standalone.h> + +//#define TRAILER_SIZE 16 +/* #define POS_CHKSUM 0 */ +/* #define POS_TERM_WORD 1 */ +/* #define RAWCOPPER_TRAILER_SIZE 2 */ +/* #define MAGIC_WORD_TERM_TRAILER 0x7fff0006 */ + +namespace Belle2 { + + /** + * The Raw Trailer class ver.1 ( the latest version since May, 2014 ) + * This class defines the format of the trailer of RawCOPPER class data + * and used for extracting trailer info from RawCOPPER object + */ + // class RawTrailer_latest : public TObject { + class RawTrailer_latest { + public: + //! Default constructor + RawTrailer_latest(); + + //! Destructor + ~RawTrailer_latest(); + + //! set buffer + void CheckBuffer(); //! set buffer + + int* GetBuffer(); //! return buffer + + void SetBuffer(int* bufin); //! set buffer + + void Initialize(); //! set buffer + + void SetChksum(int chksum); //! initialize header + + void SetMagicWord(); //! Set magic word + + int GetTrlNwords(); //! Set # of trailer words + + unsigned int GetChksum(); //! initialize header + + int GetMagicWord(); //! Get magic word + + enum { + RAWTRAILER_NWORDS = 2 + }; + + enum { + MAGIC_WORD_TERM_TRAILER = 0x7fff0006 + }; + + enum { + POS_CHKSUM = 0, + POS_TERM_WORD = 1 + }; + + private: + //! do not record buffer ( RawCOPPER includes buffer of RawHeader and RawTrailer_latest ) + int* m_buffer; //! do not record + + /// To derive from TObject + // ver.2 Do not record m_buffer pointer. (Dec.19, 2014) + // ClassDef(RawTrailer_latest, 1); + }; + + + inline void RawTrailer_latest::CheckBuffer() + { + if (m_buffer == NULL) { + B2FATAL("m_buffer is NULL. Exiting..."); + } + } + + inline int* RawTrailer_latest::GetBuffer() + { + return m_buffer; + } + + inline void RawTrailer_latest::SetBuffer(int* bufin) + { + m_buffer = bufin; + } + + inline void RawTrailer_latest::Initialize() + { + SetMagicWord(); + } + + inline void RawTrailer_latest::SetChksum(int chksum) + { + CheckBuffer(); + m_buffer[ POS_CHKSUM ] = chksum; + } + + inline void RawTrailer_latest::SetMagicWord() + { + CheckBuffer(); + m_buffer[ POS_TERM_WORD ] = MAGIC_WORD_TERM_TRAILER; + } + + inline int RawTrailer_latest::GetMagicWord() + { + CheckBuffer(); + return m_buffer[ POS_TERM_WORD ]; + } + + inline unsigned int RawTrailer_latest::GetChksum() + { + CheckBuffer(); + return (unsigned int)(m_buffer[ POS_CHKSUM ]); + } + + inline int RawTrailer_latest::GetTrlNwords() + { + return RAWTRAILER_NWORDS; + } + + + + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawTrailer_v0.h b/ROPCApplications/rawdata/dataobjects/include/RawTrailer_v0.h new file mode 100644 index 0000000000000000000000000000000000000000..c5ce476fa7f42845aa8d3325064a640206d11f50 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawTrailer_v0.h @@ -0,0 +1,137 @@ +//+ +// File : RawTrailer_v0.cc +// Description : Module to handle RawTraeiler attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWTRAILER_V0_H +#define RAWTRAILER_V0_H + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <rawdata/switch_basf2_standalone.h> + +/* #include <framework/datastore/DataStore.h> */ +/* #include <TObject.h> */ + +//#define TRAILER_SIZE 16 +/* #define POS_CHKSUM 0 */ +/* #define POS_TERM_WORD 1 */ +/* #define RAWCOPPER_TRAILER_SIZE 2 */ +/* #define MAGIC_WORD_TERM_TRAILER 0x7fff0006 */ + +namespace Belle2 { + + /** + * The Raw Trailer class ver.0 ( from August, 2013 to April, 2014 ) + * This class defines the format of the trailer of RawCOPPER class data + * and used for extracting trailer info from RawCOPPER object + */ + // class RawTrailer_v0 : public TObject { + class RawTrailer_v0 { + public: + RawTrailer_v0(); /// Default constructor + + ~RawTrailer_v0(); /// Destructor + + void CheckBuffer(); //! set buffer + + int* GetBuffer(); //! return buffer + + void SetBuffer(int* bufin); //! set buffer + + void Initialize(); //! set buffer + + void SetChksum(int chksum); //! initialize header + + void SetMagicWord(); //! Set magic word + + int GetTrlNwords(); //! Set # of trailer words + + unsigned int GetChksum(); //! initialize header + + int GetMagicWord(); //! Get magic word + + enum { + RAWTRAILER_NWORDS = 2 + }; + + enum { + MAGIC_WORD_TERM_TRAILER = 0x7fff0006 + }; + + enum { + POS_CHKSUM = 0, + POS_TERM_WORD = 1 + }; + + private: + //! do not record buffer ( RawCOPPER includes buffer of RawHeader and RawTrailer_v0 ) + int* m_buffer; //! do not record + + /// To derive from TObject + // ver.2 Do not record m_buffer pointer. (Dec.19, 2014) + // ClassDef(RawTrailer_v0, 2); + }; + + + inline void RawTrailer_v0::CheckBuffer() + { + if (m_buffer == NULL) { + B2FATAL("m_buffer is NULL. Exiting..."); + } + } + + inline int* RawTrailer_v0::GetBuffer() + { + return m_buffer; + } + + inline void RawTrailer_v0::SetBuffer(int* bufin) + { + m_buffer = bufin; + } + + inline void RawTrailer_v0::Initialize() + { + SetMagicWord(); + } + + inline void RawTrailer_v0::SetChksum(int chksum) + { + CheckBuffer(); + m_buffer[ POS_CHKSUM ] = chksum; + } + + inline void RawTrailer_v0::SetMagicWord() + { + CheckBuffer(); + m_buffer[ POS_TERM_WORD ] = MAGIC_WORD_TERM_TRAILER; + } + + inline int RawTrailer_v0::GetMagicWord() + { + CheckBuffer(); + return m_buffer[ POS_TERM_WORD ]; + } + + inline unsigned int RawTrailer_v0::GetChksum() + { + CheckBuffer(); + return (unsigned int)(m_buffer[ POS_CHKSUM ]); + } + + inline int RawTrailer_v0::GetTrlNwords() + { + return RAWTRAILER_NWORDS; + } + + + + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/RawTrailer_v1.h b/ROPCApplications/rawdata/dataobjects/include/RawTrailer_v1.h new file mode 100644 index 0000000000000000000000000000000000000000..f9d31ff1569b478713dda705ea9b1f5b64532e2d --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/RawTrailer_v1.h @@ -0,0 +1,140 @@ +//+ +// File : RawTrailer_v1.cc +// Description : Module to handle RawTraeiler attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#ifndef RAWTRAILER_V1_H +#define RAWTRAILER_V1_H + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <rawdata/switch_basf2_standalone.h> + +/* #include <framework/datastore/DataStore.h> */ +/* #include <TObject.h> */ + +//#define TRAILER_SIZE 16 +/* #define POS_CHKSUM 0 */ +/* #define POS_TERM_WORD 1 */ +/* #define RAWCOPPER_TRAILER_SIZE 2 */ +/* #define MAGIC_WORD_TERM_TRAILER 0x7fff0006 */ + +namespace Belle2 { + + /** + * The Raw Trailer class ver.1 ( the latest version since May, 2014 ) + * This class defines the format of the trailer of RawCOPPER class data + * and used for extracting trailer info from RawCOPPER object + */ + // class RawTrailer_v1 : public TObject { + class RawTrailer_v1 { + public: + //! Default constructor + RawTrailer_v1(); + + //! Destructor + ~RawTrailer_v1(); + + //! set buffer + void CheckBuffer(); //! set buffer + + int* GetBuffer(); //! return buffer + + void SetBuffer(int* bufin); //! set buffer + + void Initialize(); //! set buffer + + void SetChksum(int chksum); //! initialize header + + void SetMagicWord(); //! Set magic word + + int GetTrlNwords(); //! Set # of trailer words + + unsigned int GetChksum(); //! initialize header + + int GetMagicWord(); //! Get magic word + + enum { + RAWTRAILER_NWORDS = 2 + }; + + enum { + MAGIC_WORD_TERM_TRAILER = 0x7fff0006 + }; + + enum { + POS_CHKSUM = 0, + POS_TERM_WORD = 1 + }; + + private: + //! do not record buffer ( RawCOPPER includes buffer of RawHeader and RawTrailer_v1 ) + int* m_buffer; //! do not record + + /// To derive from TObject + // ver.2 Do not record m_buffer pointer. (Dec.19, 2014) + // ClassDef(RawTrailer_v1, 1); + }; + + + inline void RawTrailer_v1::CheckBuffer() + { + if (m_buffer == NULL) { + B2FATAL("m_buffer is NULL. Exiting..."); + } + } + + inline int* RawTrailer_v1::GetBuffer() + { + return m_buffer; + } + + inline void RawTrailer_v1::SetBuffer(int* bufin) + { + m_buffer = bufin; + } + + inline void RawTrailer_v1::Initialize() + { + SetMagicWord(); + } + + inline void RawTrailer_v1::SetChksum(int chksum) + { + CheckBuffer(); + m_buffer[ POS_CHKSUM ] = chksum; + } + + inline void RawTrailer_v1::SetMagicWord() + { + CheckBuffer(); + m_buffer[ POS_TERM_WORD ] = MAGIC_WORD_TERM_TRAILER; + } + + inline int RawTrailer_v1::GetMagicWord() + { + CheckBuffer(); + return m_buffer[ POS_TERM_WORD ]; + } + + inline unsigned int RawTrailer_v1::GetChksum() + { + CheckBuffer(); + return (unsigned int)(m_buffer[ POS_CHKSUM ]); + } + + inline int RawTrailer_v1::GetTrlNwords() + { + return RAWTRAILER_NWORDS; + } + + + + +} + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/include/linkdef.h b/ROPCApplications/rawdata/dataobjects/include/linkdef.h new file mode 100644 index 0000000000000000000000000000000000000000..222cce688e3d5ec50d973333dd4556b2dee1a72e --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/include/linkdef.h @@ -0,0 +1,36 @@ +#ifdef __CINT__ + +#pragma link off all globals; +#pragma link off all classes; +#pragma link off all functions; +#pragma link C++ nestedclasses; + +#pragma link C++ class Belle2::RawCOPPERFormat+; +#pragma link C++ class Belle2::RawCOPPERFormat_v0+; +#pragma link C++ class Belle2::RawCOPPERFormat_v1+; +#pragma link C++ class Belle2::RawCOPPERFormat_latest+; +#pragma link C++ class Belle2::PreRawCOPPERFormat_v1+; +#pragma link C++ class Belle2::PreRawCOPPERFormat_latest+; +#pragma link C++ class Belle2::PostRawCOPPERFormat_v1+; +#pragma link C++ class Belle2::PostRawCOPPERFormat_latest+; +#pragma link C++ class Belle2::RawHeader_v0+; +#pragma link C++ class Belle2::RawHeader_v1+; +#pragma link C++ class Belle2::RawHeader_latest+; +#pragma link C++ class Belle2::RawTrailer_v0+; +#pragma link C++ class Belle2::RawTrailer_v1+; +#pragma link C++ class Belle2::RawTrailer_latest+; + +#pragma link C++ class Belle2::RawDataBlock+; +#pragma link C++ class Belle2::RawCOPPER+; +#pragma link C++ class Belle2::RawCDC+; +#pragma link C++ class Belle2::RawSVD+; +#pragma link C++ class Belle2::RawECL+; +#pragma link C++ class Belle2::RawTOP+; +#pragma link C++ class Belle2::RawARICH+; +#pragma link C++ class Belle2::RawKLM+; +#pragma link C++ class Belle2::RawFTSW+; +#pragma link C++ class Belle2::RawTLU+; +#pragma link C++ class Belle2::RawPXD+; +#pragma link C++ class Belle2::RawTRG+; + +#endif diff --git a/ROPCApplications/rawdata/dataobjects/src/PostRawCOPPERFormat_latest.cc b/ROPCApplications/rawdata/dataobjects/src/PostRawCOPPERFormat_latest.cc new file mode 100644 index 0000000000000000000000000000000000000000..0362bc83488a4177ebbc7b13e8374f7c00a8756a --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/PostRawCOPPERFormat_latest.cc @@ -0,0 +1,416 @@ +//+ +// File : RawCOPPERFormat_latest.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/PostRawCOPPERFormat_latest.h> + + +using namespace std; +using namespace Belle2; + +//#define DESY +//#define NO_ERROR_STOP +//ClassImp(PostRawCOPPERFormat_latest); + +PostRawCOPPERFormat_latest::PostRawCOPPERFormat_latest() +{ +} + +PostRawCOPPERFormat_latest::~PostRawCOPPERFormat_latest() +{ +} + + +int PostRawCOPPERFormat_latest::GetDetectorNwords(int n, int finesse_num) +{ + int nwords = 0; + if (GetFINESSENwords(n, finesse_num) > 0) { + nwords = GetFINESSENwords(n, finesse_num) + - (SIZE_B2LHSLB_HEADER + SIZE_B2LHSLB_TRAILER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER); + } + return nwords; +} + + +unsigned int PostRawCOPPERFormat_latest::CalcDriverChkSum(int n) +{ + char err_buf[500]; + sprintf(err_buf, "[FATAL] This function is not supported.(block %d) Exiting...: \n%s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); + B2FATAL(err_buf); + return 0; +} + + +int PostRawCOPPERFormat_latest::GetFINESSENwords(int n, int finesse_num) +{ + int pos_nwords_0, pos_nwords_1; + int nwords; + switch (finesse_num) { + case 0 : + pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_1ST_FINESSE; + pos_nwords_1 = GetBufferPos(n) + tmp_header.POS_OFFSET_2ND_FINESSE; + nwords = m_buffer[ pos_nwords_1 ] - m_buffer[ pos_nwords_0 ]; + break; + case 1 : + pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_2ND_FINESSE; + pos_nwords_1 = GetBufferPos(n) + tmp_header.POS_OFFSET_3RD_FINESSE; + nwords = m_buffer[ pos_nwords_1 ] - m_buffer[ pos_nwords_0 ]; + break; + case 2 : + pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_3RD_FINESSE; + pos_nwords_1 = GetBufferPos(n) + tmp_header.POS_OFFSET_4TH_FINESSE; + nwords = m_buffer[ pos_nwords_1 ] - m_buffer[ pos_nwords_0 ]; + break; + case 3 : + pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_4TH_FINESSE; + { + int nwords_1 = GetBlockNwords(n) + - SIZE_COPPER_DRIVER_TRAILER + - tmp_trailer.GetTrlNwords(); + nwords = nwords_1 - m_buffer[ pos_nwords_0 ]; + } + break; + default : + char err_buf[500]; + sprintf(err_buf, "[FATAL] Invalid finesse # : %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); + B2FATAL(err_buf); + } + + if (nwords < 0 || nwords > 1e6) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : # of words is strange. %d : eve 0x%x exp %d run %d sub %d\n %s %s %d\n", + nwords, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); + B2FATAL(err_buf); + } + + return nwords; + +} + + + + +unsigned int PostRawCOPPERFormat_latest::GetB2LFEE32bitEventNumber(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : No event # in B2LFEE header. (block %d) Exiting... : eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + n, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); + B2FATAL(err_buf); + return 0; +} + + + +void PostRawCOPPERFormat_latest::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum_rawcprhdr, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + char err_buf[500]; + int err_flag = 0; + + // + // Check incrementation of event # + // + *cur_evenum_rawcprhdr = GetEveNo(n); + *cur_exprunsubrun_no = GetExpRunSubrun(n); + + if (prev_exprunsubrun_no == *cur_exprunsubrun_no) { + if ((unsigned int)(prev_evenum + 1) != *cur_evenum_rawcprhdr) { + sprintf(err_buf, "CORRUPTED DATA: Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n%s %s %d\n", + n, prev_evenum, *cur_evenum_rawcprhdr, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + } + + + // + // Check checksum calculated by DeSerializerCOPPER() + // + tmp_trailer.SetBuffer(GetRawTrlBufPtr(n)); + unsigned int xor_chksum = CalcXORChecksum(GetBuffer(n), GetBlockNwords(n) - tmp_trailer.GetTrlNwords()); + if (tmp_trailer.GetChksum() != xor_chksum) { + sprintf(err_buf, + "[FATAL] ERROR_EVENT : checksum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x : eve 0x%x exp %d run %d sub %d\n %s %s %d\n", + n, GetBlockNwords(n), *cur_evenum_rawcprhdr, tmp_trailer.GetChksum(), xor_chksum, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + + + if (err_flag == 1) { + printf("[DEBUG] %s\n", err_buf); + printf("[DEBUG] ========== dump a data blcok : block # %d==========\n", n); + PrintData(GetBuffer(n), GetBlockNwords(n)); + printf("Print out variables to reduce unused-variables-warnings : %u %u\n", prev_copper_ctr, *cur_copper_ctr); + B2FATAL(err_buf); + } + + return; + +} + +bool PostRawCOPPERFormat_latest::CheckCOPPERMagic(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : No magic word # in COPPER header (block %d). Exiting...: eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + n, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); + B2FATAL(err_buf); + return false; +} + +void PostRawCOPPERFormat_latest::CheckUtimeCtimeTRGType(int n) +{ + char err_buf[500]; + sprintf(err_buf, "[FATAL] This function is not supported (block %d). Exiting...\n%s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); + B2FATAL(err_buf); +} + +unsigned int PostRawCOPPERFormat_latest::FillTopBlockRawHeader(unsigned int m_node_id, unsigned int prev_eve32, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) + +{ + char err_buf[500]; + sprintf(err_buf, "[FATAL] This function should be called by PrePostRawCOPPERFormat_***. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("Print out variables to reduce unused-variables-warnings : %u %u %u %u\n", + m_node_id, prev_eve32, prev_exprunsubrun_no, *cur_exprunsubrun_no); + printf("[DEBUG] %s\n", err_buf); + B2FATAL(err_buf); +} + + +int PostRawCOPPERFormat_latest::CheckB2LHSLBMagicWords(int* finesse_buf, int finesse_nwords) +{ + char err_buf[500]; + sprintf(err_buf, "[FATAL] This function should be called by PrePostRawCOPPERFormat_***. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("Print out variables to reduce unused-variables-warnings : %p %d\n", finesse_buf, finesse_nwords); + printf("[DEBUG] %s\n", err_buf); + B2FATAL(err_buf); +} + +int PostRawCOPPERFormat_latest::CheckCRC16(int n, int finesse_num) +{ + + // + // Calculate CRC16 + // + int finesse_nwords = GetFINESSENwords(n, finesse_num); + if (finesse_nwords <= 0) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : The specified finesse(%c) seems to be empty(nwords = %d). Cannot calculate CRC16. Exiting...: eve 0x%x exp %d run %d sub %d\n %s %s %d\n", + 65 + finesse_num, finesse_nwords, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + int* copper_buf = GetBuffer(n); + + + unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, &(copper_buf[ tmp_header.POS_TTCTIME_TRGTYPE ]), 1); + temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(copper_buf[ tmp_header.POS_EVE_NO ]), 1); + temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(copper_buf[ tmp_header.POS_TTUTIME ]), 1); + temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(copper_buf[ tmp_header.POS_EXP_RUN_NO ]), 1); + int* buf = GetFINESSEBuffer(n, finesse_num) + SIZE_B2LHSLB_HEADER + POS_B2L_CTIME; + int pos_nwords = finesse_nwords - (SIZE_B2LHSLB_HEADER + POS_B2L_CTIME + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER); + temp_crc16 = CalcCRC16LittleEndian(temp_crc16, buf, pos_nwords); + + // + // Compare CRC16 with B2LCRC16 + // + buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n, + finesse_num) - ((SIZE_B2LFEE_TRAILER - POS_B2LFEE_ERRCNT_CRC16) + SIZE_B2LHSLB_TRAILER) ; + + if (GetEveNo(n) % 100000 == 0) { + printf("#### PostRawCOPPER : Eve %.8x block %d finesse %d B2LCRC16 %.8x calculated CRC16 %.8x\n", GetEveNo(n), n, finesse_num, + *buf, temp_crc16); + } + + // if ( false ) { + if ((unsigned short)(*buf & 0xFFFF) != temp_crc16) { + + // dump an event + int copper_nwords = copper_buf[ tmp_header.POS_NWORDS ]; + PrintData(copper_buf, copper_nwords); + // Check whether packet-CRC error has occcured or not. + if (copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ] & tmp_header.B2LINK_PACKET_CRC_ERROR) { + // + // Do not stop data + // + char err_buf[500]; + sprintf(err_buf, + "[FATAL] POST B2link event CRC16 error with B2link Packet CRC error. data(%x) calc(%x) fns nwords %d type 0x%.8x : slot%c eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + *buf , temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ], + 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num)); +#ifndef NO_ERROR_STOP + B2FATAL(err_buf); +#endif + } else { + // + // Stop taking data + // + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : POST B2link event CRC16 error without B2link Packet CRC error. data(%x) calc(%x) fns nwords %d type 0x%.8x: slot%c eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + *buf , temp_crc16, GetFINESSENwords(n, finesse_num), copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ], + 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + PrintData(GetFINESSEBuffer(n, finesse_num), GetFINESSENwords(n, finesse_num)); +#ifndef NO_ERROR_STOP + B2FATAL(err_buf); +#endif + } + // Modify XOR checksum due to adding a bit flag + copper_buf[ copper_nwords - tmp_trailer.RAWTRAILER_NWORDS + tmp_trailer.POS_CHKSUM ] + ^= copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ]; + copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_EVENT_CRC_ERROR; + copper_buf[ copper_nwords - tmp_trailer.RAWTRAILER_NWORDS + tmp_trailer.POS_CHKSUM ] + ^= copper_buf[ tmp_header.POS_TRUNC_MASK_DATATYPE ]; + } + + return 1; + + +} + + +int* PostRawCOPPERFormat_latest::PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcpr_info) +{ + int* packed_buf = NULL; + + int poswords_to = 0; + int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th }; + int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th }; + + // calculate the event length + int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords(); + + for (int i = 0; i < 4; i++) { + if (detector_buf[ i ] == NULL || nwords[ i ] <= 0) continue; // for an empty FINESSE slot + length_nwords += nwords[ i ]; + length_nwords += SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + // allocate buffer + packed_buf = new int[ length_nwords ]; + memset(packed_buf, 0, sizeof(int) * length_nwords); + + // + // Fill RawHeader + // + tmp_header.SetBuffer(packed_buf); + + packed_buf[ tmp_header.POS_NWORDS ] = length_nwords; // total length + packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] = + 0x7f7f0000 + | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK) + | tmp_header.RAWHEADER_NWORDS; // ver.#, header length + packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.exp_num << 22) + | (rawcpr_info.run_subrun_num & 0x003FFFFF); // exp. and run # + packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.eve_num; // eve # + packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.tt_ctime & 0x7FFFFFF) << 4; // tt_ctime + packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.tt_utime; // tt_utime + packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.node_id; // node ID + + // fill the positions of finesse buffers + packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + + packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ]; + if (nwords[ 0 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] += + nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ]; + if (nwords[ 1 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] += + nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ]; + if (nwords[ 2 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + + SIZE_B2LHSLB_TRAILER; + } + poswords_to += tmp_header.GetHdrNwords(); + + // Fill COPPER header + poswords_to += SIZE_COPPER_HEADER; + + // Fill FINESSE buffer + for (int i = 0; i < 4; i++) { + + if (detector_buf[ i ] == NULL || nwords[ i ] <= 0) continue; // for an empty FINESSE slot + + // Fill b2link HSLB header + packed_buf[ poswords_to + POS_B2LHSLB_MAGIC ] = 0xffaa0000 | (0xffff & rawcpr_info.eve_num); + poswords_to += SIZE_B2LHSLB_HEADER; + + // Fill b2link FEE header + packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.b2l_ctime & 0x7FFFFFF) << 4; + poswords_to += SIZE_B2LFEE_HEADER; + + // copy the 1st Detector Buffer + memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*sizeof(int)); + poswords_to += nwords[ i ]; + + // Fill b2link FEE trailer + unsigned int crc16 = 0; + packed_buf[ poswords_to + POS_B2LFEE_ERRCNT_CRC16 ] = crc16 & + 0xffff; // Error count is stored in this buffer for ver.2 format but it is set to zero here. + poswords_to += SIZE_B2LFEE_TRAILER; + + // Fill b2link HSLB trailer + poswords_to += SIZE_B2LHSLB_TRAILER; + + } + + // Fill COPPER trailer + poswords_to += SIZE_COPPER_TRAILER; + + // Fill RawTrailer + packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER; + poswords_to += tmp_trailer.GetTrlNwords(); + + *packed_buf_nwords = poswords_to; + + return packed_buf; +} + diff --git a/ROPCApplications/rawdata/dataobjects/src/PostRawCOPPERFormat_v1.cc b/ROPCApplications/rawdata/dataobjects/src/PostRawCOPPERFormat_v1.cc new file mode 100644 index 0000000000000000000000000000000000000000..49dbb6bfa5fe515eb8bfa297843341550b725a4d --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/PostRawCOPPERFormat_v1.cc @@ -0,0 +1,426 @@ +//+ +// File : RawCOPPERFormat_v1.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/PostRawCOPPERFormat_v1.h> + + +using namespace std; +using namespace Belle2; + +//#define DESY +//#define NO_DATA_CHECK +//#define WO_FIRST_EVENUM_CHECK + +//ClassImp(PostRawCOPPERFormat_v1); + +PostRawCOPPERFormat_v1::PostRawCOPPERFormat_v1() +{ +} + +PostRawCOPPERFormat_v1::~PostRawCOPPERFormat_v1() +{ +} + +// int PostRawCOPPERFormat_v1::GetBufferPos(int n) +// { +// if (m_buffer == NULL || m_nwords <= 0) { +// char err_buf[500]; +// sprintf(err_buf, "[DEBUG] [ERROR] RawPacket buffer(%p) is not available or length(%d) is not set.\n %s %s %d\n", +// m_buffer, m_nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__); +// string err_str = err_buf; throw (err_str); +// } + +// if (n >= (m_num_events * m_num_nodes)) { +// char err_buf[500]; +// sprintf(err_buf, "[DEBUG] Invalid COPPER block No. (%d : max %d ) is specified. Exiting... \n %s %s %d\n", +// n, (m_num_events * m_num_nodes), __FILE__, __PRETTY_FUNCTION__, __LINE__); +// string err_str = err_buf; throw (err_str); +// } +// int pos_nwords = 0; + + +// for (int i = 1; i <= n ; i++) { +// tmp_header.SetBuffer(&m_buffer[ pos_nwords ]); +// pos_nwords += tmp_header.GetNwords(); + +// if (pos_nwords >= m_nwords) { +// char err_buf[500]; +// sprintf(err_buf, "CORRUPTED DATA: value of pos_nwords(%d) is larger than m_nwords(%d). Exiting...\n %s %s %d\n", +// pos_nwords, m_nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__); +// string err_str = err_buf; throw (err_str); +// // exit(1); +// } +// } +// return pos_nwords; +// } + + +int PostRawCOPPERFormat_v1::GetDetectorNwords(int n, int finesse_num) +{ + + int nwords = 0; + if (GetFINESSENwords(n, finesse_num) > 0) { + nwords = GetFINESSENwords(n, finesse_num) + - (SIZE_B2LHSLB_HEADER + SIZE_B2LHSLB_TRAILER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER); + } + return nwords; + +} + + +unsigned int PostRawCOPPERFormat_v1::CalcDriverChkSum(int n) +{ + char err_buf[500]; + sprintf(err_buf, "This function is not supported.(block %d) Exiting...: \n%s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + return 0; +} + + +int PostRawCOPPERFormat_v1::GetFINESSENwords(int n, int finesse_num) +{ + int pos_nwords_0, pos_nwords_1; + int nwords; + switch (finesse_num) { + case 0 : + pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_1ST_FINESSE; + pos_nwords_1 = GetBufferPos(n) + tmp_header.POS_OFFSET_2ND_FINESSE; + nwords = m_buffer[ pos_nwords_1 ] - m_buffer[ pos_nwords_0 ]; + break; + case 1 : + pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_2ND_FINESSE; + pos_nwords_1 = GetBufferPos(n) + tmp_header.POS_OFFSET_3RD_FINESSE; + nwords = m_buffer[ pos_nwords_1 ] - m_buffer[ pos_nwords_0 ]; + break; + case 2 : + pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_3RD_FINESSE; + pos_nwords_1 = GetBufferPos(n) + tmp_header.POS_OFFSET_4TH_FINESSE; + nwords = m_buffer[ pos_nwords_1 ] - m_buffer[ pos_nwords_0 ]; + break; + case 3 : + pos_nwords_0 = GetBufferPos(n) + tmp_header.POS_OFFSET_4TH_FINESSE; + { + int nwords_1 = GetBlockNwords(n) + - SIZE_COPPER_DRIVER_TRAILER + - tmp_trailer.GetTrlNwords(); + nwords = nwords_1 - m_buffer[ pos_nwords_0 ]; + } + break; + default : + char err_buf[500]; + sprintf(err_buf, "Invalid finesse # : %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (nwords < 0 || nwords > 1e6) { + char err_buf[500]; + sprintf(err_buf, "# of words is strange. %d : %s %s %d\n", nwords, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + return nwords; + +} + + + + +unsigned int PostRawCOPPERFormat_v1::GetB2LFEE32bitEventNumber(int n) +{ + char err_buf[500]; + sprintf(err_buf, "No event # in B2LFEE header. (block %d) Exiting...\n%s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + return 0; +} + + + +void PostRawCOPPERFormat_v1::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum_rawcprhdr, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + + char err_buf[500]; + sprintf(err_buf, + "This function for format ver.1 is not supported. (n %d preveve %u eve %u prectr %u ctr %u prevrun %u run %u) Exiting...\n %s %s %d\n", + n, prev_evenum, *cur_evenum_rawcprhdr, prev_copper_ctr, *cur_copper_ctr, + prev_exprunsubrun_no, *cur_exprunsubrun_no, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + +// char err_buf[500]; +// int err_flag = 0; +// // +// // Check incrementation of event # +// // +// *cur_evenum_rawcprhdr = GetEveNo(n); +// *cur_exprunsubrun_no = GetExpRunSubrun(n); +// if ( +// prev_exprunsubrun_no == *cur_exprunsubrun_no +// #ifdef WO_FIRST_EVENUM_CHECK +// && prev_evenum != 0xFFFFFFFF && *cur_evenum_rawcprhdr != 0 +// #endif +// ) { +// if ((unsigned int)(prev_evenum + 1) != *cur_evenum_rawcprhdr) { +// sprintf(err_buf, "CORRUPTED DATA: Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n%s %s %d\n", +// n, prev_evenum, *cur_evenum_rawcprhdr, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } +// } + + +// // +// // Check checksum calculated by DeSerializerCOPPER() +// // +// tmp_trailer.SetBuffer(GetRawTrlBufPtr(n)); +// unsigned int xor_chksum = CalcXORChecksum(GetBuffer(n), GetBlockNwords(n) - tmp_trailer.GetTrlNwords()); +// if (tmp_trailer.GetChksum() != xor_chksum) { +// sprintf(err_buf, +// "CORRUPTED DATA: checksum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x\n %s %s %d\n", +// n, GetBlockNwords(n), *cur_evenum_rawcprhdr, tmp_trailer.GetChksum(), xor_chksum, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } + + +// if (err_flag == 1) { +// printf("[DEBUG] ========== dump a data blcok : block # %d==========\n", n); +// PrintData(GetBuffer(n), GetBlockNwords(n)); +// printf("Print out variables to reduce unused-variables-warnings : %u %u\n", prev_copper_ctr, *cur_copper_ctr); +// string err_str = err_buf; +// throw (err_str); + +// // sleep(1234567); +// // exit(-1); +// } + + return; + +} + +bool PostRawCOPPERFormat_v1::CheckCOPPERMagic(int n) +{ + char err_buf[500]; + sprintf(err_buf, "No magic word # in COPPER header (block %d). Exiting...\n%s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + return false; +} + +void PostRawCOPPERFormat_v1::CheckUtimeCtimeTRGType(int n) +{ + char err_buf[500]; + sprintf(err_buf, "This function is not supported (block %d). Exiting...\n%s %s %d\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +} + + +unsigned int PostRawCOPPERFormat_v1::FillTopBlockRawHeader(unsigned int m_node_id, unsigned int prev_eve32, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + char err_buf[500]; + sprintf(err_buf, "This function should be called by PrePostRawCOPPERFormat_***. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("Print out variables to reduce unused-variables-warnings : %u %u %u %u\n", + m_node_id, prev_eve32, prev_exprunsubrun_no, *cur_exprunsubrun_no); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +} + + +int PostRawCOPPERFormat_v1::CheckB2LHSLBMagicWords(int* finesse_buf, int finesse_nwords) +{ + char err_buf[500]; + sprintf(err_buf, "This function should be called by PrePostRawCOPPERFormat_***. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("Print out variables to reduce unused-variables-warnings : %p %d\n", finesse_buf, finesse_nwords); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +} + +int PostRawCOPPERFormat_v1::CheckCRC16(int n, int finesse_num) +{ + + // + // Calculate CRC16 + // + int finesse_nwords = GetFINESSENwords(n, finesse_num); + if (finesse_nwords <= 0) { + char err_buf[500]; + sprintf(err_buf, "The specified finesse(%d) seems to be empty(nwords = %d). Cannot calculate CRC16. Exiting...\n %s %s %d\n", + finesse_num, finesse_nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, &(m_buffer[ tmp_header.POS_TTCTIME_TRGTYPE ]), 1); + temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(m_buffer[ tmp_header.POS_EVE_NO ]), 1); + temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(m_buffer[ tmp_header.POS_TTUTIME ]), 1); + temp_crc16 = CalcCRC16LittleEndian(temp_crc16, &(m_buffer[ tmp_header.POS_EXP_RUN_NO ]), 1); + int* buf = GetFINESSEBuffer(n, finesse_num) + SIZE_B2LHSLB_HEADER + POS_B2L_CTIME; + int pos_nwords = finesse_nwords - (SIZE_B2LHSLB_HEADER + POS_B2L_CTIME + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER); + temp_crc16 = CalcCRC16LittleEndian(temp_crc16, buf, pos_nwords); + + // + // Compare CRC16 with B2LCRC16 + // + buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n, + finesse_num) - ((SIZE_B2LFEE_TRAILER - POS_B2LFEE_CRC16) + SIZE_B2LHSLB_TRAILER) ; + + if (GetEveNo(n) % 10000 == 0) { + printf("#### PostRawCOPPER : Eve %.8x block %d finesse %d B2LCRC16 %.8x calculated CRC16 %.8x\n", GetEveNo(n), n, finesse_num, + *buf, temp_crc16); + } + if ((unsigned short)(*buf & 0xFFFF) != temp_crc16) { + // if ( false ) { + PrintData(m_buffer, m_nwords); + printf("POST CRC16 error %x %x %d\n", *buf , temp_crc16, GetFINESSENwords(n, finesse_num)); + printf("\n"); + int* temp_buf = GetFINESSEBuffer(n, finesse_num); + printf("%.8x ", 0); + for (int k = 0; k < GetFINESSENwords(n, finesse_num); k++) { + printf("%.8x ", temp_buf[ k ]); + if ((k + 1) % 10 == 0) printf("\n%.8x : ", k); + } + printf("\n"); + fflush(stdout); + char err_buf[500]; + sprintf(err_buf, + "[DEBUG] [ERROR] B2LCRC16 (%.4x) differs from one ( %.4x) calculated by PostRawCOPPERfromat class. Exiting...\n %s %s %d\n", + (unsigned short)(*buf & 0xFFFF), temp_crc16, __FILE__, __PRETTY_FUNCTION__, __LINE__); + B2FATAL(err_buf); + } + + return 1; + + +} + + +int* PostRawCOPPERFormat_v1::PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcpr_info) +{ + int* packed_buf = NULL; + + int poswords_to = 0; + int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th }; + int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th }; + + // calculate the event length + int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords(); + + for (int i = 0; i < 4; i++) { + if (detector_buf[ i ] == NULL || nwords[ i ] <= 0) continue; // for an empty FINESSE slot + length_nwords += nwords[ i ]; + length_nwords += SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + // allocate buffer + packed_buf = new int[ length_nwords ]; + memset(packed_buf, 0, sizeof(int) * length_nwords); + + // + // Fill RawHeader + // + tmp_header.SetBuffer(packed_buf); + packed_buf[ tmp_header.POS_NWORDS ] = length_nwords; // total length + + packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] = + 0x7f7f0000 + | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK) + | tmp_header.RAWHEADER_NWORDS; // ver.#, header length + + packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.exp_num << 22) + | (rawcpr_info.run_subrun_num & 0x003FFFFF); // exp. and run # + packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.eve_num; // eve # + packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.tt_ctime & 0x7FFFFFF) << 4; // tt_ctime + packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.tt_utime; // tt_utime + packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.node_id; // node ID + + // fill the positions of finesse buffers + packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + + packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ]; + if (nwords[ 0 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] += + nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ]; + if (nwords[ 1 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] += + nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ]; + if (nwords[ 2 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + + SIZE_B2LHSLB_TRAILER; + } + poswords_to += tmp_header.GetHdrNwords(); + + // Fill COPPER header + poswords_to += SIZE_COPPER_HEADER; + + // Fill FINESSE buffer + for (int i = 0; i < 4; i++) { + + if (detector_buf[ i ] == NULL || nwords[ i ] <= 0) continue; // for an empty FINESSE slot + + // Fill b2link HSLB header + packed_buf[ poswords_to + POS_B2LHSLB_MAGIC ] = 0xffaa0000 | (0xffff & rawcpr_info.eve_num); + poswords_to += SIZE_B2LHSLB_HEADER; + + // Fill b2link FEE header + packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.b2l_ctime & 0x7FFFFFF) << 4; + poswords_to += SIZE_B2LFEE_HEADER; + + // copy the 1st Detector Buffer + memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*sizeof(int)); + poswords_to += nwords[ i ]; + + // Fill b2link FEE trailer + unsigned int crc16 = 0; + packed_buf[ poswords_to + POS_B2LFEE_CRC16 ] = ((0xffff & rawcpr_info.eve_num) << 16) | (crc16 & 0xffff); + poswords_to += SIZE_B2LFEE_TRAILER; + + // Fill b2link HSLB trailer + poswords_to += SIZE_B2LHSLB_TRAILER; + + } + + // Fill COPPER trailer + poswords_to += SIZE_COPPER_TRAILER; + + // Fill RawTrailer + packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER; + poswords_to += tmp_trailer.GetTrlNwords(); + + *packed_buf_nwords = poswords_to; + + return packed_buf; +} + diff --git a/ROPCApplications/rawdata/dataobjects/src/PreRawCOPPERFormat_latest.cc b/ROPCApplications/rawdata/dataobjects/src/PreRawCOPPERFormat_latest.cc new file mode 100644 index 0000000000000000000000000000000000000000..f5c09ecfc718c93ff6768d5539df689e76e0a252 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/PreRawCOPPERFormat_latest.cc @@ -0,0 +1,1428 @@ +//+ +// File : PreRawCOPPERFormat_latest.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/PreRawCOPPERFormat_latest.h> + + +using namespace std; +using namespace Belle2; + +//#define NO_ERROR_STOP +//#define WO_FIRST_EVENUM_CHECK + +//ClassImp(PreRawCOPPERFormat_latest); + +PreRawCOPPERFormat_latest::PreRawCOPPERFormat_latest() +{ +} + +PreRawCOPPERFormat_latest::~PreRawCOPPERFormat_latest() +{ +} + +unsigned int PreRawCOPPERFormat_latest::CalcDriverChkSum(int n) +{ + int min = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS; + int max = GetBufferPos(n) + GetBlockNwords(n) + - tmp_trailer.RAWTRAILER_NWORDS - SIZE_COPPER_DRIVER_TRAILER; + unsigned int chksum = 0; + for (int i = min; i < max; i++) { + chksum ^= m_buffer[ i ]; + } + return chksum; +} + + + +int PreRawCOPPERFormat_latest::GetDetectorNwords(int n, int finesse_num) +{ + + int nwords = 0; + if (GetFINESSENwords(n, finesse_num) > 0) { + nwords = GetFINESSENwords(n, finesse_num) + - (SIZE_B2LHSLB_HEADER + SIZE_B2LHSLB_TRAILER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER); + } + return nwords; + +} + + +int PreRawCOPPERFormat_latest::GetFINESSENwords(int n, int finesse_num) +{ + if (!CheckCOPPERMagic(n)) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : COPPER's magic word is invalid. Exiting... Maybe it is due to data corruption or different version of the data format. : slot%c eve 0x%x exp %d run %d sub %d\n %s %s %d\n", + 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s", err_buf); fflush(stdout); + PrintData(m_buffer, m_nwords); + + for (int i = 0; i < 4; i++) { + printf("[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i); + if (GetFINESSENwords(n, i) > 0) { + CheckCRC16(n, i); + } + } + printf("[DEBUG] ========== No CRC error. : block %d =========\n", n); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages + } + int pos_nwords; + switch (finesse_num) { + case 0 : + pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_A_DATA_LENGTH; + break; + case 1 : + pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_B_DATA_LENGTH; + break; + case 2 : + pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_C_DATA_LENGTH; + break; + case 3 : + pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_D_DATA_LENGTH; + break; + default : + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : Specifined FINESSE number( = %d ) is invalid. Exiting... : slot%c eve 0x%x exp %d run %d sub %d\n %s %s %d\n", + finesse_num, + 65 + finesse_num, GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages + } + return m_buffer[ pos_nwords ]; + +} + + +unsigned int PreRawCOPPERFormat_latest::GetB2LFEE32bitEventNumber(int n) +{ + +#ifndef READ_OLD_B2LFEE_FORMAT_FILE + + int err_flag = 0; + unsigned int eve_num = 0; + int flag = 0; + unsigned int eve[4]; + for (int i = 0; i < 4 ; i++) { + eve[ i ] = 0xbaadf00d; + if (GetFINESSENwords(n, i) > 0) { + int pos_nwords = GetOffsetFINESSE(n, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG; + eve[ i ] = m_buffer[ pos_nwords ]; + if (flag != 1) eve_num = eve[ i ]; + if (eve_num != eve[ i ]) err_flag = 1; + flag = 1; + } + } + + if (flag == 0) { + PrintData(m_buffer, m_nwords); + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : No HSLB data in COPPER data. Exiting... : eve 0x%x exp %d run %d sub %d\n %s %s %d\n", + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages + } + + if (err_flag == 1) { + + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : CORRUPTED DATA: Different event number over HSLBs : slot A 0x%.8x : B 0x%.8x :C 0x%.8x : D 0x%.8x : eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ], + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); + PrintData(m_buffer, m_nwords); + for (int i = 0; i < 4; i++) { + printf("[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i); + if (GetFINESSENwords(n, i) > 0) { + CheckCRC16(n, i); + } + } + printf("[DEBUG] ========== No CRC error. : block %d =========\n", n); +#ifndef NO_ERROR_STOP + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages +#endif //NO_ERROR_STOP + } + return eve_num; + +#else // READ_OLD_B2LFEE_FORMAT_FILE + + char err_buf[500]; + sprintf(err_buf, "[FATAL] You need comment out READ_OLD_B2LFEE_FORMAT_FILE if you are handling a new data format\n%s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); // to reduce multiple error messages +#endif // READ_OLD_B2LFEE_FORMAT_FILE + +} + + +void PreRawCOPPERFormat_latest::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum_rawcprhdr, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + + char err_buf[500]; + int err_flag = 0; + // + // check Magic words + // + if (!CheckCOPPERMagic(n)) { + sprintf(err_buf, + "[FATAL] ERROR_EVENT : Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u : eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + GetMagicDriverHeader(n), GetMagicFPGAHeader(n), GetMagicFPGATrailer(n), GetMagicDriverTrailer(n), + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + + // + // Event # check + // + *cur_evenum_rawcprhdr = GetEveNo(n); + unsigned int evenum_feehdr = GetB2LFEE32bitEventNumber(n); + if (*cur_evenum_rawcprhdr != evenum_feehdr) { + sprintf(err_buf, + "[FATAL] ERROR_EVENT : Event # in PreRawCOPPERFormat_latest header and FEE header is different : cprhdr 0x%x feehdr 0x%x : Exiting... : eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + *cur_evenum_rawcprhdr, evenum_feehdr, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + + // + // Check incrementation of event # + // + *cur_exprunsubrun_no = GetExpRunSubrun(n); + *cur_copper_ctr = GetCOPPERCounter(n); + if (prev_exprunsubrun_no == *cur_exprunsubrun_no) { + if ((unsigned int)(prev_evenum + 1) != *cur_evenum_rawcprhdr) { + sprintf(err_buf, + "[FATAL] ERROR_EVENT : Event # jump : i %d prev 0x%x cur 0x%x : prevrun %.8x currun %.8x: Exiting... : eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + n, prev_evenum, *cur_evenum_rawcprhdr, prev_exprunsubrun_no, *cur_exprunsubrun_no, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + if ((unsigned int)(prev_copper_ctr + 1) != *cur_copper_ctr) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : COPPER counter jump : i %d prev 0x%x cur 0x%x : eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + n, prev_copper_ctr, *cur_copper_ctr, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + } else { + printf("[DEBUG] New run started. cur run %.8x prev. run %.8x cur eve %.8x prev eve %8.x : eve 0x%x exp %d run %d sub %d\n", + *cur_exprunsubrun_no, prev_exprunsubrun_no , *cur_evenum_rawcprhdr, prev_evenum, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n)); + + // Check if the first event of a run is zero. + if ((unsigned int)GetRunNo(n) != (prev_exprunsubrun_no & RawHeader_latest::RUNNO_MASK) >> RawHeader_latest::RUNNO_SHIFT) { + if (*cur_evenum_rawcprhdr != 0) { + + unsigned int eve[4]; + for (int i = 0; i < 4 ; i++) { + eve[ i ] = 0xbaadf00d; + if (GetFINESSENwords(n, i) > 0) { + int pos_nwords = GetOffsetFINESSE(n, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG; + eve[ i ] = m_buffer[ pos_nwords ]; + } + } + sprintf(err_buf, + "[FATAL] ERROR_EVENT : Invalid Event # at the beginning of the run (It should be zero.): preveve 0x%x cureve 0x%x : prev(exp %u run %d sub %u ) cur(exp %u run %d sub %u ) ( A:0x%.8x B:0x%.8x C:0x%.8x D:0x%.8x ) Exiting... : eve 0x%x exp %d run %d sub %d\n %s %s %d\n", + prev_evenum, *cur_evenum_rawcprhdr, + prev_exprunsubrun_no >> 22 , (prev_exprunsubrun_no >> 8) & 0x3FFF, prev_exprunsubrun_no & 0xFF, + *cur_exprunsubrun_no >> 22 , (*cur_exprunsubrun_no >> 8) & 0x3FFF, *cur_exprunsubrun_no & 0xFF, + eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ], + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + } + + } + + + + // + // Check is utime and ctime_trgtype same over different FINESSE data + // + CheckUtimeCtimeTRGType(n); + + // + // Check checksum calculated by COPPER driver + // + if (GetDriverChkSum(n) != CalcDriverChkSum(n)) { + sprintf(err_buf, + "[FATAL] ERROR_EVENT : COPPER driver checkSum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x : eve 0x%x exp %d run %d sub %d\n%s %s %d\n", + n, GetBlockNwords(n), *cur_evenum_rawcprhdr, GetDriverChkSum(n), CalcDriverChkSum(n), + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + + // + // Check checksum calculated by DeSerializerCOPPER() + // + tmp_trailer.SetBuffer(GetRawTrlBufPtr(n)); + unsigned int xor_chksum = CalcXORChecksum(GetBuffer(n), GetBlockNwords(n) - tmp_trailer.GetTrlNwords()); + if (tmp_trailer.GetChksum() != xor_chksum) { + sprintf(err_buf, + "[FATAL] ERROR_EVENT : PreRawCOPPERFormat_latest checksum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x : eve 0x%x exp %d run %d sub %d\n %s %s %d\n", + n, GetBlockNwords(n), *cur_evenum_rawcprhdr, tmp_trailer.GetChksum(), xor_chksum, + GetEveNo(n), GetExpNo(n), GetRunNo(n), GetSubRunNo(n), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + + if (err_flag == 1) { + printf("%s", err_buf); fflush(stdout); + printf("[DEBUG] ========== dump a data blcok : block # %d==========\n", n); + PrintData(GetBuffer(n), GetBlockNwords(n)); + for (int i = 0; i < 4; i++) { + printf("[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i); + if (GetFINESSENwords(n, i) > 0) { + CheckCRC16(n, i); + } + } + printf("[DEBUG] ========== No CRC error : block %d =========\n", n); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages + } + + return; + +} + +bool PreRawCOPPERFormat_latest::CheckCOPPERMagic(int n) +{ + if (GetMagicDriverHeader(n) != COPPER_MAGIC_DRIVER_HEADER) { + return false; + } else if (GetMagicFPGAHeader(n) != COPPER_MAGIC_FPGA_HEADER) { + return false; + } else if (GetMagicFPGATrailer(n) != COPPER_MAGIC_FPGA_TRAILER) { + return false; + } else if (GetMagicDriverTrailer(n) != COPPER_MAGIC_DRIVER_TRAILER) { + return false; + } + return true; +} + +void PreRawCOPPERFormat_latest::CheckUtimeCtimeTRGType(int n) +{ + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int err_flag = 0; + int flag = 0; + unsigned int temp_utime = 0, temp_ctime_trgtype = 0, temp_eve = 0, temp_exprun = 0; + unsigned int temp_ctime_trgtype_footer = 0, temp_eve_footer = 0; + unsigned int utime[4], ctime_trgtype[4], eve[4], exprun[4]; + char err_buf[500]; + + memset(utime, 0, sizeof(utime)); + memset(ctime_trgtype, 0, sizeof(ctime_trgtype)); + memset(eve, 0, sizeof(eve)); + memset(exprun, 0, sizeof(exprun)); + + + for (int i = 0; i < 4; i++) { + int finesse_nwords = GetFINESSENwords(n, i); + if (finesse_nwords > 0) { + int offset_finesse = GetOffsetFINESSE(n, i); + ctime_trgtype[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ]; + utime[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_UTIME ]; + eve[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_TT_TAG ]; + exprun[ i ] = m_buffer[ offset_finesse + SIZE_B2LHSLB_HEADER + POS_EXP_RUN ]; + temp_ctime_trgtype_footer = + m_buffer[ offset_finesse + finesse_nwords - (SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER) + POS_TT_CTIME_B2LFEE ]; + temp_eve_footer = + m_buffer[ offset_finesse + finesse_nwords - (SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER) + POS_CHKSUM_B2LFEE ]; + + if (flag == 0) { + temp_ctime_trgtype = ctime_trgtype[ i ]; + temp_utime = utime[ i ]; + temp_eve = eve[ i ]; + temp_exprun = exprun[ i ]; + flag = 1; + } else { + if (temp_ctime_trgtype != ctime_trgtype[ i ] || temp_utime != utime[ i ] || + temp_eve != eve[ i ] || temp_exprun != exprun[ i ]) { + if (err_flag == 0) { + for (int j = 0; j < 4; j++) { + printf("[DEBUG] FINESSE #=%d buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x\n", + j, GetFINESSENwords(n, j), ctime_trgtype[ j ], utime[ j ], eve[ j ], exprun[ j ]); + } + } + sprintf(err_buf, "[FATAL] ERROR_EVENT : mismatch header value over FINESSEs. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + + err_flag = 1; + break; + } else if (temp_ctime_trgtype != temp_ctime_trgtype_footer || + (temp_eve & 0xffff) != ((temp_eve_footer >> 16) & 0xffff)) { + sprintf(err_buf, + "[FATAL] ERROR_EVENT : mismatch(finesse %d) between header(ctime %.8x eve %.8x) and footer(ctime %.8x eve_crc16 %.8x). Exiting...\n %s %s %d\n", + i, temp_ctime_trgtype, temp_eve, temp_ctime_trgtype_footer, temp_eve_footer, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + err_flag = 1; + } + } + } + } + + if (err_flag != 0) { + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + printf("[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", n, i); + CheckCRC16(n, i); + printf("[DEBUG] ========== CRC check is done. : block %d =========\n", n); + } + } +#ifndef NO_ERROR_STOP + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages +#endif + } + return; +} + +unsigned int PreRawCOPPERFormat_latest::FillTopBlockRawHeader(unsigned int m_node_id, unsigned int prev_eve32, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + const int datablock_id = 0; + // m_temp_value = 12345678; + // + // This function only fills RawHeader contents for the first datablock. + // # of block should be 1 + if (m_num_nodes * m_num_events != 1) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function should be used for PreRawCOPPERFormat_latest containing only one datablock, while. this object has num_nodes of %d and num_events of %d\n %s %s %d\n", + m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages + } + + ////////////////////////////////////////////////// + // + // Fill info in RawHeader + // + ////////////////////////////////////////////////// + + // + // Initialize a RawHeader part + // + memset(m_buffer, 0, sizeof(int) * tmp_header.RAWHEADER_NWORDS); + m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] = tmp_header.RAWHEADER_NWORDS & tmp_header.HDR_NWORDS_MASK; + m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & + tmp_header.FORMAT_VERSION__MASK; + m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (0x80 << tmp_header.FORMAT_VERSION_SHIFT); // PreFormat + m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= tmp_header.MAGIC_WORD; + + // + // Check FINESSEs which containes data + // + int* copper_buf = &(m_buffer[ tmp_header.RAWHEADER_NWORDS ]); + if (copper_buf[ POS_CH_A_DATA_LENGTH ] == 0 && + copper_buf[ POS_CH_B_DATA_LENGTH ] == 0 && + copper_buf[ POS_CH_C_DATA_LENGTH ] == 0 && + copper_buf[ POS_CH_D_DATA_LENGTH ] == 0) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : No FINESSE data in a copper data block. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages + } + + // + // Set total words info + // + int datablock_nwords = + tmp_header.RAWHEADER_NWORDS + + (copper_buf[ POS_DATA_LENGTH ] + + SIZE_COPPER_DRIVER_HEADER + + SIZE_COPPER_DRIVER_TRAILER) + + tmp_trailer.RAWTRAILER_NWORDS; + m_buffer[ tmp_header.POS_NWORDS ] = datablock_nwords; + + + // + // Check the consistency between data length and length in RawHeader + // + if (m_buffer[ tmp_header.POS_NWORDS ] != m_nwords) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : Data length is inconsistent m_nwords %d : nwords from COPPER data %d\n %s %s %d\n", + m_nwords, m_buffer[ tmp_header.POS_NWORDS ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages + } + + // + // Fill offset values + // + int offset_1st_finesse = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + int offset_2nd_finesse = offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ]; + int offset_3rd_finesse = offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ]; + int offset_4th_finesse = offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ]; + m_buffer[ tmp_header.POS_OFFSET_1ST_FINESSE ] = offset_1st_finesse; + m_buffer[ tmp_header.POS_OFFSET_2ND_FINESSE ] = offset_2nd_finesse; + m_buffer[ tmp_header.POS_OFFSET_3RD_FINESSE ] = offset_3rd_finesse; + m_buffer[ tmp_header.POS_OFFSET_4TH_FINESSE ] = offset_4th_finesse; + + // + // Fill Exp/Run value + // + int* finesse_buf = & + (m_buffer[ offset_1st_finesse ]); // In any finesse implementations, the top finesse buffer should be at offset_1st_finesse; + m_buffer[ tmp_header.POS_EXP_RUN_NO ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_EXP_RUN ]; + + + // + // Fill event # + // + unsigned int cur_ftsw_eve32 = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_TAG ]; + m_buffer[ tmp_header.POS_EVE_NO ] = cur_ftsw_eve32; + + // + // Copy FTSW words from B2LFEE header + // + m_buffer[ tmp_header.POS_TTCTIME_TRGTYPE ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ]; + m_buffer[ tmp_header.POS_TTUTIME ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_UTIME ]; + + // + // Set node ID, trunc_mask, data_type + // + m_buffer[ tmp_header.POS_NODE_ID ] = m_node_id; + + // + // Check error counts of b2link-packet CRC + // + // m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] = ((m_trunc_mask << 31) & 0x80000000) | (m_data_type & 0x7FFFFFFF); + // printf("%.8x %.8x %.8x %.8x : %.8x\n", + // copper_buf[ POS_CH_A_DATA_LENGTH ], copper_buf[ POS_CH_B_DATA_LENGTH ], + // copper_buf[ POS_CH_C_DATA_LENGTH ], copper_buf[ POS_CH_D_DATA_LENGTH ], m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] ); + + m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] = 0; + unsigned int ff55_higher_bits = 0, ff55_lower_bits = 0; + + if (copper_buf[ POS_CH_A_DATA_LENGTH ] != 0) { + ff55_higher_bits = (unsigned int)(m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) & + 0xFFFF0000; + ff55_lower_bits = m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF; + + if (ff55_higher_bits != 0xff550000) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : HSLB slotA's trailer magic word(0xff55) is invalid. : : eve %8u run %d foooter %.8x : %s %s %d\n", + cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF, + m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + PrintData(m_buffer, m_nwords); fflush(stdout); + B2FATAL(err_buf); + } + + if (ff55_lower_bits != 0) { + const int linkdown_bit = 15; + // const int packet_crcerr_bit = 8; + char err_buf[500]; + if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) { + sprintf(err_buf, "[FATAL] B2link down on slot A eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32, + m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + } else { + m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR; + sprintf(err_buf, "[FATAL] B2link packet CRC error slot A eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32, + m_buffer[ offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + } + printf("%s", err_buf); fflush(stdout); + PrintData(m_buffer, m_nwords); + fflush(stdout); + B2FATAL(err_buf); + } + } + + if (copper_buf[ POS_CH_B_DATA_LENGTH ] != 0) { + ff55_higher_bits = (unsigned int)(m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) & + 0xFFFF0000; + ff55_lower_bits = (m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF); + + if (ff55_higher_bits != 0xff550000) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : HSLB slotB's trailer magic word(0xff55) is invalid. : : eve %8u run %d foooter %.8x : %s %s %d\n", + cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF, + m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + PrintData(m_buffer, m_nwords); fflush(stdout); + B2FATAL(err_buf); + } + + if (ff55_lower_bits != 0) { + char err_buf[500]; + const int linkdown_bit = 15; + // const int packet_crcerr_bit = 8; + if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) { + sprintf(err_buf, "[FATAL] B2link down on slot B eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32, + m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + } else { + m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR; + sprintf(err_buf, "[FATAL] B2link packet CRC error slot B eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32, + m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + } + printf("%s", err_buf); fflush(stdout); + PrintData(m_buffer, m_nwords); fflush(stdout); + B2FATAL(err_buf); + } + } + + if (copper_buf[ POS_CH_C_DATA_LENGTH ] != 0) { + ff55_higher_bits = (unsigned int)(m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) & + 0xFFFF0000; + ff55_lower_bits = (m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF); + + if (ff55_higher_bits != 0xff550000) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : HSLB slotC's trailer magic word(0xff55) is invalid. : : eve %8u run %d foooter %.8x : %s %s %d\n", + cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF, + m_buffer[ offset_2nd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + PrintData(m_buffer, m_nwords); fflush(stdout); + B2FATAL(err_buf); + } + + if (ff55_lower_bits != 0) { + char err_buf[500]; + const int linkdown_bit = 15; + // const int packet_crcerr_bit = 8; + if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) { + sprintf(err_buf, "[FATAL] B2link down on slot C eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32, + m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + } else { + m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] |= tmp_header.B2LINK_PACKET_CRC_ERROR; + sprintf(err_buf, "[FATAL] B2link packet CRC error slot C eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32, + m_buffer[ offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + } + printf("%s", err_buf); fflush(stdout); + PrintData(m_buffer, m_nwords); fflush(stdout); + B2FATAL(err_buf); + } + } + + if (copper_buf[ POS_CH_D_DATA_LENGTH ] != 0) { + ff55_higher_bits = (unsigned int)(m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ]) & + 0xFFFF0000; + ff55_lower_bits = (m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ] & 0xFFFF); + + if (ff55_higher_bits != 0xff550000) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : HSLB slotD's trailer magic word(0xff55) is invalid. : : eve %8u run %d foooter %.8x : %s %s %d\n", + cur_ftsw_eve32, (m_buffer[ tmp_header.POS_EXP_RUN_NO ] >> 8) & 0x3FFF, + m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + PrintData(m_buffer, m_nwords); fflush(stdout); + B2FATAL(err_buf); + } + + if (ff55_lower_bits != 0) { + char err_buf[500]; + const int linkdown_bit = 15; + if ((ff55_lower_bits & (1 << linkdown_bit)) != 0) { + sprintf(err_buf, "[FATAL] B2link down on slot D eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32, + m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + } else { + sprintf(err_buf, "[FATAL] B2link packet CRC error slot D eve %8u foooter %.8x : %s %s %d\n", cur_ftsw_eve32, + m_buffer[ offset_4th_finesse + copper_buf[ POS_CH_D_DATA_LENGTH ] - SIZE_B2LHSLB_HEADER ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + } + printf("%s", err_buf); fflush(stdout); + PrintData(m_buffer, m_nwords); fflush(stdout); + B2FATAL(err_buf); + } + } + + + ////////////////////////////////////////////////// + // + // Fill info in RawTrailer + // + ////////////////////////////////////////////////// + + // + // Calculate XOR checksum + // + unsigned int chksum_top = 0, chksum_body = 0, chksum_bottom = 0; + + int top_end = tmp_header.RAWHEADER_NWORDS; + for (int i = 0; i < top_end; i++) { + chksum_top ^= m_buffer[ i ]; + } + int body_end = datablock_nwords - SIZE_COPPER_DRIVER_TRAILER - tmp_trailer.RAWTRAILER_NWORDS; + for (int i = top_end; i < body_end; i++) { + chksum_body ^= m_buffer[ i ]; + } + + int bottom_end = datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS; + for (int i = body_end; i < bottom_end; i++) { + chksum_bottom ^= m_buffer[ i ]; + } + + // + // check COPPER driver checksum + // + if (chksum_body != (unsigned int)(m_buffer[ body_end ])) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : COPPER driver checksum is not consistent.: calcd. %.8x data %.8x\n %s %s %d\n", + chksum_body, m_buffer[ body_end ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); // to reduce multiple error messages + } + + // + // Fill trailer info (checksum, magic word) + // + unsigned int chksum = chksum_top ^ chksum_body ^ chksum_bottom; + int* trl = &(m_buffer[ datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS ]); + trl[ tmp_trailer.POS_CHKSUM ] = chksum; + trl[ tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER; + + + ////////////////////////////////////////////////// + // + // Data check ( magic word, event incrementation ) + // + ////////////////////////////////////////////////// + + + // + // check magic words + // + int* fpga_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_3); + int* driver_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_4); + int err_flag = 0; + if ((unsigned int)(copper_buf[ POS_MAGIC_COPPER_1 ]) != COPPER_MAGIC_DRIVER_HEADER) { + err_flag = 1; + } else if ((unsigned int)(copper_buf[ POS_MAGIC_COPPER_2 ]) != COPPER_MAGIC_FPGA_HEADER) { + err_flag = 1; + } else if ((unsigned int)(*fpga_trailer_magic) != COPPER_MAGIC_FPGA_TRAILER) { + err_flag = 1; + } else if ((unsigned int)(*driver_trailer_magic) != COPPER_MAGIC_DRIVER_TRAILER) { + err_flag = 1; + } + if (err_flag == 1) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u\n %s %s %d\n", + GetMagicDriverHeader(datablock_id), + GetMagicFPGAHeader(datablock_id), + GetMagicFPGATrailer(datablock_id), + GetMagicDriverTrailer(datablock_id), + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); +#ifndef NO_ERROR_STOP + B2FATAL(err_buf); // to reduce multiple error messages +#endif + } + + // + // check incrementation of event # + // + + + *cur_exprunsubrun_no = GetExpRunSubrun(datablock_id); + +// if ((unsigned int)cur_ftsw_eve32 < 50) { +// printf("EVENT OUTPUT ########## %u\n", cur_ftsw_eve32); +// PrintData(GetBuffer(datablock_id), TotalBufNwords()); +// } + if (prev_exprunsubrun_no == *cur_exprunsubrun_no) { + if (prev_eve32 + 1 != cur_ftsw_eve32) { + unsigned int eve[4] = {0xbaadf00d, 0xbaadf00d, 0xbaadf00d, 0xbaadf00d }; +#ifndef NO_ERROR_STOP + if (m_num_nodes * m_num_events != 1) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function should be used for PreRawCOPPERFormat_latest containing only one datablock, while. this object has num_nodes of %d and num_events of %d\n %s %s %d\n", + m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages + } else { + for (int i = 0; i < 4 ; i++) { + // Only one block is in this m_buffer[] when m_num_nodes*m_num_events=1 + // 0 is specified as a block number + if (GetFINESSENwords(0 , i) > 0) { + int pos_nwords = GetOffsetFINESSE(0, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG; + eve[ i ] = m_buffer[ pos_nwords ]; + } + } + } + + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : Invalid event_number. Exiting...: cur 32bit eve %u preveve %u ( A:0x%.8x B:0x%.8x C:0x%.8x D:0x%.8x ) prun %u crun %u\n %s %s %d\n", + cur_ftsw_eve32, prev_eve32, + eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ], + prev_exprunsubrun_no, *cur_exprunsubrun_no, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); + + string err_str = err_buf; + printf("[DEBUG] i= %d : num entries %d : Tot words %d\n", 0 , GetNumEntries(), TotalBufNwords()); + PrintData(GetBuffer(datablock_id), TotalBufNwords()); + + for (int i = 0; i < 4; i++) { + printf("[DEBUG] ========== CRC check : block # %d finesse %d ==========\n", datablock_id, i); + if (GetFINESSENwords(datablock_id, i) > 0) { + CheckCRC16(datablock_id, i); + } + } + printf("[DEBUG] ========== No CRC error : block %d =========\n", datablock_id); + // throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages +#endif + + } + } + + return cur_ftsw_eve32; + +} + + + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 +void PreRawCOPPERFormat_latest::CheckB2LFEEHeaderVersion(int n) +{ + int* temp_buf; + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + temp_buf = GetFINESSEBuffer(n, i); + if ((temp_buf[ 3 ] & 0x40000000) == 0) { +#ifdef TEMP + // this word for exp/run + // old one (ver.1) used for SPring8 test in 2013 + printf("[DEBUG] \033[31m"); + printf("[DEBUG] ===Firmware ver. ERROR===\n "); + printf("[DEBUG] FTSW and b2tt firmwares was updated on Nov.22, 2013 and the header format attached by B2link was changed in the new firmwares.\n"); + printf("[DEBUG] If you are going to take data now, Please update the firmware.\n"); + printf("[DEBUG] For details, please see Nakao-san's e-mail [b2link_ml:0111] Re: [daq2ml:0159] beta version of trigger timing receiver firmware (b2tt) on bdaq SVN\n"); + printf("[DEBUG] Or if you are going to read data taken before the update, please use basf2 software before svn revision 7419\n"); + printf("[DEBUG] About the format please see Nakao-san's B2GM slides(p. 13 and 15) http://kds.kek.jp/getFile.py/access?contribId=143&sessionId=38&resId=0&materialId=slides&confId=13911.\n"); + printf("[DEBUG] Sorry for inconvenience.\n"); + printf("[DEBUG] \033[0m"); + fflush(stderr); + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : FTSW and b2tt firmwares are old. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + B2FATAL(err_buf); +#endif + } else { + // this word for 32bit unixtime + // new one (ver.2) + break; + } + } + + if (i == 3) { +#ifdef TEMP + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : PreRawCOPPERFormat_latest contains no FINESSE data. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages +#endif + } + } + return; +} +#endif + +//int PreRawCOPPERFormat_latest::CalcReducedDataSize(RawDataBlock* raw_datablk) +int PreRawCOPPERFormat_latest::CalcReducedDataSize(int* bufin, int nwords, int num_events, int num_nodes) +{ + // + // Calculate reduced length for a total RawDataBlock (containing multiple data blocks) + // + RawDataBlockFormat radblk_fmt; + int delete_flag = 0; + radblk_fmt.SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes); + + int reduced_nwords = 0; + for (int k = 0; k < radblk_fmt.GetNumEvents(); k++) { + int num_nodes_in_sendblock = radblk_fmt.GetNumNodes(); + for (int l = 0; l < num_nodes_in_sendblock; l++) { + int entry_id = l + k * num_nodes_in_sendblock; + + if (radblk_fmt.CheckFTSWID(entry_id) || radblk_fmt.CheckTLUID(entry_id)) { + reduced_nwords += radblk_fmt.GetBlockNwords(entry_id); + } else { + PreRawCOPPERFormat_latest temp_prerawcpr; + int temp_delete_flag = 0, temp_num_eve = 1, temp_num_nodes = 1; + + // Call CalcReducedNwords + temp_prerawcpr.SetBuffer(radblk_fmt.GetBuffer(entry_id), + radblk_fmt.GetBlockNwords(entry_id), + temp_delete_flag, temp_num_eve, + temp_num_nodes); + reduced_nwords += temp_prerawcpr.CalcReducedNwords(0); + } + } + } + return reduced_nwords; + +} + +//void PreRawCOPPERFormat_latest::CopyReducedData(RawDataBlock* raw_datablk, int* buf_to, int delete_flag_from) +void PreRawCOPPERFormat_latest::CopyReducedData(int* bufin, int nwords, int num_events, int num_nodes, int* buf_to, int* nwords_to) +{ + // + // Make a reduced buffer a total RawDataBlock (containing multiple data blocks) + // + RawDataBlockFormat radblk_fmt; + int delete_flag = 0; + radblk_fmt.SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes); + + int pos_nwords_to = 0; + for (int k = 0; k < radblk_fmt.GetNumEvents(); k++) { + int num_nodes_in_sendblock = radblk_fmt.GetNumNodes(); + for (int l = 0; l < num_nodes_in_sendblock; l++) { + int entry_id = l + k * num_nodes_in_sendblock; + if (radblk_fmt.CheckFTSWID(entry_id) || + radblk_fmt.CheckTLUID(entry_id)) { + radblk_fmt.CopyBlock(entry_id, buf_to + pos_nwords_to); + pos_nwords_to += radblk_fmt.GetBlockNwords(entry_id); + + } else { + SetBuffer(radblk_fmt.GetBuffer(entry_id), + radblk_fmt.GetBlockNwords(entry_id), 0, 1, 1); + + pos_nwords_to += CopyReducedBuffer(0, buf_to + pos_nwords_to); + + + } + } + } + + *nwords_to = pos_nwords_to; + + return ; +} + + +int PreRawCOPPERFormat_latest::CalcReducedNwords(int n) +{ + // + // Calculate reduced length for one data block which is a part of a RawDataBlock + // + + int nwords_to = 0; + + //RawCOPPER header + nwords_to += tmp_header.RAWHEADER_NWORDS; + + for (int j = 0; j < 4; j++) { + + int finesse_nwords = GetFINESSENwords(n, j); + if (finesse_nwords > 0) { + // + // B2L(HSLB/FEE) header and trailers are resized + // m_reduced_rawcpr should be PostRawCOPPERFormat_latest + // + nwords_to += + finesse_nwords + - (SIZE_B2LHSLB_HEADER - m_reduced_rawcpr.SIZE_B2LHSLB_HEADER) + - (SIZE_B2LFEE_HEADER - m_reduced_rawcpr.SIZE_B2LFEE_HEADER) + - (SIZE_B2LFEE_TRAILER - m_reduced_rawcpr.SIZE_B2LFEE_TRAILER) + - (SIZE_B2LHSLB_TRAILER - m_reduced_rawcpr.SIZE_B2LHSLB_TRAILER); + } + + } + + //RawCOPPER Trailer + nwords_to += tmp_trailer.GetTrlNwords(); + + return nwords_to; +} + + + + + + +int PreRawCOPPERFormat_latest::CopyReducedBuffer(int n, int* buf_to) +{ + // + // Make a reduced buffer for one data block which is a part of a RawDataBlock + // + + int* buf_from = NULL; + int nwords_buf_to = CalcReducedNwords(n); + int pos_nwords_to = 0; + int copy_nwords = 0; + + unsigned int removed_xor_chksum = 0; + + // copyt to ReducedRawCOPPER + // ReducedRawCOPPER m_reduced_rawcpr; + //Header copy + copy_nwords = tmp_header.RAWHEADER_NWORDS; + buf_from = GetBuffer(n); + copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to); + + + //calcurate XOR checksum diff.( depends on data-format ) + removed_xor_chksum ^= buf_from[ tmp_header.POS_VERSION_HDRNWORDS ]; + + // Unset the PreFormat bit ( 15th bit ) + buf_to[ tmp_header.POS_VERSION_HDRNWORDS ] &= 0xFFFF7FFF; + + //calcurate XOR checksum diff.( depends on data-format ) + removed_xor_chksum ^= buf_to[ tmp_header.POS_VERSION_HDRNWORDS ]; + for (int i = 0; i < SIZE_COPPER_HEADER; i++) { + removed_xor_chksum ^= buf_from[ tmp_header.RAWHEADER_NWORDS + i ]; + } + + //Check Header + // m_reduced_rawcpr.tmp_header.CheckHeader(buf_to + pos_nwords_to - copy_nwords); + + + // copy FINESSE buffer + int pos_nwords_finesse[ 4 ]; + for (int j = 0; j < 4; j++) { + pos_nwords_finesse[ j ] = pos_nwords_to; + if (GetFINESSENwords(n, j) > 0) { + int* finesse_buf = GetFINESSEBuffer(n, j); + int finesse_nwords = GetFINESSENwords(n, j); + + + CheckB2LHSLBMagicWords(finesse_buf, finesse_nwords); + // copy the whole B2LHSLB header (1word) + buf_to[ pos_nwords_to ] = finesse_buf[ POS_MAGIC_B2LHSLB ]; + pos_nwords_to++; + + // copy the last word of B2LFEE and body( DetectorBuffer ) + buf_from = + finesse_buf + + SIZE_B2LHSLB_HEADER + + POS_B2L_CTIME; //the last word of B2LFEE + + + // check finesse buffer size : ( When dumhslb was used, this type of error + // occured and RecvStream0.py died by Segmentation Fault. ) 2014.12.01. + if (finesse_nwords - SIZE_B2LHSLB_HEADER - SIZE_B2LFEE_HEADER + - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER < 0) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : Finesse buffer size is too small( %d words < %d words). May be the data are corrupted. Exiting...\n %s %s %d\n", + finesse_nwords, SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + // string err_str = err_buf; throw (err_str); + B2FATAL(err_buf); // to reduce multiple error messages + } + + //calcurate XOR checksum diff.( depends on data-format ) + for (int k = 1; k <= 4 ; k++) { + removed_xor_chksum ^= finesse_buf[ k ]; + } + + copy_nwords = + finesse_nwords + - SIZE_B2LHSLB_HEADER // already copied + - POS_B2L_CTIME // only one word copied ( SIZE_B2LFEE_HEADER - 1 = POS_B2L_CTIME ) + - SIZE_B2LFEE_TRAILER // will be copied later + - SIZE_B2LHSLB_TRAILER; // Not copied + copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to); + // printf("pos %d nwords %d nwords to %d\n", pos_nwords_to, copy_nwords, nwords_buf_to ); + + //copy B2LFEE trailer(CRC info) + buf_to[ pos_nwords_to ] = 0xffff & finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER - (SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) ]; + //copy B2LHSLB trailer(packet CRC error count) + buf_to[ pos_nwords_to ] |= (finesse_buf[ finesse_nwords - (SIZE_B2LHSLB_TRAILER - POS_MAGIC_B2LHSLB) ] << 16) & 0xFFFF0000; + + //calcurate XOR checksum diff. ( depends on data-format ) + for (int k = 0; k <= 2 ; k++) { + removed_xor_chksum ^= finesse_buf[ finesse_nwords - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER + k ]; + } + removed_xor_chksum ^= buf_to[ pos_nwords_to ]; + + pos_nwords_to++; + + // check CRC data + // CheckCRC16( n, j ); + } + } + + // copy RawCOPPER trailer + buf_from = + GetBuffer(n) + + GetBlockNwords(n) + - tmp_trailer.GetTrlNwords(); + copy_nwords = tmp_trailer.GetTrlNwords(); + copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to); + + //calcurate XOR checksum diff.( depends on data-format ) + unsigned int old_rawcopper_chksum = buf_from[ tmp_trailer.POS_CHKSUM ]; + for (int i = 0; i < SIZE_COPPER_TRAILER; i++) { + removed_xor_chksum ^= (unsigned int) * (buf_from - SIZE_COPPER_TRAILER + i); + } + + + // length check + if (pos_nwords_to != nwords_buf_to) { + char err_buf[500]; + sprintf(err_buf, "Buffer overflow. Exiting... %d %d\n", pos_nwords_to, nwords_buf_to); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + + // Recalculate XOR checksum in a RawCOPPER trailer + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS); + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE); + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE); + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE); + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE); + + // + // Apply changes followed by data size reduction + // + *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS) = nwords_buf_to; + *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE) = pos_nwords_finesse[ 0 ]; + *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE) = pos_nwords_finesse[ 1 ]; + *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE) = pos_nwords_finesse[ 2 ]; + *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE) = pos_nwords_finesse[ 3 ]; + + // Recalculate XOR checksum in a RawCOPPER trailer + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS); + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE); + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE); + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE); + removed_xor_chksum ^= *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE); + + // Recalculate XOR checksum in a RawCOPPER trailer + unsigned int new_rawcopper_chksum = CalcXORChecksum(buf_to, pos_nwords_to - tmp_trailer.GetTrlNwords()); + + + if ((old_rawcopper_chksum ^ removed_xor_chksum) != new_rawcopper_chksum) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : RawCOPPER XOR checksum is inconsistent between before/after data reduction.(%.8x != %.8x ^ %.8x = %.8x ) Exiting...\n %s %s %d\n", + new_rawcopper_chksum, old_rawcopper_chksum, removed_xor_chksum, old_rawcopper_chksum ^ removed_xor_chksum, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); // to reduce multiple error messages + } + + *(buf_to + pos_nwords_to - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM) = new_rawcopper_chksum; + + + + // + // data block # check + // + m_reduced_rawcpr.SetBuffer(buf_to, nwords_buf_to, 0, GetNumEvents(), GetNumNodes()); + if (m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes() <= 0) { + char err_buf[500]; + sprintf(err_buf, "Invalid data block numbers.(# of events %d, # of nodes %d) Exiting...\n", + m_reduced_rawcpr.GetNumEvents(), m_reduced_rawcpr.GetNumNodes()); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + // + // check no-data buffer + // + for (int i = 0; i < m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes(); i++) { + int nonzero_finesse_buf = 0; + for (int j = 0; j < 4; j++) { + + if (GetFINESSENwords(n, j) > 0) { + // m_reduced_rawcpr.CheckCRC16(i, j); + nonzero_finesse_buf++; + } + } + if (nonzero_finesse_buf == 0) { + char err_buf[500]; + sprintf(err_buf, "No non-zero FINESSE buffer. Exiting...\n"); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + } + + // post_rawcopper_latest.CheckCRC16(0, 0); + // printf("fROM =======================================\n"); + // for (int k = 0; k < nwords_buf_to; k++) { + // printf(" %.8x", GetBuffer(n)[ k ]); + // if ( ( k + 1 ) % 10 == 0) { + // printf("\n"); + // } + // } + // printf("\n"); + + // printf("tO =======================================\n"); + // for (int k = 0; k < nwords_buf_to; k++) { + // printf(" %.8x", buf_to[ k ]); + // if ( ( k + 1 ) % 10 == 0) { + // printf("\n"); + // } + // } + // printf("\n"); + // printf("=============================================\n"); + return pos_nwords_to; +} + + +int PreRawCOPPERFormat_latest::CheckB2LHSLBMagicWords(int* finesse_buf, int finesse_nwords) +{ + + if ((finesse_buf[ POS_MAGIC_B2LHSLB ] & 0xFFFF0000) == B2LHSLB_HEADER_MAGIC && + ((finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ] & 0xFFFF0000) + == B2LHSLB_TRAILER_MAGIC)) { + return 1; + } else { + PrintData(m_buffer, m_nwords); + char err_buf[500]; + sprintf(err_buf, + "Invalid B2LHSLB magic words : header 0x%x (= should be ffaa**** ) or trailer 0x%x (= should be ff55**** ). Exiting... :%s %s %d\n", + finesse_buf[ POS_MAGIC_B2LHSLB ], + finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); +#ifndef NO_ERROR_STOP + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif + } +} + + + +int PreRawCOPPERFormat_latest::CheckCRC16(int n, int finesse_num) +{ + // + // Calculate CRC16 + // + int* buf = GetFINESSEBuffer(n, finesse_num) + SIZE_B2LHSLB_HEADER; + int nwords = GetFINESSENwords(n, finesse_num) - (SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER); + unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, buf, nwords); + + // + // Compare CRC16 with B2LCRC16 + // + buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n, finesse_num) + - ((SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) + SIZE_B2LHSLB_TRAILER) ; + + if ((unsigned short)(*buf & 0xFFFF) != temp_crc16) { + PrintData(GetBuffer(n), *(GetBuffer(n) + tmp_header.POS_NWORDS)); + printf("[FATAL] ERROR_EVENT : PRE CRC16 error : slot %c B2LCRC16 %x Calculated CRC16 %x : Nwords of FINESSE buf %d\n", + 65 + finesse_num, *buf , temp_crc16, GetFINESSENwords(n, finesse_num)); + int* temp_buf = GetFINESSEBuffer(n, finesse_num); + for (int k = 0; k < GetFINESSENwords(n, finesse_num); k++) { + printf("%.8x ", temp_buf[ k ]); + if ((k + 1) % 10 == 0) { + printf("\n"); + } + } + printf("\n"); + fflush(stdout); + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : B2LCRC16 (%.4x) differs from one ( %.4x) calculated by PreRawCOPPERfromat class. Exiting...\n %s %s %d\n", + (unsigned short)(*buf & 0xFFFF), temp_crc16, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); // to reduce multiple error messages + } + return 1; + +} + +int* PreRawCOPPERFormat_latest::PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcpr_info) +{ + int* packed_buf = NULL; + + int poswords_to = 0; + int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th }; + int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th }; + + // calculate the event length + int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords(); + + for (int i = 0; i < 4; i++) { + if (detector_buf[ i ] == NULL || nwords[ i ] <= 0) continue; // for an empty FINESSE slot + length_nwords += nwords[ i ]; + length_nwords += SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + // allocate buffer + packed_buf = new int[ length_nwords ]; + memset(packed_buf, 0, sizeof(int) * length_nwords); + + // + // Fill RawHeader + // + tmp_header.SetBuffer(packed_buf); + + packed_buf[ tmp_header.POS_NWORDS ] = length_nwords; // total length + packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] = 0x7f7f8000 + | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK) + | tmp_header.RAWHEADER_NWORDS; // ver.#, header length + packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.exp_num << tmp_header.EXP_SHIFT) + | (rawcpr_info.run_subrun_num & 0x003FFFFF); // exp. and run # + packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.eve_num; // eve # + packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.tt_ctime & 0x7FFFFFF) << 4; // tt_ctime + packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.tt_utime; // tt_utime + packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.node_id; // node ID + + + + + // fill the positions of finesse buffers + packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + + packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ]; + if (nwords[ 0 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] += + nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ]; + if (nwords[ 1 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] += + nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ]; + if (nwords[ 2 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + + SIZE_B2LHSLB_TRAILER; + } + poswords_to += tmp_header.GetHdrNwords(); + + // Fill COPPER header + packed_buf[ poswords_to + POS_MAGIC_COPPER_1 ] = COPPER_MAGIC_DRIVER_HEADER; + packed_buf[ poswords_to + POS_MAGIC_COPPER_2 ] = COPPER_MAGIC_FPGA_HEADER; + packed_buf[ poswords_to + POS_EVE_NUM_COPPER ] = rawcpr_info.eve_num; + + int size_b2l_hdrtrl = SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + if (nwords[ 0 ] != 0) packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] = nwords[ 0 ] + size_b2l_hdrtrl; + if (nwords[ 1 ] != 0) packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] = nwords[ 1 ] + size_b2l_hdrtrl; + if (nwords[ 2 ] != 0) packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] = nwords[ 2 ] + size_b2l_hdrtrl; + if (nwords[ 3 ] != 0) packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] = nwords[ 3 ] + size_b2l_hdrtrl; + + packed_buf[ poswords_to + POS_DATA_LENGTH ] = + packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] + + packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] + + packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] + + packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] + + (SIZE_COPPER_HEADER - SIZE_COPPER_DRIVER_HEADER) + + (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER); + + poswords_to += SIZE_COPPER_HEADER; + + // Fill FINESSE buffer + for (int i = 0; i < 4; i++) { + + if (detector_buf[ i ] == NULL || nwords[ i ] <= 0) continue; // for an empty FINESSE slot + + // Fill b2link HSLB header + packed_buf[ poswords_to + POS_MAGIC_B2LHSLB ] = 0xffaa0000 | (0xffff & rawcpr_info.eve_num); + poswords_to += SIZE_B2LHSLB_HEADER; + int* crc16_start = &(packed_buf[ poswords_to ]); + + // Fill b2link FEE header + + packed_buf[ poswords_to + POS_TT_CTIME_TYPE ] = (rawcpr_info.tt_ctime & 0x7FFFFFF) << 4; + unsigned int temp_ctime_type = packed_buf[ poswords_to + POS_TT_CTIME_TYPE ]; + packed_buf[ poswords_to + POS_TT_TAG ] = rawcpr_info.eve_num; + packed_buf[ poswords_to + POS_TT_UTIME ] = rawcpr_info.tt_utime; + packed_buf[ poswords_to + POS_EXP_RUN ] = (rawcpr_info.exp_num << tmp_header.EXP_SHIFT) | (rawcpr_info.run_subrun_num & + 0x003FFFFF); // exp. and run # + packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.b2l_ctime & 0x7FFFFFF) << 4; + poswords_to += SIZE_B2LFEE_HEADER; + + // copy the 1st Detector Buffer + memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*sizeof(int)); + poswords_to += nwords[ i ]; + + // Fill b2link b2tt-tag trailer + packed_buf[ poswords_to + POS_TT_CTIME_B2LFEE ] = temp_ctime_type; + + // Fill b2link FEE trailer + unsigned short crc16 = CalcCRC16LittleEndian(0xffff, crc16_start, nwords[ i ] + SIZE_B2LFEE_HEADER); + packed_buf[ poswords_to + POS_CHKSUM_B2LFEE ] = ((0xffff & rawcpr_info.eve_num) << 16) | (crc16 & 0xffff); + poswords_to += SIZE_B2LFEE_TRAILER; + + // Fill b2link HSLB trailer + packed_buf[ poswords_to + POS_CHKSUM_B2LHSLB ] = 0xff550000; + poswords_to += SIZE_B2LHSLB_TRAILER; + + } + + // Fill COPPER trailer + packed_buf[ poswords_to + POS_MAGIC_COPPER_3 ] = COPPER_MAGIC_FPGA_TRAILER; + packed_buf[ poswords_to + POS_MAGIC_COPPER_4 ] = COPPER_MAGIC_DRIVER_TRAILER; + unsigned int chksum = 0; + for (int i = tmp_header.GetHdrNwords(); i < poswords_to + (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER); i++) { + chksum ^= packed_buf[ i ]; + } + packed_buf[ poswords_to + POS_CHKSUM_COPPER ] = chksum; + poswords_to += SIZE_COPPER_TRAILER; + + // Calculate RawCOPPER checksum and fill RawTrailer + chksum = 0; + for (int i = 0; i < poswords_to; i++) { + chksum ^= packed_buf[ i ]; + } + packed_buf[ poswords_to + tmp_trailer.POS_CHKSUM ] = chksum; + + packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER; + poswords_to += tmp_trailer.GetTrlNwords(); + + *packed_buf_nwords = poswords_to; + return packed_buf; +} + diff --git a/ROPCApplications/rawdata/dataobjects/src/PreRawCOPPERFormat_v1.cc b/ROPCApplications/rawdata/dataobjects/src/PreRawCOPPERFormat_v1.cc new file mode 100644 index 0000000000000000000000000000000000000000..6239e82a5c3978ac363ca32c7d4365b21e71e6aa --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/PreRawCOPPERFormat_v1.cc @@ -0,0 +1,1216 @@ +//+ +// File : PreRawCOPPERFormat_v1.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/PreRawCOPPERFormat_v1.h> + + +using namespace std; +using namespace Belle2; + +//#define DESY +//#define NO_DATA_CHECK +//#define WO_FIRST_EVENUM_CHECK + +//ClassImp(PreRawCOPPERFormat_v1); + +PreRawCOPPERFormat_v1::PreRawCOPPERFormat_v1() +{ +} + +PreRawCOPPERFormat_v1::~PreRawCOPPERFormat_v1() +{ +} + + +// int PreRawCOPPERFormat_v1::GetBufferPos(int n) +// { +// if (m_buffer == NULL || m_nwords <= 0) { +// char err_buf[500]; +// sprintf(err_buf, "[DEBUG] [ERROR] RawPacket buffer(%p) is not available or length(%d) is not set.\n %s %s %d\n", +// m_buffer, m_nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("%s", err_buf); fflush(stdout); +// string err_str = err_buf; throw (err_str); +// } + +// if (n >= (m_num_events * m_num_nodes)) { +// char err_buf[500]; +// sprintf(err_buf, "[DEBUG] Invalid COPPER block No. (%d : max %d ) is specified. Exiting... \n %s %s %d\n", +// n, (m_num_events * m_num_nodes), __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("%s", err_buf); fflush(stdout); +// string err_str = err_buf; throw (err_str); + +// } +// int pos_nwords = 0; +// for (int i = 1; i <= n ; i++) { +// int size = tmp_header.RAWHEADER_NWORDS +// + m_buffer[ pos_nwords + tmp_header.RAWHEADER_NWORDS + POS_DATA_LENGTH ] +// + SIZE_COPPER_DRIVER_HEADER +// + SIZE_COPPER_DRIVER_TRAILER +// + tmp_trailer.RAWTRAILER_NWORDS; +// // COPPER's data length include one word from COPPER trailer. so -1 is needed. +// pos_nwords += size; +// if (pos_nwords >= m_nwords) { +// char err_buf[500]; + +// sprintf(err_buf, "CORRUPTED DATA: value of pos_nwords(%d) is larger than m_nwords(%d). Exiting...\n %s %s %d\n", +// pos_nwords, m_nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("%s", err_buf); fflush(stdout); +// string err_str = err_buf; throw (err_str); +// // exit(1); +// } +// } +// return pos_nwords; +// } + + + +unsigned int PreRawCOPPERFormat_v1::CalcDriverChkSum(int n) +{ + int min = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS; + int max = GetBufferPos(n) + GetBlockNwords(n) + - tmp_trailer.RAWTRAILER_NWORDS - SIZE_COPPER_DRIVER_TRAILER; + unsigned int chksum = 0; + for (int i = min; i < max; i++) { + chksum ^= m_buffer[ i ]; + } + return chksum; +} + + + +int PreRawCOPPERFormat_v1::GetDetectorNwords(int n, int finesse_num) +{ + + int nwords = 0; + if (GetFINESSENwords(n, finesse_num) > 0) { + nwords = GetFINESSENwords(n, finesse_num) + - (SIZE_B2LHSLB_HEADER + SIZE_B2LHSLB_TRAILER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER); + } + return nwords; + +} + + +int PreRawCOPPERFormat_v1::GetFINESSENwords(int n, int finesse_num) +{ + if (!CheckCOPPERMagic(n)) { + char err_buf[500]; + PrintData(m_buffer, m_nwords); + sprintf(err_buf, + "[ERROR] COPPER's magic word is invalid. Exiting... Maybe it is due to data corruption or different version of the data format.\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + int pos_nwords; + switch (finesse_num) { + case 0 : + pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_A_DATA_LENGTH; + break; + case 1 : + pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_B_DATA_LENGTH; + break; + case 2 : + pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_C_DATA_LENGTH; + break; + case 3 : + pos_nwords = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS + POS_CH_D_DATA_LENGTH; + break; + default : + char err_buf[500]; + sprintf(err_buf, "Specifined FINESSE number( = %d ) is invalid. Exiting...\n %s %s %d\n", + finesse_num, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + return m_buffer[ pos_nwords ]; + +} + + +unsigned int PreRawCOPPERFormat_v1::GetB2LFEE32bitEventNumber(int n) +{ + +#ifndef READ_OLD_B2LFEE_FORMAT_FILE + + int err_flag = 0; + unsigned int eve_num = 0; + int flag = 0; + unsigned int eve[4]; + for (int i = 0; i < 4 ; i++) { + eve[ i ] = 12345678; + if (GetFINESSENwords(n, i) > 0) { + int pos_nwords = GetOffsetFINESSE(n, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG; + eve[ i ] = m_buffer[ pos_nwords ]; + if (flag != 1) eve_num = eve[ i ]; + if (eve_num != eve[ i ]) err_flag = 1; + flag = 1; + } + } + + if (flag == 0) { + PrintData(m_buffer, m_nwords); + char err_buf[500]; + sprintf(err_buf, "No HSLB data in COPPER data. Exiting...\n%s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (err_flag == 1) { + PrintData(m_buffer, m_nwords); + char err_buf[500]; + sprintf(err_buf, "CORRUPTED DATA: Different event number over HSLBs : slot A 0x%x : B 0x%x :C 0x%x : D 0x%x\n%s %s %d\n", + eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] [ERROR] %s\n", err_buf); fflush(stdout); +#ifndef NO_DATA_CHECK + B2FATAL(err_buf); +#endif //NO_DATA_CHECK + } + return eve_num; + +#else // READ_OLD_B2LFEE_FORMAT_FILE + + char err_buf[500]; + sprintf(err_buf, "You need comment out READ_OLD_B2LFEE_FORMAT_FILE if you are handling a new data format\n%s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + +#endif // READ_OLD_B2LFEE_FORMAT_FILE + +} + + +void PreRawCOPPERFormat_v1::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum_rawcprhdr, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + + char err_buf[500]; + sprintf(err_buf, + "This function for format ver.1 is not supported. (n %d preveve %u eve %u prectr %u ctr %u prevrun %u run %u) Exiting...\n %s %s %d\n", + n, prev_evenum, *cur_evenum_rawcprhdr, prev_copper_ctr, *cur_copper_ctr, + prev_exprunsubrun_no, *cur_exprunsubrun_no, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + +// char err_buf[500]; +// int err_flag = 0; +// // +// // check Magic words +// // +// if (!CheckCOPPERMagic(n)) { +// sprintf(err_buf, "CORRUPTED DATA: Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u\n%s %s %d\n", +// GetMagicDriverHeader(n), +// GetMagicFPGAHeader(n), +// GetMagicFPGATrailer(n), +// GetMagicDriverTrailer(n), +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } + +// // +// // Event # check +// // +// *cur_evenum_rawcprhdr = GetEveNo(n); +// unsigned int evenum_feehdr = GetB2LFEE32bitEventNumber(n); +// if (*cur_evenum_rawcprhdr != evenum_feehdr) { +// sprintf(err_buf, +// "CORRUPTED DATA: Event # in PreRawCOPPERFormat_v1 header and FEE header is different : cprhdr 0x%x feehdr 0x%x : Exiting...\n%s %s %d\n", +// *cur_evenum_rawcprhdr, evenum_feehdr, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } + +// // +// // Check incrementation of event # +// // +// *cur_runsubrun_no = GetRunNoSubRunNo(n); +// if ( +// #ifdef WO_FIRST_EVENUM_CHECK +// prev_evenum != 0xFFFFFFFF && *cur_evenum_rawcprhdr != 0 +// #else +// prev_runsubrun_no == *cur_runsubrun_no && prev_runsubrun_no >= 0 +// #endif +// ) { +// if ((unsigned int)(prev_evenum + 1) != *cur_evenum_rawcprhdr) { +// sprintf(err_buf, "CORRUPTED DATA: Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n%s %s %d\n", +// n, prev_evenum, *cur_evenum_rawcprhdr, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } +// } + + +// *cur_copper_ctr = GetCOPPERCounter(n); +// if ( +// #ifdef WO_FIRST_EVENUM_CHECK +// prev_copper_ctr != 0xFFFFFFFF +// #else +// true +// #endif +// ) { +// if ((unsigned int)(prev_copper_ctr + 1) != *cur_copper_ctr) { +// sprintf(err_buf, "COPPER counter jump : i %d prev 0x%x cur 0x%x :\n%s %s %d\n", +// n, prev_copper_ctr, *cur_copper_ctr, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); + +// #ifdef DESY +// // +// // In DESY test, we ignore this error +// // +// printf("[DEBUG] [INFO] %s", err_buf); +// #else +// err_flag = 1; +// #endif + +// } +// } + +// // +// // Check is utime and ctime_trgtype same over different FINESSE data +// // +// CheckUtimeCtimeTRGType(n); + + +// // +// // Check checksum calculated by COPPER driver +// // +// if (GetDriverChkSum(n) != CalcDriverChkSum(n)) { +// sprintf(err_buf, +// "CORRUPTED DATA: COPPER driver checkSum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x\n%s %s %d\n", +// n, +// GetBlockNwords(n), +// *cur_evenum_rawcprhdr, +// GetDriverChkSum(n), +// CalcDriverChkSum(n), +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } + + +// // +// // Check checksum calculated by DeSerializerCOPPER() +// // +// tmp_trailer.SetBuffer(GetRawTrlBufPtr(n)); +// unsigned int xor_chksum = CalcXORChecksum(GetBuffer(n), GetBlockNwords(n) - tmp_trailer.GetTrlNwords()); +// if (tmp_trailer.GetChksum() != xor_chksum) { +// sprintf(err_buf, +// "CORRUPTED DATA: PreRawCOPPERFormat_v1 checksum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x\n %s %s %d\n", +// n, GetBlockNwords(n), *cur_evenum_rawcprhdr, tmp_trailer.GetChksum(), xor_chksum, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } + + +// #ifdef DEBUG +// printf("[DEBUG] eve %d %d %d %d %d\n", +// GetEveNo(n), +// Get1stDetectorNwords(n), +// Get2ndDetectorNwords(n), +// Get3rdDetectorNwords(n), +// Get4thDetectorNwords(n) +// ); +// printf("[DEBUG] ===COPPER BLOCK==============\n"); +// printData(GetBuffer(n), GetBlockNwords(n)); + +// printf("[DEBUG] ===FINNESSE A ==============\n"); +// printData(Get1stDetectorBuffer(n), Get1stDetectorNwords(n)); + +// printf("[DEBUG] ===FINNESSE B ==============\n"); +// printData(Get2ndDetectorBuffer(n), Get2ndDetectorNwords(n)); + +// printf("[DEBUG] ===FINNESSE C ==============\n"); +// printData(Get3rdDetectorBuffer(n), Get3rdDetectorNwords(n)); + +// printf("[DEBUG] ===FINNESSE D ==============\n"); +// printData(Get4thDetectorBuffer(n), Get4thDetectorNwords(n)); +// printf("[DEBUG] === END ==============\n"); + +// #endif + +// if (err_flag == 1) { +// printf("[DEBUG] ========== dump a data blcok : block # %d==========\n", n); +// PrintData(GetBuffer(n), GetBlockNwords(n)); +// printf("%s", err_buf); fflush(stdout); +// string err_str = err_buf; +// throw (err_str); + +// // sleep(1234567); +// // exit(-1); +// } + + return; + +} + +bool PreRawCOPPERFormat_v1::CheckCOPPERMagic(int n) +{ + if (GetMagicDriverHeader(n) != COPPER_MAGIC_DRIVER_HEADER) { + return false; + } else if (GetMagicFPGAHeader(n) != COPPER_MAGIC_FPGA_HEADER) { + return false; + } else if (GetMagicFPGATrailer(n) != COPPER_MAGIC_FPGA_TRAILER) { + return false; + } else if (GetMagicDriverTrailer(n) != COPPER_MAGIC_DRIVER_TRAILER) { + return false; + } + return true; +} + +void PreRawCOPPERFormat_v1::CheckUtimeCtimeTRGType(int n) +{ + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int err_flag = 0; + int flag = 0; + unsigned int temp_utime = 0, temp_ctime_trgtype = 0; + unsigned int utime[4], ctime_trgtype[4]; + memset(utime, 0, sizeof(utime)); + memset(ctime_trgtype, 0, sizeof(ctime_trgtype)); + + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + ctime_trgtype[ i ] = m_buffer[ GetOffsetFINESSE(n, i) + + SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ]; + utime[ i ] = m_buffer[ GetOffsetFINESSE(n, i) + + SIZE_B2LHSLB_HEADER + POS_TT_UTIME ]; + if (flag == 0) { + temp_ctime_trgtype = ctime_trgtype[ i ]; + temp_utime = utime[ i ]; + flag = 1; + } else { + if (temp_ctime_trgtype != ctime_trgtype[ i ] + || temp_utime != utime[ i ]) { + err_flag = 1; + } + } + } + } + + if (err_flag != 0) { + for (int i = 0; i < 4; i++) { + printf("[DEBUG] FINESSE #=%d buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x\n", + i, GetFINESSENwords(n, i), ctime_trgtype[ i ], utime[ i ]); + } + char err_buf[500]; + sprintf(err_buf, "CORRUPTED DATA: mismatch over FINESSEs. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + return; +} + + +unsigned int PreRawCOPPERFormat_v1::FillTopBlockRawHeader(unsigned int m_node_id, unsigned int prev_eve32, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + + char err_buf[500]; + sprintf(err_buf, + "This function for format ver.1 is not supported. (node %.8x preveve %u prevrun %u currun %u ) Exiting...\n %s %s %d\n", + m_node_id, prev_eve32, prev_exprunsubrun_no, *cur_exprunsubrun_no, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + + return 0xffffffff; + +// const int datablock_id = 0; +// // m_temp_value = 12345678; +// // +// // This function only fills RawHeader contents for the first datablock. +// // # of block should be 1 +// if (m_num_nodes * m_num_events != 1) { +// char err_buf[500]; +// sprintf(err_buf, +// "This function should be used for PreRawCOPPERFormat_v1 containing only one datablock, while. this object has num_nodes of %d and num_events of %d\n %s %s %d\n", +// m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("%s", err_buf); fflush(stdout); +// string err_str = err_buf; throw (err_str); + +// } + +// ////////////////////////////////////////////////// +// // +// // Fill info in RawHeader +// // +// ////////////////////////////////////////////////// + +// // +// // Initialize a RawHeader part +// // +// memset(m_buffer, 0, sizeof(int) * tmp_header.RAWHEADER_NWORDS); +// m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] = tmp_header.RAWHEADER_NWORDS & tmp_header.HDR_NWORDS_MASK; +// m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & +// tmp_header.FORMAT_VERSION__MASK; +// m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= (0x80 << tmp_header.FORMAT_VERSION_SHIFT); // PreFormat +// m_buffer[ tmp_header.POS_VERSION_HDRNWORDS ] |= tmp_header.MAGIC_WORD; + +// // +// // Check FINESSEs which containes data +// // +// int* copper_buf = &(m_buffer[ tmp_header.RAWHEADER_NWORDS ]); +// if (copper_buf[ POS_CH_A_DATA_LENGTH ] == 0 && +// copper_buf[ POS_CH_B_DATA_LENGTH ] == 0 && +// copper_buf[ POS_CH_C_DATA_LENGTH ] == 0 && +// copper_buf[ POS_CH_D_DATA_LENGTH ] == 0) { +// char err_buf[500]; +// sprintf(err_buf, +// "No FINESSE data in a copper data block. Exiting...\n %s %s %d\n", +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("%s", err_buf); fflush(stdout); +// string err_str = err_buf; throw (err_str); +// // sleep(12345678); +// // exit(-1); +// } + +// // +// // Set total words info +// // +// int datablock_nwords = +// tmp_header.RAWHEADER_NWORDS + +// (copper_buf[ POS_DATA_LENGTH ] +// + SIZE_COPPER_DRIVER_HEADER +// + SIZE_COPPER_DRIVER_TRAILER) +// + tmp_trailer.RAWTRAILER_NWORDS; +// m_buffer[ tmp_header.POS_NWORDS ] = datablock_nwords; + + +// // +// // Check the consistency between data length and length in RawHeader +// // +// if (m_buffer[ tmp_header.POS_NWORDS ] != m_nwords) { +// char err_buf[500]; +// sprintf(err_buf, +// "CORRUPTED DATA: Data length is inconsistent m_nwords %d : nwords from COPPER data %d\n %s %s %d\n", +// m_nwords, m_buffer[ tmp_header.POS_NWORDS ], +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("%s", err_buf); fflush(stdout); +// string err_str = err_buf; throw (err_str); +// // sleep(12345678); +// // exit(-1); +// } + +// // +// // Fill offset values +// // +// int offset_1st_finesse = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; +// int offset_2nd_finesse = offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ]; +// int offset_3rd_finesse = offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ]; +// int offset_4th_finesse = offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ]; +// m_buffer[ tmp_header.POS_OFFSET_1ST_FINESSE ] = offset_1st_finesse; +// m_buffer[ tmp_header.POS_OFFSET_2ND_FINESSE ] = offset_2nd_finesse; +// m_buffer[ tmp_header.POS_OFFSET_3RD_FINESSE ] = offset_3rd_finesse; +// m_buffer[ tmp_header.POS_OFFSET_4TH_FINESSE ] = offset_4th_finesse; + +// // +// // Fill Exp/Run value +// // +// int* finesse_buf = & +// (m_buffer[ offset_1st_finesse ]); // In any finesse implementations, the top finesse buffer should be at offset_1st_finesse; +// m_buffer[ tmp_header.POS_EXP_RUN_NO ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_EXP_RUN ]; + + +// // +// // Fill event # +// // +// unsigned int cur_ftsw_eve32 = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_TAG ]; +// m_buffer[ tmp_header.POS_EVE_NO ] = cur_ftsw_eve32; + +// // +// // Copy FTSW words from B2LFEE header +// // +// m_buffer[ tmp_header.POS_TTCTIME_TRGTYPE ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ]; +// m_buffer[ tmp_header.POS_TTUTIME ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_UTIME ]; + +// // +// // Set node ID, trunc_mask, data_type +// // +// m_buffer[ tmp_header.POS_NODE_ID ] = m_node_id; +// m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] = ((m_trunc_mask << 31) & 0x80000000) | (m_data_type & 0x7FFFFFFF); + + +// ////////////////////////////////////////////////// +// // +// // Fill info in RawTrailer +// // +// ////////////////////////////////////////////////// + +// // +// // Calculate XOR checksum +// // +// unsigned int chksum_top = 0, chksum_body = 0, chksum_bottom = 0; + +// int top_end = tmp_header.RAWHEADER_NWORDS; +// for (int i = 0; i < top_end; i++) { +// chksum_top ^= m_buffer[ i ]; +// } +// int body_end = datablock_nwords - SIZE_COPPER_DRIVER_TRAILER - tmp_trailer.RAWTRAILER_NWORDS; +// for (int i = top_end; i < body_end; i++) { +// chksum_body ^= m_buffer[ i ]; +// } + +// int bottom_end = datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS; +// for (int i = body_end; i < bottom_end; i++) { +// chksum_bottom ^= m_buffer[ i ]; +// } + +// // +// // check COPPER driver checksum +// // +// if (chksum_body != (unsigned int)(m_buffer[ body_end ])) { +// char err_buf[500]; +// sprintf(err_buf, "CORRUPTED DATA: COPPER driver checksum is not consistent.: calcd. %.8x data %.8x\n %s %s %d\n", +// chksum_body, m_buffer[ body_end ], +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("%s", err_buf); fflush(stdout); +// string err_str = err_buf; throw (err_str); +// // sleep(12345678); +// // exit(-1); +// } + +// // +// // Fill trailer info (checksum, magic word) +// // +// unsigned int chksum = chksum_top ^ chksum_body ^ chksum_bottom; +// int* trl = &(m_buffer[ datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS ]); +// trl[ tmp_trailer.POS_CHKSUM ] = chksum; +// trl[ tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER; + + +// ////////////////////////////////////////////////// +// // +// // Data check ( magic word, event incrementation ) +// // +// ////////////////////////////////////////////////// + + +// // +// // check magic words +// // +// int* fpga_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_3); +// int* driver_trailer_magic = trl - (SIZE_COPPER_TRAILER - POS_MAGIC_COPPER_4); +// int err_flag = 0; +// if ((unsigned int)(copper_buf[ POS_MAGIC_COPPER_1 ]) != COPPER_MAGIC_DRIVER_HEADER) { +// err_flag = 1; +// } else if ((unsigned int)(copper_buf[ POS_MAGIC_COPPER_2 ]) != COPPER_MAGIC_FPGA_HEADER) { +// err_flag = 1; +// } else if ((unsigned int)(*fpga_trailer_magic) != COPPER_MAGIC_FPGA_TRAILER) { +// err_flag = 1; +// } else if ((unsigned int)(*driver_trailer_magic) != COPPER_MAGIC_DRIVER_TRAILER) { +// err_flag = 1; +// } +// if (err_flag == 1) { +// char err_buf[500]; +// sprintf(err_buf, "CORRUPTED DATA: Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u\n %s %s %d\n", +// GetMagicDriverHeader(datablock_id), +// GetMagicFPGAHeader(datablock_id), +// GetMagicFPGATrailer(datablock_id), +// GetMagicDriverTrailer(datablock_id), +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("[DEBUG] [ERROR] %s\n", err_buf); +// #ifndef NO_DATA_CHECK +// string err_str = err_buf; throw (err_str); + +// // sleep(12345678); +// // exit(-1); +// #endif +// } + +// // +// // check incrementation of event # +// // + +// if ((unsigned int)(prev_eve32 + 1) < 50) { +// // printf("#################EVE cur %.8x prev %.8x\n", cur_ftsw_eve32, prev_eve32); +// // fflush(stdout); +// } + + +// *cur_runsubrun_no = GetRunNoSubRunNo(datablock_id); +// if (prev_runsubrun_no == *cur_runsubrun_no && prev_runsubrun_no >= 0) { +// if ( +// #ifdef WO_FIRST_EVENUM_CHECK +// (prev_eve32 + 1 != cur_ftsw_eve32) && (prev_eve32 != 0xFFFFFFFF && cur_ftsw_eve32 != 0) +// #else +// prev_eve32 + 1 != cur_ftsw_eve32 +// #endif +// ) { +// #ifndef NO_DATA_CHECK +// char err_buf[500]; +// sprintf(err_buf, "CORRUPTED DATA: Invalid event_number. Exiting...: cur 32bit eve %u preveve %u prun %d crun %d\n %s %s %d\n", +// cur_ftsw_eve32, prev_eve32, +// prev_runsubrun_no, *cur_runsubrun_no, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("[DEBUG] [ERROR] %s\n", err_buf); + +// string err_str = err_buf; +// printf("[DEBUG] i= %d : num entries %d : Tot words %d\n", 0 , GetNumEntries(), TotalBufNwords()); +// PrintData(GetBuffer(datablock_id), TotalBufNwords()); + +// throw (err_str); +// // exit(-1); +// #endif + +// } +// } + +// return cur_ftsw_eve32; + +} + + + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 +void PreRawCOPPERFormat_v1::CheckB2LFEEHeaderVersion(int n) +{ + int* temp_buf; + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + temp_buf = GetFINESSEBuffer(n, i); + if ((temp_buf[ 3 ] & 0x40000000) == 0) { +#ifdef TEMP + // this word for exp/run + // old one (ver.1) used for SPring8 test in 2013 + printf("[DEBUG] \033[31m"); + printf("[DEBUG] ===Firmware ver. ERROR===\n "); + printf("[DEBUG] FTSW and b2tt firmwares was updated on Nov.22, 2013 and the header format attached by B2link was changed in the new firmwares.\n"); + printf("[DEBUG] If you are going to take data now, Please update the firmware.\n"); + printf("[DEBUG] For details, please see Nakao-san's e-mail [b2link_ml:0111] Re: [daq2ml:0159] beta version of trigger timing receiver firmware (b2tt) on bdaq SVN\n"); + printf("[DEBUG] Or if you are going to read data taken before the update, please use basf2 software before svn rev. 7419\n"); + printf("[DEBUG] About the format please see Nakao-san's B2GM slides(p. 13 and 15) http://kds.kek.jp/getFile.py/access?contribId=143&sessionId=38&resId=0&materialId=slides&confId=13911.\n"); + printf("[DEBUG] Sorry for inconvenience.\n"); + printf("[DEBUG] \033[0m"); + fflush(stderr); + char err_buf[500]; + sprintf(err_buf, "FTSW and b2tt firmwares are old. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif + } else { + // this word for 32bit unixtime + // new one (ver.2) + break; + } + } + + if (i == 3) { +#ifdef TEMP + char err_buf[500]; + sprintf(err_buf, "PreRawCOPPERFormat_v1 contains no FINESSE data. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif + } + } + return; +} +#endif + + +//int PreRawCOPPERFormat_v1::CalcReducedDataSize(RawDataBlock* raw_datablk) +int PreRawCOPPERFormat_v1::CalcReducedDataSize(int* bufin, int nwords, int num_events, int num_nodes) +{ + // + // Calculate reduced length for a total RawDataBlock (containing multiple data blocks) + // + PreRawCOPPERFormat_v1 rawcpr_fmt; + int delete_flag = 0; + rawcpr_fmt.SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes); + + int reduced_nwords = 0; + for (int k = 0; k < rawcpr_fmt.GetNumEvents(); k++) { + int num_nodes_in_sendblock = rawcpr_fmt.GetNumNodes(); + for (int l = 0; l < num_nodes_in_sendblock; l++) { + int entry_id = l + k * num_nodes_in_sendblock; + if (rawcpr_fmt.CheckFTSWID(entry_id) || rawcpr_fmt.CheckTLUID(entry_id)) { + // No size reduction for non-COPPER data ( FTSW and TLU data blocks ) + reduced_nwords += rawcpr_fmt.GetBlockNwords(entry_id); + } else { + PreRawCOPPERFormat_v1 temp_prerawcpr; + int temp_delete_flag = 0, temp_num_eve = 1, temp_num_nodes = 1; + + // Call CalcReducedNwords + temp_prerawcpr.SetBuffer(rawcpr_fmt.GetBuffer(entry_id), + rawcpr_fmt.GetBlockNwords(entry_id), + temp_delete_flag, temp_num_eve, + temp_num_nodes); + reduced_nwords += temp_prerawcpr.CalcReducedNwords(0); + } + } + } + return reduced_nwords; + +} + +//void PreRawCOPPERFormat_v1::CopyReducedData(RawDataBlock* raw_datablk, int* buf_to, int delete_flag_from) +void PreRawCOPPERFormat_v1::CopyReducedData(int* bufin, int nwords, int num_events, int num_nodes, int* buf_to, int* nwords_to) +{ + // + // Make a reduced buffer a total RawDataBlock (containing multiple data blocks) + // + PreRawCOPPERFormat_v1 rawcpr_fmt; + int delete_flag = 0; + rawcpr_fmt.SetBuffer(bufin, nwords, delete_flag, num_events, num_nodes); + + int pos_nwords_to = 0; + for (int k = 0; k < rawcpr_fmt.GetNumEvents(); k++) { + int num_nodes_in_sendblock = rawcpr_fmt.GetNumNodes(); + for (int l = 0; l < num_nodes_in_sendblock; l++) { + int entry_id = l + k * num_nodes_in_sendblock; + if (rawcpr_fmt.CheckFTSWID(entry_id) || + rawcpr_fmt.CheckTLUID(entry_id)) { + rawcpr_fmt.CopyBlock(entry_id, buf_to + pos_nwords_to); + pos_nwords_to += rawcpr_fmt.GetBlockNwords(entry_id); + + } else { + SetBuffer(rawcpr_fmt.GetBuffer(entry_id), + rawcpr_fmt.GetBlockNwords(entry_id), 0, 1, 1); + + pos_nwords_to += CopyReducedBuffer(0, buf_to + pos_nwords_to); + + + } + } + } + +// int* buf_from = raw_datablk->GetWholeBuffer(); +// raw_datablk->SetBuffer(buf_to, pos_nwords_to, 0, +// raw_datablk->GetNumEvents(), raw_datablk->GetNumNodes()); +// if (delete_flag_from == 1) { delete[] buf_from;} + *nwords_to = pos_nwords_to; + return ; +} + + +int PreRawCOPPERFormat_v1::CalcReducedNwords(int n) +{ + // + // Calculate reduced length for one data block which is a part of a RawDataBlock + // + + int nwords_to = 0; + + //RawCOPPER header + nwords_to += tmp_header.RAWHEADER_NWORDS; + + for (int j = 0; j < 4; j++) { + + int finesse_nwords = GetFINESSENwords(n, j); + if (finesse_nwords > 0) { + // + // B2L(HSLB/FEE) header and trailers are resized + // m_reduced_rawcpr should be PostRawCOPPERFormat_v1 + // + nwords_to += + finesse_nwords + - (SIZE_B2LHSLB_HEADER - m_reduced_rawcpr.SIZE_B2LHSLB_HEADER) + - (SIZE_B2LFEE_HEADER - m_reduced_rawcpr.SIZE_B2LFEE_HEADER) + - (SIZE_B2LFEE_TRAILER - m_reduced_rawcpr.SIZE_B2LFEE_TRAILER) + - (SIZE_B2LHSLB_TRAILER - m_reduced_rawcpr.SIZE_B2LHSLB_TRAILER); + } + + } + + //RawCOPPER Trailer + nwords_to += tmp_trailer.GetTrlNwords(); + + return nwords_to; +} + + + + + + +int PreRawCOPPERFormat_v1::CopyReducedBuffer(int n, int* buf_to) +{ + // + // Make a reduced buffer for one data block which is a part of a RawDataBlock + // + + int* buf_from = NULL; + int nwords_buf_to = CalcReducedNwords(n); + int pos_nwords_to = 0; + int copy_nwords = 0; + + // copyt to ReducedRawCOPPER + // ReducedRawCOPPER m_reduced_rawcpr; + //Header copy + copy_nwords = tmp_header.RAWHEADER_NWORDS; + buf_from = GetBuffer(n); + copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to); + + // Unset the PreFormat bit ( 15th bit ) + buf_to[ tmp_header.POS_VERSION_HDRNWORDS ] &= 0xFFFF7FFF; + + //Check Header + // m_reduced_rawcpr.tmp_header.CheckHeader(buf_to + pos_nwords_to - copy_nwords); + + + // copy FINESSE buffer + int pos_nwords_finesse[ 4 ]; + for (int j = 0; j < 4; j++) { + pos_nwords_finesse[ j ] = pos_nwords_to; + if (GetFINESSENwords(n, j) > 0) { + int* finesse_buf = GetFINESSEBuffer(n, j); + int finesse_nwords = GetFINESSENwords(n, j); + + + CheckB2LHSLBMagicWords(finesse_buf, finesse_nwords); + // copy the whole B2LHSLB header (1word) + buf_to[ pos_nwords_to ] = finesse_buf[ POS_MAGIC_B2LHSLB ]; + pos_nwords_to++; + + // copy the last word of B2LFEE and body( DetectorBuffer ) + buf_from = + finesse_buf + + SIZE_B2LHSLB_HEADER + + POS_B2L_CTIME; //the last word of B2LFEE + + // check finesse buffer size : ( When dumhslb was used, this type of error occured and RecvStream0.py died by Segmentation Fault. ) 2014.12.01. + if (finesse_nwords - SIZE_B2LHSLB_HEADER - SIZE_B2LFEE_HEADER + - SIZE_B2LFEE_TRAILER - SIZE_B2LHSLB_TRAILER < 0) { + char err_buf[500]; + sprintf(err_buf, + "[ERROR] Finesse buffer size is too small( %d words < %d words). May be the data are corrupted. Exiting...\n %s %s %d\n", + finesse_nwords, SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + copy_nwords = + finesse_nwords + - SIZE_B2LHSLB_HEADER // already copied + - POS_B2L_CTIME // only one word copied ( SIZE_B2LFEE_HEADER - 1 = POS_B2L_CTIME ) + - SIZE_B2LFEE_TRAILER // will be copied later + - SIZE_B2LHSLB_TRAILER; // Not copied + + // printf("pos %d nwords %d nwords to %d\n", pos_nwords_to, copy_nwords, nwords_buf_to ); + copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to); + + //copy B2LFEE trailer(CRC info) + buf_to[ pos_nwords_to ] = + finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + - (SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) ]; + pos_nwords_to++; + + // check CRC data + // CheckCRC16( n, j ); + } + } + + // copy RawCOPPER trailer + buf_from = + GetBuffer(n) + + GetBlockNwords(n) + - tmp_trailer.GetTrlNwords(); + copy_nwords = tmp_trailer.GetTrlNwords(); + copyData(buf_to, &pos_nwords_to, buf_from, copy_nwords, nwords_buf_to); + + // length check + if (pos_nwords_to != nwords_buf_to) { + char err_buf[500]; + sprintf(err_buf, "Buffer overflow. Exiting... %d %d\n", pos_nwords_to, nwords_buf_to); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + // + // Apply changes followed by data size reduction + // + *(buf_to + m_reduced_rawcpr.tmp_header.POS_NWORDS) = nwords_buf_to; + *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_1ST_FINESSE) = pos_nwords_finesse[ 0 ]; + *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_2ND_FINESSE) = pos_nwords_finesse[ 1 ]; + *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_3RD_FINESSE) = pos_nwords_finesse[ 2 ]; + *(buf_to + m_reduced_rawcpr.tmp_header.POS_OFFSET_4TH_FINESSE) = pos_nwords_finesse[ 3 ]; + + + // Recalculate XOR checksum in a RawCOPPER trailer + *(buf_to + pos_nwords_to - tmp_trailer.GetTrlNwords() + tmp_trailer.POS_CHKSUM) = + CalcXORChecksum(buf_to, pos_nwords_to - tmp_trailer.GetTrlNwords()); + + // + // data block # check + // + m_reduced_rawcpr.SetBuffer(buf_to, nwords_buf_to, 0, GetNumEvents(), GetNumNodes()); + if (m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes() <= 0) { + char err_buf[500]; + sprintf(err_buf, "Invalid data block numbers.(# of events %d, # of nodes %d) Exiting...\n", + m_reduced_rawcpr.GetNumEvents(), m_reduced_rawcpr.GetNumNodes()); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + // + // check no-data buffer + // + for (int i = 0; i < m_reduced_rawcpr.GetNumEvents() * m_reduced_rawcpr.GetNumNodes(); i++) { + int nonzero_finesse_buf = 0; + for (int j = 0; j < 4; j++) { + + if (GetFINESSENwords(n, j) > 0) { + // m_reduced_rawcpr.CheckCRC16(i, j); + nonzero_finesse_buf++; + } + } + if (nonzero_finesse_buf == 0) { + char err_buf[500]; + sprintf(err_buf, "No non-zero FINESSE buffer. Exiting...\n"); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + } + + // post_rawcopper_v1.CheckCRC16(0, 0); + // printf("fROM =======================================\n"); + // for (int k = 0; k < nwords_buf_to; k++) { + // printf(" %.8x", GetBuffer(n)[ k ]); + // if ( ( k + 1 ) % 10 == 0) { + // printf("\n"); + // } + // } + // printf("\n"); + + // printf("tO =======================================\n"); + // for (int k = 0; k < nwords_buf_to; k++) { + // printf(" %.8x", buf_to[ k ]); + // if ( ( k + 1 ) % 10 == 0) { + // printf("\n"); + // } + // } + // printf("\n"); + // printf("=============================================\n"); + return pos_nwords_to; +} + + +int PreRawCOPPERFormat_v1::CheckB2LHSLBMagicWords(int* finesse_buf, int finesse_nwords) +{ + + if ((finesse_buf[ POS_MAGIC_B2LHSLB ] & 0xFFFF0000) == B2LHSLB_HEADER_MAGIC && + ((finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ] & 0xFFFF0000) + == B2LHSLB_TRAILER_MAGIC)) { + return 1; + } else { + PrintData(m_buffer, m_nwords); + char err_buf[500]; + sprintf(err_buf, "Invalid B2LHSLB magic words 0x%x 0x%x. Exiting... :%s %s %d\n", + finesse_buf[ POS_MAGIC_B2LHSLB ], + finesse_buf[ finesse_nwords - SIZE_B2LHSLB_TRAILER + POS_CHKSUM_B2LHSLB ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; + } +} + + + +int PreRawCOPPERFormat_v1::CheckCRC16(int n, int finesse_num) +{ + // + // Calculate CRC16 + // + int* buf = GetFINESSEBuffer(n, finesse_num) + SIZE_B2LHSLB_HEADER; + int nwords = GetFINESSENwords(n, finesse_num) - (SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER); + unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, buf, nwords); + + // + // Compare CRC16 with B2LCRC16 + // + buf = GetFINESSEBuffer(n, finesse_num) + GetFINESSENwords(n, finesse_num) + - ((SIZE_B2LFEE_TRAILER - POS_CHKSUM_B2LFEE) + SIZE_B2LHSLB_TRAILER) ; + + // printf("PreRawCOPPER : Eve %.8x B2LCRC16 %.8x calculated CRC16 %.8x\n", GetEveNo(n), *buf, temp_crc16 ); + + if ((unsigned short)(*buf & 0xFFFF) != temp_crc16) { + printf("PRE CRC16 error : B2LCRC16 %x Calculated CRC16 %x : Nwords of FINESSE buf %d\n", + *buf , temp_crc16, GetFINESSENwords(n, finesse_num)); + int* temp_buf = GetFINESSEBuffer(n, finesse_num); + for (int k = 0; k < GetFINESSENwords(n, finesse_num); k++) { + printf("%.8x ", temp_buf[ k ]); + if ((k + 1) % 10 == 0) { + printf("\n"); + } + } + printf("\n"); + fflush(stdout); + + char err_buf[500]; + sprintf(err_buf, + "[DEBUG] [ERROR] B2LCRC16 (%.4x) differs from one ( %.4x) calculated by PreRawCOPPERfromat class. Exiting...\n %s %s %d\n", + (unsigned short)(*buf & 0xFFFF), temp_crc16, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + return 1; + +} + +int* PreRawCOPPERFormat_v1::PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcpr_info) +{ + int* packed_buf = NULL; + + int poswords_to = 0; + int* detector_buf[ 4 ] = { detector_buf_1st, detector_buf_2nd, detector_buf_3rd, detector_buf_4th }; + int nwords[ 4 ] = { nwords_1st, nwords_2nd, nwords_3rd, nwords_4th }; + + // calculate the event length + int length_nwords = tmp_header.GetHdrNwords() + SIZE_COPPER_HEADER + SIZE_COPPER_TRAILER + tmp_trailer.GetTrlNwords(); + + for (int i = 0; i < 4; i++) { + if (detector_buf[ i ] == NULL || nwords[ i ] <= 0) continue; // for an empty FINESSE slot + length_nwords += nwords[ i ]; + length_nwords += SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + // allocate buffer + packed_buf = new int[ length_nwords ]; + memset(packed_buf, 0, sizeof(int) * length_nwords); + + // + // Fill RawHeader + // + tmp_header.SetBuffer(packed_buf); + + packed_buf[ tmp_header.POS_NWORDS ] = length_nwords; // total length + packed_buf[ tmp_header.POS_VERSION_HDRNWORDS ] = 0x7f7f8000 + | ((DATA_FORMAT_VERSION << tmp_header.FORMAT_VERSION_SHIFT) & tmp_header.FORMAT_VERSION__MASK) + | tmp_header.RAWHEADER_NWORDS; // ver.#, header length + + packed_buf[ tmp_header.POS_EXP_RUN_NO ] = (rawcpr_info.exp_num << 22) + | (rawcpr_info.run_subrun_num & 0x003FFFFF); // exp. and run # + packed_buf[ tmp_header.POS_EVE_NO ] = rawcpr_info.eve_num; // eve # + packed_buf[ tmp_header.POS_TTCTIME_TRGTYPE ] = (rawcpr_info.tt_ctime & 0x7FFFFFF) << 4; // tt_ctime + packed_buf[ tmp_header.POS_TTUTIME ] = rawcpr_info.tt_utime; // tt_utime + packed_buf[ tmp_header.POS_NODE_ID ] = rawcpr_info.node_id; // node ID + + + + + // fill the positions of finesse buffers + packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ] = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; + + packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_1ST_FINESSE ]; + if (nwords[ 0 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ] += + nwords[ 0 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_2ND_FINESSE ]; + if (nwords[ 1 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ] += + nwords[ 1 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + } + + packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] = packed_buf[ tmp_header.POS_OFFSET_3RD_FINESSE ]; + if (nwords[ 2 ] > 0) { + packed_buf[ tmp_header.POS_OFFSET_4TH_FINESSE ] += nwords[ 2 ] + SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + + SIZE_B2LHSLB_TRAILER; + } + poswords_to += tmp_header.GetHdrNwords(); + + // Fill COPPER header + packed_buf[ poswords_to + POS_MAGIC_COPPER_1 ] = COPPER_MAGIC_DRIVER_HEADER; + packed_buf[ poswords_to + POS_MAGIC_COPPER_2 ] = COPPER_MAGIC_FPGA_HEADER; + packed_buf[ poswords_to + POS_EVE_NUM_COPPER ] = rawcpr_info.eve_num; + + int size_b2l_hdrtrl = SIZE_B2LHSLB_HEADER + SIZE_B2LFEE_HEADER + SIZE_B2LFEE_TRAILER + SIZE_B2LHSLB_TRAILER; + if (nwords[ 0 ] != 0) packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] = nwords[ 0 ] + size_b2l_hdrtrl; + if (nwords[ 1 ] != 0) packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] = nwords[ 1 ] + size_b2l_hdrtrl; + if (nwords[ 2 ] != 0) packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] = nwords[ 2 ] + size_b2l_hdrtrl; + if (nwords[ 3 ] != 0) packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] = nwords[ 3 ] + size_b2l_hdrtrl; + + packed_buf[ poswords_to + POS_DATA_LENGTH ] = + packed_buf[ poswords_to + POS_CH_A_DATA_LENGTH ] + + packed_buf[ poswords_to + POS_CH_B_DATA_LENGTH ] + + packed_buf[ poswords_to + POS_CH_C_DATA_LENGTH ] + + packed_buf[ poswords_to + POS_CH_D_DATA_LENGTH ] + + (SIZE_COPPER_HEADER - SIZE_COPPER_DRIVER_HEADER) + + (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER); + + poswords_to += SIZE_COPPER_HEADER; + + // Fill FINESSE buffer + for (int i = 0; i < 4; i++) { + + if (detector_buf[ i ] == NULL || nwords[ i ] <= 0) continue; // for an empty FINESSE slot + + // Fill b2link HSLB header + packed_buf[ poswords_to + POS_MAGIC_B2LHSLB ] = 0xffaa0000 | (0xffff & rawcpr_info.eve_num); + poswords_to += SIZE_B2LHSLB_HEADER; + int* crc16_start = &(packed_buf[ poswords_to ]); + + // Fill b2link FEE header + + packed_buf[ poswords_to + POS_TT_CTIME_TYPE ] = (rawcpr_info.tt_ctime & 0x7FFFFFF) << 4; + packed_buf[ poswords_to + POS_TT_TAG ] = rawcpr_info.eve_num; + packed_buf[ poswords_to + POS_TT_UTIME ] = rawcpr_info.tt_utime; + packed_buf[ poswords_to + POS_EXP_RUN ] = (rawcpr_info.exp_num << 22) | (rawcpr_info.run_subrun_num & + 0x003FFFFF); // exp. and run # + packed_buf[ poswords_to + POS_B2L_CTIME ] = (rawcpr_info.b2l_ctime & 0x7FFFFFF) << 4; + poswords_to += SIZE_B2LFEE_HEADER; + + // copy the 1st Detector Buffer + memcpy(packed_buf + poswords_to, detector_buf[ i ], nwords[ i ]*sizeof(int)); + poswords_to += nwords[ i ]; + + // Fill b2link FEE trailer + unsigned short crc16 = CalcCRC16LittleEndian(0xffff, crc16_start, nwords[ i ] + SIZE_B2LFEE_HEADER); + packed_buf[ poswords_to + POS_CHKSUM_B2LFEE ] = ((0xffff & rawcpr_info.eve_num) << 16) | (crc16 & 0xffff); + poswords_to += SIZE_B2LFEE_TRAILER; + + // Fill b2link HSLB trailer + packed_buf[ poswords_to + POS_CHKSUM_B2LHSLB ] = 0xff550000; + poswords_to += SIZE_B2LHSLB_TRAILER; + + } + + // Fill COPPER trailer + packed_buf[ poswords_to + POS_MAGIC_COPPER_3 ] = COPPER_MAGIC_FPGA_TRAILER; + packed_buf[ poswords_to + POS_MAGIC_COPPER_4 ] = COPPER_MAGIC_DRIVER_TRAILER; + unsigned int chksum = 0; + for (int i = tmp_header.GetHdrNwords(); i < poswords_to + (SIZE_COPPER_TRAILER - SIZE_COPPER_DRIVER_TRAILER); i++) { + chksum ^= packed_buf[ i ]; + } + packed_buf[ poswords_to + POS_CHKSUM_COPPER ] = chksum; + poswords_to += SIZE_COPPER_TRAILER; + + // Calculate RawCOPPER checksum and fill RawTrailer + chksum = 0; + for (int i = 0; i < poswords_to; i++) { + chksum ^= packed_buf[ i ]; + } + packed_buf[ poswords_to + tmp_trailer.POS_CHKSUM ] = chksum; + + packed_buf[ poswords_to + tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER; + poswords_to += tmp_trailer.GetTrlNwords(); + + *packed_buf_nwords = poswords_to; + return packed_buf; +} + diff --git a/ROPCApplications/rawdata/dataobjects/src/RawARICH.cc b/ROPCApplications/rawdata/dataobjects/src/RawARICH.cc new file mode 100644 index 0000000000000000000000000000000000000000..d19e180a2927626ed8cfe2b65bb58ac982d1aa47 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawARICH.cc @@ -0,0 +1,20 @@ +//+ +// File : RawCOPPER.cc +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawARICH.h> + +using namespace std; +using namespace Belle2; + +RawARICH::RawARICH() +{ +} + +RawARICH::~RawARICH() +{ +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawCDC.cc b/ROPCApplications/rawdata/dataobjects/src/RawCDC.cc new file mode 100644 index 0000000000000000000000000000000000000000..645ec5e924b4e22b194e68117eaeacc3d22011d4 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawCDC.cc @@ -0,0 +1,20 @@ +//+ +// File : RawCOPPER.cc +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawCDC.h> + +using namespace std; +using namespace Belle2; + +RawCDC::RawCDC() +{ +} + +RawCDC::~RawCDC() +{ +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawCOPPER.cc b/ROPCApplications/rawdata/dataobjects/src/RawCOPPER.cc new file mode 100644 index 0000000000000000000000000000000000000000..40b6bc21b6b05c1f91dfab2906394e1b838fa47a --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawCOPPER.cc @@ -0,0 +1,267 @@ +//+ +// File : RawCOPPER.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawCOPPER.h> + +#include <framework/utilities/HTML.h> +#include <sstream> + + +using namespace std; +using namespace Belle2; + +//#define DESY +//#define NO_DATA_CHECK +//#define WO_FIRST_EVENUM_CHECK + +RawCOPPER::RawCOPPER() +{ + m_access = NULL; + m_version = -1; +} + +RawCOPPER::~RawCOPPER() +{ + if (m_access != NULL) delete m_access; + m_access = NULL; +} + +void RawCOPPER::SetVersion() +{ + + if (m_buffer == NULL) { + char err_buf[500]; + sprintf(err_buf, "m_buffer is NULL. Exiting..."); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (m_access != NULL) { + delete m_access; + } + + m_version = ((m_buffer[ POS_FORMAT_VERSION ]) & FORMAT_MASK) >> 8; + switch (m_version) { + case LATEST_POSTREDUCTION_FORMAT_VER : + m_access = new PostRawCOPPERFormat_latest; + // printf("Calling PostRawCOPPERFormat_latest\n"); + break; + case (0x80 + LATEST_POSTREDUCTION_FORMAT_VER) : + m_access = new PreRawCOPPERFormat_latest; + // printf("Calling PreRawCOPPERFormat_latest\n"); + break; + case 0x1 : + m_access = new PostRawCOPPERFormat_v1; + // printf("Calling RawCOPPERFormat_v1\n"); + break; + case (0x80 + 0x1) : + m_access = new PreRawCOPPERFormat_v1; + // printf("Calling PreRawCOPPERFormat_latest\n"); + break; + case 0 : + m_access = new RawCOPPERFormat_v0; + // printf("Calling RawCOPPERFormat_v0\n"); + break; + default : { + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : Invalid version of a data format(0x%.8x = 0x7f7f**..). Exiting...\n %s %s %d\n", + m_buffer[ POS_FORMAT_VERSION ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); // to reduce multiple error messages + } + } + + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + + + +} + +void RawCOPPER::SetVersion(string class_name) +{ + + if (m_access != NULL) { + delete m_access; + } + + if (class_name == "PostRawCOPPERFormat_latest") { + m_access = new PostRawCOPPERFormat_latest; + m_version = (0 << 7) | LATEST_POSTREDUCTION_FORMAT_VER; + } else if (class_name == "PreRawCOPPERFormat_latest") { + m_access = new PreRawCOPPERFormat_latest; + m_version = (1 << 7) | LATEST_POSTREDUCTION_FORMAT_VER; + } else if (class_name == "PostRawCOPPERFormat_v1") { + m_access = new PostRawCOPPERFormat_v1; + m_version = (0 << 7) | 1; + } else if (class_name == "PreRawCOPPERFormat_v1") { + m_access = new PreRawCOPPERFormat_v1; + m_version = (1 << 7) | 1; + } else if (class_name == "RawCOPPERFormat_v0") { + m_access = new RawCOPPERFormat_v0; + m_version = (0 << 7) | 0; + } else { + char err_buf[500]; + sprintf(err_buf, "Invalid name of a data format class (%s). Exiting...\n %s %s %d\n", + class_name.c_str(), __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + +// if( class_name == "RawCOPPERFormat_v0" ){ +// m_access = new RawCOPPERFormat_v0; +// m_version = ( 0 << 7 ) | 0; +// }else if( class_name == "PreRawCOPPERFormat_v0" ){ +// m_access = new PreRawCOPPERFormat_v0; +// m_version = ( 1 << 7 ) | 0; +// }else if( class_name == "RawCOPPERFormat_v2" ){ +// m_access = new RawCOPPERFormat_v2; +// m_version = ( 0 << 7 ) | 2; +// }else if( class_name == "PreRawCOPPERFormat_v2" ){ +// m_access = new PreRawCOPPERFormat_v2; +// m_version = ( 1 << 7 ) | 2; +// } + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + +} + +void RawCOPPER::SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes) +{ + if (bufin == NULL) { + char err_buf[500]; + sprintf(err_buf, "[DEBUG] bufin is NULL. Exting...\n"); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (!m_use_prealloc_buf && m_buffer != NULL) delete[] m_buffer; + + if (delete_flag == 0) { + m_use_prealloc_buf = true; + } else { + m_use_prealloc_buf = false; + } + + m_nwords = nwords; + m_buffer = bufin; + + m_num_nodes = num_nodes; + m_num_events = num_events; + + SetVersion(); + +} + + +void RawCOPPER::ShowBuffer() +{ + + printf("\n"); + printf("POINTER %p\n", m_buffer); + for (int k = 0; k < 100; k++) { + printf("%.8x ", m_buffer[ k ]); + if ((k + 1) % 10 == 0) printf("\n%.8x : ", k); + } + printf("\n"); + +} + + +void RawCOPPER::PackDetectorBuf( + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info) +{ + + if (m_access != NULL) { + delete m_access; + } + m_access = new PostRawCOPPERFormat_latest; + m_version = LATEST_POSTREDUCTION_FORMAT_VER; + m_num_events = 1; + m_num_nodes = 1; + + int* packed_buf = NULL; + packed_buf = m_access->PackDetectorBuf(&m_nwords, + detector_buf_1st, nwords_1st, + detector_buf_2nd, nwords_2nd, + detector_buf_3rd, nwords_3rd, + detector_buf_4th, nwords_4th, + rawcprpacker_info); + + int delete_flag = 1; // Not use preallocated buffer. Delete m_buffer when destructer is called. + SetBuffer(packed_buf, m_nwords, delete_flag, m_num_events, m_num_nodes); + + delete_flag = 0; // For m_access, need not to delete m_buffer + m_access->SetBuffer(m_buffer, m_nwords, delete_flag, m_num_events, m_num_nodes); + + return; +} + + +void RawCOPPER::PackDetectorBuf4DummyData( + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info) +{ + + if (m_access != NULL) { + delete m_access; + } + m_access = new PreRawCOPPERFormat_latest; + m_version = LATEST_POSTREDUCTION_FORMAT_VER; + m_num_events = 1; + m_num_nodes = 1; + + int* packed_buf = NULL; + packed_buf = m_access->PackDetectorBuf(&m_nwords, + detector_buf_1st, nwords_1st, + detector_buf_2nd, nwords_2nd, + detector_buf_3rd, nwords_3rd, + detector_buf_4th, nwords_4th, + rawcprpacker_info); + + int delete_flag = 1; // Not use preallocated buffer. Delete m_buffer when destructer is called. + SetBuffer(packed_buf, m_nwords, delete_flag, m_num_events, m_num_nodes); + + delete_flag = 0; // For m_access, need not to delete m_buffer + m_access->SetBuffer(m_buffer, m_nwords, delete_flag, m_num_events, m_num_nodes); + + return; +} + +std::string RawCOPPER::getInfoHTML() const +{ + std::stringstream s; + //TODO: All these methods should be const instead. + RawCOPPER* nonconst_this = const_cast<RawCOPPER*>(this); + nonconst_this->CheckVersionSetBuffer(); + + const int nEntries = nonconst_this->GetNumEntries(); + s << "Entries: " << nEntries; + s << ", Total size (32bit words): " << m_nwords << "<br>"; + + s << "COPPER format version: " << m_version; + + const char a = 'A'; //finesses are counted from A to D + for (int iEntry = 0; iEntry < nEntries; ++iEntry) { + s << "<h4>Entry " << iEntry << "</h4>"; + s << "Node ID: 0x" << std::hex << nonconst_this->GetNodeID(iEntry) << std::dec; + for (int iFinesse = 0; iFinesse < 4; iFinesse++) { + const int nWords = nonconst_this->GetDetectorNwords(iEntry, iFinesse); + const int* buf = nonconst_this->GetDetectorBuffer(iEntry, iFinesse); + s << "<p>Finesse " << char(a + iFinesse) << " (Size: " << nWords << ")</p>"; + s << HTML::getHexDump(buf, nWords) << "<br>"; + } + } + + return s.str(); +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat.cc b/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat.cc new file mode 100644 index 0000000000000000000000000000000000000000..262afe5c41a83892a74d4eaeb0fd7dbaeef27e8f --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat.cc @@ -0,0 +1,195 @@ +//+ +// File : RawCOPPERFormat.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 3 - July - 2014 +//- + +#include <rawdata/dataobjects/RawCOPPERFormat.h> + +using namespace std; +using namespace Belle2; +//ClassImp(RawCOPPERFormat); + + +RawCOPPERFormat::RawCOPPERFormat() +{ +// m_nwords = 0; +// m_num_nodes = 0; +// m_num_events = 0; +// m_buffer = NULL; +} + +unsigned int RawCOPPERFormat::CalcXORChecksum(int* buf, int nwords) +{ + unsigned int checksum = 0; + for (int i = 0; i < nwords; i++) { + checksum = checksum ^ buf[ i ]; + } + return checksum; +} + + + + +int RawCOPPERFormat::GetOffsetFINESSE(int n, int finesse_num) +{ + switch (finesse_num) { + case 0 : + return GetOffset1stFINESSE(n); + break; + case 1 : + return GetOffset2ndFINESSE(n); + break; + case 2 : + return GetOffset3rdFINESSE(n); + break; + case 3 : + return GetOffset4thFINESSE(n); + break; + default : + break; + } + + char err_buf[500]; + sprintf(err_buf, "[FATAL] Specifined FINESSE number( = %d ) is invalid. Exiting...\n%s %s %d\n", finesse_num, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +} + + + + +int* RawCOPPERFormat::GetFINESSEBuffer(int n, int finesse_num) +{ + switch (finesse_num) { + case 0 : + return Get1stFINESSEBuffer(n); + break; + case 1 : + return Get2ndFINESSEBuffer(n); + break; + case 2 : + return Get3rdFINESSEBuffer(n); + break; + case 3 : + return Get4thFINESSEBuffer(n); + break; + default : + break; + } + + char err_buf[500]; + sprintf(err_buf, "[FATAL] Specifined FINESSE number( = %d ) is invalid. Exiting...\n%s %s %d\n", finesse_num, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +} + + + + +int* RawCOPPERFormat::GetDetectorBuffer(int n, int finesse_num) +{ + switch (finesse_num) { + case 0 : + return Get1stDetectorBuffer(n); + break; + case 1 : + return Get2ndDetectorBuffer(n); + break; + case 2 : + return Get3rdDetectorBuffer(n); + break; + case 3 : + return Get4thDetectorBuffer(n); + break; + default : + break; + } + + char err_buf[500]; + sprintf(err_buf, "[FATAL] Specifined FINESSE number( = %d ) is invalid. Exiting...\n%s %s %d\n", finesse_num, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +} + + + +unsigned int RawCOPPERFormat::GetB2LHeaderWord(int n, int finesse_buffer_pos) +{ + unsigned int word[4]; + unsigned int ret_word = 0; + int flag = 0, err_flag = 0; + + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + word[ i ] = m_buffer[ GetOffsetFINESSE(n, i) + finesse_buffer_pos ]; + if (flag != 0 && (ret_word != word[ i ])) { + err_flag = 1; + } + ret_word = word[ i ]; + flag = 1; + } + } + + if (flag == 0) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : No HSLB data in COPPER data. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (err_flag == 1) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : Different event number over HSLBs : slot A 0x%x : B 0x%x :C 0x%x : D 0x%x\n %s %s %d\n", + word[ 0 ], word[ 1 ], word[ 2 ], word[ 3 ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] %s\n", err_buf); +#ifndef NO_DATA_CHECK + B2FATAL(err_buf); +#endif + } + return ret_word; +} + + +// This function is available after RawCOPPER/Header ver.2 +int RawCOPPERFormat::GetPacketCRCError(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function is not supported in the version of RawCOPPER format that you're using. n=%d : %s %s %d: Exiting...\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; +} + +// This function is available after RawCOPPER/Header ver.2 +int RawCOPPERFormat::GetEventCRCError(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function is not supported in the version of RawCOPPER format that you're using. n=%d : %s %s %d: Exiting...\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; +} + +int RawCOPPERFormat::GetEventCRC16Value(int n, int finesse_num) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function is not supported in the version of RawCOPPER format that you're using. n=%d fin=%d : %s %s %d: Exiting...\n", + n, finesse_num, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; +} + diff --git a/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat_latest.cc b/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat_latest.cc new file mode 100644 index 0000000000000000000000000000000000000000..33f759c21b6c130a59ee020e05b6fd45b451190d --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat_latest.cc @@ -0,0 +1,88 @@ +//+ +// File : RawCOPPERFormat_latest.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawCOPPERFormat_latest.h> + + +using namespace std; +using namespace Belle2; + +//#define DESY +//#define NO_DATA_CHECK +//#define WO_FIRST_EVENUM_CHECK + +//ClassImp(RawCOPPERFormat_latest); + +RawCOPPERFormat_latest::RawCOPPERFormat_latest() +{ +} + +RawCOPPERFormat_latest::~RawCOPPERFormat_latest() +{ +} + +int RawCOPPERFormat_latest::GetNumFINESSEBlock(int n) +{ + int cnt = 0; + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + cnt++; + } + } + return cnt; +} + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 +void RawCOPPERFormat_latest::CheckB2LFEEHeaderVersion(int n) +{ + + int* temp_buf; + + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + temp_buf = GetFINESSEBuffer(n, i); + if ((temp_buf[ 3 ] & 0x40000000) == 0) { +#ifdef TEMP + // this word for exp/run + // old one (ver.1) used for SPring8 test in 2013 + printf("[DEBUG] \033[31m"); + printf("[DEBUG] ===Firmware ver. ERROR===\n "); + printf("[DEBUG] FTSW and b2tt firmwares was updated on Nov.22, 2013 and the header format attached by B2link was changed in the new firmwares.\n"); + printf("[DEBUG] If you are going to take data now, Please update the firmware.\n"); + printf("[DEBUG] For details, please see Nakao-san's e-mail [b2link_ml:0111] Re: [daq2ml:0159] beta version of trigger timing receiver firmware (b2tt) on bdaq SVN\n"); + printf("[DEBUG] Or if you are going to read data taken before the update, please use basf2 software before svn rev. 7419\n"); + printf("[DEBUG] About the format please see Nakao-san's B2GM slides(p. 13 and 15) http://kds.kek.jp/getFile.py/access?contribId=143&sessionId=38&resId=0&materialId=slides&confId=13911.\n"); + printf("[DEBUG] Sorry for inconvenience.\n"); + printf("[DEBUG] \033[0m"); + fflush(stderr); + char err_buf[500]; + sprintf(err_buf, "FTSW and b2tt firmwares are old. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif + } else { + // this word for 32bit unixtime + // new one (ver.2) + break; + } + } + + if (i == 3) { +#ifdef TEMP + char err_buf[500]; + sprintf(err_buf, "RawCOPPERFormat_latest contains no FINESSE data. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif + } + } + return; +} +#endif diff --git a/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat_v0.cc b/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat_v0.cc new file mode 100644 index 0000000000000000000000000000000000000000..580ff25d9d1aec04d07798e27a2a0e1e92441d49 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat_v0.cc @@ -0,0 +1,716 @@ +//+ +// File : RawCOPPERFormat_v0.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawCOPPERFormat_v0.h> + + +using namespace std; +using namespace Belle2; + +//#define DESY +//#define NO_DATA_CHECK +//#define WO_FIRST_EVENUM_CHECK + +//ClassImp(RawCOPPERFormat_v0); + +RawCOPPERFormat_v0::RawCOPPERFormat_v0() +{ +} + +RawCOPPERFormat_v0::~RawCOPPERFormat_v0() +{ +} + +unsigned int RawCOPPERFormat_v0::CalcDriverChkSum(int n) +{ + int min = GetBufferPos(n) + tmp_header.RAWHEADER_NWORDS; + int max = GetBufferPos(n) + GetBlockNwords(n) + - tmp_trailer.RAWTRAILER_NWORDS - SIZE_COPPER_DRIVER_TRAILER; + unsigned int chksum = 0; + for (int i = min; i < max; i++) { + chksum ^= m_buffer[ i ]; + } + return chksum; +} + + +int RawCOPPERFormat_v0::GetNumFINESSEBlock(int n) +{ + + int cnt = 0; + int pos_nwords = GetBufferPos(n) + POS_CH_A_DATA_LENGTH + tmp_header.RAWHEADER_NWORDS; + if (m_buffer[ pos_nwords ] > 0) cnt++; + pos_nwords = GetBufferPos(n) + POS_CH_B_DATA_LENGTH + tmp_header.RAWHEADER_NWORDS; + if (m_buffer[ pos_nwords ] > 0) cnt++; + pos_nwords = GetBufferPos(n) + POS_CH_C_DATA_LENGTH + tmp_header.RAWHEADER_NWORDS; + if (m_buffer[ pos_nwords ] > 0) cnt++; + pos_nwords = GetBufferPos(n) + POS_CH_D_DATA_LENGTH + tmp_header.RAWHEADER_NWORDS; + if (m_buffer[ pos_nwords ] > 0) cnt++; + return cnt; +} + +int RawCOPPERFormat_v0::GetDetectorNwords(int n, int finesse_num) +{ + switch (finesse_num) { + case 0 : + return Get1stDetectorNwords(n); + break; + case 1 : + return Get2ndDetectorNwords(n); + break; + case 2 : + return Get3rdDetectorNwords(n); + break; + case 3 : + return Get4thDetectorNwords(n); + break; + default : + break; + } + + char err_buf[500]; + sprintf(err_buf, "Specifined FINESSE number( = %d ) is invalid. Exiting...\n %s %s %d\n", finesse_num, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); +} + + +int RawCOPPERFormat_v0::GetFINESSENwords(int n, int finesse_num) +{ + switch (finesse_num) { + case 0 : + return Get1stFINESSENwords(n); + break; + case 1 : + return Get2ndFINESSENwords(n); + break; + case 2 : + return Get3rdFINESSENwords(n); + break; + case 3 : + return Get4thFINESSENwords(n); + break; + default : + break; + } + + char err_buf[500]; + sprintf(err_buf, "Specifined FINESSE number( = %d ) is invalid. Exiting...\n %s %s %d\n", + finesse_num, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); +} + + + + + + + + +unsigned int RawCOPPERFormat_v0::GetB2LFEE32bitEventNumber(int n) +{ + +#ifndef READ_OLD_B2LFEE_FORMAT_FILE + + int err_flag = 0; + unsigned int eve_num = 0; + int flag = 0; + unsigned int eve[4]; + for (int i = 0; i < 4 ; i++) { + eve[ i ] = 12345678; + if (GetFINESSENwords(n, i) > 0) { + int pos_nwords = GetOffsetFINESSE(n, i) + SIZE_B2LHSLB_HEADER + POS_TT_TAG; + eve[ i ] = m_buffer[ pos_nwords ]; + if (flag != 1) eve_num = eve[ i ]; + if (eve_num != eve[ i ]) err_flag = 1; + flag = 1; + } + } + + if (flag == 0) { + char err_buf[500]; + sprintf(err_buf, "No HSLB data in COPPER data. Exiting...\n%s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (err_flag == 1) { + + char err_buf[500]; + sprintf(err_buf, "CORRUPTED DATA: Different event number over HSLBs : slot A 0x%x : B 0x%x :C 0x%x : D 0x%x\n%s %s %d\n", + eve[ 0 ], eve[ 1 ], eve[ 2 ], eve[ 3 ], + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("[DEBUG] [ERROR] %s\n", err_buf); +#ifndef NO_DATA_CHECK + B2FATAL(err_buf); +#endif //NO_DATA_CHECK + } + return eve_num; + +#else // READ_OLD_B2LFEE_FORMAT_FILE + + char err_buf[500]; + sprintf(err_buf, "You need comment out READ_OLD_B2LFEE_FORMAT_FILE if you are handling a new data format\n%s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif // READ_OLD_B2LFEE_FORMAT_FILE + +} + + + + +void RawCOPPERFormat_v0::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum_rawcprhdr, + unsigned int prev_copper_ctr, unsigned int* cur_copper_ctr, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + + char err_buf[500]; + sprintf(err_buf, + "This function for format ver.0 is not supported. (n %d preveve %u eve %u prectr %u ctr %u prevrun %u run %u) Exiting...\n %s %s %d\n", + n, prev_evenum, *cur_evenum_rawcprhdr, prev_copper_ctr, *cur_copper_ctr, + prev_exprunsubrun_no, *cur_exprunsubrun_no, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + +// char err_buf[500]; +// int err_flag = 0; +// // +// // check Magic words +// // +// if (!CheckCOPPERMagic(n)) { +// sprintf(err_buf, "CORRUPTED DATA: Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u\n%s %s %d\n", +// GetMagicDriverHeader(n), +// GetMagicFPGAHeader(n), +// GetMagicFPGATrailer(n), +// GetMagicDriverTrailer(n), +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } +// // +// // Event # check +// // +// *cur_evenum_rawcprhdr = GetEveNo(n); +// unsigned int evenum_feehdr = GetB2LFEE32bitEventNumber(n); +// if (*cur_evenum_rawcprhdr != evenum_feehdr) { +// sprintf(err_buf, +// "CORRUPTED DATA: Event # in RawCOPPERFormat_v0 header and FEE header is different : cprhdr 0x%x feehdr 0x%x : Exiting...\n%s %s %d\n", +// *cur_evenum_rawcprhdr, evenum_feehdr, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } + +// // +// // Check incrementation of event # +// // +// *cur_runsubrun_no = GetRunNoSubRunNo(n); +// if ( +// #ifdef WO_FIRST_EVENUM_CHECK +// prev_evenum != 0xFFFFFFFF && *cur_evenum_rawcprhdr != 0 +// #else +// prev_runsubrun_no == *cur_runsubrun_no && prev_runsubrun_no >= 0 +// #endif +// ) { +// if ((unsigned int)(prev_evenum + 1) != *cur_evenum_rawcprhdr) { +// sprintf(err_buf, "CORRUPTED DATA: Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n%s %s %d\n", +// n, prev_evenum, *cur_evenum_rawcprhdr, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } +// } + + +// *cur_copper_ctr = GetCOPPERCounter(n); +// if ( +// #ifdef WO_FIRST_EVENUM_CHECK +// prev_copper_ctr != 0xFFFFFFFF +// #else +// true +// #endif +// ) { +// if ((unsigned int)(prev_copper_ctr + 1) != *cur_copper_ctr) { +// sprintf(err_buf, "COPPER counter jump : i %d prev 0x%x cur 0x%x :\n%s %s %d\n", +// n, prev_copper_ctr, *cur_copper_ctr, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); + +// #ifdef DESY +// // +// // In DESY test, we ignore this error +// // +// printf("[DEBUG] [INFO] %s", err_buf); +// #else +// err_flag = 1; +// #endif + +// } +// } + +// // +// // Check is utime and ctime_trgtype same over different FINESSE data +// // +// CheckUtimeCtimeTRGType(n); + + +// // +// // Check checksum calculated by COPPER driver +// // +// if (GetDriverChkSum(n) != CalcDriverChkSum(n)) { +// sprintf(err_buf, +// "CORRUPTED DATA: COPPER driver checkSum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x\n%s %s %d\n", +// n, +// GetBlockNwords(n), +// *cur_evenum_rawcprhdr, +// GetDriverChkSum(n), +// CalcDriverChkSum(n), +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } + + +// // +// // Check checksum calculated by DeSerializerCOPPER() +// // + +// tmp_trailer.SetBuffer(GetRawTrlBufPtr(n)); +// unsigned int xor_chksum = CalcXORChecksum(GetBuffer(n), GetBlockNwords(n) - tmp_trailer.GetTrlNwords()); +// if (tmp_trailer.GetChksum() != xor_chksum) { +// sprintf(err_buf, +// "CORRUPTED DATA: RawCOPPERFormat_v0 checksum error : block %d : length %d eve 0x%x : Trailer chksum 0x%.8x : calcd. now 0x%.8x\n %s %s %d\n", +// n, GetBlockNwords(n), *cur_evenum_rawcprhdr, tmp_trailer.GetChksum(), xor_chksum, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// err_flag = 1; +// } + + +// #ifdef DEBUG +// printf("[DEBUG] eve %u %d %d %d %d\n", +// GetEveNo(n), +// Get1stDetectorNwords(n), +// Get2ndDetectorNwords(n), +// Get3rdDetectorNwords(n), +// Get4thDetectorNwords(n) +// ); +// printf("[DEBUG] ===COPPER BLOCK==============\n"); +// printData(GetBuffer(n), GetBlockNwords(n)); + +// printf("[DEBUG] ===FINNESSE A ==============\n"); +// printData(Get1stDetectorBuffer(n), Get1stDetectorNwords(n)); + +// printf("[DEBUG] ===FINNESSE B ==============\n"); +// printData(Get2ndDetectorBuffer(n), Get2ndDetectorNwords(n)); + +// printf("[DEBUG] ===FINNESSE C ==============\n"); +// printData(Get3rdDetectorBuffer(n), Get3rdDetectorNwords(n)); + +// printf("[DEBUG] ===FINNESSE D ==============\n"); +// printData(Get4thDetectorBuffer(n), Get4thDetectorNwords(n)); +// printf("[DEBUG] === END ==============\n"); + +// #endif + +// if (err_flag == 1) { +// printf("[DEBUG] ========== dump a data blcok : block # %d==========\n", n); +// PrintData(GetBuffer(n), GetBlockNwords(n)); +// string err_str = err_buf; +// throw (err_str); +// // sleep(1234567); +// // exit(-1); +// } + + return; + +} + +bool RawCOPPERFormat_v0::CheckCOPPERMagic(int n) +{ + if (GetMagicDriverHeader(n) != COPPER_MAGIC_DRIVER_HEADER) { + return false; + } else if (GetMagicFPGAHeader(n) != COPPER_MAGIC_FPGA_HEADER) { + return false; + } else if (GetMagicFPGATrailer(n) != COPPER_MAGIC_FPGA_TRAILER) { + return false; + } else if (GetMagicDriverTrailer(n) != COPPER_MAGIC_DRIVER_TRAILER) { + return false; + } + return true; +} + +void RawCOPPERFormat_v0::CheckUtimeCtimeTRGType(int n) +{ + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 + CheckB2LFEEHeaderVersion(n); +#endif + int err_flag = 0; + int flag = 0; + unsigned int temp_utime = 0, temp_ctime_trgtype = 0; + unsigned int utime[4], ctime_trgtype[4]; + memset(utime, 0, sizeof(utime)); + memset(ctime_trgtype, 0, sizeof(ctime_trgtype)); + + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + ctime_trgtype[ i ] = m_buffer[ GetOffsetFINESSE(n, i) + + SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ]; + utime[ i ] = m_buffer[ GetOffsetFINESSE(n, i) + + SIZE_B2LHSLB_HEADER + POS_TT_UTIME ]; + if (flag == 0) { + temp_ctime_trgtype = ctime_trgtype[ i ]; + temp_utime = utime[ i ]; + flag = 1; + } else { + if (temp_ctime_trgtype != ctime_trgtype[ i ] + || temp_utime != utime[ i ]) { + err_flag = 1; + } + } + } + } + + if (err_flag != 0) { + for (int i = 0; i < 4; i++) { + printf("[DEBUG] FINESSE #=%d buffsize %d ctimeTRGtype 0x%.8x utime 0x%.8x\n", + i, GetFINESSENwords(n, i), ctime_trgtype[ i ], utime[ i ]); + } + char err_buf[500]; + sprintf(err_buf, "CORRUPTED DATA: mismatch over FINESSEs. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + return; +} + + +unsigned int RawCOPPERFormat_v0::FillTopBlockRawHeader(unsigned int m_node_id, unsigned int prev_eve32, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + + char err_buf[500]; + sprintf(err_buf, + "This function for format ver.0 is not supported. (node %.8x preveve %u prevrun %u currun %u ) Exiting...\n %s %s %d\n", + m_node_id, prev_eve32, prev_exprunsubrun_no, *cur_exprunsubrun_no, + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + return 0; + +// const int cpr_id = 0; +// // m_temp_value = 12345678; +// // +// // This function only fills RawHeader contents for the first datablock. +// // # of block should be 1 +// if (m_num_nodes * m_num_events != 1) { +// char err_buf[500]; +// sprintf(err_buf, +// "This function should be used for RawCOPPERFormat_v0 containing only one datablock, while. this object has num_nodes of %d and num_events of %d\n %s %s %d\n", +// m_num_nodes, m_num_events, __FILE__, __PRETTY_FUNCTION__, __LINE__); +// string err_str = err_buf; throw (err_str); +// } + +// ////////////////////////////////////////////////// +// // +// // Fill info in RawHeader +// // +// ////////////////////////////////////////////////// + +// // +// // Initialize a RawHeader part +// // +// memset(m_buffer, 0, sizeof(int) * tmp_header.RAWHEADER_NWORDS); +// m_buffer[ tmp_header.POS_HDR_NWORDS ] = tmp_header.RAWHEADER_NWORDS; + +// // +// // Check FINESSEs which containes data +// // +// int* copper_buf = &(m_buffer[ tmp_header.RAWHEADER_NWORDS ]); +// if (copper_buf[ POS_CH_A_DATA_LENGTH ] == 0 && +// copper_buf[ POS_CH_B_DATA_LENGTH ] == 0 && +// copper_buf[ POS_CH_C_DATA_LENGTH ] == 0 && +// copper_buf[ POS_CH_D_DATA_LENGTH ] == 0) { +// char err_buf[500]; +// sprintf(err_buf, +// "No FINESSE data in a copper data block. Exiting...\n %s %s %d\n", +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// string err_str = err_buf; throw (err_str); +// // sleep(12345678); +// // exit(-1); +// } + +// // +// // Set total words info +// // +// int datablock_nwords = +// tmp_header.RAWHEADER_NWORDS + +// (copper_buf[ POS_DATA_LENGTH ] +// + SIZE_COPPER_DRIVER_HEADER +// + SIZE_COPPER_DRIVER_TRAILER) +// + tmp_trailer.RAWTRAILER_NWORDS; +// m_buffer[ tmp_header.POS_NWORDS ] = datablock_nwords; + +// // +// // Check the consistency between data length and length in RawHeader +// // +// if (m_buffer[ tmp_header.POS_NWORDS ] != m_nwords) { +// char err_buf[500]; +// sprintf(err_buf, +// "CORRUPTED DATA: Data length is inconsistent m_nwords %d : nwords from COPPER data %d\n %s %s %d\n", +// m_nwords, m_buffer[ tmp_header.POS_NWORDS ], +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// string err_str = err_buf; throw (err_str); +// // sleep(12345678); +// // exit(-1); +// } + +// // +// // Fill offset values +// // +// int offset_1st_finesse = tmp_header.RAWHEADER_NWORDS + SIZE_COPPER_HEADER; +// int offset_2nd_finesse = offset_1st_finesse + copper_buf[ POS_CH_A_DATA_LENGTH ]; +// int offset_3rd_finesse = offset_2nd_finesse + copper_buf[ POS_CH_B_DATA_LENGTH ]; +// int offset_4th_finesse = offset_3rd_finesse + copper_buf[ POS_CH_C_DATA_LENGTH ]; +// m_buffer[ tmp_header.POS_OFFSET_1ST_FINESSE ] = offset_1st_finesse; +// m_buffer[ tmp_header.POS_OFFSET_2ND_FINESSE ] = offset_2nd_finesse; +// m_buffer[ tmp_header.POS_OFFSET_3RD_FINESSE ] = offset_3rd_finesse; +// m_buffer[ tmp_header.POS_OFFSET_4TH_FINESSE ] = offset_4th_finesse; + +// // +// // Fill Exp/Run value +// // +// int* finesse_buf = & +// (m_buffer[ offset_1st_finesse ]); // In any finesse implementations, the top finesse buffer should be at offset_1st_finesse; +// m_buffer[ tmp_header.POS_EXP_RUN_NO ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_EXP_RUN ]; + + +// // +// // Fill event # +// // +// unsigned int cur_ftsw_eve32 = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_TAG ]; +// m_buffer[ tmp_header.POS_EVE_NO ] = cur_ftsw_eve32; + +// // +// // Copy FTSW words from B2LFEE header +// // +// m_buffer[ tmp_header.POS_TTCTIME_TRGTYPE ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_CTIME_TYPE ]; +// m_buffer[ tmp_header.POS_TTUTIME ] = finesse_buf[ SIZE_B2LHSLB_HEADER + POS_TT_UTIME ]; + +// // +// // Set node ID, trunc_mask, data_type +// // +// m_buffer[ tmp_header.POS_NODE_ID ] = m_node_id; +// m_buffer[ tmp_header.POS_TRUNC_MASK_DATATYPE ] = ((m_trunc_mask << 31) & 0x80000000) | (m_data_type & 0x7FFFFFFF); + +// // +// // Set RawHeader magic word +// // +// m_buffer[ tmp_header.POS_TERM_HEADER ] = tmp_header.MAGIC_WORD_TERM_HEADER; + + +// // +// // Add node-info +// // +// if (m_buffer[ tmp_header.POS_NUM_NODES ] < tmp_header.NUM_MAX_NODES) { +// m_buffer[ tmp_header.POS_NODES_1 + m_buffer[ tmp_header.POS_NUM_NODES ] ] = m_node_id; +// } +// m_buffer[ tmp_header.POS_NUM_NODES ]++; + + + +// ////////////////////////////////////////////////// +// // +// // Fill info in RawTrailer +// // +// ////////////////////////////////////////////////// + +// // +// // Calculate XOR checksum +// // +// unsigned int chksum_top = 0, chksum_body = 0, chksum_bottom = 0; + +// int top_end = tmp_header.RAWHEADER_NWORDS; +// for (int i = 0; i < top_end; i++) { +// chksum_top ^= m_buffer[ i ]; +// } +// int body_end = datablock_nwords - SIZE_COPPER_DRIVER_TRAILER - tmp_trailer.RAWTRAILER_NWORDS; +// for (int i = top_end; i < body_end; i++) { +// chksum_body ^= m_buffer[ i ]; +// } + +// int bottom_end = datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS; +// for (int i = body_end; i < bottom_end; i++) { +// chksum_bottom ^= m_buffer[ i ]; +// } + +// // +// // check COPPER driver checksum +// // +// if (chksum_body != (unsigned int)(m_buffer[ body_end ])) { +// char err_buf[500]; +// sprintf(err_buf, "CORRUPTED DATA: COPPER driver checksum is not consistent.: calcd. %.8x data %.8x\n %s %s %d\n", +// chksum_body, m_buffer[ body_end ], +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// string err_str = err_buf; throw (err_str); +// // sleep(12345678); +// // exit(-1); +// } + +// // +// // Fill trailer info (checksum, magic word) +// // +// unsigned int chksum = chksum_top ^ chksum_body ^ chksum_bottom; +// int* trl = &(m_buffer[ datablock_nwords - tmp_trailer.RAWTRAILER_NWORDS ]); +// trl[ tmp_trailer.POS_CHKSUM ] = chksum; +// trl[ tmp_trailer.POS_TERM_WORD ] = tmp_trailer.MAGIC_WORD_TERM_TRAILER; + + +// ////////////////////////////////////////////////// +// // +// // Data check ( magic word, event incrementation ) +// // +// ////////////////////////////////////////////////// + + +// // +// // check magic words +// // +// int* fpga_trailer_magic = trl - 3; +// int* driver_trailer_magic = trl - 1; +// int err_flag = 0; +// if ((unsigned int)(copper_buf[ POS_MAGIC_COPPER_1 ]) != COPPER_MAGIC_DRIVER_HEADER) { +// err_flag = 1; +// } else if ((unsigned int)(copper_buf[ POS_MAGIC_COPPER_2 ]) != COPPER_MAGIC_FPGA_HEADER) { +// err_flag = 1; +// } else if ((unsigned int)(*fpga_trailer_magic) != COPPER_MAGIC_FPGA_TRAILER) { +// err_flag = 1; +// } else if ((unsigned int)(*driver_trailer_magic) != COPPER_MAGIC_DRIVER_TRAILER) { +// err_flag = 1; +// } +// if (err_flag == 1) { +// char err_buf[500]; +// sprintf(err_buf, "CORRUPTED DATA: Invalid Magic word 0x7FFFF0008=%u 0xFFFFFAFA=%u 0xFFFFF5F5=%u 0x7FFF0009=%u\n %s %s %d\n", +// GetMagicDriverHeader(cpr_id), +// GetMagicFPGAHeader(cpr_id), +// GetMagicFPGATrailer(cpr_id), +// GetMagicDriverTrailer(cpr_id), +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("[DEBUG] [ERROR] %s\n", err_buf); +// #ifndef NO_DATA_CHECK +// string err_str = err_buf; throw (err_str); + +// // sleep(12345678); +// // exit(-1); +// #endif +// } + +// // +// // check incrementation of event # +// // +// *cur_runsubrun_no = GetRunNoSubRunNo(cpr_id); +// if (prev_runsubrun_no == *cur_runsubrun_no && prev_runsubrun_no >= 0) { +// if ( +// #ifdef WO_FIRST_EVENUM_CHECK +// (prev_eve32 + 1 != cur_ftsw_eve32) && (prev_eve32 != 0xFFFFFFFF && cur_ftsw_eve32 != 0) +// #else +// prev_eve32 + 1 != cur_ftsw_eve32 +// #endif +// ) { + +// #ifndef NO_DATA_CHECK +// char err_buf[500]; +// sprintf(err_buf, "CORRUPTED DATA: Invalid event_number. Exiting...: cur 32bit eve %u preveve %u prun %d crun %d\n %s %s %d\n", +// cur_ftsw_eve32, prev_eve32, +// prev_runsubrun_no, *cur_runsubrun_no, +// __FILE__, __PRETTY_FUNCTION__, __LINE__); +// printf("[DEBUG] [ERROR] %s\n", err_buf); + +// string err_str = err_buf; +// printf("[DEBUG] i= %d : num entries %d : Tot words %d\n", 0 , GetNumEntries(), TotalBufNwords()); +// PrintData(GetBuffer(cpr_id), TotalBufNwords()); +// throw (err_str); +// // exit(-1); +// #endif + +// } +// } + +// return cur_ftsw_eve32; + +} + + + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 +void RawCOPPERFormat_v0::CheckB2LFEEHeaderVersion(int n) +{ + + int* temp_buf; + + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + temp_buf = GetFINESSEBuffer(n, i); + if ((temp_buf[ 3 ] & 0x40000000) == 0) { +#ifdef TEMP + // this word for exp/run + // old one (ver.1) used for SPring8 test in 2013 + printf("[DEBUG] \033[31m"); + printf("[DEBUG] ===Firmware ver. ERROR===\n "); + printf("[DEBUG] FTSW and b2tt firmwares was updated on Nov.22, 2013 and the header format attached by B2link was changed in the new firmwares.\n"); + printf("[DEBUG] If you are going to take data now, Please update the firmware.\n"); + printf("[DEBUG] For details, please see Nakao-san's e-mail [b2link_ml:0111] Re: [daq2ml:0159] beta version of trigger timing receiver firmware (b2tt) on bdaq SVN\n"); + printf("[DEBUG] Or if you are going to read data taken before the update, please use basf2 software before rev. 7419\n"); + printf("[DEBUG] About the format please see Nakao-san's B2GM slides(p. 13 and 15) http://kds.kek.jp/getFile.py/access?contribId=143&sessionId=38&resId=0&materialId=slides&confId=13911.\n"); + printf("[DEBUG] Sorry for inconvenience.\n"); + printf("[DEBUG] \033[0m"); + fflush(stderr); + char err_buf[500]; + sprintf(err_buf, "FTSW and b2tt firmwares are old. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif + } else { + // this word for 32bit unixtime + // new one (ver.2) + break; + } + } + + if (i == 3) { +#ifdef TEMP + char err_buf[500]; + sprintf(err_buf, "RawCOPPERFormat_v0 contains no FINESSE data. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif + } + } + return; +} +#endif + +int* RawCOPPERFormat_v0::PackDetectorBuf(int* packed_buf_nwords, + int* detector_buf_1st, int nwords_1st, + int* detector_buf_2nd, int nwords_2nd, + int* detector_buf_3rd, int nwords_3rd, + int* detector_buf_4th, int nwords_4th, + RawCOPPERPackerInfo rawcprpacker_info) +{ + char err_buf[500]; + sprintf(err_buf, "This function is not supported (%p %p %d %p %d %p %d %p %d %p ) Exiting...\n%s %s %d\n" + , packed_buf_nwords, detector_buf_1st, nwords_1st, detector_buf_2nd, nwords_2nd, detector_buf_3rd, + nwords_3rd, detector_buf_4th, nwords_4th, &rawcprpacker_info, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat_v1.cc b/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat_v1.cc new file mode 100644 index 0000000000000000000000000000000000000000..4c28debc3fbe0b0f177bb4a4fca364f85245b676 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawCOPPERFormat_v1.cc @@ -0,0 +1,88 @@ +//+ +// File : RawCOPPERFormat_v1.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawCOPPERFormat_v1.h> + + +using namespace std; +using namespace Belle2; + +//#define DESY +//#define NO_DATA_CHECK +//#define WO_FIRST_EVENUM_CHECK + +//ClassImp(RawCOPPERFormat_v1); + +RawCOPPERFormat_v1::RawCOPPERFormat_v1() +{ +} + +RawCOPPERFormat_v1::~RawCOPPERFormat_v1() +{ +} + +int RawCOPPERFormat_v1::GetNumFINESSEBlock(int n) +{ + int cnt = 0; + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + cnt++; + } + } + return cnt; +} + +#ifdef USE_B2LFEE_FORMAT_BOTH_VER1_AND_2 +void RawCOPPERFormat_v1::CheckB2LFEEHeaderVersion(int n) +{ + + int* temp_buf; + + for (int i = 0; i < 4; i++) { + if (GetFINESSENwords(n, i) > 0) { + temp_buf = GetFINESSEBuffer(n, i); + if ((temp_buf[ 3 ] & 0x40000000) == 0) { +#ifdef TEMP + // this word for exp/run + // old one (ver.1) used for SPring8 test in 2013 + printf("[DEBUG] \033[31m"); + printf("[DEBUG] ===Firmware ver. ERROR===\n "); + printf("[DEBUG] FTSW and b2tt firmwares was updated on Nov.22, 2013 and the header format attached by B2link was changed in the new firmwares.\n"); + printf("[DEBUG] If you are going to take data now, Please update the firmware.\n"); + printf("[DEBUG] For details, please see Nakao-san's e-mail [b2link_ml:0111] Re: [daq2ml:0159] beta version of trigger timing receiver firmware (b2tt) on bdaq SVN\n"); + printf("[DEBUG] Or if you are going to read data taken before the update, please use basf2 software before rev. 7419\n"); + printf("[DEBUG] About the format please see Nakao-san's B2GM slides(p. 13 and 15) http://kds.kek.jp/getFile.py/access?contribId=143&sessionId=38&resId=0&materialId=slides&confId=13911.\n"); + printf("[DEBUG] Sorry for inconvenience.\n"); + printf("[DEBUG] \033[0m"); + fflush(stderr); + char err_buf[500]; + sprintf(err_buf, "FTSW and b2tt firmwares are old. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif + } else { + // this word for 32bit unixtime + // new one (ver.2) + break; + } + } + + if (i == 3) { +#ifdef TEMP + char err_buf[500]; + sprintf(err_buf, "RawCOPPERFormat_v1 contains no FINESSE data. Exiting...\n %s %s %d\n", + __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); +#endif + } + } + return; +} +#endif diff --git a/ROPCApplications/rawdata/dataobjects/src/RawDataBlock.cc b/ROPCApplications/rawdata/dataobjects/src/RawDataBlock.cc new file mode 100644 index 0000000000000000000000000000000000000000..b8bd56264788f72ec766ce155d94341cbb8bd1e8 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawDataBlock.cc @@ -0,0 +1,69 @@ +//+ +// File : RawDataBlock.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawDataBlock.h> + +using namespace std; +using namespace Belle2; + +RawDataBlock::RawDataBlock() +{ + m_access_dblk = new RawDataBlockFormat; + + m_nwords = 0; + m_use_prealloc_buf = 0; + m_buffer = NULL; + m_num_nodes = 0; + m_num_events = 0; +} + +RawDataBlock::~RawDataBlock() +{ + if (m_access_dblk != NULL) delete m_access_dblk; + m_access_dblk = NULL; + + if (!m_use_prealloc_buf && m_buffer != NULL) { + delete[] m_buffer; + } +} + +void RawDataBlock::SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes) +{ + + if (bufin == NULL) { + char err_buf[500]; + sprintf(err_buf, "[DEBUG] bufin is NULL. Exting...\n"); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (!m_use_prealloc_buf && m_buffer != NULL) delete[] m_buffer; + + if (delete_flag == 0) { + m_use_prealloc_buf = true; + } else { + m_use_prealloc_buf = false; + } + // m_nwords = bufin[0]; + m_nwords = nwords; + m_buffer = bufin; + + m_num_nodes = num_nodes; + m_num_events = num_events; + + // Set to access class + m_access_dblk->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); + + // Set length at the first word of the buffer + // + // Assign header and trailer + // + // m_header.SetBuffer(&(bufin[ 0 ])); + // m_trailer.SetBuffer(&(bufin[ m_nwords - m_trailer.GetTrlNwords() ])); + +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawDataBlockFormat.cc b/ROPCApplications/rawdata/dataobjects/src/RawDataBlockFormat.cc new file mode 100644 index 0000000000000000000000000000000000000000..57080678d5d5e93fbd91cf9db44d183c87cd2e8a --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawDataBlockFormat.cc @@ -0,0 +1,177 @@ +//+ +// File : RawDataBlockFormat.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawDataBlockFormat.h> + +using namespace std; +using namespace Belle2; + +RawDataBlockFormat::RawDataBlockFormat() +{ + m_nwords = 0; + m_use_prealloc_buf = 0; + m_buffer = NULL; + m_num_nodes = 0; + m_num_events = 0; +} + +RawDataBlockFormat::~RawDataBlockFormat() +{ + if (!m_use_prealloc_buf && m_buffer != NULL) { + delete[] m_buffer; + } +} + +int RawDataBlockFormat::GetBufferPos(int n) +{ + if (m_buffer == NULL || m_nwords <= 0) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] RawPacket buffer(%p) is not available or length(%d) is not set.\n %s %s %d\n", + m_buffer, m_nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (n >= (m_num_events * m_num_nodes)) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] Invalid COPPER block No. (%d : max %d ) is specified. Exiting... \n %s %s %d\n", + n, (m_num_events * m_num_nodes), __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + int pos_nwords = 0; + for (int i = 1; i <= n ; i++) { + if (m_buffer[ pos_nwords ] <= 0) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : length of this data block is strange ( %d words ). Maybe data is corrupted or RawHeader info has not been filled yet. Exiting...", + m_buffer[ pos_nwords ]); + printf("%s", err_buf); + B2FATAL(err_buf); + } else { + pos_nwords += m_buffer[ pos_nwords ]; + } + if (pos_nwords >= m_nwords) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : value of pos_nwords(%d) is larger than m_nwords(%d). Exiting...\n %s %s %d\n", + pos_nwords, m_nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); // to reduce multiple error messages + } + } + return pos_nwords; + +} + + +int RawDataBlockFormat::CheckFTSWID(int n) +{ + int pos = POS_NODE_ID; + if (m_buffer[ GetBufferPos(n) + TEMP_POS_NWORDS_HEADER ] == OLD_FTSW_NWORDS_HEADER) { + pos = POS_FTSW_ID_OLD; + } + + if ((m_buffer[ GetBufferPos(n) + pos ] & 0xffffff00) == 0x54544400) { // "TTD" + format version ( 0x20=DESY, 0x31=2018/7/11) + return 1; + } else { + return 0; + } +} + + +int RawDataBlockFormat::CheckTLUID(int n) +{ + int pos = POS_NODE_ID; + if (m_buffer[ GetBufferPos(n) + TEMP_POS_NWORDS_HEADER ] == OLD_FTSW_NWORDS_HEADER) { + pos = POS_FTSW_ID_OLD; + } + if (m_buffer[ GetBufferPos(n) + pos ] == 0x544c5520) { // "TLU " + return 1; + } else { + return 0; + } +} + +int RawDataBlockFormat::TotalBufNwords() +{ + return m_nwords; +} + + +int RawDataBlockFormat::GetBlockNwords(int n) +{ + int size; + if (n == (m_num_events * m_num_nodes) - 1) { + size = m_nwords - GetBufferPos(n); + } else { + size = GetBufferPos(n + 1) - GetBufferPos(n); + } + return size; +} + + +int* RawDataBlockFormat::GetWholeBuffer() +{ + return m_buffer; +} + +int* RawDataBlockFormat::GetBuffer(int n) +{ + int pos_nwords = GetBufferPos(n); + return &(m_buffer[ pos_nwords ]); +} + + +void RawDataBlockFormat::SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes) +{ + + if (bufin == NULL) { + char err_buf[500]; + sprintf(err_buf, "[DEBUG] bufin is NULL. Exting...\n"); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (!m_use_prealloc_buf && m_buffer != NULL) delete[] m_buffer; + + if (delete_flag == 0) { + m_use_prealloc_buf = true; + } else { + m_use_prealloc_buf = false; + } + + m_nwords = nwords; + m_buffer = bufin; + + m_num_nodes = num_nodes; + m_num_events = num_events; + + return; + +} + + + +void RawDataBlockFormat::PrintData(int* buf, int nwords) +{ + printf("[DEBUG] "); + for (int i = 0; i < nwords; i++) { + printf("%.8x ", buf[ i ]); + if (i % 10 == 9) printf("\n[DEBUG] "); + } + printf("\n[DEBUG] "); + printf("\n"); + return; +} + +void RawDataBlockFormat::CopyBlock(int n, int* buf_to) +{ + memcpy(buf_to, GetBuffer(n), GetBlockNwords(n) * sizeof(int)); + return; +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawECL.cc b/ROPCApplications/rawdata/dataobjects/src/RawECL.cc new file mode 100644 index 0000000000000000000000000000000000000000..7cc2a4eb958dd9a149256585479d239d6bac8bff --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawECL.cc @@ -0,0 +1,20 @@ +//+ +// File : RawCOPPER.cc +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawECL.h> + +using namespace std; +using namespace Belle2; + +RawECL::RawECL() +{ +} + +RawECL::~RawECL() +{ +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawFTSW.cc b/ROPCApplications/rawdata/dataobjects/src/RawFTSW.cc new file mode 100644 index 0000000000000000000000000000000000000000..725d0a2e40a5674d79258701f39403aebac33986 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawFTSW.cc @@ -0,0 +1,132 @@ +//+ +// File : RawFTSW.cc +// Description : Module to handle raw data from FTSW +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawFTSW.h> +using namespace std; +using namespace Belle2; + +RawFTSW::RawFTSW() +{ + m_access = NULL; + m_version = -1; +} + +RawFTSW::~RawFTSW() +{ + if (m_access != NULL) delete m_access; + m_access = NULL; +} + +void RawFTSW::SetVersion() +{ + if (m_buffer == NULL) { + char err_buf[500]; + sprintf(err_buf, "m_buffer is NULL. Exiting..."); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (m_access != NULL) { + delete m_access; + m_access = nullptr; + } + + // + // Assign unpacker + // + // Special treatment is required because Nakao-san did not set verision number in ver.0,1, and 2 header. + // I'm using the size of header which is stored the 1st word (0,1,..) + // + int temp_version = m_version; + if (m_buffer[ POS_HEADER_SIZE ] == VER_0_HEADER_SIZE) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] Ver.0 of RawFTSW( so-called early DESYtest format ) is detected but not supported. (header size = 0x%.8x ) Exiting...\n %s %s %d\n", + m_buffer[ POS_HEADER_SIZE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } else if (m_buffer[ POS_NODE_FORMAT_ID ] == FORMAT_ID_VER_0TO2) { + if (m_buffer[ POS_HEADER_SIZE ] == VER_2_HEADER_SIZE) { + m_access = new RawFTSWFormat_v2; + m_version = 2; // as of 2019.3.2, the latest version is 2. + } else if (m_buffer[ POS_HEADER_SIZE ] == VER_1_HEADER_SIZE) { + m_access = new RawFTSWFormat_v1; + m_version = 1; + } else { + char err_buf[500]; + sprintf(err_buf, "[FATAL] ERROR_EVENT : Invalid RawFTSW header size of FTSW data format(= 0x%.8x words). Exiting...\n %s %s %d\n", + m_buffer[ POS_HEADER_SIZE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + } else if (m_buffer[ POS_NODE_FORMAT_ID ] == FORMAT_ID_VER_0TO3 || + m_buffer[ POS_NODE_FORMAT_ID ] == 0x54544432 || + m_buffer[ POS_NODE_FORMAT_ID ] == 0x54544433 || + m_buffer[ POS_NODE_FORMAT_ID ] == 0x54544434 || + m_buffer[ POS_NODE_FORMAT_ID ] == 0x54544435 || + m_buffer[ POS_NODE_FORMAT_ID ] == 0x54544436 || + m_buffer[ POS_NODE_FORMAT_ID ] == 0x54544437 || + m_buffer[ POS_NODE_FORMAT_ID ] == 0x54544438 || + m_buffer[ POS_NODE_FORMAT_ID ] == 0x54544439) { + // Request from Nakao-san in Oct. 15, 2019 + // - FORMAT_ID_VER_0TO3 should be changed to x"54544431" from x"5454421" + // - RawFTSWFormat_latest should accept newer format versions(= larger version number), so that this unpacker + // will not stop due to version error, when Nakao-san updated version number in RawFTSW data. + // -- Since the latest format is basically the addition of some variables in RawFTSW data, probably it won't cause the problem. + m_access = new RawFTSWFormat_latest; + m_version = 3; // as of 2019.3.20 the latest version is 3. + } else { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] ERROR_EVENT : Invalid RawFTSW header size(= 0x%.8x words) or version number.(=0x%.8x) Exiting...\n %s %s %d\n", + m_buffer[ POS_HEADER_SIZE ], m_buffer[ POS_NODE_FORMAT_ID ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + + if (temp_version >= 0 && temp_version != m_version) { + char err_buf[500]; + sprintf(err_buf, + "[FATAL] Already assigned RawFTSW format version (= %.8x) is different from the one (= 0x%.8x) from the current event. Exiting...\n %s %s %d\n", + temp_version, m_version, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + m_access->SetBuffer(m_buffer, m_nwords, 0, m_num_events, m_num_nodes); +} + + +// I copied this from RawCOPPER.cc, since SetVersion() is necessary for RawFTSW after ver.2 is included. +void RawFTSW::SetBuffer(int* bufin, int nwords, int delete_flag, int num_events, int num_nodes) +{ + + if (bufin == NULL) { + char err_buf[500]; + sprintf(err_buf, "[FATAL] bufin is NULL. Exting...\n"); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + if (!m_use_prealloc_buf && m_buffer != NULL) delete[] m_buffer; + + if (delete_flag == 0) { + m_use_prealloc_buf = true; + } else { + m_use_prealloc_buf = false; + } + + m_nwords = nwords; + m_buffer = bufin; + + m_num_nodes = num_nodes; + m_num_events = num_events; + + SetVersion(); + +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat.cc b/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat.cc new file mode 100644 index 0000000000000000000000000000000000000000..dcab7dcb6029a9efac1cb6d69dc940261432fc84 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat.cc @@ -0,0 +1,102 @@ +//+ +// File : RawFTSWFormat.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- +#include <rawdata/dataobjects/RawFTSWFormat.h> + + +using namespace std; +using namespace Belle2; + +RawFTSWFormat::RawFTSWFormat() +{ +} + +RawFTSWFormat::~RawFTSWFormat() +{ +} + +// This function is only available in RawFTSW ver.1 (for DESY test) +int RawFTSWFormat::Get15bitTLUTag(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function is not supported in the version of RawFTSW format that you're using. n=%d : %s %s %d: Exiting...\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; +} + +// This function is only available in RawFTSW ver.2 +int RawFTSWFormat::GetIsHER(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function is not supported in the version of RawFTSW format that you're using. n=%d : %s %s %d: Exiting...\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; +} + + + +unsigned int RawFTSWFormat::GetTimeSinceLastInjection(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function is not supported in the version of RawFTSW format that you're using. n=%d : %s %s %d: Exiting...\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; + +} + +unsigned int RawFTSWFormat::GetTimeSincePrevTrigger(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function is not supported in the version of RawFTSW format that you're using. n=%d : %s %s %d: Exiting...\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; +} + +unsigned int RawFTSWFormat::GetBunchNumber(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function is not supported in the version of RawFTSW format that you're using. n=%d : %s %s %d: Exiting...\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; +} + +unsigned int RawFTSWFormat::GetFrameCount(int n) +{ + char err_buf[500]; + sprintf(err_buf, + "[FATAL] This function is not supported in the version of RawFTSW format that you're using. n=%d : %s %s %d: Exiting...\n", + n, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s\n", err_buf); fflush(stdout); + B2FATAL(err_buf); + return -1; +} + +void RawFTSWFormat::GetPCTimeVal(int n, struct timeval* tv) +{ + // According to Nakao-san's comment at DAQ meeting on Feb.28, 2020. If one calls this function for older data, just returning 0 values is fine. + // No need to either end up with FATAL message or issue any WARNING messages (Actually, WARNING messages would be issued in every event when implemented.) + tv->tv_sec = 0; + tv->tv_usec = 0; + + return; + +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat_latest.cc b/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat_latest.cc new file mode 100644 index 0000000000000000000000000000000000000000..2b727b298402d59b2cbc13ccb0828ee8845a4234 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat_latest.cc @@ -0,0 +1,179 @@ +//+ +// File : RawFTSWFormat_latest.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 18 - Feb - 2019 +//- +#include <rawdata/dataobjects/RawFTSWFormat_latest.h> + + +using namespace std; +using namespace Belle2; + +RawFTSWFormat_latest::RawFTSWFormat_latest() +{ +} + +RawFTSWFormat_latest::~RawFTSWFormat_latest() +{ +} + + +unsigned int RawFTSWFormat_latest::GetTTCtimeTRGType(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_TT_CTIME_TRGTYPE ]); +} + + + +int RawFTSWFormat_latest::GetTTCtime(int n) +{ + return (int)((GetTTCtimeTRGType(n) & TTCTIME_MASK) >> TTCTIME_SHIFT); +} + +int RawFTSWFormat_latest::GetTRGType(int n) +{ + return (int)(GetTTCtimeTRGType(n) & TRGTYPE_MASK); +} + +unsigned int RawFTSWFormat_latest::GetTTUtime(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_TT_UTIME ]); +} + +void RawFTSWFormat_latest::GetTTTimeVal(int n, struct timeval* tv) +{ + tv->tv_sec = GetTTUtime(n); + tv->tv_usec = (int)(((double)GetTTCtime(n)) / 127.216); + + return ; +} + +void RawFTSWFormat_latest::GetTTTimeSpec(int n, struct timespec* ts) +{ + ts->tv_sec = GetTTUtime(n); + ts->tv_nsec = (long)(((double)GetTTCtime(n)) / 0.127216); + + return ; +} + +void RawFTSWFormat_latest::GetPCTimeVal(int n, struct timeval* tv) +{ + tv->tv_sec = (unsigned int)(m_buffer[ GetBufferPos(n) + POS_TVSEC_FROM_PC ]); + tv->tv_usec = (unsigned int)(m_buffer[ GetBufferPos(n) + POS_TVUSEC_FROM_PC ]); + return ; +} + + +unsigned long long int RawFTSWFormat_latest::GetTTTimeNs(int n) +{ + return (unsigned long long int)GetTTUtime(n) * 1e9 + (long)((double)GetTTCtime(n) / 0.127216); +} + +int RawFTSWFormat_latest::GetNwordsHeader(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_HDR_NWORDS ]; +} + + +unsigned int RawFTSWFormat_latest::GetFTSWNodeID(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_NODE_ID ]); +} + +unsigned int RawFTSWFormat_latest::GetEveNo(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_EVE_NO_1 ]; +} + +unsigned int RawFTSWFormat_latest::GetMagicTrailer(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_MAGIC_1 ]; +} + +int RawFTSWFormat_latest::GetIsHER(int n) +{ + int* buffer = GetBuffer(n); + int ret = (buffer[ POS_INJECTION_INFO ] & INJ_HER_LER_MASK) >> INJ_HER_LER_SHIFT; + return ret; // 1 -> HER, 0 -> LER +} + + +unsigned int RawFTSWFormat_latest::GetTimeSinceLastInjection(int n) +{ + unsigned int* buffer = (unsigned int*) GetBuffer(n); + unsigned int ret = (buffer[ POS_INJECTION_INFO ] & INJ_TIME_MASK) >> INJ_TIME_SHIFT; + return ret; +} + +unsigned int RawFTSWFormat_latest::GetTimeSincePrevTrigger(int n) +{ + unsigned int* buffer = (unsigned int*) GetBuffer(n); + unsigned int ret = buffer[ POS_TIME_SINCE_PREV_TRG ]; + return ret; +} + +unsigned int RawFTSWFormat_latest::GetBunchNumber(int n) +{ + unsigned int* buffer = (unsigned int*) GetBuffer(n); + unsigned int ret = (buffer[ POS_BUNCH_NUM ] & INJ_BUNCH_MASK) >> INJ_BUNCH_SHIFT; + return ret; +} + +unsigned int RawFTSWFormat_latest::GetFrameCount(int n) +{ + unsigned int* buffer = (unsigned int*) GetBuffer(n); + unsigned int ret = buffer[ POS_FRAME_COUNT ]; + return ret; +} + + +void RawFTSWFormat_latest::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + int err_flag = 0; + char err_buf[500]; + *cur_evenum = GetEveNo(n); + *cur_exprunsubrun_no = GetExpRunSubrun(n); + + +#ifndef NO_DATA_CHECK + if (prev_exprunsubrun_no == *cur_exprunsubrun_no) { + if ((unsigned int)(prev_evenum + 1) != *cur_evenum) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n %s %s %d\n", + n, prev_evenum, *cur_evenum, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + err_flag = 1; + } + } +#endif + + if (GetBlockNwords(n) != SIZE_FTSW_PACKET) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : invalid FTSW packet length : block %d nwords %d must be %d : Exiting...\n %s %s %d\n", + n, GetBlockNwords(n), SIZE_FTSW_PACKET, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + err_flag = 1; + } + + if (GetMagicTrailer(n) != FTSW_MAGIC_TRAILER) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : invalid magic word : block %d magic word 0x%x must be 0x%x : Exiting...\n %s %s %d\n", + n, GetMagicTrailer(n), FTSW_MAGIC_TRAILER, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + err_flag = 1; + } + + if (err_flag == 1) { + printf("[DEBUG] ========== dump a data block : block # %d==========\n", n); + printf("[DEBUG] "); + for (int k = 0 ; k < GetBlockNwords(n); k++) { + printf("0x%.8x ", (GetBuffer(n))[k]); + if (k % 10 == 9) printf("\n[DEBUG] "); + } + fflush(stderr); + B2FATAL(err_buf); + } + + return; +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat_v1.cc b/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat_v1.cc new file mode 100644 index 0000000000000000000000000000000000000000..14e1f29a849f748559907b99810c1b3db5fa3dc4 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat_v1.cc @@ -0,0 +1,139 @@ +//+ +// File : RawFTSWFormat_v1.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- +#include <rawdata/dataobjects/RawFTSWFormat_v1.h> + + +using namespace std; +using namespace Belle2; + +RawFTSWFormat_v1::RawFTSWFormat_v1() +{ +} + +RawFTSWFormat_v1::~RawFTSWFormat_v1() +{ +} + + +int RawFTSWFormat_v1::Get15bitTLUTag(int n) +{ + return (int)((unsigned int)(m_buffer[ GetBufferPos(n) + POS_FTSW_4 ]) & 0x00007FFF); +} + +unsigned int RawFTSWFormat_v1::GetTTCtimeTRGType(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_TT_CTIME_TRGTYPE ]); +} + + + +int RawFTSWFormat_v1::GetTTCtime(int n) +{ + return (int)((GetTTCtimeTRGType(n) & TTCTIME_MASK) >> TTCTIME_SHIFT); +} + +int RawFTSWFormat_v1::GetTRGType(int n) +{ + return (int)(GetTTCtimeTRGType(n) & TRGTYPE_MASK); +} + +unsigned int RawFTSWFormat_v1::GetTTUtime(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_TT_UTIME ]); +} + +void RawFTSWFormat_v1::GetTTTimeVal(int n, struct timeval* tv) +{ + tv->tv_sec = GetTTUtime(n); + tv->tv_usec = (int)(((double)GetTTCtime(n)) / 127.216); + + return ; +} + +void RawFTSWFormat_v1::GetTTTimeSpec(int n, struct timespec* ts) +{ + ts->tv_sec = GetTTUtime(n); + ts->tv_nsec = (long)(((double)GetTTCtime(n)) / 0.127216); + + return ; +} + +unsigned long long int RawFTSWFormat_v1::GetTTTimeNs(int n) +{ + return (unsigned long long int)GetTTUtime(n) * 1e9 + (long)((double)GetTTCtime(n) / 0.127216); +} + +int RawFTSWFormat_v1::GetNwordsHeader(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_HDR_NWORDS ]; +} + + +unsigned int RawFTSWFormat_v1::GetFTSWNodeID(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_NODE_ID ]); +} + +unsigned int RawFTSWFormat_v1::GetEveNo(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_EVE_NO ]; +} + +unsigned int RawFTSWFormat_v1::GetMagicTrailer(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_MAGIC_1 ]; +} + +void RawFTSWFormat_v1::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + int err_flag = 0; + char err_buf[500]; + *cur_evenum = GetEveNo(n); + *cur_exprunsubrun_no = GetExpRunSubrun(n); + + +#ifndef NO_DATA_CHECK + if (prev_exprunsubrun_no == *cur_exprunsubrun_no) { + if ((unsigned int)(prev_evenum + 1) != *cur_evenum) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n %s %s %d\n", + n, prev_evenum, *cur_evenum, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + err_flag = 1; + } + } +#endif + + if (GetBlockNwords(n) != SIZE_FTSW_PACKET) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : invalid FTSW packet length : block %d nwords %d must be %d : Exiting...\n %s %s %d\n", + n, GetBlockNwords(n), SIZE_FTSW_PACKET, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + err_flag = 1; + } + + if (GetMagicTrailer(n) != FTSW_MAGIC_TRAILER) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : invalid magic word : block %d magic word 0x%x must be 0x%x : Exiting...\n %s %s %d\n", + n, GetMagicTrailer(n), FTSW_MAGIC_TRAILER, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + err_flag = 1; + } + + if (err_flag == 1) { + printf("[DEBUG] ========== dump a data block : block # %d==========\n", n); + printf("[DEBUG] "); + for (int k = 0 ; k < GetBlockNwords(n); k++) { + printf("0x%.8x ", (GetBuffer(n))[k]); + if (k % 10 == 9) printf("\n[DEBUG] "); + } + fflush(stderr); + B2FATAL(err_buf); + } + + return; +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat_v2.cc b/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat_v2.cc new file mode 100644 index 0000000000000000000000000000000000000000..90108611f3eab0e8df057102eafc6dd0f5dff573 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawFTSWFormat_v2.cc @@ -0,0 +1,172 @@ +//+ +// File : RawFTSWFormat_v2.cc +// Description : Module to handle raw data from COPPER. +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 18 - Feb - 2019 +//- +#include <rawdata/dataobjects/RawFTSWFormat_v2.h> + + +using namespace std; +using namespace Belle2; + +RawFTSWFormat_v2::RawFTSWFormat_v2() +{ +} + +RawFTSWFormat_v2::~RawFTSWFormat_v2() +{ +} + + +unsigned int RawFTSWFormat_v2::GetTTCtimeTRGType(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_TT_CTIME_TRGTYPE ]); +} + + + +int RawFTSWFormat_v2::GetTTCtime(int n) +{ + return (int)((GetTTCtimeTRGType(n) & TTCTIME_MASK) >> TTCTIME_SHIFT); +} + +int RawFTSWFormat_v2::GetTRGType(int n) +{ + return (int)(GetTTCtimeTRGType(n) & TRGTYPE_MASK); +} + +unsigned int RawFTSWFormat_v2::GetTTUtime(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_TT_UTIME ]); +} + +void RawFTSWFormat_v2::GetTTTimeVal(int n, struct timeval* tv) +{ + tv->tv_sec = GetTTUtime(n); + tv->tv_usec = (int)(((double)GetTTCtime(n)) / 127.216); + + return ; +} + +void RawFTSWFormat_v2::GetTTTimeSpec(int n, struct timespec* ts) +{ + ts->tv_sec = GetTTUtime(n); + ts->tv_nsec = (long)(((double)GetTTCtime(n)) / 0.127216); + + return ; +} + +unsigned long long int RawFTSWFormat_v2::GetTTTimeNs(int n) +{ + return (unsigned long long int)GetTTUtime(n) * 1e9 + (long)((double)GetTTCtime(n) / 0.127216); +} + +int RawFTSWFormat_v2::GetNwordsHeader(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_HDR_NWORDS ]; +} + + +unsigned int RawFTSWFormat_v2::GetFTSWNodeID(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_NODE_ID ]); +} + +unsigned int RawFTSWFormat_v2::GetEveNo(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_EVE_NO_1 ]; +} + +unsigned int RawFTSWFormat_v2::GetMagicTrailer(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_MAGIC_1 ]; +} + +int RawFTSWFormat_v2::GetIsHER(int n) +{ + int* buffer = GetBuffer(n); + int ret = (buffer[ POS_INJECTION_INFO ] & INJ_HER_LER_MASK) >> INJ_HER_LER_SHIFT; + return ret; // 1 -> HER, 0 -> LER +} + + +unsigned int RawFTSWFormat_v2::GetTimeSinceLastInjection(int n) +{ + unsigned int* buffer = (unsigned int*) GetBuffer(n); + unsigned int ret = (buffer[ POS_INJECTION_INFO ] & INJ_TIME_MASK) >> INJ_TIME_SHIFT; + return ret; +} + +unsigned int RawFTSWFormat_v2::GetTimeSincePrevTrigger(int n) +{ + unsigned int* buffer = (unsigned int*) GetBuffer(n); + unsigned int ret = buffer[ POS_TIME_SINCE_PREV_TRG ]; + return ret; +} + +unsigned int RawFTSWFormat_v2::GetBunchNumber(int n) +{ + unsigned int* buffer = (unsigned int*) GetBuffer(n); + unsigned int ret = (buffer[ POS_BUNCH_NUM ] & INJ_BUNCH_MASK) >> INJ_BUNCH_SHIFT; + return ret; +} + +unsigned int RawFTSWFormat_v2::GetFrameCount(int n) +{ + unsigned int* buffer = (unsigned int*) GetBuffer(n); + unsigned int ret = buffer[ POS_FRAME_COUNT ]; + return ret; +} + + +void RawFTSWFormat_v2::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum, + unsigned int prev_exprunsubrun_no, unsigned int* cur_exprunsubrun_no) +{ + int err_flag = 0; + char err_buf[500]; + *cur_evenum = GetEveNo(n); + *cur_exprunsubrun_no = GetExpRunSubrun(n); + + +#ifndef NO_DATA_CHECK + if (prev_exprunsubrun_no == *cur_exprunsubrun_no) { + if ((unsigned int)(prev_evenum + 1) != *cur_evenum) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n %s %s %d\n", + n, prev_evenum, *cur_evenum, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + err_flag = 1; + } + } +#endif + + if (GetBlockNwords(n) != SIZE_FTSW_PACKET) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : invalid FTSW packet length : block %d nwords %d must be %d : Exiting...\n %s %s %d\n", + n, GetBlockNwords(n), SIZE_FTSW_PACKET, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + err_flag = 1; + } + + if (GetMagicTrailer(n) != FTSW_MAGIC_TRAILER) { + sprintf(err_buf, "[FATAL] ERROR_EVENT : invalid magic word : block %d magic word 0x%x must be 0x%x : Exiting...\n %s %s %d\n", + n, GetMagicTrailer(n), FTSW_MAGIC_TRAILER, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); + err_flag = 1; + } + + if (err_flag == 1) { + printf("[DEBUG] ========== dump a data block : block # %d==========\n", n); + printf("[DEBUG] "); + for (int k = 0 ; k < GetBlockNwords(n); k++) { + printf("0x%.8x ", (GetBuffer(n))[k]); + if (k % 10 == 9) printf("\n[DEBUG] "); + } + fflush(stderr); + string err_str = err_buf; throw (err_str); + + } + + return; +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawHeader_latest.cc b/ROPCApplications/rawdata/dataobjects/src/RawHeader_latest.cc new file mode 100644 index 0000000000000000000000000000000000000000..a96d510b369148eb88b88c68782784d73c584cf4 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawHeader_latest.cc @@ -0,0 +1,134 @@ +//+ +// File : RawHeader_latest.cc +// Description : Module to handle RawHeader_latest attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawHeader_latest.h> + + +using namespace Belle2; + +//ClassImp(RawHeader_latest); + +RawHeader_latest::RawHeader_latest() +{ + m_buffer = NULL; + // initialize(); + // cout << "RawHeader_latest NULL constructor" << endl; +} + +RawHeader_latest::RawHeader_latest(int* buffer) +{ + m_buffer = buffer; +} + +RawHeader_latest::~RawHeader_latest() +{ +} + + +// int RawHeader_latest::AddNodeInfo(int node_id) +// { +// CheckSetBuffer(); +// // When the number of total nodes exceeds NUM_MAX_NODES +// if (GetNumNodes() >= NUM_MAX_NODES) { +// m_buffer[ POS_NUM_NODES ]++; +// return -1; +// } + +// m_buffer[ POS_NODES_1 + m_buffer[ POS_NUM_NODES ] ] = node_id; +// m_buffer[ POS_NUM_NODES ]++; +// return 0; +// } + +// int RawHeader_latest::GetNodeInfo(int node_no, int* node_id) +// { +// CheckGetBuffer(); +// if (node_no >= GetNumNodes()) { +// return -1; +// } +// *node_id = m_buffer[ POS_NODES_1 + node_no ]; + +// return 0; +// } + + + + +void RawHeader_latest::CheckHeader(int* buf) +{ + + // # of words + if (buf[ POS_NWORDS ] > 2.5e7 || buf[ POS_NWORDS ] < RAWHEADER_NWORDS) { + char err_buf[500]; + sprintf(err_buf, "data size error %d words. Exiting... : %s %s %d\n", + buf[ POS_NWORDS ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + // # of words in this block + if ((buf[ POS_VERSION_HDRNWORDS ] & HDR_NWORDS_MASK) != RAWHEADER_NWORDS) { + char err_buf[500]; + sprintf(err_buf, "Invalid header size %d words should be %d. Exiting... : %s %s %d\n", + buf[ buf[ POS_VERSION_HDRNWORDS ] & HDR_NWORDS_MASK ], + RAWHEADER_NWORDS, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + // exp. run # + + // utime( 2010 1/1 - 2050 1/1 ) + if ((unsigned int)(buf[ POS_TTUTIME ]) < (unsigned int)0x4b3cbc70 || + (unsigned int)0x9679f770 < (unsigned int)(buf[ POS_TTUTIME ])) { + char err_buf[500]; + sprintf(err_buf, "Invalid utime %d . Exiting...: %s %s %d\n", + buf[ POS_TTUTIME ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + // truncation mask + + // offset + if (buf[ POS_OFFSET_1ST_FINESSE ] < RAWHEADER_NWORDS) { + char err_buf[500]; + sprintf(err_buf, "Invalid data offset for 1st finesse buffer(%d). Exiting...: %s %s %d\n", + buf[ POS_OFFSET_1ST_FINESSE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (buf[ POS_OFFSET_2ND_FINESSE ] < buf[ POS_OFFSET_1ST_FINESSE ]) { + char err_buf[500]; + sprintf(err_buf, "Invalid data offset for 2nd finesse buffer(%d). Exiting...: %s %s %d\n", + buf[ POS_OFFSET_2ND_FINESSE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (buf[ POS_OFFSET_3RD_FINESSE ] < buf[ POS_OFFSET_2ND_FINESSE ]) { + char err_buf[500]; + sprintf(err_buf, "Invalid data offset for 3rd finesse buffer(%d). Exiting...: %s %s %d\n", + buf[ POS_OFFSET_3RD_FINESSE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (buf[ POS_OFFSET_4TH_FINESSE ] < buf[ POS_OFFSET_3RD_FINESSE ] || + buf[ POS_OFFSET_4TH_FINESSE ] > buf[ POS_NWORDS ]) { + char err_buf[500]; + sprintf(err_buf, "Invalid data offset for 4th finesse buffer(%d). Exiting...: %s %s %d\n", + buf[ POS_OFFSET_4TH_FINESSE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + return; + + +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawHeader_v0.cc b/ROPCApplications/rawdata/dataobjects/src/RawHeader_v0.cc new file mode 100644 index 0000000000000000000000000000000000000000..3e44ad9bb7c838a90abf4816f588c41f8ffeeb08 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawHeader_v0.cc @@ -0,0 +1,57 @@ +//+ +// File : RawHeader_v0.cc +// Description : Module to handle RawHeader_v0 attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawHeader_v0.h> + + + +using namespace Belle2; + +//ClassImp(RawHeader_v0); + +RawHeader_v0::RawHeader_v0() +{ + m_buffer = NULL; + // initialize(); + // cout << "RawHeader_v0 NULL constructor" << endl; +} + +RawHeader_v0::RawHeader_v0(int* buffer) +{ + m_buffer = buffer; +} + +RawHeader_v0::~RawHeader_v0() +{ +} + + +int RawHeader_v0::AddNodeInfo(int node_id) +{ + CheckSetBuffer(); + // When the number of total nodes exceeds NUM_MAX_NODES + if (GetNumNodes() >= NUM_MAX_NODES) { + m_buffer[ POS_NUM_NODES ]++; + return -1; + } + + m_buffer[ POS_NODES_1 + m_buffer[ POS_NUM_NODES ] ] = node_id; + m_buffer[ POS_NUM_NODES ]++; + return 0; +} + +int RawHeader_v0::GetNodeInfo(int node_no, int* node_id) +{ + CheckGetBuffer(); + if (node_no >= GetNumNodes()) { + return -1; + } + *node_id = m_buffer[ POS_NODES_1 + node_no ]; + + return 0; +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawHeader_v1.cc b/ROPCApplications/rawdata/dataobjects/src/RawHeader_v1.cc new file mode 100644 index 0000000000000000000000000000000000000000..517a50bbbc78fb065b09b2648f5d094c8068e571 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawHeader_v1.cc @@ -0,0 +1,136 @@ +//+ +// File : RawHeader_v1.cc +// Description : Module to handle RawHeader_v1 attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawHeader_v1.h> + + + +using namespace Belle2; + +//ClassImp(RawHeader_v1); + +RawHeader_v1::RawHeader_v1() +{ + m_buffer = NULL; + // initialize(); + // cout << "RawHeader_v1 NULL constructor" << endl; +} + +RawHeader_v1::RawHeader_v1(int* buffer) +{ + m_buffer = buffer; +} + +RawHeader_v1::~RawHeader_v1() +{ +} + + +// int RawHeader_v1::AddNodeInfo(int node_id) +// { +// CheckSetBuffer(); +// // When the number of total nodes exceeds NUM_MAX_NODES +// if (GetNumNodes() >= NUM_MAX_NODES) { +// m_buffer[ POS_NUM_NODES ]++; +// return -1; +// } + +// m_buffer[ POS_NODES_1 + m_buffer[ POS_NUM_NODES ] ] = node_id; +// m_buffer[ POS_NUM_NODES ]++; +// return 0; +// } + +// int RawHeader_v1::GetNodeInfo(int node_no, int* node_id) +// { +// CheckGetBuffer(); +// if (node_no >= GetNumNodes()) { +// return -1; +// } +// *node_id = m_buffer[ POS_NODES_1 + node_no ]; + +// return 0; +// } + + + + +void RawHeader_v1::CheckHeader(int* buf) +{ + + // # of words + if (buf[ POS_NWORDS ] > 2.5e7 || + buf[ POS_NWORDS ] < RAWHEADER_NWORDS) { + char err_buf[500]; + sprintf(err_buf, "data size error %d words. Exiting... : %s %s %d\n", + buf[ POS_NWORDS ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + // # of words in this block + if ((buf[ POS_VERSION_HDRNWORDS ] & HDR_NWORDS_MASK) != RAWHEADER_NWORDS) { + char err_buf[500]; + sprintf(err_buf, "Invalid header size %d words should be %d. Exiting... : %s %s %d\n", + buf[ buf[ POS_VERSION_HDRNWORDS ] & HDR_NWORDS_MASK ], + RAWHEADER_NWORDS, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + // exp. run # + + // utime( 2010 1/1 - 2050 1/1 ) + if ((unsigned int)(buf[ POS_TTUTIME ]) < (unsigned int)0x4b3cbc70 || + (unsigned int)0x9679f770 < (unsigned int)(buf[ POS_TTUTIME ])) { + char err_buf[500]; + sprintf(err_buf, "Invalid utime %d . Exiting...: %s %s %d\n", + buf[ POS_TTUTIME ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + // truncation mask + + // offset + if (buf[ POS_OFFSET_1ST_FINESSE ] < RAWHEADER_NWORDS) { + char err_buf[500]; + sprintf(err_buf, "Invalid data offset for 1st finesse buffer(%d). Exiting...: %s %s %d\n", + buf[ POS_OFFSET_1ST_FINESSE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); fflush(stdout); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (buf[ POS_OFFSET_2ND_FINESSE ] < buf[ POS_OFFSET_1ST_FINESSE ]) { + char err_buf[500]; + sprintf(err_buf, "Invalid data offset for 2nd finesse buffer(%d). Exiting...: %s %s %d\n", + buf[ POS_OFFSET_2ND_FINESSE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (buf[ POS_OFFSET_3RD_FINESSE ] < buf[ POS_OFFSET_2ND_FINESSE ]) { + char err_buf[500]; + sprintf(err_buf, "Invalid data offset for 3rd finesse buffer(%d). Exiting...: %s %s %d\n", + buf[ POS_OFFSET_3RD_FINESSE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + if (buf[ POS_OFFSET_4TH_FINESSE ] < buf[ POS_OFFSET_3RD_FINESSE ] || + buf[ POS_OFFSET_4TH_FINESSE ] > buf[ POS_NWORDS ]) { + char err_buf[500]; + sprintf(err_buf, "Invalid data offset for 4th finesse buffer(%d). Exiting...: %s %s %d\n", + buf[ POS_OFFSET_4TH_FINESSE ], __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + B2FATAL(err_buf); + } + + return; + + +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawKLM.cc b/ROPCApplications/rawdata/dataobjects/src/RawKLM.cc new file mode 100644 index 0000000000000000000000000000000000000000..b866c31eff1ad29c923d13d1e4713ab439b59d74 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawKLM.cc @@ -0,0 +1,20 @@ +//+ +// File : RawCOPPER.cc +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawKLM.h> + +using namespace std; +using namespace Belle2; + +RawKLM::RawKLM() +{ +} + +RawKLM::~RawKLM() +{ +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawPXD.cc b/ROPCApplications/rawdata/dataobjects/src/RawPXD.cc new file mode 100644 index 0000000000000000000000000000000000000000..1571db423ee76a64bdb1997151f33000a1896dc4 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawPXD.cc @@ -0,0 +1,96 @@ +//+ +// File : RawPXD.cc +// Description : RawPXD Data format +// +// Author : Bjoern Spruck / Klemens Lautenbach +// Date : 13 - Aug - 2013 +//- + +#include "rawdata/dataobjects/RawPXD.h" +#include "framework/logging/Logger.h" +#include <framework/utilities/HTML.h> + +using namespace std; +using namespace Belle2; + +RawPXD::RawPXD() : m_nwords(0), m_buffer(NULL) +{ +} + +RawPXD::RawPXD(int* buffer, int length_in_Bytes) + : m_nwords(0), m_buffer(NULL) +{ + m_nwords = (length_in_Bytes + 3) / 4; + m_buffer = new int[m_nwords]; + memcpy(m_buffer, buffer, m_nwords * sizeof(int)); +} + +unsigned int RawPXD::endian_swap(unsigned int x) +{ + x = (x >> 24) | + ((x << 8) & 0x00FF0000) | + ((x >> 8) & 0x0000FF00) | + (x << 24); + return x; +} + +RawPXD::RawPXD(const std::vector <unsigned int>& header, const std::vector <std::vector <unsigned char>>& payload) + : m_nwords(0), m_buffer(NULL) +{ + // This function is only used by the PXDPacker in simulations. Does not make sense for other cases + // Now create header from payload , this can be done with less loops, but speed is not the issue here + int nr_frames = header.size(); + int payload_size = 0; // in 32 bit words + for (auto& it : header) { + payload_size += (it + 3) / 4; // in 32 bit word, rounded up + } + + m_nwords = 2 + nr_frames + payload_size; // 321 bit words + + m_buffer = new int[m_nwords]; + + B2DEBUG(20, "RawPXD Frames " << header.size() << " Data " << payload_size << " (32 bitword)"); + + // now we know the actual payload length + int offset = 0; + m_buffer[offset++] = 0xBEBAFECA ; // 0xCAFEBABEu; + m_buffer[offset++] = endian_swap(nr_frames); + // and now append the frame length table + for (auto& it : payload) { + m_buffer[offset++] = endian_swap(it.size()); // in chars, rounded up to 32 bit boundary + } + // and now append the actual paylaod data + unsigned char* data = (unsigned char*) &m_buffer[offset]; + for (auto& it : payload) { + memcpy(data, it.data(), it.size()); + data += (it.size() + 3) & 0xFFFFFFFC; // in chars, rounded up to 32 bit boundary + } + // done +} + +RawPXD::~RawPXD() +{ + if (m_buffer != NULL) delete[] m_buffer; +} + +int RawPXD::size() const +{ + return m_nwords; +} + +int* RawPXD::data(void) +{ + int* ptr = &m_buffer[0]; + return ptr; +} + +std::string RawPXD::getInfoHTML() const +{ + std::string s; + if (m_nwords >= 2) + s += "Frames: " + std::to_string(endian_swap(m_buffer[1])) + ", "; + + s += "Size (32bit words): " + std::to_string(m_nwords) + "<br /><br />"; + s += HTML::getHexDump(m_buffer, m_nwords); + return s; +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawSVD.cc b/ROPCApplications/rawdata/dataobjects/src/RawSVD.cc new file mode 100644 index 0000000000000000000000000000000000000000..9954e8b5f6cc609586c5504691aa3bcd4fa0b75e --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawSVD.cc @@ -0,0 +1,20 @@ +//+ +// File : RawCOPPER.cc +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawSVD.h> + +using namespace std; +using namespace Belle2; + +RawSVD::RawSVD() +{ +} + +RawSVD::~RawSVD() +{ +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawTLU.cc b/ROPCApplications/rawdata/dataobjects/src/RawTLU.cc new file mode 100644 index 0000000000000000000000000000000000000000..a5bf184a86f33518e66fba92be7a3eea5dad716c --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawTLU.cc @@ -0,0 +1,22 @@ +//+ +// File : RawTLU.cc +// Description : Module to handle raw data from TLU(for the telescope beam test) +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Jan - 2014 +//- + +#include <rawdata/dataobjects/RawTLU.h> +using namespace std; +using namespace Belle2; + +RawTLU::RawTLU() +{ + m_access = new RawTLUFormat; +} + +RawTLU::~RawTLU() +{ + if (m_access != NULL) delete m_access; + m_access = NULL; +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawTLUFormat.cc b/ROPCApplications/rawdata/dataobjects/src/RawTLUFormat.cc new file mode 100644 index 0000000000000000000000000000000000000000..d3f5bd961e01c4c49c4f1d48698a95060cee4903 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawTLUFormat.cc @@ -0,0 +1,108 @@ +//+ +// File : RawTLUFormat.cc +// Description : Module to handle raw data from TLU(for the telescope beam test) +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Jan - 2014 +//- + +#include <rawdata/dataobjects/RawTLUFormat.h> +using namespace std; +using namespace Belle2; + +RawTLUFormat::RawTLUFormat() +{ +} + +RawTLUFormat::~RawTLUFormat() +{ +} + +// int RawTLUFormat::GetNwords(int n) +// { +// return m_buffer[ GetBufferPos(n) + POS_NWORDS ]; +// } + +int RawTLUFormat::GetNwordsHeader(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_HDR_NWORDS ]; +} + + +unsigned int RawTLUFormat::GetNodeID(int n) +{ + return (unsigned int)(m_buffer[ GetBufferPos(n) + POS_NODE_ID ]); +} + +unsigned int RawTLUFormat::GetEveNo(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_EVE_NO ]; +} + + +int RawTLUFormat::GetRunNo(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_RUN_NO ]; +} + + +unsigned int RawTLUFormat::GetMagicTrailer(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_MAGIC_1 ]; +} + +unsigned int RawTLUFormat::GetTLUEventTag(int n) +{ + return m_buffer[ GetBufferPos(n) + POS_TLU_EVENTTAG ]; +} + + +void RawTLUFormat::CheckData(int n, + unsigned int prev_evenum, unsigned int* cur_evenum) +{ + int err_flag = 0; + char err_buf[500]; + *cur_evenum = GetEveNo(n); + +#ifndef NO_DATA_CHECK + if (prev_evenum != 0xFFFFFFFF && *cur_evenum != 0) { + if ((unsigned int)(prev_evenum + 1) != *cur_evenum) { + sprintf(err_buf, "Event # jump : i %d prev 0x%x cur 0x%x : Exiting...\n %s %s %d\n", + n, prev_evenum, *cur_evenum, __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + } +#endif + + if (GetEveNo(n) != GetTLUEventTag(n)) { + sprintf(err_buf, "invalid TLU event tag : blodk %d header eve # %u TLU tag %u. Exiting...\n %s %s %d\n", + n, GetEveNo(n), GetTLUEventTag(n), __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + + + if (GetBlockNwords(n) != SIZE_TLU_PACKET) { + sprintf(err_buf, "invalid TLU packet length : block %d nwords %d must be %d : Exiting...\n %s %s %d\n", + n, GetBlockNwords(n), SIZE_TLU_PACKET, __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + + if (GetMagicTrailer(n) != TLU_MAGIC_TRAILER) { + sprintf(err_buf, "invalid magic word : block %d magic word 0x%x must be 0x%x : Exiting...\n %s %s %d\n", + n, GetMagicTrailer(n), TLU_MAGIC_TRAILER, __FILE__, __PRETTY_FUNCTION__, __LINE__); + err_flag = 1; + } + + if (err_flag == 1) { + printf("[DEBUG] ========== dump a data block : block # %d==========\n", n); + printf("[DEBUG] "); + for (int k = 0 ; k < GetBlockNwords(n); k++) { + printf("0x%.8x ", (GetBuffer(n))[k]); + if (k % 10 == 9) printf("\n[DEBUG] "); + } + fflush(stderr); + B2FATAL(err_buf); + } + + return; +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawTOP.cc b/ROPCApplications/rawdata/dataobjects/src/RawTOP.cc new file mode 100644 index 0000000000000000000000000000000000000000..34cdf87b77baa7f36236360627fd84a9985aed1b --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawTOP.cc @@ -0,0 +1,20 @@ +//+ +// File : RawCOPPER.cc +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawTOP.h> + +using namespace std; +using namespace Belle2; + +RawTOP::RawTOP() +{ +} + +RawTOP::~RawTOP() +{ +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawTRG.cc b/ROPCApplications/rawdata/dataobjects/src/RawTRG.cc new file mode 100644 index 0000000000000000000000000000000000000000..518c6831cef63391a019ce6c489f5035ebbcfc0f --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawTRG.cc @@ -0,0 +1,22 @@ +//+ +// File : RawTRG.cc +// Description : Module to handle raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawTRG.h> + +using namespace std; +using namespace Belle2; + +RawTRG::RawTRG() +{ +} + +RawTRG::~RawTRG() +{ +} + + diff --git a/ROPCApplications/rawdata/dataobjects/src/RawTrailer_latest.cc b/ROPCApplications/rawdata/dataobjects/src/RawTrailer_latest.cc new file mode 100644 index 0000000000000000000000000000000000000000..12ec673cb58ffda8e8f4d936018af8e070611b99 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawTrailer_latest.cc @@ -0,0 +1,22 @@ +//+ +// File : RawTrailer_latest.cc +// Description : Module to handle RawTraeiler attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawTrailer_latest.h> + +using namespace Belle2; + +//ClassImp(RawTrailer_latest); + +RawTrailer_latest::RawTrailer_latest() +{ + m_buffer = NULL; +} + +RawTrailer_latest::~RawTrailer_latest() +{ +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawTrailer_v0.cc b/ROPCApplications/rawdata/dataobjects/src/RawTrailer_v0.cc new file mode 100644 index 0000000000000000000000000000000000000000..935528bab3b7606d2b99a3cc316fbcf56be0f128 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawTrailer_v0.cc @@ -0,0 +1,22 @@ +//+ +// File : RawTrailer_v0.cc +// Description : Module to handle RawTraeiler attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawTrailer_v0.h> + +using namespace Belle2; + +//ClassImp(RawTrailer_v0); + +RawTrailer_v0::RawTrailer_v0() +{ + m_buffer = NULL; +} + +RawTrailer_v0::~RawTrailer_v0() +{ +} diff --git a/ROPCApplications/rawdata/dataobjects/src/RawTrailer_v1.cc b/ROPCApplications/rawdata/dataobjects/src/RawTrailer_v1.cc new file mode 100644 index 0000000000000000000000000000000000000000..ebdecededc23cbf246a0ea40f70bbd1b5f496796 --- /dev/null +++ b/ROPCApplications/rawdata/dataobjects/src/RawTrailer_v1.cc @@ -0,0 +1,22 @@ +//+ +// File : RawTrailer_v1.cc +// Description : Module to handle RawTraeiler attached to raw data from COPPER +// +// Author : Satoru Yamada, IPNS, KEK +// Date : 2 - Aug - 2013 +//- + +#include <rawdata/dataobjects/RawTrailer_v1.h> + +using namespace Belle2; + +//ClassImp(RawTrailer_v1); + +RawTrailer_v1::RawTrailer_v1() +{ + m_buffer = NULL; +} + +RawTrailer_v1::~RawTrailer_v1() +{ +} diff --git a/ROPCApplications/rawdata/include/CRCCalculator.h b/ROPCApplications/rawdata/include/CRCCalculator.h new file mode 100644 index 0000000000000000000000000000000000000000..f3f6dcd0f86fcc418dce4d366e7bbbe62f2178ca --- /dev/null +++ b/ROPCApplications/rawdata/include/CRCCalculator.h @@ -0,0 +1,30 @@ +#ifndef CRCCALCULATOR_H +#define CRCCALCULATOR_H +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <cstddef> + + +//namespace Belle2 { + +/** + * Function to calculate CRC16 (polynomial 0x1021, initial value=0xffff) + * Belle2link(HSLB/FEE) trailers.are supposed to contain CRC information. + * This function will be used to check them in DAQ system if CPU power allows. + */ +unsigned short CalcCRC16LittleEndian(unsigned short crc16, const int buf[], int nwords); + +/** + * Function to copy data + * Just copy data from buf_from to (buf_to + pos_nwords_to) + * It also check buffer overflow by using nwords_buf_to + */ +void copyData(int* buf_to, int* pos_nwords_to, const int* buf_from, + const int copy_nwords, const int nwords_buf_to); + + + +//} +#endif diff --git a/ROPCApplications/rawdata/include/RawCOPPERPackerInfo.h b/ROPCApplications/rawdata/include/RawCOPPERPackerInfo.h new file mode 100644 index 0000000000000000000000000000000000000000..cf36177023e44d488fcac03e98a53520d68e5c68 --- /dev/null +++ b/ROPCApplications/rawdata/include/RawCOPPERPackerInfo.h @@ -0,0 +1,54 @@ +#ifndef RAWCOPPERPACKERINFO_H +#define RAWCOPPERPACKERINFO_H + +namespace Belle2 { + /** + * struct to contain header information used by RawCOPPERFormat::Packer() + */ + class RawCOPPERPackerInfo { + public : + + /// Experiment number (10bit) + unsigned int exp_num; /// Experiment number (10bit) + + unsigned int run_subrun_num; /// Run # and subrun # ( 22bit ) + + unsigned int eve_num; /// Event Number (32bit) + + unsigned int node_id; /// Node ID (32bit) + + unsigned int + tt_ctime; /// 27bit clock ticks at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user guide + + unsigned int + tt_utime; /// 32bit unitx time at trigger timing distributed by FTSW. For details, see Nakao-san's belle2link user guide + + unsigned int + b2l_ctime; /// 27bit clock ticks at trigger timing measured by HSLB on COPPER. For details, see Nakao-san's belle2link user guide + + unsigned int + hslb_crc16_error_bit; /// 4bit errorflag for CRC errors in data transfer via b2link. ( bit0,1,2,3 -> finesse slot a,b,c,d) + + unsigned int truncation_mask; /// Not defined yet + + unsigned int type_of_data; /// Not defined yet + + + //! struct of information to fill in a header + RawCOPPERPackerInfo() + { + exp_num = 0; + run_subrun_num = 0; + eve_num = 0; + node_id = 0x00000000; // See RawCOPPERFormat.h + tt_ctime = 0; + tt_utime = 0; + b2l_ctime = 0; + hslb_crc16_error_bit = 0x0; + truncation_mask = 0; + type_of_data = 0; + } + + }; +} +#endif diff --git a/ROPCApplications/rawdata/include/switch_basf2_standalone.h b/ROPCApplications/rawdata/include/switch_basf2_standalone.h new file mode 100644 index 0000000000000000000000000000000000000000..46cb91259a8bbabbbe5974a3a4167aab87d20c28 --- /dev/null +++ b/ROPCApplications/rawdata/include/switch_basf2_standalone.h @@ -0,0 +1,23 @@ +#pragma once + +#define NOBASF2 + +#ifndef B2FATAL + +#ifdef NOBASF2 +#include <iostream> +#define B2FATAL(a) {std::cerr << a << std::endl; exit(1);} +#else +#include <framework/logging/Logger.h> +#endif + +#endif + +// To compile DAQ program on COPPER(SL5.7) +#ifndef OVERRIDE_CPP17 +#ifndef NOBASF2 +#define OVERRIDE_CPP17 override +#else +#define OVERRIDE_CPP17 +#endif +#endif diff --git a/ROPCApplications/rawdata/src/CRCCalculator.cc b/ROPCApplications/rawdata/src/CRCCalculator.cc new file mode 100644 index 0000000000000000000000000000000000000000..a59c998280e890efd98695092f5ae6614c8229da --- /dev/null +++ b/ROPCApplications/rawdata/src/CRCCalculator.cc @@ -0,0 +1,92 @@ +#include <rawdata/CRCCalculator.h> +#include <rawdata/switch_basf2_standalone.h> +#include <string> +using namespace std; + + + +unsigned short CalcCRC16LittleEndian(unsigned short crc16, const int buf[], int nwords) +{ + + if (nwords < 0) { + char err_buf[500]; + sprintf(err_buf, "nwords value(%d) is invalid. Cannot calculate CRC16. Exiting...\n %s %s %d\n", + nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__); + printf("%s", err_buf); fflush(stdout); + string err_str = err_buf; + throw (err_str); + } + + const unsigned short CRC16Table0x1021[ 256 ] = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, + 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, + 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, + 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, + 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, + + 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, + 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, + 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, + 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, + 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, + 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, + 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, + + 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, + 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, + 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, + 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, + 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, + + 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, + 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, + 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, + 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, + 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, + 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, + 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 + }; + + int cnt = 0, nints = 0; + // printf("### %.8x %.4x\n", buf[ 0 ], crc16); + while (nwords != 0) { + + unsigned char temp_buf = *((unsigned char*)(buf + nints) + (-(cnt % 4) + 3)); + crc16 = CRC16Table0x1021[(crc16 >> (16 - CHAR_BIT)) ^ temp_buf ] ^ (crc16 << CHAR_BIT); + // printf("%.2x %.4x\n", temp_buf, crc16); + if ((cnt % 4) == 3) { + nwords--; + nints++; + // printf("### %.8x\n", buf[ nints ] ); + } + + cnt++; + } + + + return crc16; + +} + + +void copyData(int* buf_to, int* pos_nwords_to, const int* buf_from, + const int copy_nwords, const int nwords_buf_to) +{ + + if (*pos_nwords_to + copy_nwords > nwords_buf_to) { + B2FATAL("Buffer overflow. Exiting..."); + } + + memcpy(buf_to + *pos_nwords_to, buf_from, copy_nwords * sizeof(int)); + *pos_nwords_to += copy_nwords; + return; +} + diff --git a/Scripts/pll_status_small.py b/Scripts/pll_status_small.py index 1647bf329970c393775fc3b0a8cd94d2ca257cc0..21036599d0e63d565a07b522ffed8f951eb9619a 100755 --- a/Scripts/pll_status_small.py +++ b/Scripts/pll_status_small.py @@ -127,33 +127,39 @@ def fanout_window(fpga, win): win.addstr(4,1,'Trigger counter : ' , curses.A_BOLD) win.addstr(4,19,'{0:5d}'.format(fpga.triggerCounter()),GREEN) - win.addstr(5,1,'RX ready : ' , curses.A_BOLD) - for link in range( 6 ): + + win.refresh() + + +def link_window(fpga, win): + #win.addstr(5,1,'RX ready : ' , curses.A_BOLD) + for link in range( 48 ): if fpga.validChannel( link ) == 0: - win.addstr(5,12+3*link, 'XX' , CYAN ) + win.addstr(5+link%24+int(link%24/6), 6 + 22 * int( link / 24 ) , 'XX' , CYAN ) elif fpga.rxReadyIndividual(link) == 1: - win.addstr(5,12+3*link, 'OK' , GREEN ) + win.addstr(5+link%24+int(link%24/6), 6 + 22 * int( link / 24 ) , 'OK' , GREEN ) else: - win.addstr(5,12+3*link,'NO',RED) - win.addstr(5,31,'TX ready : ' , curses.A_BOLD) - for link in range( 6 ): + win.addstr(5+link%24+int(link%24/6), 6 + 22 * int( link / 24 ) , 'NO' , RED) + #win.addstr(5,31,'TX ready : ' , curses.A_BOLD) + for link in range( 48 ): if fpga.validChannel( link ) == 0: - win.addstr(5,42+3*link, 'XX' , CYAN ) + win.addstr(5+link%24+int(link%24/6), 10 + 22 * int( link / 24 ), 'XX' , CYAN ) elif fpga.txReadyIndividual(link) == 1: - win.addstr(5,42+3*link, 'OK' , GREEN ) + win.addstr(5+link%24+int(link%24/6), 10 + 22 * int( link / 24 ), 'OK' , GREEN ) else: - win.addstr(5,42+3*link,'NO',RED) - win.addstr(5,62,'b2link ready : ' , curses.A_BOLD) - for link in range( 6 ): + win.addstr(5+link%24+int(link%24/6), 10 + 22 * int( link / 24 ), 'NO' , RED) + #win.addstr(5,62,'b2link ready : ' , curses.A_BOLD) + for link in range( 48 ): if fpga.validChannel( link ) == 0: - win.addstr(5,77+3*link, 'XX' , CYAN ) + win.addstr(5+link%24+int(link%24/6), 14 + 22 * int( link / 24 ), 'XX' , CYAN ) elif fpga.b2linkReadyIndividual( link ) == 1: - win.addstr(5,77+3*link, 'OK' , GREEN ) + win.addstr(5+link%24+int(link%24/6), 14 + 22 * int( link / 24 ), 'OK' , GREEN ) else: - win.addstr(5,77+3*link,'NO',RED) + win.addstr(5+link%24+int(link%24/6), 14 + 22 * int( link / 24 ), 'NO' , RED) win.refresh() + def program_plls(plls, devices, files, win): #win.move(2,1) @@ -297,7 +303,7 @@ def draw_menu(stdscr, dev): y_max, x_max = stdscr.getmaxyx() - win1 = stdscr.subwin(y_max/6, x_max-2, 0, 0) + win1 = stdscr.subwin(y_max/6, int(0.7*(x_max))-2, 0, 0) win1.box() win1.addstr(1, 1, "Command window", CYAN | curses.A_BOLD) board = EepromAT24C(dev, const.EEPROM_FPGA_BUS, const.EEPROM_U64_ADD) @@ -320,7 +326,7 @@ def draw_menu(stdscr, dev): win1.refresh() win2 = stdscr.subwin(y_max/6, - x_max - 2, + int(0.7*x_max - 2), y_max/6, 0) win2.box() @@ -328,36 +334,49 @@ def draw_menu(stdscr, dev): win2.refresh() win3 = stdscr.subwin(y_max/6, - x_max - 2, + int(0.7*x_max - 2), 2*y_max/6, 0) win3.box() win3.addstr(1, 1, "PLL SI54345_2", CYAN | curses.A_BOLD) win3.refresh() - win4 = stdscr.subwin( y_max/6 , x_max-2 , 3*y_max/6, 0 ) + win4 = stdscr.subwin( y_max/6 , int(0.7*x_max-2) , 3*y_max/6, 0 ) win4.box() win4.addstr(1, 1, "PLL SI54344", CYAN | curses.A_BOLD) win4.refresh() win5 = stdscr.subwin(y_max/6, - x_max -2, + int(0.7*x_max -2), 4*y_max/6, 0) win5.box() win5.addstr(1, 1, "Clock FTSW", CYAN | curses.A_BOLD) win5.refresh() + win7 = stdscr.subwin( y_max - 1 , int( 0.3*x_max ) , 0 , int(0.7*x_max) ) + win7.box() + win7.addstr( 4 , 6 , "RX" , curses.A_BOLD ) + win7.addstr( 4 , 10 , "TX" , curses.A_BOLD ) + win7.addstr( 4 , 14 , "B2L" , curses.A_BOLD ) + win7.addstr( 4 , 6+22 , "RX" , curses.A_BOLD ) + win7.addstr( 4 , 10+22 , "TX" , curses.A_BOLD ) + win7.addstr( 4 , 14+22 , "B2L" , curses.A_BOLD ) + for i in range( 24 ) : + win7.addstr( 5 + i + int( i / 6 ) , 2 , str(i) ) + win7.addstr( 5 + i + int( i / 6 ) , 22 , str(i + 24 ) ) + win7.refresh() + win6 = stdscr.subwin(y_max/6, - x_max -2, + int(0.7*x_max -2), 5*y_max/6, 0) win6.box() win6.addstr(1, 1, "F2: Program PLLs (source = FTSW), F3: Program PLLs (source = Osci.)", CYAN | curses.A_BOLD) win6.addstr(2, 1, "F4: Clear LOS/OOF/LOL Flag, F5 : Hard reset, F6: Soft reset", CYAN | curses.A_BOLD) - win6.addstr(3, 1, "F7: Reset trigger counter, 0/1/2/3/4/5: Resynchronize b2link 0/1/2/3/4/5", CYAN | curses.A_BOLD) + win6.addstr(3, 1, "F7: Reset trigger counter, r: Resynchronize one b2link", CYAN | curses.A_BOLD) win6.addstr(4, 1, "a: Activate all channels, d: Deactivate all channels", CYAN | curses.A_BOLD) - win6.addstr(5, 1, "z/x/c/v/b/n: (Des)activate channels 0/1/2/3/4/5", CYAN | curses.A_BOLD) + win6.addstr(5, 1, "m: (Des)activate one channel", CYAN | curses.A_BOLD) win6.refresh() @@ -444,36 +463,30 @@ def draw_menu(stdscr, dev): win1.addstr(line, 1, "..... Done") line = line + 1 win1.refresh() - elif k== ord('0'): - fpga.resynchronizeLink(0) - elif k== ord('1'): - fpga.resynchronizeLink(1) - elif k== ord('2'): - fpga.resynchronizeLink(2) - elif k== ord('3'): - fpga.resynchronizeLink(3) - elif k== ord('4'): - fpga.resynchronizeLink(4) - elif k== ord('5'): - fpga.resynchronizeLink(5) + elif k== ord('r'): + win7.addstr(37 , 2 , "Enter channel number to reset + ENTER" , curses.A_BOLD ) + win7.refresh() + curses.echo() + stdscr.nodelay (False) + the_s = stdscr.getstr( 37 , 149 ) + win7.addstr(37 , 2 , " " , curses.A_BOLD ) + curses.noecho() + fpga.resynchronizeLink( int(the_s) ) elif k== curses.KEY_F7: fpga.resetTriggerCounter() elif k==ord('a'): fpga.activateAllChannels() elif k==ord('d'): fpga.deactivateAllChannels() - elif k==ord('z'): - fpga.toggleActivateChannel( 0 ) - elif k==ord('x'): - fpga.toggleActivateChannel( 1 ) - elif k==ord('c'): - fpga.toggleActivateChannel( 2 ) - elif k==ord('v'): - fpga.toggleActivateChannel( 3 ) - elif k==ord('b'): - fpga.toggleActivateChannel( 4 ) - elif k==ord('n'): - fpga.toggleActivateChannel( 5 ) + elif k==ord('m'): + win7.addstr(38 , 2 , "Enter channel to (un)mask + ENTER" , curses.A_BOLD ) + win7.refresh() + curses.echo() + stdscr.nodelay (False) + the_s = stdscr.getstr( 38 , 145 ) + win7.addstr(38 , 2 , " " , curses.A_BOLD ) + curses.noecho() + fpga.toggleActivateChannel( int(the_s) ) # cursor_x = max (0, cursor_x) # cursor_x = min (width - 1, cursor_x) @@ -486,7 +499,7 @@ def draw_menu(stdscr, dev): pll_window(pll_si5345_2, devices[2], fpga, freq[2], conf[2], win3) pll_window(pll_si5344 , devices[3], fpga, freq[3], conf[3], win4) fanout_window(fpga, win5) - + link_window( fpga , win7 ) # Refresh the screen #stdscr.refresh ()