Commit 0aee1da9 authored by Stephen's avatar Stephen
Browse files

Add time correction to Muon loading

This commit adds time correction to the Muon loader. As the histogram data was loaded using the LoadISISNexus algorithm the time zero needed to be applied afterwards.
parent 3c295dcc
......@@ -38,6 +38,9 @@ loadDeadTimesFromNexus(const NeXus::NXEntry &entry,
const bool isFileMultiPeriod);
// Load first good data from the nexus entry
double loadFirstGoodDataFromNexus(const NeXus::NXEntry &entry);
// Load time zero from the nexus entry
double loadTimeZeroFromNexusFile(const NeXus::NXEntry &entry);
} // namespace LoadMuonNexus3Helper
} // namespace DataHandling
} // namespace Mantid
......@@ -28,6 +28,8 @@ public:
virtual void loadMuonLogData() = 0;
// Returns the good frames from the nexus entry
virtual void loadGoodFrames() = 0;
// Apply time zero correction
virtual void applyTimeZeroCorrection() = 0;
// Load detector grouping
virtual API::Workspace_sptr loadDetectorGrouping() = 0;
// Load dead time table
......@@ -48,4 +50,4 @@ protected:
std::string m_filename;
};
} // namespace DataHandling
} // namespace Mantid
\ No newline at end of file
} // namespace Mantid
......@@ -30,6 +30,8 @@ public:
void loadMuonLogData() override;
// Returns the good frames from the nexus entry
void loadGoodFrames() override;
// Apply the time zero correction to the workspace time axis
void applyTimeZeroCorrection() override;
// Load detector grouping
API::Workspace_sptr loadDetectorGrouping() override;
// Load default grouping from ID
......@@ -44,4 +46,4 @@ private:
bool m_isFileMultiPeriod;
};
} // namespace DataHandling
} // namespace Mantid
\ No newline at end of file
} // namespace Mantid
......@@ -31,7 +31,6 @@
#include <functional>
#include <vector>
#include <iostream>
namespace Mantid {
namespace DataHandling {
......@@ -144,15 +143,22 @@ void LoadMuonNexus3::exec() {
Workspace2D_sptr workspace2D =
std::dynamic_pointer_cast<Workspace2D>(outWS);
m_loadMuonStrategy = std::make_unique<SinglePeriodLoadMuonStrategy>(
g_log, m_filename, entry, workspace2D, m_entrynumber,
g_log, m_filename, entry, workspace2D, static_cast<int>(m_entrynumber),
m_isFileMultiPeriod);
}
m_loadMuonStrategy->loadMuonLogData();
m_loadMuonStrategy->loadGoodFrames();
m_loadMuonStrategy->applyTimeZeroCorrection();
// Grouping info should be returned if user has set the property
auto loadedGrouping = m_loadMuonStrategy->loadDetectorGrouping();
setProperty("DetectorGroupingTable", loadedGrouping);
if (!getPropertyValue("DetectorGroupingTable").empty()) {
setProperty("DetectorGroupingTable", loadedGrouping);
};
// Deadtime table should be returned if user has set the property
auto deadtimeTable = m_loadMuonStrategy->loadDeadTimeTable();
setProperty("DeadTimeTable", deadtimeTable);
if (!getPropertyValue("DeadTimeTable").empty()) {
setProperty("DeadTimeTable", deadtimeTable);
}
}
/**
......@@ -191,6 +197,9 @@ void LoadMuonNexus3::loadMuonProperties(const NXEntry &entry) {
LoadMuonNexus3Helper::loadMainFieldDirectionFromNexus(entry);
setProperty("MainFieldDirection", mainFieldDirection);
double timeZero = LoadMuonNexus3Helper::loadTimeZeroFromNexusFile(entry);
setProperty("timeZero", timeZero);
try {
auto firstGoodData =
LoadMuonNexus3Helper::loadFirstGoodDataFromNexus(entry);
......
......@@ -12,8 +12,6 @@
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceFactory.h"
#include <iostream>
namespace Mantid {
namespace DataHandling {
namespace LoadMuonNexus3Helper {
......@@ -35,7 +33,9 @@ NXInt loadGoodFramesDataFromNexus(const NXEntry &entry,
NXInt goodFrames = entry.openNXInt("good_frames");
goodFrames.load();
return goodFrames;
} catch (...) {
} catch (std::runtime_error) {
throw std::runtime_error(
"Could not load good frames data from nexus file, check Nexus file");
}
} else {
try {
......@@ -44,7 +44,9 @@ NXInt loadGoodFramesDataFromNexus(const NXEntry &entry,
NXInt goodFrames = periodClass.openNXInt("good_frames");
goodFrames.load();
return goodFrames;
} catch (...) {
} catch (std::runtime_error) {
throw std::runtime_error(
"Could not load good frames data from nexus file, check Nexus file");
}
}
}
......@@ -67,18 +69,17 @@ loadDetectorGroupingFromNexus(NXEntry &entry,
std::vector<detid_t> detectorsLoaded;
std::vector<detid_t> grouping;
// Return the detectors which are loaded
// Get the detectors which are loaded
// then find the grouping ID for each detector
for (int64_t spectraIndex = 0; spectraIndex < numberOfSpectra;
spectraIndex++) {
const auto detIdSet =
const auto &detIdSet =
localWorkspace->getSpectrum(spectraIndex).getDetectorIDs();
for (auto detector : detIdSet) {
detectorsLoaded.emplace_back(detector);
}
}
if (!isFileMultiPeriod) {
// Simplest case - one grouping entry per detector
for (const auto &detectorNumber : detectorsLoaded) {
grouping.emplace_back(groupingData[detectorNumber - 1]);
}
......@@ -97,10 +98,9 @@ std::string loadMainFieldDirectionFromNexus(const NeXus::NXEntry &entry) {
if (orientation[0] == 't') {
mainFieldDirection = "Transverse";
}
} catch (...) {
} catch (std::runtime_error) {
// no data - assume main field was longitudinal
}
return mainFieldDirection;
}
std::tuple<std::vector<detid_t>, std::vector<double>>
......@@ -167,6 +167,16 @@ double loadFirstGoodDataFromNexus(const NeXus::NXEntry &entry) {
throw e;
}
}
double loadTimeZeroFromNexusFile(const NeXus::NXEntry &entry) {
try {
NXClass detectorEntry = entry.openNXGroup("instrument/detector_1");
double timeZero = static_cast<double>(detectorEntry.getFloat("time_zero"));
return timeZero;
} catch (std::runtime_error) {
throw std::runtime_error("Could not load time zero, check Nexus file");
}
}
} // namespace LoadMuonNexus3Helper
} // namespace DataHandling
} // namespace Mantid
\ No newline at end of file
} // namespace Mantid
......@@ -5,14 +5,16 @@
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataHandling/SinglePeriodLoadMuonStrategy.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/GroupingLoader.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataHandling/ISISRunLogs.h"
#include "MantidDataHandling/LoadMuonLog.h"
#include "MantidDataHandling/LoadMuonNexus3Helper.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/UnitLabelTypes.h"
namespace Mantid {
namespace DataHandling {
......@@ -142,6 +144,27 @@ Workspace_sptr SinglePeriodLoadMuonStrategy::loadDeadTimeTable() const {
return deadtimeWorkspace;
}
/**
* Performs time-zero correction on the loaded workspace and also changes the
* unit label on the time axis, which is incorrect due to being loaded using
* LoadISISNexus2
*/
void SinglePeriodLoadMuonStrategy::applyTimeZeroCorrection() {
m_logger.notice("Applying time zero correction");
double timeZero = LoadMuonNexus3Helper::loadTimeZeroFromNexusFile(m_entry);
auto &newUnit = std::dynamic_pointer_cast<Kernel::Units::Label>(
Kernel::UnitFactory::Instance().create("Label"));
newUnit->setLabel("Time", Kernel::Units::Symbol::Microsecond);
m_workspace->getAxis(0)->unit() = newUnit;
int numHistograms = static_cast<int>(m_workspace->getNumberHistograms());
PARALLEL_FOR_NO_WSP_CHECK()
for (int i = 0; i < numHistograms; i++) {
auto &timeAxis = m_workspace->mutableX(i);
timeAxis = timeAxis - timeZero;
}
}
} // namespace DataHandling
} // namespace Mantid
......@@ -46,8 +46,13 @@ public:
std::dynamic_pointer_cast<Workspace2D>(output_ws);
const Mantid::API::Run &run1 = output2D_1->run();
int goodfrm1 = run1.getPropertyAsIntegerValue("goodfrm");
TS_ASSERT_EQUALS(goodfrm1, 36197);
int goodfrm = run1.getPropertyAsIntegerValue("goodfrm");
TS_ASSERT_EQUALS(goodfrm, 36197);
double firstGoodData = ld.getProperty("FirstGoodData");
TS_ASSERT_EQUALS(firstGoodData, 0.384);
double timeZero = ld.getProperty("TimeZero");
TS_ASSERT_DELTA(timeZero, 0.1599999, 1e-5);
// check time zero applied to time axis
}
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment