Skip to content

Commit 375996c

Browse files
Merge pull request #38479 from peterfpeterson/muon-loader-confidence
Move muon loader confidence check out of exec method - ornl-next
2 parents 18ada7e + dceeace commit 375996c

30 files changed

+203
-204
lines changed

Framework/DataHandling/inc/MantidDataHandling/LoadMuonNexus1.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,7 @@ together based on the groupings in the NeXus file. </LI>
5454
*/
5555
class MANTID_DATAHANDLING_DLL LoadMuonNexus1 : public LoadMuonNexus {
5656
public:
57-
/// Default constructor
5857
LoadMuonNexus1();
59-
/// Algorithm's name for identification overriding a virtual method
60-
const std::string name() const override { return "LoadMuonNexus"; }
61-
/// Summary of algorithms purpose
62-
const std::string summary() const override {
63-
return "The LoadMuonNexus algorithm will read the given NeXus Muon data "
64-
"file Version 1 and use the results to populate the named "
65-
"workspace. LoadMuonNexus may be invoked by LoadNexus if it is "
66-
"given a NeXus file of this type.";
67-
}
68-
69-
/// Algorithm's version for identification overriding a virtual method
70-
int version() const override { return 1; }
71-
/// Algorithm's category for identification overriding a virtual method
72-
const std::string category() const override { return "DataHandling\\Nexus;Muon\\DataHandling"; }
7358

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

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

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

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

10086
/// Creates Detector Grouping Table using all the data from the range
101-
DataObjects::TableWorkspace_sptr createDetectorGroupingTable(std::vector<int> specToLoad, std::vector<int> grouping);
87+
DataObjects::TableWorkspace_sptr createDetectorGroupingTable(std::vector<int> const &specToLoad,
88+
std::vector<int> const &grouping);
10289
};
10390

10491
} // namespace DataHandling

Framework/DataHandling/inc/MantidDataHandling/LoadMuonNexus2.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,23 @@ together based on the groupings in the NeXus file. </LI>
4646
*/
4747
class MANTID_DATAHANDLING_DLL LoadMuonNexus2 : public LoadMuonNexus {
4848
public:
49-
/// Default constructor
5049
LoadMuonNexus2();
51-
/// Algorithm's name for identification overriding a virtual method
52-
const std::string name() const override { return "LoadMuonNexus"; }
53-
/// Summary of algorithms purpose
50+
5451
const std::string summary() const override {
5552
return "The LoadMuonNexus algorithm will read the given NeXus Muon data "
5653
"file Version 2 and use the results to populate the named "
5754
"workspace. LoadMuonNexus may be invoked by LoadNexus if it is "
5855
"given a NeXus file of this type.";
5956
}
6057

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

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

7064
private:
71-
/// Overwrites Algorithm method
7265
void exec() override;
73-
/// Execute this version of the algorithm
74-
void doExec();
7566

7667
HistogramData::Histogram loadData(const Mantid::HistogramData::BinEdges &edges, const Mantid::NeXus::NXInt &counts,
7768
int period, int spec);

Framework/DataHandling/src/LoadMuonNexus1.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ Workspace_sptr LoadMuonNexus1::loadDetectorGrouping(NXRoot &root, const Geometry
516516
[&groupingData](const auto spec) { return groupingData[spec - 1]; });
517517
} else {
518518
// User selected an entry number
519-
for (auto &spec : specToLoad) {
519+
for (auto const &spec : specToLoad) {
520520
int index = spec - 1 + static_cast<int>((m_entrynumber - 1) * m_numberOfSpectra);
521521
grouping.emplace_back(groupingData[index]);
522522
}
@@ -548,7 +548,7 @@ Workspace_sptr LoadMuonNexus1::loadDetectorGrouping(NXRoot &root, const Geometry
548548

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

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

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

641-
for (auto &group : groupingMap) {
642+
for (auto const &group : groupingMap) {
642643
if (group.first != 0) // Skip 0 group
643644
{
644645
TableRow newRow = detectorGroupingTable->appendRow();
@@ -658,7 +659,7 @@ TableWorkspace_sptr LoadMuonNexus1::createDetectorGroupingTable(std::vector<int>
658659
* @param localWorkspace :: A pointer to the workspace in which the data will
659660
* be stored
660661
*/
661-
void LoadMuonNexus1::loadData(size_t hist, specnum_t &i, specnum_t specNo, MuonNexusReader &nxload,
662+
void LoadMuonNexus1::loadData(size_t hist, specnum_t const &i, specnum_t specNo, MuonNexusReader &nxload,
662663
const int64_t lengthIn, const DataObjects::Workspace2D_sptr &localWorkspace) {
663664
// Read in a spectrum
664665
// Put it into a vector, discarding the 1st entry, which is rubbish

Framework/DataHandling/src/LoadMuonNexus2.cpp

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ using Mantid::HistogramData::Histogram;
5353
using namespace Mantid::NeXus;
5454
using Mantid::Types::Core::DateAndTime;
5555

56-
namespace {
57-
inline std::string getLoadAlgName(int confidence1, int confidenceV2) {
58-
return confidence1 > confidenceV2 ? "LoadMuonNexus" : "LoadMuonNexusV2";
59-
}
60-
} // namespace
61-
62-
/// Empty default constructor
6356
LoadMuonNexus2::LoadMuonNexus2() : LoadMuonNexus() {}
6457

6558
/** Executes the right version of the Muon nexus loader
@@ -68,50 +61,6 @@ LoadMuonNexus2::LoadMuonNexus2() : LoadMuonNexus() {}
6861
*values
6962
*/
7063
void LoadMuonNexus2::exec() {
71-
LoadMuonNexus1 load1;
72-
LoadMuonNexusV2 loadV2;
73-
load1.initialize();
74-
loadV2.initialize();
75-
76-
std::string filePath = getPropertyValue("Filename");
77-
Kernel::NexusDescriptor descriptor(filePath);
78-
int confidence1 = load1.confidence(descriptor);
79-
int confidence2 = this->confidence(descriptor);
80-
int confidenceV2 = 0;
81-
// If the file is hdf5 then we can possibly use LoadMuonNexusV2
82-
// To check this we'll have to create an HDF5 descriptor for the file.
83-
if (Kernel::NexusDescriptor::isReadable(filePath, Kernel::NexusDescriptor::Version5)) {
84-
Kernel::NexusHDF5Descriptor descriptorHDF5(filePath);
85-
confidenceV2 = loadV2.confidence(descriptorHDF5);
86-
};
87-
88-
// if none can load the file throw
89-
if (confidence1 < 80 && confidence2 < 80 && confidenceV2 < 80) {
90-
throw Kernel::Exception::FileError("Cannot open the file ", filePath);
91-
}
92-
// Now pick the correct alg
93-
if (confidence2 > std::max(confidence1, confidenceV2)) {
94-
// Use this loader
95-
doExec();
96-
} else {
97-
// Version 1 or V2
98-
auto childAlg = createChildAlgorithm(getLoadAlgName(confidence1, confidenceV2), 0, 1, true, 1);
99-
auto loader = std::dynamic_pointer_cast<API::Algorithm>(childAlg);
100-
loader->copyPropertiesFrom(*this);
101-
loader->executeAsChildAlg();
102-
this->copyPropertiesFrom(*loader);
103-
API::Workspace_sptr outWS = loader->getProperty("OutputWorkspace");
104-
setProperty("OutputWorkspace", outWS);
105-
}
106-
}
107-
108-
/** Read in a muon nexus file of the version 2.
109-
*
110-
* @throw Exception::FileError If the Nexus file cannot be found/opened
111-
* @throw std::invalid_argument If the optional properties are set to invalid
112-
*values
113-
*/
114-
void LoadMuonNexus2::doExec() {
11564
// Create the root Nexus class
11665
NXRoot root(getPropertyValue("Filename"));
11766

Framework/DataHandling/src/LoadNexus.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ void LoadNexus::exec() {
112112
}
113113

114114
void LoadNexus::runLoadMuonNexus() {
115-
auto loadMuonNexus = createChildAlgorithm("LoadMuonNexus", 0., 1.);
115+
// The Load algorithm will choose the correct LoadMuonNexus algorithm
116+
auto loadMuonNexus = createChildAlgorithm("Load", 0., 1.);
117+
116118
// Pass through the same input filename
117119
loadMuonNexus->setPropertyValue("Filename", m_filename);
118120
// Set the workspace property

Framework/Muon/test/AlphaCalcTest.h

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "MantidAPI/IAlgorithm.h"
1313
#include "MantidAPI/Workspace.h"
1414
#include "MantidDataHandling/GroupDetectors.h"
15+
#include "MantidDataHandling/Load.h"
1516
#include "MantidDataHandling/LoadInstrument.h"
1617
#include "MantidDataHandling/LoadMuonNexus2.h"
1718
#include "MantidDataObjects/Workspace2D.h"
@@ -33,15 +34,9 @@ class AlphaCalcTest : public CxxTest::TestSuite {
3334
}
3435

3536
void testCalAlphaManySpectra() {
36-
// system("pause");
37-
// Load the muon nexus file
38-
loader.initialize();
39-
loader.setPropertyValue("Filename", "emu00006473.nxs");
40-
loader.setPropertyValue("OutputWorkspace", "EMU6473");
41-
TS_ASSERT_THROWS_NOTHING(loader.execute());
42-
TS_ASSERT_EQUALS(loader.isExecuted(), true);
37+
auto const workspace = loadFile("emu00006473.nxs");
4338

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

5853
void testCalAlphaTwoSpectra() {
59-
// Load the muon nexus file
60-
loader.initialize();
61-
loader.setPropertyValue("Filename", "emu00006473.nxs");
62-
loader.setPropertyValue("OutputWorkspace", "EMU6473");
63-
TS_ASSERT_THROWS_NOTHING(loader.execute());
64-
TS_ASSERT_EQUALS(loader.isExecuted(), true);
54+
auto const workspace = loadFile("emu00006473.nxs");
6555

66-
alphaCalc.setPropertyValue("InputWorkspace", "EMU6473");
56+
alphaCalc.setProperty("InputWorkspace", workspace);
6757
alphaCalc.setPropertyValue("ForwardSpectra", "1");
6858
alphaCalc.setPropertyValue("BackwardSpectra", "17");
6959
alphaCalc.setPropertyValue("FirstGoodValue", "0.3");
@@ -92,6 +82,21 @@ class AlphaCalcTest : public CxxTest::TestSuite {
9282
void test_incorrect_spectra_numbers() {}
9383

9484
private:
85+
MatrixWorkspace_sptr loadFile(std::string const &filename) {
86+
Mantid::DataHandling::Load loader;
87+
loader.initialize();
88+
loader.setChild(true);
89+
loader.setPropertyValue("Filename", filename);
90+
91+
TS_ASSERT_THROWS_NOTHING(loader.execute());
92+
TS_ASSERT_EQUALS(loader.isExecuted(), true);
93+
94+
TS_ASSERT_EQUALS("LoadMuonNexus", loader.getPropertyValue("LoaderName"));
95+
TS_ASSERT_EQUALS("1", loader.getPropertyValue("LoaderVersion"));
96+
97+
Workspace_sptr outWS = loader.getProperty("OutputWorkspace");
98+
return std::dynamic_pointer_cast<MatrixWorkspace>(outWS);
99+
}
100+
95101
AlphaCalc alphaCalc;
96-
Mantid::DataHandling::LoadMuonNexus2 loader;
97102
};

Framework/Muon/test/ApplyDeadTimeCorrTest.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "MantidAPI/IAlgorithm.h"
1313
#include "MantidAPI/TableRow.h"
1414
#include "MantidAPI/Workspace.h"
15-
#include "MantidDataHandling/LoadMuonNexus2.h"
15+
#include "MantidDataHandling/Load.h"
1616
#include "MantidDataObjects/TableWorkspace.h"
1717
#include "MantidDataObjects/Workspace2D.h"
1818
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"
@@ -42,7 +42,7 @@ class ApplyDeadTimeCorrTest : public CxxTest::TestSuite {
4242
}
4343

4444
void testExec() {
45-
MatrixWorkspace_sptr inputWs = loadDataFromFile();
45+
auto const inputWs = loadFile("emu00006473.nxs");
4646
auto deadTimes = makeDeadTimeTable(32);
4747

4848
ApplyDeadTimeCorr applyDeadTime;
@@ -80,7 +80,7 @@ class ApplyDeadTimeCorrTest : public CxxTest::TestSuite {
8080
}
8181

8282
void testDifferentSize() {
83-
MatrixWorkspace_sptr inputWs = loadDataFromFile();
83+
auto const inputWs = loadFile("emu00006473.nxs");
8484

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

101101
void testSelectedSpectrum() {
102-
MatrixWorkspace_sptr inputWs = loadDataFromFile();
102+
auto const inputWs = loadFile("emu00006473.nxs");
103103

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

179179
auto &run = inputWs->mutableRun();
@@ -207,22 +207,20 @@ class ApplyDeadTimeCorrTest : public CxxTest::TestSuite {
207207
return deadTimes;
208208
}
209209

210-
/**
211-
* Loads data from the test data file
212-
* @returns :: Workspace with loaded data
213-
*/
214-
MatrixWorkspace_sptr loadDataFromFile() {
215-
Mantid::DataHandling::LoadMuonNexus2 loader;
210+
MatrixWorkspace_sptr loadFile(std::string const &filename) {
211+
Mantid::DataHandling::Load loader;
216212
loader.initialize();
217213
loader.setChild(true);
218-
loader.setPropertyValue("Filename", "emu00006473.nxs");
219-
loader.setPropertyValue("OutputWorkspace", "__NotUsed");
214+
loader.setPropertyValue("Filename", filename);
215+
220216
TS_ASSERT_THROWS_NOTHING(loader.execute());
221217
TS_ASSERT_EQUALS(loader.isExecuted(), true);
222-
Workspace_sptr data = loader.getProperty("OutputWorkspace");
223-
auto matrixWS = std::dynamic_pointer_cast<MatrixWorkspace>(data);
224-
TS_ASSERT(data);
225-
return matrixWS;
218+
219+
TS_ASSERT_EQUALS("LoadMuonNexus", loader.getPropertyValue("LoaderName"));
220+
TS_ASSERT_EQUALS("1", loader.getPropertyValue("LoaderVersion"));
221+
222+
Workspace_sptr outWS = loader.getProperty("OutputWorkspace");
223+
return std::dynamic_pointer_cast<MatrixWorkspace>(outWS);
226224
}
227225

228226
/// Test dead time value

0 commit comments

Comments
 (0)