Mantid
Loading...
Searching...
No Matches
LoadCanSAS1D2.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/Axis.h"
11#include "MantidAPI/Run.h"
18
19#include <Poco/DOM/DOMParser.h>
20#include <Poco/DOM/Document.h>
21#include <Poco/DOM/NodeList.h>
22
23using Poco::XML::Element;
24using Poco::XML::Node;
25using Poco::XML::NodeList;
26
27using namespace Mantid::Kernel;
28using namespace Mantid::API;
29using namespace Mantid::DataObjects;
30
31namespace Mantid::DataHandling {
32
34
35
36void LoadCanSAS1D2::init() {
38 declareProperty(std::make_unique<PropertyWithValue<bool>>("LoadTransmission", false, Direction::Input),
39 "Load the transmission related data from the file if it is present "
40 "(optional, default False).");
41}
42
45 bool loadTrans = getProperty("LoadTransmission");
46 if (!loadTrans)
47 return; // all done. It is not to load the transmission, nor check if it
48 // exists.
49 if (trans_gp.empty() && trans_can_gp.empty()) {
50 return; // all done, not transmission inside
51 }
52
53 std::string out_wsname = this->getProperty("OutputWorkspace");
54 processTransmission(trans_gp, "sample", out_wsname);
55 processTransmission(trans_can_gp, "can", out_wsname);
56}
57
67void LoadCanSAS1D2::processTransmission(const std::vector<MatrixWorkspace_sptr> &trans_gp,
68 const std::string &trans_name, const std::string &output_name) {
69
70 std::string trans_wsname = std::string(output_name).append("_trans_").append(trans_name);
71 const std::string fileName = getPropertyValue("Filename");
72
73 std::string propertyWS;
74 if (trans_name == "sample")
75 propertyWS = "TransmissionWorkspace";
76 else
77 propertyWS = "TransmissionCanWorkspace";
78 const std::string doc = "The transmission workspace";
79
80 if (trans_gp.size() == 1) {
82 WS->mutableRun().addProperty("Filename", fileName);
83 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(propertyWS, trans_wsname, Direction::Output),
84 doc);
85
86 setProperty(propertyWS, WS);
87 } else if (trans_gp.size() > 1) {
89 for (unsigned int i = 0; i < trans_gp.size(); i++) {
90 MatrixWorkspace_sptr newWork = trans_gp[i];
91
92 newWork->mutableRun().addProperty("Filename", fileName);
93 std::stringstream pname;
94 std::stringstream name;
95 pname << propertyWS << i;
96 name << trans_wsname << i;
97 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(pname.str(), name.str(), Direction::Output),
98 doc);
99 setProperty(pname.str(), newWork);
100 group->addWorkspace(newWork);
101 }
102 std::string pname = std::string(propertyWS).append("GP");
103 declareProperty(std::make_unique<WorkspaceProperty<WorkspaceGroup>>(pname, trans_wsname, Direction::Output), doc);
104 setProperty(pname, group);
105 }
106}
107
119MatrixWorkspace_sptr LoadCanSAS1D2::loadEntry(Poco::XML::Node *const workspaceData, std::string &runName) {
120 MatrixWorkspace_sptr main_out = LoadCanSAS1D::loadEntry(workspaceData, runName);
121 bool loadTrans = getProperty("LoadTransmission");
122 if (!loadTrans)
123 return main_out; // all done. It is not to load the transmission, nor check
124 // if it exists.
125
126 auto *workspaceElem = dynamic_cast<Element *>(workspaceData);
127 // check(workspaceElem, "<SASentry>"); // already done at
128 // LoadCanSAS1D::loadEntry
129 Poco::AutoPtr<NodeList> sasTransList = workspaceElem->getElementsByTagName("SAStransmission_spectrum");
130 if (!sasTransList->length()) {
131 g_log.warning() << "There is no transmission data for this file " << getPropertyValue("Filename") << '\n';
132 return main_out;
133 }
134
135 for (unsigned short trans_index = 0; trans_index < sasTransList->length(); trans_index++) {
136 // foreach SAStransmission_spectrum
137 Node *idataElem = sasTransList->item(trans_index);
138 auto *sasTrasElem = dynamic_cast<Element *>(idataElem);
139 if (!sasTrasElem)
140 continue;
141 std::vector<API::MatrixWorkspace_sptr> &group =
142 (sasTrasElem->getAttribute("name") == "sample") ? trans_gp : trans_can_gp;
143
144 // getting number of Tdata elements in the xml file
145 Poco::AutoPtr<NodeList> tdataElemList = sasTrasElem->getElementsByTagName("Tdata");
146 size_t nBins = tdataElemList->length();
147
148 MatrixWorkspace_sptr dataWS = WorkspaceFactory::Instance().create("Workspace2D", 1, nBins, nBins);
149
150 createLogs(workspaceElem, dataWS);
151
152 std::string title = main_out->getTitle();
153 title += ":trans";
154 title += sasTrasElem->getAttribute("name");
155 dataWS->setTitle(title);
156 dataWS->setDistribution(true);
157 dataWS->setYUnit("");
158
159 // load workspace data
160 auto &X = dataWS->mutableX(0);
161 auto &Y = dataWS->mutableY(0);
162 auto &E = dataWS->mutableE(0);
163 int vecindex = 0;
164 // iterate through each Tdata element and get the values of "Lambda",
165 //"T" and "Tdev" text nodes and fill X,Y,E vectors
166 for (unsigned long index = 0; index < nBins; ++index) {
167 idataElem = tdataElemList->item(index);
168 auto *elem = dynamic_cast<Element *>(idataElem);
169 if (elem) {
170 // setting X vector
171 std::string nodeVal;
172 Element *qElem = elem->getChildElement("Lambda");
173 check(qElem, "Lambda");
174 nodeVal = qElem->innerText();
175 std::stringstream x(nodeVal);
176 double d;
177 x >> d;
178 X[vecindex] = d;
179
180 // setting Y vector
181 Element *iElem = elem->getChildElement("T");
182 check(qElem, "T");
183 nodeVal = iElem->innerText();
184 std::stringstream y(nodeVal);
185 y >> d;
186 Y[vecindex] = d;
187
188 // setting the error vector
189 Element *idevElem = elem->getChildElement("Tdev");
190 if (idevElem) {
191 check(qElem, "Tdev");
192 nodeVal = idevElem->innerText();
193 std::stringstream e(nodeVal);
194 e >> d;
195 E[vecindex] = d;
196 } else {
197 E[vecindex] = std::sqrt(d);
198 }
199
200 ++vecindex;
201 }
202 }
203
204 runLoadInstrument(main_out->getInstrument()->getName(), dataWS);
205 dataWS->getAxis(0)->setUnit("Wavelength");
206
207 // add to group
208 group.emplace_back(dataWS);
209 }
210 return main_out;
211}
212} // namespace Mantid::DataHandling
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#define DECLARE_FILELOADER_ALGORITHM(classname)
DECLARE_FILELOADER_ALGORITHM should be used in place of the standard DECLARE_ALGORITHM macro when wri...
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
Class to hold a set of workspaces.
A property class for workspaces.
This algorithm loads 1 CanSAS1d xml file into a workspace.
Definition: LoadCanSAS1D2.h:55
std::vector< API::MatrixWorkspace_sptr > trans_gp
Definition: LoadCanSAS1D2.h:72
void exec() override
Overwrites Algorithm method.
std::vector< API::MatrixWorkspace_sptr > trans_can_gp
Definition: LoadCanSAS1D2.h:72
void processTransmission(const std::vector< API::MatrixWorkspace_sptr > &trans_gp, const std::string &trans_name, const std::string &output_name)
Process the Transmission workspaces in order to output them to Mantid.
API::MatrixWorkspace_sptr loadEntry(Poco::XML::Node *const workspaceData, std::string &runName) override
Extends the LoadEntry to deal with the possibility of Transmission data.
void exec() override
Overwrites Algorithm method.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
Definition: LoadCanSAS1D.h:42
void runLoadInstrument(const std::string &inst_name, const API::MatrixWorkspace_sptr &localWorkspace)
Run LoadInstrument Child Algorithm.
void init() override
Overwrites Algorithm method.
void check(const Poco::XML::Element *const toCheck, const std::string &name) const
Checks if the pointer to the loaded data is not null or throws if it is.
void createLogs(const Poco::XML::Element *const sasEntry, const API::MatrixWorkspace_sptr &wSpace) const
Loads data into the run log.
virtual API::MatrixWorkspace_sptr loadEntry(Poco::XML::Node *const workspaceData, std::string &runName)
Loads an individual SASentry element into a new workspace.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
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< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54