Exemplo n.º 1
0
void MDHistoToWorkspace2D::exec() {
  IMDHistoWorkspace_sptr inWS =
      IMDHistoWorkspace_sptr(getProperty("InputWorkspace"));

  rank = inWS->getNumDims();
  size_t nSpectra = calculateNSpectra(inWS);
  std::cout << "nSpectra = " << nSpectra << std::endl;

  boost::shared_ptr<const IMDDimension> lastDim = inWS->getDimension(rank - 1);
  std::cout << "spectraLength = " << lastDim->getNBins() << std::endl;

  Mantid::DataObjects::Workspace2D_sptr outWS;
  outWS = boost::dynamic_pointer_cast<Mantid::DataObjects::Workspace2D>(
      WorkspaceFactory::Instance().create(
          "Workspace2D", nSpectra, lastDim->getNBins(), lastDim->getNBins()));
  outWS->setYUnit("Counts");

  coord_t *pos = (coord_t *)malloc(rank * sizeof(coord_t));
  memset(pos, 0, rank * sizeof(coord_t));
  currentSpectra = 0;
  recurseData(inWS, outWS, 0, pos);
  copyMetaData(inWS, outWS);

  // checkW2D(outWS);

  setProperty("OutputWorkspace", boost::dynamic_pointer_cast<Workspace>(outWS));
}
Exemplo n.º 2
0
void LoadFlexiNexus::load2DWorkspace(NeXus::File *fin) {
  Mantid::DataObjects::Workspace2D_sptr ws;
  int nSpectra, spectraLength;
  std::vector<int> data;

  // read the data first
  fin->getDataCoerce(data);

  Info inf = fin->getInfo();
  if (inf.dims.size() == 1) {
    nSpectra = 1;
    spectraLength = static_cast<int>(inf.dims[0]);
  } else {
    nSpectra = static_cast<int>(inf.dims[0]);
    spectraLength = static_cast<int>(inf.dims[1]);
  }

  g_log.debug() << "Reading " << nSpectra << " spectra of length "
                << spectraLength << "." << std::endl;

  // need to locate x-axis data too.....
  std::map<std::string, std::string>::const_iterator it;
  std::vector<double> xData;
  if ((it = dictionary.find("x-axis")) == dictionary.end()) {
    xData.resize(spectraLength);
    for (int i = 0; i < spectraLength; i++) {
      xData[i] = (double)i;
    }
  } else {
    if (safeOpenpath(fin, it->second)) {
      fin->getDataCoerce(xData);
    }
  }

  // need to locate y-axis data too.....
  std::vector<double> yData;
  if ((it = dictionary.find("y-axis")) == dictionary.end()) {
    yData.resize(nSpectra);
    for (int i = 0; i < nSpectra; i++) {
      yData[i] = (double)i;
    }
  } else {
    if (safeOpenpath(fin, it->second)) {
      fin->getDataCoerce(yData);
    }
  }

  // fill the data.......
  ws = boost::dynamic_pointer_cast<Mantid::DataObjects::Workspace2D>(
      WorkspaceFactory::Instance().create("Workspace2D", nSpectra,
                                          spectraLength, spectraLength));
  for (int wsIndex = 0; wsIndex < nSpectra; wsIndex++) {
    Mantid::MantidVec &Y = ws->dataY(wsIndex);
    for (int j = 0; j < spectraLength; j++) {
      Y[j] = data[spectraLength * wsIndex + j];
    }
    // Create and fill another vector for the errors, containing sqrt(count)
    Mantid::MantidVec &E = ws->dataE(wsIndex);
    std::transform(Y.begin(), Y.end(), E.begin(), dblSqrt);
    ws->setX(wsIndex, xData);
    // Xtof		ws->getAxis(1)->spectraNo(i)= i;
    ws->getSpectrum(wsIndex)
        ->setSpectrumNo(static_cast<specid_t>(yData[wsIndex]));
    ws->getSpectrum(wsIndex)
        ->setDetectorID(static_cast<detid_t>(yData[wsIndex]));
  }

  ws->setYUnit("Counts");

  // assign an x-axis-name
  if ((it = dictionary.find("x-axis-name")) == dictionary.end()) {
    const std::string xname("no axis name found");
    ws->getAxis(0)->title() = xname;
  } else {
    const std::string xname(it->second);
    ws->getAxis(0)->title() = xname;
    if (xname.compare("TOF") == 0) {
      g_log.debug() << "Setting X-unit to be TOF" << std::endl;
      ws->getAxis(0)->setUnit("TOF");
    }
  }

  addMetaData(fin, ws, (ExperimentInfo_sptr)ws);

  // assign the workspace
  setProperty("OutputWorkspace", boost::dynamic_pointer_cast<Workspace>(ws));
}