Newer
Older
#include "MantidDataHandling/LoadIsawDetCal.h"
#include "MantidAPI/DetectorInfo.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/MultipleFileProperty.h"
#include "MantidAPI/Run.h"
Lynch, Vickie
committed
#include "MantidDataObjects/EventList.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/Instrument/ObjCompAssembly.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/Strings.h"
Gigg, Martyn Anthony
committed
#include "MantidKernel/V3D.h"
#include <boost/algorithm/string/trim.hpp>
Lynch, Vickie
committed
namespace Mantid {
namespace DataHandling {
Lynch, Vickie
committed
// Register the class into the algorithm factory
DECLARE_ALGORITHM(LoadIsawDetCal)
Lynch, Vickie
committed
using namespace Kernel;
using namespace API;
using namespace Geometry;
using namespace DataObjects;
Lynch, Vickie
committed
/** Initialisation method
*/
void LoadIsawDetCal::init() {
declareProperty(Kernel::make_unique<WorkspaceProperty<Workspace>>(
"InputWorkspace", "", Direction::InOut,
boost::make_shared<InstrumentValidator>()),
"The workspace containing the geometry to be calibrated.");
Lynch, Vickie
committed
const auto exts = std::vector<std::string>({".DetCal"});
declareProperty(
Kernel::make_unique<API::MultipleFileProperty>("Filename", exts),
"The input filename of the ISAW DetCal file (Two files "
"allowed for SNAP) ");
declareProperty(
Kernel::make_unique<API::FileProperty>(
"Filename2", "", API::FileProperty::OptionalLoad, ".DetCal"),
"The input filename of the second ISAW DetCal file (West "
"banks for SNAP) ");
declareProperty("TimeOffset", 0.0, "Time Offset", Direction::Output);
}
namespace {
const constexpr double DegreesPerRadian = 180.0 / M_PI;
std::string getBankName(const std::string &bankPart, const int idnum) {
if (bankPart == "WISHpanel" && idnum < 10) {
return bankPart + "0" + std::to_string(idnum);
} else {
return bankPart + std::to_string(idnum);
}
}
std::string getInstName(API::Workspace_const_sptr wksp) {
MatrixWorkspace_const_sptr matrixWksp =
boost::dynamic_pointer_cast<const MatrixWorkspace>(wksp);
if (matrixWksp) {
return matrixWksp->getInstrument()->getName();
}
PeaksWorkspace_const_sptr peaksWksp =
boost::dynamic_pointer_cast<const PeaksWorkspace>(wksp);
if (peaksWksp) {
return peaksWksp->getInstrument()->getName();
}
throw std::runtime_error("Failed to determine instrument name");
}
}
std::map<std::string, std::string> LoadIsawDetCal::validateInputs() {
std::map<std::string, std::string> result;
// two detcal files is only valid for snap
std::vector<std::string> filenames = getFilenames();
if (filenames.size() == 0) {
result["Filename"] = "Must supply .detcal file";
} else if (filenames.size() == 2) {
Workspace_const_sptr wksp = getProperty("InputWorkspace");
const auto instname = getInstName(wksp);
result["Filename"] = "Two files is only valid for SNAP";
}
} else if (filenames.size() > 2) {
result["Filename"] = "Supply at most two .detcal files";
/** Executes the algorithm
*
* @throw runtime_error Thrown if algorithm cannot execute
*/
void LoadIsawDetCal::exec() {
// Get the input workspace
Workspace_sptr ws = getProperty("InputWorkspace");
MatrixWorkspace_sptr inputW =
boost::dynamic_pointer_cast<MatrixWorkspace>(ws);
PeaksWorkspace_sptr inputP = boost::dynamic_pointer_cast<PeaksWorkspace>(ws);
Instrument_sptr inst = getCheckInst(ws);
std::string instname = inst->getName();
// Output summary to log file
double width, height, depth, detd, x, y, z, base_x, base_y, base_z, up_x,
up_y, up_z;
std::ifstream input(filenames[0].c_str(), std::ios_base::in);
std::string line;
std::string detname;
// Build a list of Rectangular Detectors
std::vector<boost::shared_ptr<RectangularDetector>> detList;
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
for (int i = 0; i < inst->nelements(); i++) {
boost::shared_ptr<RectangularDetector> det;
boost::shared_ptr<ICompAssembly> assem;
boost::shared_ptr<ICompAssembly> assem2;
det = boost::dynamic_pointer_cast<RectangularDetector>((*inst)[i]);
if (det) {
detList.push_back(det);
} else {
// Also, look in the first sub-level for RectangularDetectors (e.g. PG3).
// We are not doing a full recursive search since that will be very long
// for lots of pixels.
assem = boost::dynamic_pointer_cast<ICompAssembly>((*inst)[i]);
if (assem) {
for (int j = 0; j < assem->nelements(); j++) {
det = boost::dynamic_pointer_cast<RectangularDetector>((*assem)[j]);
if (det) {
detList.push_back(det);
} else {
// Also, look in the second sub-level for RectangularDetectors (e.g.
// PG3).
// We are not doing a full recursive search since that will be very
// long for lots of pixels.
assem2 = boost::dynamic_pointer_cast<ICompAssembly>((*assem)[j]);
if (assem2) {
for (int k = 0; k < assem2->nelements(); k++) {
det = boost::dynamic_pointer_cast<RectangularDetector>(
(*assem2)[k]);
if (det) {
detList.push_back(det);
Lynch, Vickie
committed
}
}
}
Lynch, Vickie
committed
}
}
}
}
std::unordered_set<int> uniqueBanks; // for CORELLI and WISH
std::string bankPart = "bank";
bankPart = "WISHpanel";
if (detList.empty()) {
// Get all children
std::vector<IComponent_const_sptr> comps;
inst->getChildren(comps, true);
for (auto &comp : comps) {
std::string bankName = comp->getName();
boost::trim(bankName);
boost::erase_all(bankName, bankPart);
int bank = 0;
Strings::convert(bankName, bank);
// Track unique bank numbers
uniqueBanks.insert(bank);
}
}
auto expInfoWS = boost::dynamic_pointer_cast<ExperimentInfo>(ws);
auto &detectorInfo = expInfoWS->mutableDetectorInfo();
std::vector<ComponentScaling> rectangularDetectorScalings;
while (std::getline(input, line)) {
if (line[0] == '7') {
double mL1, mT0;
std::stringstream(line) >> count >> mL1 >> mT0;
setProperty("TimeOffset", mT0);
// Convert from cm to m
center(0.0, 0.0, -mL1, "undulator", ws, detectorInfo);
center(0.0, 0.0, -mL1, "moderator", ws, detectorInfo);
// mT0 and time of flight are both in microsec
API::Run &run = inputW->mutableRun();
// Check to see if LoadEventNexus had T0 from TOPAZ Parameter file
IAlgorithm_sptr alg1 = createChildAlgorithm("ChangeBinOffset");
alg1->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputW);
alg1->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", inputW);
if (run.hasProperty("T0")) {
double T0IDF = run.getPropertyValueAsType<double>("T0");
alg1->setProperty("Offset", mT0 - T0IDF);
} else {
alg1->setProperty("Offset", mT0);
}
alg1->executeAsChildAlg();
inputW = alg1->getProperty("OutputWorkspace");
// set T0 in the run parameters
run.addProperty<double>("T0", mT0, true);
Lynch, Vickie
committed
if (line[0] != '5')
continue;
std::stringstream(line) >> count >> id >> nrows >> ncols >> width >>
height >> depth >> detd >> x >> y >> z >> base_x >> base_y >> base_z >>
up_x >> up_y >> up_z;
if (id == 10 && filenames.size() == 2 && instname == "SNAP") {
while (std::getline(input, line)) {
if (line[0] != '5')
continue;
std::stringstream(line) >> count >> id >> nrows >> ncols >> width >>
height >> depth >> detd >> x >> y >> z >> base_x >> base_y >>
base_z >> up_x >> up_y >> up_z;
if (id == 10)
break;
}
boost::shared_ptr<RectangularDetector> det;
std::string bankName = getBankName(bankPart, id);
[&bankName](const boost::shared_ptr<RectangularDetector> &detector) {
return detector->getName() == bankName;
if (matchingDetector != detList.end()) {
det = *matchingDetector;
V3D rX(base_x, base_y, base_z);
V3D rY(up_x, up_y, up_z);
detname = det->getName();
center(x, y, z, detname, ws, detectorInfo);
ComponentScaling detScaling;
detScaling.scaleX = CM_TO_M * width / det->xsize();
detScaling.scaleY = CM_TO_M * height / det->ysize();
detScaling.componentName = detname;
rectangularDetectorScalings.push_back(detScaling);
doRotation(rX, rY, detectorInfo, det);
auto bank = uniqueBanks.find(id);
if (bank == uniqueBanks.end())
bankName = getBankName(bankPart, idnum);
// Retrieve it
// for Corelli with sixteenpack under bank
std::vector<Geometry::IComponent_const_sptr> children;
boost::shared_ptr<const Geometry::ICompAssembly> asmb =
boost::dynamic_pointer_cast<const Geometry::ICompAssembly>(
inst->getComponentByName(bankName));
asmb->getChildren(children, false);
comp = children[0];
}
if (comp) {
// Omitted scaling tubes
detname = comp->getFullName();
center(x, y, z, detname, ws, detectorInfo);
bool doWishCorrection =
(instname == "WISH"); // TODO: find out why this is needed for WISH
doRotation(rX, rY, detectorInfo, comp, doWishCorrection);
Lynch, Vickie
committed
}
// Do this last, to avoid the issue of invalidating DetectorInfo
applyScalings(ws, rectangularDetectorScalings);
setProperty("InputWorkspace", ws);
Lynch, Vickie
committed
/**
* The intensity function calculates the intensity as a function of detector
* position and angles
* @param x :: The shift along the X-axis
* @param y :: The shift along the Y-axis
* @param z :: The shift along the Z-axis
* @param detname :: The detector name
* @param ws :: The workspace
* @param detectorInfo :: The detector info object for the workspace
void LoadIsawDetCal::center(const double x, const double y, const double z,
const std::string &detname, API::Workspace_sptr ws,
API::DetectorInfo &detectorInfo) {
Instrument_sptr inst = getCheckInst(ws);
IComponent_const_sptr comp = inst->getComponentByName(detname);
if (comp == nullptr) {
throw std::runtime_error("Component with name " + detname +
" was not found.");
}
const V3D position(x * CM_TO_M, y * CM_TO_M, z * CM_TO_M);
detectorInfo.setPosition(*comp, position);
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
}
/**
* Gets the instrument of the workspace, checking that the workspace
* and the instrument are as expected.
*
* @param ws workspace (expected Matrix or Peaks Workspace)
*
* @throw std::runtime_error if there's any problem with the workspace or it is
* not possible to get an instrument object from it
*/
Instrument_sptr LoadIsawDetCal::getCheckInst(API::Workspace_sptr ws) {
MatrixWorkspace_sptr inputW =
boost::dynamic_pointer_cast<MatrixWorkspace>(ws);
PeaksWorkspace_sptr inputP = boost::dynamic_pointer_cast<PeaksWorkspace>(ws);
// Get some stuff from the input workspace
Instrument_sptr inst;
if (inputW) {
inst = boost::const_pointer_cast<Instrument>(inputW->getInstrument());
if (!inst)
throw std::runtime_error("Could not get a valid instrument from the "
"MatrixWorkspace provided as input");
} else if (inputP) {
inst = boost::const_pointer_cast<Instrument>(inputP->getInstrument());
if (!inst)
throw std::runtime_error("Could not get a valid instrument from the "
"PeaksWorkspace provided as input");
} else {
throw std::runtime_error("Could not get a valid instrument from the "
"workspace which does not seem to be valid as "
"input (must be either MatrixWorkspace or "
"PeaksWorkspace");
}
return inst;
}
std::vector<std::string> LoadIsawDetCal::getFilenames() {
std::vector<std::string> filenamesFromPropertyUnraveld;
std::vector<std::vector<std::string>> filenamesFromProperty =
this->getProperty("Filename");
for (const auto &outer : filenamesFromProperty) {
std::copy(outer.begin(), outer.end(),
std::back_inserter(filenamesFromPropertyUnraveld));
}
// shouldn't be used except for legacy cases
const std::string filename2 = this->getProperty("Filename2");
if (!filename2.empty())
filenamesFromPropertyUnraveld.push_back(filename2);
return filenamesFromPropertyUnraveld;
}
/**
* Perform the rotation for the calibration
*
* @param rX the vector of (base_x, base_y, base_z) from the calibration file
* @param rY the vector of (up_x, up_y, up_z) from the calibration file
* @param detectorInfo the DetectorInfo object from the workspace
* @param comp the component to rotate
* @param doWishCorrection if true apply a special correction for WISH
*/
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
void LoadIsawDetCal::doRotation(V3D rX, V3D rY, DetectorInfo &detectorInfo,
boost::shared_ptr<const IComponent> comp,
bool doWishCorrection) {
// These are the ISAW axes
rX.normalize();
rY.normalize();
// These are the original axes
const V3D oX(1., 0., 0.);
const V3D oY(0., 1., 0.);
// Axis that rotates X
V3D ax1 = oX.cross_prod(rX);
// Rotation angle from oX to rX
double angle1 = oX.angle(rX) * DegreesPerRadian;
if (doWishCorrection)
angle1 += 180.0;
// Create the first quaternion
Quat Q1(angle1, ax1);
// Now we rotate the original Y using Q1
V3D roY = oY;
Q1.rotate(roY);
// Find the axis that rotates oYr onto rY
V3D ax2 = roY.cross_prod(rY);
const double angle2 = roY.angle(rY) * DegreesPerRadian;
Quat Q2(angle2, ax2);
// Final = those two rotations in succession; Q1 is done first.
Quat Rot = Q2 * Q1;
// Then find the corresponding relative position
const auto parent = comp->getParent();
if (parent) {
Quat rot0 = parent->getRelativeRot();
rot0.inverse();
Rot *= rot0;
}
const auto grandparent = parent->getParent();
if (grandparent) {
Quat rot0 = grandparent->getRelativeRot();
rot0.inverse();
Rot *= rot0;
}
detectorInfo.setRotation(*comp.get(), Rot);
}
/**
* Apply the scalings from the calibration file. This is called after doing the
*moves and rotations associated with the calibration, to avoid the problem of
*invalidation DetectorInfo after writing to the parameter map.
*
* @param rectangularDetectorScalings a vector containing a component ID, and
*values for scalex and scaley
*/
const std::vector<ComponentScaling> &rectangularDetectorScalings) {
for (const auto &scaling : rectangularDetectorScalings) {
IAlgorithm_sptr alg1 = createChildAlgorithm("ResizeRectangularDetector");
alg1->setProperty<Workspace_sptr>("Workspace", ws);
alg1->setProperty("ComponentName", scaling.componentName);
alg1->setProperty("ScaleX", scaling.scaleX);
alg1->setProperty("ScaleY", scaling.scaleY);
alg1->executeAsChildAlg();
Lynch, Vickie
committed
} // namespace Algorithm
} // namespace Mantid