FSUDAQ/ClassData.h

206 lines
6.9 KiB
C
Raw 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-23 13:43:05 -04:00
//#include "CAENDigitizer.h"
//#include "CAENDigitizerType.h"
//#include "macro.h"
class Data{
public:
2022-08-23 13:43:05 -04:00
unsigned int nByte; /// number of byte
char *buffer; /// readout buffer
uint32_t AllocatedSize;
uint32_t BufferSize;
2022-08-23 13:43:05 -04:00
///uint32_t NumEvents[MaxNChannels];
///CAEN_DGTZ_DPP_PHA_Event_t *Events[MaxNChannels]; /// events buffer
///CAEN_DGTZ_DPP_PHA_Waveforms_t *Waveform[MaxNChannels]; /// waveforms buffer
public:
2022-08-23 13:43:05 -04:00
Data();
~Data();
2022-08-23 13:43:05 -04:00
void AllocateMemory();
void FreeMemory();
2022-08-17 16:08:49 -04:00
2022-08-23 13:43:05 -04:00
void SaveBuffer(char * fileName);
2022-08-23 13:43:05 -04:00
void DecodeBuffer(int verbose = 0);
protected:
unsigned int ReadBuffer(unsigned int nWord, int verbose = 0);
2022-08-17 16:08:49 -04:00
2022-08-23 13:43:05 -04:00
};
//==========================================
inline Data::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;
///}
}
inline Data::~Data(){
if( buffer != NULL ) delete buffer;
///for( int i = 0 ; i < MaxNChannels; i++){
/// delete Events [i];
/// delete Waveform [i];
///}
}
inline void Data::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);
}
inline void Data::FreeMemory(){
printf("======= Free memory\n");
delete buffer;
}
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("%d | 0x%08x\n", nWord, word);
return word;
}
inline void Data::SaveBuffer(char * fileName){
FILE * haha = fopen(fileName, "a+");
fwrite(buffer, nByte, 1, haha);
fclose(haha);
}
inline void Data::DecodeBuffer(int verbose){
/// verbose : 0 = off, 1 = only energy + timestamp, 2 = show header, 3 = wave
if( buffer == NULL ) {
printf(" buffer is empty \n");
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);
2022-08-23 13:43:05 -04:00
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);
2022-08-23 13:43:05 -04:00
nw = nw + 2;
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-23 13:43:05 -04:00
2022-08-17 16:08:49 -04:00
2022-08-23 13:43:05 -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);
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);
2022-08-23 13:43:05 -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-17 16:08:49 -04:00
2022-08-23 13:43:05 -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);
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);
2022-08-17 16:08:49 -04:00
2022-08-23 13:43:05 -04:00
///===== 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);
2022-08-17 16:08:49 -04:00
2022-08-23 13:43:05 -04:00
bool isTrigger0 = (( word >> 15 ) & 0x1 );
unsigned int wave0 = ( word & 0x3FFF);
2022-08-17 16:08:49 -04:00
2022-08-23 13:43:05 -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-23 13:43:05 -04:00
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);
2022-08-17 16:08:49 -04:00
}
2022-08-23 13:43:05 -04:00
}
}else{
if( verbose >= 2 ) printf("incorrect buffer header. \n");
break;
2022-08-17 16:08:49 -04:00
}
2022-08-23 13:43:05 -04:00
nw++;
}while(true);
}
#endif