Beispiel #1
0
/** Returns a cropped workspace with data equal to and above the specified x limit
 *
 *  @param workspace :: MatrixWorkspace
 *  @param x :: Minimum allowed x-value in the data.
 *  @return MatrixWorkspace cropped to values with x >= specified limit.
 */
MatrixWorkspace_sptr PoldiTruncateData::getWorkspaceAboveX(MatrixWorkspace_sptr workspace, double x)
{
    Algorithm_sptr crop = getCropAlgorithmForWorkspace(workspace);
    crop->setProperty("Xmin", x);

    return getOutputWorkspace(crop);
}
Beispiel #2
0
/** Sum all detector pixels except monitors and masked detectors
 *  @param WS ::    The workspace containing the spectrum to sum
 *  @return A Workspace2D containing the sum
 */
API::MatrixWorkspace_sptr CalculateTransmissionBeamSpreader::sumSpectra(API::MatrixWorkspace_sptr WS)
{
  Algorithm_sptr childAlg = createSubAlgorithm("SumSpectra");
  childAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", WS);
  childAlg->setProperty<bool>("IncludeMonitors", false);
  childAlg->executeAsSubAlg();
  return childAlg->getProperty("OutputWorkspace");
}
Beispiel #3
0
/** Extracts OutputWorkspace property from supplied algorithm is present.
 *
 *  This methods executes the given algorithm and tries to extract the output workspace.
 *
 *  @param algorithm :: Pointer to algorithm.
 *  @return MatrixWorkspace stored in algorithm's OutputWorkspace property.
 */
MatrixWorkspace_sptr PoldiTruncateData::getOutputWorkspace(Algorithm_sptr algorithm)
{
    if(!algorithm || !algorithm->execute()) {
        throw std::runtime_error("Workspace could not be retrieved successfully.");
    }

    MatrixWorkspace_sptr outputWorkspace = algorithm->getProperty("OutputWorkspace");
    return outputWorkspace;
}
/** Perform SortEvents on the output workspaces
 * but only if they are EventWorkspaces.
 *
 * @param ws :: any Workspace. Does nothing if not EventWorkspace.
 */
void AlignAndFocusPowder::doSortEvents(Mantid::API::Workspace_sptr ws) {
  EventWorkspace_sptr eventWS = boost::dynamic_pointer_cast<EventWorkspace>(ws);
  if (!eventWS)
    return;
  Algorithm_sptr alg = this->createChildAlgorithm("SortEvents");
  alg->setProperty("InputWorkspace", eventWS);
  alg->setPropertyValue("SortBy", "X Value");
  alg->executeAsChildAlg();
}
/** Process WorkspaceGroup inputs.
 *
 * Overriden from Algorithm base class.
 *
 * This should be called after checkGroups(), which sets up required members.
 * It goes through each member of the group(s), creates and sets an algorithm
 * for each and executes them one by one.
 *
 * If there are several group input workspaces, then the member of each group
 * is executed pair-wise.
 *
 * @param sourceAlg : Source algorithm
 * @param vecMultiPeriodGroups : Vector of pre-identified multiperiod groups.
 * @return true - if all the workspace members are executed.
 */
bool MultiPeriodGroupWorker::processGroups(
    Algorithm *const sourceAlg,
    const VecWSGroupType &vecMultiPeriodGroups) const {
  // If we are not processing multiperiod groups, use the base behaviour.
  if (vecMultiPeriodGroups.empty()) {
    return false; // Indicates that this is not a multiperiod group workspace.
  }
  Property *outputWorkspaceProperty = sourceAlg->getProperty("OutputWorkspace");
  const std::string outName = outputWorkspaceProperty->value();

  const size_t nPeriods = vecMultiPeriodGroups[0]->size();
  WorkspaceGroup_sptr outputWS = boost::make_shared<WorkspaceGroup>();
  AnalysisDataService::Instance().addOrReplace(outName, outputWS);

  double progress_proportion = 1.0 / static_cast<double>(nPeriods);
  // Loop through all the periods. Create spawned algorithms of the same type as
  // this to process pairs from the input groups.
  for (size_t i = 0; i < nPeriods; ++i) {
    const int periodNumber = static_cast<int>(i + 1);
    // use create Child Algorithm that look like this one
    Algorithm_sptr alg = sourceAlg->createChildAlgorithm(
        sourceAlg->name(), progress_proportion * periodNumber,
        progress_proportion * (1 + periodNumber), sourceAlg->isLogging(),
        sourceAlg->version());
    if (!alg) {
      throw std::runtime_error("Algorithm creation failed.");
    }
    // Don't make the new algorithm a child so that it's workspaces are stored
    // correctly
    alg->setChild(false);
    alg->setRethrows(true);
    alg->initialize();
    // Copy properties that aren't workspaces properties.
    sourceAlg->copyNonWorkspaceProperties(alg.get(), periodNumber);

    if (this->useCustomWorkspaceProperty()) {
      const std::string inputWorkspaces =
          createFormattedInputWorkspaceNames(i, vecMultiPeriodGroups);
      // Set the input workspace property.
      alg->setPropertyValue(this->m_workspacePropertyName, inputWorkspaces);
    } else {
      // Configure input properties that are group workspaces.
      copyInputWorkspaceProperties(alg.get(), sourceAlg, periodNumber);
    }
    const std::string outName_i = outName + "_" + Strings::toString(i + 1);
    alg->setPropertyValue("OutputWorkspace", outName_i);
    // Run the spawned algorithm.
    if (!alg->execute()) {
      throw std::runtime_error("Execution of " + sourceAlg->name() +
                               " for group entry " + Strings::toString(i + 1) +
                               " failed.");
    }
    // Add the output workpace from the spawned algorithm to the group.
    outputWS->add(outName_i);
  }

  sourceAlg->setProperty("OutputWorkspace", outputWS);

  return true;
}
Beispiel #6
0
  /** Execute the algorithm.
   */
  void OneStepMDEW::exec()
  {
    std::string tempWsName = getPropertyValue("OutputWorkspace") + "_nxs";

    Algorithm_sptr childAlg;

    // -------- First we load the event nexus file -------------
    childAlg = AlgorithmFactory::Instance().create("LoadEventNexus", 1); // new Mantid::NeXus::LoadEventNexus();
    childAlg->initialize();
    childAlg->setPropertyValue("Filename", getPropertyValue("Filename"));
    childAlg->setPropertyValue("OutputWorkspace", tempWsName);
    childAlg->executeAsSubAlg();

//    Workspace_sptr tempWS = childAlg->getProperty<Workspace>("OutputWorkspace");
//    IEventWorkspace_sptr tempEventWS = boost::dynamic_pointer_cast<IEventWorkspace>(AnalysisDataService::Instance().retrieve(tempWsName));
    //    IEventWorkspace_sptr tempEventWS = boost::dynamic_pointer_cast<IEventWorkspace>(AnalysisDataService::Instance().retrieve(tempWsName));


    // --------- Now Convert -------------------------------
    //childAlg = createSubAlgorithm("ConvertToDiffractionMDWorkspace");
    childAlg = AlgorithmFactory::Instance().create("ConvertToDiffractionMDWorkspace", 1);  // new ConvertToDiffractionMDWorkspace();
    childAlg->initialize();
    childAlg->setPropertyValue("InputWorkspace", tempWsName);
    childAlg->setProperty<bool>("ClearInputWorkspace", false);
    childAlg->setProperty<bool>("LorentzCorrection", true);
    childAlg->setPropertyValue("OutputWorkspace", getPropertyValue("OutputWorkspace"));
    childAlg->executeAsSubAlg();

//    Workspace_sptr tempWS = childAlg->getProperty("OutputWorkspace");
//    IMDEventWorkspace_sptr outWS = boost::dynamic_pointer_cast<IMDEventWorkspace>(tempWS);
    IMDEventWorkspace_sptr outWS = boost::dynamic_pointer_cast<IMDEventWorkspace>(
        AnalysisDataService::Instance().retrieve(getPropertyValue("OutputWorkspace")));

    setProperty<Workspace_sptr>("OutputWorkspace", outWS);
  }
Beispiel #7
0
/** Perform SortEvents on the output workspaces (accumulation or output)
 * but only if they are EventWorkspaces. This will help the GUI
 * cope with redrawing.
 *
 * @param ws :: any Workspace. Does nothing if not EventWorkspace.
 */
void LoadLiveData::doSortEvents(Mantid::API::Workspace_sptr ws) {
  EventWorkspace_sptr eventWS = boost::dynamic_pointer_cast<EventWorkspace>(ws);
  if (!eventWS)
    return;
  CPUTimer tim;
  Algorithm_sptr alg = this->createChildAlgorithm("SortEvents");
  alg->setProperty("InputWorkspace", eventWS);
  alg->setPropertyValue("SortBy", "X Value");
  alg->executeAsChildAlg();
  g_log.debug() << tim << " to perform SortEvents on " << ws->name() << '\n';
}
/** Returns a MatrixWorkspace with all spectrum summed up.
 *
 *  The summation is done with the SumSpectra-algorithm.
 *
 *  @param workspace :: MatrixWorkspace
 *  @return MatrixWorkspace with one spectrum which contains all counts.
 */
MatrixWorkspace_sptr
PoldiTruncateData::getSummedSpectra(MatrixWorkspace_sptr workspace) {
  Algorithm_sptr sumSpectra = createChildAlgorithm("SumSpectra");

  if (!sumSpectra) {
    throw std::runtime_error("Could not create SumSpectra algorithm.");
  }

  sumSpectra->setProperty("InputWorkspace", workspace);

  return getOutputWorkspace(sumSpectra);
}
/** Creates a CropWorkspace-algorithm for the given workspace
 *
 *  This method calls createChildAlgorithm() to create an instance of the
 *CropWorkspace algorithm.
 *  If the creation is successful, the supplied workspace is set as
 *InputParameter.
 *
 *  @param workspace :: MatrixWorkspace
 *  @return Pointer to crop algorithm.
 */
Algorithm_sptr PoldiTruncateData::getCropAlgorithmForWorkspace(
    MatrixWorkspace_sptr workspace) {
  Algorithm_sptr crop = createChildAlgorithm("CropWorkspace");

  if (!crop) {
    throw std::runtime_error("Could not create CropWorkspace algorithm");
  }

  crop->setProperty("InputWorkspace", workspace);

  return crop;
}
//-----------------------------------------------------------------------------------------------------------------------
/// Run the Child Algorithm LoadInstrument (or LoadInstrumentFromRaw)
void LoadInstrument::runLoadParameterFile() {
  g_log.debug("Loading the parameter definition...");

  // First search for XML parameter file in same folder as IDF file
  const std::string::size_type dir_end = m_filename.find_last_of("\\/");
  std::string directoryName =
      m_filename.substr(0, dir_end + 1); // include final '/'.
  std::string fullPathParamIDF = getFullPathParamIDF(directoryName);

  if (fullPathParamIDF.empty()) {
    // Not found, so search the other places were it may occur
    Kernel::ConfigServiceImpl &configService =
        Kernel::ConfigService::Instance();
    std::vector<std::string> directoryNames =
        configService.getInstrumentDirectories();

    for (auto directoryName : directoryNames) {
      // This will iterate around the directories from user ->etc ->install, and
      // find the first beat file
      fullPathParamIDF = getFullPathParamIDF(directoryName);
      // stop when you find the first one
      if (!fullPathParamIDF.empty())
        break;
    }
  }

  if (!fullPathParamIDF.empty()) {

    g_log.debug() << "Parameter file: " << fullPathParamIDF << std::endl;
    // Now execute the Child Algorithm. Catch and log any error, but don't stop.
    try {
      // To allow the use of ExperimentInfo instead of workspace, we call it
      // manually
      Algorithm_sptr loadParamAlg = createChildAlgorithm("LoadParameterFile");
      loadParamAlg->setProperty("Filename", fullPathParamIDF);
      loadParamAlg->setProperty("Workspace", m_workspace);
      loadParamAlg->execute();
      g_log.debug("Parameters loaded successfully.");
    } catch (std::invalid_argument &e) {
      g_log.information(
          "LoadParameterFile: No parameter file found for this instrument");
      g_log.information(e.what());
    } catch (std::runtime_error &e) {
      g_log.information(
          "Unable to successfully run LoadParameterFile Child Algorithm");
      g_log.information(e.what());
    }
  } else {
    g_log.information("No parameter file found for this instrument");
  }
}
Beispiel #11
0
/** Extracts a single spectrum from a Workspace2D into a new workspaces. Uses CropWorkspace to do this.
 *  @param WS ::    The workspace containing the spectrum to extract
 *  @param index :: The workspace index of the spectrum to extract
 *  @return A Workspace2D containing the extracted spectrum
 */
API::MatrixWorkspace_sptr CalculateTransmissionBeamSpreader::extractSpectrum(API::MatrixWorkspace_sptr WS, const size_t index)
{
  // Check that given spectra are monitors
  if ( !WS->getDetector(index)->isMonitor() )
  {
    g_log.information("The Incident Beam Monitor UDET provided is not marked as a monitor");
  }

  Algorithm_sptr childAlg = createSubAlgorithm("ExtractSingleSpectrum",0.0,0.4);
  childAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", WS);
  childAlg->setProperty<int>("WorkspaceIndex", static_cast<int>(index));
  childAlg->executeAsSubAlg();
  return childAlg->getProperty("OutputWorkspace");
}
/**
 * Process the two groups together and set the result accordingly
 * @param groupOne :: Input group 1
 * @param groupTwo :: Input group 2
 */
void CheckWorkspacesMatch::processGroups(
    boost::shared_ptr<const API::WorkspaceGroup> groupOne,
    boost::shared_ptr<const API::WorkspaceGroup> groupTwo) {
  // Check their sizes
  const size_t totalNum = static_cast<size_t>(groupOne->getNumberOfEntries());
  if (groupOne->getNumberOfEntries() != groupTwo->getNumberOfEntries()) {
    this->result = "GroupWorkspaces size mismatch.";
    return;
  }

  // See if there are any other properties that require setting
  const std::vector<Property *> &allProps = this->getProperties();
  std::vector<Property *> nonDefaultProps;
  nonDefaultProps.reserve(allProps.size());
  for (size_t i = 0; i < allProps.size(); ++i) {
    Property *p = allProps[i];
    const std::string &propName = p->name();
    // Skip those not set and the input workspaces
    if (p->isDefault() || propName == "Workspace1" || propName == "Workspace2")
      continue;
    nonDefaultProps.push_back(p);
  }
  const size_t numNonDefault = nonDefaultProps.size();

  const double progressFraction = 1.0 / static_cast<double>(totalNum);
  std::vector<std::string> namesOne = groupOne->getNames();
  std::vector<std::string> namesTwo = groupTwo->getNames();
  for (size_t i = 0; i < totalNum; ++i) {
    // We should use an algorithm for each so that the output properties are
    // reset properly
    Algorithm_sptr checker = this->createChildAlgorithm(
        this->name(), progressFraction * (double)i,
        progressFraction * (double)(i + 1), false, this->version());
    checker->setPropertyValue("Workspace1", namesOne[i]);
    checker->setPropertyValue("Workspace2", namesTwo[i]);
    for (size_t j = 0; j < numNonDefault; ++j) {
      Property *p = nonDefaultProps[j];
      checker->setPropertyValue(p->name(), p->value());
    }
    checker->execute();
    std::string success = checker->getProperty("Result");
    if (success != this->successString()) {
      if (!this->result.empty())
        this->result += "\n";
      this->result +=
          success + ". Inputs=[" + namesOne[i] + "," + namesTwo[i] + "]";
    }
  }
}
Beispiel #13
0
void SofQW::exec() {
  // Find the approopriate algorithm
  std::string method = this->getProperty("Method");
  std::string child = "SofQW" + method;
  
  // Setup and run
  Algorithm_sptr childAlg = boost::dynamic_pointer_cast<Algorithm>(
      createChildAlgorithm(child, 0.0, 1.0));
  // This will add the Method property to the child algorithm but it will be
  // ignored anyway...
  childAlg->copyPropertiesFrom(*this);
  childAlg->execute();

  MatrixWorkspace_sptr outputWS = childAlg->getProperty("OutputWorkspace");
  this->setProperty("OutputWorkspace", outputWS);
}
Beispiel #14
0
/**
 * Run new CompareWorkspaces algorithm as a child algorithm.
 *
 * Result string formatted the same way as before; "Success!" when workspaces
 * match or a newline separated list of mismatch messages.
 *
 * @param group_compare Should output be formatted like group comparison?
 * @return A string containing either successString() or mismatch messages
 */
std::string CheckWorkspacesMatch::runCompareWorkspaces(bool group_compare) {
  // This algorithm produces a single result string
  std::string result;

  // Use new CompareWorkspaces algorithm to perform comparison
  Algorithm_sptr compare = this->createChildAlgorithm("CompareWorkspaces");
  compare->setRethrows(true);
  compare->setLogging(false);

  // Forward workspace properties
  Workspace_sptr ws1 = getProperty("Workspace1");
  Workspace_sptr ws2 = getProperty("Workspace2");
  compare->setProperty("Workspace1", ws1);
  compare->setProperty("Workspace2", ws2);

  // Copy any other non-default properties
  const std::vector<Property *> &allProps = this->getProperties();
  auto propCount = allProps.size();
  for (size_t i = 0; i < propCount; ++i) {
    Property *prop = allProps[i];
    const std::string &pname = prop->name();

    if (!prop->isDefault() && pname != "Workspace1" && pname != "Workspace2" &&
        pname != "Result")
      compare->setPropertyValue(pname, prop->value());
  }

  // Execute comparison
  compare->execute();

  // Generate result string
  if (!compare->getProperty("Result")) {
    ITableWorkspace_sptr table = compare->getProperty("Messages");
    auto rowcount = table->rowCount();
    for (size_t i = 0; i < rowcount; ++i) {
      result += table->cell<std::string>(i, 0);

      // Emulate special case output format when comparing groups
      if (group_compare &&
          table->cell<std::string>(i, 0) !=
              "Type mismatch. One workspace is a group, the other is not." &&
          table->cell<std::string>(i, 0) != "GroupWorkspaces size mismatch.") {

        result += ". Inputs=[" + table->cell<std::string>(i, 1) + "," +
                  table->cell<std::string>(i, 2) + "]";
      }

      if (i < (rowcount - 1))
        result += "\n";
    }
  } else {
    result = successString();
  }

  return result;
}
/** Add workspace2 to workspace1 by adding spectrum.
  */
MatrixWorkspace_sptr
AlignAndFocusPowder::conjoinWorkspaces(API::MatrixWorkspace_sptr ws1,
                                       API::MatrixWorkspace_sptr ws2,
                                       size_t offset) {
  // Get information from ws1: maximum spectrum number, and store original
  // spectrum Nos
  size_t nspec1 = ws1->getNumberHistograms();
  specnum_t maxspecNo1 = 0;
  std::vector<specnum_t> origspecNos;
  for (size_t i = 0; i < nspec1; ++i) {
    specnum_t tmpspecNo = ws1->getSpectrum(i).getSpectrumNo();
    origspecNos.push_back(tmpspecNo);
    if (tmpspecNo > maxspecNo1)
      maxspecNo1 = tmpspecNo;
  }

  g_log.information() << "[DBx536] Max spectrum number of ws1 = " << maxspecNo1
                      << ", Offset = " << offset << ".\n";

  size_t nspec2 = ws2->getNumberHistograms();

  // Conjoin 2 workspaces
  Algorithm_sptr alg = this->createChildAlgorithm("AppendSpectra");
  alg->initialize();
  ;

  alg->setProperty("InputWorkspace1", ws1);
  alg->setProperty("InputWorkspace2", ws2);
  alg->setProperty("OutputWorkspace", ws1);
  alg->setProperty("ValidateInputs", false);

  alg->executeAsChildAlg();

  API::MatrixWorkspace_sptr outws = alg->getProperty("OutputWorkspace");

  // FIXED : Restore the original spectrum Nos to spectra from ws1
  for (size_t i = 0; i < nspec1; ++i) {
    specnum_t tmpspecNo = outws->getSpectrum(i).getSpectrumNo();
    outws->getSpectrum(i).setSpectrumNo(origspecNos[i]);

    g_log.information() << "[DBx540] Conjoined spectrum " << i
                        << ": restore spectrum number to "
                        << outws->getSpectrum(i).getSpectrumNo()
                        << " from spectrum number = " << tmpspecNo << ".\n";
  }

  // Rename spectrum number
  if (offset >= 1) {
    for (size_t i = 0; i < nspec2; ++i) {
      specnum_t newspecid = maxspecNo1 + static_cast<specnum_t>((i) + offset);
      outws->getSpectrum(nspec1 + i).setSpectrumNo(newspecid);
      // ISpectrum* spec = outws->getSpectrum(nspec1+i);
      // if (spec)
      // spec->setSpectrumNo(3);
    }
  }

  return outws;
}
Beispiel #16
0
/** Get a pointer to an instrument in one of 3 ways: InputWorkspace,
 * InstrumentName, InstrumentFilename
 * @param alg :: algorithm from which to get the property values.
 * */
Geometry::Instrument_const_sptr
LoadCalFile::getInstrument3Ways(Algorithm *alg) {
  MatrixWorkspace_sptr inWS = alg->getProperty("InputWorkspace");
  std::string InstrumentName = alg->getPropertyValue("InstrumentName");
  std::string InstrumentFilename = alg->getPropertyValue("InstrumentFilename");

  // Some validation
  int numParams = 0;
  if (inWS)
    numParams++;
  if (!InstrumentName.empty())
    numParams++;
  if (!InstrumentFilename.empty())
    numParams++;

  if (numParams > 1)
    throw std::invalid_argument("You must specify exactly ONE way to get an "
                                "instrument (workspace, instrument name, or "
                                "IDF file). You specified more than one.");
  if (numParams == 0)
    throw std::invalid_argument("You must specify exactly ONE way to get an "
                                "instrument (workspace, instrument name, or "
                                "IDF file). You specified none.");

  // ---------- Get the instrument one of 3 ways ---------------------------
  Instrument_const_sptr inst;
  if (inWS) {
    inst = inWS->getInstrument();
  } else {
    Algorithm_sptr childAlg =
        alg->createChildAlgorithm("LoadInstrument", 0.0, 0.2);
    MatrixWorkspace_sptr tempWS = boost::make_shared<Workspace2D>();
    childAlg->setProperty<MatrixWorkspace_sptr>("Workspace", tempWS);
    childAlg->setPropertyValue("Filename", InstrumentFilename);
    childAlg->setPropertyValue("InstrumentName", InstrumentName);
    childAlg->setProperty("RewriteSpectraMap",
                          Mantid::Kernel::OptionalBool(false));
    childAlg->executeAsChildAlg();
    inst = tempWS->getInstrument();
  }

  return inst;
}
// Private function to load parameter file specified by a full path name into
// given workspace, returning success.
bool LoadIDFFromNexus::loadParameterFile(
    const std::string &fullPathName,
    const MatrixWorkspace_sptr localWorkspace) {

  try {
    // load and also populate instrument parameters from this 'fallback'
    // parameter file
    Algorithm_sptr loadParamAlg = createChildAlgorithm("LoadParameterFile");
    loadParamAlg->setProperty("Filename", fullPathName);
    loadParamAlg->setProperty("Workspace", localWorkspace);
    loadParamAlg->execute();
    g_log.notice() << "Instrument parameter file: " << fullPathName
                   << " has been loaded.\n\n";
    return true; // Success
  } catch (std::runtime_error &) {
    g_log.debug() << "Instrument parameter file: " << fullPathName
                  << " not found or un-parsable.\n";
    return false; // Failure
  }
}
Beispiel #18
0
void SofQW::exec() {
  // Find the approopriate algorithm
  std::string method = this->getProperty("Method");
  std::string child = "SofQW" + method;

  // Setup and run
  Algorithm_sptr childAlg = boost::dynamic_pointer_cast<Algorithm>(
      createChildAlgorithm(child, 0.0, 1.0));
  // This will add the Method property to the child algorithm but it will be
  // ignored anyway...
  childAlg->copyPropertiesFrom(*this);
  childAlg->execute();

  MatrixWorkspace_sptr outputWS = childAlg->getProperty("OutputWorkspace");

  this->setProperty("OutputWorkspace", outputWS);

  // Progress reports & cancellation
  MatrixWorkspace_const_sptr inputWorkspace = getProperty("InputWorkspace");
  const size_t nHistos = inputWorkspace->getNumberHistograms();
  auto m_progress = make_unique<Progress>(this, 0.0, 1.0, nHistos);
  m_progress->report("Creating output workspace");
}
Beispiel #19
0
/// Constructor
AlgorithmProxy::AlgorithmProxy(Algorithm_sptr alg)
    : PropertyManagerOwner(),
      m_executeAsync(new Poco::ActiveMethod<bool, Poco::Void, AlgorithmProxy>(
                         this, &AlgorithmProxy::executeAsyncImpl)),
      m_name(alg->name()), m_category(alg->category()),
      m_categorySeparator(alg->categorySeparator()), m_alias(alg->alias()),
      m_summary(alg->summary()), m_version(alg->version()), m_alg(alg),
      m_isExecuted(), m_isLoggingEnabled(true), m_loggingOffset(0),
      m_isAlgStartupLoggingEnabled(true), m_rethrow(false), m_isChild(false) {
    if (!alg) {
        throw std::logic_error("Unable to create a proxy algorithm.");
    }
    alg->initialize();
    copyPropertiesFrom(*alg);
}
  void EstimatePDDetectorResolution::retrieveInstrumentParameters()
  {
#if 0
    // Call SolidAngle to get solid angles for all detectors
    Algorithm_sptr calsolidangle = createChildAlgorithm("SolidAngle", -1, -1, true);
    calsolidangle->initialize();

    calsolidangle->setProperty("InputWorkspace", m_inputWS);

    calsolidangle->execute();
    if (!calsolidangle->isExecuted())
      throw runtime_error("Unable to run solid angle. ");

    m_solidangleWS = calsolidangle->getProperty("OutputWorkspace");
    if (!m_solidangleWS)
      throw runtime_error("Unable to get solid angle workspace from SolidAngle(). ");


    size_t numspec = m_solidangleWS->getNumberHistograms();
    for (size_t i = 0; i < numspec; ++i)
      g_log.debug() << "[DB]: " << m_solidangleWS->readY(i)[0] << "\n";
#endif

    // Calculate centre neutron velocity
    Property* cwlproperty = m_inputWS->run().getProperty("LambdaRequest");
    if (!cwlproperty)
      throw runtime_error("Unable to locate property LambdaRequest as central wavelength. ");
    TimeSeriesProperty<double>* cwltimeseries = dynamic_cast<TimeSeriesProperty<double>* >(cwlproperty);
    if (!cwltimeseries)
      throw runtime_error("LambdaReqeust is not a TimeSeriesProperty in double. ");
    if (cwltimeseries->size() != 1)
      throw runtime_error("LambdaRequest should contain 1 and only 1 entry. ");

    double centrewavelength = cwltimeseries->nthValue(0);
    string unit = cwltimeseries->units();
    if (unit.compare("Angstrom") == 0)
      centrewavelength *= 1.0E-10;
    else
      throw runtime_error("Unit is not recognized");

    m_centreVelocity = PhysicalConstants::h/PhysicalConstants::NeutronMass/centrewavelength;
    g_log.notice() << "Centre wavelength = " << centrewavelength << ", Centre neutron velocity = " << m_centreVelocity << "\n";

    // Calcualte L1 sample to source
    Instrument_const_sptr instrument = m_inputWS->getInstrument();
    V3D samplepos = instrument->getSample()->getPos();
    V3D sourcepos = instrument->getSource()->getPos();
    m_L1 = samplepos.distance(sourcepos);
    g_log.notice() << "L1 = " << m_L1 << "\n";

    return;
  }
Beispiel #21
0
/**
 * @brief CompareWorkspaces::processGroups
 * @param groupOne
 * @param groupTwo
 */
void CompareWorkspaces::processGroups(
    boost::shared_ptr<const API::WorkspaceGroup> groupOne,
    boost::shared_ptr<const API::WorkspaceGroup> groupTwo) {

  // Check their sizes
  const size_t totalNum = static_cast<size_t>(groupOne->getNumberOfEntries());
  if (groupOne->getNumberOfEntries() != groupTwo->getNumberOfEntries()) {
    recordMismatch("GroupWorkspaces size mismatch.");
    return;
  }

  // See if there are any other properties that require setting
  const std::vector<Property *> &allProps = this->getProperties();
  std::vector<Property *> nonDefaultProps;
  nonDefaultProps.reserve(allProps.size());
  for (auto p : allProps) {
    const std::string &propName = p->name();
    // Skip those not set and the input workspaces
    if (p->isDefault() || propName == "Workspace1" || propName == "Workspace2")
      continue;
    nonDefaultProps.push_back(p);
  }
  const size_t numNonDefault = nonDefaultProps.size();

  const double progressFraction = 1.0 / static_cast<double>(totalNum);
  std::vector<std::string> namesOne = groupOne->getNames();
  std::vector<std::string> namesTwo = groupTwo->getNames();
  for (size_t i = 0; i < totalNum; ++i) {
    // We should use an algorithm for each so that the output properties are
    // reset properly
    Algorithm_sptr checker = this->createChildAlgorithm(
        this->name(), progressFraction * static_cast<double>(i),
        progressFraction * static_cast<double>(i + 1), false, this->version());
    checker->setPropertyValue("Workspace1", namesOne[i]);
    checker->setPropertyValue("Workspace2", namesTwo[i]);
    for (size_t j = 0; j < numNonDefault; ++j) {
      Property *p = nonDefaultProps[j];
      checker->setPropertyValue(p->name(), p->value());
    }
    checker->execute();

    bool success = checker->getProperty("Result");
    if (!success) {
      ITableWorkspace_sptr table = checker->getProperty("Messages");
      recordMismatch(table->cell<std::string>(0, 0), namesOne[i], namesTwo[i]);
    }
  }
}
Beispiel #22
0
/** Uses 'Linear' as a subalgorithm to fit the log of the exponential curve expected for the transmission.
 *  @param WS :: The single-spectrum workspace to fit
 *  @return A workspace containing the fit
 */
API::MatrixWorkspace_sptr CalculateTransmissionBeamSpreader::fitToData(API::MatrixWorkspace_sptr WS)
{
  g_log.information("Fitting the experimental transmission curve");
  Algorithm_sptr childAlg = createSubAlgorithm("Linear",0.6,1.0);
  childAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", WS);
  const double lambdaMin = getProperty("MinWavelength");
  const double lambdaMax = getProperty("MaxWavelength");
  childAlg->setProperty<double>("StartX",lambdaMin);
  childAlg->setProperty<double>("EndX",lambdaMax);
  childAlg->executeAsSubAlg();

  std::string fitStatus = childAlg->getProperty("FitStatus");
  if ( fitStatus != "success" )
  {
    g_log.error("Unable to successfully fit the data: " + fitStatus);
    throw std::runtime_error("Unable to successfully fit the data");
  }
 
  // Only get to here if successful
  MatrixWorkspace_sptr result = childAlg->getProperty("OutputWorkspace");

  if (logFit)
  {
    // Need to transform back to 'unlogged'
    double b = childAlg->getProperty("FitIntercept");
    double m = childAlg->getProperty("FitSlope");
    b = std::pow(10,b);
    m = std::pow(10,m);

    const MantidVec & X = result->readX(0);
    MantidVec & Y = result->dataY(0);
    MantidVec & E = result->dataE(0);
    for (size_t i = 0; i < Y.size(); ++i)
    {
      Y[i] = b*(std::pow(m,0.5*(X[i]+X[i+1])));
      E[i] = std::abs(E[i]*Y[i]);
    }
  }

  return result;
}
bool ReflectometryReductionOneAuto::processGroups() {
    auto group = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
                     getPropertyValue("InputWorkspace"));
    const std::string outputIvsQ = this->getPropertyValue("OutputWorkspace");
    const std::string outputIvsLam =
        this->getPropertyValue("OutputWorkspaceWavelength");

    // Create a copy of ourselves
    Algorithm_sptr alg = this->createChildAlgorithm(
                             this->name(), -1, -1, this->isLogging(), this->version());
    alg->setChild(false);
    alg->setRethrows(true);

    // Copy all the non-workspace properties over
    std::vector<Property *> props = this->getProperties();
    for (auto prop = props.begin(); prop != props.end(); ++prop) {
        if (*prop) {
            IWorkspaceProperty *wsProp = dynamic_cast<IWorkspaceProperty *>(*prop);
            if (!wsProp)
                alg->setPropertyValue((*prop)->name(), (*prop)->value());
        }
    }

    // Check if the transmission runs are groups or not
    const std::string firstTrans = this->getPropertyValue("FirstTransmissionRun");
    WorkspaceGroup_sptr firstTransG;
    if (!firstTrans.empty()) {
        auto firstTransWS =
            AnalysisDataService::Instance().retrieveWS<Workspace>(firstTrans);
        firstTransG = boost::dynamic_pointer_cast<WorkspaceGroup>(firstTransWS);

        if (!firstTransG)
            alg->setProperty("FirstTransmissionRun", firstTrans);
        else if (group->size() != firstTransG->size())
            throw std::runtime_error("FirstTransmissionRun WorkspaceGroup must be "
                                     "the same size as the InputWorkspace "
                                     "WorkspaceGroup");
    }

    const std::string secondTrans =
        this->getPropertyValue("SecondTransmissionRun");
    WorkspaceGroup_sptr secondTransG;
    if (!secondTrans.empty()) {
        auto secondTransWS =
            AnalysisDataService::Instance().retrieveWS<Workspace>(secondTrans);
        secondTransG = boost::dynamic_pointer_cast<WorkspaceGroup>(secondTransWS);

        if (!secondTransG)
            alg->setProperty("SecondTransmissionRun", secondTrans);
        else if (group->size() != secondTransG->size())
            throw std::runtime_error("SecondTransmissionRun WorkspaceGroup must be "
                                     "the same size as the InputWorkspace "
                                     "WorkspaceGroup");
    }

    std::vector<std::string> IvsQGroup, IvsLamGroup;

    // Execute algorithm over each group member (or period, if this is
    // multiperiod)
    size_t numMembers = group->size();
    for (size_t i = 0; i < numMembers; ++i) {
        const std::string IvsQName =
            outputIvsQ + "_" + boost::lexical_cast<std::string>(i + 1);
        const std::string IvsLamName =
            outputIvsLam + "_" + boost::lexical_cast<std::string>(i + 1);

        alg->setProperty("InputWorkspace", group->getItem(i)->name());
        alg->setProperty("OutputWorkspace", IvsQName);
        alg->setProperty("OutputWorkspaceWavelength", IvsLamName);

        // Handle transmission runs
        if (firstTransG)
            alg->setProperty("FirstTransmissionRun", firstTransG->getItem(i)->name());
        if (secondTransG)
            alg->setProperty("SecondTransmissionRun",
                             secondTransG->getItem(i)->name());

        alg->execute();

        IvsQGroup.push_back(IvsQName);
        IvsLamGroup.push_back(IvsLamName);

        // We use the first group member for our thetaout value
        if (i == 0)
            this->setPropertyValue("ThetaOut", alg->getPropertyValue("ThetaOut"));
    }

    // Group the IvsQ and IvsLam workspaces
    Algorithm_sptr groupAlg = this->createChildAlgorithm("GroupWorkspaces");
    groupAlg->setChild(false);
    groupAlg->setRethrows(true);

    groupAlg->setProperty("InputWorkspaces", IvsLamGroup);
    groupAlg->setProperty("OutputWorkspace", outputIvsLam);
    groupAlg->execute();

    groupAlg->setProperty("InputWorkspaces", IvsQGroup);
    groupAlg->setProperty("OutputWorkspace", outputIvsQ);
    groupAlg->execute();

    // If this is a multiperiod workspace and we have polarization corrections
    // enabled
    if (this->getPropertyValue("PolarizationAnalysis") !=
            noPolarizationCorrectionMode()) {
        if (group->isMultiperiod()) {
            // Perform polarization correction over the IvsLam group
            Algorithm_sptr polAlg =
                this->createChildAlgorithm("PolarizationCorrection");
            polAlg->setChild(false);
            polAlg->setRethrows(true);

            polAlg->setProperty("InputWorkspace", outputIvsLam);
            polAlg->setProperty("OutputWorkspace", outputIvsLam);
            polAlg->setProperty("PolarizationAnalysis",
                                this->getPropertyValue("PolarizationAnalysis"));
            polAlg->setProperty("CPp", this->getPropertyValue(cppLabel()));
            polAlg->setProperty("CRho", this->getPropertyValue(crhoLabel()));
            polAlg->setProperty("CAp", this->getPropertyValue(cApLabel()));
            polAlg->setProperty("CAlpha", this->getPropertyValue(cAlphaLabel()));
            polAlg->execute();

            // Now we've overwritten the IvsLam workspaces, we'll need to recalculate
            // the IvsQ ones
            alg->setProperty("FirstTransmissionRun", "");
            alg->setProperty("SecondTransmissionRun", "");
            for (size_t i = 0; i < numMembers; ++i) {
                const std::string IvsQName =
                    outputIvsQ + "_" + boost::lexical_cast<std::string>(i + 1);
                const std::string IvsLamName =
                    outputIvsLam + "_" + boost::lexical_cast<std::string>(i + 1);
                alg->setProperty("InputWorkspace", IvsLamName);
                alg->setProperty("OutputWorkspace", IvsQName);
                alg->setProperty("OutputWorkspaceWavelength", IvsLamName);
                alg->execute();
            }
        } else {
            g_log.warning("Polarization corrections can only be performed on "
                          "multiperiod workspaces.");
        }
    }

    // We finished successfully
    this->setPropertyValue("OutputWorkspace", outputIvsQ);
    this->setPropertyValue("OutputWorkspaceWavelength", outputIvsLam);
    setExecuted(true);
    notificationCenter().postNotification(
        new FinishedNotification(this, isExecuted()));
    return true;
}
Beispiel #24
0
/** Execute the algorithm.
 */
void LoadLiveData::exec() {
  // The full, post-processed output workspace
  m_outputWS = this->getProperty("OutputWorkspace");

  // Validate inputs
  if (this->hasPostProcessing()) {
    if (this->getPropertyValue("AccumulationWorkspace").empty())
      throw std::invalid_argument("Must specify the AccumulationWorkspace "
                                  "parameter if using PostProcessing.");

    // The accumulated but not post-processed output workspace
    m_accumWS = this->getProperty("AccumulationWorkspace");
  } else {
    // No post-processing, so the accumulation and output are the same
    m_accumWS = m_outputWS;
  }

  // Get or create the live listener
  ILiveListener_sptr listener = this->getLiveListener();

  // Do we need to reset the data?
  bool dataReset = listener->dataReset();

  // The listener returns a MatrixWorkspace containing the chunk of live data.
  Workspace_sptr chunkWS;
  bool dataNotYetGiven = true;
  while (dataNotYetGiven) {
    try {
      chunkWS = listener->extractData();
      dataNotYetGiven = false;
    } catch (Exception::NotYet &ex) {
      g_log.warning() << "The " << listener->name()
                      << " is not ready to return data: " << ex.what() << "\n";
      g_log.warning()
          << "Trying again in 10 seconds - cancel the algorithm to stop.\n";
      const int tenSeconds = 40;
      for (int i = 0; i < tenSeconds; ++i) {
        Poco::Thread::sleep(10000 / tenSeconds); // 250 ms
        this->interruption_point();
      }
    }
  }

  // TODO: Have the ILiveListener tell me exactly the time stamp
  DateAndTime lastTimeStamp = DateAndTime::getCurrentTime();
  this->setPropertyValue("LastTimeStamp", lastTimeStamp.toISO8601String());

  // Now we process the chunk
  Workspace_sptr processed = this->processChunk(chunkWS);

  bool PreserveEvents = this->getProperty("PreserveEvents");
  EventWorkspace_sptr processedEvent =
      boost::dynamic_pointer_cast<EventWorkspace>(processed);
  if (!PreserveEvents && processedEvent) {
    // Convert the monitor workspace, if there is one and it's necessary
    MatrixWorkspace_sptr monitorWS = processedEvent->monitorWorkspace();
    auto monitorEventWS =
        boost::dynamic_pointer_cast<EventWorkspace>(monitorWS);
    if (monitorEventWS) {
      auto monAlg = this->createChildAlgorithm("ConvertToMatrixWorkspace");
      monAlg->setProperty("InputWorkspace", monitorEventWS);
      monAlg->executeAsChildAlg();
      if (!monAlg->isExecuted())
        g_log.error(
            "Failed to convert monitors from events to histogram form.");
      monitorWS = monAlg->getProperty("OutputWorkspace");
    }

    // Now do the main workspace
    Algorithm_sptr alg = this->createChildAlgorithm("ConvertToMatrixWorkspace");
    alg->setProperty("InputWorkspace", processedEvent);
    std::string outputName = "__anonymous_livedata_convert_" +
                             this->getPropertyValue("OutputWorkspace");
    alg->setPropertyValue("OutputWorkspace", outputName);
    alg->execute();
    if (!alg->isExecuted())
      throw std::runtime_error("Error when calling ConvertToMatrixWorkspace "
                               "(since PreserveEvents=False). See log.");
    // Replace the "processed" workspace with the converted one.
    MatrixWorkspace_sptr temp = alg->getProperty("OutputWorkspace");
    if (monitorWS)
      temp->setMonitorWorkspace(monitorWS); // Set back the monitor workspace
    processed = temp;
  }

  // How do we accumulate the data?
  std::string accum = this->getPropertyValue("AccumulationMethod");

  // If the AccumulationWorkspace does not exist, we always replace the
  // AccumulationWorkspace.
  // Also, if the listener said we are resetting the data, then we clear out the
  // old.
  if (!m_accumWS || dataReset)
    accum = "Replace";

  g_log.notice() << "Performing the " << accum << " operation.\n";

  // Perform the accumulation and set the AccumulationWorkspace workspace
  if (accum == "Replace")
    this->replaceChunk(processed);
  else if (accum == "Append")
    this->appendChunk(processed);
  else
    // Default to Add.
    this->addChunk(processed);

  // At this point, m_accumWS is set.

  if (this->hasPostProcessing()) {
    // ----------- Run post-processing -------------
    this->runPostProcessing();
    // Set both output workspaces
    this->setProperty("AccumulationWorkspace", m_accumWS);
    this->setProperty("OutputWorkspace", m_outputWS);
    doSortEvents(m_outputWS);
  } else {
    // ----------- No post-processing -------------
    m_outputWS = m_accumWS;
    // We DO NOT set AccumulationWorkspace.
    this->setProperty("OutputWorkspace", m_outputWS);
  }

  // Output group requires some additional handling
  WorkspaceGroup_sptr out_gws =
      boost::dynamic_pointer_cast<WorkspaceGroup>(m_outputWS);
  if (out_gws) {
    size_t n = static_cast<size_t>(out_gws->getNumberOfEntries());
    for (size_t i = 0; i < n; ++i) {
      auto ws = out_gws->getItem(i);
      std::string itemName = ws->name();
      std::string wsName =
          getPropertyValue("OutputWorkspace") + "_" + std::to_string(i + 1);
      if (wsName != itemName) {
        if (AnalysisDataService::Instance().doesExist(itemName)) {
          // replace the temporary name with the proper one
          AnalysisDataService::Instance().rename(itemName, wsName);
        }
      } else {
        // touch the workspace in the ADS to issue a notification to update the
        // GUI
        AnalysisDataService::Instance().addOrReplace(itemName, ws);
      }
    }
  }
}
bool ReflectometryReductionOneAuto::processGroups() {
  // isPolarizationCorrectionOn is used to decide whether
  // we should process our Transmission WorkspaceGroup members
  // as individuals (not multiperiod) when PolarizationCorrection is off,
  // or sum over all of the workspaces in the group
  // and used that sum as our TransmissionWorkspace when PolarizationCorrection
  // is on.
  const bool isPolarizationCorrectionOn =
      this->getPropertyValue("PolarizationAnalysis") !=
      noPolarizationCorrectionMode();
  // Get our input workspace group
  auto group = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
      getPropertyValue("InputWorkspace"));
  // Get name of IvsQ workspace
  const std::string outputIvsQ = this->getPropertyValue("OutputWorkspace");
  // Get name of IvsLam workspace
  const std::string outputIvsLam =
      this->getPropertyValue("OutputWorkspaceWavelength");

  // Create a copy of ourselves
  Algorithm_sptr alg = this->createChildAlgorithm(
      this->name(), -1, -1, this->isLogging(), this->version());
  alg->setChild(false);
  alg->setRethrows(true);

  // Copy all the non-workspace properties over
  std::vector<Property *> props = this->getProperties();
  for (auto &prop : props) {
    if (prop) {
      IWorkspaceProperty *wsProp = dynamic_cast<IWorkspaceProperty *>(prop);
      if (!wsProp)
        alg->setPropertyValue(prop->name(), prop->value());
    }
  }

  // Check if the transmission runs are groups or not
  const std::string firstTrans = this->getPropertyValue("FirstTransmissionRun");
  WorkspaceGroup_sptr firstTransG;
  if (!firstTrans.empty()) {
    auto firstTransWS =
        AnalysisDataService::Instance().retrieveWS<Workspace>(firstTrans);
    firstTransG = boost::dynamic_pointer_cast<WorkspaceGroup>(firstTransWS);

    if (!firstTransG) {
      // we only have one transmission workspace, so we use it as it is.
      alg->setProperty("FirstTransmissionRun", firstTrans);
    } else if (group->size() != firstTransG->size() &&
               !isPolarizationCorrectionOn) {
      // if they are not the same size then we cannot associate a transmission
      // group workspace member with every input group workpspace member.
      throw std::runtime_error("FirstTransmissionRun WorkspaceGroup must be "
                               "the same size as the InputWorkspace "
                               "WorkspaceGroup");
    }
  }

  const std::string secondTrans =
      this->getPropertyValue("SecondTransmissionRun");
  WorkspaceGroup_sptr secondTransG;
  if (!secondTrans.empty()) {
    auto secondTransWS =
        AnalysisDataService::Instance().retrieveWS<Workspace>(secondTrans);
    secondTransG = boost::dynamic_pointer_cast<WorkspaceGroup>(secondTransWS);

    if (!secondTransG)
      // we only have one transmission workspace, so we use it as it is.
      alg->setProperty("SecondTransmissionRun", secondTrans);

    else if (group->size() != secondTransG->size() &&
             !isPolarizationCorrectionOn) {
      // if they are not the same size then we cannot associate a transmission
      // group workspace member with every input group workpspace member.
      throw std::runtime_error("SecondTransmissionRun WorkspaceGroup must be "
                               "the same size as the InputWorkspace "
                               "WorkspaceGroup");
    }
  }
  std::vector<std::string> IvsQGroup, IvsLamGroup;

  // Execute algorithm over each group member (or period, if this is
  // multiperiod)
  size_t numMembers = group->size();
  for (size_t i = 0; i < numMembers; ++i) {
    const std::string IvsQName =
        outputIvsQ + "_" + boost::lexical_cast<std::string>(i + 1);
    const std::string IvsLamName =
        outputIvsLam + "_" + boost::lexical_cast<std::string>(i + 1);

    // If our transmission run is a group and PolarizationCorrection is on
    // then we sum our transmission group members.
    //
    // This is done inside of the for loop to avoid the wrong workspace being
    // used when these arguments are passed through to the exec() method.
    // If this is not set in the loop, exec() will fetch the first workspace
    // from the specified Transmission Group workspace that the user entered.
    if (firstTransG && isPolarizationCorrectionOn) {
      auto firstTransmissionSum = sumOverTransmissionGroup(firstTransG);
      alg->setProperty("FirstTransmissionRun", firstTransmissionSum);
    }
    if (secondTransG && isPolarizationCorrectionOn) {
      auto secondTransmissionSum = sumOverTransmissionGroup(secondTransG);
      alg->setProperty("SecondTransmissionRun", secondTransmissionSum);
    }

    // Otherwise, if polarization correction is off, we process them
    // using one transmission group member at a time.
    if (firstTransG && !isPolarizationCorrectionOn) // polarization off
      alg->setProperty("FirstTransmissionRun", firstTransG->getItem(i)->name());
    if (secondTransG && !isPolarizationCorrectionOn) // polarization off
      alg->setProperty("SecondTransmissionRun",
                       secondTransG->getItem(i)->name());

    alg->setProperty("InputWorkspace", group->getItem(i)->name());
    alg->setProperty("OutputWorkspace", IvsQName);
    alg->setProperty("OutputWorkspaceWavelength", IvsLamName);
    alg->execute();

    MatrixWorkspace_sptr tempFirstTransWS =
        alg->getProperty("FirstTransmissionRun");

    IvsQGroup.push_back(IvsQName);
    IvsLamGroup.push_back(IvsLamName);

    // We use the first group member for our thetaout value
    if (i == 0)
      this->setPropertyValue("ThetaOut", alg->getPropertyValue("ThetaOut"));
  }

  // Group the IvsQ and IvsLam workspaces
  Algorithm_sptr groupAlg = this->createChildAlgorithm("GroupWorkspaces");
  groupAlg->setChild(false);
  groupAlg->setRethrows(true);

  groupAlg->setProperty("InputWorkspaces", IvsLamGroup);
  groupAlg->setProperty("OutputWorkspace", outputIvsLam);
  groupAlg->execute();

  groupAlg->setProperty("InputWorkspaces", IvsQGroup);
  groupAlg->setProperty("OutputWorkspace", outputIvsQ);
  groupAlg->execute();

  // If this is a multiperiod workspace and we have polarization corrections
  // enabled
  if (isPolarizationCorrectionOn) {
    if (group->isMultiperiod()) {
      // Perform polarization correction over the IvsLam group
      Algorithm_sptr polAlg =
          this->createChildAlgorithm("PolarizationCorrection");
      polAlg->setChild(false);
      polAlg->setRethrows(true);

      polAlg->setProperty("InputWorkspace", outputIvsLam);
      polAlg->setProperty("OutputWorkspace", outputIvsLam);
      polAlg->setProperty("PolarizationAnalysis",
                          this->getPropertyValue("PolarizationAnalysis"));
      polAlg->setProperty("CPp", this->getPropertyValue(cppLabel()));
      polAlg->setProperty("CRho", this->getPropertyValue(crhoLabel()));
      polAlg->setProperty("CAp", this->getPropertyValue(cApLabel()));
      polAlg->setProperty("CAlpha", this->getPropertyValue(cAlphaLabel()));
      polAlg->execute();

      // Now we've overwritten the IvsLam workspaces, we'll need to recalculate
      // the IvsQ ones
      alg->setProperty("FirstTransmissionRun", "");
      alg->setProperty("SecondTransmissionRun", "");
      for (size_t i = 0; i < numMembers; ++i) {
        const std::string IvsQName =
            outputIvsQ + "_" + boost::lexical_cast<std::string>(i + 1);
        const std::string IvsLamName =
            outputIvsLam + "_" + boost::lexical_cast<std::string>(i + 1);
        alg->setProperty("InputWorkspace", IvsLamName);
        alg->setProperty("OutputWorkspace", IvsQName);
        alg->setProperty("CorrectionAlgorithm", "None");
        alg->setProperty("OutputWorkspaceWavelength", IvsLamName);
        alg->execute();
      }
    } else {
      g_log.warning("Polarization corrections can only be performed on "
                    "multiperiod workspaces.");
    }
  }

  // We finished successfully
  this->setPropertyValue("OutputWorkspace", outputIvsQ);
  this->setPropertyValue("OutputWorkspaceWavelength", outputIvsLam);
  setExecuted(true);
  notificationCenter().postNotification(
      new FinishedNotification(this, isExecuted()));
  return true;
}
/**
 * Determine the instrument from the various input parameters.
 *
 * @return The correct instrument.
 */
Instrument_const_sptr CreateChunkingFromInstrument::getInstrument() {
  // try the input workspace
  MatrixWorkspace_sptr inWS = getProperty(PARAM_IN_WKSP);
  if (inWS) {
    return inWS->getInstrument();
  }

  // temporary workspace to hang everything else off of
  MatrixWorkspace_sptr tempWS(new Workspace2D());
  // name of the instrument
  string instName = getPropertyValue(PARAM_INST_NAME);

  // see if there is an input file
  string filename = getPropertyValue(PARAM_IN_FILE);
  if (!filename.empty()) {
    string top_entry_name("entry"); // TODO make more flexible

    // get the instrument name from the filename
    size_t n = filename.rfind('/');
    if (n != std::string::npos) {
      std::string temp = filename.substr(n + 1, filename.size() - n - 1);
      n = temp.find('_');
      if (n != std::string::npos && n > 0) {
        instName = temp.substr(0, n);
      }
    }

    // read information from the nexus file itself
    try {
      NeXus::File nxsfile(filename);

      // get the run start time
      string start_time;
      nxsfile.openGroup(top_entry_name, "NXentry");
      nxsfile.readData("start_time", start_time);
      tempWS->mutableRun().addProperty(
          "run_start", DateAndTime(start_time).toISO8601String(), true);

      // get the instrument name
      nxsfile.openGroup("instrument", "NXinstrument");
      nxsfile.readData("name", instName);
      nxsfile.closeGroup();

      // Test if IDF exists in file, move on quickly if not
      nxsfile.openPath("instrument/instrument_xml");
      nxsfile.close();
      IAlgorithm_sptr loadInst =
          createChildAlgorithm("LoadIDFFromNexus", 0.0, 0.2);
      // Now execute the Child Algorithm. Catch and log any error, but don't
      // stop.
      try {
        loadInst->setPropertyValue("Filename", filename);
        loadInst->setProperty<MatrixWorkspace_sptr>("Workspace", tempWS);
        loadInst->setPropertyValue("InstrumentParentPath", top_entry_name);
        loadInst->execute();
      } catch (std::invalid_argument &) {
        g_log.error("Invalid argument to LoadIDFFromNexus Child Algorithm ");
      } catch (std::runtime_error &) {
        g_log.debug("No instrument definition found in " + filename + " at " +
                    top_entry_name + "/instrument");
      }

      if (loadInst->isExecuted())
        return tempWS->getInstrument();
      else
        g_log.information("No IDF loaded from Nexus file.");

    } catch (::NeXus::Exception &) {
      g_log.information("No instrument definition found in " + filename +
                        " at " + top_entry_name + "/instrument");
    }
  }

  // run LoadInstrument if other methods have not run
  string instFilename = getPropertyValue(PARAM_INST_FILE);

  Algorithm_sptr childAlg = createChildAlgorithm("LoadInstrument", 0.0, 0.2);
  childAlg->setProperty<MatrixWorkspace_sptr>("Workspace", tempWS);
  childAlg->setPropertyValue("Filename", instFilename);
  childAlg->setPropertyValue("InstrumentName", instName);
  childAlg->executeAsChildAlg();
  return tempWS->getInstrument();
}
/** Process groups. Groups are processed differently depending on transmission
 * runs and polarization analysis. If transmission run is a matrix workspace, it
 * will be applied to each of the members in the input workspace group. If
 * transmission run is a workspace group, the behaviour is different depending
 * on polarization analysis. If polarization analysis is off (i.e.
 * 'PolarizationAnalysis' is set to 'None') each item in the transmission group
 * is associated with the corresponding item in the input workspace group. If
 * polarization analysis is on (i.e. 'PolarizationAnalysis' is 'PA' or 'PNR')
 * items in the transmission group will be summed to produce a matrix workspace
 * that will be applied to each of the items in the input workspace group. See
 * documentation of this algorithm for more details.
*/
bool ReflectometryReductionOneAuto2::processGroups() {
  // this algorithm effectively behaves as MultiPeriodGroupAlgorithm
  m_usingBaseProcessGroups = true;

  // Get our input workspace group
  auto group = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
      getPropertyValue("InputWorkspace"));
  // Get name of IvsQ workspace (native binning)
  const std::string outputIvsQ = getPropertyValue("OutputWorkspace");
  // Get name of IvsQ (native binning) workspace
  const std::string outputIvsQBinned =
      getPropertyValue("OutputWorkspaceBinned");
  // Get name of IvsLam workspace
  const std::string outputIvsLam =
      getPropertyValue("OutputWorkspaceWavelength");

  // Create a copy of ourselves
  Algorithm_sptr alg =
      createChildAlgorithm(name(), -1, -1, isLogging(), version());
  alg->setChild(false);
  alg->setRethrows(true);

  // Copy all the non-workspace properties over
  const std::vector<Property *> props = getProperties();
  for (auto &prop : props) {
    if (prop) {
      IWorkspaceProperty *wsProp = dynamic_cast<IWorkspaceProperty *>(prop);
      if (!wsProp)
        alg->setPropertyValue(prop->name(), prop->value());
    }
  }

  const bool polarizationAnalysisOn =
      getPropertyValue("PolarizationAnalysis") != "None";

  // Check if the transmission runs are groups or not

  const std::string firstTrans = getPropertyValue("FirstTransmissionRun");
  WorkspaceGroup_sptr firstTransG;
  MatrixWorkspace_sptr firstTransSum;
  if (!firstTrans.empty()) {
    auto firstTransWS =
        AnalysisDataService::Instance().retrieveWS<Workspace>(firstTrans);
    firstTransG = boost::dynamic_pointer_cast<WorkspaceGroup>(firstTransWS);
    if (!firstTransG) {
      alg->setProperty("FirstTransmissionRun", firstTrans);
    } else if (polarizationAnalysisOn) {
      firstTransSum = sumTransmissionWorkspaces(firstTransG);
    }
  }
  const std::string secondTrans = getPropertyValue("SecondTransmissionRun");
  WorkspaceGroup_sptr secondTransG;
  MatrixWorkspace_sptr secondTransSum;
  if (!secondTrans.empty()) {
    auto secondTransWS =
        AnalysisDataService::Instance().retrieveWS<Workspace>(secondTrans);
    secondTransG = boost::dynamic_pointer_cast<WorkspaceGroup>(secondTransWS);
    if (!secondTransG) {
      alg->setProperty("SecondTransmissionRun", secondTrans);
    } else if (polarizationAnalysisOn) {
      secondTransSum = sumTransmissionWorkspaces(secondTransG);
    }
  }

  std::vector<std::string> IvsQGroup, IvsQUnbinnedGroup, IvsLamGroup;

  // Execute algorithm over each group member
  for (size_t i = 0; i < group->size(); ++i) {

    const std::string IvsQName = outputIvsQ + "_" + std::to_string(i + 1);
    const std::string IvsQBinnedName =
        outputIvsQBinned + "_" + std::to_string(i + 1);
    const std::string IvsLamName = outputIvsLam + "_" + std::to_string(i + 1);

    if (firstTransG) {
      if (!polarizationAnalysisOn)
        alg->setProperty("FirstTransmissionRun",
                         firstTransG->getItem(i)->getName());
      else
        alg->setProperty("FirstTransmissionRun", firstTransSum);
    }
    if (secondTransG) {
      if (!polarizationAnalysisOn)
        alg->setProperty("SecondTransmissionRun",
                         secondTransG->getItem(i)->getName());
      else
        alg->setProperty("SecondTransmissionRun", secondTransSum);
    }

    alg->setProperty("InputWorkspace", group->getItem(i)->getName());
    alg->setProperty("OutputWorkspace", IvsQName);
    alg->setProperty("OutputWorkspaceBinned", IvsQBinnedName);
    alg->setProperty("OutputWorkspaceWavelength", IvsLamName);
    alg->execute();

    IvsQGroup.push_back(IvsQName);
    IvsQUnbinnedGroup.push_back(IvsQBinnedName);
    IvsLamGroup.push_back(IvsLamName);
  }

  // Group the IvsQ and IvsLam workspaces
  Algorithm_sptr groupAlg = createChildAlgorithm("GroupWorkspaces");
  groupAlg->setChild(false);
  groupAlg->setRethrows(true);
  groupAlg->setProperty("InputWorkspaces", IvsLamGroup);
  groupAlg->setProperty("OutputWorkspace", outputIvsLam);
  groupAlg->execute();
  groupAlg->setProperty("InputWorkspaces", IvsQGroup);
  groupAlg->setProperty("OutputWorkspace", outputIvsQ);
  groupAlg->execute();
  groupAlg->setProperty("InputWorkspaces", IvsQUnbinnedGroup);
  groupAlg->setProperty("OutputWorkspace", outputIvsQBinned);
  groupAlg->execute();

  // Set other properties so they can be updated in the Reflectometry interface
  setPropertyValue("ThetaIn", alg->getPropertyValue("ThetaIn"));
  setPropertyValue("MomentumTransferMin",
                   alg->getPropertyValue("MomentumTransferMin"));
  setPropertyValue("MomentumTransferMax",
                   alg->getPropertyValue("MomentumTransferMax"));
  setPropertyValue("MomentumTransferStep",
                   alg->getPropertyValue("MomentumTransferStep"));
  setPropertyValue("ScaleFactor", alg->getPropertyValue("ScaleFactor"));

  if (!polarizationAnalysisOn) {
    // No polarization analysis. Reduction stops here
    setPropertyValue("OutputWorkspace", outputIvsQ);
    setPropertyValue("OutputWorkspaceBinned", outputIvsQBinned);
    setPropertyValue("OutputWorkspaceWavelength", outputIvsLam);
    return true;
  }

  if (!group->isMultiperiod()) {
    g_log.warning("Polarization corrections can only be performed on "
                  "multiperiod workspaces.");
    setPropertyValue("OutputWorkspace", outputIvsQ);
    setPropertyValue("OutputWorkspaceBinned", outputIvsQBinned);
    setPropertyValue("OutputWorkspaceWavelength", outputIvsLam);
    return true;
  }

  Algorithm_sptr polAlg = createChildAlgorithm("PolarizationCorrection");
  polAlg->setChild(false);
  polAlg->setRethrows(true);
  polAlg->setProperty("InputWorkspace", outputIvsLam);
  polAlg->setProperty("OutputWorkspace", outputIvsLam);
  polAlg->setProperty("PolarizationAnalysis",
                      getPropertyValue("PolarizationAnalysis"));
  polAlg->setProperty("CPp", getPropertyValue("CPp"));
  polAlg->setProperty("CRho", getPropertyValue("CRho"));
  polAlg->setProperty("CAp", getPropertyValue("CAp"));
  polAlg->setProperty("CAlpha", getPropertyValue("CAlpha"));
  polAlg->execute();

  // Now we've overwritten the IvsLam workspaces, we'll need to recalculate
  // the IvsQ ones
  alg->setProperty("FirstTransmissionRun", "");
  alg->setProperty("SecondTransmissionRun", "");
  alg->setProperty("CorrectionAlgorithm", "None");
  alg->setProperty("ThetaIn", Mantid::EMPTY_DBL());
  alg->setProperty("ProcessingInstructions", "0");
  for (size_t i = 0; i < group->size(); ++i) {
    const std::string IvsQName = outputIvsQ + "_" + std::to_string(i + 1);
    const std::string IvsQBinnedName =
        outputIvsQBinned + "_" + std::to_string(i + 1);
    const std::string IvsLamName = outputIvsLam + "_" + std::to_string(i + 1);
    alg->setProperty("InputWorkspace", IvsLamName);
    alg->setProperty("OutputWorkspace", IvsQName);
    alg->setProperty("OutputWorkspaceBinned", IvsQBinnedName);
    alg->setProperty("OutputWorkspaceWavelength", IvsLamName);
    alg->execute();
  }

  setPropertyValue("OutputWorkspace", outputIvsQ);
  setPropertyValue("OutputWorkspaceBinned", outputIvsQBinned);
  setPropertyValue("OutputWorkspaceWavelength", outputIvsLam);

  return true;
}
/*
 Executes the underlying algorithm to create the MVP model.
 @param factory : visualisation factory to use.
 @param loadingProgressUpdate : Handler for GUI updates while algorithm
 progresses.
 @param drawingProgressUpdate : Handler for GUI updates while
 vtkDataSetFactory::create occurs.
 */
vtkSmartPointer<vtkDataSet>
EventNexusLoadingPresenter::execute(vtkDataSetFactory *factory,
                                    ProgressAction &loadingProgressUpdate,
                                    ProgressAction &drawingProgressUpdate) {
  using namespace Mantid::API;
  using namespace Mantid::Geometry;

  this->m_view->getLoadInMemory(); // TODO, nexus reader algorithm currently has
                                   // no use of this.

  if (this->shouldLoad()) {
    Poco::NObserver<ProgressAction,
                    Mantid::API::Algorithm::ProgressNotification>
        observer(loadingProgressUpdate, &ProgressAction::handler);
    AnalysisDataService::Instance().remove("MD_EVENT_WS_ID");

    Algorithm_sptr loadAlg =
        AlgorithmManager::Instance().createUnmanaged("LoadEventNexus");
    loadAlg->initialize();
    loadAlg->setChild(true);
    loadAlg->setPropertyValue("Filename", this->m_filename);
    loadAlg->setPropertyValue("OutputWorkspace", "temp_ws");
    loadAlg->addObserver(observer);
    loadAlg->executeAsChildAlg();
    loadAlg->removeObserver(observer);

    Workspace_sptr temp = loadAlg->getProperty("OutputWorkspace");
    IEventWorkspace_sptr tempWS =
        boost::dynamic_pointer_cast<IEventWorkspace>(temp);

    Algorithm_sptr convertAlg = AlgorithmManager::Instance().createUnmanaged(
        "ConvertToDiffractionMDWorkspace", 1);
    convertAlg->initialize();
    convertAlg->setChild(true);
    convertAlg->setProperty("InputWorkspace", tempWS);
    convertAlg->setProperty<bool>("ClearInputWorkspace", false);
    convertAlg->setProperty<bool>("LorentzCorrection", true);
    convertAlg->setPropertyValue("OutputWorkspace", "converted_ws");
    convertAlg->addObserver(observer);
    convertAlg->executeAsChildAlg();
    convertAlg->removeObserver(observer);

    IMDEventWorkspace_sptr outWS = convertAlg->getProperty("OutputWorkspace");
    AnalysisDataService::Instance().addOrReplace("MD_EVENT_WS_ID", outWS);
  }

  Workspace_sptr result =
      AnalysisDataService::Instance().retrieve("MD_EVENT_WS_ID");
  Mantid::API::IMDEventWorkspace_sptr eventWs =
      boost::dynamic_pointer_cast<Mantid::API::IMDEventWorkspace>(result);
  m_wsTypeName = eventWs->id();

  factory->setRecursionDepth(this->m_view->getRecursionDepth());
  auto visualDataSet = factory->oneStepCreate(
      eventWs, drawingProgressUpdate); // HACK: progressUpdate should be
                                       // argument for drawing!

  this->extractMetadata(*eventWs);
  this->appendMetadata(visualDataSet, eventWs->getName());

  return visualDataSet;
}
Beispiel #29
0
void Load::loadMultipleFiles() {
  // allFilenames contains "rows" of filenames. If the row has more than 1 file
  // in it
  // then that row is to be summed across each file in the row
  const std::vector<std::vector<std::string>> allFilenames =
      getProperty("Filename");
  std::string outputWsName = getProperty("OutputWorkspace");

  std::vector<std::string> wsNames(allFilenames.size());
  std::transform(allFilenames.begin(), allFilenames.end(), wsNames.begin(),
                 generateWsNameFromFileNames);

  auto wsName = wsNames.cbegin();
  assert(allFilenames.size() == wsNames.size());

  std::vector<API::Workspace_sptr> loadedWsList;
  loadedWsList.reserve(allFilenames.size());

  Workspace_sptr tempWs;

  // Cycle through the filenames and wsNames.
  for (auto filenames = allFilenames.cbegin(); filenames != allFilenames.cend();
       ++filenames, ++wsName) {
    auto filename = filenames->cbegin();
    Workspace_sptr sumWS = loadFileToWs(*filename, *wsName);

    ++filename;
    for (; filename != filenames->cend(); ++filename) {
      tempWs = loadFileToWs(*filename, "__@loadsum_temp@");
      sumWS = plusWs(sumWS, tempWs);
    }

    API::WorkspaceGroup_sptr group =
        boost::dynamic_pointer_cast<WorkspaceGroup>(sumWS);
    if (group) {
      std::vector<std::string> childWsNames = group->getNames();
      auto childWsName = childWsNames.begin();
      size_t count = 1;
      for (; childWsName != childWsNames.end(); ++childWsName, ++count) {
        Workspace_sptr childWs = group->getItem(*childWsName);
        const std::string childName =
            group->getName() + "_" + std::to_string(count);
        API::AnalysisDataService::Instance().addOrReplace(childName, childWs);
        // childWs->setName(group->getName() + "_" +
        // boost::lexical_cast<std::string>(count));
      }
    }
    // Add the sum to the list of loaded workspace names.
    loadedWsList.push_back(sumWS);
  }

  // If we only have one loaded ws, set it as the output.
  if (loadedWsList.size() == 1) {
    setProperty("OutputWorkspace", loadedWsList[0]);
    AnalysisDataService::Instance().rename(loadedWsList[0]->getName(),
                                           outputWsName);
  }
  // Else we have multiple loaded workspaces - group them and set the group as
  // output.
  else {
    API::WorkspaceGroup_sptr group = groupWsList(loadedWsList);
    setProperty("OutputWorkspace", group);

    std::vector<std::string> childWsNames = group->getNames();
    size_t count = 1;
    for (auto &childWsName : childWsNames) {
      if (childWsName == outputWsName) {
        Mantid::API::Workspace_sptr child = group->getItem(childWsName);
        // child->setName(child->getName() + "_" +
        // boost::lexical_cast<std::string>(count));
        const std::string childName =
            child->getName() + "_" + std::to_string(count);
        API::AnalysisDataService::Instance().addOrReplace(childName, child);
        count++;
      }
    }

    childWsNames = group->getNames();
    count = 1;
    for (auto &childWsName : childWsNames) {
      Workspace_sptr childWs = group->getItem(childWsName);
      std::string outWsPropName = "OutputWorkspace_" + std::to_string(count);
      ++count;
      declareProperty(Kernel::make_unique<WorkspaceProperty<Workspace>>(
          outWsPropName, childWsName, Direction::Output));
      setProperty(outWsPropName, childWs);
    }
  }

  // Clean up.
  if (tempWs) {
    Algorithm_sptr alg =
        AlgorithmManager::Instance().createUnmanaged("DeleteWorkspace");
    alg->initialize();
    alg->setChild(true);
    alg->setProperty("Workspace", tempWs);
    alg->execute();
  }
}
Beispiel #30
0
/**
 * Set MDFrames for workspaces from legacy files
 * @param ws:: poitner to the workspace which needs to be corrected
 */
void LoadMD::setMDFrameOnWorkspaceFromLegacyFile(API::IMDWorkspace_sptr ws) {

  g_log.information()
      << "LoadMD: Encountered a legacy file which has a mismatch between "
         "its MDFrames and its Special Coordinate System. "
         "Attempting to convert MDFrames.\n";
  auto numberOfDimensions = ws->getNumDims();

  // Select an MDFrame based on the special coordinates.
  // Note that for None, we select a General Coordinate System,
  // unless the name is "Unknown frame"
  std::string selectedFrame;

  switch (m_coordSystem) {
  case Mantid::Kernel::QLab:
    selectedFrame = Mantid::Geometry::QLab::QLabName;
    break;
  case Mantid::Kernel::QSample:
    selectedFrame = Mantid::Geometry::QSample::QSampleName;
    break;
  case Mantid::Kernel::HKL:
    selectedFrame = Mantid::Geometry::HKL::HKLName;
    break;
  default:
    selectedFrame = Mantid::Geometry::GeneralFrame::GeneralFrameName;
  }

  // Get the old frames just in case something goes wrong. In this case we
  // reset the frames.

  std::vector<std::string> oldFrames(
      numberOfDimensions, Mantid::Geometry::GeneralFrame::GeneralFrameName);
  for (size_t index = 0; index < numberOfDimensions; ++index) {
    oldFrames[index] = ws->getDimension(index)->getMDFrame().name();
  }

  // We want to set only up to the first three dimensions to the selected Frame;
  // Everything else will be set to a General Frame
  std::vector<std::string> framesToSet(
      numberOfDimensions, Mantid::Geometry::GeneralFrame::GeneralFrameName);
  auto fillUpTo = numberOfDimensions > 3 ? 3 : numberOfDimensions;
  std::fill_n(framesToSet.begin(), fillUpTo, selectedFrame);

  try {
    // Set the MDFrames for each axes
    Algorithm_sptr setMDFrameAlg = this->createChildAlgorithm("SetMDFrame");
    int axesCounter = 0;
    for (auto &frame : framesToSet) {
      setMDFrameAlg->setProperty("InputWorkspace", ws);
      setMDFrameAlg->setProperty("MDFrame", frame);
      setMDFrameAlg->setProperty("Axes", std::vector<int>(1, axesCounter));
      ++axesCounter;
      setMDFrameAlg->executeAsChildAlg();
    }
  } catch (...) {
    g_log.warning() << "LoadMD: An issue occured while trying to correct "
                       "MDFrames. Trying to revert to original.\n";
    // Revert to the old frames.
    Algorithm_sptr setMDFrameAlg = this->createChildAlgorithm("SetMDFrame");
    int axesCounter = 0;
    for (auto &oldFrame : oldFrames) {
      setMDFrameAlg->setProperty("InputWorkspace", ws);
      setMDFrameAlg->setProperty("MDFrame", oldFrame);
      setMDFrameAlg->setProperty("Axes", std::vector<int>(1, axesCounter));
      ++axesCounter;
      setMDFrameAlg->executeAsChildAlg();
    }
  }
}