Mantid
Loading...
Searching...
No Matches
NormaliseVanadium.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"
15#include "MantidKernel/Unit.h"
16
17/* Following A.J.Schultz's anvred, scaling the vanadium spectra:
18 */
19
20namespace Mantid::Crystal {
21
22// Register the class into the algorithm factory
23DECLARE_ALGORITHM(NormaliseVanadium)
24
25using namespace Kernel;
26using namespace Geometry;
27using namespace API;
28using namespace DataObjects;
29
31
33 // The input workspace must have an instrument and units of wavelength
34 auto wsValidator = std::make_shared<InstrumentValidator>();
35
36 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsValidator),
37 "The X values for the input workspace must be in units of "
38 "wavelength or TOF");
39 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
40 "Output workspace name");
41
42 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
43 mustBePositive->setLower(0.0);
44 declareProperty("Wavelength", 1.0, mustBePositive, "Normalizes spectra to this wavelength");
45}
46
48 // Retrieve the input workspace
49 m_inputWS = getProperty("InputWorkspace");
50 std::string unitStr = m_inputWS->getAxis(0)->unit()->unitID();
51
52 // Get the input parameters
53 double lambdanorm = getProperty("Wavelength"); // in 1/cm
54
55 MatrixWorkspace_sptr correctionFactors = WorkspaceFactory::Instance().create(m_inputWS);
56
57 const auto numHists = static_cast<int64_t>(m_inputWS->getNumberHistograms());
58 const auto specSize = static_cast<int64_t>(m_inputWS->blocksize());
59
60 // If sample not at origin, shift cached positions.
61 const auto &spectrumInfo = m_inputWS->spectrumInfo();
62 double L1 = spectrumInfo.l1();
63
64 Progress prog(this, 0.0, 1.0, numHists);
65 // Loop over the spectra
66 PARALLEL_FOR_IF(Kernel::threadSafe(*m_inputWS, *correctionFactors))
67 for (int64_t i = 0; i < int64_t(numHists); ++i) {
68 // PARALLEL_START_INTERRUPT_REGION //FIXME: Restore
69
70 // Get a reference to the Y's in the output WS for storing the factors
71 auto &Y = correctionFactors->mutableY(i);
72 auto &E = correctionFactors->mutableE(i);
73
74 // Copy over bin boundaries
75 const auto &inSpec = m_inputWS->getSpectrum(i);
76 correctionFactors->setSharedX(i, inSpec.sharedX());
77 const auto &Yin = inSpec.y();
78 const auto &Ein = inSpec.e();
79
80 // If no detector is found, skip onto the next spectrum
81 if (!spectrumInfo.hasDetectors(i))
82 continue;
83
87 spectrumInfo.getDetectorValues(tof, wl, Kernel::DeltaEMode::Elastic, false, i, pmap);
88 auto timeflight = inSpec.points();
89 if (unitStr == "TOF")
90 wl.fromTOF(timeflight.mutableRawData(), timeflight.mutableRawData(), L1, 0, pmap);
91
92 // Loop through the bins in the current spectrum
93 double lambp = 0;
94 double lambm = 0;
95 double normp = 0;
96 double normm = 0;
97 for (int64_t j = 0; j < specSize; j++) {
98 const double lambda = timeflight[j];
99 if (lambda > lambdanorm) {
100 lambp = lambda;
101 normp = Yin[j];
102 break;
103 }
104 lambm = lambda;
105 normm = Yin[j];
106 }
107 double normvalue = normm + (lambdanorm - lambm) * (normp - normm) / (lambp - lambm);
108 for (int64_t j = 0; j < specSize; j++) {
109 Y[j] = Yin[j] / normvalue;
110 E[j] = Ein[j] / normvalue;
111 }
112
113 prog.report();
114
115 // PARALLEL_END_INTERRUPT_REGION
116 }
117 // PARALLEL_CHECK_INTERRUPT_REGION
118
119 setProperty("OutputWorkspace", correctionFactors);
120}
121
122} // namespace Mantid::Crystal
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
const std::vector< double > * lambda
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
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
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
API::MatrixWorkspace_sptr m_inputWS
A virtual function in which additional properties of an algorithm should be declared.
void exec() override
Execution code.
void init() override
Initialisation code.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
void fromTOF(std::vector< double > &xdata, std::vector< double > &ydata, const double &_l1, const int &_emode, std::initializer_list< std::pair< const UnitParams, double > > params)
Convert from time-of-flight to the concrete unit.
Definition: Unit.cpp:177
Time of flight in microseconds.
Definition: Unit.h:283
Wavelength in Angstrom.
Definition: Unit.h:302
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::unordered_map< UnitParams, double > UnitParametersMap
Definition: Unit.h:30
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
Definition: MultiThreaded.h:22
Generate a tableworkspace to store the calibration results.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54