Skip to content

Commit

Permalink
Merge pull request #37214 from peterfpeterson/ewm3412_loadevent_accum…
Browse files Browse the repository at this point in the history
…ulator

LoadEventNexus with compression refactor - ornl-next
  • Loading branch information
peterfpeterson committed Apr 25, 2024
2 parents 0a392b3 + 81b49ea commit c421d86
Show file tree
Hide file tree
Showing 21 changed files with 1,312 additions and 105 deletions.
5 changes: 5 additions & 0 deletions Framework/DataHandling/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ set(SRC_FILES
src/ApplyDiffCal.cpp
src/BankPulseTimes.cpp
src/CheckMantidVersion.cpp
src/CompressEventAccumulator.cpp
src/CompressEvents.cpp
src/CreateChunkingFromInstrument.cpp
src/CreatePolarizationEfficiencies.cpp
Expand Down Expand Up @@ -151,6 +152,7 @@ set(SRC_FILES
src/PDLoadCharacterizations.cpp
src/ParallelEventLoader.cpp
src/PatchBBY.cpp
src/ProcessBankCompressed.cpp
src/ProcessBankData.cpp
src/PulseIndexer.cpp
src/RawFileInfo.cpp
Expand Down Expand Up @@ -223,6 +225,7 @@ set(INC_FILES
inc/MantidDataHandling/BankPulseTimes.h
inc/MantidDataHandling/BitStream.h
inc/MantidDataHandling/CheckMantidVersion.h
inc/MantidDataHandling/CompressEventAccumulator.h
inc/MantidDataHandling/CompressEvents.h
inc/MantidDataHandling/CreateChunkingFromInstrument.h
inc/MantidDataHandling/CreatePolarizationEfficiencies.h
Expand Down Expand Up @@ -370,6 +373,7 @@ set(INC_FILES
inc/MantidDataHandling/PDLoadCharacterizations.h
inc/MantidDataHandling/ParallelEventLoader.h
inc/MantidDataHandling/PatchBBY.h
inc/MantidDataHandling/ProcessBankCompressed.h
inc/MantidDataHandling/ProcessBankData.h
inc/MantidDataHandling/PulseIndexer.h
inc/MantidDataHandling/RawFileInfo.h
Expand Down Expand Up @@ -446,6 +450,7 @@ set(TEST_FILES
ApplyDiffCalTest.h
BankPulseTimesTest.h
CheckMantidVersionTest.h
CompressEventAccumulatorTest.h
CompressEventsTest.h
CreateChunkingFromInstrumentTest.h
CreatePolarizationEfficienciesTest.h
Expand Down
@@ -0,0 +1,72 @@
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2024 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include "MantidDataHandling/DllConfig.h"
#include "MantidDataObjects/EventList.h"

#include <vector>

namespace Mantid {
namespace DataHandling {

enum class CompressBinningMode { LINEAR, LOGARITHMIC };

/** CompressEventAccumulator : TODO: DESCRIPTION
*/
class MANTID_DATAHANDLING_DLL CompressEventAccumulator {
public:
// TODO parameter for expected number of events
CompressEventAccumulator(std::shared_ptr<std::vector<double>> histogram_bin_edges, const double divisor,
CompressBinningMode bin_mode);
virtual ~CompressEventAccumulator() = default; // needed because this is an abstract base class

virtual void addEvent(const float tof) = 0;
virtual void createWeightedEvents(std::vector<Mantid::DataObjects::WeightedEventNoTime> *raw_events) const = 0;

std::size_t numberHistBins() const;
virtual double totalWeight() const = 0;
virtual DataObjects::EventSortType getSortType() const = 0;

protected:
template <typename INT_TYPE> double getBinCenter(const INT_TYPE bin) const;
boost::optional<size_t> findBin(const float tof) const;
/// shared pointer for the histogram bin boundaries
const std::shared_ptr<std::vector<double>> m_histogram_edges;

private:
/// keep track if the m_tof is already sorted
// offset is applied after division
// see EventList::findLinearBin for implementation on what that means
double m_divisor;
double m_offset;
/// function pointer on how to find the bin boundaries
boost::optional<size_t> (*m_findBin)(const MantidVec &, const double, const double, const double, const bool);

protected:
/// track whether this has allocated the "big" memory objects
bool m_initialized;
};

/**
* @brief The CompressEventAccumulatorFactory Factory object that will create the correct type of
* CompressEventAccumulator based on configuration information.
*/
class MANTID_DATAHANDLING_DLL CompressEventAccumulatorFactory {
public:
CompressEventAccumulatorFactory(std::shared_ptr<std::vector<double>> histogram_bin_edges, const double divisor,
CompressBinningMode bin_mode);
std::unique_ptr<CompressEventAccumulator> create(const std::size_t num_events);

private:
double m_divisor;
CompressBinningMode m_bin_mode;
const std::shared_ptr<std::vector<double>> m_histogram_edges;
};

} // namespace DataHandling
} // namespace Mantid
Expand Up @@ -65,6 +65,10 @@ class MANTID_DATAHANDLING_DLL DefaultEventLoader {
/// in the event list.
std::vector<std::vector<std::vector<Mantid::DataObjects::WeightedEvent> *>> weightedEventVectors;

/// Vector where index = event_id; value = ptr to std::vector<WeightedEventNoTime> in the
/// event list.
std::vector<std::vector<std::vector<Mantid::DataObjects::WeightedEventNoTime> *>> weightedNoTimeEventVectors;

/// Vector where (index = pixel ID+pixelID_to_wi_offset), value = workspace
/// index)
std::vector<size_t> pixelID_to_wi_vector;
Expand All @@ -88,7 +92,7 @@ template <class T> void DefaultEventLoader::makeMapToEventLists(std::vector<std:
vectors.resize(m_ws.nPeriods());
if (event_id_is_spec) {
// Find max spectrum no
auto *ax1 = m_ws.getAxis(1);
const auto *ax1 = m_ws.getAxis(1);
specnum_t maxSpecNo = -std::numeric_limits<specnum_t>::max(); // So that any number will be
// greater than this
for (size_t i = 0; i < ax1->length(); i++) {
Expand Down
Expand Up @@ -39,7 +39,7 @@ class MANTID_DATAHANDLING_DLL LoadBankFromDiskTask : public Kernel::Task {
void loadPulseTimes(::NeXus::File &file);
std::unique_ptr<std::vector<uint64_t>> loadEventIndex(::NeXus::File &file);
void prepareEventId(::NeXus::File &file, int64_t &start_event, int64_t &stop_event,
const std::vector<uint64_t> &event_index);
const uint64_t &start_event_index);
std::unique_ptr<std::vector<uint32_t>> loadEventId(::NeXus::File &file);
std::unique_ptr<std::vector<float>> loadTof(::NeXus::File &file);
std::unique_ptr<std::vector<float>> loadEventWeights(::NeXus::File &file);
Expand Down
Expand Up @@ -178,6 +178,7 @@ class MANTID_DATAHANDLING_DLL LoadEventNexus : public API::NexusFileLoader {

/// Tolerance for CompressEvents; use -1 to mean don't compress.
double compressTolerance;
bool compressEvents;

/// Pulse times for ALL banks, taken from proton_charge log.
std::shared_ptr<BankPulseTimes> m_allBanksPulseTimes;
Expand Down
@@ -0,0 +1,94 @@
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright &copy; 2024 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include "MantidDataHandling/BankPulseTimes.h"
#include "MantidDataHandling/CompressEventAccumulator.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidDataObjects/EventList.h"
#include "MantidKernel/Task.h"

#include <vector>

namespace Mantid {
namespace API {
class Progress; // forward declare
}
namespace DataHandling {
class DefaultEventLoader; // forward declare
class CompressEventAccumulatorFactory;

/** ProcessBankCompressed : TODO: DESCRIPTION
*/
class MANTID_DATAHANDLING_DLL ProcessBankCompressed : public Mantid::Kernel::Task {
public:
ProcessBankCompressed(DefaultEventLoader &m_loader, const std::string &entry_name, Mantid::API::Progress *prog,
std::shared_ptr<std::vector<uint32_t>> event_detid,
std::shared_ptr<std::vector<float>> event_tof, size_t startAt,
std::shared_ptr<std::vector<uint64_t>> event_index,
std::shared_ptr<BankPulseTimes> bankPulseTimes, detid_t min_detid, detid_t max_detid,
std::shared_ptr<std::vector<double>> histogram_bin_edges, const double divisor);

void run() override;

void addEvent(const size_t period_index, const size_t event_index);

void createWeightedEvents(const size_t period_index, const detid_t detid,
std::vector<Mantid::DataObjects::WeightedEventNoTime> *raw_events);

/// method only intended for testing
double totalWeight() const;

private:
void createAccumulators(const bool precount);
void collectEvents();
void addToEventLists();

// disable default constructor
ProcessBankCompressed();
/// Algorithm being run
DefaultEventLoader &m_loader;
const std::string m_entry_name;
/// Progress reporting
API::Progress *m_prog;

/// factory for creating accumulators
std::unique_ptr<CompressEventAccumulatorFactory> m_factory;

/// event pixel ID array
std::shared_ptr<std::vector<uint32_t>> m_event_detid;
/// event TOF array
std::shared_ptr<std::vector<float>> m_event_tof;
/// index of the first event from event_index
const size_t m_firstEventIndex;
/// vector of event index (length of # of pulses)
std::shared_ptr<std::vector<uint64_t>> m_event_index;
/// Pulse times for this bank
std::shared_ptr<BankPulseTimes> m_bankPulseTimes;

/**
* Objects holding individual spectra. This is accessed as [periodIndex][detidIndex]
*/
std::vector<std::vector<std::unique_ptr<DataHandling::CompressEventAccumulator>>> m_spectra_accum;

/*
* After events are added, this will contain the sorting information to set out the output event lists
*/
std::vector<DataObjects::EventSortType> m_sorting;

// inclusive
const detid_t m_detid_min;
// inclusive
const detid_t m_detid_max;
// inclusive
const float m_tof_min;
// exclusive
const float m_tof_max;
};

} // namespace DataHandling
} // namespace Mantid
Expand Up @@ -10,7 +10,6 @@
#include "MantidGeometry/IDTypes.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/Task.h"
#include "MantidKernel/Timer.h"

#include <memory>

Expand Down Expand Up @@ -42,7 +41,7 @@ class ProcessBankData : public Mantid::Kernel::Task {
* @param min_event_id ;: minimum detector ID to load
* @param max_event_id :: maximum detector ID to load
*/ // API::IFileLoader<Kernel::NexusDescriptor>
ProcessBankData(DefaultEventLoader &loader, std::string entry_name, API::Progress *prog,
ProcessBankData(DefaultEventLoader &loader, const std::string &entry_name, API::Progress *prog,
std::shared_ptr<std::vector<uint32_t>> event_id,
std::shared_ptr<std::vector<float>> event_time_of_flight, size_t numEvents, size_t startAt,
std::shared_ptr<std::vector<uint64_t>> event_index,
Expand Down

0 comments on commit c421d86

Please sign in to comment.