added machanism to prevent multiple instant of the application

This commit is contained in:
Ryan Tang 2023-03-22 12:36:24 -04:00
parent a65d7ffa94
commit 7a8065f285
5 changed files with 153 additions and 29 deletions

View File

@ -4,6 +4,7 @@
#include <QLabel> #include <QLabel>
#include <QSet> #include <QSet>
#include <QList> #include <QList>
#include <QFrame>
#define NCOL 10 // number of column #define NCOL 10 // number of column
@ -47,10 +48,6 @@ SOLARISpanel::SOLARISpanel(Digitizer2Gen **digi, unsigned short nDigi,
QList<int> haha ; QList<int> haha ;
haha << mapping[i][j] << ((i << 8 ) + j); haha << mapping[i][j] << ((i << 8 ) + j);
if( mapping[i][j] >= 0 ) detIDListTemp << haha; if( mapping[i][j] >= 0 ) detIDListTemp << haha;
for( int k = 0 ; k < nDetType; k++){
int lowID = (k == 0 ? 0 : detMaxID[k-1]);
if( lowID <= mapping[i][j] && mapping[i][j] < detMaxID[k] ) nDet[k] ++ ;
}
if( j % 16 == 15 ) printf("\n"); if( j % 16 == 15 ) printf("\n");
} }
printf("------------------ \n"); printf("------------------ \n");
@ -58,12 +55,18 @@ SOLARISpanel::SOLARISpanel(Digitizer2Gen **digi, unsigned short nDigi,
//----- consolidate detIDList; //----- consolidate detIDList;
detIDList.clear(); detIDList.clear();
QList<int> haha;
haha << detIDListTemp[0][0];
detIDList << haha;
bool repeated = false; bool repeated = false;
for( int i = 0; i < detIDListTemp.size(); i++ ){ for( int i = 0; i < detIDListTemp.size(); i++ ){
repeated = false; repeated = false;
if( detIDList.size() == 0 ){
detIDList << detIDListTemp[i];
for( int k = 0 ; k < nDetType; k++){
int lowID = (k == 0 ? 0 : detMaxID[k-1]);
if( lowID <= detIDListTemp[i][0] && detIDListTemp[i][0] < detMaxID[k] ) nDet[k] ++ ;
}
continue;
}
for( int j = 0; j < detIDList.size() ; j++){ for( int j = 0; j < detIDList.size() ; j++){
if( detIDList[j][0] == detIDListTemp[i][0] ) { if( detIDList[j][0] == detIDListTemp[i][0] ) {
repeated = true; repeated = true;
@ -71,7 +74,13 @@ SOLARISpanel::SOLARISpanel(Digitizer2Gen **digi, unsigned short nDigi,
break; break;
} }
} }
if( !repeated ) detIDList << detIDListTemp[i]; if( !repeated ) {
detIDList << detIDListTemp[i];
for( int k = 0 ; k < nDetType; k++){
int lowID = (k == 0 ? 0 : detMaxID[k-1]);
if( lowID <= detIDListTemp[i][0] && detIDListTemp[i][0] < detMaxID[k] ) nDet[k] ++ ;
}
}
} }
//---- sort detIDList; //---- sort detIDList;
@ -79,7 +88,14 @@ SOLARISpanel::SOLARISpanel(Digitizer2Gen **digi, unsigned short nDigi,
return a.at(0) < b.at(0); return a.at(0) < b.at(0);
}); });
//------------- display detector summary
//qDebug() << detIDList; //qDebug() << detIDList;
printf("---------- num. of det. Type : %d\n", nDetType);
for( int i =0; i < nDetType; i ++ ) {
printf(" Type-%d (%6s) : %3d det. (%3d - %3d)\n", i, detType[i].toStdString().c_str(), nDet[i], (i==0 ? 0 : detMaxID[i-1]), detMaxID[i]-1);
}
//------------ create Widgets //------------ create Widgets
leDisplay = new QLineEdit ***[(int) SettingItems.size()]; leDisplay = new QLineEdit ***[(int) SettingItems.size()];
@ -93,11 +109,21 @@ SOLARISpanel::SOLARISpanel(Digitizer2Gen **digi, unsigned short nDigi,
leDisplay[k][i] = new QLineEdit *[(int) mapping[i].size()]; leDisplay[k][i] = new QLineEdit *[(int) mapping[i].size()];
sbSetting[k][i] = new RSpinBox *[(int) mapping[i].size()]; sbSetting[k][i] = new RSpinBox *[(int) mapping[i].size()];
chkOnOff[k][i] = new QCheckBox *[(int) mapping[i].size()]; chkOnOff[k][i] = new QCheckBox *[(int) mapping[i].size()];
for( int j = 0 ; j < (int) mapping[i].size() ; j++){
leDisplay[k][i][j] = nullptr;
sbSetting[k][i][j] = nullptr;
chkOnOff[k][i][j] = nullptr;
}
} }
} }
cbTrigger = new RComboBox *[nDet[0]]; /// nDet[0] store the number of det. for the Array cbTrigger = new RComboBox ** [nDetType];
for(int i = 0; i < nDetType; i++){
cbTrigger[i] = new RComboBox *[nDet[i]]; /// nDet[0] store the number of det. for the Array
for( int j = 0; j < nDet[i]; j ++){
cbTrigger[i][j] = nullptr;
}
}
//---------- Set Panel //---------- Set Panel
QVBoxLayout * mainLayout = new QVBoxLayout(this); this->setLayout(mainLayout); QVBoxLayout * mainLayout = new QVBoxLayout(this); this->setLayout(mainLayout);
QTabWidget * tabWidget = new QTabWidget(this); mainLayout->addWidget(tabWidget); QTabWidget * tabWidget = new QTabWidget(this); mainLayout->addWidget(tabWidget);
@ -137,12 +163,19 @@ SOLARISpanel::SOLARISpanel(Digitizer2Gen **digi, unsigned short nDigi,
} }
QFrame *line = new QFrame(tab);
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
line->setFixedHeight(10);
layout->addWidget(line, 1, 0, 1, NCOL);
//range of detID //range of detID
int lowID = (detTypeID == 0 ? 0 : detMaxID[detTypeID-1]); int lowID = (detTypeID == 0 ? 0 : detMaxID[detTypeID-1]);
for(int i = 0; i < detIDList.size(); i++){ for(int i = 0; i < detIDList.size(); i++){
if( detIDList[i][0] >= detMaxID[detTypeID] || lowID > detIDList[i][0] ) continue; if( detIDList[i][0] >= detMaxID[detTypeID] || lowID > detIDList[i][0] ) continue;
CreateDetGroup(SettingID, detIDList[i], layout, i/NCOL + 1, i%NCOL); CreateDetGroup(SettingID, detIDList[i], layout, i/NCOL + 2, i%NCOL);
} }
} }
@ -156,6 +189,30 @@ SOLARISpanel::SOLARISpanel(Digitizer2Gen **digi, unsigned short nDigi,
SOLARISpanel::~SOLARISpanel(){ SOLARISpanel::~SOLARISpanel(){
printf("%s\n", __func__);
for( int k = 0; k < (int) SettingItems.size() ; k ++){
for( int i = 0; i < nDigiMapping; i++){
delete [] leDisplay[k][i] ;
delete [] sbSetting[k][i] ;
delete [] chkOnOff[k][i] ;
}
delete [] leDisplay[k];
delete [] sbSetting[k];
delete [] chkOnOff[k] ;
}
delete [] leDisplay;
delete [] sbSetting;
delete [] chkOnOff;
for( int k = 0; k < (int) detType.size() ; k++){
delete [] cbTrigger[k];
}
delete [] cbTrigger;
printf("end of %s\n", __func__);
} }
void SOLARISpanel::CreateDetGroup(int SettingID, QList<int> detID, QGridLayout * &layout, int row, int col){ void SOLARISpanel::CreateDetGroup(int SettingID, QList<int> detID, QGridLayout * &layout, int row, int col){
@ -249,20 +306,28 @@ void SOLARISpanel::CreateDetGroup(int SettingID, QList<int> detID, QGridLayout *
} }
//TODO ==== detMaxID[0] should belong to Array int detTypeID = 0;
if( 0 <= detID[0] && detID[0] < detMaxID[0] && SettingItems[SettingID].GetPara() == PHA::CH::TriggerThreshold.GetPara()){ for( int i = 0; i < (int) detType.size(); i++){
cbTrigger[detID[0]] = new RComboBox(this); int lowID = (i == 0) ? 0 : detMaxID[i-1];
cbTrigger[detID[0]]->addItem("Non-Trigger", 0x0); if( lowID <= detID[0] && detID[0] < detMaxID[i]) {
cbTrigger[detID[0]]->addItem("Self Trigger", -1); detTypeID = i;
cbTrigger[detID[0]]->addItem("Trigger all", 0x7); break;
cbTrigger[detID[0]]->addItem("Trigger (e)", 0x1); }
cbTrigger[detID[0]]->addItem("Trigger (xf)", 0x2); }
cbTrigger[detID[0]]->addItem("Trigger (xn)", 0x4);
cbTrigger[detID[0]]->addItem("Trigger 011", 0x3); if( SettingItems[SettingID].GetPara() == PHA::CH::TriggerThreshold.GetPara()){
cbTrigger[detID[0]]->addItem("Trigger 110", 0x6); cbTrigger[detTypeID][detID[0]] = new RComboBox(this);
cbTrigger[detID[0]]->addItem("Trigger 101", 0x5); cbTrigger[detTypeID][detID[0]]->addItem("Non-Trigger", 0x0);
cbTrigger[detID[0]]->addItem("Oops....", -999); cbTrigger[detTypeID][detID[0]]->addItem("Self Trigger", -1);
layout0->addWidget(cbTrigger[detID[0]], 8, 0, 1, 3); cbTrigger[detTypeID][detID[0]]->addItem("Trigger all", 0x7);
cbTrigger[detTypeID][detID[0]]->addItem("Trigger (e)", 0x1);
cbTrigger[detTypeID][detID[0]]->addItem("Trigger (xf)", 0x2);
cbTrigger[detTypeID][detID[0]]->addItem("Trigger (xn)", 0x4);
cbTrigger[detTypeID][detID[0]]->addItem("Trigger 011", 0x3);
cbTrigger[detTypeID][detID[0]]->addItem("Trigger 110", 0x6);
cbTrigger[detTypeID][detID[0]]->addItem("Trigger 101", 0x5);
cbTrigger[detTypeID][detID[0]]->addItem("Oops....", -999);
layout0->addWidget(cbTrigger[detTypeID][detID[0]], 8, 0, 1, 3);
} }
layout->addWidget(groupbox, row, col); layout->addWidget(groupbox, row, col);

View File

@ -52,9 +52,9 @@ private:
unsigned short nDigi; unsigned short nDigi;
std::vector<std::vector<int>> mapping; std::vector<std::vector<int>> mapping;
QStringList detType; QStringList detType;
std::vector<int> nDet; // number of disting detector
std::vector<int> detMaxID; std::vector<int> detMaxID;
QList<QList<int>> detIDList; // 1-D array of { detID, (Digi << 8 ) + ch} QList<QList<int>> detIDList; // 1-D array of { detID, (Digi << 8 ) + ch}
std::vector<int> nDet;
int nDigiMapping; /// mapping.size() int nDigiMapping; /// mapping.size()
std::vector<int> nChMapping; /// mapping[i].size(), NON -1 std::vector<int> nChMapping; /// mapping[i].size(), NON -1
@ -68,7 +68,7 @@ private:
RSpinBox **** sbSetting; RSpinBox **** sbSetting;
QCheckBox **** chkOnOff; QCheckBox **** chkOnOff;
RComboBox ** cbTrigger; //[detID] for array only RComboBox *** cbTrigger; //[detTypeID][detID] for array only
bool enableSignalSlot; bool enableSignalSlot;

View File

@ -2,5 +2,7 @@
#define MACRO_H #define MACRO_H
#define MaxNumberOfDigitizer 20 #define MaxNumberOfDigitizer 20
#define DAQLockFile "DAQLock.dat"
#define PIDFile "pid.dat"
#endif #endif

View File

@ -1,11 +1,67 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication> #include <QApplication>
#include <QMessageBox>
#include <QProcess>
#include <QPushButton>
#include <QFile>
int main(int argc, char *argv[]){
int main(int argc, char *argv[])
{
QApplication a(argc, argv); QApplication a(argc, argv);
bool isLock = false;
int pid = 0;
QFile lockFile(DAQLockFile);
if( lockFile.open(QIODevice::Text | QIODevice::ReadOnly) ){
QTextStream in(&lockFile);
QString line = in.readLine();
isLock = line.toInt();
lockFile.close();
}
QFile pidFile(PIDFile);
if( pidFile.open(QIODevice::Text | QIODevice::ReadOnly)){
QTextStream in(&pidFile);
QString line = in.readLine();
pid = line.toInt();
pidFile.close();
}
if( isLock ) {
qDebug() << "The DAQ program is already opened. PID is " + QString::number(pid) + ", and delete the " + DAQLockFile ;
QMessageBox msgBox;
msgBox.setWindowTitle("Oopss....");
msgBox.setText("The DAQ program is already opened. PID is " + QString::number(pid) + "\n You can kill the procee by \"kill -9 <pid>\" and delete the " + DAQLockFile + "\n or click the \"Kill\" button");
msgBox.setIcon(QMessageBox::Information);
QPushButton * kill = msgBox.addButton("Kill", QMessageBox::AcceptRole);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
if(msgBox.clickedButton() == kill){
remove(DAQLockFile);
QProcess::execute("kill", QStringList() << "-9" << QString::number(pid));
}else{
return 0;
}
}
lockFile.open(QIODevice::Text | QIODevice::WriteOnly);
lockFile.write( "1" );
lockFile.close();
pidFile.open(QIODevice::Text | QIODevice::WriteOnly);
pidFile.write( QString::number(QCoreApplication::applicationPid() ).toStdString().c_str() );
pidFile.close();
MainWindow w; MainWindow w;
w.show(); w.show();
return a.exec(); return a.exec();
} }

View File

@ -295,6 +295,7 @@ MainWindow::~MainWindow(){
delete influx; delete influx;
} }
remove(DAQLockFile);
} }