46const std::string CAL_FILE(
"Filename");
47const std::string GROUP_FILE(
"GroupFilename");
48const std::string MAKE_CAL(
"MakeCalWorkspace");
49const std::string MAKE_GRP(
"MakeGroupingWorkspace");
50const std::string MAKE_MSK(
"MakeMaskWorkspace");
67const std::string
LoadDiffCal::summary()
const {
return "Loads a calibration file for powder diffraction"; }
75 const std::vector<std::string> exts{
".h5",
".hd5",
".hdf",
".cal"};
77 "Path to the .h5 file.");
79 std::vector<std::string>{
".xml",
".cal"}),
80 "Overrides grouping from CalFileName");
83 "Set to true to create a GroupingWorkspace with called "
84 "WorkspaceName_group.");
87 "Set to true to create a CalibrationWorkspace with called "
88 "WorkspaceName_cal.");
91 "Set to true to create a MaskWorkspace with called WorkspaceName_mask.");
94 "The base of the output workspace names. Names will have '_group', "
95 "'_cal', '_mask' appended to them.");
97 std::string grpName(
"Calibration Validation");
101 "Set DIFA and TZERO to zero if there is an error and the "
110bool endswith(
const std::string &str,
const std::string &ending) {
111 if (ending.size() > str.size()) {
115 return std::equal(str.begin() + str.size() - ending.size(), str.end(), ending.begin());
118void setGroupWSProperty(API::Algorithm *alg,
const std::string &prefix,
const GroupingWorkspace_sptr &wksp) {
119 alg->declareProperty(std::make_unique<WorkspaceProperty<DataObjects::GroupingWorkspace>>(
121 "Set the the output GroupingWorkspace, if any.");
122 alg->setProperty(
"OutputGroupingWorkspace", wksp);
125void setMaskWSProperty(API::Algorithm *alg,
const std::string &prefix,
const MaskWorkspace_sptr &wksp) {
126 alg->declareProperty(std::make_unique<WorkspaceProperty<DataObjects::MaskWorkspace>>(
128 "Set the the output MaskWorkspace, if any.");
129 alg->setProperty(
"OutputMaskWorkspace", wksp);
132void setCalWSProperty(API::Algorithm *alg,
const std::string &prefix,
const ITableWorkspace_sptr &wksp) {
133 alg->declareProperty(
134 std::make_unique<WorkspaceProperty<ITableWorkspace>>(
"OutputCalWorkspace", prefix +
"_cal",
Direction::Output),
135 "Set the output Diffraction Calibration workspace, if any.");
136 alg->setProperty(
"OutputCalWorkspace", wksp);
143 bool makeMask =
getProperty(PropertyNames::MAKE_MSK);
144 bool makeGrouping =
getProperty(PropertyNames::MAKE_GRP);
145 if ((!makeMask) & (!makeGrouping))
154 std::string idf =
H5Util::readString(file,
"/calibration/instrument/instrument_source");
155 std::string instrumentName =
H5Util::readString(file,
"/calibration/instrument/name");
158 <<
"NAME: " << instrumentName <<
"\n";
164 childAlg->setPropertyValue(
"InstrumentName", instrumentName);
166 childAlg->setPropertyValue(
"Filename", idf);
169 childAlg->executeAsChildAlg();
178 bool makeWS =
getProperty(PropertyNames::MAKE_GRP);
180 g_log.
information(
"Not loading GroupingWorkspace from the calibration file");
185 if (!
isDefault(PropertyNames::GROUP_FILE)) {
190 size_t numDet = detids.size();
195 wksp->mutableRun().addProperty(
"Filename",
m_filename);
197 for (
size_t i = 0; i < numDet; ++i) {
198 auto detid =
static_cast<detid_t>(detids[i]);
199 wksp->setValue(detid, groups[i]);
207 bool makeWS =
getProperty(PropertyNames::MAKE_MSK);
213 size_t numDet = detids.size();
218 wksp->mutableRun().addProperty(
"Filename",
m_filename);
220 for (
size_t i = 0; i < numDet; ++i) {
221 bool shouldUse = (use[i] > 0);
222 auto detid =
static_cast<detid_t>(detids[i]);
224 wksp->setMasked(detid, !shouldUse);
226 wksp->setValue(detid, (shouldUse ? 0. : 1.));
234 const std::vector<double> &
difa,
const std::vector<double> &
tzero,
235 const std::vector<int32_t> &dasids,
const std::vector<double> &offsets,
236 const std::vector<int32_t> &use) {
237 bool makeWS =
getProperty(PropertyNames::MAKE_CAL);
243 size_t numDet = detids.size();
246 bool haveDasids = !dasids.empty();
247 bool haveOffsets = !offsets.empty();
248 bool fixIssues =
getProperty(
"FixConversionIssues");
252 bool useTofMax = !
isEmpty(tofMax);
256 wksp->addColumn(
"int",
"detid");
257 wksp->addColumn(
"double",
"difc");
258 wksp->addColumn(
"double",
"difa");
259 wksp->addColumn(
"double",
"tzero");
262 wksp->addColumn(
"int",
"dasid");
264 wksp->addColumn(
"double",
"offset");
267 wksp->addColumn(
"double",
"tofmin");
269 wksp->addColumn(
"double",
"tofmax");
272 for (
size_t i = 0; i < numDet; ++i) {
281 newrow << offsets[i];
286 std::stringstream msg;
287 if (tofMinRow != tofMin) {
288 msg <<
"TofMin shifted from " << tofMin <<
" to " << tofMinRow <<
" ";
295 if (tofMaxRow != tofMax) {
296 msg <<
"TofMax shifted from " << tofMax <<
" to " << tofMaxRow;
299 if (!msg.str().empty()) {
301 std::stringstream longMsg;
302 longMsg <<
"[detid=" << detids[i];
304 longMsg <<
", dasid=" << dasids[i];
305 longMsg <<
"] " << msg.str();
308 if (fixIssues && (!use[i])) {
309 longMsg <<
" pixel is masked, ";
310 longMsg <<
" changing difa (" << wksp->
cell<
double>(i, 2) <<
" to 0.)";
311 wksp->cell<
double>(i, 2) = 0.;
313 longMsg <<
" and tzero (" << wksp->cell<
double>(i, 3) <<
" to 0.)";
314 wksp->cell<
double>(i, 3) = 0.;
322 wksp->cell<
double>(i,
index) = tofMin;
324 wksp->cell<
double>(i,
index + 1) = tofMax;
333 this->
g_log.
warning() << badCount <<
" rows have reduced time-of-flight range\n";
342 bool makeWS =
getProperty(PropertyNames::MAKE_GRP);
346 if (
isDefault(PropertyNames::GROUP_FILE))
350 g_log.
information() <<
"Override grouping with information from \"" << filename <<
"\"\n";
352 throw std::runtime_error(
"Do not have an instrument defined before loading separate grouping");
356 if (filename.find(
".cal") != std::string::npos) {
358 alg->setProperty(
"InputWorkspace", wksp);
359 alg->setPropertyValue(PropertyNames::CAL_FILE, filename);
360 alg->setProperty<
bool>(PropertyNames::MAKE_CAL,
false);
361 alg->setProperty<
bool>(PropertyNames::MAKE_GRP,
true);
362 alg->setProperty<
bool>(PropertyNames::MAKE_MSK,
false);
364 alg->executeAsChildAlg();
367 wksp = alg->getProperty(
"OutputGroupingWorkspace");
370 alg->setProperty(
"InputWorkspace", wksp);
371 alg->setProperty(
"InputFile", filename);
372 alg->executeAsChildAlg();
375 wksp = alg->getProperty(
"OutputWorkspace");
381 bool makeCalWS =
getProperty(PropertyNames::MAKE_CAL);
382 bool makeMaskWS =
getProperty(PropertyNames::MAKE_MSK);
383 bool makeGroupWS =
getProperty(PropertyNames::MAKE_GRP);
386 bool haveGroupingFile = !
isDefault(PropertyNames::GROUP_FILE);
389 alg->setPropertyValue(
"CalFilename",
m_filename);
390 alg->setProperty(
"InputWorkspace", inputWs);
391 alg->setPropertyValue(
"InstrumentName",
getPropertyValue(
"InstrumentName"));
392 alg->setPropertyValue(
"InstrumentFilename",
getPropertyValue(
"InstrumentFilename"));
393 alg->setProperty<
bool>(
"MakeOffsetsWorkspace", makeCalWS);
394 alg->setProperty<
bool>(
"MakeGroupingWorkspace", makeGroupWS);
395 alg->setProperty<
bool>(
"MakeMaskWorkspace", makeMaskWS);
397 alg->executeAsChildAlg();
406 setMaskWSProperty(
this,
m_workspaceName, std::dynamic_pointer_cast<DataObjects::MaskWorkspace>(wksp));
411 if (haveGroupingFile) {
435 H5::Exception::dontPrint();
439 }
catch (FileIException &) {
445 Group calibrationGroup;
447 calibrationGroup = file.openGroup(
"calibration");
448 }
catch (FileIException &e) {
449#if H5_VERSION_GE(1, 8, 13)
451 H5::Exception::printErrorStack();
453 e.printError(stderr);
480 if (detids.empty()) {
481 throw std::runtime_error(
"File was missing required field \"/calibraion/detid\"");
484 throw std::runtime_error(
"File was missing required field \"/calibraion/difc\"");
489 groups.assign(detids.size(), 1);
491 use.assign(detids.size(), 1);
493 difa.assign(detids.size(), 0.);
495 tzero.assign(detids.size(), 0.);
503Parallel::ExecutionMode
509 static_cast<void>(storageModes);
510 return Parallel::ExecutionMode::Identical;
#define DECLARE_ALGORITHM(classname)
std::map< DeltaEMode::Type, std::string > index
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
bool isDefault(const std::string &name) const
static bool isEmpty(const NumT toCheck)
checks that the value was not set by users, uses the value in empty double/int.
A specialized class for dealing with file properties.
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
@ Load
allowed here which will be passed to the algorithm
ITableWorkspace is an implementation of Workspace in which the data are organised in columns of same ...
Helper class for reporting progress from algorithms.
TableRow represents a row in a TableWorkspace.
T & cell(size_t col)
Templated method to access the element col in the row.
A property class for workspaces.
static bool instrumentIsSpecified(API::Algorithm *alg)
static void getInstrument3WaysInit(Mantid::API::Algorithm *alg)
For use by getInstrument3Ways, initializes the properties.
static Geometry::Instrument_const_sptr getInstrument3Ways(API::Algorithm *alg)
Get a pointer to an instrument in one of 3 ways: InputWorkspace, InstrumentName, InstrumentFilename.
LoadDiffCal : TODO: DESCRIPTION.
int version() const override
Algorithm's version for identification.
Parallel::ExecutionMode getParallelExecutionMode(const std::map< std::string, Parallel::StorageMode > &storageModes) const override
Get correct execution mode based on input storage modes for an MPI run.
Geometry::Instrument_const_sptr m_instrument
std::string m_workspaceName
void init() override
Initialize the algorithm's properties.
void loadGroupingFromAlternateFile()
void getInstrument(H5::H5File &file)
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
void exec() override
Execute the algorithm.
void makeGroupingWorkspace(const std::vector< int32_t > &detids, const std::vector< int32_t > &groups)
const std::string category() const override
Algorithm's category for identification.
void makeMaskWorkspace(const std::vector< int32_t > &detids, const std::vector< int32_t > &use)
void makeCalWorkspace(const std::vector< int32_t > &detids, const std::vector< double > &difc, const std::vector< double > &difa, const std::vector< double > &tzero, const std::vector< int32_t > &dasids, const std::vector< double > &offsets, const std::vector< int32_t > &use)
Concrete workspace implementation.
The class Group represents a set of symmetry operations (or symmetry group).
Records the filename and the description of failure.
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
void debug(const std::string &msg)
Logs at debug level.
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
OptionalBool : Tri-state bool.
The concrete, templated class for properties.
double calcTofMax(const double difc, const double difa, const double tzero, const double tofmax=0.)
double calcTofMin(const double difc, const double difa, const double tzero, const double tofmin=0.)
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
template MANTID_DATAHANDLING_DLL std::vector< double > readArray1DCoerce< double >(DataSet &dataset)
MANTID_DATAHANDLING_DLL std::string readString(H5::H5File &file, const std::string &path)
template MANTID_DATAHANDLING_DLL std::vector< int32_t > readArray1DCoerce< int32_t >(DataSet &dataset)
static bool endswith(const std::string &s, const std::string &subs)
std::shared_ptr< GroupingWorkspace > GroupingWorkspace_sptr
shared pointer to the GroupingWorkspace class
std::shared_ptr< MaskWorkspace > MaskWorkspace_sptr
shared pointer to the MaskWorkspace class
int32_t detid_t
Typedef for a detector ID.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Describes the direction (within an algorithm) of a Property.
@ Input
An input workspace.
@ Output
An output workspace.