Mantid
Loading...
Searching...
No Matches
ConvertToMDMinMaxLocal.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 +
8
9#include <cfloat>
10
12
16
17using namespace Mantid::Kernel;
18using namespace Mantid::API;
19
20namespace Mantid::MDAlgorithms {
21
22// Register the algorithm into the AlgorithmFactory
23DECLARE_ALGORITHM(ConvertToMDMinMaxLocal)
24
25//----------------------------------------------------------------------------------------------
27const std::string ConvertToMDMinMaxLocal::name() const { return "ConvertToMDMinMaxLocal"; }
28
29//----------------------------------------------------------------------------------------------
32
35}
36
37//----------------------------------------------------------------------------------------------
42
43 // -------- get Input workspace
44 Mantid::API::MatrixWorkspace_sptr InWS2D = getProperty("InputWorkspace");
45
46 // Collect and Analyze the requests to the job, specified by the input
47 // parameters:
48 // a) Q selector:
49 std::string QModReq = getProperty("QDimensions");
50 // b) the energy exchange mode
51 std::string dEModReq = getProperty("dEAnalysisMode");
52 // c) other dim property;
53 std::vector<std::string> otherDimNames = getProperty("OtherDimensions");
54 // d) The output dimensions in the Q3D mode, processed together with
55 // QConversionScales
56 std::string QFrame = getProperty("Q3DFrames");
57 // e) part of the procedure, specifying the target dimensions units. Currently
58 // only Q3D target units can be converted to different flavors of hkl
59 std::string convertTo_ = getProperty("QConversionScales");
60
61 // Build the target ws description as function of the input & output ws and
62 // the parameters, supplied to the algorithm
63 MDWSDescription targWSDescr;
64
65 // get raw pointer to Q-transformation (do not delete this pointer, it's held
66 // by MDTransfFactory!)
67 MDTransfInterface *pQtransf = MDTransfFactory::Instance().create(QModReq).get();
68 // get number of dimensions this Q transformation generates from the
69 // workspace.
70 auto iEmode = Kernel::DeltaEMode::fromString(dEModReq);
71 // get total number of dimensions the workspace would have.
72 unsigned int nMatrixDim = pQtransf->getNMatrixDimensions(iEmode, InWS2D);
73 // total number of dimensions
74 size_t nDim = nMatrixDim + otherDimNames.size();
75
76 std::vector<double> MinValues, MaxValues;
77 MinValues.resize(nDim, -FLT_MAX / 10);
78 MaxValues.resize(nDim, FLT_MAX / 10);
79 // verify that the number min/max values is equivalent to the number of
80 // dimensions defined by properties and min is less max
81 targWSDescr.setMinMax(MinValues, MaxValues);
82 targWSDescr.buildFromMatrixWS(InWS2D, QModReq, dEModReq, otherDimNames);
83 // add expInfoIndex to the target workspace description for further usage as the
84 // identifier for the events, which come from this run.
85 targWSDescr.addProperty("EXP_INFO_INDEX", uint16_t(0), true);
86
87 // instantiate class, responsible for defining Mslice-type projection
89 // identify if u,v are present among input parameters and use defaults if not
90 std::vector<double> ut = getProperty("UProj");
91 std::vector<double> vt = getProperty("VProj");
92 std::vector<double> wt = getProperty("WProj");
93 try {
94 // otherwise input uv are ignored -> later it can be modified to set ub
95 // matrix if no given, but this may over-complicate things.
96 MsliceProj.setUVvectors(ut, vt, wt);
97 } catch (std::invalid_argument &) {
98 g_log.error() << "The projections are coplanar. Will use defaults "
99 "[1,0,0],[0,1,0] and [0,0,1]\n";
100 }
101
102 // set up target coordinate system and identify/set the (multi) dimension's
103 // names to use
104 targWSDescr.m_RotMatrix = MsliceProj.getTransfMatrix(targWSDescr, QFrame, convertTo_);
105
106 // preprocess detectors (or make fake detectors in CopyMD case)
107 targWSDescr.m_PreprDetTable =
108 this->preprocessDetectorsPositions(InWS2D, dEModReq, false, std::string(getProperty("PreprocDetectorsWS")));
109
110 // do the job
111 findMinMaxValues(targWSDescr, pQtransf, iEmode, MinValues, MaxValues);
112
113 setProperty("MinValues", MinValues);
114 setProperty("MaxValues", MaxValues);
115}
116
118 Kernel::DeltaEMode::Type iEMode, std::vector<double> &MinValues,
119 std::vector<double> &MaxValues) {
120
122 double signal(1), errorSq(1);
123 //
124 size_t nDims = MinValues.size();
125 MinValues.assign(nDims, DBL_MAX);
126 MaxValues.assign(nDims, -DBL_MAX);
127 //
128 auto inWS = WSDescription.getInWS();
129 std::string convUnitsID = pQtransf->inputUnitID(iEMode, inWS);
130 // initialize units conversion
131 unitsConverter.initialize(WSDescription, convUnitsID);
132 // initialize MD transformation
133 pQtransf->initialize(WSDescription);
134
135 //
136 auto nSpectra = WSDescription.m_PreprDetTable->getLogs()->getPropertyValueAsType<uint32_t>("ActualDetectorsNum");
137 auto detIDMap = WSDescription.m_PreprDetTable->getColVector<size_t>("detIDMap");
138
139 // vector to place transformed coordinates;
140 std::vector<coord_t> locCoord(nDims);
141
142 pQtransf->calcGenericVariables(locCoord, nDims);
143 // PRAGMA_OMP(parallel for reduction(||:rangeChanged))
144 for (size_t i = 0; i < nSpectra; i++) {
145 // get valid spectrum number
146 size_t iSpctr = detIDMap[i];
147
148 // update unit conversion according to current spectra
149 unitsConverter.updateConversion(i);
150 // update coordinate transformation according to the spectra
151 pQtransf->calcYDepCoordinates(locCoord, i);
152
153 // get the range of the input data in the spectra
154 auto source_range = inWS->getSpectrum(iSpctr).getXDataRange();
155
156 // extract part of this range which has well defined unit conversion
157 source_range = unitsConverter.getConversionRange(source_range.first, source_range.second);
158
159 double x1 = unitsConverter.convertUnits(source_range.first);
160 double x2 = unitsConverter.convertUnits(source_range.second);
161
162 std::vector<double> range = pQtransf->getExtremumPoints(x1, x2, i);
163 // transform coordinates
164 for (double &k : range) {
165
166 pQtransf->calcMatrixCoord(k, locCoord, signal, errorSq);
167 // identify min-max ranges for current spectrum
168 for (size_t j = 0; j < nDims; j++) {
169 if (locCoord[j] < MinValues[j])
170 MinValues[j] = locCoord[j];
171 if (locCoord[j] > MaxValues[j])
172 MaxValues[j] = locCoord[j];
173 }
174 }
175 }
176}
177} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
int nSpectra
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 addProperty(Kernel::Property *prop, bool overwrite=false)
Add data to the object in the form of a property.
Definition: LogManager.h:79
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 error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
ConvertToMDMinMaxLocal : Algorithm to calculate limits for ConvertToMD.
void findMinMaxValues(MDWSDescription &WSDescription, MDTransfInterface *const pQtransf, Kernel::DeltaEMode::Type iEMode, std::vector< double > &MinValues, std::vector< double > &MaxValues)
void init() override
Virtual method - must be overridden by concrete algorithm.
void exec() override
Execute the algorithm.
DataObjects::TableWorkspace_const_sptr preprocessDetectorsPositions(const Mantid::API::MatrixWorkspace_const_sptr &InWS2D, const std::string &dEModeRequested, bool updateMasks, const std::string &OutWSName)
The method responsible for analyzing input workspace parameters and preprocessing detectors positions...
void init() override
Initialize the algorithm's properties.
Interface to set of sub-classes used by ConvertToMD algorithm and responsible for conversion of input...
virtual bool calcYDepCoordinates(std::vector< coord_t > &Coord, size_t i)=0
generalizes the code to calculate Y-variables within the detector's loop of the workspace
virtual const std::string inputUnitID(Kernel::DeltaEMode::Type dEmode, API::MatrixWorkspace_const_sptr inWS) const =0
returns the unit ID for the input units, the particular transformation expects.
virtual bool calcMatrixCoord(const double &X, std::vector< coord_t > &Coord, double &signal, double &errSq) const =0
The method to calculate all remaining coordinates, defined within the inner loop given that the input...
virtual void initialize(const MDWSDescription &)=0
set up transformation from the class, which can provide all variables necessary for the conversion
virtual bool calcGenericVariables(std::vector< coord_t > &Coord, size_t n_ws_variabes)=0
Method deployed out of the loop and calculates all variables needed within the loop.
virtual unsigned int getNMatrixDimensions(Kernel::DeltaEMode::Type mode, API::MatrixWorkspace_const_sptr inWS) const =0
return the number of dimensions, calculated by the transformation from the workspace.
virtual std::vector< double > getExtremumPoints(const double xMin, const double xMax, size_t det_num) const =0
method returns the vector of input coordinates values where the transformed coordinates reach its ext...
helper class describes the properties of target MD workspace, which should be obtained as the result ...
void setMinMax(const std::vector< double > &minVal, const std::vector< double > &maxVal)
function sets up min-max values to the dimensions, described by the class
API::MatrixWorkspace_const_sptr getInWS() const
void buildFromMatrixWS(const API::MatrixWorkspace_sptr &pWS, const std::string &QMode, const std::string &dEMode, const std::vector< std::string > &dimPropertyNames=std::vector< std::string >())
method builds MD Event ws description from a matrix workspace and the transformations,...
DataObjects::TableWorkspace_const_sptr m_PreprDetTable
std::vector< double > getTransfMatrix(MDAlgorithms::MDWSDescription &TargWSDescription, const std::string &FrameRequested, const std::string &QScaleRequested) const
method to build the Q-coordinates transformation.
void setUVvectors(const std::vector< double > &ut, const std::vector< double > &vt, const std::vector< double > &wt)
helper function which verifies if projection vectors are specified and if their values are correct wh...
void updateConversion(size_t i)
Method updates unit conversion given the index of detector parameters in the array of detectors.
void initialize(const MDWSDescription &targetWSDescr, const std::string &unitsTo, bool forceViaTOF=false)
Initialize unit conversion helper This method is interface to internal initialize method,...
double convertUnits(double val) const
do actual unit conversion from input to oputput data
std::pair< double, double > getConversionRange(double x1, double x2) const
Method verify if the Units transformation is well defined in the range provided and if not returns th...
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
STL namespace.
static Type fromString(const std::string &modeStr)
Returns the emode from the given string.
Definition: DeltaEMode.cpp:69
Type
Define the available energy transfer modes It is important to assign enums proper numbers,...
Definition: DeltaEMode.h:29
@ Output
An output workspace.
Definition: Property.h:54