Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-25 05:06:25

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     [[noreturn]] void throw_(std::string const &_message) const {
0150       throw cms::Exception("EcalDQM") << path_ << ": " << _message;
0151     }
0152 
0153     mutable std::vector<MonitorElement *> mes_;
0154 
0155     mutable std::string path_;
0156     binning::ObjectType otype_;
0157     binning::BinningType btype_;
0158     MonitorElement::Kind kind_;
0159     bool lumiFlag_;   // when true, histograms will be saved every lumi section
0160                       // (default false)
0161     bool batchMode_;  // when true, histograms are not GUI-ready (default false)
0162 
0163     mutable bool active_;
0164 
0165   public:
0166     struct ConstBin {
0167     protected:
0168       MESet const *meSet_;
0169 
0170     public:
0171       unsigned iME;
0172       int iBin;
0173       binning::ObjectType otype;
0174 
0175       ConstBin() : meSet_(nullptr), iME(-1), iBin(-1), otype(binning::nObjType) {}
0176       ConstBin(MESet const &, unsigned = 0, int = 1);
0177       ConstBin(ConstBin const &_orig) : meSet_(_orig.meSet_), iME(_orig.iME), iBin(_orig.iBin), otype(_orig.otype) {}
0178       ConstBin &operator=(ConstBin const &);
0179       bool operator==(ConstBin const &_rhs) const {
0180         return meSet_ != nullptr && meSet_ == _rhs.meSet_ && iME == _rhs.iME && iBin == _rhs.iBin;
0181       }
0182       bool isChannel(EcalElectronicsMapping const *electronicsMap) const {
0183         if (meSet_)
0184           return binning::isValidIdBin(electronicsMap, otype, meSet_->getBinType(), iME, iBin);
0185         else
0186           return false;
0187       }
0188       uint32_t getId() const {
0189         if (meSet_)
0190           return binning::idFromBin(otype, meSet_->getBinType(), iME, iBin);
0191         else
0192           return 0;
0193       }
0194       double getBinContent() const {
0195         if (meSet_ && iME != unsigned(-1))
0196           return meSet_->getME(iME)->getBinContent(iBin);
0197         else
0198           return 0.;
0199       }
0200       double getBinError() const {
0201         if (meSet_ && iME != unsigned(-1))
0202           return meSet_->getME(iME)->getBinError(iBin);
0203         else
0204           return 0.;
0205       }
0206       double getBinEntries() const {
0207         if (meSet_ && iME != unsigned(-1))
0208           return meSet_->getME(iME)->getBinEntries(iBin);
0209         else
0210           return 0.;
0211       }
0212       MonitorElement const *getME() const {
0213         if (meSet_ && iME != unsigned(-1))
0214           return meSet_->getME(iME);
0215         else
0216           return nullptr;
0217       }
0218       void setMESet(MESet const &_meSet) { meSet_ = &_meSet; }
0219       MESet const *getMESet() const { return meSet_; }
0220     };
0221 
0222     struct Bin : public ConstBin {
0223     protected:
0224       MESet *meSet_;
0225 
0226     public:
0227       Bin() : ConstBin(), meSet_(nullptr) {}
0228       Bin(MESet &_set, unsigned _iME = 0, int _iBin = 1) : ConstBin(_set, _iME, _iBin), meSet_(&_set) {}
0229       Bin(Bin const &_orig) : ConstBin(_orig), meSet_(_orig.meSet_) {}
0230       ConstBin &operator=(Bin const &_rhs) {
0231         bool wasNull(ConstBin::meSet_ == nullptr);
0232         ConstBin::operator=(_rhs);
0233         if (wasNull)
0234           meSet_ = _rhs.meSet_;
0235         return *this;
0236       }
0237       void fill(double _w = 1.) {
0238         if (meSet_)
0239           meSet_->fill_(iME, iBin, _w);
0240       }
0241       void fill(double _y, double _w = 1.) {
0242         if (meSet_)
0243           meSet_->fill_(iME, iBin, _y, _w);
0244       }
0245       void setBinContent(double _content) {
0246         if (meSet_ && iME != unsigned(-1))
0247           meSet_->getME(iME)->setBinContent(iBin, _content);
0248       }
0249       void setBinError(double _error) {
0250         if (meSet_ && iME != unsigned(-1))
0251           meSet_->getME(iME)->setBinError(iBin, _error);
0252       }
0253       void setBinEntries(double _entries) {
0254         if (meSet_ && iME != unsigned(-1))
0255           meSet_->getME(iME)->setBinEntries(iBin, _entries);
0256       }
0257       MonitorElement *getME() const {
0258         if (meSet_ && iME != unsigned(-1))
0259           return meSet_->getME(iME);
0260         else
0261           return nullptr;
0262       }
0263       void setMESet(MESet &_meSet) {
0264         ConstBin::meSet_ = &_meSet;
0265         meSet_ = &_meSet;
0266       }
0267       MESet *getMESet() const { return meSet_; }
0268     };
0269 
0270     /* const_iterator
0271      iterates over bins
0272      supports automatic transition between MEs in the same set
0273      underflow -> bin == 0 overflow -> bin == -1
0274   */
0275     struct const_iterator {
0276       const_iterator() : bin_() {}
0277       const_iterator(EcalElectronicsMapping const *, MESet const &_meSet, unsigned _iME = 0, int _iBin = 1)
0278           : bin_(_meSet, _iME, _iBin) {}
0279       const_iterator(EcalElectronicsMapping const *, MESet const &, DetId const &);
0280       const_iterator(const_iterator const &_orig) = default;
0281       const_iterator(const_iterator &&_orig) = default;
0282       const_iterator &operator=(const_iterator const &_orig) = default;
0283       const_iterator &operator=(const_iterator &&_orig) = default;
0284       bool operator==(const_iterator const &_rhs) const { return bin_ == _rhs.bin_; }
0285       bool operator!=(const_iterator const &_rhs) const { return !(bin_ == _rhs.bin_); }
0286       ConstBin const *operator->() const { return &bin_; }
0287       const_iterator &operator++();
0288       const_iterator &toNextChannel(EcalElectronicsMapping const *);
0289       bool up();
0290       bool down();
0291       bool left();
0292       bool right();
0293 
0294     protected:
0295       ConstBin bin_;
0296     };
0297 
0298     struct iterator : public const_iterator {
0299       iterator() : const_iterator(), bin_() {}
0300       iterator(EcalElectronicsMapping const *electronicsMap, MESet &_meSet, unsigned _iME = 0, int _iBin = 1)
0301           : const_iterator(electronicsMap, _meSet, _iME, _iBin), bin_(_meSet) {
0302         bin_.ConstBin::operator=(const_iterator::bin_);
0303       }
0304       iterator(EcalElectronicsMapping const *electronicsMap, MESet &_meSet, DetId const &_id)
0305           : const_iterator(electronicsMap, _meSet, _id), bin_(_meSet) {
0306         bin_.ConstBin::operator=(const_iterator::bin_);
0307       }
0308       iterator(iterator const &_orig) = default;
0309       iterator &operator=(iterator const &) = default;
0310       iterator &operator=(const_iterator const &_rhs) {
0311         const_iterator::operator=(_rhs);
0312         bin_.ConstBin::operator=(const_iterator::bin_);
0313         return *this;
0314       }
0315       Bin *operator->() { return &bin_; }
0316       Bin const *operator->() const { return &bin_; }
0317       const_iterator &operator++() {
0318         const_iterator::operator++();
0319         bin_.ConstBin::operator=(const_iterator::bin_);
0320         return *this;
0321       }
0322       const_iterator &toNextChannel(EcalElectronicsMapping const *electronicsMap) {
0323         const_iterator::toNextChannel(electronicsMap);
0324         bin_.ConstBin::operator=(const_iterator::bin_);
0325         return *this;
0326       }
0327       bool up() {
0328         bool res(const_iterator::up());
0329         bin_.ConstBin::operator=(const_iterator::bin_);
0330         return res;
0331       }
0332       bool down() {
0333         bool res(const_iterator::down());
0334         bin_.ConstBin::operator=(const_iterator::bin_);
0335         return res;
0336       }
0337       bool left() {
0338         bool res(const_iterator::left());
0339         bin_.ConstBin::operator=(const_iterator::bin_);
0340         return res;
0341       }
0342       bool right() {
0343         bool res(const_iterator::right());
0344         bin_.ConstBin::operator=(const_iterator::bin_);
0345         return res;
0346       }
0347 
0348     private:
0349       Bin bin_;
0350     };
0351 
0352     virtual const_iterator begin(EcalElectronicsMapping const *electronicsMap) const {
0353       return const_iterator(electronicsMap, *this);
0354     }
0355 
0356     virtual const_iterator end(EcalElectronicsMapping const *electronicsMap) const {
0357       return const_iterator(electronicsMap, *this, -1, -1);
0358     }
0359 
0360     virtual const_iterator beginChannel(EcalElectronicsMapping const *electronicsMap) const {
0361       const_iterator itr(electronicsMap, *this, 0, 0);
0362       return itr.toNextChannel(electronicsMap);
0363     }
0364 
0365     virtual iterator begin(EcalElectronicsMapping const *electronicsMap) { return iterator(electronicsMap, *this); }
0366 
0367     virtual iterator end(EcalElectronicsMapping const *electronicsMap) {
0368       return iterator(electronicsMap, *this, -1, -1);
0369     }
0370 
0371     virtual iterator beginChannel(EcalElectronicsMapping const *electronicsMap) {
0372       iterator itr(electronicsMap, *this, 0, 0);
0373       itr.toNextChannel(electronicsMap);
0374       return itr;
0375     }
0376   };
0377 
0378 }  // namespace ecaldqm
0379 
0380 namespace ecaldqm {
0381 
0382   class MESetCollection {
0383     using MESetColletionType = std::map<std::string, std::unique_ptr<MESet>>;
0384 
0385   public:
0386     using iterator = MESetColletionType::iterator;
0387     using const_iterator = MESetColletionType::const_iterator;
0388 
0389     void insert(const std::string &key, MESet *ptr) { _MESetColletion.emplace(key, ptr); }
0390     void insert(const std::string &&key, MESet *ptr) { _MESetColletion.emplace(key, ptr); }
0391 
0392     void erase(const std::string &key) { _MESetColletion.erase(key); }
0393 
0394     auto begin() { return _MESetColletion.begin(); }
0395     auto end() const { return _MESetColletion.end(); }
0396 
0397     const_iterator find(const std::string &key) const { return _MESetColletion.find(key); }
0398     iterator find(const std::string &key) { return _MESetColletion.find(key); }
0399 
0400     //return a reference, but this collection still has the ownership
0401     MESet &at(const std::string &key) { return *(_MESetColletion.at(key).get()); }
0402 
0403   private:
0404     MESetColletionType _MESetColletion;
0405   };
0406 
0407 }  // namespace ecaldqm
0408 
0409 #endif