Mantid
Loading...
Searching...
No Matches
TimeAtSampleStrategyIndirect.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 +
14#include "MantidKernel/Logger.h"
16#include "MantidKernel/V3D.h"
17#include <cmath>
18#include <memory>
19#include <sstream>
20#include <utility>
21
22using namespace Mantid::Kernel;
23using namespace Mantid::Geometry;
24
25namespace Mantid::API {
26
30 : m_spectrumInfo(ws->spectrumInfo()), m_L1s(m_spectrumInfo.l1()),
31 m_beamDir(ws->componentInfo().referenceFrame()->vecPointingAlongBeam()), m_componentInfo(ws->componentInfo()),
32 m_detectorInfo(ws->detectorInfo()) {}
33
37double TimeAtSampleStrategyIndirect::getEfixed(const size_t &workspace_index) const {
38 if (!m_spectrumInfo.hasDetectors(workspace_index)) {
39 throw Kernel::Exception::NotFoundError("TimeAtSampleStrategyIndirect: no detectors for spectrum",
40 std::to_string(workspace_index));
41 }
42 // The first detector, for a spectrum of several
43 const auto detIndex = m_spectrumInfo.spectrumDefinition(workspace_index)[0].first;
44 const auto noEfixed = [&]() {
45 std::stringstream errmsg;
46 errmsg << "Inelastic instrument detector " << m_detectorInfo.detid(detIndex) << " of spectrum " << workspace_index
47 << " does not have EFixed ";
48 return std::runtime_error(errmsg.str());
49 };
50
51 double efixed{0.};
52 // A spectrum of several detectors has no parameters of its own
53 if (m_spectrumInfo.hasUniqueDetector(workspace_index)) {
54 try {
55 const auto values = m_componentInfo.getNumberParameter(detIndex, "Efixed");
56 if (!values.empty()) {
57 efixed = values.front();
58 }
59 } catch (std::runtime_error &) {
60 throw noEfixed();
61 }
62 }
63 if (efixed <= 0.) {
64 throw noEfixed();
65 }
66
67 return efixed;
68}
69
70Correction TimeAtSampleStrategyIndirect::calculate(const size_t &workspace_index) const {
71
72 // A constant among all spectra
73 constexpr double TWO_MEV_OVER_MASS = 2. * PhysicalConstants::meV / PhysicalConstants::NeutronMass;
74
75 if (m_spectrumInfo.isMonitor(workspace_index)) {
76 // use the same math as TimeAtSampleStrategyElastic
77 const double L1m =
79 const double scale = std::abs(m_L1s / L1m);
80 return Correction(scale, 0.);
81 } else {
82 const double efix = this->getEfixed(workspace_index);
83 const double l2 = m_spectrumInfo.l2(workspace_index);
84 const double shift = -1. * l2 / sqrt(efix * TWO_MEV_OVER_MASS);
85
86 // 1.0 * tof + shift
87 constexpr double NO_MULTIPLY_TOF{1.};
88 return Correction(NO_MULTIPLY_TOF, shift);
89 }
90}
91
92} // namespace Mantid::API
bool isMonitor(const size_t index) const
Returns true if the detector(s) associated with the spectrum are monitors.
Kernel::V3D sourcePosition() const
Returns the source position.
bool hasDetectors(const size_t index) const
Returns true if the spectrum is associated with detectors in the instrument.
Kernel::V3D position(const size_t index) const
Returns the position of the spectrum with given index.
double l2(const size_t index) const
Returns L2 (distance from sample to spectrum).
bool hasUniqueDetector(const size_t index) const
Returns true if the spectrum is associated with exactly one detector.
const SpectrumDefinition & spectrumDefinition(const size_t index) const
Returns a const reference to the SpectrumDefinition of the spectrum.
Correction calculate(const size_t &workspace_index) const override
double getEfixed(const size_t &workspace_index) const
This will throw an exception if efixed is not found or negative.
TimeAtSampleStrategyIndirect(std::shared_ptr< const Mantid::API::MatrixWorkspace > ws)
Constructor.
std::vector< double > getNumberParameter(const size_t componentIndex, const std::string &name, bool recursive=true) const
detid_t detid(const size_t index) const
Returns the detector ID for the given logical detector index.
Exception for when an item is not found in a collection.
Definition Exception.h:145
constexpr double scalar_prod(const V3D &v) const noexcept
Calculates the cross product.
Definition V3D.h:281
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
static constexpr double NeutronMass
Mass of the neutron in kg.
static constexpr double meV
1 meV in Joules.
std::string to_string(const wide_integer< Bits, Signed > &n)
The Correction struct to be applied as factor * TOF + offset multiplicativeFactor: TOF correction fac...