Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DQM/EcalCommon/interface/MESet.h"
0002 
0003 #include "DQM/EcalCommon/interface/EcalDQMCommonUtils.h"
0004 #include "DQM/EcalCommon/interface/MESetUtils.h"
0005 #include "DQM/EcalCommon/interface/StatusManager.h"
0006 
0007 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0008 #include "FWCore/ServiceRegistry/interface/Service.h"
0009 
0010 #include "TPRegexp.h"
0011 #include "TString.h"
0012 
0013 namespace ecaldqm {
0014   MESet::MESet()
0015       : mes_(0),
0016         path_(""),
0017         otype_(binning::nObjType),
0018         btype_(binning::nBinType),
0019         kind_(MonitorElement::Kind::INVALID),
0020         lumiFlag_(false),
0021         batchMode_(false),
0022         active_(false) {}
0023 
0024   MESet::MESet(std::string const &_path,
0025                binning::ObjectType _otype,
0026                binning::BinningType _btype,
0027                MonitorElement::Kind _kind)
0028       : mes_(0),
0029         path_(_path),
0030         otype_(_otype),
0031         btype_(_btype),
0032         kind_(_kind),
0033         lumiFlag_(false),
0034         batchMode_(false),
0035         active_(false) {
0036     if (path_.empty() || path_.find('/') == std::string::npos ||
0037         (otype_ != binning::kChannel && path_.find('/') == path_.size() - 1))
0038       throw_(_path + " cannot be used for ME path name");
0039 
0040     switch (kind_) {
0041       case MonitorElement::Kind::REAL:
0042       case MonitorElement::Kind::TH1F:
0043       case MonitorElement::Kind::TPROFILE:
0044       case MonitorElement::Kind::TH2F:
0045       case MonitorElement::Kind::TPROFILE2D:
0046         break;
0047       default:
0048         throw_("Unsupported MonitorElement kind");
0049     }
0050   }
0051 
0052   MESet::MESet(MESet const &_orig)
0053       : mes_(_orig.mes_),
0054         path_(_orig.path_),
0055         otype_(_orig.otype_),
0056         btype_(_orig.btype_),
0057         kind_(_orig.kind_),
0058         lumiFlag_(_orig.lumiFlag_),
0059         batchMode_(_orig.batchMode_),
0060         active_(_orig.active_) {}
0061 
0062   MESet::~MESet() {}
0063 
0064   MESet &MESet::operator=(MESet const &_rhs) {
0065     mes_ = _rhs.mes_;
0066     path_ = _rhs.path_;
0067     otype_ = _rhs.otype_;
0068     btype_ = _rhs.btype_;
0069     kind_ = _rhs.kind_;
0070     active_ = _rhs.active_;
0071 
0072     return *this;
0073   }
0074 
0075   MESet *MESet::clone(std::string const &_path /* = ""*/) const {
0076     std::string path(path_);
0077     if (!_path.empty())
0078       path_ = _path;
0079     MESet *copy(new MESet(*this));
0080     path_ = path;
0081     return copy;
0082   }
0083 
0084   void MESet::clear() const {
0085     active_ = false;
0086     mes_.clear();
0087   }
0088 
0089   void MESet::setAxisTitle(std::string const &_title, int _axis /* = 1*/) {
0090     if (!active_)
0091       return;
0092 
0093     unsigned nME(mes_.size());
0094     for (unsigned iME(0); iME < nME; iME++)
0095       mes_[iME]->setAxisTitle(_title, _axis);
0096   }
0097 
0098   void MESet::reset(EcalElectronicsMapping const *electronicsMap,
0099                     double _content /* = 0.*/,
0100                     double _err /* = 0.*/,
0101                     double _entries /* = 0.*/) {
0102     if (!active_)
0103       return;
0104 
0105     resetAll(_content, _err, _entries);
0106   }
0107 
0108   void MESet::resetAll(double _content /* = 0.*/, double _err /* = 0.*/, double _entries /* = 0.*/) {
0109     if (!active_)
0110       return;
0111 
0112     unsigned nME(mes_.size());
0113 
0114     if (kind_ == MonitorElement::Kind::REAL) {
0115       for (unsigned iME(0); iME < nME; iME++)
0116         mes_[iME]->Fill(_content);
0117       return;
0118     }
0119 
0120     bool simple(true);
0121     if (_content != 0. || _err != 0. || _entries != 0.)
0122       simple = false;
0123 
0124     for (unsigned iME(0); iME < nME; iME++) {
0125       TH1 *h(mes_[iME]->getTH1());
0126       h->Reset();
0127       if (simple)
0128         continue;
0129 
0130       int nbinsX(h->GetNbinsX());
0131       int nbinsY(h->GetNbinsY());
0132       double entries(0.);
0133       for (int ix(1); ix <= nbinsX; ix++) {
0134         for (int iy(1); iy <= nbinsY; iy++) {
0135           int bin(h->GetBin(ix, iy));
0136           h->SetBinContent(bin, _content);
0137           h->SetBinError(bin, _err);
0138           if (kind_ == MonitorElement::Kind::TPROFILE) {
0139             static_cast<TProfile *>(h)->SetBinEntries(bin, _entries);
0140             entries += _entries;
0141           } else if (kind_ == MonitorElement::Kind::TPROFILE2D) {
0142             static_cast<TProfile2D *>(h)->SetBinEntries(bin, _entries);
0143             entries += _entries;
0144           }
0145         }
0146       }
0147       if (entries == 0.)
0148         entries = _entries;
0149       h->SetEntries(_entries);
0150     }
0151   }
0152 
0153   std::string MESet::formPath(std::map<std::string, std::string> const &_replacements) const {
0154     TString path(path_);
0155 
0156     for (typename MESet::PathReplacements::const_iterator repItr(_replacements.begin()); repItr != _replacements.end();
0157          ++repItr) {
0158       TString pattern("\\%\\(");
0159       pattern += repItr->first;
0160       pattern += "\\)s";
0161 
0162       TPRegexp re(pattern);
0163 
0164       re.Substitute(path, repItr->second, "g");
0165     }
0166 
0167     return path.Data();
0168   }
0169 
0170   bool MESet::maskMatches(DetId const &_id,
0171                           uint32_t _mask,
0172                           StatusManager const *_statusManager,
0173                           EcalTrigTowerConstituentsMap const *trigTowerMap) const {
0174     if (!_statusManager)
0175       return false;
0176 
0177     if ((_statusManager->getStatus(_id.rawId()) & _mask) != 0)
0178       return true;
0179 
0180     int subdet(_id.subdetId());
0181 
0182     bool searchNeighborsInTower(btype_ == binning::kTriggerTower || btype_ == binning::kSuperCrystal);
0183 
0184     // turn off masking for good channel for the time being
0185     // update the RenderPlugin then enable again
0186 
0187     switch (subdet) {
0188       case EcalBarrel:  // this is a DetId for single crystal in barrel
0189       {
0190         EBDetId ebId(_id);
0191         EcalTrigTowerDetId ttId(ebId.tower());
0192         if ((_statusManager->getStatus(ttId.rawId()) & _mask) != 0)
0193           return true;
0194 
0195         if (searchNeighborsInTower) {
0196           std::vector<DetId> ids(trigTowerMap->constituentsOf(ttId));
0197           for (std::vector<DetId>::iterator idItr(ids.begin()); idItr != ids.end(); ++idItr)
0198             if ((_statusManager->getStatus(idItr->rawId()) & _mask) != 0)
0199               return true;
0200         }
0201       } break;
0202 
0203       case EcalEndcap:
0204         if (isEcalScDetId(_id)) {
0205           EcalScDetId scId(_id);
0206           for (int ix(1); ix <= 5; ix++) {
0207             for (int iy(1); iy <= 5; iy++) {
0208               int iix((scId.ix() - 1) * 5 + ix);
0209               int iiy((scId.iy() - 1) * 5 + iy);
0210               if (!EEDetId::validDetId(iix, iiy, scId.zside()))
0211                 continue;
0212               if ((_statusManager->getStatus(EEDetId(iix, iiy, scId.zside()).rawId()) & _mask) != 0)
0213                 return true;
0214             }
0215           }
0216         } else {
0217           EEDetId eeId(_id);
0218           EcalScDetId scId(eeId.sc());
0219           if ((_statusManager->getStatus(scId.rawId()) & _mask) != 0)
0220             return true;
0221 
0222           if (searchNeighborsInTower) {
0223             for (int ix(1); ix <= 5; ix++) {
0224               for (int iy(1); iy <= 5; iy++) {
0225                 int iix((scId.ix() - 1) * 5 + ix);
0226                 int iiy((scId.iy() - 1) * 5 + iy);
0227                 if (!EEDetId::validDetId(iix, iiy, scId.zside()))
0228                   continue;
0229                 if ((_statusManager->getStatus(EEDetId(iix, iiy, scId.zside()).rawId()) & _mask) != 0)
0230                   return true;
0231               }
0232             }
0233           }
0234         }
0235         break;
0236 
0237       case EcalTriggerTower: {
0238         EcalTrigTowerDetId ttId(_id);
0239         std::vector<DetId> ids(trigTowerMap->constituentsOf(ttId));
0240         for (std::vector<DetId>::iterator idItr(ids.begin()); idItr != ids.end(); ++idItr)
0241           if ((_statusManager->getStatus(idItr->rawId()) & _mask) != 0)
0242             return true;
0243       } break;
0244 
0245       default:
0246         break;
0247     }
0248 
0249     return false;
0250   }
0251 
0252   void MESet::fill_(unsigned _iME, int _bin, double _w) {
0253     if (kind_ == MonitorElement::Kind::REAL)
0254       return;
0255 
0256     MonitorElement *me(mes_[_iME]);
0257     if (!me)
0258       return;
0259 
0260     TH1 *h(me->getTH1());
0261 
0262     int nbinsX(h->GetNbinsX());
0263 
0264     double x(h->GetXaxis()->GetBinCenter(_bin % (nbinsX + 2)));
0265 
0266     if (kind_ == MonitorElement::Kind::TH1F || kind_ == MonitorElement::Kind::TPROFILE)
0267       me->Fill(x, _w);
0268     else {
0269       double y(h->GetYaxis()->GetBinCenter(_bin / (nbinsX + 2)));
0270       me->Fill(x, y, _w);
0271     }
0272   }
0273 
0274   void MESet::fill_(unsigned _iME, int _bin, double _y, double _w) {
0275     if (kind_ != MonitorElement::Kind::TH2F && kind_ != MonitorElement::Kind::TPROFILE2D)
0276       return;
0277 
0278     MonitorElement *me(mes_[_iME]);
0279     if (!me)
0280       return;
0281 
0282     TH1 *h(me->getTH1());
0283 
0284     int nbinsX(h->GetNbinsX());
0285 
0286     double x(h->GetXaxis()->GetBinCenter(_bin % (nbinsX + 2)));
0287     me->Fill(x, _y, _w);
0288   }
0289 
0290   void MESet::fill_(unsigned _iME, double _x, double _wy, double _w) {
0291     MonitorElement *me(mes_[_iME]);
0292     if (!me)
0293       return;
0294 
0295     if (kind_ == MonitorElement::Kind::REAL)
0296       me->Fill(_x);
0297     else if (kind_ == MonitorElement::Kind::TH1F || kind_ == MonitorElement::Kind::TPROFILE)
0298       me->Fill(_x, _wy);
0299     else
0300       me->Fill(_x, _wy, _w);
0301   }
0302 
0303   MESet::ConstBin::ConstBin(MESet const &_meSet, unsigned _iME /* = 0*/, int _iBin /* = 1*/)
0304       : meSet_(&_meSet), iME(_iME), iBin(_iBin), otype(binning::nObjType) {
0305     if (iME == unsigned(-1))
0306       return;
0307 
0308     // current internal bin numbering scheme does not allow 1D histograms
0309     // (overflow & underflow in each y)
0310     MonitorElement::Kind kind(meSet_->getKind());
0311     //    if(kind != MonitorElement::Kind::TH1F && kind !=
0312     //    MonitorElement::Kind::TPROFILE &&
0313     if (kind != MonitorElement::Kind::TH2F && kind != MonitorElement::Kind::TPROFILE2D)
0314       throw cms::Exception("InvalidOperation") << "MESet::ConstBin::Ctor: const_iterator only available for MESet of "
0315                                                   "2D histograms";
0316 
0317     MonitorElement const *me(meSet_->getME(iME));
0318 
0319     if (!me)
0320       throw cms::Exception("InvalidOperation")
0321           << "MESet::ConstBin::Ctor: ME " << iME << " does not exist for MESet " << meSet_->getPath();
0322 
0323     if (iBin == 1 && (kind == MonitorElement::Kind::TH2F || kind == MonitorElement::Kind::TPROFILE2D))
0324       iBin = me->getNbinsX() + 3;
0325 
0326     otype = binning::getObject(meSet_->getObjType(), iME);
0327   }
0328 
0329   MESet::ConstBin &MESet::ConstBin::operator=(ConstBin const &_rhs) {
0330     if (meSet_->getObjType() != _rhs.meSet_->getObjType() || meSet_->getBinType() != _rhs.meSet_->getBinType())
0331       throw cms::Exception("IncompatibleAssignment")
0332           << "Iterator of otype " << _rhs.meSet_->getObjType() << " and btype " << _rhs.meSet_->getBinType()
0333           << " to otype " << meSet_->getObjType() << " and btype " << meSet_->getBinType() << " ("
0334           << _rhs.meSet_->getPath() << " to " << meSet_->getPath() << ")";
0335 
0336     iME = _rhs.iME;
0337     iBin = _rhs.iBin;
0338     otype = _rhs.otype;
0339 
0340     return *this;
0341   }
0342 
0343   MESet::const_iterator::const_iterator(EcalElectronicsMapping const *electronicsMap,
0344                                         MESet const &_meSet,
0345                                         DetId const &_id)
0346       : bin_() {
0347     binning::ObjectType otype(_meSet.getObjType());
0348     unsigned iME(binning::findPlotIndex(electronicsMap, otype, _id));
0349     if (iME == unsigned(-1))
0350       return;
0351 
0352     binning::BinningType btype(_meSet.getBinType());
0353     int bin(binning::findBin2D(electronicsMap, otype, btype, _id));
0354     if (bin == 0)
0355       return;
0356 
0357     bin_.setMESet(_meSet);
0358     bin_.iME = iME;
0359     bin_.iBin = bin;
0360     bin_.otype = otype;
0361   }
0362 
0363   MESet::const_iterator &MESet::const_iterator::operator++() {
0364     unsigned &iME(bin_.iME);
0365     MESet const *meSet(bin_.getMESet());
0366 
0367     if (iME == unsigned(-1))
0368       return *this;
0369 
0370     int &bin(bin_.iBin);
0371     binning::ObjectType &otype(bin_.otype);
0372 
0373     MonitorElement::Kind kind(meSet->getKind());
0374     MonitorElement const *me(meSet->getME(iME));
0375 
0376     int nbinsX(me->getNbinsX());
0377 
0378     ++bin;
0379     if (bin == 1) {
0380       iME = 0;
0381       me = meSet->getME(iME);
0382       nbinsX = me->getNbinsX();
0383       if (kind == MonitorElement::Kind::TH2F || kind == MonitorElement::Kind::TPROFILE2D)
0384         bin = nbinsX + 3;
0385       otype = binning::getObject(meSet->getObjType(), iME);
0386     }
0387 
0388     if (bin % (nbinsX + 2) == nbinsX + 1) {
0389       if (kind == MonitorElement::Kind::TH1F || kind == MonitorElement::Kind::TPROFILE ||
0390           bin / (nbinsX + 2) == me->getNbinsY()) {
0391         iME += 1;
0392         me = meSet->getME(iME);
0393         if (!me) {
0394           iME = unsigned(-1);
0395           bin = -1;
0396           otype = binning::nObjType;
0397         } else {
0398           nbinsX = me->getNbinsX();
0399           if (kind == MonitorElement::Kind::TH2F || kind == MonitorElement::Kind::TPROFILE2D)
0400             bin = nbinsX + 3;
0401           else
0402             bin = 1;
0403 
0404           otype = binning::getObject(meSet->getObjType(), iME);
0405         }
0406       } else
0407         bin += 2;
0408     }
0409 
0410     return *this;
0411   }
0412 
0413   MESet::const_iterator &MESet::const_iterator::toNextChannel(const EcalElectronicsMapping *electronicsMap) {
0414     if (!bin_.getMESet())
0415       return *this;
0416     do
0417       operator++();
0418     while (bin_.iME != unsigned(-1) && !bin_.isChannel(electronicsMap));
0419 
0420     return *this;
0421   }
0422 
0423   bool MESet::const_iterator::up() {
0424     MESet const *meSet(bin_.getMESet());
0425     if (bin_.iME == unsigned(-1) || bin_.iBin < 1)
0426       return false;
0427 
0428     MonitorElement::Kind kind(meSet->getKind());
0429     if (kind != MonitorElement::Kind::TH2F && kind != MonitorElement::Kind::TPROFILE2D)
0430       return false;
0431 
0432     MonitorElement const *me(meSet->getME(bin_.iME));
0433 
0434     if (bin_.iBin / (me->getNbinsX() + 2) >= me->getNbinsY())
0435       return false;
0436 
0437     bin_.iBin += me->getNbinsX() + 2;
0438 
0439     return true;
0440   }
0441 
0442   bool MESet::const_iterator::down() {
0443     MESet const *meSet(bin_.getMESet());
0444     if (bin_.iME == unsigned(-1) || bin_.iBin < 1)
0445       return false;
0446 
0447     MonitorElement::Kind kind(meSet->getKind());
0448     if (kind != MonitorElement::Kind::TH2F && kind != MonitorElement::Kind::TPROFILE2D)
0449       return false;
0450 
0451     MonitorElement const *me(meSet->getME(bin_.iME));
0452 
0453     if (bin_.iBin / (me->getNbinsX() + 2) <= 1)
0454       return false;
0455 
0456     bin_.iBin -= me->getNbinsX() + 2;
0457 
0458     return true;
0459   }
0460 
0461   bool MESet::const_iterator::left() {
0462     MESet const *meSet(bin_.getMESet());
0463     if (bin_.iME == unsigned(-1) || bin_.iBin < 1)
0464       return false;
0465 
0466     MonitorElement::Kind kind(meSet->getKind());
0467     if (kind != MonitorElement::Kind::TH2F && kind != MonitorElement::Kind::TPROFILE2D)
0468       return false;
0469 
0470     MonitorElement const *me(meSet->getME(bin_.iME));
0471 
0472     if (bin_.iBin % (me->getNbinsX() + 2) <= 1)
0473       return false;
0474 
0475     bin_.iBin -= 1;
0476 
0477     return true;
0478   }
0479 
0480   bool MESet::const_iterator::right() {
0481     MESet const *meSet(bin_.getMESet());
0482     if (bin_.iME == unsigned(-1) || bin_.iBin < 1)
0483       return false;
0484 
0485     MonitorElement::Kind kind(meSet->getKind());
0486     if (kind != MonitorElement::Kind::TH2F && kind != MonitorElement::Kind::TPROFILE2D)
0487       return false;
0488 
0489     MonitorElement const *me(meSet->getME(bin_.iME));
0490 
0491     if (bin_.iBin % (me->getNbinsX() + 2) >= me->getNbinsX())
0492       return false;
0493 
0494     bin_.iBin += 1;
0495 
0496     return true;
0497   }
0498 }  // namespace ecaldqm