Mantid
Loading...
Searching...
No Matches
ISISJournalGetExperimentRuns.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2020 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 +
17
18#include <algorithm>
19
20namespace Mantid::DataHandling {
21DECLARE_ALGORITHM(ISISJournalGetExperimentRuns)
22
30
31namespace {
32// These strings define the XML tags for the metadata we are going to return or
33// filter by
34static constexpr const char *INVESTIGATION_ID = "experiment_identifier";
35static constexpr const char *RUN_NAME = "name";
36static constexpr const char *RUN_NUMBER = "run_number";
37static constexpr const char *RUN_TITLE = "title";
38
46std::vector<IJournal::RunData> getRuns(IJournal *journal, std::string const &investigationId) {
47 static auto valuesToLookup = std::vector<std::string>{RUN_NUMBER, RUN_TITLE};
48 auto filters = IJournal::RunData{{INVESTIGATION_ID, investigationId}};
49 auto runs = journal->getRuns(valuesToLookup, filters);
50 return runs;
51}
52
59ITableWorkspace_sptr convertRunDataToTable(const std::vector<IJournal::RunData> &runs) {
60 auto workspace = API::WorkspaceFactory::Instance().createTable("TableWorkspace");
61
62 workspace->addColumn("str", "Name");
63 workspace->addColumn("str", "Run Number");
64 workspace->addColumn("str", "Title");
65
66 for (auto &run : runs) {
67 TableRow row = workspace->appendRow();
68 const auto runNameIt = run.find(RUN_NAME);
69 const auto runNumberIt = run.find(RUN_NUMBER);
70 const auto runTitleIt = run.find(RUN_TITLE);
71 if (runNameIt != run.cend() && runNumberIt != run.cend() && runTitleIt != run.cend())
72 row << runNameIt->second << runNumberIt->second << runTitleIt->second;
73 }
74 return workspace;
75}
76
80std::vector<std::string> getInstruments() {
81 auto instruments = std::vector<std::string>();
82 const auto &instrInfo = ConfigService::Instance().getFacility("ISIS").instruments();
83 std::transform(instrInfo.cbegin(), instrInfo.cend(), std::back_inserter(instruments),
84 [](const auto instrument) { return instrument.name(); });
85 return instruments;
86}
87
93std::string getDefaultInstrument(std::vector<std::string> const &instruments) {
94 if (instruments.empty())
95 return std::string();
96
97 auto instrument = ConfigService::Instance().getInstrument().name();
98 if (std::find(instruments.cbegin(), instruments.cend(), instrument) == instruments.cend()) {
99 instrument = instruments.front();
100 }
101 return instrument;
102}
103} // namespace
104
106 auto const instruments = getInstruments();
107 auto const instrument = getDefaultInstrument(instruments);
108
109 declareProperty("Instrument", instrument, std::make_shared<StringListValidator>(instruments), "The instrument name");
110 declareProperty("Cycle", "", std::make_shared<MandatoryValidator<std::string>>(), "The cycle name, for example 19_4");
111 declareProperty("InvestigationId", "", std::make_shared<MandatoryValidator<std::string>>(),
112 "ID of the selected investigation");
114 std::make_unique<API::WorkspaceProperty<API::ITableWorkspace>>("OutputWorkspace", "", Kernel::Direction::Output),
115 "The name of the workspace to store the run details.");
116}
117
119 auto instrument = getPropertyValue("Instrument");
120 auto cycle = getPropertyValue("Cycle");
121 auto investigationId = getPropertyValue("InvestigationId");
122 auto journal = makeJournal(instrument, cycle);
123
124 auto runs = getRuns(journal.get(), investigationId);
125 auto workspace = convertRunDataToTable(runs);
126 setProperty("OutputWorkspace", workspace);
127}
128
135std::unique_ptr<IJournal> ISISJournalGetExperimentRuns::makeJournal(std::string const &instrument,
136 std::string const &cycle) {
137 return std::make_unique<ISISJournal>(instrument, cycle);
138}
139} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
IJournal: Interface for classes that fetch information from journal files.
Definition: IJournal.h:19
std::map< std::string, std::string > RunData
Definition: IJournal.h:21
TableRow represents a row in a TableWorkspace.
Definition: TableRow.h:39
A property class for workspaces.
void exec() override
Virtual method - must be overridden by concrete algorithm.
virtual std::unique_ptr< API::IJournal > makeJournal(std::string const &instrument, std::string const &cycle=std::string())
Construct a journal; can be overridden by tests to return a mock.
void init() override
Virtual method - must be overridden by concrete algorithm.
ISISJournal: Helper class to aid in fetching ISIS specific run information from journal files.
Definition: ISISJournal.h:31
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
ListValidator is a validator that requires the value of a property to be one of a defined list of pos...
Definition: ListValidator.h:29
Validator to check that a property is not left empty.
Manage the lifetime of a class intended to be a singleton.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
@ Output
An output workspace.
Definition: Property.h:54