Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:07:14

0001 #include "DQM/EcalCommon/interface/MESetMulti.h"
0002 
0003 namespace ecaldqm {
0004   MESetMulti::MESetMulti(MESet const &_seed, ReplCandidates const &_replCandidates)
0005       : MESet(_seed), current_(nullptr), sets_(), replCandidates_(_replCandidates) {
0006     PathReplacements replacements;
0007     std::map<std::string, unsigned> indices;
0008     // recursive function to set replacements
0009     // indices gives the multi index in each dimension
0010     // dimensions are alphanumerically ordered from the use of std::map
0011     std::function<bool(typename ReplCandidates::const_iterator &)> setReplacements(
0012         [&setReplacements, &replacements, &indices, this](typename ReplCandidates::const_iterator &_rItr) {
0013           unsigned &index(indices[_rItr->first]);
0014           replacements[_rItr->first] = _rItr->second[index];
0015           // one dimension set, go to next
0016           ++_rItr;
0017           if (_rItr == this->replCandidates_.end()) {
0018             // this is the last dimension. Increment the index and retutn to the
0019             // first
0020             _rItr = this->replCandidates_.begin();
0021             ++index;
0022           } else if (setReplacements(_rItr))
0023             ++index;
0024 
0025           if (index != _rItr->second.size())
0026             return false;
0027           // index has counted to the maximum of this dimension, carry over
0028           index = 0;
0029           return true;
0030         });
0031 
0032     // [dim0 = 0, dim1 = 0] -> 0, [dim0 = 0, dim1 = 1] -> 1, ...
0033     while (true) {
0034       replacements.clear();
0035       typename ReplCandidates::const_iterator rItr(replCandidates_.begin());
0036       bool last(setReplacements(rItr));
0037       sets_.push_back(_seed.clone(formPath(replacements)));
0038       if (last)
0039         break;
0040     }
0041 
0042     current_ = sets_[0];
0043   }
0044 
0045   MESetMulti::MESetMulti(MESetMulti const &_orig)
0046       : MESet(_orig), current_(nullptr), sets_(_orig.sets_.size(), nullptr), replCandidates_(_orig.replCandidates_) {
0047     if (sets_.empty())
0048       return;
0049 
0050     for (unsigned iS(0); iS < sets_.size(); ++iS) {
0051       if (!_orig.sets_[iS])
0052         continue;
0053       sets_[iS] = _orig.sets_[iS]->clone();
0054       if (_orig.sets_[iS] == _orig.current_)
0055         current_ = sets_[iS];
0056     }
0057   }
0058 
0059   MESetMulti::~MESetMulti() {
0060     for (unsigned iS(0); iS < sets_.size(); ++iS)
0061       delete sets_[iS];
0062   }
0063 
0064   MESet &MESetMulti::operator=(MESet const &_rhs) {
0065     for (unsigned iS(0); iS < sets_.size(); ++iS)
0066       delete sets_[iS];
0067     sets_.clear();
0068     current_ = nullptr;
0069 
0070     MESetMulti const *pRhs(dynamic_cast<MESetMulti const *>(&_rhs));
0071     if (pRhs) {
0072       sets_.assign(pRhs->sets_.size(), nullptr);
0073 
0074       for (unsigned iS(0); iS < pRhs->sets_.size(); ++iS) {
0075         sets_[iS] = pRhs->sets_[iS]->clone();
0076         if (pRhs->sets_[iS] == pRhs->current_)
0077           current_ = sets_[iS];
0078       }
0079 
0080       replCandidates_ = pRhs->replCandidates_;
0081     }
0082 
0083     return MESet::operator=(_rhs);
0084   }
0085 
0086   MESet *MESetMulti::clone(std::string const &_path /* = ""*/) const {
0087     std::string path(path_);
0088     if (!_path.empty())
0089       path_ = _path;
0090     MESet *copy(new MESetMulti(*this));
0091     path_ = path;
0092     return copy;
0093   }
0094 
0095   void MESetMulti::book(DQMStore::IBooker &_ibooker, EcalElectronicsMapping const *electronicsMap) {
0096     for (unsigned iS(0); iS < sets_.size(); ++iS)
0097       sets_[iS]->book(_ibooker, electronicsMap);
0098 
0099     active_ = true;
0100   }
0101 
0102   bool MESetMulti::retrieve(EcalElectronicsMapping const *electronicsMap,
0103                             DQMStore::IGetter &_igetter,
0104                             std::string *_failedPath /* = 0*/) const {
0105     for (unsigned iS(0); iS < sets_.size(); ++iS)
0106       if (!sets_[iS]->retrieve(electronicsMap, _igetter, _failedPath))
0107         return false;
0108 
0109     active_ = true;
0110     return true;
0111   }
0112 
0113   void MESetMulti::clear() const {
0114     for (unsigned iS(0); iS < sets_.size(); ++iS)
0115       sets_[iS]->clear();
0116 
0117     active_ = false;
0118   }
0119 
0120   void MESetMulti::reset(EcalElectronicsMapping const *electronicsMap,
0121                          double _content /* = 0*/,
0122                          double _error /* = 0.*/,
0123                          double _entries /* = 0.*/) {
0124     for (unsigned iS(0); iS < sets_.size(); ++iS)
0125       sets_[iS]->reset(electronicsMap, _content, _error, _entries);
0126   }
0127 
0128   void MESetMulti::resetAll(double _content /* = 0*/, double _error /* = 0.*/, double _entries /* = 0.*/) {
0129     for (unsigned iS(0); iS < sets_.size(); ++iS)
0130       sets_[iS]->resetAll(_content, _error, _entries);
0131   }
0132 
0133   void MESetMulti::use(unsigned _iSet) const {
0134     if (_iSet >= sets_.size())
0135       throw_("MESetMulti index out of range");
0136 
0137     current_ = sets_[_iSet];
0138   }
0139 
0140   unsigned MESetMulti::getIndex(PathReplacements const &_replacements) const {
0141     unsigned index(0);
0142     unsigned base(1);
0143     for (typename ReplCandidates::const_reverse_iterator cItr(replCandidates_.rbegin()); cItr != replCandidates_.rend();
0144          ++cItr) {
0145       typename PathReplacements::const_iterator rItr(_replacements.find(cItr->first));
0146       if (rItr == _replacements.end())
0147         throw_(cItr->first + " not given in the key for getIndex");
0148       unsigned nC(cItr->second.size());
0149       unsigned iR(0);
0150       for (; iR != nC; ++iR)
0151         if (rItr->second == cItr->second[iR])
0152           break;
0153       if (iR == nC)
0154         throw_(rItr->second + " not found in replacement candidates");
0155       index += iR * base;
0156       base *= nC;
0157     }
0158 
0159     return index;
0160   }
0161 }  // namespace ecaldqm