/**
  * Overwridden addOrReplace member to attach the name to the workspace when a
 * workspace object is added to the service.
  * This will overwrite one of the same name. If the workspace is group adds or
 * replaces its members.
  * @param name The name of the object
  * @param workspace The shared pointer to the workspace to store
  */
void AnalysisDataServiceImpl::addOrReplace(
    const std::string &name,
    const boost::shared_ptr<API::Workspace> &workspace) {
  verifyName(name);

  // Attach the name to the workspace
  if (workspace)
    workspace->setName(name);
  Kernel::DataService<API::Workspace>::addOrReplace(name, workspace);

  // if a group is added add its members as well
  auto group = boost::dynamic_pointer_cast<WorkspaceGroup>(workspace);
  if (!group)
    return;
  group->observeADSNotifications(true);
  for (size_t i = 0; i < group->size(); ++i) {
    auto ws = group->getItem(i);
    std::string wsName = ws->name();
    // make up a name for an anonymous workspace
    if (wsName.empty()) {
      wsName = name + "_" + boost::lexical_cast<std::string>(i + 1);
    } else if (doesExist(wsName)) { // if ws is already there do nothing
      wsName.clear();
    }
    // add member workspace if needed
    if (!wsName.empty()) {
      addOrReplace(wsName, ws);
    }
  }
}
  /** Construct an output TableWorkspace for fitting result (profile parameters)
    */
  TableWorkspace_sptr RefinePowderInstrumentParameters2::genOutputProfileTable(map<string, Parameter> parameters,
                                                                     double startchi2, double finalchi2)
  {
    // 1. Create TableWorkspace
    TableWorkspace_sptr tablews(new TableWorkspace);

    tablews->addColumn("str", "Name");
    tablews->addColumn("double", "Value");
    tablews->addColumn("str", "FitOrTie");
    tablews->addColumn("double", "Min");
    tablews->addColumn("double", "Max");
    tablews->addColumn("double", "StepSize");
    tablews->addColumn("double", "Error");

    // 2. For chi^2
    addOrReplace(parameters, "Chi2_Init", startchi2);
    addOrReplace(parameters, "Chi2_Result", finalchi2);

    // 3. Set values
    map<string, Parameter>::iterator pariter;
    for (pariter = parameters.begin(); pariter != parameters.end(); ++pariter)
    {
      Parameter& param = pariter->second;
      TableRow newrow = tablews->appendRow();

      string fitortie;
      if (param.fit)
        fitortie = "fit";
      else
        fitortie = "tie";

      newrow << param.name << param.value << fitortie << param.minvalue << param.maxvalue
             << param.stepsize << param.error;
    }

    return tablews;
  }