Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-07 00:41:11

0001 // system includes
0002 #include <cmath>
0003 #include <vector>
0004 #include <string>
0005 #include <map>
0006 
0007 // user includes
0008 #include "DQMServices/Core/interface/DQMEDAnalyzer.h"
0009 #include "DQMServices/Core/interface/DQMStore.h"
0010 #include "DataFormats/Common/interface/ErrorSummaryEntry.h"
0011 #include "DataFormats/Common/interface/Handle.h"
0012 #include "DataFormats/FWLite/interface/Event.h"
0013 #include "DataFormats/FWLite/interface/Handle.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/Framework/interface/Frameworkfwd.h"
0017 #include "FWCore/Framework/interface/MakerMacros.h"
0018 #include "FWCore/Framework/interface/TriggerNamesService.h"
0019 #include "FWCore/MessageLogger/interface/ELseverityLevel.h"
0020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0021 #include "FWCore/Utilities/interface/Algorithms.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/ParameterSet/interface/Registry.h"
0024 #include "TPad.h"
0025 
0026 using namespace std;
0027 using namespace edm;
0028 
0029 class DQMMessageLogger : public DQMEDAnalyzer {
0030 public:
0031   /// Constructor
0032   DQMMessageLogger(const edm::ParameterSet &);
0033 
0034   /// Destructor
0035   ~DQMMessageLogger() override = default;
0036 
0037   /// Get the analysis
0038   void analyze(const edm::Event &, const edm::EventSetup &) override;
0039 
0040 protected:
0041   void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override;
0042 
0043 private:
0044   // ----------member data ---------------------------
0045 
0046   // Switch for verbosity
0047   std::string metname;
0048 
0049   std::map<std::string, int> moduleMap;
0050   std::map<std::string, int> categoryMap;
0051   std::map<std::string, int> categoryWCount;
0052   std::map<std::string, int> categoryECount;
0053   // from parameters
0054   std::vector<std::string> categories_vector;
0055   std::string directoryName;
0056   edm::EDGetTokenT<std::vector<edm::ErrorSummaryEntry> > errorSummary_;
0057 
0058   //The histos
0059   MonitorElement *categories_errors;
0060   MonitorElement *categories_warnings;
0061   MonitorElement *modules_errors;
0062   MonitorElement *modules_warnings;
0063   MonitorElement *total_errors;
0064   MonitorElement *total_warnings;
0065 };
0066 
0067 DQMMessageLogger::DQMMessageLogger(const ParameterSet &parameters) {
0068   categories_errors = nullptr;
0069   categories_warnings = nullptr;
0070   modules_errors = nullptr;
0071   modules_warnings = nullptr;
0072   total_errors = nullptr;
0073   total_warnings = nullptr;
0074 
0075   //Get from cfg file
0076   categories_vector = parameters.getParameter<vector<string> >("Categories");
0077   directoryName = parameters.getParameter<string>("Directory");
0078   errorSummary_ = consumes<std::vector<edm::ErrorSummaryEntry> >(
0079       parameters.getUntrackedParameter<std::string>("errorSummary", "logErrorHarvester"));
0080 }
0081 
0082 void DQMMessageLogger::bookHistograms(DQMStore::IBooker &ibooker, edm::Run const &iRun, edm::EventSetup const &iSetup) {
0083   metname = "errorAnalyzer";
0084 
0085   // MAKE CATEGORYMAP USING INPUT FROM CFG FILE
0086   for (unsigned int i = 0; i < categories_vector.size(); i++) {
0087     categoryMap.insert(pair<string, int>(categories_vector[i], i + 1));
0088   }
0089 
0090   // MAKE MODULEMAP
0091   using TNS = Service<edm::service::TriggerNamesService>;
0092   using stringvec = vector<std::string>;
0093   TNS tns;
0094   stringvec const &trigpaths = tns->getTrigPaths();
0095 
0096   for (auto const &trigpath : trigpaths) {
0097     stringvec strings = tns->getTrigPathModules(trigpath);
0098 
0099     for (auto &k : strings) {
0100       moduleMap.insert(pair<string, int>(k, moduleMap.size() + 1));
0101     }
0102   }
0103 
0104   // BOOK THE HISTOGRAMS
0105   LogTrace(metname) << "[DQMMessageLogger] Parameters initialization";
0106 
0107   if (!moduleMap.empty()) {
0108     ibooker.setCurrentFolder(directoryName + "/Errors");
0109     modules_errors = ibooker.book1D("modules_errors", "Errors per module", moduleMap.size(), 0, moduleMap.size());
0110     ibooker.setCurrentFolder(directoryName + "/Warnings");
0111 
0112     modules_warnings = ibooker.book1D("modules_warnings", "Warnings per module", moduleMap.size(), 0, moduleMap.size());
0113 
0114     for (auto it = moduleMap.begin(); it != moduleMap.end(); ++it) {
0115       modules_errors->setBinLabel((*it).second, (*it).first);
0116       modules_warnings->setBinLabel((*it).second, (*it).first);
0117     }
0118     modules_errors->getTH1()->GetXaxis()->LabelsOption("v");
0119     modules_warnings->getTH1()->GetXaxis()->LabelsOption("v");
0120   }
0121 
0122   if (!categoryMap.empty()) {
0123     ibooker.setCurrentFolder(directoryName + "/Errors");
0124     categories_errors =
0125         ibooker.book1D("categories_errors", "Errors per category", categoryMap.size(), 0, categoryMap.size());
0126     ibooker.setCurrentFolder(directoryName + "/Warnings");
0127     categories_warnings =
0128         ibooker.book1D("categories_warnings", "Warnings per category", categoryMap.size(), 0, categoryMap.size());
0129 
0130     for (auto it = categoryMap.begin(); it != categoryMap.end(); ++it) {
0131       categories_errors->setBinLabel((*it).second, (*it).first);
0132       categories_warnings->setBinLabel((*it).second, (*it).first);
0133     }
0134     categories_warnings->getTH1()->GetXaxis()->LabelsOption("v");
0135     categories_errors->getTH1()->GetXaxis()->LabelsOption("v");
0136   }
0137 
0138   // HOW MANY BINS SHOULD THE ERROR HIST HAVE?
0139   int nbins = 11;
0140   total_warnings = ibooker.book1D("total_warnings", "Total warnings per event", nbins, -0.5, nbins + 0.5);
0141   ibooker.setCurrentFolder(directoryName + "/Errors");
0142   total_errors = ibooker.book1D("total_errors", "Total errors per event", nbins, -0.5, nbins + 0.5);
0143 
0144   for (int i = 0; i < nbins; ++i) {
0145     stringstream out;
0146     out << i;
0147     string s = out.str();
0148     total_errors->setBinLabel(i + 1, s);
0149     total_warnings->setBinLabel(i + 1, s);
0150   }
0151 }
0152 
0153 void DQMMessageLogger::analyze(const Event &iEvent, const EventSetup &iSetup) {
0154   LogTrace(metname) << "[DQMMessageLogger] Analysis of event # ";
0155 
0156   // Take the ErrorSummaryEntry container
0157   Handle<std::vector<edm::ErrorSummaryEntry> > errors;
0158   iEvent.getByToken(errorSummary_, errors);
0159   // Check that errors is valid
0160   if (!errors.isValid()) {
0161     return;
0162   }
0163   // Compare severity level of error with ELseveritylevel instance el : "-e" should be the lowest error
0164   ELseverityLevel el(ELseverityLevel::ELsev_error);
0165 
0166   // Find the total number of errors in iEvent
0167   if (errors->empty()) {
0168     if (total_errors != nullptr) {
0169       total_errors->Fill(0);
0170     }
0171     if (total_warnings != nullptr) {
0172       total_warnings->Fill(0);
0173     }
0174   } else {
0175     int e = 0;
0176     int w = 0;
0177     for (int i = 0, n = errors->size(); i < n; i++) {
0178       if ((*errors)[i].severity.getLevel() < el.getLevel()) {
0179         w += (*errors)[i].count;
0180       } else {
0181         e += (*errors)[i].count;
0182       }
0183     }
0184     if (total_errors != nullptr) {
0185       total_errors->Fill(e);
0186     }
0187     if (total_warnings != nullptr) {
0188       total_warnings->Fill(w);
0189     }
0190   }
0191 
0192   for (int i = 0, n = errors->size(); i < n; i++) {
0193     //cout << "Severity for error/warning: " << (*errors)[i].severity << " " <<(*errors)[i].module  << endl;
0194 
0195     if (!errors->empty()) {
0196       // IF THIS IS AN ERROR on the ELseverityLevel SCALE, FILL ERROR HISTS
0197       if ((*errors)[i].severity.getLevel() >= el.getLevel()) {
0198         if (categories_errors != nullptr) {
0199           auto it = categoryMap.find((*errors)[i].category);
0200           if (it != categoryMap.end()) {
0201             // FILL THE RIGHT BIN
0202             categories_errors->Fill((*it).second - 1, (*errors)[i].count);
0203           }
0204         }
0205         //  if (categoryECount.size()<=40)
0206         //    categoryECount[(*errors)[i].category]+=(*errors)[i].count;
0207 
0208         if (modules_errors != nullptr) {
0209           // remove the first part of the module string, what is before ":"
0210           string s = (*errors)[i].module;
0211           size_t pos = s.find(':');
0212           string s_temp = s.substr(pos + 1, s.size());
0213           auto it = moduleMap.find(s_temp);
0214           if (it != moduleMap.end()) {
0215             // FILL THE RIGHT BIN
0216             modules_errors->Fill((*it).second - 1, (*errors)[i].count);
0217           }
0218         }
0219         // IF ONLY WARNING, FILL WARNING HISTS
0220       } else {
0221         if (categories_warnings != nullptr) {
0222           auto it = categoryMap.find((*errors)[i].category);
0223           if (it != categoryMap.end()) {
0224             // FILL THE RIGHT BIN
0225             categories_warnings->Fill((*it).second - 1, (*errors)[i].count);
0226           }
0227         }
0228 
0229         //  if (categoryWCount.size()<=40)
0230         //    categoryWCount[(*errors)[i].category]+=(*errors)[i].count;
0231 
0232         if (modules_warnings != nullptr) {
0233           // remove the first part of the module string, what is before ":"
0234           string s = (*errors)[i].module;
0235           size_t pos = s.find(':');
0236           string s_temp = s.substr(pos + 1, s.size());
0237           auto it = moduleMap.find(s_temp);
0238           if (it != moduleMap.end()) {
0239             // FILL THE RIGHT BIN
0240             modules_warnings->Fill((*it).second - 1, (*errors)[i].count);
0241           }
0242         }
0243       }
0244     }
0245   }
0246 }
0247 
0248 #include "FWCore/PluginManager/interface/ModuleDef.h"
0249 #include "FWCore/Framework/interface/MakerMacros.h"
0250 DEFINE_FWK_MODULE(DQMMessageLogger);