FSUDAQ_Qt6/Hit.h

90 lines
1.9 KiB
C
Raw Normal View History

#ifndef Hit_H
#define Hit_H
2024-02-19 11:53:29 -05:00
#include <vector>
class Hit{
public:
2024-01-19 03:07:16 -05:00
unsigned short sn;
2024-07-17 14:53:49 -04:00
unsigned short ch;
unsigned short energy;
unsigned short energy2;
unsigned long long timestamp;
unsigned short fineTime;
bool pileUp;
2024-01-19 03:07:16 -05:00
unsigned short traceLength;
std::vector<short> trace;
Hit(){
Clear();
}
void Clear(){
sn = 0;
ch = 0;
energy = 0;
energy2 = 0;
timestamp = 0;
fineTime = 0;
2024-01-19 03:07:16 -05:00
traceLength = 0;
pileUp = false;
trace.clear();
}
void Print(){
printf("(%5d, %2d) %6d %16llu, %6d, %d, %5ld\n", sn, ch, energy, timestamp, fineTime, pileUp, trace.size());
}
2024-01-19 03:07:16 -05:00
void PrintTrace(){
for( unsigned short i = 0; i < traceLength; i++){
printf("%3u | %6d \n", i, trace[i]);
}
}
2024-03-04 12:22:23 -05:00
// Define operator< for sorting
bool operator<(const Hit& other) const {
return timestamp < other.timestamp;
2024-07-17 14:53:49 -04:00
}
void WriteHitsToCAENBinary(FILE * file, bool withTrace){
if( file == nullptr ) return;
uint16_t header = 0xCAE1; // default to have the energy only
uint32_t flag = 0;
uint8_t waveFormCode = 1; // input
size_t dummy;
if( energy2 > 0 ) header += 0x4;
if( traceLength > 0 && withTrace ) header += 0x8;
dummy = fwrite(&header, 2, 1, file);
dummy = fwrite(&sn, 2, 1, file);
dummy = fwrite(&ch, 2, 1, file);
uint64_t timestampPS = timestamp * 1000 + fineTime;
dummy = fwrite(&timestampPS, 8, 1, file);
dummy = fwrite(&energy, 2, 1, file);
if( energy2 > 0 ) dummy = fwrite(&energy2, 2, 1, file);
dummy = fwrite(&flag, 4, 1, file);
if( traceLength > 0 && withTrace ){
dummy = fwrite(&waveFormCode, 1, 1, file);
dummy = fwrite(&traceLength, 4, 1, file);
for( int j = 0; j < traceLength; j++ ){
dummy = fwrite(&(trace[j]), 2, 1, file);
}
}
if( dummy != 1 ) printf("write file error.\n");
}
};
#endif