SiStripConfObject

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#ifndef SiStripConfObject_h
#define SiStripConfObject_h

#include "CondFormats/Serialization/interface/Serializable.h"

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <iterator>
#include <sstream>

#include "FWCore/MessageLogger/interface/MessageLogger.h"

class TrackerTopology;

/**
 * Author M. De Mattia - 16/11/2009
 *
 * Simple class used to store configuration values. <br>
 * It stores a map<std::string, std::string> with all the parameters and their values. <br>
 * The put and get methods are provided to store and access the parameters. <br>
 * The put method retuns a bool which is true if the insertion was successuful. If the parameter
 * is already existing the insertion will not happen and the return value will be false. <br>
 * The get method is templated and works like the getParameter<type> of the framework. <br>
 * The isParameter method can be used to check whether a parameter exists. It will return a
 * bool with the result. <br>
 * The printSummary and printDebug method return both the full list of parameters. <br>
 */

class SiStripConfObject {
public:
  SiStripConfObject() {}

  template <class valueType>
  bool put(const std::string& name, const valueType& inputValue) {
    std::stringstream ss;
    ss << inputValue;
    if (parameters.insert(std::make_pair(name, ss.str())).second)
      return true;
    return false;
  }

  /// Updating the value stored as 'name' with 'inputValue'.
  /// False if parameter 'name' does not exist (and nothing is done then - use put(..) instead!),
  /// otherwise true.
  template <class valueType>
  bool update(const std::string& name, const valueType& inputValue) {
    parMap::iterator it = parameters.find(name);
    if (it == parameters.end()) {
      std::cout << "WARNING in SiStripConfObject::update: parameter " << name << " not found, "
                << "so cannot be updated to '" << inputValue << "'." << std::endl;
      return false;
    } else {
      std::stringstream ss;
      ss << inputValue;
      it->second = ss.str();
      return true;
    }
  }

  template <class valueType>
  valueType get(const std::string& name) const {
    valueType returnValue = 0;
    parMap::const_iterator it = parameters.find(name);
    std::stringstream ss;
    if (it != parameters.end()) {
      ss << it->second;
      ss >> returnValue;
    } else {
      std::cout << "WARNING: parameter " << name << " not found. Returning default value" << std::endl;
    }
    return returnValue;
  }

  bool isParameter(const std::string& name) const { return (parameters.find(name) != parameters.end()); }

  /// Prints the full list of parameters
  void printSummary(std::stringstream& ss, const TrackerTopology* trackerTopo) const;
  /// Prints the full list of parameters
  void printDebug(std::stringstream& ss, const TrackerTopology* trackerTopo) const;

  typedef std::map<std::string, std::string> parMap;

  parMap parameters;

  COND_SERIALIZABLE;
};

template <>
std::string SiStripConfObject::get<std::string>(const std::string& name) const;
template <>
bool SiStripConfObject::put<std::vector<int> >(const std::string& name, const std::vector<int>& inputValue);
template <>
bool SiStripConfObject::update<std::vector<int> >(const std::string& name, const std::vector<int>& inputValue);
template <>
std::vector<int> SiStripConfObject::get<std::vector<int> >(const std::string& name) const;
template <>
bool SiStripConfObject::put<std::vector<std::string> >(const std::string& name,
                                                       const std::vector<std::string>& inputValue);
template <>
bool SiStripConfObject::update<std::vector<std::string> >(const std::string& name,
                                                          const std::vector<std::string>& inputValue);
template <>
std::vector<std::string> SiStripConfObject::get<std::vector<std::string> >(const std::string& name) const;

#endif