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