17#include <Poco/DOM/DOMParser.h>
18#include <Poco/DOM/Document.h>
19#include <Poco/DOM/Element.h>
20#include <boost/algorithm/string/detail/classification.hpp>
21#include <boost/algorithm/string/split.hpp>
30using namespace Kernel;
32using namespace Geometry;
33using namespace DataObjects;
40 "The workspace containing the geometry to be calibrated.");
42 "The name of the output [[CalFile]]");
56 throw std::invalid_argument(
"No InputWorkspace");
61 throw std::invalid_argument(
"The InputWorkspace does not have an instrument definition");
63 std::string instname = inst->getName();
69 std::vector<std::string> vgroups;
70 boost::split(vgroups, instname, boost::algorithm::detail::is_any_ofF<char>(
",/*"));
71 if (vgroups.empty()) {
72 g_log.
error(
"Could not determine group names. Group names should be "
73 "separated by / or ,");
74 throw std::runtime_error(
"Could not determine group names. Group names "
75 "should be separated by / or ,");
79 std::map<std::string, int> group_map;
81 for (std::vector<std::string>::const_iterator it = vgroups.begin(); it != vgroups.end(); ++it)
82 group_map[(*it)] = ++
index;
88 using sptr_ICompAss = std::shared_ptr<const Geometry::ICompAssembly>;
89 using sptr_IComp = std::shared_ptr<const Geometry::IComponent>;
90 using sptr_IDet = std::shared_ptr<const Geometry::IDetector>;
91 std::queue<std::pair<sptr_ICompAss, int>> assemblies;
92 sptr_ICompAss current = std::dynamic_pointer_cast<const Geometry::ICompAssembly>(inst);
94 sptr_IComp currentIComp;
95 sptr_ICompAss currentchild;
97 int top_group, child_group;
100 top_group = group_map[current->getName()];
101 assemblies.emplace(current, top_group);
107 Progress prog(
this, 0.0, 0.8, assemblies.size());
108 while (!assemblies.empty())
110 current = assemblies.front().first;
111 top_group = assemblies.front().second;
113 int nchilds = current->nelements();
115 for (
int i = 0; i < nchilds; ++i) {
116 currentIComp = (*(current.get()))[i];
117 currentDet = std::dynamic_pointer_cast<const Geometry::IDetector>(currentIComp);
118 if (currentDet.get())
120 instrcalib[number++] = std::make_pair(currentDet->getID(), top_group);
123 currentchild = std::dynamic_pointer_cast<const Geometry::ICompAssembly>(currentIComp);
124 if (currentchild.get()) {
125 child_group = group_map[currentchild->getName()];
126 if (child_group == 0)
127 child_group = top_group;
128 assemblies.emplace(currentchild, child_group);
137 const bool overwrite =
false;
143 std::ifstream file(filename.c_str());
148 std::ostringstream mess;
149 mess <<
"Calibration file " << filename <<
" already exist. Only grouping will be modified";
156 std::ostringstream message;
157 std::fstream outfile;
161 outfile.open(filename.c_str(), std::ios::out);
162 if (!outfile.is_open()) {
163 message <<
"Can't open Calibration File: " << filename;
165 throw std::runtime_error(message.str());
168 infile.open(filename.c_str(), std::ios::in);
169 std::string newfilename = filename +
"2";
170 outfile.open(newfilename.c_str(), std::ios::out);
171 if (!infile.is_open()) {
172 message <<
"Can't open input Calibration File: " << filename;
174 throw std::runtime_error(message.str());
176 if (!outfile.is_open()) {
177 message <<
"Can't open new Calibration File: " << newfilename;
179 throw std::runtime_error(message.str());
187 int number, udet, select, group;
191 while (getline(infile, str)) {
192 if (str.empty() || str[0] ==
'#')
194 std::istringstream istr(str);
195 istr >> number >> udet >> offset >> select >> group;
200 group = ((*it).second).second;
218 os << std::fixed << std::setw(9) << number << std::fixed << std::setw(15) << udet << std::fixed
219 << std::setprecision(7) << std::setw(15) << offset << std::fixed << std::setw(8) << select << std::fixed
220 << std::setw(8) << group <<
"\n";
225 os <<
"# Diffraction focusing calibration file created by Mantid"
227 os <<
"# Detectors have been grouped using assembly names:" <<
groups <<
"\n";
229 os <<
"# Template file " << filename <<
" has been used"
231 os <<
"# Only grouping has been changed, offset from template file have "
235 os <<
"# No template file, all offsets set to 0.0 and select to 1"
238 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.
@ 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 init() override
Initialisation code.
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.
instrcalmap instrcalib
Calibration map used if the *.cal file exist.
void saveGroupingFile(const std::string &, bool overwrite) const
Creates and saves the output file.
std::string groups
The names of the groups.
void writeHeaders(std::ostream &os, const std::string &filename, bool overwrite) const
Writes out the header to the output file.
bool groupingFileDoesExist(const std::string &filename) const
Determine whether the grouping file already exists.
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.