FSUDAQ/DAQ/programSetting.cpp
2022-10-31 16:39:19 -04:00

264 lines
9.2 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 = "http://fsunuc.physics.fsu.edu/influx/";
std::string ProgramSetting::databaseName = "testing";
std::string ProgramSetting::DataSavingPath = "/home/catrina/FSUDAQ";
std::string ProgramSetting::ExpName = "Test";
std::string ProgramSetting::ElogIP = "128.186.111.127";
bool ProgramSetting::EnableElog = false;
bool ProgramSetting::EnableDatabase = false;
const std::string ProgramSetting::settingFileName = "FSUDAQ.sh";
///this is declared at FSUDAQ.cpp
extern unsigned short lastRunID;
extern unsigned short nDigi;
extern Digitizer ** digi;
ProgramSetting::ProgramSetting(const TGWindow *p){
printf("----- %s \n", __func__);
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", kHorizontalFrame); 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", kHorizontalFrame); 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);
cbDatabase->SetState(kButtonUp);
cbDatabase->Connect("Clicked()", "ProgramSetting", this, "SetDataBaseElogOnOff()");
cbElog = new TGCheckButton(vfOnOff, "Enable Elog", 1); vfOnOff->AddFrame(cbElog, kaka);
cbElog->SetState(kButtonUp);
cbElog->Connect("Clicked()", "ProgramSetting", this, "SetDataBaseElogOnOff()");
}
{///============== Read Time event building
}
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;
fMain->Cleanup();
delete fMain;
}
void ProgramSetting::SetSetting(){
DataSavingPath = txtDataPath->GetText();
ExpName = txtExpName->GetText();
databaseIP = txtIP->GetText();
databaseName = txtDBName->GetText();
ElogIP = txtElogIP->GetText();
PrintSettings();
SaveProgramSetting();
gSystem->mkdir(( ProgramSetting::DataSavingPath + "/data").c_str());
gSystem->mkdir(( ProgramSetting::DataSavingPath + "/settings").c_str());
///save digitizer settings to the settings folder
for( int i = 0; i < nDigi; i ++){
digi[i]->OpenSettingBinary(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();
}
void ProgramSetting::PrintSettings(){
printf("Data Saving Path : %s\n", DataSavingPath.c_str());
printf(" ExpName : %s\n", ExpName.c_str());
printf(" IP : %s\n", databaseIP.c_str());
printf(" Name : %s\n", databaseName.c_str());
printf(" Elog IP : %s\n", ElogIP.c_str());
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);
lineNum ++;
pos = line.find("=");
if( pos > 1 ){
pos += 2;
///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::databaseIP = line.substr(pos); break;
case 4: ProgramSetting::databaseName = line.substr(pos); break;
case 5: ProgramSetting::ElogIP = line.substr(pos); break;
case 6: lastRunID = std::atoi(line.substr(pos).c_str()); break;
}
}
}
fileIn.close();
}else{
printf("%s | Cannot open file : %s.\n", __func__, ProgramSetting::settingFileName.c_str());
printf("Creating One with default setting");
SaveProgramSetting();
}
PrintSettings();
}
void ProgramSetting::SaveProgramSetting(){
printf("+++++++ %s \n", __func__);
FILE * fileOut = fopen(ProgramSetting::settingFileName.c_str(), "w");
if( fileOut != NULL ){
fputs( ("ExpName = " + ProgramSetting::ExpName + "\n").c_str(), fileOut);
fputs( ("DataPath = " + ProgramSetting::DataSavingPath + "\n").c_str(), fileOut);
fputs( ("DatabaseIP = " + ProgramSetting::databaseIP + "\n").c_str(), fileOut);
fputs( ("DatabaseName = " + ProgramSetting::databaseName + "\n").c_str(), fileOut);
fputs( ("ElogIP = " + ProgramSetting::ElogIP + "\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
}else{
printf("%s | Cannot open file : %s.\n", __func__, ProgramSetting::settingFileName.c_str());
}
}
void ProgramSetting::SetDataBaseElogOnOff(){
EnableDatabase = ( cbDatabase->GetState() == kButtonDown ? true : false );
EnableElog = ( cbElog->GetState() == kButtonDown ? true : false );
}