Mantid
Loading...
Searching...
No Matches
CreateLogTimeCorrection.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 +
11#include "MantidAPI/Run.h"
12#include "MantidAPI/TableRow.h"
15
16#include <fstream>
17
18using namespace Mantid;
19using namespace Mantid::API;
20using namespace Mantid::DataObjects;
21using namespace Mantid::Geometry;
22using namespace Mantid::Kernel;
23
24using namespace std;
25
26namespace Mantid::Algorithms {
27
28DECLARE_ALGORITHM(CreateLogTimeCorrection)
29
30//----------------------------------------------------------------------------------------------
34 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input,
35 std::make_shared<InstrumentValidator>()),
36 "Name of the input workspace to generate log correct from.");
37
38 declareProperty(std::make_unique<WorkspaceProperty<TableWorkspace>>("OutputWorkspace", "", Direction::Output),
39 "Name of the output workspace containing the corrections.");
40
41 declareProperty(std::make_unique<FileProperty>("OutputFilename", "", FileProperty::OptionalSave),
42 "Name of the output time correction file.");
43}
44
45//----------------------------------------------------------------------------------------------
49 // 1. Process input
50 MatrixWorkspace_sptr dataWS = getProperty("InputWorkspace");
51
52 // Check whether the output workspace name is same as input
53 string outwsname = getPropertyValue("OutputWorkspace");
54 if (outwsname == dataWS->getName()) {
55 stringstream errmsg;
56 errmsg << "It is not allowed to use the same name by both input matrix "
57 "workspace and output table workspace.";
58 g_log.error(errmsg.str());
59 throw runtime_error(errmsg.str());
60 }
61
62 const auto &detectorInfo = dataWS->detectorInfo();
63
64 // 2. Log some information
65 logGeometryInformation(detectorInfo);
66
67 // 3. Calculate log time correction
68 auto corrections = calculateCorrections(detectorInfo);
69
70 // 4. Output
71 TableWorkspace_sptr outWS = generateCorrectionTable(detectorInfo, corrections);
72 setProperty("OutputWorkspace", outWS);
73
74 string filename = getProperty("OutputFilename");
75 g_log.information() << "Output file name is " << filename << ".\n";
76 if (!filename.empty()) {
77 writeCorrectionToFile(filename, detectorInfo, corrections);
78 }
79}
80
81//----------------------------------------------------------------------------------------------
85
86 g_log.information() << "Sample position = " << detectorInfo.samplePosition() << "; "
87 << "Source position = " << detectorInfo.sourcePosition() << ", L1 = " << detectorInfo.l1() << "; "
88 << "Number of detector/pixels = " << detectorInfo.size() << ".\n";
89}
90
91//----------------------------------------------------------------------------------------------
96std::vector<double> CreateLogTimeCorrection::calculateCorrections(const Geometry::DetectorInfo &detectorInfo) const {
97
98 std::vector<double> corrections(detectorInfo.size());
99 const double l1 = detectorInfo.l1();
100 for (size_t detectorIndex = 0; detectorIndex < detectorInfo.size(); ++detectorIndex) {
101
102 double corrfactor = l1 / (l1 + detectorInfo.l2(detectorIndex));
103 corrections[detectorIndex] = corrfactor;
104 }
105 return corrections;
106}
107
108//----------------------------------------------------------------------------------------------
112 const std::vector<double> &corrections) const {
113 auto tablews = std::make_shared<TableWorkspace>();
114
115 tablews->addColumn("int", "DetectorID");
116 tablews->addColumn("double", "Correction");
117 tablews->addColumn("double", "L2");
118
119 const auto &detectorIds = detectorInfo.detectorIDs();
120
121 for (size_t detectorIndex = 0; detectorIndex < detectorInfo.size(); ++detectorIndex) {
122
123 if (!detectorInfo.isMonitor(detectorIndex)) {
124 const detid_t detid = detectorIds[detectorIndex];
125 const double correction = corrections[detectorIndex];
126 const double l2 = detectorInfo.l2(detectorIndex);
127
128 TableRow newrow = tablews->appendRow();
129 newrow << detid << correction << l2;
130 }
131 }
132
133 return tablews;
134}
135//----------------------------------------------------------------------------------------------
138void CreateLogTimeCorrection::writeCorrectionToFile(const string &filename, const Geometry::DetectorInfo &detectorInfo,
139 const std::vector<double> &corrections) const {
140 ofstream ofile;
141 ofile.open(filename.c_str());
142
143 if (ofile.is_open()) {
144
145 const auto &detectorIds = detectorInfo.detectorIDs();
146 for (size_t detectorIndex = 0; detectorIndex < corrections.size(); ++detectorIndex) {
147 if (!detectorInfo.isMonitor(detectorIndex)) {
148 ofile << detectorIds[detectorIndex] << "\t" << setw(20) << setprecision(5) << corrections[detectorIndex]
149 << "\n";
150 }
151 }
152 ofile.close();
153 } else {
154 g_log.error() << "Unable to open file " << filename << " to write!\n";
155 }
156}
157
158} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IntArray detectorIndex
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
Kernel::Logger & g_log
Definition: Algorithm.h:451
@ OptionalSave
to specify a file to write to but an empty string is
Definition: FileProperty.h:50
TableRow represents a row in a TableWorkspace.
Definition: TableRow.h:39
A property class for workspaces.
CreateLogTimeCorrection : Create correction file and workspace to correct event time against recorded...
DataObjects::TableWorkspace_sptr generateCorrectionTable(const Geometry::DetectorInfo &detectorInfo, const std::vector< double > &corrections) const
Write L2 map and correction map to a TableWorkspace.
std::vector< double > calculateCorrections(const Geometry::DetectorInfo &detectorInfo) const
Calculate the log time correction for each pixel, i.e., correcton from event time at detector to time...
void writeCorrectionToFile(const std::string &filename, const Geometry::DetectorInfo &detectorInfo, const std::vector< double > &corrections) const
Write correction map to a text file.
void logGeometryInformation(const Geometry::DetectorInfo &detectorInfo) const
Log geometry information.
void exec() override
Implement abstract Algorithm methods.
Geometry::DetectorInfo is an intermediate step towards a DetectorInfo that is part of Instrument-2....
Definition: DetectorInfo.h:49
double l2(const size_t index) const
Returns L2 (distance from sample to spectrum).
Kernel::V3D samplePosition() const
Returns the sample position.
const std::vector< detid_t > & detectorIDs() const
Returns a sorted vector of all detector IDs.
double l1() const
Returns L1 (distance from source to sample).
Kernel::V3D sourcePosition() const
Returns the source position.
size_t size() const
Returns the size of the DetectorInfo, i.e., the number of detectors in the instrument.
bool isMonitor(const size_t index) const
Returns true if the detector is a monitor.
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:77
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
Helper class which provides the Collimation Length for SANS instruments.
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54