Newer
Older
Doucet, Mathieu
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/EQSANSTofStructure.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidDataObjects/Events.h"
#include "MantidDataObjects/EventList.h"
#include "MantidDataObjects/EventWorkspace.h"
Russell Taylor
committed
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/TimeSeriesProperty.h"
Doucet, Mathieu
committed
Doucet, Mathieu
committed
using namespace Mantid::Kernel;
using namespace Mantid::DataObjects;
Doucet, Mathieu
committed
using namespace Mantid::Geometry;
Doucet, Mathieu
committed
namespace Mantid {
namespace Algorithms {
Doucet, Mathieu
committed
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(EQSANSTofStructure)
using namespace Kernel;
using namespace API;
using namespace Geometry;
Federico Montesino Pouzols
committed
EQSANSTofStructure::EQSANSTofStructure()
: API::Algorithm(), frame_tof0(0.), flight_path_correction(false),
low_tof_cut(0.), high_tof_cut(0.) {}
declareProperty(make_unique<WorkspaceProperty<EventWorkspace>>(
"InputWorkspace", "", Direction::Input,
boost::make_shared<WorkspaceUnitValidator>("TOF")),
"Workspace to apply the TOF correction to");
declareProperty("FlightPathCorrection", false,
"If True, the neutron flight path correction will be applied",
Kernel::Direction::Input);
declareProperty("LowTOFCut", 0.0, "Width of the TOF margin to cut on the "
"lower end of the TOF distribution of each "
"frame",
Kernel::Direction::Input);
declareProperty("HighTOFCut", 0.0, "Width of the TOF margin to cut on the "
"upper end of the TOF distribution of "
"each frame",
Kernel::Direction::Input);
// Output parameters
declareProperty("FrameSkipping", false,
"If True, the data was taken in frame skipping mode",
Kernel::Direction::Output);
declareProperty("TofOffset", 0.0, "TOF offset that was applied to the data",
Kernel::Direction::Output);
declareProperty(
"WavelengthMin", 0.0,
"Lower bound of the wavelength distribution of the first frame",
Kernel::Direction::Output);
declareProperty(
"WavelengthMax", 0.0,
"Upper bound of the wavelength distribution of the first frame",
Kernel::Direction::Output);
declareProperty(
"WavelengthMinFrame2", 0.0,
"Lower bound of the wavelength distribution of the second frame",
Kernel::Direction::Output);
declareProperty(
"WavelengthMaxFrame2", 0.0,
"Upper bound of the wavelength distribution of the second frame",
Kernel::Direction::Output);
Doucet, Mathieu
committed
}
Russell Taylor
committed
EventWorkspace_sptr inputWS = getProperty("InputWorkspace");
Doucet, Mathieu
committed
flight_path_correction = getProperty("FlightPathCorrection");
low_tof_cut = getProperty("LowTOFCut");
high_tof_cut = getProperty("HighTOFCut");
Doucet, Mathieu
committed
// Calculate the frame width
auto frequencyLog = dynamic_cast<TimeSeriesProperty<double> *>(
inputWS->run().getLogData("frequency"));
if (!frequencyLog) {
throw std::runtime_error("Frequency log not found.");
}
double frequency = frequencyLog->getStatistics().mean;
double tof_frame_width = 1.0e6 / frequency;
// Determine whether we need frame skipping or not by checking the chopper
// speed
bool frame_skipping = false;
auto chopper_speedLog = dynamic_cast<TimeSeriesProperty<double> *>(
inputWS->run().getLogData("Speed1"));
if (!chopper_speedLog) {
throw std::runtime_error("Chopper speed log not found.");
}
const double chopper_speed = chopper_speedLog->getStatistics().mean;
if (std::fabs(chopper_speed - frequency / 2.0) < 1.0)
frame_skipping = true;
Doucet, Mathieu
committed
// Get TOF offset
Doucet, Mathieu
committed
frame_tof0 = getTofOffset(inputWS, frame_skipping);
Doucet, Mathieu
committed
// Calculate the frame width
double tmp_frame_width =
frame_skipping ? tof_frame_width * 2.0 : tof_frame_width;
double frame_offset = 0.0;
if (frame_tof0 >= tmp_frame_width)
frame_offset =
tmp_frame_width * (static_cast<int>(frame_tof0 / tmp_frame_width));
this->execEvent(inputWS, frame_tof0, frame_offset, tof_frame_width,
tmp_frame_width, frame_skipping);
void EQSANSTofStructure::execEvent(
Mantid::DataObjects::EventWorkspace_sptr inputWS, double threshold,
double frame_offset, double tof_frame_width, double tmp_frame_width,
bool frame_skipping) {
const size_t numHists = inputWS->getNumberHistograms();
Progress progress(this, 0.0, 1.0, numHists);
// Get the nominal sample-to-detector distance (in mm)
Mantid::Kernel::Property *prop =
inputWS->run().getProperty("sample_detector_distance");
auto dp = dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop);
if (!dp) {
throw std::runtime_error("sample_detector_distance log not found.");
}
// Loop through the spectra and apply correction
PARALLEL_FOR1(inputWS)
for (int64_t ispec = 0; ispec < int64_t(numHists); ++ispec) {
Doucet, Mathieu
committed
IDetector_const_sptr det;
try {
det = inputWS->getDetector(ispec);
} catch (Exception::NotFoundError &) {
g_log.warning() << "Workspace index " << ispec
<< " has no detector assigned to it - discarding\n";
Russell Taylor
committed
// 'continue' statement moved outside catch block because Mac Intel
// compiler has a problem with it being here in an openmp block.
Doucet, Mathieu
committed
}
Doucet, Mathieu
committed
// Get the flight path from the sample to the detector pixel
const V3D samplePos = inputWS->getInstrument()->getSample()->getPos();
const V3D scattered_flight_path = det->getPos() - samplePos;
// Sample-to-source distance
const V3D sourcePos = inputWS->getInstrument()->getSource()->getPos();
const V3D SSD = samplePos - sourcePos;
double tof_factor =
(SSD.norm() + scattered_flight_path.norm()) / (SSD.norm() + SDD);
Doucet, Mathieu
committed
PARALLEL_START_INTERUPT_REGION
// Get the pointer to the output event list
std::vector<TofEvent> &events = inputWS->getSpectrum(ispec).getEvents();
std::vector<TofEvent>::iterator it;
Doucet, Mathieu
committed
std::vector<TofEvent> clean_events;
for (it = events.begin(); it < events.end(); ++it) {
double newtof = it->tof();
newtof += frame_offset;
Doucet, Mathieu
committed
// Correct for the scattered neutron flight path
if (flight_path_correction)
newtof /= tof_factor;
Doucet, Mathieu
committed
while (newtof < threshold)
newtof += tmp_frame_width;
Doucet, Mathieu
committed
Doucet, Mathieu
committed
// Remove events that don't fall within the accepted time window
double rel_tof = newtof - frame_tof0;
double x = (static_cast<int>(floor(rel_tof * 10)) %
static_cast<int>(floor(tof_frame_width * 10))) *
0.1;
if (x < low_tof_cut || x > tof_frame_width - high_tof_cut) {
Doucet, Mathieu
committed
continue;
}
Doucet, Mathieu
committed
// At this point the events in the second frame are still off by a frame
if (frame_skipping && rel_tof > tof_frame_width)
newtof += tof_frame_width;
clean_events.emplace_back(newtof, it->pulseTime());
Doucet, Mathieu
committed
events.clear();
events.reserve(clean_events.size());
for (it = clean_events.begin(); it < clean_events.end(); ++it) {
Doucet, Mathieu
committed
events.push_back(*it);
}
progress.report("TOF structure");
Doucet, Mathieu
committed
PARALLEL_END_INTERUPT_REGION
Doucet, Mathieu
committed
}
Doucet, Mathieu
committed
PARALLEL_CHECK_INTERUPT_REGION
Doucet, Mathieu
committed
}
double EQSANSTofStructure::getTofOffset(EventWorkspace_const_sptr inputWS,
bool frame_skipping) {
//# Storage for chopper information read from the logs
double chopper_set_phase[4] = {0, 0, 0, 0};
double chopper_speed[4] = {0, 0, 0, 0};
double chopper_actual_phase[4] = {0, 0, 0, 0};
double chopper_wl_1[4] = {0, 0, 0, 0};
double chopper_wl_2[4] = {0, 0, 0, 0};
double frame_wl_1 = 0;
double frame_srcpulse_wl_1 = 0;
double frame_wl_2 = 0;
double chopper_srcpulse_wl_1[4] = {0, 0, 0, 0};
double chopper_frameskip_wl_1[4] = {0, 0, 0, 0};
double chopper_frameskip_wl_2[4] = {0, 0, 0, 0};
double chopper_frameskip_srcpulse_wl_1[4] = {0, 0, 0, 0};
Doucet, Mathieu
committed
// Calculate the frame width
auto frequencyLog = dynamic_cast<TimeSeriesProperty<double> *>(
inputWS->run().getLogData("frequency"));
if (!frequencyLog) {
throw std::runtime_error("Frequency log not found.");
}
double frequency = frequencyLog->getStatistics().mean;
double tof_frame_width = 1.0e6 / frequency;
double tmp_frame_width = tof_frame_width;
if (frame_skipping)
tmp_frame_width *= 2.0;
// Choice of parameter set
int m_set = 0;
if (frame_skipping)
m_set = 1;
bool first = true;
bool first_skip = true;
double frameskip_wl_1 = 0;
double frameskip_srcpulse_wl_1 = 0;
double frameskip_wl_2 = 0;
// Read chopper information
std::ostringstream phase_str;
auto log = dynamic_cast<TimeSeriesProperty<double> *>(
inputWS->run().getLogData(phase_str.str()));
if (!log) {
throw std::runtime_error("Phase log not found.");
}
chopper_set_phase[i] = log->getStatistics().mean;
std::ostringstream speed_str;
log = dynamic_cast<TimeSeriesProperty<double> *>(
inputWS->run().getLogData(speed_str.str()));
if (!log) {
throw std::runtime_error("Speed log not found.");
}
chopper_speed[i] = log->getStatistics().mean;
// Only process choppers with non-zero speed
if (chopper_speed[i] <= 0)
continue;
chopper_actual_phase[i] =
chopper_set_phase[i] - CHOPPER_PHASE_OFFSET[m_set][i];
while (chopper_actual_phase[i] < 0)
chopper_actual_phase[i] += tmp_frame_width;
double x1 =
(chopper_actual_phase[i] -
(tmp_frame_width * 0.5 * CHOPPER_ANGLE[i] / 360.)); // opening edge
double x2 =
(chopper_actual_phase[i] +
(tmp_frame_width * 0.5 * CHOPPER_ANGLE[i] / 360.)); // closing edge
if (!frame_skipping) // not skipping
while (x1 < 0) {
x1 += tmp_frame_width;
x2 += tmp_frame_width;
}
}
if (x1 > 0) {
chopper_wl_1[i] = 3.9560346 * x1 / CHOPPER_LOCATION[i];
chopper_srcpulse_wl_1[i] =
3.9560346 * (x1 - chopper_wl_1[i] * PULSEWIDTH) / CHOPPER_LOCATION[i];
} else
chopper_wl_1[i] = chopper_srcpulse_wl_1[i] = 0.;
if (x2 > 0)
chopper_wl_2[i] = 3.9560346 * x2 / CHOPPER_LOCATION[i];
chopper_wl_2[i] = 0.;
if (first) {
frame_wl_1 = chopper_wl_1[i];
frame_srcpulse_wl_1 = chopper_srcpulse_wl_1[i];
frame_wl_2 = chopper_wl_2[i];
first = false;
} else {
if (frame_skipping &&
i == 2) // ignore chopper 1 and 2 forthe shortest wl.
{
frame_wl_1 = chopper_wl_1[i];
frame_srcpulse_wl_1 = chopper_srcpulse_wl_1[i];
}
if (frame_wl_1 < chopper_wl_1[i])
frame_wl_1 = chopper_wl_1[i];
if (frame_wl_2 > chopper_wl_2[i])
frame_wl_2 = chopper_wl_2[i];
if (frame_srcpulse_wl_1 < chopper_srcpulse_wl_1[i])
frame_srcpulse_wl_1 = chopper_srcpulse_wl_1[i];
if (frame_skipping) {
if (x1 > 0) {
x1 += tof_frame_width; // skipped pulse
chopper_frameskip_wl_1[i] = 3.9560346 * x1 / CHOPPER_LOCATION[i];
chopper_frameskip_srcpulse_wl_1[i] =
3.9560346 * (x1 - chopper_wl_1[i] * PULSEWIDTH) /
CHOPPER_LOCATION[i];
} else
chopper_wl_1[i] = chopper_srcpulse_wl_1[i] = 0.;
if (x2 > 0) {
x2 += tof_frame_width;
chopper_frameskip_wl_2[i] = 3.9560346 * x2 / CHOPPER_LOCATION[i];
} else
chopper_wl_2[i] = 0.;
if (i < 2 && chopper_frameskip_wl_1[i] > chopper_frameskip_wl_2[i])
continue;
if (first_skip) {
frameskip_wl_1 = chopper_frameskip_wl_1[i];
frameskip_srcpulse_wl_1 = chopper_frameskip_srcpulse_wl_1[i];
frameskip_wl_2 = chopper_frameskip_wl_2[i];
first_skip = false;
} else {
if (i == 2) // ignore chopper 1 and 2 forthe longest wl.
frameskip_wl_2 = chopper_frameskip_wl_2[i];
if (chopper_frameskip_wl_1[i] < chopper_frameskip_wl_2[i] &&
frameskip_wl_1 < chopper_frameskip_wl_1[i])
frameskip_wl_1 = chopper_frameskip_wl_1[i];
if (chopper_frameskip_wl_1[i] < chopper_frameskip_wl_2[i] &&
frameskip_srcpulse_wl_1 < chopper_frameskip_srcpulse_wl_1[i])
frameskip_srcpulse_wl_1 = chopper_frameskip_srcpulse_wl_1[i];
if (frameskip_wl_2 > chopper_frameskip_wl_2[i])
frameskip_wl_2 = chopper_frameskip_wl_2[i];
}
}
}
if (frame_wl_1 >= frame_wl_2) // too many frames later. So figure it out
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
double n_frame[4] = {0, 0, 0, 0};
double c_wl_1[4] = {0, 0, 0, 0};
double c_wl_2[4] = {0, 0, 0, 0};
bool passed = false;
do {
frame_wl_1 = c_wl_1[0] =
chopper_wl_1[0] +
3.9560346 * n_frame[0] * tof_frame_width / CHOPPER_LOCATION[0];
frame_wl_2 = c_wl_2[0] =
chopper_wl_2[0] +
3.9560346 * n_frame[0] * tof_frame_width / CHOPPER_LOCATION[0];
for (int i = 1; i < 4; i++) {
n_frame[i] = n_frame[i - 1] - 1;
passed = false;
do {
n_frame[i] += 1;
c_wl_1[i] =
chopper_wl_1[i] +
3.9560346 * n_frame[i] * tof_frame_width / CHOPPER_LOCATION[i];
c_wl_2[i] =
chopper_wl_2[i] +
3.9560346 * n_frame[i] * tof_frame_width / CHOPPER_LOCATION[i];
if (frame_wl_1 < c_wl_2[i] && frame_wl_2 > c_wl_1[i]) {
passed = true;
break;
}
if (frame_wl_2 < c_wl_1[i])
break; // over shot
} while (n_frame[i] - n_frame[i - 1] < 10);
if (!passed) {
n_frame[0] += 1;
break;
} else {
if (frame_wl_1 < c_wl_1[i])
frame_wl_1 = c_wl_1[i];
if (frame_wl_2 > c_wl_2[i])
frame_wl_2 = c_wl_2[i];
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
}
} while (!passed && n_frame[0] < 99);
if (frame_wl_2 > frame_wl_1) {
int n = 3;
if (c_wl_1[2] > c_wl_1[3])
n = 2;
frame_srcpulse_wl_1 =
c_wl_1[n] - 3.9560346 * c_wl_1[n] * PULSEWIDTH / CHOPPER_LOCATION[n];
for (int i = 0; i < 4; i++) {
chopper_wl_1[i] = c_wl_1[i];
chopper_wl_2[i] = c_wl_2[i];
if (frame_skipping) {
chopper_frameskip_wl_1[i] =
c_wl_1[i] +
3.9560346 * 2. * tof_frame_width / CHOPPER_LOCATION[i];
chopper_frameskip_wl_2[i] =
c_wl_2[i] +
3.9560346 * 2. * tof_frame_width / CHOPPER_LOCATION[i];
if (i == 0) {
frameskip_wl_1 = chopper_frameskip_wl_1[i];
frameskip_wl_2 = chopper_frameskip_wl_2[i];
} else {
if (frameskip_wl_1 < chopper_frameskip_wl_1[i])
frameskip_wl_1 = chopper_frameskip_wl_1[i];
if (frameskip_wl_2 > chopper_frameskip_wl_2[i])
frameskip_wl_2 = chopper_frameskip_wl_2[i];
}
}
} else
frame_srcpulse_wl_1 = 0.0;
}
// Get source and detector locations
Doucet, Mathieu
committed
// get the name of the mapping file as set in the parameter files
std::vector<std::string> temp =
inputWS->getInstrument()->getStringParameter("detector-name");
Doucet, Mathieu
committed
std::string det_name = "detector1";
if (temp.empty())
g_log.information() << "The instrument parameter file does not contain the "
"'detector-name' parameter: trying 'detector1'";
Doucet, Mathieu
committed
else
det_name = temp[0];
double source_z = inputWS->getInstrument()->getSource()->getPos().Z();
double detector_z =
inputWS->getInstrument()->getComponentByName(det_name)->getPos().Z();
double source_to_detector = (detector_z - source_z) * 1000.0;
Doucet, Mathieu
committed
frame_tof0 = frame_srcpulse_wl_1 / 3.9560346 * source_to_detector;
g_log.information() << "Frame width " << tmp_frame_width << '\n';
g_log.information() << "TOF offset = " << frame_tof0 << " microseconds\n";
g_log.information() << "Band defined by T1-T4 " << frame_wl_1 << " "
<< frame_wl_2;
if (frame_skipping)
g_log.information() << " + " << frameskip_wl_1 << " " << frameskip_wl_2
g_log.information() << "Chopper Actual Phase Lambda1 Lambda2\n";
for (int i = 0; i < 4; i++)
g_log.information() << i << " " << chopper_actual_phase[i] << " "
<< chopper_wl_1[i] << " " << chopper_wl_2[i] << '\n';
double low_wl_discard = 3.9560346 * low_tof_cut / source_to_detector;
double high_wl_discard = 3.9560346 * high_tof_cut / source_to_detector;
setProperty("FrameSkipping", frame_skipping);
setProperty("TofOffset", frame_tof0);
setProperty("WavelengthMin", frame_wl_1 + low_wl_discard);
setProperty("WavelengthMax", frame_wl_2 - high_wl_discard);
if (frame_skipping) {
setProperty("WavelengthMinFrame2", frameskip_wl_1 + low_wl_discard);
setProperty("WavelengthMaxFrame2", frameskip_wl_2 - high_wl_discard);
return frame_tof0;
}
Doucet, Mathieu
committed
} // namespace Algorithms
} // namespace Mantid