17#include "MantidIndexing/Extract.h"
18#include "MantidIndexing/IndexInfo.h"
26#include <boost/algorithm/string.hpp>
27#include <boost/format.hpp>
39 :
Algorithm(), m_FilterWithDerivative(true),
41 m_min_Eresolution(0.08),
43 m_max_Eresolution(0.5e-3), m_peakEnergyRatio2reject(0.1), m_phase(0), m_chopper(), m_pFilterLog(
nullptr) {}
50 "The input workspace containing the monitor's spectra "
51 "measured after the last chopper");
52 auto nonNegative = std::make_shared<Kernel::BoundedValidator<int>>();
53 nonNegative->setLower(0);
56 "The workspace index (ID) of the spectra, containing first monitor's"
57 " signal to analyze.");
59 "The workspace index (ID) of the spectra, containing second monitor's"
60 " signal to analyze.");
63 "Name of the instrument log, "
64 "containing chopper angular velocity. If 'Defined in IDF' "
65 "option is specified, "
66 "the log name is obtained from the IDF");
68 "Name of the instrument log, "
69 "containing chopper delay time or chopper phase v.r.t. the pulse time. "
70 "If 'Defined in IDF' option is specified, "
71 "the log name is obtained from IDF");
73 "Name of the instrument log, "
74 "with positive values indicating that instrument is running\n "
75 "and 0 or negative that it is not.\n"
76 "The log is used to identify time interval to evaluate"
77 " chopper speed and chopper delay which matter.\n"
78 "If such log is not present, average log values are calculated "
79 "within experiment start&end time range.");
81 "Use derivative of 'FilterBaseLog' "
82 "rather then log values itself to filter invalid time intervals.\n"
83 "Invalid values are then the "
84 "values where the derivative of the log turns zero.\n"
85 "E.g. the 'proton_chage' log grows for each frame "
86 "when instrument is counting and is constant otherwise.");
88 std::make_unique<Kernel::EnabledWhenProperty>(
91 auto maxInRange = std::make_shared<Kernel::BoundedValidator<double>>();
92 maxInRange->setLower(1.e-6);
93 maxInRange->setUpper(0.1);
96 "The maximal energy resolution possible for an "
97 "instrument at working energies (full width at half "
98 "maximum). \nPeaks, sharper then "
99 "this width are rejected. Accepted limits are: 1e^(-6)-0.1");
101 auto minInRange = std::make_shared<Kernel::BoundedValidator<double>>();
102 minInRange->setLower(0.001);
103 minInRange->setUpper(0.5);
105 "The minimal energy resolution possible for an "
106 "instrument at working energies (full width at half maximum).\n"
107 "Peaks broader then this width are rejected. Accepted limits are: "
110 auto peakInRange = std::make_shared<Kernel::BoundedValidator<double>>();
111 peakInRange->setLower(0.0);
112 minInRange->setUpper(1.);
114 "Ratio of a peak energy to the maximal energy among all peaks. "
115 "If the ratio is lower then the value specified here, "
116 "peak is treated as insignificant and rejected.\n"
117 "Accepted limits are:0.0 (All accepted) to 1 -- only one peak \n"
118 "(or peaks with max and equal intensity) are accepted.");
120 "Usually peaks are analyzed and accepted "
121 "only if identified on both monitors. If this property is set to true, "
122 "only first monitor peaks are analyzed.\n"
123 "This is debugging option as getEi has to use both monitors.");
127 "Name of the output matrix workspace, containing single spectra with"
128 " monitor peaks energies\n"
129 "together with total intensity within each peak.");
141template <
class T>
void removeInvalidValues(
const std::vector<bool> &guessValid, std::vector<T> &guess) {
142 std::vector<T> new_guess;
143 new_guess.reserve(guess.size());
145 for (
size_t i = 0; i < guessValid.size(); i++) {
147 new_guess.emplace_back(guess[i]);
150 new_guess.swap(guess);
159 peakKeeper(
double pos,
double heigh,
double sig) :
position(pos),
height(heigh),
sigma(sig) {
160 this->energy = std::sqrt(2 * M_PI) *
height *
sigma;
163 bool operator<(
const peakKeeper &str)
const {
return (energy > str.energy); }
177 auto pInstrument = inputWS->getInstrument();
182 m_chopper = pInstrument->getComponentByName(
"chopper-position");
184 throw std::runtime_error(
"Instrument " + pInstrument->getName() +
" does not have 'chopper-position' component");
186 auto phase =
m_chopper->getNumberParameter(
"initial_phase");
189 throw std::runtime_error(
"Can not find initial_phase parameter"
190 " attached to the chopper-position component");
192 if (phase.size() > 1) {
193 throw std::runtime_error(
"Can not deal with multiple phases for initial_phase"
194 " parameter attached to the chopper-position component");
202 double chopSpeed, chopDelay;
204 g_log.
debug() << boost::str(boost::format(
"*Identified avrg ChopSpeed: %8.2f and Delay: %8.2f\n") % chopSpeed %
207 auto moderator = pInstrument->getSource();
208 double chopDistance =
m_chopper->getDistance(*moderator);
209 double velocity = chopDistance / chopDelay;
216 const auto &detector1 = inputWS->spectrumInfo().detector(det1WSIndex);
217 double mon1Distance = detector1.getDistance(*moderator);
218 double TOF0 = mon1Distance / velocity;
230 auto &baseSpectrum = inputWS->getSpectrum(det1WSIndex);
231 std::pair<double, double> TOF_range = baseSpectrum.getXDataRange();
233 double Period = (0.5 * 1.e+6) / chopSpeed;
238 std::vector<double> guess_opening;
241 if (guess_opening.empty()) {
242 throw std::runtime_error(
243 "Can not find any chopper opening time within TOF range: " + boost::lexical_cast<std::string>(TOF_range.first) +
244 ':' + boost::lexical_cast<std::string>(TOF_range.second));
249 std::pair<double, double> Mon1_Erange = monitorWS->getSpectrum(0).getXDataRange();
250 std::pair<double, double> Mon2_Erange = monitorWS->getSpectrum(1).getXDataRange();
251 double eMin = std::max(Mon1_Erange.first, Mon2_Erange.first);
252 double eMax = std::min(Mon1_Erange.second, Mon2_Erange.second);
253 g_log.
debug() << boost::str(boost::format(
"Monitors record data in energy range Emin=%8.2f; Emax=%8.2f\n") % eMin %
257 std::vector<double> guess_ei;
258 guess_ei.reserve(guess_opening.size());
260 for (
double time : guess_opening) {
261 double eGuess = destUnit->singleFromTOF(time);
262 if (eGuess > eMin && eGuess < eMax) {
263 guess_ei.emplace_back(eGuess);
267 " fell within both monitor's recording energy range\n";
269 for (
double ei : guess_ei) {
270 g_log.
debug() << boost::str(boost::format(
" %8.2f; ") % ei);
274 std::sort(guess_ei.begin(), guess_ei.end());
276 std::vector<size_t> irange_min, irange_max;
277 std::vector<bool> guessValid;
279 g_log.
debug() <<
"*Looking for real energy peaks on first monitor\n";
280 findBinRanges(monitorWS->x(0), monitorWS->y(0), guess_ei, this->m_min_Eresolution / (2. * std::sqrt(2. * M_LN2)),
281 irange_min, irange_max, guessValid);
284 removeInvalidValues<double>(guessValid, guess_ei);
287 std::vector<size_t> irange1_min, irange1_max;
289 g_log.
debug() <<
"*Looking for real energy peaks on second monitor\n";
290 findBinRanges(monitorWS->x(1), monitorWS->y(1), guess_ei, this->m_min_Eresolution / (2. * std::sqrt(2. * M_LN2)),
291 irange1_min, irange1_max, guessValid);
292 removeInvalidValues<double>(guessValid, guess_ei);
293 removeInvalidValues<size_t>(guessValid, irange_min);
294 removeInvalidValues<size_t>(guessValid, irange_max);
298 irange1_min.assign(irange_min.begin(), irange_min.end());
299 irange1_max.assign(irange_max.begin(), irange_max.end());
302 " peaks with sufficient signal around guess chopper opening\n";
304 std::vector<peakKeeper> peaks;
306 double maxPeakEnergy(0);
307 std::vector<size_t> monsRangeMin(2), monsRangeMax(2);
308 for (
size_t i = 0; i < guess_ei.size(); i++) {
309 monsRangeMin[0] = irange_min[i];
310 monsRangeMax[0] = irange_max[i];
311 monsRangeMin[1] = irange1_min[i];
312 monsRangeMax[1] = irange1_max[i];
318 if (peaks.back().energy > maxPeakEnergy)
319 maxPeakEnergy = peaks.back().energy;
324 size_t nPeaks = peaks.size();
326 throw std::runtime_error(
"Can not identify any energy peaks");
329 guessValid.resize(nPeaks);
330 bool needsRemoval(
false);
331 for (
size_t i = 0; i < nPeaks; i++) {
332 peaks[i].energy /= maxPeakEnergy;
334 guessValid[i] =
false;
335 g_log.
debug() <<
"*Rejecting peak at Ei=" + boost::lexical_cast<std::string>(peaks[i].
position) +
336 " as its total energy lower then the threshold\n";
339 guessValid[i] =
true;
343 removeInvalidValues<peakKeeper>(guessValid, peaks);
344 nPeaks = peaks.size();
346 std::sort(peaks.begin(), peaks.end());
349 auto result_ws = create<Workspace2D>(1, Points(nPeaks));
351 HistogramX peaks_positions(peaks.size());
352 std::transform(peaks.cbegin(), peaks.cend(), peaks_positions.begin(), [](peakKeeper peak) { return peak.position; });
353 auto &Signal = result_ws->mutableY(0);
354 std::transform(peaks.cbegin(), peaks.cend(), Signal.begin(), [](peakKeeper peak) { return peak.height; });
356 auto &Error = result_ws->mutableE(0);
357 std::transform(peaks.cbegin(), peaks.cend(), Error.begin(), [](peakKeeper peak) { return peak.sigma; });
359 result_ws->setPoints(0, peaks_positions);
361 setProperty(
"OutputWorkspace", std::move(result_ws));
373 std::shared_ptr<Kernel::Unit> &destUnit) {
375 g_log.
debug() <<
"*Found : " << guess_opening.size()
376 <<
" chopper prospective opening within time frame: " << TOF_range.first <<
" to: " << TOF_range.second
379 for (
double time : guess_opening) {
380 g_log.
debug() << boost::str(boost::format(
" %8.2f; ") % time);
383 g_log.
debug() <<
" Corresponding to energies:\n";
384 for (
double time : guess_opening) {
385 double ei = destUnit->singleFromTOF(time);
386 g_log.
debug() << boost::str(boost::format(
" %8.2f; ") % ei);
401 std::string filerLogName;
402 std::string filterBase =
getProperty(
"FilterBaseLog");
403 if (boost::iequals(filterBase,
"Defined in IDF")) {
404 filerLogName =
m_chopper->getStringParameter(
"FilterBaseLog")[0];
407 filerLogName = filterBase;
412 }
catch (std::runtime_error &) {
413 g_log.
warning() <<
" Can not retrieve (double) filtering log: " + filerLogName +
414 " from current workspace\n"
415 " Using total experiment range to "
416 "find logs averages for chopper parameters\n";
436 const std::vector<size_t> &monsRangeMin,
const std::vector<size_t> &monsRangeMax,
437 double &peakPos,
double &peakHeight,
double &peakTwoSigma) {
442 double sMin(std::numeric_limits<double>::max());
444 double xOfMax(0), dXmax(0);
447 const auto &
X = inputWS->x(
index);
448 const auto &S = inputWS->y(
index);
449 size_t ind_min = monsRangeMin[
index];
450 size_t ind_max = monsRangeMax[
index];
452 if (std::fabs(
double(ind_max - ind_min)) < 5)
459 for (
size_t i = ind_min; i < ind_max; i++) {
460 double dX =
X[i + 1] -
X[i];
461 double signal = S[i] / dX;
473 if (sMax * dXmax <= 2)
478 double SmoothRange = 2 * maxSigma;
480 std::vector<double> SAvrg, binsAvrg;
483 double realPeakPos(xOfMax);
485 bool foundRealPeakPos(
false);
486 std::vector<double> der1Avrg, der2Avrg, peaks, hillsPos, SAvrg1, binsAvrg1;
490 foundRealPeakPos =
true;
491 realPeakPos = peaks[0];
494 size_t ic(0), stay_still_count(0);
495 bool iterations_fail(
false);
496 while ((nPeaks > 1 || nHills > 2) && (!iterations_fail)) {
498 const auto nPrevHills = nHills;
503 binsAvrg.swap(binsAvrg1);
504 if (nPeaks == 1 && !foundRealPeakPos) {
505 foundRealPeakPos =
true;
506 realPeakPos = peaks[0];
509 if (nPrevHills <= nHills) {
512 stay_still_count = 0;
514 if (ic > 50 || stay_still_count > 3)
515 iterations_fail =
true;
517 if (iterations_fail) {
519 " smoothing iterations at still_count: " +
std::to_string(stay_still_count) +
520 " Wrong energy or noisy peak at Ei=" + boost::lexical_cast<std::string>(Ei)
524 " at energy: " + boost::lexical_cast<std::string>(Ei) +
527 g_log.
debug() <<
"*Peak rejected as n-peaks !=1 after averaging\n";
534 peakTwoSigma = hillsPos[peakIndex + 1] - hillsPos[peakIndex];
536 if (hillsPos.size() == 2) {
537 peakTwoSigma = hillsPos[1] - hillsPos[0];
547 peakHeight = Intensity / (0.5 * std::sqrt(2. * M_PI) * peakTwoSigma) - sMin;
548 peakPos = realPeakPos;
566 const std::vector<size_t> &monsRangeMin,
const std::vector<size_t> &monsRangeMax,
572 double peak1Pos, peak1TwoSigma, peak1Height;
573 if (!
peakGuess(inputWS, 0, Ei, monsRangeMin, monsRangeMax, peak1Pos, peak1Height, peak1TwoSigma))
575 if (0.25 * peak1TwoSigma > maxSigma || peak1TwoSigma < minSigma) {
576 g_log.
debug() <<
"*Rejecting due to width: Peak at mon1 Ei=" + boost::lexical_cast<std::string>(peak1Pos) +
577 " with Height:" + boost::lexical_cast<std::string>(peak1Height) +
578 " and 2*Sigma: " + boost::lexical_cast<std::string>(peak1TwoSigma)
584 double peak2Pos, peak2TwoSigma, peak2Height;
585 if (!
peakGuess(inputWS, 1, Ei, monsRangeMin, monsRangeMax, peak2Pos, peak2Height, peak2TwoSigma))
594 if (std::fabs(peak1Pos - peak2Pos) > 0.25 * (peak1TwoSigma + peak2TwoSigma)) {
595 g_log.
debug() <<
"*Rejecting due to displacement between Peak at mon1: Ei=" +
596 boost::lexical_cast<std::string>(peak1Pos) +
597 " with Height:" + boost::lexical_cast<std::string>(peak1Height) +
598 " and 2*Sigma: " + boost::lexical_cast<std::string>(peak1TwoSigma) +
599 "\n and Peak at mon2: Ei= " + boost::lexical_cast<std::string>(peak2Pos) +
600 "and height: " + boost::lexical_cast<std::string>(peak1Height)
608 twoSigma = peak1TwoSigma;
619bool signChanged(
double val,
int &prevSign) {
620 int curSign = (val >= 0 ? 1 : -1);
621 bool changed = curSign != prevSign;
640 std::vector<double> &deriv, std::vector<double> &zeros) {
641 size_t nPoints = signal.size();
642 deriv.resize(nPoints);
645 std::list<double> funVal;
646 double bin0 = bins[1] - bins[0];
647 double f0 = signal[0] / bin0;
648 double bin1 = bins[2] - bins[1];
649 double f1 = signal[1] / bin1;
653 funVal.push_front(f1);
654 deriv[0] = 2 * (f1 - f0) / (bin0 + bin1);
655 int prevSign = (deriv[0] >= 0 ? 1 : -1);
657 for (
size_t i = 1; i < nPoints - 1; i++) {
658 bin1 = (bins[i + 2] - bins[i + 1]);
659 f1 = signal[i + 1] / bin1;
660 deriv[i] = (f1 - f0) / (bins[i + 2] - bins[i]);
663 funVal.push_front(f1);
665 if (signChanged(deriv[i], prevSign)) {
667 zeros.emplace_back(0.5 * (bins[i - 1] + bins[i]));
670 deriv[nPoints - 1] = 2 * (f1 - f0) / (bin1 + bin0);
671 if (signChanged(deriv[nPoints - 1], prevSign)) {
672 zeros.emplace_back(bins[nPoints - 1]);
680void getBinRange(
const HistogramData::HistogramX &eBins,
double eMin,
double eMax,
size_t &index_min,
683 const auto &bins = eBins.rawData();
684 const size_t nBins = bins.size();
685 if (eMin <= bins[0]) {
691 if (eMax >= eBins[nBins - 1]) {
692 index_max = nBins - 1;
695 if (index_max >= nBins)
696 index_max = nBins - 1;
701bool refineEGuess(
const HistogramX &eBins,
const HistogramY &signal,
double &eGuess,
size_t index_min,
704 size_t ind_Emax = index_min;
706 for (
size_t i = index_min; i < index_max; i++) {
707 double dX = eBins[i + 1] - eBins[i];
708 double sig = signal[i] /
dX;
714 if (ind_Emax == index_min || ind_Emax == index_max) {
717 eGuess = 0.5 * (eBins[ind_Emax] + eBins[ind_Emax + 1]);
742 double eResolution, std::vector<size_t> &irangeMin, std::vector<size_t> &irangeMax,
743 std::vector<bool> &guessValid) {
746 guessValid.resize(guess_energy.size());
749 size_t ind_min, ind_max;
754 std::vector<peakKeeper2> guess_peak(guess_energy.size());
755 for (
size_t nGuess = 0; nGuess < guess_energy.size(); nGuess++) {
756 double eGuess = guess_energy[nGuess];
757 getBinRange(eBins, eGuess * (1 - 4 * eResolution), eGuess * (1 + 4 * eResolution), ind_min, ind_max);
758 guess_peak[nGuess] = peakKeeper2(eBins[ind_min], eBins[ind_max]);
761 for (
size_t i = 1; i < guess_energy.size(); i++) {
763 double mid_pnt = 0.5 * (guess_peak[i - 1].right_rng + guess_peak[i].left_rng);
764 guess_peak[i - 1].right_rng = mid_pnt;
765 guess_peak[i].left_rng = mid_pnt;
769 for (
size_t nGuess = 0; nGuess < guess_energy.size(); nGuess++) {
771 double eGuess = guess_energy[nGuess];
772 getBinRange(eBins, guess_peak[nGuess].
left_rng, guess_peak[nGuess].
right_rng, ind_min, ind_max);
774 if (refineEGuess(eBins, signal, eGuess, ind_min, ind_max)) {
775 getBinRange(eBins, std::max(guess_peak[nGuess].
left_rng, eGuess * (1 - 3 * eResolution)),
776 std::max(guess_peak[nGuess].
right_rng, eGuess * (1 + 3 * eResolution)), ind_min, ind_max);
777 irangeMin.emplace_back(ind_min);
778 irangeMax.emplace_back(ind_max);
779 guessValid[nGuess] =
true;
781 guessValid[nGuess] =
false;
782 g_log.
debug() <<
"*Incorrect guess energy: " << boost::lexical_cast<std::string>(eGuess)
783 <<
" no energy peak found in 4*Sigma range\n";
788 if (!irangeMax.empty()) {
789 if (irangeMax[0] < irangeMin[0]) {
790 irangeMax.swap(irangeMin);
808 wsIndex0 = inputWS->getIndexFromSpectrumNumber(specNum1);
810 size_t wsIndex1 = inputWS->getIndexFromSpectrumNumber(specNum2);
813 std::shared_ptr<API::HistoWorkspace> working_ws = DataObjects::create<API::HistoWorkspace>(
814 *inputWS, Indexing::extract(inputWS->indexInfo(), std::vector<size_t>{wsIndex0, wsIndex1}),
815 inputWS->histogram(wsIndex0));
818 working_ws->setSharedY(0, inputWS->sharedY(wsIndex0));
820 working_ws->setSharedY(1, inputWS->sharedY(wsIndex1));
822 working_ws->setSharedE(0, inputWS->sharedE(wsIndex0));
824 working_ws->setSharedE(1, inputWS->sharedE(wsIndex1));
826 if (inputWS->getAxis(0)->unit()->caption() !=
"Energy") {
829 conv->setProperty(
"InputWorkspace", working_ws);
830 conv->setProperty(
"OutputWorkspace", working_ws);
831 conv->setPropertyValue(
"Target",
"Energy");
832 conv->setPropertyValue(
"EMode",
"Elastic");
851 std::vector<double> &guess_opening_times) {
853 if (ChopDelay >= TOF_range.second) {
854 std::string chop = boost::str(boost::format(
"%.2g") % ChopDelay);
855 std::string t_min = boost::str(boost::format(
"%.2g") % TOF_range.first);
856 std::string t_max = boost::str(boost::format(
"%.2g") % TOF_range.second);
857 throw std::runtime_error(
"Logical error: Chopper opening time: " + chop +
" is outside of time interval: " + t_min +
858 ":" + t_max +
" of the signal, measured on monitors.");
862 size_t n_openings =
static_cast<size_t>((TOF_range.second - ChopDelay) / Period) + 1;
865 if (ChopDelay < TOF_range.first) {
866 n_start =
static_cast<size_t>((TOF_range.first - ChopDelay) / Period) + 1;
867 n_openings -= n_start;
870 guess_opening_times.resize(n_openings);
871 for (
size_t i = n_start; i < n_openings + n_start; i++) {
872 guess_opening_times[i - n_start] = ChopDelay +
static_cast<double>(i) * Period;
883 const std::string &propertyName) {
885 std::string LogName = this->
getProperty(propertyName);
886 if (boost::iequals(LogName,
"Defined in IDF")) {
887 auto AllNames =
m_chopper->getStringParameter(propertyName);
888 if (AllNames.size() != 1)
890 LogName = AllNames[0];
892 auto pIProperty = (inputWS->run().getProperty(LogName));
906 std::vector<Kernel::SplittingInterval> &splitter) {
915 throw std::runtime_error(
"Could not retrieve a time series property for the property name " + propertyName);
918 if (splitter.empty()) {
919 auto TimeStart = inputWS->run().startTime();
920 auto TimeEnd = inputWS->run().endTime();
921 pTimeSeries->filterByTime(TimeStart, TimeEnd);
923 pTimeSeries->filterByTimes(splitter);
925 if (pTimeSeries->size() == 0) {
926 throw std::runtime_error(
"Can not find average value for log defined by property" + propertyName +
927 " As no valid log values are found.");
930 return pTimeSeries->getStatistics().mean;
948bool SelectInterval(
const Types::Core::DateAndTime &t_beg,
const Types::Core::DateAndTime &t_end,
double value,
949 bool &inSelection, Types::Core::DateAndTime &startTime, Types::Core::DateAndTime &endTime) {
959 if (endTime > startTime)
976 std::vector<Kernel::SplittingInterval> splitter;
978 std::unique_ptr<Kernel::TimeSeriesProperty<double>> pDerivative;
981 bool inSelection(
false);
983 Types::Core::DateAndTime startTime, endTime;
987 auto it = dateAndTimes.begin();
990 std::map<Types::Core::DateAndTime, double> derivMap;
994 derivMap = pDerivative->valueAsCorrectMap();
995 itder = derivMap.begin();
999 if (dateAndTimes.size() <= 1) {
1000 SelectInterval(it->first, it->first, itder->second, inSelection, startTime, endTime);
1002 startTime = inputWS->run().startTime();
1003 endTime = inputWS->run().endTime();
1005 splitter.emplace_back(interval);
1007 throw std::runtime_error(
"filtered all data points. Nothing to do");
1010 SelectInterval(it->first, next->first, itder->second, inSelection, startTime, endTime);
1016 for (; next != dateAndTimes.end(); ++next, ++itder) {
1017 if (SelectInterval(it->first, next->first, itder->second, inSelection, startTime, endTime)) {
1019 splitter.emplace_back(interval);
1024 if (inSelection && (endTime > startTime)) {
1026 splitter.emplace_back(interval);
1030 chop_speed = this->
getAvrgLogValue(inputWS,
"ChopperSpeedLog", splitter);
1031 chop_speed = std::fabs(chop_speed);
1032 if (chop_speed < 1.e-7) {
1033 throw std::runtime_error(
"Chopper speed can not be zero ");
1035 chop_delay = std::fabs(this->
getAvrgLogValue(inputWS,
"ChopperDelayLog", splitter));
1040 throw std::runtime_error(
"ChopperDelayLog has been removed from workspace "
1041 "during the algorithm execution");
1042 std::string units = pProperty->units();
1044 if (units ==
"deg" || units.c_str()[0] == -80) {
1045 chop_delay *= 1.e+6 / (360. * chop_speed);
1047 chop_delay +=
m_phase / chop_speed;
1069 const std::shared_ptr<const Geometry::IComponent> &chopper,
1070 const std::string &prop_name,
const std::string &err_presence,
1071 const std::string &err_type,
bool fail, std::map<std::string, std::string> &result) {
1073 std::string LogName = algo->
getProperty(prop_name);
1074 if (boost::iequals(LogName,
"Defined in IDF")) {
1076 auto theLogs = chopper->getStringParameter(prop_name);
1077 if (theLogs.empty()) {
1079 result[prop_name] =
"Can not retrieve parameter " + prop_name +
" from the instrument definition file.";
1082 LogName = theLogs[0];
1084 result[prop_name] =
"Can not retrieve parameter " + prop_name +
" from the instrument definition file.";
1093 result[prop_name] =
"Workspace contains " + err_type + LogName +
" But its type is not a timeSeries property";
1096 }
catch (std::runtime_error &) {
1098 result[prop_name] =
"Workspace has to contain " + err_presence + LogName;
1112 std::map<std::string, std::string> result;
1116 result[
"Workspace"] =
"Input workspace can not be identified";
1119 if (!inputWS->isHistogramData()) {
1120 result[
"Workspace"] =
"Only histogram workspaces are currently supported. "
1121 "Rebin input workspace first.";
1126 inputWS->getIndexFromSpectrumNumber(specNum1);
1127 }
catch (std::runtime_error &) {
1128 result[
"Monitor1SpecID"] =
"Input workspace does not contain spectra with ID: " +
std::to_string(specNum1);
1132 inputWS->getIndexFromSpectrumNumber(specNum2);
1133 }
catch (std::runtime_error &) {
1134 result[
"Monitor2SpecID"] =
"Input workspace does not contain spectra with ID: " +
std::to_string(specNum2);
1137 m_chopper = inputWS->getInstrument()->getComponentByName(
"chopper-position");
1139 result[
"Workspace_chopper"] =
" For this algorithm to work workspace has"
1140 " to contain well defined 'chopper-position' component";
1144 check_time_series_property(
this, inputWS,
m_chopper,
"ChopperSpeedLog",
1145 "chopper speed log with name: ",
"chopper speed log ",
true, result);
1146 check_time_series_property(
this, inputWS,
m_chopper,
"ChopperDelayLog",
1147 "property related to chopper delay log with name: ",
"chopper delay log ",
true, result);
1148 bool failed = check_time_series_property(
this, inputWS,
m_chopper,
"FilterBaseLog",
1149 "filter base log named: ",
"filter base log: ",
false, result);
1151 g_log.
warning() <<
" Can not find a log to identify good DAE operations.\n"
1152 " Assuming that good operations start from experiment time=0";
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
Base class from which all concrete algorithm classes should be derived.
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.
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.
A property class for workspaces.
Estimate all incident energies, used by chopper instrument.
double getAvrgLogValue(const API::MatrixWorkspace_sptr &inputWS, const std::string &propertyName, std::vector< Kernel::SplittingInterval > &splitter)
Return average time series log value for the appropriately filtered log.
Kernel::TimeSeriesProperty< double > * m_pFilterLog
void findGuessOpeningTimes(const std::pair< double, double > &TOF_range, double ChopDelay, double Period, std::vector< double > &guess_opening_times)
function calculates list of provisional chopper opening times.
bool findMonitorPeak(const API::MatrixWorkspace_sptr &inputWS, double Ei, const std::vector< size_t > &monsRangeMin, const std::vector< size_t > &monsRangeMax, double &position, double &height, double &twoSigma)
Get energy of monitor peak if one is present.
Kernel::Property * getPLogForProperty(const API::MatrixWorkspace_sptr &inputWS, const std::string &propertyName)
Finds pointer to log value for the property with the name provided.
std::shared_ptr< const Geometry::IComponent > m_chopper
API::MatrixWorkspace_sptr buildWorkspaceToFit(const API::MatrixWorkspace_sptr &inputWS, size_t &wsIndex0)
Build 2-spectra workspace in units of energy, used as source to identify actual monitors spectra.
void exec() override
Executes the algorithm – found all existing monitor peaks.
void findChopSpeedAndDelay(const API::MatrixWorkspace_sptr &inputWS, double &chop_speed, double &chop_delay)
process logs and retrieve chopper speed and chopper delay
std::map< std::string, std::string > validateInputs() override
Cross-check properties with each other.
bool peakGuess(const API::MatrixWorkspace_sptr &inputWS, size_t index, double Ei, const std::vector< size_t > &monsRangeMin, const std::vector< size_t > &monsRangeMax, double &peakPos, double &peakHeight, double &peakTwoSigma)
Former lambda to identify guess values for a peak at given index and set up these parameters as input...
void findBinRanges(const HistogramData::HistogramX &eBins, const HistogramData::HistogramY &signal, const std::vector< double > &guess_energy, double eResolution, std::vector< size_t > &irangeMin, std::vector< size_t > &irangeMax, std::vector< bool > &guessValid)
Find indexes of each expected peak intervals.
size_t calcDerivativeAndCountZeros(const std::vector< double > &bins, const std::vector< double > &signal, std::vector< double > &deriv, std::vector< double > &zeros)
Bare-bone function to calculate numerical derivative, and estimate number of zeros this derivative ha...
bool m_FilterWithDerivative
if true, take derivate of the filter log to identify interval when instrument is running.
void printDebugModeInfo(const std::vector< double > &guess_opening, const std::pair< double, double > &TOF_range, std::shared_ptr< Kernel::Unit > &destUnit)
Auxiliary method to print guess chopper energies in debug mode.
void init() override
Initialization method.
void setFilterLog(const API::MatrixWorkspace_sptr &inputWS)
The internal procedure to set filter log from properties, defining it.
double m_min_Eresolution
maximal relative peak width to consider acceptable.
double m_peakEnergyRatio2reject
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertySettings(const std::string &name, std::unique_ptr< IPropertySettings > settings)
void debug(const std::string &msg)
Logs at debug level.
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
Base class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Class holding a start/end time and a destination for splitting event lists and logs.
A specialised Property class for holding a series of time-value pairs.
std::map< Types::Core::DateAndTime, TYPE > valueAsCorrectMap() const
Return the time series as a correct C++ map<DateAndTime, TYPE>.
std::unique_ptr< TimeSeriesProperty< double > > getDerivative() const
Return time series property, containing time derivative of current property.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
MANTID_KERNEL_DLL void smoothInRange(const std::vector< double > &input, std::vector< double > &output, double avrgInterval, std::vector< double > const *const binBndrs=nullptr, size_t startIndex=0, size_t endIndex=0, std::vector< double > *const outBins=nullptr)
Basic running average of input vector within specified range, considering variable bin-boundaries if ...
MANTID_KERNEL_DLL int getBinIndex(const std::vector< double > &bins, const double value)
Return the index into a vector of bin boundaries for a particular X value.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
int32_t specnum_t
Typedef for a spectrum Number.
std::string to_string(const wide_integer< Bits, Signed > &n)
constexpr bool operator<(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)
@ Input
An input workspace.
@ Output
An output workspace.