Skip to content
Snippets Groups Projects
MaskDetectorsInShape.cpp 2.95 KiB
Newer Older
Nick Draper's avatar
Nick Draper committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/MaskDetectorsInShape.h"
Nick Draper's avatar
Nick Draper committed
#include "MantidKernel/ArrayProperty.h"

namespace Mantid
{
namespace DataHandling
{
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM( MaskDetectorsInShape)
Nick Draper's avatar
Nick Draper committed

using namespace Kernel;
using namespace API;
Nick Draper's avatar
Nick Draper committed

/// (Empty) Constructor
MaskDetectorsInShape::MaskDetectorsInShape()
{
}
Nick Draper's avatar
Nick Draper committed

/// Destructor
MaskDetectorsInShape::~MaskDetectorsInShape()
{
}
Nick Draper's avatar
Nick Draper committed

void MaskDetectorsInShape::init()
{
  declareProperty(new WorkspaceProperty<> ("Workspace", "", Direction::InOut),
      "Name of the input workspace");
  declareProperty("ShapeXML", "", new MandatoryValidator<std::string> (),
      "XML definition of the user defined shape");
  declareProperty("IncludeMonitors", false,
      "Whether to include monitors if they are contained in the shape (default false)");
}
Nick Draper's avatar
Nick Draper committed

void MaskDetectorsInShape::exec()
{
  // Get the input workspace
  MatrixWorkspace_sptr WS = getProperty("Workspace");
Nick Draper's avatar
Nick Draper committed

  const bool includeMonitors = getProperty("IncludeMonitors");
  const std::string shapeXML = getProperty("ShapeXML");
Nick Draper's avatar
Nick Draper committed

  std::vector<int> foundDets = runFindDetectorsInShape(WS, shapeXML, includeMonitors);
  if (foundDets.empty())
  {
    g_log.information("No detectors were found in the shape, nothing was masked");
    return;
  }
  runMaskDetectors(WS, foundDets);
  setProperty("Workspace", WS);
}
Nick Draper's avatar
Nick Draper committed

/// Run the FindDetectorsInShape sub-algorithm
std::vector<int> MaskDetectorsInShape::runFindDetectorsInShape(API::MatrixWorkspace_sptr workspace,
    const std::string shapeXML, const bool includeMonitors)
{
  IAlgorithm_sptr alg = createSubAlgorithm("FindDetectorsInShape");
  alg->setPropertyValue("IncludeMonitors", includeMonitors ? "1" : "0");
  alg->setPropertyValue("ShapeXML", shapeXML);
  alg->setProperty<MatrixWorkspace_sptr> ("Workspace", workspace);
  try
  {
    if (!alg->execute())
Nick Draper's avatar
Nick Draper committed
    {
      throw std::runtime_error("FindDetectorsInShape sub-algorithm has not executed successfully\n");
Nick Draper's avatar
Nick Draper committed
    }
  } catch (std::runtime_error&)
  {
    g_log.error("Unable to successfully execute FindDetectorsInShape sub-algorithm");
    throw;
  }
  progress(0.5);
Nick Draper's avatar
Nick Draper committed

  //extract the results
  return alg->getProperty("DetectorList");
}

void MaskDetectorsInShape::runMaskDetectors(API::MatrixWorkspace_sptr workspace, const std::vector<int> detectorIds)
{
  IAlgorithm_sptr alg = createSubAlgorithm("MaskDetectors", 0.85, 1.0);
  alg->setProperty<std::vector<int> > ("DetectorList", detectorIds);
  alg->setProperty<MatrixWorkspace_sptr> ("Workspace", workspace);
  try
  {
    if (!alg->execute())
Nick Draper's avatar
Nick Draper committed
    {
      throw std::runtime_error("MaskDetectors sub-algorithm has not executed successfully\n");
Nick Draper's avatar
Nick Draper committed
    }
  } catch (std::runtime_error&)
  {
    g_log.error("Unable to successfully execute MaskDetectors sub-algorithm");
    throw;
  }
  progress(1);
}
Nick Draper's avatar
Nick Draper committed

} // namespace DataHandling
Nick Draper's avatar
Nick Draper committed
} // namespace Mantid