FSUDAQ/ClassData.h

185 lines
6.9 KiB
C++

#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:
unsigned int nByte; /// number of byte
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(){
delete buffer;
for( int i = 0 ; i < MaxNChannels; i++){
delete Events [i];
delete Waveform [i];
}
}
void AllocateMemory(){
BufferSize = 100000; /// byte
buffer = (char *) malloc( BufferSize);
printf("Allocated %d byte for buffer \n", BufferSize);
for( int i = 0 ; i < MaxNChannels ; i++ ) Events[i] = (CAEN_DGTZ_DPP_PHA_Event_t *) malloc( BufferSize);
printf("Allocated %d byte for Events for each channel \n", BufferSize);
}
void FreeMemory(){
printf("======= Free memory, not impletment yet \n");
}
void ReadAllBuffer(int verbose = 0){
/// verbose : 0 = off, 1 = only energy + timestamp, 2 = show header, 3 = wave
if( buffer == NULL ) return;
unsigned int nw = 0;
do{
if( verbose >= 2 ) printf("######################################### Board Agg.\n");
unsigned int word = ReadBuffer(nw, verbose);
if( ( (word >> 28) & 0xF ) == 0xA ) { /// start of Board Agg
unsigned int nWord = word & 0x0FFFFFFF ;
if( verbose >= 2 ) printf(" number of words in this Agg : %d \n", nWord);
nw = nw + 1; word = ReadBuffer(nw, verbose);
unsigned int BoardID = ((word >> 27) & 0x1F);
bool BoardFailFlag = ((word >> 26) & 0x1 );
unsigned int ChannelMask = ( word & 0xFF ) ;
if( verbose >= 2 ) printf("Board ID : %d, FailFlag = %d, ChannelMask = 0x%x\n", BoardID, BoardFailFlag, ChannelMask);
nw = nw + 2;
unsigned int AggCounter = ReadBuffer(nw, verbose);
if( verbose >= 2 ) printf("Agg Counter : %d \n", AggCounter);
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);
bool hasFormatInfo = ((word >> 31) & 0x1);
unsigned int aggSize = ( word & 0x3FFFFFF ) ;
if( verbose >= 2 ) printf(" size : %d \n", aggSize);
unsigned int nSample = 0; /// wave form;
unsigned int nEvents = 0;
if( hasFormatInfo ){
nw = nw + 1; word = ReadBuffer(nw, verbose);
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 );
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);
nEvents = aggSize / (nSample/2 + 2 + hasExtra2 );
if( verbose >= 2 ) printf("=========== nEvents : %d \n", nEvents);
}else{
if( verbose >= 2 ) printf("does not has format info. unable to read buffer.\n");
break;
}
for( int ev = 0; ev < nEvents ; ev++){
if( verbose >= 2 ) printf("=================================== event : %d\n", ev);
nw = nw +1 ; word = ReadBuffer(nw, verbose);
bool channelTag = ((word >> 31) & 0x1);
unsigned int timeStamp = (word & 0x7FFFFFFF);
int channel = chMask*2 + channelTag;
if( verbose >= 2 ) printf("ch : %d, timeStamp %u \n", channel, timeStamp);
///===== read waveform
for( int wi = 0; wi < nSample/2; wi++){
nw = nw +1 ; word = ReadBuffer(nw, verbose - 2);
bool isTrigger1 = (( word >> 31 ) & 0x1 );
unsigned int wave1 = (( word >> 16) & 0x3FFF);
bool isTrigger0 = (( word >> 15 ) & 0x1 );
unsigned int wave0 = ( word & 0x3FFF);
if( verbose >= 3 && ev == 0 ){
printf("%4d| %5d, %d \n", 2*wi, wave0, isTrigger0);
printf("%4d| %5d, %d \n", 2*wi+1, wave1, isTrigger1);
}
}
nw = nw +1 ; word = ReadBuffer(nw, verbose);
unsigned int extra2 = word;
nw = nw +1 ; word = ReadBuffer(nw, verbose);
unsigned int extra = (( word >> 16) & 0x3FF);
unsigned int energy = (word & 0x7FFF);
bool pileUp = ((word >> 15) & 0x1);
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);
}
}
}else{
if( verbose >= 2 ) printf("incorrect buffer header. \n");
break;
}
nw++;
}while(true);
}
unsigned int ReadBuffer(unsigned int nWord, int verbose = 0){
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("%d | 0x%08x\n", nWord, word);
return word;
}
};
#endif