コード例 #1
0
ファイル: RefReduction.cpp プロジェクト: trnielsen/mantid
IEventWorkspace_sptr RefReduction::loadData(const std::string dataRun,
    const std::string polarization)
{
  const std::string instrument = getProperty("Instrument");

  // Check whether dataRun refers to an existing workspace
  // Create a good name for the raw workspace
  std::string ws_name = "__ref_"+dataRun+"-"+polarization+"_raw";
  IEventWorkspace_sptr rawWS;
  if (AnalysisDataService::Instance().doesExist(dataRun))
  {
    rawWS = AnalysisDataService::Instance().retrieveWS<EventWorkspace>(dataRun);
    g_log.notice() << "Found workspace: " << dataRun << std::endl;
    m_output_message += "    |Input data run is a workspace: " + dataRun + "\n";
  }
  else if (AnalysisDataService::Instance().doesExist(ws_name))
  {
    rawWS = AnalysisDataService::Instance().retrieveWS<EventWorkspace>(ws_name);
    g_log.notice() << "Using existing workspace: " << ws_name << std::endl;
    m_output_message += "    |Found workspace from previous reduction: " + ws_name + "\n";
  }
  else
  {
    // If we can't find a workspace, find a file to load
    std::string path = FileFinder::Instance().getFullPath(dataRun);

    if (path.size()==0 || !Poco::File(path).exists())
    {
      try
      {
        std::vector<std::string> paths = FileFinder::Instance().findRuns(instrument+dataRun);
        path = paths[0];
      }
      catch(Exception::NotFoundError&) { /* Pass. We report the missing file later. */ }
    }

    if (path.size()==0 || !Poco::File(path).exists())
    {
      try
      {
        std::vector<std::string> paths = FileFinder::Instance().findRuns(dataRun);
        path = paths[0];
      }
      catch(Exception::NotFoundError&) { /* Pass. We report the missing file later. */ }
    }

    if (Poco::File(path).exists()) {
      g_log.notice() << "Found: " << path << std::endl;
      m_output_message += "    |Loading from " + path + "\n";
      IAlgorithm_sptr loadAlg = createChildAlgorithm("LoadEventNexus", 0, 0.2);
      loadAlg->setProperty("Filename", path);
      if (polarization.compare(PolStateNone)!=0)
        loadAlg->setProperty("NXentryName", polarization);
      loadAlg->executeAsChildAlg();
      rawWS = loadAlg->getProperty("OutputWorkspace");
      if (rawWS->getNumberEvents()==0)
      {
        g_log.notice() << "No data in " << polarization << std::endl;
        m_output_message += "    |No data for " + polarization + "\n";
        return rawWS;
      }

      // Move the detector to the right position
      if (instrument.compare("REF_M")==0)
      {
          double det_distance = rawWS->getInstrument()->getDetector(0)->getPos().Z();
          Mantid::Kernel::Property* prop = rawWS->run().getProperty("SampleDetDis");
          Mantid::Kernel::TimeSeriesProperty<double>* dp = dynamic_cast<Mantid::Kernel::TimeSeriesProperty<double>* >(prop);
          double sdd = dp->getStatistics().mean/1000.0;
          IAlgorithm_sptr mvAlg = createChildAlgorithm("MoveInstrumentComponent", 0.2, 0.25);
          mvAlg->setProperty<MatrixWorkspace_sptr>("Workspace", rawWS);
          mvAlg->setProperty("ComponentName", "detector1");
          mvAlg->setProperty("Z", sdd-det_distance);
          mvAlg->setProperty("RelativePosition", true);
          mvAlg->executeAsChildAlg();
          g_log.notice() << "Ensuring correct Z position: Correction = "
              << Poco::NumberFormatter::format(sdd-det_distance)
              << " m" << std::endl;
      }
      AnalysisDataService::Instance().addOrReplace(ws_name, rawWS);
    } else {
        g_log.error() << "Could not find a data file for " << dataRun << std::endl;
        throw std::invalid_argument("Could not find a data file for the given input");
    }
  }

  // Crop TOF as needed and set binning
  double tofMin = getProperty("TOFMin");
  double tofMax = getProperty("TOFMax");
  if (isEmpty(tofMin) || isEmpty(tofMax))
  {
    const MantidVec& x = rawWS->readX(0);
    if (isEmpty(tofMin)) tofMin = *std::min_element(x.begin(), x.end());
    if (isEmpty(tofMax)) tofMax = *std::max_element(x.begin(), x.end());
  }

  int nBins = getProperty("NBins");
  double tofStep = getProperty("TOFStep");
  if (!isEmpty(nBins))
    tofStep = (tofMax-tofMin)/nBins;
  else
    nBins = (int)floor( (tofMax-tofMin)/tofStep );

  std::vector<double> params;
  params.push_back(tofMin);
  params.push_back(tofStep);
  params.push_back(tofMax);

  IAlgorithm_sptr rebinAlg = createChildAlgorithm("Rebin", 0.25, 0.3);
  rebinAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", rawWS);
  rebinAlg->setProperty("Params", params);
  rebinAlg->setProperty("PreserveEvents", true);
  rebinAlg->executeAsChildAlg();
  MatrixWorkspace_sptr outputWS = rebinAlg->getProperty("OutputWorkspace");
  m_output_message += "    |TOF binning: "
      +  Poco::NumberFormatter::format(tofMin) + " to "
      +  Poco::NumberFormatter::format(tofMax) + " in steps of "
      +  Poco::NumberFormatter::format(tofStep) + " microsecs\n";

  // Normalise by current
  IAlgorithm_sptr normAlg = createChildAlgorithm("NormaliseByCurrent", 0.3, 0.35);
  normAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputWS);
  //normAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS);
  normAlg->executeAsChildAlg();
  outputWS = normAlg->getProperty("OutputWorkspace");

  // Convert to wavelength
  IAlgorithm_sptr convAlg = createChildAlgorithm("ConvertUnits", 0.35, 0.4);
  convAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputWS);
  convAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS);
  convAlg->setProperty("Target", "Wavelength");
  convAlg->executeAsChildAlg();

  // Rebin in wavelength
  const MantidVec& x = outputWS->readX(0);
  double wlMin = *std::min_element(x.begin(), x.end());
  double wlMax = *std::max_element(x.begin(), x.end());

  std::vector<double> wl_params;
  wl_params.push_back(wlMin);
  wl_params.push_back((wlMax-wlMin)/nBins);
  wl_params.push_back(wlMax);

  IAlgorithm_sptr rebinAlg2 = createChildAlgorithm("Rebin", 0.25, 0.3);
  rebinAlg2->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputWS);
  rebinAlg2->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", outputWS);
  rebinAlg2->setProperty("Params", wl_params);
  rebinAlg2->setProperty("PreserveEvents", true);
  rebinAlg2->executeAsChildAlg();

  IEventWorkspace_sptr outputEvtWS = boost::dynamic_pointer_cast<IEventWorkspace>(outputWS);
  return outputEvtWS;
}
コード例 #2
0
ファイル: RefReduction.cpp プロジェクト: trnielsen/mantid
MatrixWorkspace_sptr RefReduction::processData(const std::string polarization)
{
  m_output_message += "Processing " + polarization + '\n';
  const std::string dataRun = getPropertyValue("DataRun");
  IEventWorkspace_sptr evtWS = loadData(dataRun, polarization);
  MatrixWorkspace_sptr dataWS = boost::dynamic_pointer_cast<MatrixWorkspace>(evtWS);
  MatrixWorkspace_sptr dataWSTof = boost::dynamic_pointer_cast<MatrixWorkspace>(evtWS);
    

  // If we have no events, stop here
  if (evtWS->getNumberEvents()==0) return dataWS;

  // Get low-res pixel range
  int low_res_min = 0;
  int low_res_max = 0;
  const bool cropLowRes = getProperty("CropLowResDataAxis");
  const std::vector<int> lowResRange = getProperty("LowResDataAxisPixelRange");
  if (cropLowRes)
  {
    if (lowResRange.size()<2) {
      g_log.error() << "LowResDataAxisPixelRange parameter should be a vector of two values" << std::endl;
      throw std::invalid_argument("LowResDataAxisPixelRange parameter should be a vector of two values");
    }
    low_res_min = lowResRange[0];
    low_res_max = lowResRange[1];
    m_output_message += "    |Cropping low-res axis: ["
        + Poco::NumberFormatter::format(low_res_min) + ", "
        + Poco::NumberFormatter::format(low_res_max) + "]\n";
  }

  // Get peak range
  const std::vector<int> peakRange = getProperty("SignalPeakPixelRange");
  if (peakRange.size()<2) {
    g_log.error() << "SignalPeakPixelRange parameter should be a vector of two values" << std::endl;
    throw std::invalid_argument("SignalPeakPixelRange parameter should be a vector of two values");
  }

  // Get scattering angle in degrees
  double theta = getProperty("Theta");
  const std::string instrument = getProperty("Instrument");
  const bool integrateY = instrument.compare("REF_M")==0;

  // Get pixel ranges in real pixels
  int xmin = 0;
  int xmax = 0;
  int ymin = 0;
  int ymax = 0;
  if (integrateY)
  {
    if (isEmpty(theta)) theta = calculateAngleREFM(dataWS);
    if (!cropLowRes) low_res_max = NY_PIXELS-1;
    xmin = 0;
    xmax = NX_PIXELS-1;
    ymin = low_res_min;
    ymax = low_res_max;
  } else {
    if (isEmpty(theta)) theta = calculateAngleREFL(dataWS);
    if (!cropLowRes) low_res_max = NX_PIXELS-1;
    ymin = 0;
    ymax = NY_PIXELS-1;
    xmin = low_res_min;
    xmax = low_res_max;
  }
  m_output_message += "    |Scattering angle: "
      + Poco::NumberFormatter::format(theta,6) + " deg\n";

  // Subtract background
  if (getProperty("SubtractSignalBackground"))
  {
    // Get background range
    const std::vector<int> bckRange = getProperty("SignalBackgroundPixelRange");
    if (bckRange.size()<2) {
      g_log.error() << "SignalBackgroundPixelRange parameter should be a vector of two values" << std::endl;
      throw std::invalid_argument("SignalBackgroundPixelRange parameter should be a vector of two values");
    }

    IAlgorithm_sptr convAlg = createChildAlgorithm("ConvertToMatrixWorkspace", 0.50, 0.55);
    convAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS);
    convAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", dataWS);
    convAlg->executeAsChildAlg();

    dataWS = subtractBackground(dataWS, dataWS,
        peakRange[0], peakRange[1], bckRange[0], bckRange[1], low_res_min, low_res_max);
    m_output_message += "    |Subtracted background ["
        + Poco::NumberFormatter::format(bckRange[0]) + ", "
        + Poco::NumberFormatter::format(bckRange[1]) + "]\n";
  }

  // Process normalization run
  if (getProperty("PerformNormalization"))
  {
    MatrixWorkspace_sptr normWS = processNormalization();
    IAlgorithm_sptr rebinAlg = createChildAlgorithm("RebinToWorkspace", 0.50, 0.55);
    rebinAlg->setProperty<MatrixWorkspace_sptr>("WorkspaceToRebin", normWS);
    rebinAlg->setProperty<MatrixWorkspace_sptr>("WorkspaceToMatch", dataWS);
    rebinAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", normWS);
    rebinAlg->executeAsChildAlg();
    normWS = rebinAlg->getProperty("OutputWorkspace");

    IAlgorithm_sptr divAlg = createChildAlgorithm("Divide", 0.55, 0.65);
    divAlg->setProperty<MatrixWorkspace_sptr>("LHSWorkspace", dataWS);
    divAlg->setProperty<MatrixWorkspace_sptr>("RHSWorkspace", normWS);
    divAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", dataWS);
    divAlg->executeAsChildAlg();

    IAlgorithm_sptr repAlg = createChildAlgorithm("ReplaceSpecialValues", 0.55, 0.65);
    repAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS);
    repAlg->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", dataWS);
    repAlg->setProperty("NaNValue", 0.0);
    repAlg->setProperty("NaNError", 0.0);
    repAlg->setProperty("InfinityValue", 0.0);
    repAlg->setProperty("InfinityError", 0.0);
    repAlg->executeAsChildAlg();
    m_output_message += "Normalization completed\n";
  }

//    // Integrate over Y
//    IAlgorithm_sptr refAlg = createChildAlgorithm("RefRoi", 0.90, 0.95);
//    refAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS);
//    refAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS);
//    refAlg->setProperty("NXPixel", NX_PIXELS);
//    refAlg->setProperty("NYPixel", NY_PIXELS);
//    refAlg->setProperty("YPixelMin", ymin);
//    refAlg->setProperty("YPixelMax", ymax);
//    refAlg->setProperty("XPixelMin", xmin);
//    refAlg->setProperty("XPixelMax", xmax);
//    refAlg->setProperty("IntegrateY", integrateY);
//    refAlg->setProperty("ScatteringAngle", theta);
//    refAlg->executeAsChildAlg();
//    
//    // Convert back to TOF
//    IAlgorithm_sptr convAlgToTof = createChildAlgorithm("ConvertUnits", 0.85, 0.90);
//    convAlgToTof->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS);
//    convAlgToTof->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", dataWSTof);
//    convAlgToTof->setProperty("Target", "TOF");
//    convAlgToTof->executeAsChildAlg();
//
//    MatrixWorkspace_sptr outputWS2 = convAlgToTof->getProperty("OutputWorkspace");
//    declareProperty(new WorkspaceProperty<>("OutputWorkspace_jc_" + polarization, "TOF_"+polarization, Direction::Output));
//    setProperty("OutputWorkspace_jc_" + polarization, outputWS2);

    //integrated over Y and keep in lambda scale
    IAlgorithm_sptr refAlg1 = createChildAlgorithm("RefRoi", 0.90, 0.95);
    refAlg1->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS);
    refAlg1->setProperty("NXPixel", NX_PIXELS);
    refAlg1->setProperty("NYPixel", NY_PIXELS);
    refAlg1->setProperty("ConvertToQ", false);
    refAlg1->setProperty("YPixelMin", ymin);
    refAlg1->setProperty("YPixelMax", ymax);
    refAlg1->setProperty("XPixelMin", xmin);
    refAlg1->setProperty("XPixelMax", xmax);
    refAlg1->setProperty("IntegrateY", integrateY);
    refAlg1->setProperty("ScatteringAngle", theta);
    refAlg1->executeAsChildAlg();
    MatrixWorkspace_sptr outputWS2 = refAlg1->getProperty("OutputWorkspace");
    declareProperty(new WorkspaceProperty<>("OutputWorkspace_jc_" + polarization, "Lambda_"+polarization, Direction::Output));
    setProperty("OutputWorkspace_jc_" + polarization, outputWS2);
    
    // Conversion to Q
  IAlgorithm_sptr refAlg = createChildAlgorithm("RefRoi", 0.90, 0.95);
  refAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", dataWS);
  refAlg->setProperty("NXPixel", NX_PIXELS);
  refAlg->setProperty("NYPixel", NY_PIXELS);
  refAlg->setProperty("ConvertToQ", true);

  refAlg->setProperty("YPixelMin", ymin);
  refAlg->setProperty("YPixelMax", ymax);
  refAlg->setProperty("XPixelMin", xmin);
  refAlg->setProperty("XPixelMax", xmax);
  refAlg->setProperty("IntegrateY", integrateY);
  refAlg->setProperty("ScatteringAngle", theta);
  refAlg->executeAsChildAlg();

  MatrixWorkspace_sptr output2DWS = refAlg->getProperty("OutputWorkspace");
  std::vector<int> spectra;
  for(int i=peakRange[0]; i<peakRange[1]+1; i++) spectra.push_back(i);

  IAlgorithm_sptr grpAlg = createChildAlgorithm("GroupDetectors", 0.95, 0.99);
  grpAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", output2DWS);
  grpAlg->setProperty("SpectraList", spectra);
  grpAlg->executeAsChildAlg();

  MatrixWorkspace_sptr outputWS = grpAlg->getProperty("OutputWorkspace");

  const std::string prefix = getPropertyValue("OutputWorkspacePrefix");
  if (polarization.compare(PolStateNone)==0)
  {
    declareProperty(new WorkspaceProperty<>("OutputWorkspace", prefix, Direction::Output));
    setProperty("OutputWorkspace", outputWS);
    declareProperty(new WorkspaceProperty<>("OutputWorkspace2D", "2D_"+prefix, Direction::Output));
    setProperty("OutputWorkspace2D", output2DWS);
  } else {
    std::string wsName = prefix+polarization;
    Poco::replaceInPlace(wsName, "entry", "");
    declareProperty(new WorkspaceProperty<>("OutputWorkspace_"+polarization, wsName, Direction::Output));
    setProperty("OutputWorkspace_"+polarization, outputWS);
    declareProperty(new WorkspaceProperty<>("OutputWorkspace2D_"+polarization, "2D_"+wsName, Direction::Output));
    setProperty("OutputWorkspace2D_"+polarization, output2DWS);
  }
  m_output_message += "Reflectivity calculation completed\n";
  return outputWS;
}