Reader

SiPixel2DTemplateDBObject

char2float

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#ifndef CondFormats_SiPixelObjects_SiPixel2DTemplateDBObject_h
#define CondFormats_SiPixelObjects_SiPixel2DTemplateDBObject_h 1

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

#include <vector>
#include <map>
#include <cstdint>
#include "FWCore/MessageLogger/interface/MessageLogger.h"

// ******************************************************************************************
//! \class SiPixel2DTemplateDBObject
//!
// ******************************************************************************************

class SiPixel2DTemplateDBObject {
public:
  SiPixel2DTemplateDBObject()
      : index_(0), maxIndex_(0), numOfTempl_(1), version_(-99.9), isInvalid_(false), sVector_(0) {
    sVector_.reserve(1000000);
  }
  virtual ~SiPixel2DTemplateDBObject() {}

  //- Allows the dbobject to be read out like cout
  friend std::ostream& operator<<(std::ostream& s, const SiPixel2DTemplateDBObject& dbobject);

  //- Fills integer from dbobject
  SiPixel2DTemplateDBObject& operator>>(int& i) {
    isInvalid_ = false;
    if (index_ <= maxIndex_) {
      i = (int)(*this).sVector_[index_];
      index_++;
    } else
      (*this).setInvalid();
    return *this;
  }
  //- Fills float from dbobject
  SiPixel2DTemplateDBObject& operator>>(float& f) {
    isInvalid_ = false;
    if (index_ <= maxIndex_) {
      f = (*this).sVector_[index_];
      index_++;
    } else
      (*this).setInvalid();
    return *this;
  }

  //- Functions to monitor integrity of dbobject
  void setVersion(float version) { version_ = version; }
  void setInvalid() { isInvalid_ = true; }
  bool fail() { return isInvalid_; }

  //- Setter functions
  void push_back(float entry) { sVector_.push_back(entry); }
  void setIndex(int index) { index_ = index; }
  void setMaxIndex(int maxIndex) { maxIndex_ = maxIndex; }
  void setNumOfTempl(int numOfTempl) { numOfTempl_ = numOfTempl; }

  //- Accessor functions
  int index() const { return index_; }
  int maxIndex() const { return maxIndex_; }
  int numOfTempl() const { return numOfTempl_; }
  float version() const { return version_; }
  std::vector<float> const& sVector() const { return sVector_; }

  //- Able to set the index for template header
  void incrementIndex(int i) { index_ += i; }

  //- Allows storage of header (type = char[80]) in dbobject
  union char2float {
    char c[4];
    float f;
  };

  //- To be used to select template calibration based on detid
  void putTemplateIDs(std::map<unsigned int, short>& t_ID) { templ_ID = t_ID; }
  const std::map<unsigned int, short>& getTemplateIDs() const { return templ_ID; }

  bool putTemplateID(const uint32_t& detid, short& value) {
    std::map<unsigned int, short>::const_iterator id = templ_ID.find(detid);
    if (id != templ_ID.end()) {
      edm::LogError("SiPixel2DTemplateDBObject")
          << "2Dtemplate ID for DetID " << detid << " is already stored. Skipping this put" << std::endl;
      return false;
    } else
      templ_ID[detid] = value;
    return true;
  }

  short getTemplateID(const uint32_t& detid) const {
    std::map<unsigned int, short>::const_iterator id = templ_ID.find(detid);
    if (id != templ_ID.end())
      return id->second;
    else
      edm::LogError("SiPixel2DTemplateDBObject")
          << "2Dtemplate ID for DetID " << detid << " is not stored" << std::endl;
    return 0;
  }

  class Reader {
  public:
    Reader(SiPixel2DTemplateDBObject const& object) : object_(object), index_(0), isInvalid_(false) {}

    bool fail() { return isInvalid_; }

    int index() const { return index_; }
    //- Able to set the index for template header
    void incrementIndex(int i) { index_ += i; }

    //- Fills integer from dbobject
    Reader& operator>>(int& i) {
      isInvalid_ = false;
      if (index_ <= object_.maxIndex_) {
        i = (int)object_.sVector_[index_];
        index_++;
      } else
        isInvalid_ = true;
      return *this;
    }
    //- Fills float from dbobject
    Reader& operator>>(float& f) {
      isInvalid_ = false;
      if (index_ <= object_.maxIndex_) {
        f = object_.sVector_[index_];
        index_++;
      } else
        isInvalid_ = true;
      return *this;
    }

  private:
    SiPixel2DTemplateDBObject const& object_;
    int index_;
    bool isInvalid_;
  };
  friend class Reader;

private:
  int index_;
  int maxIndex_;
  int numOfTempl_;
  float version_;
  bool isInvalid_;
  std::vector<float> sVector_;
  std::map<unsigned int, short> templ_ID;

  COND_SERIALIZABLE;
};  //end SiPixel2DTemplateDBObject
#endif