Files
musrfit/src/musredit_qt6/musredit/PGetSingleHistoRunBlockDialog.cpp

390 lines
15 KiB
C++

/****************************************************************************
PGetSingleHistoRunBlockDialog.cpp
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
*****************************************************************************/
/***************************************************************************
* Copyright (C) 2009-2025 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/**
* @file PGetSingleHistoRunBlockDialog.cpp
* @brief Implementation of the PGetSingleHistoRunBlockDialog class.
* @details This file implements the dialog for creating single histogram type
* RUN blocks in msr files. It handles user input validation and generates
* properly formatted msr-file text for all single histogram RUN block parameters.
*
* @author Andreas Suter
* @date 2009-2025
* @copyright Copyright (C) 2009-2025 by Andreas Suter
* @license GNU General Public License v2 or later
*/
#include <QLineEdit>
#include <QValidator>
#include <QComboBox>
#include <QCheckBox>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
#include "PGetSingleHistoRunBlockDialog.h"
//----------------------------------------------------------------------------------------------------
/**
* @brief Constructs the single histogram RUN block dialog.
*
* @details Initializes the dialog UI and sets up input validators for all
* numeric fields. The dialog is created as modal. The lifetime correction
* checkbox is initialized based on the provided parameter.
*
* @par Input Validators:
* - Integer validators: histogram number, norm, data range, background range,
* background fit parameter, packing, t0, lifetime parameter
* - Double validators: fixed background value, fit range
*
* @param helpUrl URL to the online documentation for single histogram RUN blocks.
* Defaults to empty string if not provided.
* @param lifetimeCorrection Initial state of the lifetime correction checkbox.
* Defaults to true (enabled).
*/
PGetSingleHistoRunBlockDialog::PGetSingleHistoRunBlockDialog(const QString helpUrl,
const bool lifetimeCorrection) : fHelpUrl(helpUrl)
{
setupUi(this);
setModal(true);
fForwardHistoNo_lineEdit->setValidator( new QIntValidator(fForwardHistoNo_lineEdit) );
fNorm_lineEdit->setValidator( new QIntValidator(fNorm_lineEdit) );
fDataStart_lineEdit->setValidator( new QIntValidator(fDataStart_lineEdit) );
fDataEnd_lineEdit->setValidator( new QIntValidator(fDataEnd_lineEdit) );
fBackgroundFix_lineEdit->setValidator( new QDoubleValidator(fBackgroundFix_lineEdit) );
fBackgroundFit_lineEdit->setValidator( new QIntValidator(fBackgroundFit_lineEdit) );
fBackgroundStart_lineEdit->setValidator( new QIntValidator(fBackgroundStart_lineEdit) );
fBackgroundEnd_lineEdit->setValidator( new QIntValidator(fBackgroundEnd_lineEdit) );
fFitRangeStart_lineEdit->setValidator( new QDoubleValidator(fFitRangeStart_lineEdit) );
fFitRangeEnd_lineEdit->setValidator( new QDoubleValidator(fFitRangeEnd_lineEdit) );
fPacking_lineEdit->setValidator( new QIntValidator(fPacking_lineEdit) );
fT0_lineEdit->setValidator( new QIntValidator(fT0_lineEdit) );
fLifetime_lineEdit->setValidator( new QIntValidator(fLifetime_lineEdit) );
fLifetimeCorrection_checkBox->setChecked(lifetimeCorrection);
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Generates the RUN header line for the single histogram block.
*
* @details Creates the first line of the RUN block containing the run file name,
* beamline identifier, institute name, and data file format. The beamline is
* converted to uppercase.
*
* @return Formatted RUN header string, e.g.:
* "RUN data001 GPS PSI ROOT (name beamline institute data-file-format)\\n"
*/
QString PGetSingleHistoRunBlockDialog::getRunHeaderInfo()
{
QString str="";
str = "RUN " + fRunFileName_lineEdit->text() + " ";
str += fBeamline_lineEdit->text().toUpper() + " ";
str += fInstitute_comboBox->currentText() + " ";
str += fFileFormat_comboBox->currentText() + " (name beamline institute data-file-format)\n";
return str;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Generates the parameter map line for the single histogram block.
*
* @details The map line defines the mapping between theory function parameters
* and the fit parameters defined in the FITPARAMETER block. It should contain
* only space-separated integers.
*
* @param[out] valid Set to true if the map contains only digits and spaces,
* false if invalid characters are found.
*
* @return Formatted map string, e.g.: "map 1 2 3 4\\n"
*/
QString PGetSingleHistoRunBlockDialog::getMap(bool &valid)
{
QString str = fMap_lineEdit->text().trimmed().remove(" ");
// check if potentially proper map line
for (int i=0; i<str.length(); i++) {
if (!str[i].isDigit()) {
valid = false;
break;
}
}
str = "map " + fMap_lineEdit->text() + "\n";
return str;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Generates the data range specification for the single histogram block.
*
* @details Creates the "data" line specifying the histogram bin range to be
* used for fitting. Both start and end bins are required.
*
* @param[out] valid Set to true if both data range values are provided,
* false if either is missing.
*
* @return Formatted data range string, e.g.: "data 100 8000\\n"
* Returns empty string if values are missing.
*/
QString PGetSingleHistoRunBlockDialog::getData(bool &valid)
{
QString str="";
if (fDataStart_lineEdit->text().isEmpty() || fDataEnd_lineEdit->text().isEmpty()) {
valid = false;
} else {
str = "data ";
str += fDataStart_lineEdit->text() + " ";
str += fDataEnd_lineEdit->text() + "\n";
valid = true;
}
return str;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Generates the background specification for the single histogram block.
*
* @details Creates either a "background" line (bin ranges for background estimation),
* "backgr.fix" line (fixed background value), or "backgr.fit" line (fit parameter
* for background). Only one type should be specified; if multiple are provided,
* the last one takes precedence.
*
* @par Background Types:
* - **background**: Two integers specifying bin range [start, end] for background estimation
* - **backgr.fix**: One double specifying fixed background counts per bin
* - **backgr.fit**: One integer specifying the fit parameter number for background
*
* @param[out] valid Set to true if valid background info is provided, false if
* no background info given (a default "background 0 10" is returned).
*
* @return Formatted background string, e.g.: "background 50 100\\n"
* or "backgr.fix 15.3\\n" or "backgr.fit 5\\n"
*/
QString PGetSingleHistoRunBlockDialog::getBackground(bool &valid)
{
QString str="";
valid = true;
// check that either backgr.fix or background is given, but not both
if (fBackgroundStart_lineEdit->text().isEmpty() && fBackgroundEnd_lineEdit->text().isEmpty() &&
fBackgroundFix_lineEdit->text().isEmpty() &&
fBackgroundFit_lineEdit->text().isEmpty()) {
valid = false;
str = "background 0 10\n";
} else {
if (!fBackgroundStart_lineEdit->text().isEmpty()) { // assume the rest is given, not fool prove but ...
str = "background ";
str += fBackgroundStart_lineEdit->text() + " ";
str += fBackgroundEnd_lineEdit->text() + "\n";
}
if (!fBackgroundFix_lineEdit->text().isEmpty()) {
str = "backgr.fix ";
str += fBackgroundFix_lineEdit->text() + "\n";
}
if (!fBackgroundFit_lineEdit->text().isEmpty()) {
str = "backgr.fit ";
str += fBackgroundFit_lineEdit->text() + "\n";
}
}
return str;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Generates the fit range specification for the single histogram block.
*
* @details Creates the "fit" line specifying the time range (in microseconds)
* over which the fit will be performed. If no fit range is provided by the user,
* a default range of [0.0, 10.0] is returned.
*
* @param[out] valid Set to true if both fit range values are provided by the user,
* false if using the default range.
*
* @return Formatted fit range string, e.g.: "fit 0.5 9.5\\n"
*/
QString PGetSingleHistoRunBlockDialog::getFitRange(bool &valid)
{
QString str="";
if (fFitRangeStart_lineEdit->text().isEmpty() || fFitRangeEnd_lineEdit->text().isEmpty()) {
str += "fit 0.0 10.0\n";
valid = false;
} else {
str += "fit ";
str += fFitRangeStart_lineEdit->text() + " ";
str += fFitRangeEnd_lineEdit->text() + "\n";
valid = true;
}
return str;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Generates the packing (rebinning) specification for the single histogram block.
*
* @details Creates the "packing" line specifying how many histogram bins should
* be combined into one. A packing of 1 means no rebinning, 2 means combine
* pairs of bins, etc. If no packing is provided, a default value of 1 is used.
*
* @param[out] present Set to true if a packing value was provided by the user,
* false if using the default value.
*
* @return Formatted packing string, e.g.: "packing 10\\n"
*/
QString PGetSingleHistoRunBlockDialog::getPacking(bool &present)
{
QString str="";
if (fPacking_lineEdit->text().isEmpty()) {
present = false;
str += "packing 1\n";
} else {
present = true;
str += "packing " + fPacking_lineEdit->text() + "\n\n";
}
return str;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Generates the T0 (time zero) specification for the single histogram block.
*
* @details Creates the "t0" line specifying the time zero bin for the histogram.
* T0 is the bin corresponding to muon implantation time. This parameter is optional.
*
* @param[out] present Set to true if a t0 value was provided, false if not specified.
*
* @return Formatted t0 string, e.g.: "t0 250\\n"
* Returns empty string if t0 is not specified.
*/
QString PGetSingleHistoRunBlockDialog::getT0(bool &present)
{
QString str="";
if (!fT0_lineEdit->text().isEmpty()) {
str = "t0 ";
str += fT0_lineEdit->text() + "\n";
present = true;
} else {
present = false;
}
return str;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Generates the muon lifetime parameter line for the single histogram block.
*
* @details Creates the "lifetime" line specifying which fit parameter contains
* the muon lifetime value. This is used when the muon lifetime should be fitted
* rather than using the fixed standard value. This parameter is optional.
*
* @param[out] present Set to true if a lifetime parameter was provided, false otherwise.
*
* @return Formatted lifetime string, e.g.: "lifetime 3\\n"
* Returns empty string if lifetime parameter is not specified.
*/
QString PGetSingleHistoRunBlockDialog::getMuonLifetimeParam(bool &present)
{
QString str="";
if (!fLifetime_lineEdit->text().isEmpty()) {
str = "lifetime ";
str += fLifetime_lineEdit->text() + "\n";
present = true;
} else {
present = false;
}
return str;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Generates the lifetime correction flag for the single histogram block.
*
* @details Creates the "lifetimecorrection" line if the checkbox is enabled.
* When present, musrfit will apply muon lifetime correction to the data,
* effectively removing the exponential decay component from the signal.
*
* @param[out] present Set to true if lifetime correction is enabled, false otherwise.
*
* @return "lifetimecorrection\\n" if enabled, empty string if disabled.
*/
QString PGetSingleHistoRunBlockDialog::getLifetimeCorrection(bool &present)
{
QString str="";
if (fLifetimeCorrection_checkBox->isChecked()) {
str = "lifetimecorrection\n";
present = true;
} else {
present = false;
}
return str;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief Opens the online help documentation for single histogram RUN blocks.
*
* @details Attempts to open the help URL in the system's default web browser
* using QDesktopServices. If the URL is empty, displays an informational message.
* If the browser fails to open, displays an error message with a clickable link.
*/
void PGetSingleHistoRunBlockDialog::helpContent()
{
if (fHelpUrl.isEmpty()) {
QMessageBox::information(this, "INFO", "Will eventually show a help window");
} else {
bool ok = QDesktopServices::openUrl(QUrl(fHelpUrl, QUrl::TolerantMode));
if (!ok) {
QString msg = QString("<p>Sorry: Couldn't open default web-browser for the help.<br>Please try: <a href=\"%1\">musrfit docu</a> in your web-browser.").arg(fHelpUrl);
QMessageBox::critical( nullptr, tr("FATAL ERROR"), msg, QMessageBox::Close );
}
}
}
//----------------------------------------------------------------------------------------------------
// END
//----------------------------------------------------------------------------------------------------