Mantid
Loading...
Searching...
No Matches
BankPulseTimes.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 +
8
9#include <nexus/NeXusFile.hpp>
10
11using namespace Mantid::Kernel;
12//===============================================================================================
13// BankPulseTimes
14//===============================================================================================
15
17const unsigned int BankPulseTimes::FirstPeriod = 1;
18
19//----------------------------------------------------------------------------------------------
25BankPulseTimes::BankPulseTimes(::NeXus::File &file, const std::vector<int> &pNumbers) : periodNumbers(pNumbers) {
26 file.openData("event_time_zero");
27 // Read the offset (time zero)
28 // If the offset is not present, use Unix epoch
29 if (!file.hasAttr("offset"))
30 startTime = "1970-01-01T00:00:00Z";
31 else
32 file.getAttr("offset", startTime);
33 Mantid::Types::Core::DateAndTime start(startTime);
34 // Load the seconds offsets
35
36 const auto heldTimeZeroType = file.getInfo().type;
37 // Nexus only requires event_time_zero to be a NXNumber, we support two
38 // possilites
39 if (heldTimeZeroType == ::NeXus::FLOAT64) {
40 std::vector<double> seconds;
41 file.getData(seconds);
42 file.closeData();
43 // Now create the pulseTimes
44 if (seconds.size() == 0)
45 throw std::runtime_error("event_time_zero field has no data!");
46
47 std::transform(seconds.cbegin(), seconds.cend(), std::back_inserter(pulseTimes),
48 [start](double seconds) { return start + seconds; });
49 } else if (heldTimeZeroType == ::NeXus::UINT64) {
50 std::vector<uint64_t> nanoseconds;
51 file.getData(nanoseconds);
52 file.closeData();
53 // Now create the pulseTimes
54 if (nanoseconds.size() == 0)
55 throw std::runtime_error("event_time_zero field has no data!");
56
57 std::transform(nanoseconds.cbegin(), nanoseconds.cend(), std::back_inserter(pulseTimes),
58 [start](int64_t nanoseconds) { return start + nanoseconds; });
59 } else {
60 throw std::invalid_argument("Unsupported type for event_time_zero");
61 }
62 // Ensure that we always have a consistency between nPulses and
63 // periodNumbers containers
64 if (pulseTimes.size() != pNumbers.size()) {
65 periodNumbers = std::vector<int>(pulseTimes.size(), FirstPeriod);
66 ;
67 }
68}
69
70//----------------------------------------------------------------------------------------------
75BankPulseTimes::BankPulseTimes(const std::vector<Mantid::Types::Core::DateAndTime> &times) {
76 if (times.size() == 0)
77 return;
78
79 pulseTimes = times;
80 periodNumbers = std::vector<int>(pulseTimes.size(), FirstPeriod); // TODO we are fixing this at 1 period for all
81}
82
83//----------------------------------------------------------------------------------------------
92bool BankPulseTimes::equals(size_t otherNumPulse, const std::string &otherStartTime) {
93 return ((this->startTime == otherStartTime) && (this->pulseTimes.size() == otherNumPulse));
94}
BankPulseTimes(::NeXus::File &file, const std::vector< int > &pNumbers)
Constructor with NeXus::File.
static const unsigned int FirstPeriod
Starting number for assigning periods.
std::string startTime
String describing the start time.
std::vector< int > periodNumbers
Vector of period numbers corresponding to each pulse.
bool equals(size_t otherNumPulse, const std::string &otherStartTime)
Equals.
std::vector< Mantid::Types::Core::DateAndTime > pulseTimes
Array of the pulse times.