Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DQMServices/Core/interface/DQMStore.h"
0002 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/ServiceRegistry/interface/Service.h"
0005 
0006 #include <vector>
0007 #include <string>
0008 #include <map>
0009 
0010 class DQMMessageLoggerClient : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
0011 public:
0012   typedef dqm::legacy::DQMStore DQMStore;
0013   typedef dqm::legacy::MonitorElement MonitorElement;
0014   // Constructor
0015   DQMMessageLoggerClient(const edm::ParameterSet &);
0016   // Destructor
0017   ~DQMMessageLoggerClient() override;
0018 
0019 protected:
0020   void beginJob() override;
0021   //void beginRun(const edm::Run&, const edm::EventSetup&);
0022 
0023   void beginRun(const edm::Run &, const edm::EventSetup &) override;
0024 
0025   // Get the analysis
0026   void analyze(const edm::Event &, const edm::EventSetup &) override;
0027 
0028   // Save the histos
0029   void endRun(const edm::Run &, const edm::EventSetup &) override;
0030 
0031 private:
0032   void fillHistograms();
0033 
0034   // ----------member data ---------------------------
0035 
0036   DQMStore *theDbe;
0037   edm::ParameterSet parameters;
0038   std::string directoryName;
0039 
0040   std::vector<std::string> binLabel;
0041   std::vector<Double_t> binContent;
0042 
0043   int nBinsErrors;
0044   int nBinsWarnings;
0045 
0046   MonitorElement *modulesErrorsFound;
0047   MonitorElement *modulesWarningsFound;
0048   MonitorElement *categoriesErrorsFound;
0049   MonitorElement *categoriesWarningsFound;
0050 };
0051 
0052 using namespace std;
0053 using namespace edm;
0054 
0055 // -----------------------------
0056 //  constructors and destructor
0057 // -----------------------------
0058 
0059 DQMMessageLoggerClient::DQMMessageLoggerClient(const edm::ParameterSet &ps) {
0060   parameters = ps;
0061   theDbe = nullptr;
0062   modulesErrorsFound = nullptr;
0063   modulesWarningsFound = nullptr;
0064   categoriesWarningsFound = nullptr;
0065   categoriesErrorsFound = nullptr;
0066   directoryName = parameters.getParameter<string>("Directory");
0067 }
0068 
0069 DQMMessageLoggerClient::~DQMMessageLoggerClient() = default;
0070 
0071 void DQMMessageLoggerClient::beginJob() {
0072   //LogTrace(metname)<<"[DQMMessageLoggerClient] Parameters initialization";
0073   theDbe = Service<DQMStore>().operator->();
0074 
0075   if (theDbe != nullptr) {
0076     theDbe->setCurrentFolder(directoryName);
0077   }
0078 }
0079 
0080 void DQMMessageLoggerClient::beginRun(const edm::Run &r, const edm::EventSetup &es) {}
0081 
0082 void DQMMessageLoggerClient::analyze(const edm::Event &e, const edm::EventSetup &context) {}
0083 
0084 void DQMMessageLoggerClient::fillHistograms() {
0085   // directoryName should be the same as for DQMMessageLogger
0086   //theDbe->setCurrentFolder(directoryName);
0087   /*
0088   cout << theDbe->pwd() << endl;
0089   vector<string> vec = theDbe->getSubdirs();
0090   for(int i=0; i<vec.size(); i++){
0091     cout << vec[i] << endl;
0092   }
0093   */
0094   theDbe->goUp();
0095 
0096   vector<string> entries;
0097   entries.push_back(directoryName + "/Warnings/modules_warnings");
0098   entries.push_back(directoryName + "/Errors/modules_errors");
0099   entries.push_back(directoryName + "/Warnings/categories_warnings");
0100   entries.push_back(directoryName + "/Errors/categories_errors");
0101 
0102   int mel = 0;
0103 
0104   for (auto ent = entries.begin(); ent != entries.end(); ++ent) {
0105     mel++;
0106     //RESET VECTORS
0107     binContent.clear();
0108     binLabel.clear();
0109 
0110     // RETURN ME
0111 
0112     MonitorElement *me = theDbe->get(*ent);
0113     // GET TH1F
0114     if (theDbe->get(*ent)) {
0115       if (TH1 *rootHisto = me->getTH1()) {
0116         int nonzeros = 0;
0117         int Nbins = me->getNbinsX();
0118 
0119         // LOOP OVER TH1F
0120         for (int bin = 1; bin <= Nbins; ++bin) {
0121           if (rootHisto->GetBinContent(bin) > 0) {
0122             nonzeros++;
0123             binContent.push_back(rootHisto->GetBinContent(bin));
0124             binLabel.emplace_back(rootHisto->GetXaxis()->GetBinLabel(bin));
0125           }
0126         }
0127 
0128         switch (mel) {
0129           case 1:
0130             theDbe->setCurrentFolder(directoryName + "/Warnings");
0131             modulesWarningsFound = theDbe->get(directoryName + "/Warnings/modulesWarningsFound");
0132             if (nonzeros > 0) {
0133               modulesWarningsFound = theDbe->book1D(
0134                   "modulesWarningsFound", "Warnings per module", binContent.size(), 0, binContent.size());
0135             } else {
0136               modulesWarningsFound = theDbe->book1D("modulesWarningsFound", "Warnings per module", 1, 0, 1);
0137               modulesWarningsFound->setBinLabel(1, "Module name");
0138             }
0139             for (int i = 0; i < nonzeros; ++i) {
0140               if (modulesWarningsFound != nullptr) {
0141                 //gPad->SetBottomMargin(2);
0142                 //cout << binContent[i] <<" "<<binLabel[i] << endl;
0143                 modulesWarningsFound->setBinContent(i + 1, binContent[i]);
0144                 modulesWarningsFound->setBinLabel(i + 1, binLabel[i]);
0145               }
0146             }
0147             if (nonzeros > 4)
0148               modulesWarningsFound->getTH1()->GetXaxis()->LabelsOption("v");
0149             break;
0150           case 2:
0151             theDbe->setCurrentFolder(directoryName + "/Errors");
0152             modulesErrorsFound = theDbe->get(directoryName + "/Errors/modulesErrorsFound");
0153             if (nonzeros > 0) {
0154               modulesErrorsFound =
0155                   theDbe->book1D("modulesErrorsFound", "Errors per module", binContent.size(), 0, binContent.size());
0156             } else {
0157               modulesErrorsFound = theDbe->book1D("modulesErrorsFound", "Errors per module", 1, 0, 1);
0158               modulesErrorsFound->setBinLabel(1, "Module name");
0159             }
0160             for (int i = 0; i < nonzeros; ++i) {
0161               if (modulesErrorsFound != nullptr) {
0162                 //gPad->SetBottomMargin(2);
0163                 modulesErrorsFound->setBinContent(i + 1, binContent[i]);
0164                 modulesErrorsFound->setBinLabel(i + 1, binLabel[i]);
0165               }
0166             }
0167             if (nonzeros > 4)
0168               modulesErrorsFound->getTH1()->GetXaxis()->LabelsOption("v");
0169             break;
0170           case 3:
0171             theDbe->setCurrentFolder(directoryName + "/Warnings");
0172             categoriesWarningsFound = theDbe->get(directoryName + "/Warnings/categoriesWarningsFound");
0173             if (nonzeros > 0) {
0174               categoriesWarningsFound = theDbe->book1D(
0175                   "categoriesWarningsFound", "Warnings per category", binContent.size(), 0, binContent.size());
0176             } else {
0177               categoriesWarningsFound = theDbe->book1D("categoriesWarningsFound", "Warnings per category", 1, 0, 1);
0178               categoriesWarningsFound->setBinLabel(1, "Category name");
0179             }
0180             for (int i = 0; i < nonzeros; ++i) {
0181               if (categoriesWarningsFound != nullptr) {
0182                 //gPad->SetBottomMargin(2);
0183                 //cout << binContent[i] <<" " <<binLabel[i] << endl;
0184                 categoriesWarningsFound->setBinContent(i + 1, binContent[i]);
0185                 categoriesWarningsFound->setBinLabel(i + 1, binLabel[i]);
0186               }
0187             }
0188             if (nonzeros > 4)
0189               categoriesWarningsFound->getTH1()->GetXaxis()->LabelsOption("v");
0190             break;
0191           case 4:
0192             theDbe->setCurrentFolder(directoryName + "/Errors");
0193             categoriesErrorsFound = theDbe->get(directoryName + "/Errors/categoriesErrorsFound");
0194             if (nonzeros > 0) {
0195               categoriesErrorsFound = theDbe->book1D(
0196                   "categoriesErrorsFound", "Errors per category", binContent.size(), 0, binContent.size());
0197             } else {
0198               categoriesErrorsFound = theDbe->book1D("categoriesErrorsFound", "Errors per category", 1, 0, 1);
0199               categoriesErrorsFound->setBinLabel(1, "Category name");
0200             }
0201             for (int i = 0; i < nonzeros; ++i) {
0202               if (categoriesErrorsFound != nullptr) {
0203                 //gPad->SetBottomMargin(2);
0204                 categoriesErrorsFound->setBinContent(i + 1, binContent[i]);
0205                 categoriesErrorsFound->setBinLabel(i + 1, binLabel[i]);
0206               }
0207             }
0208             if (nonzeros > 4)
0209               categoriesErrorsFound->getTH1()->GetXaxis()->LabelsOption("v");
0210             break;
0211         }
0212       }
0213     }
0214   }
0215 }
0216 
0217 void DQMMessageLoggerClient::endRun(const Run &r, const EventSetup &es) { fillHistograms(); }
0218 
0219 #include "FWCore/PluginManager/interface/ModuleDef.h"
0220 #include "FWCore/Framework/interface/MakerMacros.h"
0221 DEFINE_FWK_MODULE(DQMMessageLoggerClient);