Mantid
Loading...
Searching...
No Matches
SaveBankScatteringAngles.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
14
15#include <cmath>
16#include <fstream>
17
18namespace {
19
20double radToDeg(const double rad) {
21 const double radToDegFactor = 180 / M_PI;
22 return rad * radToDegFactor;
23}
24} // anonymous namespace
25
26namespace Mantid::DataHandling {
27
28using namespace API;
29
30DECLARE_ALGORITHM(SaveBankScatteringAngles)
31
32const std::string SaveBankScatteringAngles::name() const { return "SaveBankScatteringAngles"; }
33
34const std::string SaveBankScatteringAngles::summary() const {
35 return "Save scattering angles two-theta and phi for each workspace in a "
36 "GroupWorkspace of focused banks";
37}
38
39int SaveBankScatteringAngles::version() const { return 1; }
40
41const std::vector<std::string> SaveBankScatteringAngles::seeAlso() const { return {"SaveFocusedXYE"}; }
42
43const std::string SaveBankScatteringAngles::category() const { return "DataHandling\\Text;Diffraction\\DataHandling"; }
44
45const std::string SaveBankScatteringAngles::PROP_FILENAME = "Filename";
46
47const std::string SaveBankScatteringAngles::PROP_INPUT_WS = "InputWorkspace";
48
51 "A GroupWorkspace where every sub-workspace is a "
52 "single-spectra focused run corresponding to a particular "
53 "bank");
54
55 const static std::vector<std::string> exts{".txt", ".new"};
56 declareProperty(std::make_unique<FileProperty>(PROP_FILENAME, "", FileProperty::Save, exts),
57 "The name of the file to save to");
58}
59
61 const std::string filename = getProperty(PROP_FILENAME);
62 std::ofstream outFile(filename.c_str());
63
64 if (!outFile) {
65 g_log.error(strerror(errno));
66 throw Kernel::Exception::FileError("Unable to create file: ", filename);
67 }
68
69 outFile << std::fixed << std::setprecision(10);
70
72 for (int i = 0; i < inputWS->getNumberOfEntries(); ++i) {
73 const auto ws = inputWS->getItem(i);
74 const auto matrixWS = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
75 const auto &instrument = matrixWS->getInstrument();
76 const auto &samplePosition = instrument->getSample()->getPos();
77 const auto &sourcePosition = instrument->getSource()->getPos();
78 const auto &detector = matrixWS->getDetector(0);
79 const auto &detectorPosition = detector->getPos();
80
81 const auto beamVector = samplePosition - sourcePosition;
82 const auto detectorVector = detectorPosition - samplePosition;
83
84 const auto twoTheta = radToDeg(beamVector.angle(detectorVector));
85 const auto phi = radToDeg(detector->getPhi());
86 const auto group = matrixWS->getSpectrum(0).getSpectrumNo();
87
88 outFile << "bank : " << i << " "
89 << "group: " << group << " " << twoTheta << " " << phi << '\n';
90 }
91
92 outFile.close();
93
94 if (outFile.fail()) {
95 g_log.error(strerror(errno));
96 throw Kernel::Exception::FileError("Failed to save file", filename);
97 }
98}
99
100std::map<std::string, std::string> SaveBankScatteringAngles::validateInputs() {
101 std::map<std::string, std::string> issues;
102
104 for (const auto &ws : *inputWS) {
105 const auto matrixWS = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
106 if (matrixWS) {
107 if (matrixWS->getNumberHistograms() != 1) {
108 issues[PROP_INPUT_WS] = "The workspace " + matrixWS->getName() +
109 " has the wrong number of histograms. It "
110 "should contain data for a single focused "
111 "spectra";
112 return issues;
113 }
114 } else {
115 issues[PROP_INPUT_WS] =
116 "The workspace " + ws->getName() + " is of the wrong type. It should be a MatrixWorkspace";
117 return issues;
118 }
119 }
120 return issues;
121}
122
123} // namespace Mantid::DataHandling
#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
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
A property class for workspaces.
std::map< std::string, std::string > validateInputs() override
Perform validation of ALL the input properties of the algorithm.
void init() override
Virtual method - must be overridden by concrete algorithm.
void exec() override
Virtual method - must be overridden by concrete algorithm.
const std::string summary() const override
function returns a summary message that will be displayed in the default GUI, and in the help.
const std::vector< std::string > seeAlso() const override
Function to return all of the seeAlso (these are not validated) algorithms related to this algorithm....
int version() const override
function to return a version of the algorithm, must be overridden in all algorithms
const std::string category() const override
function to return a category of the algorithm.
Records the filename and the description of failure.
Definition: Exception.h:98
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53