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"
17#include "MantidHistogramData/Histogram.h"
18#include "MantidHistogramData/HistogramMath.h"
21
22namespace Mantid::Algorithms {
23
24using namespace Kernel;
25using namespace API;
26using namespace Geometry;
27using namespace DataObjects;
28using namespace HistogramData;
29
30// Register the algorithm into the AlgorithmFactory
31DECLARE_ALGORITHM(MonitorEfficiencyCorUser)
32
33
37 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input,
38 std::make_shared<InstrumentValidator>()),
39 "The workspace to correct for monitor efficiency");
40 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
41 "The name of the workspace in which to store the result.");
42}
43
45 m_inputWS = this->getProperty("InputWorkspace");
46
47 m_outputWS = this->getProperty("OutputWorkspace");
48
49 // If input and output workspaces are not the same, create a new workspace for
50 // the output
51 if (m_outputWS != this->m_inputWS) {
52 m_outputWS = create<MatrixWorkspace>(*m_inputWS);
53 }
54 m_Ei = m_inputWS->run().getPropertyValueAsType<double>("Ei");
55
56 std::string mon_counts_log;
57
58 // get name of the monitor counts sample log from the instrument parameter
59 // file
60 try {
61 mon_counts_log = getValFromInstrumentDef("monitor_counts_log");
63 // the default value is monitor_counts
64 mon_counts_log = "monitor_counts";
65 }
66
67 m_monitorCounts = m_inputWS->run().getPropertyValueAsType<double>(mon_counts_log);
68
69 // get Efficiency formula from the IDF - Parameters file
70 const std::string effFormula = getValFromInstrumentDef("formula_mon_eff");
71
72 // Calculate Efficiency for E = Ei
73 const double eff0 = m_monitorCounts * calculateFormulaValue(effFormula, m_Ei);
74
75 // Calculate the number of spectra in this workspace
76 const auto numberOfSpectra = static_cast<int>(this->m_inputWS->getNumberHistograms());
77 API::Progress prog(this, 0.0, 1.0, numberOfSpectra);
78 auto numberOfSpectra_i = static_cast<int64_t>(numberOfSpectra); // cast to make openmp happy
79
80 // Loop over the histograms (detector spectra)
81 double factor = 1 / eff0;
83 for (int64_t i = 0; i < numberOfSpectra_i; ++i) {
85 m_outputWS->setHistogram(i, m_inputWS->histogram(i) * factor);
86
87 prog.report("Detector Efficiency correction...");
89 } // end for i
91
92 setProperty("OutputWorkspace", m_outputWS);
93}
94
101double MonitorEfficiencyCorUser::calculateFormulaValue(const std::string &formula, double energy) {
102 try {
103 mu::Parser p;
104 p.DefineVar("e", &energy);
105 p.SetExpr(formula);
106 double eff = p.Eval();
107 g_log.debug() << "Formula: " << formula << " with: " << energy << "evaluated to: " << eff << '\n';
108 return eff;
109
110 } catch (mu::Parser::exception_type &e) {
112 "Error calculating formula from string. Muparser error message is: " + e.GetMsg());
113 }
114}
115
121std::string MonitorEfficiencyCorUser::getValFromInstrumentDef(const std::string &parameterName) {
122
123 const auto &componentInfo = m_inputWS->componentInfo();
124 // Searched from the instrument's first component, so that parameters on it or on the instrument are found
125 const auto firstComponent = componentInfo.children(componentInfo.root()).front();
126 if (componentInfo.hasParameter(firstComponent, parameterName)) {
127 std::string ret = componentInfo.getParameterAsString(firstComponent, parameterName);
128 g_log.debug() << "Parsed parameter " << parameterName << ": " << ret << "\n";
129 return ret;
130 } else {
131 throw Kernel::Exception::InstrumentDefinitionError("There is no <" + parameterName +
132 "> in the instrument definition!");
133 }
134}
135
136} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:547
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...
#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.
Kernel::Logger & g_log
Definition Algorithm.h:426
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:145
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
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.
@ Input
An input workspace.
Definition Property.h:54
@ Output
An output workspace.
Definition Property.h:55