Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:49

0001 // -*- C++ -*-
0002 //
0003 // Package:    LogErrorFilter
0004 // Class:      LogErrorFilter
0005 //
0006 /**\class LogErrorFilter LogErrorFilter.cc FWCore/LogErrorFilter/src/LogErrorFilter.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Jean-Roch Vlimant
0015 //         Created:  Thu Nov 12 15:59:28 CET 2009
0016 //
0017 //
0018 
0019 // user include files
0020 #include "FWCore/Framework/interface/stream/EDFilter.h"
0021 #include "FWCore/Framework/interface/Event.h"
0022 #include "FWCore/Framework/interface/MakerMacros.h"
0023 #include "DataFormats/Common/interface/ErrorSummaryEntry.h"
0024 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0027 
0028 // system include files
0029 #include <memory>
0030 
0031 //
0032 // class declaration
0033 //
0034 
0035 class LogErrorFilter : public edm::stream::EDFilter<edm::stream::WatchLuminosityBlocks> {
0036 public:
0037   explicit LogErrorFilter(edm::ParameterSet const&);
0038   ~LogErrorFilter() override;
0039   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0040 
0041 private:
0042   bool filter(edm::Event&, edm::EventSetup const&) override;
0043 
0044   void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0045 
0046   // ----------member data ---------------------------
0047   edm::EDGetTokenT<std::vector<edm::ErrorSummaryEntry>> harvesterToken_;
0048   bool atLeastOneError_;
0049   bool atLeastOneWarning_;
0050   bool atLeastOneEntry_;
0051   bool useThresholdsPerKind_;
0052 
0053   unsigned int maxErrorKindsPerLumi_;
0054   unsigned int maxWarningKindsPerLumi_;
0055 
0056   std::vector<std::string> avoidCategories_;
0057 
0058   std::map<std::string, unsigned int> errorCounts_;
0059   std::map<std::string, unsigned int> warningCounts_;
0060 };
0061 
0062 //
0063 // constants, enums and typedefs
0064 //
0065 
0066 //
0067 // static data member definitions
0068 //
0069 
0070 //
0071 // constructors and destructor
0072 //
0073 LogErrorFilter::LogErrorFilter(edm::ParameterSet const& iConfig)
0074     : atLeastOneError_(iConfig.getParameter<bool>("atLeastOneError")),
0075       atLeastOneWarning_(iConfig.getParameter<bool>("atLeastOneWarning")),
0076       atLeastOneEntry_(atLeastOneError_ && atLeastOneWarning_),
0077       useThresholdsPerKind_(iConfig.getParameter<bool>("useThresholdsPerKind")),
0078       avoidCategories_(iConfig.getParameter<std::vector<std::string>>("avoidCategories")) {
0079   if (!atLeastOneError_ && !atLeastOneWarning_) {
0080     throw edm::Exception(edm::errors::Configuration)
0081         << "Useless configuration of the error/warning filter. Need to select on an error or a warning or both.\n";
0082   }
0083   harvesterToken_ = consumes<std::vector<edm::ErrorSummaryEntry>>(iConfig.getParameter<edm::InputTag>("harvesterTag"));
0084   maxErrorKindsPerLumi_ = 999999;
0085   maxWarningKindsPerLumi_ = 999999;
0086   if (useThresholdsPerKind_) {
0087     maxErrorKindsPerLumi_ = iConfig.getParameter<unsigned int>("maxErrorKindsPerLumi");
0088     maxWarningKindsPerLumi_ = iConfig.getParameter<unsigned int>("maxWarningKindsPerLumi");
0089   }
0090 }
0091 
0092 LogErrorFilter::~LogErrorFilter() {
0093   // do anything here that needs to be done at desctruction time
0094   // (e.g. close files, deallocate resources etc.)
0095 }
0096 
0097 //
0098 // member functions
0099 //
0100 
0101 // ------------ method called on each new Event  ------------
0102 bool LogErrorFilter::filter(edm::Event& iEvent, edm::EventSetup const&) {
0103   if (auto errorsAndWarnings = iEvent.getHandle(harvesterToken_)) {
0104     if (useThresholdsPerKind_) {
0105       unsigned int errorsBelowThreshold = 0;
0106       unsigned int warningsBelowThreshold = 0;
0107       // update counters here
0108       for (auto const& summary : *errorsAndWarnings) {
0109         if (std::find(avoidCategories_.begin(), avoidCategories_.end(), summary.category) != avoidCategories_.end())
0110           continue;
0111         std::string kind = summary.category + ":" + summary.module;
0112         int iSeverity = summary.severity.getLevel();
0113         if (iSeverity == edm::ELseverityLevel::ELsev_error) {
0114           unsigned int& iCount = errorCounts_[kind];
0115           iCount++;
0116           if (iCount <= maxErrorKindsPerLumi_)
0117             errorsBelowThreshold++;
0118         } else if (iSeverity == edm::ELseverityLevel::ELsev_warning) {
0119           unsigned int& iCount = warningCounts_[kind];
0120           iCount++;
0121           if (iCount <= maxWarningKindsPerLumi_)
0122             warningsBelowThreshold++;
0123         }
0124       }
0125       return ((atLeastOneEntry_ && (errorsBelowThreshold > 0 || warningsBelowThreshold > 0)) ||
0126               (atLeastOneError_ && errorsBelowThreshold > 0) || (atLeastOneWarning_ && warningsBelowThreshold > 0));
0127     } else {
0128       //no separation by kind, just count any errors/warnings
0129       if (atLeastOneEntry_) {
0130         for (auto const& summary : *errorsAndWarnings) {
0131           edm::ELseverityLevel const& severity = summary.severity;
0132           if (severity.getLevel() != edm::ELseverityLevel::ELsev_error and
0133               severity.getLevel() != edm::ELseverityLevel::ELsev_warning) {
0134             continue;
0135           }
0136           if (!avoidCategories_.empty()) {
0137             //veto categories from user input.
0138             if (std::find(avoidCategories_.begin(), avoidCategories_.end(), summary.category) !=
0139                 avoidCategories_.end()) {
0140               continue;
0141             }
0142           }
0143           return true;
0144         }
0145         return false;
0146       } else {
0147         if (atLeastOneError_ || atLeastOneWarning_) {
0148           unsigned int nError = 0;
0149           unsigned int nWarning = 0;
0150           for (auto const& summary : *errorsAndWarnings) {
0151             //veto categories from user input.
0152             if (!avoidCategories_.empty()) {
0153               if (std::find(avoidCategories_.begin(), avoidCategories_.end(), summary.category) !=
0154                   avoidCategories_.end()) {
0155                 continue;
0156               }
0157             }
0158             edm::ELseverityLevel const& severity = summary.severity;
0159             //count errors
0160             if (severity.getLevel() == edm::ELseverityLevel::ELsev_error) {
0161               ++nError;
0162             }
0163             //count warnings
0164             if (severity.getLevel() == edm::ELseverityLevel::ELsev_warning) {
0165               ++nWarning;
0166             }
0167           }
0168           if (atLeastOneError_ && nError != 0) {
0169             return (true);
0170           }
0171           if (atLeastOneWarning_ && nWarning != 0) {
0172             return (true);
0173           }
0174         }
0175       }
0176     }
0177   }
0178   return (false);
0179 }
0180 
0181 void LogErrorFilter::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) {
0182   if (useThresholdsPerKind_) {
0183     for (auto& err : errorCounts_) {
0184       err.second = 0;
0185     }
0186     for (auto& err : warningCounts_) {
0187       err.second = 0;
0188     }
0189   }
0190 
0191   return;
0192 }
0193 
0194 void LogErrorFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0195   edm::ParameterSetDescription desc;
0196   desc.add<edm::InputTag>("harvesterTag");
0197   desc.add<bool>("atLeastOneError");
0198   desc.add<bool>("atLeastOneWarning");
0199   desc.add<bool>("useThresholdsPerKind");
0200   desc.add<unsigned int>("maxErrorKindsPerLumi", 999999);
0201   desc.add<unsigned int>("maxWarningKindsPerLumi", 999999);
0202   desc.add<std::vector<std::string>>("avoidCategories");
0203   descriptions.add("logErrorFilter", desc);
0204 }
0205 
0206 //define this as a plug-in
0207 DEFINE_FWK_MODULE(LogErrorFilter);