24#include <boost/algorithm/string.hpp>
40 :
SlicingAlgorithm(), outWS(), implicitFunction(), indexMultiplier(), signals(nullptr), errors(nullptr),
48 "An input MDWorkspace.");
55 std::string grp =
"Methods";
57 "XML string describing the implicit function determining "
58 "which bins to use.");
62 "Alternative binning method where you iterate through every event, "
63 "placing them in the proper bin.\n"
64 "This may be faster for workspaces with few events and lots of output "
69 "Temporary parameter: true to run in parallel. This is ignored for "
70 "file-backed workspaces, where running in parallel makes things slower "
71 "due to disk thrashing.");
76 "An input MDHistoWorkspace used to accumulate results from "
77 "multiple MDEventWorkspaces. If unspecified a blank "
78 "MDHistoWorkspace will be created.");
81 "A name for the output MDHistoWorkspace.");
93template <
typename MDE,
size_t nd>
96 auto outCenter = std::vector<coord_t>(
m_outD);
102 size_t numVertexes = 0;
106 size_t lastLinearIndex = 0;
109 for (
size_t i = 0; i < numVertexes; i++) {
111 const coord_t *inCenter = vertexes.get() + i * nd;
117 size_t linearIndex = 0;
122 for (
size_t bd = 0; bd <
m_outD; bd++) {
127 if ((
x >= 0) && (ix >= chunkMin[bd]) && (ix < chunkMax[bd])) {
139 if ((i > 0) && (linearIndex != lastLinearIndex)) {
144 lastLinearIndex = linearIndex;
173 for (
auto it = events.begin(); it != events.end(); ++it) {
175 const coord_t *inCenter = it->getCenter();
181 size_t linearIndex = 0;
186 for (
size_t bd = 0; bd <
m_outD; bd++) {
191 if ((
x >= 0) && (ix >= chunkMin[bd]) && (ix < chunkMax[bd])) {
204 errors[linearIndex] +=
static_cast<signal_t>(it->getErrorSquared());
242 outWS->setTo(0.0, 0.0, 0.0);
248 size_t chunkDimension = 0;
253 if (chunkNumBins < 1)
259 if (bc->isFileBacked())
265 size_t progNumSteps = 0;
267 prog->setNotifyStep(0.1);
268 prog->resetNumSteps(100, 0.00, 1.0);
274 PRAGMA_OMP( parallel
for schedule(dynamic,1)
if (doParallel) )
275 for (
int chunk = 0; chunk < int(
m_binDimensions[chunkDimension]->getNBins()); chunk += chunkNumBins) {
278 std::vector<size_t> chunkMin(
m_outD);
279 std::vector<size_t> chunkMax(
m_outD);
280 for (
size_t bd = 0; bd <
m_outD; bd++) {
286 chunkMin[chunkDimension] = size_t(chunk);
287 if (
size_t(chunk + chunkNumBins) >
m_binDimensions[chunkDimension]->getNBins())
288 chunkMax[chunkDimension] =
m_binDimensions[chunkDimension]->getNBins();
290 chunkMax[chunkDimension] = size_t(chunk + chunkNumBins);
297 std::vector<API::IMDNode *> boxes;
299 ws->
getBox()->getBoxes(boxes, 1000,
true, function.get());
303 if (bc->isFileBacked())
309 g_log.
debug() <<
"Chunk " << chunk <<
": found " << boxes.size() <<
" boxes within the implicit function.\n";
310 progNumSteps += boxes.size();
311 prog->setNumSteps(progNumSteps);
316 for (
auto &boxe : boxes) {
319 if (box && !box->getIsMasked())
320 this->
binMDBox(box, chunkMin.data(), chunkMax.data());
336 prog->report(
"Applying implicit function.");
337 signal_t nan = std::numeric_limits<signal_t>::quiet_NaN();
355 if (!ImplicitFunctionXML.empty())
360 prog = std::make_unique<Progress>(
this, 0.0, 1.0, 1);
363 std::shared_ptr<IMDHistoWorkspace>
tmp = this->
getProperty(
"TemporaryDataWorkspace");
364 outWS = std::dynamic_pointer_cast<MDHistoWorkspace>(
tmp);
374 for (
size_t i = 0; i <
m_bases.size(); i++)
388 if (!IterateEvents) {
389 g_log.
warning() <<
"IterateEvents=False is no longer supported. Setting "
390 "IterateEvents=True.\n";
391 IterateEvents =
true;
401 if (
m_inWS->isMDHistoWorkspace()) {
402 throw std::runtime_error(
"Cannot rebin a workspace that is histogrammed and has no original "
403 "workspace that is an MDEventWorkspace. "
404 "Reprocess the input so that it contains full MDEvents.");
412 outWS->setCoordinateSystem(inEWS->getSpecialCoordinateSystem());
414 outWS->copyExperimentInfos(*inEWS);
415 }
catch (std::runtime_error &) {
416 g_log.
warning() << this->
name() <<
" was not able to copy experiment info to output workspace "
417 <<
outWS->getName() <<
'\n';
422 outWS->setDisplayNormalization(
m_inWS->displayNormalizationHisto());
426 setProperty(
"OutputWorkspace", std::dynamic_pointer_cast<Workspace>(
outWS));
#define DECLARE_ALGORITHM(classname)
#define CALL_MDEVENT_FUNCTION(funcname, workspace)
Macro that makes it possible to call a templated method for a MDEventWorkspace using a IMDEventWorksp...
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
#define PARALLEL_CRITICAL(name)
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PRAGMA_OMP(expression)
#define PARALLEL_GET_MAX_THREADS
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
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.
std::atomic< bool > m_cancel
Set to true to stop execution.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
static void sortObjByID(std::vector< IMDNode * > &boxes)
Static method for sorting a list of MDBoxBase pointers by their file position, ascending.
A property class for workspaces.
std::unique_ptr< coord_t[]> getVertexesArray(size_t &numVertices) const override
signal_t getSignal() const override
Returns the integrated signal from all points within.
signal_t getErrorSquared() const override
Returns the integrated error squared from all points within.
Templated class for a multi-dimensional event "box".
const std::vector< MDE > & getConstEvents() const
Get vector of constant events to use.
uint64_t getNPoints() const override
std::shared_ptr< MDEventWorkspace< MDE, nd > > sptr
Typedef for a shared pointer of this kind of event workspace.
MDBoxBase< MDE, nd > * getBox()
Mantid::API::BoxController_sptr getBoxController() override
Returns the BoxController used in this workspace.
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.
void debug(const std::string &msg)
Logs at debug level.
void warning(const std::string &msg)
Logs at warning level.
The concrete, templated class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
void binMDBox(DataObjects::MDBox< MDE, nd > *box, const size_t *const chunkMin, const size_t *const chunkMax)
Method to bin a single MDBox.
std::unique_ptr< Mantid::Geometry::MDImplicitFunction > implicitFunction
ImplicitFunction used.
std::vector< size_t > indexMultiplier
Cached values for speed up.
void exec() override
Run the algorithm.
const std::string name() const override
Algorithm's name for identification.
std::unique_ptr< Mantid::API::Progress > prog
Progress reporting.
void binByIterating(typename DataObjects::MDEventWorkspace< MDE, nd >::sptr ws)
Helper method.
void init() override
Initialise the properties.
Mantid::DataObjects::MDHistoWorkspace_sptr outWS
The output MDHistoWorkspace.
Abstract Algorithm class that will be used by: BinMD and SliceMD and shares code for getting a slice ...
std::unique_ptr< API::CoordTransform > m_transformFromOriginal
Coordinate transformation to save in the output workspace (original->binned)
Mantid::API::IMDWorkspace_sptr m_intermediateWS
Intermediate original workspace.
std::unique_ptr< Mantid::Geometry::MDImplicitFunction > getImplicitFunctionForChunk(const size_t *const chunkMin, const size_t *const chunkMax)
Create an implicit function for picking boxes, based on the indexes in the output MDHistoWorkspace.
std::unique_ptr< API::CoordTransform > m_transform
Coordinate transformation to apply.
void createTransform()
Read the algorithm properties and creates the appropriate transforms for slicing the MDEventWorkspace...
Mantid::API::IMDWorkspace_sptr m_inWS
Input workspace.
std::unique_ptr< API::CoordTransform > m_transformToOriginal
Coordinate transformation to save in the output workspace (binned->original)
std::unique_ptr< DataObjects::CoordTransformAffine > m_transformFromIntermediate
Coordinate transformation to save in the output WS, from the intermediate WS.
size_t m_outD
Number of dimensions in the output (binned) workspace.
std::vector< Mantid::Kernel::VMD > m_bases
Basis vectors of the output dimensions, normalized to unity length.
void initSlicingProps()
Initialise the properties.
Mantid::Kernel::VMD m_translation
Translation from the OUTPUT to the INPUT workspace i.e.
std::unique_ptr< DataObjects::CoordTransformAffine > m_transformToIntermediate
Coordinate transformation to save in the intermediate WS.
std::vector< Mantid::Geometry::MDHistoDimension_sptr > m_binDimensions
Bin dimensions to actually use.
std::shared_ptr< IMDEventWorkspace > IMDEventWorkspace_sptr
Shared pointer to Mantid::API::IMDEventWorkspace.
std::shared_ptr< BoxController > BoxController_sptr
Shared ptr to BoxController.
std::size_t numEvents(::NeXus::File &file, bool &hasTotalCounts, bool &oldNeXusFileNames, const std::string &prefix, const NexusHDF5Descriptor &descriptor)
Get the number of events in the currently opened group.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
double signal_t
Typedef for the signal recorded in a MDBox, etc.
@ Input
An input workspace.
@ Output
An output workspace.