Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-03-17 23:26:14

0001 #ifndef MESet_H
0002 #define MESet_H
0003 
0004 #include "DQM/EcalCommon/interface/MESetBinningUtils.h"
0005 
0006 #include "DQMServices/Core/interface/DQMStore.h"
0007 
0008 #include "DataFormats/DetId/interface/DetId.h"
0009 #include "DataFormats/EcalDetId/interface/EcalElectronicsId.h"
0010 
0011 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
0012 #include "Geometry/CaloTopology/interface/EcalTrigTowerConstituentsMap.h"
0013 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0014 #include "Geometry/CaloTopology/interface/CaloTopology.h"
0015 
0016 #include "FWCore/Utilities/interface/Exception.h"
0017 
0018 #include <string>
0019 #include <map>
0020 #include <memory>
0021 #include <vector>
0022 
0023 namespace ecaldqm {
0024   /*
0025   class MESet
0026   Base class for MonitorElement wrappers
0027   Interface between ME bins and DetId
0028 */
0029 
0030   // struct made to simplify passing multiple setup
0031   // variables (see DQWorker.h for implementation)
0032   // to MESet functions
0033   struct EcalDQMSetupObjects {
0034     EcalElectronicsMapping const *electronicsMap;
0035     EcalTrigTowerConstituentsMap const *trigtowerMap;
0036     CaloGeometry const *geometry;
0037     CaloTopology const *topology;
0038   };
0039 
0040   class StatusManager;
0041 
0042   class MESet {
0043   public:
0044     typedef dqm::legacy::DQMStore DQMStore;
0045     typedef dqm::legacy::MonitorElement MonitorElement;
0046     typedef std::map<std::string, std::string> PathReplacements;
0047 
0048     MESet();
0049     MESet(std::string const &, binning::ObjectType, binning::BinningType, MonitorElement::Kind);
0050     MESet(MESet const &);
0051     virtual ~MESet();
0052 
0053     virtual MESet &operator=(MESet const &);
0054 
0055     virtual MESet *clone(std::string const & = "") const;
0056 
0057     virtual void book(DQMStore::IBooker &, EcalElectronicsMapping const *) {}
0058     virtual bool retrieve(EcalElectronicsMapping const *, DQMStore::IGetter &, std::string * = nullptr) const {
0059       return false;
0060     }
0061     virtual void clear() const;
0062 
0063     // Overloaded functions deal with different ids or
0064     // inputs to fill, setBinContent, etc and each determines
0065     // the correct bin to fill based on what is passed.
0066     //
0067     // Note: not every fill, setBinContent, etc necessarily uses
0068     // EcalDQMSetupObjects, but they are passed one anyway to
0069     // avoid accidentally casting a DetId or a EcalElectronicsId
0070     // to an int or a double and have it exercute the wrong function.
0071     // This would be tricky to debug if this error is made, so it
0072     // makes more sense for these functions to look consistent in
0073     // terms of passing EcalDQMSetupObjects.
0074     virtual void fill(EcalDQMSetupObjects const, DetId const &, double = 1., double = 1., double = 1.) {}
0075     virtual void fill(EcalDQMSetupObjects const, EcalElectronicsId const &, double = 1., double = 1., double = 1.) {}
0076     virtual void fill(EcalDQMSetupObjects const, int, double = 1., double = 1., double = 1.) {}
0077     virtual void fill(EcalDQMSetupObjects const, double, double = 1., double = 1.) {}
0078 
0079     virtual void setBinContent(EcalDQMSetupObjects const, DetId const &, double) {}
0080     virtual void setBinContent(EcalDQMSetupObjects const, EcalElectronicsId const &, double) {}
0081     virtual void setBinContent(EcalDQMSetupObjects const, int, double) {}
0082     virtual void setBinContent(EcalDQMSetupObjects const, DetId const &, int, double) {}
0083     virtual void setBinContent(EcalDQMSetupObjects const, EcalElectronicsId const &, int, double) {}
0084     virtual void setBinContent(EcalDQMSetupObjects const, int, int, double) {}
0085 
0086     virtual void setBinError(EcalDQMSetupObjects const, DetId const &, double) {}
0087     virtual void setBinError(EcalDQMSetupObjects const, EcalElectronicsId const &, double) {}
0088     virtual void setBinError(EcalDQMSetupObjects const, int, double) {}
0089     virtual void setBinError(EcalDQMSetupObjects const, DetId const &, int, double) {}
0090     virtual void setBinError(EcalDQMSetupObjects const, EcalElectronicsId const &, int, double) {}
0091     virtual void setBinError(EcalDQMSetupObjects const, int, int, double) {}
0092 
0093     virtual void setBinEntries(EcalDQMSetupObjects const, DetId const &, double) {}
0094     virtual void setBinEntries(EcalDQMSetupObjects const, EcalElectronicsId const &, double) {}
0095     virtual void setBinEntries(EcalDQMSetupObjects const, int, double) {}
0096     virtual void setBinEntries(EcalDQMSetupObjects const, DetId const &, int, double) {}
0097     virtual void setBinEntries(EcalDQMSetupObjects const, EcalElectronicsId const &, int, double) {}
0098     virtual void setBinEntries(EcalDQMSetupObjects const, int, int, double) {}
0099 
0100     virtual double getBinContent(EcalDQMSetupObjects const, DetId const &, int = 0) const { return 0.; }
0101     virtual double getBinContent(EcalDQMSetupObjects const, EcalElectronicsId const &, int = 0) const { return 0.; }
0102     virtual double getBinContent(EcalDQMSetupObjects const, int, int = 0) const { return 0.; }
0103 
0104     virtual double getBinError(EcalDQMSetupObjects const, DetId const &, int = 0) const { return 0.; }
0105     virtual double getBinError(EcalDQMSetupObjects const, EcalElectronicsId const &, int = 0) const { return 0.; }
0106     virtual double getBinError(EcalDQMSetupObjects const, int, int = 0) const { return 0.; }
0107 
0108     virtual double getBinEntries(EcalDQMSetupObjects const, DetId const &, int = 0) const { return 0.; }
0109     virtual double getBinEntries(EcalDQMSetupObjects const, EcalElectronicsId const &, int = 0) const { return 0.; }
0110     virtual double getBinEntries(EcalDQMSetupObjects const, int, int = 0) const { return 0.; }
0111 
0112     // title, axis
0113     virtual void setAxisTitle(std::string const &, int = 1);
0114 
0115     virtual void reset(EcalElectronicsMapping const *, double = 0., double = 0., double = 0.);
0116     virtual void resetAll(double = 0., double = 0., double = 0.);
0117 
0118     virtual bool maskMatches(DetId const &, uint32_t, StatusManager const *, EcalTrigTowerConstituentsMap const *) const;
0119 
0120     virtual std::string const &getPath() const { return path_; }
0121     binning::ObjectType getObjType() const { return otype_; }
0122     binning::BinningType getBinType() const { return btype_; }
0123     MonitorElement::Kind getKind() const { return kind_; }
0124     bool isActive() const { return active_; }  // booked or retrieved
0125     virtual bool isVariableBinning() const { return false; }
0126     virtual MonitorElement const *getME(unsigned _iME) const { return (_iME < mes_.size() ? mes_[_iME] : nullptr); }
0127     virtual MonitorElement *getME(unsigned _iME) { return (_iME < mes_.size() ? mes_[_iME] : nullptr); }
0128 
0129     std::string formPath(PathReplacements const &) const;
0130 
0131     void setLumiFlag() { lumiFlag_ = true; };
0132     bool getLumiFlag() const { return lumiFlag_; }
0133     void setBatchMode() { batchMode_ = true; }
0134     bool getBatchMode() const { return batchMode_; }
0135 
0136   protected:
0137     virtual void fill_(unsigned, int, double);
0138     virtual void fill_(unsigned, int, double, double);
0139     virtual void fill_(unsigned, double, double, double);
0140 
0141     virtual void checkME_(unsigned _iME) const {
0142       if (!getME(_iME)) {
0143         std::stringstream ss;
0144         ss << "ME does not exist at index " << _iME;
0145         throw_(ss.str());
0146       }
0147     }
0148 
0149     void throw_(std::string const &_message) const { throw cms::Exception("EcalDQM") << path_ << ": " << _message; }
0150 
0151     mutable std::vector<MonitorElement *> mes_;
0152 
0153     mutable std::string path_;
0154     binning::ObjectType otype_;
0155     binning::BinningType btype_;
0156     MonitorElement::Kind kind_;
0157     bool lumiFlag_;   // when true, histograms will be saved every lumi section
0158                       // (default false)
0159     bool batchMode_;  // when true, histograms are not GUI-ready (default false)
0160 
0161     mutable bool active_;
0162 
0163   public:
0164     struct ConstBin {
0165     protected:
0166       MESet const *meSet_;
0167 
0168     public:
0169       unsigned iME;
0170       int iBin;
0171       binning::ObjectType otype;
0172 
0173       ConstBin() : meSet_(nullptr), iME(-1), iBin(-1), otype(binning::nObjType) {}
0174       ConstBin(MESet const &, unsigned = 0, int = 1);
0175       ConstBin(ConstBin const &_orig) : meSet_(_orig.meSet_), iME(_orig.iME), iBin(_orig.iBin), otype(_orig.otype) {}
0176       ConstBin &operator=(ConstBin const &);
0177       bool operator==(ConstBin const &_rhs) const {
0178         return meSet_ != nullptr && meSet_ == _rhs.meSet_ && iME == _rhs.iME && iBin == _rhs.iBin;
0179       }
0180       bool isChannel(EcalElectronicsMapping const *electronicsMap) const {
0181         if (meSet_)
0182           return binning::isValidIdBin(electronicsMap, otype, meSet_->getBinType(), iME, iBin);
0183         else
0184           return false;
0185       }
0186       uint32_t getId() const {
0187         if (meSet_)
0188           return binning::idFromBin(otype, meSet_->getBinType(), iME, iBin);
0189         else
0190           return 0;
0191       }
0192       double getBinContent() const {
0193         if (meSet_ && iME != unsigned(-1))
0194           return meSet_->getME(iME)->getBinContent(iBin);
0195         else
0196           return 0.;
0197       }
0198       double getBinError() const {
0199         if (meSet_ && iME != unsigned(-1))
0200           return meSet_->getME(iME)->getBinError(iBin);
0201         else
0202           return 0.;
0203       }
0204       double getBinEntries() const {
0205         if (meSet_ && iME != unsigned(-1))
0206           return meSet_->getME(iME)->getBinEntries(iBin);
0207         else
0208           return 0.;
0209       }
0210       MonitorElement const *getME() const {
0211         if (meSet_ && iME != unsigned(-1))
0212           return meSet_->getME(iME);
0213         else
0214           return nullptr;
0215       }
0216       void setMESet(MESet const &_meSet) { meSet_ = &_meSet; }
0217       MESet const *getMESet() const { return meSet_; }
0218     };
0219 
0220     struct Bin : public ConstBin {
0221     protected:
0222       MESet *meSet_;
0223 
0224     public:
0225       Bin() : ConstBin(), meSet_(nullptr) {}
0226       Bin(MESet &_set, unsigned _iME = 0, int _iBin = 1) : ConstBin(_set, _iME, _iBin), meSet_(&_set) {}
0227       Bin(Bin const &_orig) : ConstBin(_orig), meSet_(_orig.meSet_) {}
0228       ConstBin &operator=(Bin const &_rhs) {
0229         bool wasNull(ConstBin::meSet_ == nullptr);
0230         ConstBin::operator=(_rhs);
0231         if (wasNull)
0232           meSet_ = _rhs.meSet_;
0233         return *this;
0234       }
0235       void fill(double _w = 1.) {
0236         if (meSet_)
0237           meSet_->fill_(iME, iBin, _w);
0238       }
0239       void fill(double _y, double _w = 1.) {
0240         if (meSet_)
0241           meSet_->fill_(iME, iBin, _y, _w);
0242       }
0243       void setBinContent(double _content) {
0244         if (meSet_ && iME != unsigned(-1))
0245           meSet_->getME(iME)->setBinContent(iBin, _content);
0246       }
0247       void setBinError(double _error) {
0248         if (meSet_ && iME != unsigned(-1))
0249           meSet_->getME(iME)->setBinError(iBin, _error);
0250       }
0251       void setBinEntries(double _entries) {
0252         if (meSet_ && iME != unsigned(-1))
0253           meSet_->getME(iME)->setBinEntries(iBin, _entries);
0254       }
0255       MonitorElement *getME() const {
0256         if (meSet_ && iME != unsigned(-1))
0257           return meSet_->getME(iME);
0258         else
0259           return nullptr;
0260       }
0261       void setMESet(MESet &_meSet) {
0262         ConstBin::meSet_ = &_meSet;
0263         meSet_ = &_meSet;
0264       }
0265       MESet *getMESet() const { return meSet_; }
0266     };
0267 
0268     /* const_iterator
0269      iterates over bins
0270      supports automatic transition between MEs in the same set
0271      underflow -> bin == 0 overflow -> bin == -1
0272   */
0273     struct const_iterator {
0274       const_iterator() : bin_() {}
0275       const_iterator(EcalElectronicsMapping const *, MESet const &_meSet, unsigned _iME = 0, int _iBin = 1)
0276           : bin_(_meSet, _iME, _iBin) {}
0277       const_iterator(EcalElectronicsMapping const *, MESet const &, DetId const &);
0278       const_iterator(const_iterator const &_orig) : bin_(_orig.bin_) {}
0279       const_iterator &operator=(const_iterator const &_rhs) {
0280         bin_ = _rhs.bin_;
0281         return *this;
0282       }
0283       bool operator==(const_iterator const &_rhs) const { return bin_ == _rhs.bin_; }
0284       bool operator!=(const_iterator const &_rhs) const { return !(bin_ == _rhs.bin_); }
0285       ConstBin const *operator->() const { return &bin_; }
0286       const_iterator &operator++();
0287       const_iterator &toNextChannel(EcalElectronicsMapping const *);
0288       bool up();
0289       bool down();
0290       bool left();
0291       bool right();
0292 
0293     protected:
0294       ConstBin bin_;
0295     };
0296 
0297     struct iterator : public const_iterator {
0298       iterator() : const_iterator(), bin_() {}
0299       iterator(EcalElectronicsMapping const *electronicsMap, MESet &_meSet, unsigned _iME = 0, int _iBin = 1)
0300           : const_iterator(electronicsMap, _meSet, _iME, _iBin), bin_(_meSet) {
0301         bin_.ConstBin::operator=(const_iterator::bin_);
0302       }
0303       iterator(EcalElectronicsMapping const *electronicsMap, MESet &_meSet, DetId const &_id)
0304           : const_iterator(electronicsMap, _meSet, _id), bin_(_meSet) {
0305         bin_.ConstBin::operator=(const_iterator::bin_);
0306       }
0307       iterator(iterator const &_orig) : const_iterator(_orig), bin_(_orig.bin_) {}
0308       iterator &operator=(const_iterator const &_rhs) {
0309         const_iterator::operator=(_rhs);
0310         bin_.ConstBin::operator=(const_iterator::bin_);
0311         return *this;
0312       }
0313       Bin *operator->() { return &bin_; }
0314       Bin const *operator->() const { return &bin_; }
0315       const_iterator &operator++() {
0316         const_iterator::operator++();
0317         bin_.ConstBin::operator=(const_iterator::bin_);
0318         return *this;
0319       }
0320       const_iterator &toNextChannel(EcalElectronicsMapping const *electronicsMap) {
0321         const_iterator::toNextChannel(electronicsMap);
0322         bin_.ConstBin::operator=(const_iterator::bin_);
0323         return *this;
0324       }
0325       bool up() {
0326         bool res(const_iterator::up());
0327         bin_.ConstBin::operator=(const_iterator::bin_);
0328         return res;
0329       }
0330       bool down() {
0331         bool res(const_iterator::down());
0332         bin_.ConstBin::operator=(const_iterator::bin_);
0333         return res;
0334       }
0335       bool left() {
0336         bool res(const_iterator::left());
0337         bin_.ConstBin::operator=(const_iterator::bin_);
0338         return res;
0339       }
0340       bool right() {
0341         bool res(const_iterator::right());
0342         bin_.ConstBin::operator=(const_iterator::bin_);
0343         return res;
0344       }
0345 
0346     private:
0347       Bin bin_;
0348     };
0349 
0350     virtual const_iterator begin(EcalElectronicsMapping const *electronicsMap) const {
0351       return const_iterator(electronicsMap, *this);
0352     }
0353 
0354     virtual const_iterator end(EcalElectronicsMapping const *electronicsMap) const {
0355       return const_iterator(electronicsMap, *this, -1, -1);
0356     }
0357 
0358     virtual const_iterator beginChannel(EcalElectronicsMapping const *electronicsMap) const {
0359       const_iterator itr(electronicsMap, *this, 0, 0);
0360       return itr.toNextChannel(electronicsMap);
0361     }
0362 
0363     virtual iterator begin(EcalElectronicsMapping const *electronicsMap) { return iterator(electronicsMap, *this); }
0364 
0365     virtual iterator end(EcalElectronicsMapping const *electronicsMap) {
0366       return iterator(electronicsMap, *this, -1, -1);
0367     }
0368 
0369     virtual iterator beginChannel(EcalElectronicsMapping const *electronicsMap) {
0370       iterator itr(electronicsMap, *this, 0, 0);
0371       itr.toNextChannel(electronicsMap);
0372       return itr;
0373     }
0374   };
0375 
0376 }  // namespace ecaldqm
0377 
0378 namespace ecaldqm {
0379 
0380   class MESetCollection {
0381     using MESetColletionType = std::map<std::string, std::unique_ptr<MESet>>;
0382 
0383   public:
0384     using iterator = MESetColletionType::iterator;
0385     using const_iterator = MESetColletionType::const_iterator;
0386 
0387     void insert(const std::string &key, MESet *ptr) { _MESetColletion.emplace(key, ptr); }
0388     void insert(const std::string &&key, MESet *ptr) { _MESetColletion.emplace(key, ptr); }
0389 
0390     void erase(const std::string &key) { _MESetColletion.erase(key); }
0391 
0392     auto begin() { return _MESetColletion.begin(); }
0393     auto end() const { return _MESetColletion.end(); }
0394 
0395     const_iterator find(const std::string &key) const { return _MESetColletion.find(key); }
0396     iterator find(const std::string &key) { return _MESetColletion.find(key); }
0397 
0398     //return a reference, but this collection still has the ownership
0399     MESet &at(const std::string &key) { return *(_MESetColletion.at(key).get()); }
0400 
0401   private:
0402     MESetColletionType _MESetColletion;
0403   };
0404 
0405 }  // namespace ecaldqm
0406 
0407 #endif