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