-
Notifications
You must be signed in to change notification settings - Fork 122
/
ReflectometryReductionOneAuto3.cpp
1171 lines (1024 loc) · 45 KB
/
ReflectometryReductionOneAuto3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidReflectometry/ReflectometryReductionOneAuto3.h"
#include "MantidAPI/BoostOptionalToAlgorithmProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/RegexStrings.h"
#include "MantidKernel/Strings.h"
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
namespace Mantid {
namespace Reflectometry {
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
namespace { // anonymous
// Property names
namespace Prop {
static const std::string FLIPPERS{"Flippers"};
static const std::string POLARIZATION_ANALYSIS{"PolarizationAnalysis"};
} // namespace Prop
namespace CorrectionMethod {
static const std::string WILDES{"Wildes"};
static const std::string FREDRIKZE{"Fredrikze"};
// Map correction methods to which correction-option property name they use
static const std::map<std::string, std::string> OPTION_NAME{
{CorrectionMethod::WILDES, Prop::FLIPPERS},
{CorrectionMethod::FREDRIKZE, Prop::POLARIZATION_ANALYSIS}};
void validate(const std::string &method) {
if (!CorrectionMethod::OPTION_NAME.count(method))
throw std::invalid_argument("Unsupported polarization correction method: " +
method);
}
} // namespace CorrectionMethod
std::vector<std::string> workspaceNamesInGroup(const std::string &groupName) {
auto group =
AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(groupName);
return group->getNames();
}
std::string vectorToString(const std::vector<std::string> &vec) {
std::string result;
for (auto item : vec) {
if (!result.empty())
result += ",";
result += item;
}
return result;
}
void removeAllWorkspacesFromGroup(const std::string &groupName) {
auto group =
AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(groupName);
group->removeAll();
}
void removeWorkspacesFromADS(const std::vector<std::string> &workspaceNames) {
for (auto workspaceName : workspaceNames)
AnalysisDataService::Instance().remove(workspaceName);
}
} // namespace
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ReflectometryReductionOneAuto3)
namespace {
const std::string OUTPUT_WORKSPACE_BINNED_DEFAULT_PREFIX("IvsQ_binned");
const std::string OUTPUT_WORKSPACE_DEFAULT_PREFIX("IvsQ");
const std::string OUTPUT_WORKSPACE_WAVELENGTH_DEFAULT_PREFIX("IvsLam");
} // namespace
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string ReflectometryReductionOneAuto3::name() const {
return "ReflectometryReductionOneAuto";
}
/// Algorithm's version for identification. @see Algorithm::version
int ReflectometryReductionOneAuto3::version() const { return 3; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string ReflectometryReductionOneAuto3::category() const {
return "Reflectometry\\ISIS";
}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string ReflectometryReductionOneAuto3::summary() const {
return "Reduces a single TOF/Lambda reflectometry run into a mod Q vs I/I0 "
"workspace attempting to pick instrument parameters for missing "
"properties";
}
/** Validate individual transmission runs
*
* @return :: void
*/
void ReflectometryReductionOneAuto3::getTransmissionRun(
std::map<std::string, std::string> &results,
WorkspaceGroup_sptr &workspaceGroup, const std::string &transmissionRun) {
const std::string str = getPropertyValue(transmissionRun);
if (!str.empty()) {
auto transmissionGroup =
AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(str);
// If it is not a group, we don't need to validate its size
if (!transmissionGroup)
return;
const bool polarizationCorrections = getProperty("PolarizationAnalysis");
if (workspaceGroup->size() != transmissionGroup->size() &&
!polarizationCorrections) {
// If they are not the same size then we cannot associate a transmission
// group member with every input group member.
results[transmissionRun] = transmissionRun +
" group must be the "
"same size as the InputWorkspace group "
"when polarization analysis is false.";
}
}
}
/** Validate transmission runs
*
* @return :: result of the validation as a map
*/
std::map<std::string, std::string>
ReflectometryReductionOneAuto3::validateInputs() {
std::map<std::string, std::string> results;
// Validate transmission runs only if our input workspace is a group
if (!checkGroups())
return results;
auto group = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
getPropertyValue("InputWorkspace"));
if (!group)
return results;
// First and second transmission runs
getTransmissionRun(results, group, "FirstTransmissionRun");
getTransmissionRun(results, group, "SecondTransmissionRun");
return results;
}
std::string ReflectometryReductionOneAuto3::getRunNumberForWorkspaceGroup(
const WorkspaceGroup_const_sptr &group) {
// Return the run number for the first child workspace
if (!group)
throw std::runtime_error("Invalid workspace group type");
if (group->getNumberOfEntries() < 1)
throw std::runtime_error("Cannot run algorithm on empty group");
auto childWs = group->getItem(0);
auto childMatrixWs = std::dynamic_pointer_cast<MatrixWorkspace>(childWs);
if (!childMatrixWs)
throw std::runtime_error("Child workspace is not a MatrixWorkspace");
return getRunNumber(*childMatrixWs);
}
// Get output workspace names from the user-specified properties, or default
// names if the properties were not specified
auto ReflectometryReductionOneAuto3::getOutputWorkspaceNames()
-> WorkspaceNames {
WorkspaceNames result;
MatrixWorkspace_const_sptr matrixWs = getProperty("InputWorkspace");
std::string runNumber;
if (matrixWs) {
runNumber = getRunNumber(*matrixWs);
} else {
// Casting to WorkspaceGroup doesn't work - I think because InputWorkspace
// is declared as a MatrixWorkspace - so get it from the ADS instead
auto groupWs = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
getPropertyValue("InputWorkspace"));
runNumber = getRunNumberForWorkspaceGroup(groupWs);
}
if (isDefault("OutputWorkspaceBinned"))
result.iVsQBinned = OUTPUT_WORKSPACE_BINNED_DEFAULT_PREFIX + runNumber;
else
result.iVsQBinned = getPropertyValue("OutputWorkspaceBinned");
if (isDefault("OutputWorkspace"))
result.iVsQ = OUTPUT_WORKSPACE_DEFAULT_PREFIX + runNumber;
else
result.iVsQ = getPropertyValue("OutputWorkspace");
if (isDefault("OutputWorkspaceWavelength"))
result.iVsLam = OUTPUT_WORKSPACE_WAVELENGTH_DEFAULT_PREFIX + runNumber;
else
result.iVsLam = getPropertyValue("OutputWorkspaceWavelength");
return result;
}
// Set default names for output workspaces
void ReflectometryReductionOneAuto3::setDefaultOutputWorkspaceNames() {
const bool isDebug = getProperty("Debug");
auto outputNames = getOutputWorkspaceNames();
if (isDefault("OutputWorkspaceBinned")) {
setPropertyValue("OutputWorkspaceBinned", outputNames.iVsQBinned);
}
if (isDefault("OutputWorkspace")) {
setPropertyValue("OutputWorkspace", outputNames.iVsQ);
}
if (isDebug && isDefault("OutputWorkspaceWavelength")) {
setPropertyValue("OutputWorkspaceWavelength", outputNames.iVsLam);
}
}
/** Initialize the algorithm's properties.
*/
void ReflectometryReductionOneAuto3::init() {
// Input ws
declareProperty(
std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"InputWorkspace", "", Direction::Input, PropertyMode::Mandatory),
"Input run in TOF or wavelength");
// Reduction type
initReductionProperties();
// Analysis mode
const std::vector<std::string> analysisMode{"PointDetectorAnalysis",
"MultiDetectorAnalysis"};
auto analysisModeValidator =
std::make_shared<StringListValidator>(analysisMode);
declareProperty("AnalysisMode", analysisMode[0], analysisModeValidator,
"Analysis mode. This property is only used when "
"ProcessingInstructions is not set.",
Direction::Input);
// Processing instructions
declareProperty(std::make_unique<PropertyWithValue<std::string>>(
"ProcessingInstructions", "", Direction::Input),
"Grouping pattern of spectrum numbers to yield only the"
" detectors of interest. See GroupDetectors for syntax.");
// Theta
declareProperty("ThetaIn", Mantid::EMPTY_DBL(), "Angle in degrees",
Direction::Input);
// ThetaLogName
declareProperty("ThetaLogName", "",
"The name ThetaIn can be found in the run log as");
// Whether to correct detectors
declareProperty(
std::make_unique<PropertyWithValue<bool>>("CorrectDetectors", true,
Direction::Input),
"Moves detectors to twoTheta if ThetaIn or ThetaLogName is given");
// Detector position correction type
const std::vector<std::string> correctionType{"VerticalShift",
"RotateAroundSample"};
auto correctionTypeValidator = std::make_shared<CompositeValidator>();
correctionTypeValidator->add(
std::make_shared<MandatoryValidator<std::string>>());
correctionTypeValidator->add(
std::make_shared<StringListValidator>(correctionType));
declareProperty(
"DetectorCorrectionType", correctionType[0], correctionTypeValidator,
"When correcting detector positions, this determines whether detectors"
"should be shifted vertically or rotated around the sample position.",
Direction::Input);
setPropertySettings("DetectorCorrectionType",
std::make_unique<Kernel::EnabledWhenProperty>(
"CorrectDetectors", IS_EQUAL_TO, "1"));
// Wavelength limits
declareProperty("WavelengthMin", Mantid::EMPTY_DBL(),
"Wavelength Min in angstroms", Direction::Input);
declareProperty("WavelengthMax", Mantid::EMPTY_DBL(),
"Wavelength Max in angstroms", Direction::Input);
initMonitorProperties();
initBackgroundProperties();
initTransmissionProperties();
initAlgorithmicProperties(true);
initMomentumTransferProperties();
// Polarization correction
declareProperty(std::make_unique<PropertyWithValue<bool>>(
"PolarizationAnalysis", false, Direction::Input),
"Apply polarization corrections");
// Flood correction
std::vector<std::string> propOptions = {"Workspace", "ParameterFile"};
declareProperty("FloodCorrection", "Workspace",
std::make_shared<StringListValidator>(propOptions),
"The way to apply flood correction: "
"Workspace - use FloodWorkspace property to get the flood "
"workspace, ParameterFile - use parameters in the parameter "
"file to construct and apply flood correction workspace.");
declareProperty(
std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"FloodWorkspace", "", Direction::Input, PropertyMode::Optional),
"A flood workspace to apply; if empty and FloodCorrection is "
"'Workspace' then no correction is applied.");
// Init properties for diagnostics
initDebugProperties();
// Output workspace in Q
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspaceBinned", "", Direction::Output,
PropertyMode::Optional),
"Output workspace in Q (rebinned workspace)");
// Output workspace in Q (unbinned)
declareProperty(
std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output, PropertyMode::Optional),
"Output workspace in Q (native binning)");
// Output workspace in wavelength
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspaceWavelength", "", Direction::Output,
PropertyMode::Optional),
"Output workspace in wavelength");
setPropertySettings(
"OutputWorkspaceWavelength",
std::make_unique<Kernel::EnabledWhenProperty>("Debug", IS_EQUAL_TO, "1"));
initTransmissionOutputProperties();
}
/** Execute the algorithm.
*/
void ReflectometryReductionOneAuto3::exec() {
applyFloodCorrections();
setDefaultOutputWorkspaceNames();
MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
auto instrument = inputWS->getInstrument();
bool const isDebug = getProperty("Debug");
Algorithm_sptr alg = createChildAlgorithm("ReflectometryReductionOne");
alg->initialize();
// Mandatory properties
alg->setProperty("SummationType", getPropertyValue("SummationType"));
alg->setProperty("ReductionType", getPropertyValue("ReductionType"));
alg->setProperty("IncludePartialBins",
getPropertyValue("IncludePartialBins"));
alg->setProperty("Diagnostics", getPropertyValue("Diagnostics"));
alg->setProperty("Debug", isDebug);
double wavMin = checkForMandatoryInstrumentDefault<double>(
this, "WavelengthMin", instrument, "LambdaMin");
alg->setProperty("WavelengthMin", wavMin);
double wavMax = checkForMandatoryInstrumentDefault<double>(
this, "WavelengthMax", instrument, "LambdaMax");
alg->setProperty("WavelengthMax", wavMax);
convertProcessingInstructions(instrument, inputWS);
alg->setProperty("ProcessingInstructions", m_processingInstructions);
// Now that we know the detectors of interest, we can move them if
// necessary (i.e. if theta is given). If not, we calculate theta from the
// current detector positions
bool correctDetectors = getProperty("CorrectDetectors");
double theta;
if (!getPointerToProperty("ThetaIn")->isDefault()) {
theta = getProperty("ThetaIn");
} else if (!getPropertyValue("ThetaLogName").empty()) {
theta = getThetaFromLogs(inputWS, getPropertyValue("ThetaLogName"));
} else {
// Calculate theta from detector positions
theta = calculateTheta(inputWS);
// Never correct detector positions if ThetaIn or ThetaLogName is not
// specified
correctDetectors = false;
}
// Pass theta to the child algorithm
alg->setProperty("ThetaIn", theta);
if (correctDetectors) {
inputWS = correctDetectorPositions(inputWS, 2 * theta);
}
// Optional properties
alg->setPropertyValue("TransmissionProcessingInstructions",
getPropertyValue("TransmissionProcessingInstructions"));
populateMonitorProperties(alg, instrument);
alg->setPropertyValue("NormalizeByIntegratedMonitors",
getPropertyValue("NormalizeByIntegratedMonitors"));
bool transRunsFound = populateTransmissionProperties(alg);
if (!transRunsFound)
populateAlgorithmicCorrectionProperties(alg, instrument);
alg->setPropertyValue("SubtractBackground",
getPropertyValue("SubtractBackground"));
alg->setPropertyValue("BackgroundProcessingInstructions",
getPropertyValue("BackgroundProcessingInstructions"));
alg->setPropertyValue("BackgroundCalculationMethod",
getPropertyValue("BackgroundCalculationMethod"));
alg->setPropertyValue("DegreeOfPolynomial",
getPropertyValue("DegreeOfPolynomial"));
alg->setPropertyValue("CostFunction", getPropertyValue("CostFunction"));
alg->setProperty("InputWorkspace", inputWS);
alg->execute();
// Set the unbinned output workspace in Q, scaled and cropped if necessary
MatrixWorkspace_sptr IvsQ = alg->getProperty("OutputWorkspace");
IvsQ = scale(IvsQ);
const auto params = getRebinParams(IvsQ, theta);
auto IvsQC = cropQ(IvsQ, params);
setProperty("OutputWorkspace", IvsQC);
// Set the binned output workspace in Q
if (params.hasQStep()) {
MatrixWorkspace_sptr IvsQB = rebin(IvsQ, params);
setProperty("OutputWorkspaceBinned", IvsQB);
} else {
g_log.error("NRCalculateSlitResolution failed. Workspace in Q will not be "
"rebinned. Please provide dQ/Q.");
setProperty("OutputWorkspaceBinned", IvsQC);
}
// Set the output workspace in wavelength, if debug outputs are enabled
if (!isDefault("OutputWorkspaceWavelength") || isChild()) {
MatrixWorkspace_sptr IvsLam = alg->getProperty("OutputWorkspaceWavelength");
setProperty("OutputWorkspaceWavelength", IvsLam);
}
// Set the output transmission workspaces
setWorkspacePropertyFromChild(alg, "OutputWorkspaceTransmission");
setWorkspacePropertyFromChild(alg, "OutputWorkspaceFirstTransmission");
setWorkspacePropertyFromChild(alg, "OutputWorkspaceSecondTransmission");
// Set other properties so they can be updated in the Reflectometry interface
setProperty("ThetaIn", theta);
setProperty("MomentumTransferMin", params.qMin);
setProperty("MomentumTransferMax", params.qMax);
if (params.hasQStep())
setProperty("MomentumTransferStep", -(*params.qStep));
if (getPointerToProperty("ScaleFactor")->isDefault())
setProperty("ScaleFactor", 1.0);
}
/** Returns the detectors of interest, specified via processing instructions.
* Note that this returns the names of the parent detectors of the first and
* last spectrum indices in the processing instructions. It is assumed that all
* the interim detectors have the same parent.
*
* @param inputWS :: the input workspace
* @return :: the names of the detectors of interest
*/
std::vector<std::string> ReflectometryReductionOneAuto3::getDetectorNames(
const MatrixWorkspace_sptr &inputWS) {
std::vector<std::string> wsIndices;
boost::split(wsIndices, m_processingInstructionsWorkspaceIndex,
boost::is_any_of(":,-+"));
// vector of comopnents
std::vector<std::string> detectors;
try {
for (const auto &wsIndex : wsIndices) {
size_t index = boost::lexical_cast<size_t>(wsIndex);
auto detector = inputWS->getDetector(index);
auto parent = detector->getParent();
if (parent) {
auto parentType = parent->type();
auto detectorName = (parentType == "Instrument") ? detector->getName()
: parent->getName();
detectors.emplace_back(detectorName);
}
}
} catch (const boost::bad_lexical_cast &) {
throw std::runtime_error("Invalid processing instructions: " +
m_processingInstructionsWorkspaceIndex);
}
return detectors;
}
/** Correct an instrument component by shifting it vertically or
* rotating it around the sample.
*
* @param inputWS :: the input workspace
* @param twoTheta :: the angle to move detectors to
* @return :: the corrected workspace
*/
MatrixWorkspace_sptr ReflectometryReductionOneAuto3::correctDetectorPositions(
MatrixWorkspace_sptr inputWS, const double twoTheta) {
auto detectorsOfInterest = getDetectorNames(inputWS);
// Detectors of interest may be empty. This happens for instance when we input
// a workspace that was previously reduced using this algorithm. In this case,
// we shouldn't correct the detector positions
if (detectorsOfInterest.empty())
return inputWS;
const std::set<std::string> detectorSet(detectorsOfInterest.begin(),
detectorsOfInterest.end());
const std::string correctionType = getProperty("DetectorCorrectionType");
MatrixWorkspace_sptr corrected = inputWS;
for (const auto &detector : detectorSet) {
IAlgorithm_sptr alg =
createChildAlgorithm("SpecularReflectionPositionCorrect");
alg->setProperty("InputWorkspace", corrected);
alg->setProperty("TwoTheta", twoTheta);
alg->setProperty("DetectorCorrectionType", correctionType);
alg->setProperty("DetectorComponentName", detector);
alg->execute();
corrected = alg->getProperty("OutputWorkspace");
}
return corrected;
}
/** Calculate the theta value of the detector of interest specified via
* processing instructions
*
* @param inputWS :: the input workspace
* @return :: the angle of the detector (only the first detector is considered)
*/
double ReflectometryReductionOneAuto3::calculateTheta(
const MatrixWorkspace_sptr &inputWS) {
const auto detectorsOfInterest = getDetectorNames(inputWS);
// Detectors of interest may be empty. This happens for instance when we input
// a workspace that was previously reduced using this algorithm. In this case,
// we can't calculate theta
if (detectorsOfInterest.empty())
return 0.0;
IAlgorithm_sptr alg =
createChildAlgorithm("SpecularReflectionCalculateTheta");
alg->setProperty("InputWorkspace", inputWS);
alg->setProperty("DetectorComponentName", detectorsOfInterest[0]);
alg->execute();
const double theta = alg->getProperty("TwoTheta");
// Take a factor of 0.5 of the detector position, which is expected to be at
// 2 * theta
return theta * 0.5;
}
/** Set algorithmic correction properties
*
* @param alg :: ReflectometryReductionOne algorithm
* @param instrument :: The instrument attached to the workspace
*/
void ReflectometryReductionOneAuto3::populateAlgorithmicCorrectionProperties(
const IAlgorithm_sptr &alg, const Instrument_const_sptr &instrument) {
// With algorithmic corrections, monitors should not be integrated, see below
const std::string correctionAlgorithm = getProperty("CorrectionAlgorithm");
if (correctionAlgorithm == "PolynomialCorrection") {
alg->setProperty("NormalizeByIntegratedMonitors", false);
alg->setProperty("CorrectionAlgorithm", "PolynomialCorrection");
alg->setPropertyValue("Polynomial", getPropertyValue("Polynomial"));
} else if (correctionAlgorithm == "ExponentialCorrection") {
alg->setProperty("NormalizeByIntegratedMonitors", false);
alg->setProperty("CorrectionAlgorithm", "ExponentialCorrection");
alg->setProperty("C0", getPropertyValue("C0"));
alg->setProperty("C1", getPropertyValue("C1"));
} else if (correctionAlgorithm == "AutoDetect") {
// Figure out what to do from the instrument
try {
const auto corrVec = instrument->getStringParameter("correction");
if (corrVec.empty()) {
throw std::runtime_error(
"Could not find parameter 'correction' in "
"parameter file. Cannot auto detect the type of "
"correction.");
}
const std::string correctionStr = corrVec[0];
if (correctionStr == "polynomial") {
const auto polyVec = instrument->getStringParameter("polystring");
if (polyVec.empty())
throw std::runtime_error("Could not find parameter 'polystring' in "
"parameter file. Cannot apply polynomial "
"correction.");
alg->setProperty("CorrectionAlgorithm", "PolynomialCorrection");
alg->setProperty("Polynomial", polyVec[0]);
} else if (correctionStr == "exponential") {
const auto c0Vec = instrument->getStringParameter("C0");
if (c0Vec.empty())
throw std::runtime_error(
"Could not find parameter 'C0' in parameter "
"file. Cannot apply exponential correction.");
const auto c1Vec = instrument->getStringParameter("C1");
if (c1Vec.empty())
throw std::runtime_error(
"Could not find parameter 'C1' in parameter "
"file. Cannot apply exponential correction.");
alg->setProperty("C0", c0Vec[0]);
alg->setProperty("C1", c1Vec[0]);
}
alg->setProperty("NormalizeByIntegratedMonitors", false);
} catch (std::runtime_error &e) {
g_log.error() << e.what()
<< ". Polynomial correction will not be performed.";
alg->setProperty("CorrectionAlgorithm", "None");
}
} else {
alg->setProperty("CorrectionAlgorithm", "None");
}
}
auto ReflectometryReductionOneAuto3::getRebinParams(
const MatrixWorkspace_sptr &inputWS, const double theta) -> RebinParams {
bool qMinIsDefault = true, qMaxIsDefault = true;
auto const qMin = getPropertyOrDefault("MomentumTransferMin",
inputWS->x(0).front(), qMinIsDefault);
auto const qMax = getPropertyOrDefault("MomentumTransferMax",
inputWS->x(0).back(), qMaxIsDefault);
return RebinParams{qMin, qMinIsDefault, qMax, qMaxIsDefault,
getQStep(inputWS, theta)};
}
/** Get the binning step the final output workspace in Q
*
* @param inputWS :: the workspace in Q
* @param theta :: the angle of this run
* @return :: the rebin step in Q, or none if it could not be found
*/
boost::optional<double>
ReflectometryReductionOneAuto3::getQStep(const MatrixWorkspace_sptr &inputWS,
const double theta) {
Property *qStepProp = getProperty("MomentumTransferStep");
double qstep;
if (!qStepProp->isDefault()) {
qstep = getProperty("MomentumTransferStep");
qstep = -qstep;
} else {
if (theta == 0.0) {
throw std::runtime_error(
"Theta determined from the detector positions is "
"0.0. Please provide a value for theta manually "
"or correct the detector position before running "
"this algorithm.");
}
IAlgorithm_sptr calcRes = createChildAlgorithm("NRCalculateSlitResolution");
calcRes->setProperty("Workspace", inputWS);
calcRes->setProperty("TwoTheta", 2 * theta);
calcRes->execute();
if (!calcRes->isExecuted()) {
return boost::none;
}
qstep = calcRes->getProperty("Resolution");
qstep = -qstep;
}
return qstep;
}
/** Rebin a workspace in Q.
*
* @param inputWS :: the workspace in Q
* @param params :: A vector containing the three rebin parameters (min, step
* and max)
* @return :: the output workspace
*/
MatrixWorkspace_sptr
ReflectometryReductionOneAuto3::rebin(const MatrixWorkspace_sptr &inputWS,
const RebinParams ¶ms) {
IAlgorithm_sptr algRebin = createChildAlgorithm("Rebin");
algRebin->initialize();
algRebin->setProperty("InputWorkspace", inputWS);
algRebin->setProperty("OutputWorkspace", inputWS);
algRebin->setProperty("Params", params.asVector());
algRebin->execute();
MatrixWorkspace_sptr binnedWS = algRebin->getProperty("OutputWorkspace");
return binnedWS;
}
/** Optionally scale a workspace.
*
* @param inputWS :: the workspace to scale
* @return :: the scaled workspace if the ScaleFactor was set or the
* unchanged input workspace otherwise.
*/
MatrixWorkspace_sptr
ReflectometryReductionOneAuto3::scale(MatrixWorkspace_sptr inputWS) {
Property *scaleProp = getProperty("ScaleFactor");
if (scaleProp->isDefault())
return inputWS;
double scaleFactor = getProperty("ScaleFactor");
IAlgorithm_sptr algScale = createChildAlgorithm("Scale");
algScale->initialize();
algScale->setProperty("InputWorkspace", inputWS);
algScale->setProperty("OutputWorkspace", inputWS);
algScale->setProperty("Factor", 1.0 / scaleFactor);
algScale->execute();
MatrixWorkspace_sptr scaledWS = algScale->getProperty("OutputWorkspace");
return scaledWS;
}
/** Optionally crop a workspace in Q.
*
* @param inputWS :: the workspace to scale
* @param params :: A vector containing the three rebin parameters (min, step
* and max)
* @return :: the rebinned workspace if a min/max was set, or the unchanged
* input workspace otherwise.
*/
MatrixWorkspace_sptr
ReflectometryReductionOneAuto3::cropQ(MatrixWorkspace_sptr inputWS,
const RebinParams ¶ms) {
if (params.qMinIsDefault && params.qMaxIsDefault)
return inputWS;
IAlgorithm_sptr algCrop = createChildAlgorithm("CropWorkspace");
algCrop->initialize();
algCrop->setProperty("InputWorkspace", inputWS);
algCrop->setProperty("OutputWorkspace", inputWS);
if (!(params.qMinIsDefault))
algCrop->setProperty("XMin", params.qMin);
if (!(params.qMaxIsDefault))
algCrop->setProperty("XMax", params.qMax);
algCrop->execute();
MatrixWorkspace_sptr croppedWS = algCrop->getProperty("OutputWorkspace");
return croppedWS;
}
/**
* @brief Get the Property Or return a default given value
*
* @param propertyName : the name of the property to get
* @param defaultValue : the default value to use if the property is not set
* @param isDefault [out] : true if the default value was used
*/
double ReflectometryReductionOneAuto3::getPropertyOrDefault(
const std::string &propertyName, const double defaultValue,
bool &isDefault) {
Property *property = getProperty(propertyName);
isDefault = property->isDefault();
if (isDefault)
return defaultValue;
else
return getProperty(propertyName);
}
/** Check if input workspace is a group
*/
bool ReflectometryReductionOneAuto3::checkGroups() {
const std::string wsName = getPropertyValue("InputWorkspace");
return (AnalysisDataService::Instance().doesExist(wsName) &&
AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(wsName));
} // namespace Algorithms
void ReflectometryReductionOneAuto3::setOutputWorkspaces(
const WorkspaceNames &outputGroupNames,
std::vector<std::string> &IvsLamGroup,
std::vector<std::string> &IvsQBinnedGroup,
std::vector<std::string> &IvsQGroup) {
// Group the IvsQ and IvsLam workspaces
Algorithm_sptr groupAlg = createChildAlgorithm("GroupWorkspaces");
groupAlg->setChild(false);
groupAlg->setRethrows(true);
if (!IvsLamGroup.empty()) {
groupAlg->setProperty("InputWorkspaces", IvsLamGroup);
groupAlg->setProperty("OutputWorkspace", outputGroupNames.iVsLam);
groupAlg->execute();
}
groupAlg->setProperty("InputWorkspaces", IvsQBinnedGroup);
groupAlg->setProperty("OutputWorkspace", outputGroupNames.iVsQBinned);
groupAlg->execute();
groupAlg->setProperty("InputWorkspaces", IvsQGroup);
groupAlg->setProperty("OutputWorkspace", outputGroupNames.iVsQ);
groupAlg->execute();
setPropertyValue("OutputWorkspace", outputGroupNames.iVsQ);
setPropertyValue("OutputWorkspaceBinned", outputGroupNames.iVsQBinned);
setPropertyValue("OutputWorkspaceWavelength", outputGroupNames.iVsLam);
}
/** Process groups. Groups are processed differently depending on transmission
* runs and polarization analysis. If transmission run is a matrix workspace,
* it will be applied to each of the members in the input workspace group. If
* transmission run is a workspace group, the behaviour is different depending
* on polarization analysis. If polarization analysis is false each item in the
* transmission group is associated with the corresponding item in the input
* workspace group. If polarization analysis is true items in the transmission
* group will be summed to produce a matrix workspace that will be applied to
* each of the items in the input workspace group. See documentation of this
* algorithm for more details.
*/
bool ReflectometryReductionOneAuto3::processGroups() {
// this algorithm effectively behaves as MultiPeriodGroupAlgorithm
m_usingBaseProcessGroups = true;
// Get our input workspace group
auto group = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
getPropertyValue("InputWorkspace"));
// Get the output workspace names
const auto output = getOutputWorkspaceNames();
// Create a copy of ourselves
Algorithm_sptr alg =
createChildAlgorithm(name(), -1, -1, isLogging(), version());
alg->setChild(false);
alg->setRethrows(true);
// Copy all the non-workspace properties over
const std::vector<Property *> props = getProperties();
for (auto &prop : props) {
if (prop) {
IWorkspaceProperty *wsProp = dynamic_cast<IWorkspaceProperty *>(prop);
if (!wsProp)
alg->setPropertyValue(prop->name(), prop->value());
}
}
const bool polarizationAnalysisOn = getProperty("PolarizationAnalysis");
// Check if the transmission runs are groups or not
const std::string firstTrans = getPropertyValue("FirstTransmissionRun");
WorkspaceGroup_sptr firstTransG;
MatrixWorkspace_sptr firstTransSum;
if (!firstTrans.empty()) {
auto firstTransWS =
AnalysisDataService::Instance().retrieveWS<Workspace>(firstTrans);
firstTransG = std::dynamic_pointer_cast<WorkspaceGroup>(firstTransWS);
if (!firstTransG) {
alg->setProperty("FirstTransmissionRun", firstTrans);
} else {
g_log.information("A group has been passed as the first transmission run "
"so the first run only is being used");
alg->setProperty("FirstTransmissionRun", firstTransG->getItem(0));
}
}
const std::string secondTrans = getPropertyValue("SecondTransmissionRun");
WorkspaceGroup_sptr secondTransG;
MatrixWorkspace_sptr secondTransSum;
if (!secondTrans.empty()) {
auto secondTransWS =
AnalysisDataService::Instance().retrieveWS<Workspace>(secondTrans);
secondTransG = std::dynamic_pointer_cast<WorkspaceGroup>(secondTransWS);
if (!secondTransG) {
alg->setProperty("SecondTransmissionRun", secondTrans);
} else {
g_log.information("A group has been passed as the second transmission "
"run so the first run only is being used");
alg->setProperty("secondTransmissionRun", secondTransG->getItem(0));
}
}
std::vector<std::string> IvsQBinnedGroup, IvsQGroup, IvsLamGroup;
std::string runNumber = getRunNumberForWorkspaceGroup(group);
// Execute algorithm over each group member
for (size_t i = 0; i < group->size(); ++i) {
auto inputName = group->getItem(i)->getName();
auto outputNames = getOutputNamesForGroups(inputName, runNumber, i);
const std::string IvsQName = outputNames.iVsQ;
const std::string IvsQBinnedName = outputNames.iVsQBinned;
const std::string IvsLamName = outputNames.iVsLam;
alg->setProperty("InputWorkspace", inputName);
alg->setProperty("Debug", true);
alg->setProperty("OutputWorkspace", IvsQName);
alg->setProperty("OutputWorkspaceBinned", IvsQBinnedName);
alg->setProperty("OutputWorkspaceWavelength", IvsLamName);
if (!isDefault("FloodWorkspace")) {
MatrixWorkspace_sptr flood = getProperty("FloodWorkspace");
alg->setProperty("FloodWorkspace", flood);
}
alg->execute();
IvsQGroup.emplace_back(IvsQName);
IvsQBinnedGroup.emplace_back(IvsQBinnedName);
if (AnalysisDataService::Instance().doesExist(IvsLamName)) {
IvsLamGroup.emplace_back(IvsLamName);
}
}
// Group the IvsQ and IvsLam workspaces
Algorithm_sptr groupAlg = createChildAlgorithm("GroupWorkspaces");
groupAlg->setChild(false);
groupAlg->setRethrows(true);
if (!IvsLamGroup.empty()) {
groupAlg->setProperty("InputWorkspaces", IvsLamGroup);
groupAlg->setProperty("OutputWorkspace", output.iVsLam);
groupAlg->execute();
}
groupAlg->setProperty("InputWorkspaces", IvsQBinnedGroup);
groupAlg->setProperty("OutputWorkspace", output.iVsQBinned);
groupAlg->execute();
groupAlg->setProperty("InputWorkspaces", IvsQGroup);
groupAlg->setProperty("OutputWorkspace", output.iVsQ);
groupAlg->execute();
// Set other properties so they can be updated in the Reflectometry interface
setPropertyValue("ThetaIn", alg->getPropertyValue("ThetaIn"));
setPropertyValue("MomentumTransferMin",
alg->getPropertyValue("MomentumTransferMin"));
setPropertyValue("MomentumTransferMax",
alg->getPropertyValue("MomentumTransferMax"));
setPropertyValue("MomentumTransferStep",
alg->getPropertyValue("MomentumTransferStep"));
setPropertyValue("ScaleFactor", alg->getPropertyValue("ScaleFactor"));
setOutputWorkspaces(output, IvsLamGroup, IvsQBinnedGroup, IvsQGroup);
if (!polarizationAnalysisOn) {
// No polarization analysis. Reduction stops here
return true;
}
applyPolarizationCorrection(output.iVsLam);
// Polarization correction may have changed the number of workspaces in the
// groups
IvsLamGroup.clear();
IvsQBinnedGroup.clear();
IvsQGroup.clear();
// Now we've overwritten the IvsLam workspaces, we'll need to recalculate
// the IvsQ ones
alg->setProperty("FirstTransmissionRun", "");
alg->setProperty("SecondTransmissionRun", "");
alg->setProperty("CorrectionAlgorithm", "None");
auto outputIvsLamNames = workspaceNamesInGroup(output.iVsLam);
for (size_t i = 0; i < outputIvsLamNames.size(); ++i) {
auto inputName = group->getItem(i)->getName();
auto outputNames = getOutputNamesForGroups(inputName, runNumber, i);
const std::string IvsQName = outputNames.iVsQ;
const std::string IvsQBinnedName = outputNames.iVsQBinned;
const std::string IvsLamName = outputIvsLamNames[i];
// Find the spectrum processing instructions for ws index 0
auto currentWorkspace = std::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve(outputIvsLamNames[i]));
auto newProcInst = convertToSpectrumNumber("0", currentWorkspace);
alg->setProperty("ProcessingInstructions", newProcInst);
alg->setProperty("InputWorkspace", IvsLamName);
alg->setProperty("OutputWorkspace", IvsQName);
alg->setProperty("OutputWorkspaceBinned", IvsQBinnedName);
alg->setProperty("OutputWorkspaceWavelength", IvsLamName);
alg->execute();
IvsQBinnedGroup.emplace_back(IvsQBinnedName);
IvsQGroup.emplace_back(IvsQName);
if (AnalysisDataService::Instance().doesExist(IvsLamName)) {
IvsLamGroup.emplace_back(IvsLamName);
}
}
setOutputWorkspaces(output, IvsLamGroup, IvsQBinnedGroup, IvsQGroup);
return true;
}
/** Get the output workspace names for a workspace in a group.
* If an input workspace has been passed with the format
* TOF_<runNumber>_<otherInfo> then the output workspaces will be of the same
* format otherwise they are numbered according to the wsGroupNumber
*/
auto ReflectometryReductionOneAuto3::getOutputNamesForGroups(
const std::string &inputName, const std::string &runNumber,
const size_t wsGroupNumber) -> WorkspaceNames {
const auto output = getOutputWorkspaceNames();
std::string informativeName = "TOF" + runNumber + "_";
WorkspaceNames outputNames;
const auto inputNameSize = inputName.size();
const auto informativeNameSize = informativeName.size();