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 this will cause a crash if they are viewing the log viewer
99 deadDets.emplace_back(id);
100 ++countDets;
101 }
102 file << '\n';
103 }
104 if (i % iprogress_step == 0) {
105 progress(static_cast<double>(i) / numSpec_d);
107 }
108 }
109
110 g_log.notice() << "Found a total of " << countDets << " 'dead' detectors within " << countSpec
111 << " 'dead' spectra.\n";
112
113 // Assign it to the output workspace property
114 setProperty("OutputWorkspace", integratedWorkspace);
115 setProperty("FoundDead", deadDets);
116
117 // Close the output file
118 file.close();
119}
120
123 g_log.information() << "Integrating input workspace\n";
124
125 auto childAlg = createChildAlgorithm("Integration");
126 // Now execute integration.
127 // pass inputed values straight to Integration, checking must be done there
128 childAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", getProperty("InputWorkspace"));
129 childAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", getProperty("OutputWorkspace"));
130 childAlg->setProperty<double>("RangeLower", getProperty("RangeLower"));
131 childAlg->setProperty<double>("RangeUpper", getProperty("RangeUpper"));
132 childAlg->executeAsChildAlg();
133
134 return childAlg->getProperty("OutputWorkspace");
135}
136
137} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
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.
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.
Kernel::Logger & g_log
Definition Algorithm.h:422
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
void interruption_point()
This is called during long-running operations, and check if the algorithm has requested that it be ca...
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:126
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
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:42
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54