Mantid
Loading...
Searching...
No Matches
MonitorEfficiencyCorUser.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 +
10#include "MantidAPI/Run.h"
16#include "MantidHistogramData/Histogram.h"
17#include "MantidHistogramData/HistogramMath.h"
20
21namespace Mantid::Algorithms {
22
23using namespace Kernel;
24using namespace API;
25using namespace Geometry;
26using namespace DataObjects;
27using namespace HistogramData;
28
29// Register the algorithm into the AlgorithmFactory
30DECLARE_ALGORITHM(MonitorEfficiencyCorUser)
31
32
36 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input,
37 std::make_shared<InstrumentValidator>()),
38 "The workspace to correct for monitor efficiency");
39 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
40 "The name of the workspace in which to store the result.");
41}
42
44 m_inputWS = this->getProperty("InputWorkspace");
45
46 m_outputWS = this->getProperty("OutputWorkspace");
47
48 // If input and output workspaces are not the same, create a new workspace for
49 // the output
50 if (m_outputWS != this->m_inputWS) {
51 m_outputWS = create<MatrixWorkspace>(*m_inputWS);
52 }
53 m_Ei = m_inputWS->run().getPropertyValueAsType<double>("Ei");
54
55 std::string mon_counts_log;
56
57 // get name of the monitor counts sample log from the instrument parameter
58 // file
59 try {
60 mon_counts_log = getValFromInstrumentDef("monitor_counts_log");
62 // the default value is monitor_counts
63 mon_counts_log = "monitor_counts";
64 }
65
66 m_monitorCounts = m_inputWS->run().getPropertyValueAsType<double>(mon_counts_log);
67
68 // get Efficiency formula from the IDF - Parameters file
69 const std::string effFormula = getValFromInstrumentDef("formula_mon_eff");
70
71 // Calculate Efficiency for E = Ei
72 const double eff0 = m_monitorCounts * calculateFormulaValue(effFormula, m_Ei);
73
74 // Calculate the number of spectra in this workspace
75 const auto numberOfSpectra = static_cast<int>(this->m_inputWS->getNumberHistograms());
76 API::Progress prog(this, 0.0, 1.0, numberOfSpectra);
77 auto numberOfSpectra_i = static_cast<int64_t>(numberOfSpectra); // cast to make openmp happy
78
79 // Loop over the histograms (detector spectra)
80 double factor = 1 / eff0;
82 for (int64_t i = 0; i < numberOfSpectra_i; ++i) {
84 m_outputWS->setHistogram(i, m_inputWS->histogram(i) * factor);
85
86 prog.report("Detector Efficiency correction...");
88 } // end for i
90
91 setProperty("OutputWorkspace", m_outputWS);
92}
93
100double MonitorEfficiencyCorUser::calculateFormulaValue(const std::string &formula, double energy) {
101 try {
102 mu::Parser p;
103 p.DefineVar("e", &energy);
104 p.SetExpr(formula);
105 double eff = p.Eval();
106 g_log.debug() << "Formula: " << formula << " with: " << energy << "evaluated to: " << eff << '\n';
107 return eff;
108
109 } catch (mu::Parser::exception_type &e) {
111 "Error calculating formula from string. Muparser error message is: " + e.GetMsg());
112 }
113}
114
120std::string MonitorEfficiencyCorUser::getValFromInstrumentDef(const std::string &parameterName) {
121
122 const ParameterMap &pmap = m_inputWS->constInstrumentParameters();
123 Instrument_const_sptr instrument = m_inputWS->getInstrument();
124 Parameter_sptr par = pmap.getRecursive(instrument->getChild(0).get(), parameterName);
125 if (par) {
126 std::string ret = par->asString();
127 g_log.debug() << "Parsed parameter " << parameterName << ": " << ret << "\n";
128 return ret;
129 } else {
130 throw Kernel::Exception::InstrumentDefinitionError("There is no <" + parameterName +
131 "> in the instrument definition!");
132 }
133}
134
135} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double energy
Definition: GetAllEi.cpp:157
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
Definition: MultiThreaded.h:94
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
API::MatrixWorkspace_const_sptr m_inputWS
The user selected (input) workspace.
double calculateFormulaValue(const std::string &, double)
Calculate the value of a formula.
double m_Ei
stores the incident energy of the neutrons
std::string getValFromInstrumentDef(const std::string &)
Returns the value associated to a parameter name in the IDF.
API::MatrixWorkspace_sptr m_outputWS
The output workspace, maybe the same as the input one.
double m_monitorCounts
stores the total count of neutrons from the monitor
Exception for errors associated with the instrument definition.
Definition: Exception.h:220
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
std::shared_ptr< Parameter > Parameter_sptr
Typedef for the shared pointer.
Definition: Parameter.h:195
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
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