CMS 3D CMS Logo

HLTLogMonitorFilter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HLTLogMonitorFilter
4 // Class: HLTLogMonitorFilter
5 //
13 //
14 // Original Author: Andrea Bocci
15 // Created: Thu Nov 5 15:16:46 CET 2009
16 //
17 
18 
19 // system include files
20 #include <cstdint>
21 #include <map>
22 
23 // user include files
32 
33 //
34 // class declaration
35 //
36 
38 public:
39  explicit HLTLogMonitorFilter(const edm::ParameterSet &);
40  ~HLTLogMonitorFilter() override;
41 
42  struct CategoryEntry {
43  uint32_t threshold; // configurable threshold, after which messages in this Category start to be logarithmically prescaled
44  uint32_t prescale; // current prescale for this Category
45  uint64_t counter; // number of events that fired this Category
46  uint64_t accepted; // number of events acepted for this Category
47  bool done; // track if this Category has already been seen in the current event
48 
49  CategoryEntry(uint32_t t = 0) :
50  threshold(t), // default-constructed entries have the threshold set to 0, which means the associated Category is disabled
51  prescale(1),
52  counter(0),
53  accepted(0),
54  done(false)
55  { }
56 
57  bool accept() {
58  if (not done) {
59  done = true;
60  ++counter;
61  }
62 
63  // fail if the prescaler is disabled (threshold == 0), or if the counter is not a multiple of the prescale
64  if ((threshold == 0) or (counter % prescale))
65  return false;
66 
67  // quasi-logarithmic increase in the prescale factor (should be safe even if threshold is 1)
68  if (counter == prescale * threshold)
69  prescale *= threshold;
70 
71  ++accepted;
72  return true;
73  }
74 
75  };
76 
77  typedef std::map<std::string, CategoryEntry> CategoryMap;
78 
79  // ---------- private methods -----------------------
80 
82  bool filter(edm::Event&, const edm::EventSetup &) override;
83 
85  void beginJob() override;
86 
88  void endJob() override;
89 
91  bool knownCategory(const std::string & category);
92 
95 
98 
100  void summary();
101 
102 
103  // ---------- member data ---------------------------
104  uint32_t m_prescale; // default threshold, after which messages in each Category start to be logarithmically prescaled
105  CategoryMap m_data; // map each category name to its prescale data
106 };
107 
108 
109 // system include files
110 #include <sstream>
111 #include <iomanip>
112 #include <memory>
113 #include <boost/range.hpp>
114 #include <boost/algorithm/string.hpp>
115 
116 // user include files
119 
120 //
121 // constructors and destructor
122 //
124  m_prescale(),
125  m_data()
126 {
127  m_prescale = config.getParameter<uint32_t>("default_threshold");
128 
129  typedef std::vector<edm::ParameterSet> VPSet;
130  const VPSet & categories = config.getParameter<VPSet>("categories");
131  for (auto const & categorie : categories) {
132  const std::string & name = categorie.getParameter<std::string>("name");
133  uint32_t threshold = categorie.getParameter<uint32_t>("threshold");
134  addCategory(name, threshold);
135  }
136 
137  produces<std::vector<edm::ErrorSummaryEntry> >();
138 }
139 
141 
142 //
143 // member functions
144 //
145 
146 // ------------ method called on each new Event ------------
148  // no LogErrors or LogWarnings, skip processing and reject the event
149  if (not edm::FreshErrorsExist(event.streamID().value()))
150  return false;
151 
152  // clear "done" flag in all Categories
153  for(auto& entry : m_data)
154  entry.second.done = false;
155 
156  // look at the LogErrors and LogWarnings, and accept the event if at least one is under threshold or matches the prescale
157  bool accept = false;
159 
160  std::vector<edm::ErrorSummaryEntry> errorSummary{ edm::LoggedErrorsSummary(event.streamID().value()) };
161  for( auto const& entry : errorSummary ) {
162  // split the message category
163  typedef boost::split_iterator<std::string::const_iterator> splitter;
164  for (splitter i = boost::make_split_iterator(entry.category, boost::first_finder("|", boost::is_equal()));
165  i != splitter();
166  ++i)
167  {
168  // extract the substring corresponding to the split_iterator
169  // FIXME: this can be avoided if the m_data map is keyed on boost::sub_range<std::string>
170  category.assign(i->begin(), i->end());
171 
172  // access the message category, or create a new one as needed, and check the prescale
173  if (getCategory(category).accept())
174  accept = true;
175  }
176  }
177 
178  // harvest the errors, but only if the filter will accept the event
179  std::unique_ptr<std::vector<edm::ErrorSummaryEntry> > errors(new std::vector<edm::ErrorSummaryEntry>());
180  if (accept) {
181  errors->swap(errorSummary);
182  }
183  event.put(std::move(errors));
184 
185  return accept;
186 }
187 
188 // ------------ method called at the end of the Job ---------
191 }
192 // ------------ method called at the end of the Job ---------
195  summary();
196 }
197 
200  return (m_data.find( category ) != m_data.end());
201 }
202 
205  // check after inserting, as either the new CategoryEntry is needed, or an error condition is raised
206  std::pair<CategoryMap::iterator, bool> result = m_data.insert( std::make_pair(category, CategoryEntry(threshold)) );
207  if (not result.second)
208  throw cms::Exception("Configuration") << "Duplicate entry for category " << category;
209  return result.first->second;
210 }
211 
214  // check before inserting, to avoid the construction of a CategoryEntry object
215  auto i = m_data.find(category);
216  if (i != m_data.end())
217  return i->second;
218  else
219  return m_data.insert( std::make_pair(category, CategoryEntry(m_prescale)) ).first->second;
220 }
221 
224  std::stringstream out;
225  out << "Log-Report ---------- HLTLogMonitorFilter Summary ------------\n"
226  << "Log-Report Threshold Prescale Issued Accepted Rejected Category\n";
227  for(auto const& entry : m_data) {
228  out << "Log-Report "
229  << std::right
230  << std::setw(10) << entry.second.threshold << ' '
231  << std::setw(10) << entry.second.prescale << ' '
232  << std::setw(10) << entry.second.counter << ' '
233  << std::setw(10) << entry.second.accepted << ' '
234  << std::setw(10) << (entry.second.counter - entry.second.accepted) << ' '
235  << std::left << entry.first << '\n';
236  }
237  edm::LogVerbatim("Report") << out.str();
238 }
239 
240 // define as a framework plugin
T getParameter(std::string const &) const
def splitter(iterator, n)
Definition: confdb.py:13
CategoryEntry & getCategory(const std::string &category)
return the entry for requested category, if it exists, or create a new one with the default threshold...
CategoryEntry & addCategory(const std::string &category, uint32_t threshold)
create a new entry for the given category, with the given threshold value
bool EnableLoggedErrorsSummary()
bool knownCategory(const std::string &category)
check if the requested category has a valid entry
~HLTLogMonitorFilter() override
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
Definition: config.py:1
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:30
bool FreshErrorsExist(unsigned int iStreamID)
bool DisableLoggedErrorsSummary()
void summary()
summarize to LogInfo
void beginJob() override
EDFilter beginJob method.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
std::map< std::string, CategoryEntry > CategoryMap
void endJob() override
EDFilter endJob method.
unsigned int value() const
Definition: StreamID.h:42
unsigned long long uint64_t
Definition: Time.h:15
StreamID streamID() const
Definition: Event.h:95
std::vector< ErrorSummaryEntry > LoggedErrorsSummary(unsigned int iStreamID)
HLTLogMonitorFilter(const edm::ParameterSet &)
bool filter(edm::Event &, const edm::EventSetup &) override
EDFilter accept method.
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1