/* * File Name : convert.c * Creation Date : Nov 14 2000 * Author : Y.Nakaune */ #include #include #include #include #include #include "convert.h" int convt_level = 0; /*---conversion level---*/ /*---------------------------------------------- usage -----------------------------------------------*/ static void usage ( char *cmd ) { fprintf( stderr, "\n" ); fprintf( stderr, "Usage : %s [-l level] [-s number] [-e number] [-o header] \n", cmd ); fprintf( stderr, " l : set conversion level. \n" ); fprintf( stderr, " 0 : all convert. ( default )\n" ); fprintf( stderr, " 1 : only first hit convert. \n" ); fprintf( stderr, " s : set start_number to convert. \n" ); fprintf( stderr, " ( default = 1 )\n" ); fprintf( stderr, " e : set end_number to convert. \n" ); fprintf( stderr, " ( default = last )\n" ); fprintf( stderr, " o : set output_file. [header].dat\n" ); fprintf( stderr, " ( default = .dat )\n" ); fprintf( stderr, " : set the raw_data_file to read.\n" ); fprintf( stderr, " raw_data_file must be \"xxxx.raw\".\n" ); fprintf( stderr, "\n" ); exit( 1 ); } /*---------------------------------------------- exit_err -----------------------------------------------*/ void exit_err( char *mess ) { fprintf( stderr, "test: %s: %s\n", mess, ( errno > 0 && errno < sys_nerr ? sys_errlist[errno] : "" ) ); exit(1); } /*--------------------------------------------- read_flag Read Flag from fp ----------------------------------------------*/ unsigned int read_flag( FILE *fp ) { unsigned int flag; if ( fread( &flag, 1, sizeof(int), fp ) != sizeof( int ) ) { exit_err( "Read flag" ); } return( flag ); } /*--------------------------------------------- flag_judge read and judge Flag from fp ----------------------------------------------*/ void flag_judge( FILE *fp, unsigned int flag ) { char str[20]; if ( read_flag( fp ) != flag ) { if ( flag == START_FLG ) strcpy( str, "START" ); else if ( flag == RUNbegin_FLG ) strcpy( str, "RUNbegin" ); else if ( flag == TIME_FLG ) strcpy( str, "TIME" ); else if ( flag == TIME_END_FLG ) strcpy( str, "TIME_END" ); else if ( flag == FILE_FLG ) strcpy( str, "CFG_FILE" ); else if ( flag == FILE_END_FLG ) strcpy( str, "CFG_FILE_END" ); else if ( flag == TGC_FLG ) strcpy( str, "TGC_CFG" ); else if ( flag == TGC_END_FLG ) strcpy( str, "TGC_CFG_END" ); else if ( flag == CMT_FLG ) strcpy( str, "CMT" ); else if ( flag == CMT_END_FLG ) strcpy( str, "CMT_END" ); else if ( flag == RUNbegin_END_FLG ) strcpy( str, "RUNbegin_END" ); else if ( flag == TMC_FLG ) strcpy( str, "TMC" ); else if ( flag == TMC_END_FLG ) strcpy( str, "TMC_END" ); else if ( flag == ADC_FLG ) strcpy( str, "ADC" ); else if ( flag == ADC_END_FLG ) strcpy( str, "ADC_END" ); else if ( flag == SCALER_FLG ) strcpy( str, "SCALER" ); else if ( flag == SCALER_END_FLG ) strcpy( str, "SCALER_END" ); else if ( flag == EVT_END_FLG ) strcpy( str, "EVT_END" ); else if ( flag == RUNend_END_FLG ) strcpy( str, "RUNend_END" ); else if ( flag == END_FLG ) strcpy( str, "END" ); fprintf( stderr, "\"%s\" wrong flag\n", str); exit(1); } } /*----------------------------------------------------------- readCFG Read CFG from STDIN ----------------------------------------------------------*/ int readCFG( Cfg_adc *adc, Cfg_tmc *tmc, FILE *fp ) { int cfg_data_size; if ( read_flag( fp ) != CFG_FLG ) { perror( "CFG wrong flag" ); return -1; } if ( fread( &cfg_data_size, 1, sizeof(int), fp ) != sizeof(int) ) { perror( "readCFG : cfg_data_size" ); return -1; } if ( fread( adc, 1, sizeof(Cfg_adc), fp ) != sizeof(Cfg_adc) ) { perror( "readCFG : ADC" ); return -1; } if ( fread( tmc, 1, sizeof(Cfg_tmc), fp ) != sizeof(Cfg_tmc) ) { perror( "readCFG : TMC" ); return -1; } if ( read_flag( fp ) != CFG_END_FLG ) { perror( "CFG_END wrong flag" ); return -1; } return 0; } /*----------------------------------------------- readADC Read ADC data from STDIN -----------------------------------------------*/ void readADC( FILE *daq_fp ) { int i, data_size; Evt_adc *evt; if ( fread( &data_size, 1, sizeof( int ), daq_fp ) != sizeof( int ) ) { exit_err( "readADC: data_size error" ); } if ( ( evt = (Evt_adc *)malloc( (size_t)data_size ) ) == NULL ) { exit_err( "read_adc: malloc" ); } if( fread( evt, 1, (size_t)data_size, daq_fp ) != (size_t)data_size ) { exit_err( "read_adc: adc data2" ); } adc_event_number = 0; for( i = 0; i < ( data_size / sizeof( Evt_adc ) ); i++ ) { adc_event[i].mod_number = evt[i].header >> 4; adc_event[i].ch_number = evt[i].header & 0x00f; adc_event[i].data = evt[i].content & 0xfff; adc_event_number += 1; if (adc_event_number >= ADC_EVENT_SIZE) { fprintf(stderr," ADC DATA exceeds size limit \n"); break; } } } /*----------------------------------------------- readTMC Read TMC data from STDIN -----------------------------------------------*/ void readTMC( FILE *daq_fp ) { int a, b, i, data_size; static int mod, ch; static double time; static double tmc_time[TMC_MOD][TMC_CH]; event *evt; /*---initialize tmc_time---*/ for ( a = 0; a < TMC_MOD; a++ ) { for ( b = 0; b < TMC_CH; b++ ) { tmc_time[a][b] = -1; number_of_same_tmc[a][b] = 0; } } if ( fread( &data_size, 1, sizeof( int ), daq_fp ) != sizeof( int ) ) { exit_err( "readTMC: data_size error" ); } if ( ( evt = (event *)malloc( (size_t)data_size ) ) == NULL ) { exit_err( "read_tmc: malloc" ); } if( fread( evt, 1, (size_t)data_size, daq_fp ) != (size_t)data_size ) { exit_err( "read_tmc: tmc data2" ); } tmc_event_number = 0; if ( convt_level == 0 ) { for( i = 0; i < ( data_size / sizeof( event ) ); i++ ) { tmc_event[i].mod_number = evt[i].header >> 5; tmc_event[i].ch_number = evt[i].header & 0x001f; tmc_event[i].time = evt[i].content & 0xfff; tmc_event_number += 1; number_of_same_tmc[tmc_event[i].mod_number][tmc_event[i].ch_number] = 1; if ( tmc_event_number >= TMC_EVENT_SIZE ) { fprintf( stderr, "TMC DATA exceeds size limit \n" ); break; } } } else if ( convt_level == 1 ) { #if 1 /*---Filter to Get Only First Hit---*/ for( i = 0; i < ( data_size / sizeof( event ) ); i++ ) { mod = evt[i].header >> 5; ch = evt[i].header & 0x001f; time = evt[i].content & 0xfff; number_of_same_tmc[mod][ch]++; if ( tmc_time[mod][ch] < time ){ tmc_time[mod][ch] = time; } } tmc_event_number = 0; for ( mod = 0; mod < TMC_MOD; mod++ ) { for ( ch = 0; ch < TMC_CH; ch++ ) { if ( tmc_time[mod][ch] != -1 ) { tmc_event[tmc_event_number].mod_number = mod; tmc_event[tmc_event_number].ch_number = ch; tmc_event[tmc_event_number].time = tmc_time[mod][ch]; tmc_event_number += 1; } } } } #endif } /*--------------------------------------------------- tTOd time to distance ---------------------------------------------------*/ double tTOd( int tmc_time ) { double time, distance; time = T0 - TMC_FACT * tmc_time ; distance = DRIFT_VEROCITY * time; return( distance ); } /*======================================================== main MAIN =======================================================*/ int main( int argc, char **argv ) { int RUNbegin_number, RUNend_number; /* Run Number */ int tmstart_size, tmend_size; /* array size of time */ int file_size, cmt_size; /* array size of cfg_file & comment */ int evt_number; /* number of read out events */ int tmc_sci_event_number = 0, /* tmc_sci number of read out events */ tmc_dc_event_number = 0, /* tmc_dc number of read out events */ tmc_tgc_event_number = 0, /* tmc_tgc number of read out events */ adc_sci_event_number = 0, /* adc_sci number of read out events */ adc_tgc_event_number = 0; /* adc_tgc number of read out events */ int start_number = -1, /* start number of read out event */ end_number = -1; /* end number of read out event */ int i, len, mod, ch; int opt, file_flag = 0; static char output_file[128]; /* DST file name */ static char start_date[256], end_date[256]; /* RUN start/end time */ static char cfg_file[128], comment[4056]; /* cfg file name & comment */ FILE *fp_in = NULL, *fp_out = NULL, *fp_cfg = NULL; Cfg_adc cfg_adc; Cfg_tmc cfg_tmc; TGC_Cfg tgc_cfg; char buf_con[16]; int scaler_data[N_SCALER_CH]; /* scaler's data */ int flag; /*----------Options----------*/ while ( ( opt = getopt( argc, argv, "l:s:e:o:h" ) ) != -1 ) { switch ( opt ) { case 'l': convt_level = atoi( optarg ); break; case 's': start_number = atoi( optarg ); break; case 'e': end_number = atoi( optarg ); break; case 'o': strcpy( output_file, optarg ); file_flag = 1; break; default: usage( argv[0] ); } } if ( optind < argc ) { if ( ( fp_in = fopen( argv[optind], "r" ) ) == NULL ) { exit_err( argv[optind] ); } } else usage( argv[0] ); if ( start_number > end_number ) { usage( argv[0] ); } if ( convt_level > 1 ) { usage( argv[0] ); } /*----------output_file open----------*/ if ( file_flag != 1 ) { len = strlen( argv[optind] ) - 4; strncpy( output_file, argv[optind], len ); } strcat( output_file, DOT_DAT ); if ( ( fp_out = fopen( output_file, "r" ) ) != NULL ) { printf( "Data file %s exists. \n", output_file ); printf( "Do you want to overwrite ? (y/n) : " ); fgets( buf_con, sizeof( buf_con ), stdin ); if ( buf_con[0] != 'y' ) { printf( "\n" ); exit(0); } } if ( ( fp_out = fopen( output_file, "w" ) ) == NULL ) { exit_err( output_file ); } flag_judge( fp_in, START_FLG ); /*----------Read CFG data from STDIN----------*/ readCFG( &cfg_adc, &cfg_tmc, fp_in ); /*---get run number---*/ flag_judge( fp_in, RUNbegin_FLG ); if ( fread( &RUNbegin_number, 1, sizeof(int), fp_in ) != sizeof(int) ) { perror( "\"RUNbegin\" : RUNbegin_number's size error" ); return( -1 ); } /*---get time---*/ flag_judge( fp_in, TIME_FLG ); if ( fread( &tmstart_size, 1, sizeof(int), fp_in ) != sizeof(int) ) { perror( "\"TIME\" : start_time's string size error" ); return( -1 ); } if ( fread( &start_date, 1, sizeof(char) * tmstart_size, fp_in ) != sizeof(char) * tmstart_size ) { perror( "\"TIME\" : start_time's size error" ); return( -1 ); } flag_judge( fp_in, TIME_END_FLG ); /*---get cfg_file_name---*/ flag_judge( fp_in, FILE_FLG ); if ( fread( &file_size, 1, sizeof(int), fp_in ) != sizeof(int) ) { perror( "\"FILE\" : cfg_file's string size error" ); return( -1 ); } if ( fread( &cfg_file, 1, sizeof(char) * file_size, fp_in ) != sizeof(char) * file_size ) { perror( "\"FILE\" : cfg_file's size error" ); return( -1 ); } flag_judge( fp_in, FILE_END_FLG ); /*----------DAQ.cfg open----------*/ if ( ( fp_cfg = fopen( cfg_file, "r" ) ) == NULL ) { exit_err( cfg_file ); } /*----------Read DAQ.cfg----------*/ read_daq_cfg( fp_cfg ); /*---get TGC.cfg---*/ flag_judge( fp_in, TGC_FLG ); if ( fread( &file_size, 1, sizeof(int), fp_in ) != sizeof(int) ) { perror( "\"TGC_CFG\" : tgc.cfg structure size error" ); return( -1 ); } if ( fread( &tgc_cfg, 1, sizeof(TGC_Cfg), fp_in ) != sizeof(TGC_Cfg) ) { perror( "\"TGC_CFG\" : cfg_file's size error" ); return( -1 ); } flag_judge( fp_in, TGC_END_FLG ); /*---get comment---*/ flag_judge( fp_in, CMT_FLG ); if ( fread( &cmt_size, 1, sizeof(int), fp_in ) != sizeof(int) ) { perror( "\"FILE\" : comment's string size error" ); return( -1 ); } if ( fread( &comment, 1, sizeof(char) * cmt_size, fp_in ) != sizeof(char) * cmt_size ) { perror( "\"FILE\" : comment's size error" ); return( -1 ); } flag_judge( fp_in, CMT_END_FLG ); flag_judge( fp_in, RUNbegin_END_FLG ); /*---output header printing to data file---*/ fprintf( fp_out, "#\n" ); fprintf( fp_out, "# Run Number : %d\n", RUNbegin_number ); fprintf( fp_out, "# Run Start time : %s\n", start_date ); fprintf( fp_out, "# Config. File : %s\n", cfg_file ); fprintf( fp_out, "# D1 (x y z) : %d %d %d \n", tgc_cfg.doublet_x, tgc_cfg.doublet_y, tgc_cfg.doublet_z ); fprintf( fp_out, "# T3 (x y z) : %d %d %d \n", tgc_cfg.triplet_x, tgc_cfg.triplet_y, tgc_cfg.triplet_z ); fprintf( fp_out, "# HV (kV) : %2.1f %2.1f %2.1f %2.1f %2.1f\n", tgc_cfg.HV[0], tgc_cfg.HV[1], tgc_cfg.HV[2], tgc_cfg.HV[3], tgc_cfg.HV[4] ); fprintf( fp_out, "# Pressure (mmAq): %d %d %d %d %d\n", tgc_cfg.Pressure[0], tgc_cfg.Pressure[1], tgc_cfg.Pressure[2], tgc_cfg.Pressure[3], tgc_cfg.Pressure[4] ); fprintf( fp_out, "# convt level : %d\n", convt_level ); fprintf( fp_out, "# Comment : %s\n", comment ); fprintf( fp_out, "#\n" ); /*==========Read data==========*/ while ( read_flag( fp_in ) == EVT_FLG ) { if( fread( &evt_number, 1, sizeof(int), fp_in ) != sizeof(int) ) { exit_err( "EVT : evt_number's size error" ); } if ( evt_number >= start_number ) { fprintf( fp_out, "event %6d\n", evt_number ); } #ifdef ADC_ON flag = read_flag(fp_in); if (flag == ADC_FLG) { /*----------Read ADC----------*/ readADC( fp_in ); flag_judge( fp_in, ADC_END_FLG ); /*----------Read TMC----------*/ flag_judge( fp_in, TMC_FLG ); readTMC( fp_in ); flag_judge( fp_in, TMC_END_FLG ); } else if (flag == TMC_FLG) { /*----------Read TMC----------*/ readTMC( fp_in ); flag_judge( fp_in, TMC_END_FLG ); /*----------Read ADC----------*/ flag_judge( fp_in, ADC_FLG ); readADC( fp_in ); flag_judge( fp_in, ADC_END_FLG ); } else { fprintf( stderr, " wrong flag %x \n",flag); exit(1); } #else /*----------Read TMC----------*/ flag_judge( fp_in, TMC_FLG ); readTMC( fp_in ); flag_judge( fp_in, TMC_END_FLG ); /*----------convert----------*/ #endif for ( i = 0; i < tmc_event_number; i++ ) { mod = tmc_event[i].mod_number; ch = tmc_event[i].ch_number; if ( tmc_sci_cfg[mod][ch].ctnb != -1 ) { tmc_sci_event_number++; } else if ( tmc_dc_cfg[mod][ch].chmbnb != -1 ) { tmc_dc_event_number++; } else if ( tmc_tgc_cfg[mod][ch].lay != -1 ) { tmc_tgc_event_number++; } } for ( i = 0; i < adc_event_number; i++ ) { mod = adc_event[i].mod_number; ch = adc_event[i].ch_number; if ( adc_sci_cfg[mod][ch].ctnb != -1 ) { adc_sci_event_number++; } else if ( adc_tgc_cfg[mod][ch].lay != -1 ) { adc_tgc_event_number++; } } if ( evt_number >= start_number ) { fprintf( fp_out, "adc_sci_data %3d\n", adc_sci_event_number ); for ( i = 0; i < adc_event_number; i++ ) { mod = adc_event[i].mod_number; ch = adc_event[i].ch_number; if ( adc_sci_cfg[mod][ch].ctnb != -1 ) { fprintf( fp_out, "%2d %4d\n", adc_sci_cfg[mod][ch].ctnb, adc_event[i].data ); } } fprintf( fp_out, "tmc_sci_data %3d\n", tmc_sci_event_number ); for ( i = 0; i < tmc_event_number; i++ ) { mod = tmc_event[i].mod_number; ch = tmc_event[i].ch_number; if ( tmc_sci_cfg[mod][ch].ctnb != -1 ) { fprintf( fp_out, "%2d %10.3f %4d\n", tmc_sci_cfg[mod][ch].ctnb, ( (float)tmc_event[i].time * TMC_FACT ), number_of_same_tmc[mod][ch] ); } } fprintf( fp_out, "tmc_dc_data %3d\n", tmc_dc_event_number ); for ( i = 0; i < tmc_event_number; i++ ) { mod = tmc_event[i].mod_number; ch = tmc_event[i].ch_number; if ( tmc_dc_cfg[mod][ch].chmbnb != -1 ) { fprintf( fp_out, "%2d %4d %10f %10.3f %4d\n", tmc_dc_cfg[mod][ch].chmbnb, tmc_dc_cfg[mod][ch].xy, tTOd( tmc_event[i].time ), ( (float)tmc_event[i].time * TMC_FACT ), number_of_same_tmc[mod][ch] ); } } fprintf( fp_out, "adc_tgc_data %3d\n", adc_tgc_event_number ); for ( i = 0; i < adc_event_number; i++ ) { mod = adc_event[i].mod_number; ch = adc_event[i].ch_number; if ( adc_tgc_cfg[mod][ch].lay != -1 ) { fprintf( fp_out, "%2d %4d %4d %4d\n", adc_tgc_cfg[mod][ch].lay, adc_tgc_cfg[mod][ch].ws, adc_tgc_cfg[mod][ch].tgch, adc_event[i].data ); } } fprintf( fp_out, "tmc_tgc_data %3d\n", tmc_tgc_event_number ); for ( i = 0; i < tmc_event_number; i++ ) { mod = tmc_event[i].mod_number; ch = tmc_event[i].ch_number; if ( tmc_tgc_cfg[mod][ch].lay != -1 ) { fprintf( fp_out, "%2d %4d %4d %10.3f %4d\n", tmc_tgc_cfg[mod][ch].lay, tmc_tgc_cfg[mod][ch].ws, tmc_tgc_cfg[mod][ch].tgch, ( (float)tmc_event[i].time * TMC_FACT ), number_of_same_tmc[mod][ch] ); } } } flag_judge( fp_in, EVT_END_FLG ); if ( evt_number >= start_number ) { fprintf( fp_out, "end %7d\n", evt_number ); } /*----------initialize----------*/ tmc_sci_event_number = 0; tmc_dc_event_number = 0; tmc_tgc_event_number = 0; adc_sci_event_number = 0; adc_tgc_event_number = 0; if ( end_number != -1 ) { if ( evt_number >= end_number ) break; } } /*==========loop end==========*/ if ( end_number == -1 ) { #ifdef ADC_ON /*----------Read Scaler----------*/ for ( i = 0; i < N_SCALER_CH; i++ ) { /* printf( "scaler_data[%d] = %d\n", i, scaler_data[i] ); */ if ( fread( &scaler_data[i], 1, sizeof(int), fp_in ) != sizeof(int) ) { perror( "\"SCALER\" : scaler_data[i]'s size error" ); return( -1 ); } } flag_judge( fp_in, SCALER_END_FLG ); #endif /*---get run number---*/ flag_judge( fp_in, RUNend_FLG ); if ( fread( &RUNend_number, 1, sizeof(int), fp_in ) != sizeof(int) ) { perror( "\"RUNend\" : RUNend_number's size wrong" ); } else if ( RUNend_number != RUNbegin_number ) { perror( "\"RUNend\" : RUNend_number != RUNbegin_number" ); } /*---get run_end_time---*/ flag_judge( fp_in, TIME_FLG ); if ( fread( &tmend_size, 1, sizeof(int), fp_in ) != sizeof(int) ) { perror( "\"TIME\" : end_time's string size error" ); return( -1 ); } if ( fread( &end_date, 1, sizeof(char) * tmend_size, fp_in ) != sizeof(char) * tmend_size ) { perror( "\"TIME\" : end_time's size error" ); return( -1 ); } flag_judge( fp_in, TIME_END_FLG ); flag_judge( fp_in, RUNend_END_FLG ); flag_judge( fp_in, END_FLG ); fprintf( fp_out, "#\n" ); fprintf( fp_out, "# Run Number : %d\n", RUNend_number ); fprintf( fp_out, "# Run End time : %s\n", end_date ); fprintf( fp_out, "#\n" ); } fclose( fp_in ); fclose( fp_out ); fclose( fp_cfg ); return 0; } /* End of File */