17#include <boost/algorithm/string/detail/classification.hpp>
18#include <boost/algorithm/string/split.hpp>
19#include <boost/algorithm/string/trim.hpp>
32using namespace Kernel;
42 std::make_shared<InstrumentValidator>()),
43 "A workspace that contains a reference to the instrument of interest. "
44 "You can use LoadEmptyInstrument to create such a workspace.");
46 "The name of the output CalFile");
48 "A string of the instrument component names to use as separate groups. "
49 "/ or , can be used to separate multiple groups.");
71 std::vector<std::string> vgroups;
72 boost::split(vgroups, groupsname, boost::algorithm::detail::is_any_ofF<char>(
",/*"));
73 if (vgroups.empty()) {
74 g_log.
error(
"Could not determine group names. Group names should be "
75 "separated by / or ,");
76 throw std::runtime_error(
"Could not determine group names. Group names "
77 "should be separated by / or ,");
81 std::map<std::string, int> group_map;
83 for (
auto &vgroup : vgroups) {
85 group_map[vgroup] = ++
index;
92 using sptr_ICompAss = std::shared_ptr<const Geometry::ICompAssembly>;
93 using sptr_IComp = std::shared_ptr<const Geometry::IComponent>;
94 using sptr_IDet = std::shared_ptr<const Geometry::IDetector>;
95 std::queue<std::pair<sptr_ICompAss, int>> assemblies;
96 sptr_ICompAss current = std::dynamic_pointer_cast<const Geometry::ICompAssembly>(inst);
98 sptr_IComp currentIComp;
99 sptr_ICompAss currentchild;
101 int top_group, child_group;
104 top_group = group_map[current->getName()];
105 assemblies.emplace(current, top_group);
108 std::string filename =
getProperty(
"GroupingFilename");
114 Progress prog(
this, 0.0, 0.8, assemblies.size());
115 while (!assemblies.empty())
117 current = assemblies.front().first;
118 top_group = assemblies.front().second;
120 int nchilds = current->nelements();
122 for (
int i = 0; i < nchilds; ++i) {
123 currentIComp = (*(current.get()))[i];
124 currentDet = std::dynamic_pointer_cast<const Geometry::IDetector>(currentIComp);
125 if (currentDet.get())
128 instrcalib[currentDet->getID()] = std::make_pair(number++, top_group);
130 instrcalib[number++] = std::make_pair(currentDet->getID(), top_group);
133 currentchild = std::dynamic_pointer_cast<const Geometry::ICompAssembly>(currentIComp);
134 if (currentchild.get()) {
135 child_group = group_map[currentchild->getName()];
136 if (child_group == 0)
137 child_group = top_group;
138 assemblies.emplace(currentchild, child_group);
151 std::ifstream file(filename.c_str());
156 std::ostringstream mess;
157 mess <<
"Calibration file " << filename <<
" already exist. Only grouping will be modified";
164 std::ostringstream message;
165 std::fstream outfile;
169 outfile.open(filename.c_str(), std::ios::out);
170 if (!outfile.is_open()) {
171 message <<
"Can't open Calibration File: " << filename;
173 throw std::runtime_error(message.str());
176 infile.open(filename.c_str(), std::ios::in);
177 std::string newfilename = filename +
"2";
178 outfile.open(newfilename.c_str(), std::ios::out);
179 if (!infile.is_open()) {
180 message <<
"Can't open input Calibration File: " << filename;
182 throw std::runtime_error(message.str());
184 if (!outfile.is_open()) {
185 message <<
"Can't open new Calibration File: " << newfilename;
187 throw std::runtime_error(message.str());
195 int number, udet, select,
group;
199 while (getline(infile, str)) {
200 if (str.empty() || str[0] ==
'#')
202 std::istringstream istr(str);
203 istr >> number >> udet >> offset >> select >>
group;
208 group = ((*it).second).second;
226 os << std::fixed << std::setw(9) << number << std::fixed << std::setw(15) << udet << std::fixed
227 << std::setprecision(7) << std::setw(15) << offset << std::fixed << std::setw(8) << select << std::fixed
228 << std::setw(8) <<
group <<
"\n";
233 os <<
"# Diffraction focusing calibration file created by Mantid"
235 os <<
"# Detectors have been grouped using assembly names:" <<
groups <<
"\n";
237 os <<
"# Template file " << filename <<
" has been used"
239 os <<
"# Only grouping has been changed, offset from template file have "
243 os <<
"# No template file, all offsets set to 0.0 and select to 1"
246 os <<
"# Number UDET offset select group"
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
A specialized class for dealing with file properties.
@ Save
to specify a file to write to, the file may or may not exist
Helper class for reporting progress from algorithms.
A property class for workspaces.
void saveGroupingFile(const std::string &, bool overwrite) const
Creates and saves the output file.
void writeHeaders(std::ostream &os, const std::string &filename, bool overwrite) const
Writes out the header to the output file.
std::string groups
The names of the groups.
bool groupingFileDoesExist(const std::string &filename) const
Determine whether the grouping file already exists.
instrcalmap instrcalib
Calibration map used if the *.cal file exist.
void exec() override
Execution code.
static void writeCalEntry(std::ostream &os, int number, int udet, double offset, int select, int group)
Writes a single calibration line to the output file.
void init() override
Initialisation code.
void error(const std::string &msg)
Logs at error level.
void information(const std::string &msg)
Logs at information level.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
@ Input
An input workspace.