diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6c4450a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,50 @@ +{ + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "list": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp", + "script.C": "cpp" + } +} \ No newline at end of file diff --git a/ClassDigitizer2Gen.cpp b/ClassDigitizer2Gen.cpp index b24f114..b308fbd 100644 --- a/ClassDigitizer2Gen.cpp +++ b/ClassDigitizer2Gen.cpp @@ -180,7 +180,7 @@ void Digitizer2Gen::StopACQ(){ } -void Digitizer2Gen::SetPHADataFormat(unsigned dataFormat){ +void Digitizer2Gen::SetPHADataFormat(unsigned short dataFormat){ ///========== get endpoint and endpoint folder handle if( dataFormat < 15 ){ @@ -307,7 +307,7 @@ void Digitizer2Gen::SetPHADataFormat(unsigned dataFormat){ int Digitizer2Gen::ReadData(){ //printf("========= %s \n", __func__); - + if( evt->dataType == 0){ ret = CAEN_FELib_ReadData(ep_handle, 100, &evt->channel, @@ -315,12 +315,12 @@ int Digitizer2Gen::ReadData(){ &evt->fine_timestamp, &evt->timestamp_ns, &evt->energy, - &evt->analog_probes[0], - &evt->analog_probes[1], - &evt->digital_probes[0], - &evt->digital_probes[1], - &evt->digital_probes[2], - &evt->digital_probes[3], + evt->analog_probes[0], + evt->analog_probes[1], + evt->digital_probes[0], + evt->digital_probes[1], + evt->digital_probes[2], + evt->digital_probes[3], &evt->analog_probes_type[0], &evt->analog_probes_type[1], &evt->digital_probes_type[0], @@ -343,7 +343,7 @@ int Digitizer2Gen::ReadData(){ &evt->timestamp, &evt->fine_timestamp, &evt->energy, - &evt->analog_probes[0], + evt->analog_probes[0], &evt->analog_probes_type[0], &evt->traceLenght, &evt->flags_low_priority, @@ -435,12 +435,12 @@ void Digitizer2Gen::SaveDataToFile(){ fwrite(&evt->traceLenght, 8, 1, outFile); fwrite(evt->analog_probes_type, 2, 1, outFile); fwrite(evt->digital_probes_type, 4, 1, outFile); - fwrite(&evt->analog_probes[0], evt->traceLenght*4, 1, outFile); - fwrite(&evt->analog_probes[1], evt->traceLenght*4, 1, outFile); - fwrite(&evt->digital_probes[0], evt->traceLenght, 1, outFile); - fwrite(&evt->digital_probes[1], evt->traceLenght, 1, outFile); - fwrite(&evt->digital_probes[2], evt->traceLenght, 1, outFile); - fwrite(&evt->digital_probes[3], evt->traceLenght, 1, outFile); + fwrite(evt->analog_probes[0], evt->traceLenght*4, 1, outFile); + fwrite(evt->analog_probes[1], evt->traceLenght*4, 1, outFile); + fwrite(evt->digital_probes[0], evt->traceLenght, 1, outFile); + fwrite(evt->digital_probes[1], evt->traceLenght, 1, outFile); + fwrite(evt->digital_probes[2], evt->traceLenght, 1, outFile); + fwrite(evt->digital_probes[3], evt->traceLenght, 1, outFile); }else if( evt->dataType == 1){ fwrite(&dataStartIndetifier, 2, 1, outFile); fwrite(&evt->channel, 1, 1, outFile); @@ -451,7 +451,7 @@ void Digitizer2Gen::SaveDataToFile(){ fwrite(&evt->flags_low_priority, 2, 1, outFile); fwrite(&evt->traceLenght, 8, 1, outFile); fwrite(&evt->analog_probes_type[0], 1, 1, outFile); - fwrite(&evt->analog_probes[0], evt->traceLenght*4, 1, outFile); + fwrite(evt->analog_probes[0], evt->traceLenght*4, 1, outFile); }else if( evt->dataType == 2){ fwrite(&dataStartIndetifier, 2, 1, outFile); fwrite(&evt->channel, 1, 1, outFile); @@ -466,6 +466,8 @@ void Digitizer2Gen::SaveDataToFile(){ fwrite(&evt->energy, 2, 1, outFile); fwrite(&evt->timestamp, 6, 1, outFile); }else if( evt->dataType == 15){ + fwrite(&dataStartIndetifier, 2, 1, outFile); + fwrite(&evt->dataSize, 8, 1, outFile); fwrite(evt->data, evt->dataSize, 1, outFile); } @@ -496,10 +498,10 @@ void Digitizer2Gen::ProgramPHA(bool testPulse){ WriteValue("/ch/0..63/par/ChPreTriggerS" , "100"); WriteValue("/ch/0..63/par/WaveResolution" , "RES8"); /// 8 ns WriteValue("/ch/0..63/par/WaveAnalogProbe0" , "ADCInput"); - WriteValue("/ch/0..63/par/WaveAnalogProbe1" , "TimeFilter"); + WriteValue("/ch/0..63/par/WaveAnalogProbe1" , "EnergyFilter"); WriteValue("/ch/0..63/par/WaveDigitalProbe0" , "Trigger"); WriteValue("/ch/0..63/par/WaveDigitalProbe1" , "TimeFilterArmed"); - WriteValue("/ch/0..63/par/WaveDigitalProbe2" , "EnergyFilterBaselineFreeze"); + WriteValue("/ch/0..63/par/WaveDigitalProbe2" , "EnergyFilterPeaking"); WriteValue("/ch/0..63/par/WaveDigitalProbe3" , "EnergyFilterPeakReady"); // Event configuration @@ -507,7 +509,7 @@ void Digitizer2Gen::ProgramPHA(bool testPulse){ // Filter parameters WriteValue("/ch/0..63/par/TimeFilterRiseTimeS" , "10"); - WriteValue("/ch/0..63/par/TriggerThr" , "3"); + WriteValue("/ch/0..63/par/TriggerThr" , "100"); WriteValue("/ch/0..63/par/PulsePolarity" , "Positive"); WriteValue("/ch/0..63/par/EnergyFilterBaselineAvg" , "Medium"); WriteValue("/ch/0..63/par/EnergyFilterFineGain" , "1.0"); @@ -526,7 +528,6 @@ void Digitizer2Gen::ProgramPHA(bool testPulse){ } - std::string Digitizer2Gen::ErrorMsg(const char * funcName){ printf("======== %s | %s\n",__func__, funcName); char msg[1024]; diff --git a/ClassDigitizer2Gen.h b/ClassDigitizer2Gen.h index e155c72..bb6828c 100644 --- a/ClassDigitizer2Gen.h +++ b/ClassDigitizer2Gen.h @@ -65,7 +65,7 @@ class Digitizer2Gen { void StartACQ(); void StopACQ(); - void SetPHADataFormat(unsigned dataFormat); // 0 = all data, + void SetPHADataFormat(unsigned short dataFormat); // 0 = all data, // 1 = analog trace-0 only + flags // 2 = no trace, only ch, energy, timestamp, fine_timestamp + flags // 3 = only ch, energy, timestamp, minimum diff --git a/Event.h b/Event.h index 90798ab..6e3f940 100644 --- a/Event.h +++ b/Event.h @@ -27,7 +27,7 @@ class Event { uint8_t analog_probes_type[2]; // 3 bit uint8_t digital_probes_type[4]; // 4 bit int32_t * analog_probes[2]; // 18 bit - uint8_t * digital_probes[2]; // 1 bit + uint8_t * digital_probes[4]; // 1 bit uint16_t trigger_threashold; // 16 bit size_t event_size; // 64 bit uint32_t aggCounter; // 32 bit @@ -54,24 +54,22 @@ class Event { void ClearMemory(){ if( data != NULL ) delete data; - if( analog_probes != NULL) { - delete analog_probes[0]; - delete analog_probes[1]; - } + if( analog_probes[0] != NULL) delete analog_probes[0]; + if( analog_probes[1] != NULL) delete analog_probes[1]; - if( digital_probes != NULL) { - delete digital_probes[0]; - delete digital_probes[1]; - delete digital_probes[2]; - delete digital_probes[3]; - } + if( digital_probes[0] != NULL) delete digital_probes[0]; + if( digital_probes[1] != NULL) delete digital_probes[1]; + if( digital_probes[2] != NULL) delete digital_probes[2]; + if( digital_probes[3] != NULL) delete digital_probes[3]; } void SetDataType(unsigned int type){ dataType = type; ClearMemory(); - if( dataType != 0xF){ + if( dataType == 0xF){ + data = new uint8_t[20*1024*1024]; + }else{ analog_probes[0] = new int32_t[MaxTraceLenght]; analog_probes[1] = new int32_t[MaxTraceLenght]; @@ -79,8 +77,7 @@ class Event { digital_probes[1] = new uint8_t[MaxTraceLenght]; digital_probes[2] = new uint8_t[MaxTraceLenght]; digital_probes[3] = new uint8_t[MaxTraceLenght]; - }else{ - data = new uint8_t[20*1024*1024]; + } } @@ -89,13 +86,57 @@ class Event { printf("ch: %2d, energy: %u, timestamp: %lu ch, traceLenght: %lu\n", channel, energy, timestamp, traceLenght); } + std::string AnaProbeType(uint8_t probeType){ + switch(probeType){ + case 0: return "ADC"; + case 1: return "Time filter"; + case 2: return "Energy filter"; + default : return "none"; + } + } + + std::string DigiProbeType(uint8_t probeType){ + switch(probeType){ + case 0: return "Trigger"; + case 1: return "Time filter armed"; + case 2: return "Re-trigger guard"; + case 3: return "Energy filter baseline freeze"; + case 4: return "Energy filter peaking"; + case 5: return "Energy filter peaking ready"; + case 6: return "Energy filter pile-up guard"; + case 7: return "Event pile-up"; + case 8: return "ADC saturation"; + case 9: return "ADC saturation protection"; + case 10: return "Post-saturation event"; + case 11: return "Energy filter saturation"; + case 12: return "Signal inhibit"; + default : return "none"; + } + } + void PrintAll(){ + printf("============= Type : %u\n", dataType); printf("ch : %2d (0x%02X), fail: %d, flush: %d\n", channel, channel, board_fail, flush); printf("energy: %u, timestamp: %lu, fine_timestamp: %u \n", energy, timestamp, fine_timestamp); printf("flag (high): 0x%02X, (low): 0x%03X, traceLength: %lu\n", flags_high_priority, flags_low_priority, traceLenght); - printf("Agg counter : %u, trigger : %u, downSampling: %u \n", aggCounter, trigger_threashold, downSampling); - printf("AnaProbe Type: %u, %u\n", analog_probes_type[0], analog_probes_type[1]); - printf("DigProbe Type: %u, %u, %u, %u\n", digital_probes_type[0], digital_probes_type[1], digital_probes_type[2], digital_probes_type[3]); + printf("Agg counter : %u, trigger Thr.: %u, downSampling: %u \n", aggCounter, trigger_threashold, downSampling); + printf("AnaProbe Type: %s(%u), %s(%u)\n", AnaProbeType(analog_probes_type[0]).c_str(), analog_probes_type[0], + AnaProbeType(analog_probes_type[1]).c_str(), analog_probes_type[1]); + printf("DigProbe Type: %s(%u), %s(%u), %s(%u), %s(%u)\n", DigiProbeType(digital_probes_type[0]).c_str(), digital_probes_type[0], + DigiProbeType(digital_probes_type[1]).c_str(), digital_probes_type[1], + DigiProbeType(digital_probes_type[2]).c_str(), digital_probes_type[2], + DigiProbeType(digital_probes_type[3]).c_str(), digital_probes_type[3]); + } + + void PrintTrace(unsigned short ID){ + for(int i = 0; i < traceLenght; i++){ + if( ID == 0 ) printf("%4d| %6d\n", i, analog_probes[0][i]); + if( ID == 1 ) printf("%4d| %6d\n", i, analog_probes[1][i]); + if( ID == 2 ) printf("%4d| %u\n", i, digital_probes[0][i]); + if( ID == 3 ) printf("%4d| %u\n", i, digital_probes[1][i]); + if( ID == 4 ) printf("%4d| %u\n", i, digital_probes[2][i]); + if( ID == 5 ) printf("%4d| %u\n", i, digital_probes[3][i]); + } } void PrintAllTrace(){ diff --git a/SolReader.h b/SolReader.h new file mode 100644 index 0000000..3cb2a7a --- /dev/null +++ b/SolReader.h @@ -0,0 +1,191 @@ +#ifndef SOLREADER_H +#define SOLREADER_H + +#include /// for FILE +#include +#include +#include +#include +#include // time in nano-sec + +#include "Event.h" + +class SolReader { + private: + FILE * inFile; + unsigned int inFileSize; + unsigned int filePos; + unsigned int totNumBlock; + + unsigned short blockStart; + unsigned int numBlock; + + void init(); + + public: + SolReader(); + SolReader(std::string fileName, unsigned short dataType); + ~SolReader(); + + void OpenFile(std::string fileName); + int ReadBlock(int opt); // opt = 0, noraml, 1, fast + + void ScanNumBlock(); + + unsigned int GetNumBlock(){return numBlock;} + + Event * evt; + +}; + +void SolReader::init(){ + numBlock = 0; + filePos = 0; + totNumBlock = 0; + evt = new Event(); +} + +SolReader::SolReader(){ + init(); +} + +SolReader::SolReader(std::string fileName, unsigned short dataType = 0){ + init(); + OpenFile(fileName); + evt->SetDataType(dataType); +} + +SolReader::~SolReader(){ + if( !inFile ) fclose(inFile); +} + +inline void SolReader::OpenFile(std::string fileName){ + inFile = fopen(fileName.c_str(), "r"); + if( inFile == NULL ){ + printf("Cannot open file : %s \n", fileName.c_str()); + }else{ + fseek(inFile, 0L, SEEK_END); + inFileSize = ftell(inFile); + rewind(inFile); + } +} + +inline int SolReader::ReadBlock(int opt = 0){ + if( inFile == NULL ) return -1; + if( feof(inFile) ) return -1; + if( inFileSize < filePos) return -1; + + fread(&blockStart, 2, 1, inFile); + + if( (blockStart & 0xAAA0) != 0xAAA0 ) { + printf("header fail.\n"); + return -2 ; + } + + if( ( blockStart & 0xF ) == 15 ){ + evt->SetDataType(15); + } + evt->dataType = blockStart & 0xF; + + if( evt->dataType == 0){ + if( opt == 0 ){ + fread(&evt->channel, 1, 1, inFile); + fread(&evt->energy, 2, 1, inFile); + fread(&evt->timestamp, 6, 1, inFile); + fread(&evt->fine_timestamp, 2, 1, inFile); + fread(&evt->flags_high_priority, 1, 1, inFile); + fread(&evt->flags_low_priority, 2, 1, inFile); + fread(&evt->downSampling, 1, 1, inFile); + fread(&evt->board_fail, 1, 1, inFile); + fread(&evt->flush, 1, 1, inFile); + fread(&evt->trigger_threashold, 2, 1, inFile); + fread(&evt->event_size, 8, 1, inFile); + fread(&evt->aggCounter, 4, 1, inFile); + }else{ + fseek(inFile, 31, SEEK_CUR); + } + fread(&evt->traceLenght, 8, 1, inFile); + if( opt == 0){ + fread(evt->analog_probes_type, 2, 1, inFile); + fread(evt->digital_probes_type, 4, 1, inFile); + fread(evt->analog_probes[0], evt->traceLenght*4, 1, inFile); + fread(evt->analog_probes[1], evt->traceLenght*4, 1, inFile); + fread(evt->digital_probes[0], evt->traceLenght, 1, inFile); + fread(evt->digital_probes[1], evt->traceLenght, 1, inFile); + fread(evt->digital_probes[2], evt->traceLenght, 1, inFile); + fread(evt->digital_probes[3], evt->traceLenght, 1, inFile); + }else{ + fseek(inFile, 6 + evt->traceLenght*(12), SEEK_CUR); + } + }else if( evt->dataType == 1){ + if( opt == 0 ){ + fread(&evt->channel, 1, 1, inFile); + fread(&evt->energy, 2, 1, inFile); + fread(&evt->timestamp, 6, 1, inFile); + fread(&evt->fine_timestamp, 2, 1, inFile); + fread(&evt->flags_high_priority, 1, 1, inFile); + fread(&evt->flags_low_priority, 2, 1, inFile); + }else{ + fseek(inFile, 14, SEEK_CUR); + } + fread(&evt->traceLenght, 8, 1, inFile); + if( opt == 0){ + fread(&evt->analog_probes_type[0], 1, 1, inFile); + fread(evt->analog_probes[0], evt->traceLenght*4, 1, inFile); + }else{ + fseek(inFile, 1 + evt->traceLenght*4, SEEK_CUR); + } + }else if( evt->dataType == 2){ + if( opt == 0 ){ + fread(&evt->channel, 1, 1, inFile); + fread(&evt->energy, 2, 1, inFile); + fread(&evt->timestamp, 6, 1, inFile); + fread(&evt->fine_timestamp, 2, 1, inFile); + fread(&evt->flags_high_priority, 1, 1, inFile); + fread(&evt->flags_low_priority, 2, 1, inFile); + }else{ + fseek(inFile, 14, SEEK_CUR); + } + }else if( evt->dataType == 3){ + if( opt == 0 ){ + fread(&evt->channel, 1, 1, inFile); + fread(&evt->energy, 2, 1, inFile); + fread(&evt->timestamp, 6, 1, inFile); + }else{ + fseek(inFile, 9, SEEK_CUR); + } + }else if( evt->dataType == 15){ + fread(&evt->dataSize, 8, 1, inFile); + if( opt == 0){ + fread(evt->data, evt->dataSize, 1, inFile); + }else{ + fseek(inFile, evt->dataSize, SEEK_CUR); + } + } + + numBlock ++; + filePos = ftell(inFile); + return 0; +} + + +void SolReader::ScanNumBlock(){ + if( inFile == NULL ) return; + if( feof(inFile) ) return; + + while( ReadBlock(1) == 0){ + //printf("%u, traceLen %lu, %u/%u\n", numBlock, evt->traceLenght, filePos, inFileSize); + } + + totNumBlock = numBlock; + numBlock = 0; + + printf("Scan complete: number of data Block : %u\n", totNumBlock); + + rewind(inFile); + filePos = 0; + +} + + +#endif \ No newline at end of file diff --git a/script.C b/script.C new file mode 100644 index 0000000..908ae2f --- /dev/null +++ b/script.C @@ -0,0 +1,28 @@ +#include "SolReader.h" + +void script(){ + + SolReader * reader = new SolReader("haha_000.sol"); + Event * evt = reader->evt; + + reader->ScanNumBlock(); + + + for( int i = 0; i < 2; i++){ + printf("########### nBlock : %u\n", reader->GetNumBlock()); + reader->ReadBlock(); + evt->PrintAll(); + reader->evt->PrintAllTrace(); + + } + + //printf("reader traceLength : %lu \n", evt->traceLenght); + + /* + for( int i = 0; i < evt->traceLenght; i++){ + + printf("%4d| %d\n", i, evt->analog_probes[0][i]); + + } + */ +} \ No newline at end of file diff --git a/test.cpp b/test.cpp index a39181e..a447586 100644 --- a/test.cpp +++ b/test.cpp @@ -12,6 +12,8 @@ int main(int argc, char* argv[]){ printf("\t CAEN firmware DPP-PHA testing \n"); printf("##########################################\n"); + remove("haha_000.sol"); + const char * url = "dig2://192.168.0.100/"; Digitizer2Gen * digi = new Digitizer2Gen(); @@ -69,9 +71,7 @@ int main(int argc, char* argv[]){ //printf("0x%016lX \n", *(digi->evt.GetWord3())); //printf("0x%016lX \n", *(digi->evt.GetWord4())); - - - //digi->SaveDataToFile(); + digi->SaveDataToFile(); if( digi->evt->channel == 63 ) { clock_gettime(CLOCK_REALTIME, &t1);