/**
 * Populate spectra mapping to detector IDs
 *
 * @param workspace: Workspace2D object
 * @param nxbins: number of bins in X
 * @param nybins: number of bins in Y
 */
void SANSInstrumentCreationHelper::runLoadMappingTable(
    Mantid::DataObjects::Workspace2D_sptr workspace, int nxbins, int nybins) {
  // Get the number of monitor channels
  size_t nMonitors(0);
  size_t nXbins, nYbins;
  boost::shared_ptr<const Instrument> instrument = workspace->getInstrument();
  std::vector<detid_t> monitors = instrument->getMonitors();
  nMonitors = monitors.size();

  // Number of monitors should be consistent with data file format
  if (nMonitors != 2) {
    std::stringstream error;
    error << "Geometry error for " << instrument->getName()
          << ": Spice data format defines 2 monitors, " << nMonitors
          << " were/was found";
    throw std::runtime_error(error.str());
  }
  if (nxbins >= 0) {
    nXbins = size_t(nxbins);
  } else {
    throw std::invalid_argument("number of x-bins < 0");
  }
  if (nybins >= 0) {
    nYbins = size_t(nybins);
  } else {
    throw std::invalid_argument("number of y-bins < 0");
  }

  // Generate mapping of detector/channel IDs to spectrum No

  // Detector/channel counter
  size_t wi = 0;

  // Monitor: IDs start at 1 and increment by 1
  for (size_t i = 0; i < nMonitors; i++) {
    // std::cout << "SANS instrument monitor number " << i << '\n';
    workspace->getSpectrum(wi).setSpectrumNo(specnum_t(wi));
    workspace->getSpectrum(wi).setDetectorID(detid_t(wi + 1));
    wi++;
  }

  // Detector pixels
  for (size_t ix = 0; ix < nXbins; ix++) {
    for (size_t iy = 0; iy < nYbins; iy++) {
      workspace->getSpectrum(wi).setSpectrumNo(specnum_t(wi));
      workspace->getSpectrum(wi)
          .setDetectorID(detid_t(1000000 + iy * 1000 + ix));
      wi++;
    }
  }
}
void
MDHistoToWorkspace2D::checkW2D(Mantid::DataObjects::Workspace2D_sptr outWS) {
  size_t nSpectra = outWS->getNumberHistograms();
  size_t length = outWS->blocksize();
  MantidVec x, y, e;

  g_log.information() << "W2D has " << nSpectra << " histograms of length "
                      << length;
  for (size_t i = 0; i < nSpectra; i++) {
    ISpectrum *spec = outWS->getSpectrum(i);
    x = spec->dataX();
    y = spec->dataY();
    e = spec->dataE();
    if (x.size() != length) {
      g_log.information() << "Spectrum " << i << " x-size mismatch, is "
                          << x.size() << " should be " << length << "\n";
    }
    if (y.size() != length) {
      g_log.information() << "Spectrum " << i << " y-size mismatch, is "
                          << y.size() << " should be " << length << "\n";
    }
    if (e.size() != length) {
      g_log.information() << "Spectrum " << i << " e-size mismatch, is "
                          << e.size() << " should be " << length << "\n";
    }
  }
}
Example #3
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));
}