Mantid
Loading...
Searching...
No Matches
StatisticsOfPeaksWorkspace.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 +
15#include "MantidKernel/Utils.h"
16
17#include <fstream>
18
19using namespace Mantid::Geometry;
20using namespace Mantid::DataObjects;
21using namespace Mantid::Kernel;
22using namespace Mantid::API;
23using namespace Mantid::PhysicalConstants;
24
25namespace Mantid::Crystal {
26
27// Register the algorithm into the AlgorithmFactory
28DECLARE_ALGORITHM(StatisticsOfPeaksWorkspace)
29
30//----------------------------------------------------------------------------------------------
34
35//----------------------------------------------------------------------------------------------
39 declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>("InputWorkspace", "", Direction::Input),
40 "An input PeaksWorkspace with an instrument.");
41 std::vector<std::string> propOptions;
42 propOptions.reserve(2 * m_pointGroups.size() + 5);
43 std::transform(m_pointGroups.cbegin(), m_pointGroups.cend(), std::back_inserter(propOptions),
44 [](const auto &group) { return group->getSymbol(); });
45 std::transform(m_pointGroups.cbegin(), m_pointGroups.cend(), std::back_inserter(propOptions),
46 [](const auto &group) { return group->getName(); });
47 // Scripts may have Orthorhombic misspelled from past bug in PointGroupFactory
48 propOptions.emplace_back("222 (Orthorombic)");
49 propOptions.emplace_back("mm2 (Orthorombic)");
50 propOptions.emplace_back("2mm (Orthorombic)");
51 propOptions.emplace_back("m2m (Orthorombic)");
52 propOptions.emplace_back("mmm (Orthorombic)");
53 declareProperty("PointGroup", propOptions[0], std::make_shared<StringListValidator>(propOptions),
54 "Which point group applies to this crystal?");
55
56 std::vector<std::string> centeringOptions;
57 const std::vector<ReflectionCondition_sptr> reflectionConditions = getAllReflectionConditions();
58 centeringOptions.reserve(2 * reflectionConditions.size());
59 std::transform(reflectionConditions.cbegin(), reflectionConditions.cend(), std::back_inserter(centeringOptions),
60 [](const auto &condition) { return condition->getSymbol(); });
61 std::transform(reflectionConditions.cbegin(), reflectionConditions.cend(), std::back_inserter(centeringOptions),
62 [](const auto &condition) { return condition->getName(); });
63 declareProperty("LatticeCentering", centeringOptions[0], std::make_shared<StringListValidator>(centeringOptions),
64 "Appropriate lattice centering for the peaks.");
65
66 declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>("OutputWorkspace", "", Direction::Output),
67 "Output PeaksWorkspace");
69 std::make_unique<WorkspaceProperty<ITableWorkspace>>("StatisticsTable", "StatisticsTable", Direction::Output),
70 "An output table workspace for the statistics of the peaks.");
71 const std::vector<std::string> sortTypes{"ResolutionShell", "Bank", "RunNumber", "Overall"};
72 declareProperty("SortBy", sortTypes[0], std::make_shared<StringListValidator>(sortTypes),
73 "Sort the peaks by resolution shell in d-Spacing(default), "
74 "bank, run number, or only overall statistics.");
75 const std::vector<std::string> equivTypes{"Mean", "Median"};
76 declareProperty("EquivalentIntensities", equivTypes[0], std::make_shared<StringListValidator>(equivTypes),
77 "Replace intensities by mean(default), "
78 "or median.");
79 declareProperty(std::make_unique<PropertyWithValue<double>>("SigmaCritical", 3.0, Direction::Input),
80 "Removes peaks whose intensity deviates more than "
81 "SigmaCritical from the mean (or median).");
82 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("EquivalentsWorkspace", "EquivalentIntensities",
84 "Output Equivalent Intensities");
85 declareProperty("WeightedZScore", false,
86 "Use weighted ZScore if true.\n"
87 "If false, standard ZScore (default).");
88}
89
90//----------------------------------------------------------------------------------------------
94
95 ws = getProperty("InputWorkspace");
96 std::string sortType = getProperty("SortBy");
97 IPeaksWorkspace_sptr tempWS = WorkspaceFactory::Instance().createPeaks();
98 // Copy over ExperimentInfo from input workspace
99 tempWS->copyExperimentInfoFrom(ws.get());
100
101 // We must sort the peaks
102 std::vector<std::pair<std::string, bool>> criteria;
103 if (sortType.compare(0, 2, "Re") == 0)
104 criteria.emplace_back("DSpacing", false);
105 else if (sortType.compare(0, 2, "Ru") == 0)
106 criteria.emplace_back("RunNumber", true);
107 criteria.emplace_back("BankName", true);
108 criteria.emplace_back("h", true);
109 criteria.emplace_back("k", true);
110 criteria.emplace_back("l", true);
111 ws->sort(criteria);
112
113 // =========================================
114 std::vector<Peak> peaks = ws->getPeaks();
115 doSortHKL(ws, "Overall");
116 if (sortType.compare(0, 2, "Ov") == 0)
117 return;
118 // run number
119 std::string oldSequence;
120 if (sortType.compare(0, 2, "Re") == 0) {
121 double dspacing = peaks[0].getDSpacing();
122 if (dspacing > 3.0)
123 oldSequence = "Inf - 3.0";
124 else if (dspacing > 2.5)
125 oldSequence = "3.0 - 2.5";
126 else if (dspacing > 2.0)
127 oldSequence = "2.5 - 2.0";
128 else if (dspacing > 1.5)
129 oldSequence = "2.0 - 1.5";
130 else if (dspacing > 1.0)
131 oldSequence = "1.5 - 1.0";
132 else if (dspacing > 0.5)
133 oldSequence = "1.0 - 0.5";
134 else
135 oldSequence = "0.5 - 0.0";
136 } else if (sortType.compare(0, 2, "Ru") == 0)
137 oldSequence = std::to_string(peaks[0].getRunNumber());
138 else
139 oldSequence = peaks[0].getBankName();
140 // Go through each peak at this run / bank
141 for (int wi = 0; wi < ws->getNumberPeaks(); wi++) {
142
143 Peak &p = peaks[wi];
144 std::string sequence;
145 if (sortType.compare(0, 2, "Re") == 0) {
146 double dspacing = p.getDSpacing();
147 if (dspacing > 3.0)
148 sequence = "Inf - 3.0";
149 else if (dspacing > 2.5)
150 sequence = "3.0 - 2.5";
151 else if (dspacing > 2.0)
152 sequence = "2.5 - 2.0";
153 else if (dspacing > 1.5)
154 sequence = "2.0 - 1.5";
155 else if (dspacing > 1.0)
156 sequence = "1.5 - 1.0";
157 else if (dspacing > 0.5)
158 sequence = "1.0 - 0.5";
159 else
160 sequence = "0.5 - 0.0";
161 } else if (sortType.compare(0, 2, "Ru") == 0)
162 sequence = std::to_string(p.getRunNumber());
163 else
164 sequence = p.getBankName();
165
166 if (sequence != oldSequence && tempWS->getNumberPeaks() > 0) {
167 if (tempWS->getNumberPeaks() > 1)
168 doSortHKL(tempWS, oldSequence);
169 tempWS = WorkspaceFactory::Instance().createPeaks();
170 // Copy over ExperimentInfo from input workspace
171 tempWS->copyExperimentInfoFrom(ws.get());
172 oldSequence = sequence;
173 }
174 tempWS->addPeak(p);
175 }
176 doSortHKL(tempWS, oldSequence);
177}
178//----------------------------------------------------------------------------------------------
184void StatisticsOfPeaksWorkspace::doSortHKL(const Mantid::API::Workspace_sptr &ws, const std::string &runName) {
185 std::string pointGroup = getPropertyValue("PointGroup");
186 std::string latticeCentering = getPropertyValue("LatticeCentering");
187 std::string wkspName = getPropertyValue("OutputWorkspace");
188 std::string tableName = getPropertyValue("StatisticsTable");
189 std::string equivalentIntensities = getPropertyValue("EquivalentIntensities");
190 double sigmaCritical = getProperty("SigmaCritical");
191 bool weightedZ = getProperty("WeightedZScore");
192 auto statsAlg = createChildAlgorithm("SortHKL");
193 statsAlg->setProperty("InputWorkspace", ws);
194 statsAlg->setPropertyValue("OutputWorkspace", wkspName);
195 statsAlg->setPropertyValue("StatisticsTable", tableName);
196 statsAlg->setProperty("PointGroup", pointGroup);
197 statsAlg->setProperty("LatticeCentering", latticeCentering);
198 statsAlg->setProperty("RowName", runName);
199 if (runName != "Overall")
200 statsAlg->setProperty("Append", true);
201 statsAlg->setPropertyValue("EquivalentIntensities", equivalentIntensities);
202 statsAlg->setProperty("SigmaCritical", sigmaCritical);
203 statsAlg->setProperty("WeightedZScore", weightedZ);
204 statsAlg->executeAsChildAlg();
205 PeaksWorkspace_sptr statsWksp = statsAlg->getProperty("OutputWorkspace");
206 ITableWorkspace_sptr tablews = statsAlg->getProperty("StatisticsTable");
207 MatrixWorkspace_sptr equivws = statsAlg->getProperty("EquivalentsWorkspace");
208 if (runName == "Overall")
209 setProperty("OutputWorkspace", statsWksp);
210 setProperty("StatisticsTable", tablews);
211 setProperty("EquivalentsWorkspace", equivws);
212}
213
214} // namespace Mantid::Crystal
#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
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
A property class for workspaces.
std::vector< Mantid::Geometry::PointGroup_sptr > m_pointGroups
Point Groups possible.
void doSortHKL(const Mantid::API::Workspace_sptr &ws, const std::string &runName)
Runs SortHKL on workspace.
void init() override
Initialise the properties.
int getRunNumber() const override
Return the run number this peak was measured at.
Definition: BasePeak.cpp:78
Structure describing a single-crystal peak.
Definition: Peak.h:34
double getDSpacing() const override
Calculate the d-spacing of the peak, in 1/Angstroms
Definition: Peak.cpp:416
std::string getBankName() const
Find the name of the bank that is the parent of the detector.
Definition: Peak.cpp:353
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
The concrete, templated class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< IPeaksWorkspace > IPeaksWorkspace_sptr
shared pointer to Mantid::API::IPeaksWorkspace
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< PeaksWorkspace > PeaksWorkspace_sptr
Typedef for a shared pointer to a peaks workspace.
MANTID_GEOMETRY_DLL const ReflectionConditions & getAllReflectionConditions()
MANTID_GEOMETRY_DLL std::vector< PointGroup_sptr > getAllPointGroups()
Definition: PointGroup.cpp:195
A namespace containing physical constants that are required by algorithms and unit routines.
Definition: Atom.h:14
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