Mantid
Loading...
Searching...
No Matches
AverageLogData.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 +
9#include "MantidAPI/Run.h"
11
12using namespace Mantid::Kernel;
13using namespace Mantid::API;
14
15namespace Mantid::Algorithms {
16
17// Register the algorithm into the AlgorithmFactory
18DECLARE_ALGORITHM(AverageLogData)
19
20//----------------------------------------------------------------------------------------------
24
25//----------------------------------------------------------------------------------------------
29
30//----------------------------------------------------------------------------------------------
32const std::string AverageLogData::name() const { return "AverageLogData"; }
33
35int AverageLogData::version() const { return 1; }
36
38const std::string AverageLogData::category() const { return "DataHandling\\Logs"; }
39
40//----------------------------------------------------------------------------------------------
44 declareProperty(std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
45 "An input workspace that contains a Sample log property, and "
46 "a proton charge property.");
47 declareProperty("LogName", "", "Name of the log to be averaged");
48 declareProperty("FixZero", true,
49 "If true, the proton charge and the log "
50 "value time series are assumed to start at "
51 "the same moment.");
54}
55
56//----------------------------------------------------------------------------------------------
60 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
61 std::string logname = this->getProperty("LogName");
62 if (logname.empty()) {
63 throw std::runtime_error("Failed to supply a LogName");
64 }
65 if (!inputWS->run().hasProperty(logname)) {
66 throw std::runtime_error("There is no property " + logname + " in the workspace.");
67 }
68
69 auto *slog = dynamic_cast<Kernel::TimeSeriesProperty<double> *>(inputWS->run().getLogData(logname));
70 if (!slog) {
71 throw std::runtime_error("Problem reading property " + logname);
72 }
74 dynamic_cast<Kernel::TimeSeriesProperty<double> *>(inputWS->run().getLogData("proton_charge"));
75 if (!pclog) {
76 throw std::runtime_error("Problem reading the proton charge property");
77 }
78
79 double average(0), error(0), protoncharge(0);
80 double diffSeconds = static_cast<double>((slog->firstTime() - pclog->firstTime()).total_nanoseconds()) * 1e-9;
81 if (getProperty("FixZero")) {
82 diffSeconds = 0.;
83 }
84 std::vector<double> stime = slog->timesAsVectorSeconds();
85 std::vector<double> svalue = slog->valuesAsVector();
86 std::vector<double> pctime = pclog->timesAsVectorSeconds();
87 std::vector<double> pcvalue = pclog->valuesAsVector();
88
89 stime.emplace_back(EMPTY_DBL());
90 svalue.emplace_back(0.0);
91 pctime.emplace_back(EMPTY_DBL() * 1.1); // larger than stime
92 pcvalue.emplace_back(0.0);
93
94 auto isvalue = svalue.begin(), ipctime = pctime.begin(), ipcvalue = pcvalue.begin();
95
96 for (auto istime = stime.begin(); istime < (--stime.end()); ++istime) {
97 // ignore all proton pulses before the lowest time for the log
98 while ((*ipctime) < (*istime) + diffSeconds) {
99 ++ipctime;
100 ++ipcvalue;
101 }
102 // add together proton pulses before the current log time and the next log
103 // time
104 while ((*ipctime) < (*(istime + 1)) + diffSeconds) {
105 protoncharge += (*ipcvalue);
106 average += (*ipcvalue) * (*isvalue);
107 error += (*ipcvalue) * (*isvalue) * (*isvalue);
108 ++ipctime;
109 ++ipcvalue;
110 }
111 ++isvalue;
112 }
113
114 if (protoncharge != 0) {
115 g_log.warning() << "Proton charge is 0. Average and standard deviations are NANs\n";
116 }
117 g_log.debug() << "Sum = " << average << "\nSum squares = " << error << "\nPC = " << protoncharge << '\n';
118 average /= protoncharge;
119 error /= protoncharge;
120 error = std::sqrt(std::fabs(error - average * average));
121
122 g_log.information() << "Average value of " << logname << " is " << average << " +/- " << error << '\n';
123 setProperty("Average", average);
124 setProperty("Error", error);
125}
126
127} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double error
Definition: IndexPeaks.cpp:133
int64_t total_nanoseconds(const DateAndTime &self)
Definition: DateAndTime.cpp:33
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
Kernel::Logger & g_log
Definition: Algorithm.h:451
A property class for workspaces.
~AverageLogData() override
Destructor.
const std::string name() const override
Algorithm's name for identification.
const std::string category() const override
Algorithm's category for identification.
int version() const override
Algorithm's version for identification.
void init() override
Initialize the algorithm's properties.
void exec() override
Execute the algorithm.
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 warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
A specialised Property class for holding a series of time-value pairs.
std::vector< TYPE > valuesAsVector() const
Return the time series's values (unfiltered) as a vector<TYPE>
Types::Core::DateAndTime firstTime() const
Returns the first time regardless of filter.
std::vector< double > timesAsVectorSeconds() const
Return the series as list of times, where the time is the number of seconds since the start.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54