Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:12:40

0001 
0002 #include "DQMOffline/EGamma/interface/ElectronDqmHarvesterBase.h"
0003 //#include "DQMServices/Core/interface/DQMStore.h"
0004 #include "FWCore/ServiceRegistry/interface/Service.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 #include "TMath.h"
0008 #include "TFile.h"
0009 #include "TH1F.h"
0010 #include "TH1I.h"
0011 #include "TH2F.h"
0012 #include "TProfile.h"
0013 #include "TTree.h"
0014 #include <iostream>
0015 #include <algorithm>
0016 #include <sstream>
0017 
0018 ElectronDqmHarvesterBase::ElectronDqmHarvesterBase(const edm::ParameterSet &conf)
0019     : bookPrefix_("ele"), bookIndex_(0), histoNamesReady(false), finalDone_(false) {
0020   verbosity_ = conf.getUntrackedParameter<int>("Verbosity");
0021   finalStep_ = conf.getParameter<std::string>("FinalStep");
0022   inputFile_ = conf.getParameter<std::string>("InputFile");
0023   outputFile_ = conf.getParameter<std::string>("OutputFile");
0024   inputInternalPath_ = conf.getParameter<std::string>("InputFolderName");
0025   outputInternalPath_ = conf.getParameter<std::string>("OutputFolderName");
0026 }
0027 
0028 ElectronDqmHarvesterBase::~ElectronDqmHarvesterBase() {}
0029 
0030 void ElectronDqmHarvesterBase::setBookPrefix(const std::string &prefix) { bookPrefix_ = prefix; }
0031 
0032 void ElectronDqmHarvesterBase::setBookIndex(short index) { bookIndex_ = index; }
0033 
0034 void ElectronDqmHarvesterBase::setBookEfficiencyFlag(const bool &eff_flag) { bookEfficiencyFlag_ = eff_flag; }
0035 
0036 void ElectronDqmHarvesterBase::setBookStatOverflowFlag(const bool &statOverflow_flag) {
0037   bookStatOverflowFlag_ = statOverflow_flag;
0038 }
0039 
0040 std::string ElectronDqmHarvesterBase::newName(const std::string &name) {
0041   if (bookPrefix_.empty()) {
0042     return name;
0043   }
0044   std::ostringstream oss;
0045   oss << bookPrefix_;
0046   if (bookIndex_ >= 0) {
0047     oss << bookIndex_++;
0048   }
0049   oss << "_" << name;
0050   return oss.str();
0051 }
0052 
0053 const std::string *ElectronDqmHarvesterBase::find(DQMStore::IGetter &iGetter, const std::string &name) {
0054   if (!histoNamesReady) {
0055     histoNamesReady = true;
0056     histoNames_ = iGetter.getMEs();
0057   }
0058   std::vector<const std::string *> res;
0059   std::size_t nsize = name.size();
0060 
0061   for (const auto &histoName : histoNames_) {
0062     std::size_t lsize = histoName.size();
0063     if ((lsize >= nsize) && (histoName.find(name) == (lsize - nsize))) {
0064       res.push_back(&histoName);
0065     }
0066   }
0067   if (res.empty()) {
0068     std::ostringstream oss;
0069     oss << "Histogram " << name << " not found in " << outputInternalPath_;
0070     char sep = ':';
0071     for (auto const &histoName : histoNames_) {
0072       oss << sep << ' ' << histoName;
0073       sep = ',';
0074     }
0075     oss << '.';
0076     edm::LogWarning("ElectronDqmHarvesterBase::find") << oss.str();
0077     return nullptr;
0078   } else if (res.size() > 1) {
0079     std::ostringstream oss;
0080     oss << "Ambiguous histograms for " << name << " in " << outputInternalPath_;
0081     char sep = ':';
0082     for (auto const resItr : res) {
0083       oss << sep << ' ' << *resItr;
0084       sep = ',';
0085     }
0086     oss << '.';
0087     edm::LogWarning("ElectronDqmHarvesterBase::find") << oss.str();
0088     return nullptr;
0089   }
0090   return res[0];
0091 }
0092 
0093 void ElectronDqmHarvesterBase::beginJob() {}
0094 
0095 void ElectronDqmHarvesterBase::dqmEndLuminosityBlock(DQMStore::IBooker &iBooker,
0096                                                      DQMStore::IGetter &iGetter,
0097                                                      edm::LuminosityBlock const &,
0098                                                      edm::EventSetup const &) {
0099   if (finalStep_ == "AtLumiEnd") {
0100     if (finalDone_) {
0101       edm::LogWarning("ElectronDqmHarvesterBase::endLuminosityBlock") << "finalize() already called";
0102     }
0103     iBooker.setCurrentFolder(outputInternalPath_);
0104     finalDone_ = true;
0105   }
0106 }
0107 
0108 void ElectronDqmHarvesterBase::dqmEndJob(DQMStore::IBooker &iBooker, DQMStore::IGetter &iGetter) {
0109   if (finalStep_ == "AtJobEnd") {
0110     if (finalDone_) {
0111       edm::LogWarning("ElectronDqmHarvesterBase::dqmEndJob") << "finalize() already called";
0112     }
0113     iBooker.setCurrentFolder(outputInternalPath_);
0114     finalDone_ = true;
0115   }
0116   iBooker.setCurrentFolder(outputInternalPath_);
0117   finalize(iBooker, iGetter);
0118 }
0119 
0120 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::get(DQMStore::IGetter &iGetter,
0121                                                                         const std::string &name) {
0122   const std::string *fullName = find(iGetter, name);
0123   if (fullName) {
0124     return iGetter.get(inputInternalPath_ + "/" + *fullName);
0125   } else {
0126     return nullptr;
0127   }
0128 }
0129 
0130 void ElectronDqmHarvesterBase::remove(DQMStore::IBooker &iBooker, DQMStore::IGetter &iGetter, const std::string &name) {
0131   // TODO: remove no longer supported in DQMStore.
0132 }
0133 
0134 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::bookH1andDivide(DQMStore::IBooker &iBooker,
0135                                                                                     DQMStore::IGetter &iGetter,
0136                                                                                     const std::string &name,
0137                                                                                     const std::string &num,
0138                                                                                     const std::string &denom,
0139                                                                                     const std::string &titleX,
0140                                                                                     const std::string &titleY,
0141                                                                                     const std::string &title) {
0142   return bookH1andDivide(iBooker, iGetter, name, get(iGetter, num), get(iGetter, denom), titleX, titleY, title);
0143 }
0144 
0145 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::bookH2andDivide(DQMStore::IBooker &iBooker,
0146                                                                                     DQMStore::IGetter &iGetter,
0147                                                                                     const std::string &name,
0148                                                                                     const std::string &num,
0149                                                                                     const std::string &denom,
0150                                                                                     const std::string &titleX,
0151                                                                                     const std::string &titleY,
0152                                                                                     const std::string &title) {
0153   return bookH2andDivide(iBooker, iGetter, name, get(iGetter, num), get(iGetter, denom), titleX, titleY, title);
0154 }
0155 
0156 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::cloneH1(DQMStore::IBooker &iBooker,
0157                                                                             DQMStore::IGetter &iGetter,
0158                                                                             const std::string &clone,
0159                                                                             const std::string &original,
0160                                                                             const std::string &title) {
0161   return cloneH1(iBooker, iGetter, clone, get(iGetter, original), title);
0162 }
0163 
0164 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::profileX(DQMStore::IBooker &iBooker,
0165                                                                              DQMStore::IGetter &iGetter,
0166                                                                              const std::string &me2d,
0167                                                                              const std::string &title,
0168                                                                              const std::string &titleX,
0169                                                                              const std::string &titleY,
0170                                                                              Double_t minimum,
0171                                                                              Double_t maximum) {
0172   return profileX(iBooker, iGetter, get(iGetter, me2d), title, titleX, titleY, minimum, maximum);
0173 }
0174 
0175 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::profileY(DQMStore::IBooker &iBooker,
0176                                                                              DQMStore::IGetter &iGetter,
0177                                                                              const std::string &me2d,
0178                                                                              const std::string &title,
0179                                                                              const std::string &titleX,
0180                                                                              const std::string &titleY,
0181                                                                              Double_t minimum,
0182                                                                              Double_t maximum) {
0183   return profileY(iBooker, iGetter, get(iGetter, me2d), title, titleX, titleY, minimum, maximum);
0184 }
0185 
0186 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::bookH1(DQMStore::IBooker &iBooker,
0187                                                                            const std::string &name,
0188                                                                            const std::string &title,
0189                                                                            int nchX,
0190                                                                            double lowX,
0191                                                                            double highX,
0192                                                                            const std::string &titleX,
0193                                                                            const std::string &titleY,
0194                                                                            Option_t *option) {
0195   iBooker.setCurrentFolder(outputInternalPath_);
0196   MonitorElement *me = iBooker.book1D(newName(name), title, nchX, lowX, highX);
0197   if (!titleX.empty()) {
0198     me->setAxisTitle(titleX);
0199   }
0200   if (!titleY.empty()) {
0201     me->getTH1F()->GetYaxis()->SetTitle(titleY.c_str());
0202   }
0203   if (TString(option) != "") {
0204     me->setOption(option);
0205   }
0206   if (bookStatOverflowFlag_) {
0207     me->setStatOverflows(kTRUE);
0208   }
0209   return me;
0210 }
0211 
0212 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::bookH1withSumw2(DQMStore::IBooker &iBooker,
0213                                                                                     const std::string &name,
0214                                                                                     const std::string &title,
0215                                                                                     int nchX,
0216                                                                                     double lowX,
0217                                                                                     double highX,
0218                                                                                     const std::string &titleX,
0219                                                                                     const std::string &titleY,
0220                                                                                     Option_t *option) {
0221   iBooker.setCurrentFolder(outputInternalPath_);
0222   MonitorElement *me = iBooker.book1D(newName(name), title, nchX, lowX, highX);
0223   me->enableSumw2();
0224   if (!titleX.empty()) {
0225     me->setAxisTitle(titleX);
0226   }
0227   if (!titleY.empty()) {
0228     me->getTH1F()->GetYaxis()->SetTitle(titleY.c_str());
0229   }
0230   if (TString(option) != "") {
0231     me->setOption(option);
0232   }
0233   if (bookStatOverflowFlag_) {
0234     me->getTH1F()->StatOverflows(kTRUE);
0235   }
0236   return me;
0237 }
0238 
0239 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::bookH2(DQMStore::IBooker &iBooker,
0240                                                                            const std::string &name,
0241                                                                            const std::string &title,
0242                                                                            int nchX,
0243                                                                            double lowX,
0244                                                                            double highX,
0245                                                                            int nchY,
0246                                                                            double lowY,
0247                                                                            double highY,
0248                                                                            const std::string &titleX,
0249                                                                            const std::string &titleY,
0250                                                                            Option_t *option) {
0251   iBooker.setCurrentFolder(outputInternalPath_);
0252   MonitorElement *me = iBooker.book2D(newName(name), title, nchX, lowX, highX, nchY, lowY, highY);
0253   if (!titleX.empty()) {
0254     me->setAxisTitle(titleX);
0255   }
0256   if (!titleY.empty()) {
0257     me->getTH2F()->GetYaxis()->SetTitle(titleY.c_str());
0258   }
0259   if (TString(option) != "") {
0260     me->setOption(option);
0261   }
0262   if (bookStatOverflowFlag_) {
0263     me->getTH1F()->StatOverflows(kTRUE);
0264   }
0265   return me;
0266 }
0267 
0268 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::bookH2withSumw2(DQMStore::IBooker &iBooker,
0269                                                                                     const std::string &name,
0270                                                                                     const std::string &title,
0271                                                                                     int nchX,
0272                                                                                     double lowX,
0273                                                                                     double highX,
0274                                                                                     int nchY,
0275                                                                                     double lowY,
0276                                                                                     double highY,
0277                                                                                     const std::string &titleX,
0278                                                                                     const std::string &titleY,
0279                                                                                     Option_t *option) {
0280   iBooker.setCurrentFolder(outputInternalPath_);
0281   MonitorElement *me = iBooker.book2D(newName(name), title, nchX, lowX, highX, nchY, lowY, highY);
0282   me->enableSumw2();
0283   if (!titleX.empty()) {
0284     me->setAxisTitle(titleX);
0285   }
0286   if (!titleY.empty()) {
0287     me->getTH2F()->GetYaxis()->SetTitle(titleY.c_str());
0288   }
0289   if (TString(option) != "") {
0290     me->setOption(option);
0291   }
0292   if (bookStatOverflowFlag_) {
0293     me->getTH1F()->StatOverflows(kTRUE);
0294   }
0295   return me;
0296 }
0297 
0298 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::bookP1(DQMStore::IBooker &iBooker,
0299                                                                            const std::string &name,
0300                                                                            const std::string &title,
0301                                                                            int nchX,
0302                                                                            double lowX,
0303                                                                            double highX,
0304                                                                            double lowY,
0305                                                                            double highY,
0306                                                                            const std::string &titleX,
0307                                                                            const std::string &titleY,
0308                                                                            Option_t *option) {
0309   iBooker.setCurrentFolder(outputInternalPath_);
0310   MonitorElement *me = iBooker.bookProfile(newName(name), title, nchX, lowX, highX, lowY, highY, " ");
0311   if (!titleX.empty()) {
0312     me->getTProfile()->GetXaxis()->SetTitle(titleX.c_str());
0313   }
0314   if (!titleY.empty()) {
0315     me->getTProfile()->GetYaxis()->SetTitle(titleY.c_str());
0316   }
0317   if (TString(option) != "") {
0318     me->getTProfile()->SetOption(option);
0319   }
0320   if (bookStatOverflowFlag_) {
0321     me->getTH1F()->StatOverflows(kTRUE);
0322   }
0323   return me;
0324 }
0325 
0326 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::bookH1andDivide(DQMStore::IBooker &iBooker,
0327                                                                                     DQMStore::IGetter &iGetter,
0328                                                                                     const std::string &name,
0329                                                                                     MonitorElement *num,
0330                                                                                     MonitorElement *denom,
0331                                                                                     const std::string &titleX,
0332                                                                                     const std::string &titleY,
0333                                                                                     const std::string &title) {
0334   if ((!num) || (!denom))
0335     return nullptr;
0336   iBooker.setCurrentFolder(outputInternalPath_);
0337   std::string name2 = newName(name);
0338   TH1F *h_temp = (TH1F *)num->getTH1F()->Clone(name2.c_str());
0339   h_temp->Reset();
0340   h_temp->Divide(num->getTH1(), denom->getTH1(), 1, 1, "b");
0341   h_temp->GetXaxis()->SetTitle(titleX.c_str());
0342   h_temp->GetYaxis()->SetTitle(titleY.c_str());
0343   if (!title.empty()) {
0344     h_temp->SetTitle(title.c_str());
0345   }
0346   if (verbosity_ > 0) {
0347     h_temp->Print();
0348   }
0349   MonitorElement *me = iBooker.book1D(name2, h_temp);
0350   if (bookEfficiencyFlag_) {
0351     me->setEfficiencyFlag();
0352   }
0353   delete h_temp;
0354   return me;
0355 }
0356 
0357 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::bookH2andDivide(DQMStore::IBooker &iBooker,
0358                                                                                     DQMStore::IGetter &iGetter,
0359                                                                                     const std::string &name,
0360                                                                                     MonitorElement *num,
0361                                                                                     MonitorElement *denom,
0362                                                                                     const std::string &titleX,
0363                                                                                     const std::string &titleY,
0364                                                                                     const std::string &title) {
0365   if ((!num) || (!denom))
0366     return nullptr;
0367   iBooker.setCurrentFolder(outputInternalPath_);
0368   std::string name2 = newName(name);
0369   TH2F *h_temp = (TH2F *)num->getTH2F()->Clone(name2.c_str());
0370   h_temp->Reset();
0371   h_temp->Divide(num->getTH1(), denom->getTH1(), 1, 1, "b");
0372   h_temp->GetXaxis()->SetTitle(titleX.c_str());
0373   h_temp->GetYaxis()->SetTitle(titleY.c_str());
0374   if (!title.empty()) {
0375     h_temp->SetTitle(title.c_str());
0376   }
0377   if (verbosity_ > 0) {
0378     h_temp->Print();
0379   }
0380   MonitorElement *me = iBooker.book2D(name2, h_temp);
0381   if (bookEfficiencyFlag_) {
0382     me->setEfficiencyFlag();
0383   }
0384   delete h_temp;
0385   return me;
0386 }
0387 
0388 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::cloneH1(DQMStore::IBooker &iBooker,
0389                                                                             DQMStore::IGetter &iGetter,
0390                                                                             const std::string &name,
0391                                                                             MonitorElement *original,
0392                                                                             const std::string &title) {
0393   if (!original)
0394     return nullptr;
0395   iBooker.setCurrentFolder(outputInternalPath_);
0396   std::string name2 = newName(name);
0397   TH1F *h_temp = (TH1F *)original->getTH1F()->Clone(name2.c_str());
0398   h_temp->Reset();
0399   if (!title.empty()) {
0400     h_temp->SetTitle(title.c_str());
0401   }
0402   MonitorElement *me = iBooker.book1D(name2, h_temp);
0403   delete h_temp;
0404   return me;
0405 }
0406 
0407 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::profileX(DQMStore::IBooker &iBooker,
0408                                                                              DQMStore::IGetter &iGetter,
0409                                                                              MonitorElement *me2d,
0410                                                                              const std::string &title,
0411                                                                              const std::string &titleX,
0412                                                                              const std::string &titleY,
0413                                                                              Double_t minimum,
0414                                                                              Double_t maximum) {
0415   iBooker.setCurrentFolder(outputInternalPath_);
0416   std::string name2 = me2d->getName() + "_pfx";
0417   TProfile *p1_temp = me2d->getTH2F()->ProfileX();
0418   if (!title.empty()) {
0419     p1_temp->SetTitle(title.c_str());
0420   }
0421   if (!titleX.empty()) {
0422     p1_temp->GetXaxis()->SetTitle(titleX.c_str());
0423   }
0424   if (!titleY.empty()) {
0425     p1_temp->GetYaxis()->SetTitle(titleY.c_str());
0426   }
0427   if (minimum != -1111) {
0428     p1_temp->SetMinimum(minimum);
0429   }
0430   if (maximum != -1111) {
0431     p1_temp->SetMaximum(maximum);
0432   }
0433   MonitorElement *me = iBooker.bookProfile(name2, p1_temp);
0434   delete p1_temp;
0435   return me;
0436 }
0437 
0438 ElectronDqmHarvesterBase::MonitorElement *ElectronDqmHarvesterBase::profileY(DQMStore::IBooker &iBooker,
0439                                                                              DQMStore::IGetter &iGetter,
0440                                                                              MonitorElement *me2d,
0441                                                                              const std::string &title,
0442                                                                              const std::string &titleX,
0443                                                                              const std::string &titleY,
0444                                                                              Double_t minimum,
0445                                                                              Double_t maximum) {
0446   iBooker.setCurrentFolder(outputInternalPath_);
0447   std::string name2 = me2d->getName() + "_pfy";
0448   TProfile *p1_temp = me2d->getTH2F()->ProfileY();
0449   if (!title.empty()) {
0450     p1_temp->SetTitle(title.c_str());
0451   }
0452   if (!titleX.empty()) {
0453     p1_temp->GetXaxis()->SetTitle(titleX.c_str());
0454   }
0455   if (!titleY.empty()) {
0456     p1_temp->GetYaxis()->SetTitle(titleY.c_str());
0457   }
0458   if (minimum != -1111) {
0459     p1_temp->SetMinimum(minimum);
0460   }
0461   if (maximum != -1111) {
0462     p1_temp->SetMaximum(maximum);
0463   }
0464   MonitorElement *me = iBooker.bookProfile(name2, p1_temp);
0465   delete p1_temp;
0466   return me;
0467 }