#include #include #include #include #include #include // time in nano-sec void RawDecorder(uint8_t * data, size_t size, int verbose = 0){ uint64_t haha ; unsigned short dataType; int word = 0; int traceWord = -2; for( int i =0 ; i < ((int)size / 8) ; i++){ ///the data is array of 1 byte. /// by get the point of the data[8*i] /// cast it into uint64_t pointer, that will get 8 bytes all togteher /// get this point value, and bit swap it. haha = __builtin_bswap64(*(uint64_t *)&(data[8*i])); if (verbose > 0 ) printf("%7d | %3d | %3d | 0x%016lX \n", i, word, traceWord, haha); if ( word == 0 ){ /// aggregation header, or start header, or end header dataType = ( haha >> 56 ) & 0xFF ; if ( verbose> 0 & dataType == 0x30 ) printf("====== start Run data\n"); if ( verbose> 0 & dataType == 0x32 ) printf("====== Stop Run data\n"); if ( verbose> 0 & (dataType >> 4) == 0x2 ) printf("====== data aggregation start\n"); }else{ if( dataType == 0x30 ){ if( word == 1){ if( (haha & 0xFFFFFFFF) != 0x2000000) printf("*** something wrong.\n"); } uint64_t channelMask; if( word == 2){ channelMask = ( haha & 0xFFFFFFFF ); } if( word == 3){ channelMask += (( haha & 0xFFFFFFFF ) << 32); if (verbose > 0 ) printf("Channel mask : 0x%016lX\n", channelMask); } } if( (dataType >> 4 ) == 0x2 ) { unsigned short chID ; uint64_t timestamp ; bool hasTrace; unsigned short fl ; // flag low unsigned short fh ; // flog hight unsigned short energy ; unsigned short fTime ; unsigned short traceLength; unsigned short anaProb0[512]; unsigned short anaProb1[512]; bool digProb0[512]; bool digProb1[512]; bool digProb2[512]; bool digProb3[512]; if( word == 1){ chID = ((haha >> 56) & 0x7F); timestamp = (haha & 0xFFFFFFFFFFFF); }else if( word == 2){ hasTrace = ((haha >> 62) & 0x1); fl = (( haha >> 50) & 0x1FFF); fh = (( haha >> 41) & 0xFF); energy = (haha & 0xFFFF); fTime = ((haha >> 15) & 0x7FF); }else{ if( ((haha >> 63) & 0x1 ) == 1 && traceWord == -2) { //printf("=====last word of header\n"); traceWord = 0; if (verbose > 0 ) printf("ch %2d, energy %5u, time %10lu, fTime %5u %10.2f ms 0x%X, 0x%X, %u\n", chID, energy, timestamp, fTime, (timestamp*8 + fTime*0.0078125) / 1e+6 , fl, fh, hasTrace); word ++; continue; } if( hasTrace) { if( traceWord == 0) { traceLength = (haha & 0xFFF ); word ++; traceWord ++; continue; } if( traceWord > 0 ){ anaProb0[traceWord-1] = (haha & 0x3FFF); anaProb1[traceWord-1] = ((haha >> 16) & 0x3FFF); digProb0[traceWord-1] = ((haha >> 14) & 0x1); digProb1[traceWord-1] = ((haha >> 15) & 0x1); digProb2[traceWord-1] = ((haha >> 30) & 0x1); digProb3[traceWord-1] = ((haha >> 31) & 0x1); traceWord ++; if( traceWord - 1 == traceLength ){ //printf("============= end of one data, traceLength = %d\n", traceLength); word = 0; traceWord = -2; } } } } } } word++; } } int main(){ int ret; timespec t0, t1, t2; //#################################### Open digitizer uint64_t dev_handle; ret = CAEN_FELib_Open("dig2://192.168.0.100", &dev_handle); //#################################### Get some values char value[256]; ret = CAEN_FELib_GetValue(dev_handle, "/par/ModelName", value); printf("Model name:\t%s\n", value); ret = CAEN_FELib_GetValue(dev_handle, "/par/NumCh", value); short NChannel = atoi(value); printf("Channels:\t%u\n", NChannel); //#################################### Reset ret = CAEN_FELib_SendCommand(dev_handle, "/cmd/reset"); //#################################### Program digitizer /// Channel enable ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/ChEnable", "true"); /// Global trigger configuration ret = CAEN_FELib_SetValue(dev_handle, "/par/GlobalTriggerSource", "SwTrg | TestPulse"); // use test Pulse ret = CAEN_FELib_SetValue(dev_handle, "/par/TestPulsePeriod", "1000000"); /// ns, 1 msec ret = CAEN_FELib_SetValue(dev_handle, "/par/TestPulseWidth", "16"); ///ns /// Wave configuration ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/ChRecordLengthS", "512"); // when setting sample, the ChRecordLengthT will not set. vice versa ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/WaveTriggerSource", "GlobalTriggerSource"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/WaveResolution", "RES8"); // 8 ns ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/WaveAnalogProbe0", "ADCInput"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/WaveAnalogProbe1", "TimeFilter"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/WaveDigitalProbe0", "Trigger"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/WaveDigitalProbe1", "TimeFilterArmed"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/WaveDigitalProbe2", "EnergyFilterBaselineFreeze"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/WaveDigitalProbe3", "EnergyFilterPeakReady"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/ChPreTriggerS", "100"); /// Event configuration ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EventTriggerSource", "GlobalTriggerSource"); /// Filter parameters ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/TimeFilterRiseTimeS", "10"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EnergyFilterRiseTimeS", "100"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EnergyFilterFlatTopS", "100"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/TriggerThr", "3"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EnergyFilterPeakingPosition", "80"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EnergyFilterPoleZeroS", "1000"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/TimeFilterRetriggerGuardS", "10"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EnergyFilterPileupGuardT", "10"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EnergyFilterBaselineGuardS", "100"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/PulsePolarity", "Positive"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EnergyFilterLFLimitation", "Off"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EnergyFilterBaselineAvg", "Medium"); ret = CAEN_FELib_SetValue(dev_handle, "/ch/0..63/par/EnergyFilterFineGain", "1.0"); if( ret != CAEN_FELib_Success) printf("errro in set value\n"); //#################################### setting readout ret = CAEN_FELib_GetValue(dev_handle, "/ch/10/par/ChRecordLengthS", value); if( ret != CAEN_FELib_Success) printf("errro in get value\n"); printf("trace length : %s sample\n", value); //#################################### Set the end point for data /* {//----------------------- RAW uint64_t ep_handle; ret = CAEN_FELib_GetHandle(dev_handle, "/endpoint/raw", &ep_handle); //---------- configure endpoint uint64_t ep_folder_handle; ret = CAEN_FELib_GetParentHandle(ep_handle, NULL, &ep_folder_handle); ret = CAEN_FELib_SetValue(ep_folder_handle, "/par/activeendpoint", "raw"); ret = CAEN_FELib_SetReadDataFormat(ep_handle, " [ \ { \"name\": \"DATA\", \"type\": \"U8\", \"dim\": 1 }, \ { \"name\": \"SIZE\", \"type\": \"SIZE_T\" }, \ { \"name\": \"N_EVENTS\", \"type\": \"U32\" }, \ ]" ); uint8_t* data = new uint8_t[200000]; size_t size; /// number of byte of the data, size/8 = word [64 bits] uint32_t n_events; printf("Starting ACQ\n"); ret = CAEN_FELib_SendCommand(dev_handle, "/cmd/armacquisition"); ret = CAEN_FELib_SendCommand(dev_handle, "/cmd/swstartacquisition"); int count = 0; for( int i = 0; i < 10; i++){ //usleep(1000000); clock_gettime(CLOCK_REALTIME, &t0); ret = CAEN_FELib_ReadData(ep_handle, 100, data, &size, &n_events ); count ++; clock_gettime(CLOCK_REALTIME, &t1); if( ret == CAEN_FELib_Success ) { printf("================= %u, %ld byte, %d \n", n_events, size, count); RawDecorder(data, size); clock_gettime(CLOCK_REALTIME, &t2); printf("t2-t1 : %ld\n", t2.tv_nsec-t1.tv_nsec); } printf("t1-t0 : %ld\n", t1.tv_nsec-t0.tv_nsec); } printf("Stop ACQ\n"); ret = CAEN_FELib_SendCommand(dev_handle, "/cmd/disarmacquisition"); } /**/ {//------------------------ DPPPHA uint64_t ep_handle; ret = CAEN_FELib_GetHandle(dev_handle, "/endpoint/dpppha", &ep_handle); //---------- configure endpoint uint64_t ep_folder_handle; ret = CAEN_FELib_GetParentHandle(ep_handle, NULL, &ep_folder_handle); ret = CAEN_FELib_SetValue(ep_folder_handle, "/par/activeendpoint", "dpppha"); ret = CAEN_FELib_SetReadDataFormat(ep_handle, " [ \ { \"name\" : \"CHANNEL\", \"type\" : \"U8\" }, \ { \"name\" : \"TIMESTAMP\", \"type\" : \"U64\" }, \ { \"name\" : \"FINE_TIMESTAMP\", \"type\" : \"U16\" }, \ { \"name\" : \"ENERGY\", \"type\" : \"U16\" }, \ { \"name\" : \"ANALOG_PROBE_1\", \"type\" : \"I32\", \"dim\" : 1 }, \ { \"name\" : \"ANALOG_PROBE_2\", \"type\" : \"I32\", \"dim\" : 1 }, \ { \"name\" : \"DIGITAL_PROBE_1\", \"type\" : \"U8\", \"dim\" : 1 }, \ { \"name\" : \"DIGITAL_PROBE_2\", \"type\" : \"U8\", \"dim\" : 1 }, \ { \"name\" : \"DIGITAL_PROBE_3\", \"type\" : \"U8\", \"dim\" : 1 }, \ { \"name\" : \"DIGITAL_PROBE_4\", \"type\" : \"U8\", \"dim\" : 1 }, \ { \"name\" : \"ANALOG_PROBE_1_TYPE\", \"type\" : \"U8\" }, \ { \"name\" : \"ANALOG_PROBE_2_TYPE\", \"type\" : \"U8\" }, \ { \"name\" : \"DIGITAL_PROBE_1_TYPE\", \"type\" : \"U8\" }, \ { \"name\" : \"DIGITAL_PROBE_2_TYPE\", \"type\" : \"U8\" }, \ { \"name\" : \"DIGITAL_PROBE_3_TYPE\", \"type\" : \"U8\" }, \ { \"name\" : \"DIGITAL_PROBE_4_TYPE\", \"type\" : \"U8\" }, \ { \"name\" : \"WAVEFORM_SIZE\", \"type\" : \"SIZE_T\" }, \ { \"name\" : \"FLAGS_LOW_PRIORITY\", \"type\" : \"U16\"}, \ { \"name\" : \"FLAGS_HIGH_PRIORITY\", \"type\" : \"U16\" }, \ { \"name\" : \"EVENT_SIZE\", \"type\" : \"SIZE_T\" } \ ] \ " ); uint8_t channel; uint64_t timestamp; uint16_t fine_timestamp; uint16_t energy; int32_t* analog_probes[2]; uint8_t* digital_probes[4]; analog_probes[0] = new int32_t[512]; analog_probes[1] = new int32_t[512]; digital_probes[0] = new uint8_t[512]; digital_probes[1] = new uint8_t[512]; digital_probes[2] = new uint8_t[512]; digital_probes[3] = new uint8_t[512]; uint8_t analog_probes_type[2]; uint8_t digital_probes_type[4]; size_t n_samples; uint16_t flags_low_priority; uint16_t flags_high_priority; size_t event_size; //#################################### Read data loop printf("Starting ACQ\n"); ret = CAEN_FELib_SendCommand(dev_handle, "/cmd/armacquisition"); ret = CAEN_FELib_SendCommand(dev_handle, "/cmd/swstartacquisition"); timespec tsum; int count = 0; for( int i = 0; i < 64*5; i++){ //usleep(10000); clock_gettime(CLOCK_REALTIME, &t0); ret = CAEN_FELib_ReadData(ep_handle, 100, &channel, ×tamp, &fine_timestamp, &energy, analog_probes[0], analog_probes[1], digital_probes[0], digital_probes[1], digital_probes[2], digital_probes[3], &analog_probes_type[0], &analog_probes_type[1], &digital_probes_type[0], &digital_probes_type[1], &digital_probes_type[2], &digital_probes_type[3], &n_samples, &flags_low_priority, &flags_high_priority, &event_size ); count ++; clock_gettime(CLOCK_REALTIME, &t1); printf("%3d | t1-t0 : %ld\n", i, t1.tv_nsec-t0.tv_nsec); tsum.tv_nsec += t1.tv_nsec-t0.tv_nsec; if( channel == 63) { printf("================###################=== tsum : %ld\n", tsum.tv_nsec); tsum.tv_nsec = 0; } if( ret == CAEN_FELib_Success ) { printf("================= %ld, %d \n", event_size, count); printf("ch : %u, energy : %u, time : %lu, fTime : %u \n", channel, energy, timestamp, fine_timestamp); printf("nSample: %ld, Flag low: %u, Flag high : %u\n", n_samples, flags_low_priority, flags_high_priority); printf("Ana prob type: %u, %u, Digi : %u %u %u %u\n", analog_probes_type[0], analog_probes_type[1], digital_probes_type[0], digital_probes_type[1], digital_probes_type[2], digital_probes_type[3] ); for( int i = 0; i < n_samples; i++){ if( i % 100 == 0 ) printf("%3d | %d, %d, %u, %u, %u, %u \n", i, analog_probes[0][i], analog_probes[1][i], digital_probes[0][i], digital_probes[1][i], digital_probes[2][i], digital_probes[3][i]); } } } printf("Stop ACQ\n"); ret = CAEN_FELib_SendCommand(dev_handle, "/cmd/disarmacquisition"); }/**/ //#################################### Close digitizer ret = CAEN_FELib_Close(dev_handle); }