Skip to content
Snippets Groups Projects
MuonFitPropertyBrowser.cpp 55.6 KiB
Newer Older
#include "MantidQtMantidWidgets/MuonFitPropertyBrowser.h"
#include "MantidQtMantidWidgets/PropertyHandler.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidKernel/VectorHelper.h"
#include "MantidQtMantidWidgets/StringEditorFactory.h"
#include "MantidQtMantidWidgets/MuonFitDataSelector.h"

// Suppress a warning coming out of code that isn't ours
#if defined(__INTEL_COMPILER)
#pragma warning disable 1125
#elif defined(__GNUC__)
#if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
#pragma GCC diagnostic push
#endif
#pragma GCC diagnostic ignored "-Woverloaded-virtual"
#endif
#include "DoubleEditorFactory.h"
#include "qteditorfactory.h"
#if defined(__INTEL_COMPILER)
#pragma warning enable 1125
#elif defined(__GNUC__)
#if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
#pragma GCC diagnostic pop
#endif
#endif

#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/Expression.h"
#include "MantidAPI/IBackgroundFunction.h"
#include "MantidAPI/IPeakFunction.h"

#include "qttreepropertybrowser.h"
#include "qtpropertymanager.h"

#include <Poco/ActiveResult.h>

#include <QSettings>
#include <QMessageBox>
#include <QFormLayout>

#include <QLayout>
#include <QLabel>
#include <QPushButton>
#include <QMenu>
#include <QSignalMapper>

#include <QCheckBox>

namespace {
Mantid::Kernel::Logger g_log("MuonFitPropertyBrowser");
const QString CUSTOM_LABEL{"Custom"};
const QString ALL_GROUPS_LABEL{"All Groups"};
const QString ALL_PAIRS_LABEL{"All Pairs"};
const QString ALL_PERIODS_LABEL{"All Periods"};
namespace MantidQt {
namespace MantidWidgets {
using namespace Mantid::API;
const std::string MuonFitPropertyBrowser::SIMULTANEOUS_PREFIX{"MuonSimulFit_"};

/**
 * Constructor
 * @param parent :: The parent widget - must be an ApplicationWindow
 * @param mantidui :: The UI form for MantidPlot
*/
MuonFitPropertyBrowser::MuonFitPropertyBrowser(QWidget *parent,
                                               QObject *mantidui)
    : FitPropertyBrowser(parent, mantidui), m_widgetSplitter(nullptr),
      m_mainSplitter(nullptr) {}

/**
* Initialise the muon fit property browser.
*/
void MuonFitPropertyBrowser::init() {
  QWidget *w = new QWidget(this);

  QSettings settings;
  settings.beginGroup("Mantid/FitBrowser");

  /* Create function group */
  QtProperty *functionsGroup = m_groupManager->addProperty("Functions");
  QtProperty *settingsGroup(NULL);

  // Seperates the data and the settings into two seperate categories
  settingsGroup = m_groupManager->addProperty("Data");

  QSettings multiFitSettings;
  multiFitSettings.beginGroup("");

  /* Create function group */
  QtProperty *multiFitSettingsGroup(NULL);

  // Seperates the data and the settings into two seperate categories
  multiFitSettingsGroup = m_groupManager->addProperty("Data");

  // Have slightly different names as requested by the muon scientists.
  m_startX =
      addDoubleProperty(QString("Start (%1s)").arg(QChar(0x03BC))); //(mu);
  m_endX = addDoubleProperty(QString("End (%1s)").arg(QChar(0x03BC)));

Anthony Lim's avatar
Anthony Lim committed
  m_normalization = m_enumManager->addProperty("Normalization");
  m_keepNorm = m_boolManager->addProperty("Fix Normalization");
  bool keepNorm = settings.value("Fix Normalization", QVariant(false)).toBool();
  m_boolManager->setValue(m_keepNorm, keepNorm);

  m_workspace = m_enumManager->addProperty("Workspace");
  m_workspaceIndex = m_intManager->addProperty("Workspace Index");
  m_output = m_stringManager->addProperty("Output");
  m_minimizer = m_enumManager->addProperty("Minimizer");
  m_minimizers << "Levenberg-Marquardt"
               << "Simplex"
               << "Conjugate gradient (Fletcher-Reeves imp.)"
               << "Conjugate gradient (Polak-Ribiere imp.)"
               << "BFGS";
  m_enumManager->setEnumNames(m_minimizer, m_minimizers);
  m_costFunction = m_enumManager->addProperty("Cost function");
  m_costFunctions << "Least squares"
                  << "Ignore positive peaks";
  m_enumManager->setEnumNames(m_costFunction, m_costFunctions);

  m_plotDiff = m_boolManager->addProperty("Plot Difference");
  bool plotDiff = settings.value("Plot Difference", QVariant(true)).toBool();
  m_boolManager->setValue(m_plotDiff, plotDiff);

  m_evaluationType = m_enumManager->addProperty("Evaluate Function As");
  m_evaluationType->setToolTip(
      "Consider using Histogram fit which may produce more accurate results.");
  m_evaluationTypes << "CentrePoint"
                    << "Histogram";
  m_enumManager->setEnumNames(m_evaluationType, m_evaluationTypes);
  int evaluationType =
      settings.value(m_evaluationType->propertyName(), 0).toInt();
  m_enumManager->setValue(m_evaluationType, evaluationType);

  settingsGroup->addSubProperty(m_workspace);
  settingsGroup->addSubProperty(m_workspaceIndex);
  settingsGroup->addSubProperty(m_startX);
Anthony Lim's avatar
Anthony Lim committed
  settingsGroup->addSubProperty(m_endX);
  settingsGroup->addSubProperty(m_normalization);
  settingsGroup->addSubProperty(m_keepNorm);

  // Disable "Browse" button - use case is that first run will always be the one
  // selected on front tab. User will type in the runs they want rather than
  // using the Browse button. (If they want to "Browse" they can use front tab).
  multiFitSettingsGroup->addSubProperty(m_startX);
  multiFitSettingsGroup->addSubProperty(m_endX);
  m_groupsToFit = m_enumManager->addProperty("Groups/Pairs to fit");
Anthony Lim's avatar
Anthony Lim committed
  m_groupsToFitOptions << ALL_GROUPS_LABEL << ALL_PAIRS_LABEL << CUSTOM_LABEL;
  m_showGroupValue << "groups";
  m_showGroup = m_enumManager->addProperty("Selected Groups");
  m_enumManager->setEnumNames(m_groupsToFit, m_groupsToFitOptions);
  multiFitSettingsGroup->addSubProperty(m_groupsToFit);
  multiFitSettingsGroup->addSubProperty(m_showGroup);
Anthony Lim's avatar
Anthony Lim committed
  m_enumManager->setEnumNames(m_showGroup, m_showGroupValue);
  QString tmp = "fwd";
  addGroupCheckbox(tmp);
  tmp = "bwd";
  addGroupCheckbox(tmp);
  m_periodsToFit = m_enumManager->addProperty("Periods to fit");
  m_periodsToFitOptions << ALL_PERIODS_LABEL << "1"
                        << "2" << CUSTOM_LABEL;
  m_showPeriodValue << "1";
  m_showPeriods = m_enumManager->addProperty("Selected Periods");
  m_enumManager->setEnumNames(m_periodsToFit, m_periodsToFitOptions);
  multiFitSettingsGroup->addSubProperty(m_periodsToFit);
  multiFitSettingsGroup->addSubProperty(m_showPeriods);
  m_enumManager->setEnumNames(m_showPeriods, m_showPeriodValue);
Anthony Lim's avatar
Anthony Lim committed
  /* Create editors and assign them to the managers */
  createEditors(w);

  updateDecimals();
  m_functionsGroup = m_browser->addProperty(functionsGroup);
  m_settingsGroup = m_browser->addProperty(settingsGroup);
  m_multiFitSettingsGroup = m_browser->addProperty(multiFitSettingsGroup);
  connect(m_browser, SIGNAL(currentItemChanged(QtBrowserItem *)), this,
Anthony Lim's avatar
Anthony Lim committed
          SLOT(currentItemChanged(QtBrowserItem *)));
Anthony Lim's avatar
Anthony Lim committed
  m_btnGroup = new QGroupBox(tr("Reselect Data"));
  QHBoxLayout *btnLayout = new QHBoxLayout;
  m_reselectGroupBtn = new QPushButton("Groups/Pairs");
  m_reselectPeriodBtn = new QPushButton("Periods");
  m_generateBtn = new QPushButton("Combine Periods");
  m_groupWindow = new QDialog(this);
  m_periodWindow = new QDialog(this);
  m_comboWindow = new QDialog(this);
  m_reselectGroupBtn->setEnabled(false);
  m_reselectPeriodBtn->setEnabled(false);
Anthony Lim's avatar
Anthony Lim committed
  connect(m_reselectGroupBtn, SIGNAL(released()), this,
          SLOT(groupBtnPressed()));
  connect(m_reselectPeriodBtn, SIGNAL(released()), this,
          SLOT(periodBtnPressed()));
  connect(m_generateBtn, SIGNAL(released()), this, SLOT(generateBtnPressed()));

  btnLayout->addWidget(m_reselectGroupBtn);
  btnLayout->addWidget(m_reselectPeriodBtn);
  btnLayout->addWidget(m_generateBtn);

  m_btnGroup->setLayout(btnLayout);
  // Don't show "Function" or "Data" sections as they have separate widgets
  m_browser->setItemVisible(m_functionsGroup, false);
  m_browser->setItemVisible(m_settingsGroup, false);
Anthony Lim's avatar
Anthony Lim committed
  m_browser->setItemVisible(m_multiFitSettingsGroup, true);
  // Custom settings that are specific and asked for by the muon scientists.
  QtProperty *customSettingsGroup = m_groupManager->addProperty("Settings");
  m_rawData = m_boolManager->addProperty("Fit To Raw Data");
  bool data = settings.value("Fit To Raw Data", QVariant(false)).toBool();
  m_boolManager->setValue(m_rawData, data);

  m_showParamErrors = m_boolManager->addProperty("Show Parameter Errors");
  // XXX: showParamErrors is true by default for Muons
  bool showParamErrors =
      settings.value(m_showParamErrors->propertyName(), true).toBool();
  m_boolManager->setValue(m_showParamErrors, showParamErrors);
  m_parameterManager->setErrorsEnabled(showParamErrors);

  customSettingsGroup->addSubProperty(m_minimizer);
  customSettingsGroup->addSubProperty(m_plotDiff);
  customSettingsGroup->addSubProperty(m_rawData);
  customSettingsGroup->addSubProperty(m_showParamErrors);
  customSettingsGroup->addSubProperty(m_evaluationType);
  m_customSettingsGroup = m_browser->addProperty(customSettingsGroup);

  // Initialise the layout.
  initBasicLayout(w);
Tom Perkins's avatar
Tom Perkins committed
  // Create an empty splitter that can hold extra widgets
  m_widgetSplitter = new QSplitter(Qt::Vertical, w);
Tom Perkins's avatar
Tom Perkins committed
  m_widgetSplitter->setSizePolicy(QSizePolicy::Policy::Expanding,
                                  QSizePolicy::Policy::Expanding);

  // This splitter separates the "extra widgets" region from the browser
  m_mainSplitter = new QSplitter(Qt::Vertical, w);
  m_mainSplitter->insertWidget(0, m_widgetSplitter);
  m_mainSplitter->insertWidget(1, m_browser);
  m_mainSplitter->setStretchFactor(0, 1);
  m_mainSplitter->setStretchFactor(1, 0);

  // Insert after the buttons
  auto parentLayout = qobject_cast<QVBoxLayout *>(w->layout());
  if (parentLayout) {
    const int index = parentLayout->count() - 1;
    constexpr int stretchFactor = 10; // so these widgets get any extra space
    parentLayout->insertWidget(index, m_mainSplitter, stretchFactor);
    parentLayout->setSpacing(0);
    parentLayout->setMargin(0);
    parentLayout->setContentsMargins(0, 0, 0, 0);
Anthony Lim's avatar
Anthony Lim committed
    parentLayout->insertWidget(index + 1, m_btnGroup);
  // Update tooltips when function structure is (or might've been) changed in
  // any way
  connect(this, SIGNAL(functionChanged()), SLOT(updateStructureTooltips()));
Anthony Lim's avatar
Anthony Lim committed
// Set up the execution of the muon fit menu
void MuonFitPropertyBrowser::executeFitMenu(const QString &item) {
Anthony Lim's avatar
Anthony Lim committed
  if (item == "TFAsymm") {
	  emit functionUpdateRequested();
	  doTFAsymmFit();
Anthony Lim's avatar
Anthony Lim committed
  } else {
    FitPropertyBrowser::executeFitMenu(item);
  }
// Create group/pair selection pop up
Anthony Lim's avatar
Anthony Lim committed
void MuonFitPropertyBrowser::groupBtnPressed() { genGroupWindow(); }
// Create period selection pop up
Anthony Lim's avatar
Anthony Lim committed
void MuonFitPropertyBrowser::periodBtnPressed() { genPeriodWindow(); }
// Create combination selection pop up
Anthony Lim's avatar
Anthony Lim committed
void MuonFitPropertyBrowser::generateBtnPressed() { genCombinePeriodWindow(); }
pulate the fit button.
* This initialization includes:
*   1. SIGNALs/SLOTs when properties change.
*   2. Actions and associated SIGNALs/SLOTs.
* @param fitMapper the QMap to the fit mapper
* @param fitMenu the QMenu for the fit button
Anthony Lim's avatar
Anthony Lim committed
void MuonFitPropertyBrowser::populateFitMenuButton(QSignalMapper *fitMapper,
                                                   QMenu *fitMenu) {

  m_fitActionTFAsymm = new QAction("TF Asymmetry Fit", this);
  fitMapper->setMapping(m_fitActionTFAsymm, "TFAsymm");
Anthony Lim's avatar
Anthony Lim committed

  FitPropertyBrowser::populateFitMenuButton(fitMapper, fitMenu);
  connect(m_fitActionTFAsymm, SIGNAL(triggered()), fitMapper, SLOT(map()));
  fitMenu->addSeparator();
  fitMenu->addAction(m_fitActionTFAsymm);
/// Enable/disable the Fit button;
void MuonFitPropertyBrowser::setFitEnabled(bool yes) {
Anthony Lim's avatar
Anthony Lim committed
  m_fitActionFit->setEnabled(yes);
  m_fitActionSeqFit->setEnabled(yes);
Anthony Lim's avatar
Anthony Lim committed
  // only allow TFAsymm fit if not keeping norm
  if (!m_boolManager->value(m_keepNorm) && yes) {
Anthony Lim's avatar
Anthony Lim committed
    m_fitActionTFAsymm->setEnabled(yes);
}
/**
* Set the input workspace name
*/
void MuonFitPropertyBrowser::setWorkspaceName(const QString &wsName) {
  int i = m_workspaceNames.indexOf(wsName);
  if (i < 0) {
    // workspace may not be found because add notification hasn't been processed
    // yet
    populateWorkspaceNames();
    i = m_workspaceNames.indexOf(wsName);
  }
  if (i >= 0)
    m_enumManager->setValue(m_workspace, i);
/** Called when a dropdown menu is changed
Anthony Lim's avatar
Anthony Lim committed
* @param prop :: A pointer to the function name property
*/
void MuonFitPropertyBrowser::enumChanged(QtProperty *prop) {
Anthony Lim's avatar
Anthony Lim committed
  if (!m_changeSlotsEnabled)
    return;
  if (prop == m_groupsToFit) {
    int j = m_enumManager->value(m_groupsToFit);
    QString option = m_groupsToFitOptions[j];
    if (option == ALL_GROUPS_LABEL) {
Anthony Lim's avatar
Anthony Lim committed
      setAllGroups();
      m_reselectGroupBtn->setEnabled(false);
    } else if (option == ALL_PAIRS_LABEL) {
Anthony Lim's avatar
Anthony Lim committed
      setAllPairs();
      m_reselectGroupBtn->setEnabled(false);
    } else if (option == CUSTOM_LABEL) {
Anthony Lim's avatar
Anthony Lim committed
      m_reselectGroupBtn->setEnabled(true);
      genGroupWindow();
    }
    updateGroupDisplay();

  } else if (prop == m_periodsToFit) {
    int j = m_enumManager->value(m_periodsToFit);
    QString option = m_periodsToFitOptions[j];
    if (option == CUSTOM_LABEL) {
Anthony Lim's avatar
Anthony Lim committed
      m_reselectPeriodBtn->setEnabled(true);
      genPeriodWindow();
    } else if (option == ALL_PERIODS_LABEL) {
Anthony Lim's avatar
Anthony Lim committed
      setAllPeriods();
      m_reselectPeriodBtn->setEnabled(false);
Anthony Lim's avatar
Anthony Lim committed
    } else {
      for (auto iter = m_periodBoxes.constBegin();
           iter != m_periodBoxes.constEnd(); ++iter) {
        if (option == iter.key()) {
Anthony Lim's avatar
Anthony Lim committed
          m_boolManager->setValue(iter.value(), true);
        } else {
          m_boolManager->setValue(iter.value(), false);
        }
        m_reselectPeriodBtn->setEnabled(false);
      }
    }
    updatePeriodDisplay();
Anthony Lim's avatar
Anthony Lim committed
  } else if (prop == m_workspace) {
    // make sure the output is updated
    FitPropertyBrowser::enumChanged(prop);
    int j = m_enumManager->value(m_workspace);
    std::string option = m_workspaceNames[j].toStdString();
    setOutputName(option);
  } else {
Anthony Lim's avatar
Anthony Lim committed
    FitPropertyBrowser::enumChanged(prop);
  }
Anthony Lim's avatar
Anthony Lim committed
/** Sets the display for
* selected groups
*/
void MuonFitPropertyBrowser::updateGroupDisplay() {
Anthony Lim's avatar
Anthony Lim committed
  m_showGroupValue.clear();
  auto tmp = getChosenGroups().join(",").toStdString();
Anthony Lim's avatar
Anthony Lim committed
  m_showGroupValue << getChosenGroups().join(",");
  m_enumManager->setEnumNames(m_showGroup, m_showGroupValue);
  m_multiFitSettingsGroup->property()->addSubProperty(m_showGroup);
}
/** Sets the display for
* selected periods
*/
void MuonFitPropertyBrowser::updatePeriodDisplay() {
Anthony Lim's avatar
Anthony Lim committed
  m_showPeriodValue.clear();
  auto tmp = getChosenPeriods();
  tmp.replaceInStrings(QRegExp(","), "+");
  m_showPeriodValue << tmp.join(",");
  m_enumManager->setEnumNames(m_showPeriods, m_showPeriodValue);
  if (m_periodsToFitOptions.size() > 1) {
    m_multiFitSettingsGroup->property()->addSubProperty(m_showPeriods);
  }
/** Called when a double property changed
 * @param prop :: A pointer to the property
void MuonFitPropertyBrowser::doubleChanged(QtProperty *prop) {
  if (!m_changeSlotsEnabled)
    return;

  double value = m_doubleManager->value(prop);
    // call setWorkspace to change maxX in functions
    setWorkspace(m_compositeFunction);
    getHandler()->setAttribute(QString("Start (%1s)").arg(QChar(0x03BC)),
                               value); // (mu)
    emit startXChanged(startX());
    emit xRangeChanged(startX(), endX());
    return;
  } else if (prop == m_endX) {
    // call setWorkspace to change minX in functions
    setWorkspace(m_compositeFunction);
    getHandler()->setAttribute(QString("End (%1s)").arg(QChar(0x03BC)), value);
    emit endXChanged(endX());
    emit xRangeChanged(startX(), endX());
    return;
Anthony Lim's avatar
Anthony Lim committed
  } else { // check if it is a constraint
    MantidQt::MantidWidgets::PropertyHandler *h =
        getHandler()->findHandler(prop);
    if (!h)
      return;

    QtProperty *parProp = h->getParameterProperty(prop);
    if (parProp) {
      if (prop->propertyName() == "LowerBound") {
        double loBound = m_doubleManager->value(prop);
        h->addConstraint(parProp, true, false, loBound, 0);
      } else if (prop->propertyName() == "UpperBound") {
        double upBound = m_doubleManager->value(prop);
        h->addConstraint(parProp, false, true, 0, upBound);
    } else { // it could be an attribute
/** @returns the normalization
*/
double MuonFitPropertyBrowser::normalization() const {
Anthony Lim's avatar
Anthony Lim committed
  return readNormalization()[0];
}
void MuonFitPropertyBrowser::setNormalization() {
Anthony Lim's avatar
Anthony Lim committed
  m_normalizationValue.clear();
  m_normalizationValue.append(QString::number(normalization()));
Anthony Lim's avatar
Anthony Lim committed
  m_enumManager->setEnumNames(m_normalization, m_normalizationValue);
/** Called when a bool property changed
 * @param prop :: A pointer to the property
 */
void MuonFitPropertyBrowser::boolChanged(QtProperty *prop) {
  if (prop == m_rawData) {
    const bool val = m_boolManager->value(prop);
    emit fitRawDataClicked(val);
Anthony Lim's avatar
Anthony Lim committed
  if (prop == m_keepNorm) {
    const bool val = m_boolManager->value(prop);
    if (val) { // record data for later
      double norm = readNormalization()[0];
      ITableWorkspace_sptr table = WorkspaceFactory::Instance().createTable();
      AnalysisDataService::Instance().addOrReplace("__keepNorm__", table);
      table->addColumn("double", "norm");
      table->addColumn("int", "spectra");
      TableRow row = table->appendRow();
      row << norm << 0;
      // remove TFAsymm fit
      m_fitActionTFAsymm->setEnabled(false);

    } else { // remove data so it is not used later
      AnalysisDataService::Instance().remove("__keepNorm__");

      // if fit is enabled so should TFAsymm
      if (m_fitActionSeqFit->isEnabled()) {
        m_fitActionTFAsymm->setEnabled(true);
      }
    }
Anthony Lim's avatar
Anthony Lim committed
    // search map for group/pair change
    bool done = false;
    for (auto iter = m_groupBoxes.constBegin(); iter != m_groupBoxes.constEnd();
         ++iter) {
      if (iter.value() == prop) {
        done = true;
        updateGroupDisplay();
        emit groupBoxClicked();
      }
    }
    // search map for period change
    if (done == false) {
      for (auto iter = m_periodBoxes.constBegin();
           iter != m_periodBoxes.constEnd(); ++iter) {
        if (iter.value() == prop) {
          done = true;
          updatePeriodDisplay();
          emit periodBoxClicked();
        }
      }
    }
    if (done == false) {
      // defer to parent class
      FitPropertyBrowser::boolChanged(prop);
    }
/**
*Get the registered function names
*/
void MuonFitPropertyBrowser::populateFunctionNames() {
  const std::vector<std::string> names = FunctionFactory::Instance().getKeys();
  m_registeredFunctions.clear();
  m_registeredPeaks.clear();
  m_registeredBackgrounds.clear();
  for (size_t i = 0; i < names.size(); i++) {
    std::string fnName = names[i];
    QString qfnName = QString::fromStdString(fnName);
    if (qfnName == "MultiBG")
      continue;

    auto f = FunctionFactory::Instance().createFunction(fnName);
    const std::vector<std::string> categories = f->categories();
    bool muon = false;
    for (size_t j = 0; j < categories.size(); ++j) {
      if ((categories[j] == "Muon") || (categories[j] == "General") ||
          (categories[j] == "Background"))
      m_registeredFunctions << qfnName;
    }
    IPeakFunction *pf = dynamic_cast<IPeakFunction *>(f.get());
    // CompositeFunction* cf = dynamic_cast<CompositeFunction*>(f.get());
    if (pf) {
      m_registeredPeaks << qfnName;
    } else if (dynamic_cast<IBackgroundFunction *>(f.get())) {
      m_registeredBackgrounds << qfnName;
      m_registeredOther << qfnName;
    }
  }
}
/**
* Creates an instance of Fit algorithm, sets its properties and launches it.
* @param maxIterations is the maximum number of iterations for the fit
void MuonFitPropertyBrowser::doTFAsymmFit(int maxIterations) {
Anthony Lim's avatar
Anthony Lim committed
  const std::string wsName = workspaceName();

  if (wsName.empty()) {
    QMessageBox::critical(this, "Mantid - Error", "Workspace name is not set");
    return;
  }

  const auto ws = getWorkspace();
  if (!ws) {
    return;
  }

  if (compositeFunction()->nParams() == 0) {
    throw std::runtime_error("No function has been defiend for fitting");
  }
  m_initialParameters.resize(compositeFunction()->nParams());
  for (size_t i = 0; i < compositeFunction()->nParams(); i++) {
    m_initialParameters[i] = compositeFunction()->getParameter(i);
  }
  m_fitActionUndoFit->setEnabled(true);

  // Calculate the asymmetry

  std::string funStr = getFittingFunction()->asString();

  Mantid::API::IAlgorithm_sptr asymmAlg =
      Mantid::API::AlgorithmManager::Instance().create(
          "CalculateMuonAsymmetry");
  asymmAlg->initialize();
  asymmAlg->setPropertyValue("FittingFunction", funStr);
  asymmAlg->setProperty("InputDataType", "asymmetry");
  asymmAlg->setProperty("InputWorkspace", ws);
  asymmAlg->setProperty("StartX", startX());
  asymmAlg->setProperty("EndX", endX());
  asymmAlg->setPropertyValue("OutputWorkspace", wsName);
  asymmAlg->setPropertyValue("Minimizer", minimizer(true));
  asymmAlg->setProperty("MaxIterations", maxIterations);
  std::vector<double> norm = readNormalization();
  std::vector<int> spectra;
  spectra.push_back(0);

  asymmAlg->setProperty("Spectra", spectra);
  asymmAlg->setProperty("PreviousNormalizationConstant", norm);
  asymmAlg->execute();
  if (!asymmAlg->isExecuted()) {
    throw std::runtime_error("Asymmetry Calculation has failed.");
  }
  // record result
  auto tmp = asymmAlg->getPropertyValue("NormalizationConstant");
Anthony Lim's avatar
Anthony Lim committed
  std::vector<double> normEst =
      Mantid::Kernel::VectorHelper::splitStringIntoVector<double>(tmp);
Anthony Lim's avatar
Anthony Lim committed
  ITableWorkspace_sptr table = WorkspaceFactory::Instance().createTable();
  AnalysisDataService::Instance().addOrReplace("__norm__", table);
  table->addColumn("double", "norm");
  table->addColumn("int", "spectra");

  for (double norm : normEst) {
    TableRow row = table->appendRow();

    row << norm << 0;
  }
  /////////////////////////////////////////////////
  // calculate the fit explicitly -> above does not get the function exactly
  // right
  try {
    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("Fit");
    alg->initialize();
    if (isHistogramFit()) {
      alg->setProperty("EvaluationType", "Histogram");
    }
    alg->setPropertyValue("Function", funStr);
    alg->setProperty("InputWorkspace", ws); // try the raw workspace....
    alg->setProperty("WorkspaceIndex", workspaceIndex());
    alg->setProperty("StartX", startX());
    alg->setProperty("EndX", endX());
    alg->setPropertyValue("Output", outputName());
    alg->setPropertyValue("Minimizer", minimizer(true));
    alg->setProperty("IgnoreInvalidData", ignoreInvalidData());
    alg->setPropertyValue("CostFunction", costFunction());
    alg->setProperty("MaxIterations", maxIterations);
    alg->setProperty("PeakRadius", getPeakRadius());
    if (!isHistogramFit()) {
      alg->setProperty("Normalise", getShouldBeNormalised());
Anthony Lim's avatar
Anthony Lim committed
      // Always output each composite function but not necessarily plot it
      alg->setProperty("OutputCompositeMembers", true);
      if (alg->existsProperty("ConvolveMembers")) {
        alg->setProperty("ConvolveMembers", convolveMembers());
      }
    }
    observeFinish(alg);
    alg->executeAsync();

  } catch (const std::exception &e) {
    QString msg = "Fit algorithm failed.\n\n" + QString(e.what()) + "\n";
    QMessageBox::critical(this, "Mantid - Error", msg);
  }
  setNormalization();
}*/
/**
* Creates an instance of Fit algorithm, sets its properties and launches it.
*/
void MuonFitPropertyBrowser::doTFAsymmFit() {
	std::string wsName = workspaceName();
	auto norms=readMultipleNormalization();	
	std::vector<double> normVec;
	// TFAsymm calculation -> there is already some estimated data
	//rescale WS:
	rescaleWS(norms, wsName,1.0);
	//The order of the input is the same
	// as the order of the workspace list
	// create a vec of norm in the same order
	std::string tmp = wsName;
	std::replace(tmp.begin(), tmp.end(), ' ', ';');
	auto it = norms.find(tmp);
	normVec.push_back(it->second);
	// If we are doing a simultaneous fit, rescale here too
	const int nWorkspaces = static_cast<int>(m_workspacesToFit.size());
	if (nWorkspaces > 1) {
		for (int i = 1; i < nWorkspaces; i++) {
			rescaleWS(norms,m_workspacesToFit[i],1.0);
			tmp = m_workspacesToFit[i];
			std::replace(tmp.begin(), tmp.end(), ' ', ';');
			auto it = norms.find(tmp);
			normVec.push_back(it->second);
		}
	}
	//get function for TFAsymm fit
	// N(1+function)
	std::string funStr = getTFAsymmFitFunction(getFittingFunction()->asString(), normVec);
	
	//do the TF Asymm fit
	wsName = workspaceName();

	if (wsName.empty()) {
		QMessageBox::critical(this, "Mantid - Error", "Workspace name is not set");
		return;
	}
	try {
		m_initialParameters.resize(compositeFunction()->nParams());
		for (size_t i = 0; i < compositeFunction()->nParams(); i++) {
			m_initialParameters[i] = compositeFunction()->getParameter(i);
		}
		m_fitActionUndoFit->setEnabled(true);

		// Delete any existing results for this workspace, UNLESS we are doing a
		// simultaneous fit
		if (m_workspacesToFit.size() < 2) {
			if (AnalysisDataService::Instance().doesExist(
				wsName + "_NormalisedCovarianceMatrix")) {
				FrameworkManager::Instance().deleteWorkspace(
					wsName + "_NormalisedCovarianceMatrix");
			}
			if (AnalysisDataService::Instance().doesExist(wsName + "_Parameters")) {
				FrameworkManager::Instance().deleteWorkspace(wsName + "_Parameters");
			}
			if (AnalysisDataService::Instance().doesExist(wsName + "_Workspace")) {
				FrameworkManager::Instance().deleteWorkspace(wsName + "_Workspace");
			}
		}

		IAlgorithm_sptr alg = AlgorithmManager::Instance().create("Fit");
		alg->initialize();
		if (m_compositeFunction->name() == "MultiBG") {
			alg->setProperty("Function", "");
		}
		else {
			alg->setPropertyValue("Function", funStr);
		}
		if (rawData())
			alg->setPropertyValue("InputWorkspace", wsName + "_Raw");
		else
			alg->setPropertyValue("InputWorkspace", wsName);
		alg->setProperty("WorkspaceIndex", workspaceIndex());
		alg->setProperty("StartX", startX());
		alg->setProperty("EndX", endX());
		alg->setPropertyValue("Minimizer", minimizer());
		alg->setPropertyValue("CostFunction", costFunction());

		// If we are doing a simultaneous fit, set this up here
		const int nWorkspaces = static_cast<int>(m_workspacesToFit.size());
		if (nWorkspaces > 1) {
			alg->setPropertyValue("InputWorkspace", m_workspacesToFit[0]);
			// Remove existing results with the same name
			if (AnalysisDataService::Instance().doesExist(outputName())) {
				AnalysisDataService::Instance().deepRemoveGroup(outputName());
			}
			for (int i = 1; i < nWorkspaces; i++) {
				std::string suffix = boost::lexical_cast<std::string>(i);
				alg->setPropertyValue("InputWorkspace_" + suffix, m_workspacesToFit[i]);
				alg->setProperty("WorkspaceIndex_" + suffix, workspaceIndex());
				alg->setProperty("StartX_" + suffix, startX());
				alg->setProperty("EndX_" + suffix, endX());
			}
		}
		else {
			setSingleFitLabel(wsName);
		}
		alg->setPropertyValue("Output", outputName());

		observeFinish(alg);
		alg->executeAsync();
	}
	catch (const std::exception &e) {
		QString msg = "Fit algorithm failed.\n\n" + QString(e.what()) + "\n";
		QMessageBox::critical(this, "Mantid - Error", msg);
	}/*
	//transform data back to Asymm
	wsName = workspaceName();
	//*********************************************************************************
	//replace with update
	norms = readMultipleNormalization();
	//rescale WS:
	rescaleWS(norms, wsName, -1.0);
	// If we are doing a simultaneous fit, rescale here too
	if (nWorkspaces > 1) {
		for (int i = 1; i < nWorkspaces; i++) {
			rescaleWS(norms, m_workspacesToFit[i], -1.0);
		}
	}*/
		// Fit
		runFit();	
}
std::vector<double> readNormalization() {
Anthony Lim's avatar
Anthony Lim committed
  std::vector<double> norm;
  if (!AnalysisDataService::Instance().doesExist("__norm__")) {
    norm.push_back(22.423);
  } else {
    Mantid::API::ITableWorkspace_sptr table =
        boost::dynamic_pointer_cast<Mantid::API::ITableWorkspace>(
            Mantid::API::AnalysisDataService::Instance().retrieve("__norm__"));
    auto colNorm = table->getColumn("norm");

    for (size_t j = 0; j < table->rowCount(); j++) {
Anthony Lim's avatar
Anthony Lim committed
      norm.push_back((*colNorm)[j]); // record and update norm....
    }
  }
  return norm;
std::map<std::string, double> readMultipleNormalization() {
	std::map<std::string,double> norm;
	if (AnalysisDataService::Instance().doesExist("multiNorm")) {
		Mantid::API::ITableWorkspace_sptr table =
			boost::dynamic_pointer_cast<Mantid::API::ITableWorkspace>(
				Mantid::API::AnalysisDataService::Instance().retrieve("multiNorm"));
		auto colNorm = table->getColumn("norm");
		auto colName = table->getColumn("name");

		for (size_t j = 0; j < table->rowCount(); j++) {
			norm[colName->cell<std::string>(j)]=((*colNorm)[j]); // record and update norm....
		}
		
	}
	else {
		g_log.error("No normalizations found to read");
	}
	return norm;
}
std::string MuonFitPropertyBrowser::getTFAsymmFitFunction(const std::string original, const std::vector<double> norm) {
	std::vector<std::string> splitString;
	std::string output;
	//splits into different groups/pairs
	//first element is setup
	boost::algorithm::split(splitString,original, boost::is_any_of(";"));
	int normCounter = 0;
	for (int j = 0; j < splitString.size(); j++) {
		if (splitString[j].substr(0,5)=="name=") {
			/*
			std::string function = ";(name=FlatBackground,$domains=i,A0=" +
			std::to_string(norm[normCounter]);
			function += ";(name = FlatBackground, A0 = 1.0, ties = (A0 = 1.0); ";
			splitString[j].insert(0, function);
			std::size_t endOfFunction = splitString[j].find(",$");
			splitString[j].insert(endOfFunction, ")");
			splitString[j].substr(0, endOfFunction + 1);
			normCounter+= 1;*/
		}
		output += splitString[j] ; 
	}
	//
	return output;
}

void MuonFitPropertyBrowser::rescaleWS(const std::map<std::string, double>norm,const std::string wsName,const double shift) {
	//get norm: 
	std::string tmp = wsName;
	// stored with ; instead of spaces
	std::replace(tmp.begin(), tmp.end(), ' ', ';');
	auto it = norm.find(tmp);
	if (it == norm.end()) {
		g_log.error("WS not found: "+wsName);
		return;
	}
	double value = it->second;
	//go back to normalized counts
	if (shift == 1.0) {
		IAlgorithm_sptr alg = AlgorithmManager::Instance().create("Scale");
		alg->initialize();
		alg->setProperty("InputWorkspace", wsName);
		alg->setProperty("OutputWorkspace", wsName);
		alg->setProperty("Factor",shift);
		alg->setProperty("Operation", "Add");
		alg->execute();
	}
	IAlgorithm_sptr alg = AlgorithmManager::Instance().create("Scale");
	alg->initialize();
	alg->setProperty("InputWorkspace", wsName);
	alg->setProperty("OutputWorkspace", wsName);
	alg->setProperty("Factor", value);
	alg->setProperty("Operation", "Multiply");
	alg->execute();
	// if to asymmetry 
	if (shift == -1) {
		IAlgorithm_sptr alg = AlgorithmManager::Instance().create("Scale");
		alg->initialize();
		alg->setProperty("InputWorkspace", wsName);
		alg->setProperty("OutputWorkspace", wsName);
		alg->setProperty("Factor", shift);
		alg->setProperty("Operation", "Add");
		alg->execute();
	}
}
 * Requests checks and updates prior to running a fit
void MuonFitPropertyBrowser::fit() { emit preFitChecksRequested(false); }
/**
 * Creates an instance of Fit algorithm, sets its properties and launches it.
 */
void MuonFitPropertyBrowser::runFit() {
  std::string wsName = workspaceName();

  if (wsName.empty()) {
    QMessageBox::critical(this, "Mantid - Error", "Workspace name is not set");
    m_initialParameters.resize(compositeFunction()->nParams());
    for (size_t i = 0; i < compositeFunction()->nParams(); i++) {
      m_initialParameters[i] = compositeFunction()->getParameter(i);
    }
    m_fitActionUndoFit->setEnabled(true);

    // Delete any existing results for this workspace, UNLESS we are doing a
    // simultaneous fit
    if (m_workspacesToFit.size() < 2) {
      if (AnalysisDataService::Instance().doesExist(
              wsName + "_NormalisedCovarianceMatrix")) {
        FrameworkManager::Instance().deleteWorkspace(
            wsName + "_NormalisedCovarianceMatrix");
      }
      if (AnalysisDataService::Instance().doesExist(wsName + "_Parameters")) {
        FrameworkManager::Instance().deleteWorkspace(wsName + "_Parameters");
      }
      if (AnalysisDataService::Instance().doesExist(wsName + "_Workspace")) {
        FrameworkManager::Instance().deleteWorkspace(wsName + "_Workspace");
      }
    IAlgorithm_sptr alg = AlgorithmManager::Instance().create("Fit");
    if (m_compositeFunction->name() == "MultiBG") {
      alg->setPropertyValue("Function", "");
    } else if (m_compositeFunction->nFunctions() > 1) {
      alg->setProperty("Function", boost::dynamic_pointer_cast<IFunction>(
                                       m_compositeFunction));
    } else {
      alg->setProperty("Function", boost::dynamic_pointer_cast<IFunction>(
                                       m_compositeFunction->getFunction(0)));
    }
      alg->setPropertyValue("InputWorkspace", wsName + "_Raw");
      alg->setPropertyValue("InputWorkspace", wsName);
    alg->setProperty("WorkspaceIndex", workspaceIndex());
    alg->setProperty("StartX", startX());
    alg->setProperty("EndX", endX());
    alg->setPropertyValue("Minimizer", minimizer());
    alg->setPropertyValue("CostFunction", costFunction());

    // If we are doing a simultaneous fit, set this up here
    const int nWorkspaces = static_cast<int>(m_workspacesToFit.size());
    if (nWorkspaces > 1) {
      alg->setPropertyValue("InputWorkspace", m_workspacesToFit[0]);
      // Remove existing results with the same name
      if (AnalysisDataService::Instance().doesExist(outputName())) {
        AnalysisDataService::Instance().deepRemoveGroup(outputName());
      }
      for (int i = 1; i < nWorkspaces; i++) {
        std::string suffix = boost::lexical_cast<std::string>(i);
        alg->setPropertyValue("InputWorkspace_" + suffix, m_workspacesToFit[i]);
        alg->setProperty("WorkspaceIndex_" + suffix, workspaceIndex());
        alg->setProperty("StartX_" + suffix, startX());
        alg->setProperty("EndX_" + suffix, endX());
      }
Anthony Lim's avatar
Anthony Lim committed
    } else {
      setSingleFitLabel(wsName);
Anthony Lim's avatar
Anthony Lim committed
    alg->setPropertyValue("Output", outputName());
    observeFinish(alg);
    alg->executeAsync();
  } catch (const std::exception &e) {
    QString msg = "Fit algorithm failed.\n\n" + QString(e.what()) + "\n";
    QMessageBox::critical(this, "Mantid - Error", msg);
void MuonFitPropertyBrowser::runSequentialFit() {
  emit sequentialFitRequested();
}

/**
 * Requests checks and updates prior to running a sequential fit
 */
void MuonFitPropertyBrowser::sequentialFit() {
  emit preFitChecksRequested(true);
 * Connect to the AnalysisDataService when shown
void MuonFitPropertyBrowser::showEvent(QShowEvent *e) {
  (void)e;
  observePostDelete();
  populateWorkspaceNames();
}

/** Check if the workspace can be used in the fit. The accepted types are
  * MatrixWorkspaces same size and that it isn't the generated raw file.
  * @param ws :: The workspace
  */