Newer
Older
}
/**
* Focuses a run, produces a focused workspace, and saves it into a
* file.
*
* @param cs user settings for calibration (this does not calibrate but
* uses calibration input files such as vanadium runs
*
Federico Montesino Pouzols
committed
* @param fullFilename full path for the output (focused) filename
* @param runNo input run to focus
*
* @param bank instrument bank number to focus
*
* @param specNos string specifying a list of spectra (for cropped
* focusing)
*/
void EnggDiffractionPresenter::doFocusing(const EnggDiffCalibSettings &cs,
const std::string &fullFilename,
const std::string &runNo, size_t bank,
const std::string &specNos,
const std::string &dgFile) {
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
ITableWorkspace_sptr vanIntegWS;
MatrixWorkspace_sptr vanCurvesWS;
MatrixWorkspace_sptr inWS;
const std::string vanNo = m_view->currentVanadiumNo();
loadOrCalcVanadiumWorkspaces(vanNo, cs.m_inputDirCalib, vanIntegWS,
vanCurvesWS, cs.m_forceRecalcOverwrite);
const std::string inWSName = "engggui_focusing_input_ws";
const std::string instStr = m_view->currentInstrument();
try {
auto load = Algorithm::fromString("Load");
load->initialize();
load->setPropertyValue("Filename", instStr + runNo);
load->setPropertyValue("OutputWorkspace", inWSName);
load->execute();
AnalysisDataServiceImpl &ADS = Mantid::API::AnalysisDataService::Instance();
inWS = ADS.retrieveWS<MatrixWorkspace>(inWSName);
} catch (std::runtime_error &re) {
g_log.error()
<< "Error while loading sample data for focusing. "
"Could not run the algorithm Load succesfully for the focusing "
"sample (run number: " +
runNo + "). Error description: " + re.what() +
" Please check also the previous log messages for details.";
throw;
}
std::string outWSName;
if (!dgFile.empty()) {
outWSName = "engggui_focusing_output_ws_texture_bank_" +
boost::lexical_cast<std::string>(bank);
} else if (specNos.empty()) {
outWSName = "engggui_focusing_output_ws_bank_" +
boost::lexical_cast<std::string>(bank);
} else {
outWSName = "engggui_focusing_output_ws_cropped";
}
try {
auto alg = Algorithm::fromString("EnggFocus");
alg->initialize();
alg->setProperty("InputWorkspace", inWSName);
alg->setProperty("OutputWorkspace", outWSName);
alg->setProperty("VanIntegrationWorkspace", vanIntegWS);
alg->setProperty("VanCurvesWorkspace", vanCurvesWS);
// cropped / normal focusing
if (specNos.empty()) {
alg->setPropertyValue("Bank", boost::lexical_cast<std::string>(bank));
} else {
alg->setPropertyValue("SpectrumNumbers", specNos);
// TODO: use detector positions (from calibrate full) when available
// alg->setProperty(DetectorPositions, TableWorkspace)
alg->execute();
const bool plotFocusedWS = m_view->focusedOutWorkspace();
m_view->plotFocusedSpectrum(outWSName);
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
} catch (std::runtime_error &re) {
g_log.error() << "Error in calibration. ",
"Could not run the algorithm EnggCalibrate succesfully for bank " +
boost::lexical_cast<std::string>(bank) + ". Error description: " +
re.what() + " Please check also the log messages for details.";
throw;
}
g_log.notice() << "Produced focused workspace: " << outWSName << std::endl;
try {
g_log.debug() << "Going to save focused output into nexus file: "
<< fullFilename << std::endl;
auto alg = Algorithm::fromString("SaveNexus");
alg->initialize();
alg->setPropertyValue("InputWorkspace", outWSName);
alg->setPropertyValue("Filename", fullFilename);
alg->execute();
} catch (std::runtime_error &re) {
g_log.error() << "Error in calibration. ",
"Could not run the algorithm EnggCalibrate succesfully for bank " +
boost::lexical_cast<std::string>(bank) + ". Error description: " +
re.what() + " Please check also the log messages for details.";
throw;
g_log.notice() << "Saved focused workspace as file: " << fullFilename
<< std::endl;
* 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 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
*
* @param forceRecalc whether to calculate Vanadium corrections even
* if the files of pre-calculated results are found
void EnggDiffractionPresenter::loadOrCalcVanadiumWorkspaces(
const std::string &vanNo, const std::string &inputDirCalib,
ITableWorkspace_sptr &vanIntegWS, MatrixWorkspace_sptr &vanCurvesWS,
bool forceRecalc) {
bool foundPrecalc = false;
std::string preIntegFilename, preCurvesFilename;
findPrecalcVanadiumCorrFilenames(vanNo, inputDirCalib, preIntegFilename,
preCurvesFilename, foundPrecalc);
if (forceRecalc || !foundPrecalc) {
g_log.notice()
<< "Calculating Vanadium corrections. This may take a few seconds..."
<< std::endl;
try {
calcVanadiumWorkspaces(vanNo, vanIntegWS, vanCurvesWS);
} catch (std::invalid_argument &ia) {
g_log.error() << "Failed to calculate Vanadium corrections. "
"There was an error in the execution of the algorithms "
"required to calculate Vanadium corrections. Some "
"properties passed to the algorithms were invalid. "
"This is possibly because some of the settings are not "
"consistent. Please check the log messages for "
"details. Details: " +
Federico Montesino Pouzols
committed
std::string(ia.what()) << std::endl;
throw;
} catch (std::runtime_error &re) {
g_log.error() << "Failed to calculate Vanadium corrections. "
"There was an error while executing one of the "
"algorithms used to perform Vanadium corrections. "
"There was no obvious error in the input properties "
"but the algorithm failed. Please check the log "
"messages for details." +
Federico Montesino Pouzols
committed
std::string(re.what()) << std::endl;
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
throw;
}
} else {
g_log.notice()
<< "Found precalculated Vanadium correction features for Vanadium run "
<< vanNo << ". Re-using these files: " << preIntegFilename << ", and "
<< preCurvesFilename << std::endl;
try {
loadVanadiumPrecalcWorkspaces(preIntegFilename, preCurvesFilename,
vanIntegWS, vanCurvesWS);
} catch (std::invalid_argument &ia) {
g_log.error() << "Error while loading precalculated Vanadium corrections",
"The files with precalculated Vanadium corection features (spectra "
"integration and per-bank curves) were found (with names '" +
preIntegFilename + "' and '" + preCurvesFilename +
"', respectively, but there was a problem with the inputs to the "
"load algorithms to load them: " +
std::string(ia.what());
throw;
} catch (std::runtime_error &re) {
g_log.error() << "Error while loading precalculated Vanadium corrections",
"The files with precalculated Vanadium corection features (spectra "
"integration and per-bank curves) were found (with names '" +
preIntegFilename + "' and '" + preCurvesFilename +
"', respectively, but there was a problem while loading them. "
"Please check the log messages for details. You might want to "
"delete those files or force recalculations (in settings). Error "
"details: " +
std::string(re.what());
throw;
}
}
}
/**
* builds the expected names of the precalculated Vanadium correction
* files and tells if both files are found, similar to:
* ENGINX_precalculated_vanadium_run000236516_integration.nxs
* ENGINX_precalculated_vanadium_run00236516_bank_curves.nxs
* @param vanNo Vanadium run number
* @param inputDirCalib calibration directory in settings
* @param preIntegFilename if not found on disk, the string is set as empty
* @param preCurvesFilename if not found on disk, the string is set as empty
* @param found true if both files are found and (re-)usable
void EnggDiffractionPresenter::findPrecalcVanadiumCorrFilenames(
const std::string &vanNo, const std::string &inputDirCalib,
std::string &preIntegFilename, std::string &preCurvesFilename,
bool &found) {
found = false;
const std::string runNo = std::string(2, '0').append(vanNo);
preIntegFilename =
g_enginxStr + "_precalculated_vanadium_run" + runNo + "_integration.nxs";
preCurvesFilename =
g_enginxStr + "_precalculated_vanadium_run" + runNo + "_bank_curves.nxs";
Poco::Path pathInteg(inputDirCalib);
pathInteg.append(preIntegFilename);
Poco::Path pathCurves(inputDirCalib);
pathCurves.append(preCurvesFilename);
if (Poco::File(pathInteg).exists() && Poco::File(pathCurves).exists()) {
preIntegFilename = pathInteg.toString();
preCurvesFilename = pathCurves.toString();
found = true;
}
* Load precalculated results from Vanadium corrections previously
* calculated.
Federico Montesino Pouzols
committed
* @param preIntegFilename filename (can be full path) where the
* vanadium spectra integration table should be loaded from
*
* @param preCurvesFilename filename (can be full path) where the
* vanadium per-bank curves should be loaded from
*
* @param vanIntegWS output (matrix) workspace loaded from the
* precalculated Vanadium correction file, with the integration
* resutls
*
* @param vanCurvesWS output (matrix) workspace loaded from the
* precalculated Vanadium correction file, with the per-bank curves
*/
void EnggDiffractionPresenter::loadVanadiumPrecalcWorkspaces(
Federico Montesino Pouzols
committed
const std::string &preIntegFilename, const std::string &preCurvesFilename,
ITableWorkspace_sptr &vanIntegWS, MatrixWorkspace_sptr &vanCurvesWS) {
AnalysisDataServiceImpl &ADS = Mantid::API::AnalysisDataService::Instance();
auto alg = Algorithm::fromString("LoadNexus");
alg->initialize();
Federico Montesino Pouzols
committed
alg->setPropertyValue("Filename", preIntegFilename);
std::string integWSName = g_vanIntegrationWSName;
Federico Montesino Pouzols
committed
alg->setPropertyValue("OutputWorkspace", integWSName);
Federico Montesino Pouzols
committed
// alg->getProperty("OutputWorkspace");
vanIntegWS = ADS.retrieveWS<ITableWorkspace>(integWSName);
auto algCurves = Algorithm::fromString("LoadNexus");
algCurves->initialize();
Federico Montesino Pouzols
committed
algCurves->setPropertyValue("Filename", preCurvesFilename);
std::string curvesWSName = "engggui_vanadium_curves_ws";
algCurves->setPropertyValue("OutputWorkspace", curvesWSName);
algCurves->execute();
Federico Montesino Pouzols
committed
// algCurves->getProperty("OutputWorkspace");
vanCurvesWS = ADS.retrieveWS<MatrixWorkspace>(curvesWSName);
/**
* Calculate vanadium corrections (in principle only for when
* pre-calculated results are not available). This is expensive.
*
* @param vanNo Vanadium run number
*
* @param vanIntegWS where to keep the Vanadium run spectra
* integration values
*
* @param vanCurvesWS workspace where to keep the per-bank vanadium
* curves
*/
void EnggDiffractionPresenter::calcVanadiumWorkspaces(
Federico Montesino Pouzols
committed
const std::string &vanNo, ITableWorkspace_sptr &vanIntegWS,
MatrixWorkspace_sptr &vanCurvesWS) {
auto load = Algorithm::fromString("Load");
Federico Montesino Pouzols
committed
load->setPropertyValue("Filename",
vanNo); // TODO more specific build Vanadium filename
std::string vanWSName = "engggui_vanadium_ws";
load->setPropertyValue("OutputWorkspace", vanWSName);
load->execute();
Federico Montesino Pouzols
committed
AnalysisDataServiceImpl &ADS = Mantid::API::AnalysisDataService::Instance();
MatrixWorkspace_sptr vanWS = ADS.retrieveWS<MatrixWorkspace>(vanWSName);
// TODO?: maybe use setChild() and then load->getProperty("OutputWorkspace");
auto alg = Algorithm::fromString("EnggVanadiumCorrections");
alg->initialize();
alg->setProperty("VanadiumWorkspace", vanWS);
std::string integName = g_vanIntegrationWSName;
alg->setPropertyValue("OutIntegrationWorkspace", integName);
Federico Montesino Pouzols
committed
std::string curvesName = "engggui_van_curves_ws";
alg->setPropertyValue("OutCurvesWorkspace", curvesName);
alg->execute();
Federico Montesino Pouzols
committed
ADS.remove(vanWSName);
vanIntegWS = ADS.retrieveWS<ITableWorkspace>(integName);
vanCurvesWS = ADS.retrieveWS<MatrixWorkspace>(curvesName);
} // namespace CustomInterfaces
} // namespace MantidQt