Skip to content

Commit

Permalink
misc code improvements and better dev documentation, re #12610
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeMPouzols committed Aug 12, 2015
1 parent 8bc0b43 commit 41bd8f0
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ class DLLExport EnggDiffractionPresenter : public IEnggDiffractionPresenter {
void parseCalibrateFilename(const std::string &path, std::string &instName,
std::string &vanNo, std::string &ceriaNo);

void loadVanadiumWorkspaces(const std::string &vanNo, const std::string &dir,
Mantid::API::MatrixWorkspace_sptr &vanIntegWS,
Mantid::API::MatrixWorkspace_sptr &vanCurvesWS);
void
loadVanadiumPrecalcWorkspaces(const std::string &vanNo,
const std::string &dir,
Mantid::API::MatrixWorkspace_sptr &vanIntegWS,
Mantid::API::MatrixWorkspace_sptr &vanCurvesWS);

void calcVanadiumWorkspaces(const std::string &vanNo,
Mantid::API::MatrixWorkspace_sptr &vanIntegWS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
</widget>
</item>
<item row="2" column="1" colspan="4">
<widget class="QLineEdit" name="lineEdit_current_calib_path">
<widget class="QLineEdit" name="lineEdit_current_calib_filename">
<property name="readOnly">
<bool>true</bool>
</property>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ class MANTIDQT_CUSTOMINTERFACES_DLL EnggDiffractionViewQtGUI

std::string askNewCalibrationFilename(const std::string &suggestedFname);

std::vector<std::string> logMsgs() const { return m_logMsgs; }
std::string askExistingCalibFilename();

std::vector<std::string> logMsgs() const { return m_logMsgs; }

std::string getRBNumber() const;

Expand All @@ -81,12 +83,17 @@ class MANTIDQT_CUSTOMINTERFACES_DLL EnggDiffractionViewQtGUI

std::string currentCeriaNo() const;

std::string currentCalibFile() const;

std::string newVanadiumNo() const;

std::string newCeriaNo() const;

std::string outCalibFilename() const { return m_outCalibFilename; }

void newCalibLoaded(const std::string &vanadiumNo, const std::string &ceriaNo,
const std::string &fname);

private slots:
/// for buttons, do calibrate and similar
void loadCalibrationClicked();
Expand Down Expand Up @@ -136,18 +143,24 @@ private slots:
Ui::EnggDiffractionQtTabCalib m_uiTabCalib;
Ui::EnggDiffractionQtTabSettings m_uiTabSettings;

// instrument selected (ENGIN-X, etc.)
/// instrument selected (ENGIN-X, etc.)
std::string m_currentInst;
// current calibration produced in the 'Calibration' tab
/// current calibration produced in the 'Calibration' tab
std::string m_currentCalibFilename;
// calibration settings - from/to the 'settings' tab
/// calibration settings - from/to the 'settings' tab
EnggDiffCalibSettings m_calibSettings;
std::string m_outCalibFilename;

/// for the 'Rebin' parameter of some Engg* algorithms
static const double s_defaultRebinWidth;
static const double g_defaultRebinWidth;

/// supported file extensions string for IPARM files (for the open
/// file dialogs)
static const std::string g_iparmExtStr;
/// supported file extensions string for the pixel (full) claibration
static const std::string g_pixelCalibExt;

// presenter as in the model-view-presenter
/// presenter as in the model-view-presenter
boost::scoped_ptr<IEnggDiffractionPresenter> m_presenter;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class IEnggDiffractionView {
IEnggDiffractionView(){};
virtual ~IEnggDiffractionView(){};

/// @name Direct (and usually modal) user interaction
//@{
/**
* Display a warning to the user (for example as a pop-up window).
*
Expand Down Expand Up @@ -75,6 +77,14 @@ class IEnggDiffractionView {
virtual std::string
askNewCalibrationFilename(const std::string &suggestedFname) = 0;

/**
* Gets an (existing file) filename from the user, to load a calibration.
*
* @return filename given by the user (empty if cancelled / not interested)
*/
virtual std::string askExistingCalibFilename() = 0;
//@}

/**
* Gives one or more messages that this View wants to send to the
* logging system.
Expand Down Expand Up @@ -117,6 +127,14 @@ class IEnggDiffractionView {
*/
virtual std::string currentCeriaNo() const = 0;

/**
* The filename of the current calibration (corresponding to the
* current Vanadium, Ceria)
*
* @return filename (normally full path)
*/
virtual std::string currentCalibFile() const = 0;

/**
* The Vanadium run number to use for a new calibration
*
Expand All @@ -138,6 +156,18 @@ class IEnggDiffractionView {
*/
virtual std::string outCalibFilename() const = 0;

/**
* A new calibration is calculated or loaded => update display and
* widgets. This becomes the new 'current' calibration.
*
* @param vanadiumNo new Vanadium run number
* @param ceraNo new Ceria run number
* @param fname new calibration file name
*/
virtual void newCalibLoaded(const std::string &vanadiumNo,
const std::string &ceriaNo,
const std::string &fname) = 0;

/**
* Save settings (normally when closing the interface). This
* concerns only GUI settings, such as window max/min status and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ void EnggDiffractionPresenter::processStart() {

void EnggDiffractionPresenter::processLoadExistingCalib() {
EnggDiffCalibSettings cs = m_view->currentCalibSettings();

std::string fname = m_view->askExistingCalibFilename();

std::string instName, vanNo, ceriaNo;
parseCalibrateFilename(fname, instName, vanNo, ceriaNo);

m_view->newCalibLoaded(vanNo, ceriaNo, fname);
}

void EnggDiffractionPresenter::processCalcCalib() {
Expand All @@ -102,7 +109,14 @@ void EnggDiffractionPresenter::processCalcCalib() {
std::string sugg = buildCalibrateSuggestedFilename(vanNo, ceriaNo);

std::string outFilename = m_view->askNewCalibrationFilename(sugg);
doCalib(cs, vanNo, ceriaNo, outFilename);
try {
doCalib(cs, vanNo, ceriaNo, outFilename);
m_view->newCalibLoaded(vanNo, ceriaNo, outFilename);
} catch (std::runtime_error &re) {
g_log.error()
<< "The calibration calculations failed. See log messages for details. "
<< std::endl;
}
}

void EnggDiffractionPresenter::processLogMsg() {
Expand All @@ -123,11 +137,19 @@ void EnggDiffractionPresenter::processShutDown() {
cleanup();
}

/**
* Calculate a calibration, responding the the "new calibration"
* action/button.
*
* @param cs user settings
* @param vanNo Vanadium run number
* @param ceriaNo Ceria run number
* @param outFilename output filename chosen by the user
*/
void EnggDiffractionPresenter::doCalib(const EnggDiffCalibSettings &cs,
const std::string &vanNo,
const std::string &ceriaNo,
const std::string &outFilename) {

MatrixWorkspace_sptr vanIntegWS;
MatrixWorkspace_sptr vanCurvesWS;
MatrixWorkspace_sptr ceriaWS;
Expand All @@ -142,17 +164,19 @@ void EnggDiffractionPresenter::doCalib(const EnggDiffCalibSettings &cs,
tzero.reserve(numBanks);
for (size_t i = 0; i < difc.size(); i++) {
auto alg = Algorithm::fromString("EnggCalibrate");
alg->initialize();
alg->setProperty("InputWorkspace", ceriaWS);

try {
alg->initialize();
alg->setProperty("InputWorkspace", ceriaWS);
alg->execute();
} catch (std::runtime_error &re) {
m_view->userError("Error in calibration",
"Could not run EnggCalibrate succesfully for bank " +
boost::lexical_cast<std::string>(i) +
". Error description: " + re.what() +
" Please check also the log messages for details.");
g_log.information() << "Could not write calibration file because of the "
"errors (see log). " << std::endl;
throw re;
}
difc[i] = alg->getProperty("Difc");
tzero[i] = alg->getProperty("Zero");
Expand Down Expand Up @@ -262,6 +286,8 @@ std::string EnggDiffractionPresenter::buildCalibrateSuggestedFilename(
}

/**
* Load precalculated results from Vanadium corrections previously
* calculated.
*
* @param dir directory where the vanadium run should be looked for
* (normally the input calibration directory chosen in the settings of
Expand All @@ -274,7 +300,7 @@ std::string EnggDiffractionPresenter::buildCalibrateSuggestedFilename(
* @param vanCurvesWS output (matrix) workspace loaded from the
* precalculated Vanadium correction file, with the per-bank curves
*/
void EnggDiffractionPresenter::loadVanadiumWorkspaces(
void EnggDiffractionPresenter::loadVanadiumPrecalcWorkspaces(
const std::string &vanNo, const std::string &dir,
MatrixWorkspace_sptr &vanIntegWS, MatrixWorkspace_sptr &vanCurvesWS) {
std::string integFullPath = "";
Expand Down Expand Up @@ -344,8 +370,21 @@ void EnggDiffractionPresenter::findPrecalcVanadiumCorrFilenames(
}

/**
* Produce the two workspaces that are required to apply Vanadium
* corrections. Try to load them if precalculated results are
* available from files, otherwise load the source Vanadium run
* workspace and do the calculations.
*
* @param vanNo
* @param vanNo Vanadium run number
*
* @param inputDirCalib The 'calibration files' input directory given
* in settings
*
* @param vanIntegWS workspace where to create/load the Vanadium
* spectra integration
*
* @param vanCurvesWS workspace where to create/load the Vanadium
* aggregated per-bank curve
*/
void EnggDiffractionPresenter::loadOrCalcanadiumWorkspaces(
const std::string &vanNo, const std::string &inputDirCalib,
Expand Down Expand Up @@ -384,7 +423,8 @@ void EnggDiffractionPresenter::loadOrCalcanadiumWorkspaces(
<< "Found precalculated Vanadium correction features for Vanadium run "
<< vanNo << ". Re-using them." << std::endl;
try {
loadVanadiumWorkspaces(vanNo, inputDirCalib, vanIntegWS, vanCurvesWS);
loadVanadiumPrecalcWorkspaces(vanNo, inputDirCalib, vanIntegWS,
vanCurvesWS);
} catch (std::runtime_error &re) {
m_view->userError(
"Error while loading precalculated Vanadium corrections",
Expand Down

0 comments on commit 41bd8f0

Please sign in to comment.