Mantid
Loading...
Searching...
No Matches
LoadEventAndCompress.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 +
16
18
19using std::size_t;
20using std::string;
21using namespace Kernel;
22using namespace API;
23using namespace DataObjects;
24
25// Register the algorithm into the AlgorithmFactory
26DECLARE_ALGORITHM(LoadEventAndCompress)
27
28//----------------------------------------------------------------------------------------------
29
30
31const string LoadEventAndCompress::name() const { return "LoadEventAndCompress"; }
32
34int LoadEventAndCompress::version() const { return 1; }
35
37const string LoadEventAndCompress::category() const { return "Workflow\\DataHandling"; }
38
40const string LoadEventAndCompress::summary() const { return "Load an event workspace by chunks and compress"; }
41
42//----------------------------------------------------------------------------------------------
46 // algorithms to copy properties from
47 auto algLoadEventNexus = AlgorithmManager::Instance().createUnmanaged("LoadEventNexus");
48 algLoadEventNexus->initialize();
49 auto algDetermineChunking = AlgorithmManager::Instance().createUnmanaged("DetermineChunking");
50 algDetermineChunking->initialize();
51
52 // declare properties
53 copyProperty(algLoadEventNexus, "Filename");
54 copyProperty(algLoadEventNexus, "OutputWorkspace");
55 copyProperty(algDetermineChunking, "MaxChunkSize");
56 declareProperty("CompressTOFTolerance", .01);
57 copyProperty(algLoadEventNexus, "CompressBinningMode");
58 setPropertyGroup("CompressBinningMode", ""); // unset the group from LoadEventNexus
59
60 copyProperty(algLoadEventNexus, "FilterByTofMin");
61 copyProperty(algLoadEventNexus, "FilterByTofMax");
62 copyProperty(algLoadEventNexus, "FilterByTimeStart");
63 copyProperty(algLoadEventNexus, "FilterByTimeStop");
64
65 std::string grp1 = "Filter Events";
66 setPropertyGroup("FilterByTofMin", grp1);
67 setPropertyGroup("FilterByTofMax", grp1);
68 setPropertyGroup("FilterByTimeStart", grp1);
69 setPropertyGroup("FilterByTimeStop", grp1);
70
71 copyProperty(algLoadEventNexus, "NXentryName");
72 copyProperty(algLoadEventNexus, "LoadMonitors");
73 copyProperty(algLoadEventNexus, "MonitorsLoadOnly");
74 copyProperty(algLoadEventNexus, "FilterMonByTofMin");
75 copyProperty(algLoadEventNexus, "FilterMonByTofMax");
76 copyProperty(algLoadEventNexus, "FilterMonByTimeStart");
77 copyProperty(algLoadEventNexus, "FilterMonByTimeStop");
78
79 setPropertySettings("MonitorsLoadOnly", std::make_unique<VisibleWhenProperty>("LoadMonitors", IS_EQUAL_TO, "1"));
80 auto asEventsIsOn = [] {
81 std::unique_ptr<IPropertySettings> settings =
82 std::make_unique<VisibleWhenProperty>("MonitorsLoadOnly", IS_EQUAL_TO, "1");
83 return settings;
84 };
85 setPropertySettings("FilterMonByTofMin", asEventsIsOn());
86 setPropertySettings("FilterMonByTofMax", asEventsIsOn());
87 setPropertySettings("FilterMonByTimeStart", asEventsIsOn());
88 setPropertySettings("FilterMonByTimeStop", asEventsIsOn());
89
90 std::string grp4 = "Monitors";
91 setPropertyGroup("LoadMonitors", grp4);
92 setPropertyGroup("MonitorsLoadOnly", grp4);
93 setPropertyGroup("FilterMonByTofMin", grp4);
94 setPropertyGroup("FilterMonByTofMax", grp4);
95 setPropertyGroup("FilterMonByTimeStart", grp4);
96 setPropertyGroup("FilterMonByTimeStop", grp4);
97
98 auto range = std::make_shared<BoundedValidator<double>>();
99 range->setBounds(0., 100.);
100 declareProperty("FilterBadPulses", 95., range);
101}
102
105 double maxChunkSize = getProperty("MaxChunkSize");
106
107 auto alg = createChildAlgorithm("DetermineChunking");
108 alg->setProperty("Filename", filename);
109 alg->setProperty("MaxChunkSize", maxChunkSize);
110 alg->executeAsChildAlg();
111 ITableWorkspace_sptr chunkingTable = alg->getProperty("OutputWorkspace");
112
113 if (chunkingTable->rowCount() > 1)
114 g_log.information() << "Will load data in " << chunkingTable->rowCount() << " chunks\n";
115 else
116 g_log.information("Not chunking");
117
118 return chunkingTable;
119}
120
123 g_log.debug() << "loadChunk(" << rowIndex << ")\n";
124
125 auto rowCount = static_cast<double>(m_chunkingTable->rowCount());
126 double progStart{0};
127 double progStop{1};
128 if (rowCount != 0) {
129 progStart = static_cast<double>(rowIndex) / rowCount;
130 progStop = static_cast<double>(rowIndex + 1) / rowCount;
131 }
132
133 auto alg = createChildAlgorithm("LoadEventNexus", progStart, progStop, true);
134 alg->setProperty<string>("Filename", getProperty("Filename"));
135 alg->setProperty<double>("FilterByTofMin", getProperty("FilterByTofMin"));
136 alg->setProperty<double>("FilterByTofMax", getProperty("FilterByTofMax"));
137 alg->setProperty<double>("FilterByTimeStart", getProperty("FilterByTimeStart"));
138 alg->setProperty<double>("FilterByTimeStop", getProperty("FilterByTimeStop"));
139
140 alg->setProperty<string>("NXentryName", getProperty("NXentryName"));
141 alg->setProperty<bool>("LoadMonitors", getProperty("LoadMonitors"));
142 alg->setProperty<string>("MonitorsLoadOnly", getProperty("MonitorsLoadOnly"));
143 alg->setProperty<double>("FilterMonByTofMin", getProperty("FilterMonByTofMin"));
144 alg->setProperty<double>("FilterMonByTofMax", getProperty("FilterMonByTofMax"));
145 alg->setProperty<double>("FilterMonByTimeStart", getProperty("FilterMonByTimeStart"));
146 alg->setProperty<double>("FilterMonByTimeStop", getProperty("FilterMonByTimeStop"));
147 alg->setProperty<double>("CompressTolerance", getProperty("CompressTOFTolerance"));
148 alg->setProperty<string>("CompressBinningMode", getProperty("CompressBinningMode"));
149 alg->setProperty<int>("NumberOfBins", 1);
150
151 if (m_filterBadPulses > 0.)
152 alg->setProperty<double>("FilterBadPulsesLowerCutoff", m_filterBadPulses);
153
154 // determine if loading logs - always load logs for first chunk or
155 // `FilterBadPulses` which will change delete some of the proton_charge log
156 // and change its value
157 bool loadLogs = (rowIndex == 0) || (m_filterBadPulses > 0.);
158 if (!loadLogs) {
159 // logs are needed for any of these
160 const double filterByTimeStart = getProperty("FilterByTimeStart");
161 const double filterByTimeStop = getProperty("FilterByTimeStop");
162 const double filterMonByTimeStart = getProperty("FilterMonByTimeStart");
163 const double filterMonByTimeStop = getProperty("FilterMonByTimeStop");
164 loadLogs = (!isEmpty(filterByTimeStart)) || (!isEmpty(filterByTimeStop)) || (!isEmpty(filterMonByTimeStart)) ||
165 (!isEmpty(filterMonByTimeStop));
166 }
167 alg->setProperty<bool>("LoadLogs", loadLogs);
168
169 // set chunking information
170 if (rowCount > 0.) {
171 const std::vector<string> COL_NAMES = m_chunkingTable->getColumnNames();
172 for (const auto &colName : COL_NAMES) {
173 alg->setProperty(colName, m_chunkingTable->getRef<int>(colName, rowIndex));
174 }
175 }
176
177 alg->executeAsChildAlg();
178 Workspace_sptr wksp = alg->getProperty("OutputWorkspace");
179 return std::dynamic_pointer_cast<MatrixWorkspace>(wksp);
180}
181
182//----------------------------------------------------------------------------------------------
186 const std::string filename = getPropertyValue("Filename");
187 m_filterBadPulses = getProperty("FilterBadPulses");
188
190
191 Progress progress(this, 0.0, 1.0, 2);
192
193 // first run is free
194 progress.report("Loading Chunk");
195 MatrixWorkspace_sptr resultWS = loadChunk(0);
196
197 // load the other chunks
198 const size_t numRows = m_chunkingTable->rowCount();
199
200 progress.resetNumSteps(numRows, 0, 1);
201
202 for (size_t i = 1; i < numRows; ++i) {
204
205 // remove logs
206 auto removeLogsAlg = createChildAlgorithm("RemoveLogs");
207 removeLogsAlg->setProperty("Workspace", temp);
208 removeLogsAlg->executeAsChildAlg();
209 temp = removeLogsAlg->getProperty("Workspace");
210
211 // accumulate data
212 auto plusAlg = createChildAlgorithm("Plus");
213 plusAlg->setProperty("LHSWorkspace", resultWS);
214 plusAlg->setProperty("RHSWorkspace", temp);
215 plusAlg->setProperty("OutputWorkspace", resultWS);
216 plusAlg->setProperty("ClearRHSWorkspace", true);
217 plusAlg->executeAsChildAlg();
218 resultWS = plusAlg->getProperty("OutputWorkspace");
219
220 progress.report();
221 }
222
223 // don't assume that any chunk had the correct binning so just reset it here
224 EventWorkspace_sptr totalEventWS = std::dynamic_pointer_cast<EventWorkspace>(resultWS);
225 if (totalEventWS->getNEvents())
226 totalEventWS->resetAllXToSingleBin();
227
228 // Don't bother compressing combined workspace. DetermineChunking is designed
229 // to prefer loading full banks so no further savings should be available.
230
231 setProperty("OutputWorkspace", resultWS);
232}
233} // namespace Mantid::WorkflowAlgorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
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) override
Create a Child Algorithm.
void copyProperty(const API::Algorithm_sptr &alg, const std::string &name)
Copy a property from an existing algorithm.
Kernel::IPropertyManager::TypedValue getProperty(const std::string &name) const override
Get the property held by this object.
std::string getPropertyValue(const std::string &name) const override
Get the property held by this object.
Helper class for reporting progress from algorithms.
Definition Progress.h:25
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
void resetNumSteps(int64_t nsteps, double start, double end)
Change the number of steps between start/end.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
LoadEventAndCompress : TODO: DESCRIPTION.
API::MatrixWorkspace_sptr loadChunk(const size_t rowIndex) override
void init() override
Initialize the algorithm's properties.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
API::ITableWorkspace_sptr determineChunk(const std::string &filename) override
int version() const override
Algorithm's version for identification.
const std::string category() const override
Algorithm's category for identification.
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< EventWorkspace > EventWorkspace_sptr
shared pointer to the EventWorkspace class