2023-06-02 15:41:26 -04:00
|
|
|
#include "Analyser.h"
|
2023-06-27 15:47:34 -04:00
|
|
|
#include "CustomWidgets.h"
|
2023-05-26 18:06:37 -04:00
|
|
|
|
|
|
|
#include <QRandomGenerator>
|
|
|
|
#include <random>
|
|
|
|
|
2023-10-05 13:13:15 -04:00
|
|
|
Analyzer::Analyzer(Digitizer ** digi, unsigned int nDigi, QMainWindow * parent ): QMainWindow(parent), dataList(NULL){
|
2023-05-26 18:06:37 -04:00
|
|
|
|
|
|
|
this->digi = digi;
|
|
|
|
this->nDigi = nDigi;
|
|
|
|
|
2023-06-12 15:45:16 -04:00
|
|
|
setWindowTitle("Online Analyzer");
|
|
|
|
setGeometry(0, 0, 1000, 800);
|
|
|
|
|
2023-06-20 11:57:39 -04:00
|
|
|
influx = nullptr;
|
2024-08-26 13:59:04 -04:00
|
|
|
dataBaseIP = "";
|
2023-06-20 11:57:39 -04:00
|
|
|
dataBaseName = "";
|
2024-08-26 13:59:04 -04:00
|
|
|
dataBaseToken = "";
|
2023-06-20 11:57:39 -04:00
|
|
|
|
2023-10-05 13:13:15 -04:00
|
|
|
dataList = new Data*[nDigi];
|
|
|
|
typeList.clear();
|
|
|
|
snList.clear();
|
|
|
|
|
|
|
|
for( unsigned int k = 0; k < nDigi; k ++) {
|
|
|
|
dataList[k] = digi[k]->GetData();
|
|
|
|
typeList.push_back(digi[k]->GetDPPType());
|
|
|
|
snList.push_back(digi[k]->GetSerialNumber());
|
|
|
|
}
|
|
|
|
|
2024-03-13 17:07:34 -04:00
|
|
|
isBuildBackward = false;
|
2023-10-05 13:13:15 -04:00
|
|
|
mb = new MultiBuilder(dataList, typeList, snList);
|
2023-05-26 18:06:37 -04:00
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
// buildTimerThread = new TimingThread(this);
|
|
|
|
// buildTimerThread->SetWaitTimeinSec(1.0); //^Set event build interval
|
|
|
|
// connect( buildTimerThread, &TimingThread::timeUp, this, &Analyzer::UpdateHistograms);
|
2023-05-26 18:06:37 -04:00
|
|
|
|
|
|
|
QWidget * layoutWidget = new QWidget(this);
|
|
|
|
setCentralWidget(layoutWidget);
|
|
|
|
layout = new QGridLayout(layoutWidget);
|
|
|
|
layoutWidget->setLayout(layout);
|
|
|
|
|
|
|
|
// QPushButton * bnSetting = new QPushButton("Settings", this);
|
|
|
|
// layout->addWidget(bnSetting);
|
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
anaThread = new QThread(this);
|
|
|
|
anaWorker = new AnalyzerWorker(this);
|
|
|
|
anaTimer = new QTimer();
|
|
|
|
isWorking = false;
|
|
|
|
|
|
|
|
anaWorker->moveToThread(anaThread);
|
|
|
|
|
|
|
|
connect(anaTimer, &QTimer::timeout, anaWorker, [=](){
|
|
|
|
if( isWorking ) return;
|
2024-08-29 15:47:07 -04:00
|
|
|
isWorking = true;
|
2024-08-29 14:45:11 -04:00
|
|
|
anaWorker->UpdateHistograms();
|
2024-08-29 15:47:07 -04:00
|
|
|
isWorking = false;
|
2024-08-29 14:45:11 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// connect(anaWorker, &AnalyzerWorker::workDone, this, [=](){
|
|
|
|
// printf(" --------- work Done\n");
|
|
|
|
// });
|
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
connect( anaWorker, &AnalyzerWorker::workDone, this, &Analyzer::ReplotHistograms);
|
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
anaThread->start();
|
|
|
|
|
2023-05-26 18:06:37 -04:00
|
|
|
}
|
|
|
|
|
2023-06-02 15:41:26 -04:00
|
|
|
Analyzer::~Analyzer(){
|
2024-08-21 14:50:55 -04:00
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
printf("Analyzer::%s\n", __func__);
|
|
|
|
anaTimer->stop();
|
|
|
|
|
|
|
|
printf(" is anaThread is running %d \n", anaThread->isRunning());
|
|
|
|
if( anaThread->isRunning() ){
|
|
|
|
anaThread->quit();
|
|
|
|
anaThread->wait();
|
2024-08-21 14:50:55 -04:00
|
|
|
}
|
|
|
|
|
2024-08-29 14:45:11 -04:00
|
|
|
printf("------ end of anaThread \n");
|
|
|
|
|
2023-06-20 11:57:39 -04:00
|
|
|
delete influx;
|
2023-06-14 15:41:26 -04:00
|
|
|
delete mb;
|
2023-10-05 13:13:15 -04:00
|
|
|
delete [] dataList;
|
2023-05-26 18:06:37 -04:00
|
|
|
}
|
|
|
|
|
2024-03-13 17:07:34 -04:00
|
|
|
double Analyzer::RandomGauss(double mean, double sigma){
|
|
|
|
|
|
|
|
// Box-Muller transform to generate normally distributed random numbers
|
|
|
|
double u1 = QRandomGenerator::global()->generateDouble();
|
|
|
|
double u2 = QRandomGenerator::global()->generateDouble();
|
|
|
|
double z0 = sqrt(-2.0 * log(u1)) * cos(2 * M_PI * u2);
|
|
|
|
|
|
|
|
// Apply mean and standard deviation
|
|
|
|
return mean + z0 * sigma;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-26 13:59:04 -04:00
|
|
|
void Analyzer::SetDatabase(QString IP, QString Name, QString Token){
|
|
|
|
dataBaseIP = IP;
|
|
|
|
dataBaseName = Name;
|
|
|
|
dataBaseToken = Token;
|
|
|
|
|
|
|
|
if( influx ) {
|
|
|
|
delete influx;
|
|
|
|
influx = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
influx = new InfluxDB(dataBaseIP.toStdString());
|
|
|
|
|
|
|
|
if( influx->TestingConnection() ){
|
|
|
|
printf("InfluxDB URL (%s) is Valid. Version : %s\n", dataBaseIP.toStdString().c_str(), influx->GetVersionString().c_str());
|
|
|
|
|
|
|
|
if( influx->GetVersionNo() > 1 && dataBaseToken.isEmpty() ) {
|
|
|
|
printf("A Token is required for accessing the database.\n");
|
|
|
|
delete influx;
|
|
|
|
influx = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
influx->SetToken(dataBaseToken.toStdString());
|
|
|
|
|
|
|
|
//==== chck database exist
|
|
|
|
influx->CheckDatabases();
|
|
|
|
std::vector<std::string> databaseList = influx->GetDatabaseList();
|
|
|
|
bool foundDatabase = false;
|
|
|
|
for( int i = 0; i < (int) databaseList.size(); i++){
|
|
|
|
if( databaseList[i] == dataBaseName.toStdString() ) foundDatabase = true;
|
2024-08-26 15:25:19 -04:00
|
|
|
// printf("%d | %s\n", i, databaseList[i].c_str());
|
2024-08-26 13:59:04 -04:00
|
|
|
}
|
|
|
|
if( foundDatabase ){
|
|
|
|
influx->AddDataPoint("test value=1");
|
|
|
|
influx->WriteData(dataBaseName.toStdString());
|
|
|
|
influx->ClearDataPointsBuffer();
|
|
|
|
if( influx->IsWriteOK() ){
|
|
|
|
printf("test write database OK.\n");
|
|
|
|
}else{
|
|
|
|
printf("################# test write database FAIL.\n");
|
|
|
|
delete influx;
|
|
|
|
influx = nullptr;
|
|
|
|
}
|
|
|
|
}else{
|
2024-09-30 18:38:48 -04:00
|
|
|
printf(RED "Database name : %s NOT found.\n" RESET, dataBaseName.toStdString().c_str());
|
2024-08-26 13:59:04 -04:00
|
|
|
delete influx;
|
|
|
|
influx = nullptr;
|
|
|
|
}
|
|
|
|
}else{
|
2024-09-30 18:38:48 -04:00
|
|
|
printf(RED "InfluxDB URL (%s) is NOT Valid. \n" RESET, dataBaseIP.toStdString().c_str());
|
2024-08-26 13:59:04 -04:00
|
|
|
delete influx;
|
|
|
|
influx = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-15 11:48:18 -04:00
|
|
|
void Analyzer::RedefineEventBuilder(std::vector<int> idList){
|
|
|
|
delete mb;
|
2023-10-05 13:13:15 -04:00
|
|
|
delete [] dataList;
|
|
|
|
typeList.clear();
|
|
|
|
snList.clear();
|
|
|
|
dataList = new Data*[idList.size()];
|
|
|
|
|
|
|
|
for( size_t k = 0; k < idList.size(); k ++) {
|
|
|
|
dataList[k] = digi[idList[k]]->GetData();
|
|
|
|
typeList.push_back(digi[idList[k]]->GetDPPType());
|
|
|
|
snList.push_back(digi[idList[k]]->GetSerialNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
mb = new MultiBuilder(dataList, typeList, snList);
|
2023-06-15 11:48:18 -04:00
|
|
|
}
|
|
|
|
|
2023-06-20 11:57:39 -04:00
|
|
|
void Analyzer::BuildEvents(bool verbose){
|
2024-10-22 13:56:26 -04:00
|
|
|
// qDebug() << __func__ << "| thread:" << QThread::currentThreadId();
|
2024-08-29 14:45:11 -04:00
|
|
|
// unsigned int nData = mb->GetNumOfDigitizer();
|
|
|
|
// std::vector<int> idList = mb->GetDigiIDList();
|
2024-08-19 12:27:51 -04:00
|
|
|
// for( unsigned int i = 0; i < nData; i++ ) digiMTX[idList[i]].lock();
|
2023-06-15 11:48:18 -04:00
|
|
|
if( isBuildBackward ){
|
2023-06-20 11:57:39 -04:00
|
|
|
mb->BuildEventsBackWard(maxNumEventBuilt, verbose);
|
2023-06-15 11:48:18 -04:00
|
|
|
}else{
|
2024-08-01 17:03:50 -04:00
|
|
|
mb->BuildEvents(0, true, verbose);
|
2023-06-15 11:48:18 -04:00
|
|
|
}
|
2024-08-29 14:45:11 -04:00
|
|
|
// mb->PrintStat();
|
2024-08-19 12:27:51 -04:00
|
|
|
// for( unsigned int i = 0; i < nData; i++ ) digiMTX[idList[i]].unlock();
|
2023-05-26 18:06:37 -04:00
|
|
|
|
2023-06-12 16:32:01 -04:00
|
|
|
}
|
2023-06-02 18:13:47 -04:00
|
|
|
|
2024-08-26 13:59:04 -04:00
|
|
|
void Analyzer::SetDatabaseButton(){
|
|
|
|
|
|
|
|
QDialog dialog;
|
|
|
|
dialog.setWindowTitle("Influx Database");
|
|
|
|
|
|
|
|
QGridLayout layout(&dialog);
|
|
|
|
|
|
|
|
//------------------------------
|
|
|
|
QLabel ipLabel("Database IP : ");
|
|
|
|
layout.addWidget(&ipLabel, 0, 0);
|
|
|
|
|
|
|
|
QLineEdit ipLineEdit;
|
|
|
|
ipLineEdit.setFixedSize(1000, 20);
|
|
|
|
ipLineEdit.setText(dataBaseIP);
|
|
|
|
layout.addWidget(&ipLineEdit, 0, 1);
|
|
|
|
|
|
|
|
//------------------------------
|
|
|
|
QLabel nameLabel("Database Name : ");
|
|
|
|
layout.addWidget(&nameLabel, 1, 0);
|
|
|
|
|
|
|
|
QLineEdit nameLineEdit;
|
|
|
|
nameLineEdit.setFixedSize(1000, 20);
|
|
|
|
nameLineEdit.setText(dataBaseName);
|
|
|
|
layout.addWidget(&nameLineEdit, 1, 1);
|
|
|
|
|
|
|
|
//------------------------------
|
|
|
|
QLabel tokenLabel("Database Token : ");
|
|
|
|
layout.addWidget(&tokenLabel, 2, 0);
|
|
|
|
|
|
|
|
QLineEdit tokenLineEdit;
|
|
|
|
tokenLineEdit.setFixedSize(1000, 20);
|
|
|
|
tokenLineEdit.setText(dataBaseToken);
|
|
|
|
layout.addWidget(&tokenLineEdit, 2, 1);
|
|
|
|
|
|
|
|
layout.addWidget(new QLabel("Only for version 2+, version 1+ can be skipped."), 3, 0, 1, 2);
|
|
|
|
|
|
|
|
// Buttons for OK and Cancel
|
|
|
|
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
|
|
|
layout.addWidget(&buttonBox);
|
|
|
|
|
|
|
|
QObject::connect(&buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
|
|
|
|
QObject::connect(&buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
|
|
|
|
|
|
|
|
dialog.resize(400, dialog.sizeHint().height()); // Set the width to 400 pixels
|
|
|
|
|
|
|
|
// Show the dialog and get the result
|
|
|
|
if (dialog.exec() == QDialog::Accepted) {
|
2024-08-26 15:25:19 -04:00
|
|
|
SetDatabase(ipLineEdit.text().trimmed(), nameLineEdit.text().trimmed(),tokenLineEdit.text().trimmed());
|
2024-08-26 13:59:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-12 16:32:01 -04:00
|
|
|
//^####################################### below are open to customization
|
2023-05-26 18:06:37 -04:00
|
|
|
|
2023-06-12 16:32:01 -04:00
|
|
|
void Analyzer::SetUpCanvas(){
|
2023-05-30 10:08:39 -04:00
|
|
|
|
2023-05-26 18:06:37 -04:00
|
|
|
}
|
|
|
|
|
2023-06-02 15:41:26 -04:00
|
|
|
void Analyzer::UpdateHistograms(){
|
2023-05-26 18:06:37 -04:00
|
|
|
|
2023-05-31 17:30:46 -04:00
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Analyzer::ReplotHistograms(){
|
|
|
|
|
|
|
|
|
2023-05-26 18:06:37 -04:00
|
|
|
}
|