29 return "Diffraction\\DataHandling;DataHandling\\Instrument";
42 "Workspace to edit the detector information");
49 "Spectrum Numbers (note that it is not detector ID or workspace "
50 "indices). The list must be either empty or have a size "
51 "equal to input workspace's histogram number. ");
53 auto required = std::make_shared<MandatoryValidator<std::vector<double>>>();
57 "Secondary flight (L2) paths for each detector. Number of L2 "
58 "given must be same as number of histogram.");
62 "Polar angles (two thetas) for detectors. Number of 2theta "
63 "given must be same as number of histogram.");
67 "Azimuthal angles (out-of-plane) for detectors. "
68 "Number of azimuthal angles given must be same as number of histogram.");
72 "User specified detector IDs of the spectra. "
73 "Number of specified detector IDs must be either zero or "
74 "number of histogram");
78 "Name of the newly built instrument. If left empty, "
79 "the original instrument will be used. ");
82template <
typename NumT> std::string
checkValues(
const std::vector<NumT> &thingy,
const size_t numHist) {
83 if ((!thingy.empty()) && thingy.size() != numHist) {
85 msg <<
"Must equal number of spectra or be empty (" << numHist <<
" != " << thingy.size() <<
")";
93 std::map<std::string, std::string> result;
97 bool hasWorkspacePtr(
false);
103 hasWorkspacePtr =
true;
104 numHist =
workspace->getNumberHistograms();
110 const std::vector<int32_t> specids = this->
getProperty(
"SpectrumIDs");
111 if (!hasWorkspacePtr) {
113 numHist = specids.size();
121 result[
"SpectrumIDs"] =
error;
126 result[
"L2"] =
error;
128 const std::vector<double> tth = this->
getProperty(
"Polar");
131 result[
"Polar"] =
error;
133 const std::vector<double> phi = this->
getProperty(
"Azimuthal");
136 result[
"Azimuthal"] =
error;
138 const vector<int> detids =
getProperty(
"DetectorIDs");
141 result[
"DetectorIDs"] =
error;
157 double l1 = this->
getProperty(
"PrimaryFlightPath");
160 if (!originstrument) {
161 std::string errmsg(
"It is not supported that L1 is not given, ",
162 "while there is no instrument associated to input workspace.");
164 throw std::runtime_error(errmsg);
168 l1 = source->getDistance(*sample);
174 std::vector<int32_t> specids = this->
getProperty(
"SpectrumIDs");
177 size_t numHist =
workspace->getNumberHistograms();
178 for (
size_t i = 0; i < numHist; ++i) {
179 specids.emplace_back(
workspace->getSpectrum(i).getSpectrumNo());
185 const vector<int> vec_detids =
getProperty(
"DetectorIDs");
186 const bool renameDetID(!vec_detids.empty());
189 const std::vector<double> l2s = this->
getProperty(
"L2");
190 const std::vector<double> tths = this->
getProperty(
"Polar");
191 std::vector<double> phis = this->
getProperty(
"Azimuthal");
195 throw std::runtime_error(
"User must specify L2 for all spectra. ");
198 throw std::runtime_error(
"User must specify 2theta for all spectra.");
203 phis.assign(l2s.size(), 0.);
207 for (
size_t ib = 0; ib < l2s.size(); ib++) {
208 g_log.
information() <<
"Detector " << specids[ib] <<
" L2 = " << l2s[ib] <<
" 2Theta = " << tths[ib] <<
'\n';
209 if (specids[ib] < 0) {
211 stringstream errmsgss;
212 errmsgss <<
"Detector ID = " << specids[ib] <<
" cannot be less than 0.";
213 throw std::invalid_argument(errmsgss.str());
215 if (l2s[ib] <= 0.0) {
216 throw std::invalid_argument(
"L2 cannot be less or equal to 0");
221 const auto spec2indexmap =
workspace->getSpectrumToWorkspaceIndexMap();
224 size_t nspec =
workspace->getNumberHistograms();
228 std::vector<double> storL2s(nspec, 0.);
229 std::vector<double> stor2Thetas(nspec, 0.);
230 std::vector<double> storPhis(nspec, 0.);
231 vector<int> storDetIDs(nspec, 0);
234 for (
size_t i = 0; i < specids.size(); i++) {
236 auto it = spec2indexmap.find(specids[i]);
237 if (it == spec2indexmap.end()) {
239 errss <<
"Spectrum Number " << specids[i] <<
" is not found. "
240 <<
"Instrument won't be edited for this spectrum. \n";
242 throw std::runtime_error(errss.str());
246 size_t workspaceindex = it->second;
248 storL2s[workspaceindex] = l2s[i];
249 stor2Thetas[workspaceindex] = tths[i];
250 storPhis[workspaceindex] = phis[i];
252 storDetIDs[workspaceindex] = vec_detids[i];
254 g_log.
debug() <<
"workspace index = " << workspaceindex <<
" is for Spectrum " << specids[i] <<
'\n';
262 if (!originstrument) {
263 std::string errmsg(
"It is not supported that InstrumentName is not given, ",
264 "while there is no instrument associated to input workspace.");
266 throw std::runtime_error(errmsg);
268 name = originstrument->getName();
272 auto instrument = std::make_shared<Geometry::Instrument>(
name);
273 if (!
bool(instrument)) {
275 errss <<
"Trying to use a Parametrized Instrument as an Instrument.";
277 throw std::runtime_error(errss.str());
282 instrument->add(samplepos);
283 instrument->markAsSamplePos(samplepos);
284 samplepos->
setPos(0.0, 0.0, 0.0);
287 instrument->add(source);
288 instrument->markAsSource(source);
289 source->
setPos(0.0, 0.0, -1.0 * l1);
292 for (
size_t i = 0; i <
workspace->getNumberHistograms(); i++) {
297 newdetid = storDetIDs[i];
303 double l2 = storL2s[i];
304 double tth = stor2Thetas[i];
305 double phi = storPhis[i];
312 auto &spectrum =
workspace->getSpectrum(i);
314 g_log.
debug() <<
"Orignal spectrum " << spectrum.getSpectrumNo() <<
"has " << spectrum.getDetectorIDs().size()
317 spectrum.clearDetectorIDs();
318 spectrum.addDetectorID(newdetid);
319 instrument->add(detector);
320 instrument->markAsDetector(detector);
#define DECLARE_ALGORITHM(classname)
IPeaksWorkspace_sptr workspace
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
static bool isEmpty(const NumT toCheck)
checks that the value was not set by users, uses the value in empty double/int.
A property class for workspaces.
EditInstrumentGeometry : TODO: DESCRIPTION.
int version() const override
Algorithm's version for identification overriding a virtual method.
std::map< std::string, std::string > validateInputs() override
Validate the inputs that must be parallel.
void exec() override
Run the algorithm.
void init() override
Initialise the properties.
const std::string name() const override
function to return a name of the algorithm, must be overridden in all algorithms
const std::string category() const override
Algorithm's category for identification overriding a virtual method.
Component is a wrapper for a Component which can modify some of its parameters, e....
void setPos(double, double, double) override
Set the IComponent position, x, y, z respective to parent (if present)
This class represents a detector - i.e.
Object Component class, this class brings together the physical attributes of the component to the po...
Support for a property that holds an array of values.
void debug(const std::string &msg)
Logs at debug level.
void error(const std::string &msg)
Logs at error level.
void information(const std::string &msg)
Logs at information level.
void spherical(const double R, const double theta, const double phi) noexcept
Sets the vector position based on spherical coordinates.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::string checkValues(const std::vector< NumT > &thingy, const size_t numHist)
std::shared_ptr< const IComponent > IComponent_const_sptr
Typdef of a shared pointer to a const IComponent.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
int32_t detid_t
Typedef for a detector ID.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
@ InOut
Both an input & output workspace.