35 : m_iColScan(0), m_iColPt(1), m_iColFilename(2), m_iColStartDetID(3), m_iMonitorCounts(4), m_iTime(5),
36 m_setQRange(true), m_isBaseName(false), m_removeBackground(false) {}
42 "Name of table workspace for data file names in the experiment.");
46 declareProperty(
"DetectorSampleDistanceShift", 0.0,
"Amount of shift in sample-detector distance from 0.3750 meter.");
48 declareProperty(
"DetectorCenterXShift", 0.0,
"Amount of shift of detector center in X-direction from (115, 128).");
50 declareProperty(
"DetectorCenterYShift", 0.0,
"Amount of shift of detector center in Y-direction from (115, 128).");
54 declareProperty(
"CreateVirtualInstrument",
false,
"Flag to create virtual instrument.");
58 "Name of table workspace containing all the detectors.");
61 "Name of MDEventWorkspace containing all experimental data.");
64 "A vector of 3 doubles for position of source.");
67 "A vector of 3 doubles for position of sample.");
70 "A vector of 8 doubles to determine a cubic pixel's size.");
73 "It is specified as true if the data "
74 "file names listed in InputWorkspace are "
75 "base name without directory.");
79 "Name of optional background workspace.");
82 "Directory where data files are if InputWorkspace gives data file name "
83 "as the base file name as indicated by 'IsBaseName'.");
93 bool createvirtual =
getProperty(
"CreateVirtualInstrument");
94 bool inputvalid =
getInputs(createvirtual, errmsg);
96 g_log.
error() <<
"Importing error: " << errmsg <<
"\n";
97 throw std::runtime_error(errmsg);
105 if (!bkgdwsname.empty()) {
110 throw std::invalid_argument(
"Input background workspace does not have "
111 "correct number of spectra.");
128 for (
size_t i = 0; i < 3; ++i) {
139 std::vector<Kernel::V3D> vec_detpos;
140 std::vector<detid_t> vec_detid;
149 throw std::runtime_error(
"Failed to create virtual instrument.");
161 size_t nDimension = 3;
165 std::vector<std::string> vec_ID(3);
166 vec_ID[0] =
"Q_sample_x";
167 vec_ID[1] =
"Q_sample_y";
168 vec_ID[2] =
"Q_sample_z";
170 std::vector<std::string> dimensionNames(3);
171 dimensionNames[0] =
"Q_sample_x";
172 dimensionNames[1] =
"Q_sample_y";
173 dimensionNames[2] =
"Q_sample_z";
189 for (
size_t d = 0;
d < 3; ++
d)
195 for (
size_t i = 0; i < nDimension; ++i) {
196 std::string
id = vec_ID[i];
197 std::string
name = dimensionNames[i];
203 mdws->setCoordinateSystem(coordinateSystem);
220#if defined _WIN32 || defined _WIN64
233 if (numrows > 1 && !usevirtual) {
234 g_log.
warning(
"There are more than 1 experiment to import. "
235 "Make sure that all of them have the same instrument.");
239 for (
size_t ir = 0; ir < numrows; ++ir) {
249 std::stringstream filess;
252 filess << rawfilename;
253 std::string filename(filess.str());
265 g_log.
notice() <<
"[DB] Scan = " << scanid <<
"\n";
268 <<
m_iTime <<
"-th for time/duration"
273 time =
static_cast<double>(time_f);
274 }
catch (
const std::runtime_error &) {
285 std::vector<API::IMDNode *> boxes;
288 progress(0.90,
"Set up MDBoxes' dimensions. ");
290 auto it1 = boxes.begin();
291 auto it1_end = boxes.end();
292 for (; it1 != it1_end; it1++) {
294 for (
size_t dim = 0; dim < 3; ++dim) {
297 throw std::runtime_error(
"Unable to cast to MDBox");
298 mdbox->setExtents(dim, -10, 10);
300 mdbox->refreshCache(
nullptr);
317 if (!dataws->run().hasProperty(
"_omega") || !dataws->run().hasProperty(
"_chi") || !dataws->run().hasProperty(
"_phi"))
318 throw std::runtime_error(
"Data workspace does not have sample log _phi, _chi or _omega. "
319 "Unable to set goniometer and calcualte roation matrix R.");
323 setalg->initialize();
324 setalg->setProperty(
"Workspace", dataws);
325 setalg->setProperty(
"Axis0",
"_omega,0,1,0,-1");
326 setalg->setProperty(
"Axis1",
"_chi,0,0,1,-1");
327 setalg->setProperty(
"Axis2",
"_phi,0,1,0,-1");
330 if (setalg->isExecuted()) {
331 rotationMatrix = dataws->run().getGoniometer().getR();
332 g_log.
debug() <<
"Ratation matrix: " << rotationMatrix.
str() <<
"\n";
334 g_log.
debug() <<
"Ratation matrix: " << rotationMatrix.
str() <<
"\n";
336 throw std::runtime_error(
"Unable to set Goniometer.");
355 const detid_t &startdetid,
const int scannumber,
356 const int runnumber,
double measuretime,
357 int monitor_counts) {
366 std::dynamic_pointer_cast<MDEventWorkspace<MDEvent<3>, 3>>(
m_outputWS);
371 Kernel::V3D sourcePos = dataws->getInstrument()->getSource()->getPos();
372 Kernel::V3D samplePos = dataws->getInstrument()->getSample()->getPos();
373 if (dataws->x(0).size() != 2)
374 throw std::runtime_error(
"Input matrix workspace has wrong dimension in X-axis.");
375 double momentum = 0.5 * (dataws->x(0)[0] + dataws->x(0)[1]);
376 Kernel::V3D ki = (samplePos - sourcePos) * (momentum / sourcePos.
norm());
379 <<
", momentum = " << momentum <<
"\n"
380 <<
"k_i = " << ki.
toString() <<
"\n";
383 size_t numspec = dataws->getNumberHistograms();
384 const auto &specInfo = dataws->spectrumInfo();
385 double maxsignal = 0;
386 size_t nummdevents = 0;
387 for (
size_t iws = 0; iws < numspec; ++iws) {
389 double signal = dataws->y(iws)[0];
391 if (
fabs(signal) < 0.001)
395 std::vector<Mantid::coord_t> q_sample(3);
399 detid_t native_detid = specInfo.detector(iws).getID();
400 detid_t detid = native_detid + startdetid;
403 inserter.insertMDEvent(
static_cast<float>(signal),
static_cast<float>(
error *
error),
404 static_cast<uint16_t
>(runnumber), 0, detid, q_sample.data());
408 g_log.
debug() <<
"Insert DetID " << detid <<
", signal = " << signal <<
", with q_sample = " << q_sample[0] <<
", "
409 << q_sample[1] <<
", " << q_sample[2] <<
"\n";
412 if (signal > maxsignal)
417 g_log.
information() <<
"Imported Matrixworkspace of run number " << runnumber <<
": Max. Signal = " << maxsignal
418 <<
", Add " << nummdevents <<
" MDEvents "
427 expinfo->setInstrument(tmp_inst);
429 expinfo->mutableRun().setGoniometer(dataws->run().getGoniometer(),
false);
430 int scan_run_number = scannumber * 1000 + runnumber;
431 expinfo->mutableRun().addProperty(
"run_number", scan_run_number);
432 expinfo->mutableRun().addProperty(
"duration", measuretime);
433 expinfo->mutableRun().addProperty(
"monitor", monitor_counts);
435 const std::vector<Kernel::Property *> vec_property = dataws->run().getProperties();
436 for (
auto property : vec_property) {
437 expinfo->mutableRun().addProperty(property->clone());
451 std::stringstream errss;
456 const std::vector<std::string> datacolnames =
m_expDataTableWS->getColumnNames();
457 if (datacolnames.size() != 6) {
458 errss <<
"InputWorkspace must have 6 columns. But now it has " << datacolnames.size() <<
" columns. \n";
461 errss <<
"Data file name Table (InputWorkspace)'s Column " <<
m_iColFilename
462 <<
" must be 'File Name' or 'Filename' but not " << datacolnames[
m_iColFilename] <<
". "
465 errss <<
"Data file name Table (InputWorkspace)'s Column " <<
m_iColStartDetID
466 <<
" must be 'Staring DetID' or 'StartDetID' but not " << datacolnames[
m_iColStartDetID] <<
". "
474 g_log.
warning() <<
"About to deal with virtual instrument" << virtualinstrument <<
"\n";
475 if (virtualinstrument) {
479 if (detcolnames.size() != 5) {
480 errss <<
"Detector table (DetectorTableWorkspace) must have 5 columns"
485 std::vector<double> sourcepos =
getProperty(
"SourcePosition");
486 if (sourcepos.size() != 3)
487 errss <<
"SourcePosition must have 3 items. Input has " << sourcepos.size() <<
" instead.\n";
494 std::vector<double> samplepos =
getProperty(
"SamplePosition");
495 if (samplepos.size() != 3) {
496 errss <<
"SamplePosition must have 3 items. Input has " << samplepos.size() <<
" instead.\n";
508 errmsg = errss.str();
510 return (errmsg.empty());
526 std::vector<coord_t> &qSample,
531 kf = (detPos - samplePos) * (momentum / (detPos - samplePos).norm());
537 qSample[0] =
static_cast<float>(q_sample.
X());
538 qSample[1] =
static_cast<float>(q_sample.
Y());
539 qSample[2] =
static_cast<float>(q_sample.
Z());
553 std::string &errmsg) {
561 loader->initialize();
562 loader->setProperty(
"Filename", filename);
567 loader->setProperty(
"LoadInstrument",
true);
569 loader->setProperty(
"DetectorCenterXShift",
m_detXShift);
570 loader->setProperty(
"DetectorCenterYShift",
m_detYShift);
579 double wavelength =
getProperty(
"UserDefinedWavelength");
582 loader->setProperty(
"UserSpecifiedWaveLength", wavelength);
586 dataws = loader->getProperty(
"OutputWorkspace");
587 loaded =
static_cast<bool>(dataws);
588 }
catch (std::runtime_error &runerror) {
590 errmsg = runerror.what();
603 std::vector<detid_t> &vec_detid) {
606 vec_detpos.resize(numrows);
607 vec_detid.resize(numrows);
610 for (
size_t i = 0; i < numrows; ++i) {
612 vec_detid[i] = detid;
618 vec_detpos[i] = detpos;
628 for (
size_t i = 0; i < vec_q.size(); ++i) {
645 if (dataws->getNumberHistograms() !=
m_backgroundWS->getNumberHistograms())
646 throw std::runtime_error(
"Impossible to have this situation");
648 size_t numhist = dataws->getNumberHistograms();
649 for (
size_t i = 0; i < numhist; ++i) {
651 if (
fabs(bkgd_y) > 1.E-2) {
652 dataws->mutableY(i)[0] -= bkgd_y;
653 dataws->mutableE(i)[0] = std::sqrt(dataws->y(i)[0]);
#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.
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
@ OptionalDirectory
to specify a directory that does not have to exist
A property class for workspaces.
Templated class for a multi-dimensional event "box".
static API::IMDEventWorkspace_sptr CreateMDWorkspace(size_t nd, const std::string &eventType="MDLeanEvent", const Mantid::API::MDNormalization &preferredNormalization=Mantid::API::MDNormalization::VolumeNormalization, const Mantid::API::MDNormalization &preferredNormalizationHisto=Mantid::API::MDNormalization::VolumeNormalization)
Create a MDEventWorkspace of the given type.
MDEventInserter : Helper class that provides a generic interface for adding events to an MDWorkspace ...
Templated class for the multi-dimensional event workspace.
QSample : Q in the sample frame.
Support for a property that holds an array of values.
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 notice(const std::string &msg)
Logs at notice level.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
T Invert()
LU inversion routine.
std::string str() const
Convert the matrix into a simple linear string expression.
constexpr double X() const noexcept
Get x.
constexpr double Y() const noexcept
Get y.
std::string toString() const
void setZ(const double zz) noexcept
Set is z position.
double norm() const noexcept
void setX(const double xx) noexcept
Set is x position.
void setY(const double yy) noexcept
Set is y position.
constexpr double Z() const noexcept
Get z.
ConvertCWSDExpToMomentum : TODO: DESCRIPTION.
API::ITableWorkspace_sptr m_expDataTableWS
void addMDEvents(bool usevirtual)
Add MDEvents to MDEventWorkspace from data set in the experiment Run number is determined by the row ...
Geometry::Instrument_sptr m_virtualInstrument
bool m_isBaseName
Flag to use m_dataDir.
API::IMDEventWorkspace_sptr createExperimentMDWorkspace()
Create output workspace.
void parseDetectorTable(std::vector< Kernel::V3D > &vec_detpos, std::vector< detid_t > &vec_detid)
Parase detetor table workspace to vector of detector positions.
std::vector< coord_t > m_maxQVec
void updateQRange(const std::vector< Mantid::coord_t > &vec_q)
Update (sample) Q range.
bool m_removeBackground
Background workspace.
API::MatrixWorkspace_const_sptr m_backgroundWS
void exec() override
Main exec.
const std::string name() const override
Algorithm's name.
void init() override
Init to declare property.
API::MatrixWorkspace_sptr loadSpiceData(const std::string &filename, bool &loaded, std::string &errmsg)
Load SPICE data to Matrix workspace.
std::vector< coord_t > m_minQVec
bool getInputs(bool virtualinstrument, std::string &errmsg)
Examine input.
double m_detSampleDistanceShift
Shifts in detector position set from user (calibration): all in the unit as meter.
Kernel::V3D convertToQSample(const Kernel::V3D &samplePos, const Kernel::V3D &ki, const Kernel::V3D &detPos, const double &momentum, std::vector< Mantid::coord_t > &qSample, const Kernel::DblMatrix &rotationMatrix)
Convert |Q| with detector position to Q_sample.
API::IMDEventWorkspace_sptr m_outputWS
std::vector< double > m_extentMins
std::string m_dataDir
Data directory.
void removeBackground(const API::MatrixWorkspace_sptr &dataws)
Remove background from.
std::vector< size_t > m_numBins
void setupTransferMatrix(const API::MatrixWorkspace_sptr &dataws, Kernel::DblMatrix &rotationMatrix)
Set goniometer to matrix workspace and get its rotation matrix R (from Q-sample to Q-lab and output 1...
void convertSpiceMatrixToMomentumMDEvents(const API::MatrixWorkspace_sptr &dataws, bool usevirtual, const detid_t &startdetid, const int scannumber, const int runnumber, double measuretime, int monitor_counts)
Convert a SPICE 2D Det MatrixWorkspace to MDEvents and append to an MDEventWorkspace It is optional t...
API::ITableWorkspace_sptr m_detectorListTableWS
void createVirtualInstrument()
Create virtual instrument.
std::vector< double > m_extentMaxs
std::shared_ptr< IMDEventWorkspace > IMDEventWorkspace_sptr
Shared pointer to Mantid::API::IMDEventWorkspace.
std::shared_ptr< ExperimentInfo > ExperimentInfo_sptr
Shared pointer to ExperimentInfo.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
MANTID_GEOMETRY_DLL Geometry::Instrument_sptr createVirtualInstrument(Kernel::V3D sourcePos, Kernel::V3D samplePos, const std::vector< Kernel::V3D > &vecdetpos, const std::vector< detid_t > &vecdetid)
std::shared_ptr< MDHistoDimension > MDHistoDimension_sptr
Shared pointer to a MDHistoDimension.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
SpecialCoordinateSystem
Special coordinate systems for Q3D.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
int32_t detid_t
Typedef for a detector ID.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
@ Input
An input workspace.
@ Output
An output workspace.