FSUDAQ/test.cpp

281 lines
7.0 KiB
C++
Raw Normal View History

2022-08-23 13:43:05 -04:00
#include "ClassData.h"
2022-09-28 15:09:27 -04:00
#include "ClassDigitizer.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TGraph.h"
#include "TH1.h"
2022-10-19 18:14:58 -04:00
#include "TFile.h"
#include "TTree.h"
2022-09-28 15:09:27 -04:00
#include <sys/time.h> /** struct timeval, select() */
#include <termios.h> /** tcgetattr(), tcsetattr() */
2022-09-28 15:09:27 -04:00
static struct termios g_old_kbd_mode;
static void cooked(void){
tcsetattr(0, TCSANOW, &g_old_kbd_mode);
}
static void uncooked(void){
struct termios new_kbd_mode;
/** put keyboard (stdin, actually) in raw, unbuffered mode */
2022-09-28 15:09:27 -04:00
tcgetattr(0, &g_old_kbd_mode);
memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
new_kbd_mode.c_lflag &= ~(ICANON | ECHO);
new_kbd_mode.c_cc[VTIME] = 0;
new_kbd_mode.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_kbd_mode);
}
static void raw(void){
static char init;
if(init) return;
/** put keyboard (stdin, actually) in raw, unbuffered mode */
2022-09-28 15:09:27 -04:00
uncooked();
/** when we exit, go back to normal, "cooked" mode */
2022-09-28 15:09:27 -04:00
atexit(cooked);
init = 1;
}
int keyboardhit(){
struct timeval timeout;
fd_set read_handles;
int status;
raw();
/** check stdin (fd 0) for activity */
2022-09-28 15:09:27 -04:00
FD_ZERO(&read_handles);
FD_SET(0, &read_handles);
timeout.tv_sec = timeout.tv_usec = 0;
status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
if(status < 0){
printf("select() failed in keyboardhit()\n");
exit(1);
}
return (status);
}
int getch(void){
unsigned char temp;
raw();
/** stdin = fd 0 */
2022-09-28 15:09:27 -04:00
if(read(0, &temp, 1) != 1) return 0;
return temp;
}
2022-10-19 18:14:58 -04:00
unsigned long get_time(){
unsigned long time_us;
2022-09-28 15:09:27 -04:00
struct timeval t1;
struct timezone tz;
gettimeofday(&t1, &tz);
2022-10-19 18:14:58 -04:00
time_us = (t1.tv_sec) * 1000000 + t1.tv_usec;
return time_us;
2022-09-28 15:09:27 -04:00
}
2022-08-03 19:00:41 -04:00
int main(int argc, char* argv[]){
/**////##################### Demo for loading and change setting without open a digitizer
Digitizer * dig = new Digitizer();
dig->OpenDigitizer(0, 1, false, true);
dig->LoadSettingBinary("setting_323.bin");
//dig->ProgramPHABoard();
//dig->OpenSettingBinary("setting_323.bin");
//dig->ReadAllSettingsFromBoard();
//dig->PrintSettingFromMemory();
dig->WriteRegister(Register::DPP::SoftwareClear_W, 1);
///dig->SaveSettingAsText("haha.txt");
std::remove("Test_323_139_000.fsu");
2022-10-24 17:01:05 -04:00
{///============ Checking the buffer size calculation
unsigned short B = 10; /// BLT
unsigned short Eg = 511; /// Event / dual channel
bool DT = 1; /// dual trace
bool E2 = 1; /// extra 2;
bool Wr = 1; /// wave record;
unsigned short AP2 = 0; /// 00 = input, 01 = Threshold, 10 = Trapezoid - Baseline, 11 = baseline
unsigned short AP1 = 1; /// 00 = input, 01 = RC-CR, 10 = RC-CR2, 11 = Trapezoid
unsigned short DP1 = 0x0000; /// peaking,
unsigned short RL = 100; /// record Length
unsigned short AO = 0x0;
for( int i = 0; i < dig->GetNChannel(); i++){
dig->WriteRegister(Register::DPP::NumberEventsPerAggregate_G, Eg, i);
dig->WriteRegister(Register::DPP::RecordLength_G, RL, i);
}
dig->WriteRegister(Register::DPP::MaxAggregatePerBlockTransfer, B);
dig->WriteRegister(Register::DPP::AggregateOrganization, AO);
uint32_t bit = 0x0C0115;
bit += (DT << 11);
bit += (AP1 << 12);
bit += (AP2 << 14);
bit += (Wr << 16);
bit += (E2 << 17);
bit += (DP1 << 20);
printf("---- Bd Config : 0x%08X \n", bit);
dig->WriteRegister(Register::DPP::BoardConfiguration, bit);
2022-10-24 17:01:05 -04:00
unsigned int bSize = dig->CalByteForBuffer();
int bbbb = (((2 + E2 + Wr*RL*4) * Eg + 2)*8 + 2)*B *4 *2 + 4 * 4;
printf("=========== exp Buffer size : %8u byte \n", bbbb);
2022-10-24 17:01:05 -04:00
usleep(1e6);
///using CAEN method
char * buffer = NULL;
uint32_t size;
CAEN_DGTZ_MallocReadoutBuffer(dig->GetHandle(), (char **)& buffer, &size);
printf("CAEN calculated Buffer Size : %8u byte = %.2f MB \n", size, size/1024./1024.);
printf(" diff : %8u byte \n", size > 2*bSize ? size - 2*bSize : 2*bSize - size);
2022-10-24 17:01:05 -04:00
delete buffer;
}/**/
dig->GetData()->SetSaveWaveToMemory(true);
dig->StartACQ();
for( int i = 0; i < 2; i++){
usleep(1000000);
dig->ReadData();
printf("------------------- %d\n", i);
2022-10-24 17:01:05 -04:00
unsigned long time1 = get_time();
dig->GetData()->DecodeBuffer(false,4);
2022-10-24 17:01:05 -04:00
unsigned long time2 = get_time();
printf("********************* decode time : %lu usec\n", time2-time1);
dig->GetData()->PrintStat();
//dig->GetData()->SaveBuffer("Test");
}
dig->StopACQ();
2022-10-19 18:14:58 -04:00
/**///##################### test with 2 digitizers
2022-10-19 18:14:58 -04:00
/**
const int nBoard = 2;
2022-08-26 17:18:43 -04:00
Digitizer **dig = new Digitizer *[nBoard];
2022-08-03 19:00:41 -04:00
for( int i = 0 ; i < nBoard; i++){
int board = i % 3;
int port = i/3;
2022-09-26 16:47:20 -04:00
dig[i] = new Digitizer(board, port, false, true);
2022-10-19 18:14:58 -04:00
dig[i]->OpenSettingBinary("setting_" + to_string(dig[i]->GetSerialNumber()) + ".bin");
dig[i]->LoadSettingBinary("setting_" + to_string(dig[0]->GetSerialNumber()) + ".bin");
}
2022-10-07 16:15:58 -04:00
dig[0]->SaveSettingAsText("haha.txt");
2022-10-06 13:06:11 -04:00
2022-10-19 18:14:58 -04:00
delete dig[0];
delete dig[1];
2022-10-06 13:06:11 -04:00
2022-09-28 15:09:27 -04:00
TApplication * app = new TApplication("app", &argc, argv);
TCanvas * canvas = new TCanvas("c", "haha", 1200, 400);
canvas->Divide(3, 1);
TH1F * h1 = new TH1F("h1", "count", dig[0]->GetNChannel(), 0, dig[0]->GetNChannel());
TH1F * h2 = new TH1F("h2", "energy ch-0", 400, 0, 40000);
TGraph * g1 = new TGraph();
canvas->cd(1); h1->Draw("hist");
canvas->cd(2); h2->Draw();
canvas->cd(3); g1->Draw("AP");
2022-08-03 19:00:41 -04:00
Data * data = dig[0]->GetData();
2022-08-17 16:08:49 -04:00
data->AllocateMemory();
remove("test.bin");
dig[0]->StartACQ();
2022-09-28 15:09:27 -04:00
std::vector<unsigned short> haha ;
2022-09-28 15:09:27 -04:00
uint32_t PreviousTime = get_time();
uint32_t CurrentTime = 0;
uint32_t ElapsedTime = 0;
while(true){
if(keyboardhit()) {
break;
}
usleep(50000);
dig[0]->ReadData();
2022-09-28 15:09:27 -04:00
if( data->nByte > 0 ){
data->SaveBuffer("test");
2022-09-28 15:09:27 -04:00
data->DecodeBuffer(0);
unsigned short nData = data->NumEvents[0]; //channel-0
haha = data->Waveform1[0][nData-1];
2022-09-28 15:09:27 -04:00
for( int i = 0; i < waveFormLength; i++) g1->SetPoint(i, i*ch2ns, haha[i]);
canvas->cd(3); g1->Draw("AP");
canvas->Modified();
2022-09-28 15:09:27 -04:00
canvas->Update();
gSystem->ProcessEvents();
}
CurrentTime = get_time();
ElapsedTime = CurrentTime - PreviousTime; /// milliseconds
if( ElapsedTime > 1000 ){
2022-09-28 17:54:33 -04:00
int temp = system("clear");
2022-09-28 15:09:27 -04:00
data->PrintStat();
for(int i = 0; i < dig[0]->GetNChannel(); i++){
h1->Fill(i, data->NumEvents[i]);
}
for( int i = 0; i < data->NumEvents[0]; i++){
h2->Fill( data->Energy[0][i]);
}
data->ClearData();
canvas->cd(1); h1->Draw("hist");
canvas->cd(2); h2->Draw();
canvas->Modified();
canvas->Update();
gSystem->ProcessEvents();
2022-09-28 15:09:27 -04:00
PreviousTime = CurrentTime;
printf("Press any key to Stop\n");
2022-09-28 15:09:27 -04:00
}
}
dig[0]->StopACQ();
2022-08-03 19:00:41 -04:00
2022-09-28 15:09:27 -04:00
app->Run();
delete [] dig;
2022-08-03 19:00:41 -04:00
2022-08-26 17:18:43 -04:00
/*********************/
2022-08-23 13:43:05 -04:00
2022-08-03 19:00:41 -04:00
return 0;
}