Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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     unsigned iM(0);
0034     while (true) {
0035       replacements.clear();
0036       typename ReplCandidates::const_iterator rItr(replCandidates_.begin());
0037       bool last(setReplacements(rItr));
0038       sets_.push_back(_seed.clone(formPath(replacements)));
0039       if (last)
0040         break;
0041       ++iM;
0042     }
0043 
0044     current_ = sets_[0];
0045   }
0046 
0047   MESetMulti::MESetMulti(MESetMulti const &_orig)
0048       : MESet(_orig), current_(nullptr), sets_(_orig.sets_.size(), nullptr), replCandidates_(_orig.replCandidates_) {
0049     if (sets_.empty())
0050       return;
0051 
0052     for (unsigned iS(0); iS < sets_.size(); ++iS) {
0053       if (!_orig.sets_[iS])
0054         continue;
0055       sets_[iS] = _orig.sets_[iS]->clone();
0056       if (_orig.sets_[iS] == _orig.current_)
0057         current_ = sets_[iS];
0058     }
0059   }
0060 
0061   MESetMulti::~MESetMulti() {
0062     for (unsigned iS(0); iS < sets_.size(); ++iS)
0063       delete sets_[iS];
0064   }
0065 
0066   MESet &MESetMulti::operator=(MESet const &_rhs) {
0067     for (unsigned iS(0); iS < sets_.size(); ++iS)
0068       delete sets_[iS];
0069     sets_.clear();
0070     current_ = nullptr;
0071 
0072     MESetMulti const *pRhs(dynamic_cast<MESetMulti const *>(&_rhs));
0073     if (pRhs) {
0074       sets_.assign(pRhs->sets_.size(), nullptr);
0075 
0076       for (unsigned iS(0); iS < pRhs->sets_.size(); ++iS) {
0077         sets_[iS] = pRhs->sets_[iS]->clone();
0078         if (pRhs->sets_[iS] == pRhs->current_)
0079           current_ = sets_[iS];
0080       }
0081 
0082       replCandidates_ = pRhs->replCandidates_;
0083     }
0084 
0085     return MESet::operator=(_rhs);
0086   }
0087 
0088   MESet *MESetMulti::clone(std::string const &_path /* = ""*/) const {
0089     std::string path(path_);
0090     if (!_path.empty())
0091       path_ = _path;
0092     MESet *copy(new MESetMulti(*this));
0093     path_ = path;
0094     return copy;
0095   }
0096 
0097   void MESetMulti::book(DQMStore::IBooker &_ibooker, EcalElectronicsMapping const *electronicsMap) {
0098     for (unsigned iS(0); iS < sets_.size(); ++iS)
0099       sets_[iS]->book(_ibooker, electronicsMap);
0100 
0101     active_ = true;
0102   }
0103 
0104   bool MESetMulti::retrieve(EcalElectronicsMapping const *electronicsMap,
0105                             DQMStore::IGetter &_igetter,
0106                             std::string *_failedPath /* = 0*/) const {
0107     for (unsigned iS(0); iS < sets_.size(); ++iS)
0108       if (!sets_[iS]->retrieve(electronicsMap, _igetter, _failedPath))
0109         return false;
0110 
0111     active_ = true;
0112     return true;
0113   }
0114 
0115   void MESetMulti::clear() const {
0116     for (unsigned iS(0); iS < sets_.size(); ++iS)
0117       sets_[iS]->clear();
0118 
0119     active_ = false;
0120   }
0121 
0122   void MESetMulti::reset(EcalElectronicsMapping const *electronicsMap,
0123                          double _content /* = 0*/,
0124                          double _error /* = 0.*/,
0125                          double _entries /* = 0.*/) {
0126     for (unsigned iS(0); iS < sets_.size(); ++iS)
0127       sets_[iS]->reset(electronicsMap, _content, _error, _entries);
0128   }
0129 
0130   void MESetMulti::resetAll(double _content /* = 0*/, double _error /* = 0.*/, double _entries /* = 0.*/) {
0131     for (unsigned iS(0); iS < sets_.size(); ++iS)
0132       sets_[iS]->resetAll(_content, _error, _entries);
0133   }
0134 
0135   void MESetMulti::use(unsigned _iSet) const {
0136     if (_iSet >= sets_.size())
0137       throw_("MESetMulti index out of range");
0138 
0139     current_ = sets_[_iSet];
0140   }
0141 
0142   unsigned MESetMulti::getIndex(PathReplacements const &_replacements) const {
0143     unsigned index(0);
0144     unsigned base(1);
0145     for (typename ReplCandidates::const_reverse_iterator cItr(replCandidates_.rbegin()); cItr != replCandidates_.rend();
0146          ++cItr) {
0147       typename PathReplacements::const_iterator rItr(_replacements.find(cItr->first));
0148       if (rItr == _replacements.end())
0149         throw_(cItr->first + " not given in the key for getIndex");
0150       unsigned nC(cItr->second.size());
0151       unsigned iR(0);
0152       for (; iR != nC; ++iR)
0153         if (rItr->second == cItr->second[iR])
0154           break;
0155       if (iR == nC)
0156         throw_(rItr->second + " not found in replacement candidates");
0157       index += iR * base;
0158       base *= nC;
0159     }
0160 
0161     return index;
0162   }
0163 }  // namespace ecaldqm