Skip to content

Commit 3b0bfad

Browse files
Add event filtering and loading logs
1 parent a8a69c5 commit 3b0bfad

File tree

1 file changed

+30
-109
lines changed

1 file changed

+30
-109
lines changed

Framework/DataHandling/src/AlignAndFocusPowderSlim.cpp

Lines changed: 30 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "MantidAPI/Axis.h"
1010
#include "MantidAPI/FileProperty.h"
1111
#include "MantidAPI/MatrixWorkspace.h"
12+
#include "MantidAPI/Run.h"
13+
#include "MantidAPI/Sample.h"
1214
#include "MantidDataHandling/LoadEventNexus.h"
1315
#include "MantidDataObjects/EventList.h"
1416
#include "MantidDataObjects/MaskWorkspace.h"
@@ -18,6 +20,7 @@
1820
#include "MantidGeometry/Instrument/DetectorInfo.h"
1921
#include "MantidKernel/ArrayLengthValidator.h"
2022
#include "MantidKernel/ArrayProperty.h"
23+
#include "MantidKernel/TimeSeriesProperty.h"
2124
#include "MantidKernel/Timer.h"
2225
#include "MantidKernel/VectorHelper.h"
2326
#include "MantidNexus/H5Util.h"
@@ -38,6 +41,7 @@ using Mantid::DataObjects::Workspace2D;
3841
using Mantid::Kernel::ArrayLengthValidator;
3942
using Mantid::Kernel::ArrayProperty;
4043
using Mantid::Kernel::Direction;
44+
using Mantid::Kernel::TimeSeriesProperty;
4145

4246
namespace { // anonymous namespace
4347
namespace PropertyNames {
@@ -103,30 +107,15 @@ class NexusLoader {
103107
const std::pair<uint64_t, uint64_t> &eventRange) {
104108
// g_log.information(NxsFieldNames::TIME_OF_FLIGHT);
105109
auto tof_SDS = event_group.openDataSet(NxsFieldNames::TIME_OF_FLIGHT);
106-
// TODO probably should resize data array
107-
/*
108-
// This is the data size
109-
::NeXus::Info id_info = m_h5file.getInfo();
110-
const auto dim0 = static_cast<size_t>(LoadBankFromDiskTask::recalculateDataSize(id_info.dims[0]));
111-
*/
112110

111+
// H5Util resizes the vector
113112
if (m_is_time_filtered) {
114-
throw std::runtime_error("filtering not implemented");
115-
// TODO sort this out H5Util doesn't have slab read yet
116-
/*
117-
// These are the arguments to getSlab()
118-
std::vector<int64_t> loadStart(1, eventRange.first);
119-
int64_t slabsize = (eventRange.second == std::numeric_limits<size_t>::max())
120-
? dim0 - eventRange.first
121-
: eventRange.second - eventRange.first;
122-
std::vector<int64_t> loadSize(1, slabsize);
123-
124-
data->resize(loadSize[0]);
125-
Mantid::NeXus::NeXusIOHelper::readNexusSlab<float, Mantid::NeXus::NeXusIOHelper::PreventNarrowing>(
126-
*data, m_h5file, NxsFieldNames::TIME_OF_FLIGHT, loadStart, loadSize);
127-
*/
113+
const size_t offset = eventRange.first;
114+
const size_t slabsize = (eventRange.second == std::numeric_limits<size_t>::max())
115+
? std::numeric_limits<size_t>::max()
116+
: eventRange.second - eventRange.first;
117+
NeXus::H5Util::readArray1DCoerce(tof_SDS, *data, slabsize, offset);
128118
} else {
129-
// TODO probably should resize data array
130119
NeXus::H5Util::readArray1DCoerce(tof_SDS, *data);
131120
}
132121

@@ -143,30 +132,15 @@ class NexusLoader {
143132
const std::pair<uint64_t, uint64_t> &eventRange) {
144133
// g_log.information(NxsFieldNames::DETID);
145134
auto detID_SDS = event_group.openDataSet(NxsFieldNames::DETID);
146-
// TODO probably should resize data array
147-
/*
148-
// This is the data size
149-
::NeXus::Info id_info = m_h5file.getInfo();
150-
const auto dim0 = static_cast<size_t>(LoadBankFromDiskTask::recalculateDataSize(id_info.dims[0]));
151-
*/
152135

136+
// H5Util resizes the vector
153137
if (m_is_time_filtered) {
154-
throw std::runtime_error("filtering not implemented");
155-
// TODO sort this out H5Util doesn't have slab read yet
156-
/*
157-
// These are the arguments to getSlab()
158-
std::vector<int64_t> loadStart(1, eventRange.first);
159-
int64_t slabsize = (eventRange.second == std::numeric_limits<size_t>::max())
160-
? dim0 - eventRange.first
161-
: eventRange.second - eventRange.first;
162-
std::vector<int64_t> loadSize(1, slabsize);
163-
164-
data->resize(loadSize[0]);
165-
Mantid::NeXus::NeXusIOHelper::readNexusSlab<uint32_t, Mantid::NeXus::NeXusIOHelper::PreventNarrowing>(
166-
*data, m_h5file, NxsFieldNames::DETID, loadStart, loadSize);
167-
*/
138+
const size_t offset = eventRange.first;
139+
const size_t slabsize = (eventRange.second == std::numeric_limits<size_t>::max())
140+
? std::numeric_limits<size_t>::max()
141+
: eventRange.second - eventRange.first;
142+
NeXus::H5Util::readArray1DCoerce(detID_SDS, *data, slabsize, offset);
168143
} else {
169-
// TODO probably should resize data array
170144
NeXus::H5Util::readArray1DCoerce(detID_SDS, *data);
171145
}
172146
}
@@ -452,7 +426,6 @@ void AlignAndFocusPowderSlim::exec() {
452426

453427
// Load the instrument
454428
// prog->doReport("Loading instrument"); TODO add progress bar stuff
455-
// LoadEventNexus::loadInstrument<MatrixWorkspace_sptr>(filename, wksp, "entry", this, &descriptor);
456429
LoadEventNexus::loadInstrument<MatrixWorkspace_sptr>(filename, wksp, ENTRY_TOP_LEVEL, this, &descriptor);
457430

458431
const std::string cal_filename = getPropertyValue(PropertyNames::CAL_FILE);
@@ -463,25 +436,12 @@ void AlignAndFocusPowderSlim::exec() {
463436
}
464437

465438
/*
466-
// load run metadata
467-
// prog->doReport("Loading metadata"); TODO add progress bar stuff
468-
try {
469-
LoadEventNexus::loadEntryMetadata(filename, WS, "entry", descriptor);
470-
} catch (std::exception &e) {
471-
g_log.warning() << "Error while loading meta data: " << e.what() << '\n';
472-
}
473-
474439
// create IndexInfo
475440
// prog->doReport("Creating IndexInfo"); TODO add progress bar stuff
476441
const std::vector<int32_t> range;
477442
LoadEventNexusIndexSetup indexSetup(WS, EMPTY_INT(), EMPTY_INT(), range);
478443
auto indexInfo = indexSetup.makeIndexInfo();
479444
const size_t numHist = indexInfo.size();
480-
481-
// make output workspace with correct number of histograms
482-
MatrixWorkspace_sptr outWS = WorkspaceFactory::Instance().create(WS, numHist, 2, 1);
483-
// set spectrum index information
484-
outWS->setIndexInfo(indexInfo);
485445
*/
486446

487447
// load the events
@@ -561,63 +521,24 @@ void AlignAndFocusPowderSlim::exec() {
561521
m_calibration, m_masked, binWidth, linearBins);
562522
constexpr size_t GRAINSIZE_BANK{2};
563523
tbb::parallel_for(tbb::blocked_range<size_t>(0, bankEntryNames.size(), GRAINSIZE_BANK), task);
524+
}
564525

565-
/*
566-
NexusLoader loader(h5file, is_time_filtered, pulse_start_index, pulse_stop_index);
567-
568-
size_t specnum = 0;
569-
for (const std::string &entry_name : bankEntryNames) {
570-
std::cout << "ENTRY: " << entry_name << std::endl;
571-
const auto startTimeBank = std::chrono::high_resolution_clock::now();
572-
573-
// TODO should re-use vectors to save malloc/free calls
574-
std::unique_ptr<std::vector<uint32_t>> event_detid = std::make_unique<std::vector<uint32_t>>();
575-
std::unique_ptr<std::vector<float>> event_time_of_flight = std::make_unique<std::vector<float>>();
576-
// TODO std::unique_ptr<std::vector<float>> event_weight; some other time
577-
g_log.information() << "Loading bank " << entry_name << '\n';
578-
h5file.openGroup(entry_name, "NXevent_data");
579-
580-
// get filtering range
581-
const auto eventRange = loader.getEventIndexRange(h5file);
582-
583-
loader.loadTOF(event_time_of_flight, eventRange);
584-
loader.loadDetid(event_detid, eventRange);
585-
586-
if (event_time_of_flight->empty() || event_detid->empty()) {
587-
g_log.warning() << "No data for bank " << entry_name << '\n';
588-
h5file.closeGroup();
589-
continue;
590-
}
591-
592-
const auto startTimeSetup = std::chrono::high_resolution_clock::now();
593-
const auto [minval, maxval] = parallel_minmax(event_detid.get());
594-
BankCalibration calibration(static_cast<detid_t>(minval), static_cast<detid_t>(maxval), m_calibration);
595-
596-
auto &spectrum = wksp->getSpectrum(specnum);
597-
Histogrammer histogrammer(&spectrum.readX(), binWidth, linearBins);
598-
const auto numEvent = event_time_of_flight->size();
599-
// std::atomic allows for multi-threaded accumulation and who cares about floats when you are just
600-
// counting things
601-
std::vector<std::atomic_uint32_t> y_temp(spectrum.dataY().size());
602-
addTimer("setup" + entry_name, startTimeSetup, std::chrono::high_resolution_clock::now());
603-
604-
const auto startTimeProcess = std::chrono::high_resolution_clock::now();
605-
constexpr size_t GRAINSIZE_EVENT{2000};
606-
ProcessEventsTask task(&histogrammer, event_detid.get(), event_time_of_flight.get(), &calibration, &y_temp,
607-
&m_masked);
608-
tbb::parallel_for(tbb::blocked_range<size_t>(0, numEvent, GRAINSIZE_EVENT), task);
609-
auto &y_values = spectrum.dataY();
610-
std::copy(y_temp.cbegin(), y_temp.cend(), y_values.begin());
611-
addTimer("proc" + entry_name, startTimeProcess, std::chrono::high_resolution_clock::now());
612-
addTimer(entry_name, startTimeBank, std::chrono::high_resolution_clock::now());
526+
// close the file so child algorithms can do their thing
527+
h5file.close();
613528

614-
h5file.closeGroup();
615-
specnum++;
616-
}
617-
*/
529+
// load run metadata
530+
// prog->doReport("Loading metadata"); TODO add progress bar stuff
531+
try {
532+
LoadEventNexus::loadEntryMetadata(filename, wksp, ENTRY_TOP_LEVEL, descriptor);
533+
} catch (std::exception &e) {
534+
g_log.warning() << "Error while loading meta data: " << e.what() << '\n';
618535
}
619536

620-
// TODO load logs
537+
// load logs
538+
auto periodLog = std::make_unique<const TimeSeriesProperty<int>>("period_log"); // not used
539+
int nPeriods{1};
540+
LoadEventNexus::runLoadNexusLogs<MatrixWorkspace_sptr>(filename, wksp, *this, false, nPeriods, periodLog);
541+
621542
wksp->setYUnit("Counts");
622543
wksp->getAxis(0)->setUnit("DSpacing");
623544
setProperty(PropertyNames::OUTPUT_WKSP, std::move(wksp));

0 commit comments

Comments
 (0)