22#include <nexus/NeXusFile.hpp>
23#include <nexus/NeXusException.hpp>
31using Types::Core::DateAndTime;
41const string PARAM_IN_FILE(
"Filename");
43const string PARAM_IN_WKSP(
"InputWorkspace");
45const string PARAM_INST_NAME(
"InstrumentName");
47const string PARAM_INST_FILE(
"InstrumentFilename");
49const string PARAM_CHUNK_NAMES(
"ChunkNames");
51const string PARAM_CHUNK_BY(
"ChunkBy");
53const string PARAM_MAX_RECURSE(
"MaxRecursionDepth");
55const string PARAM_OUT_WKSP(
"OutputWorkspace");
57const string PARAM_MAX_BANK_NUM(
"MaxBankNumber");
71 return "Creates chunking at a level of the instrument or instrument "
79 string grp1Name(
"Specify the Instrument");
81 std::vector<std::string> extensions{
"_event.nxs",
".nxs.h5",
".nxs"};
83 "The name of the event nexus file to read, including its full or "
88 "Optional: An input workspace with the instrument we want to use.");
91 "Optional: Name of the instrument to base the ChunkingWorkpace on which "
92 "to base the GroupingWorkspace.");
95 "Optional: Path to the instrument definition file on which to base the "
104 string grp2Name(
"Specify Instrument Components");
107 "Optional: A string of the instrument component names to use "
108 "as separate groups. "
109 "Use / or , to separate multiple groups. "
110 "If empty, then an empty GroupingWorkspace will be created.");
111 vector<string> grouping{
"",
"All",
"Group",
"Column",
"bank"};
112 declareProperty(PARAM_CHUNK_BY,
"", std::make_shared<StringListValidator>(grouping),
113 "Only used if GroupNames is empty: All detectors as one group, Groups "
114 "(East,West for SNAP), Columns for SNAP, detector banks");
120 declareProperty(PARAM_MAX_RECURSE, 5,
"Number of levels to search into the instrument (default=5)");
121 declareProperty(PARAM_MAX_BANK_NUM, 300,
"Maximum bank number to search for in the instrument");
124 "An output workspace describing the cunking.");
129 map<string, string> result;
139 if (!filename.empty())
141 if (!inWSname.empty())
143 if (!instName.empty())
145 if (!instFilename.empty())
151 msg =
"Must specify instrument one way";
152 }
else if (numInst > 1) {
153 msg =
"Can only specify instrument one way";
156 result[PARAM_IN_FILE] = msg;
157 result[PARAM_IN_WKSP] = msg;
158 result[PARAM_INST_NAME] = msg;
159 result[PARAM_INST_FILE] = msg;
166 if (chunkNames.empty() && chunkGroups.empty()) {
167 msg =
"Must specify either " + PARAM_CHUNK_NAMES +
" or " + PARAM_CHUNK_BY;
168 }
else if ((!chunkNames.empty()) && (!chunkGroups.empty())) {
169 msg =
"Must specify either " + PARAM_CHUNK_NAMES +
" or " + PARAM_CHUNK_BY +
" not both";
172 result[PARAM_CHUNK_NAMES] = msg;
173 result[PARAM_CHUNK_BY] = msg;
188 if (str.length() < prefix.length())
191 return (str.substr(0, prefix.length()) == prefix);
206 return comp->getName();
212 return parent->getName();
231 const auto compName = comp->getName();
232 auto it = std::find(names.cbegin(), names.cend(), compName);
233 if (it != names.cend())
239 const auto parName = parent->getName();
241 it = std::find(names.cbegin(), names.cend(), parName);
242 if (it != names.cend())
260 return std::vector<string>();
275 return inWS->getInstrument();
285 if (!filename.empty()) {
286 string top_entry_name(
"entry");
289 size_t n = filename.rfind(
'/');
290 if (
n != std::string::npos) {
291 std::string temp = filename.substr(
n + 1, filename.size() -
n - 1);
293 if (
n != std::string::npos &&
n > 0) {
294 instName = temp.substr(0,
n);
300 NeXus::File nxsfile(filename);
304 nxsfile.openGroup(top_entry_name,
"NXentry");
305 nxsfile.readData(
"start_time", start_time);
306 tempWS->mutableRun().addProperty(
"run_start", DateAndTime(start_time).toISO8601String(),
true);
309 nxsfile.openGroup(
"instrument",
"NXinstrument");
310 nxsfile.readData(
"name", instName);
311 nxsfile.closeGroup();
314 nxsfile.openPath(
"instrument/instrument_xml");
320 loadInst->setPropertyValue(
"Filename", filename);
322 loadInst->setPropertyValue(
"InstrumentParentPath", top_entry_name);
324 }
catch (std::invalid_argument &) {
325 g_log.
error(
"Invalid argument to LoadIDFFromNexus Child Algorithm ");
326 }
catch (std::runtime_error &) {
327 g_log.
debug(
"No instrument definition found in " + filename +
" at " + top_entry_name +
"/instrument");
330 if (loadInst->isExecuted())
331 return tempWS->getInstrument();
335 }
catch (::NeXus::Exception &) {
336 g_log.
information(
"No instrument definition found in " + filename +
" at " + top_entry_name +
"/instrument");
345 childAlg->setPropertyValue(
"Filename", instFilename);
346 childAlg->setPropertyValue(
"InstrumentName", instName);
348 childAlg->executeAsChildAlg();
349 return tempWS->getInstrument();
361 strategy->addColumn(
"str",
"BankName");
367 if (groupLevel ==
"All") {
369 }
else if (inst->getName() ==
"SNAP" && groupLevel ==
"Group") {
371 groupNames.emplace_back(
"East");
372 groupNames.emplace_back(
"West");
376 int maxBankNum = this->
getProperty(PARAM_MAX_BANK_NUM);
380 int maxRecurseDepth = this->
getProperty(PARAM_MAX_RECURSE);
381 map<string, vector<string>> grouping;
383 PRAGMA_OMP(parallel
for schedule(dynamic, 1) )
384 for (
int num = 0; num < maxBankNum; ++num) {
387 mess <<
"bank" << num;
393 if (groupNames.empty()) {
400 if (!parent.empty()) {
401 grouping.try_emplace(parent, vector<string>());
402 grouping[parent].emplace_back(comp->getName());
411 if (grouping.empty())
412 throw std::runtime_error(
"Failed to find any banks in the instrument");
415 for (
auto &group : grouping) {
417 for (
const auto &bank : group.second) {
418 banks << bank <<
",";
421 string banksStr = banks.str();
422 banksStr = banksStr.substr(0, banksStr.size() - 1);
425 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.
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.