FSUDAQ/DAQ/programSetting.cpp

334 lines
13 KiB
C++

#include <TApplication.h>
#include <TGClient.h>
#include <TSystem.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom.h>
#include <TRootEmbeddedCanvas.h>
#include <TGTableContainer.h>
#include <TGFileDialog.h>
#include <fstream>
#include <iostream>
#include <sys/stat.h>
#include <experimental/filesystem>
#include "programSetting.h"
namespace fs = std::experimental::filesystem;
std::string ProgramSetting::databaseIP = "https://fsunuc.physics.fsu.edu/influx/";
std::string ProgramSetting::databaseName = "testing";
std::string ProgramSetting::DataSavingPath = "/home/catrina/FSUDAQ/expDir";
std::string ProgramSetting::ExpName = "ZZZZZ";
std::string ProgramSetting::ElogIP = "elog.physics.fsu.edu";
bool ProgramSetting::EnableElog = true;
bool ProgramSetting::EnableDatabase = true;
unsigned short ProgramSetting::maxPort = 4;
unsigned short ProgramSetting::maxBoardPrePort = 3;
/// fixed program things
const std::string ProgramSetting::settingFileName = "FSUDAQ.sh";
const std::string ProgramSetting::programPath = "~/FSUDAQ/";
///this is declared at FSUDAQ.cpp
extern unsigned short lastRunID;
extern unsigned short nDigi;
extern Digitizer ** digi;
ProgramSetting::ProgramSetting(const TGWindow *p){
fMain = new TGMainFrame(p, 600, 400);
fMain->SetWindowName("Program Setting");
fMain->Connect("CloseWindow()", "ProgramSetting", this, "CloseWindow()");
TGLayoutHints * haha = new TGLayoutHints(kLHintsRight | kLHintsCenterY, 5,5,5,2);
TGLayoutHints * kaka = new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 5,5,0,0);
TGVerticalFrame * vframe = new TGVerticalFrame(fMain); fMain->AddFrame(vframe);
{///============== Data Saving Path
TGGroupFrame * gfData = new TGGroupFrame(vframe, "Data Storage", kVerticalFrame); vframe->AddFrame(gfData, new TGLayoutHints(kLHintsNormal, 5, 5, 5, 5));
TGHorizontalFrame * hfData = new TGHorizontalFrame(gfData); gfData->AddFrame(hfData, new TGLayoutHints(kLHintsNormal, 0, 0, 5, 0));
TGVerticalFrame * vfLabel = new TGVerticalFrame(hfData, 200); hfData->AddFrame(vfLabel );
TGVerticalFrame * vfTxt = new TGVerticalFrame(hfData); hfData->AddFrame(vfTxt);
TGVerticalFrame * vfAux = new TGVerticalFrame(hfData); hfData->AddFrame(vfAux);
TGLabel * lbExpName = new TGLabel(vfLabel, "ExpName :"); vfLabel->AddFrame(lbExpName, haha);
TGLabel * lbDataPath = new TGLabel(vfLabel, "Data Absolute Path :"); vfLabel->AddFrame(lbDataPath, haha);
txtExpName = new TGTextEntry(vfTxt, ExpName.c_str()); vfTxt->AddFrame(txtExpName, kaka);
txtExpName->Connect("ReturnPressed()", "ProgramSetting", this, "SetSetting()");
txtExpName->Resize(300, 20);
txtDataPath = new TGTextEntry(vfTxt, DataSavingPath.c_str()); vfTxt->AddFrame(txtDataPath, kaka);
txtDataPath->Connect("ReturnPressed()", "ProgramSetting", this, "SetSetting()");
txtDataPath->Resize(300, 20);
TGLabel * lbdummy0 = new TGLabel(vfAux, ""); vfAux->AddFrame(lbdummy0, haha);
TGTextButton * bSetFolder = new TGTextButton(vfAux, "Set Folder"); vfAux->AddFrame(bSetFolder, kaka);
bSetFolder->Connect("Clicked()", "ProgramSetting", this, "SetFolder()");
}
{///============== Database & Elog
TGGroupFrame * gfDatabase = new TGGroupFrame(vframe, "Database & Elog Setting", kVerticalFrame); vframe->AddFrame(gfDatabase, new TGLayoutHints(kLHintsExpandX, 5, 5, 5, 5));
TGHorizontalFrame * hfDB1 = new TGHorizontalFrame(gfDatabase); gfDatabase->AddFrame(hfDB1, new TGLayoutHints(kLHintsNormal, 0, 0, 5, 0));
TGVerticalFrame * vfLabel = new TGVerticalFrame(hfDB1, 200); hfDB1->AddFrame(vfLabel);
TGVerticalFrame * vfTxt = new TGVerticalFrame(hfDB1); hfDB1->AddFrame(vfTxt);
TGVerticalFrame * vfOnOff = new TGVerticalFrame(hfDB1); hfDB1->AddFrame(vfOnOff);
TGLabel * lbIP = new TGLabel(vfLabel, "DB IP :"); vfLabel->AddFrame(lbIP, haha);
TGLabel * lbDBName = new TGLabel(vfLabel, "DB Name :"); vfLabel->AddFrame(lbDBName, haha);
TGLabel * lbElogIP = new TGLabel(vfLabel, "Elog IP :"); vfLabel->AddFrame(lbElogIP, haha);
txtIP = new TGTextEntry(vfTxt, databaseIP.c_str()); vfTxt->AddFrame(txtIP, kaka);
txtIP->Resize(300, 20);
txtIP->Connect("ReturnPressed()", "ProgramSetting", this, "SetSetting()");
txtDBName = new TGTextEntry(vfTxt, databaseName.c_str()); vfTxt->AddFrame(txtDBName, kaka);
txtDBName->Resize(300, 20);
txtDBName->Connect("ReturnPressed()", "ProgramSetting", this, "SetSetting()");
txtElogIP = new TGTextEntry(vfTxt, ElogIP.c_str()); vfTxt->AddFrame(txtElogIP, kaka);
txtElogIP->Resize(300, 20);
txtElogIP->Connect("ReturnPressed()", "ProgramSetting", this, "SetSetting()");
TGLabel * lbdummy0 = new TGLabel(vfOnOff, ""); vfOnOff->AddFrame(lbdummy0, haha);
cbDatabase = new TGCheckButton(vfOnOff, "Enable Database", 1); vfOnOff->AddFrame(cbDatabase, kaka);
if( ProgramSetting::EnableDatabase ){
cbDatabase->SetState(kButtonDown);
}else{
cbDatabase->SetState(kButtonUp);
}
cbDatabase->Connect("Clicked()", "ProgramSetting", this, "SetDataBaseElogOnOff()");
cbElog = new TGCheckButton(vfOnOff, "Enable Elog", 1); vfOnOff->AddFrame(cbElog, kaka);
if( ProgramSetting::EnableElog ){
cbElog->SetState(kButtonDown);
}else{
cbElog->SetState(kButtonUp);
}
cbElog->Connect("Clicked()", "ProgramSetting", this, "SetDataBaseElogOnOff()");
}
{///============== Other setting Read Time event building
TGGroupFrame * gfOther = new TGGroupFrame(vframe, "Other Settings", kVerticalFrame); vframe->AddFrame(gfOther, new TGLayoutHints(kLHintsExpandX, 5, 5, 5, 5));
TGHorizontalFrame * hfOther1 = new TGHorizontalFrame(gfOther); gfOther->AddFrame(hfOther1, new TGLayoutHints(kLHintsNormal, 0, 0, 5, 0));
TGLabel * lbPort = new TGLabel(hfOther1, "Number of Port :"); hfOther1->AddFrame(lbPort, kaka);
numPort = new TGNumberEntry(hfOther1, ProgramSetting::maxPort, 0, 0, TGNumberFormat::kNESInteger, TGNumberFormat::kNEANonNegative); hfOther1->AddFrame(numPort, kaka);
numPort->SetWidth(50);
numPort->SetLimits(TGNumberFormat::kNELLimitMinMax, 1, MaxNPorts);
numPort->Connect("Modified()", "ProgramSetting", this, "SetSearchRange()");
TGHorizontalFrame * hfOther2 = new TGHorizontalFrame(gfOther); gfOther->AddFrame(hfOther2, new TGLayoutHints(kLHintsNormal, 0, 0, 5, 0));
TGLabel * lbBoard = new TGLabel(hfOther2, "Number of Board / Port :"); hfOther2->AddFrame(lbBoard, kaka);
numBoard = new TGNumberEntry(hfOther2, ProgramSetting::maxBoardPrePort, 0, 0, TGNumberFormat::kNESInteger, TGNumberFormat::kNEANonNegative); hfOther2->AddFrame(numBoard, kaka);
numBoard->SetWidth(50);
numBoard->SetLimits(TGNumberFormat::kNELLimitMinMax, 1, 9);
numBoard->Connect("Modified()", "ProgramSetting", this, "SetSearchRange()");
}
fMain->MapSubwindows();
fMain->Resize(fMain->GetDefaultSize());
fMain->MapWindow();
}
ProgramSetting::~ProgramSetting(){
printf("----- %s \n", __func__);
delete txtIP;
delete txtDBName;
delete txtDataPath;
delete txtExpName;
delete txtElogIP;
delete cbDatabase;
delete cbElog;
delete numPort;
delete numBoard;
fMain->Cleanup();
delete fMain;
}
void ProgramSetting::LogMsg(TString msg){
Emit("LogMsg(char*)", msg.Data());
}
void ProgramSetting::SetDataBaseElogOnOff(){
EnableDatabase = ( cbDatabase->GetState() == kButtonDown ? true : false );
EnableElog = ( cbElog->GetState() == kButtonDown ? true : false );
int status = SaveProgramSetting();
if( status == 1 ) LogMsg(Form("Saved Program settings to %s",ProgramSetting::settingFileName.c_str()));
}
void ProgramSetting::SetSearchRange(){
maxPort = numPort->GetNumber();
maxBoardPrePort = numPort->GetNumber();
int status = SaveProgramSetting();
if( status == 1 ) LogMsg(Form("Saved Program settings to %s",ProgramSetting::settingFileName.c_str()));
}
void ProgramSetting::SetSetting(){
DataSavingPath = txtDataPath->GetText();
ExpName = txtExpName->GetText();
databaseIP = txtIP->GetText();
databaseName = txtDBName->GetText();
ElogIP = txtElogIP->GetText();
PrintSettings();
int status = SaveProgramSetting();
if( status == 1 ) LogMsg(Form("Saved Program settings to %s",ProgramSetting::settingFileName.c_str()));
gSystem->mkdir(( ProgramSetting::DataSavingPath + "/data").c_str());
gSystem->mkdir(( ProgramSetting::DataSavingPath + "/settings").c_str());
///save digitizer settings to the settings folder
if( digi != NULL ){
for( int i = 0; i < nDigi; i ++){
digi[i]->SaveAllSettingsAsBin(ProgramSetting::DataSavingPath + "/settings/setting_" + std::to_string(digi[i]->GetSerialNumber()) + ".bin");
}
}
Emit("SetSetting()");
}
void ProgramSetting::SetFolder(){
const char *filetypes[] = { "All Files", "*.*", 0, 0 };
TGFileInfo fi;
fi.fFileTypes = filetypes;
fi.SetIniDir(".");
printf("fIniDir = %s\n", fi.fIniDir);
new TGFileDialog(gClient->GetRoot(), fMain, kDOpen, &fi);
printf("Open file: %s (dir: %s)\n", fi.fFilename, fi.fIniDir);
if( fi.fFilename == NULL) return;
txtDataPath->SetText(fi.fFilename, false);
SetSetting();
}
///============= Static function
void ProgramSetting::PrintSettings(){
printf(" Data Saving Path : %s\n", DataSavingPath.c_str());
printf(" ExpName : %s\n", ExpName.c_str());
printf(" Database Enable : %d\n", EnableDatabase);
printf(" Database IP : %s\n", databaseIP.c_str());
printf(" Database Name : %s\n", databaseName.c_str());
printf(" Elog Enable : %d\n", EnableElog);
printf(" Elog IP : %s\n", ElogIP.c_str());
printf(" Max Port Scan : %d\n", maxPort);
printf("Max Board / Port Scan : %d\n", maxBoardPrePort);
printf(" last run ID : %d\n", lastRunID);
}
void ProgramSetting::LoadProgramSetting(){
std::ifstream fileIn;
fileIn.open(ProgramSetting::settingFileName.c_str(), std::ios::in);
if( fileIn ){
std::string line;
int lineNum = 0;
size_t pos = 0;
while (fileIn.good() ){
std::getline(fileIn, line);
pos = line.find("=");
if( pos > 1 ){
lineNum ++;
pos += 1;
///printf("%d | %s , %d \n", lineNum, line.c_str(), pos);
switch ( lineNum ){
case 1: ProgramSetting::ExpName = line.substr(pos); break;
case 2: ProgramSetting::DataSavingPath = line.substr(pos); break;
case 3: ProgramSetting::EnableDatabase = std::atoi(line.substr(pos).c_str()); break;
case 4: ProgramSetting::databaseIP = line.substr(pos); break;
case 5: ProgramSetting::databaseName = line.substr(pos); break;
case 6: ProgramSetting::EnableElog = std::atoi(line.substr(pos).c_str()); break;
case 7: ProgramSetting::ElogIP = line.substr(pos); break;
case 8: ProgramSetting::maxPort = std::atoi(line.substr(pos).c_str()); break;
case 9: ProgramSetting::maxBoardPrePort = std::atoi(line.substr(pos).c_str()); break;
case 10: lastRunID = std::atoi(line.substr(pos).c_str()); break;
}
}
}
fileIn.close();
if( lineNum < 10 ) {
printf("Something wrong with the %s file. Creating a new one.\n", ProgramSetting::settingFileName.c_str());
SaveProgramSetting();
}
}else{
printf("%s | Cannot open file : %s.\n", __func__, ProgramSetting::settingFileName.c_str());
printf("Creating One with default setting");
SaveProgramSetting();
}
PrintSettings();
}
int ProgramSetting::SaveProgramSetting(){
printf("+++++++ %s \n", __func__);
std::string filePath = ProgramSetting::settingFileName;
FILE * fileOut = fopen(filePath.c_str(), "w");
if( fileOut != NULL ){
fputs( ("ExpName=" + ProgramSetting::ExpName + "\n").c_str(), fileOut);
fputs( ("DataPath=" + ProgramSetting::DataSavingPath + "\n").c_str(), fileOut);
fputs( ("EnableDatabase=" + std::to_string(ProgramSetting::EnableDatabase) + "\n").c_str(), fileOut);
fputs( ("DatabaseIP=" + ProgramSetting::databaseIP + "\n").c_str(), fileOut);
fputs( ("DatabaseName=" + ProgramSetting::databaseName + "\n").c_str(), fileOut);
fputs( ("EnableElog=" + std::to_string(ProgramSetting::EnableElog) + "\n").c_str(), fileOut);
fputs( ("ElogIP=" + ProgramSetting::ElogIP + "\n").c_str(), fileOut);
fputs( ("MaxPort=" + std::to_string(ProgramSetting::maxPort) + "\n").c_str(), fileOut);
fputs( ("MaxBoardPrePort=" + std::to_string(ProgramSetting::maxBoardPrePort) + "\n").c_str(), fileOut);
fputs( ("lastRunID=" + std::to_string(lastRunID) + "\n").c_str(), fileOut);
fclose(fileOut);
gSystem->CopyFile(ProgramSetting::settingFileName.c_str(), (ProgramSetting::DataSavingPath + "/FSUDAQ.sh").c_str(), true); /// overwrite
return 1;
}else{
printf("%s | Cannot open file : %s.\n", __func__, filePath.c_str());
return 0;
}
return 0;
}