Mantid
Loading...
Searching...
No Matches
SaveCalFile.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 +
10#include <cmath>
11#include <fstream>
12
13using namespace Mantid::Kernel;
14using namespace Mantid::API;
15using namespace Mantid::DataObjects;
16using namespace Mantid::Geometry;
17using Mantid::Types::Core::DateAndTime;
18
19namespace Mantid::DataHandling {
20
21// Register the algorithm into the AlgorithmFactory
23
24//----------------------------------------------------------------------------------------------
27void SaveCalFile::init() {
28 declareProperty(std::make_unique<WorkspaceProperty<GroupingWorkspace>>("GroupingWorkspace", "", Direction::Input,
30 "Optional: An GroupingWorkspace workspace giving the grouping info.");
31
32 declareProperty(std::make_unique<WorkspaceProperty<OffsetsWorkspace>>("OffsetsWorkspace", "", Direction::Input,
34 "Optional: An OffsetsWorkspace workspace giving the detector calibration "
35 "values.");
36
37 declareProperty(
38 std::make_unique<WorkspaceProperty<MaskWorkspace>>("MaskWorkspace", "", Direction::Input, PropertyMode::Optional),
39 "Optional: An Workspace workspace giving which detectors are masked.");
40
41 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save, ".cal"),
42 "Path to the .cal file that will be created.");
43
44 auto offsetprecision = std::make_shared<BoundedValidator<int>>();
45 offsetprecision->setLower(7);
46 offsetprecision->setUpper(11);
47 declareProperty("OffsetPrecision", 7, offsetprecision, "Precision of offsets (between 7 and 11 decimal).");
48}
49
50//----------------------------------------------------------------------------------------------
54 GroupingWorkspace_sptr groupWS = getProperty("GroupingWorkspace");
55 OffsetsWorkspace_sptr offsetsWS = getProperty("OffsetsWorkspace");
56 MaskWorkspace_sptr maskWS = getProperty("MaskWorkspace");
57 std::string Filename = getPropertyValue("Filename");
58 m_precision = getProperty("OffsetPrecision");
59
60 // Do the saving
61 SaveCalFile::saveCalFile(Filename, groupWS, offsetsWS, maskWS);
62}
63
64//-----------------------------------------------------------------------
75void SaveCalFile::saveCalFile(const std::string &calFileName, const GroupingWorkspace_sptr &groupWS,
76 const OffsetsWorkspace_sptr &offsetsWS, const MaskWorkspace_sptr &maskWS) {
78
79 bool doGroup = false;
80 if (groupWS) {
81 doGroup = true;
82 inst = groupWS->getInstrument();
83 }
84
85 bool doOffsets = false;
86 if (offsetsWS) {
87 doOffsets = true;
88 inst = offsetsWS->getInstrument();
89 }
90
91 bool doMask = false;
92 if (maskWS) {
93 doMask = true;
94 inst = maskWS->getInstrument();
95 if (!inst)
96 g_log.warning() << "Mask workspace " << maskWS->getName() << " has no instrument associated with."
97 << "\n";
98 }
99
100 g_log.information() << "Status: doGroup = " << doGroup << " doOffsets = " << doOffsets << " doMask = " << doMask
101 << "\n";
102
103 if (!inst)
104 throw std::invalid_argument("You must give at least one of the grouping, "
105 "offsets or masking workspaces.");
106
107 // Header of the file
108 std::ofstream fout(calFileName.c_str());
109 fout << "# Calibration file for instrument " << inst->getName() << " written on "
110 << DateAndTime::getCurrentTime().toISO8601String() << ".\n";
111 fout << "# Format: number UDET offset select group\n";
112
113 // Get all the detectors
114 detid2det_map allDetectors;
115 inst->getDetectors(allDetectors);
116 int64_t number = 0;
117
118 detid2det_map::const_iterator it;
119 for (it = allDetectors.begin(); it != allDetectors.end(); ++it) {
120 detid_t detectorID = it->first;
121 // Geometry::IDetector_const_sptr det = it->second;
122
123 // Find the offset, if any
124 double offset = 0.0;
125 if (doOffsets)
126 offset = offsetsWS->getValue(detectorID, 0.0);
127
128 // Find the group, if any
129 int64_t group = 1;
130 if (doGroup)
131 group = static_cast<int64_t>(groupWS->getValue(detectorID, 0.0));
132
133 // Find the selection, if any
134 int selected = 1;
135 if (doMask && (maskWS->isMasked(detectorID)))
136 selected = 0;
137
138 // if(group > 0)
139 fout << std::fixed << std::setw(9) << number << std::fixed << std::setw(15) << detectorID << std::fixed
140 << std::setprecision(m_precision) << std::setw(15) << offset << std::fixed << std::setw(8) << selected
141 << std::fixed << std::setw(8) << group << "\n";
142
143 number++;
144 }
145}
146
147} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
@ Save
to specify a file to write to, the file may or may not exist
A property class for workspaces.
Algorithm to save a 5-column ascii .cal file from to 3 workspaces: a GroupingWorkspace,...
Definition SaveCalFile.h:25
void saveCalFile(const std::string &calFileName, const Mantid::DataObjects::GroupingWorkspace_sptr &groupWS, const Mantid::DataObjects::OffsetsWorkspace_sptr &offsetsWS, const Mantid::DataObjects::MaskWorkspace_sptr &maskWS)
Reads the calibration file.
int m_precision
Offset precision.
Definition SaveCalFile.h:55
void exec() override
Run the algorithm.
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
std::shared_ptr< GroupingWorkspace > GroupingWorkspace_sptr
shared pointer to the GroupingWorkspace class
std::shared_ptr< MaskWorkspace > MaskWorkspace_sptr
shared pointer to the MaskWorkspace class
std::shared_ptr< OffsetsWorkspace > OffsetsWorkspace_sptr
shared pointer to the OffsetsWorkspace class
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
int32_t detid_t
Typedef for a detector ID.
std::map< detid_t, Geometry::IDetector_const_sptr > detid2det_map
Typedef of a map from detector ID to detector shared pointer.
Definition Instrument.h:27
@ Input
An input workspace.
Definition Property.h:53