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
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
119 std::shared_ptr<Geometry::Instrument> inst1 = std::make_shared<Geometry::Instrument>();
120 inst1->setName("EMU");
121
122 for (int period = 1; period < nPeriods + 1; period++) {
123 // Period 1 yvalues : 1,2,3,4,5,6,7,8,9,10
124 // Period 2 yvalues : 2,3,4,5,6,7,8,9,10,11 etc..
125 MatrixWorkspace_sptr ws = createCountsWorkspace(nspec, maxt, period);
126
127 wsGroup->addWorkspace(ws);
128 std::string wsName = wsNameStem + std::to_string(period);
129 AnalysisDataService::Instance().addOrReplace(wsName, ws);
130 }
131
132 return wsGroup;
133}
134
135Mantid::API::WorkspaceGroup_sptr createMultiPeriodAsymmetryData(const int &nPeriods, size_t nspec, size_t maxt,
136 const std::string &wsGroupName) {
137 Mantid::API::WorkspaceGroup_sptr wsGroup = std::make_shared<Mantid::API::WorkspaceGroup>();
138 Mantid::API::AnalysisDataService::Instance().addOrReplace(wsGroupName, wsGroup);
139
140 std::string wsNameStem = "MuonDataPeriod_";
141
142 std::shared_ptr<Mantid::Geometry::Instrument> inst1 = std::make_shared<Mantid::Geometry::Instrument>();
143 inst1->setName("EMU");
144
145 for (int period = 1; period < nPeriods + 1; period++) {
147 createAsymmetryWorkspace(nspec, maxt, yDataAsymmetry(10.0 * period, 0.1 * period));
148
149 wsGroup->addWorkspace(ws);
150 std::string wsName = wsNameStem + std::to_string(period);
151 Mantid::API::AnalysisDataService::Instance().addOrReplace(wsName, ws);
152 }
153
154 return wsGroup;
155}
156
164ITableWorkspace_sptr createDeadTimeTable(const size_t &nspec, const std::vector<double> &deadTimes) {
165
166 auto deadTimeTable =
167 std::dynamic_pointer_cast<ITableWorkspace>(WorkspaceFactory::Instance().createTable("TableWorkspace"));
168
169 deadTimeTable->addColumn("int", "Spectrum Number");
170 deadTimeTable->addColumn("double", "Dead Time");
171
172 if (deadTimes.size() != nspec) {
173 return deadTimeTable;
174 }
175
176 for (size_t spec = 0; spec < deadTimes.size(); spec++) {
177 TableRow newRow = deadTimeTable->appendRow();
178 newRow << static_cast<int>(spec) + 1;
179 newRow << deadTimes[spec];
180 }
181
182 return deadTimeTable;
183}
184
191ITableWorkspace_sptr createTimeZeroTable(const size_t &numSpec, const std::vector<double> &timeZeros) {
192
193 auto timeZeroTable =
194 std::dynamic_pointer_cast<ITableWorkspace>(WorkspaceFactory::Instance().createTable("TableWorkspace"));
195
196 timeZeroTable->addColumn("double", "time zero");
197
198 if (timeZeros.size() != numSpec) {
199 return timeZeroTable;
200 }
201
202 for (size_t specNum = 0; specNum < numSpec; ++specNum) {
203 TableRow row = timeZeroTable->appendRow();
204 row << timeZeros[specNum];
205 }
206
207 return timeZeroTable;
208}
209
217MatrixWorkspace_sptr createWorkspaceWithInstrumentandRun(const std::string &instrName, int runNumber, size_t nSpectra) {
218
219 Geometry::Instrument_const_sptr instr = std::make_shared<Geometry::Instrument>(instrName);
220 MatrixWorkspace_sptr ws = WorkspaceFactory::Instance().create("Workspace2D", nSpectra, 1, 1);
221 ws->setInstrument(instr);
222 ws->mutableRun().addProperty("run_number", runNumber);
223 return ws;
224}
225
235WorkspaceGroup_sptr createWorkspaceGroupConsecutiveDetectorIDs(const int &nWorkspaces, size_t nspec, size_t maxt,
236 const std::string &wsGroupName) {
237
238 WorkspaceGroup_sptr wsGroup = std::make_shared<WorkspaceGroup>();
239 AnalysisDataService::Instance().addOrReplace(wsGroupName, wsGroup);
240
241 std::string wsNameStem = "MuonDataPeriod_";
242
243 for (int period = 1; period < nWorkspaces + 1; period++) {
244 // Period 1 yvalues : 1,2,3,4,5,6,7,8,9,10
245 // Period 2 yvalues : 2,3,4,5,6,7,8,9,10,11 etc..
246 size_t detIDstart = (period - 1) * nspec + 1;
247 MatrixWorkspace_sptr ws = createCountsWorkspace(nspec, maxt, period, detIDstart);
248 wsGroup->addWorkspace(ws);
249
250 std::string wsName = wsNameStem + std::to_string(period);
251 AnalysisDataService::Instance().addOrReplace(wsName, ws);
252 }
253
254 return wsGroup;
255}
256
257} // namespace MuonWorkspaceCreationHelper
std::vector< uint32_t > m_count
sum of all events seen in an individual bin
const double m_phi
Definition LoadEMU.cpp:223
int64_t 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, const 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)