Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:34

0001 #ifndef CondFormats_SiPixelObjects_SiPixelTemplateDBObject_h
0002 #define CondFormats_SiPixelObjects_SiPixelTemplateDBObject_h 1
0003 
0004 #include "CondFormats/Serialization/interface/Serializable.h"
0005 
0006 #include <vector>
0007 #include <map>
0008 #include <cstdint>
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 
0011 // ******************************************************************************************
0012 //! \class SiPixelTemplateDBObject
0013 //!
0014 // ******************************************************************************************
0015 
0016 class SiPixelTemplateDBObject {
0017 public:
0018   struct Reader {
0019     explicit Reader(SiPixelTemplateDBObject const& idb) : index_(0), isInvalid_(false), db(idb) {}
0020 
0021     //- Fills integer from dbobject
0022     Reader& operator>>(int& i) {
0023       isInvalid_ = false;
0024       if (index_ <= db.maxIndex()) {
0025         i = (int)db.sVector()[index_];
0026         index_++;
0027       } else
0028         setInvalid();
0029       return *this;
0030     }
0031     //- Fills float from dbobject
0032     Reader& operator>>(float& f) {
0033       isInvalid_ = false;
0034       if (index_ <= db.maxIndex()) {
0035         f = db.sVector()[index_];
0036         index_++;
0037       } else
0038         setInvalid();
0039       return *this;
0040     }
0041 
0042     //- Functions to monitor integrity of dbobject
0043     void setInvalid() { isInvalid_ = true; }
0044     bool fail() { return isInvalid_; }
0045     int index() const { return index_; }
0046     int numOfTempl() const { return db.numOfTempl(); }
0047     float version() const { return db.version(); }
0048     std::vector<float> const& sVector() const { return db.sVector(); }
0049 
0050     //- Able to set the index for template header
0051     void incrementIndex(int i) { index_ += i; }
0052 
0053     int index_;
0054     bool isInvalid_;
0055     SiPixelTemplateDBObject const& db;
0056   };
0057 
0058   Reader reader() const { return Reader(*this); }
0059 
0060   SiPixelTemplateDBObject() : index_(0), maxIndex_(0), numOfTempl_(1), version_(-99.9), isInvalid_(false), sVector_(0) {
0061     sVector_.reserve(1000000);
0062   }
0063   virtual ~SiPixelTemplateDBObject() {}
0064 
0065   //- Allows the dbobject to be read out like cout
0066   friend std::ostream& operator<<(std::ostream& s, const SiPixelTemplateDBObject& dbobject);
0067 
0068   //- Fills integer from dbobject
0069   SiPixelTemplateDBObject& operator>>(int& i) {
0070     isInvalid_ = false;
0071     if (index_ <= maxIndex_) {
0072       i = (int)(*this).sVector_[index_];
0073       index_++;
0074     } else
0075       (*this).setInvalid();
0076     return *this;
0077   }
0078   //- Fills float from dbobject
0079   SiPixelTemplateDBObject& operator>>(float& f) {
0080     isInvalid_ = false;
0081     if (index_ <= maxIndex_) {
0082       f = (*this).sVector_[index_];
0083       index_++;
0084     } else
0085       (*this).setInvalid();
0086     return *this;
0087   }
0088 
0089   //- Functions to monitor integrity of dbobject
0090   void setVersion(float version) { version_ = version; }
0091   void setInvalid() { isInvalid_ = true; }
0092   bool fail() { return isInvalid_; }
0093 
0094   //- Setter functions
0095   void push_back(float entry) { sVector_.push_back(entry); }
0096   void setIndex(int index) { index_ = index; }
0097   void setMaxIndex(int maxIndex) { maxIndex_ = maxIndex; }
0098   void setNumOfTempl(int numOfTempl) { numOfTempl_ = numOfTempl; }
0099 
0100   //- Accessor functions
0101   int index() const { return index_; }
0102   int maxIndex() const { return maxIndex_; }
0103   int numOfTempl() const { return numOfTempl_; }
0104   float version() const { return version_; }
0105   std::vector<float> const& sVector() const { return sVector_; }
0106 
0107   //- Able to set the index for template header
0108   void incrementIndex(int i) { index_ += i; }
0109 
0110   //- Allows storage of header (type = char[80]) in dbobject
0111   union char2float {
0112     char c[4];
0113     float f;
0114   };
0115 
0116   //- To be used to select template calibration based on detid
0117   void putTemplateIDs(std::map<unsigned int, short>& t_ID) { templ_ID = t_ID; }
0118   const std::map<unsigned int, short>& getTemplateIDs() const { return templ_ID; }
0119 
0120   bool putTemplateID(const uint32_t& detid, short& value) {
0121     std::map<unsigned int, short>::const_iterator id = templ_ID.find(detid);
0122     if (id != templ_ID.end()) {
0123       edm::LogError("SiPixelTemplateDBObject")
0124           << "Template ID for DetID " << detid << " is already stored. Skipping this put" << std::endl;
0125       return false;
0126     } else
0127       templ_ID[detid] = value;
0128     return true;
0129   }
0130 
0131   short getTemplateID(const uint32_t& detid) const {
0132     std::map<unsigned int, short>::const_iterator id = templ_ID.find(detid);
0133     if (id != templ_ID.end())
0134       return id->second;
0135     else
0136       edm::LogError("SiPixelTemplateDBObject") << "Template ID for DetID " << detid << " is not stored" << std::endl;
0137     return 0;
0138   }
0139 
0140 private:
0141   int index_;
0142   int maxIndex_;
0143   int numOfTempl_;
0144   float version_;
0145   bool isInvalid_;
0146   std::vector<float> sVector_;
0147   std::map<unsigned int, short> templ_ID;
0148 
0149   COND_SERIALIZABLE;
0150 };  //end SiPixelTemplateDBObject
0151 #endif