diff --git a/.gitignore b/.gitignore index 396b2aa..5acf123 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ test FSUDAQ +test_indep diff --git a/ClassData.h b/ClassData.h index 2778a5a..2be0d7b 100644 --- a/ClassData.h +++ b/ClassData.h @@ -18,7 +18,7 @@ class Data{ public: - int nByte; /// number of byte + unsigned int nByte; /// number of byte char *buffer; /// readout buffer uint32_t NumEvents[MaxNChannels]; uint32_t AllocatedSize; @@ -40,35 +40,138 @@ class Data{ } ~Data(){ + delete buffer; for( int i = 0 ; i < MaxNChannels; i++){ delete Events [i]; delete Waveform [i]; } } - void AllocateMemory(int handle){ - int ret = CAEN_DGTZ_MallocReadoutBuffer(handle, &buffer, &AllocatedSize); /// output: buffer and allocatedSize - ret |= CAEN_DGTZ_MallocDPPEvents(handle, reinterpret_cast(&Events), &AllocatedSize) ; - for( int i = 0 ; i < MaxNChannels; i++){ - ret |= CAEN_DGTZ_MallocDPPWaveforms(handle, reinterpret_cast(&Waveform[i]), &AllocatedSize); - } - if (ret != 0) { - printf("Can't allocate memory buffers\n"); - CAEN_DGTZ_SWStopAcquisition(handle); - CAEN_DGTZ_CloseDigitizer(handle); - CAEN_DGTZ_FreeReadoutBuffer(&buffer); - CAEN_DGTZ_FreeDPPEvents(handle, reinterpret_cast(&Events)); - }else{ - printf("====== Allocated %d byte memory for data buffer.\n", AllocatedSize); - } + void AllocateMemory(){ + + BufferSize = 100000; /// byte + buffer = (char *) malloc( BufferSize); + printf("Allocated %d byte for buffer \n", BufferSize); + + for( int i = 0 ; i < MaxNChannels ; i++ ) Events[i] = (CAEN_DGTZ_DPP_PHA_Event_t *) malloc( BufferSize); + printf("Allocated %d byte for Events for each channel \n", BufferSize); + } - void FreeMemory(int handle){ - printf("======= Free memory \n"); - CAEN_DGTZ_FreeReadoutBuffer(&buffer); - CAEN_DGTZ_FreeDPPEvents(handle, reinterpret_cast(&Events)); - CAEN_DGTZ_FreeDPPWaveforms(handle, Waveform); + void FreeMemory(){ + printf("======= Free memory, not impletment yet \n"); } + + void ReadAllBuffer(){ + if( buffer == NULL ) return; + + unsigned int nw = 0; + + do{ + printf("#########################################\n"); + unsigned int word = ReadBuffer(nw); + if( ( (word >> 28) & 0xF ) == 0xA ) { /// start of Board Agg + unsigned int nWord = word & 0x0FFFFFFF ; + printf(" number of words in this Agg : %d \n", nWord); + + nw = nw + 1; word = ReadBuffer(nw); + unsigned int BoardID = ((word >> 27) & 0x1F); + bool BoardFailFlag = ((word >> 26) & 0x1 ); + unsigned int ChannelMask = ( word & 0xFF ) ; + printf("Board ID : %d, FailFlag = %d, ChannelMask = 0x%x\n", BoardID, BoardFailFlag, ChannelMask); + + nw = nw + 2; + unsigned int AggCounter = ReadBuffer(nw); + printf("Agg Counter : %d \n", AggCounter); + + printf("----------------------\n"); + + nw = nw + 1; + do{ + word = ReadBuffer(nw); + bool hasFormatInfo = ((word >> 31) & 0x1); + unsigned int aggSize = ( word & 0x3FFFFFF ) ; + printf(" size : %d \n", aggSize); + unsigned int nSample = 0; /// wave form; + unsigned int nEvents = 0; + if( hasFormatInfo ){ + nw = nw + 1; word = ReadBuffer(nw); + 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 ); + + printf("dualTrace : %d, Energy : %d, Time: %d, Wave : %d, Extra2: %d, Extra2Option: %d \n", + hasDualTrace, hasEnergy, hasTimeStamp, hasWaveForm, hasExtra2, extra2Option); + printf("Ana Probe 1 & 2: %d %d , Digi Probe: %d, nSample : %d \n", + analogProbe1, analogProbe2, digitalProbe, nSample); + + nEvents = aggSize / (nSample/2 + 2 + hasExtra2 ); + printf("=========== nEvents : %d \n", nEvents); + }else{ + printf("does not has format info. unable to read buffer.\n"); + break; + } + + for( int ev = 0; ev < nEvents ; ev++){ + printf("=================================== event : %d\n", ev); + nw = nw +1 ; word = ReadBuffer(nw); + bool channelTag = ((word >> 31) & 0x1); + unsigned int timeStamp = (word & 0x7FFFFFFF); + int channel = log(ChannelMask)/log(2) *2 + channelTag; + printf("ch : %d, timeStamp %u \n", channel, timeStamp); + + ///===== read waveform + for( int wi = 0; wi < nSample/2; wi++){ + nw = nw +1 ; word = ReadBuffer(nw, false); + bool isTrigger1 = (( word >> 31 ) & 0x1 ); + unsigned int wave1 = (( word >> 16) & 0x3FFF); + + bool isTrigger0 = (( word >> 15 ) & 0x1 ); + unsigned int wave0 = ( word & 0x3FFF); + + if( ev == 0 ){ + printf(" %5d, %d \n", wave0, isTrigger0); + printf(" %5d, %d \n", wave1, isTrigger1); + } + } + + nw = nw +1 ; word = ReadBuffer(nw); + unsigned int extra2 = word; + + nw = nw +1 ; word = ReadBuffer(nw); + unsigned int extra = (( word >> 16) & 0x3FF); + unsigned int energy = (word & 0x7FFF); + bool pileUp = ((word >> 15) & 0x1); + + printf("PileUp : %d , extra : 0x%4x, energy : %d \n", pileUp, extra, energy); + + } + + nw ++; + }while( nw < nWord); + }else{ + printf("incorrect buffer header. \n"); + break; + } + }while(true); + } + + unsigned int ReadBuffer(unsigned int nWord, bool verbose = true){ + if( buffer == NULL ) return 0; + + unsigned int word = 0; + for( int i = 0 ; i < 4 ; i++) word += ((buffer[i + 4 * nWord] & 0xFF) << 8*i); + if( verbose) printf("%d | 0x%08x\n", nWord, word); + return word; + } + }; diff --git a/ClassDigitizer.cpp b/ClassDigitizer.cpp index ced9aa9..91b8ece 100644 --- a/ClassDigitizer.cpp +++ b/ClassDigitizer.cpp @@ -186,7 +186,7 @@ int Digitizer::CloseDigitizer(){ ret = CAEN_DGTZ_SWStopAcquisition(handle); ret |= CAEN_DGTZ_CloseDigitizer(handle); - data->FreeMemory(handle); + data->FreeMemory(); return ret; } @@ -277,7 +277,7 @@ int Digitizer::ProgramBoard(){ /** Set the mode used to syncronize the acquisition between different boards. In this example the sync is disabled */ - ret |= CAEN_DGTZ_SetRunSynchronizationMode(handle, CAEN_DGTZ_RUN_SYNC_Disabled); + ret |= CAEN_DGTZ_SetRunSynchronizationMode(handle, CAEN_DGTZ_RUN_SYNC_Disabled); ErrorMsg("End of ProgramBoard"); return ret; @@ -359,58 +359,40 @@ unsigned int Digitizer::CalByteForBuffer(){ void Digitizer::ReadData(){ - /// The Event Aggreation defined how many events in one ReadData. when 0 = automatics - - uint32_t BufferSize = 10000000; - - //char buffer[10000]; - char *buffer; - - buffer = (char *) malloc( BufferSize); - //ret = CAEN_DGTZ_MallocReadoutBuffer(handle, &buffer, &BufferSize); - printf("Allocated %d byte for buffer \n", BufferSize); - //ErrorMsg("CAEN_DGTZ_MallocReadoutBuffer"); - - CAEN_DGTZ_DPP_PHA_Event_t * Events[MaxNChannels]; /// events buffer - for( int i = 0 ; i < MaxNChannels ; i++ ) Events[i] = (CAEN_DGTZ_DPP_PHA_Event_t *) malloc( BufferSize); - //ret = CAEN_DGTZ_MallocDPPEvents(handle, reinterpret_cast(&Events), &BufferSize) ; - printf("Allocated %d byte for Events \n", BufferSize); - //ErrorMsg("CAEN_DGTZ_MallocReadoutBuffer"); - - ret = CAEN_DGTZ_ReadData(handle, CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT, buffer, &BufferSize); - printf("Read Buffer size %d byte \n", BufferSize); - if (ret) { - ErrorMsg("ReadData"); + if( data->buffer == NULL ) { + printf("need allocate memory for readout buffer\n"); return; } - data->nByte = BufferSize; - if (data->nByte == 0 ) { - return; - } - uint32_t NumEvents[MaxNChannels]; - ret = (CAEN_DGTZ_ErrorCode) CAEN_DGTZ_GetDPPEvents(handle, buffer, BufferSize, reinterpret_cast(&Events), NumEvents); + //ret = CAEN_DGTZ_ReadData(handle, CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT, data->buffer, &(data->nByte)); + //printf("Read Buffer size %d byte \n", data->nByte); + //if (ret || data->nByte == 0) { + // ErrorMsg("ReadData"); + // return; + //} + + ret = (CAEN_DGTZ_ErrorCode) CAEN_DGTZ_GetDPPEvents(handle, data->buffer, data->BufferSize, reinterpret_cast(&(data->Events)), data->NumEvents); for( int i = 0 ; i < MaxNChannels; i++){ - printf(" extracted %d events in ch-%02d\n", NumEvents[i], i); + printf(" extracted %d events in ch-%02d\n", data->NumEvents[i], i); } ErrorMsg("GetDPPEvents"); printf("================================================\n"); ///========== print events for( int ch = 0; ch < NChannel; ch++){ - printf("--------- ch - %d, %d\n", ch, NumEvents[ch]); - for( int ev = 0; ev < NumEvents[ch]; ev++){ - printf("%4d, %lu, %d, %d, %d \n", ev, Events[ch][ev].TimeTag, - Events[ch][ev].Energy, - Events[ch][ev].Extras, - Events[ch][ev].Extras2); + printf("--------- ch - %d, %d\n", ch, data->NumEvents[ch]); + for( int ev = 0; ev < data->NumEvents[ch]; ev++){ + printf("%4d, %lu, %d, %d, %d \n", ev, (data->Events[ch][ev]).TimeTag, + (data->Events[ch][ev]).Energy, + (data->Events[ch][ev]).Extras, + (data->Events[ch][ev]).Extras2); } } FILE * haha = fopen("output.bin", "w+"); - fwrite(buffer, BufferSize, 1, haha); + fwrite(data->buffer, data->nByte, 1, haha); fclose(haha); } @@ -475,7 +457,7 @@ void Digitizer::SetMaxNumberOfAggregatePerBlackTransfer(unsigned int numEvent){ void Digitizer::SetInputDynamicRange(unsigned int TwoVol_0_or_halfVol_1, int ch){ WriteRegister( Register::DPP::InputDynamicRange, TwoVol_0_or_halfVol_1, ch); ErrorMsg("SetInputDynamicRange");} void Digitizer::SetPreTriggerSample(unsigned int nSample, int ch) { WriteRegister( Register::DPP::PreTrigger, nSample / 4, ch); ErrorMsg("SetPreTriggerSample");} void Digitizer::SetPreTriggerDuration(unsigned int ns, int ch) { WriteRegister( Register::DPP::PreTrigger, ns / ch2ns / 4, ch); ErrorMsg("SetPreTriggerSample");} -void Digitizer::SetDCOffset(float offsetPrecentage, int ch) { WriteRegister( Register::DPP::ChannelDCOffset, uint( ADCFullSize * offsetPrecentage), ch ); ErrorMsg("SetDCOffset");} +void Digitizer::SetDCOffset(float offsetPrecentage, int ch) { WriteRegister( Register::DPP::ChannelDCOffset, uint( 0xFFFF * (1.0-offsetPrecentage)), ch ); ErrorMsg("SetDCOffset");} void Digitizer::SetVetoWidth(uint32_t bit, int ch) { WriteRegister( Register::DPP::VetoWidth, bit, ch); ErrorMsg("SetVetoWidth");} void Digitizer::SetTriggerPolarity(bool RiseingIsZero, int ch ){ diff --git a/DigitizerPHA.cpp b/DigitizerPHA.cpp index 5a2a3a5..78445c8 100644 --- a/DigitizerPHA.cpp +++ b/DigitizerPHA.cpp @@ -24,36 +24,127 @@ int DigitizerPHA::ProgramBoard(){ /// record energy /// digitial virtual probe 1 = Peaking /// digitial virtual probe 2 = trigger - ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::BoardConfiguration , 0x000F8114); - + ///ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::BoardConfiguration , 0x000F8114); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::BoardConfiguration , 0x014F8905); ErrorMsg("PHA-ProgramBoard"); - SetRecordLength(2000); - SetPreTriggerDuration(500); + + /* + ///==================== Set default Channel setting using CAEN function + CAEN_DGTZ_DPP_PHA_Params_t DPPParams; + memset(&DPPParams, 0, sizeof(CAEN_DGTZ_DPP_PHA_Params_t)); + + for(int i = 0; i < NChannel; i++){ + DPPParams.M[i] = 100000; /// decay time [ns] + DPPParams.m[i] = 992; /// flat-top [ns] + DPPParams.k[i] = 96; /// rise-time [ns] + DPPParams.ftd[i] = 192; /// flat-top delay, peaking time [ns] + DPPParams.a[i] = 4; /// Trigger Filter smoothing factor, 1, 2, 3, 4, 16, 32 + DPPParams.b[i] = 96; /// input rise time [ns] + DPPParams.thr[i] = 100; /// Threshold [LSB] + DPPParams.nsbl[i] = 3; /// Baseline samples, 0 = 0, when > 0, pow(4, n+1) + DPPParams.nspk[i] = 2; /// peak samples, 4^n + DPPParams.pkho[i] = 992 ; /// peak hold off [ns] + DPPParams.trgho[i] = 480 ; /// trigger hold off [ns] + DPPParams.twwdt[i] = 0 ; /// rise time validation window, 0x1070 + DPPParams.trgwin[i] = 0 ; /// trigger coincident window + DPPParams.dgain[i] = 0; /// digial gain for digial probe, 2^n + DPPParams.enf[i] = 1 ; /// energy normalization factor (fine gain?) + DPPParams.decimation[i] = 0 ; /// waveform decimation, 2^n, when n = 0, disable + DPPParams.blho[i] = 0; /// not use + } + + ret = CAEN_DGTZ_SetDPPParameters(handle, channelMask, &DPPParams); + ErrorMsg("PHA-CAEN_DGTZ_SetDPPParameters"); + + for(int i=0; ibuffer), &(data->AllocatedSize)); + //ret |= CAEN_DGTZ_MallocDPPEvents(handle, reinterpret_cast(&(data->Events)), &(data->AllocatedSize)) ; + + + //WriteRegister(Register::DPP::RecordLength_G, 0x138); + //WriteRegister(Register::DPP::InputDynamicRange, 0x0); + //WriteRegister(Register::DPP::NumberEventsPerAggregate_G, 0x1A); + //WriteRegister(Register::DPP::PreTrigger, 0x1F); + // + //WriteRegister(Register::DPP::PHA::RCCR2SmoothingFactor, 0x2); + //WriteRegister(Register::DPP::PHA::InputRiseTime, 0x6); + //WriteRegister(Register::DPP::PHA::TrapezoidRiseTime, 0x6); + //WriteRegister(Register::DPP::PHA::TrapezoidFlatTop, 0x3E); + //WriteRegister(Register::DPP::PHA::PeakingTime, 0xC); + //WriteRegister(Register::DPP::PHA::DecayTime, 0x186A); + //WriteRegister(Register::DPP::PHA::TriggerThreshold, 0x64); + //WriteRegister(Register::DPP::PHA::RiseTimeValidationWindow, 0x0); + //WriteRegister(Register::DPP::PHA::TriggerHoldOffWidth, 0x1E); + //WriteRegister(Register::DPP::PHA::PeakHoldOff, 0x3E); + //WriteRegister(Register::DPP::DPPAlgorithmControl, 0x0C30200F); + //WriteRegister(Register::DPP::PHA::ShapedTriggerWidth, 0x6); + //WriteRegister(Register::DPP::ChannelDCOffset, 0xCCCC); + //WriteRegister(Register::DPP::PHA::DPPAlgorithmControl2_G, 0x0); + //WriteRegister(Register::DPP::PHA::FineGain, 0xDFB1); + //WriteRegister(Register::DPP::VetoWidth, 0xA); + // + // + //WriteRegister(Register::DPP::BoardConfiguration, 0x14F8905); + //WriteRegister(Register::DPP::AggregateOrganization, 0x2); + //WriteRegister(Register::DPP::AcquisitionControl, 0x0); + //WriteRegister(Register::DPP::GlobalTriggerMask, 0x80000000); + //WriteRegister(Register::DPP::ReadoutControl, 0x10); + + SetRecordLength(4096*4); + SetPreTriggerDuration(1000*4); SetBaselineSampling(3); SetDCOffset(0.2); SetInputDynamicRange(0); SetPulsePolarity(0); - SetTriggerThreshold(64); - SetTriggerHoldOff(30); + SetTriggerThreshold(400); + SetTriggerHoldOff(248); SetTriggerSmoothingFactor(2); SetTriggerOutputWidth(6); SetInputRiseTime(6); //SetTrapezoidRescaling(23); - SetTrapezoidRiseTime(6); - SetTrapezoidFlatTop(62); - AutoSetTrapezoidRescaling(); - SetDecayTime(3125); - SetPeakingTime(12); - SetPeakingHoldOff(62); - SetPeakSampling(2); - - SetEnergyFineGain(0xDFB1); + SetTrapezoidRiseTime(31); + SetTrapezoidFlatTop(31); + + SetDecayTime(312); + SetPeakingTime(3); + SetPeakingHoldOff(25); + SetPeakSampling(8); + SetRiseTimeValidWindow(0); - SetEventAggregation(10); /// max 511 + SetTrapezoidRescaling(13); + SetEnergyFineGain(30000); + + //AutoSetTrapezoidRescalingAndFindGate(1.0); + + //SetDPPAlgorithmControl(0xC30200E); + //SetEnergyFineGain(0xDFB1); + + SetEventAggregation(2); /// max 511 SetMaxNumberOfAggregatePerBlackTransfer(4); SetAggregateOrganization(0); ///0 or 1 = no division in buffer, n = 2^n divsions @@ -98,7 +189,7 @@ void DigitizerPHA::SetPeakingHoldOff(unsigned int nSample, int ch ) { WriteR void DigitizerPHA::SetEnergyFineGain(unsigned int gain, int ch ) { WriteRegister(Register::DPP::PHA::FineGain, gain & 0xFFFF, ch); ErrorMsg("PHA-SetEnergyFineGain");} void DigitizerPHA::SetRiseTimeValidWindow(unsigned int nSample, int ch ){ WriteRegister(Register::DPP::PHA::RiseTimeValidationWindow,nSample & 0x03FF, ch); ErrorMsg("PHA-SetRiseTimeValidWindow");} -void DigitizerPHA::AutoSetTrapezoidRescaling(int ch){ +void DigitizerPHA::AutoSetTrapezoidRescalingAndFindGate(double gain, int ch){ int startCh = 0; int endCh = MaxNChannels; @@ -110,10 +201,18 @@ void DigitizerPHA::AutoSetTrapezoidRescaling(int ch){ for( int i = startCh ; i <= endCh; i++){ double riseTime = ReadRegister(Register::DPP::PHA::TrapezoidRiseTime, i) * 4 * ch2ns; double flatTop = ReadRegister(Register::DPP::PHA::TrapezoidFlatTop, i) * 4 * ch2ns; - unsigned int shift = (unsigned int ) ( log(riseTime * flatTop ) / log(2.0)); + unsigned int shift = (unsigned int ) ( log(riseTime * flatTop ) / log(2.0)) ; SetTrapezoidRescaling(shift, i); + + double haha = 0xFFFF * gain * (pow(2, shift) / riseTime / flatTop); + unsigned int fineGain = (unsigned int) (haha); + if( fineGain > 0xFFFF ){ + SetEnergyFineGain(0xFFFF, i); + }else{ + SetEnergyFineGain(fineGain, i); + } } - ErrorMsg("SetTrapezoidRescaling"); + ErrorMsg("PHA-AutoSetTrapezoidRescalingAndFindGate"); } @@ -200,7 +299,7 @@ void DigitizerPHA::PrintChannelSettingFromDigitizer(int ch){ CAEN_DGTZ_ReadRegister(handle, Register::DPP::RecordLength_G + (ch << 8), value); printf("%24s %5d samples = %5.0f ns \n", "Record Length", ((value[0] * 8) & MaxRecordLength), ((value[0] * 8) & MaxRecordLength) * ch2ns); ///Record length CAEN_DGTZ_ReadRegister(handle, Register::DPP::PreTrigger + (ch << 8), value); printf("%24s %5d samples = %5.0f ns \n", "Pre-tigger", value[0] * 4, value[0] * 4 * ch2ns); ///Pre-trigger printf("%24s %5.0f samples, DPP-[20:22]\n", "baseline mean", pow(4, 1 + baseline)); ///Ns baseline - CAEN_DGTZ_ReadRegister(handle, Register::DPP::ChannelDCOffset + (ch << 8), value); printf("%24s %.2f %% of %d\n", "DC offset", value[0] * 100./ ADCFullSize, ADCFullSize); ///DC offset + CAEN_DGTZ_ReadRegister(handle, Register::DPP::ChannelDCOffset + (ch << 8), value); printf("%24s %.2f %% \n", "DC offset", 100.0 - value[0] * 100./ 0xFFFF); ///DC offset CAEN_DGTZ_ReadRegister(handle, Register::DPP::InputDynamicRange + (ch << 8), value); printf("%24s %.1f Vpp \n", "input Dynamic", value[0] == 0 ? 2 : 0.5); ///InputDynamic printf("%24s %s, DPP-[16]\n", "polarity", polarity == 0 ? "Positive" : "negative"); ///Polarity @@ -216,7 +315,7 @@ void DigitizerPHA::PrintChannelSettingFromDigitizer(int ch){ int riseTime = value[0] * 4 * ch2ns; CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::TrapezoidFlatTop + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "Trap. flat time", value[0], value[0] * 4 * ch2ns); ///Trap. flat time int flatTopTime = value[0] * 4 * ch2ns; - double shift = log(riseTime * flatTopTime ) / log(2); + double shift = log(riseTime * flatTopTime ) / log(2) - 2; printf("%24s %4d bit =? %.1f = Ceil( Log(rise [ns] x decay [ns])/Log(2) ), DPP-[0:5]\n", "Trap. Rescaling", trapRescaling, shift ); ///Trap. Rescaling Factor CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::DecayTime + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "Decay time", value[0], value[0] * 4 * ch2ns); ///Trap. pole zero CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::PeakingTime + (ch << 8), value); printf("%24s %4d samples, %5.0f ns = %.2f %% of FlatTop\n", "Peaking time", value[0], value[0] * 4 * ch2ns, value[0] * 400. * ch2ns / flatTopTime ); ///Peaking time @@ -224,7 +323,7 @@ void DigitizerPHA::PrintChannelSettingFromDigitizer(int ch){ printf("%24s %4.0f samples, DPP-[12:13]\n", "Peak mean", pow(4, NsPeak)); ///Ns peak printf("==========----- Other \n"); - CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::FineGain + (ch << 8), value); printf("%24s %d \n", "Energy fine gain", value[0]); ///Energy fine gain + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::FineGain + (ch << 8), value); printf("%24s %d = 0x%x\n", "Energy fine gain", value[0], value[0]); ///Energy fine gain CAEN_DGTZ_ReadRegister(handle, Register::DPP::ChannelADCTemperature + (ch << 8), value); printf("%24s %d C\n", "Temperature", value[0]); ///Temperature CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::RiseTimeValidationWindow + (ch << 8), value); printf("%24s %.0f ns \n", "RiseTime Vaild Win.", value[0] * ch2ns); CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::ChannelStopAcquisition + (ch << 8), value); printf("%24s %d = %s \n", "Stop Acq bit", value[0] & 1 , (value[0] & 1 ) == 0 ? "Run" : "Stop"); diff --git a/DigitizerPHA.h b/DigitizerPHA.h index 0cb11ad..d581242 100644 --- a/DigitizerPHA.h +++ b/DigitizerPHA.h @@ -18,9 +18,10 @@ class DigitizerPHA : public Digitizer { void SetTimeStampRecording(bool OnOff); void SetEnergyRecording(bool OnOff); void SetVirtualProbe1(unsigned int bit); + + void AutoSetTrapezoidRescalingAndFindGate(double gain = 1, int ch = -1); void SetTrapezoidRescaling(unsigned int rightShiftBits, int ch = -1); /// DPPAlgoritmControl bit-0:5 - void AutoSetTrapezoidRescaling(int ch = -1); /// Set the rescalling from the risetime and flattop void SetPeakSampling(unsigned int bit, int ch = -1); /// DPPAlgoritmControl bit-10:11 void SetPulsePolarity(bool PositiveIsZero, int ch = -1); /// DPPAlgoritmControl bit-16 void SetBaselineSampling(unsigned int bit, int ch = -1); /// DPPAlgoritmControl bit-20:22 diff --git a/Makefile b/Makefile index b39f199..db7af09 100755 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ OBJS = ClassDigitizer.o DigitizerPHA.o DigitizerPSD.o FSUDAQ.o ######################################################################### -all : test FSUDAQ +all : test FSUDAQ test_indep clean : /bin/rm -f $(OBJS) test FSUDAQ FSUDAQDict.cxx *.pcm @@ -37,6 +37,9 @@ test : test.cpp ClassDigitizer.o DigitizerPHA.o DigitizerPSD.o @echo "--------- making test" $(CC) $(COPTS) -o test test.cpp ClassDigitizer.o DigitizerPHA.o DigitizerPSD.o $(CAENLIBS) +test_indep : test_indep.cpp RegisterAddress.h macro.h + $(CC) $(COPTS) -o test_indep test_indep.cpp $(CAENLIBS) + FSUDAQDict.cxx : FSUDAQ.h FSUDAQLinkDef.h @echo "----------- creating pcm and cxx for root" diff --git a/test.cpp b/test.cpp index 6033cb6..d2d6e94 100644 --- a/test.cpp +++ b/test.cpp @@ -20,12 +20,12 @@ int main(int argc, char* argv[]){ //dig[0].WriteRegister(Register::DPP::FrontPanelIOControl, 0x100, -1); //dig[0].WriteRegister(Register::DPP::TriggerValidationMask, 0, -1); //dig[0].SetDPPAlgorithmControl(0x830200E); - dig[0].SetInputDynamicRange(2); - dig[0].SetTriggerThreshold(20); + //dig[0].SetInputDynamicRange(2); + //dig[0].SetTriggerThreshold(20); //dig[0].SetChannelMask(0x8000); /// only channel 15 //dig[0].SetChannelMask(0xffff); /// all channels - dig[0].SetWaveFormRecording(false); + //dig[0].SetWaveFormRecording(true); //dig[0].SetPileUpFlag(false); //dig[0].SetBits(Register::DPP::BoardConfiguration, 0x0101, 4, 20); // TRG validation window @@ -48,10 +48,8 @@ int main(int argc, char* argv[]){ //dig[0].PrintChannelSettingFromDigitizer(15); - //Data * data = dig[0].data; - - - //data->AllocateMemory(dig[0].GetHandle()); + Data * data = dig[0].data; + data->AllocateMemory(); dig[0].StartACQ(); @@ -63,8 +61,8 @@ int main(int argc, char* argv[]){ dig[0].StopACQ(); - - + data->ReadAllBuffer(); + delete [] dig; //delete psd; diff --git a/test_indep.cpp b/test_indep.cpp new file mode 100644 index 0000000..dc396ce --- /dev/null +++ b/test_indep.cpp @@ -0,0 +1,413 @@ +#include +#include +#include +#include +#include ///memset +#include ///cout +#include + +#include "CAENDigitizer.h" +#include "CAENDigitizerType.h" +#include "macro.h" +#include "RegisterAddress.h" + +using namespace std; + +void PrintChannelSettingFromDigitizer(int handle, int ch, float ch2ns){ + + printf("\e[33m================================================\n"); + printf("================ Setting for channel %d \n", ch); + printf("================================================\e[0m\n"); + ///DPP algorithm Control + uint32_t * value = new uint32_t[16]; + CAEN_DGTZ_ReadRegister(handle, Register::DPP::DPPAlgorithmControl + (ch << 8), value); + printf(" 32 28 24 20 16 12 8 4 0\n"); + printf(" | | | | | | | | |\n"); + cout <<" DPP algorithm Control : 0b" << bitset<32>(value[0]); + printf(" = 0x%x\n", value[0]); + + int trapRescaling = int(value[0]) & 0x1f ; + int polarity = int(value[0] >> 16) & 0x1; /// in bit[16] + int baseline = int(value[0] >> 20) & 0x7; /// in bit[22:20] + int NsPeak = int(value[0] >> 12) & 0x3; /// in bit[13:12] + int rollOver = int(value[0] >> 26) & 0x1; + int pileUp = int(value[0] >> 27) & 0x1; + + ///DPP algorithm Control 2 + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::DPPAlgorithmControl2_G + (ch << 8), value); + cout <<" DPP algorithm Control 2: 0b" << bitset<32>(value[0]) ; + printf(" = 0x%x\n", value[0]); + + int extras2WordOption = int(value[0] >> 8) & 0x3; + string extra2WordOptStr = ""; + switch (extras2WordOption){ + case 0 : extra2WordOptStr = "[0:15] Baseline *4 [16:31] Extended Time Stamp"; break; + case 2 : extra2WordOptStr = "[0:9] Fine Time Stamp [10:15] Reserved [16:31] Extended Time Stamp"; break; + case 4 : extra2WordOptStr = "[0:15] Total Trigger Counter [16:31] Lost Trigger Counter"; break; + case 5 : extra2WordOptStr = "[0:15] Event After the Zero Crossing [16:31] Event Before the Zero Crossing"; break; + default: extra2WordOptStr = "Reserved"; break; + } + + printf(" ch2ns : %.0f ns\n", ch2ns); + + printf("==========----- input \n"); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::RecordLength_G + (ch << 8), value); printf("%24s %5d samples = %5.0f ns \n", "Record Length", ((value[0] * 8) & MaxRecordLength), ((value[0] * 8) & MaxRecordLength) * ch2ns); ///Record length + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PreTrigger + (ch << 8), value); printf("%24s %5d samples = %5.0f ns \n", "Pre-tigger", value[0] * 4, value[0] * 4 * ch2ns); ///Pre-trigger + printf("%24s %5.0f samples, DPP-[20:22]\n", "baseline mean", pow(4, 1 + baseline)); ///Ns baseline + CAEN_DGTZ_ReadRegister(handle, Register::DPP::ChannelDCOffset + (ch << 8), value); printf("%24s %.2f %% \n", "DC offset", 100.0 - value[0] * 100./ 0xFFFF); ///DC offset + CAEN_DGTZ_ReadRegister(handle, Register::DPP::InputDynamicRange + (ch << 8), value); printf("%24s %.1f Vpp \n", "input Dynamic", value[0] == 0 ? 2 : 0.5); ///InputDynamic + printf("%24s %s, DPP-[16]\n", "polarity", polarity == 0 ? "Positive" : "negative"); ///Polarity + + printf("==========----- discriminator \n"); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::TriggerThreshold + (ch << 8), value); printf("%24s %4d LSB\n", "Threshold", value[0]); ///Threshold + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::TriggerHoldOffWidth + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "trigger hold off", value[0], value[0] * 4 * ch2ns); ///Trigger Hold off + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::RCCR2SmoothingFactor + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "Fast Dis. smoothing", (value[0] & 0x1f) * 2, (value[0] & 0x1f) * 2 * ch2ns ); ///Fast Discriminator smoothing + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::ShapedTriggerWidth + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "Fast Dis. output width", value[0], value[0] * 4 * ch2ns); ///Fast Dis. output width + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::InputRiseTime + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "Input rise time ", value[0], value[0] * 4 * ch2ns); ///Input rise time + + printf("==========----- Trapezoid \n"); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::TrapezoidRiseTime + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "Trap. rise time", value[0], value[0] * 4 * ch2ns); ///Trap. rise time, 2 for 1 ch to 2ns + int riseTime = value[0] * 4 * ch2ns; + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::TrapezoidFlatTop + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "Trap. flat time", value[0], value[0] * 4 * ch2ns); ///Trap. flat time + int flatTopTime = value[0] * 4 * ch2ns; + double shift = log(riseTime * flatTopTime ) / log(2) - 2; + printf("%24s %4d bit =? %.1f = Ceil( Log(rise [ns] x decay [ns])/Log(2) ), DPP-[0:5]\n", "Trap. Rescaling", trapRescaling, shift ); ///Trap. Rescaling Factor + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::DecayTime + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "Decay time", value[0], value[0] * 4 * ch2ns); ///Trap. pole zero + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::PeakingTime + (ch << 8), value); printf("%24s %4d samples, %5.0f ns = %.2f %% of FlatTop\n", "Peaking time", value[0], value[0] * 4 * ch2ns, value[0] * 400. * ch2ns / flatTopTime ); ///Peaking time + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::PeakHoldOff + (ch << 8), value); printf("%24s %4d samples, %5.0f ns \n", "Peak hole off", value[0], value[0] * 4 *ch2ns ); ///Peak hold off + printf("%24s %4.0f samples, DPP-[12:13]\n", "Peak mean", pow(4, NsPeak)); ///Ns peak + + printf("==========----- Other \n"); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::FineGain + (ch << 8), value); printf("%24s %d = 0x%x\n", "Energy fine gain", value[0], value[0]); ///Energy fine gain + CAEN_DGTZ_ReadRegister(handle, Register::DPP::ChannelADCTemperature + (ch << 8), value); printf("%24s %d C\n", "Temperature", value[0]); ///Temperature + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::RiseTimeValidationWindow + (ch << 8), value); printf("%24s %.0f ns \n", "RiseTime Vaild Win.", value[0] * ch2ns); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::PHA::ChannelStopAcquisition + (ch << 8), value); printf("%24s %d = %s \n", "Stop Acq bit", value[0] & 1 , (value[0] & 1 ) == 0 ? "Run" : "Stop"); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::ChannelStatus + (ch << 8), value); printf("%24s 0x%x \n", "Status bit", (value[0] & 0xff) ); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::AMCFirmwareRevision + (ch << 8), value); printf("%24s 0x%x \n", "AMC firmware rev.", value[0] ); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::VetoWidth + (ch << 8), value); printf("%24s 0x%x \n", "VetoWidth bit", value[0] ); + printf("%24s %d = %s\n", "RollOverFlag, DPP-[26]", rollOver, rollOver ? "enable" : "disable" ); + printf("%24s %d = %s\n", "Pile-upFlag, DPP-[27]", pileUp, pileUp ? "enable" : "disable" ); + printf("%24s %d, %s \n", "Extra2 opt, DPP2-[8:10]", extras2WordOption, extra2WordOptStr.c_str()); + printf("========= events storage and transfer\n"); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::NumberEventsPerAggregate_G + (ch << 8), value); printf("%24s %d \n", "Event Aggregate", value[0] & 0x3FF); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::AggregateOrganization, value); printf("%24s %d \n", "Buffer Division", ((value[0] & 0x007) < 2 ? 0 : (int)pow(2, value[0] & 7))); + CAEN_DGTZ_ReadRegister(handle, Register::DPP::MaxNumberOfAggregatePerBlackTransfer , value); printf("%24s %d \n", "Num of Agg. / ReadData", value[0] & 0x1FF); + + printf("========================================= end of ch-%d\n", ch); + +} + + +unsigned int ReadBuffer(unsigned int nWord, char * buffer, bool verbose = true){ + if( buffer == NULL ) return 0; + + unsigned int word = 0; + for( int i = 0 ; i < 4 ; i++) word += ((buffer[i + 4 * nWord] & 0xFF) << 8*i); + if( verbose) printf("%d | 0x%08x\n", nWord, word); + return word; +} + + + +int main(int argc, char* argv[]){ + + + ///============== open digitizer + int handle; + + printf("======== open board\n"); + int ret = CAEN_DGTZ_OpenDigitizer(CAEN_DGTZ_OpticalLink, 0, 0, 0, &handle); + + CAEN_DGTZ_BoardInfo_t BoardInfo; + ret = (int) CAEN_DGTZ_GetInfo(handle, &BoardInfo); + int NChannel = BoardInfo.Channels; + uint32_t channelMask = 0xFFFF; + float ch2ns = 4.0; + switch(BoardInfo.Model){ + case CAEN_DGTZ_V1730: ch2ns = 2.0; break; ///ns -> 500 MSamples/s + case CAEN_DGTZ_V1725: ch2ns = 4.0; break; ///ns -> 250 MSamples/s + } + unsigned int ADCbits = BoardInfo.ADC_NBits; + + if( ret != 0 ) { printf("==== open digitizer\n"); return 0;} + + ///======= reset + ret = CAEN_DGTZ_Reset(handle); + + printf("======== program board\n"); + + //ret |= CAEN_DGTZ_SetDPPAcquisitionMode(handle, CAEN_DGTZ_DPP_ACQ_MODE_List, CAEN_DGTZ_DPP_SAVE_PARAM_EnergyAndTime); + ret |= CAEN_DGTZ_SetDPPAcquisitionMode(handle, CAEN_DGTZ_DPP_ACQ_MODE_Mixed, CAEN_DGTZ_DPP_SAVE_PARAM_EnergyAndTime); + + ret |= CAEN_DGTZ_WriteRegister(handle, 0x8000, 0x01000114); /// Channel Control Reg (indiv trg, seq readout) ?? + + /// Set the number of samples for each waveform + ret |= CAEN_DGTZ_SetRecordLength(handle, 1000); + if( ret != 0 ) { printf("==== set Record Length.\n"); return 0;} + + /// Set the digitizer acquisition mode (CAEN_DGTZ_SW_CONTROLLED or CAEN_DGTZ_S_IN_CONTROLLED) + ret = CAEN_DGTZ_SetAcquisitionMode(handle, CAEN_DGTZ_SW_CONTROLLED); /// software command + + /// Set the I/O level (CAEN_DGTZ_IOLevel_NIM or CAEN_DGTZ_IOLevel_TTL) + ret |= CAEN_DGTZ_SetIOLevel(handle, CAEN_DGTZ_IOLevel_NIM); + + /** Set the digitizer's behaviour when an external trigger arrives: + CAEN_DGTZ_TRGMODE_DISABLED: do nothing + CAEN_DGTZ_TRGMODE_EXTOUT_ONLY: generate the Trigger Output signal + CAEN_DGTZ_TRGMODE_ACQ_ONLY = generate acquisition trigger + CAEN_DGTZ_TRGMODE_ACQ_AND_EXTOUT = generate both Trigger Output and acquisition trigger + see CAENDigitizer user manual, chapter "Trigger configuration" for details */ + ret |= CAEN_DGTZ_SetExtTriggerInputMode(handle, CAEN_DGTZ_TRGMODE_ACQ_ONLY); + if( ret != 0 ) { printf("==== CAEN_DGTZ_SetExtTriggerInputMode.\n"); return 0;} + + ret = CAEN_DGTZ_SetChannelEnableMask(handle, 0xFFFF); + if( ret != 0 ) { printf("==== CAEN_DGTZ_SetChannelEnableMask.\n"); return 0;} + + ret = CAEN_DGTZ_SetNumEventsPerAggregate(handle, 0); + if( ret != 0 ) { printf("==== CAEN_DGTZ_SetNumEventsPerAggregate. %d\n", ret); return 0;} + + //ret = CAEN_DGTZ_SetDPPEventAggregation(handle, 0, 0); + //if( ret != 0 ) { printf("==== CAEN_DGTZ_SetDPPEventAggregation. %d\n", ret); return 0;} + + + /** Set the mode used to syncronize the acquisition between different boards. + In this example the sync is disabled */ + ret = CAEN_DGTZ_SetRunSynchronizationMode(handle, CAEN_DGTZ_RUN_SYNC_Disabled); + if( ret != 0 ) { printf("==== set board error.\n"); return 0;} + + printf("======== program Channels\n"); + ///CAEN_DGTZ_DPP_PHA_Params_t DPPParams; + ///memset(&DPPParams, 0, sizeof(CAEN_DGTZ_DPP_PHA_Params_t)); + ///for(int i = 0; i < NChannel; i++){ + /// DPPParams.M[i] = 5000; /// decay time [ns] + /// DPPParams.m[i] = 992; /// flat-top [ns] + /// DPPParams.k[i] = 96; /// rise-time [ns] + /// DPPParams.ftd[i] = 192; /// flat-top delay, peaking time [ns] + /// DPPParams.a[i] = 4; /// Trigger Filter smoothing factor, 1, 2, 3, 4, 16, 32 + /// DPPParams.b[i] = 96; /// input rise time [ns] + /// DPPParams.thr[i] = 100; /// Threshold [LSB] + /// DPPParams.nsbl[i] = 3; /// Baseline samples, 0 = 0, when > 0, pow(4, n+1) /// in DPP Control + /// DPPParams.nspk[i] = 2; /// peak samples, 4^n /// in DPP Control + /// DPPParams.pkho[i] = 992 ; /// peak hold off [ns] + /// DPPParams.trgho[i] = 480 ; /// trigger hold off [ns] + /// DPPParams.twwdt[i] = 0 ; /// rise time validation window, 0x1070 + /// DPPParams.trgwin[i] = 0 ; /// trigger coincident window + /// DPPParams.dgain[i] = 0; /// digial gain for digial probe, 2^n + /// DPPParams.enf[i] = 1 ; /// energy normalization factor (fine gain?) + /// DPPParams.decimation[i] = 0 ; /// waveform decimation, 2^n, when n = 0, disable + /// DPPParams.blho[i] = 0; /// not use + ///} + ///ret = CAEN_DGTZ_SetDPPParameters(handle, channelMask, &DPPParams); + + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::DecayTime + 0x7000 , 5000 ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::TrapezoidFlatTop + 0x7000 , 62 ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::TrapezoidRiseTime + 0x7000 , 6 ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::PeakingTime + 0x7000 , 6 ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::RCCR2SmoothingFactor + 0x7000 , 4 ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::InputRiseTime + 0x7000 , 6 ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::TriggerThreshold + 0x7000 , 64 ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::PeakHoldOff + 0x7000 , 0x3E ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::TriggerHoldOffWidth + 0x7000 , 0x3E ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PHA::RiseTimeValidationWindow + 0x7000 , 0x0 ); + + + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::ChannelDCOffset + 0x7000 , 0xEEEE ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::PreTrigger + 0x7000 , 124 ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::InputDynamicRange + 0x7000 , 0x0 ); + + + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::BoardConfiguration , 0x10E0114 ); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::NumberEventsPerAggregate_G + 0x7000, 5); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::AggregateOrganization, 0); + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::MaxNumberOfAggregatePerBlackTransfer, 40); + + ret |= CAEN_DGTZ_WriteRegister(handle, Register::DPP::DPPAlgorithmControl + 0x7000, 0xe30200f); + + + if( ret != 0 ) { printf("==== set channels error.\n"); return 0;} + + printf("================ allowcate memory \n"); + + int Nb; /// number of byte + char *buffer = NULL; /// readout buffer + uint32_t NumEvents[MaxNChannels]; + uint32_t AllocatedSize, BufferSize; + CAEN_DGTZ_DPP_PHA_Event_t *Events[MaxNChannels]; /// events buffer + CAEN_DGTZ_DPP_PHA_Waveforms_t *Waveform[MaxNChannels]; /// waveforms buffer + + ret = CAEN_DGTZ_MallocReadoutBuffer(handle, &buffer, &AllocatedSize); + printf("allowcated %d byte ( %d words) for buffer\n", AllocatedSize, AllocatedSize/4); + ret |= CAEN_DGTZ_MallocDPPEvents(handle, reinterpret_cast(&Events), &AllocatedSize) ; + printf("allowcated %d byte for Events\n", AllocatedSize); + for( int i = 0 ; i < NChannel; i++){ + ret |= CAEN_DGTZ_MallocDPPWaveforms(handle, reinterpret_cast(&Waveform[i]), &AllocatedSize); + printf("allowcated %d byte for waveform-%d\n", AllocatedSize, i); + } + + if( ret != 0 ) { printf("==== memory allocation error.\n"); return 0;} + + PrintChannelSettingFromDigitizer(handle, 4, ch2ns); + + printf("============ Start ACQ \n"); + CAEN_DGTZ_SWStartAcquisition(handle); + + sleep(1); + + printf("============ Read Data \n"); + + ret = CAEN_DGTZ_ReadData(handle, CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT, buffer, &BufferSize); + if (ret) { + printf("Error when reading data %d\n", ret); + return 0; + } + Nb = BufferSize; + if (Nb == 0 || ret) { + return 0; + } + ret |= (CAEN_DGTZ_ErrorCode) CAEN_DGTZ_GetDPPEvents(handle, buffer, BufferSize, reinterpret_cast(&Events), NumEvents); + if (ret) { + printf("Error when getting events from data %d\n", ret); + return 0; + } + + for (int ch = 0; ch < NChannel; ch++) { + if( NumEvents[ch] > 0 ) printf("------------------------ %d, %d\n", ch, NumEvents[ch]); + for (int ev = 0; ev < NumEvents[ch]; ev++) { + ///TrgCnt[ch]++; + + if( ev == 0 ){ + printf("%3s, %6s, %13s | %5s | %13s | %13s \n", "ev", "energy", "timetag", "ex2", "rollover", "timeStamp"); + } + + if (Events[ch][ev].Energy > 0 && Events[ch][ev].TimeTag > 0 ) { + ///ECnt[ch]++; + unsigned long long timetag = (unsigned long long) Events[ch][ev].TimeTag; + unsigned long long rollOver = Events[ch][ev].Extras2 >> 16; + rollOver = rollOver << 31; + timetag += rollOver ; + + printf("%3d, %6d, %13lu | %5u | %13llu | %13llu \n", + ev, Events[ch][ev].Energy, + Events[ch][ev].TimeTag, + Events[ch][ev].Extras2 , + rollOver >> 32, timetag); + + } else { /// PileUp + ///PurCnt[ch]++; + } + + } /// loop on events + } /// loop on channels + + printf("============ Stop ACQ \n"); + ret |= CAEN_DGTZ_ClearData(handle); + + + printf("============= Read Buffer \n"); + + unsigned int nw = 0; + + do{ + printf("######################################### Board Agg.\n"); + unsigned int word = ReadBuffer(nw, buffer); + if( ( (word >> 28) & 0xF ) == 0xA ) { /// start of Board Agg + unsigned int nWord = word & 0x0FFFFFFF ; + printf(" number of words in this Agg : %d \n", nWord); + + nw = nw + 1; word = ReadBuffer(nw, buffer); + unsigned int BoardID = ((word >> 27) & 0x1F); + bool BoardFailFlag = ((word >> 26) & 0x1 ); + unsigned int ChannelMask = ( word & 0xFF ) ; + printf("Board ID : %d, FailFlag = %d, ChannelMask = 0x%x\n", BoardID, BoardFailFlag, ChannelMask); + + nw = nw + 2; + unsigned int AggCounter = ReadBuffer(nw, buffer); + printf("Agg Counter : %d \n", AggCounter); + + printf("---------------------- Dual Channel Block\n"); + + for( int chMask = 0; chMask < 8 ; chMask ++ ){ + if( ((ChannelMask >> chMask) & 0x1 ) == 0 ) continue; + nw = nw + 1; word = ReadBuffer(nw, buffer); + bool hasFormatInfo = ((word >> 31) & 0x1); + unsigned int aggSize = ( word & 0x3FFFFFF ) ; + printf(" size : %d \n", aggSize); + unsigned int nSample = 0; /// wave form; + unsigned int nEvents = 0; + if( hasFormatInfo ){ + nw = nw + 1; word = ReadBuffer(nw, buffer); + 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 ); + + printf("dualTrace : %d, Energy : %d, Time: %d, Wave : %d, Extra2: %d, Extra2Option: %d \n", + hasDualTrace, hasEnergy, hasTimeStamp, hasWaveForm, hasExtra2, extra2Option); + printf("Ana Probe 1 & 2: %d %d , Digi Probe: %d, nSample : %d \n", + analogProbe1, analogProbe2, digitalProbe, nSample); + + nEvents = aggSize / (nSample/2 + 2 + hasExtra2 ); + printf("=========== nEvents : %d \n", nEvents); + }else{ + printf("does not has format info. unable to read buffer.\n"); + break; + } + + for( int ev = 0; ev < nEvents ; ev++){ + printf("=================================== event : %d\n", ev); + nw = nw +1 ; word = ReadBuffer(nw, buffer); + bool channelTag = ((word >> 31) & 0x1); + unsigned int timeStamp = (word & 0x7FFFFFFF); + int channel = chMask*2 + channelTag; + printf("ch : %d, timeStamp %u \n", channel, timeStamp); + + ///===== read waveform + for( int wi = 0; wi < nSample/2; wi++){ + nw = nw +1 ; word = ReadBuffer(nw, buffer, false); + bool isTrigger1 = (( word >> 31 ) & 0x1 ); + unsigned int wave1 = (( word >> 16) & 0x3FFF); + + bool isTrigger0 = (( word >> 15 ) & 0x1 ); + unsigned int wave0 = ( word & 0x3FFF); + + if( ev == 0 ){ + printf(" %5d, %d \n", wave0, isTrigger0); + printf(" %5d, %d \n", wave1, isTrigger1); + } + } + + nw = nw +1 ; word = ReadBuffer(nw, buffer); + unsigned int extra2 = word; + + nw = nw +1 ; word = ReadBuffer(nw, buffer); + unsigned int extra = (( word >> 16) & 0x3FF); + unsigned int energy = (word & 0x7FFF); + bool pileUp = ((word >> 15) & 0x1); + + printf("PileUp : %d , extra : 0x%04x, energy : %d \n", pileUp, extra, energy); + + } + } + }else{ + printf("incorrect buffer header. \n"); + break; + } + nw++; + }while(true); + + + printf("=========== close Digitizer \n"); + CAEN_DGTZ_SWStopAcquisition(handle); + CAEN_DGTZ_CloseDigitizer(handle); + CAEN_DGTZ_FreeReadoutBuffer(&buffer); + CAEN_DGTZ_FreeDPPEvents(handle, reinterpret_cast(&Events)); + CAEN_DGTZ_FreeDPPWaveforms(handle, Waveform); + + return 0; +}