21#include <boost/lexical_cast.hpp>
46 "A sample data workspace.");
49 "A monitor workspace associated with the sample workspace.");
51 "This is the starting point for the incident energy calculation.");
54 "A workspace containing the "
55 "integrated detector vanadium.");
61 "A grouping workspace");
62 this->
declareProperty(
"AlternateGroupingTag",
"",
"Allows modification to the OldGroupingFile property name");
64 "The name for the output workspace.");
66 "The name for the output TIB workspace.");
73 g_log.
notice() <<
"Starting DgsConvertToEnergyTransfer\n";
75 const std::string reductionManagerName = this->
getProperty(
"ReductionProperties");
76 std::shared_ptr<PropertyManager> reductionManager;
80 throw std::runtime_error(
"DgsConvertToEnergyTransfer cannot run without a "
81 "reduction PropertyManager.");
88 std::string monWsName = inputWS->getName() +
"_monitors";
89 bool preserveEvents =
false;
93 g_log.
notice() <<
"Processing for " << facility <<
'\n';
94 double eiGuess = this->
getProperty(
"IncidentEnergyGuess");
98 if (
"SNS" == facility) {
102 throw std::runtime_error(
"Incident energy guess MUST be given!");
105 const bool useEiGuess = reductionManager->getProperty(
"UseIncidentEnergyGuess");
106 const double tZeroGuess = reductionManager->getProperty(
"TimeZeroGuess");
107 std::vector<double> etBinning = reductionManager->getProperty(
"EnergyTransferRange");
110 if (etBinning.empty()) {
111 double emin = -0.5 * eiGuess;
112 double deltaE = 0.01 * eiGuess;
113 double emax = 0.99 * eiGuess;
114 etBinning.emplace_back(emin);
115 etBinning.emplace_back(deltaE);
116 etBinning.emplace_back(emax);
119 double incidentEnergy = 0.0;
120 double monPeak = 0.0;
121 specnum_t eiMon1Spec =
static_cast<specnum_t>(reductionManager->getProperty(
"Monitor1SpecId"));
122 specnum_t eiMon2Spec =
static_cast<specnum_t>(reductionManager->getProperty(
"Monitor2SpecId"));
124 if (
"SNS" == facility) {
126 preserveEvents =
true;
129 incidentEnergy = eiGuess;
134 g_log.
notice() <<
"Trying to determine file name\n";
135 std::string runFileName = inputWS->run().getProperty(
"Filename")->value();
136 if (runFileName.empty())
137 throw std::runtime_error(
"Cannot find run filename, therefore cannot "
138 "find the initial energy");
140 std::string loadAlgName;
141 std::string fileProp;
142 if (boost::ends_with(runFileName,
"_event.nxs") || boost::ends_with(runFileName,
".nxs.h5") ||
143 boost::ends_with(runFileName,
".nxs")) {
145 loadAlgName =
"LoadNexusMonitors";
146 fileProp =
"Filename";
149 if (boost::ends_with(runFileName,
"_neutron_event.dat")) {
151 loadAlgName =
"LoadPreNexusMonitors";
152 boost::replace_first(runFileName,
"_neutron_event.dat",
"_runinfo.xml");
153 fileProp =
"RunInfoFilename";
158 loadmon->setProperty(fileProp, runFileName);
159 loadmon->setProperty(
"OutputWorkspace", monWsName);
160 loadmon->executeAsChildAlg();
161 Workspace_sptr monWSOutput = loadmon->getProperty(
"OutputWorkspace");
164 monWS = std::dynamic_pointer_cast<MatrixWorkspace>(monWSOutput);
165 if ((monWSOutput) && (!monWS))
169 "implemented in DGSReduction yet");
174 getei->setProperty(
"InputWorkspace", monWS);
175 getei->setProperty(
"Monitor1Spec", eiMon1Spec);
176 getei->setProperty(
"Monitor2Spec", eiMon2Spec);
177 getei->setProperty(
"EnergyEstimate", eiGuess);
178 getei->executeAsChildAlg();
179 incidentEnergy = getei->getProperty(
"IncidentEnergy");
180 tZero = getei->getProperty(
"Tzero");
185 alg->setProperty(
"InputWorkspace", inputWS);
186 alg->setProperty(
"OutputWorkspace", outputWS);
187 alg->setProperty(
"Offset", -tZero);
188 alg->executeAsChildAlg();
189 outputWS = alg->getProperty(
"OutputWorkspace");
193 addLog->setProperty(
"Workspace", outputWS);
194 addLog->setProperty(
"LogName",
"CalculatedT0");
195 addLog->setProperty(
"LogType",
"Number");
196 addLog->setProperty(
"LogText", boost::lexical_cast<std::string>(tZero));
197 addLog->executeAsChildAlg();
202 getei->setProperty(
"InputWorkspace", inputWS);
203 getei->setProperty(
"Monitor1Spec", eiMon1Spec);
204 getei->setProperty(
"Monitor2Spec", eiMon2Spec);
205 getei->setProperty(
"EnergyEstimate", eiGuess);
206 getei->executeAsChildAlg();
208 monPeak = getei->getProperty(
"FirstMonitorPeak");
209 const specnum_t monIndex =
static_cast<specnum_t>(getei->getProperty(
"FirstMonitorIndex"));
211 incidentEnergy = getei->getProperty(
"IncidentEnergy");
214 cbo->setProperty(
"InputWorkspace", inputWS);
215 cbo->setProperty(
"OutputWorkspace", outputWS);
216 cbo->setProperty(
"Offset", -monPeak);
217 cbo->executeAsChildAlg();
218 outputWS = cbo->getProperty(
"OutputWorkspace");
220 const auto &specInfo = inputWS->spectrumInfo();
221 const V3D &monPos = specInfo.position(monIndex);
222 std::string srcName = inputWS->getInstrument()->getSource()->getName();
225 moveInstComp->setProperty(
"Workspace", outputWS);
226 moveInstComp->setProperty(
"ComponentName", srcName);
227 moveInstComp->setProperty(
"X", monPos.
X());
228 moveInstComp->setProperty(
"Y", monPos.
Y());
229 moveInstComp->setProperty(
"Z", monPos.
Z());
230 moveInstComp->setProperty(
"RelativePosition",
false);
231 moveInstComp->executeAsChildAlg();
234 const double binOffset = -monPeak;
236 if (
"ISIS" == facility) {
237 std::string detcalFile;
238 if (reductionManager->existsProperty(
"SampleDetCalFilename"))
239 detcalFile = reductionManager->getPropertyValue(
"SampleDetCalFilename");
242 detcalFile = inputWS->run().getProperty(
"Filename")->value();
243 if (!detcalFile.empty()) {
244 const bool relocateDets = reductionManager->getProperty(
"RelocateDetectors");
246 loaddetinfo->setProperty(
"Workspace", outputWS);
247 loaddetinfo->setProperty(
"DataFilename", detcalFile);
248 loaddetinfo->setProperty(
"RelocateDets", relocateDets);
249 loaddetinfo->executeAsChildAlg();
250 outputWS = loaddetinfo->getProperty(
"Workspace");
252 throw std::runtime_error(
"Cannot find detcal filename in run object or as parameter.");
257 const bool doTibSub = reductionManager->getProperty(
"TimeIndepBackgroundSub");
263 double tibTofStart =
getDblPropOrParam(
"TibTofRangeStart", reductionManager,
"bkgd-range-min", inputWS);
264 tibTofStart += binOffset;
265 double tibTofEnd =
getDblPropOrParam(
"TibTofRangeEnd", reductionManager,
"bkgd-range-max", inputWS);
266 tibTofEnd += binOffset;
267 const double tibTofWidth = tibTofEnd - tibTofStart;
268 std::vector<double> params{tibTofStart, tibTofWidth, tibTofEnd};
270 bool treatTibAsEvents =
false;
273 std::vector<std::string> backgroundType =
274 inputWS->getInstrument()->getStringParameter(
"treat-background-as-events");
275 if (backgroundType.empty()) {
277 treatTibAsEvents =
false;
279 if (
"yes" == backgroundType[0] ||
"true" == backgroundType[0]) {
280 treatTibAsEvents =
true;
284 if (
"SNS" == facility) {
288 if (treatTibAsEvents) {
292 createBkg->setProperty(
"InputWorkspace", outputWS);
293 createBkg->setProperty(
"RangeStart", tibTofStart);
294 createBkg->setProperty(
"RangeEnd", tibTofEnd);
295 createBkg->executeAsChildAlg();
296 bkgWS = createBkg->getProperty(
"OutputWorkspace");
301 std::string origBkgWsName =
"background_origin_ws";
303 rebin->setProperty(
"InputWorkspace", outputWS);
304 rebin->setProperty(
"OutputWorkspace", origBkgWsName);
305 rebin->setProperty(
"Params", params);
306 rebin->setProperty(
"PreserveEvents",
false);
307 rebin->setProperty(
"IgnoreBinErrors",
true);
308 rebin->executeAsChildAlg();
313 cnvun->setProperty(
"InputWorkspace", outputWS);
314 cnvun->setProperty(
"OutputWorkspace", outputWS);
315 cnvun->setProperty(
"Target",
"DeltaE");
316 cnvun->setProperty(
"EMode",
"Direct");
317 cnvun->setProperty(
"EFixed", incidentEnergy);
318 cnvun->executeAsChildAlg();
319 outputWS = cnvun->getProperty(
"OutputWorkspace");
322 rebin->setProperty(
"InputWorkspace", outputWS);
323 rebin->setProperty(
"OutputWorkspace", outputWS);
324 rebin->setProperty(
"Params", etBinning);
325 rebin->setProperty(
"PreserveEvents",
false);
326 rebin->executeAsChildAlg();
327 outputWS = rebin->getProperty(
"OutputWorkspace");
330 cnvun->setProperty(
"InputWorkspace", outputWS);
331 cnvun->setProperty(
"OutputWorkspace", outputWS);
332 cnvun->setProperty(
"Target",
"TOF");
333 cnvun->setProperty(
"EMode",
"Direct");
334 cnvun->setProperty(
"EFixed", incidentEnergy);
335 cnvun->executeAsChildAlg();
336 outputWS = cnvun->getProperty(
"OutputWorkspace");
339 cnvToDist->setProperty(
"Workspace", outputWS);
340 cnvToDist->executeAsChildAlg();
341 outputWS = cnvToDist->getProperty(
"Workspace");
345 flatBg->setProperty(
"InputWorkspace", origBkgWS);
346 flatBg->setProperty(
"StartX", tibTofStart);
347 flatBg->setProperty(
"EndX", tibTofEnd);
348 flatBg->setProperty(
"Mode",
"Mean");
349 flatBg->setProperty(
"OutputMode",
"Return Background");
350 flatBg->executeAsChildAlg();
351 bkgWS = flatBg->getProperty(
"OutputWorkspace");
357 cnvToDist->setProperty(
"Workspace", bkgWS);
358 cnvToDist->executeAsChildAlg();
359 bkgWS = cnvToDist->getProperty(
"Workspace");
364 minus->setProperty(
"LHSWorkspace", outputWS);
365 minus->setProperty(
"RHSWorkspace", bkgWS);
366 minus->setProperty(
"OutputWorkspace", outputWS);
367 minus->executeAsChildAlg();
374 cnvToDist->setProperty(
"Workspace", outputWS);
375 cnvToDist->executeAsChildAlg();
376 outputWS = cnvToDist->getProperty(
"Workspace");
379 flatBg->setProperty(
"InputWorkspace", outputWS);
380 flatBg->setProperty(
"OutputWorkspace", outputWS);
381 flatBg->setProperty(
"StartX", tibTofStart);
382 flatBg->setProperty(
"EndX", tibTofEnd);
383 flatBg->setProperty(
"Mode",
"Mean");
384 flatBg->executeAsChildAlg();
385 outputWS = flatBg->getProperty(
"OutputWorkspace");
388 if (!treatTibAsEvents) {
391 cnvFrDist->setProperty(
"Workspace", outputWS);
392 cnvFrDist->executeAsChildAlg();
393 outputWS = cnvFrDist->getProperty(
"Workspace");
399 norm->setProperty(
"InputWorkspace", outputWS);
400 norm->setProperty(
"OutputWorkspace", outputWS);
401 norm->setProperty(
"InputMonitorWorkspace", monWS);
402 norm->setProperty(
"TofRangeOffset", binOffset);
403 norm->executeAsChildAlg();
404 outputWS = norm->getProperty(
"OutputWorkspace");
407 g_log.
notice() <<
"Converting to energy transfer.\n";
409 cnvun->setProperty(
"InputWorkspace", outputWS);
410 cnvun->setProperty(
"OutputWorkspace", outputWS);
411 cnvun->setProperty(
"Target",
"DeltaE");
412 cnvun->setProperty(
"EMode",
"Direct");
413 cnvun->setProperty(
"EFixed", incidentEnergy);
414 cnvun->executeAsChildAlg();
415 outputWS = cnvun->getProperty(
"OutputWorkspace");
419 rebin->setProperty(
"InputWorkspace", outputWS);
420 rebin->setProperty(
"OutputWorkspace", outputWS);
421 rebin->setProperty(
"Params", etBinning);
422 rebin->setProperty(
"IgnoreBinErrors",
true);
423 rebin->setProperty(
"PreserveEvents", preserveEvents);
424 rebin->executeAsChildAlg();
425 outputWS = rebin->getProperty(
"OutputWorkspace");
428 if (
"SNS" == facility) {
430 cnvun->setProperty(
"InputWorkspace", outputWS);
431 cnvun->setProperty(
"OutputWorkspace", outputWS);
432 cnvun->setProperty(
"Target",
"Wavelength");
433 cnvun->executeAsChildAlg();
434 outputWS = cnvun->getProperty(
"OutputWorkspace");
438 alg2->setProperty(
"InputWorkspace", outputWS);
439 alg2->setProperty(
"OutputWorkspace", outputWS);
440 alg2->executeAsChildAlg();
441 outputWS = alg2->getProperty(
"OutputWorkspace");
444 cnvun->setProperty(
"InputWorkspace", outputWS);
445 cnvun->setProperty(
"OutputWorkspace", outputWS);
446 cnvun->setProperty(
"Target",
"DeltaE");
447 cnvun->executeAsChildAlg();
448 outputWS = cnvun->getProperty(
"OutputWorkspace");
453 alg->setProperty(
"InputWorkspace", outputWS);
454 alg->setProperty(
"OutputWorkspace", outputWS);
455 alg->executeAsChildAlg();
456 outputWS = alg->getProperty(
"OutputWorkspace");
459 const bool correctKiKf = reductionManager->getProperty(
"CorrectKiKf");
463 kikf->setProperty(
"InputWorkspace", outputWS);
464 kikf->setProperty(
"OutputWorkspace", outputWS);
465 kikf->setProperty(
"EMode",
"Direct");
466 kikf->executeAsChildAlg();
467 outputWS = kikf->getProperty(
"OutputWorkspace");
471 const bool sofphieIsDistribution = reductionManager->getProperty(
"SofPhiEIsDistribution");
474 rebin->setProperty(
"InputWorkspace", outputWS);
475 rebin->setProperty(
"OutputWorkspace", outputWS);
476 rebin->setProperty(
"IgnoreBinErrors",
true);
477 if (sofphieIsDistribution)
478 rebin->setProperty(
"PreserveEvents",
false);
479 rebin->executeAsChildAlg();
480 outputWS = rebin->getProperty(
"OutputWorkspace");
482 if (sofphieIsDistribution) {
485 distrib->setProperty(
"Workspace", outputWS);
486 distrib->executeAsChildAlg();
487 outputWS = distrib->getProperty(
"Workspace");
491 crop->setProperty(
"InputWorkspace", outputWS);
492 crop->setProperty(
"OutputWorkspace", outputWS);
493 crop->setProperty(
"XMin", etBinning[0]);
494 crop->setProperty(
"XMax", etBinning[2]);
495 crop->executeAsChildAlg();
496 outputWS = crop->getProperty(
"OutputWorkspace");
503 divide->setProperty(
"LHSWorkspace", outputWS);
504 divide->setProperty(
"RHSWorkspace", detVanWS);
505 divide->setProperty(
"OutputWorkspace", outputWS);
506 divide->executeAsChildAlg();
507 outputWS = divide->getProperty(
"OutputWorkspace");
513 std::string oldGroupFile;
514 std::string filePropMod = this->
getProperty(
"AlternateGroupingTag");
515 std::string fileProp = filePropMod +
"OldGroupingFilename";
516 if (reductionManager->existsProperty(fileProp))
517 oldGroupFile = reductionManager->getPropertyValue(fileProp);
519 remap->setProperty(
"InputWorkspace", outputWS);
520 remap->setProperty(
"OutputWorkspace", outputWS);
521 remap->setProperty(
"MaskWorkspace", maskWS);
522 remap->setProperty(
"GroupingWorkspace", groupWS);
523 remap->setProperty(
"OldGroupingFile", oldGroupFile);
524 remap->executeAsChildAlg();
525 outputWS = remap->getProperty(
"OutputWorkspace");
527 if (
"ISIS" == facility) {
528 double scaleFactor = inputWS->getInstrument()->getNumberParameter(
"scale-factor")[0];
530 scaleAlg->setProperty(
"InputWorkspace", outputWS);
531 scaleAlg->setProperty(
"Factor", scaleFactor);
532 scaleAlg->setProperty(
"Operation",
"Multiply");
533 scaleAlg->setProperty(
"OutputWorkspace", outputWS);
534 scaleAlg->executeAsChildAlg();
#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.
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.
Marks code as not implemented yet.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
A specialised Property class for holding a series of time-value pairs.
TimeSeriesPropertyStatistics getStatistics() const
Return a TimeSeriesPropertyStatistics object.
constexpr double X() const noexcept
Get x.
constexpr double Y() const noexcept
Get y.
constexpr double Z() const noexcept
Get z.
DgsConvertToEnergyTransfer : This is the algorithm responsible for the conversion from TOF to energy ...
const std::string category() const override
Algorithm's category for identification.
int version() const override
Algorithm's version for identification.
void exec() override
Execute the algorithm.
void init() override
Initialize the algorithm's properties.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
int32_t specnum_t
Typedef for a spectrum Number.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
double getDblPropOrParam(const std::string &pmProp, Mantid::Kernel::PropertyManager_sptr &pm, const std::string &instParam, Mantid::API::MatrixWorkspace_sptr &ws, const double overrideValue=Mantid::EMPTY_DBL())
Function to get double property or instrument parameter value.
@ Input
An input workspace.
@ Output
An output workspace.