2023-04-11 11:13:23 -04:00
# include "ClassDigitizer.h"
Digitizer : : Digitizer ( ) {
Initalization ( ) ;
}
Digitizer : : Digitizer ( int boardID , int portID , bool program , bool verbose ) {
Initalization ( ) ;
OpenDigitizer ( boardID , portID , program , verbose ) ;
}
Digitizer : : ~ Digitizer ( ) {
CloseDigitizer ( ) ;
delete data ;
}
void Digitizer : : Initalization ( ) {
2023-10-06 17:57:43 -04:00
data = nullptr ;
2023-04-11 11:13:23 -04:00
portID = - 1 ;
boardID = - 1 ;
handle = - 1 ;
2023-10-09 17:46:32 -04:00
NumInputCh = 16 ;
NumRegChannel = 16 ;
isInputChEqRegCh = true ;
2023-10-05 12:26:17 -04:00
NCoupledCh = 8 ;
2023-04-11 11:13:23 -04:00
ADCbits = 1 ;
DPPType = 0 ;
ADCFullSize = 0 ;
2023-06-20 11:57:39 -04:00
tick2ns = 0 ;
2023-04-11 11:13:23 -04:00
BoardInfo = { } ;
2023-10-09 13:12:48 -04:00
regChannelMask = 0xFFFF ;
2023-04-11 11:13:23 -04:00
VMEBaseAddress = 0 ;
LinkType = CAEN_DGTZ_USB ; /// default USB
IOlev = CAEN_DGTZ_IOLevel_NIM ; ///default NIM
isSettingFilledinMemeory = false ;
settingFileName = " " ;
2024-01-18 23:41:18 -05:00
isSettingFileExist = false ;
2024-01-19 14:15:37 -05:00
isSettingFileUpdate = false ;
2023-04-11 11:13:23 -04:00
settingFile = NULL ;
ret = - 1 ;
isConnected = false ;
AcqRun = false ;
isDummy = true ;
}
void Digitizer : : Reset ( ) {
2023-05-15 17:18:53 -04:00
// ret = CAEN_DGTZ_Reset(handle);
// if( ret != 0 ) ErrorMsg(__func__);
2023-04-11 11:13:23 -04:00
2023-05-15 17:18:53 -04:00
// ret |= CAEN_DGTZ_WriteRegister(handle, DPP::SoftwareClear_W, 1);
// if( ret != 0 ) ErrorMsg("Reset-SoftwareClear_W");
// Clear data off the Output Buffer, the event counter, perform a FPGA global reset to default configuration
ret = CAEN_DGTZ_WriteRegister ( handle , DPP : : SoftwareReset_W , 1 ) ;
if ( ret ! = 0 ) ErrorMsg ( " Reset-SoftwareReset_W " ) ;
2023-04-11 11:13:23 -04:00
}
void Digitizer : : PrintBoard ( ) {
printf ( " Connected to Model %s with handle %d using %s \n " , BoardInfo . ModelName , handle , LinkType = = CAEN_DGTZ_USB ? " USB " : " Optical Link " ) ;
2023-10-09 13:12:48 -04:00
printf ( " Sampling rate : %.0f MHz = %.1f ns \n " , 1000 / tick2ns , tick2ns ) ;
2023-10-09 17:46:32 -04:00
printf ( " No. of Input Channels : %d \n " , NumInputCh ) ;
printf ( " No. of Reg Channels : %d, mask : 0x%X \n " , NumRegChannel , regChannelMask ) ;
2023-10-09 13:12:48 -04:00
printf ( " SerialNumber : \ e[1m \ e[33m %d \ e[0m \n " , BoardInfo . SerialNumber ) ;
printf ( " DPPType : %d (%s) \n " , DPPType , GetDPPString ( ) . c_str ( ) ) ;
printf ( " ADC bit : \ e[33m%d \ e[0m, %d = 0x%X \n " , ADCbits , ADCFullSize , ADCFullSize ) ;
printf ( " ROC FPGA Release : %s \n " , BoardInfo . ROC_FirmwareRel ) ;
printf ( " AMC FPGA Release : %s \n " , BoardInfo . AMC_FirmwareRel ) ;
2023-04-11 11:13:23 -04:00
}
int Digitizer : : OpenDigitizer ( int boardID , int portID , bool program , bool verbose ) {
this - > boardID = boardID ;
this - > portID = portID ;
if ( boardID < 0 | | portID < 0 ) return 0 ; /// for using the Digitizer Class without open digitizer
/***************************************************/
/** Open the digitizer and read board information */
/***************************************************/
if ( verbose ) printf ( " ============= Opening Digitizer at Board %d, Port %d \n " , boardID , portID ) ;
///-------- try USB first
2023-12-15 16:02:46 -05:00
if ( portID < 4 ) {
LinkType = CAEN_DGTZ_USB ; /// Link Type
ret = ( int ) CAEN_DGTZ_OpenDigitizer ( LinkType , boardID , 0 , VMEBaseAddress , & handle ) ;
if ( ret ! = 0 ) { ///---------- try Optical link
LinkType = CAEN_DGTZ_OpticalLink ;
ret = ( int ) CAEN_DGTZ_OpenDigitizer ( LinkType , portID , boardID , VMEBaseAddress , & handle ) ;
}
ErrorMsg ( " === Open Digitizer port " + std : : to_string ( portID ) + " board " + std : : to_string ( boardID ) ) ;
} else {
LinkType = CAEN_DGTZ_USB_A4818 ; // portID = A4818 PID
2023-04-11 11:13:23 -04:00
ret = ( int ) CAEN_DGTZ_OpenDigitizer ( LinkType , portID , boardID , VMEBaseAddress , & handle ) ;
2024-02-26 15:37:06 -05:00
ErrorMsg ( " === Open Digitizer using A4818 (PID: " + std : : to_string ( portID ) + " ) board " + std : : to_string ( boardID ) + " . " ) ;
2023-04-11 11:13:23 -04:00
}
2023-12-12 19:01:41 -05:00
if ( ret = = 0 ) {
if ( LinkType = = CAEN_DGTZ_USB ) printf ( " Open digitizer via USB, board : %d \n " , boardID ) ;
if ( LinkType = = CAEN_DGTZ_OpticalLink ) printf ( " Open digitizer via Optical Link, port : %d, board : %d \n " , portID , boardID ) ;
if ( LinkType = = CAEN_DGTZ_USB_A4818 ) printf ( " Open digitizer via A4818, port : %d, board : %d \n " , portID , boardID ) ;
}
2023-04-11 11:13:23 -04:00
if ( ret ! = 0 ) {
if ( verbose ) printf ( " Can't open digitizer \n " ) ;
return - 1 ;
} else {
///----- Getting Board Info
ret = ( int ) CAEN_DGTZ_GetInfo ( handle , & BoardInfo ) ;
if ( ret ! = 0 ) {
if ( verbose ) printf ( " Can't read board info \n " ) ;
} else {
isConnected = true ;
2023-10-09 17:46:32 -04:00
NumRegChannel = BoardInfo . Channels ;
NumInputCh = NumRegChannel ;
isInputChEqRegCh = true ;
regChannelMask = pow ( 2 , NumInputCh ) - 1 ;
2023-04-11 11:13:23 -04:00
switch ( BoardInfo . Model ) {
2023-10-09 17:46:32 -04:00
case CAEN_DGTZ_V1730 : tick2ns = 2.0 ; NCoupledCh = NumInputCh / 2 ; break ; ///ns -> 500 MSamples/s
case CAEN_DGTZ_V1725 : tick2ns = 4.0 ; NCoupledCh = NumInputCh / 2 ; break ; ///ns -> 250 MSamples/s
2023-10-09 13:12:48 -04:00
case CAEN_DGTZ_V1740 : {
2023-10-09 17:46:32 -04:00
NumInputCh = 64 ;
NCoupledCh = NumRegChannel ;
isInputChEqRegCh = false ;
2023-10-09 13:12:48 -04:00
tick2ns = 16.0 ; break ; ///ns -> 62.5 MSamples/s
}
2023-06-20 11:57:39 -04:00
default : tick2ns = 4.0 ; break ;
2023-04-11 11:13:23 -04:00
}
2023-10-06 17:57:43 -04:00
2023-10-09 17:46:32 -04:00
data = new Data ( NumInputCh ) ;
2023-06-20 11:57:39 -04:00
data - > tick2ns = tick2ns ;
2023-04-11 11:13:23 -04:00
data - > boardSN = BoardInfo . SerialNumber ;
ADCbits = BoardInfo . ADC_NBits ;
ADCFullSize = ( unsigned int ) ( pow ( 2 , ADCbits ) - 1 ) ;
}
}
///====================== Check DPP firmware revision
sscanf ( BoardInfo . AMC_FirmwareRel , " %d " , & DPPType ) ;
data - > DPPType = DPPType ;
2023-04-17 11:52:06 -04:00
switch ( DPPType ) {
case 0x80 : data - > DPPTypeStr = " PHA " ; break ; // x724
case 0x82 : data - > DPPTypeStr = " xCI " ; break ; // x720
case 0x83 : data - > DPPTypeStr = " PSD " ; break ; // x720
case 0x84 : data - > DPPTypeStr = " PSD " ; break ; // x751
case 0x85 : data - > DPPTypeStr = " ZLE " ; break ; // x751
case 0x86 : data - > DPPTypeStr = " PSD " ; break ; // x743
case 0x87 : data - > DPPTypeStr = " QDC " ; break ; // x740
case 0x88 : data - > DPPTypeStr = " PSD " ; break ; // x730
case 0x89 : data - > DPPTypeStr = " DAW " ; break ; // x724
case 0x8B : data - > DPPTypeStr = " PHA " ; break ; // x730
case 0x8C : data - > DPPTypeStr = " ZLE " ; break ; // x730
case 0x8D : data - > DPPTypeStr = " DAW " ; break ; // x730
2023-10-09 13:12:48 -04:00
default : data - > DPPTypeStr = " STD " ; break ; // stardard
2023-04-17 11:52:06 -04:00
}
2023-04-11 11:13:23 -04:00
/// change address 0xEF08 (5 bits), this will reflected in the 2nd word of the Board Agg. header.
2023-05-01 16:20:16 -04:00
ret = CAEN_DGTZ_WriteRegister ( handle , DPP : : BoardID , ( DPPType & 0xF ) ) ;
2023-10-13 18:02:19 -04:00
2023-04-11 11:13:23 -04:00
if ( verbose ) {
PrintBoard ( ) ;
if ( DPPType < 0x80 ) {
printf ( " This digitizer does not have DPP-PHA firmware \n " ) ;
} else {
2023-10-09 13:12:48 -04:00
printf ( " \t ==== This digitizer has a DPP firmware! " ) ;
printf ( " \ e[32m \t %s \ e[0m \n " , GetDPPString ( ) . c_str ( ) ) ;
2023-04-11 11:13:23 -04:00
}
}
ErrorMsg ( " ========== Set BoardID " ) ;
///======================= Check virtual probe
2023-10-09 13:12:48 -04:00
if ( DPPType ! = DPPType : : DPP_QDC_CODE ) {
int probes [ MAX_SUPPORTED_PROBES ] ;
int numProbes ;
ret = CAEN_DGTZ_GetDPP_SupportedVirtualProbes ( handle , 1 , probes , & numProbes ) ;
ErrorMsg ( " === Get Supported Virtual Probes " ) ;
if ( verbose ) {
printf ( " \t ==== supported virtual probe (number of Probe : %d) \n " , numProbes ) ;
for ( int i = 0 ; i < numProbes ; i + + ) {
printf ( " \t \t %8d " , probes [ i ] ) ;
switch ( probes [ i ] ) {
case 0 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_Input \n " ) ; break ;
case 1 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_Delta \n " ) ; break ;
case 2 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_Delta2 \n " ) ; break ;
case 3 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_Trapezoid \n " ) ; break ;
case 4 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_TrapezoidReduced \n " ) ; break ;
case 5 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_Baseline \n " ) ; break ;
case 6 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_Threshold \n " ) ; break ;
case 7 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_CFD \n " ) ; break ;
case 8 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_SmoothedInput \n " ) ; break ;
case 9 : printf ( " CAEN_DGTZ_DPP_VIRTUALPROBE_None \n " ) ; break ;
case 10 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_TRGWin \n " ) ; break ;
case 11 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_Armed \n " ) ; break ;
case 12 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_PkRun \n " ) ; break ;
case 13 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_Peaking \n " ) ; break ;
case 14 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_CoincWin \n " ) ; break ;
case 15 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_BLHoldoff \n " ) ; break ;
case 16 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_TRGHoldoff \n " ) ; break ;
case 17 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_TRGVal \n " ) ; break ;
case 18 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_ACQVeto \n " ) ; break ;
case 19 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_BFMVeto \n " ) ; break ;
case 20 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_ExtTRG \n " ) ; break ;
case 21 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_OverThr \n " ) ; break ;
case 22 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_TRGOut \n " ) ; break ;
case 23 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_Coincidence \n " ) ; break ;
case 24 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_PileUp \n " ) ; break ;
case 25 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_Gate \n " ) ; break ;
case 26 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_GateShort \n " ) ; break ;
case 27 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_Trigger \n " ) ; break ;
case 28 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_None \n " ) ; break ;
case 29 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_BLFreeze \n " ) ; break ;
case 30 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_Busy \n " ) ; break ;
case 31 : printf ( " CAEN_DGTZ_DPP_DIGITALPROBE_PrgVeto \n " ) ; break ;
default : printf ( " Unknown probe \n " ) ; break ;
}
}
2023-04-11 11:13:23 -04:00
}
2023-10-09 13:12:48 -04:00
}
2023-04-11 11:13:23 -04:00
ErrorMsg ( " end of OpenDigitizer " ) ;
2023-11-06 17:59:16 -05:00
softwareDisable = false ;
2023-04-11 11:13:23 -04:00
if ( isConnected ) isDummy = false ;
2023-10-13 18:02:19 -04:00
2023-04-11 11:13:23 -04:00
if ( isConnected & & program ) {
2023-10-09 13:12:48 -04:00
if ( DPPType = = DPPType : : DPP_PHA_CODE ) ProgramBoard_PHA ( ) ;
if ( DPPType = = DPPType : : DPP_PSD_CODE ) ProgramBoard_PSD ( ) ;
if ( DPPType = = DPPType : : DPP_QDC_CODE ) ProgramBoard_QDC ( ) ;
2023-04-11 11:13:23 -04:00
}
2023-05-03 17:53:35 -04:00
//if( isConnected ) ReadAllSettingsFromBoard();
2023-04-11 11:13:23 -04:00
return ret ;
}
int Digitizer : : CloseDigitizer ( ) {
if ( ! isConnected ) return 0 ;
isConnected = false ;
2023-10-12 16:27:56 -04:00
ret = CAEN_DGTZ_SWStopAcquisition ( handle ) ;
2023-04-11 11:13:23 -04:00
printf ( " -------- Closing Digtizer Board : %d Port : %d \n " , boardID , portID ) ;
printf ( " Model %s with handle %d using %s \n " , BoardInfo . ModelName , handle , LinkType = = CAEN_DGTZ_USB ? " USB " : " Optical Link " ) ;
ret | = CAEN_DGTZ_CloseDigitizer ( handle ) ;
return ret ;
}
2023-10-09 13:12:48 -04:00
void Digitizer : : SetRegChannelMask ( uint32_t mask ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return ;
2023-04-11 11:13:23 -04:00
if ( ! isConnected ) return ;
2023-10-09 13:12:48 -04:00
regChannelMask = mask ;
ret | = CAEN_DGTZ_SetChannelEnableMask ( handle , regChannelMask ) ;
SetSettingToMemory ( DPP : : RegChannelEnableMask , mask ) ;
2024-01-18 23:41:18 -05:00
SaveSettingToFile ( DPP : : RegChannelEnableMask , mask ) ;
2023-04-11 11:13:23 -04:00
ErrorMsg ( __func__ ) ;
}
2023-10-09 17:46:32 -04:00
bool Digitizer : : GetInputChannelOnOff ( unsigned ch ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return false ;
2023-10-09 17:46:32 -04:00
regChannelMask = GetSettingFromMemory ( DPP : : RegChannelEnableMask ) ;
if ( isInputChEqRegCh ) return ( regChannelMask & ( 1 < < ch ) ) ;
int grpID = ch / 8 ; //may change for not grouped in 8;
return ( regChannelMask & ( 1 < < grpID ) ) ;
}
2023-10-09 13:12:48 -04:00
void Digitizer : : SetRegChannelOnOff ( unsigned short ch , bool onOff ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return ;
2023-04-11 11:13:23 -04:00
if ( ! isConnected ) return ;
2023-10-09 13:12:48 -04:00
regChannelMask = ( ( regChannelMask & ~ ( 1 < < ch ) ) | ( onOff < < ch ) ) ;
SetRegChannelMask ( regChannelMask ) ;
2023-04-11 11:13:23 -04:00
}
2024-01-18 23:09:50 -05:00
void Digitizer : : ProgramBoard ( ) {
if ( DPPType = = DPPType : : DPP_PHA_CODE ) ProgramBoard_PHA ( ) ;
if ( DPPType = = DPPType : : DPP_PSD_CODE ) ProgramBoard_PSD ( ) ;
if ( DPPType = = DPPType : : DPP_QDC_CODE ) ProgramBoard_QDC ( ) ;
}
2023-10-09 13:12:48 -04:00
int Digitizer : : ProgramBoard_PHA ( ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return 0 ;
2023-05-03 17:53:35 -04:00
printf ( " ===== Digitizer::%s \n " , __func__ ) ;
2023-05-02 15:43:05 -04:00
2023-05-15 17:18:53 -04:00
//ret = CAEN_DGTZ_Reset(handle);
Reset ( ) ;
2023-04-11 11:13:23 -04:00
2023-05-01 16:20:16 -04:00
ret = CAEN_DGTZ_WriteRegister ( handle , DPP : : RecordLength_G + 0x7000 , 62 ) ;
2023-10-20 16:40:30 -04:00
//ret = CAEN_DGTZ_WriteRegister(handle, DPP::BoardConfiguration, 0x0F8915); /// has Extra2, dual trace, input and trap-baseline
ret = CAEN_DGTZ_WriteRegister ( handle , DPP : : BoardConfiguration , 0x0E8915 ) ; /// has Extra2, no trace
2024-01-19 14:40:59 -05:00
//ret = CAEN_DGTZ_WriteRegister(handle, DPP::BoardConfiguration, 0x0D8115); /// diable Extra2
2023-04-11 11:13:23 -04:00
//TODO change to write register
ret = CAEN_DGTZ_SetAcquisitionMode ( handle , CAEN_DGTZ_SW_CONTROLLED ) ; /// software command
ret | = CAEN_DGTZ_SetIOLevel ( handle , CAEN_DGTZ_IOLevel_NIM ) ;
ret | = CAEN_DGTZ_SetExtTriggerInputMode ( handle , CAEN_DGTZ_TRGMODE_ACQ_ONLY ) ;
ret = CAEN_DGTZ_SetChannelEnableMask ( handle , 0xFFFF ) ;
//ret = CAEN_DGTZ_SetNumEventsPerAggregate(handle, 0);
ret = CAEN_DGTZ_SetRunSynchronizationMode ( handle , CAEN_DGTZ_RUN_SYNC_Disabled ) ;
if ( ret ! = 0 ) { printf ( " ==== set board error. \n " ) ; return 0 ; }
uint32_t address ;
2023-05-01 16:20:16 -04:00
address = DPP : : PHA : : DecayTime ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 5000 ) ;
address = DPP : : PHA : : TrapezoidFlatTop ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 0x1A ) ;
address = DPP : : PHA : : TrapezoidRiseTime ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 6 ) ;
address = DPP : : PHA : : PeakingTime ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 6 ) ;
address = DPP : : PHA : : RCCR2SmoothingFactor ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 4 ) ;
address = DPP : : PHA : : InputRiseTime ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 6 ) ;
address = DPP : : PHA : : TriggerThreshold ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 1000 ) ;
address = DPP : : PHA : : PeakHoldOff ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 0x3E ) ;
address = DPP : : PHA : : TriggerHoldOffWidth ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 0x3E ) ;
address = DPP : : PHA : : RiseTimeValidationWindow ; ret | = CAEN_DGTZ_WriteRegister ( handle , address + 0x7000 , 0x0 ) ;
2023-10-11 18:28:19 -04:00
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x0 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x1 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x2 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x3 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x4 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x5 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x6 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x7 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x8 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x9 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xA , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xB , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xC , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xD , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xE , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xF , 0xAAAA ) ;
2023-05-01 16:20:16 -04:00
ret | = CAEN_DGTZ_WriteRegister ( handle , ( uint32_t ) ( DPP : : PreTrigger ) + 0x7000 , 32 ) ;
ret | = CAEN_DGTZ_WriteRegister ( handle , ( uint32_t ) ( DPP : : InputDynamicRange ) + 0x7000 , 0x0 ) ;
2023-12-12 19:01:41 -05:00
ret | = CAEN_DGTZ_WriteRegister ( handle , ( int32_t ) ( DPP : : DPPAlgorithmControl ) + 0x7000 , 0x030200f ) ;
2023-10-11 18:28:19 -04:00
if ( ret ! = 0 ) { printf ( " !!!!!!!! set channels error. \n " ) ; }
2023-10-13 18:02:19 -04:00
2024-01-18 23:09:50 -05:00
AutoSetDPPEventAggregation ( ) ;
2023-10-13 18:02:19 -04:00
/// change address 0xEF08 (5 bits), this will reflected in the 2nd word of the Board Agg. header.
ret = CAEN_DGTZ_WriteRegister ( handle , DPP : : BoardID , ( DPPType & 0xF ) ) ;
//WriteRegister(DPP::BoardID, (DPPType & 0xF));
2023-04-11 11:13:23 -04:00
isSettingFilledinMemeory = false ; /// unlock the ReadAllSettingsFromBoard();
2023-10-11 18:28:19 -04:00
usleep ( 1000 * 300 ) ;
2023-04-11 11:13:23 -04:00
ReadAllSettingsFromBoard ( ) ;
return ret ;
}
2023-10-09 13:12:48 -04:00
int Digitizer : : ProgramBoard_PSD ( ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return 0 ;
2023-05-12 16:06:32 -04:00
printf ( " ===== Digitizer::%s \n " , __func__ ) ;
2023-05-15 17:18:53 -04:00
//ret = CAEN_DGTZ_Reset(handle);
Reset ( ) ;
2023-10-20 16:40:30 -04:00
//ret = CAEN_DGTZ_WriteRegister(handle, DPP::BoardConfiguration, 0x0F0115); /// has Extra2, dual trace, input and CFD
ret = CAEN_DGTZ_WriteRegister ( handle , DPP : : BoardConfiguration , 0x0E0115 ) ; /// has Extra2, no trace
2023-05-12 16:06:32 -04:00
ret = CAEN_DGTZ_SetAcquisitionMode ( handle , CAEN_DGTZ_SW_CONTROLLED ) ; /// software command
ret | = CAEN_DGTZ_SetIOLevel ( handle , CAEN_DGTZ_IOLevel_NIM ) ;
ret | = CAEN_DGTZ_SetExtTriggerInputMode ( handle , CAEN_DGTZ_TRGMODE_ACQ_ONLY ) ;
2023-06-20 16:18:02 -04:00
ret | = CAEN_DGTZ_SetChannelEnableMask ( handle , 0xFFFF ) ;
2023-05-12 16:06:32 -04:00
2023-10-11 18:28:19 -04:00
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x0 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x1 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x2 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x3 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x4 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x5 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x6 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x7 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x8 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0x9 , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xA , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xB , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xC , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xD , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xE , 0xAAAA ) ;
ret | = CAEN_DGTZ_SetChannelDCOffset ( handle , 0xF , 0xAAAA ) ;
2023-05-12 16:06:32 -04:00
2023-05-31 17:30:46 -04:00
ret | = CAEN_DGTZ_WriteRegister ( handle , ( uint32_t ) ( DPP : : PreTrigger ) + 0x7000 , 20 ) ;
ret | = CAEN_DGTZ_WriteRegister ( handle , ( uint32_t ) ( DPP : : RecordLength_G ) + 0x7000 , 80 ) ;
2023-05-15 17:18:53 -04:00
2023-05-31 17:30:46 -04:00
ret | = CAEN_DGTZ_WriteRegister ( handle , ( uint32_t ) ( DPP : : PSD : : ShortGateWidth ) + 0x7000 , 32 ) ;
ret | = CAEN_DGTZ_WriteRegister ( handle , ( uint32_t ) ( DPP : : PSD : : LongGateWidth ) + 0x7000 , 64 ) ;
ret | = CAEN_DGTZ_WriteRegister ( handle , ( uint32_t ) ( DPP : : PSD : : GateOffset ) + 0x7000 , 19 ) ;
2023-05-12 16:06:32 -04:00
2023-10-11 18:28:19 -04:00
if ( ret ! = 0 ) { printf ( " !!!!!!!! set channels error. \n " ) ; }
2023-10-13 18:02:19 -04:00
/// change address 0xEF08 (5 bits), this will reflected in the 2nd word of the Board Agg. header.
ret = CAEN_DGTZ_WriteRegister ( handle , DPP : : BoardID , ( DPPType & 0xF ) ) ;
//WriteRegister(DPP::BoardID, (DPPType & 0xF));
2023-12-12 19:01:41 -05:00
2024-01-18 23:09:50 -05:00
AutoSetDPPEventAggregation ( ) ;
2023-12-12 19:01:41 -05:00
2023-05-15 17:18:53 -04:00
isSettingFilledinMemeory = false ; /// unlock the ReadAllSettingsFromBoard();
2023-10-11 18:28:19 -04:00
usleep ( 1000 * 300 ) ;
2023-05-12 16:06:32 -04:00
ReadAllSettingsFromBoard ( ) ;
return ret ;
}
2023-10-09 13:12:48 -04:00
int Digitizer : : ProgramBoard_QDC ( ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return 0 ;
2023-06-20 16:18:02 -04:00
printf ( " ===== Digitizer::%s \n " , __func__ ) ;
Reset ( ) ;
2023-10-11 18:28:19 -04:00
int ret = 0 ;
2023-12-12 19:01:41 -05:00
//WriteRegister(DPP::QDC::NumberEventsPerAggregate, 0x10, -1);
2023-10-19 14:29:28 -04:00
WriteRegister ( DPP : : QDC : : RecordLength_W , 16 , - 1 ) ; // 128 sample = 2048 ns
2023-10-13 18:02:19 -04:00
WriteRegister ( DPP : : QDC : : PreTrigger , 60 , - 1 ) ; // at 60 sample = 960 ns
2023-10-12 16:27:56 -04:00
2023-10-11 18:28:19 -04:00
WriteRegister ( DPP : : QDC : : GateWidth , 100 / 16 , - 1 ) ;
WriteRegister ( DPP : : QDC : : GateOffset , 0 , - 1 ) ;
WriteRegister ( DPP : : QDC : : FixedBaseline , 0 , - 1 ) ;
2023-10-12 16:27:56 -04:00
2023-10-19 14:29:28 -04:00
//WriteRegister(DPP::QDC::DPPAlgorithmControl, 0x300112); // with test pulse, positive
//WriteRegister(DPP::QDC::DPPAlgorithmControl, 0x300102); // No test pulse, positive
WriteRegister ( DPP : : QDC : : DPPAlgorithmControl , 0x310102 ) ; // No test pulse, negative
2023-10-12 16:27:56 -04:00
2023-10-11 18:28:19 -04:00
WriteRegister ( DPP : : QDC : : TriggerHoldOffWidth , 100 / 16 , - 1 ) ;
WriteRegister ( DPP : : QDC : : TRGOUTWidth , 100 / 16 , - 1 ) ;
//WriteRegister(DPP::QDC::OverThresholdWidth, 100/16, -1);
WriteRegister ( DPP : : QDC : : SubChannelMask , 0xFF , - 1 ) ;
2023-10-12 16:27:56 -04:00
WriteRegister ( DPP : : QDC : : DCOffset , 0xAAAA , - 1 ) ;
2023-10-11 18:28:19 -04:00
WriteRegister ( DPP : : QDC : : TriggerThreshold_sub0 , 100 , - 1 ) ;
WriteRegister ( DPP : : QDC : : TriggerThreshold_sub1 , 100 , - 1 ) ;
WriteRegister ( DPP : : QDC : : TriggerThreshold_sub2 , 100 , - 1 ) ;
WriteRegister ( DPP : : QDC : : TriggerThreshold_sub3 , 100 , - 1 ) ;
WriteRegister ( DPP : : QDC : : TriggerThreshold_sub4 , 100 , - 1 ) ;
WriteRegister ( DPP : : QDC : : TriggerThreshold_sub5 , 100 , - 1 ) ;
WriteRegister ( DPP : : QDC : : TriggerThreshold_sub6 , 100 , - 1 ) ;
WriteRegister ( DPP : : QDC : : TriggerThreshold_sub7 , 100 , - 1 ) ;
2024-01-18 23:09:50 -05:00
WriteRegister ( DPP : : BoardConfiguration , 0xE0110 ) ;
2023-12-12 19:01:41 -05:00
//WriteRegister(DPP::AggregateOrganization, 0x0);
//WriteRegister(DPP::MaxAggregatePerBlockTransfer, 100);
2023-10-11 18:28:19 -04:00
WriteRegister ( DPP : : AcquisitionControl , 0x0 ) ;
WriteRegister ( DPP : : GlobalTriggerMask , 0x0 ) ;
WriteRegister ( DPP : : FrontPanelTRGOUTEnableMask , 0x0 ) ;
WriteRegister ( DPP : : FrontPanelIOControl , 0x0 ) ;
WriteRegister ( DPP : : QDC : : GroupEnableMask , 0xFF ) ;
2023-10-13 18:02:19 -04:00
/// change address 0xEF08 (5 bits), this will reflected in the 2nd word of the Board Agg. header.
ret = CAEN_DGTZ_WriteRegister ( handle , DPP : : BoardID , ( DPPType & 0xF ) ) ;
//WriteRegister(DPP::BoardID, (DPPType & 0xF));
2023-10-11 18:28:19 -04:00
2024-01-18 23:09:50 -05:00
AutoSetDPPEventAggregation ( ) ;
2023-12-12 19:01:41 -05:00
2023-06-20 16:18:02 -04:00
isSettingFilledinMemeory = false ; /// unlock the ReadAllSettingsFromBoard();
2023-10-11 18:28:19 -04:00
usleep ( 1000 * 300 ) ;
2023-06-20 16:18:02 -04:00
ReadAllSettingsFromBoard ( ) ;
return ret ;
}
2023-04-11 11:13:23 -04:00
//========================================================= ACQ control
void Digitizer : : StartACQ ( ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return ;
2023-04-11 11:13:23 -04:00
if ( AcqRun ) return ;
2023-11-17 17:41:10 -05:00
2024-02-23 18:31:39 -05:00
// ret |= CAEN_DGTZ_SetDPPEventAggregation(handle, 0, 0); // Auto set
2023-12-12 19:01:41 -05:00
2023-10-10 11:06:03 -04:00
unsigned int bufferSize = 0 ;
if ( DPPType = = V1730_DPP_PHA_CODE ) {
bufferSize = CalByteForBuffer ( ) ;
if ( bufferSize > 160 * 1024 * 1024 ) {
printf ( " ============= buffer size bigger than 160 MB (%u) \n " , bufferSize ) ;
//return;
}
2023-11-17 17:41:10 -05:00
} else if ( DPPType = = V1730_DPP_PSD_CODE ) {
bufferSize = 200 * 1024 * 1024 ; //TODO allocate 200 MB for PSD
} else if ( DPPType = = V1740_DPP_QDC_CODE ) {
bufferSize = 200 * 1024 * 1024 ; //TODO allocate 200 MB for QDC
} else {
printf ( " DPP type not supported. ACQ not start. \n " ) ;
return ;
2023-04-11 11:13:23 -04:00
}
2023-05-12 16:06:32 -04:00
2023-04-11 11:13:23 -04:00
data - > AllocateMemory ( bufferSize ) ;
2023-06-29 18:59:36 -04:00
unsigned int acqID = ExtractBits ( GetSettingFromMemory ( DPP : : AcquisitionControl ) , DPP : : Bit_AcquistionControl : : StartStopMode ) ;
unsigned int trgOutID = ExtractBits ( GetSettingFromMemory ( DPP : : FrontPanelIOControl ) , DPP : : Bit_FrontPanelIOControl : : TRGOUTConfig ) ;
std : : string acqStr = " " ;
for ( int i = 0 ; i < ( int ) DPP : : Bit_AcquistionControl : : ListStartStopMode . size ( ) ; i + + ) {
if ( DPP : : Bit_AcquistionControl : : ListStartStopMode [ i ] . second = = acqID ) {
acqStr = DPP : : Bit_AcquistionControl : : ListStartStopMode [ i ] . first ;
}
}
std : : string trgOutStr = " " ;
for ( int i = 0 ; i < ( int ) DPP : : Bit_FrontPanelIOControl : : ListTRGOUTConfig . size ( ) ; i + + ) {
if ( DPP : : Bit_FrontPanelIOControl : : ListTRGOUTConfig [ i ] . second = = ( trgOutID < < 14 ) ) {
trgOutStr = DPP : : Bit_FrontPanelIOControl : : ListTRGOUTConfig [ i ] . first ;
}
}
2024-02-19 16:42:04 -05:00
if ( DPPType = = DPPType : : DPP_PHA_CODE ) {
printf ( " Setting Trapzoid Scaling Factor and Fine Gain \n " ) ;
for ( int ch = 0 ; ch < NumRegChannel ; ch + + ) {
unsigned int riseTime = GetSettingFromMemory ( DPP : : PHA : : TrapezoidRiseTime , ch ) ;
unsigned int decayTime = GetSettingFromMemory ( DPP : : PHA : : DecayTime , ch ) ;
unsigned int SHF = std : : floor ( std : : log2 ( riseTime * decayTime ) ) ;
SetBits ( DPP : : DPPAlgorithmControl , DPP : : Bit_DPPAlgorithmControl_PHA : : TrapRescaling , SHF , ch ) ;
//Always fix the fineGate = fg = 1
unsigned int f = 0xFFFF * pow ( 2 , SHF ) / riseTime / decayTime ;
WriteRegister ( DPP : : PHA : : FineGain , f , ch ) ;
}
}
2024-02-23 18:31:39 -05:00
AcqRun = true ;
data - > ClearTriggerRate ( ) ;
data - > ClearData ( ) ;
2023-06-29 18:59:36 -04:00
printf ( " ACQ mode : %s (%d), TRG-OUT mode : %s (%d) \n " , acqStr . c_str ( ) , acqID , trgOutStr . c_str ( ) , trgOutID ) ;
2024-02-19 16:42:04 -05:00
ret = CAEN_DGTZ_SWStartAcquisition ( handle ) ;
if ( ret ! = 0 ) {
ErrorMsg ( " Start ACQ " ) ;
return ;
}
printf ( " \ e[1m \ e[33m======= Acquisition Started for %d | Board %d, Port %d \ e[0m \n " , BoardInfo . SerialNumber , boardID , portID ) ;
2024-02-23 18:31:39 -05:00
2024-02-19 16:42:04 -05:00
2023-04-11 11:13:23 -04:00
}
void Digitizer : : StopACQ ( ) {
if ( ! AcqRun ) return ;
int ret = CAEN_DGTZ_SWStopAcquisition ( handle ) ;
ret | = CAEN_DGTZ_ClearData ( handle ) ;
if ( ret ! = 0 ) ErrorMsg ( " something wrong when try to stop ACQ and clear buffer " ) ;
2023-05-12 16:06:32 -04:00
printf ( " \n \ e[1m \ e[33m====== Acquisition STOPPED for %d | Board %d, Port %d \ e[0m \n " , BoardInfo . SerialNumber , boardID , portID ) ;
2023-04-11 11:13:23 -04:00
AcqRun = false ;
2023-08-22 14:38:37 -04:00
data - > PrintStat ( ) ;
2023-04-11 11:13:23 -04:00
data - > ClearTriggerRate ( ) ;
2024-02-23 18:31:39 -05:00
data - > ClearNumEventsDecoded ( ) ;
2023-04-19 18:08:20 -04:00
data - > ClearBuffer ( ) ;
2024-02-23 18:31:39 -05:00
data - > ClearReferenceTime ( ) ;
2023-05-22 18:23:24 -04:00
data - > ZeroTotalFileSize ( ) ;
2023-04-11 11:13:23 -04:00
}
unsigned int Digitizer : : CalByteForBuffer ( ) {
unsigned int numAggBLT ;
unsigned int chMask ;
unsigned int boardCfg ;
2023-10-09 17:46:32 -04:00
unsigned int eventAgg [ NumInputCh / 2 ] ;
unsigned int recordLength [ NumInputCh / 2 ] ;
2023-04-11 11:13:23 -04:00
unsigned int aggOrgan ;
if ( isConnected ) {
2023-05-01 16:20:16 -04:00
numAggBLT = ReadRegister ( DPP : : MaxAggregatePerBlockTransfer , 0 , false ) ;
2023-10-09 13:12:48 -04:00
chMask = ReadRegister ( DPP : : RegChannelEnableMask , 0 , false ) ;
2023-05-01 16:20:16 -04:00
boardCfg = ReadRegister ( DPP : : BoardConfiguration , 0 , false ) ;
aggOrgan = ReadRegister ( DPP : : AggregateOrganization , 0 , false ) ;
2023-04-11 11:13:23 -04:00
2023-10-09 17:46:32 -04:00
for ( int pCh = 0 ; pCh < NumInputCh / 2 ; pCh + + ) {
2023-05-01 16:20:16 -04:00
eventAgg [ pCh ] = ReadRegister ( DPP : : NumberEventsPerAggregate_G , pCh * 2 , false ) ;
recordLength [ pCh ] = ReadRegister ( DPP : : RecordLength_G , pCh * 2 , false ) ;
2023-04-11 11:13:23 -04:00
}
} else {
2023-05-01 16:20:16 -04:00
numAggBLT = GetSettingFromMemory ( DPP : : MaxAggregatePerBlockTransfer ) ;
2023-10-09 13:12:48 -04:00
chMask = GetSettingFromMemory ( DPP : : RegChannelEnableMask ) ;
2023-05-01 16:20:16 -04:00
boardCfg = GetSettingFromMemory ( DPP : : BoardConfiguration ) ;
aggOrgan = GetSettingFromMemory ( DPP : : AggregateOrganization ) ;
2023-10-09 17:46:32 -04:00
for ( int pCh = 0 ; pCh < NumInputCh / 2 ; pCh + + ) {
2023-05-01 16:20:16 -04:00
eventAgg [ pCh ] = GetSettingFromMemory ( DPP : : NumberEventsPerAggregate_G , pCh * 2 ) ;
recordLength [ pCh ] = GetSettingFromMemory ( DPP : : RecordLength_G , pCh * 2 ) ;
2023-04-11 11:13:23 -04:00
}
}
///printf(" agg. orgainzation (bit) : 0x%X \n", aggOrgan);
///printf(" Channel Mask : %04X \n", chMask);
///printf("Max number of Agg per Readout : %u \n", numAggBLT);
///printf(" is Extra2 enabed : %u \n", ((boardCfg >> 17) & 0x1) );
///printf(" is Record wave : %u \n", ((boardCfg >> 16) & 0x1) );
2023-10-09 17:46:32 -04:00
///for( int pCh = 0; pCh < NumInputCh/2; pCh++){
2023-04-11 11:13:23 -04:00
/// printf("Paired Ch : %d, RecordLength (bit value): %u, Event per Agg. : %u \n", pCh, recordLength[pCh], eventAgg[pCh]);
///}
2023-04-14 16:12:52 -04:00
unsigned int bufferSize = aggOrgan ; // just for get rip of the warning in complier
bufferSize = 0 ;
2023-10-09 17:46:32 -04:00
for ( int pCh = 0 ; pCh < NumInputCh / 2 ; pCh + + ) {
2023-04-11 11:13:23 -04:00
if ( ( chMask & ( 3 < < ( 2 * pCh ) ) ) = = 0 ) continue ;
bufferSize + = 2 + ( 2 + ( ( boardCfg > > 17 ) & 0x1 ) + ( ( boardCfg > > 16 ) & 0x1 ) * recordLength [ pCh ] * 4 ) * eventAgg [ pCh ] ;
}
bufferSize + = 4 ; /// Bd. Agg Header
bufferSize = bufferSize * numAggBLT * 4 ; /// 1 words = 4 byte
///printf("=============== Buffer Size : %8d Byte \n", bufferSize );
return bufferSize ;
}
2023-04-14 16:12:52 -04:00
int Digitizer : : ReadData ( ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return CAEN_DGTZ_DigitizerNotReady ;
2023-04-14 16:12:52 -04:00
if ( ! isConnected ) return CAEN_DGTZ_DigitizerNotFound ;
if ( ! AcqRun ) return CAEN_DGTZ_WrongAcqMode ;
2023-04-11 11:13:23 -04:00
if ( data - > buffer = = NULL ) {
printf ( " need allocate memory for readout buffer \n " ) ;
2023-04-14 16:12:52 -04:00
return CAEN_DGTZ_InvalidBuffer ;
2023-04-11 11:13:23 -04:00
}
ret = CAEN_DGTZ_ReadData ( handle , CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT , data - > buffer , & ( data - > nByte ) ) ;
2023-05-01 16:20:16 -04:00
//uint32_t EventSize = ReadRegister(DPP::EventSize); // Is it as same as data->nByte?
2023-08-17 12:38:15 -04:00
// if( data->nByte > 0 ) printf("Read Buffer size %d byte \n", data->nByte);
2023-04-11 11:13:23 -04:00
2023-11-20 12:10:36 -05:00
if ( ret ! = CAEN_DGTZ_Success | | data - > nByte = = 0 ) {
2023-04-11 11:13:23 -04:00
ErrorMsg ( __func__ ) ;
2023-11-20 12:10:36 -05:00
if ( ret = = CAEN_DGTZ_OutOfMemory ) {
printf ( " Abort ReadData. \n " ) ;
return ret ;
}
2023-04-11 11:13:23 -04:00
}
2023-04-14 16:12:52 -04:00
2023-11-20 12:10:36 -05:00
ReadACQStatus ( ) ;
2023-11-07 15:24:28 -05:00
2023-04-14 16:12:52 -04:00
return ret ;
2023-04-11 11:13:23 -04:00
}
2023-11-07 15:24:28 -05:00
void Digitizer : : ReadAndPrintACQStatue ( ) {
2023-04-11 11:13:23 -04:00
if ( ! isConnected ) return ;
2023-05-01 16:20:16 -04:00
unsigned int status = ReadRegister ( DPP : : AcquisitionStatus_R ) ;
2023-04-11 11:13:23 -04:00
printf ( " =================== Print ACQ status \n " ) ;
printf ( " 32 28 24 20 16 12 8 4 0 \n " ) ;
printf ( " | | | | | | | | | \n " ) ;
std : : cout < < " 0b " < < std : : bitset < 32 > ( status ) < < std : : endl ;
printf ( " Acq state (0x%1X): %s \n " , ( status > > 2 ) & 0x1 , ( ( status > > 2 ) & 0x1 ) = = 0 ? " stopped " : " running " ) ;
printf ( " Event Ready (0x%1X): %s \n " , ( status > > 3 ) & 0x1 , ( ( status > > 3 ) & 0x1 ) = = 0 ? " no event in buffer " : " event in buffer " ) ;
printf ( " Event Full (0x%1X): %s \n " , ( status > > 4 ) & 0x1 , ( ( status > > 4 ) & 0x1 ) = = 0 ? " not full " : " full " ) ;
printf ( " Clock source (0x%1X): %s \n " , ( status > > 5 ) & 0x1 , ( ( status > > 5 ) & 0x1 ) = = 0 ? " internal " : " external " ) ;
printf ( " Board ready (0x%1X): %s \n " , ( status > > 8 ) & 0x1 , ( ( status > > 8 ) & 0x1 ) = = 0 ? " not ready " : " ready " ) ;
printf ( " Ch shutDown (0x%1X): %s \n " , ( status > > 19 ) & 0x1 , ( ( status > > 19 ) & 0x1 ) = = 0 ? " Channels are on " : " channels are shutdown " ) ;
printf ( " TRG-IN 0x%1X \n " , ( status > > 16 ) & 0x1 ) ;
printf ( " Ch temp state 0x%04X \n " , ( status > > 20 ) & 0xF ) ;
}
//===========================================================
//===========================================================
//===========================================================
2023-05-01 16:20:16 -04:00
void Digitizer : : WriteRegister ( Reg registerAddress , uint32_t value , int ch , bool isSave2MemAndFile ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return ;
2023-10-11 18:28:19 -04:00
printf ( " %30s[0x%04X](digi-%d,ch-%02d) [0x%04X]: 0x%08X \n " , registerAddress . GetNameChar ( ) , registerAddress . GetAddress ( ) , GetSerialNumber ( ) , ch , registerAddress . ActualAddress ( ch ) , value ) ;
2023-04-11 11:13:23 -04:00
if ( ! isConnected ) {
2023-05-05 18:18:09 -04:00
//SetSettingToMemory(registerAddress, value, ch); //TODO should the binary setting be edited offline?
//SaveSettingToFile(registerAddress, value, ch);
2023-04-11 11:13:23 -04:00
return ;
}
2023-09-08 17:21:05 -04:00
if ( registerAddress . GetRWType ( ) = = RW : : ReadONLY ) return ;
2023-04-11 11:13:23 -04:00
ret = CAEN_DGTZ_WriteRegister ( handle , registerAddress . ActualAddress ( ch ) , value ) ;
2023-05-05 18:18:09 -04:00
2023-09-08 17:21:05 -04:00
if ( ret = = 0 & & isSave2MemAndFile & & registerAddress . GetRWType ( ) = = RW : : ReadWrite ) {
2023-05-05 18:18:09 -04:00
if ( ch < 0 ) {
2023-10-09 17:18:38 -04:00
if ( registerAddress . GetAddress ( ) < 0x8000 ) {
2023-10-09 17:46:32 -04:00
for ( int i = 0 ; i < NumInputCh ; i + + ) {
2023-10-09 17:18:38 -04:00
SetSettingToMemory ( registerAddress , value , i ) ;
SaveSettingToFile ( registerAddress , value , i ) ;
}
} else {
SetSettingToMemory ( registerAddress , value , 0 ) ;
SaveSettingToFile ( registerAddress , value , 0 ) ;
2023-05-05 18:18:09 -04:00
}
} else {
SetSettingToMemory ( registerAddress , value , ch ) ;
SaveSettingToFile ( registerAddress , value , ch ) ;
if ( registerAddress . IsCoupled ( ) ) {
SetSettingToMemory ( registerAddress , value , ch % 2 = = 0 ? ch + 1 : ch - 1 ) ;
SaveSettingToFile ( registerAddress , value , ch % 2 = = 0 ? ch + 1 : ch - 1 ) ;
}
}
2023-04-11 11:13:23 -04:00
}
2023-10-09 13:12:48 -04:00
std : : stringstream ss ;
ss < < std : : hex < < registerAddress . ActualAddress ( ch ) ;
ErrorMsg ( " WriteRegister:0x " + ss . str ( ) + " ( " + registerAddress . GetName ( ) + " ) " ) ;
2023-04-11 11:13:23 -04:00
}
2023-05-01 16:20:16 -04:00
uint32_t Digitizer : : ReadRegister ( Reg registerAddress , unsigned short ch , bool isSave2MemAndFile , std : : string str ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return 0 ;
2023-04-11 11:13:23 -04:00
if ( ! isConnected ) return 0 ;
2023-09-08 17:21:05 -04:00
if ( registerAddress . GetRWType ( ) = = RW : : WriteONLY ) return 0 ;
2023-04-11 11:13:23 -04:00
2023-04-19 13:41:43 -04:00
ret = CAEN_DGTZ_ReadRegister ( handle , registerAddress . ActualAddress ( ch ) , & returnData ) ;
2023-04-11 11:13:23 -04:00
2023-10-09 17:18:38 -04:00
if ( ret = = 0 & & isSave2MemAndFile ) {
//if( isSave2MemAndFile) {
2023-04-19 13:41:43 -04:00
SetSettingToMemory ( registerAddress , returnData , ch ) ;
SaveSettingToFile ( registerAddress , returnData , ch ) ;
2023-04-11 11:13:23 -04:00
}
2023-10-09 13:12:48 -04:00
std : : stringstream ss ;
ss < < std : : hex < < registerAddress . ActualAddress ( ch ) ;
ErrorMsg ( " ReadRegister:0x " + ss . str ( ) + " ( " + registerAddress . GetName ( ) + " ) " ) ;
2023-04-11 11:13:23 -04:00
if ( str ! = " " ) printf ( " %s : 0x%04X(0x%04X) is 0x%08X \n " , str . c_str ( ) ,
2023-04-19 13:41:43 -04:00
registerAddress . ActualAddress ( ch ) , registerAddress . GetAddress ( ) , returnData ) ;
return returnData ;
2023-04-11 11:13:23 -04:00
}
uint32_t Digitizer : : PrintRegister ( uint32_t address , std : : string msg ) {
if ( ! isConnected ) return 0 ;
printf ( " \ e[33m---------------------------------------------------- \n " ) ;
printf ( " ------------ %s = 0x%X \n " , msg . c_str ( ) , address ) ;
printf ( " ---------------------------------------------------- \ e[0m \n " ) ;
uint32_t * value = new uint32_t [ 1 ] ;
CAEN_DGTZ_ReadRegister ( handle , address , value ) ;
printf ( " %*s 32 28 24 20 16 12 8 4 0 \n " , ( int ) msg . length ( ) , " " ) ;
printf ( " %*s | | | | | | | | | \n " , ( int ) msg . length ( ) , " " ) ;
printf ( " %*s " , ( int ) msg . length ( ) , " " ) ;
std : : cout < < " : 0b " < < std : : bitset < 32 > ( value [ 0 ] ) < < std : : endl ;
printf ( " %*s : 0x%X \n " , ( int ) msg . length ( ) , msg . c_str ( ) , value [ 0 ] ) ;
return value [ 0 ] ;
}
//========================================== setting file IO
2023-05-01 16:20:16 -04:00
Reg Digitizer : : FindRegister ( uint32_t address ) {
2023-04-11 11:13:23 -04:00
2023-05-01 16:20:16 -04:00
Reg tempReg ;
2023-04-11 11:13:23 -04:00
///========= Find Match Register
2023-10-09 13:12:48 -04:00
if ( DPPType = = DPPType : : DPP_PHA_CODE | | DPPType = = DPPType : : DPP_PSD_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterBoardList_PHAPSD [ p ] ; p + + ) {
if ( address = = RegisterBoardList_PHAPSD [ p ] . GetAddress ( ) ) {
tempReg = RegisterBoardList_PHAPSD [ p ] ;
break ;
}
2023-04-11 11:13:23 -04:00
}
2023-10-09 13:12:48 -04:00
if ( tempReg . GetName ( ) = = " " ) {
if ( DPPType = = V1730_DPP_PHA_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterChannelList_PHA [ p ] ; p + + ) {
if ( address = = RegisterChannelList_PHA [ p ] . GetAddress ( ) ) {
tempReg = RegisterChannelList_PHA [ p ] ;
break ;
}
2023-04-11 11:13:23 -04:00
}
}
2023-10-09 13:12:48 -04:00
if ( DPPType = = V1730_DPP_PSD_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterChannelList_PSD [ p ] ; p + + ) {
if ( address = = RegisterChannelList_PSD [ p ] . GetAddress ( ) ) {
tempReg = RegisterChannelList_PSD [ p ] ;
break ;
}
2023-04-11 11:13:23 -04:00
}
}
}
2023-10-09 13:12:48 -04:00
} else {
for ( int p = 0 ; p < ( int ) RegisterBoardList_QDC [ p ] ; p + + ) {
if ( address = = RegisterBoardList_QDC [ p ] . GetAddress ( ) ) {
tempReg = RegisterBoardList_QDC [ p ] ;
break ;
}
}
for ( int p = 0 ; p < ( int ) RegisterChannelList_QDC [ p ] ; p + + ) {
if ( address = = RegisterChannelList_QDC [ p ] . GetAddress ( ) ) {
tempReg = RegisterChannelList_QDC [ p ] ;
break ;
}
}
2023-04-11 11:13:23 -04:00
}
return tempReg ;
}
void Digitizer : : ReadAllSettingsFromBoard ( bool force ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return ;
2023-04-11 11:13:23 -04:00
if ( ! isConnected ) return ;
if ( AcqRun ) return ;
if ( isSettingFilledinMemeory & & ! force ) return ;
2023-10-09 13:12:48 -04:00
printf ( " ===== Digitizer(%d)::%s \n " , GetSerialNumber ( ) , __func__ ) ;
2023-04-11 11:13:23 -04:00
/// board setting
2023-10-09 13:12:48 -04:00
if ( DPPType = = DPPType : : DPP_PHA_CODE | | DPPType = = DPPType : : DPP_PSD_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterBoardList_PHAPSD . size ( ) ; p + + ) {
if ( RegisterBoardList_PHAPSD [ p ] . GetRWType ( ) = = RW : : WriteONLY ) continue ;
ReadRegister ( RegisterBoardList_PHAPSD [ p ] ) ;
}
regChannelMask = GetSettingFromMemory ( DPP : : RegChannelEnableMask ) ;
/// Channels Setting
2023-10-09 17:46:32 -04:00
for ( int ch = 0 ; ch < NumInputCh ; ch + + ) {
2023-10-09 13:12:48 -04:00
if ( DPPType = = V1730_DPP_PHA_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterChannelList_PHA . size ( ) ; p + + ) {
if ( RegisterChannelList_PHA [ p ] . GetRWType ( ) = = RW : : WriteONLY ) continue ;
ReadRegister ( RegisterChannelList_PHA [ p ] , ch ) ;
}
}
if ( DPPType = = V1730_DPP_PSD_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterChannelList_PSD . size ( ) ; p + + ) {
if ( RegisterChannelList_PSD [ p ] . GetRWType ( ) = = RW : : WriteONLY ) continue ;
ReadRegister ( RegisterChannelList_PSD [ p ] , ch ) ;
}
2023-04-11 11:13:23 -04:00
}
}
2023-10-09 13:12:48 -04:00
} else {
for ( int p = 0 ; p < ( int ) RegisterBoardList_QDC . size ( ) ; p + + ) {
if ( RegisterBoardList_QDC [ p ] . GetRWType ( ) = = RW : : WriteONLY ) continue ;
ReadRegister ( RegisterBoardList_QDC [ p ] ) ;
}
regChannelMask = GetSettingFromMemory ( DPP : : QDC : : GroupEnableMask ) ;
2023-10-09 17:46:32 -04:00
for ( int ch = 0 ; ch < GetNumRegChannels ( ) ; ch + + ) {
2023-10-09 13:12:48 -04:00
for ( int p = 0 ; p < ( int ) RegisterChannelList_QDC . size ( ) ; p + + ) {
if ( RegisterChannelList_QDC [ p ] . GetRWType ( ) = = RW : : WriteONLY ) continue ;
ReadRegister ( RegisterChannelList_QDC [ p ] , ch ) ;
2023-04-11 11:13:23 -04:00
}
}
2023-10-09 13:12:48 -04:00
2023-04-11 11:13:23 -04:00
}
2023-10-13 18:02:19 -04:00
2023-10-19 14:29:28 -04:00
//printf("BoardID : 0x%X = DataFormat \n", GetSettingFromMemory(DPP::BoardID));
2023-10-13 18:02:19 -04:00
2023-04-11 11:13:23 -04:00
isSettingFilledinMemeory = true ;
2023-05-02 15:43:05 -04:00
2023-04-11 11:13:23 -04:00
}
void Digitizer : : ProgramSettingsToBoard ( ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return ;
2023-05-02 15:43:05 -04:00
if ( ! isConnected | | isDummy ) return ;
2023-04-11 11:13:23 -04:00
2023-05-02 15:43:05 -04:00
printf ( " ========== %s \n " , __func__ ) ;
2023-12-12 19:13:38 -05:00
const short pauseMilliSec = 20 ;
2023-11-17 16:47:11 -05:00
2023-05-01 16:20:16 -04:00
Reg haha ;
2023-04-11 11:13:23 -04:00
2023-10-09 13:12:48 -04:00
if ( DPPType = = DPPType : : DPP_PHA_CODE | | DPPType = = DPPType : : DPP_PSD_CODE ) {
/// board setting
2023-12-12 19:01:41 -05:00
//for( int p = 0; p < (int) RegisterBoardList_PHAPSD.size(); p++){
// if( RegisterBoardList_PHAPSD[p].GetRWType() == RW::ReadWrite) {
// haha = RegisterBoardList_PHAPSD[p];
// WriteRegister(haha, GetSettingFromMemory(haha), -1, false);
// usleep(pauseMilliSec * 1000);
// }
//}
2023-12-12 19:20:31 -05:00
haha = DPP : : BoardConfiguration ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : AcquisitionControl ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : GlobalTriggerMask ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : FrontPanelIOControl ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : FrontPanelTRGOUTEnableMask ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : RegChannelEnableMask ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
2023-10-09 13:12:48 -04:00
/// Channels Setting
2023-10-09 17:46:32 -04:00
for ( int ch = 0 ; ch < NumInputCh ; ch + + ) {
2023-10-09 13:12:48 -04:00
if ( DPPType = = V1730_DPP_PHA_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterChannelList_PHA . size ( ) ; p + + ) {
if ( RegisterChannelList_PHA [ p ] . GetRWType ( ) = = RW : : ReadWrite ) {
haha = RegisterChannelList_PHA [ p ] ;
WriteRegister ( haha , GetSettingFromMemory ( haha , ch ) , ch , false ) ;
2023-11-17 16:47:11 -05:00
usleep ( pauseMilliSec * 1000 ) ;
2023-10-09 13:12:48 -04:00
}
}
}
if ( DPPType = = V1730_DPP_PSD_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterChannelList_PSD . size ( ) ; p + + ) {
if ( RegisterChannelList_PSD [ p ] . GetRWType ( ) = = RW : : ReadWrite ) {
haha = RegisterChannelList_PSD [ p ] ;
WriteRegister ( haha , GetSettingFromMemory ( haha , ch ) , ch , false ) ;
2023-11-17 16:47:11 -05:00
usleep ( pauseMilliSec * 1000 ) ;
2023-10-09 13:12:48 -04:00
}
2023-05-02 15:43:05 -04:00
}
2023-04-11 11:13:23 -04:00
}
}
2023-10-09 13:12:48 -04:00
2023-12-12 19:20:31 -05:00
2023-10-09 13:12:48 -04:00
} else {
/// board setting
2023-12-12 19:01:41 -05:00
//for( int p = 0; p < (int) RegisterBoardList_QDC.size(); p++){
// if( RegisterBoardList_QDC[p].GetRWType() == RW::ReadWrite) {
// haha = RegisterBoardList_QDC[p];
// WriteRegister(haha, GetSettingFromMemory(haha), -1, false);
// usleep(pauseMilliSec * 1000);
// }
//}
2023-12-12 19:20:31 -05:00
haha = DPP : : BoardConfiguration ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : AcquisitionControl ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : GlobalTriggerMask ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : FrontPanelIOControl ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : FrontPanelTRGOUTEnableMask ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
haha = DPP : : QDC : : GroupEnableMask ; WriteRegister ( haha , GetSettingFromMemory ( haha ) , - 1 , false ) ;
2023-10-09 13:12:48 -04:00
/// Channels Setting
2023-10-09 17:46:32 -04:00
for ( int ch = 0 ; ch < GetNumRegChannels ( ) ; ch + + ) {
2023-10-09 13:12:48 -04:00
for ( int p = 0 ; p < ( int ) RegisterChannelList_QDC . size ( ) ; p + + ) {
if ( RegisterChannelList_QDC [ p ] . GetRWType ( ) = = RW : : ReadWrite ) {
haha = RegisterChannelList_QDC [ p ] ;
2023-05-02 15:43:05 -04:00
WriteRegister ( haha , GetSettingFromMemory ( haha , ch ) , ch , false ) ;
2023-11-17 16:47:11 -05:00
usleep ( pauseMilliSec * 1000 ) ;
2023-05-02 15:43:05 -04:00
}
2023-04-11 11:13:23 -04:00
}
}
2023-10-09 13:12:48 -04:00
2023-12-12 19:20:31 -05:00
}
//set agg
ret = CAEN_DGTZ_SetNumEventsPerAggregate ( handle , 10 ) ;
ret | = CAEN_DGTZ_SetDPPEventAggregation ( handle , 0 , 0 ) ; // Auto set
2023-04-11 11:13:23 -04:00
}
2023-05-01 16:20:16 -04:00
void Digitizer : : SetSettingToMemory ( Reg registerAddress , unsigned int value , unsigned short ch ) {
2023-04-11 11:13:23 -04:00
unsigned short index = registerAddress . Index ( ch ) ;
if ( index > SETTINGSIZE ) return ;
setting [ index ] = value ;
}
2023-05-01 16:20:16 -04:00
unsigned int Digitizer : : GetSettingFromMemory ( Reg registerAddress , unsigned short ch ) {
2023-04-11 11:13:23 -04:00
unsigned short index = registerAddress . Index ( ch ) ;
if ( index > SETTINGSIZE ) return 0xFFFF ;
return setting [ index ] ;
}
void Digitizer : : PrintSettingFromMemory ( ) {
for ( int i = 0 ; i < SETTINGSIZE ; i + + ) printf ( " %4d | 0x%04X |0x%08X = %u \n " , i , i * 4 , setting [ i ] , setting [ i ] ) ;
}
void Digitizer : : SetSettingBinaryPath ( std : : string fileName ) {
settingFile = fopen ( fileName . c_str ( ) , " r+ " ) ;
if ( settingFile = = NULL ) {
printf ( " cannot open file %s. Create one. \n " , fileName . c_str ( ) ) ;
ReadAllSettingsFromBoard ( ) ;
SaveAllSettingsAsBin ( fileName ) ;
this - > settingFileName = fileName ;
2024-01-18 23:41:18 -05:00
isSettingFileExist = true ;
2023-04-11 11:13:23 -04:00
} else {
this - > settingFileName = fileName ;
2024-01-18 23:41:18 -05:00
isSettingFileExist = true ;
2023-04-11 11:13:23 -04:00
fclose ( settingFile ) ;
printf ( " setting file already exist. do nothing. Should program the digitizer \n " ) ;
}
}
int Digitizer : : LoadSettingBinaryToMemory ( std : : string fileName ) {
settingFile = fopen ( fileName . c_str ( ) , " r " ) ;
if ( settingFile = = NULL ) {
printf ( " %s does not exist or cannot load. \n " , fileName . c_str ( ) ) ;
2024-01-18 23:41:18 -05:00
isSettingFileExist = false ;
2023-04-11 11:13:23 -04:00
return - 1 ;
} else {
2024-01-18 23:41:18 -05:00
isSettingFileExist = true ;
2023-04-11 11:13:23 -04:00
settingFileName = fileName ;
fclose ( settingFile ) ;
2023-05-01 16:20:16 -04:00
uint32_t fileDPP = ( ( ReadSettingFromFile ( DPP : : AMCFirmwareRevision_R , 0 ) > > 8 ) & 0xFF ) ;
2023-04-11 11:13:23 -04:00
/// compare seeting DPP version;
2023-04-11 11:50:52 -04:00
if ( isConnected & & DPPType ! = ( int ) fileDPP ) {
2023-04-11 11:13:23 -04:00
printf ( " DPPType in the file is %s(0x%X), but the dgitizer DPPType is %s(0x%X). \n " , GetDPPString ( fileDPP ) . c_str ( ) , fileDPP , GetDPPString ( ) . c_str ( ) , DPPType ) ;
return - 1 ;
} else {
/// load binary to memoery
DPPType = fileDPP ;
2023-05-23 11:19:43 -04:00
printf ( " DPPType in the file is %s(0x%X). Board Type is %s \n " , GetDPPString ( fileDPP ) . c_str ( ) , fileDPP , GetDPPString ( ) . c_str ( ) ) ;
2023-04-11 11:13:23 -04:00
settingFile = fopen ( fileName . c_str ( ) , " r " ) ;
size_t dummy = fread ( setting , SETTINGSIZE * sizeof ( unsigned int ) , 1 , settingFile ) ;
fclose ( settingFile ) ;
2023-04-14 16:12:52 -04:00
2023-05-23 11:19:43 -04:00
if ( dummy ! = 0 ) printf ( " reach the end of file (read %ld). \n " , dummy ) ;
2023-04-11 11:13:23 -04:00
2023-05-01 16:20:16 -04:00
uint32_t boardInfo = GetSettingFromMemory ( DPP : : BoardInfo_R ) ;
2023-11-20 17:47:12 -05:00
if ( ( boardInfo & 0xFF ) = = 0x0E ) { tick2ns = 4.0 ; NumRegChannel = 16 ; } // 725
if ( ( boardInfo & 0xFF ) = = 0x0B ) { tick2ns = 2.0 ; NumRegChannel = 16 ; } // 730
if ( ( boardInfo & 0xFF ) = = 0x04 ) { tick2ns = 16.0 ; NumRegChannel = 8 ; } // 740
2023-04-11 11:13:23 -04:00
///Should seperate file<->memory, memory<->board
///ProgramSettingsToBoard(); /// do nothing if not connected.
return 0 ;
}
}
}
2023-05-01 16:20:16 -04:00
unsigned int Digitizer : : ReadSettingFromFile ( Reg registerAddress , unsigned short ch ) {
2024-01-18 23:41:18 -05:00
if ( ! isSettingFileExist ) return - 1 ;
2023-04-11 11:13:23 -04:00
unsigned short index = registerAddress . Index ( ch ) ;
settingFile = fopen ( settingFileName . c_str ( ) , " r " ) ;
///fseek( settingFile, address, SEEK_SET);
fseek ( settingFile , index * 4 , SEEK_SET ) ;
///printf(" at pos %lu Byte = index(%lu)\n", ftell(settingFile), ftell(settingFile)/4);
unsigned int lala [ 1 ] ;
size_t dummy = fread ( lala , sizeof ( unsigned int ) , 1 , settingFile ) ;
///printf(" data at pos %lu(%lu) : %X = %d\n", ftell(settingFile) - sizeof(unsigned int), (ftell(settingFile) - sizeof(unsigned int))/4, lala[0], lala[0]);
fclose ( settingFile ) ;
2023-04-14 16:12:52 -04:00
if ( dummy = = 0 ) printf ( " reach the end of file \n " ) ;
2023-04-11 11:13:23 -04:00
return lala [ 0 ] ;
}
2023-05-01 16:20:16 -04:00
void Digitizer : : SaveSettingToFile ( Reg registerAddress , unsigned int value , unsigned short ch ) {
2023-10-27 17:51:45 -04:00
2024-01-18 23:41:18 -05:00
if ( ! isSettingFileExist ) return ;
2024-01-19 14:15:37 -05:00
if ( ! isSettingFileUpdate ) return ;
2023-10-27 17:51:45 -04:00
2024-01-19 14:15:37 -05:00
// printf("Write setting file : %s. %s, ch:%u, 0x%8X\n", settingFileName.c_str(), registerAddress.GetNameChar(), ch, value);
2024-01-19 13:21:59 -05:00
2023-04-11 11:13:23 -04:00
unsigned short index = registerAddress . Index ( ch ) ;
setting [ index ] = value ;
settingFile = fopen ( settingFileName . c_str ( ) , " r+ " ) ;
///fseek( settingFile, address, SEEK_SET);
fseek ( settingFile , index * 4 , SEEK_SET ) ;
unsigned int jaja [ 1 ] = { value } ;
2023-04-11 11:50:52 -04:00
fwrite ( jaja , sizeof ( unsigned int ) , 1 , settingFile ) ;
2023-04-11 11:13:23 -04:00
///printf("fwrite ret : %d, 0x%0X, 0x%0X, %d, 0x%X = %d\n", (int)dummy, registerAddress, index*4, index, jaja[0], jaja[0]);
fclose ( settingFile ) ;
}
void Digitizer : : SaveAllSettingsAsBin ( std : : string fileName ) {
2024-02-26 16:17:41 -05:00
SaveAllSettingsAsTextForRun ( fileName ) ;
if ( ! isSettingFilledinMemeory ) return ;
settingFileName = fileName ;
isSettingFileExist = true ;
}
void Digitizer : : SaveAllSettingsAsTextForRun ( std : : string fileName ) {
2023-04-11 11:13:23 -04:00
if ( ! isSettingFilledinMemeory ) return ;
FILE * binFile = fopen ( fileName . c_str ( ) , " w+ " ) ;
if ( binFile = = NULL ) {
printf ( " Cannot open %s. \n " , fileName . c_str ( ) ) ;
return ;
}
fwrite ( setting , SETTINGSIZE * sizeof ( unsigned int ) , 1 , binFile ) ;
fseek ( binFile , 0L , SEEK_END ) ;
unsigned int inFileSize = ftell ( binFile ) ;
printf ( " Created file : %s. file size : %d Byte \n " , fileName . c_str ( ) , inFileSize ) ;
fclose ( binFile ) ;
2023-10-27 17:51:45 -04:00
2023-04-11 11:13:23 -04:00
}
void Digitizer : : SaveAllSettingsAsText ( std : : string fileName ) {
2023-11-20 17:47:12 -05:00
if ( ! isSettingFilledinMemeory & & isConnected ) return ;
2023-04-11 11:13:23 -04:00
FILE * txtFile = fopen ( fileName . c_str ( ) , " w+ " ) ;
if ( txtFile = = NULL ) {
printf ( " Cannot open %s. \n " , fileName . c_str ( ) ) ;
return ;
}
2023-05-01 16:20:16 -04:00
Reg haha ;
2023-04-11 11:13:23 -04:00
for ( unsigned int i = 0 ; i < SETTINGSIZE ; i + + ) {
haha . SetName ( " " ) ;
uint32_t actualAddress = haha . CalAddress ( i ) ;
2023-10-09 13:12:48 -04:00
if ( DPPType = = V1730_DPP_PHA_CODE | | DPPType = = V1730_DPP_PSD_CODE ) {
///printf("%7d--- 0x%04X, 0x%04X\n", i, haha->GetAddress(), haha->ActualAddress());
for ( int p = 0 ; p < ( int ) RegisterBoardList_PHAPSD . size ( ) ; p + + ) {
if ( haha . GetAddress ( ) = = ( uint32_t ) RegisterBoardList_PHAPSD [ p ] ) haha = RegisterBoardList_PHAPSD [ p ] ;
2023-04-11 11:13:23 -04:00
}
2023-10-09 13:12:48 -04:00
if ( DPPType = = V1730_DPP_PHA_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterChannelList_PHA . size ( ) ; p + + ) {
if ( haha . GetAddress ( ) = = ( uint32_t ) RegisterChannelList_PHA [ p ] ) haha = RegisterChannelList_PHA [ p ] ;
}
}
if ( DPPType = = V1730_DPP_PSD_CODE ) {
for ( int p = 0 ; p < ( int ) RegisterChannelList_PSD . size ( ) ; p + + ) {
if ( haha . GetAddress ( ) = = ( uint32_t ) RegisterChannelList_PSD [ p ] ) haha = RegisterChannelList_PSD [ p ] ;
}
2023-04-11 11:13:23 -04:00
}
2023-10-09 13:12:48 -04:00
} else {
for ( int p = 0 ; p < ( int ) RegisterBoardList_QDC . size ( ) ; p + + ) {
if ( haha . GetAddress ( ) = = ( uint32_t ) RegisterBoardList_QDC [ p ] ) haha = RegisterBoardList_QDC [ p ] ;
}
for ( int p = 0 ; p < ( int ) RegisterChannelList_QDC . size ( ) ; p + + ) {
if ( haha . GetAddress ( ) = = ( uint32_t ) RegisterChannelList_QDC [ p ] ) haha = RegisterChannelList_QDC [ p ] ;
}
2023-04-11 11:13:23 -04:00
}
if ( haha . GetName ( ) ! = " " ) {
std : : string typeStr ;
2023-09-08 17:21:05 -04:00
if ( haha . GetRWType ( ) = = RW : : ReadWrite ) typeStr = " R/W " ;
if ( haha . GetRWType ( ) = = RW : : ReadONLY ) typeStr = " R " ;
if ( haha . GetRWType ( ) = = RW : : WriteONLY ) typeStr = " W " ;
2023-04-11 11:13:23 -04:00
fprintf ( txtFile , " 0x%04X %30s 0x%08X %s %u \n " , actualAddress ,
haha . GetNameChar ( ) ,
setting [ i ] ,
typeStr . c_str ( ) ,
setting [ i ] ) ;
}
}
2023-11-20 17:47:12 -05:00
printf ( " Saved setting as text to %s. \n " , fileName . c_str ( ) ) ;
fclose ( txtFile ) ;
2023-04-11 11:13:23 -04:00
}
std : : string Digitizer : : GetDPPString ( int DPPType ) {
std : : string DPPTypeStr = " " ;
if ( DPPType = = 0 ) DPPType = this - > DPPType ;
switch ( DPPType ) {
case V1724_DPP_PHA_CODE : DPPTypeStr = " DPP-PHA x724 " ; break ; /// 0x80
case V1720_DPP_CI_CODE : DPPTypeStr = " DPP-CI x720 " ; break ; /// 0x82
case V1720_DPP_PSD_CODE : DPPTypeStr = " DPP-PSD x720 " ; break ; /// 0x83
case V1751_DPP_PSD_CODE : DPPTypeStr = " DPP-PSD x751 " ; break ; /// 0x84
case V1751_DPP_ZLE_CODE : DPPTypeStr = " DPP-ZLE x751 " ; break ; /// 0x85
case V1743_DPP_CI_CODE : DPPTypeStr = " DPP-PSD x743 " ; break ; /// 0x86
case V1740_DPP_QDC_CODE : DPPTypeStr = " DPP-QDC x740 " ; break ; /// 0x87
case V1730_DPP_PSD_CODE : DPPTypeStr = " DPP-PSD x730 " ; break ; /// 0x88
case V1730_DPP_PHA_CODE : DPPTypeStr = " DPP-PHA x730 " ; break ; /// 0x8B
case V1730_DPP_ZLE_CODE : DPPTypeStr = " DPP-ZLE x730 " ; break ; /// 0x8C
case V1730_DPP_DAW_CODE : DPPTypeStr = " DPP-DAW x730 " ; break ; /// 0x8D
}
return DPPTypeStr ;
}
void Digitizer : : ErrorMsg ( std : : string header ) {
switch ( ret ) {
///case CAEN_DGTZ_Success : /** 0 */ printf("%s | Operation completed successfully.\n", header.c_str()); break;
2023-10-09 13:12:48 -04:00
case CAEN_DGTZ_CommError : /** -1 */ printf ( " %s %d | %d, Communication Error. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_GenericError : /** -2 */ printf ( " %s %d | %d, Unspecified error. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InvalidParam : /** -3 */ printf ( " %s %d | %d, Invalid parameter. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InvalidLinkType : /** -4 */ printf ( " %s %d | %d, Invalid Link Type. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InvalidHandle : /** -5 */ printf ( " %s %d | %d, Invalid device handler. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_MaxDevicesError : /** -6 */ printf ( " %s %d | %d, Maximum number of devices exceeded. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_BadBoardType : /** -7 */ printf ( " %s %d | %d, Operation not allowed on this type of board. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_BadInterruptLev : /** -8 */ printf ( " %s %d | %d, The interrupt level is not allowed. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_BadEventNumber : /** -9 */ printf ( " %s %d | %d, The event number is bad. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_ReadDeviceRegisterFail : /** -10 */ printf ( " %s %d | %d, Unable to read the registry. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_WriteDeviceRegisterFail : /** -11 */ printf ( " %s %d | %d, Unable to write the registry. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InvalidChannelNumber : /** -13 */ printf ( " %s %d | %d, The channel number is invalid. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_ChannelBusy : /** -14 */ printf ( " %s %d | %d, The channel is busy. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_FPIOModeInvalid : /** -15 */ printf ( " %s %d | %d, Invalid FPIO mode. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_WrongAcqMode : /** -16 */ printf ( " %s %d | %d, Wrong Acquistion mode. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_FunctionNotAllowed : /** -17 */ printf ( " %s %d | %d, This function is not allowed on this module. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_Timeout : /** -18 */ printf ( " %s %d | %d, Communication Timeout. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InvalidBuffer : /** -19 */ printf ( " %s %d | %d, The buffer is invalid. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_EventNotFound : /** -20 */ printf ( " %s %d | %d, The event is not found. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InvalidEvent : /** -21 */ printf ( " %s %d | %d, The event is invalid. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_OutOfMemory : /** -22 */ printf ( " %s %d | %d, Out of memory. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_CalibrationError : /** -23 */ printf ( " %s %d | %d, Unable to calibrate the board. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_DigitizerNotFound : /** -24 */ printf ( " %s %d | %d, Unbale to open the digitizer. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_DigitizerAlreadyOpen : /** -25 */ printf ( " %s %d | %d, The digitizer is already open. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_DigitizerNotReady : /** -26 */ printf ( " %s %d | %d, The digitizer is not ready. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InterruptNotConfigured : /** -27 */ printf ( " %s %d | %d, The digitizer has no IRQ configured. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_DigitizerMemoryCorrupted : /** -28 */ printf ( " %s %d | %d, The digitizer flash memory is corrupted. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_DPPFirmwareNotSupported : /** -29 */ printf ( " %s %d | %d, The digitier DPP firmware is not supported in this lib version. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InvalidLicense : /** -30 */ printf ( " %s %d | %d, Invalid firmware licence. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InvalidDigitizerStatus : /** -31 */ printf ( " %s %d | %d, The digitizer is found in a corrupted status. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_UnsupportedTrace : /** -32 */ printf ( " %s %d | %d, The given trace is not supported. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_InvalidProbe : /** -33 */ printf ( " %s %d | %d, The given probe is not supported. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_UnsupportedBaseAddress : /** -34 */ printf ( " %s %d | %d, The base address is not supported. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
case CAEN_DGTZ_NotYetImplemented : /** -99 */ printf ( " %s %d | %d, The function is not yet implemented. \n " , header . c_str ( ) , BoardInfo . SerialNumber , ret ) ; break ;
2023-04-11 11:13:23 -04:00
}
}
//============================== DPP-Alpgorthm Control
void Digitizer : : SetDPPAlgorithmControl ( uint32_t bit , int ch ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return ;
2023-05-01 16:20:16 -04:00
WriteRegister ( DPP : : DPPAlgorithmControl , bit , ch ) ;
2023-04-11 11:13:23 -04:00
if ( ret ! = 0 ) ErrorMsg ( __func__ ) ;
}
2023-05-01 16:20:16 -04:00
unsigned int Digitizer : : ReadBits ( Reg address , unsigned int bitLength , unsigned int bitSmallestPos , int ch ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return 0 ;
2023-04-11 11:13:23 -04:00
int tempCh = ch ;
if ( ch < 0 & & address < 0x8000 ) tempCh = 0 ; /// take ch-0
uint32_t bit = ReadRegister ( address , tempCh ) ;
bit = ( bit > > bitSmallestPos ) & uint ( pow ( 2 , bitLength ) - 1 ) ;
return bit ;
}
2023-05-01 16:20:16 -04:00
void Digitizer : : SetBits ( Reg address , unsigned int bitValue , unsigned int bitLength , unsigned int bitSmallestPos , int ch ) {
2023-11-06 17:59:16 -05:00
if ( softwareDisable ) return ;
2023-04-11 11:13:23 -04:00
///printf("address : 0x%X, value : 0x%X, len : %d, pos : %d, ch : %d \n", address, bitValue, bitLength, bitSmallestPos, ch);
uint32_t bit ;
uint32_t bitmask = ( uint ( pow ( 2 , bitLength ) - 1 ) < < bitSmallestPos ) ;
int tempCh = ch ;
if ( ch < 0 & & address < 0x8000 ) tempCh = 0 ; /// take ch-0
bit = ReadRegister ( address , tempCh ) ;
///printf("bit : 0x%X, bitmask : 0x%X \n", bit, bitmask);
bit = ( bit & ~ bitmask ) | ( bitValue < < bitSmallestPos ) ;
///printf("bit : 0x%X, ch : %d \n", bit, ch);
WriteRegister ( address , bit , ch ) ;
if ( ret ! = 0 ) ErrorMsg ( __func__ ) ;
}