Skip to content
Snippets Groups Projects
SaveDspacemap.cpp 4.52 KiB
Newer Older
#include "MantidDataHandling/SaveDspacemap.h"
#include "MantidDataObjects/OffsetsWorkspace.h"
#include "MantidKernel/System.h"
#include "MantidAPI/FileProperty.h"
#include <fstream>


namespace Mantid
{
namespace DataHandling
{

  // Register the algorithm into the AlgorithmFactory
  DECLARE_ALGORITHM(SaveDspacemap)
  
  using namespace Mantid::Kernel;
  using namespace Mantid::API;
  using namespace Mantid::DataObjects;
  using namespace Mantid::Geometry;


  //----------------------------------------------------------------------------------------------
  /** Constructor
   */
  SaveDspacemap::SaveDspacemap()
  {
  }
    
  //----------------------------------------------------------------------------------------------
  /** Destructor
   */
  SaveDspacemap::~SaveDspacemap()
  {
  }
  

  //----------------------------------------------------------------------------------------------
  /// Sets documentation strings for this algorithm
  void SaveDspacemap::initDocs()
  {
    this->setWikiSummary("Saves an [[OffsetsWorkspace]] into a POWGEN-format binary dspace map file.");
    this->setOptionalMessage("Saves an OffsetsWorkspace into a POWGEN-format binary dspace map file.");
    this->setWikiDescription(
        "The POWGEN d-space map file format is a binary list of the "
        "conversion. It needs to be a minimum size, determined by the PadDetID parameter."
        );
  }

  //----------------------------------------------------------------------------------------------
  /** Initialize the algorithm's properties.
   */
  void SaveDspacemap::init()
  {
    declareProperty(new WorkspaceProperty<OffsetsWorkspace>("InputWorkspace","",Direction::Input), "An input OffsetsWorkspace to save.");

    declareProperty(new FileProperty("DspacemapFile", "", FileProperty::Save, ".dat"),
       "The DspacemapFile on output contains the d-space mapping");

    declareProperty("PadDetID", 300000, "Pad Data to this number of pixels");
  }

  //----------------------------------------------------------------------------------------------
  /** Execute the algorithm.
   */
  void SaveDspacemap::exec()
  {
    Mantid::DataObjects::OffsetsWorkspace_sptr offsetsWS = getProperty("InputWorkspace");
    std::string filename = getPropertyValue("DspacemapFile");
    CalculateDspaceFromCal(offsetsWS, filename);
  }


  //-----------------------------------------------------------------------
  /**
   * 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
    IInstrument_const_sptr instrument = offsetsWS->getInstrument();
    double l1;
    double beamline_norm;
    instrument->getInstrumentParameters(l1,beamline,beamline_norm, samplePos);

    //To get all the detector ID's
    detid2det_map allDetectors;
    detid2det_map::const_iterator it;
    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_sptr det;
      // Find the detector with that detector id
      it = allDetectors.find(i);
      if (it != allDetectors.end())
        factor = Instrument::calcConversion(l1, beamline, beamline_norm, samplePos, det, offsetsWS->getValue(i, 0.0), false);
        //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();
  }


} // namespace Mantid
} // namespace DataHandling