/**
 * Get the viewable peaks. Essentially copied from the slice viewer.
 * @returns A vector indicating which of the peaks are viewable.
 */
std::vector<bool> ConcretePeaksPresenterVsi::getViewablePeaks() const {
  // Need to apply a transform.
  // Don't bother to find peaks in the region if there are no peaks to find.
  Mantid::API::ITableWorkspace_sptr outTable;

  if (this->m_peaksWorkspace->getNumberPeaks() >= 1) {
    double effectiveRadius = 1e-2;
    std::string viewable = m_viewableRegion->toExtentsAsString();
    Mantid::API::IPeaksWorkspace_sptr peaksWS = m_peaksWorkspace;

    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("PeaksInRegion");
    alg->setChild(true);
    alg->setRethrows(true);
    alg->initialize();
    alg->setProperty("InputWorkspace", peaksWS);
    alg->setProperty("OutputWorkspace", peaksWS->name() + "_peaks_in_region");
    alg->setProperty("Extents", viewable);
    alg->setProperty("CheckPeakExtents", true);
    alg->setProperty("PeakRadius", effectiveRadius);
    alg->setPropertyValue("CoordinateFrame", m_frame);
    alg->execute();
    outTable = alg->getProperty("OutputWorkspace");
    std::vector<bool> viewablePeaks(outTable->rowCount());
    for (size_t i = 0; i < outTable->rowCount(); ++i) {
      viewablePeaks[i] = outTable->cell<Mantid::API::Boolean>(i, 1);
    }
    m_viewablePeaks = viewablePeaks;
  } else {
    // No peaks will be viewable
    m_viewablePeaks = std::vector<bool>();
  }

  return m_viewablePeaks;
}
Esempio n. 2
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;
}
Esempio n. 3
0
    /** This method executes the ListInstruments algorithm
     * and fills the instrument box with instrument lists returned by ICat API
     * @return shared pointer to workspace which contains instrument names
     */
    std::vector<std::string> ICatUtils::executeListInstruments()
    {
      QString algName("CatalogListInstruments");
      const int version=-1;
      Mantid::API::IAlgorithm_sptr alg;
      try
      {
        alg = Mantid::API::AlgorithmManager::Instance().create(algName.toStdString(),version);
      }
      catch(...)
      {
        throw std::runtime_error("Error when Populating the instrument list box");

      }

      Poco::ActiveResult<bool> result(alg->executeAsync());
      while( !result.available() )
      {
        QCoreApplication::processEvents();
      }
      if(!alg->isExecuted())
      {
        //if the algorithm failed check the session id passed is valid
        if(!isSessionValid(alg))
        {
          //at this point session is invalid, popup loginbox to login
          if(login())
          {
            //now populate instrument box
            std::vector<std::string> instruments =executeListInstruments();
            return instruments;
          }
          else
          {
            throw std::runtime_error("Please Login to the information catalog using the login menu provided to do the investigation search.");
          }
        }
        else
        {
          return std::vector<std::string>();
        }
      }
      std::vector<std::string>instrlist;
      try
      {

        instrlist= alg->getProperty("InstrumentList");
      }
      catch (Mantid::Kernel::Exception::NotFoundError&)
      {
        throw;
      }
      return instrlist;

    }
Esempio n. 4
0
    bool ICatUtils::isSessionValid(const Mantid::API::IAlgorithm_sptr& alg)
    {
      try
      {
        return  alg->getProperty("IsValid");
      }
      catch (Mantid::Kernel::Exception::NotFoundError&)
      {
        throw;
      }

    }
Esempio n. 5
0
/**
 * Create a list of files from the given algorithm property.
 */
void FindFilesThread::getFilesFromAlgorithm() {
  Mantid::API::IAlgorithm_sptr algorithm =
      Mantid::API::AlgorithmManager::Instance().createUnmanaged(
          m_algorithm.toStdString());

  if (!algorithm)
    throw std::invalid_argument("Cannot create algorithm " +
                                m_algorithm.toStdString() + ".");

  algorithm->initialize();
  const std::string propName = m_property.toStdString();
  algorithm->setProperty(propName, m_text);

  Property *prop = algorithm->getProperty(propName);
  m_valueForProperty = QString::fromStdString(prop->value());

  FileProperty *fileProp = dynamic_cast<FileProperty *>(prop);
  MultipleFileProperty *multiFileProp =
      dynamic_cast<MultipleFileProperty *>(prop);

  if (fileProp) {
    m_filenames.push_back(fileProp->value());
  } else if (multiFileProp) {
    // This flattens any summed files to a set of single files so that you lose
    // the information about
    // what was summed
    std::vector<std::vector<std::string>> filenames =
        algorithm->getProperty(propName);
    std::vector<std::string> flattenedNames =
        VectorHelper::flattenVector(filenames);

    for (auto filename = flattenedNames.begin();
         filename != flattenedNames.end(); ++filename) {
      m_filenames.push_back(*filename);
    }
  }
}
/**
  @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;
}
Esempio n. 7
0
/**
* Create a list of file extensions from the given algorithm
* @param algName :: The name of the algorithm
* @param propName :: The name of the property
* @returns A list of file extensions
*/
QStringList
MWRunFiles::getFileExtensionsFromAlgorithm(const QString &algName,
                                           const QString &propName) {
  Mantid::API::IAlgorithm_sptr algorithm =
      Mantid::API::AlgorithmManager::Instance().createUnmanaged(
          algName.toStdString());
  QStringList fileExts;
  if (!algorithm)
    return fileExts;
  algorithm->initialize();
  Property *prop = algorithm->getProperty(propName.toStdString());
  FileProperty *fileProp = dynamic_cast<FileProperty *>(prop);
  MultipleFileProperty *multiFileProp =
      dynamic_cast<MultipleFileProperty *>(prop);

  std::vector<std::string> allowed;
  QString preferredExt;

  if (fileProp) {
    allowed = fileProp->allowedValues();
    preferredExt = QString::fromStdString(fileProp->getDefaultExt());
  } else if (multiFileProp) {
    allowed = multiFileProp->allowedValues();
    preferredExt = QString::fromStdString(multiFileProp->getDefaultExt());
  } else {
    return fileExts;
  }

  std::vector<std::string>::const_iterator iend = allowed.end();
  int index(0);
  for (std::vector<std::string>::const_iterator it = allowed.begin();
       it != iend; ++it) {
    if (!it->empty()) {
      QString ext = QString::fromStdString(*it);
      fileExts.append(ext);
      if (ext == preferredExt) {
        fileExts.move(index, 0);
      }
      ++index;
    }
  }

  return fileExts;
}
Esempio n. 8
0
std::vector<size_t>
ConcretePeaksPresenter::findVisiblePeakIndexes(const PeakBoundingBox &box) {
  std::vector<size_t> indexes;
  // Don't bother to find peaks in the region if there are no peaks to find.
  if (this->m_peaksWS->getNumberPeaks() >= 1) {

    double radius =
        m_viewPeaks
            ->getRadius(); // Effective radius of each peak representation.

    Mantid::API::IPeaksWorkspace_sptr peaksWS =
        boost::const_pointer_cast<Mantid::API::IPeaksWorkspace>(
            this->m_peaksWS);

    PeakBoundingBox transformedViewableRegion = box.makeSliceBox(radius);

    transformedViewableRegion.transformBox(m_transform);

    Mantid::API::IAlgorithm_sptr alg =
        AlgorithmManager::Instance().create("PeaksInRegion");
    alg->setChild(true);
    alg->setRethrows(true);
    alg->initialize();
    alg->setProperty("InputWorkspace", peaksWS);
    alg->setProperty("OutputWorkspace", peaksWS->name() + "_peaks_in_region");
    alg->setProperty("Extents", transformedViewableRegion.toExtents());
    alg->setProperty("CheckPeakExtents", false); // consider all peaks as points
    alg->setProperty("PeakRadius", radius);
    alg->setPropertyValue("CoordinateFrame", m_transform->getFriendlyName());
    alg->execute();
    ITableWorkspace_sptr outTable = alg->getProperty("OutputWorkspace");

    for (size_t i = 0; i < outTable->rowCount(); ++i) {
      const bool insideRegion = outTable->cell<Boolean>(i, 1);
      if (insideRegion) {
        indexes.push_back(i);
      }
    }
  }
  return indexes;
}
Esempio n. 9
0
/**
 * Groups the workspace according to grouping provided.
 *
 * @param ws :: Workspace to group
 * @param  g :: The grouping information
 * @return Sptr to created grouped workspace
 */
MatrixWorkspace_sptr groupWorkspace(MatrixWorkspace_const_sptr ws, const Grouping& g)
{
  // As I couldn't specify multiple groups for GroupDetectors, I am going down quite a complicated
  // route - for every group distinct grouped workspace is created using GroupDetectors. These
  // workspaces are then merged into the output workspace.

  // Create output workspace
  MatrixWorkspace_sptr outWs =
    WorkspaceFactory::Instance().create(ws, g.groups.size(), ws->readX(0).size(), ws->blocksize());

  for(size_t gi = 0; gi < g.groups.size(); gi++)
  {
    Mantid::API::IAlgorithm_sptr alg = AlgorithmManager::Instance().create("GroupDetectors");
    alg->setChild(true); // So Output workspace is not added to the ADS
    alg->initialize();
    alg->setProperty("InputWorkspace", boost::const_pointer_cast<MatrixWorkspace>(ws));
    alg->setPropertyValue("SpectraList", g.groups[gi]);
    alg->setPropertyValue("OutputWorkspace", "grouped"); // Is not actually used, just to make validators happy
    alg->execute();

    MatrixWorkspace_sptr grouped = alg->getProperty("OutputWorkspace");

    // Copy the spectrum
    *(outWs->getSpectrum(gi)) = *(grouped->getSpectrum(0));

    // Update spectrum number
    outWs->getSpectrum(gi)->setSpectrumNo(static_cast<specid_t>(gi));

    // Copy to the output workspace
    outWs->dataY(gi) = grouped->readY(0);
    outWs->dataX(gi) = grouped->readX(0);
    outWs->dataE(gi) = grouped->readE(0);
  }

  return outWs;
}