Mantid
Loading...
Searching...
No Matches
UnGroupWorkspace.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
11
12namespace Mantid::Algorithms {
13
14DECLARE_ALGORITHM(UnGroupWorkspace)
15
16using namespace Kernel;
17using namespace API;
18
22 // Get the list of workspaces in the ADS
23 auto workspaceList = data_store.getObjectNames();
24 std::unordered_set<std::string> groupWorkspaceList;
25 // Not iterate over, removing all those which are not group workspaces
26 for (const auto &name : workspaceList) {
27 WorkspaceGroup_const_sptr group = std::dynamic_pointer_cast<const WorkspaceGroup>(data_store.retrieve(name));
28 // RNT: VC returns bad pointer after erase
29 // if ( !group ) workspaceList.erase(it);
30 if (group) {
31 groupWorkspaceList.insert(name);
32 }
33 }
34 // Declare a text property with the list of group workspaces as its allowed
35 // values
36 declareProperty("InputWorkspace", "", "Name of the input workspace to ungroup",
37 std::make_shared<StringListValidator>(groupWorkspaceList));
38}
39
44 const std::string inputws = getProperty("InputWorkspace");
46
47 // Retrieve the input workspace
48 Workspace_sptr wsSptr = data_store.retrieve(inputws);
49 // Try to cast it to a WorkspaceGroup
50 WorkspaceGroup_sptr wsGrpSptr = std::dynamic_pointer_cast<WorkspaceGroup>(wsSptr);
51 // Test the cast succeeded - it always should because of ListValidator on
52 // input property
53 if (!wsGrpSptr) {
54 throw std::runtime_error("Selected Workspace is not a WorkspaceGroup");
55 }
56 // Notify observers that a WorkspaceGroup is about to be unrolled
57 data_store.notificationCenter.postNotification(new Mantid::API::WorkspaceUnGroupingNotification(inputws, wsSptr));
58 // Now remove the WorkspaceGroup from the ADS
59 data_store.remove(inputws);
60}
61} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
UnGroupingWorkspace notification is sent from UnGroupWorkspace algorithm before the WorkspaceGroup is...
The Analysis data service stores instances of the Workspace objects and anything that derives from te...
virtual void remove(const std::string &name)
Overridden remove member to delete its name held by the workspace itself.
void exec() override
overridden execute method
void init() override
Overridden Init method.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
std::vector< std::string > getObjectNames(DataServiceSort sortState=DataServiceSort::Unsorted, DataServiceHidden hiddenState=DataServiceHidden::Auto, const std::string &contain="") const
Returns a vector of strings containing all object names in the ADS.
Definition: DataService.h:398
std::shared_ptr< T > retrieve(const std::string &name) const
Get a shared pointer to a stored data object.
Definition: DataService.h:350
Poco::NotificationCenter notificationCenter
Sends notifications to observers.
Definition: DataService.h:484
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< const WorkspaceGroup > WorkspaceGroup_const_sptr
shared pointer to Mantid::API::WorkspaceGroup, pointer to const version