Mantid
Loading...
Searching...
No Matches
SlitCalculator.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
11#include "MantidAPI/Progress.h"
14
15#include <cmath>
16
19 Q_UNUSED(parent);
20 ui.setupUi(this);
21 if (currentInstrumentName == "") {
22 // set up the initial instrument if there is not one associated
23 // with slit calculator
24 currentInstrumentName = "INTER";
26 }
28}
30 // used in refl main window to indicate that slitCalculator fields
31 // need to update because another instrument has been selected.
33}
35void SlitCalculator::setInstrument(const std::string &instrumentName) {
36 // we want to get the most up-to-date definition, so we use the current
37 // date/time
38 auto date = Mantid::Types::Core::DateAndTime::getCurrentTime().toISO8601String();
39 // find the full path to the definition file
40 auto filename = Mantid::API::InstrumentFileFinder::getInstrumentFilename(instrumentName, date);
41 // parse the XML that we have found for the definition
43 filename, instrumentName, Mantid::Kernel::Strings::loadFile(filename));
44 // retrieving the mangled name of the instrument
45 std::string instrumentNameMangled = parser.getMangledName();
46 // See if we have a definition already in the InstrumentDataService
47 if (Mantid::API::InstrumentDataService::Instance().doesExist(instrumentNameMangled)) {
48 // If it does, set the associated instrument to the one we have found.
49 this->instrument = Mantid::API::InstrumentDataService::Instance().retrieve(instrumentNameMangled);
50 } else {
51 // We set the instrument from XML that we have found.
53 this->instrument = parser.parseXML(&prog);
54 }
56}
57
58void SlitCalculator::show() { QDialog::show(); }
59
62 // fetch the components that we need for values from IDF
63 auto slit1Component = instrument->getComponentByName("slit1");
64 auto slit2Component = instrument->getComponentByName("slit2");
65 auto sampleComponent = instrument->getComponentByName("some-surface-holder");
66 // check that they have been fetched from the IDF
67 if (slit1Component.get() != nullptr && slit2Component.get() != nullptr && sampleComponent.get() != nullptr) {
68 // convert from meters to millimeters
69 const double s1s2 = 1e3 * (slit1Component->getDistance(*slit2Component));
70 // set value in field of slitCalculator
71 ui.spinSlit1Slit2->setValue(s1s2);
72 // convert from meters to millimeters
73 const double s2sa = 1e3 * (slit2Component->getDistance(*sampleComponent));
74 // set value in field of slitCalculator
75 ui.spinSlit2Sample->setValue(s2sa);
76 } else {
77 // the parameters slit1, slit2 and sample-holder where not found
78 // set the values in SlitCalculator up so that it is obvious
79 // we did not retrieve them from any IDF.
80 ui.spinSlit1Slit2->setValue(0.0);
81 ui.spinSlit2Sample->setValue(0.0);
82 }
83}
85void SlitCalculator::setCurrentInstrumentName(const std::string &instrumentName) {
86 this->currentInstrumentName = instrumentName;
87}
90 const auto currentInstrument = getInstrument();
91 if (currentInstrument->getName() != currentInstrumentName) {
93 }
94 // Gather input
95 const double s1s2 = ui.spinSlit1Slit2->value();
96 const double s2sa = ui.spinSlit2Sample->value();
97 const double res = ui.spinResolution->value();
98 const double footprint = ui.spinFootprint->value();
99 const double angle = ui.spinAngle->value();
100
101 // Calculate values
103 algSlit->initialize();
104 algSlit->setChild(true);
105 algSlit->setProperty("Slit1Slit2", s1s2);
106 algSlit->setProperty("Slit2SA", s2sa);
107 algSlit->setProperty("Resolution", res);
108 algSlit->setProperty("Footprint", footprint);
109 algSlit->setProperty("Angle", angle);
110 algSlit->execute();
111
112 const double s1 = algSlit->getProperty("Slit1");
113 const double s2 = algSlit->getProperty("Slit2");
114
115 // Update output
116 ui.slit1Text->setText(QString::number(s1, 'f', 3));
117 ui.slit2Text->setText(QString::number(s2, 'f', 3));
118}
119} // namespace MantidQt::MantidWidgets
void setInstrument(const std::string &instrumentName)
Mantid::Geometry::Instrument_const_sptr instrument
void setCurrentInstrumentName(const std::string &instrumentName) override
Mantid::Geometry::Instrument_const_sptr getInstrument()
void setupSlitCalculatorWithInstrumentValues(const Mantid::Geometry::Instrument_const_sptr &)
static std::string getInstrumentFilename(const std::string &instrumentName, const std::string &date="")
Get the IDF using the instrument name and date.
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
Creates an instrument data from a XML instrument description file.
std::shared_ptr< Instrument > parseXML(Kernel::ProgressBase *progressReporter)
Parse XML contents.
std::string getMangledName()
Handle used in the singleton constructor for instrument file should append the value file sha-1 check...
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
MANTID_KERNEL_DLL std::string loadFile(const std::string &filename)
Loads the entire contents of a text file into a string.
Definition: Strings.cpp:28