From dbefdc69a4bc0b42254405cbfbc1797b746e3717 Mon Sep 17 00:00:00 2001 From: Andreas Suter Date: Thu, 23 Apr 2020 16:35:55 +0200 Subject: [PATCH] work on the scripting part of mupp. Preparing for the step of parameter transformation. --- src/musredit_qt5/mupp/PmuppGui.cpp | 132 +++++++++++++----- src/musredit_qt5/mupp/PmuppGui.h | 7 +- src/musredit_qt5/mupp/PmuppScript.cpp | 79 ++++++++--- src/musredit_qt5/mupp/PmuppScript.h | 1 + .../mupp/examples/field-vs-energy.txt | 4 +- .../mupp/examples/field-vs-temp.txt | 19 +++ .../mupp/examples/sigma-vs-temp.txt | 18 +++ src/musredit_qt5/mupp/mupp.cpp | 29 ++-- 8 files changed, 220 insertions(+), 69 deletions(-) create mode 100644 src/musredit_qt5/mupp/examples/field-vs-temp.txt create mode 100644 src/musredit_qt5/mupp/examples/sigma-vs-temp.txt diff --git a/src/musredit_qt5/mupp/PmuppGui.cpp b/src/musredit_qt5/mupp/PmuppGui.cpp index 95f0ea9f..c4d8faca 100644 --- a/src/musredit_qt5/mupp/PmuppGui.cpp +++ b/src/musredit_qt5/mupp/PmuppGui.cpp @@ -256,7 +256,7 @@ PmuppGui::PmuppGui( QStringList fln, QWidget *parent, Qt::WindowFlags f ) // setup widgets fBoxLayout_Main = new QBoxLayout(QBoxLayout::TopToBottom); - fBoxLayout_Main->setGeometry(QRect(10, 40, 600, 400)); + fBoxLayout_Main->setGeometry(QRect(10, 40, 600, 500)); fBoxLayout_Top = new QBoxLayout(QBoxLayout::LeftToRight); fGridLayout_Left = new QGridLayout(); @@ -329,23 +329,39 @@ PmuppGui::PmuppGui( QStringList fln, QWidget *parent, Qt::WindowFlags f ) fBoxLayout_Top->addLayout(fGridLayout_Left); fBoxLayout_Top->addLayout(fGridLayout_Right); + fCmdLineHistory = new QPlainTextEdit(this); + fCmdLineHistory->setReadOnly(true); + fCmdLineHistory->setMaximumBlockCount(50); + // fill cmd history + for (int i=0; iinsertPlainText(QString("%1\n").arg(fCmdHistory[i])); fCmdLine = new QLineEdit(this); fCmdLine->installEventFilter(this); fCmdLine->setText("$ "); fCmdLine->setFocus(); // sets the keyboard focus fExitButton = new QPushButton("E&xit", this); + QVBoxLayout *cmdLayout = new QVBoxLayout; + QLabel *cmdLabel = new QLabel("History:"); + cmdLayout->addWidget(cmdLabel); + cmdLayout->addWidget(fCmdLineHistory); + cmdLayout->addWidget(fCmdLine); + fBoxLayout_Cmd = new QBoxLayout(QBoxLayout::LeftToRight); - fBoxLayout_Cmd->addWidget(fCmdLine); + fBoxLayout_Cmd->addLayout(cmdLayout); fBoxLayout_Cmd->addWidget(fExitButton); + fBoxLayout_Cmd->setAlignment(fCmdLine, Qt::AlignBottom); + fBoxLayout_Cmd->setAlignment(fExitButton, Qt::AlignBottom); - QFrame* hline = new QFrame(); - hline->setFrameShape(QFrame::HLine); - hline->setFrameShadow(QFrame::Sunken); + fCmdSplitter = new QSplitter(Qt::Vertical, this); + QWidget *topWidget = new QWidget(this); // only needed since splitter needs a QWidget + topWidget->setLayout(fBoxLayout_Top); + QWidget *cmdWidget = new QWidget(this); // only needed since splitter needs a QWidget + cmdWidget->setLayout(fBoxLayout_Cmd); + fCmdSplitter->addWidget(topWidget); + fCmdSplitter->addWidget(cmdWidget); - fBoxLayout_Main->addLayout(fBoxLayout_Top); - fBoxLayout_Main->addWidget(hline); - fBoxLayout_Main->addLayout(fBoxLayout_Cmd); + fBoxLayout_Main->addWidget(fCmdSplitter); // establish all the necessary signal/slots connect(fRefreshCollection, SIGNAL( pressed() ), this, SLOT( refresh() )); @@ -718,16 +734,19 @@ void PmuppGui::normVal() void PmuppGui::helpCmds() { QMessageBox::information(this, "cmd help", "help: this help.
"\ - "exit/quit: close mupp.
"\ - "load: calls the open file dialog.
"\ + "ditto: addX/Y for the new selection as for the previous ones.
"\ "dump collections: dumps all currently loaded collections.
"\ "dump XY: dumps X/Y tree.
"\ - "refresh: refresh the currently selected collection.
"\ - "plot: plot the X/Y data.
"\ + "exit/quit: close mupp.
"\ + "load: calls the open file dialog.
"\ + "norm <flag>: normalize plots to maximum if <flag> is true
"\ "macro <fln>: saves the X/Y data as a ROOT macro (*.C).
"\ "path <macroPath>: sets the path to the place where the macros will be saved.
"\ - "select collection <nn>: select collection in GUI.
"\ + "plot: plot the X/Y data.
"\ + "refresh: refresh the currently selected collection.
"\ + "select <nn>: select collection in GUI.
"\ "    <nn> is either the collection name or the row number.
"\ + "x <param_name> / y <param_name>: add the parameter to the select axis.
"\ "flush: flush the history buffer."); } @@ -787,6 +806,7 @@ bool PmuppGui::eventFilter(QObject *o, QEvent *e) if (fCmdHistory.size() >= 50) fCmdHistory.removeFirst(); fCmdHistory.push_back(cmd); + fCmdLineHistory->insertPlainText(QString("%1\n").arg(cmd)); } } return false; @@ -866,9 +886,6 @@ void PmuppGui::readCmdHistory() //---------------------------------------------------------------------------------------------------- void PmuppGui::writeCmdHistory() { - if (fCmdHistory.size() == 0) - return; - QProcessEnvironment procEnv = QProcessEnvironment::systemEnvironment(); QString home = procEnv.value("HOME", ""); if (home.isEmpty()) @@ -877,7 +894,7 @@ void PmuppGui::writeCmdHistory() QString pathName = home + "/.musrfit/mupp/"; QDir dir(pathName); if (!dir.exists()) { - // directory $HOME/.musrfit/musredit does not exist hence create it + // directory $HOME/.musrfit/mupp does not exist hence create it dir.mkpath(pathName); } pathName += "mupp_history.txt"; @@ -1014,13 +1031,23 @@ void PmuppGui::remove() /** * @brief PmuppGui::addX */ -void PmuppGui::addX() +void PmuppGui::addX(QString param) { - // get the parameter name from the parameter list widget - QListWidgetItem *item = fParamList->currentItem(); - if (item == 0) - return; - QString paramName = item->text(); + QString paramName(""); + if (!param.isEmpty()) { + // check that param exists + if (fParamList->findItems(param, Qt::MatchFixedString).empty()) { + QMessageBox::critical(this, "**ERROR**", QString("Parameter X '%1' not found").arg(param)); + return; + } + paramName = param; + } else { + // get the parameter name from the parameter list widget + QListWidgetItem *item = fParamList->currentItem(); + if (item == 0) + return; + paramName = item->text(); + } int idx = fColList->currentRow(); QString xLabel = QString("%1 (-%2-)").arg(paramName).arg(idx); @@ -1046,13 +1073,23 @@ void PmuppGui::addX() /** * @brief PmuppGui::addY */ -void PmuppGui::addY() -{ - // get the parameter name from the parameter list widget - QListWidgetItem *item = fParamList->currentItem(); - if (item == 0) - return; - QString paramName = item->text(); +void PmuppGui::addY(QString param) +{ + QString paramName(""); + if (!param.isEmpty()) { + // check that param exists + if (fParamList->findItems(param, Qt::MatchFixedString).empty()) { + QMessageBox::critical(this, "**ERROR**", QString("Parameter Y '%1' not found").arg(param)); + return; + } + paramName = param; + } else { + // get the parameter name from the parameter list widget + QListWidgetItem *item = fParamList->currentItem(); + if (item == 0) + return; + paramName = item->text(); + } int idx = fColList->currentRow(); QString yLabel = QString("%1 (-%2-)").arg(paramName).arg(idx); @@ -1900,13 +1937,39 @@ void PmuppGui::handleCmds() } fMacroPath.replace(envVar.prepend("$"), fullPath); } - } else if (cmd.startsWith("select collection") || cmd.startsWith("sc")) { + } else if (cmd.startsWith("select") || cmd.startsWith("sc")) { selectCollection(cmd); + } else if (cmd.startsWith("x")) { + QStringList tok = cmd.split(" ", QString::SkipEmptyParts); + addX(tok[1]); + } else if (cmd.startsWith("y")) { + QStringList tok = cmd.split(" ", QString::SkipEmptyParts); + addY(tok[1]); + } else if (cmd.startsWith("ditto")) { + addDitto(); + } else if (cmd.startsWith("norm")) { + QStringList tok = cmd.split(" ", QString::SkipEmptyParts); + if (tok.size() != 2) { + QMessageBox::critical(this, "**ERROR**", "found wrong norm cmd, will ignore it."); + return; + } + if (!tok[1].trimmed().compare("true", Qt::CaseInsensitive)) { + fNormalizeAction->setChecked(true); + } else if (!tok[1].trimmed().compare("false", Qt::CaseInsensitive)) { + fNormalizeAction->setChecked(false); + } else { + QMessageBox::critical(this, "**ERROR**", "found wrong norm cmd, will ignore it."); + return; + } } else if (cmd.startsWith("flush")) { fCmdHistory.clear(); fCmdLine->setText("$ "); + fCmdLineHistory->clear(); + fCmdLineHistory->insertPlainText("$"); } else if (cmd.startsWith("help")) { helpCmds(); + } else { + helpCmds(); } } @@ -2148,8 +2211,8 @@ void PmuppGui::selectCollection(QString cmd) bool ok; int ival, pos=-1; QString str = cmd; - if (cmd.contains("select collection")) { - str.remove("select collection"); + if (cmd.contains("select")) { + str.remove("select"); str = str.trimmed(); } else if (cmd.contains("sc")) { pos = cmd.indexOf("sc"); @@ -2163,6 +2226,9 @@ void PmuppGui::selectCollection(QString cmd) if (ok) { // it is a number if (ival < fColList->count()) fColList->setCurrentRow(ival); + else + QMessageBox::critical(this, "**ERROR**", + QString("Found Row Selection %1 which is > #Selections=%2").arg(ival).arg(fColList->count())); } else { // assume it is a collection name for (int i=0; icount(); i++) { if (fColList->item(i)->text() == str) { diff --git a/src/musredit_qt5/mupp/PmuppGui.h b/src/musredit_qt5/mupp/PmuppGui.h index cad0ce51..658e8f71 100644 --- a/src/musredit_qt5/mupp/PmuppGui.h +++ b/src/musredit_qt5/mupp/PmuppGui.h @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -170,6 +171,8 @@ private: QPushButton *fRemoveX; QPushButton *fRemoveY; QPushButton *fPlot; + QSplitter *fCmdSplitter; + QPlainTextEdit *fCmdLineHistory; QLineEdit *fCmdLine; QPushButton *fExitButton; @@ -198,8 +201,8 @@ private: private slots: void refresh(); void remove(); - void addX(); - void addY(); + void addX(QString param=""); + void addY(QString param=""); void removeX(); void removeY(); void addDitto(); diff --git a/src/musredit_qt5/mupp/PmuppScript.cpp b/src/musredit_qt5/mupp/PmuppScript.cpp index 61a32746..c6357a4b 100644 --- a/src/musredit_qt5/mupp/PmuppScript.cpp +++ b/src/musredit_qt5/mupp/PmuppScript.cpp @@ -51,6 +51,7 @@ PmuppScript::PmuppScript(QStringList script) : fLoadPath = QString("./"); fSavePath = QString("./"); fSelected = -2; // nothing selected + fNorm = false; fAdmin = new PmuppAdmin(); } @@ -98,10 +99,12 @@ int PmuppScript::executeScript() status = selectAll(); } else if (cmd.startsWith("select ")) { status = select(cmd); - } else if (cmd.startsWith("addX")) { + } else if (cmd.startsWith("x")) { status = addX(cmd); - } else if (cmd.startsWith("addY")) { + } else if (cmd.startsWith("y")) { status = addY(cmd); + } else if (cmd.startsWith("norm")) { + fNorm = true; } else if (cmd.startsWith("plot")) { status = plot(cmd); } else if (cmd.startsWith("macro")) { @@ -504,6 +507,7 @@ int PmuppScript::macro(const QString str, const QString plotFln) QString collName; int count=0; double x_min=0.0, x_max=0.0, x_min_new=0.0, x_max_new=0.0, y_min=0.0, y_max=0.0, y_min_new=0.0, y_max_new=0.0; + double dval, y_min_norm=1.0e10; for (int i=0; iGetCollectionName(i); @@ -547,15 +551,27 @@ int PmuppScript::macro(const QString str, const QString plotFln) } fout << " // y-values" << endl; for (int k=0; k fPlotInfo; + bool fNorm; QString fLoadPath; QString fSavePath; diff --git a/src/musredit_qt5/mupp/examples/field-vs-energy.txt b/src/musredit_qt5/mupp/examples/field-vs-energy.txt index ab265b2a..260c66a1 100644 --- a/src/musredit_qt5/mupp/examples/field-vs-energy.txt +++ b/src/musredit_qt5/mupp/examples/field-vs-energy.txt @@ -9,8 +9,8 @@ load YBCO-40nm-T120K-FC150mT-Escan.db selectAll -addX dataE -addY Field +x dataE +y Field savePath ./ plot FieldVsEnergy.pdf diff --git a/src/musredit_qt5/mupp/examples/field-vs-temp.txt b/src/musredit_qt5/mupp/examples/field-vs-temp.txt new file mode 100644 index 00000000..44cd728d --- /dev/null +++ b/src/musredit_qt5/mupp/examples/field-vs-temp.txt @@ -0,0 +1,19 @@ +# This is a comment + +loadPath ./ + +load YBCO-40nm-FC-E3p8keV-B10mT-Tscan.db +load YBCO-40nm-FC-E3p8keV-B150mT-Tscan.db + +selectAll + +norm +x dataT +y Field + +savePath ./ +plot FieldVsTemp.pdf + +macro FieldVsTemp.C + +# end diff --git a/src/musredit_qt5/mupp/examples/sigma-vs-temp.txt b/src/musredit_qt5/mupp/examples/sigma-vs-temp.txt new file mode 100644 index 00000000..3c74bcc5 --- /dev/null +++ b/src/musredit_qt5/mupp/examples/sigma-vs-temp.txt @@ -0,0 +1,18 @@ +# This is a comment + +loadPath ./ + +load YBCO-40nm-FC-E3p8keV-B10mT-Tscan.db +load YBCO-40nm-FC-E3p8keV-B150mT-Tscan.db + +selectAll + +x dataT +y Sigma + +savePath ./ +plot SigmaVsTemp.pdf + +macro SigmaVsTemp.C + +# end diff --git a/src/musredit_qt5/mupp/mupp.cpp b/src/musredit_qt5/mupp/mupp.cpp index 9f032628..301eb2c7 100644 --- a/src/musredit_qt5/mupp/mupp.cpp +++ b/src/musredit_qt5/mupp/mupp.cpp @@ -72,8 +72,9 @@ void mupp_syntax() cout << " select : selects collection , where is either the number" << endl; cout << " of the collections, or its name, e.g. " << endl; cout << " select YBCO-40nm-T5K-FC150mT-Escan.db" << endl; - cout << " addX