/*--> Do not Modify, file created automagically <--*/ /*--> MIDAS Spectrum creation routine <--*/ #include #include #include "user_spec_defs.h" #include #include #define GroupA 514 #define GroupB 515 #define SYNC 4 #define PATTERN 2 #define ADC 1 #define DELAYMASK 0xFFF #define DELAYCONST 35 #define ERROR_LIMIT 3 static unsigned int oldtop[16],oldbot[16],count[16],synctop,syncbot,hadsync,debug, counter, time_cnt; static long long last_sync , this_time, tim_diff, first_sync; struct list { int id, related_record ; } ; static struct list ids[] = { 0 , 1 , 1024 , 0, 3 , 0 } ; struct last_time { int data, th, tl, too_long, hits ; long long abs_time; }; static struct last_time records[4] ; int init(void) { int i,j; printf("Init Started \n"); counter = 0; debug = 2 ; hadsync = 0; time_cnt = 3; /* the number of items in the time array */ return 0; } int sortin(void) { int a,b,c,i,spectrum,tim,timerr,idx,pgidx ; char instrg[80]; // printf("Hello world \n"); // printf("Data received %d ",gid[0]); // gets(instrg); /*--> ====================================================== <--*/ /*--> all data is unsigned int <--*/ /*--> For GREAT format data we always have mult == 1 <--*/ /*--> if gid [ 0 ] == -1 .... OTHER wrd, data format is <--*/ /*--> gdata[ 0 ] = module number <--*/ /*--> [ 1 ] = Information code <--*/ /*--> [ 2 ] = Info field (High bits) <--*/ /*--> [ 3 ] = Info field (Low bits) <--*/ /*--> [ 4 ] = Time Stamp (High bits) <--*/ /*--> [ 5 ] = Time Stamp (Low bits) <--*/ /*--> <--*/ /*--> if gid [ 0 ] >= 0 .... ADC Data, data fmt is <--*/ /*--> gdata[ 0 ] = Fail bit value <--*/ /*--> [ 1 ] = Veto bit value <--*/ /*--> [ 2 ] = Data <--*/ /*--> [ 3 ] = Time Stamp (High bits) <--*/ /*--> [ 4 ] = Time Stamp (Low bits) <--*/ /*--> <--*/ /*--> ====================================================== <--*/ /* the global user defined variables are int guseri[n] n= 0=>3 */ /* the global user defined variables are uint guserui[n] n= 0=>3 */ /* the global user defined variables are short gusers[n] n= 0=>3 */ /* the global user defined variables are float guserf[n] n= 0=>3 */ /* so what are we doing ..... well say guser0 is a flag */ /* guser1 is time MSB */ /* guser2 is time LSB */ /* guser3 is ADC data */ if ( hadsync == 1 ) { first_sync = last_sync ; hadsync = 2; } if ( gid[ 0 ] >= 0 && gid[ 0 ] < 4000 ) { counter++; tim = gdata[4] + ( gdata[3] << 16 ) ; if ( counter == 1 ) { printf("\nFirst Data found at time %04X%08X \n",synctop, tim); printf(" Version 3\n"); printf("User flag is set to %d \n",guseri[0]); } inc1d( 1, gid[ 0 ] ); if ( hadsync == 2 ) { /* time is now valid so start looking */ for ( a=0; a< time_cnt ;a++ ) { /*run through the list checking for item we have*/ if ( gid[0] == ids[a].id ) { /* found the record we want */ records[a].data = gdata[2] ; records[a].th = gdata[3] ; records[a].tl = gdata[4] ; this_time = (long long ) synctop ; if( debug == 1 ) printf("This time top : %llX \n",this_time ); this_time = ( this_time << 28 ) + ( long long )( tim ); /* calculate the full time value */ if ( debug == 1 ) { /* print debug info */ printf("id:%d\n",ids[a].id); printf("this time %lld \n",this_time); printf("last time %lld \n",records[a].abs_time); printf("synctop %08X\n", synctop ) ; printf("Tim = %08X ", tim ); gets(instrg); if(instrg[0] == 'q' ) debug = 0 ; } tim_diff = this_time - records[a].abs_time ; if ( tim_diff > 0xffff ) { records[a].too_long++; } else { spectrum = a + 2 ; b = ( int )tim_diff; inc1d( spectrum, b); spectrum = a + 10; /* rebin spectra */ b = b / 5; inc1d( spectrum, b ); } records[a].abs_time = this_time ; records[a].hits++; } } } } else if ( gid[ 0 ] == -1 ) { inc1d( ID, 5000 + gdata[1]); if ( gdata[1] == SYNC ){ synctop = gdata[3] + ( gdata[2] << 16 ); syncbot = gdata[5] + ( gdata[4] << 16 ); last_sync = ( long long ) synctop ; last_sync = ( last_sync << 28 ) + ( long long ) ( syncbot ); if (hadsync == 0){ printf("\nSync : %04X%08X full sync = %lld\n",synctop,syncbot, last_sync); hadsync++; } } } else { inc1d( 1, 6000 ); } return 0; } int finish(void) { /* compare the counts across the group */ /* val1d ( int int < spectrum channel > ) */ /* returns an int of the number of counts in the channel */ int a,i,total[16],diff[16]; for ( a=0; a< time_cnt ;a++ ) { printf("ID: %d | Total Hits : %d | Too Long %d \n", ids[a].id, records[a].hits, records[a].too_long ); } printf("Total events : %d \n", counter ); printf("First_sync : %lld , Last sync : %lld \n",first_sync , last_sync ); tim_diff = last_sync - first_sync ; tim_diff = tim_diff / 100000000 ; printf(" Timestamps indicate elapsed time is %lld seconds \n", tim_diff ); return 0; }