diff --git a/ClassData.h b/ClassData.h index 4d6c2d0..e52cef6 100644 --- a/ClassData.h +++ b/ClassData.h @@ -9,14 +9,13 @@ #include ///cout #include -//#include "CAENDigitizer.h" -//#include "CAENDigitizerType.h" -//#include "macro.h" +#include "CAENDigitizerType.h" class Data{ public: + int DPPType; unsigned int nByte; /// number of byte char *buffer; /// readout buffer uint32_t AllocatedSize; @@ -38,6 +37,9 @@ class Data{ protected: unsigned int ReadBuffer(unsigned int nWord, int verbose = 0); + + int DecodePHADualChannelBlock(unsigned int nWord, unsigned int ChannelMask, int verbose); + int DecodePSDDualChannelBlock(unsigned int nWord, unsigned int ChannelMask, int verbose); }; @@ -120,78 +122,17 @@ inline void Data::DecodeBuffer(int verbose){ unsigned int AggCounter = ReadBuffer(nw, verbose); if( verbose >= 2 ) printf("Agg Counter : %d \n", AggCounter); - - for( int chMask = 0; chMask < 8 ; chMask ++ ){ if( ((ChannelMask >> chMask) & 0x1 ) == 0 ) continue; if( verbose >= 2 ) printf("---------------------- Dual Channel Block : %d\n", chMask *2 ); - nw = nw + 1; word = ReadBuffer(nw, verbose); - bool hasFormatInfo = ((word >> 31) & 0x1); - unsigned int aggSize = ( word & 0x3FFFFFF ) ; - if( verbose >= 2 ) printf(" size : %d \n", aggSize); - unsigned int nSample = 0; /// wave form; - unsigned int nEvents = 0; - if( hasFormatInfo ){ - nw = nw + 1; word = ReadBuffer(nw, verbose); - nSample = ( word & 0xFFFF ) * 8; - unsigned int digitalProbe = ( (word >> 16 ) & 0xF ); - unsigned int analogProbe2 = ( (word >> 20 ) & 0x3 ); - unsigned int analogProbe1 = ( (word >> 22 ) & 0x3 ); - unsigned int extra2Option = ( (word >> 24 ) & 0x7 ); - bool hasWaveForm = ( (word >> 27 ) & 0x1 ); - bool hasExtra2 = ( (word >> 28 ) & 0x1 ); - bool hasTimeStamp = ( (word >> 29 ) & 0x1 ); - bool hasEnergy = ( (word >> 30 ) & 0x1 ); - bool hasDualTrace = ( (word >> 31 ) & 0x1 ); - - if( verbose >= 2 ) printf("dualTrace : %d, Energy : %d, Time: %d, Wave : %d, Extra2: %d, Extra2Option: %d \n", - hasDualTrace, hasEnergy, hasTimeStamp, hasWaveForm, hasExtra2, extra2Option); - if( verbose >= 2 ) printf("Ana Probe 1 & 2: %d %d , Digi Probe: %d, nSample : %d \n", - analogProbe1, analogProbe2, digitalProbe, nSample); - - nEvents = aggSize / (nSample/2 + 2 + hasExtra2 ); - if( verbose >= 2 ) printf("=========== nEvents : %d \n", nEvents); - }else{ - if( verbose >= 2 ) printf("does not has format info. unable to read buffer.\n"); - break; + + if( DPPType == V1730_DPP_PHA_CODE ) { + nw = DecodePHADualChannelBlock(nw + 1, chMask, verbose); + if ( nw < 0 ) break; } - - for( int ev = 0; ev < nEvents ; ev++){ - if( verbose >= 2 ) printf("=================================== event : %d\n", ev); - nw = nw +1 ; word = ReadBuffer(nw, verbose); - bool channelTag = ((word >> 31) & 0x1); - unsigned int timeStamp = (word & 0x7FFFFFFF); - int channel = chMask*2 + channelTag; - if( verbose >= 2 ) printf("ch : %d, timeStamp %u \n", channel, timeStamp); - - ///===== read waveform - for( int wi = 0; wi < nSample/2; wi++){ - nw = nw +1 ; word = ReadBuffer(nw, verbose - 2); - bool isTrigger1 = (( word >> 31 ) & 0x1 ); - unsigned int wave1 = (( word >> 16) & 0x3FFF); - - bool isTrigger0 = (( word >> 15 ) & 0x1 ); - unsigned int wave0 = ( word & 0x3FFF); - - if( verbose >= 3 && ev == 0 ){ - printf("%4d| %5d, %d \n", 2*wi, wave0, isTrigger0); - printf("%4d| %5d, %d \n", 2*wi+1, wave1, isTrigger1); - } - } - - nw = nw +1 ; word = ReadBuffer(nw, verbose); - unsigned int extra2 = word; - - nw = nw +1 ; word = ReadBuffer(nw, verbose); - unsigned int extra = (( word >> 16) & 0x3FF); - unsigned int energy = (word & 0x7FFF); - bool pileUp = ((word >> 15) & 0x1); - - if( verbose >= 2 ) printf("PileUp : %d , extra : 0x%04x, energy : %d \n", pileUp, extra, energy); - - if( verbose >= 1 ) printf("ch : %2d, PileUp : %d , energy : %d, timestamp : %u\n", - channel, pileUp, energy, timeStamp); - + if( DPPType == V1730_DPP_PSD_CODE ) { + nw = DecodePHADualChannelBlock(nw + 1, chMask, verbose); + if ( nw < 0 ) break; } } }else{ @@ -202,4 +143,290 @@ inline void Data::DecodeBuffer(int verbose){ }while(true); } +inline int Data::DecodePHADualChannelBlock(unsigned int nWord, unsigned int ChannelMask, int verbose){ + + int nw = nWord; + unsigned int word = ReadBuffer(nw, verbose); + + bool hasFormatInfo = ((word >> 31) & 0x1); + unsigned int aggSize = ( word & 0x7FFFFFFF ) ; + if( verbose >= 2 ) printf(" size : %d \n", aggSize); + unsigned int nSample = 0; /// wave form; + unsigned int nEvents = 0; + unsigned int extra2Option = 0; + if( hasFormatInfo ){ + nw = nw + 1; word = ReadBuffer(nw, verbose); + + nSample = ( word & 0xFFFF ) * 8; + extra2Option = ( (word >> 24 ) & 0x7 ); + + unsigned int digitalProbe = ( (word >> 16 ) & 0xF ); + unsigned int analogProbe2 = ( (word >> 20 ) & 0x3 ); + unsigned int analogProbe1 = ( (word >> 22 ) & 0x3 ); + bool hasWaveForm = ( (word >> 27 ) & 0x1 ); + bool hasExtra2 = ( (word >> 28 ) & 0x1 ); + bool hasTimeStamp = ( (word >> 29 ) & 0x1 ); + bool hasEnergy = ( (word >> 30 ) & 0x1 ); + bool hasDualTrace = ( (word >> 31 ) & 0x1 ); + + if( verbose >= 2 ) { + printf("dualTrace : %d, Energy : %d, Time: %d, Wave : %d, Extra2: %d \n", + hasDualTrace, hasEnergy, hasTimeStamp, hasWaveForm, hasExtra2); + if( hasExtra2 ){ + printf("...... extra 2 : "); + switch (extra2Option){ + case 0: printf("[0:15] trapwzoid baseline * 4 [16:31] Extended timestamp (16-bit)\n"); break; + case 1: printf("Reserved\n"); break; + case 2: printf("[0:9] Fine time stamp [10:15] Reserved [16:31] Extended timestamp (16-bit)\n"); break; + case 3: printf("Reserved\n"); break; + case 4: printf("[0:15] Total trigger counter [16:31] Lost trigger counter\n"); break; + case 5: printf("[0:15] Event after Zero crossing [16:31] Event before Zero crossing\n"); break; + case 6: printf("Reserved\n"); break; + case 7: printf("Reserved\n"); break; + } + } + printf("...... Analog Probe 1 : "); + switch (analogProbe1 ){ + case 0 : printf("Input \n"); break; + case 1 : printf("RC-CR (1st derivative) \n"); break; + case 2 : printf("RC-CR2 (2st derivative) \n"); break; + case 3 : printf("trapazoid \n"); break; + } + printf("...... Analog Probe 2 : "); + switch (analogProbe2 ){ + case 0 : printf("Input \n"); break; + case 1 : printf("Theshold \n"); break; + case 2 : printf("trapezoid - baseline \n"); break; + case 3 : printf("baseline \n"); break; + } + printf("...... Digital Probe : "); + switch (digitalProbe ){ + case 0 : printf("Peaking \n"); break; + case 1 : printf("Armed (trigger) \n"); break; + case 2 : printf("Peak Run \n"); break; + case 3 : printf("Pile up \n"); break; + case 4 : printf("Peaking \n"); break; + case 5 : printf("Trigger Validation Window \n"); break; + case 6 : printf("Baseline for energy calculation \n"); break; + case 7 : printf("Trigger holdoff \n"); break; + case 8 : printf("Trigger Validation \n"); break; + case 9 : printf("ACQ Busy \n"); break; + case 10 : printf("Trigger window \n"); break; + case 11 : printf("Ext. Trigger \n"); break; + case 12 : printf("Busy = memory is full \n"); break; + } + } + nEvents = aggSize / (nSample/2 + 2 + hasExtra2 ); + if( verbose >= 2 ) printf("=========== nEvents : %d \n", nEvents); + }else{ + if( verbose >= 2 ) printf("does not has format info. unable to read buffer.\n"); + return -1; + } + + ///========== decode an event + for( int ev = 0; ev < nEvents ; ev++){ + if( verbose >= 2 ) printf("=================================== event : %d\n", ev); + nw = nw +1 ; word = ReadBuffer(nw, verbose); + bool channelTag = ((word >> 31) & 0x1); + unsigned int timeStamp0 = (word & 0x7FFFFFFF); + int channel = ChannelMask*2 + channelTag; + if( verbose >= 2 ) printf("ch : %d, timeStamp0 %u \n", channel, timeStamp0); + + ///===== read waveform + unsigned int triggerAtSample = 0 ; + for( int wi = 0; wi < nSample/2; wi++){ + nw = nw +1 ; word = ReadBuffer(nw, verbose - 2); + bool isTrigger1 = (( word >> 31 ) & 0x1 ); + bool dp1 = (( word >> 30 ) & 0x1 ); + unsigned int wave1 = (( word >> 16) & 0x3FFF); + + bool isTrigger0 = (( word >> 15 ) & 0x1 ); + bool dp0 = (( word >> 14 ) & 0x1 ); + unsigned int wave0 = ( word & 0x3FFF); + + if( isTrigger0 == 1 ) triggerAtSample = 2*wi ; + if( isTrigger1 == 1 ) triggerAtSample = 2*wi + 1; + + if( verbose >= 3 && ev == 0 ){ + printf("%4d| %5d, %d, %d \n", 2*wi, wave0, dp0, isTrigger0); + printf("%4d| %5d, %d, %d \n", 2*wi+1, wave1, dp1, isTrigger1); + } + } + + nw = nw +1 ; word = ReadBuffer(nw, verbose); + unsigned int extra2 = word; + unsigned long long extTimeStamp = 0; + if( extra2Option == 0 || extra2Option == 2 ) extTimeStamp = (extra2 >> 15); + + unsigned long long timeStamp = (extTimeStamp << 32) ; + timeStamp = timeStamp + timeStamp0; + + nw = nw +1 ; word = ReadBuffer(nw, verbose); + unsigned int extra = (( word >> 16) & 0x3FF); + unsigned int energy = (word & 0x7FFF); + bool pileUpOrRollOver = ((word >> 15) & 0x1); + bool pileUp = (extra & 0x200); + bool rollOver = (extra & 0x002); + + if( verbose >= 2 ) { + printf("PileUp : %d , extra : 0x%03x, energy : %d \n", pileUp, extra, energy); + + printf(" lost event : %d \n", ((extra >> 0) & 0x1) ); + printf(" roll-over : %d \n", ((extra >> 1) & 0x1) ); + printf(" fake-event : %d \n", ((extra >> 3) & 0x1) ); + printf(" input sat. : %d \n", ((extra >> 4) & 0x1) ); + printf(" lost trg : %d \n", ((extra >> 5) & 0x1) ); + printf(" tot trg : %d \n", ((extra >> 6) & 0x1) ); + printf(" coincident : %d \n", ((extra >> 7) & 0x1) ); + printf(" not coin. : %d \n", ((extra >> 8) & 0x1) ); + printf(" pile-up : %d \n", ((extra >> 9) & 0x1) ); + printf(" trapezoid sat. : %d \n", ((extra >> 10) & 0x1) ); + + } + + if( verbose >= 1 ) printf("ch : %2d, PileUp : %d , energy : %d, roll-Over: %d, timestamp : %llu, triggerAt : %d\n", + channel, pileUp, energy, rollOver, timeStamp, triggerAtSample); + + } + + ///=========== Key information + /// ch, energy, timestamp + /// trace + + + return nw; +} + +inline int Data::DecodePSDDualChannelBlock(unsigned int nWord, unsigned int ChannelMask, int verbose){ + int nw = nWord; + unsigned int word = ReadBuffer(nw, verbose); + + if( (word >> 31) != 1 ) return -1; + + unsigned int aggSize = ( word & 0x3FFFFF ) ; + if( verbose >= 2 ) printf(" size : %d \n", aggSize); + + unsigned int nEvents = 0; + nw = nw + 1; word = ReadBuffer(nw, verbose); + unsigned int nSample = ( word & 0xFFFF ) * 8; + unsigned int digitalProbe1 = ( (word >> 16 ) & 0x7 ); + unsigned int digitalProbe2 = ( (word >> 19 ) & 0x7 ); + unsigned int analogProbe = ( (word >> 22 ) & 0x3 ); + unsigned int extraOption = ( (word >> 24 ) & 0x7 ); + bool hasWaveForm = ( (word >> 27 ) & 0x1 ); + bool hasExtra = ( (word >> 28 ) & 0x1 ); + bool hasTimeStamp = ( (word >> 29 ) & 0x1 ); + bool hasCharge = ( (word >> 30 ) & 0x1 ); + bool hasDualTrace = ( (word >> 31 ) & 0x1 ); + + if( verbose >= 2 ) { + printf("dualTrace : %d, Charge : %d, Time: %d, Wave : %d, Extra: %d\n", + hasDualTrace, hasCharge, hasTimeStamp, hasWaveForm, hasExtra); + if( hasExtra ){ + printf(".... extra : "); + switch(extraOption){ + case 0: printf("[0:15] trapwzoid baseline * 4 [16:31] Extended timestamp (16-bit)\n"); break; + case 1: printf("[0:11] reserved [12] lost trigger counted [13] 1024 trigger counted [14] Over-range\n"); + printf("[15] trigger lost [16:31] Extended timestamp (16-bit)\n"); break; + case 2: printf("[0:9] Fine time stamp [10:15] flag [10:15] Reserved [16:31] Extended timestamp (16-bit)\n"); break; + case 3: printf("Reserved\n"); break; + case 4: printf("[0:15] Total trigger counter [16:31] Lost trigger counter\n"); break; + case 5: printf("[0:15] Event after Zero crossing [16:31] Event before Zero crossing\n"); break; + case 6: printf("Reserved\n"); break; + case 7: printf("debug, must be 0x12345678\n"); break; + } + } + printf(".... digital Probe 1 : "); + switch(digitalProbe1){ + case 0 : printf("Long gate \n"); break; + case 1 : printf("Over threshold \n"); break; + case 2 : printf("Shaped TRG \n"); break; + case 3 : printf("TRG Val. Acceptance \n"); break; + case 4 : printf("Pile-Up \n"); break; + case 5 : printf("Coincidence \n"); break; + case 6 : printf("Reserved \n"); break; + case 7 : printf("Trigger \n"); break; + } + printf(".... digital Probe 2 : "); + switch(digitalProbe2){ + case 0 : printf("Short gate \n"); break; + case 1 : printf("Over threshold \n"); break; + case 2 : printf("TRG Validation \n"); break; + case 3 : printf("TRG HoldOff \n"); break; + case 4 : printf("Pile-Up \n"); break; + case 5 : printf("Coincidence \n"); break; + case 6 : printf("Reserved \n"); break; + case 7 : printf("Trigger \n"); break; + } + printf(".... analog Probe (dual trace : %d): ", hasDualTrace); + if( hasDualTrace ) { + switch(analogProbe){ + case 0 : printf("Input and baseline \n"); break; + case 1 : printf("CFD and baseline \n"); break; + case 2 : printf("Input and CFD \n"); break; + } + }else{ + switch(analogProbe){ + case 0 : printf("Input \n"); break; + case 1 : printf("CFD \n"); break; + } + } + } + + nEvents = aggSize / (nSample/2 + 2 + hasExtra ); + if( verbose >= 2 ) printf("=========== nEvents : %d \n", nEvents); + + ///========= Decode an event + for( int ev = 0; ev < nEvents ; ev++){ + if( verbose >= 2 ) printf("=================================== event : %d\n", ev); + nw = nw +1 ; word = ReadBuffer(nw, verbose); + bool channelTag = ((word >> 31) & 0x1); + unsigned int timeStamp0 = (word & 0x7FFFFFFF); + int channel = ChannelMask*2 + channelTag; + if( verbose >= 2 ) printf("ch : %d, timeStamp %u \n", channel, timeStamp0); + + ///===== read waveform + for( int wi = 0; wi < nSample/2; wi++){ + nw = nw +1 ; word = ReadBuffer(nw, verbose - 2); + bool dp2b = (( word >> 31 ) & 0x1 ); + bool dp1b = (( word >> 30 ) & 0x1 ); + unsigned int waveb = (( word >> 16) & 0x3FFF); + + bool dp2a = (( word >> 15 ) & 0x1 ); + bool dp1a = (( word >> 14 ) & 0x1 ); + unsigned int wavea = ( word & 0x3FFF); + + if( verbose >= 3 && ev == 0 ){ + printf("%4d| %5d, %d, %d \n", 2*wi, wavea, dp1a, dp2a); + printf("%4d| %5d, %d, %d \n", 2*wi+1, waveb, dp1b, dp2b); + } + } + + nw = nw +1 ; word = ReadBuffer(nw, verbose); + unsigned int extra = word; + unsigned long long extTimeStamp = 0; + if( extraOption == 0 || extraOption == 2 ) extTimeStamp = (extra >> 15); + + unsigned long long timeStamp = (extTimeStamp << 32) ; + timeStamp = timeStamp + timeStamp0; + + nw = nw +1 ; word = ReadBuffer(nw, verbose); + unsigned int Qlong = (( word >> 16) & 0xFFFF); + unsigned int Qshort = (word & 0x7FFF); + bool isEnergyCorrect = ((word >> 15) & 0x1); + + if( verbose >= 2 ) printf("extra : 0x%08x, Qshort : %d, Qlong : %d \n", extra, Qshort, Qlong); + + if( verbose >= 1 ) printf("ch : %2d, Qshort : %d, Qlong : %d, timestamp : %llu\n", + channel, Qshort, Qlong, timeStamp); + + } + + ///=========== Key information + /// ch, Qshort, Qlong , timestamp + /// trace + + return nw; +} + #endif diff --git a/ClassDigitizer.cpp b/ClassDigitizer.cpp index c263d76..acd71db 100644 --- a/ClassDigitizer.cpp +++ b/ClassDigitizer.cpp @@ -100,6 +100,9 @@ int Digitizer::OpenDigitizer(int boardID, int portID, bool verbose){ if (DPPType < 128) { if( verbose) printf("This digitizer does not have DPP-PHA firmware\n"); } + data->DPPType = DPPType; + + //SetBoardID(BoardInfo.SerialNumber); } } diff --git a/ClassDigitizer.h b/ClassDigitizer.h index 13e3305..5edfa60 100644 --- a/ClassDigitizer.h +++ b/ClassDigitizer.h @@ -52,6 +52,8 @@ class Digitizer{ void SetBits(uint32_t address, unsigned int bitValue, unsigned int bitLength, unsigned int bitSmallestPos, int ch = -1); void SetDPPAlgorithmControl(uint32_t bit, int ch = -1); + //void SetBoardID(unsigned int ID) {WriteRegister(Register::DPP::BoardID, ID)); + ///================ Get Settings int GetSerialNumber() {return BoardInfo.SerialNumber;} int GetChannelMask() {return channelMask;} diff --git a/test.cpp b/test.cpp index ec8d159..da40188 100644 --- a/test.cpp +++ b/test.cpp @@ -44,7 +44,7 @@ int main(int argc, char* argv[]){ ///********************* method for using Data Class for decoding bin file - /** + FILE * haha = fopen("output.bin", "r"); fseek(haha, 0L, SEEK_END); size_t inFileSize = ftell(haha); @@ -57,24 +57,25 @@ int main(int argc, char* argv[]){ fclose(haha); Data * data = new Data(); + data->DPPType = V1730_DPP_PHA_CODE; data->buffer = buffer; - data->DecodeBuffer(3); + data->DecodeBuffer(14); /*********************/ ///****************************** casting digitizer type - Digitizer ** dig = new Digitizer *[2] ; - //dig[0] = new Digitizer(); - dig[0] = new DigitizerPHA(); - dig[1] = new DigitizerPSD(); - - printf("%d \n", dig[0]->GetDPPType()); - printf("%d \n", dig[1]->GetDPPType()); - - ((DigitizerPHA *)dig[0])->PrintBoardConfiguration(); - - ((DigitizerPHA *)dig[0])->SetDPPAlgorithmControl2(0x10000); + ///Digitizer ** dig = new Digitizer *[2] ; + /////dig[0] = new Digitizer(); + ///dig[0] = new DigitizerPHA(); + ///dig[1] = new DigitizerPSD(); + /// + ///printf("%d \n", dig[0]->GetDPPType()); + ///printf("%d \n", dig[1]->GetDPPType()); + /// + ///((DigitizerPHA *)dig[0])->PrintBoardConfiguration(); + /// + ///((DigitizerPHA *)dig[0])->SetDPPAlgorithmControl2(0x10000);