Mantid
Loading...
Searching...
No Matches
SANSInstrumentCreationHelper.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/*********************************************************************************
8 * PLEASE READ THIS!!!!!!!
9 *
10 * This collection of functions MAY NOT be used in any test from a package
11 *below
12 * the level of DataHandling (e.g. Kernel, Geometry, API, DataObjects).
13 * I.e. It can only be used by plugin/algorithm-level packages (e.g.
14 *DataHandling)
15 *********************************************************************************/
18#include "MantidAPI/Axis.h"
24
25/*****************************************************
26 * SANS instrument helper class
27 *****************************************************/
28
29using Mantid::detid_t;
31using namespace Mantid::Geometry;
32using namespace Mantid::API;
34
35// Number of detector pixels in each dimension
37// The test instrument has 2 monitors
39
40/*
41 * Generate a SANS test workspace, with instrument geometry.
42 * The geometry is the SANSTEST geometry, with a 30x30 pixel 2D detector.
43 *
44 * @param workspace: name of the workspace to be created.
45 */
47 // Create a test workspace with test data with a well defined peak
48 // The test instrument has two monitor channels
50 AnalysisDataService::Instance().addOrReplace(workspace, ws);
51 ws->getAxis(0)->unit() = Mantid::Kernel::UnitFactory::Instance().create("Wavelength");
52 ws->setYUnit("");
53
54 // Load instrument geometry
55 runLoadInstrument("SANSTEST", ws);
57
58 return ws;
59}
60
65void SANSInstrumentCreationHelper::runLoadInstrument(const std::string &inst_name,
67 // Determine the search directory for XML instrument definition files (IDFs)
68 // std::string directoryName =
69 // Mantid::Kernel::ConfigService::Instance().getInstrumentDirectory();
70
71 // For Nexus Mantid processed, Instrument XML file name is read from nexus
72 std::string instrumentID = inst_name;
73 // force ID to upper case
74 std::transform(instrumentID.begin(), instrumentID.end(), instrumentID.begin(), toupper);
75
77 loadInst.initialize();
78 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
79 loadInst.setPropertyValue("Filename", "unit_testing/" + instrumentID + "_Definition.xml");
80 loadInst.setProperty<MatrixWorkspace_sptr>("Workspace", workspace);
81 loadInst.setProperty("RewriteSpectraMap", Mantid::Kernel::OptionalBool(false));
82 loadInst.execute();
83}
84
93 int nxbins, int nybins) {
94 // Get the number of monitor channels
95 size_t nMonitors(0);
96 size_t nXbins, nYbins;
97 std::shared_ptr<const Instrument> instrument = workspace->getInstrument();
98 std::vector<detid_t> monitors = instrument->getMonitors();
99 nMonitors = monitors.size();
100
101 // Number of monitors should be consistent with data file format
102 if (nMonitors != 2) {
103 std::stringstream error;
104 error << "Geometry error for " << instrument->getName() << ": Spice data format defines 2 monitors, " << nMonitors
105 << " were/was found";
106 throw std::runtime_error(error.str());
107 }
108 if (nxbins >= 0) {
109 nXbins = size_t(nxbins);
110 } else {
111 throw std::invalid_argument("number of x-bins < 0");
112 }
113 if (nybins >= 0) {
114 nYbins = size_t(nybins);
115 } else {
116 throw std::invalid_argument("number of y-bins < 0");
117 }
118
119 // Generate mapping of detector/channel IDs to spectrum No
120
121 // Detector/channel counter
122 size_t wi = 0;
123
124 // Monitor: IDs start at 1 and increment by 1
125 for (size_t i = 0; i < nMonitors; i++) {
126 // std::cout << "SANS instrument monitor number " << i << '\n';
127 workspace->getSpectrum(wi).setSpectrumNo(specnum_t(wi));
128 workspace->getSpectrum(wi).setDetectorID(detid_t(wi + 1));
129 wi++;
130 }
131
132 // Detector pixels
133 for (size_t ix = 0; ix < nXbins; ix++) {
134 for (size_t iy = 0; iy < nYbins; iy++) {
135 workspace->getSpectrum(wi).setSpectrumNo(specnum_t(wi));
136 workspace->getSpectrum(wi).setDetectorID(detid_t(1000000 + iy * 1000 + ix));
137 wi++;
138 }
139 }
140}
double error
Definition: IndexPeaks.cpp:133
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
void initialize() override
Initialization method invoked by the framework.
Definition: Algorithm.cpp:283
bool execute() override final
The actions to be performed by the algorithm on a dataset.
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
Definition: Algorithm.cpp:1975
Loads instrument data from an XML or Nexus instrument description file and adds it to a workspace.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
OptionalBool : Tri-state bool.
Definition: OptionalBool.h:25
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
static void runLoadMappingTable(const Mantid::DataObjects::Workspace2D_sptr &workspace, int nxbins, int nybins)
Populate spectra mapping to detector IDs.
static void runLoadInstrument(const std::string &inst_name, const Mantid::DataObjects::Workspace2D_sptr &workspace)
Run the Child Algorithm LoadInstrument (as for LoadRaw)
static Mantid::DataObjects::Workspace2D_sptr createSANSInstrumentWorkspace(const std::string &workspace)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16
Mantid::DataObjects::Workspace2D_sptr create2DWorkspace123(int64_t nHist, int64_t nBins, bool isHist=false, const std::set< int64_t > &maskedWorkspaceIndices=std::set< int64_t >(), bool hasDx=false)