FSUDAQ/DAQ/ClassData.h

699 lines
24 KiB
C
Raw Permalink Normal View History

#ifndef DATA_H
#define DATA_H
#include <stdio.h>
#include <string>
#include <sstream>
#include <cmath>
#include <cstring> ///memset
#include <iostream> ///cout
#include <bitset>
2022-08-26 17:18:43 -04:00
#include <vector>
2022-08-23 15:49:03 -04:00
#include "CAENDigitizerType.h"
2022-08-26 17:18:43 -04:00
#include "macro.h"
#define MaxNData 10000 /// 10k events per channels
class Data{
public:
int DPPType; /// this is set from the boardID from the Board Agg. header when it is > 0
2022-10-19 18:14:58 -04:00
unsigned short boardSN;
float ch2ns;
unsigned int nByte; /// number of byte from read buffer
char *buffer; /// readout buffer
uint32_t AllocatedSize;
2022-08-26 17:18:43 -04:00
double TriggerRate[MaxNChannels]; /// Hz
unsigned long TotNumEvents[MaxNChannels];
2022-10-19 18:14:58 -04:00
unsigned short NumEventsDecoded[MaxNChannels];
/// stored Raw event
bool IsNotRollOverFakeAgg;
2022-10-19 18:14:58 -04:00
unsigned short NumEvents[MaxNChannels];
unsigned long long Timestamp[MaxNChannels][MaxNData]; /// 47 bit
unsigned short fineTime[MaxNChannels][MaxNData]; /// 10 bits, in unit of ch2ns / 1000 = ps
unsigned short Energy[MaxNChannels][MaxNData]; /// 15 bit
unsigned short Energy2[MaxNChannels][MaxNData]; /// 15 bit, in PSD, Energy = Qshort, Energy2 = Qlong
2022-08-26 17:18:43 -04:00
std::vector<unsigned short> Waveform1[MaxNChannels][MaxNData];
std::vector<unsigned short> Waveform2[MaxNChannels][MaxNData];
2022-10-19 18:14:58 -04:00
std::vector<bool> DigiWaveform1[MaxNChannels][MaxNData];
std::vector<bool> DigiWaveform2[MaxNChannels][MaxNData];
public:
2022-08-23 13:43:05 -04:00
Data();
~Data();
void Allocate80MBMemory();
void AllocateMemory(uint32_t size);
2022-08-17 16:08:49 -04:00
2022-10-07 16:15:58 -04:00
void SetSaveWaveToMemory(bool OnOff) { this->SaveWaveToMemory = OnOff; }
2022-08-26 17:18:43 -04:00
void ClearData();
void ClearTriggerRate();
void ClearBuffer();
2022-08-26 17:18:43 -04:00
void CopyBuffer( const char * buffer, const unsigned int size);
2022-08-26 17:18:43 -04:00
void SaveBuffer(const char * fileName);
unsigned int GetPresentFileSize() {return presentFileSizeByte;}
2022-10-19 18:14:58 -04:00
void PrintBuffer() const; //Incorrect
2022-10-24 17:01:05 -04:00
void DecodeBuffer(bool fastDecode, int verbose = 0); /// fastDecode will not save waveform
void DecodeBuffer(char * buffer, unsigned int size, bool fastDecode, int verbose = 0); // for outside data
2022-10-19 18:14:58 -04:00
void PrintStat() const;
void PrintData() const;
2022-08-23 13:43:05 -04:00
protected:
2022-08-26 17:18:43 -04:00
unsigned int nw;
2022-10-07 16:15:58 -04:00
bool SaveWaveToMemory;
2022-08-26 17:18:43 -04:00
///for temperary
std::vector<unsigned short> tempWaveform1;
std::vector<unsigned short> tempWaveform2;
std::vector<bool> tempDigiWaveform1;
std::vector<bool> tempDigiWaveform2;
2022-08-23 13:43:05 -04:00
unsigned int ReadBuffer(unsigned int nWord, int verbose = 0);
2022-08-23 15:49:03 -04:00
2022-10-07 16:15:58 -04:00
int DecodePHADualChannelBlock(unsigned int ChannelMask, bool fastDecode, int verbose);
int DecodePSDDualChannelBlock(unsigned int ChannelMask, bool fastDecode, int verbose);
2022-08-17 16:08:49 -04:00
unsigned int presentFileSizeByte;
unsigned short saveFileIndex;
2022-08-23 13:43:05 -04:00
};
//==========================================
inline Data::Data(){
ch2ns = 2.0;
boardSN = 0;
DPPType = V1730_DPP_PHA_CODE;
2022-10-26 14:55:06 -04:00
IsNotRollOverFakeAgg = false;
2022-08-23 13:43:05 -04:00
buffer = NULL;
for ( int i = 0; i < MaxNChannels; i++) TotNumEvents[i] = 0;
2022-08-26 17:18:43 -04:00
ClearData();
ClearTriggerRate();
2022-10-07 16:15:58 -04:00
SaveWaveToMemory = true;
2022-08-26 17:18:43 -04:00
nw = 0;
saveFileIndex = 0;
2022-08-23 13:43:05 -04:00
}
inline Data::~Data(){
if( buffer != NULL ) delete buffer;
2022-08-23 13:43:05 -04:00
}
inline void Data::AllocateMemory(uint32_t size){
ClearBuffer();
AllocatedSize = size;
buffer = (char *) malloc( AllocatedSize);
printf("Allocated %u byte for buffer = %u words\n", AllocatedSize, AllocatedSize / 4);
}
inline void Data::Allocate80MBMemory(){
AllocateMemory( 80 * 1024 * 1024 ); /// 80 M Byte
2022-08-23 13:43:05 -04:00
}
2022-10-19 18:14:58 -04:00
inline void Data::ClearTriggerRate(){
for( int i = 0 ; i < MaxNChannels; i++) {
TriggerRate[i] = 0.0;
NumEventsDecoded[i] = 0;
}
}
2022-08-26 17:18:43 -04:00
inline void Data::ClearData(){
nByte = 0;
AllocatedSize = 0;
2022-10-26 14:55:06 -04:00
IsNotRollOverFakeAgg = false;
2022-08-26 17:18:43 -04:00
for( int i = 0 ; i < MaxNChannels; i++){
NumEvents[i] = 0;
for( int j = 0; j < MaxNData; j++){
Timestamp[i][j] = 0;
fineTime[i][j] = 0;
2022-08-26 17:18:43 -04:00
Energy[i][j] = 0;
Energy2[i][j] = 0;
Waveform1[i][j].clear();
Waveform2[i][j].clear();
DigiWaveform1[i][j].clear();
DigiWaveform2[i][j].clear();
}
}
tempWaveform1.clear();
tempWaveform2.clear();
tempDigiWaveform1.clear();
tempDigiWaveform2.clear();
2022-08-23 13:43:05 -04:00
}
inline void Data::ClearBuffer(){
delete buffer;
AllocatedSize = 0;
nByte = 0;
}
inline void Data::CopyBuffer(const char * buffer, const unsigned int size){
std::memcpy(this->buffer, buffer, size);
}
2022-08-26 17:18:43 -04:00
inline void Data::SaveBuffer(const char * fileName){
char saveFileName[100];
sprintf(saveFileName, "%s_%03d_%03d_%03u.fsu", fileName , boardSN, DPPType, saveFileIndex);
FILE * haha = fopen(saveFileName, "a+");
fseek(haha, 0L, SEEK_END);
unsigned int inFileSize = ftell(haha); /// unsigned int = Max ~ 4 GB
///printf("file Size = %u Byte\n", inFileSize);
if( inFileSize > (unsigned int)MaxSaveFileSize ) { /// 2 GB
fclose(haha);
saveFileIndex ++;
2022-10-07 16:15:58 -04:00
sprintf(saveFileName, "%s_%03u.fsu", fileName , saveFileIndex);
FILE * haha = fopen(saveFileName, "a+");
}
2022-08-23 13:43:05 -04:00
fwrite(buffer, nByte, 1, haha);
presentFileSizeByte = ftell(haha);
2022-10-25 16:56:52 -04:00
///printf("=========== file Size : %u Byte = %.2f MB\n", presentFileSizeByte, presentFileSizeByte/1024./1024.);
2022-08-23 13:43:05 -04:00
fclose(haha);
}
2022-10-19 18:14:58 -04:00
inline void Data::PrintStat() const{
2022-10-26 14:55:06 -04:00
if( !IsNotRollOverFakeAgg ) {
2022-10-24 17:01:05 -04:00
printf(" this is roll-over fake event or no events.\n");
return;
}
printf("%2s | %6s | %9s | %6s\n", "ch", "# Evt.", "Rate [Hz]", "Tot. Evt.");
printf("---+--------+-----------+----------\n");
for(int ch = 0; ch < MaxNChannels; ch++){
2022-10-19 18:14:58 -04:00
printf("%2d | %6d | %9.2f | %6lu\n", ch, NumEventsDecoded[ch], TriggerRate[ch], TotNumEvents[ch]);
}
printf("---+--------+-----------+----------\n");
}
2022-10-19 18:14:58 -04:00
inline void Data::PrintBuffer() const{
unsigned int length = sizeof(buffer);
for( int i = 0; i < length; i++){
printf("%3d | 0x%08x \n", i, buffer[i]);
}
2022-10-19 18:14:58 -04:00
}
inline void Data::PrintData() const{
printf("============================= Print Data\n");
for( int ch = 0; ch < MaxNChannels ; ch++){
if( NumEvents[ch] == 0 ) continue;
printf("------------ ch : %d, %d \n", ch, NumEvents[ch]);
2022-10-19 18:14:58 -04:00
for( int ev = 0; ev < NumEvents[ch] ; ev++){
printf("%4d, %5u, %15llu, %5u \n", ev, Energy[ch][ev], Timestamp[ch][ev], fineTime[ch][ev]);
}
}
}
2022-10-07 17:03:27 -04:00
//####################################################### Decode
inline unsigned int Data::ReadBuffer(unsigned int nWord, int verbose){
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 >= 2) printf("%6d | 0x%08X | ", nWord, word);
return word;
}
inline void Data::DecodeBuffer(char * buffer, unsigned int size, bool fastDecode, int verbose){
2022-08-24 17:50:21 -04:00
this->buffer = buffer;
this->nByte = size;
2022-10-07 16:15:58 -04:00
DecodeBuffer(fastDecode, verbose);
2022-08-24 17:50:21 -04:00
}
2022-10-07 16:15:58 -04:00
inline void Data::DecodeBuffer(bool fastDecode, int verbose){
2022-08-23 13:43:05 -04:00
/// verbose : 0 = off, 1 = only energy + timestamp, 2 = show header, 3 = wave
if( buffer == NULL ) {
2022-08-26 17:18:43 -04:00
if( verbose >= 1 ) printf(" buffer is empty \n");
2022-08-23 13:43:05 -04:00
return;
}
if( nByte == 0 ) {
return;
}
2022-08-26 17:18:43 -04:00
nw = 0;
2022-08-23 13:43:05 -04:00
2022-10-19 18:14:58 -04:00
ClearTriggerRate();
2022-08-23 13:43:05 -04:00
do{
if( verbose >= 1 ) printf("Data::DecodeBuffer ######################################### Board Agg.\n");
2022-08-23 13:43:05 -04:00
unsigned int word = ReadBuffer(nw, verbose);
if( ( (word >> 28) & 0xF ) == 0xA ) { /// start of Board Agg
unsigned int nWord = word & 0x0FFFFFFF ;
if( verbose >= 1 ) printf("Number of words in this Agg : %u = %u Byte\n", nWord, nWord * 4);
2022-08-23 13:43:05 -04:00
nw = nw + 1; word = ReadBuffer(nw, verbose);
unsigned int BoardID = ((word >> 27) & 0x1F);
2022-10-26 14:55:06 -04:00
unsigned short pattern = ((word >> 8 ) & 0x7FFF );
2022-08-23 13:43:05 -04:00
bool BoardFailFlag = ((word >> 26) & 0x1 );
unsigned int ChannelMask = ( word & 0xFF ) ;
2022-10-26 14:55:06 -04:00
if( verbose >= 1 ) printf("Board ID(type) : %d, FailFlag = %d, Patten = %u, ChannelMask = 0x%X\n",
BoardID, BoardFailFlag, pattern, ChannelMask);
if( BoardID > 0 ) {
switch(BoardID){
case 0x8 : DPPType = V1730_DPP_PSD_CODE; break;
case 0xB : DPPType = V1730_DPP_PHA_CODE; break;
}
}
2022-10-26 14:55:06 -04:00
nw = nw + 1;
unsigned int bdAggCounter = ReadBuffer(nw, verbose);
if( verbose >= 1 ) printf("Board Agg Counter : %u \n", bdAggCounter & 0x7FFFFF);
nw = nw + 1;
unsigned int bdAggTimeTag = ReadBuffer(nw, verbose);
if( verbose >= 2 ) printf("Agg Counter : %u \n", bdAggTimeTag);
2022-08-17 16:08:49 -04:00
for( int chMask = 0; chMask < MaxNChannels/2 ; chMask ++ ){
2022-08-23 13:43:05 -04:00
if( ((ChannelMask >> chMask) & 0x1 ) == 0 ) continue;
2022-10-19 18:14:58 -04:00
if( verbose >= 2 ) printf("==================== Dual Channel Block, ch Mask : %d, nw : %d\n", chMask *2, nw);
2022-08-23 15:49:03 -04:00
if( DPPType == V1730_DPP_PHA_CODE ) {
2022-10-07 16:15:58 -04:00
if ( DecodePHADualChannelBlock(chMask, fastDecode, verbose) < 0 ) break;
2022-08-23 13:43:05 -04:00
}
2022-08-23 15:49:03 -04:00
if( DPPType == V1730_DPP_PSD_CODE ) {
2022-10-07 16:15:58 -04:00
if ( DecodePHADualChannelBlock(chMask, fastDecode, verbose) < 0 ) break;
2022-08-17 16:08:49 -04:00
}
2022-08-23 13:43:05 -04:00
}
}else{
if( verbose >= 1 ) printf("nw : %d, incorrect buffer header. \n", nw);
2022-08-23 13:43:05 -04:00
break;
2022-08-17 16:08:49 -04:00
}
2022-08-23 13:43:05 -04:00
nw++;
///printf("nw : %d ,x 4 = %d, nByte : %d \n", nw, 4*nw, nByte);
}while(4*nw < nByte);
2022-10-19 18:14:58 -04:00
///Calculate trigger rate and first and last Timestamp
for(int ch = 0; ch < MaxNChannels; ch++){
2022-10-26 14:55:06 -04:00
if( NumEventsDecoded[ch] > 0 ) IsNotRollOverFakeAgg = true;
2022-10-19 18:14:58 -04:00
unsigned long long dTime = Timestamp[ch][NumEvents[ch]-1] - Timestamp[ch][NumEvents[ch] - NumEventsDecoded[ch]];
double sec = dTime * ch2ns / 1e9;
if( sec != 0 && NumEventsDecoded[ch] > 1 ){
2022-10-31 21:26:10 -04:00
TriggerRate[ch] = NumEventsDecoded[ch]/sec;
}
}
2022-08-23 13:43:05 -04:00
}
2022-10-07 16:15:58 -04:00
inline int Data::DecodePHADualChannelBlock(unsigned int ChannelMask, bool fastDecode, int verbose){
2022-08-23 15:49:03 -04:00
2022-08-26 17:18:43 -04:00
nw = nw + 1;
2022-08-23 15:49:03 -04:00
unsigned int word = ReadBuffer(nw, verbose);
bool hasFormatInfo = ((word >> 31) & 0x1);
unsigned int aggSize = ( word & 0x7FFFFFFF ) ;
if( verbose >= 2 ) printf("Dual Channel size : %d \n", aggSize);
2022-08-23 15:49:03 -04:00
unsigned int nSample = 0; /// wave form;
unsigned int nEvents = 0;
unsigned int extra2Option = 0;
2022-10-19 18:14:58 -04:00
bool hasExtra2 = false;
2022-08-26 17:18:43 -04:00
bool hasDualTrace = 0 ;
2022-08-23 15:49:03 -04:00
if( hasFormatInfo ){
nw = nw + 1; word = ReadBuffer(nw, verbose);
nSample = ( word & 0xFFFF ) * 8;
extra2Option = ( (word >> 24 ) & 0x7 );
2022-10-19 18:14:58 -04:00
hasExtra2 = ( (word >> 28 ) & 0x1 );
if( !fastDecode || verbose >= 2){
2022-10-24 17:01:05 -04:00
unsigned int digitalProbe = ( (word >> 16 ) & 0xF );
unsigned int analogProbe2 = ( (word >> 20 ) & 0x3 );
unsigned int analogProbe1 = ( (word >> 22 ) & 0x3 );
bool hasWaveForm = ( (word >> 27 ) & 0x1 );
bool hasTimeStamp = ( (word >> 29 ) & 0x1 );
bool hasEnergy = ( (word >> 30 ) & 0x1 );
hasDualTrace = ( (word >> 31 ) & 0x1 );
if( verbose >= 2 ) {
printf("DualTrace : %d, Energy : %d, Time: %d, Wave : %d, Extra2: %d \n",
hasDualTrace, hasEnergy, hasTimeStamp, hasWaveForm, hasExtra2);
2022-08-23 15:49:03 -04:00
}
2022-10-24 17:01:05 -04:00
if( verbose >= 3){
if( hasExtra2 ){
printf("...... extra 2 : ");
switch (extra2Option){
case 0: printf("[0:15] trapwzoid baseline * 4 [16:31] Extended timestamp (16-bit)\n"); break;
case 1: printf("Reserved\n"); break;
case 2: printf("[0:9] Fine time stamp [10:15] Reserved [16:31] Extended timestamp (16-bit)\n"); break;
case 3: printf("Reserved\n"); break;
case 4: printf("[0:15] Total trigger counter [16:31] Lost trigger counter\n"); break;
case 5: printf("[0:15] Event after Zero crossing [16:31] Event before Zero crossing\n"); break;
case 6: printf("Reserved\n"); break;
case 7: printf("Reserved\n"); break;
}
}
printf("...... Analog Probe 1 : ");
switch (analogProbe1 ){
case 0 : printf("Input \n"); break;
case 1 : printf("RC-CR (1st derivative) \n"); break;
case 2 : printf("RC-CR2 (2st derivative) \n"); break;
case 3 : printf("trapazoid \n"); break;
}
printf("...... Analog Probe 2 : ");
switch (analogProbe2 ){
case 0 : printf("Input \n"); break;
case 1 : printf("Theshold \n"); break;
case 2 : printf("trapezoid - baseline \n"); break;
case 3 : printf("baseline \n"); break;
}
printf("...... Digital Probe : ");
switch (digitalProbe ){
case 0 : printf("Peaking \n"); break;
case 1 : printf("Armed (trigger) \n"); break;
case 2 : printf("Peak Run \n"); break;
case 3 : printf("Pile up \n"); break;
case 4 : printf("Peaking \n"); break;
case 5 : printf("Trigger Validation Window \n"); break;
case 6 : printf("Baseline for energy calculation \n"); break;
case 7 : printf("Trigger holdoff \n"); break;
case 8 : printf("Trigger Validation \n"); break;
case 9 : printf("ACQ Busy \n"); break;
case 10 : printf("Trigger window \n"); break;
case 11 : printf("Ext. Trigger \n"); break;
case 12 : printf("Busy = memory is full \n"); break;
}
2022-08-23 15:49:03 -04:00
}
}
nEvents = (aggSize - 2) / (nSample/2 + 2 + hasExtra2 );
2022-10-19 18:14:58 -04:00
if( verbose >= 2 ) printf("-------------------nEvents : %d \n", nEvents);
2022-08-23 15:49:03 -04:00
}else{
if( verbose >= 2 ) printf("does not has format info. unable to read buffer.\n");
2022-08-26 17:18:43 -04:00
return 0;
2022-08-23 15:49:03 -04:00
}
///========== decode an event
for( int ev = 0; ev < nEvents ; ev++){
2022-10-19 18:14:58 -04:00
if( verbose >= 2 ) printf("------ event : %d\n", ev);
2022-08-23 15:49:03 -04:00
nw = nw +1 ; word = ReadBuffer(nw, verbose);
bool channelTag = ((word >> 31) & 0x1);
unsigned int timeStamp0 = (word & 0x7FFFFFFF);
int channel = ChannelMask*2 + channelTag;
if( verbose >= 2 ) printf("ch : %d, timeStamp0 %u \n", channel, timeStamp0);
2022-08-26 17:18:43 -04:00
//TODO Skip
2022-08-23 15:49:03 -04:00
///===== read waveform
2022-10-24 17:01:05 -04:00
if( !fastDecode && SaveWaveToMemory ) {
2022-08-26 17:18:43 -04:00
tempWaveform1.clear();
tempWaveform2.clear();
tempDigiWaveform1.clear();
}
2022-08-23 15:49:03 -04:00
unsigned int triggerAtSample = 0 ;
2022-10-24 17:01:05 -04:00
if( fastDecode ){
nw += nSample/2;
}else{
for( int wi = 0; wi < nSample/2; wi++){
nw = nw +1 ; word = ReadBuffer(nw, verbose-2);
///The CAEN manual is wrong, the bit [31:16] is anaprobe 1
2022-10-24 17:01:05 -04:00
bool isTrigger1 = (( word >> 31 ) & 0x1 );
bool dp1 = (( word >> 30 ) & 0x1 );
unsigned short wave1 = (( word >> 16) & 0x3FFF);
///The CAEN manual is wrong, the bit [31:16] is anaprobe 2
2022-10-24 17:01:05 -04:00
bool isTrigger0 = (( word >> 15 ) & 0x1 );
bool dp0 = (( word >> 14 ) & 0x1 );
unsigned short wave0 = ( word & 0x3FFF);
2022-10-24 17:01:05 -04:00
if( SaveWaveToMemory){
if( hasDualTrace ){
tempWaveform1.push_back(wave1);
tempWaveform2.push_back(wave0);
tempDigiWaveform1.push_back(dp0);
2022-10-24 17:01:05 -04:00
}else{
tempWaveform1.push_back(wave1);
tempWaveform1.push_back(wave0);
tempDigiWaveform1.push_back(dp0);
tempDigiWaveform1.push_back(dp1);
2022-10-24 17:01:05 -04:00
}
}
2022-08-26 17:18:43 -04:00
2022-10-24 17:01:05 -04:00
if( isTrigger0 == 1 ) triggerAtSample = 2*wi ;
if( isTrigger1 == 1 ) triggerAtSample = 2*wi + 1;
if( verbose >= 4 && ev == 0 ){
if( !hasDualTrace ){
printf("%4d| %5d, %d, %d \n", 2*wi, wave0, dp0, isTrigger0);
printf("%4d| %5d, %d, %d \n", 2*wi+1, wave1, dp1, isTrigger1);
}else{
printf("%4d| %5d, %5d | %d, %d | %d %d\n", wi, wave0, wave1, dp0, dp1, isTrigger0, isTrigger1);
}
2022-08-26 17:18:43 -04:00
}
}
2022-08-23 15:49:03 -04:00
2022-10-24 17:01:05 -04:00
if( SaveWaveToMemory ) {
if( hasDualTrace ){
Waveform1[channel][NumEvents[channel]] = tempWaveform1;
Waveform2[channel][NumEvents[channel]] = tempWaveform2;
}else{
Waveform1[channel][NumEvents[channel]] = tempWaveform1;
}
DigiWaveform1[channel][NumEvents[channel]] = tempDigiWaveform1;
}
2022-08-26 17:18:43 -04:00
}
2022-08-23 15:49:03 -04:00
unsigned long long extTimeStamp = 0;
2022-10-19 18:14:58 -04:00
unsigned int extra2 = 0;
if( hasExtra2 ){
nw = nw +1 ; word = ReadBuffer(nw, verbose);
extra2 = word;
if( extra2Option == 0 || extra2Option == 2 ) extTimeStamp = (extra2 >> 16);
}
unsigned long long timeStamp = (extTimeStamp << 31) ;
2022-08-23 15:49:03 -04:00
timeStamp = timeStamp + timeStamp0;
if( verbose >= 2 && hasExtra2 ) printf("extra2 : 0x%0X, TimeStamp : %llu\n", extra2, timeStamp);
2022-08-23 15:49:03 -04:00
nw = nw +1 ; word = ReadBuffer(nw, verbose);
unsigned int extra = (( word >> 16) & 0x3FF);
unsigned int energy = (word & 0x7FFF);
bool rollOver = (extra & 0x002);
2022-10-24 17:01:05 -04:00
bool pileUp = (extra & 0x200);
bool pileUpOrRollOver = ((word >> 15) & 0x1);
2022-08-23 15:49:03 -04:00
if( verbose >= 3 ) {
2022-10-24 17:01:05 -04:00
printf("PileUp : %d , extra : 0x%03x, energy : %d \n", pileUp, extra, energy);
2022-08-23 15:49:03 -04:00
printf(" lost event : %d \n", ((extra >> 0) & 0x1) );
printf(" roll-over : %d (fake event)\n", ((extra >> 1) & 0x1) );
2022-08-23 15:49:03 -04:00
printf(" fake-event : %d \n", ((extra >> 3) & 0x1) );
printf(" input sat. : %d \n", ((extra >> 4) & 0x1) );
printf(" lost trg : %d \n", ((extra >> 5) & 0x1) );
printf(" tot trg : %d \n", ((extra >> 6) & 0x1) );
printf(" coincident : %d \n", ((extra >> 7) & 0x1) );
printf(" not coin. : %d \n", ((extra >> 8) & 0x1) );
printf(" pile-up : %d \n", ((extra >> 9) & 0x1) );
printf(" trapezoid sat. : %d \n", ((extra >> 10) & 0x1) );
}
2022-09-28 15:09:27 -04:00
if( rollOver == 0 ) {
2022-09-28 15:09:27 -04:00
Energy[channel][NumEvents[channel]] = energy;
Timestamp[channel][NumEvents[channel]] = timeStamp;
if(extra2Option == 0 || extra2Option == 2 ) fineTime[channel][NumEvents[channel]] = (extra2 & 0x07FF );
NumEvents[channel] ++;
2022-10-19 18:14:58 -04:00
NumEventsDecoded[channel] ++;
TotNumEvents[channel] ++;
}
2022-10-19 18:14:58 -04:00
if( verbose >= 1 ) printf("%4d | ch : %2d, PileUp : %d , energy : %5d, rollOver: %d, timestamp : %10llu, triggerAt : %d, nSample : %d, %f sec\n",
NumEvents[channel], channel, pileUp, energy, rollOver, timeStamp, triggerAtSample, nSample , timeStamp * 4. / 1e9);
2022-08-26 17:18:43 -04:00
2022-08-23 15:49:03 -04:00
}
///=========== Key information
/// ch, energy, timestamp
/// trace
2022-10-19 18:14:58 -04:00
2022-08-23 15:49:03 -04:00
return nw;
}
2022-10-07 16:15:58 -04:00
inline int Data::DecodePSDDualChannelBlock(unsigned int ChannelMask, bool fastDecode, int verbose){
2022-08-26 17:18:43 -04:00
nw = nw + 1;
2022-08-23 15:49:03 -04:00
unsigned int word = ReadBuffer(nw, verbose);
2022-08-26 17:18:43 -04:00
if( (word >> 31) != 1 ) return 0;
2022-08-23 15:49:03 -04:00
unsigned int aggSize = ( word & 0x3FFFFF ) ;
if( verbose >= 2 ) printf(" size : %d \n", aggSize);
unsigned int nEvents = 0;
nw = nw + 1; word = ReadBuffer(nw, verbose);
unsigned int nSample = ( word & 0xFFFF ) * 8;
unsigned int digitalProbe1 = ( (word >> 16 ) & 0x7 );
unsigned int digitalProbe2 = ( (word >> 19 ) & 0x7 );
unsigned int analogProbe = ( (word >> 22 ) & 0x3 );
unsigned int extraOption = ( (word >> 24 ) & 0x7 );
bool hasWaveForm = ( (word >> 27 ) & 0x1 );
bool hasExtra = ( (word >> 28 ) & 0x1 );
bool hasTimeStamp = ( (word >> 29 ) & 0x1 );
bool hasCharge = ( (word >> 30 ) & 0x1 );
bool hasDualTrace = ( (word >> 31 ) & 0x1 );
if( verbose >= 2 ) {
printf("dualTrace : %d, Charge : %d, Time: %d, Wave : %d, Extra: %d\n",
hasDualTrace, hasCharge, hasTimeStamp, hasWaveForm, hasExtra);
if( hasExtra ){
printf(".... extra : ");
switch(extraOption){
case 0: printf("[0:15] trapwzoid baseline * 4 [16:31] Extended timestamp (16-bit)\n"); break;
case 1: printf("[0:11] reserved [12] lost trigger counted [13] 1024 trigger counted [14] Over-range\n");
printf("[15] trigger lost [16:31] Extended timestamp (16-bit)\n"); break;
case 2: printf("[0:9] Fine time stamp [10:15] flag [10:15] Reserved [16:31] Extended timestamp (16-bit)\n"); break;
case 3: printf("Reserved\n"); break;
case 4: printf("[0:15] Total trigger counter [16:31] Lost trigger counter\n"); break;
case 5: printf("[0:15] Event after Zero crossing [16:31] Event before Zero crossing\n"); break;
case 6: printf("Reserved\n"); break;
case 7: printf("debug, must be 0x12345678\n"); break;
}
}
printf(".... digital Probe 1 : ");
switch(digitalProbe1){
case 0 : printf("Long gate \n"); break;
case 1 : printf("Over threshold \n"); break;
case 2 : printf("Shaped TRG \n"); break;
case 3 : printf("TRG Val. Acceptance \n"); break;
case 4 : printf("Pile-Up \n"); break;
case 5 : printf("Coincidence \n"); break;
case 6 : printf("Reserved \n"); break;
case 7 : printf("Trigger \n"); break;
}
printf(".... digital Probe 2 : ");
switch(digitalProbe2){
case 0 : printf("Short gate \n"); break;
case 1 : printf("Over threshold \n"); break;
case 2 : printf("TRG Validation \n"); break;
case 3 : printf("TRG HoldOff \n"); break;
case 4 : printf("Pile-Up \n"); break;
case 5 : printf("Coincidence \n"); break;
case 6 : printf("Reserved \n"); break;
case 7 : printf("Trigger \n"); break;
}
printf(".... analog Probe (dual trace : %d): ", hasDualTrace);
if( hasDualTrace ) {
switch(analogProbe){
case 0 : printf("Input and baseline \n"); break;
case 1 : printf("CFD and baseline \n"); break;
case 2 : printf("Input and CFD \n"); break;
}
}else{
switch(analogProbe){
case 0 : printf("Input \n"); break;
case 1 : printf("CFD \n"); break;
}
}
}
nEvents = (aggSize -2) / (nSample/2 + 2 + hasExtra );
2022-10-19 18:14:58 -04:00
if( verbose >= 2 ) printf("----------------- nEvents : %d \n", nEvents);
2022-08-23 15:49:03 -04:00
///========= Decode an event
for( int ev = 0; ev < nEvents ; ev++){
2022-10-19 18:14:58 -04:00
if( verbose >= 2 ) printf("--------------------------- event : %d\n", ev);
2022-08-23 15:49:03 -04:00
nw = nw +1 ; word = ReadBuffer(nw, verbose);
bool channelTag = ((word >> 31) & 0x1);
unsigned int timeStamp0 = (word & 0x7FFFFFFF);
int channel = ChannelMask*2 + channelTag;
if( verbose >= 2 ) printf("ch : %d, timeStamp %u \n", channel, timeStamp0);
///===== read waveform
2022-10-24 17:01:05 -04:00
if( !fastDecode && SaveWaveToMemory ) {
2022-08-26 17:18:43 -04:00
tempWaveform1.clear();
tempWaveform2.clear();
tempDigiWaveform1.clear();
tempDigiWaveform2.clear();
}
2022-10-24 17:01:05 -04:00
if( fastDecode ){
nw += nSample/2;
}else{
for( int wi = 0; wi < nSample/2; wi++){
nw = nw +1 ; word = ReadBuffer(nw, verbose - 2);
bool dp2b = (( word >> 31 ) & 0x1 );
bool dp1b = (( word >> 30 ) & 0x1 );
unsigned short waveb = (( word >> 16) & 0x3FFF);
bool dp2a = (( word >> 15 ) & 0x1 );
bool dp1a = (( word >> 14 ) & 0x1 );
unsigned short wavea = ( word & 0x3FFF);
if( SaveWaveToMemory){
if( hasDualTrace ){
tempWaveform1.push_back(wavea);
tempWaveform2.push_back(waveb);
}else{
tempWaveform1.push_back(wavea);
tempWaveform1.push_back(waveb);
}
tempDigiWaveform1.push_back(dp1a);
tempDigiWaveform1.push_back(dp1b);
tempDigiWaveform2.push_back(dp2a);
tempDigiWaveform2.push_back(dp2b);
}
if( verbose >= 3 && ev == 0 ){
printf("%4d| %5d, %d, %d \n", 2*wi, wavea, dp1a, dp2a);
printf("%4d| %5d, %d, %d \n", 2*wi+1, waveb, dp1b, dp2b);
}
}
if( SaveWaveToMemory ) {
2022-08-26 17:18:43 -04:00
if( hasDualTrace ){
2022-10-24 17:01:05 -04:00
Waveform1[channel][NumEvents[channel]] = tempWaveform1;
Waveform2[channel][NumEvents[channel]] = tempWaveform2;
2022-08-26 17:18:43 -04:00
}else{
2022-10-24 17:01:05 -04:00
Waveform1[channel][NumEvents[channel]] = tempWaveform1;
2022-08-26 17:18:43 -04:00
}
2022-10-24 17:01:05 -04:00
DigiWaveform1[channel][NumEvents[channel]] = tempDigiWaveform1;
DigiWaveform2[channel][NumEvents[channel]] = tempDigiWaveform2;
2022-08-23 15:49:03 -04:00
}
}
nw = nw +1 ; word = ReadBuffer(nw, verbose);
unsigned int extra = word;
unsigned long long extTimeStamp = 0;
if( extraOption == 0 || extraOption == 2 ) extTimeStamp = (extra >> 16);
2022-08-23 15:49:03 -04:00
unsigned long long timeStamp = (extTimeStamp << 31) ;
2022-08-23 15:49:03 -04:00
timeStamp = timeStamp + timeStamp0;
nw = nw +1 ; word = ReadBuffer(nw, verbose);
unsigned int Qlong = (( word >> 16) & 0xFFFF);
unsigned int Qshort = (word & 0x7FFF);
bool isEnergyCorrect = ((word >> 15) & 0x1);
if( isEnergyCorrect == 1 ) {
NumEvents[channel] ++;
2022-10-19 18:14:58 -04:00
NumEventsDecoded[channel] ++;
TotNumEvents[channel] ++;
}
2022-08-23 15:49:03 -04:00
if( verbose >= 2 ) printf("extra : 0x%08x, Qshort : %d, Qlong : %d \n", extra, Qshort, Qlong);
if( verbose >= 1 ) printf("ch : %2d, Qshort : %d, Qlong : %d, timestamp : %llu\n",
channel, Qshort, Qlong, timeStamp);
2022-08-26 17:18:43 -04:00
Timestamp[channel][NumEvents[channel]] = timeStamp;
2022-08-23 15:49:03 -04:00
}
///=========== Key information
/// ch, Qshort, Qlong , timestamp
/// trace
return nw;
}
#endif