32 "Input Peaks Workspace for Run 1");
35 "Input Peaks Workspace for Run 2");
37 declareProperty(
"Tolerance", .12,
"Integer offset for h,k,and l values to be considered valid.(def=.12)");
39 declareProperty(
"MIND", -1.0,
"Minimium d-spacing to consider,(def=-1)");
40 declareProperty(
"MAXD", -1.0,
"Maximum d-spacing to consider,(def=-1)");
51 declareProperty(
"AvErrIndex", 0.0,
"Average abs offset from integer values for indexed peaks",
74 int &Nindexed,
double &AvErrIndexed,
double &AvErrorAll,
82 double TotOffsetIndx = 0;
83 double TotOffsetAll = 0;
84 int Npeaks = Peaks->getNumberPeaks();
86 for (
int i = 0; i < Npeaks; i++) {
87 V3D hkl = InvUB2 * Peaks->getPeak(i).getQLabFrame();
89 for (
int k = 0; k < 3; k++) {
90 double offset = hkl[k] - floor(hkl[k]);
93 offset =
fabs(offset);
94 if (offset > maxOffset)
100 TotOffsetIndx += maxOffset;
103 TotOffsetAll += maxOffset;
108 AvErrIndexed = TotOffsetIndx / Nindexed;
116 AvErrorAll = TotOffsetAll / Npeaks;
132 g_log.
error(
"Each peaks workspace MUST have only one run");
133 throw std::invalid_argument(
"Each peaks workspace MUST have only one run");
138 bool Run1HasOrientedLattice =
true;
139 if (!PeaksRun1->sample().hasOrientedLattice()) {
141 Run1HasOrientedLattice =
false;
143 const std::string fft(
"FindUBUsingFFT");
145 findUB->initialize();
147 findUB->setProperty(
"MIND",
static_cast<double>(
getProperty(
"MIND")));
148 findUB->setProperty(
"MAXD",
static_cast<double>(
getProperty(
"MAXD")));
149 findUB->setProperty(
"Tolerance",
Tolerance);
151 findUB->executeAsChildAlg();
153 if (!PeaksRun1->sample().hasOrientedLattice()) {
154 g_log.
notice(std::string(
"Could not find UB for ") + std::string(PeaksRun1->getName()));
155 throw std::invalid_argument(std::string(
"Could not find UB for ") + std::string(PeaksRun1->getName()));
160 UB1 = PeaksRun1->sample().getOrientedLattice().getUB();
165 double avErrIndx, avErrAll;
168 if (N1 < .6 * PeaksRun1->getNumberPeaks()) {
169 g_log.
notice(std::string(
"UB did not index well for ") + std::string(PeaksRun1->getName()));
170 throw std::invalid_argument(std::string(
"UB did not index well for ") + std::string(PeaksRun1->getName()));
175 auto lat2 = std::make_unique<OrientedLattice>(PeaksRun1->sample().getOrientedLattice());
177 PeaksRun2->mutableSample().setOrientedLattice(std::move(lat2));
179 if (!Run1HasOrientedLattice)
180 PeaksRun1->mutableSample().setOrientedLattice(
nullptr);
185 for (
int i = 0; i < PeaksRun2->getNumberPeaks(); i++) {
186 PeaksRun2->getPeak(i).setGoniometerMatrix(Gon22);
189 int RunNum = PeaksRun2->getPeak(0).getRunNumber();
191 int Npeaks = PeaksRun2->getNumberPeaks();
194 std::array<double, 5> MinData = {{0., 0., 0., 0., 0.}};
200 for (
auto dir : directionList)
201 for (
int sgn = 1; sgn > -2; sgn -= 2) {
203 Quat Q(sgn * dphi, dir);
208 double dummyAvErrIndx, dummyAvErrAll;
209 IndexRaw(PeaksRun2, Rot * UB1, Nindexed, dummyAvErrIndx, dummyAvErrAll,
Tolerance);
211 if (Nindexed > MinData[0]) {
212 MinData[0] = Nindexed;
221 g_log.
debug() <<
"Best direction unOptimized is (" << (MinData[1] * MinData[2]) <<
"," << (MinData[1] * MinData[3])
222 <<
"," << (MinData[1] * MinData[4]) <<
")\n";
226 auto ws = createWorkspace<Workspace2D>(1, 3 * Npeaks, 3 * Npeaks);
230 for (
int i = 0; i < Npeaks; ++i) {
231 Xvals.emplace_back(i);
232 Xvals.emplace_back(i);
233 Xvals.emplace_back(i);
236 ws->setPoints(0, Xvals);
239 V3D dir(MinData[2], MinData[3], MinData[4]);
241 Quat Q(MinData[1] * dphi, dir);
247 MinData[2] = omchiphi[2];
248 MinData[3] = omchiphi[1];
249 MinData[4] = omchiphi[0];
251 std::string FunctionArgs =
"name=PeakHKLErrors, PeakWorkspaceName=" + PeaksRun2->getName() +
",OptRuns=" + RunNumStr +
252 ",phi" + RunNumStr +
"=" + boost::lexical_cast<std::string>(MinData[2]) +
",chi" +
253 RunNumStr +
"=" + boost::lexical_cast<std::string>(MinData[3]) +
",omega" + RunNumStr +
254 "=" + boost::lexical_cast<std::string>(MinData[4]);
256 std::string Constr = boost::lexical_cast<std::string>(MinData[2] - 5) +
"<phi" + RunNumStr +
"<" +
257 boost::lexical_cast<std::string>(MinData[2] + 5);
258 Constr +=
"," + boost::lexical_cast<std::string>(MinData[3] - 5) +
"<chi" + RunNumStr +
"<" +
259 boost::lexical_cast<std::string>(MinData[3] + 5) +
",";
261 Constr += boost::lexical_cast<std::string>(MinData[4] - 5) +
"<omega" + RunNumStr +
"<" +
262 boost::lexical_cast<std::string>(MinData[4] + 5);
264 std::string Ties =
"SampleXOffset=0.0,SampleYOffset=0.0,SampleZOffset=0.0,"
265 "GonRotx=0.0,GonRoty=0.0,GonRotz=0.0";
276 std::string outputName =
"out";
282 std::shared_ptr<API::ITableWorkspace> results =
Fit->
getProperty(
"OutputParameters");
286 MinData[2] = results->Double(6, 1);
287 MinData[3] = results->Double(7, 1);
288 MinData[4] = results->Double(8, 1);
290 g_log.
debug() <<
"Best direction Optimized is (" << (MinData[2]) <<
"," << (MinData[3]) <<
"," << (MinData[4])
303 double deg, ax1, ax2, ax3;
305 if (dphi * deg < 0) {
312 double phi2 =
static_cast<double>(
getProperty(
"Run1Phi")) + dphi;
313 double chi2 = acos(ax2) / M_PI * 180;
314 double omega2 = atan2(ax3, -ax1) / M_PI * 180;
316 g_log.
notice() <<
"============================ Results ============================\n";
317 g_log.
notice() <<
" phi,chi, and omega= (" << phi2 <<
"," << chi2 <<
"," << omega2 <<
")\n";
318 g_log.
notice() <<
" #indexed =" << Nindexed <<
'\n';
319 g_log.
notice() <<
" ==============================================\n";
333 for (
int i = 0; i < PeaksRun2->getNumberPeaks(); i++) {
334 PeaksRun2->getPeak(i).setGoniometerMatrix(Gon2a);
337 auto latt2 = std::make_unique<OrientedLattice>(PeaksRun2->mutableSample().getOrientedLattice());
340 latt2->setUB(Gon2a * Mk * UB1);
342 PeaksRun2->mutableSample().setOrientedLattice(std::move(latt2));
355 for (
int peak = 0; peak < Peaks->getNumberPeaks(); peak++) {
356 int thisRunNum = Peaks->getPeak(peak).getRunNumber();
357 GoniometerMatrix = Peaks->getPeak(peak).getGoniometerMatrix();
361 RunNumber = thisRunNum;
363 else if (thisRunNum != RunNumber)
380 return GoniometerMatrix * UB;
#define DECLARE_ALGORITHM(classname)
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 initialize() override
Initialization method invoked by the framework.
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 executeAsChildAlg() override
Execute as a Child Algorithm.
A property class for workspaces.
bool CheckForOneRun(const DataObjects::PeaksWorkspace_sptr &Peaks, Kernel::Matrix< double > &GoniometerMatrix) const
Checks that a PeaksWorkspace has only one run.
void IndexRaw(const DataObjects::PeaksWorkspace_sptr &Peaks, const Kernel::Matrix< double > &UBraw, int &Nindexed, double &AvErrIndexed, double &AvErrorAll, double tolerance) const
Calculate indexing stats if the peaks had been indexed with given UBraw by NOT applying the goniomete...
void exec() override
Run the algorithm.
Kernel::Matrix< double > getUBRaw(const Kernel::Matrix< double > &UB, const Kernel::Matrix< double > &GoniometerMatrix) const
Returns the raw UB, its inverse indexes using Q lab.
void init() override
Initialise the properties.
A generic fitting algorithm.
Class to represent a particular goniometer setting, which is described by the rotation matrix.
std::vector< double > getEulerAngles(const std::string &convention="YZX")
Return Euler angles acording to a convention.
static std::vector< Kernel::V3D > MakeHemisphereDirections(int n_steps)
Make list of direction vectors uniformly distributed over a hemisphere.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
void notice(const std::string &msg)
Logs at notice level.
void error(const std::string &msg)
Logs at error level.
T Invert()
LU inversion routine.
void normalize()
Normalize.
void getAngleAxis(double &_deg, double &_ax0, double &_ax1, double &ax2) const
Extracts the angle of roatation and axis.
std::vector< double > getRotation(bool check_normalisation=false, bool throw_on_errors=false) const
returns the rotation matrix defined by this quaternion as an 9-point
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
double normalize()
Make a normalized vector (return norm value)
std::shared_ptr< PeaksWorkspace > PeaksWorkspace_sptr
Typedef for a shared pointer to a peaks workspace.
constexpr double Tolerance
Standard tolerance value.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
std::string to_string(const wide_integer< Bits, Signed > &n)
Describes the direction (within an algorithm) of a Property.
@ InOut
Both an input & output workspace.
@ Input
An input workspace.
@ Output
An output workspace.