/** Compress event
  */
DataObjects::EventWorkspace_sptr
CountEventsInPulses::compressEvents(DataObjects::EventWorkspace_sptr inputws,
                                    double tolerance) {
  API::Algorithm_sptr alg =
      this->createChildAlgorithm("CompressEvents", 0.9, 1.0, true);
  alg->initialize();

  alg->setProperty("InputWorkspace", inputws);
  alg->setProperty("OutputWorkspace", "TempWS");
  alg->setProperty("Tolerance", tolerance);

  bool successful = alg->execute();

  DataObjects::EventWorkspace_sptr outputws;

  if (!successful) {
    // Failed
    outputws = inputws;
    g_log.warning() << "CompressEvents() Failed!" << std::endl;
  } else {
    // Successful
    outputws = alg->getProperty("OutputWorkspace");
    if (!outputws) {
      g_log.error()
          << "CompressEvents failed as the output is not a MatrixWorkspace. "
          << std::endl;
      throw std::runtime_error(
          "SumSpectra failed as the output is not a MatrixWorkspace.");
    }

    /*
    API::MatrixWorkspace_sptr testws = alg->getProperty("OutputWorkspace");
    if (!testws)
    {
        g_log.error() << "CompressEvents failed as the output is not a
    MatrixWorkspace. " << std::endl;
        throw std::runtime_error("SumSpectra failed as the output is not a
    MatrixWorkspace.");
    }
    else
    {
        outputws =
    boost::dynamic_pointer_cast<DataObjects::EventWorkspace>(testws);
    }
   */
  }

  return outputws;
}
Example #2
0
/** Executes the algorithm
 */
void FilterByTime2::exec() {
  DataObjects::EventWorkspace_const_sptr inWS =
      this->getProperty("InputWorkspace");
  if (!inWS) {
    g_log.error() << "Input is not EventWorkspace" << std::endl;
    throw std::invalid_argument("Input is not EventWorksapce");
  } else {
    g_log.debug() << "DB5244 InputWorkspace Name = " << inWS->getName()
                  << std::endl;
  }

  double starttime = this->getProperty("StartTime");
  double stoptime = this->getProperty("StopTime");
  std::string absstarttime = this->getProperty("AbsoluteStartTime");
  std::string absstoptime = this->getProperty("AbsoluteStopTime");

  std::string start, stop;
  if ((absstarttime != "") && (absstoptime != "") && (starttime <= 0.0) &&
      (stoptime <= 0.0)) {
    // Use the absolute string
    start = absstarttime;
    stop = absstoptime;
  } else if ((absstarttime != "" || absstoptime != "") &&
             (starttime > 0.0 || stoptime > 0.0)) {
    throw std::invalid_argument(
        "It is not allowed to provide both absolute time and relative time.");
  } else {
    // Use second
    std::stringstream ss;
    ss << starttime;
    start = ss.str();
    std::stringstream ss2;
    ss2 << stoptime;
    stop = ss2.str();
  }

  // 1. Generate Filters
  g_log.debug() << "\nDB441: About to generate Filter.  StartTime = "
                << starttime << "  StopTime = " << stoptime << std::endl;

  API::Algorithm_sptr genfilter =
      createChildAlgorithm("GenerateEventsFilter", 0.0, 20.0, true, 1);
  genfilter->initialize();
  genfilter->setPropertyValue("InputWorkspace", inWS->getName());
  genfilter->setPropertyValue("OutputWorkspace", "FilterWS");
  genfilter->setProperty("StartTime", start);
  genfilter->setProperty("StopTime", stop);
  genfilter->setProperty("UnitOfTime", "Seconds");
  genfilter->setProperty("FastLog", false);

  bool sucgen = genfilter->execute();
  if (!sucgen) {
    g_log.error() << "Unable to generate event filters" << std::endl;
    throw std::runtime_error("Unable to generate event filters");
  } else {
    g_log.debug() << "Filters are generated. " << std::endl;
  }

  API::Workspace_sptr filterWS = genfilter->getProperty("OutputWorkspace");
  if (!filterWS) {
    g_log.error() << "Unable to retrieve generated SplittersWorkspace object "
                     "from AnalysisDataService." << std::endl;
    throw std::runtime_error("Unable to retrieve Splittersworkspace. ");
  }

  // 2. Filter events
  g_log.debug() << "\nAbout to filter events. "
                << "\n";

  API::Algorithm_sptr filter =
      createChildAlgorithm("FilterEvents", 20.0, 100.0, true, 1);
  filter->initialize();
  filter->setPropertyValue("InputWorkspace", inWS->getName());
  filter->setPropertyValue("OutputWorkspaceBaseName", "ResultWS");
  filter->setProperty("SplitterWorkspace", filterWS);
  filter->setProperty("FilterByPulseTime", true);

  bool sucfilt = filter->execute();
  if (!sucfilt) {
    g_log.error() << "Unable to filter events" << std::endl;
    throw std::runtime_error("Unable to filter events");
  } else {
    g_log.debug() << "Filter events is successful. " << std::endl;
  }

  DataObjects::EventWorkspace_sptr optws =
      filter->getProperty("OutputWorkspace_0");

  this->setProperty("OutputWorkspace", optws);
}