Mantid
Loading...
Searching...
No Matches
FindDeadDetectors.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
11#include <fstream>
12
13namespace Mantid::Algorithms {
14
15// Register the class into the algorithm factory
16DECLARE_ALGORITHM(FindDeadDetectors)
17
18using namespace Kernel;
19using namespace API;
20
23 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
24 "Name of the input workspace");
25 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
26 "Each histogram from the input workspace maps to a histogram in this\n"
27 "workspace with one value that indicates if there was a dead detector");
28
29 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
30 mustBePositive->setLower(0);
31 declareProperty("DeadThreshold", 0.0, mustBePositive,
32 "The threshold against which to judge if a spectrum belongs to a dead\n"
33 "detector");
34 // As the property takes ownership of the validator pointer, have to take care
35 // to pass in a unique
36 // pointer to each property.
37 declareProperty("LiveValue", 0.0, mustBePositive,
38 "The value to assign to an integrated spectrum flagged as 'live'\n"
39 "(default 0.0)");
40 declareProperty("DeadValue", 100.0, mustBePositive,
41 "The value to assign to an integrated spectrum flagged as 'dead'\n"
42 "(default 100.0)");
43 // EMPTY_DBL() is a tag that tells us that no value has been set and we want
44 // to use the default
45 declareProperty("RangeLower", EMPTY_DBL(),
46 "No bin with a boundary at an x value less than this will be used\n"
47 "in the summation that decides if a detector is 'dead' (default: the\n"
48 "start of each histogram)");
49 declareProperty("RangeUpper", EMPTY_DBL(),
50 "No bin with a boundary at an x value higher than this value will\n"
51 "be used in the summation that decides if a detector is 'dead'\n"
52 "(default: the end of each histogram)");
53 declareProperty("OutputFile", "", "A filename to which to write the list of dead detector UDETs");
54 // This output property will contain the list of UDETs for the dead detectors
55 declareProperty("FoundDead", std::vector<detid_t>(), Direction::Output);
56}
57
63 double deadThreshold = getProperty("DeadThreshold");
64 double liveValue = getProperty("LiveValue");
65 double deadValue = getProperty("DeadValue");
66
67 // Try and open the output file, if specified, and write a header
68 std::ofstream file(getPropertyValue("OutputFile").c_str());
69 file << "Index Spectrum UDET(S)\n";
70
71 // Get the integrated input workspace
72 MatrixWorkspace_sptr integratedWorkspace = integrateWorkspace();
73
74 std::vector<detid_t> deadDets;
75 int countSpec = 0, countDets = 0;
76
77 // iterate over the data values setting the live and dead values
78 g_log.information() << "Marking dead detectors\n";
79 const int64_t numSpec = integratedWorkspace->getNumberHistograms();
80 const auto numSpec_d = static_cast<double>(numSpec);
81 int64_t iprogress_step = numSpec / 100;
82 if (iprogress_step == 0)
83 iprogress_step = 1;
84 for (int64_t i = 0; i < int64_t(numSpec); ++i) {
85 // Spectrum in the integratedWorkspace
86 double &y = integratedWorkspace->mutableY(i)[0];
87 if (y > deadThreshold) {
88 y = liveValue;
89 } else {
90 ++countSpec;
91 y = deadValue;
92 const auto &spec = integratedWorkspace->getSpectrum(i);
93 // Write the spectrum number to file
94 file << i << " " << spec.getSpectrumNo();
95 for (const auto &id : spec.getDetectorIDs()) {
96 // Write the detector ID to file, log & the FoundDead output property
97 file << " " << id;
98 // we could write dead detectors to the log but if they are viewing the
99 // log in the MantidPlot viewer it will crash MantidPlot
100 deadDets.emplace_back(id);
101 ++countDets;
102 }
103 file << '\n';
104 }
105 if (i % iprogress_step == 0) {
106 progress(static_cast<double>(i) / numSpec_d);
108 }
109 }
110
111 g_log.notice() << "Found a total of " << countDets << " 'dead' detectors within " << countSpec
112 << " 'dead' spectra.\n";
113
114 // Assign it to the output workspace property
115 setProperty("OutputWorkspace", integratedWorkspace);
116 setProperty("FoundDead", deadDets);
117
118 // Close the output file
119 file.close();
120}
121
124 g_log.information() << "Integrating input workspace\n";
125
126 auto childAlg = createChildAlgorithm("Integration");
127 // Now execute integration.
128 // pass inputed values straight to Integration, checking must be done there
129 childAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", getProperty("InputWorkspace"));
130 childAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", getProperty("OutputWorkspace"));
131 childAlg->setProperty<double>("RangeLower", getProperty("RangeLower"));
132 childAlg->setProperty<double>("RangeUpper", getProperty("RangeUpper"));
133 childAlg->executeAsChildAlg();
134
135 return childAlg->getProperty("OutputWorkspace");
136}
137
138} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
Kernel::Logger & g_log
Definition: Algorithm.h:451
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
void interruption_point()
This is called during long-running operations, and check if the algorithm has requested that it be ca...
Definition: Algorithm.cpp:1687
A property class for workspaces.
API::MatrixWorkspace_sptr integrateWorkspace()
Run Integration as a Child Algorithm.
void exec() override
Executes the algorithm.
void init() override
Initialisation method.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
Definition: Logger.cpp:95
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
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54