#include "VMEUtilities.h" extern "C"{ #include #include #include #include #include #include #include #include #include "math.h" #include "time.h" #include "sys/types.h" #include extern int nfa125; extern int fa125A32Base; extern int tiA32Base; DMA_MEM_ID vmeIN,vmeOUT; extern unsigned int *dma_dabufp; extern DMANODE *the_event; } extern int VERBOSE; #define FA125_ADDR (5<<19) #define TI_ADDR (21<<19) int Thresholds[20][72]; int DAC_Values[20][72]; char DETECTOR[128]; int HOSTROC; unsigned int NAboveThreshold[20][72]; map > > fRawData; void MyReadoutCode(); void DecodeData(unsigned int); void DecodeDataNEW(unsigned int); extern unsigned long int EventCounter; struct data_struct { unsigned int new_type; unsigned int type; unsigned int slot_id_hd; unsigned int mod_id_hd; unsigned int slot_id_tr; unsigned int n_evts; unsigned int blk_num; unsigned int n_words; unsigned int evt_num_1; unsigned int time_now; unsigned int time_1; unsigned int time_2; unsigned int chan; unsigned int width; unsigned int valid_1; unsigned int adc_1; unsigned int valid_2; unsigned int adc_2; unsigned int over; unsigned int adc_sum; unsigned int pulse_num; unsigned int thres_bin; unsigned int quality; unsigned int integral; unsigned int time; unsigned int chan_a; unsigned int source_a; unsigned int chan_b; unsigned int source_b; unsigned int group; unsigned int time_coarse; unsigned int time_fine; unsigned int vmin; unsigned int vpeak; unsigned int scaler[18];/* data stream scalers */ }; struct data_structNEW { unsigned int new_type; unsigned int type; unsigned int slot_id_hd; unsigned int mod_id_hd; unsigned int slot_id_tr; unsigned int n_evts; unsigned int blk_num; unsigned int n_words; unsigned int evt_num_1; unsigned int time_now; unsigned int time_1; unsigned int time_2; unsigned int chan; unsigned int width; unsigned int npk; unsigned int le_time; unsigned int time_quality; unsigned int overflow_cnt; unsigned int pedestal; unsigned int fm_amplitude; unsigned int peak_amplitude; unsigned int peak_time; unsigned int valid_1; unsigned int adc_1; unsigned int valid_2; unsigned int adc_2; unsigned int over; unsigned int adc_sum; unsigned int pulse_num; unsigned int thres_bin; unsigned int quality; unsigned int integral; unsigned int time; unsigned int chan_a; unsigned int source_a; unsigned int chan_b; unsigned int source_b; unsigned int group; unsigned int time_coarse; unsigned int time_fine; unsigned int vmin; unsigned int vpeak; unsigned int scaler[18];/* data stream scalers */ }; volatile struct data_structNEW fadc_data; VMEUtilities::VMEUtilities(){ GetHostName(); NADCSAMPLES = 100; EventCounter = 0; fPulserOffset = 0; fPulseHeight = 1000000; // Set correct number of ADCs if (DETECTOR[0] == 'c'){ //sprintf(DET,"CDC"); if (HOSTROC == 1 || HOSTROC == 4) NADCS = 13; else NADCS = 12; } else { //sprintf(DET,"FDC"); if (HOSTROC == 2 || HOSTROC == 3 || HOSTROC == 5 || HOSTROC == 8 || HOSTROC == 11 || HOSTROC == 12) NADCS = 14; else NADCS = 15; } if (VERBOSE) cout << "VMEUtilities object created" << endl; } int VMEUtilities::InitializeADCs(){ if(vmeOpenDefaultWindows() != OK) { cout<<"vmeOpenDefaultWindows failed. Terminate right here."<length; int SLOTNUM = 0; int EVENT = -1; int CHANNEL = -1; int WSize = 0; int idx = 0; //int pedcnt = 0; int adc1, adc2; int DATAReady=0; int EventsInBlock=0; int TriggerTime_1=0; int TriggerTime_2=0; //int TrueSize=0; fRawData.clear(); int Indx = 0; cout<<"EVENT LENGTH: "<data[Indx]); DecodeDataNEW(data); } // end of loop over all data cout<<"EVENT LENGTH: "<> 27; } else/* data type continuation word */ { fadc_data.new_type = 0; fadc_data.type = type_last; } switch( fadc_data.type ) { case 0:/* BLOCK HEADER */ fadc_data.slot_id_hd = (data & 0x7C00000) >> 22; fadc_data.mod_id_hd = (data & 0x3C0000) >> 18; fadc_data.n_evts = (data & 0x000FF); fadc_data.blk_num = (data & 0x3FF00) >> 8; if( i_print ) printf("%8X - BLOCK HEADER - slot = %d id = %d n_evts = %d n_blk = %d\n", data, fadc_data.slot_id_hd, fadc_data.mod_id_hd, fadc_data.n_evts, fadc_data.blk_num); break; case 1:/* BLOCK TRAILER */ fadc_data.slot_id_tr = (data & 0x7C00000) >> 22; fadc_data.n_words = (data & 0x3FFFFF); if( i_print ) printf("%8X - BLOCK TRAILER - slot = %d n_words = %d\n", data, fadc_data.slot_id_tr, fadc_data.n_words); break; case 2:/* EVENT HEADER */ if( fadc_data.new_type ) { slot_id_ev_hd = (data & 0x7C00000) >> 22; fadc_data.evt_num_1 = (data & 0x03FFFFF); if( i_print ) printf("%8X - EVENT HEADER - slot = %d evt_num = %d\n", data, slot_id_ev_hd, fadc_data.evt_num_1); } break; case 3:/* TRIGGER TIME */ if( fadc_data.new_type ) { fadc_data.time_1 = (data & 0xFFFFFF); if( i_print ) printf("%8X - TRIGGER TIME 1 - time = 0x%08x\n", data, fadc_data.time_1); fadc_data.time_now = 1; time_last = 1; } else { if( time_last == 1 ) { fadc_data.time_2 = (data & 0xFFFFFF); if( i_print ) printf("%8X - TRIGGER TIME 2 - time = 0x%08x\n", data, fadc_data.time_2); fadc_data.time_now = 2; } else if( i_print ) printf("%8X - TRIGGER TIME - (ERROR)\n", data); time_last = fadc_data.time_now; } break; case 4:/* WINDOW RAW DATA */ if( fadc_data.new_type ) { fadc_data.chan = (data & 0x7F00000) >> 20; fadc_data.width = (data & 0xFFF); //fRawData[slot_id_ev_hd][fadc_data.chan].clear(); if( i_print ) printf("%8X - WINDOW RAW DATA - chan = %2d width = %d\n", data, fadc_data.chan, fadc_data.width); nsamples=0; } else { fadc_data.valid_1 = 1; fadc_data.valid_2 = 1; fadc_data.adc_1 = (data & 0x1FFF0000) >> 16; if( data & 0x20000000 ) fadc_data.valid_1 = 0; fadc_data.adc_2 = (data & 0x1FFF); if( data & 0x2000 ) fadc_data.valid_2 = 0; if( i_print ) printf("%8X - RAW SAMPLES (%3d) - valid = %d adc = %4d (%03X) valid = %d adc = %4d (%03X)\n", data, nsamples,fadc_data.valid_1, fadc_data.adc_1, fadc_data.adc_1, fadc_data.valid_2, fadc_data.adc_2, fadc_data.adc_2); fRawData[slot_id_ev_hd][fadc_data.chan].push_back(fadc_data.adc_1); fRawData[slot_id_ev_hd][fadc_data.chan].push_back(fadc_data.adc_2); nsamples += 2; } break; case 5:/* WINDOW SUM */ fadc_data.over = 0; fadc_data.chan = (data & 0x7800000) >> 23; fadc_data.adc_sum = (data & 0x3FFFFF); if( data & 0x400000 ) fadc_data.over = 1; if( i_print ) printf("%8X - WINDOW SUM - chan = %d over = %d adc_sum = 0x%08x\n", data, fadc_data.chan, fadc_data.over, fadc_data.adc_sum); break; case 6:/* PULSE RAW DATA */ if( fadc_data.new_type ) { fadc_data.chan = (data & 0x07F00000) >> 20; fadc_data.pulse_num = 0; /* fadc_data.chan = (data & 0x7800000) >> 23; */ /* fadc_data.pulse_num = (data & 0x600000) >> 21; */ fadc_data.thres_bin = (data & 0x3FF); if( i_print ) printf("%8X - PULSE RAW DATA - chan = %d pulse # = %d threshold bin = %d\n", data, fadc_data.chan, fadc_data.pulse_num, fadc_data.thres_bin); } else { fadc_data.valid_1 = 1; fadc_data.valid_2 = 1; fadc_data.adc_1 = (data & 0x1FFF0000) >> 16; if( data & 0x20000000 ) fadc_data.valid_1 = 0; fadc_data.adc_2 = (data & 0x1FFF); if( data & 0x2000 ) fadc_data.valid_2 = 0; if( i_print ) printf("%8X - PULSE RAW SAMPLES - valid = %d adc = %d valid = %d adc = %d\n", data, fadc_data.valid_1, fadc_data.adc_1, fadc_data.valid_2, fadc_data.adc_2); } break; case 7:/* PULSE INTEGRAL */ fadc_data.chan = (data & 0x07F00000) >> 20; fadc_data.pulse_num = 0; fadc_data.quality = (data & 0x180000) >> 19; fadc_data.integral = (data & 0x7FFFF); if( i_print ) printf("%8X - PULSE INTEGRAL - chan = %d pulse # = %d quality = %d integral = %d\n", data, fadc_data.chan, fadc_data.pulse_num, fadc_data.quality, fadc_data.integral); break; case 8:/* PULSE TIME */ fadc_data.chan = (data & 0x07F00000) >> 20; fadc_data.pulse_num = 0; /* fadc_data.chan = (data & 0x7800000) >> 23; */ /* fadc_data.pulse_num = (data & 0x600000) >> 21; */ fadc_data.quality = (data & 0x180000) >> 19; fadc_data.time = (data & 0xFFFF); fadc_data.time_coarse = (data & 0xFFC0) >> 6; fadc_data.time_fine = (data & 0x3F); if( i_print ) printf("%8X - PULSE TIME - chan = %d pulse # = %d qual = %d t = %d (c = %d f = %d)\n", data, fadc_data.chan, fadc_data.pulse_num, fadc_data.quality, fadc_data.time, fadc_data.time_coarse, fadc_data.time_fine); break; case 9:/* STREAMING RAW DATA */ if( fadc_data.new_type ) { fadc_data.chan_a = (data & 0x3C00000) >> 22; fadc_data.source_a = (data & 0x4000000) >> 26; fadc_data.chan_b = (data & 0x1E0000) >> 17; fadc_data.source_b = (data & 0x200000) >> 21; if( i_print ) printf("%8X - STREAMING RAW DATA - ena A = %d chan A = %d ena B = %d chan B = %d\n", data, fadc_data.source_a, fadc_data.chan_a, fadc_data.source_b, fadc_data.chan_b); } else { fadc_data.valid_1 = 1; fadc_data.valid_2 = 1; fadc_data.adc_1 = (data & 0x1FFF0000) >> 16; if( data & 0x20000000 ) fadc_data.valid_1 = 0; fadc_data.adc_2 = (data & 0x1FFF); if( data & 0x2000 ) fadc_data.valid_2 = 0; fadc_data.group = (data & 0x40000000) >> 30; if( fadc_data.group ) { if( i_print ) printf("%8X - RAW SAMPLES B - valid = %d adc = %d valid = %d adc = %d\n", data, fadc_data.valid_1, fadc_data.adc_1, fadc_data.valid_2, fadc_data.adc_2); } else if( i_print ) printf("%8X - RAW SAMPLES A - valid = %d adc = %d valid = %d adc = %d\n", data, fadc_data.valid_1, fadc_data.adc_1, fadc_data.valid_2, fadc_data.adc_2); } break; case 10:/* PULSE PARAMETERS */ /* fadc_data.chan = (data & 0x7800000) >> 23; */ /* fadc_data.pulse_num = (data & 0x600000) >> 21; */ fadc_data.chan = (data & 0x07F00000) >> 20; fadc_data.pulse_num = 0; fadc_data.vmin = (data & 0x1FF000) >> 12; fadc_data.vpeak = (data & 0xFFF); if( i_print ) printf("%8X - PULSE V - chan = %d pulse # = %d vmin = %d vpeak = %d\n", data, fadc_data.chan, fadc_data.pulse_num, fadc_data.vmin, fadc_data.vpeak); break; case 11:/* UNDEFINED TYPE */ if( i_print ) printf("%8X - UNDEFINED TYPE = %d\n", data, fadc_data.type); break; case 12:/* SCALER HEADER */ num_scalers = data & 0x3F;/* number of scaler words to follow */ scaler_index = 1;/* identify next word as a scaler value */ if( i_print ) printf("%8X - SCALER HEADER = %d (NUM SCALERS = %d)\n", data, fadc_data.type, num_scalers); break; case 13:/* END OF EVENT */ if( i_print ) printf("%8X - END OF EVENT = %d\n", data, fadc_data.type); break; case 14:/* DATA NOT VALID (no data available) */ slot_id_dnv = (data & 0x7C00000) >> 22; if( i_print ) printf("%8X - DATA NOT VALID = %d slot = %d\n", data, fadc_data.type, slot_id_dnv); break; case 15:/* FILLER WORD */ slot_id_fill = (data & 0x7C00000) >> 22; if( i_print ) printf("%8X - FILLER WORD = %d slot = %d\n", data, fadc_data.type, slot_id_fill); break; } type_last = fadc_data.type; /* save type of current data word */ } } void DecodeDataNEW(unsigned int data) { /* for new data format - 10/23/13 - EJ */ static unsigned int type_last = 15;/* initialize to type FILLER WORD */ static unsigned int time_last = 0; static unsigned int scaler_index = 0; static unsigned int num_scalers = 1; static unsigned int slot_id_ev_hd = 0; static unsigned int slot_id_dnv = 0; static unsigned int slot_id_fill = 0; static int nsamples=0; static int ipk=0; /* static int goto_raw=0; */ int i_print = 0; if( scaler_index )/* scaler data word */ { fadc_data.type = 16;/* scaler data words as type 16 */ fadc_data.new_type = 0; if( scaler_index < num_scalers ) { fadc_data.scaler[scaler_index - 1] = data; if( i_print ) printf("%8X - SCALER(%d) = %d\n", data, (scaler_index - 1), data); scaler_index++; } else/* last scaler word */ { fadc_data.scaler[scaler_index - 1] = data; if( i_print ) printf("%8X - SCALER(%d) = %d\n", data, (scaler_index - 1), data); scaler_index = 0; num_scalers = 1; } } else/* non-scaler word */ { if( data & 0x80000000 )/* data type defining word */ { fadc_data.new_type = 1; fadc_data.type = (data & 0x78000000) >> 27; } else/* data type continuation word */ { fadc_data.new_type = 0; fadc_data.type = type_last; } switch( fadc_data.type ) { case 0:/* BLOCK HEADER */ fadc_data.slot_id_hd = (data & 0x7C00000) >> 22; fadc_data.mod_id_hd = (data & 0x3C0000) >> 18; fadc_data.n_evts = (data & 0x000FF); fadc_data.blk_num = (data & 0x7F00) >> 8; if( i_print ) printf("%8X - BLOCK HEADER - slot = %d id = %d n_evts = %d n_blk = %d\n", data, fadc_data.slot_id_hd, fadc_data.mod_id_hd, fadc_data.n_evts, fadc_data.blk_num); break; case 1:/* BLOCK TRAILER */ fadc_data.slot_id_tr = (data & 0x7C00000) >> 22; fadc_data.n_words = (data & 0x3FFFFF); if( i_print ) printf("%8X - BLOCK TRAILER - slot = %d n_words = %d\n", data, fadc_data.slot_id_tr, fadc_data.n_words); break; case 2:/* EVENT HEADER */ if( fadc_data.new_type ) { slot_id_ev_hd = (data & 0x7C00000) >> 22; fadc_data.evt_num_1 = (data & 0x03FFFFF); if( i_print ) printf("%8X - EVENT HEADER - slot = %d evt_num = %d\n", data, slot_id_ev_hd, fadc_data.evt_num_1); } break; case 3:/* TRIGGER TIME */ if( fadc_data.new_type ) { fadc_data.time_1 = (data & 0xFFFFFF); if( i_print ) printf("%8X - TRIGGER TIME 1 - time = 0x%08x\n", data, fadc_data.time_1); fadc_data.time_now = 1; time_last = 1; } else { if( time_last == 1 ) { fadc_data.time_2 = (data & 0xFFFFFF); if( i_print ) printf("%8X - TRIGGER TIME 2 - time = 0x%08x\n", data, fadc_data.time_2); fadc_data.time_now = 2; } else if( i_print ) printf("%8X - TRIGGER TIME - (ERROR)\n", data); time_last = fadc_data.time_now; } break; case 4:/* WINDOW RAW DATA */ if( fadc_data.new_type ) { fadc_data.chan = (data & 0x7F00000) >> 20; fadc_data.width = (data & 0xFFF); if( i_print ) printf("%8X - WINDOW RAW DATA - chan = %2d width = %d\n", data, fadc_data.chan, fadc_data.width); nsamples=0; } else { fadc_data.valid_1 = 1; fadc_data.valid_2 = 1; fadc_data.adc_1 = (data & 0x1FFF0000) >> 16; if( data & 0x20000000 ) fadc_data.valid_1 = 0; fadc_data.adc_2 = (data & 0x1FFF); if( data & 0x2000 ) fadc_data.valid_2 = 0; if( i_print ) printf("%8X - RAW SAMPLES (%3d) - valid = %d adc = %4d (%03X) valid = %d adc = %4d (%03X)\n", data, nsamples,fadc_data.valid_1, fadc_data.adc_1, fadc_data.adc_1, fadc_data.valid_2, fadc_data.adc_2, fadc_data.adc_2); fRawData[slot_id_ev_hd][fadc_data.chan].push_back(fadc_data.adc_1); fRawData[slot_id_ev_hd][fadc_data.chan].push_back(fadc_data.adc_2); nsamples += 2; } break; case 5:/* PULSE DATA, CDC */ if( fadc_data.new_type ) { fadc_data.chan = (data & 0x7F00000) >> 20; fadc_data.npk = (data & 0xF8000)>>15; fadc_data.le_time = (data & 0x7FF0)>>4; fadc_data.time_quality = (data & (1<<3))>>3; fadc_data.overflow_cnt = (data & 0x7); if( i_print ) printf("%8X - PULSE DATA (CDC IT) - chan = %2d LE time = %d Q = %d OVF = %d\n", data, fadc_data.chan, fadc_data.le_time, fadc_data.time_quality, fadc_data.overflow_cnt); } else { fadc_data.pedestal = (data & 0x7F800000)>>23; fadc_data.integral = (data & 0x007FFE00)>>9; fadc_data.fm_amplitude = (data & 0x000001FF); if( i_print ) printf("%8X - PULSE DATA (CDC IT) - ped = %d integral = %d firstmax ampl = %d\n", data, fadc_data.pedestal, fadc_data.integral, fadc_data.fm_amplitude); } break; case 6:/* PULSE DATA, FDC - Integral and Time */ if( fadc_data.new_type ) { fadc_data.chan = (data & 0x7F00000) >> 20; fadc_data.npk = (data & 0xF8000)>>15; fadc_data.le_time = (data & 0x7FF0)>>4; fadc_data.time_quality = (data & (1<<3))>>3; fadc_data.overflow_cnt = (data & 0x7); ipk = 0; if( i_print ) printf("%8X - PULSE DATA (FDC IT) - chan = %2d NPK = %d LE time = %d Q = %d OVF = %d\n", data, fadc_data.chan, fadc_data.npk, fadc_data.le_time, fadc_data.time_quality, fadc_data.overflow_cnt); } else { ipk++; fadc_data.pedestal = (data & 0x7F800000)>>23; fadc_data.integral = (data & 0x007FFE00)>>9; fadc_data.fm_amplitude = (data & 0x000001FF); if( i_print ) printf("%8X - PULSE DATA (FDC IT) %d - ped = %d integral = %d firstmax ampl = %d\n", data, ipk, fadc_data.pedestal, fadc_data.integral, fadc_data.fm_amplitude); } break; case 9:/* PULSE DATA, FDC - Peak Ampl and Time */ if( fadc_data.new_type ) { fadc_data.chan = (data & 0x7F00000) >> 20; fadc_data.le_time = (data & 0x7FF0)>>4; fadc_data.time_quality = (data & (1<<3))>>3; fadc_data.overflow_cnt = (data & 0x7); ipk = 0; if( i_print ) printf("%8X - PULSE DATA (FDC AT) - chan = %2d NPK = %d LE time = %d Q = %d OVF = %d\n", data, fadc_data.chan, fadc_data.npk, fadc_data.le_time, fadc_data.time_quality, fadc_data.overflow_cnt); } else { ipk++; fadc_data.peak_amplitude = (data & 0x7ff80000)>>19; fadc_data.peak_time = (data & 0x0007f800)>>11; fadc_data.pedestal = (data & 0x000007ff); if( i_print ) printf("%8X - PULSE DATA (FDC AT) %d - Ampl = %d Time = %d Pedestal = %d\n", data, ipk, fadc_data.peak_amplitude, fadc_data.peak_time, fadc_data.pedestal); } break; case 7: case 8: case 10: case 11: case 12:/* UNDEFINED TYPE */ if( i_print ) printf("%8X - UNDEFINED TYPE = %d\n", data, fadc_data.type); break; case 13:/* END OF EVENT */ if( i_print ) printf("%8X - END OF EVENT = %d\n", data, fadc_data.type); break; case 14:/* DATA NOT VALID (no data available) */ slot_id_dnv = (data & 0x7C00000) >> 22; if( i_print ) printf("%8X - DATA NOT VALID = %d slot = %d\n", data, fadc_data.type, slot_id_dnv); break; case 15:/* FILLER WORD */ slot_id_fill = (data & 0x7C00000) >> 22; if( i_print ) printf("%8X - FILLER WORD = %d slot = %d\n", data, fadc_data.type, slot_id_fill); break; } type_last = fadc_data.type; /* save type of current data word */ } }