23using namespace Kernel;
24using namespace DataObjects;
33 return "GenerateGoniometerIndependentBackground";
41 return "CorrectionFunctions\\BackgroundCorrections";
46 return "Extract the background from a dataset where sample is rotated through multiple positions";
54 "Input workspaces. Must be :ref:`EventWorkspace` and have at least 2 input workspaces.");
56 const std::vector<std::string> exts{
".map",
".xml"};
59 "A file that consists of lists of spectra numbers to group. To be read by :ref:`algm-LoadDetectorsGroupingFile`");
62 "Starting percentage range of input files that will be combined to create the background");
65 "Ending percentage range of input files that will be combined to create the background");
68 "Extracted background workspace.");
72 std::map<std::string, std::string> issues;
75 const double percentMin =
getProperty(
"PercentMin");
76 const double percentMax =
getProperty(
"PercentMax");
78 if (percentMin >= percentMax) {
79 issues[
"PercentMin"] =
"PercentMin must be less than PercentMax";
80 issues[
"PercentMax"] =
"PercentMin must be less than PercentMax";
83 if (inputs.size() < 2) {
84 issues[
"InputWorkspaces"] =
"Requires at least 2 input workspaces";
91 issues[
"InputWorkspaces"] =
"Workspace \"" + inputs.front() +
"\" is not an EventWorkspace";
95 if (!firstWS->isCommonBins()) {
96 issues[
"InputWorkspaces"] =
"Workspaces require common bins.";
101 const auto blocksize = firstWS->blocksize();
102 const auto eventType = firstWS->getEventType();
103 const auto instrumentName = firstWS->getInstrument()->getName();
104 double protonChargeMin = firstWS->run().getProtonCharge();
105 double protonChargeMax = firstWS->run().getProtonCharge();
107 for (
const auto &input : inputs) {
110 issues[
"InputWorkspaces"] =
"Workspace \"" + input +
"\" is not an EventWorkspace";
114 if (ws->getNumberHistograms() != numHist) {
115 issues[
"InputWorkspaces"] =
"Number of spectra mismatch.";
119 if (!ws->isCommonBins()) {
120 issues[
"InputWorkspaces"] =
"Workspaces require common bins.";
124 if (ws->blocksize() != blocksize) {
125 issues[
"InputWorkspaces"] =
"Size mismatch.";
129 if (ws->getEventType() != eventType) {
130 issues[
"InputWorkspaces"] =
"Mismatched type of events in the EventWorkspaces.";
134 if (ws->getInstrument()->getName() != instrumentName) {
135 issues[
"InputWorkspaces"] =
"Instrument name mismatch.";
139 protonChargeMin = std::min(ws->run().getProtonCharge(), protonChargeMin);
140 protonChargeMax = std::max(ws->run().getProtonCharge(), protonChargeMax);
143 if (issues.count(
"InputWorkspaces") == 0 && protonChargeMin > 0 &&
144 (protonChargeMax - protonChargeMin) / protonChargeMin > 0.01)
145 issues[
"InputWorkspaces"] =
"Proton charge must not vary more than 1%";
156 std::vector<EventWorkspace_const_sptr> inputWS;
158 for (
const auto &input : inputs) {
161 inputWS.push_back(ws);
168 const auto numInputs = inputWS.size();
170 const double percentMin =
getProperty(
"PercentMin");
171 const auto minN = (size_t)(percentMin / 100 * (
double)numInputs);
173 const double percentMax =
getProperty(
"PercentMax");
174 const auto maxN = std::max(minN + 1, (
size_t)(percentMax / 100 * (
double)numInputs));
176 g_log.
information() <<
"background will use " << maxN - minN <<
" out of a total of " << inputWS.size()
177 <<
" workspaces starting from " << minN <<
"\n";
181 alg->setProperty(
"InputWorkspace", inputs.front());
182 alg->executeAsChildAlg();
187 std::vector<MatrixWorkspace_sptr> grouped_inputs;
189 for (
auto const &input : inputs) {
190 const auto msg =
"Running GroupDetectors on " + input;
195 group->setPropertyValue(
"InputWorkspace", input);
196 group->setProperty(
"CopyGroupingFromWorkspace", groupWS);
197 group->executeAsChildAlg();
201 grouped_inputs.push_back(std::move(output));
205 const size_t numGroups = grouped_inputs.front()->getNumberHistograms();
206 const auto blocksize = grouped_inputs.front()->blocksize();
207 const auto Xvalues = grouped_inputs.front()->readX(0);
215 const auto detIDlist = grouped_inputs.front()->getSpectrum(
group).getDetectorIDs();
216 const std::vector<detid_t> detIDlistV(detIDlist.begin(), detIDlist.end());
218 const auto indexList = inputWS.at(0)->getIndicesFromDetectorIDs(detIDlistV);
220 for (
size_t x = 0;
x < blocksize;
x++) {
222 std::vector<std::pair<double, size_t>> intensity_input_map;
223 for (
size_t f = 0; f < numInputs; f++) {
224 intensity_input_map.push_back(std::make_pair(grouped_inputs.at(f)->readY(
group).at(
x), f));
227 std::sort(intensity_input_map.begin(), intensity_input_map.end());
229 const auto start = Xvalues.at(
x);
230 const auto end = Xvalues.at(
x + 1);
232 for (
size_t n = minN;
n < maxN;
n++) {
233 const auto inWS = inputWS.at(intensity_input_map.at(
n).second);
235 for (
auto &idx : indexList) {
236 const auto el = inWS->getSpectrum(idx);
237 auto &outSpec = outputWS->getSpectrum(idx);
239 switch (el.getEventType()) {
256 outputWS /= (double)(maxN - minN);
258 g_log.
debug() <<
"Output workspace has " << outputWS->getNumberEvents() <<
" events\n";
260 setProperty(
"OutputWorkspace", std::move(outputWS));
265 const EventType eventType,
const double start,
268 for (
auto &event : events) {
269 if (event.tof() >= end)
272 if (event.tof() >= start)
#define DECLARE_ALGORITHM(classname)
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.
@ Load
allowed here which will be passed to the algorithm
Helper class for reporting progress from algorithms.
A property class for workspaces.
GenerateGoniometerIndependentBackground : TODO: DESCRIPTION.
void exec() override
Execute the algorithm.
const std::string category() const override
Algorithm's category for identification.
void init() override
Initialize the algorithm's properties.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
void filterAndAddEvents(const std::vector< T > &events, Mantid::DataObjects::EventList &outSpec, const Mantid::API::EventType eventType, const double start, const double end)
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
int version() const override
Algorithm's version for identification.
static std::vector< std::string > unWrapGroups(const std::vector< std::string > &)
Flattens the list of group workspaces (if any) into list of workspaces.
void switchTo(Mantid::API::EventType newType) override
Switch the EventList to use the given EventType (TOF, WEIGHTED, or WEIGHTED_NOTIME)
void addEventQuickly(const Types::Event::TofEvent &event)
Append an event to the histogram, without clearing the cache, to make it faster.
This class is intended to fulfill the design specified in <https://github.com/mantidproject/documents...
std::size_t getNumberHistograms() const override
Get the number of histograms, usually the same as the number of pixels or detectors.
void sortAll(EventSortType sortType, Mantid::API::Progress *prog) const
Support for a property that holds an array of values.
BoundedValidator is a validator that requires the values to be between upper or lower bounds,...
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
void information(const std::string &msg)
Logs at information level.
EventType
What kind of event list is being stored.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
std::shared_ptr< GroupingWorkspace > GroupingWorkspace_sptr
shared pointer to the GroupingWorkspace class
std::shared_ptr< EventWorkspace > EventWorkspace_sptr
shared pointer to the EventWorkspace class
Helper class which provides the Collimation Length for SANS instruments.
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Output
An output workspace.