2022-08-03 19:00:41 -04:00
# ifndef DIGITIZER_H
# define DIGITIZER_H
# include <stdio.h>
# include <string>
# include <sstream>
# include <cmath>
# include <cstring> ///memset
# include <iostream> ///cout
# include <bitset>
# include "CAENDigitizer.h"
# include "CAENDigitizerType.h"
2022-08-05 18:15:50 -04:00
# include "macro.h"
2022-08-09 16:02:45 -04:00
# include "ClassData.h"
2022-08-03 19:00:41 -04:00
# include "RegisterAddress.h"
using namespace std ;
//################################################################
class Digitizer {
public :
Digitizer ( ) ;
2022-09-26 16:47:20 -04:00
Digitizer ( int boardID , int portID = 0 , bool program = false , bool verbose = false ) ;
2022-08-03 19:00:41 -04:00
~ Digitizer ( ) ;
2022-08-10 18:35:13 -04:00
2022-09-26 16:47:20 -04:00
void Initalization ( ) ;
2022-08-03 19:00:41 -04:00
void Reset ( ) ;
2022-09-16 17:53:57 -04:00
int OpenDigitizer ( int boardID , int portID = 0 , bool program = false , bool verbose = false ) ; /// portID is for optical link for using PCIe card, from 0, 1, 2, 3
2022-08-03 19:00:41 -04:00
int CloseDigitizer ( ) ;
///=================Settings
2022-10-05 17:54:10 -04:00
/// write value to digitizer, memory, and settingFile (if exist)
void WriteRegister ( uint32_t registerAddress , uint32_t value , int ch = - 1 ) ;
/// read value from digitizer and memory, and settingFile(if exist),
/// when ch == -1 for register < 0x8000, read ch = 0;
uint32_t ReadRegister ( uint32_t registerAddress , int ch = - 1 , string str = " " ) ;
2022-08-03 19:00:41 -04:00
///common for PHA and PSD digitizers
2022-10-05 17:54:10 -04:00
void SetChannelMask ( uint32_t mask ) ;
void SetRecordLength ( unsigned int ns , int ch = - 1 ) ; /// when ch == -1, mean set all channels
void SetInputDynamicRange ( unsigned int TwoVol_0_or_halfVol_1 , int ch = - 1 ) ;
void SetPreTriggerSample ( unsigned int nSample , int ch = - 1 ) ;
void SetPreTriggerDuration ( unsigned int ns , int ch = - 1 ) ;
void SetDCOffset ( float offsetPrecentage , int ch = - 1 ) ;
void SetVetoWidth ( uint32_t bit , int ch = - 1 ) ; /// See manual
void SetTriggerPolarity ( bool RiseingIsZero , int ch = - 1 ) ; ///not used for DPP firmware
void SetEventAggregation ( unsigned int numEvent , int ch = - 1 ) ;
void SetAggregateOrganization ( unsigned int bit ) ;
void SetMaxAggregatePerBlockTransfer ( unsigned int numEvent ) ;
2022-08-15 18:54:55 -04:00
2022-08-04 17:27:33 -04:00
void SetBits ( uint32_t address , unsigned int bitValue , unsigned int bitLength , unsigned int bitSmallestPos , int ch = - 1 ) ;
void SetDPPAlgorithmControl ( uint32_t bit , int ch = - 1 ) ;
2022-08-03 19:00:41 -04:00
2022-08-29 18:06:12 -04:00
void SetACQControl ( uint32_t bit ) ;
void SetGlobalTriggerMask ( uint32_t bit ) ;
void SetFrontPanelTRGOUTMask ( uint32_t bit ) ;
void SetFrontPanelIOControl ( uint32_t bit ) ;
void SetTriggerValidationMask ( uint32_t bit ) ;
2022-08-23 15:49:03 -04:00
//void SetBoardID(unsigned int ID) {WriteRegister(Register::DPP::BoardID, ID));
2022-08-03 19:00:41 -04:00
///================ Get Settings
2022-09-16 17:53:57 -04:00
std : : string GetModelName ( ) { return BoardInfo . ModelName ; }
2022-08-23 13:43:05 -04:00
int GetSerialNumber ( ) { return BoardInfo . SerialNumber ; }
int GetChannelMask ( ) { return channelMask ; }
bool GetChannelOnOff ( unsigned ch ) { return ( channelMask & ( 1 < < ch ) ) ; }
float GetCh2ns ( ) { return ch2ns ; }
int GetNChannel ( ) { return NChannel ; }
int GetHandle ( ) { return handle ; }
bool GetConnectionStatus ( ) { return isConnected ; }
int GetDPPType ( ) { return DPPType ; }
2022-09-26 16:47:20 -04:00
std : : string GetDPPTypeString ( ) { return DPPTypeStr ; }
2022-09-16 17:53:57 -04:00
int GetADCBits ( ) { return BoardInfo . ADC_NBits ; }
std : : string GetROCVersion ( ) { return BoardInfo . ROC_FirmwareRel ; }
std : : string GetAMCVersion ( ) { return BoardInfo . AMC_FirmwareRel ; }
2022-08-03 19:00:41 -04:00
2022-10-05 17:54:10 -04:00
CAEN_DGTZ_ConnectionType GetLinkType ( ) { return LinkType ; }
int GetChTemperature ( int ch ) ;
2022-08-03 19:00:41 -04:00
2022-10-05 17:54:10 -04:00
unsigned int GetRecordLengthSample ( int ch ) { return ReadRegister ( Register : : DPP : : RecordLength_G , ch ) * 8 ; }
unsigned int GetInputDynamicRange ( int ch ) { return ReadRegister ( Register : : DPP : : InputDynamicRange , ch ) ; }
unsigned int GetPreTriggerSample ( int ch ) { return ReadRegister ( Register : : DPP : : PreTrigger , ch ) * 4 ; }
float GetDCOffset ( int ch ) { return 100.0 - ReadRegister ( Register : : DPP : : ChannelDCOffset , ch ) * 100. / 0xFFFFF ; }
unsigned int GetVetoWidth ( int ch ) { return ReadRegister ( Register : : DPP : : VetoWidth , ch ) ; }
unsigned int GetEventAggregation ( int ch = - 1 ) { return ReadRegister ( Register : : DPP : : NumberEventsPerAggregate_G , ch ) ; }
unsigned int GetAggregateOrganization ( ) { return ReadRegister ( Register : : DPP : : AggregateOrganization ) ; }
2022-08-29 18:06:12 -04:00
unsigned int GetMaxNumberOfAggregatePerBlockTransfer ( ) { return ReadRegister ( Register : : DPP : : MaxAggregatePerBlockTransfer ) ; }
2022-08-04 18:02:03 -04:00
2022-08-23 13:43:05 -04:00
unsigned int ReadBits ( uint32_t address , unsigned int bitLength , unsigned int bitSmallestPos , int ch = - 1 ) ;
unsigned int GetDPPAlgorithmControl ( int ch = - 1 ) { return ReadRegister ( Register : : DPP : : DPPAlgorithmControl , ch ) ; }
2022-09-29 15:26:40 -04:00
bool IsRunning ( ) { return AcqRun ; }
2022-09-20 15:15:52 -04:00
2022-09-19 12:46:27 -04:00
uint32_t PrintRegister ( uint32_t address , std : : string msg ) ;
2022-09-16 17:53:57 -04:00
void PrintBoard ( ) ;
2022-08-23 13:43:05 -04:00
void PrintACQStatue ( ) ;
2022-08-03 19:00:41 -04:00
///================ ACQ control
void StopACQ ( ) ;
void StartACQ ( ) ;
2022-08-10 18:35:13 -04:00
void ReadData ( ) ;
2022-08-23 13:43:05 -04:00
Data * GetData ( ) { return data ; }
2022-08-09 16:02:45 -04:00
2022-08-15 18:54:55 -04:00
unsigned int CalByteForBuffer ( ) ;
2022-09-27 17:58:14 -04:00
int ProgramPHABoard ( ) ;
2022-08-09 16:02:45 -04:00
///================ Setting
2022-10-05 17:54:10 -04:00
unsigned short CalSettingIndex ( uint32_t registerAddress , int ch = - 1 ) ; /// real address is CalSettingIndex() * 4
void FillSetting ( uint32_t registerAddress , int ch = - 1 ) ;
void SetSettingToMemory ( uint32_t registerAddress , unsigned int value , int ch = - 1 ) ;
unsigned int GetSettingFromMemory ( uint32_t registerAddress , int ch = - 1 ) ;
void FillAllSettings ( ) ;
void PrintSettingFromMemory ( ) ;
2022-10-06 17:10:54 -04:00
void SetSettingFromMemory ( uint32_t registerAddress , int ch = - 1 ) ;
2022-10-06 18:25:35 -04:00
void OpenSettingBinary ( string fileName ) ; /// Open setting file, if file not exist, call CreateAndSaveSetiingFile, if file exit simple set the settignFileName
void LoadSettingBinary ( string fileName ) ; /// load settign file to memory, if digitizer connected, program digitizer
2022-10-05 17:54:10 -04:00
void CreateAndSaveSettingToFile ( string fileName ) ;
void SaveSettingToFile ( uint32_t registerAddress , unsigned int value , int ch = - 1 ) ;
unsigned int ReadSettingFromFile ( uint32_t registerAddress , int ch = - 1 ) ; /// read from setting binary
2022-10-06 18:25:35 -04:00
string GetSettingFileName ( ) { return settingFileName ; }
2022-09-27 17:58:14 -04:00
virtual int ProgramBoard ( ) ;
2022-08-23 13:43:05 -04:00
2022-10-05 17:54:10 -04:00
2022-08-03 19:00:41 -04:00
protected :
2022-08-23 13:43:05 -04:00
Data * data ;
2022-08-03 19:00:41 -04:00
///---- fixed parameter
int portID ; /// port ID for optical link for using PCIe card, from 0, 1, 2, 3
int boardID ; /// board identity
int handle ; /// i don't know why, but better separete the handle from boardID
int NChannel ; /// number of channel
2022-08-05 18:15:50 -04:00
int ADCbits ; /// ADC bit
int DPPType ; /// DPP verion
2022-09-26 16:47:20 -04:00
std : : string DPPTypeStr ; /// DPP type in string
2022-08-03 19:00:41 -04:00
unsigned int ADCFullSize ; /// pow(2, ADCbits) - 1
float ch2ns ; /// channel to ns
CAEN_DGTZ_BoardInfo_t BoardInfo ;
///----- adjustable parameters
2022-08-05 18:15:50 -04:00
uint32_t channelMask ; /// the channel mask from NChannel
2022-08-03 19:00:41 -04:00
uint32_t VMEBaseAddress ; /// For direct USB or Optical-link connection, VMEBaseAddress must be 0
CAEN_DGTZ_ConnectionType LinkType ;
CAEN_DGTZ_IOLevel_t IOlev ; /// TTL signal (1 = 1.5 to 5V, 0 = 0 to 0.7V ) or NIM signal (1 = -1 to -0.8V, 0 = 0V)
CAEN_DGTZ_DPP_AcqMode_t AcqMode ;
2022-08-09 16:02:45 -04:00
2022-08-03 19:00:41 -04:00
///------- other parameters
int ret ; /// return value, refer to CAEN_DGTZ_ErrorCode
bool isConnected ;
2022-08-10 18:35:13 -04:00
bool AcqRun ;
2022-08-03 19:00:41 -04:00
2022-08-09 16:02:45 -04:00
/// ------- setting
string settingFileName ;
FILE * settingFile ;
bool settingFileExist ;
2022-10-05 17:54:10 -04:00
unsigned int setting [ SETTINGSIZE ] ; /// Setting, 4bytes x 2048 = 8192 bytes
bool isSettingFilledinMemeory ;
2022-08-09 16:02:45 -04:00
2022-08-03 19:00:41 -04:00
///==========
void ErrorMsg ( string header = " " ) ;
} ;
# endif