Mantid
Loading...
Searching...
No Matches
MuonWorkspaceCreationHelper.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
12
15#include "MantidAPI/TableRow.h"
16#include "MantidAPI/Workspace.h"
20
21using namespace Mantid;
22using namespace Mantid::Kernel;
23using namespace Mantid::API;
24
26
27// Generate y-values which increment by 1 each time the function is called
28yDataCounts::yDataCounts() : m_count(-1) {}
29double yDataCounts::operator()(const double, size_t) {
30 m_count++;
31 return static_cast<double>(m_count);
32}
33
39yDataAsymmetry::yDataAsymmetry(const double amp, const double phi) : m_amp(amp), m_phi(phi) {}
41 m_amp = 1.5;
42 m_phi = 0.1;
43}
44double yDataAsymmetry::operator()(const double t, size_t spec) {
45 double e = exp(-t / tau);
46 double factor = (static_cast<double>(spec) + 1.0) * 0.5;
47 double phase_offset = 4 * M_PI / 180;
48 return std::max(
49 0.0, (10. * factor * (1.0 + m_amp * cos(m_omega * t + m_phi + static_cast<double>(spec) * phase_offset)) * e));
50}
51
52// Errors are fixed to 0.005
53double eData::operator()(const double, size_t) { return 0.005; }
54
69MatrixWorkspace_sptr createCountsWorkspace(size_t nspec, size_t maxt, double seed, size_t detectorIDseed, bool isHist,
70 double xStart, double xEnd) {
71
73 yDataCounts(), static_cast<int>(nspec), xStart, xEnd, (1.0 / static_cast<double>(maxt)), isHist, eData());
74
75 ws->setInstrument(ComponentCreationHelper::createTestInstrumentCylindrical(static_cast<int>(nspec)));
76
77 for (int g = 0; g < static_cast<int>(nspec); g++) {
78 auto &spec = ws->getSpectrum(g);
79 spec.addDetectorID(g + static_cast<int>(detectorIDseed));
80 spec.setSpectrumNo(g + 1);
81 ws->mutableY(g) += seed;
82 }
83
84 // Add number of good frames (required for Asymmetry calculation)
85 ws->mutableRun().addProperty("goodfrm", 10);
86 // Add instrument and run number
87 std::shared_ptr<Geometry::Instrument> inst1 = std::make_shared<Geometry::Instrument>();
88 inst1->setName("EMU");
89 ws->setInstrument(inst1);
90 ws->mutableRun().addProperty("run_number", 12345);
91
92 return ws;
93}
94
95MatrixWorkspace_sptr createCountsWorkspace(size_t nspec, size_t maxt, double seed, size_t detectorIDseed) {
96 return createCountsWorkspace(nspec, maxt, seed, detectorIDseed, true, 0.0, 1.0);
97}
98
111WorkspaceGroup_sptr createMultiPeriodWorkspaceGroup(const int &nPeriods, size_t nspec, size_t maxt,
112 const std::string &wsGroupName) {
113
114 WorkspaceGroup_sptr wsGroup = std::make_shared<WorkspaceGroup>();
115 AnalysisDataService::Instance().addOrReplace(wsGroupName, wsGroup);
116
117 std::string wsNameStem = "MuonDataPeriod_";
118 std::string wsName;
119
120 std::shared_ptr<Geometry::Instrument> inst1 = std::make_shared<Geometry::Instrument>();
121 inst1->setName("EMU");
122
123 for (int period = 1; period < nPeriods + 1; period++) {
124 // Period 1 yvalues : 1,2,3,4,5,6,7,8,9,10
125 // Period 2 yvalues : 2,3,4,5,6,7,8,9,10,11 etc..
126 MatrixWorkspace_sptr ws = createCountsWorkspace(nspec, maxt, period);
127
128 wsGroup->addWorkspace(ws);
129 wsName = wsNameStem + std::to_string(period);
130 AnalysisDataService::Instance().addOrReplace(wsName, ws);
131 }
132
133 return wsGroup;
134}
135
136Mantid::API::WorkspaceGroup_sptr createMultiPeriodAsymmetryData(const int &nPeriods, size_t nspec, size_t maxt,
137 const std::string &wsGroupName) {
138 Mantid::API::WorkspaceGroup_sptr wsGroup = std::make_shared<Mantid::API::WorkspaceGroup>();
139 Mantid::API::AnalysisDataService::Instance().addOrReplace(wsGroupName, wsGroup);
140
141 std::string wsNameStem = "MuonDataPeriod_";
142 std::string wsName;
143
144 std::shared_ptr<Mantid::Geometry::Instrument> inst1 = std::make_shared<Mantid::Geometry::Instrument>();
145 inst1->setName("EMU");
146
147 for (int period = 1; period < nPeriods + 1; period++) {
149 createAsymmetryWorkspace(nspec, maxt, yDataAsymmetry(10.0 * period, 0.1 * period));
150
151 wsGroup->addWorkspace(ws);
152 wsName = wsNameStem + std::to_string(period);
153 Mantid::API::AnalysisDataService::Instance().addOrReplace(wsName, ws);
154 }
155
156 return wsGroup;
157}
158
166ITableWorkspace_sptr createDeadTimeTable(const size_t &nspec, std::vector<double> &deadTimes) {
167
168 auto deadTimeTable =
169 std::dynamic_pointer_cast<ITableWorkspace>(WorkspaceFactory::Instance().createTable("TableWorkspace"));
170
171 deadTimeTable->addColumn("int", "Spectrum Number");
172 deadTimeTable->addColumn("double", "Dead Time");
173
174 if (deadTimes.size() != nspec) {
175 return deadTimeTable;
176 }
177
178 for (size_t spec = 0; spec < deadTimes.size(); spec++) {
179 TableRow newRow = deadTimeTable->appendRow();
180 newRow << static_cast<int>(spec) + 1;
181 newRow << deadTimes[spec];
182 }
183
184 return deadTimeTable;
185}
186
193ITableWorkspace_sptr createTimeZeroTable(const size_t &numSpec, const std::vector<double> &timeZeros) {
194
195 auto timeZeroTable =
196 std::dynamic_pointer_cast<ITableWorkspace>(WorkspaceFactory::Instance().createTable("TableWorkspace"));
197
198 timeZeroTable->addColumn("double", "time zero");
199
200 if (timeZeros.size() != numSpec) {
201 return timeZeroTable;
202 }
203
204 for (size_t specNum = 0; specNum < numSpec; ++specNum) {
205 TableRow row = timeZeroTable->appendRow();
206 row << timeZeros[specNum];
207 }
208
209 return timeZeroTable;
210}
211
219MatrixWorkspace_sptr createWorkspaceWithInstrumentandRun(const std::string &instrName, int runNumber, size_t nSpectra) {
220
221 Geometry::Instrument_const_sptr instr = std::make_shared<Geometry::Instrument>(instrName);
222 MatrixWorkspace_sptr ws = WorkspaceFactory::Instance().create("Workspace2D", nSpectra, 1, 1);
223 ws->setInstrument(instr);
224 ws->mutableRun().addProperty("run_number", runNumber);
225 return ws;
226}
227
237WorkspaceGroup_sptr createWorkspaceGroupConsecutiveDetectorIDs(const int &nWorkspaces, size_t nspec, size_t maxt,
238 const std::string &wsGroupName) {
239
240 WorkspaceGroup_sptr wsGroup = std::make_shared<WorkspaceGroup>();
241 AnalysisDataService::Instance().addOrReplace(wsGroupName, wsGroup);
242
243 std::string wsNameStem = "MuonDataPeriod_";
244 std::string wsName;
245
246 for (int period = 1; period < nWorkspaces + 1; period++) {
247 // Period 1 yvalues : 1,2,3,4,5,6,7,8,9,10
248 // Period 2 yvalues : 2,3,4,5,6,7,8,9,10,11 etc..
249 size_t detIDstart = (period - 1) * nspec + 1;
250 MatrixWorkspace_sptr ws = createCountsWorkspace(nspec, maxt, period, detIDstart);
251 wsGroup->addWorkspace(ws);
252
253 wsName = wsNameStem + std::to_string(period);
254 AnalysisDataService::Instance().addOrReplace(wsName, ws);
255 }
256
257 return wsGroup;
258}
259
260} // namespace MuonWorkspaceCreationHelper
const double m_phi
Definition: LoadEMU.cpp:227
int nSpectra
TableRow represents a row in a TableWorkspace.
Definition: TableRow.h:39
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Mantid::Geometry::Instrument_sptr createTestInstrumentCylindrical(int num_banks, const Mantid::Kernel::V3D &sourcePos=Mantid::Kernel::V3D(0.0, 0.0, -10.), const Mantid::Kernel::V3D &samplePos=Mantid::Kernel::V3D(), const double cylRadius=0.004, const double cylHeight=0.0002)
Create an test instrument with n panels of 9 cylindrical detectors, a source and a sample position.
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
Helper class which provides the Collimation Length for SANS instruments.
Mantid::API::WorkspaceGroup_sptr createWorkspaceGroupConsecutiveDetectorIDs(const int &nWorkspaces, size_t nspec, size_t maxt, const std::string &wsGroupName)
Creates a grouped workspace containing multiple workspaces with multiple spectra, the detector IDs ar...
Mantid::API::ITableWorkspace_sptr createTimeZeroTable(const size_t &numSpec, const std::vector< double > &timeZeros)
Create a simple time zero TableWorkspace with one column (time zero)
Mantid::API::WorkspaceGroup_sptr createMultiPeriodAsymmetryData(const int &nPeriods, size_t nspec, size_t maxt, const std::string &wsGroupName)
Mantid::API::MatrixWorkspace_sptr createCountsWorkspace(size_t nspec, size_t maxt, double seed, size_t detectorIDseed=1)
Create a matrix workspace appropriate for Group Counts.
Mantid::API::WorkspaceGroup_sptr createMultiPeriodWorkspaceGroup(const int &nPeriods, size_t nspec, size_t maxt, const std::string &wsGroupName)
Create a WorkspaceGroup and add to the ADS, populate with MatrixWorkspaces simulating periods as used...
Mantid::API::MatrixWorkspace_sptr createAsymmetryWorkspace(std::size_t nspec, std::size_t maxt, Function dataGenerator=yDataAsymmetry())
Create a matrix workspace appropriate for Group Asymmetry.
Mantid::API::ITableWorkspace_sptr createDeadTimeTable(const size_t &nspec, std::vector< double > &deadTimes)
Create a simple dead time TableWorkspace with two columns (spectrum number and dead time).
Mantid::API::MatrixWorkspace_sptr createWorkspaceWithInstrumentandRun(const std::string &instrName, int runNumber, size_t nSpectra=1)
Creates a single-point workspace with instrument and runNumber set.
Mantid::DataObjects::Workspace2D_sptr create2DWorkspaceFromFunction(fT yFunc, int nSpec, double x0, double x1, double dx, bool isHist=false, gT eFunc=ReturnOne())
Creates a 2D workspace from taking the function values from the input function.
std::string to_string(const wide_integer< Bits, Signed > &n)
double operator()(const double t, size_t spec)