36 std::string grpName(
"Specify the Instrument");
40 "Optional: An input workspace with the instrument we want to use.");
43 "Optional: Name of the instrument on which to base the GroupingWorkspace.");
45 const std::vector<std::string> validFilenameExtensions{
".xml",
".hdf5",
".nxs",
".nxs.h5"};
48 "Optional: Path to a file (full or relative) defining the instrument on which to base the GroupingWorkspace. "
49 "The file could be an IDF or a NeXus Geometry file. Note, InstrumentFilename or InstrumentName must be "
50 "specified, but not both.");
52 alg->setPropertyGroup(
"InputWorkspace", grpName);
53 alg->setPropertyGroup(
"InstrumentName", grpName);
54 alg->setPropertyGroup(
"InstrumentFilename", grpName);
63 if (!InstrumentName.empty())
66 std::string InstrumentFilename = alg->
getPropertyValue(
"InstrumentFilename");
67 return !InstrumentFilename.empty();
77 std::string InstrumentFilename = alg->
getPropertyValue(
"InstrumentFilename");
83 if (!InstrumentName.empty())
85 if (!InstrumentFilename.empty())
89 throw std::invalid_argument(
"You must specify exactly ONE way to get an "
90 "instrument (workspace, instrument name, or "
91 "IDF file). You specified more than one.");
93 throw std::invalid_argument(
"You must specify exactly ONE way to get an "
94 "instrument (workspace, instrument name, or "
95 "IDF file). You specified none.");
100 inst = inWS->getInstrument();
103 childAlg->setPropertyValue(
"Filename", InstrumentFilename);
104 childAlg->setPropertyValue(
"InstrumentName", InstrumentName);
105 childAlg->executeAsChildAlg();
107 inst = tempWS->getInstrument();
120 "Path to the old-style .cal grouping/calibration file (multi-column "
121 "ASCII). You must also specify the instrument.");
124 "Set to true to create a GroupingWorkspace with called "
125 "WorkspaceName_group.");
128 "Set to true to create a OffsetsWorkspace with called "
129 "WorkspaceName_offsets.");
132 "Set to true to create a MaskWorkspace with called WorkspaceName_mask.");
135 "The base of the output workspace names. Names will have '_group', "
136 "'_cal', '_offsets', '_mask' appended to them.");
145 bool MakeGroupingWorkspace =
getProperty(
"MakeGroupingWorkspace");
146 bool MakeOffsetsWorkspace =
getProperty(
"MakeOffsetsWorkspace");
147 bool MakeMaskWorkspace =
getProperty(
"MakeMaskWorkspace");
149 if (WorkspaceName.empty())
150 throw std::invalid_argument(
"Must specify WorkspaceName.");
159 std::string title = std::filesystem::path(CalFilename).filename().string();
162 if (MakeGroupingWorkspace) {
164 groupWS->setTitle(title);
167 "Set the output GroupingWorkspace, if any.");
168 groupWS->mutableRun().addProperty(
"Filename", CalFilename);
172 if (MakeOffsetsWorkspace) {
174 offsetsWS->setTitle(title);
177 "Set the output OffsetsWorkspace, if any.");
178 offsetsWS->mutableRun().addProperty(
"Filename", CalFilename);
182 if (MakeMaskWorkspace) {
184 maskWS->setTitle(title);
187 "Set the output MaskWorkspace, if any.");
188 maskWS->mutableRun().addProperty(
"Filename", CalFilename);
194 if (MakeOffsetsWorkspace) {
196 alg->setProperty(
"OffsetsWorkspace", offsetsWS);
197 alg->executeAsChildAlg();
199 calWS->setTitle(title);
202 "Set the output Diffraction Calibration workspace, if any.");
220 auto doGroup = bool(groupWS);
221 auto doOffsets = bool(offsetsWS);
222 auto doMask = bool(maskWS);
224 bool hasUnmasked(
false);
225 bool hasGrouped(
false);
227 if (!doOffsets && !doGroup && !doMask)
228 throw std::invalid_argument(
"You must give at least one of the grouping, "
229 "offsets or masking workspaces.");
231 std::ifstream grFile(calFileName.c_str());
233 throw std::runtime_error(
"Unable to open calibration file " + calFileName);
236 size_t numErrors = 0;
240 detID_to_wi = maskWS->getDetectorIDToWorkspaceIndexMap();
245 int n, udet, select,
group;
246 double n_d, udet_d, offset, select_d, group_d;
250 maskSpectrumInfo = &maskWS->mutableSpectrumInfo();
252 while (getline(grFile, str)) {
253 if (str.empty() || str[0] ==
'#')
255 std::istringstream istr(str);
258 istr >> n_d >> udet_d >> offset >> select_d >> group_d;
259 n =
static_cast<int>(n_d);
260 udet =
static_cast<int>(udet_d);
261 select =
static_cast<int>(select_d);
262 group =
static_cast<int>(group_d);
267 std::stringstream msg;
268 msg <<
"Encountered offset = " << offset <<
" at index " <<
n <<
" for udet = " << udet
269 <<
". Offsets must be greater than -1.";
270 throw std::runtime_error(msg.str());
274 offsetsWS->setValue(udet, offset);
275 }
catch (std::invalid_argument &) {
277 if (!
idIsMonitor(offsetsWS->getInstrument(), udet))
284 groupWS->setValue(udet,
double(
group));
285 if ((!hasGrouped) && (
group > 0))
287 }
catch (std::invalid_argument &) {
295 detid2index_map::const_iterator it = detID_to_wi.find(udet);
296 if (it != detID_to_wi.end()) {
297 size_t wi = it->second;
301 maskWS->getSpectrum(wi).clearData();
302 maskSpectrumInfo->setMasked(wi,
true);
303 maskWS->mutableY(wi)[0] = 1.0;
306 maskWS->mutableY(wi)[0] = 0.0;
320 Logger(
"LoadCalFile").
warning() << numErrors <<
" errors (invalid Detector ID's) found when reading .cal file '"
321 << calFileName <<
"'.\n";
322 if (doGroup && (!hasGrouped))
323 Logger(
"LoadCalFile").
warning() <<
"'" << calFileName <<
"' has no spectra grouped\n";
324 if (doMask && (!hasUnmasked))
325 Logger(
"LoadCalFile").
warning() <<
"'" << calFileName <<
"' masks all spectra\n";
336 auto monitorList = inst->getMonitors();
337 auto it = std::find(monitorList.begin(), monitorList.end(), detID);
338 return (it != monitorList.end());
#define DECLARE_ALGORITHM(classname)
Base class from which all concrete algorithm classes should be derived.
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.
@ 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
API::SpectrumInfo is an intermediate step towards a SpectrumInfo that is part of Instrument-2....
A property class for workspaces.
Algorithm to load a 5-column ascii .cal file into up to 3 workspaces: a GroupingWorkspace,...
static bool idIsMonitor(const Mantid::Geometry::Instrument_const_sptr &inst, int detID)
Checks if a detector ID is for a monitor on a given instrument.
void exec() override
Run the algorithm.
static void getInstrument3WaysInit(Mantid::API::Algorithm *alg)
For use by getInstrument3Ways, initializes the properties.
void init() override
Initialise the properties.
static bool instrumentIsSpecified(API::Algorithm const *alg)
static Geometry::Instrument_const_sptr getInstrument3Ways(API::Algorithm *alg)
Get a pointer to an instrument in one of 3 ways: InputWorkspace, InstrumentName, InstrumentFilename.
static void readCalFile(const std::string &calFileName, const Mantid::DataObjects::GroupingWorkspace_sptr &groupWS, const Mantid::DataObjects::OffsetsWorkspace_sptr &offsetsWS, const Mantid::DataObjects::MaskWorkspace_sptr &maskWS)
Reads the calibration file.
A GroupingWorkspace is a subclass of Workspace2D where each spectrum has a single number entry,...
An OffsetsWorkspace is a specialized Workspace2D where the Y value at each pixel is the offset to be ...
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
The Logger class is in charge of the publishing messages from the framework through various channels.
void warning(const std::string &msg)
Logs at warning level.
The concrete, templated class for properties.
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
std::shared_ptr< GroupingWorkspace > GroupingWorkspace_sptr
shared pointer to the GroupingWorkspace class
std::shared_ptr< MaskWorkspace > MaskWorkspace_sptr
shared pointer to the MaskWorkspace class
std::shared_ptr< OffsetsWorkspace > OffsetsWorkspace_sptr
shared pointer to the OffsetsWorkspace class
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
std::unordered_map< detid_t, size_t > detid2index_map
Map with key = detector ID, value = workspace index.
@ Input
An input workspace.
@ Output
An output workspace.