Newer
Older
Ronald Fowler
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/LoadMuonNexus.h"
Ronald Fowler
committed
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/ArrayProperty.h"
Gigg, Martyn Anthony
committed
#include "MantidAPI/FileProperty.h"
Roman Tolchenov
committed
#include "MantidAPI/Progress.h"
#include "MantidAPI/TableRow.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
Ronald Fowler
committed
Ronald Fowler
committed
#include <cmath>
#include <boost/shared_ptr.hpp>
#include "MantidNexus/MuonNexusReader.h"
#include "MantidNexus/NexusClasses.h"
#include <nexus/NeXusFile.hpp>
#include <nexus/NeXusException.hpp>
Ronald Fowler
committed
namespace Mantid
{
namespace DataHandling
Ronald Fowler
committed
{
Ronald Fowler
committed
using namespace Kernel;
using namespace API;
Russell Taylor
committed
using Geometry::Instrument;
using namespace Mantid::NeXus;
Ronald Fowler
committed
/// Empty default constructor
LoadMuonNexus::LoadMuonNexus() :
m_filename(), m_entrynumber(0), m_numberOfSpectra(0), m_numberOfPeriods(0), m_list(false),
m_interval(false), m_spec_list(), m_spec_min(0), m_spec_max(EMPTY_INT())
Ronald Fowler
committed
{}
/// Initialisation method.
void LoadMuonNexus::init()
{
Peterson, Peter
committed
declareProperty(new FileProperty("Filename", "", FileProperty::Load, ".nxs"),
Roman Tolchenov
committed
"The name of the Nexus file to load" );
Russell Taylor
committed
Roman Tolchenov
committed
declareProperty(new WorkspaceProperty<Workspace>("OutputWorkspace","",Direction::Output),
Steve Williams
committed
"The name of the workspace to be created as the output of the\n"
"algorithm. For multiperiod files, one workspace will be\n"
"generated for each period");
Sofia Antony
committed
Roman Tolchenov
committed
auto mustBePositive = boost::make_shared<BoundedValidator<int64_t> >();
Ronald Fowler
committed
mustBePositive->setLower(0);
Gigg, Martyn Anthony
committed
declareProperty( "SpectrumMin",(int64_t)0, mustBePositive,
Steve Williams
committed
"Index number of the first spectrum to read, only used if\n"
"spectrum_max is set and only for single period data\n"
"(default 0)" );
declareProperty( "SpectrumMax",(int64_t)EMPTY_INT(), mustBePositive,
Steve Williams
committed
"Index of last spectrum to read, only for single period data\n"
"(default the last spectrum)");
Roman Tolchenov
committed
Gigg, Martyn Anthony
committed
declareProperty(new ArrayProperty<specid_t>("SpectrumList"),
Steve Williams
committed
"Array, or comma separated list, of indexes of spectra to\n"
"load");
Steve Williams
committed
"Determines whether the spectra are automatically grouped\n"
"together based on the groupings in the NeXus file, only\n"
"for single period data (default no)");
Sofia Antony
committed
declareProperty("EntryNumber", (int64_t)0, mustBePositive,
Roman Tolchenov
committed
"The particular entry number to read (default: Load all workspaces and creates a workspace group)");
Anders Markvardsen
committed
std::vector<std::string> FieldOptions;
FieldOptions.push_back("Transverse");
FieldOptions.push_back("Longitudinal");
declareProperty("MainFieldDirection","Transverse", boost::make_shared<StringListValidator>(FieldOptions),
Anders Markvardsen
committed
"Output the main field direction if specified in Nexus file (default Transverse)", Direction::Output);
declareProperty("TimeZero", 0.0, "Time zero in units of micro-seconds (default to 0.0)", Direction::Output);
declareProperty("FirstGoodData", 0.0, "First good data in units of micro-seconds (default to 0.0)", Direction::Output);
declareProperty(new WorkspaceProperty<Workspace>("DeadTimeTable", "", Direction::Output, PropertyMode::Optional),
"Table or a group of tables containing detector dead times");
declareProperty(new WorkspaceProperty<Workspace>("DetectorGroupingTable", "", Direction::Output, PropertyMode::Optional),
"Table or a group of tables with information about the detector grouping stored in the file (if any)");
Ronald Fowler
committed
}
Ronald Fowler
committed
/// Validates the optional 'spectra to read' properties, if they have been set
void LoadMuonNexus::checkOptionalProperties()
{
Steve Williams
committed
//read in the settings passed to the algorithm
m_spec_list = getProperty("SpectrumList");
m_spec_max = getProperty("SpectrumMax");
Steve Williams
committed
//Are we using a list of spectra or all the spectra in a range?
m_list = !m_spec_list.empty();
m_interval = (m_spec_max != EMPTY_INT());
if ( m_spec_max == EMPTY_INT() ) m_spec_max = 0;
Ronald Fowler
committed
// Check validity of spectra list property, if set
if ( m_list )
{
const specid_t minlist = *min_element(m_spec_list.begin(),m_spec_list.end());
const specid_t maxlist = *max_element(m_spec_list.begin(),m_spec_list.end());
Ronald Fowler
committed
if ( maxlist > m_numberOfSpectra || minlist == 0)
{
g_log.error("Invalid list of spectra");
throw std::invalid_argument("Inconsistent properties defined");
}
}
Roman Tolchenov
committed
Ronald Fowler
committed
// Check validity of spectra range, if set
if ( m_interval )
{
m_spec_min = getProperty("SpectrumMin");
Ronald Fowler
committed
if ( m_spec_max < m_spec_min || m_spec_max > m_numberOfSpectra )
{
g_log.error("Invalid Spectrum min/max properties");
throw std::invalid_argument("Inconsistent properties defined");
}
}
Ronald Fowler
committed
}
Roman Tolchenov
committed
/// Run the Child Algorithm LoadInstrument
Ronald Fowler
committed
void LoadMuonNexus::runLoadInstrument(DataObjects::Workspace2D_sptr localWorkspace)
{
Roman Tolchenov
committed
IAlgorithm_sptr loadInst = createChildAlgorithm("LoadInstrument");
Roman Tolchenov
committed
// Now execute the Child Algorithm. Catch and log any error, but don't stop.
Ronald Fowler
committed
try
{
loadInst->setPropertyValue("InstrumentName", m_instrument_name);
Roman Tolchenov
committed
loadInst->setProperty<MatrixWorkspace_sptr> ("Workspace", localWorkspace);
Gigg, Martyn Anthony
committed
loadInst->setProperty("RewriteSpectraMap", false);
Roman Tolchenov
committed
loadInst->execute();
}
catch( std::invalid_argument&)
{
g_log.information("Invalid argument to LoadInstrument Child Algorithm");
Ronald Fowler
committed
}
Ronald Fowler
committed
{
g_log.information("Unable to successfully run LoadInstrument Child Algorithm");
Ronald Fowler
committed
}
// If loading instrument definition file fails,
// we may get instrument by some other means yet to be decided upon
// at present we do nothing.
//if ( ! loadInst->isExecuted() )
//{
//}
Ronald Fowler
committed
}
Roman Tolchenov
committed
/**
* Return the confidence with with this algorithm can load the file
* @param descriptor A descriptor for the file
* @returns An integer specifying the confidence level. 0 indicates it will not be used
*/
int LoadMuonNexus::confidence(Kernel::NexusDescriptor &descriptor) const
Sofia Antony
committed
{
return 0; // Not to be used but LoadMuonNexus2, which inherits from this will
Sofia Antony
committed
}
Ronald Fowler
committed
} // namespace DataHandling
} // namespace Mantid