20#include "MantidNexus/NexusFile.h"
29using Types::Core::DateAndTime;
39const string PARAM_IN_FILE(
"Filename");
41const string PARAM_IN_WKSP(
"InputWorkspace");
43const string PARAM_INST_NAME(
"InstrumentName");
45const string PARAM_INST_FILE(
"InstrumentFilename");
47const string PARAM_CHUNK_NAMES(
"ChunkNames");
49const string PARAM_CHUNK_BY(
"ChunkBy");
51const string PARAM_MAX_RECURSE(
"MaxRecursionDepth");
53const string PARAM_OUT_WKSP(
"OutputWorkspace");
55const string PARAM_MAX_BANK_NUM(
"MaxBankNumber");
69 return "Creates chunking at a level of the instrument or instrument "
77 string grp1Name(
"Specify the Instrument");
79 std::vector<std::string> extensions{
"_event.nxs",
".nxs.h5",
".nxs"};
81 "The name of the event nexus file to read, including its full or "
86 "Optional: An input workspace with the instrument we want to use.");
89 "Optional: Name of the instrument to base the ChunkingWorkpace on which "
90 "to base the GroupingWorkspace.");
93 "Optional: Path to the instrument definition file on which to base the "
102 string grp2Name(
"Specify Instrument Components");
105 "Optional: A string of the instrument component names to use "
106 "as separate groups. "
107 "Use / or , to separate multiple groups. "
108 "If empty, then an empty GroupingWorkspace will be created.");
109 vector<string> grouping{
"",
"All",
"Group",
"Column",
"bank"};
110 declareProperty(PARAM_CHUNK_BY,
"", std::make_shared<StringListValidator>(grouping),
111 "Only used if GroupNames is empty: All detectors as one group, Groups "
112 "(East,West for SNAP), Columns for SNAP, detector banks");
118 declareProperty(PARAM_MAX_RECURSE, 5,
"Number of levels to search into the instrument (default=5)");
119 declareProperty(PARAM_MAX_BANK_NUM, 300,
"Maximum bank number to search for in the instrument");
122 "An output workspace describing the cunking.");
127 map<string, string> result;
137 if (!filename.empty())
139 if (!inWSname.empty())
141 if (!instName.empty())
143 if (!instFilename.empty())
149 msg =
"Must specify instrument one way";
150 }
else if (numInst > 1) {
151 msg =
"Can only specify instrument one way";
154 result[PARAM_IN_FILE] = msg;
155 result[PARAM_IN_WKSP] = msg;
156 result[PARAM_INST_NAME] = msg;
157 result[PARAM_INST_FILE] = msg;
164 if (chunkNames.empty() && chunkGroups.empty()) {
165 msg =
"Must specify either " + PARAM_CHUNK_NAMES +
" or " + PARAM_CHUNK_BY;
166 }
else if ((!chunkNames.empty()) && (!chunkGroups.empty())) {
167 msg =
"Must specify either " + PARAM_CHUNK_NAMES +
" or " + PARAM_CHUNK_BY +
" not both";
170 result[PARAM_CHUNK_NAMES] = msg;
171 result[PARAM_CHUNK_BY] = msg;
186 if (str.length() < prefix.length())
189 return (str.substr(0, prefix.length()) == prefix);
204 return comp->getName();
210 return parent->getName();
229 const auto compName = comp->getName();
230 auto it = std::find(names.cbegin(), names.cend(), compName);
231 if (it != names.cend())
237 const auto parName = parent->getName();
239 it = std::find(names.cbegin(), names.cend(), parName);
240 if (it != names.cend())
258 return std::vector<string>();
273 return inWS->getInstrument();
283 if (!filename.empty()) {
284 string top_entry_name(
"entry");
287 size_t n = filename.rfind(
'/');
288 if (
n != std::string::npos) {
289 std::string temp = filename.substr(
n + 1, filename.size() -
n - 1);
291 if (
n != std::string::npos &&
n > 0) {
292 instName = temp.substr(0,
n);
298 Nexus::File nxsfile(filename);
302 nxsfile.openGroup(top_entry_name,
"NXentry");
303 nxsfile.readData(
"start_time", start_time);
304 tempWS->mutableRun().addProperty(
"run_start", DateAndTime(start_time).toISO8601String(),
true);
307 nxsfile.openGroup(
"instrument",
"NXinstrument");
308 nxsfile.readData(
"name", instName);
309 nxsfile.closeGroup();
312 nxsfile.openAddress(
"instrument/instrument_xml");
318 loadInst->setPropertyValue(
"Filename", filename);
320 loadInst->setPropertyValue(
"InstrumentParentPath", top_entry_name);
322 }
catch (std::invalid_argument &) {
323 g_log.
error(
"Invalid argument to LoadIDFFromNexus Child Algorithm ");
324 }
catch (std::runtime_error &) {
325 g_log.
debug(
"No instrument definition found in " + filename +
" at " + top_entry_name +
"/instrument");
328 if (loadInst->isExecuted())
329 return tempWS->getInstrument();
334 g_log.
information(
"No instrument definition found in " + filename +
" at " + top_entry_name +
"/instrument");
343 childAlg->setPropertyValue(
"Filename", instFilename);
344 childAlg->setPropertyValue(
"InstrumentName", instName);
346 childAlg->executeAsChildAlg();
347 return tempWS->getInstrument();
359 strategy->addColumn(
"str",
"BankName");
365 if (groupLevel ==
"All") {
367 }
else if (inst->getName() ==
"SNAP" && groupLevel ==
"Group") {
369 groupNames.emplace_back(
"East");
370 groupNames.emplace_back(
"West");
374 int maxBankNum = this->
getProperty(PARAM_MAX_BANK_NUM);
378 int maxRecurseDepth = this->
getProperty(PARAM_MAX_RECURSE);
379 map<string, vector<string>> grouping;
381 PRAGMA_OMP(parallel
for schedule(dynamic, 1) )
382 for (
int num = 0; num < maxBankNum; ++num) {
385 mess <<
"bank" << num;
391 if (groupNames.empty()) {
398 if (!parent.empty()) {
399 grouping.try_emplace(parent, vector<string>());
400 grouping[parent].emplace_back(comp->getName());
409 if (grouping.empty())
410 throw std::runtime_error(
"Failed to find any banks in the instrument");
413 for (
const auto &
group : grouping) {
415 for (
const auto &bank :
group.second) {
416 banks << bank <<
",";
419 string banksStr = banks.str();
423 TableRow row = strategy->appendRow();
#define DECLARE_ALGORITHM(classname)
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
#define PARALLEL_CRITICAL(name)
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PRAGMA_OMP(expression)
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
Helper class for reporting progress from algorithms.
TableRow represents a row in a TableWorkspace.
A property class for workspaces.
CreateChunkingFromInstrument : TODO: DESCRIPTION.
Geometry::Instrument_const_sptr getInstrument()
Determine the instrument from the various input parameters.
void exec() override
Execute the algorithm.
int version() const override
Algorithm's version for identification.
void init() override
Initialize the algorithm's properties.
std::map< std::string, std::string > validateInputs() override
Perform validation of ALL the input properties of the algorithm.
const std::string summary() const override
Algorithm's summary for identification.
const std::string category() const override
Algorithm's category for identification.
const std::string name() const override
Algorithm's name for identification.
Concrete workspace implementation.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
void debug(const std::string &msg)
Logs at debug level.
void error(const std::string &msg)
Logs at error level.
void information(const std::string &msg)
Logs at information level.
OptionalBool : Tri-state bool.
The concrete, templated class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
@ TOK_TRIM
remove leading and trailing whitespace from tokens
const TokenVec & asVector()
Returns a vector of tokenized strings.
Class that provides for a standard Nexus exception.
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
bool startsWith(const string &str, const string &prefix)
Returns true if str starts with prefix.
vector< string > getGroupNames(const string &names)
Split a list of instrument components into a vector of strings.
string parentName(const IComponent_const_sptr &comp, const string &prefix)
Find the name of the parent of the component that starts with the supplied prefix.
std::shared_ptr< const IComponent > IComponent_const_sptr
Typdef of a shared pointer to a const IComponent.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
@ Input
An input workspace.
@ Output
An output workspace.