Mantid
Loading...
Searching...
No Matches
QENSFitUtilities.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 +
8#include <unordered_map>
9#include <utility>
10
11namespace Mantid::API {
12
13void renameWorkspacesWith(const WorkspaceGroup_sptr &groupWorkspace,
14 std::function<std::string(std::size_t)> const &getName,
15 std::function<void(Workspace_sptr, const std::string &)> const &renamer) {
16 std::unordered_map<std::string, std::size_t> nameCount;
17 for (auto i = 0u; i < groupWorkspace->size(); ++i) {
18 const auto name = getName(i);
19 auto count = nameCount.find(name);
20
21 if (count == nameCount.end()) {
22 renamer(groupWorkspace->getItem(i), name);
23 nameCount[name] = 1;
24 } else
25 renamer(groupWorkspace->getItem(i), name + "(" + std::to_string(++count->second) + ")");
26 }
27}
28
29void renameWorkspace(const IAlgorithm_sptr &renamer, const Workspace_sptr &workspace, const std::string &newName) {
30 renamer->setProperty("InputWorkspace", workspace);
31 renamer->setProperty("OutputWorkspace", newName);
32 renamer->executeAsChildAlg();
33}
34
35bool containsMultipleData(const std::vector<MatrixWorkspace_sptr> &workspaces) {
36 const auto &first = workspaces.front();
37 return std::any_of(workspaces.cbegin(), workspaces.cend(),
38 [&first](const auto &workspace) { return workspace != first; });
39}
40
42 const WorkspaceGroup_sptr &outputGroup, std::string const &outputBaseName,
43 std::string const &, std::function<std::string(std::size_t)> const &getNameSuffix) {
44 Progress renamerProg(qensFit, 0.98, 1.0, outputGroup->size() + 1);
45 renamerProg.report("Renaming group workspaces...");
46
47 auto getName = [&](std::size_t i) {
48 std::string name = outputBaseName + "_" + getNameSuffix(i);
49 return name;
50 };
51
52 auto renamer = [&](const Workspace_sptr &workspace, const std::string &name) {
53 renameWorkspace(renameAlgorithm, workspace, name);
54 renamerProg.report("Renamed workspace in group.");
55 };
56 renameWorkspacesWith(outputGroup, getName, renamer);
57}
58
59} // namespace Mantid::API
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
int count
counter
Definition: Matrix.cpp:37
std::string getName(const IMDDimension &self)
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
void renameWorkspacesInQENSFit(Algorithm *qensFit, IAlgorithm_sptr renameAlgorithm, const WorkspaceGroup_sptr &outputGroup, std::string const &outputBaseName, std::string const &groupSuffix, std::function< std::string(std::size_t)> const &getNameSuffix)
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
void renameWorkspacesWith(const WorkspaceGroup_sptr &groupWorkspace, std::function< std::string(std::size_t)> const &getName, std::function< void(Workspace_sptr, const std::string &)> const &renamer)
void renameWorkspace(const IAlgorithm_sptr &renamer, const Workspace_sptr &workspace, const std::string &newName)
bool containsMultipleData(const std::vector< MatrixWorkspace_sptr > &workspaces)
std::string to_string(const wide_integer< Bits, Signed > &n)