Beispiel #1
0
/**
 * Loads a file into a *hidden* workspace.
 *
 * @param fileName :: file name to load.
 * @param wsName   :: workspace name, which will be prefixed by a "__"
 *
 * @returns a pointer to the loaded workspace
 */
API::Workspace_sptr Load::loadFileToWs(const std::string &fileName,
                                       const std::string &wsName) {
  Mantid::API::IAlgorithm_sptr loadAlg = createChildAlgorithm("Load", 1);

  // Get the list properties for the concrete loader load algorithm
  const std::vector<Kernel::Property *> &props = getProperties();

  // Loop through and set the properties on the Child Algorithm
  for (auto prop : props) {
    const std::string &propName = prop->name();

    if (this->existsProperty(propName)) {
      if (propName == "Filename") {
        loadAlg->setPropertyValue("Filename", fileName);
      } else if (propName == "OutputWorkspace") {
        loadAlg->setPropertyValue("OutputWorkspace", wsName);
      } else {
        loadAlg->setPropertyValue(propName, getPropertyValue(propName));
      }
    }
  }

  loadAlg->executeAsChildAlg();

  Workspace_sptr ws = loadAlg->getProperty("OutputWorkspace");
  // ws->setName(wsName);
  AnalysisDataService::Instance().addOrReplace(wsName, ws);
  return ws;
}
Beispiel #2
0
/**
 * Plus two workspaces together, "in place".
 *
 * @param ws1 :: The first workspace.
 * @param ws2 :: The second workspace.
 *
 * @returns a pointer to the result (the first workspace).
 */
API::Workspace_sptr Load::plusWs(Workspace_sptr ws1, Workspace_sptr ws2) {
  WorkspaceGroup_sptr group1 = boost::dynamic_pointer_cast<WorkspaceGroup>(ws1);
  WorkspaceGroup_sptr group2 = boost::dynamic_pointer_cast<WorkspaceGroup>(ws2);

  if (group1 && group2) {
    // If we're dealing with groups, then the child workspaces must be added
    // separately - setProperty
    // wont work otherwise.
    std::vector<std::string> group1ChildWsNames = group1->getNames();
    std::vector<std::string> group2ChildWsNames = group2->getNames();

    if (group1ChildWsNames.size() != group2ChildWsNames.size())
      throw std::runtime_error("Unable to add group workspaces with different "
                               "number of child workspaces.");

    auto group1ChildWsName = group1ChildWsNames.begin();
    auto group2ChildWsName = group2ChildWsNames.begin();

    for (; group1ChildWsName != group1ChildWsNames.end();
         ++group1ChildWsName, ++group2ChildWsName) {
      Workspace_sptr group1ChildWs = group1->getItem(*group1ChildWsName);
      Workspace_sptr group2ChildWs = group2->getItem(*group2ChildWsName);

      Mantid::API::IAlgorithm_sptr plusAlg = createChildAlgorithm("Plus", 1);
      plusAlg->setProperty<Workspace_sptr>("LHSWorkspace", group1ChildWs);
      plusAlg->setProperty<Workspace_sptr>("RHSWorkspace", group2ChildWs);
      plusAlg->setProperty<Workspace_sptr>("OutputWorkspace", group1ChildWs);
      plusAlg->executeAsChildAlg();
    }
  } else if (!group1 && !group2) {
    Mantid::API::IAlgorithm_sptr plusAlg = createChildAlgorithm("Plus", 1);
    plusAlg->setProperty<Workspace_sptr>("LHSWorkspace", ws1);
    plusAlg->setProperty<Workspace_sptr>("RHSWorkspace", ws2);
    plusAlg->setProperty<Workspace_sptr>("OutputWorkspace", ws1);
    plusAlg->executeAsChildAlg();
  } else {
    throw std::runtime_error(
        "Unable to add a group workspace to a non-group workspace");
  }

  return ws1;
}
/**
  @param  inname       Name of workspace containing peaks
  @param  params       optimized cell parameters
  @param  out          residuals from optimization
*/
void OptimizeLatticeForCellType::optLattice(std::string inname,
                                            std::vector<double> &params,
                                            double *out) {
  PeaksWorkspace_sptr ws = boost::dynamic_pointer_cast<PeaksWorkspace>(
      AnalysisDataService::Instance().retrieve(inname));
  const std::vector<Peak> &peaks = ws->getPeaks();
  size_t n_peaks = ws->getNumberPeaks();
  std::vector<V3D> q_vector;
  std::vector<V3D> hkl_vector;

  for (size_t i = 0; i < params.size(); i++)
    params[i] = std::abs(params[i]);
  for (size_t i = 0; i < n_peaks; i++) {
    q_vector.push_back(peaks[i].getQSampleFrame());
    hkl_vector.push_back(peaks[i].getHKL());
  }

  Mantid::API::IAlgorithm_sptr alg = createChildAlgorithm("CalculateUMatrix");
  alg->setPropertyValue("PeaksWorkspace", inname);
  alg->setProperty("a", params[0]);
  alg->setProperty("b", params[1]);
  alg->setProperty("c", params[2]);
  alg->setProperty("alpha", params[3]);
  alg->setProperty("beta", params[4]);
  alg->setProperty("gamma", params[5]);
  alg->executeAsChildAlg();

  ws = alg->getProperty("PeaksWorkspace");
  OrientedLattice latt = ws->mutableSample().getOrientedLattice();
  DblMatrix UB = latt.getUB();
  DblMatrix A = aMatrix(params);
  DblMatrix Bc = A;
  Bc.Invert();
  DblMatrix U1_B1 = UB * A;
  OrientedLattice o_lattice;
  o_lattice.setUB(U1_B1);
  DblMatrix U1 = o_lattice.getU();
  DblMatrix U1_Bc = U1 * Bc;

  for (size_t i = 0; i < hkl_vector.size(); i++) {
    V3D error = U1_Bc * hkl_vector[i] - q_vector[i] / (2.0 * M_PI);
    out[i] = error.norm2();
  }

  return;
}