Skip to content

Commit

Permalink
Merge pull request #38407 from robertapplin/0-muon-loader-confidence-…
Browse files Browse the repository at this point in the history
…calculation-moved

Move muon loader confidence check out of exec method
  • Loading branch information
SilkeSchomann authored Dec 5, 2024
2 parents 03905f0 + d012adf commit d6096cf
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 191 deletions.
23 changes: 5 additions & 18 deletions Framework/DataHandling/inc/MantidDataHandling/LoadMuonNexus1.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,7 @@ together based on the groupings in the NeXus file. </LI>
*/
class MANTID_DATAHANDLING_DLL LoadMuonNexus1 : public LoadMuonNexus {
public:
/// Default constructor
LoadMuonNexus1();
/// Algorithm's name for identification overriding a virtual method
const std::string name() const override { return "LoadMuonNexus"; }
/// Summary of algorithms purpose
const std::string summary() const override {
return "The LoadMuonNexus algorithm will read the given NeXus Muon data "
"file Version 1 and use the results to populate the named "
"workspace. LoadMuonNexus may be invoked by LoadNexus if it is "
"given a NeXus file of this type.";
}

/// Algorithm's version for identification overriding a virtual method
int version() const override { return 1; }
/// Algorithm's category for identification overriding a virtual method
const std::string category() const override { return "DataHandling\\Nexus;Muon\\DataHandling"; }

/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::NexusDescriptor &descriptor) const override;
Expand All @@ -79,7 +64,7 @@ class MANTID_DATAHANDLING_DLL LoadMuonNexus1 : public LoadMuonNexus {
void exec() override;

private:
void loadData(size_t hist, specnum_t &i, specnum_t specNo, MuonNexusReader &nxload, const int64_t lengthIn,
void loadData(size_t hist, specnum_t const &i, specnum_t specNo, MuonNexusReader &nxload, const int64_t lengthIn,
const DataObjects::Workspace2D_sptr &localWorkspace);
void runLoadMappingTable(DataObjects::Workspace2D_sptr);
void runLoadLog(const DataObjects::Workspace2D_sptr &);
Expand All @@ -91,14 +76,16 @@ class MANTID_DATAHANDLING_DLL LoadMuonNexus1 : public LoadMuonNexus {
void loadDeadTimes(Mantid::NeXus::NXRoot &root);

/// Creates Dead Time Table using all the data between begin and end
DataObjects::TableWorkspace_sptr createDeadTimeTable(std::vector<int> specToLoad, std::vector<double> deadTimes);
DataObjects::TableWorkspace_sptr createDeadTimeTable(std::vector<int> const &specToLoad,
std::vector<double> const &deadTimes);

/// Loads detector grouping information
API::Workspace_sptr loadDetectorGrouping(Mantid::NeXus::NXRoot &root,
const Mantid::Geometry::Instrument_const_sptr &inst);

/// Creates Detector Grouping Table using all the data from the range
DataObjects::TableWorkspace_sptr createDetectorGroupingTable(std::vector<int> specToLoad, std::vector<int> grouping);
DataObjects::TableWorkspace_sptr createDetectorGroupingTable(std::vector<int> const &specToLoad,
std::vector<int> const &grouping);
};

} // namespace DataHandling
Expand Down
11 changes: 1 addition & 10 deletions Framework/DataHandling/inc/MantidDataHandling/LoadMuonNexus2.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,23 @@ together based on the groupings in the NeXus file. </LI>
*/
class MANTID_DATAHANDLING_DLL LoadMuonNexus2 : public LoadMuonNexus {
public:
/// Default constructor
LoadMuonNexus2();
/// Algorithm's name for identification overriding a virtual method
const std::string name() const override { return "LoadMuonNexus"; }
/// Summary of algorithms purpose

const std::string summary() const override {
return "The LoadMuonNexus algorithm will read the given NeXus Muon data "
"file Version 2 and use the results to populate the named "
"workspace. LoadMuonNexus may be invoked by LoadNexus if it is "
"given a NeXus file of this type.";
}

/// Algorithm's version for identification overriding a virtual method
int version() const override { return 2; }
const std::vector<std::string> seeAlso() const override { return {"LoadNexus"}; }
/// Algorithm's category for identification overriding a virtual method
const std::string category() const override { return "DataHandling\\Nexus;Muon\\DataHandling"; }

/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::NexusDescriptor &descriptor) const override;

private:
/// Overwrites Algorithm method
void exec() override;
/// Execute this version of the algorithm
void doExec();

HistogramData::Histogram loadData(const Mantid::HistogramData::BinEdges &edges, const Mantid::NeXus::NXInt &counts,
int period, int spec);
Expand Down
15 changes: 8 additions & 7 deletions Framework/DataHandling/src/LoadMuonNexus1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ Workspace_sptr LoadMuonNexus1::loadDetectorGrouping(NXRoot &root, const Geometry
[&groupingData](const auto spec) { return groupingData[spec - 1]; });
} else {
// User selected an entry number
for (auto &spec : specToLoad) {
for (auto const &spec : specToLoad) {
int index = spec - 1 + static_cast<int>((m_entrynumber - 1) * m_numberOfSpectra);
grouping.emplace_back(groupingData[index]);
}
Expand Down Expand Up @@ -548,7 +548,7 @@ Workspace_sptr LoadMuonNexus1::loadDetectorGrouping(NXRoot &root, const Geometry

// Get the grouping
grouping.clear();
for (auto &spec : specToLoad) {
for (auto const &spec : specToLoad) {
int index = spec - 1 + i * static_cast<int>(m_numberOfSpectra);
grouping.emplace_back(groupingData[index]);
}
Expand Down Expand Up @@ -601,7 +601,8 @@ Workspace_sptr LoadMuonNexus1::loadDetectorGrouping(NXRoot &root, const Geometry
* @param deadTimes :: vector containing the corresponding dead times
* @return Dead Time Table create using the data
*/
TableWorkspace_sptr LoadMuonNexus1::createDeadTimeTable(std::vector<int> specToLoad, std::vector<double> deadTimes) {
TableWorkspace_sptr LoadMuonNexus1::createDeadTimeTable(std::vector<int> const &specToLoad,
std::vector<double> const &deadTimes) {
TableWorkspace_sptr deadTimeTable =
std::dynamic_pointer_cast<TableWorkspace>(WorkspaceFactory::Instance().createTable("TableWorkspace"));

Expand All @@ -623,8 +624,8 @@ TableWorkspace_sptr LoadMuonNexus1::createDeadTimeTable(std::vector<int> specToL
* @param grouping :: Vector containing corresponding grouping
* @return Detector Grouping Table create using the data
*/
TableWorkspace_sptr LoadMuonNexus1::createDetectorGroupingTable(std::vector<int> specToLoad,
std::vector<int> grouping) {
TableWorkspace_sptr LoadMuonNexus1::createDetectorGroupingTable(std::vector<int> const &specToLoad,
std::vector<int> const &grouping) {
auto detectorGroupingTable =
std::dynamic_pointer_cast<TableWorkspace>(WorkspaceFactory::Instance().createTable("TableWorkspace"));

Expand All @@ -638,7 +639,7 @@ TableWorkspace_sptr LoadMuonNexus1::createDetectorGroupingTable(std::vector<int>
groupingMap[grouping[i]].emplace_back(specToLoad[i]);
}

for (auto &group : groupingMap) {
for (auto const &group : groupingMap) {
if (group.first != 0) // Skip 0 group
{
TableRow newRow = detectorGroupingTable->appendRow();
Expand All @@ -658,7 +659,7 @@ TableWorkspace_sptr LoadMuonNexus1::createDetectorGroupingTable(std::vector<int>
* @param localWorkspace :: A pointer to the workspace in which the data will
* be stored
*/
void LoadMuonNexus1::loadData(size_t hist, specnum_t &i, specnum_t specNo, MuonNexusReader &nxload,
void LoadMuonNexus1::loadData(size_t hist, specnum_t const &i, specnum_t specNo, MuonNexusReader &nxload,
const int64_t lengthIn, const DataObjects::Workspace2D_sptr &localWorkspace) {
// Read in a spectrum
// Put it into a vector, discarding the 1st entry, which is rubbish
Expand Down
51 changes: 0 additions & 51 deletions Framework/DataHandling/src/LoadMuonNexus2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ using Mantid::HistogramData::Histogram;
using namespace Mantid::NeXus;
using Mantid::Types::Core::DateAndTime;

namespace {
inline std::string getLoadAlgName(int confidence1, int confidenceV2) {
return confidence1 > confidenceV2 ? "LoadMuonNexus" : "LoadMuonNexusV2";
}
} // namespace

/// Empty default constructor
LoadMuonNexus2::LoadMuonNexus2() : LoadMuonNexus() {}

/** Executes the right version of the Muon nexus loader
Expand All @@ -68,50 +61,6 @@ LoadMuonNexus2::LoadMuonNexus2() : LoadMuonNexus() {}
*values
*/
void LoadMuonNexus2::exec() {
LoadMuonNexus1 load1;
LoadMuonNexusV2 loadV2;
load1.initialize();
loadV2.initialize();

std::string filePath = getPropertyValue("Filename");
Kernel::NexusDescriptor descriptor(filePath);
int confidence1 = load1.confidence(descriptor);
int confidence2 = this->confidence(descriptor);
int confidenceV2 = 0;
// If the file is hdf5 then we can possibly use LoadMuonNexusV2
// To check this we'll have to create an HDF5 descriptor for the file.
if (Kernel::NexusDescriptor::isReadable(filePath, Kernel::NexusDescriptor::Version5)) {
Kernel::NexusHDF5Descriptor descriptorHDF5(filePath);
confidenceV2 = loadV2.confidence(descriptorHDF5);
};

// if none can load the file throw
if (confidence1 < 80 && confidence2 < 80 && confidenceV2 < 80) {
throw Kernel::Exception::FileError("Cannot open the file ", filePath);
}
// Now pick the correct alg
if (confidence2 > std::max(confidence1, confidenceV2)) {
// Use this loader
doExec();
} else {
// Version 1 or V2
auto childAlg = createChildAlgorithm(getLoadAlgName(confidence1, confidenceV2), 0, 1, true, 1);
auto loader = std::dynamic_pointer_cast<API::Algorithm>(childAlg);
loader->copyPropertiesFrom(*this);
loader->executeAsChildAlg();
this->copyPropertiesFrom(*loader);
API::Workspace_sptr outWS = loader->getProperty("OutputWorkspace");
setProperty("OutputWorkspace", outWS);
}
}

/** Read in a muon nexus file of the version 2.
*
* @throw Exception::FileError If the Nexus file cannot be found/opened
* @throw std::invalid_argument If the optional properties are set to invalid
*values
*/
void LoadMuonNexus2::doExec() {
// Create the root Nexus class
NXRoot root(getPropertyValue("Filename"));

Expand Down
4 changes: 3 additions & 1 deletion Framework/DataHandling/src/LoadNexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ void LoadNexus::exec() {
}

void LoadNexus::runLoadMuonNexus() {
auto loadMuonNexus = createChildAlgorithm("LoadMuonNexus", 0., 1.);
// The Load algorithm will choose the correct LoadMuonNexus algorithm
auto loadMuonNexus = createChildAlgorithm("Load", 0., 1.);

// Pass through the same input filename
loadMuonNexus->setPropertyValue("Filename", m_filename);
// Set the workspace property
Expand Down
37 changes: 21 additions & 16 deletions Framework/Muon/test/AlphaCalcTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "MantidAPI/IAlgorithm.h"
#include "MantidAPI/Workspace.h"
#include "MantidDataHandling/GroupDetectors.h"
#include "MantidDataHandling/Load.h"
#include "MantidDataHandling/LoadInstrument.h"
#include "MantidDataHandling/LoadMuonNexus2.h"
#include "MantidDataObjects/Workspace2D.h"
Expand All @@ -33,15 +34,9 @@ class AlphaCalcTest : public CxxTest::TestSuite {
}

void testCalAlphaManySpectra() {
// system("pause");
// Load the muon nexus file
loader.initialize();
loader.setPropertyValue("Filename", "emu00006473.nxs");
loader.setPropertyValue("OutputWorkspace", "EMU6473");
TS_ASSERT_THROWS_NOTHING(loader.execute());
TS_ASSERT_EQUALS(loader.isExecuted(), true);
auto const workspace = loadFile("emu00006473.nxs");

alphaCalc.setPropertyValue("InputWorkspace", "EMU6473");
alphaCalc.setProperty("InputWorkspace", workspace);
alphaCalc.setPropertyValue("ForwardSpectra", "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16");
alphaCalc.setPropertyValue("BackwardSpectra", "17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32");
alphaCalc.setPropertyValue("FirstGoodValue", "0.3");
Expand All @@ -56,14 +51,9 @@ class AlphaCalcTest : public CxxTest::TestSuite {
}

void testCalAlphaTwoSpectra() {
// Load the muon nexus file
loader.initialize();
loader.setPropertyValue("Filename", "emu00006473.nxs");
loader.setPropertyValue("OutputWorkspace", "EMU6473");
TS_ASSERT_THROWS_NOTHING(loader.execute());
TS_ASSERT_EQUALS(loader.isExecuted(), true);
auto const workspace = loadFile("emu00006473.nxs");

alphaCalc.setPropertyValue("InputWorkspace", "EMU6473");
alphaCalc.setProperty("InputWorkspace", workspace);
alphaCalc.setPropertyValue("ForwardSpectra", "1");
alphaCalc.setPropertyValue("BackwardSpectra", "17");
alphaCalc.setPropertyValue("FirstGoodValue", "0.3");
Expand Down Expand Up @@ -92,6 +82,21 @@ class AlphaCalcTest : public CxxTest::TestSuite {
void test_incorrect_spectra_numbers() {}

private:
MatrixWorkspace_sptr loadFile(std::string const &filename) {
Mantid::DataHandling::Load loader;
loader.initialize();
loader.setChild(true);
loader.setPropertyValue("Filename", filename);

TS_ASSERT_THROWS_NOTHING(loader.execute());
TS_ASSERT_EQUALS(loader.isExecuted(), true);

TS_ASSERT_EQUALS("LoadMuonNexus", loader.getPropertyValue("LoaderName"));
TS_ASSERT_EQUALS("1", loader.getPropertyValue("LoaderVersion"));

Workspace_sptr outWS = loader.getProperty("OutputWorkspace");
return std::dynamic_pointer_cast<MatrixWorkspace>(outWS);
}

AlphaCalc alphaCalc;
Mantid::DataHandling::LoadMuonNexus2 loader;
};
32 changes: 15 additions & 17 deletions Framework/Muon/test/ApplyDeadTimeCorrTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "MantidAPI/IAlgorithm.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/Workspace.h"
#include "MantidDataHandling/LoadMuonNexus2.h"
#include "MantidDataHandling/Load.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"
Expand Down Expand Up @@ -42,7 +42,7 @@ class ApplyDeadTimeCorrTest : public CxxTest::TestSuite {
}

void testExec() {
MatrixWorkspace_sptr inputWs = loadDataFromFile();
auto const inputWs = loadFile("emu00006473.nxs");
auto deadTimes = makeDeadTimeTable(32);

ApplyDeadTimeCorr applyDeadTime;
Expand Down Expand Up @@ -80,7 +80,7 @@ class ApplyDeadTimeCorrTest : public CxxTest::TestSuite {
}

void testDifferentSize() {
MatrixWorkspace_sptr inputWs = loadDataFromFile();
auto const inputWs = loadFile("emu00006473.nxs");

// Bigger row count than file (expect to fail)
auto deadTimes = makeDeadTimeTable(64);
Expand All @@ -99,7 +99,7 @@ class ApplyDeadTimeCorrTest : public CxxTest::TestSuite {
}

void testSelectedSpectrum() {
MatrixWorkspace_sptr inputWs = loadDataFromFile();
auto const inputWs = loadFile("emu00006473.nxs");

std::shared_ptr<ITableWorkspace> deadTimes = std::make_shared<Mantid::DataObjects::TableWorkspace>();
deadTimes->addColumn("int", "Spectrum Number");
Expand Down Expand Up @@ -173,7 +173,7 @@ class ApplyDeadTimeCorrTest : public CxxTest::TestSuite {
// Test that algorithm throws if input workspace does not contain number of
// good frames
void testNoGoodfrmPresent() {
MatrixWorkspace_sptr inputWs = loadDataFromFile();
auto const inputWs = loadFile("emu00006473.nxs");
auto deadTimes = makeDeadTimeTable(32);

auto &run = inputWs->mutableRun();
Expand Down Expand Up @@ -207,22 +207,20 @@ class ApplyDeadTimeCorrTest : public CxxTest::TestSuite {
return deadTimes;
}

/**
* Loads data from the test data file
* @returns :: Workspace with loaded data
*/
MatrixWorkspace_sptr loadDataFromFile() {
Mantid::DataHandling::LoadMuonNexus2 loader;
MatrixWorkspace_sptr loadFile(std::string const &filename) {
Mantid::DataHandling::Load loader;
loader.initialize();
loader.setChild(true);
loader.setPropertyValue("Filename", "emu00006473.nxs");
loader.setPropertyValue("OutputWorkspace", "__NotUsed");
loader.setPropertyValue("Filename", filename);

TS_ASSERT_THROWS_NOTHING(loader.execute());
TS_ASSERT_EQUALS(loader.isExecuted(), true);
Workspace_sptr data = loader.getProperty("OutputWorkspace");
auto matrixWS = std::dynamic_pointer_cast<MatrixWorkspace>(data);
TS_ASSERT(data);
return matrixWS;

TS_ASSERT_EQUALS("LoadMuonNexus", loader.getPropertyValue("LoaderName"));
TS_ASSERT_EQUALS("1", loader.getPropertyValue("LoaderVersion"));

Workspace_sptr outWS = loader.getProperty("OutputWorkspace");
return std::dynamic_pointer_cast<MatrixWorkspace>(outWS);
}

/// Test dead time value
Expand Down
Loading

0 comments on commit d6096cf

Please sign in to comment.