Mantid
Loading...
Searching...
No Matches
NormaliseByCurrent.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/Run.h"
12
13namespace Mantid::Algorithms {
14
15// Register with the algorithm factory
16DECLARE_ALGORITHM(NormaliseByCurrent)
17
18using namespace Kernel;
19using namespace API;
20using namespace DataObjects;
21
23 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
24 "Name of the input workspace");
25 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
26 "Name of the output workspace");
28 std::make_unique<Kernel::PropertyWithValue<bool>>("RecalculatePCharge", false, Kernel::Direction::Input),
29 "Re-integrates the proton charge. This will modify the "
30 "gd_prtn_chrg. Does nothing for multi-period data");
31}
32
44double NormaliseByCurrent::extractCharge(const std::shared_ptr<Mantid::API::MatrixWorkspace> &inputWS,
45 const bool integratePCharge) const {
46 // Get the good proton charge and check it's valid
47 double charge(-1.0);
48 const Run &run = inputWS->run();
49
50 int nPeriods = 0;
51 try {
52 nPeriods = run.getPropertyValueAsType<int>("nperiods");
53 } catch (Exception::NotFoundError &) {
54 g_log.information() << "No nperiods property. If this is multi-period "
55 "data, then you will be normalising against the "
56 "wrong current.\n";
57 }
58 // Handle multiperiod data.
59 // The number of periods is set above by reference
60 if (nPeriods > 1) {
61 // Fetch the period property
62 Property *currentPeriodNumberProperty = run.getLogData("current_period");
63 int periodNumber = std::stoi(currentPeriodNumberProperty->value());
64
65 // Fetch the charge property
66 Property *chargeProperty = run.getLogData("proton_charge_by_period");
67 auto *chargePropertyArray = dynamic_cast<ArrayProperty<double> *>(chargeProperty);
68 if (chargePropertyArray) {
69 charge = chargePropertyArray->operator()()[periodNumber - 1];
70 } else {
71 throw Exception::NotFoundError("Proton charge log (proton_charge_by_period) not found for this "
72 "multiperiod data workspace (" +
73 inputWS->getName() + ")",
74 "proton_charge_by_period");
75 }
76
77 if (charge == 0) {
78 throw std::domain_error("The proton charge found for period number " + std::to_string(periodNumber) +
79 " in the input workspace (" + inputWS->getName() +
80 ") run information is zero. When applying "
81 "NormaliseByCurrent on multiperiod data, a "
82 "non-zero value is required for every period in "
83 "the proton_charge_by_period log.");
84 }
85
86 } else {
87 try {
88 if (integratePCharge) {
89 inputWS->run().integrateProtonCharge();
90 }
91 charge = inputWS->run().getProtonCharge();
92 } catch (Exception::NotFoundError &) {
93 g_log.error() << "The proton charge is not set for the run attached to "
94 "the workspace(" +
95 inputWS->getName() + ")\n";
96 throw;
97 }
98
99 if (charge == 0) {
100 throw std::domain_error("The proton charge found in the input workspace (" + inputWS->getName() +
101 ") run information is zero");
102 }
103 }
104 return charge;
105}
106
108 // Get the input workspace
109 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
110 MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
111 const bool integratePCharge = getProperty("RecalculatePCharge");
112
113 // Get the good proton charge and check it's valid
114 double charge = extractCharge(inputWS, integratePCharge);
115
116 g_log.information() << "Normalisation current: " << charge << " uamps\n";
117
118 double invcharge = 1.0 / charge; // Inverse of the charge to be multiplied by
119
120 // The operator overloads properly take into account of both EventWorkspaces
121 // and doing it in place or not.
122 if (inputWS != outputWS) {
123 outputWS = inputWS * invcharge;
124 setProperty("OutputWorkspace", outputWS);
125 } else {
126 inputWS *= invcharge;
127 }
128 outputWS->mutableRun().addLogData(new Kernel::PropertyWithValue<double>("NormalizationFactor", charge));
129 outputWS->setYUnitLabel("Counts per microAmp.hour");
130}
131
132} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
Kernel::Property * getLogData(const std::string &name) const
Access a single log entry.
Definition: LogManager.h:129
HeldType getPropertyValueAsType(const std::string &name) const
Get the value of a property as the given TYPE.
Definition: LogManager.cpp:332
This class stores information regarding an experimental run as a series of log entries.
Definition: Run.h:38
A property class for workspaces.
void init() override
Virtual method - must be overridden by concrete algorithm.
void exec() override
Virtual method - must be overridden by concrete algorithm.
double extractCharge(const std::shared_ptr< Mantid::API::MatrixWorkspace > &inputWS, const bool integratePCharge) const
Extract a value for the charge from the input workspace.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
Exception for when an item is not found in a collection.
Definition: Exception.h:145
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
The concrete, templated class for properties.
Base class for properties.
Definition: Property.h:94
virtual std::string value() const =0
Returns the value of the property as a string.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54