Skip to content
Snippets Groups Projects
ExperimentInfo.cpp 40.6 KiB
Newer Older
   * @param parameterStr :: result of ParameterMap.asString()
   */
  void ExperimentInfo::readParameterMap(const std::string & parameterStr)
  {
    Geometry::ParameterMap& pmap = this->instrumentParameters();
    Instrument_const_sptr instr = this->getInstrument()->baseInstrument();

    int options = Poco::StringTokenizer::TOK_IGNORE_EMPTY;
    options += Poco::StringTokenizer::TOK_TRIM;
    Poco::StringTokenizer splitter(parameterStr, "|", options);

    Poco::StringTokenizer::Iterator iend = splitter.end();
    //std::string prev_name;
    for( Poco::StringTokenizer::Iterator itr = splitter.begin(); itr != iend; ++itr )
    {
      Poco::StringTokenizer tokens(*itr, ";");
      if( tokens.count() < 4 ) continue;
      std::string comp_name = tokens[0];
      //if( comp_name == prev_name ) continue; this blocks reading in different parameters of the same component. RNT
      //prev_name = comp_name;
      const Geometry::IComponent* comp = 0;
      if (comp_name.find("detID:") != std::string::npos)
      {
        int detID = atoi(comp_name.substr(6).c_str());
        comp = instr->getDetector(detID).get();
        if (!comp)
        {
          g_log.warning()<<"Cannot find detector "<<detID<<'\n';
          continue;
        }
      }
      else
      {
        comp = instr->getComponentByName(comp_name).get();
        if (!comp)
        {
          g_log.warning()<<"Cannot find component "<<comp_name<<'\n';
          continue;
        }
      }
      if( !comp ) continue;
      // create parameter's value as a sum of all tokens with index 3 or larger
      // this allow a parameter's value to contain ";" 
      std::string paramValue = tokens[3];
      int size = static_cast<int>(tokens.count());
      for (int i = 4; i < size; i++ )
        paramValue += ";" + tokens[4];
      pmap.add(tokens[1], comp, tokens[2], paramValue);
  //------------------------------------------------------------------------------------------------------
  // Private members
  //------------------------------------------------------------------------------------------------------
  /** 
   * Fill map with given instrument parameter
   * @param paramMap Map to populate
   * @param name The name of the parameter
   * @param paramInfo A reference to the object describing this parameter
   * @param runData A reference to the run object
   */
  void ExperimentInfo::populateWithParameter(Geometry::ParameterMap & paramMap,
                                             const std::string & name, const Geometry::XMLlogfile & paramInfo,
                                             const Run & runData)
  {
    const std::string & category = paramInfo.m_type;
    ParameterValue paramValue(paramInfo, runData); //Defines implicit conversion operator
    
    // Some names are special. Values should be convertible to double
    if (name.compare("x") == 0 || name.compare("y") == 0 || name.compare("z") == 0)
    {
      paramMap.addPositionCoordinate(paramInfo.m_component, name, paramValue);
    }
    else if (name.compare("rot") == 0 || name.compare("rotx") == 0 || name.compare("roty") == 0 || name.compare("rotz") == 0)
    {
      paramMap.addRotationParam(paramInfo.m_component, name, paramValue);
    }
    else if(category.compare("fitting") == 0)
    {
      std::ostringstream str;
      str << paramInfo.m_value << " , " << paramInfo.m_fittingFunction << " , " << name << " , " << paramInfo.m_constraint[0] << " , "
          << paramInfo.m_constraint[1] << " , " << paramInfo.m_penaltyFactor << " , "
          << paramInfo.m_tie << " , " << paramInfo.m_formula << " , "
          << paramInfo.m_formulaUnit << " , " << paramInfo.m_resultUnit << " , " << (*(paramInfo.m_interpolation));
      paramMap.add("fitting",paramInfo.m_component, name, str.str());
    }
    else if(category.compare("string") == 0)
    {
      paramMap.addString(paramInfo.m_component,name, paramInfo.m_value);
    }
    else if(category.compare("bool") == 0)
    {
      paramMap.addBool(paramInfo.m_component,name, paramValue);
    }
    else if(category.compare("int") == 0)
    {
      paramMap.addInt(paramInfo.m_component, name, paramValue);
    }
    else // assume double
    {
      paramMap.addDouble(paramInfo.m_component, name, paramValue);
    }
  }
  

namespace Mantid
{
  namespace Kernel
  {

    template<> MANTID_API_DLL
      Mantid::API::ExperimentInfo_sptr IPropertyManager::getValue<Mantid::API::ExperimentInfo_sptr>(const std::string &name) const
    {
      PropertyWithValue<Mantid::API::ExperimentInfo_sptr>* prop =
        dynamic_cast<PropertyWithValue<Mantid::API::ExperimentInfo_sptr>*>(getPointerToProperty(name));
      if (prop)
      {
        return *prop;
      }
      else
      {
        std::string message = "Attempt to assign property "+ name +" to incorrect type. Expected ExperimentInfo.";
        throw std::runtime_error(message);
      }
    }

    template<> MANTID_API_DLL
      Mantid::API::ExperimentInfo_const_sptr IPropertyManager::getValue<Mantid::API::ExperimentInfo_const_sptr>(const std::string &name) const
    {
      PropertyWithValue<Mantid::API::ExperimentInfo_sptr>* prop =
        dynamic_cast<PropertyWithValue<Mantid::API::ExperimentInfo_sptr>*>(getPointerToProperty(name));
      if (prop)
      {
        return prop->operator()();
      }
      else
      {
        std::string message = "Attempt to assign property "+ name +" to incorrect type. Expected const ExperimentInfo.";
        throw std::runtime_error(message);
      }
    }