2022-08-05 18:15:50 -04:00
|
|
|
#ifndef DATA_H
|
|
|
|
#define DATA_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstring> ///memset
|
|
|
|
#include <iostream> ///cout
|
|
|
|
#include <bitset>
|
|
|
|
|
|
|
|
#include "CAENDigitizer.h"
|
|
|
|
#include "CAENDigitizerType.h"
|
|
|
|
|
|
|
|
#include "macro.h"
|
|
|
|
|
|
|
|
class Data{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2022-08-17 16:08:49 -04:00
|
|
|
unsigned int nByte; /// number of byte
|
2022-08-05 18:15:50 -04:00
|
|
|
char *buffer; /// readout buffer
|
|
|
|
uint32_t NumEvents[MaxNChannels];
|
|
|
|
uint32_t AllocatedSize;
|
|
|
|
uint32_t BufferSize;
|
|
|
|
CAEN_DGTZ_DPP_PHA_Event_t *Events[MaxNChannels]; /// events buffer
|
|
|
|
CAEN_DGTZ_DPP_PHA_Waveforms_t *Waveform[MaxNChannels]; /// waveforms buffer
|
|
|
|
|
|
|
|
public:
|
|
|
|
Data(){
|
|
|
|
nByte = 0;
|
|
|
|
buffer = NULL;
|
|
|
|
AllocatedSize = 0;
|
|
|
|
BufferSize = 0;
|
|
|
|
for( int i = 0 ; i < MaxNChannels; i++){
|
|
|
|
NumEvents[i] = 0;
|
|
|
|
Events[i] = NULL;
|
|
|
|
Waveform[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~Data(){
|
2022-08-17 16:08:49 -04:00
|
|
|
delete buffer;
|
2022-08-05 18:15:50 -04:00
|
|
|
for( int i = 0 ; i < MaxNChannels; i++){
|
|
|
|
delete Events [i];
|
|
|
|
delete Waveform [i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:08:49 -04:00
|
|
|
void AllocateMemory(){
|
|
|
|
|
|
|
|
BufferSize = 100000; /// byte
|
|
|
|
buffer = (char *) malloc( BufferSize);
|
|
|
|
printf("Allocated %d byte for buffer \n", BufferSize);
|
|
|
|
|
|
|
|
for( int i = 0 ; i < MaxNChannels ; i++ ) Events[i] = (CAEN_DGTZ_DPP_PHA_Event_t *) malloc( BufferSize);
|
|
|
|
printf("Allocated %d byte for Events for each channel \n", BufferSize);
|
|
|
|
|
2022-08-10 18:35:13 -04:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:08:49 -04:00
|
|
|
void FreeMemory(){
|
|
|
|
printf("======= Free memory, not impletment yet \n");
|
2022-08-10 18:35:13 -04:00
|
|
|
}
|
2022-08-17 16:08:49 -04:00
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
void ReadAllBuffer(int verbose = 0){
|
|
|
|
|
|
|
|
/// verbose : 0 = off, 1 = only energy + timestamp, 2 = show header, 3 = wave
|
|
|
|
|
2022-08-17 16:08:49 -04:00
|
|
|
if( buffer == NULL ) return;
|
|
|
|
|
|
|
|
unsigned int nw = 0;
|
|
|
|
|
|
|
|
do{
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf("######################################### Board Agg.\n");
|
|
|
|
unsigned int word = ReadBuffer(nw, verbose);
|
2022-08-17 16:08:49 -04:00
|
|
|
if( ( (word >> 28) & 0xF ) == 0xA ) { /// start of Board Agg
|
|
|
|
unsigned int nWord = word & 0x0FFFFFFF ;
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf(" number of words in this Agg : %d \n", nWord);
|
2022-08-17 16:08:49 -04:00
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
nw = nw + 1; word = ReadBuffer(nw, verbose);
|
2022-08-17 16:08:49 -04:00
|
|
|
unsigned int BoardID = ((word >> 27) & 0x1F);
|
|
|
|
bool BoardFailFlag = ((word >> 26) & 0x1 );
|
|
|
|
unsigned int ChannelMask = ( word & 0xFF ) ;
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf("Board ID : %d, FailFlag = %d, ChannelMask = 0x%x\n", BoardID, BoardFailFlag, ChannelMask);
|
2022-08-17 16:08:49 -04:00
|
|
|
|
|
|
|
nw = nw + 2;
|
2022-08-18 17:34:28 -04:00
|
|
|
unsigned int AggCounter = ReadBuffer(nw, verbose);
|
|
|
|
if( verbose >= 2 ) printf("Agg Counter : %d \n", AggCounter);
|
2022-08-17 16:08:49 -04:00
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
|
2022-08-17 16:08:49 -04:00
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
for( int chMask = 0; chMask < 8 ; chMask ++ ){
|
|
|
|
if( ((ChannelMask >> chMask) & 0x1 ) == 0 ) continue;
|
|
|
|
if( verbose >= 2 ) printf("---------------------- Dual Channel Block : %d\n", chMask *2 );
|
|
|
|
nw = nw + 1; word = ReadBuffer(nw, verbose);
|
2022-08-17 16:08:49 -04:00
|
|
|
bool hasFormatInfo = ((word >> 31) & 0x1);
|
|
|
|
unsigned int aggSize = ( word & 0x3FFFFFF ) ;
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf(" size : %d \n", aggSize);
|
2022-08-17 16:08:49 -04:00
|
|
|
unsigned int nSample = 0; /// wave form;
|
|
|
|
unsigned int nEvents = 0;
|
|
|
|
if( hasFormatInfo ){
|
2022-08-18 17:34:28 -04:00
|
|
|
nw = nw + 1; word = ReadBuffer(nw, verbose);
|
2022-08-17 16:08:49 -04:00
|
|
|
nSample = ( word & 0xFFFF ) * 8;
|
|
|
|
unsigned int digitalProbe = ( (word >> 16 ) & 0xF );
|
|
|
|
unsigned int analogProbe2 = ( (word >> 20 ) & 0x3 );
|
|
|
|
unsigned int analogProbe1 = ( (word >> 22 ) & 0x3 );
|
|
|
|
unsigned int extra2Option = ( (word >> 24 ) & 0x7 );
|
|
|
|
bool hasWaveForm = ( (word >> 27 ) & 0x1 );
|
|
|
|
bool hasExtra2 = ( (word >> 28 ) & 0x1 );
|
|
|
|
bool hasTimeStamp = ( (word >> 29 ) & 0x1 );
|
|
|
|
bool hasEnergy = ( (word >> 30 ) & 0x1 );
|
|
|
|
bool hasDualTrace = ( (word >> 31 ) & 0x1 );
|
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf("dualTrace : %d, Energy : %d, Time: %d, Wave : %d, Extra2: %d, Extra2Option: %d \n",
|
|
|
|
hasDualTrace, hasEnergy, hasTimeStamp, hasWaveForm, hasExtra2, extra2Option);
|
|
|
|
if( verbose >= 2 ) printf("Ana Probe 1 & 2: %d %d , Digi Probe: %d, nSample : %d \n",
|
|
|
|
analogProbe1, analogProbe2, digitalProbe, nSample);
|
2022-08-17 16:08:49 -04:00
|
|
|
|
|
|
|
nEvents = aggSize / (nSample/2 + 2 + hasExtra2 );
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf("=========== nEvents : %d \n", nEvents);
|
2022-08-17 16:08:49 -04:00
|
|
|
}else{
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf("does not has format info. unable to read buffer.\n");
|
2022-08-17 16:08:49 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( int ev = 0; ev < nEvents ; ev++){
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf("=================================== event : %d\n", ev);
|
|
|
|
nw = nw +1 ; word = ReadBuffer(nw, verbose);
|
2022-08-17 16:08:49 -04:00
|
|
|
bool channelTag = ((word >> 31) & 0x1);
|
|
|
|
unsigned int timeStamp = (word & 0x7FFFFFFF);
|
2022-08-18 17:34:28 -04:00
|
|
|
int channel = chMask*2 + channelTag;
|
|
|
|
if( verbose >= 2 ) printf("ch : %d, timeStamp %u \n", channel, timeStamp);
|
2022-08-17 16:08:49 -04:00
|
|
|
|
|
|
|
///===== read waveform
|
|
|
|
for( int wi = 0; wi < nSample/2; wi++){
|
2022-08-18 17:34:28 -04:00
|
|
|
nw = nw +1 ; word = ReadBuffer(nw, verbose - 2);
|
2022-08-17 16:08:49 -04:00
|
|
|
bool isTrigger1 = (( word >> 31 ) & 0x1 );
|
|
|
|
unsigned int wave1 = (( word >> 16) & 0x3FFF);
|
|
|
|
|
|
|
|
bool isTrigger0 = (( word >> 15 ) & 0x1 );
|
|
|
|
unsigned int wave0 = ( word & 0x3FFF);
|
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 3 && ev == 0 ){
|
|
|
|
printf("%4d| %5d, %d \n", 2*wi, wave0, isTrigger0);
|
|
|
|
printf("%4d| %5d, %d \n", 2*wi+1, wave1, isTrigger1);
|
2022-08-17 16:08:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
nw = nw +1 ; word = ReadBuffer(nw, verbose);
|
2022-08-17 16:08:49 -04:00
|
|
|
unsigned int extra2 = word;
|
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
nw = nw +1 ; word = ReadBuffer(nw, verbose);
|
2022-08-17 16:08:49 -04:00
|
|
|
unsigned int extra = (( word >> 16) & 0x3FF);
|
|
|
|
unsigned int energy = (word & 0x7FFF);
|
|
|
|
bool pileUp = ((word >> 15) & 0x1);
|
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf("PileUp : %d , extra : 0x%04x, energy : %d \n", pileUp, extra, energy);
|
|
|
|
|
|
|
|
if( verbose >= 1 ) printf("ch : %2d, PileUp : %d , energy : %d, timestamp : %u\n",
|
|
|
|
channel, pileUp, energy, timeStamp);
|
2022-08-17 16:08:49 -04:00
|
|
|
|
|
|
|
}
|
2022-08-18 17:34:28 -04:00
|
|
|
}
|
2022-08-17 16:08:49 -04:00
|
|
|
}else{
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2 ) printf("incorrect buffer header. \n");
|
2022-08-17 16:08:49 -04:00
|
|
|
break;
|
|
|
|
}
|
2022-08-18 17:34:28 -04:00
|
|
|
nw++;
|
2022-08-17 16:08:49 -04:00
|
|
|
}while(true);
|
|
|
|
}
|
|
|
|
|
2022-08-18 17:34:28 -04:00
|
|
|
unsigned int ReadBuffer(unsigned int nWord, int verbose = 0){
|
2022-08-17 16:08:49 -04:00
|
|
|
if( buffer == NULL ) return 0;
|
|
|
|
|
|
|
|
unsigned int word = 0;
|
|
|
|
for( int i = 0 ; i < 4 ; i++) word += ((buffer[i + 4 * nWord] & 0xFF) << 8*i);
|
2022-08-18 17:34:28 -04:00
|
|
|
if( verbose >= 2) printf("%d | 0x%08x\n", nWord, word);
|
2022-08-17 16:08:49 -04:00
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
2022-08-05 18:15:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|