Mantid
Loading...
Searching...
No Matches
ProcessEventsTask.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2025 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 +
7
8#pragma once
9
11#include <ranges>
12#include <tbb/tbb.h>
13#include <vector>
14
16
17template <typename DetIDsVector, typename TofVector> class ProcessEventsTask {
18public:
19 ProcessEventsTask(DetIDsVector *detids, TofVector *tofs, const BankCalibration *calibration,
20 const std::vector<double> *binedges)
21 : y_temp(binedges->size() - 1, 0), m_detids(detids), m_tofs(tofs), m_calibration(calibration),
22 m_binedges(binedges) {}
23
25 : y_temp(other.y_temp.size(), 0), m_detids(other.m_detids), m_tofs(other.m_tofs),
27
28 void operator()(const tbb::blocked_range<size_t> &range) {
29 if (m_calibration->empty()) {
30 return;
31 }
32 // Cache values to reduce number of function calls
33 const auto &range_end = range.end();
34 const auto &binedges_cbegin = m_binedges->cbegin();
35 const auto &binedges_cend = m_binedges->cend();
36 const auto &tof_min = m_binedges->front();
37 const auto &tof_max = m_binedges->back();
38
39 // Calibrate and histogram the data
40 auto detid_iter = std::ranges::next(m_detids->begin(), range.begin());
41 auto tof_iter = std::ranges::next(m_tofs->begin(), range.begin());
42 for (size_t i = range.begin(); i < range_end; ++i) {
43 const auto &detid = *detid_iter;
44 const auto &calib_factor = m_calibration->value_calibration(detid);
45 if (calib_factor < IGNORE_PIXEL) {
46 // Apply calibration
47 const double &tof = static_cast<double>(*tof_iter) * calib_factor;
48 if ((tof < tof_max) && (!(tof < tof_min))) { // check against max first to allow skipping second
49 // Find the bin index using binary search
50 const auto &it = std::upper_bound(binedges_cbegin, binedges_cend, tof);
51
52 // Increment the count if a bin was found
53 const auto &bin = static_cast<size_t>(std::distance(binedges_cbegin, it) - 1);
54 y_temp[bin]++;
55 }
56 }
57 ++detid_iter;
58 ++tof_iter;
59 }
60 }
61
62 void join(const ProcessEventsTask &other) {
63 // Combine local histograms
64 std::transform(y_temp.begin(), y_temp.end(), other.y_temp.cbegin(), y_temp.begin(), std::plus<>{});
65 }
66
68 std::vector<uint32_t> y_temp;
69
70private:
71 DetIDsVector *m_detids;
72 TofVector *m_tofs;
74 const std::vector<double> *m_binedges;
75};
76
77} // namespace Mantid::DataHandling::AlignAndFocusPowderSlim
Class that handles all the calibration constants for a bank of detectors.
const double & value_calibration(const detid_t detid) const
This assumes that everything is in range.
void operator()(const tbb::blocked_range< size_t > &range)
ProcessEventsTask(DetIDsVector *detids, TofVector *tofs, const BankCalibration *calibration, const std::vector< double > *binedges)
std::vector< uint32_t > y_temp
Local histogram for this block/thread.