Mantid
Loading...
Searching...
No Matches
RebinByTimeBase.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#include "MantidAPI/Axis.h"
9#include "MantidAPI/Run.h"
15#include "MantidKernel/Unit.h"
17
18#include <memory>
19
20namespace Mantid::Algorithms {
21
22using namespace Mantid::Kernel;
23using namespace Mantid::API;
24using Types::Core::DateAndTime;
25
31private:
32 double m_offSet;
33
34public:
35 explicit ConvertToRelativeTime(const DateAndTime &offSet)
36 : m_offSet(static_cast<double>(offSet.totalNanoseconds()) * 1e-9) {}
37 MantidVec::value_type operator()(const MantidVec::value_type &absTNanoSec) { return (absTNanoSec * 1e-9) - m_offSet; }
38};
39
44 std::make_unique<API::WorkspaceProperty<API::IEventWorkspace>>("InputWorkspace", "", Direction::Input),
45 "An input workspace containing TOF events.");
46
47 declareProperty(std::make_unique<ArrayProperty<double>>("Params", std::make_shared<RebinParamsValidator>()),
48 "A comma separated list of first bin boundary, width, last "
49 "bin boundary. Optionally\n"
50 "this can be followed by a comma and more widths and last "
51 "boundary pairs. Values are in seconds since run start.");
52
54 std::make_unique<API::WorkspaceProperty<API::MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
55 "An output workspace.");
56}
57
63 IEventWorkspace_sptr inWS = getProperty("InputWorkspace");
64
65 if (!std::dynamic_pointer_cast<EventWorkspace>(inWS)) {
66 const std::string algName = this->name();
67 throw std::invalid_argument(algName + " Algorithm requires an EventWorkspace as an input.");
68 }
69
70 // retrieve the properties
71 const std::vector<double> inParams = getProperty("Params");
72 std::vector<double> rebinningParams;
73
74 // workspace independent determination of length
75 const auto histnumber = static_cast<int>(inWS->getNumberHistograms());
76
77 const auto nanoSecondsInASecond = static_cast<uint64_t>(1e9);
78 const DateAndTime runStartTime = inWS->run().startTime();
79 // The validator only passes parameters with size 1, or 3xn.
80
81 double tStep = 0;
82 if (inParams.size() >= 3) {
83 // Use the start of the run to offset the times provided by the user. pulse
84 // time of the events are absolute.
85 const DateAndTime startTime = runStartTime + inParams[0];
86 const DateAndTime endTime = runStartTime + inParams[2];
87 // Rebinning params in nanoseconds.
88 rebinningParams.emplace_back(static_cast<double>(startTime.totalNanoseconds()));
89 tStep = inParams[1] * nanoSecondsInASecond;
90 rebinningParams.emplace_back(tStep);
91 rebinningParams.emplace_back(static_cast<double>(endTime.totalNanoseconds()));
92 } else if (inParams.size() == 1) {
93 const uint64_t xmin = getMinX(inWS);
94 const uint64_t xmax = getMaxX(inWS);
95
96 rebinningParams.emplace_back(static_cast<double>(xmin));
97 tStep = inParams[0] * nanoSecondsInASecond;
98 rebinningParams.emplace_back(tStep);
99 rebinningParams.emplace_back(static_cast<double>(xmax));
100 }
101
102 // Validate the timestep.
103 if (tStep <= 0) {
104 throw std::invalid_argument("Cannot have a timestep less than or equal to zero.");
105 }
106
107 // Initialize progress reporting.
108 Progress prog(this, 0.0, 1.0, histnumber);
109
110 MantidVecPtr XValues_new;
111 // create new X axis, with absolute times in seconds.
112 static_cast<void>(VectorHelper::createAxisFromRebinParams(rebinningParams, XValues_new.access()));
113
114 ConvertToRelativeTime transformToRelativeT(runStartTime);
115
116 // Transform the output into relative times in seconds.
117 MantidVec OutXValues_scaled(XValues_new->size());
118 std::transform(XValues_new->begin(), XValues_new->end(), OutXValues_scaled.begin(), transformToRelativeT);
119
120 MatrixWorkspace_sptr outputWS =
121 DataObjects::create<DataObjects::Workspace2D>(*inWS, histnumber, HistogramData::BinEdges(*XValues_new));
122
123 // Copy all the axes
124 for (int i = 1; i < inWS->axes(); i++) {
125 outputWS->replaceAxis(i, std::unique_ptr<Axis>(inWS->getAxis(i)->clone(outputWS.get())));
126 outputWS->getAxis(i)->unit() = inWS->getAxis(i)->unit();
127 }
128
129 // X-unit is relative time since the start of the run.
130 outputWS->getAxis(0)->unit() = std::make_shared<Units::Time>();
131
132 // Copy the units over too.
133 for (int i = 1; i < outputWS->axes(); ++i) {
134 outputWS->getAxis(i)->unit() = inWS->getAxis(i)->unit();
135 }
136 outputWS->setYUnit(inWS->YUnit());
137 outputWS->setYUnitLabel(inWS->YUnitLabel());
138
139 // Assign it to the output workspace property
140 setProperty("OutputWorkspace", outputWS);
141
142 // Go through all the histograms and set the data
143 doHistogramming(inWS, outputWS, XValues_new, OutXValues_scaled, prog);
144}
145
146} // namespace Mantid::Algorithms
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
const std::string name() const override=0
function to return a name of the algorithm, must be overridden in all algorithms
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
Helper method to transform a MantidVector containing absolute times in nanoseconds to relative times ...
MantidVec::value_type operator()(const MantidVec::value_type &absTNanoSec)
ConvertToRelativeTime(const DateAndTime &offSet)
void init() override
Initialization method.
virtual void doHistogramming(Mantid::API::IEventWorkspace_sptr inWS, Mantid::API::MatrixWorkspace_sptr outputWS, Mantid::MantidVecPtr &XValues_new, Mantid::MantidVec &OutXValues_scaled, Mantid::API::Progress &prog)=0
Do the algorithm specific histogramming.
virtual uint64_t getMinX(Mantid::API::IEventWorkspace_sptr ws) const =0
Get the maximum x across all spectra in workspace.
virtual uint64_t getMaxX(Mantid::API::IEventWorkspace_sptr ws) const =0
Get the minimum x across all spectra in workspace.
This class is intended to fulfill the design specified in <https://github.com/mantidproject/documents...
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.
Implements a copy on write data template.
Definition: cow_ptr.h:41
DataType & access()
Access function.
Definition: cow_ptr.h:147
std::shared_ptr< IEventWorkspace > IEventWorkspace_sptr
shared pointer to Mantid::API::IEventWorkspace
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
int MANTID_KERNEL_DLL createAxisFromRebinParams(const std::vector< double > &params, std::vector< double > &xnew, const bool resize_xnew=true, const bool full_bins_only=false, const double xMinHint=std::nan(""), const double xMaxHint=std::nan(""), const bool useReverseLogarithmic=false, const double power=-1)
Creates a new output X array given a 'standard' set of rebinning parameters.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition: cow_ptr.h:172
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54