Exemple #1
0
/** This method saves the search response( investigations )data to a table
 * workspace
 *  @param response :: const reference to response object
 *  @param outputws :: shared pointer to output workspace
 */
void CICatHelper::saveSearchRessults(
    const ns1__searchByAdvancedPaginationResponse &response,
    API::ITableWorkspace_sptr &outputws) {
  if (outputws->getColumnNames().empty()) {
    outputws->addColumn("str", "Investigation id");
    outputws->addColumn("str", "Facility");
    outputws->addColumn("str", "Title");
    outputws->addColumn("str", "Instrument");
    outputws->addColumn("str", "Run range");
    outputws->addColumn("str", "Start date");
    outputws->addColumn("str", "End date");
    outputws->addColumn("str", "SessionID");
  }
  saveInvestigations(response.return_, outputws);
}
Exemple #2
0
/**
 * Loops through the response vector and saves the datasets details to a table
 * workspace.
 * @param response :: A vector containing the results of the search query.
 * @param outputws :: Shared pointer to output workspace.
 */
void ICat4Catalog::saveDataSets(std::vector<xsd__anyType *> response,
                                API::ITableWorkspace_sptr &outputws) {
  if (outputws->getColumnNames().empty()) {
    // Add rows headers to the output workspace.
    outputws->addColumn("long64", "ID");
    outputws->addColumn("str", "Name");
    outputws->addColumn("str", "Description");
    outputws->addColumn("str", "Type");
    outputws->addColumn("str", "Related investigation ID");
    outputws->addColumn("size_t", "Number of datafiles");
  }

  std::string emptyCell;
  for (auto &iter : response) {
    ns1__dataset *dataset = dynamic_cast<ns1__dataset *>(iter);
    if (dataset) {
      API::TableRow table = outputws->appendRow();

      savetoTableWorkspace(dataset->id, table);
      savetoTableWorkspace(dataset->name, table);

      if (dataset->description)
        savetoTableWorkspace(dataset->description, table);
      else
        savetoTableWorkspace(&emptyCell, table);

      if (dataset->type)
        savetoTableWorkspace(dataset->type->name, table);
      else
        savetoTableWorkspace(&emptyCell, table);

      if (dataset->investigation)
        savetoTableWorkspace(dataset->investigation->name, table);
      else
        savetoTableWorkspace(&emptyCell, table);

      size_t datafileCount = dataset->datafiles.size();
      savetoTableWorkspace(&datafileCount, table);
    } else {
      throw std::runtime_error("ICat4Catalog::saveDataSets expected a dataset. "
                               "Please contact the Mantid development team.");
    }
  }
}
Exemple #3
0
    /** This method loops through the response return_vector and saves the datasets details to a table workspace
     * @param response :: const reference to response object
     * @param outputws ::  shred pointer to workspace
     * @returns shared pointer to table workspace which stores the data
     */
    void  CICatHelper::saveDataSets(const ns1__getInvestigationIncludesResponse& response,API::ITableWorkspace_sptr& outputws)
    {
      //create table workspace
      if (outputws->getColumnNames().empty())
      {
        outputws->addColumn("str","Name");//File name
        outputws->addColumn("str","Status");
        outputws->addColumn("str","Type");
        outputws->addColumn("str","Description");
        outputws->addColumn("long64","Sample Id");
      }
      try
      {

        std::vector<ns1__dataset*> datasetVec;
        datasetVec.assign((response.return_)->datasetCollection.begin(),(response.return_)->datasetCollection.end());

        std::vector<ns1__dataset*>::const_iterator dataset_citr;
        for(dataset_citr=datasetVec.begin();dataset_citr!=datasetVec.end();++dataset_citr)
        {
          API::TableRow t = outputws->appendRow();

          // DataSet Name
          savetoTableWorkspace((*dataset_citr)->name,t);
          // DataSet Status
          savetoTableWorkspace((*dataset_citr)->datasetStatus,t);
          //DataSet Type
          savetoTableWorkspace((*dataset_citr)->datasetType,t);
          //DataSet Type
          savetoTableWorkspace((*dataset_citr)->description,t);
          //DataSet Type
          savetoTableWorkspace((*dataset_citr)->sampleId,t);
        }
      }

      catch(std::runtime_error& )
      {
        throw;
      }

      //return outputws;
    }
Exemple #4
0
/**
 * Saves result from "getDataFiles" to workspace.
 * @param response :: result response from the catalog.
 * @param outputws :: shared pointer to datasets
 */
void ICat4Catalog::saveDataFiles(std::vector<xsd__anyType *> response,
                                 API::ITableWorkspace_sptr &outputws) {
  if (outputws->getColumnNames().empty()) {
    // Add rows headers to the output workspace.
    outputws->addColumn("str", "Name");
    outputws->addColumn("str", "Location");
    outputws->addColumn("str", "Create Time");
    outputws->addColumn("long64", "Id");
    outputws->addColumn("long64", "File size(bytes)");
    outputws->addColumn("str", "File size");
    outputws->addColumn("str", "Description");
  }

  std::vector<xsd__anyType *>::const_iterator iter;
  for (iter = response.begin(); iter != response.end(); ++iter) {
    ns1__datafile *datafile = dynamic_cast<ns1__datafile *>(*iter);
    if (datafile) {
      API::TableRow table = outputws->appendRow();
      // Now add the relevant investigation data to the table.
      savetoTableWorkspace(datafile->name, table);
      savetoTableWorkspace(datafile->location, table);

      std::string createDate =
          formatDateTime(*datafile->createTime, "%Y-%m-%d %H:%M:%S");
      savetoTableWorkspace(&createDate, table);

      savetoTableWorkspace(datafile->id, table);
      savetoTableWorkspace(datafile->fileSize, table);

      std::string fileSize = bytesToString(*datafile->fileSize);
      savetoTableWorkspace(&fileSize, table);

      if (datafile->description)
        savetoTableWorkspace(datafile->description, table);
    } else {
      throw std::runtime_error("ICat4Catalog::saveDataFiles expected a "
                               "datafile. Please contact the Mantid "
                               "development team.");
    }
  }
}
Exemple #5
0
/**
 * This method loops through the response return_vector and saves the datafile
 * details to a table workspace
 * @param response :: const reference to response object
 * @param outputws :: shared pointer to table workspace which stores the data
 */
void CICatHelper::saveInvestigationIncludesResponse(
    const ns1__getInvestigationIncludesResponse &response,
    API::ITableWorkspace_sptr &outputws) {
  if (outputws->getColumnNames().empty()) {
    outputws->addColumn("str", "Name");
    outputws->addColumn("str", "Location");
    outputws->addColumn("str", "Create Time");
    outputws->addColumn("long64", "Id");
    outputws->addColumn("long64", "File size(bytes)");
    outputws->addColumn("str", "File size");
    outputws->addColumn("str", "Description");
  }

  try {
    std::vector<ns1__dataset *> datasetVec;
    datasetVec.assign((response.return_)->datasetCollection.begin(),
                      (response.return_)->datasetCollection.end());
    if (datasetVec.empty()) {
      throw std::runtime_error("No data files exists in the ICAT database for "
                               "the selected investigation");
    }
    std::vector<ns1__dataset *>::const_iterator dataset_citr;
    for (dataset_citr = datasetVec.begin(); dataset_citr != datasetVec.end();
         ++dataset_citr) {
      std::vector<ns1__datafile *> datafileVec;
      datafileVec.assign((*dataset_citr)->datafileCollection.begin(),
                         (*dataset_citr)->datafileCollection.end());
      if (datafileVec.empty()) {
        throw std::runtime_error("No data files exists in the ICAT database "
                                 "for the selected  investigation ");
      }

      std::vector<ns1__datafile *>::const_iterator datafile_citr;
      for (datafile_citr = datafileVec.begin();
           datafile_citr != datafileVec.end(); ++datafile_citr) {

        API::TableRow t = outputws->appendRow();

        // File Name
        savetoTableWorkspace((*datafile_citr)->name, t);
        savetoTableWorkspace((*datafile_citr)->location, t);

        // File creation Time.
        std::string *creationtime = nullptr;
        if ((*datafile_citr)->datafileCreateTime != nullptr) {
          time_t crtime = *(*datafile_citr)->datafileCreateTime;
          char temp[25];
          strftime(temp, 25, "%Y-%b-%d %H:%M:%S", localtime(&crtime));
          std::string ftime(temp);
          creationtime = new std::string;
          creationtime->assign(ftime);
        }
        savetoTableWorkspace(creationtime, t);
        if (creationtime)
          delete creationtime;

        //
        savetoTableWorkspace((*datafile_citr)->id, t);

        LONG64 fileSize =
            boost::lexical_cast<LONG64>(*(*datafile_citr)->fileSize);
        savetoTableWorkspace(&fileSize, t);

        savetoTableWorkspace((*datafile_citr)->description, t);
      }
    }

  } catch (std::runtime_error &) {
    throw;
  }
}
/**
 * Calculates the EISF if the fit includes a Delta function
 * @param tableWs - The TableWorkspace to append the EISF calculation to
 */
void ConvolutionFitSequential::calculateEISF(
    API::ITableWorkspace_sptr &tableWs) {
  // Get height data from parameter table
  const auto columns = tableWs->getColumnNames();
  const auto height = searchForFitParams("Height", columns).at(0);
  const auto heightErr = searchForFitParams("Height_Err", columns).at(0);
  auto heightY = tableWs->getColumn(height)->numeric_fill<>();
  auto heightE = tableWs->getColumn(heightErr)->numeric_fill<>();

  // Get amplitude column names
  const auto ampNames = searchForFitParams("Amplitude", columns);
  const auto ampErrorNames = searchForFitParams("Amplitude_Err", columns);

  // For each lorentzian, calculate EISF
  size_t maxSize = ampNames.size();
  if (ampErrorNames.size() > maxSize) {
    maxSize = ampErrorNames.size();
  }
  for (size_t i = 0; i < maxSize; i++) {
    // Get amplitude from column in table workspace
    const auto ampName = ampNames.at(i);
    auto ampY = tableWs->getColumn(ampName)->numeric_fill<>();
    const auto ampErrorName = ampErrorNames.at(i);
    auto ampErr = tableWs->getColumn(ampErrorName)->numeric_fill<>();

    // Calculate EISF and EISF error
    // total = heightY + ampY
    auto total = cloneVector(heightY);
    std::transform(total.begin(), total.end(), ampY.begin(), total.begin(),
                   std::plus<double>());
    // eisfY = heightY / total
    auto eisfY = cloneVector(heightY);
    std::transform(eisfY.begin(), eisfY.end(), total.begin(), eisfY.begin(),
                   std::divides<double>());
    // heightE squared
    auto heightESq = cloneVector(heightE);
    heightESq = squareVector(heightESq);
    // ampErr squared
    auto ampErrSq = cloneVector(ampErr);
    ampErrSq = squareVector(ampErrSq);
    // totalErr = heightE squared + ampErr squared
    auto totalErr = cloneVector(heightESq);
    std::transform(totalErr.begin(), totalErr.end(), ampErrSq.begin(),
                   totalErr.begin(), std::plus<double>());
    // heightY squared
    auto heightYSq = cloneVector(heightY);
    heightYSq = squareVector(heightYSq);
    // total Squared
    auto totalSq = cloneVector(total);
    totalSq = squareVector(totalSq);
    // errOverTotalSq = totalErr / total squared
    auto errOverTotalSq = cloneVector(totalErr);
    std::transform(errOverTotalSq.begin(), errOverTotalSq.end(),
                   totalSq.begin(), errOverTotalSq.begin(),
                   std::divides<double>());
    // heightESqOverYSq = heightESq / heightYSq
    auto heightESqOverYSq = cloneVector(heightESq);
    std::transform(heightESqOverYSq.begin(), heightESqOverYSq.end(),
                   heightYSq.begin(), heightESqOverYSq.begin(),
                   std::divides<double>());
    // sqrtESqOverYSq = squareRoot( heightESqOverYSq )
    auto sqrtESqOverYSq = cloneVector(heightESqOverYSq);
    std::transform(sqrtESqOverYSq.begin(), sqrtESqOverYSq.end(),
                   sqrtESqOverYSq.begin(),
                   static_cast<double (*)(double)>(sqrt));
    // eisfYSumRoot = eisfY * sqrtESqOverYSq
    auto eisfYSumRoot = cloneVector(eisfY);
    std::transform(eisfYSumRoot.begin(), eisfYSumRoot.end(),
                   sqrtESqOverYSq.begin(), eisfYSumRoot.begin(),
                   std::multiplies<double>());
    // eisfErr = eisfYSumRoot + errOverTotalSq
    auto eisfErr = cloneVector(eisfYSumRoot);
    std::transform(eisfErr.begin(), eisfErr.end(), errOverTotalSq.begin(),
                   eisfErr.begin(), std::plus<double>());

    // Append the calculated values to the table workspace
    auto columnName =
        ampName.substr(0, (ampName.size() - std::string("Amplitude").size()));
    columnName += "EISF";
    auto errorColumnName = ampErrorName.substr(
        0, (ampErrorName.size() - std::string("Amplitude_Err").size()));
    errorColumnName += "EISF_Err";

    tableWs->addColumn("double", columnName);
    tableWs->addColumn("double", errorColumnName);
    size_t maxEisf = eisfY.size();
    if (eisfErr.size() > maxEisf) {
      maxEisf = eisfErr.size();
    }

    Column_sptr col = tableWs->getColumn(columnName);
    Column_sptr errCol = tableWs->getColumn(errorColumnName);
    for (size_t j = 0; j < maxEisf; j++) {
      col->cell<double>(j) = eisfY.at(j);
      errCol->cell<double>(j) = eisfErr.at(j);
    }
  }
}
Exemple #7
0
/**
 * Saves investigations to a table workspace.
 * @param response :: A vector containing the results of the search query.
 * @param outputws :: Shared pointer to output workspace.
 */
void ICat4Catalog::saveInvestigations(std::vector<xsd__anyType *> response,
                                      API::ITableWorkspace_sptr &outputws) {
  if (outputws->getColumnNames().empty()) {
    // Add rows headers to the output workspace.
    outputws->addColumn("long64", "DatabaseID");
    outputws->addColumn("str", "InvestigationID");
    outputws->addColumn("str", "Facility");
    outputws->addColumn("str", "Title");
    outputws->addColumn("str", "Instrument");
    outputws->addColumn("str", "Run range");
    outputws->addColumn("str", "Start date");
    outputws->addColumn("str", "End date");
    outputws->addColumn("str", "SessionID");
  }

  // Add data to each row in the output workspace.
  std::vector<xsd__anyType *>::const_iterator iter;
  for (iter = response.begin(); iter != response.end(); ++iter) {
    // Cast from xsd__anyType to subclass (xsd__string).
    ns1__investigation *investigation =
        dynamic_cast<ns1__investigation *>(*iter);
    if (investigation) {
      API::TableRow table = outputws->appendRow();
      // Used to insert an empty string into the cell if value does not exist.
      std::string emptyCell;

      // Now add the relevant investigation data to the table (They always
      // exist).
      savetoTableWorkspace(investigation->id, table);
      savetoTableWorkspace(investigation->name, table);
      savetoTableWorkspace(investigation->facility->name, table);
      savetoTableWorkspace(investigation->title, table);
      savetoTableWorkspace(
          investigation->investigationInstruments.at(0)->instrument->name,
          table);

      // Verify that the run parameters vector exist prior to doing anything.
      // Since some investigations may not have run parameters.
      if (!investigation->parameters.empty()) {
        savetoTableWorkspace(investigation->parameters[0]->stringValue, table);
      } else {
        savetoTableWorkspace(&emptyCell, table);
      }

      // Again, we need to check first if start and end date exist prior to
      // insertion.
      if (investigation->startDate) {
        std::string startDate =
            formatDateTime(*investigation->startDate, "%Y-%m-%d");
        savetoTableWorkspace(&startDate, table);
      } else {
        savetoTableWorkspace(&emptyCell, table);
      }

      if (investigation->endDate) {
        std::string endDate =
            formatDateTime(*investigation->endDate, "%Y-%m-%d");
        savetoTableWorkspace(&endDate, table);
      } else {
        savetoTableWorkspace(&emptyCell, table);
      }
      std::string sessionID = m_session->getSessionId();
      savetoTableWorkspace(&sessionID, table);
    } else {
      throw std::runtime_error("ICat4Catalog::saveInvestigations expected an "
                               "investigation. Please contact the Mantid "
                               "development team.");
    }
  }
}
/** Forms the quadrature phase signal (squashogram)
 * @param ws :: [input] workspace containing the measured spectra
 * @param phase :: [input] table workspace containing the detector phases
 * @param n0 :: [input] vector containing the normalization constants
 * @return :: workspace containing the quadrature phase signal
 */
API::MatrixWorkspace_sptr
PhaseQuadMuon::squash(const API::MatrixWorkspace_sptr &ws,
                      const API::ITableWorkspace_sptr &phase,
                      const std::vector<double> &n0) {

  // Poisson limit: below this number we consider we don't have enough
  // statistics
  // to apply sqrt(N). This is an arbitrary number used in the original code
  // provided by scientists
  const double poissonLimit = 30.;

  // Muon life time in microseconds
  const double muLife = PhysicalConstants::MuonLifetime * 1e6;

  const size_t nspec = ws->getNumberHistograms();

  if (n0.size() != nspec) {
    throw std::invalid_argument("Invalid normalization constants");
  }

  auto names = phase->getColumnNames();
  for (auto &name : names) {
    std::transform(name.begin(), name.end(), name.begin(), ::tolower);
  }
  auto phaseIndex = findName(phaseNames, names);
  auto asymmetryIndex = findName(asymmNames, names);

  // Get the maximum asymmetry
  double maxAsym = 0.;
  for (size_t h = 0; h < nspec; h++) {
    if (phase->Double(h, asymmetryIndex) > maxAsym &&
        phase->Double(h, asymmetryIndex) != ASYMM_ERROR) {
      maxAsym = phase->Double(h, asymmetryIndex);
    }
  }

  if (maxAsym == 0.0) {
    throw std::invalid_argument("Invalid detector asymmetries");
  }
  std::vector<bool> emptySpectrum;
  emptySpectrum.reserve(nspec);
  std::vector<double> aj, bj;
  {
    // Calculate coefficients aj, bj

    double sxx = 0.;
    double syy = 0.;
    double sxy = 0.;
    for (size_t h = 0; h < nspec; h++) {
      emptySpectrum.push_back(
          std::all_of(ws->y(h).begin(), ws->y(h).end(),
                      [](double value) { return value == 0.; }));
      if (!emptySpectrum[h]) {
        const double asym = phase->Double(h, asymmetryIndex) / maxAsym;
        const double phi = phase->Double(h, phaseIndex);
        const double X = n0[h] * asym * cos(phi);
        const double Y = n0[h] * asym * sin(phi);
        sxx += X * X;
        syy += Y * Y;
        sxy += X * Y;
      }
    }

    const double lam1 = 2 * syy / (sxx * syy - sxy * sxy);
    const double mu1 = 2 * sxy / (sxy * sxy - sxx * syy);
    const double lam2 = 2 * sxy / (sxy * sxy - sxx * syy);
    const double mu2 = 2 * sxx / (sxx * syy - sxy * sxy);
    for (size_t h = 0; h < nspec; h++) {
      if (emptySpectrum[h]) {
        aj.push_back(0.0);
        bj.push_back(0.0);
      } else {
        const double asym = phase->Double(h, asymmetryIndex) / maxAsym;
        const double phi = phase->Double(h, phaseIndex);
        const double X = n0[h] * asym * cos(phi);
        const double Y = n0[h] * asym * sin(phi);
        aj.push_back((lam1 * X + mu1 * Y) * 0.5);
        bj.push_back((lam2 * X + mu2 * Y) * 0.5);
      }
    }
  }

  const size_t npoints = ws->blocksize();
  // Create and populate output workspace
  API::MatrixWorkspace_sptr ows =
      API::WorkspaceFactory::Instance().create(ws, 2, npoints + 1, npoints);

  // X
  ows->setSharedX(0, ws->sharedX(0));
  ows->setSharedX(1, ws->sharedX(0));

  // Phase quadrature
  auto &realY = ows->mutableY(0);
  auto &imagY = ows->mutableY(1);
  auto &realE = ows->mutableE(0);
  auto &imagE = ows->mutableE(1);

  const auto xPointData = ws->histogram(0).points();
  // First X value
  const double X0 = xPointData.front();

  // calculate exponential decay outside of the loop
  std::vector<double> expDecay = xPointData.rawData();
  std::transform(expDecay.begin(), expDecay.end(), expDecay.begin(),
                 [X0, muLife](double x) { return exp(-(x - X0) / muLife); });

  for (size_t i = 0; i < npoints; i++) {
    for (size_t h = 0; h < nspec; h++) {
      if (!emptySpectrum[h]) {
        // (X,Y,E) with exponential decay removed
        const double X = ws->x(h)[i];
        const double exponential = n0[h] * exp(-(X - X0) / muLife);
        const double Y = ws->y(h)[i] - exponential;
        const double E =
            (ws->y(h)[i] > poissonLimit) ? ws->e(h)[i] : sqrt(exponential);

        realY[i] += aj[h] * Y;
        imagY[i] += bj[h] * Y;
        realE[i] += aj[h] * aj[h] * E * E;
        imagE[i] += bj[h] * bj[h] * E * E;
      }
    }
    realE[i] = sqrt(realE[i]);
    imagE[i] = sqrt(imagE[i]);

    // Regain exponential decay
    realY[i] /= expDecay[i];
    imagY[i] /= expDecay[i];
    realE[i] /= expDecay[i];
    imagE[i] /= expDecay[i];
  }

  // New Y axis label
  ows->setYUnit("Asymmetry");

  return ows;
}