Mantid
Loading...
Searching...
No Matches
BoxControllerSettingsAlgorithm.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include <algorithm>
8#include <iterator>
9#include <utility>
10
16#include "MantidKernel/System.h"
17
18using namespace Mantid::Kernel;
19
20namespace Mantid::API {
21
22//----------------------------------------------------------------------------------------------
29void BoxControllerSettingsAlgorithm::initBoxControllerProps(const std::string &SplitInto, int SplitThreshold,
30 int MaxRecursionDepth) {
31 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
32 mustBePositive->setLower(0);
33 auto mustBeMoreThen1 = std::make_shared<BoundedValidator<int>>();
34 mustBeMoreThen1->setLower(1);
35
36 // Split up comma-separated properties
39 std::vector<int> valueVec;
40 valueVec.reserve(values.count());
41 std::transform(values.cbegin(), values.cend(), std::back_inserter(valueVec), boost::lexical_cast<int, std::string>);
42
43 declareProperty(std::make_unique<ArrayProperty<int>>("SplitInto", std::move(valueVec)),
44 "A comma separated list of into how many sub-grid elements each "
45 "dimension should split; "
46 "or just one to split into the same number for all dimensions. Default " +
47 SplitInto + ".");
48
49 declareProperty(std::make_unique<PropertyWithValue<int>>("SplitThreshold", SplitThreshold, mustBePositive),
50 "How many events in a box before it should be split. Default " + Strings::toString(SplitThreshold) +
51 ".");
52
53 declareProperty(std::make_unique<PropertyWithValue<int>>("MaxRecursionDepth", MaxRecursionDepth, mustBeMoreThen1),
54 "How many levels of box splitting recursion are allowed. "
55 "The smallest box will have each side length :math:`l = "
56 "(extents) / (SplitInto^{MaxRecursionDepth}).` "
57 "Default " +
58 Strings::toString(MaxRecursionDepth) + ".");
59
60 std::string grp = getBoxSettingsGroupName();
61 setPropertyGroup("SplitInto", grp);
62 setPropertyGroup("SplitThreshold", grp);
63 setPropertyGroup("MaxRecursionDepth", grp);
64}
65
74 const Mantid::Geometry::Instrument_const_sptr &instrument, const size_t ndims) {
75 const std::string splitThresholdName = "SplitThreshold";
76 const std::string splitIntoName = "SplitInto";
77 const std::string maxRecursionDepthName = "MaxRecursionDepth";
78 Property *p = getProperty(splitThresholdName);
79 if (p->isDefault()) {
80 std::vector<double> instrumentSplitThresholds = instrument->getNumberParameter(splitThresholdName, true);
81 if (!instrumentSplitThresholds.empty()) {
82 setProperty(splitThresholdName, static_cast<int>(instrumentSplitThresholds.front()));
83 }
84 }
85 p = getProperty(splitIntoName);
86 if (p->isDefault()) {
87 std::vector<double> instrumentSplitInto = instrument->getNumberParameter(splitIntoName, true);
88 if (!instrumentSplitInto.empty()) {
89 const auto splitInto = static_cast<int>(instrumentSplitInto.front());
90 std::vector<int> newSplitInto(ndims, splitInto);
91 setProperty(splitIntoName, newSplitInto);
92 }
93 }
94 p = getProperty(maxRecursionDepthName);
95 if (p->isDefault()) {
96 std::vector<double> instrumentMaxRecursionDepth = instrument->getNumberParameter(maxRecursionDepthName, true);
97 if (!instrumentMaxRecursionDepth.empty()) {
98 setProperty(maxRecursionDepthName, static_cast<int>(instrumentMaxRecursionDepth.front()));
99 }
100 }
101}
102
103//----------------------------------------------------------------------------------------------
111 const Mantid::Geometry::Instrument_const_sptr &instrument) {
112 size_t nd = bc->getNDims();
113
114 takeDefaultsFromInstrument(instrument, nd);
115
117}
118
119//----------------------------------------------------------------------------------------------
126 size_t nd = bc->getNDims();
127
128 int val;
129 val = this->getProperty("SplitThreshold");
130 bc->setSplitThreshold(val);
131 val = this->getProperty("MaxRecursionDepth");
132 bc->setMaxDepth(val);
133
134 // Build MDGridBox
135 std::vector<int> splits = getProperty("SplitInto");
136 if (splits.size() == 1) {
137 bc->setSplitInto(splits[0]);
138 } else if (splits.size() == nd) {
139 for (size_t d = 0; d < nd; ++d)
140 bc->setSplitInto(d, splits[d]);
141 } else
142 throw std::invalid_argument("SplitInto parameter has " + Strings::toString(splits.size()) +
143 " arguments. It should have either 1, or the "
144 "same as the number of dimensions.");
145 bc->resetNumBoxes();
146}
147
148} // namespace Mantid::API
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
void takeDefaultsFromInstrument(const Mantid::Geometry::Instrument_const_sptr &instrument, const size_t ndims)
Take the defaults for the box splitting from the instrument parameters.
void setBoxController(const Mantid::API::BoxController_sptr &bc, const Mantid::Geometry::Instrument_const_sptr &instrument)
Set the settings in the given box controller.
void initBoxControllerProps(const std::string &SplitInto="5", int SplitThreshold=1000, int MaxRecursionDepth=5)
Initialise the properties.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
The concrete, templated class for properties.
Base class for properties.
Definition: Property.h:94
virtual bool isDefault() const =0
Overriden function that returns if property has the same value that it was initialised with,...
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
ConstIterator cend() const
Const iterator referring to the past-the-end element in the container.
ConstIterator cbegin() const
Const iterator referring to first element in the container.
std::size_t count() const
Get the total number of tokens.
std::shared_ptr< BoxController > BoxController_sptr
Shared ptr to BoxController.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
std::string toString(const T &value)
Convert a number to a string.
Definition: Strings.cpp:703