/** Create histogram workspace
 */
MatrixWorkspace_sptr CreateSampleWorkspace::createHistogramWorkspace(
    int numPixels, int numBins, double x0, double binDelta,
    int start_at_pixelID, Geometry::Instrument_sptr inst,
    const std::string &functionString, bool isRandom) {
  MantidVecPtr x, y, e;
  x.access().resize(numBins + 1);
  e.access().resize(numBins);
  for (int i = 0; i < numBins + 1; ++i) {
    x.access()[i] = x0 + i * binDelta;
  }

  std::vector<double> xValues(x.access().begin(), x.access().end() - 1);
  y.access() = evalFunction(functionString, xValues, isRandom ? 1 : 0);
  e.access().resize(numBins);

  // calculate e as sqrt(y)
  typedef double (*uf)(double);
  uf dblSqrt = std::sqrt;
  std::transform(y.access().begin(), y.access().end(), e.access().begin(),
                 dblSqrt);

  MatrixWorkspace_sptr retVal(new DataObjects::Workspace2D);
  retVal->initialize(numPixels, numBins + 1, numBins);
  retVal->setInstrument(inst);

  for (size_t wi = 0; wi < static_cast<size_t>(numPixels); wi++) {
    retVal->setX(wi, x);
    retVal->setData(wi, y, e);
    retVal->getSpectrum(wi)->setDetectorID(detid_t(start_at_pixelID + wi));
    retVal->getSpectrum(wi)->setSpectrumNo(specid_t(wi + 1));
  }

  return retVal;
}
Example #2
0
  /**
   * Make a map of the conversion factors between tof and D-spacing
   * for all pixel IDs in a workspace.
   *
   * @param DFileName name of dspacemap file
   * @param offsetsWS :: OffsetsWorkspace with instrument and offsets
   */
  void SaveDspacemap::CalculateDspaceFromCal(Mantid::DataObjects::OffsetsWorkspace_sptr offsetsWS,
                                    std::string DFileName)
  {
    const char * filename = DFileName.c_str();
    // Get a pointer to the instrument contained in the workspace
    Instrument_const_sptr instrument = offsetsWS->getInstrument();
    double l1;
    Kernel::V3D beamline,samplePos;
    double beamline_norm;
    instrument->getInstrumentParameters(l1,beamline,beamline_norm, samplePos);

    //To get all the detector ID's
    detid2det_map allDetectors;
    instrument->getDetectors(allDetectors);

    detid2det_map::const_iterator it;
    detid_t maxdetID = 0;
    for (it = allDetectors.begin(); it != allDetectors.end(); it++)
    {
      detid_t detectorID = it->first;
      if(detectorID > maxdetID) maxdetID = detectorID;
    }
    detid_t paddetID = detid_t(getProperty("PadDetID"));
    if (maxdetID < paddetID)maxdetID = paddetID;

    // Now write the POWGEN-style Dspace mapping file
    std::ofstream fout(filename, std::ios_base::out|std::ios_base::binary);
    Progress prog(this,0.0,1.0,maxdetID);

    for (detid_t i = 0; i != maxdetID; i++)
    {
      //Compute the factor
      double factor;
      Geometry::IDetector_const_sptr det;
      // Find the detector with that detector id
      it = allDetectors.find(i);
      if (it != allDetectors.end())
      {
        det = it->second;
        factor = Instrument::calcConversion(l1, beamline, beamline_norm, samplePos, det, offsetsWS->getValue(i, 0.0));
        //Factor of 10 between ISAW and Mantid
        factor *= 0.1 ;
        if(factor<0)factor = 0.0;
        fout.write( reinterpret_cast<char*>( &factor ), sizeof(double) );
      }
      else
      {
        factor = 0;
        fout.write( reinterpret_cast<char*>( &factor ), sizeof(double) );
      }
      //Report progress
      prog.report();

    }
    fout.close();
  }