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